xref: /aosp_15_r20/external/ComputeLibrary/python/scripts/utils/tflite_helpers.py (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust# Copyright (c) 2021 Arm Limited.
2*c217d954SCole Faust#
3*c217d954SCole Faust# SPDX-License-Identifier: MIT
4*c217d954SCole Faust#
5*c217d954SCole Faust# Permission is hereby granted, free of charge, to any person obtaining a copy
6*c217d954SCole Faust# of this software and associated documentation files (the "Software"), to
7*c217d954SCole Faust# deal in the Software without restriction, including without limitation the
8*c217d954SCole Faust# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9*c217d954SCole Faust# sell copies of the Software, and to permit persons to whom the Software is
10*c217d954SCole Faust# furnished to do so, subject to the following conditions:
11*c217d954SCole Faust#
12*c217d954SCole Faust# The above copyright notice and this permission notice shall be included in all
13*c217d954SCole Faust# copies or substantial portions of the Software.
14*c217d954SCole Faust#
15*c217d954SCole Faust# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*c217d954SCole Faust# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*c217d954SCole Faust# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18*c217d954SCole Faust# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*c217d954SCole Faust# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20*c217d954SCole Faust# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21*c217d954SCole Faust# SOFTWARE.
22*c217d954SCole Faust
23*c217d954SCole Faust_TFLITE_TYPECODE2ACLNAME = {
24*c217d954SCole Faust    0: "fp32", # Float32
25*c217d954SCole Faust    1: "fp16", # Float16
26*c217d954SCole Faust    2: "integer", # Int32
27*c217d954SCole Faust    3: "qasymm8", # Uint8
28*c217d954SCole Faust    # 4: "Unsupported", # Int64
29*c217d954SCole Faust    # 5: "Unsupported", # String
30*c217d954SCole Faust    6: "integer", # Bool
31*c217d954SCole Faust    7: "qsymm16", # Int16
32*c217d954SCole Faust    # 8: "Unsupported", # Complex64
33*c217d954SCole Faust    9: "qasymm8_signed", # Int8
34*c217d954SCole Faust}
35*c217d954SCole Faust
36*c217d954SCole Faust_TFLITE_TYPECODE2NAME = {
37*c217d954SCole Faust    0: "Float32",
38*c217d954SCole Faust    1: "Float16",
39*c217d954SCole Faust    2: "Int32",
40*c217d954SCole Faust    3: "Uint8",
41*c217d954SCole Faust    4: "Int64",
42*c217d954SCole Faust    5: "String",
43*c217d954SCole Faust    6: "Bool",
44*c217d954SCole Faust    7: "Int16",
45*c217d954SCole Faust    8: "Complex64",
46*c217d954SCole Faust    9: "Int8",
47*c217d954SCole Faust}
48*c217d954SCole Faust
49*c217d954SCole Faust_TFLITE_TO_ACL = {
50*c217d954SCole Faust    "ADD": "Add",  # 0
51*c217d954SCole Faust    "AVERAGE_POOL_2D": "Pool2d",  # 1
52*c217d954SCole Faust    "CONCATENATION": "Concatenate",  # 2
53*c217d954SCole Faust    "CONV_2D": "Conv2d",  # 3
54*c217d954SCole Faust    "DEPTHWISE_CONV_2D": "DepthwiseConv2d",  # 4
55*c217d954SCole Faust    "DEPTH_TO_SPACE": "DepthToSpace",  # 5
56*c217d954SCole Faust    "DEQUANTIZE": "Dequantize",  # 6
57*c217d954SCole Faust    # "EMBEDDING_LOOKUP" : "Unsupported",                  #7
58*c217d954SCole Faust    "FLOOR": "Floor",  # 8
59*c217d954SCole Faust    "FULLY_CONNECTED": "FullyConnected",  # 9
60*c217d954SCole Faust    # "HASHTABLE_LOOKUP" : "Unsupported",                  #10
61*c217d954SCole Faust    "L2_NORMALIZATION": "L2Normalize",  # 11
62*c217d954SCole Faust    "L2_POOL_2D": "Pool2d",  # 12
63*c217d954SCole Faust    "LOCAL_RESPONSE_NORMALIZATION": "Normalize",  # 13
64*c217d954SCole Faust    "LOGISTIC": "Activation",  # 14
65*c217d954SCole Faust    # "LSH_PROJECTION" : "Unsupported",                    #15
66*c217d954SCole Faust    "LSTM": "LSTM",  # 16
67*c217d954SCole Faust    "MAX_POOL_2D": "Pool2d",  # 17
68*c217d954SCole Faust    "MUL": "Mul",  # 18
69*c217d954SCole Faust    "RELU": "Activation",  # 19
70*c217d954SCole Faust    "RELU_N1_TO_1": "Activation",  # 20
71*c217d954SCole Faust    "RELU6": "Activation",  # 21
72*c217d954SCole Faust    "RESHAPE": "Reshape",  # 22
73*c217d954SCole Faust    "RESIZE_BILINEAR": "Scale",  # 23
74*c217d954SCole Faust    "RNN": "RNN",  # 24
75*c217d954SCole Faust    "SOFTMAX": "Softmax",  # 25
76*c217d954SCole Faust    "SPACE_TO_DEPTH": "SpaceToDepth",  # 26
77*c217d954SCole Faust    # "SVDF" : "Unsupported",                              #27
78*c217d954SCole Faust    "TANH": "Activation",  # 28
79*c217d954SCole Faust    # "CONCAT_EMBEDDINGS" : "Unsupported",                 #29
80*c217d954SCole Faust    # "SKIP_GRAM" : "Unsupported",                         #30
81*c217d954SCole Faust    # "CALL" : "Unsupported",                              #31
82*c217d954SCole Faust    # "CUSTOM" : "Unsupported",                            #32
83*c217d954SCole Faust    # "EMBEDDING_LOOKUP_SPARSE" : "Unsupported",           #33
84*c217d954SCole Faust    "PAD": "Pad",  # 34
85*c217d954SCole Faust    # "UNIDIRECTIONAL_SEQUENCE_RNN" : "Unsupported",       #35
86*c217d954SCole Faust    "GATHER": "Gather",  # 36
87*c217d954SCole Faust    "BATCH_TO_SPACE_ND": "BatchToSpace",  # 37
88*c217d954SCole Faust    "SPACE_TO_BATCH_ND": "SpaceToBatch",  # 38
89*c217d954SCole Faust    "TRANSPOSE": "Permute",  # 39
90*c217d954SCole Faust    "MEAN": "Reduction",  # 40
91*c217d954SCole Faust    "SUB": "Sub",  # 41
92*c217d954SCole Faust    "DIV": "Div",  # 42
93*c217d954SCole Faust    "SQUEEZE": "Reshape",  # 43
94*c217d954SCole Faust    # "UNIDIRECTIONAL_SEQUENCE_LSTM" : "Unsupported",      #44
95*c217d954SCole Faust    "STRIDED_SLICE": "StridedSlice",  # 45
96*c217d954SCole Faust    # "BIDIRECTIONAL_SEQUENCE_RNN" : "Unsupported",        #46
97*c217d954SCole Faust    "EXP": "ElementwiseUnary",  # 47
98*c217d954SCole Faust    # "TOPK_V2" : "Unsupported",                           #48
99*c217d954SCole Faust    "SPLIT": "Split",  # 49
100*c217d954SCole Faust    "LOG_SOFTMAX": "Softmax",  # 50
101*c217d954SCole Faust    # "DELEGATE" : "Unuspported",                          #51
102*c217d954SCole Faust    # "BIDIRECTIONAL_SEQUENCE_LSTM" : "Unsupported",       #52
103*c217d954SCole Faust    "CAST": "Cast",  # 53
104*c217d954SCole Faust    "PRELU": "PRelu",  # 54
105*c217d954SCole Faust    "MAXIMUM": "ElementwiseBinary",  # 55
106*c217d954SCole Faust    "ARG_MAX": "Reduction",  # 56
107*c217d954SCole Faust    "MINIMUM": "ElementwiseBinary",  # 57
108*c217d954SCole Faust    "LESS": "ElementwiseBinary",  # 58
109*c217d954SCole Faust    "NEG": "ElementwiseUnary",  # 59
110*c217d954SCole Faust    "PADV2": "Pad",  # 60
111*c217d954SCole Faust    "GREATER": "ElementwiseBinary",  # 61
112*c217d954SCole Faust    "GREATER_EQUAL": "ElementwiseBinary",  # 62
113*c217d954SCole Faust    "LESS_EQUAL": "ElementwiseBinary",  # 63
114*c217d954SCole Faust    "SELECT": "Select",  # 64
115*c217d954SCole Faust    "SLICE": "Slice",  # 65
116*c217d954SCole Faust    "SIN": "ElementwiseUnary",  # 66
117*c217d954SCole Faust    "TRANSPOSE_CONV": "TransposeConv2d",  # 67
118*c217d954SCole Faust    # "SPARSE_TO_DENSE" : "Unsupported",                   #68
119*c217d954SCole Faust    "TILE": "Tile",  # 69
120*c217d954SCole Faust    "EXPAND_DIMS": "Reshape",  # 70
121*c217d954SCole Faust    "EQUAL": "ElementwiseBinary",  # 71
122*c217d954SCole Faust    "NOT_EQUAL": "ElementwiseBinary",  # 72
123*c217d954SCole Faust    "LOG": "ElementwiseUnary",  # 73
124*c217d954SCole Faust    "SUM": "Reduction",  # 74
125*c217d954SCole Faust    "SQRT": "Activation",  # 75
126*c217d954SCole Faust    "RSQRT": "ElementwiseUnary",  # 76
127*c217d954SCole Faust    "SHAPE": "",  # 77
128*c217d954SCole Faust    "POW": "ElementwiseBinary",  # 78
129*c217d954SCole Faust    "ARG_MIN": "Reduction",  # 79
130*c217d954SCole Faust    # "FAKE_QUANT" : "Unsupported",                        #80
131*c217d954SCole Faust    "REDUCE_PROD": "Reduction",  # 81
132*c217d954SCole Faust    "REDUCE_MAX": "Reduction",  # 82
133*c217d954SCole Faust    "PACK": "Stack",  # 83
134*c217d954SCole Faust    "LOGICAL_OR": "ElementwiseBinary",  # 84
135*c217d954SCole Faust    "ONE_HOT": "Unsupported",  # 85
136*c217d954SCole Faust    "LOGICAL_AND": "ElementwiseBinary",  # 86
137*c217d954SCole Faust    "LOGICAL_NOT": "ElementwiseUnary",  # 87
138*c217d954SCole Faust    "UNPACK": "Unstack",  # 88
139*c217d954SCole Faust    "REDUCE_MIN": "Reduction",  # 89
140*c217d954SCole Faust    # "FLOOR_DIV" :  "Unsupported",                        #90
141*c217d954SCole Faust    # "REDUCE_ANY" :  "Unsupported",                       #91
142*c217d954SCole Faust    "SQUARE": "Activation",  # 92
143*c217d954SCole Faust    "ZEROS_LIKE": "",  # 93
144*c217d954SCole Faust    "FILL": "Fill",  # 94
145*c217d954SCole Faust    # "FLOOR_MOD" :  "Unsupported",                        #95
146*c217d954SCole Faust    "RANGE": "",  # 96
147*c217d954SCole Faust    "RESIZE_NEAREST_NEIGHBOR": "Scale",  # 97
148*c217d954SCole Faust    "LEAKY_RELU": "Activation",  # 98
149*c217d954SCole Faust    "SQUARED_DIFFERENCE": "ElementwiseBinary",  # 99
150*c217d954SCole Faust    "MIRROR_PAD": "Pad",  # 100
151*c217d954SCole Faust    "ABS": "ElementwiseUnary",  # 101
152*c217d954SCole Faust    "SPLIT_V": "Split",  # 102
153*c217d954SCole Faust    # "UNIQUE" :  "Unsupported",                           #103
154*c217d954SCole Faust    # "CEIL" :  "Unsupported",                             #104
155*c217d954SCole Faust    "REVERSE_V2": "Reverse",  # 105
156*c217d954SCole Faust    "ADD_N": "Add",  # 106
157*c217d954SCole Faust    "GATHER_ND": "Gather",  # 107
158*c217d954SCole Faust    # "COS" :  "Unsupported",                              #108
159*c217d954SCole Faust    # "WHERE" :  "Unsupported",                            #109
160*c217d954SCole Faust    "RANK": "",  # 110
161*c217d954SCole Faust    "ELU": "Activation",  # 111
162*c217d954SCole Faust    # "REVERSE_SEQUENCE" : "Unsupported",                  #112
163*c217d954SCole Faust    # "MATRIX_DIAG" : "Unsupported",                       #113
164*c217d954SCole Faust    "QUANTIZE": "Quantize",  # 114
165*c217d954SCole Faust    # "MATRIX_SET_DIAG" :  "Unsupported",                  #115
166*c217d954SCole Faust    "ROUND": "ElementwiseUnary",  # 116
167*c217d954SCole Faust    "HARD_SWISH": "Activation",  # 117
168*c217d954SCole Faust    # "IF" :  "Unsupported",                               #118
169*c217d954SCole Faust    # "WHILE" :  "Unsupported",                            #119
170*c217d954SCole Faust    # "NON_MAX_SUPPRESSION_V4" :  "Unsupported",           #120
171*c217d954SCole Faust    # "NON_MAX_SUPPRESSION_V5" :  "Unsupported",           #121
172*c217d954SCole Faust    # "SCATTER_ND" :  "Unsupported",                       #122
173*c217d954SCole Faust    "SELECT_V2": "Select",  # 123
174*c217d954SCole Faust    "DENSIFY": "Cast",  # 124
175*c217d954SCole Faust    # "SEGMENT_SUM" : "Unsupported",                       #125
176*c217d954SCole Faust    "BATCH_MATMUL": "GEMM",  # 126
177*c217d954SCole Faust    # "PLACEHOLDER_FOR_GREATER_OP_CODES" :  "Unsupported", #127
178*c217d954SCole Faust    # "CUMSUM" :  "Unsupported",                           #128
179*c217d954SCole Faust    # "CALL_ONCE" : "Unsupported",                         #129
180*c217d954SCole Faust    # "BROADCAST_TO" : "Unsupported",                      #130
181*c217d954SCole Faust    # "RFFT2D" :  "Unsupported",                           #131
182*c217d954SCole Faust    # "CONV_3D" :  "Unsupported",                          #132
183*c217d954SCole Faust    # "IMAG" : "Unsupported",                              #133
184*c217d954SCole Faust    # "REAL" : "Unsupported",                              #134
185*c217d954SCole Faust    # "COMPLEX_ABS" : "Unsupported",                       #135
186*c217d954SCole Faust    # "HASHTABLE" :  "Unsupported",                        #136
187*c217d954SCole Faust    # "HASHTABLE_FIND" :  "Unsupported",                   #137
188*c217d954SCole Faust    # "HASHTABLE_IMPORT" :  "Unsupported",                 #138
189*c217d954SCole Faust    # "HASHTABLE_SIZE" :  "Unsupported",                   #139
190*c217d954SCole Faust    # "REDUCE_ALL" :  "Unsupported",                       #140
191*c217d954SCole Faust    # "CONV_3D_TRANSPOSE" : "Unsupported",                 #141
192*c217d954SCole Faust    # "VAR_HANDLE" :  "Unsupported",                       #142
193*c217d954SCole Faust    # "READ_VARIABLE" :  "Unsupported",                    #143
194*c217d954SCole Faust    # "ASSIGN_VARIABLE" :  "Unsupported",                  #144
195*c217d954SCole Faust}
196*c217d954SCole Faust
197*c217d954SCole Faust
198*c217d954SCole Faustdef tflite_typecode2aclname(toc):
199*c217d954SCole Faust    """Stringify TFLite data-type opcodes to ACL versions
200*c217d954SCole Faust
201*c217d954SCole Faust    Parameters:
202*c217d954SCole Faust    ----------
203*c217d954SCole Faust    toc: int
204*c217d954SCole Faust        TFLite type opcode
205*c217d954SCole Faust
206*c217d954SCole Faust    Returns
207*c217d954SCole Faust    ----------
208*c217d954SCole Faust    str
209*c217d954SCole Faust        Stringified opcode
210*c217d954SCole Faust
211*c217d954SCole Faust    Raises
212*c217d954SCole Faust    ------
213*c217d954SCole Faust    ValueError
214*c217d954SCole Faust        If opcode does not exist in the map
215*c217d954SCole Faust    """
216*c217d954SCole Faust    if toc in _TFLITE_TYPECODE2ACLNAME:
217*c217d954SCole Faust        return _TFLITE_TYPECODE2ACLNAME[toc]
218*c217d954SCole Faust    else:
219*c217d954SCole Faust        raise ValueError("Unknown ACL typecode %d" % toc)
220*c217d954SCole Faust
221*c217d954SCole Faustdef tflite_typecode2name(toc):
222*c217d954SCole Faust    """Stringify TFLite data-type opcodes
223*c217d954SCole Faust
224*c217d954SCole Faust    Parameters:
225*c217d954SCole Faust    ----------
226*c217d954SCole Faust    toc: int
227*c217d954SCole Faust        TFLite type opcode
228*c217d954SCole Faust
229*c217d954SCole Faust    Returns
230*c217d954SCole Faust    ----------
231*c217d954SCole Faust    str
232*c217d954SCole Faust        Stringified opcode
233*c217d954SCole Faust
234*c217d954SCole Faust    Raises
235*c217d954SCole Faust    ------
236*c217d954SCole Faust    ValueError
237*c217d954SCole Faust        If opcode does not exist in the map
238*c217d954SCole Faust    """
239*c217d954SCole Faust    if toc in _TFLITE_TYPECODE2NAME:
240*c217d954SCole Faust        return _TFLITE_TYPECODE2NAME[toc]
241*c217d954SCole Faust    else:
242*c217d954SCole Faust        raise ValueError("Unknown typecode %d" % toc)
243*c217d954SCole Faust
244*c217d954SCole Faust
245*c217d954SCole Faustdef tflite_op2acl(top):
246*c217d954SCole Faust    """Map TFLite operators to ComputeLibrary ones
247*c217d954SCole Faust
248*c217d954SCole Faust    Parameters:
249*c217d954SCole Faust    ----------
250*c217d954SCole Faust    top: str
251*c217d954SCole Faust        TFLite operator name
252*c217d954SCole Faust
253*c217d954SCole Faust    Returns
254*c217d954SCole Faust    ----------
255*c217d954SCole Faust    str
256*c217d954SCole Faust        Relevant ComputeLibrary operator name
257*c217d954SCole Faust
258*c217d954SCole Faust    Raises
259*c217d954SCole Faust    ------
260*c217d954SCole Faust    ValueError
261*c217d954SCole Faust        If operator cannot be mapped
262*c217d954SCole Faust    """
263*c217d954SCole Faust    if top in _TFLITE_TO_ACL:
264*c217d954SCole Faust        return _TFLITE_TO_ACL[top]
265*c217d954SCole Faust    else:
266*c217d954SCole Faust        raise ValueError("Operator {} does not exist in ComputeLibrary" % top)
267