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