-
Notifications
You must be signed in to change notification settings - Fork 528
/
Copy pathxnnpack_graph_schema.py
486 lines (361 loc) · 7.55 KB
/
xnnpack_graph_schema.py
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
# pyre-strict
"""
Please refer to executorch/backends/xnnpack/serialization/schema.fbs for the schema definitions
"""
from dataclasses import dataclass
from enum import IntEnum
from typing import List, Optional, Union
# Generic node data class with one input and one output
@dataclass
class XNNNode1x1:
input_id: int
output_id: int
flags: int
# Generic node data class with two inputs and one output
@dataclass
class XNNNode2x1:
input1_id: int
input2_id: int
output_id: int
flags: int
# Generic node data class for concatenation node
@dataclass
class XNNCat:
axis: int
input1_id: int
input2_id: int
input3_id: int
input4_id: int
output_id: int
flags: int
input5_id: int
# Generic node data class for convolution type nodes
@dataclass
class XNNNodeConv:
padding_top: int
padding_right: int
padding_bottom: int
padding_left: int
kernel_height: int
kernel_width: int
subsampling_height: int
subsampling_width: int
dilation_height: int
dilation_width: int
group_input_channels: int
group_output_channels: int
groups: int
adjustment_height: int
adjustment_width: int
input1_id: int
filter_id: int
bias_id: int
output_id: int
flags: int
@dataclass
class XNNPooling2D:
padding_top: int
padding_right: int
padding_bottom: int
padding_left: int
pooling_height: int
pooling_width: int
stride_height: int
stride_width: int
dilation_height: int
dilation_width: int
input_id: int
output_id: int
flags: int
# Node data class for average pooling 2d
@dataclass
class XNNAvgPooling2d(XNNPooling2D):
pass
@dataclass
class XNNMaxPooling2d(XNNPooling2D):
pass
@dataclass
class XNNConv2d(XNNNodeConv):
pass
@dataclass
class XNNConvTranspose2d(XNNNodeConv):
pass
@dataclass
class XNNAdd(XNNNode2x1):
pass
@dataclass
class XNNGlobalAvgPooling2d(XNNNode1x1):
pass
@dataclass
class XNNDiv(XNNNode2x1):
pass
@dataclass
class XNNMultiply(XNNNode2x1):
pass
@dataclass
class XNNMinimum(XNNNode2x1):
pass
@dataclass
class XNNSubtract(XNNNode2x1):
pass
@dataclass
class XNNSoftmax(XNNNode1x1):
pass
@dataclass
class XNNSigmoid(XNNNode1x1):
pass
@dataclass
class XNNFloor(XNNNode1x1):
pass
@dataclass
class XNNConvert(XNNNode1x1):
pass
@dataclass
class XNNNegate(XNNNode1x1):
pass
@dataclass
class XNNAbs(XNNNode1x1):
pass
@dataclass
class XNNConcatenate2(XNNCat):
pass
@dataclass
class XNNConcatenate3(XNNCat):
pass
@dataclass
class XNNConcatenate4(XNNCat):
pass
@dataclass
class XNNConcatenate5(XNNCat):
pass
@dataclass
class XNNBatchMatrixMultiply(XNNNode2x1):
pass
@dataclass
class XNNStaticTranspose:
num_dims: int
perm: List[int]
input_id: int
output_id: int
flags: int
@dataclass
class XNNStaticSlice:
num_dims: int
offsets: List[int]
sizes: List[int]
input_id: int
output_id: int
flags: int
@dataclass
class XNNClamp(XNNNode1x1):
pass
@dataclass
class XNNStaticResizeBilinear2D:
new_height: int
new_width: int
input_id: int
output_id: int
flags: int
@dataclass
class XNNStaticConstantPad:
pre_paddings: List[int]
post_paddings: List[int]
padding_value: float
input_id: int
output_id: int
flags: int
@dataclass
class XNNDepthwiseConv2d(XNNNodeConv):
pass
@dataclass
class XNNArgMaxPooling2d:
padding_top: int
padding_right: int
padding_bottom: int
padding_left: int
pooling_height: int
pooling_width: int
input_id: int
output_value_id: int
output_index_id: int
flags: int
# this class such that Python can infer the XNodeUnion Type. If there is only type in Union, like
# Union[XNNAdd], python will infer it's XNNAdd type instead of Union type. After we add more operators
# this one can be removed.
@dataclass
class XNNFullyConnected: # aten::Linear
input1_id: int
filter_id: int
bias_id: int
output_id: int
flags: int
@dataclass
class XNNStaticReshape:
num_dims: int
new_shape: List[int]
input_id: int
output_id: int
flags: int
@dataclass
class XNNSquareRoot(XNNNode1x1):
pass
@dataclass
class XNNReciprocalSquareRoot(XNNNode1x1):
pass
@dataclass
class XNNCeiling(XNNNode1x1):
pass
@dataclass
class XNNHardswish(XNNNode1x1):
pass
@dataclass
class XNNSquare(XNNNode1x1):
pass
@dataclass
class XNNLeakyReLU:
negative_slope: float
input_id: int
output_id: int
flags: int
@dataclass
class XNNMaximum(XNNNode2x1):
pass
@dataclass
class XNNELU:
alpha: float
input_id: int
output_id: int
flags: int
@dataclass
class XNNPReLU(XNNNode2x1):
pass
@dataclass
class XNNScaledDotProductAttention:
query_id: int
key_id: int
value_id: int
scale_id: int
mask_id: int
output_id: int
flags: int
XNodeUnion = Union[
XNNAdd,
XNNFullyConnected,
XNNSoftmax,
XNNSigmoid,
XNNStaticTranspose,
XNNClamp,
XNNConv2d,
XNNConvTranspose2d,
XNNDiv,
XNNStaticResizeBilinear2D,
XNNStaticConstantPad,
XNNAvgPooling2d,
XNNMinimum,
XNNDepthwiseConv2d,
XNNMaxPooling2d,
XNNMultiply,
XNNSubtract,
XNNFloor,
XNNConvert,
XNNGlobalAvgPooling2d,
XNNStaticReshape,
XNNArgMaxPooling2d,
XNNSquareRoot,
XNNCeiling,
XNNHardswish,
XNNLeakyReLU,
XNNMaximum,
XNNNegate,
XNNSquare,
XNNELU,
XNNAbs,
XNNPReLU,
XNNConcatenate2,
XNNConcatenate3,
XNNConcatenate4,
XNNConcatenate5,
XNNStaticSlice,
XNNScaledDotProductAttention,
XNNBatchMatrixMultiply,
XNNReciprocalSquareRoot,
]
@dataclass
class OutputMinMax:
output_min: Union[float, str]
output_max: Union[float, str]
@dataclass
class XNode:
xnode_union: "XNodeUnion"
debug_handle: int
output_min_max: Optional[OutputMinMax] = None
class XNNDatatype(IntEnum):
xnn_datatype_invalid = 0
xnn_datatype_fp32 = 1
xnn_datatype_fp16 = 2
xnn_datatype_qint8 = 3
xnn_datatype_quint8 = 4
xnn_datatype_qint32 = 5
xnn_datatype_qcint8 = 6
xnn_datatype_qcint32 = 7
xnn_datatype_qcint4 = 8
xnn_datatype_qdint8 = 9
xnn_datatype_qbint4 = 10
@dataclass
class PerChannelQuant:
scale: List[float]
channel_dim: int
@dataclass
class PerChannelGroupQuant:
scale: List[float]
channel_dim: int
group_size: int = 1
@dataclass
class PerTokenDynamicQuant:
num_nonbatch_dims: int
@dataclass
class PerTensorQuant:
scale: float
zero_point: int
XNNQuantParams = Union[
PerChannelQuant, PerTensorQuant, PerTokenDynamicQuant, PerChannelGroupQuant
]
@dataclass
class XNNTensorValue:
datatype: XNNDatatype
num_dims: int
dims: List[int]
constant_buffer_idx: int
external_id: int
flags: int
id_out: int
@dataclass
class XNNQuantizedTensorValue:
tensor_value: XNNTensorValue
quant_params: "XNNQuantParams"
XValueUnion = Union[
XNNTensorValue,
XNNQuantizedTensorValue,
]
@dataclass
class XValue:
xvalue_union: "XValueUnion"
@dataclass
class ConstantDataOffset:
offset: int
size: int
named_key: str = ""
@dataclass
class XNNGraph:
version: str
xnodes: List[XNode]
xvalues: List[XValue]
num_externs: int
input_ids: List[int]
output_ids: List[int]
constant_data: List[ConstantDataOffset]