summaryrefslogtreecommitdiff
path: root/tools/tflitefile_tool/config_saver.py
blob: 8abd3cf8bdaa0bb3a0a95645eaf0e5ec13b33cdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/python

# Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from operator_wrapping import Operator
from tensor_printer import TensorPrinter
from option_printer import OptionPrinter
from perf_predictor import PerfPredictor


class ConfigSaver(object):
    def __init__(self, file_name, operator):
        self.file_name = file_name
        self.operator = operator
        # Set self.verbose to 2 level to print more information
        self.verbose = 2
        self.op_idx = operator.operator_idx
        self.op_name = operator.opcode_str

        self.f = open(file_name, 'at')

    def __del__(self):
        self.f.close()

    def SaveInfo(self):
        self.f.write("[{}]\n".format(self.op_idx))
        if (self.op_name == 'CONV_2D'):
            self.SaveConv2DInputs()
        else:
            self.SaveInputs()

        self.SaveOutputs()

        self.SaveAttributes()

        self.f.write('\n')

    def SaveConv2DInputs(self):
        if (len(self.operator.inputs) != 3):
            raise AssertionError('Conv2D input count should be 3')

        inputs = self.operator.inputs[0]
        weights = self.operator.inputs[1]
        bias = self.operator.inputs[2]

        self.f.write("input: {}\n".format(
            TensorPrinter(self.verbose, inputs).GetShapeString()))
        self.f.write("input_type: {}\n".format(inputs.type_name))
        self.f.write("weights: {}\n".format(
            TensorPrinter(self.verbose, weights).GetShapeString()))
        self.f.write("weights_type: {}\n".format(weights.type_name))
        self.f.write("bias: {}\n".format(
            TensorPrinter(self.verbose, bias).GetShapeString()))
        self.f.write("bias_type: {}\n".format(bias.type_name))

    def SaveInputs(self):
        total = len(self.operator.inputs)
        self.f.write("input_counts: {}\n".format(total))
        for idx in range(total):
            tensor = self.operator.inputs[idx]
            input_shape_str = TensorPrinter(self.verbose, tensor).GetShapeString()
            self.f.write("input{}: {}\n".format(idx, input_shape_str))
            self.f.write("input{}_type: {}\n".format(idx, tensor.type_name))

    def SaveOutputs(self):
        total = len(self.operator.outputs)
        self.f.write("output_counts: {}\n".format(total))
        for idx in range(total):
            tensor = self.operator.outputs[idx]
            output_shape_str = TensorPrinter(self.verbose, tensor).GetShapeString()
            self.f.write("output{}: {}\n".format(idx, output_shape_str))
            self.f.write("output{}_type: {}\n".format(idx, tensor.type_name))

    def SaveFilter(self):
        self.f.write("filter_w: {}\n".format(self.operator.options.FilterWidth()))
        self.f.write("filter_h: {}\n".format(self.operator.options.FilterHeight()))

    def SaveStride(self):
        self.f.write("stride_w: {}\n".format(self.operator.options.StrideW()))
        self.f.write("stride_h: {}\n".format(self.operator.options.StrideH()))

    def SaveDilation(self):
        self.f.write("dilation_w: {}\n".format(self.operator.options.DilationWFactor()))
        self.f.write("dilation_h: {}\n".format(self.operator.options.DilationHFactor()))

    def SavePadding(self):
        if self.operator.options.Padding() == 0:
            self.f.write("padding: SAME\n")
        elif self.operator.options.Padding() == 1:
            self.f.write("padding: VALID\n")

    def SaveFusedAct(self):
        if self.operator.fused_activation is not "NONE":
            self.f.write("fused_act: {}\n".format(self.operator.fused_activation))

    def SaveAttributes(self):
        # operator option
        # Some operations does not have option. In such case no option is printed
        option_str = OptionPrinter(self.verbose, self.op_name,
                                   self.operator.options).GetOptionString()
        if self.op_name == 'AVERAGE_POOL_2D' or self.op_name == 'MAX_POOL_2D':
            self.SaveFilter()
            self.SaveStride()
            self.SavePadding()
        elif self.op_name == 'CONV_2D':
            self.SaveStride()
            self.SaveDilation()
            self.SavePadding()
        elif self.op_name == 'TRANSPOSE_CONV':
            self.SaveStride()
            self.SavePadding()
        elif self.op_name == 'DEPTHWISE_CONV_2D':
            self.SaveStride()
            self.SaveDilation()
            self.SavePadding()
            self.f.write("depthmultiplier: {}\n".format(
                self.operator.options.DepthMultiplier()))

        self.SaveFusedAct()