summaryrefslogtreecommitdiff
path: root/tools/tflitefile_tool/operator_printer.py
blob: 9b6f97d24801a8543526ec51808c769d1873689e (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
#!/usr/bin/python

# Copyright (c) 2018 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


def GetStrTensorIndex(tensors):
    return_string = "["
    for idx in range(len(tensors)):
        if idx != 0:
            return_string += ", "
        return_string += str(tensors[idx].tensor_idx)
    return_string += "]"
    return return_string


class OperatorPrinter(object):
    def __init__(self, verbose, operator):
        self.verbose = verbose
        self.operator = operator

    def PrintInfo(self, perf_predictor=None):
        if (self.verbose < 1):
            return

        op_str = "Operator {0}: {1}".format(self.operator.operator_idx,
                                            self.operator.opcode_str)

        if self.verbose == 2:
            # total instruction num
            instrs = "{:,}".format(self.operator.operation.TotalInstrNum()
                                   ) if self.operator.operation.can_compute else "???"

            # total operation cycles
            cycles = "{:,}".format(
                (perf_predictor.PredictCycles(self.operator.operation))
            ) if self.operator.operation.can_compute and perf_predictor != None else "???"

            op_str = op_str + "(instrs: {0}, cycls: {1})".format(instrs, cycles)

        print(op_str)
        print("\tFused Activation: " + self.operator.fused_activation)
        self.PrintTensors()

    def PrintTensors(self):
        print("\tInput Tensors" + GetStrTensorIndex(self.operator.inputs))
        for tensor in self.operator.inputs:
            TensorPrinter(self.verbose, tensor).PrintInfo("\t\t")
        print("\tOutput Tensors" + GetStrTensorIndex(self.operator.outputs))
        for tensor in self.operator.outputs:
            TensorPrinter(self.verbose, tensor).PrintInfo("\t\t")

        # operator option
        # Some operations does not have option. In such case no option is printed
        OptionPrinter(self.verbose, self.operator.opcode_str,
                      self.operator.options).PrintInfo("\t")