blob: 8f8d422743ff5dc014ae830a196b3573ab90550d [file] [log] [blame]
Georgios Pinitasa7171102021-08-17 12:54:59 +01001# 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_TYPECODE2NAME = {
24 0: "Float32",
25 1: "Float16",
26 2: "Int32",
27 3: "Uint8",
28 4: "Int64",
29 5: "String",
30 6: "Bool",
31 7: "Int16",
32 8: "Complex64",
33 9: "Int8",
34}
35
36_TFLITE_TO_ACL = {
37 "ADD": "Add", # 0
38 "AVERAGE_POOL_2D": "Pool2d", # 1
39 "CONCATENATION": "Concatenate", # 2
40 "CONV_2D": "Conv2d", # 3
41 "DEPTHWISE_CONV_2D": "DepthwiseConv2d", # 4
42 "DEPTH_TO_SPACE": "DepthToSpace", # 5
43 "DEQUANTIZE": "Dequantize", # 6
44 # "EMBEDDING_LOOKUP" : "Unsupported", #7
45 "FLOOR": "Floor", # 8
46 "FULLY_CONNECTED": "FullyConnected", # 9
47 # "HASHTABLE_LOOKUP" : "Unsupported", #10
48 "L2_NORMALIZATION": "L2Normalize", # 11
49 "L2_POOL_2D": "Pool2d", # 12
50 "LOCAL_RESPONSE_NORMALIZATION": "Normalize", # 13
51 "LOGISTIC": "Activation", # 14
52 # "LSH_PROJECTION" : "Unsupported", #15
53 "LSTM": "LSTM", # 16
54 "MAX_POOL_2D": "Pool2d", # 17
55 "MUL": "Mul", # 18
56 "RELU": "Activation", # 19
57 "RELU_N1_TO_1": "Activation", # 20
58 "RELU6": "Activation", # 21
59 "RESHAPE": "Reshape", # 22
60 "RESIZE_BILINEAR": "Scale", # 23
61 "RNN": "RNN", # 24
62 "SOFTMAX": "Softmax", # 25
63 "SPACE_TO_DEPTH": "SpaceToDepth", # 26
64 # "SVDF" : "Unsupported", #27
65 "TANH": "Activation", # 28
66 # "CONCAT_EMBEDDINGS" : "Unsupported", #29
67 # "SKIP_GRAM" : "Unsupported", #30
68 # "CALL" : "Unsupported", #31
69 # "CUSTOM" : "Unsupported", #32
70 # "EMBEDDING_LOOKUP_SPARSE" : "Unsupported", #33
71 "PAD": "Pad", # 34
72 # "UNIDIRECTIONAL_SEQUENCE_RNN" : "Unsupported", #35
73 "GATHER": "Gather", # 36
74 "BATCH_TO_SPACE_ND": "BatchToSpace", # 37
75 "SPACE_TO_BATCH_ND": "SpaceToBatch", # 38
76 "TRANSPOSE": "Permute", # 39
77 "MEAN": "Reduction", # 40
78 "SUB": "Sub", # 41
79 "DIV": "Div", # 42
80 "SQUEEZE": "Reshape", # 43
81 # "UNIDIRECTIONAL_SEQUENCE_LSTM" : "Unsupported", #44
82 "STRIDED_SLICE": "StridedSlice", # 45
83 # "BIDIRECTIONAL_SEQUENCE_RNN" : "Unsupported", #46
84 "EXP": "ElementwiseUnary", # 47
85 # "TOPK_V2" : "Unsupported", #48
86 "SPLIT": "Split", # 49
87 "LOG_SOFTMAX": "Softmax", # 50
88 # "DELEGATE" : "Unuspported", #51
89 # "BIDIRECTIONAL_SEQUENCE_LSTM" : "Unsupported", #52
90 "CAST": "Cast", # 53
91 "PRELU": "PRelu", # 54
92 "MAXIMUM": "ElementwiseBinary", # 55
93 "ARG_MAX": "Reduction", # 56
94 "MINIMUM": "ElementwiseBinary", # 57
95 "LESS": "ElementwiseBinary", # 58
96 "NEG": "ElementwiseUnary", # 59
97 "PADV2": "Pad", # 60
98 "GREATER": "ElementwiseBinary", # 61
99 "GREATER_EQUAL": "ElementwiseBinary", # 62
100 "LESS_EQUAL": "ElementwiseBinary", # 63
101 "SELECT": "Select", # 64
102 "SLICE": "Slice", # 65
103 "SIN": "ElementwiseUnary", # 66
104 "TRANSPOSE_CONV": "TransposeConv2d", # 67
105 # "SPARSE_TO_DENSE" : "Unsupported", #68
106 "TILE": "Tile", # 69
107 "EXPAND_DIMS": "Reshape", # 70
108 "EQUAL": "ElementwiseBinary", # 71
109 "NOT_EQUAL": "ElementwiseBinary", # 72
110 "LOG": "ElementwiseUnary", # 73
111 "SUM": "Reduction", # 74
112 "SQRT": "Activation", # 75
113 "RSQRT": "ElementwiseUnary", # 76
114 "SHAPE": "", # 77
115 "POW": "ElementwiseBinary", # 78
116 "ARG_MIN": "Reduction", # 79
117 # "FAKE_QUANT" : "Unsupported", #80
118 "REDUCE_PROD": "Reduction", # 81
119 "REDUCE_MAX": "Reduction", # 82
120 "PACK": "Stack", # 83
121 "LOGICAL_OR": "ElementwiseBinary", # 84
122 "ONE_HOT": "Unsupported", # 85
123 "LOGICAL_AND": "ElementwiseBinary", # 86
124 "LOGICAL_NOT": "ElementwiseUnary", # 87
125 "UNPACK": "Unstack", # 88
126 "REDUCE_MIN": "Reduction", # 89
127 # "FLOOR_DIV" : "Unsupported", #90
128 # "REDUCE_ANY" : "Unsupported", #91
129 "SQUARE": "Activation", # 92
130 "ZEROS_LIKE": "", # 93
131 "FILL": "Fill", # 94
132 # "FLOOR_MOD" : "Unsupported", #95
133 "RANGE": "", # 96
134 "RESIZE_NEAREST_NEIGHBOR": "Scale", # 97
135 "LEAKY_RELU": "Activation", # 98
136 "SQUARED_DIFFERENCE": "ElementwiseBinary", # 99
137 "MIRROR_PAD": "Pad", # 100
138 "ABS": "ElementwiseUnary", # 101
139 "SPLIT_V": "Split", # 102
140 # "UNIQUE" : "Unsupported", #103
141 # "CEIL" : "Unsupported", #104
142 "REVERSE_V2": "Reverse", # 105
143 "ADD_N": "Add", # 106
144 "GATHER_ND": "Gather", # 107
145 # "COS" : "Unsupported", #108
146 # "WHERE" : "Unsupported", #109
147 "RANK": "", # 110
148 "ELU": "Activation", # 111
149 # "REVERSE_SEQUENCE" : "Unsupported", #112
150 # "MATRIX_DIAG" : "Unsupported", #113
151 "QUANTIZE": "Quantize", # 114
152 # "MATRIX_SET_DIAG" : "Unsupported", #115
153 "ROUND": "ElementwiseUnary", # 116
154 "HARD_SWISH": "Activation", # 117
155 # "IF" : "Unsupported", #118
156 # "WHILE" : "Unsupported", #119
157 # "NON_MAX_SUPPRESSION_V4" : "Unsupported", #120
158 # "NON_MAX_SUPPRESSION_V5" : "Unsupported", #121
159 # "SCATTER_ND" : "Unsupported", #122
160 "SELECT_V2": "Select", # 123
161 "DENSIFY": "Cast", # 124
162 # "SEGMENT_SUM" : "Unsupported", #125
163 "BATCH_MATMUL": "GEMM", # 126
164 # "PLACEHOLDER_FOR_GREATER_OP_CODES" : "Unsupported", #127
165 # "CUMSUM" : "Unsupported", #128
166 # "CALL_ONCE" : "Unsupported", #129
167 # "BROADCAST_TO" : "Unsupported", #130
168 # "RFFT2D" : "Unsupported", #131
169 # "CONV_3D" : "Unsupported", #132
170 # "IMAG" : "Unsupported", #133
171 # "REAL" : "Unsupported", #134
172 # "COMPLEX_ABS" : "Unsupported", #135
173 # "HASHTABLE" : "Unsupported", #136
174 # "HASHTABLE_FIND" : "Unsupported", #137
175 # "HASHTABLE_IMPORT" : "Unsupported", #138
176 # "HASHTABLE_SIZE" : "Unsupported", #139
177 # "REDUCE_ALL" : "Unsupported", #140
178 # "CONV_3D_TRANSPOSE" : "Unsupported", #141
179 # "VAR_HANDLE" : "Unsupported", #142
180 # "READ_VARIABLE" : "Unsupported", #143
181 # "ASSIGN_VARIABLE" : "Unsupported", #144
182}
183
184
185def tflite_typecode2name(toc):
186 """Stringify TfLite data-type opcodes
187
188 Parameters:
189 ----------
190 toc: int
191 TfLite type opcode
192
193 Returns
194 ----------
195 str
196 Stringified opcode
197
198 Raises
199 ------
200 ValueError
201 If opcode does not exist in the map
202 """
203 if toc in _TFLITE_TYPECODE2NAME:
204 return _TFLITE_TYPECODE2NAME[toc]
205 else:
206 raise ValueError("Unknown typecode %d" % toc)
207
208
209def tflite_op2acl(top):
210 """Map TfLite operators to ComputeLibrary ones
211
212 Parameters:
213 ----------
214 top: str
215 TfLite operator name
216
217 Returns
218 ----------
219 str
220 Relevant ComputeLibrary operator name
221
222 Raises
223 ------
224 ValueError
225 If operator cannot be mapped
226 """
227 if top in _TFLITE_TO_ACL:
228 return _TFLITE_TO_ACL[top]
229 else:
230 raise ValueError("Operator {} does not exist in ComputeLibrary" % top)