-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstm_ai_utils.py
More file actions
468 lines (414 loc) · 12.2 KB
/
stm_ai_utils.py
File metadata and controls
468 lines (414 loc) · 12.2 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
###################################################################################
# Copyright (c) 2021 STMicroelectronics.
# All rights reserved.
# This software is licensed under terms that can be found in the LICENSE file in
# the root directory of this software component.
# If no LICENSE file comes with this software, it is provided AS-IS.
###################################################################################
"""
Common helper services to manage the STM.AI type
"""
import enum
import numpy as np
RT_STM_AI_NAME = 'STM.AI'
def stm_ai_error_to_str(err_code, err_type):
"""
Return a human readable description of the ai run-time error
see <AICore>::EmbedNets git (src/api/ai_platform.h file)
Parameters
----------
err_code
stm.ai error code
err_type
stm.ai error err_type
Returns
-------
str
human readable description of the error
"""
type_switcher = {
0x00: 'None',
0x01: 'Tools Platform Api Mismatch',
0x02: 'Types Mismatch',
0x10: 'Invalid handle',
0x11: 'Invalid State',
0x12: 'Invalid Input',
0x13: 'Invalid Output',
0x14: 'Invalid Param',
0x30: 'Init Failed',
0x31: 'Allocation Failed',
0x32: 'Deallocation Failed',
0x33: 'Create Failed'
}
code_switcher = {
0x0000: 'None',
0x0010: 'Network',
0x0011: 'Network Params',
0x0012: 'Network Weights',
0x0013: 'Network Activations',
0x0014: 'Layer',
0x0015: 'Tensor',
0x0016: 'Array',
0x0017: 'Invalid Ptr',
0x0018: 'Invalid Size',
0x0020: 'Invalid Format',
0x0021: 'Invalid Batch',
0x0030: 'Missed Init',
0x0040: 'In Use',
}
desc_ = 'type="{}"(0x{:x})'.format(type_switcher.get(err_type, str(err_type)), err_type)
desc_ += ', code="{}"(0x{:x})'.format(code_switcher.get(err_code, str(err_code)), err_code)
return desc_
def stm_ai_node_type_to_str(op_type_id, with_id=True):
""" # noqa: DAR005
Return a human readable description of a c-node
see <AICore>::EmbedNets git (src/layers/layers_list.h file)
Parameters
----------
op_type_id
stm.ai operator id
with_id:
add numerical value in the description
Returns
-------
str
human readable description of the c-node
"""
base = 0x100 # stateless operator
base_2 = 0x180 # stateful operator
op_type_id = op_type_id & 0xFFFF
ls_switcher = {
0: 'Output',
base: 'Base',
base + 1: 'Add',
base + 2: 'BN',
base + 3: 'Conv2D',
base + 4: 'Dense',
base + 5: 'GRU',
base + 6: 'LRN',
base + 7: 'NL',
base + 8: 'Norm',
base + 9: 'Conv2dPool',
base + 10: 'Transpose',
base + 11: 'Pool',
base + 12: 'Softmax',
base + 13: 'Split',
base + 14: 'TimeDelay',
base + 15: 'TimeDistributed',
base + 16: 'Concat',
base + 17: 'GEMM',
base + 18: 'Upsample',
base + 19: 'Eltwise',
base + 20: 'EltwiseInt',
base + 21: 'InstNorm',
base + 22: 'Pad',
base + 23: 'Slice',
base + 24: 'Tile',
base + 25: 'Reduce',
base + 26: 'RNN',
base + 27: 'Resize',
base + 28: 'Gather',
base + 29: 'Pack',
base + 30: 'UnPack',
base + 31: 'Container',
base + 32: 'Lambda',
base + 33: 'Cast',
base + 34: 'iForest',
base + 35: 'SvmReg',
base + 36: 'AFeatureExtract',
base + 37: 'SVC',
base + 38: 'ZipMap',
base_2: 'Stateful',
base_2 + 1: 'LSTM',
base_2 + 2: 'Custom',
}
desc_ = '{}'.format(ls_switcher.get(op_type_id, str(op_type_id)))
if with_id:
desc_ += ' (0x{:x})'.format(op_type_id)
return desc_
class AiBufferFormat(enum.IntEnum):
""" Map C 'ai_buffer_format' enum to Python enum. """ # noqa: DAR101,DAR201,DAR401
# see api/ai_platform.h file
AI_BUFFER_FORMAT_NONE = 0x00000040
AI_BUFFER_FORMAT_FLOAT = 0x01821040
AI_BUFFER_FORMAT_U8 = 0x00040440
AI_BUFFER_FORMAT_U16 = 0x00040840
AI_BUFFER_FORMAT_U32 = 0x00041040
AI_BUFFER_FORMAT_U64 = 0x00042040
AI_BUFFER_FORMAT_S8 = 0x00840440
AI_BUFFER_FORMAT_S16 = 0x00840840
AI_BUFFER_FORMAT_S32 = 0x00841040
AI_BUFFER_FORMAT_S64 = 0x00842040
AI_BUFFER_FORMAT_Q = 0x00840040
AI_BUFFER_FORMAT_Q7 = 0x00840447
AI_BUFFER_FORMAT_Q15 = 0x0084084f
AI_BUFFER_FORMAT_UQ = 0x00040040
AI_BUFFER_FORMAT_UQ7 = 0x00040447
AI_BUFFER_FORMAT_UQ15 = 0x0004084f
AI_BUFFER_FORMAT_BOOL = 0x00060440
""" Buffer Format Flags """
MASK = (0x01ffffff) # non-flag bits
MASK_Q = (0xffffc000)
TYPE_MASK = (0x019e3f80) # bits required for type identification
FBIT_SHIFT = (0x40) # zero point for signed fractional bits
""" Bit mask definitions """
FLOAT_MASK = 0x1
FLOAT_SHIFT = 24
SIGN_MASK = 0x1
SIGN_SHIFT = 23
TYPE_ID_MASK = 0xF
TYPE_ID_SHIFT = 17
BITS_MASK = 0x7F
BITS_SHIFT = 7
FBITS_MASK = 0x7F
FBITS_SHIFT = 0
TYPE_NONE = 0x0
TYPE_FLOAT = 0x1
TYPE_Q = 0x2
TYPE_BOOL = 0x3
FLAG_CONST = (0x1 << 30)
FLAG_STATIC = (0x1 << 29)
FLAG_IS_IO = (0x1 << 27)
@staticmethod
def to_dict(fmt):
"""Return a dict with fmt field values"""
# noqa: DAR101,DAR201,DAR401
_dict = {
'float': (fmt >> AiBufferFormat.FLOAT_SHIFT) & AiBufferFormat.FLOAT_MASK,
'sign': (fmt >> AiBufferFormat.SIGN_SHIFT) & AiBufferFormat.SIGN_MASK,
'type': (fmt >> AiBufferFormat.TYPE_ID_SHIFT) & AiBufferFormat.TYPE_ID_MASK,
'bits': (fmt >> AiBufferFormat.BITS_SHIFT) & AiBufferFormat.BITS_MASK,
'fbits': (fmt >> AiBufferFormat.FBITS_SHIFT) & AiBufferFormat.FBITS_MASK,
}
_dict['fbits'] = _dict['fbits'] - 64
_dict['np_type'] = AiBufferFormat.to_np_type(fmt)
return _dict
@staticmethod
def to_np_type(fmt):
"""Return numpy type based on AI buffer format definition"""
# noqa: DAR101,DAR201,DAR401
_type = (fmt >> AiBufferFormat.TYPE_ID_SHIFT) & AiBufferFormat.TYPE_ID_MASK
_bits = (fmt >> AiBufferFormat.BITS_SHIFT) & AiBufferFormat.BITS_MASK
_sign = (fmt >> AiBufferFormat.SIGN_SHIFT) & AiBufferFormat.SIGN_MASK
if _type == AiBufferFormat.TYPE_FLOAT:
if _bits == 32:
return np.float32
if _bits == 16:
return np.float16
return np.float64
if _type == AiBufferFormat.TYPE_NONE:
return np.void
if _type == AiBufferFormat.TYPE_BOOL:
return np.bool
if _type == AiBufferFormat.TYPE_Q:
if _sign and _bits == 8:
return np.int8
if _sign and _bits == 16:
return np.int16
if _sign and _bits == 32:
return np.int32
if _sign and _bits == 64:
return np.int64
if _bits == 8:
return np.uint8
if _bits == 16:
return np.uint16
if _bits == 32:
return np.uint32
if _bits == 64:
return np.uint64
msg_ = 'AI type ({}/{}/{}) to numpy type not supported'.format(_type, _bits, _sign)
raise NotImplementedError(msg_)
@staticmethod
def to_fmt(np_type, is_io=True, static=False, const=False):
"""Convert numpy dtype to AiBufferFormat"""
# noqa: DAR101,DAR201,DAR401
np_to_ai_format = {
np.void: AiBufferFormat.AI_BUFFER_FORMAT_NONE,
np.bool: AiBufferFormat.AI_BUFFER_FORMAT_BOOL,
np.float32: AiBufferFormat.AI_BUFFER_FORMAT_FLOAT,
np.uint8: AiBufferFormat.AI_BUFFER_FORMAT_U8,
np.uint16: AiBufferFormat.AI_BUFFER_FORMAT_U16,
np.uint32: AiBufferFormat.AI_BUFFER_FORMAT_U32,
np.uint64: AiBufferFormat.AI_BUFFER_FORMAT_U64,
np.int8: AiBufferFormat.AI_BUFFER_FORMAT_S8,
np.int16: AiBufferFormat.AI_BUFFER_FORMAT_S16,
np.int32: AiBufferFormat.AI_BUFFER_FORMAT_S32,
np.int64: AiBufferFormat.AI_BUFFER_FORMAT_S64,
}
fmt = np_to_ai_format.get(np_type, None)
if fmt is None:
msg_ = 'numpy type {} is not supported'.format(str(np_type))
raise NotImplementedError(msg_)
if is_io:
fmt |= AiBufferFormat.FLAG_IS_IO
if static:
fmt |= AiBufferFormat.FLAG_STATIC
if const:
fmt |= AiBufferFormat.FLAG_CONST
return fmt
def stm_tflm_node_type_to_str(op_type_id, with_id=True):
""" # noqa: DAR005
Return a human readable description of a TFLM-node
see <TF>::Tensorflow git (tensorflow/lite/schema/schema_generated.h file)
Parameters
----------
op_type_id
TFLM operator id/index
with_id:
add numerical value in the description
Returns
-------
str
human readable description of the c-node
"""
if 0 <= op_type_id < len(_TFLM_OPERATORS):
desc_ = '{}'.format(_TFLM_OPERATORS[op_type_id])
else:
desc_ = 'custom or unknown'
with_id = True
if with_id:
desc_ += ' ({})'.format(op_type_id)
return desc_
_TFLM_OPERATORS = [
"ADD",
"AVERAGE_POOL_2D",
"CONCATENATION",
"CONV_2D",
"DEPTHWISE_CONV_2D",
"DEPTH_TO_SPACE",
"DEQUANTIZE",
"EMBEDDING_LOOKUP",
"FLOOR",
"FULLY_CONNECTED",
"HASHTABLE_LOOKUP",
"L2_NORMALIZATION",
"L2_POOL_2D",
"LOCAL_RESPONSE_NORMALIZATION",
"LOGISTIC",
"LSH_PROJECTION",
"LSTM",
"MAX_POOL_2D",
"MUL",
"RELU",
"RELU_N1_TO_1",
"RELU6",
"RESHAPE",
"RESIZE_BILINEAR",
"RNN",
"SOFTMAX",
"SPACE_TO_DEPTH",
"SVDF",
"TANH",
"CONCAT_EMBEDDINGS",
"SKIP_GRAM",
"CALL",
"CUSTOM",
"EMBEDDING_LOOKUP_SPARSE",
"PAD",
"UNIDIRECTIONAL_SEQUENCE_RNN",
"GATHER",
"BATCH_TO_SPACE_ND",
"SPACE_TO_BATCH_ND",
"TRANSPOSE",
"MEAN",
"SUB",
"DIV",
"SQUEEZE",
"UNIDIRECTIONAL_SEQUENCE_LSTM",
"STRIDED_SLICE",
"BIDIRECTIONAL_SEQUENCE_RNN",
"EXP",
"TOPK_V2",
"SPLIT",
"LOG_SOFTMAX",
"DELEGATE",
"BIDIRECTIONAL_SEQUENCE_LSTM",
"CAST",
"PRELU",
"MAXIMUM",
"ARG_MAX",
"MINIMUM",
"LESS",
"NEG",
"PADV2",
"GREATER",
"GREATER_EQUAL",
"LESS_EQUAL",
"SELECT",
"SLICE",
"SIN",
"TRANSPOSE_CONV",
"SPARSE_TO_DENSE",
"TILE",
"EXPAND_DIMS",
"EQUAL",
"NOT_EQUAL",
"LOG",
"SUM",
"SQRT",
"RSQRT",
"SHAPE",
"POW",
"ARG_MIN",
"FAKE_QUANT",
"REDUCE_PROD",
"REDUCE_MAX",
"PACK",
"LOGICAL_OR",
"ONE_HOT",
"LOGICAL_AND",
"LOGICAL_NOT",
"UNPACK",
"REDUCE_MIN",
"FLOOR_DIV",
"REDUCE_ANY",
"SQUARE",
"ZEROS_LIKE",
"FILL",
"FLOOR_MOD",
"RANGE",
"RESIZE_NEAREST_NEIGHBOR",
"LEAKY_RELU",
"SQUARED_DIFFERENCE",
"MIRROR_PAD",
"ABS",
"SPLIT_V",
"UNIQUE",
"CEIL",
"REVERSE_V2",
"ADD_N",
"GATHER_ND",
"COS",
"WHERE",
"RANK",
"ELU",
"REVERSE_SEQUENCE",
"MATRIX_DIAG",
"QUANTIZE",
"MATRIX_SET_DIAG",
"ROUND",
"HARD_SWISH",
"IF",
"WHILE",
"NON_MAX_SUPPRESSION_V4",
"NON_MAX_SUPPRESSION_V5",
"SCATTER_ND",
"SELECT_V2",
"DENSIFY",
"SEGMENT_SUM",
"BATCH_MATMUL",
"PLACEHOLDER_FOR_GREATER_OP_CODES",
"CUMSUM",
"CALL_ONCE",
"BROADCAST_TO",
"RFFT2D",
"CONV_3D",
"IMAG",
"REAL",
"COMPLEX_ABS",
"HASHTABLE",
"HASHTABLE_FIND",
"HASHTABLE_IMPORT",
"HASHTABLE_SIZE"
]