blob: 929aebd1d4d2487ed478a268ffce9a55928b1def [file] [log] [blame]
Jeremy Johnson9b225172021-12-14 16:34:47 +00001# Copyright (c) 2020-2022, ARM Limited.
Kevin Chengfea5a372021-10-11 18:38:47 +00002#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Kevin Chengfea5a372021-10-11 18:38:47 +000015import os
Kevin Chengfea5a372021-10-11 18:38:47 +000016import json
17import flatbuffers
18import numpy as np
19import struct
Jeremy Johnson9b225172021-12-14 16:34:47 +000020from enum import IntEnum, unique
Kevin Chengfea5a372021-10-11 18:38:47 +000021from tosa import (
22 TosaGraph,
23 TosaBasicBlock,
24 TosaTensor,
25 TosaOperator,
Kevin Chengfea5a372021-10-11 18:38:47 +000026 Version,
27)
Jeremy Johnson9b225172021-12-14 16:34:47 +000028import tosa.DType as TosaDType
29import tosa.Op as TosaOp
Kevin Chengfea5a372021-10-11 18:38:47 +000030
Kevin Chenge6563f52021-10-20 12:12:02 -070031# Keep version number in sync with the version default value with schema/tosa.fbs
Kevin Chengb97cb1d2021-10-14 11:53:39 -070032TOSA_VERSION_MAJOR = 0
Eric Kunzea687b612021-11-03 17:02:57 -070033TOSA_VERSION_MINOR = 24
Kevin Chengb97cb1d2021-10-14 11:53:39 -070034TOSA_VERSION_PATCH = 0
Eric Kunzea687b612021-11-03 17:02:57 -070035TOSA_VERSION_DRAFT = True
Jeremy Johnson9b225172021-12-14 16:34:47 +000036TOSA_VERSION = [
37 TOSA_VERSION_MAJOR,
38 TOSA_VERSION_MINOR,
39 TOSA_VERSION_PATCH,
40 TOSA_VERSION_DRAFT,
41]
Eric Kunzee6596402022-06-09 21:27:36 +000042
43# File identifier needs to be kept in sync with schema/tosa.fbs
44TOSA_GRAPH_IDENTIFIER = b"\x54\x4F\x53\x41"
45
Kevin Chengfea5a372021-10-11 18:38:47 +000046# With the way flatc generates its python types, there is no programatic way
47# to get string names for the integer types. Manually maintain a string table
48# here.
Jeremy Johnson9b225172021-12-14 16:34:47 +000049DType = TosaDType.DType()
Kevin Chengfea5a372021-10-11 18:38:47 +000050DTypeNames = [
51 "UNKNOWN",
52 "BOOL",
53 "UINT8",
54 "INT4",
55 "INT8",
56 "INT16",
57 "INT32",
58 "INT48",
59 "FLOAT",
Jeremy Johnson41027732022-05-25 17:52:29 +010060 "UINT16",
Kevin Chengfea5a372021-10-11 18:38:47 +000061]
62
63ByteMask = np.uint64(0xFF)
64
65
66def dtype_str_to_val(name):
67
68 for i in range(len(DTypeNames)):
69 if name.casefold() == DTypeNames[i].casefold():
70 return i
71 raise Exception("Unable to parse DType name {}".format(name))
72
73
74class TosaSerializerUnion:
75 """This class handles encapsulating and serializing union types into flatbuffers"""
76
77 def __init__(self):
78
Jeremy Johnson9b225172021-12-14 16:34:47 +000079 # A tuple of the start and end functions.
80 # Set by the options constructors below
Kevin Chengfea5a372021-10-11 18:38:47 +000081 self.optFcns = None
82
Jeremy Johnson9b225172021-12-14 16:34:47 +000083 # The type from the tosa.Options enumeration.
84 # Set by the options constructors below.
Kevin Chengfea5a372021-10-11 18:38:47 +000085 self.utype = None
86
87 # Each of these lists is a tuple of the add function and the
88 # value being added. Set by the options constructors below.
89 self.ints = []
90 self.bools = []
91 self.floats = []
92 self.strings = []
93 self.intvecs = []
94 self.fpvecs = []
95
96 def serialize(self, builder):
97
98 # We have to build strings and vectors first
99 strList = []
100 intVecList = []
101 fpVecList = []
102
103 for fcn, val in self.strings:
104 strList.append((fcn, builder.CreateString(val)))
105
106 for fcn, val in self.intvecs:
107 intVecList.append((fcn, TosaSerializer.serializeInt32Vec(builder, val)))
108
109 for fcn, val in self.fpvecs:
110 fpVecList.append((fcn, TosaSerializer.serializeFpVec(builder, val)))
111
112 startFcn, endFcn = self.optFcns
113
114 # Then serialize the options object from the list of primitives and
115 # other serialized values
116 startFcn(builder)
117 for fcn, val in self.ints:
118 fcn(builder, val)
119
120 for fcn, val in self.bools:
121 fcn(builder, val)
122
123 for fcn, val in self.floats:
124 fcn(builder, val)
125
126 for fcn, val in strList:
127 fcn(builder, val)
128
129 for fcn, val in intVecList:
130 fcn(builder, val)
131
132 for fcn, val in fpVecList:
133 fcn(builder, val)
134
135 return endFcn(builder)
136
137
138class TosaSerializerAttribute(TosaSerializerUnion):
139 """This class handles encapsulating all of the enumerated types for attributes"""
140
141 def __init__(self):
142 super().__init__()
143
TatWai Chong7be71652022-05-10 17:26:20 -0700144 def PoolAttribute(self, kernel, stride, pad):
Kevin Chengfea5a372021-10-11 18:38:47 +0000145 from tosa import PoolAttribute as a, Attribute
146
147 self.utype = Attribute.Attribute().PoolAttribute
148
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800149 self.optFcns = (a.Start, a.End)
TatWai Chong7be71652022-05-10 17:26:20 -0700150 self.intvecs.append((a.AddPad, pad))
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800151 self.intvecs.append((a.AddKernel, kernel))
152 self.intvecs.append((a.AddStride, stride))
Kevin Chengfea5a372021-10-11 18:38:47 +0000153
TatWai Chong7be71652022-05-10 17:26:20 -0700154 def ConvAttribute(self, pad, stride, dilation):
Kevin Chengfea5a372021-10-11 18:38:47 +0000155 from tosa import ConvAttribute as a, Attribute
156
157 self.utype = Attribute.Attribute().ConvAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800158 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000159
TatWai Chong7be71652022-05-10 17:26:20 -0700160 self.intvecs.append((a.AddPad, pad))
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800161 self.intvecs.append((a.AddStride, stride))
162 self.intvecs.append((a.AddDilation, dilation))
Kevin Chengfea5a372021-10-11 18:38:47 +0000163
164 def TransposeConvAttribute(self, outpad, stride, dilation, output_shape):
165 from tosa import TransposeConvAttribute as a, Attribute
166
167 self.utype = Attribute.Attribute().TransposeConvAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800168 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000169
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800170 self.intvecs.append((a.AddOutpad, outpad))
171 self.intvecs.append((a.AddStride, stride))
172 self.intvecs.append((a.AddDilation, dilation))
173 self.intvecs.append((a.AddOutputShape, output_shape))
Kevin Chengfea5a372021-10-11 18:38:47 +0000174
Kevin Cheng38d214c2021-10-15 15:49:19 -0700175 def PadAttribute(self, padding, pad_const_int, pad_const_fp):
176 from tosa import PadAttribute as a, Attribute
Kevin Chengfea5a372021-10-11 18:38:47 +0000177
Kevin Cheng38d214c2021-10-15 15:49:19 -0700178 self.utype = Attribute.Attribute().PadAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800179 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000180
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800181 self.intvecs.append((a.AddPadding, padding))
182 self.ints.append((a.AddPadConstInt, pad_const_int))
183 self.floats.append((a.AddPadConstFp, pad_const_fp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000184
185 def AxisAttribute(self, axis):
186 from tosa import AxisAttribute as a, Attribute
187
188 self.utype = Attribute.Attribute().AxisAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800189 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000190
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800191 self.ints.append((a.AddAxis, axis))
Kevin Chengfea5a372021-10-11 18:38:47 +0000192
TatWai Chong7be71652022-05-10 17:26:20 -0700193 def ReshapeAttribute(self, new_shape):
Kevin Chengfea5a372021-10-11 18:38:47 +0000194 from tosa import ReshapeAttribute as a, Attribute
195
196 self.utype = Attribute.Attribute().ReshapeAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800197 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000198
TatWai Chong7be71652022-05-10 17:26:20 -0700199 self.intvecs.append((a.AddNewShape, new_shape))
Kevin Chengfea5a372021-10-11 18:38:47 +0000200
TatWai Chong7be71652022-05-10 17:26:20 -0700201 def SliceAttribute(self, start, size):
Kevin Chengfea5a372021-10-11 18:38:47 +0000202 from tosa import SliceAttribute as a, Attribute
203
204 self.utype = Attribute.Attribute().SliceAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800205 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000206
TatWai Chong7be71652022-05-10 17:26:20 -0700207 self.intvecs.append((a.AddStart, start))
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800208 self.intvecs.append((a.AddSize, size))
Kevin Chengfea5a372021-10-11 18:38:47 +0000209
210 def TileAttribute(self, multiples):
211 from tosa import TileAttribute as a, Attribute
212
213 self.utype = Attribute.Attribute().TileAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800214 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000215
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800216 self.intvecs.append((a.AddMultiples, multiples))
Kevin Chengfea5a372021-10-11 18:38:47 +0000217
218 def ResizeAttribute(
219 self, output_size, stride, offset, shift, stride_fp, offset_fp, mode
220 ):
221 from tosa import ResizeAttribute as a, Attribute
222
223 self.utype = Attribute.Attribute().ResizeAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800224 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000225
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800226 self.intvecs.append((a.AddOutputSize, output_size))
227 self.intvecs.append((a.AddStride, stride))
228 self.intvecs.append((a.AddOffset, offset))
229 self.ints.append((a.AddShift, shift))
230 self.fpvecs.append((a.AddStrideFp, stride_fp))
231 self.fpvecs.append((a.AddOffsetFp, offset_fp))
232 self.ints.append((a.AddMode, mode))
Kevin Chengfea5a372021-10-11 18:38:47 +0000233
234 def ClampAttribute(self, minint, maxint, minfp, maxfp):
235 from tosa import ClampAttribute as a, Attribute
236
237 self.utype = Attribute.Attribute().ClampAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800238 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000239
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800240 self.ints.append((a.AddMinInt, minint))
241 self.ints.append((a.AddMaxInt, maxint))
Kevin Chengfea5a372021-10-11 18:38:47 +0000242
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800243 self.ints.append((a.AddMinFp, minfp))
244 self.ints.append((a.AddMaxFp, maxfp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000245
246 def RescaleAttribute(
247 self, input_zp, output_zp, multiplier, shift, scale32, double_round, per_channel
248 ):
249 from tosa import RescaleAttribute as a, Attribute
250
251 self.utype = Attribute.Attribute().RescaleAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800252 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000253
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800254 self.ints.append((a.AddInputZp, input_zp))
255 self.ints.append((a.AddOutputZp, output_zp))
256 self.intvecs.append((a.AddMultiplier, multiplier))
257 self.intvecs.append((a.AddShift, shift))
258 self.bools.append((a.AddScale32, scale32))
259 self.bools.append((a.AddDoubleRound, double_round))
260 self.bools.append((a.AddPerChannel, per_channel))
Kevin Chengfea5a372021-10-11 18:38:47 +0000261
262 def MulAttribute(self, shift):
263 from tosa import MulAttribute as a, Attribute
264
265 self.utype = Attribute.Attribute().MulAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800266 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000267
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800268 self.ints.append((a.AddShift, shift))
Kevin Chengfea5a372021-10-11 18:38:47 +0000269
270 def ArithmeticRightShiftAttribute(self, round):
271 from tosa import ArithmeticRightShiftAttribute as a, Attribute
272
273 self.utype = Attribute.Attribute().ArithmeticRightShiftAttribute
274 self.optFcns = (
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800275 a.Start,
276 a.End,
Kevin Chengfea5a372021-10-11 18:38:47 +0000277 )
278
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800279 self.bools.append((a.AddRound, round))
Kevin Chengfea5a372021-10-11 18:38:47 +0000280
Kevin Chengfea5a372021-10-11 18:38:47 +0000281 def CondIfAttribute(self, then_branch, else_branch):
282 from tosa import CondIfAttribute as a, Attribute
283
284 self.utype = Attribute.Attribute().CondIfAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800285 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000286
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800287 self.strings.append((a.AddThenBranch, then_branch))
288 self.strings.append((a.AddElseBranch, else_branch))
Kevin Chengfea5a372021-10-11 18:38:47 +0000289
290 def WhileLoopAttribute(self, cond_branch, body_branch):
291 from tosa import WhileLoopAttribute as a, Attribute
292
293 self.utype = Attribute.Attribute().WhileLoopAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800294 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000295
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800296 self.strings.append((a.AddCondBranch, cond_branch))
297 self.strings.append((a.AddBodyBranch, body_branch))
Kevin Chengfea5a372021-10-11 18:38:47 +0000298
TatWai Chong7be71652022-05-10 17:26:20 -0700299 def TransposeAttribute(self, perms):
Kevin Cheng38d214c2021-10-15 15:49:19 -0700300 from tosa import TransposeAttribute as a, Attribute
301
302 self.utype = Attribute.Attribute().TransposeAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800303 self.optFcns = (a.Start, a.End)
Kevin Cheng38d214c2021-10-15 15:49:19 -0700304
TatWai Chong7be71652022-05-10 17:26:20 -0700305 self.intvecs.append((a.AddPerms, perms))
Kevin Cheng38d214c2021-10-15 15:49:19 -0700306
307 def TableAttribute(self, table):
308 from tosa import TableAttribute as a, Attribute
309
310 self.utype = Attribute.Attribute().TableAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800311 self.optFcns = (a.Start, a.End)
Kevin Cheng38d214c2021-10-15 15:49:19 -0700312
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800313 self.intvecs.append((a.AddTable, table))
Kevin Chengfea5a372021-10-11 18:38:47 +0000314
Jeremy Johnson9b225172021-12-14 16:34:47 +0000315
Kevin Chengfea5a372021-10-11 18:38:47 +0000316class TosaSerializerQuantInfo(TosaSerializerUnion):
Jeremy Johnson9b225172021-12-14 16:34:47 +0000317 """This class handles encapsulating all of the enumerated types for quantinfo"""
Kevin Chengfea5a372021-10-11 18:38:47 +0000318
319 def __init__(self):
320 super().__init__()
321
322 def ConvQuantInfo(self, input_zp, weight_zp):
323 from tosa import ConvQuantInfo as q, QuantInfo
324
325 self.utype = QuantInfo.QuantInfo().ConvQuantInfo
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800326 self.optFcns = (q.Start, q.End)
327 self.ints.append((q.AddInputZp, input_zp))
328 self.ints.append((q.AddWeightZp, weight_zp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000329
330 def UnaryQuantInfo(self, input_zp, output_zp):
331 from tosa import UnaryQuantInfo as q, QuantInfo
332
333 self.utype = QuantInfo.QuantInfo().UnaryQuantInfo
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800334 self.optFcns = (q.Start, q.End)
335 self.ints.append((q.AddInputZp, input_zp))
336 self.ints.append((q.AddOutputZp, output_zp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000337
338 def MatMulQuantInfo(self, a_zp, b_zp):
339 from tosa import MatMulQuantInfo as q, QuantInfo
340
341 self.utype = QuantInfo.QuantInfo().MatMulQuantInfo
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800342 self.optFcns = (q.Start, q.End)
343 self.ints.append((q.AddAZp, a_zp))
344 self.ints.append((q.AddBZp, b_zp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000345
346 def PadQuantInfo(self, input_zp):
347 from tosa import PadQuantInfo as q, QuantInfo
348
349 self.utype = QuantInfo.QuantInfo().PadQuantInfo
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800350 self.optFcns = (q.Start, q.End)
351 self.ints.append((q.AddInputZp, input_zp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000352
353
354class TosaSerializerTensor:
355 def __init__(
356 self,
357 name,
358 shape,
359 dtype,
360 data=None,
361 placeholderFilename=None,
362 ):
363 self.name = name
364
365 if isinstance(shape, np.ndarray):
366 shape = shape.astype(int).tolist()
367 shape = list(map(int, shape))
368
369 self.shape = shape
370 self.dtype = dtype
371
372 if isinstance(data, np.ndarray):
373 data = data.flatten().astype(int).tolist()
374 data = list(map(int, data))
375 self.data = data
376 elif isinstance(data, list):
377 data = list(map(int, data))
378 self.data = data
379 else:
380 self.data = None
381
382 # Filename for placeholder tensors. These get generated by the test generation
Jeremy Johnson9b225172021-12-14 16:34:47 +0000383 # process and are written to disk, but are considered input tensors by the
384 # network so they do not appear in the TOSA serialiazation. However, if we
385 # want to form a unit test around these input tensors, we can get the filename
386 # from here.
Kevin Chengfea5a372021-10-11 18:38:47 +0000387 self.placeholderFilename = placeholderFilename
388
389 def __str__(self):
390 str = "TosaSerializerTensor name: {} shape: {} dtype: {}".format(
391 self.name,
392 self.shape,
393 DTypeNames[self.dtype],
394 )
395 return str
396
397 def setDtype(self, dtype):
398 self.dtype = dtype
399
400 def serialize(self, builder):
401 fb_name = builder.CreateString(self.name)
402 fb_shapes = TosaSerializer.serializeInt32Vec(builder, self.shape)
403 if self.data:
404 u8_data = list()
405 # little endianess
406 if self.dtype == DType.BOOL:
407 for val in self.data:
408 val_u8 = np.uint8(val)
409 u8_data.append(val_u8)
410 elif self.dtype == DType.INT4:
411 in_size = len(self.data)
412 out_size = (in_size + 1) // 2
413 for i in range(out_size):
414 val_0 = self.data[2 * i]
415 if (2 * i + 1) < in_size:
416 val_1 = self.data[2 * i + 1]
417 else:
418 val_1 = 0
419 val_i8 = (val_0 & 0xF) | ((val_1 & 0xF) << 4)
420 val_u8 = np.uint8(val_i8)
421 u8_data.append(val_u8)
422 elif self.dtype == DType.INT8:
423 for val in self.data:
424 val_u8 = np.uint8(val)
425 u8_data.append(val_u8)
426 elif self.dtype == DType.INT16:
427 for val in self.data:
428 val_u16 = np.uint16(val)
429 b0 = val_u16 & ByteMask
430 b1 = (val_u16 >> np.uint16(8)) & ByteMask
431 u8_data.extend([b0, b1])
432 elif self.dtype == DType.INT32:
433 for val in self.data:
434 val_u32 = np.uint32(val)
435 b0 = val_u32 & ByteMask
436 b1 = (val_u32 >> np.uint32(8)) & ByteMask
437 b2 = (val_u32 >> np.uint32(16)) & ByteMask
Kevin Cheng6b078ca2021-10-13 23:12:50 -0700438 b3 = (val_u32 >> np.uint32(24)) & ByteMask
Kevin Chengfea5a372021-10-11 18:38:47 +0000439 u8_data.extend([b0, b1, b2, b3])
440 elif self.dtype == DType.INT48:
441 for val in self.data:
442 val_u64 = np.uint64(val)
443 b0 = val_u64 & ByteMask
444 b1 = (val_u64 >> np.uint64(8)) & ByteMask
445 b2 = (val_u64 >> np.uint64(16)) & ByteMask
446 b3 = (val_u64 >> np.uint64(24)) & ByteMask
447 b4 = (val_u64 >> np.uint64(32)) & ByteMask
448 b5 = (val_u64 >> np.uint64(40)) & ByteMask
449 u8_data.extend([b0, b1, b2, b3, b4, b5])
450 elif self.dtype == DType.FLOAT:
451 for val in self.data:
452 b = struct.pack("!f", val)
453 u8_data.extend([b[3], b[2], b[1], b[0]])
454 else:
455 raise Exception(
456 "unsupported data type {}".format(DTypeNames[self.dtype])
457 )
458 fb_data = TosaSerializer.serializeUint8Vec(builder, u8_data)
459
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800460 TosaTensor.Start(builder)
461 TosaTensor.AddName(builder, fb_name)
462 TosaTensor.AddShape(builder, fb_shapes)
463 TosaTensor.AddType(builder, self.dtype)
Kevin Chengfea5a372021-10-11 18:38:47 +0000464 if self.data:
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800465 TosaTensor.AddData(builder, fb_data)
Kevin Chengfea5a372021-10-11 18:38:47 +0000466
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800467 return TosaTensor.End(builder)
Kevin Chengfea5a372021-10-11 18:38:47 +0000468
469
470class TosaSerializerOperator:
471 def __init__(self, op, inputs, outputs, attributes=None, quantInfo=None):
472 self.op = op
473 self.attributes = attributes
474 self.inputs = TosaSerializer.toList(inputs)
475 self.outputs = TosaSerializer.toList(outputs)
476 self.quantInfo = quantInfo
477
478 def __str__(self):
479 str = "Op {}\n----\n".format(self.op)
480
481 for i in self.inputs:
482 str = str + " Input: {}\n".format(i)
483 for o in self.outputs:
484 str = str + " Output: {}\n".format(o)
485
486 return str
487
488 def serialize(self, builder):
489 fb_inputs = TosaSerializer.serializeStrVec(
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800490 builder, self.inputs, TosaOperator.StartInputsVector
Kevin Chengfea5a372021-10-11 18:38:47 +0000491 )
492 fb_outputs = TosaSerializer.serializeStrVec(
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800493 builder, self.outputs, TosaOperator.StartOutputsVector
Kevin Chengfea5a372021-10-11 18:38:47 +0000494 )
495 # Need to serialize quant_info and attributes enums still
496 if self.attributes is not None:
497 fb_attributes = self.attributes.serialize(builder)
498
499 if self.quantInfo is not None:
500 fb_qinfo = self.quantInfo.serialize(builder)
501
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800502 TosaOperator.Start(builder)
503 TosaOperator.AddOp(builder, self.op)
504 TosaOperator.AddInputs(builder, fb_inputs)
505 TosaOperator.AddOutputs(builder, fb_outputs)
Kevin Chengfea5a372021-10-11 18:38:47 +0000506 if self.attributes is not None:
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800507 TosaOperator.AddAttributeType(builder, self.attributes.utype)
508 TosaOperator.AddAttribute(builder, fb_attributes)
Kevin Chengfea5a372021-10-11 18:38:47 +0000509 if self.quantInfo is not None:
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800510 TosaOperator.AddQuantInfoType(builder, self.quantInfo.utype)
511 TosaOperator.AddQuantInfo(builder, fb_qinfo)
Kevin Chengfea5a372021-10-11 18:38:47 +0000512
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800513 return TosaOperator.End(builder)
Kevin Chengfea5a372021-10-11 18:38:47 +0000514
515
516class TosaSerializerBasicBlock:
517 def __init__(self, name):
518 self.name = name
519 self.operators = []
520
521 # Dict assures uniqueness, but allows us to look up by name
522 self.tensors = dict()
523
524 self.inputs = []
525 self.outputs = []
526
527 def addTensor(
528 self,
529 name,
530 shape,
531 dtype,
532 data=None,
533 placeholderFilename=None,
534 ):
Jeremy Johnson9b225172021-12-14 16:34:47 +0000535 if name not in self.tensors:
Kevin Chengfea5a372021-10-11 18:38:47 +0000536 self.tensors[name] = TosaSerializerTensor(
537 name, shape, dtype, data, placeholderFilename
538 )
539
540 return self.tensors[name]
541
542 def addInput(self, name):
543 self.inputs.append(name)
544
545 def addOutput(self, name):
546 self.outputs.append(name)
547
548 def addOperator(self, op, inputs, outputs, attributes=None, quant_info=None):
549 self.operators.append(
550 TosaSerializerOperator(op, inputs, outputs, attributes, quant_info)
551 )
552
553 def serialize(self, builder):
554 fb_name = builder.CreateString(self.name)
555 fbv_inputs = TosaSerializer.serializeStrVec(
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800556 builder, list(self.inputs), TosaBasicBlock.StartInputsVector
Kevin Chengfea5a372021-10-11 18:38:47 +0000557 )
558 fbv_outputs = TosaSerializer.serializeStrVec(
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800559 builder, list(self.outputs), TosaBasicBlock.StartOutputsVector
Kevin Chengfea5a372021-10-11 18:38:47 +0000560 )
561 fbv_tensors = TosaSerializer.serializeObjVec(
562 builder,
563 list(self.tensors.values()),
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800564 TosaBasicBlock.StartTensorsVector,
Kevin Chengfea5a372021-10-11 18:38:47 +0000565 )
566 fbv_operators = TosaSerializer.serializeObjVec(
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800567 builder, self.operators, TosaBasicBlock.StartOperatorsVector
Kevin Chengfea5a372021-10-11 18:38:47 +0000568 )
569
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800570 TosaBasicBlock.Start(builder)
571 TosaBasicBlock.AddName(builder, fb_name)
572 TosaBasicBlock.AddInputs(builder, fbv_inputs)
573 TosaBasicBlock.AddOutputs(builder, fbv_outputs)
574 TosaBasicBlock.AddTensors(builder, fbv_tensors)
575 TosaBasicBlock.AddOperators(builder, fbv_operators)
576 return TosaBasicBlock.End(builder)
Kevin Chengfea5a372021-10-11 18:38:47 +0000577
578
579@unique
580class TensorDir(IntEnum):
581 PLACEHOLDER = 0
582 CONST = 1
583 INTERMEDIATE = 2
584 RESULT = 3
585
586
587class TosaSerializer:
588 def __init__(self, pathPrefix):
Eric Kunzeae906de2022-05-30 22:40:47 -0700589 self.add_compat_methods()
Kevin Chengfea5a372021-10-11 18:38:47 +0000590 # Get the global TOSA version if not already defined
Kevin Chengfea5a372021-10-11 18:38:47 +0000591
592 self.builder = flatbuffers.Builder(0)
593
594 self.basicBlocks = []
595 self.startBasicBlock("main")
596 self.pathPrefix = pathPrefix
597
598 # Indicies used for adding/naming tensors
599 self.currInputIdx = 0
600 self.currConstIdx = 0
601 self.currLayerIdx = 1
602 self.currResultIdx = 0
603
604 # Is this an illegal test that is expected to fail?
Jeremy Johnson9b225172021-12-14 16:34:47 +0000605 self.expectedReturnCode = 0
Kevin Chengfea5a372021-10-11 18:38:47 +0000606 self.expectedFailure = False
607 self.expectedFailureDesc = ""
608
609 def __str__(self):
610 str = ""
611 for bb in self.basicBlocks:
612 str = str + bb.__str__()
613 return str
614
615 def addPlaceholder(self, shape, dtype, vals):
616 if not self.currBasicBlock:
617 raise Exception("addTensor called without valid basic block")
618
619 name = "input-{}".format(self.currInputIdx)
620 filename = "{}.npy".format(name)
621 self.currInputIdx = self.currInputIdx + 1
622
623 tens = self.currBasicBlock.addTensor(name, shape, dtype, None, filename)
624 # This is always an input to the block
625 self.currBasicBlock.addInput(name)
626
627 if vals is not None:
628 np.save(os.path.join(self.pathPrefix, filename), vals, False)
629
630 return tens
631
632 def addConst(self, shape, dtype, vals):
633 if not self.currBasicBlock:
634 raise Exception("addTensor called without valid basic block")
635
636 name = "const-{}".format(self.currInputIdx)
Kevin Chengfea5a372021-10-11 18:38:47 +0000637 self.currInputIdx = self.currInputIdx + 1
638
639 tens = self.currBasicBlock.addTensor(name, shape, dtype, vals)
640 # Add the operator now
Jeremy Johnson9b225172021-12-14 16:34:47 +0000641 self.currBasicBlock.addOperator(TosaOp.Op().CONST, [], name)
Kevin Chengfea5a372021-10-11 18:38:47 +0000642
643 return tens
644
645 def addIntermediate(self, shape, dtype):
646
647 if not self.currBasicBlock:
648 raise Exception("addTensor called without valid basic block")
649
650 name = "layer-{}".format(self.currLayerIdx)
651 self.currLayerIdx = self.currLayerIdx + 1
652
653 tens = self.currBasicBlock.addTensor(name, shape, dtype, None)
654
655 return tens
656
657 def addInputTensor(self, tensor):
658 self.currBasicBlock.addTensor(tensor.name, tensor.shape, tensor.dtype)
659 self.currBasicBlock.addInput(tensor.name)
660
661 def addOutputTensor(self, tensor):
662 self.currBasicBlock.addOutput(tensor.name)
663
664 def addOutput(self, shape, dtype):
665 if not self.currBasicBlock:
666 raise Exception("addTensor called without valid basic block")
667
668 name = "result-{}".format(self.currResultIdx)
669 self.currResultIdx = self.currResultIdx + 1
670
671 tens = self.currBasicBlock.addTensor(name, shape, dtype, None)
672 self.currBasicBlock.addOutput(name)
673 return tens
674
675 def addOperator(self, op, inputs, outputs, attributes=None, quant_info=None):
676
Jeremy Johnson9b225172021-12-14 16:34:47 +0000677 if op == TosaOp.Op().CONST:
Kevin Chengfea5a372021-10-11 18:38:47 +0000678 raise Exception("Use addConstTensor() to add CONST ops")
679
680 return self.currBasicBlock.addOperator(
681 op, inputs, outputs, attributes, quant_info
682 )
683
Jeremy Johnson9b225172021-12-14 16:34:47 +0000684 def setExpectedReturnCode(self, val, fail, desc=""):
Kevin Chengfea5a372021-10-11 18:38:47 +0000685
686 self.expectedReturnCode = val
687 self.expectedFailureDesc = desc
Jeremy Johnson9b225172021-12-14 16:34:47 +0000688 self.expectedFailure = fail
Kevin Chengfea5a372021-10-11 18:38:47 +0000689
690 def serialize(self):
691
692 builder = self.builder
693
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800694 Version.Start(builder)
695 Version.Add_major(builder, TOSA_VERSION[0])
696 Version.Add_minor(builder, TOSA_VERSION[1])
697 Version.Add_patch(builder, TOSA_VERSION[2])
698 Version.Add_draft(builder, TOSA_VERSION[3])
699 version = Version.End(builder)
Kevin Chengfea5a372021-10-11 18:38:47 +0000700
701 fbv_bb = TosaSerializer.serializeObjVec(
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800702 builder, self.basicBlocks, TosaGraph.StartBlocksVector
Kevin Chengfea5a372021-10-11 18:38:47 +0000703 )
704
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800705 TosaGraph.Start(builder)
706 TosaGraph.AddVersion(builder, version)
707 TosaGraph.AddBlocks(builder, fbv_bb)
708 graph = TosaGraph.End(builder)
Kevin Chengfea5a372021-10-11 18:38:47 +0000709
Eric Kunzee6596402022-06-09 21:27:36 +0000710 self.builder.Finish(graph, TOSA_GRAPH_IDENTIFIER)
Kevin Chengfea5a372021-10-11 18:38:47 +0000711 return self.builder.Output()
712
713 def writeJson(self, tosa_filename):
714 """Write a json test file so that it is fairly easy to pick up the test
715 and generate commands for third party tool"""
716 test_desc = dict()
717
718 test_desc["tosa_file"] = tosa_filename
719 ifm_name = []
720 ifm_file = []
721 ofm_name = []
722 ofm_file = []
723
724 for b in self.basicBlocks:
725 if b.name == "main":
726 for i in b.inputs:
727 ifm_name.append(i)
728 ifm_file.append(b.tensors[i].placeholderFilename)
729 for o in b.outputs:
730 ofm_name.append(o)
Jeremy Johnson9b225172021-12-14 16:34:47 +0000731 # Make up an OFM filename here. One isn't generated until the
732 # reference tool is run, so any name is a good name
Kevin Chengfea5a372021-10-11 18:38:47 +0000733 ofm_file.append("ref-{}.npy".format(o))
734
735 test_desc["ifm_name"] = ifm_name
736 test_desc["ifm_file"] = ifm_file
737 test_desc["ofm_name"] = ofm_name
738 test_desc["ofm_file"] = ofm_file
739 test_desc["expected_return_code"] = self.expectedReturnCode
740 test_desc["expected_failure"] = self.expectedFailure
741 if self.expectedFailureDesc:
742 test_desc["expected_failure_desc"] = self.expectedFailureDesc
743
744 return json.dumps(test_desc, indent=" ")
745
746 def startBasicBlock(self, name):
747 self.currBasicBlock = TosaSerializerBasicBlock(name)
748 self.basicBlocks.append(self.currBasicBlock)
749
750 @staticmethod
751 def serializeStrVec(builder, vec, start_fcn):
752 fb_strs = [builder.CreateString(i) for i in vec]
753 start_fcn(builder, len(fb_strs))
754 for s in fb_strs[::-1]:
755 builder.PrependUOffsetTRelative(s)
Eric Kunzeae906de2022-05-30 22:40:47 -0700756 try:
757 return builder.EndVector()
758 except TypeError:
759 return builder.EndVector(len(vec))
Kevin Chengfea5a372021-10-11 18:38:47 +0000760
761 @staticmethod
762 def serializeUint8Vec(builder, vec):
763 builder.StartVector(1, len(vec), 8)
764 for v in vec[::-1]:
765 builder.PrependUint8(v)
766 try:
767 return builder.EndVector()
768 except TypeError:
769 return builder.EndVector(len(vec))
770
771 @staticmethod
772 def serializeInt32Vec(builder, vec):
773 builder.StartVector(4, len(vec), 4)
774 for v in vec[::-1]:
775 builder.PrependInt32(v)
776 try:
777 return builder.EndVector()
778 except TypeError:
779 return builder.EndVector(len(vec))
780
781 @staticmethod
782 def serializeFpVec(builder, vec):
783 builder.StartVector(4, len(vec), 4)
784 for v in vec[::-1]:
785 builder.PrependFloat32(v)
786 try:
787 return builder.EndVector()
788 except TypeError:
789 return builder.EndVector(len(vec))
790
791 @staticmethod
792 def serializeObjVec(builder, vec, start_fcn):
793 serialized_vec = []
794 for v in vec[::-1]:
795 serialized_vec.append(v.serialize(builder))
796
797 start_fcn(builder, len(vec))
798 for v in serialized_vec:
799 builder.PrependUOffsetTRelative(v)
800 try:
801 return builder.EndVector()
802 except TypeError:
803 return builder.EndVector(len(vec))
804
805 @staticmethod
806 def toList(val):
807 if isinstance(val, list):
808 return val
809 else:
810 return [val]
Eric Kunzeae906de2022-05-30 22:40:47 -0700811
812 # Remove when switching to flatbuffers 2.0
813 # contains a mapping of the deprecated 1.12 method to the 2.0 version
814
815 def add_compat_methods(self):
816
817 from tosa import ArithmeticRightShiftAttribute
818
819 if not hasattr(ArithmeticRightShiftAttribute, "Start"):
820 ArithmeticRightShiftAttribute.Start = (
821 ArithmeticRightShiftAttribute.ArithmeticRightShiftAttributeStart
822 )
823 ArithmeticRightShiftAttribute.AddRound = (
824 ArithmeticRightShiftAttribute.ArithmeticRightShiftAttributeAddRound
825 )
826 ArithmeticRightShiftAttribute.End = (
827 ArithmeticRightShiftAttribute.ArithmeticRightShiftAttributeEnd
828 )
829 from tosa import AxisAttribute
830
831 if not hasattr(AxisAttribute, "Start"):
832 AxisAttribute.Start = AxisAttribute.AxisAttributeStart
833 AxisAttribute.AddAxis = AxisAttribute.AxisAttributeAddAxis
834 AxisAttribute.End = AxisAttribute.AxisAttributeEnd
835 from tosa import ClampAttribute
836
837 if not hasattr(ClampAttribute, "Start"):
838 ClampAttribute.Start = ClampAttribute.ClampAttributeStart
839 ClampAttribute.AddMinInt = ClampAttribute.ClampAttributeAddMinInt
840 ClampAttribute.AddMaxInt = ClampAttribute.ClampAttributeAddMaxInt
841 ClampAttribute.AddMinFp = ClampAttribute.ClampAttributeAddMinFp
842 ClampAttribute.AddMaxFp = ClampAttribute.ClampAttributeAddMaxFp
843 ClampAttribute.End = ClampAttribute.ClampAttributeEnd
844 from tosa import CondIfAttribute
845
846 if not hasattr(CondIfAttribute, "Start"):
847 CondIfAttribute.Start = CondIfAttribute.CondIfAttributeStart
848 CondIfAttribute.AddThenBranch = CondIfAttribute.CondIfAttributeAddThenBranch
849 CondIfAttribute.AddElseBranch = CondIfAttribute.CondIfAttributeAddElseBranch
850 CondIfAttribute.End = CondIfAttribute.CondIfAttributeEnd
851 from tosa import ConvAttribute
852
853 if not hasattr(ConvAttribute, "Start"):
854 ConvAttribute.Start = ConvAttribute.ConvAttributeStart
855 ConvAttribute.AddPad = ConvAttribute.ConvAttributeAddPad
856 ConvAttribute.StartPadVector = ConvAttribute.ConvAttributeStartPadVector
857 ConvAttribute.AddStride = ConvAttribute.ConvAttributeAddStride
858 ConvAttribute.StartStrideVector = (
859 ConvAttribute.ConvAttributeStartStrideVector
860 )
861 ConvAttribute.AddDilation = ConvAttribute.ConvAttributeAddDilation
862 ConvAttribute.StartDilationVector = (
863 ConvAttribute.ConvAttributeStartDilationVector
864 )
865 ConvAttribute.End = ConvAttribute.ConvAttributeEnd
866 from tosa import ConvQuantInfo
867
868 if not hasattr(ConvQuantInfo, "Start"):
869 ConvQuantInfo.Start = ConvQuantInfo.ConvQuantInfoStart
870 ConvQuantInfo.AddInputZp = ConvQuantInfo.ConvQuantInfoAddInputZp
871 ConvQuantInfo.AddWeightZp = ConvQuantInfo.ConvQuantInfoAddWeightZp
872 ConvQuantInfo.End = ConvQuantInfo.ConvQuantInfoEnd
873 from tosa import MatMulQuantInfo
874
875 if not hasattr(MatMulQuantInfo, "Start"):
876 MatMulQuantInfo.Start = MatMulQuantInfo.MatMulQuantInfoStart
877 MatMulQuantInfo.AddAZp = MatMulQuantInfo.MatMulQuantInfoAddAZp
878 MatMulQuantInfo.AddBZp = MatMulQuantInfo.MatMulQuantInfoAddBZp
879 MatMulQuantInfo.End = MatMulQuantInfo.MatMulQuantInfoEnd
880 from tosa import MulAttribute
881
882 if not hasattr(MulAttribute, "Start"):
883 MulAttribute.Start = MulAttribute.MulAttributeStart
884 MulAttribute.AddShift = MulAttribute.MulAttributeAddShift
885 MulAttribute.End = MulAttribute.MulAttributeEnd
886 from tosa import PadAttribute
887
888 if not hasattr(PadAttribute, "Start"):
889 PadAttribute.Start = PadAttribute.PadAttributeStart
890 PadAttribute.AddPadding = PadAttribute.PadAttributeAddPadding
891 PadAttribute.StartPaddingVector = (
892 PadAttribute.PadAttributeStartPaddingVector
893 )
894 PadAttribute.AddPadConstInt = PadAttribute.PadAttributeAddPadConstInt
895 PadAttribute.AddPadConstFp = PadAttribute.PadAttributeAddPadConstFp
896 PadAttribute.End = PadAttribute.PadAttributeEnd
897 from tosa import PadQuantInfo
898
899 if not hasattr(PadQuantInfo, "Start"):
900 PadQuantInfo.Start = PadQuantInfo.PadQuantInfoStart
901 PadQuantInfo.AddInputZp = PadQuantInfo.PadQuantInfoAddInputZp
902 PadQuantInfo.End = PadQuantInfo.PadQuantInfoEnd
903 from tosa import PoolAttribute
904
905 if not hasattr(PoolAttribute, "Start"):
906 PoolAttribute.Start = PoolAttribute.PoolAttributeStart
907 PoolAttribute.AddPad = PoolAttribute.PoolAttributeAddPad
908 PoolAttribute.StartPadVector = PoolAttribute.PoolAttributeStartPadVector
909 PoolAttribute.AddKernel = PoolAttribute.PoolAttributeAddKernel
910 PoolAttribute.StartKernelVector = (
911 PoolAttribute.PoolAttributeStartKernelVector
912 )
913 PoolAttribute.AddStride = PoolAttribute.PoolAttributeAddStride
914 PoolAttribute.StartStrideVector = (
915 PoolAttribute.PoolAttributeStartStrideVector
916 )
917 PoolAttribute.End = PoolAttribute.PoolAttributeEnd
918 from tosa import RescaleAttribute
919
920 if not hasattr(RescaleAttribute, "Start"):
921 RescaleAttribute.Start = RescaleAttribute.RescaleAttributeStart
922 RescaleAttribute.AddInputZp = RescaleAttribute.RescaleAttributeAddInputZp
923 RescaleAttribute.AddOutputZp = RescaleAttribute.RescaleAttributeAddOutputZp
924 RescaleAttribute.AddMultiplier = (
925 RescaleAttribute.RescaleAttributeAddMultiplier
926 )
927 RescaleAttribute.StartMultiplierVector = (
928 RescaleAttribute.RescaleAttributeStartMultiplierVector
929 )
930 RescaleAttribute.AddShift = RescaleAttribute.RescaleAttributeAddShift
931 RescaleAttribute.StartShiftVector = (
932 RescaleAttribute.RescaleAttributeStartShiftVector
933 )
934 RescaleAttribute.AddScale32 = RescaleAttribute.RescaleAttributeAddScale32
935 RescaleAttribute.AddDoubleRound = (
936 RescaleAttribute.RescaleAttributeAddDoubleRound
937 )
938 RescaleAttribute.AddPerChannel = (
939 RescaleAttribute.RescaleAttributeAddPerChannel
940 )
941 RescaleAttribute.End = RescaleAttribute.RescaleAttributeEnd
942 from tosa import ReshapeAttribute
943
944 if not hasattr(ReshapeAttribute, "Start"):
945 ReshapeAttribute.Start = ReshapeAttribute.ReshapeAttributeStart
946 ReshapeAttribute.AddNewShape = ReshapeAttribute.ReshapeAttributeAddNewShape
947 ReshapeAttribute.StartNewShapeVector = (
948 ReshapeAttribute.ReshapeAttributeStartNewShapeVector
949 )
950 ReshapeAttribute.End = ReshapeAttribute.ReshapeAttributeEnd
951 from tosa import ResizeAttribute
952
953 if not hasattr(ResizeAttribute, "Start"):
954 ResizeAttribute.Start = ResizeAttribute.ResizeAttributeStart
955 ResizeAttribute.AddOutputSize = ResizeAttribute.ResizeAttributeAddOutputSize
956 ResizeAttribute.StartOutputSizeVector = (
957 ResizeAttribute.ResizeAttributeStartOutputSizeVector
958 )
959 ResizeAttribute.AddStride = ResizeAttribute.ResizeAttributeAddStride
960 ResizeAttribute.StartStrideVector = (
961 ResizeAttribute.ResizeAttributeStartStrideVector
962 )
963 ResizeAttribute.AddOffset = ResizeAttribute.ResizeAttributeAddOffset
964 ResizeAttribute.StartOffsetVector = (
965 ResizeAttribute.ResizeAttributeStartOffsetVector
966 )
967 ResizeAttribute.AddShift = ResizeAttribute.ResizeAttributeAddShift
968 ResizeAttribute.AddStrideFp = ResizeAttribute.ResizeAttributeAddStrideFp
969 ResizeAttribute.StartStrideFpVector = (
970 ResizeAttribute.ResizeAttributeStartStrideFpVector
971 )
972 ResizeAttribute.AddOffsetFp = ResizeAttribute.ResizeAttributeAddOffsetFp
973 ResizeAttribute.StartOffsetFpVector = (
974 ResizeAttribute.ResizeAttributeStartOffsetFpVector
975 )
976 ResizeAttribute.AddMode = ResizeAttribute.ResizeAttributeAddMode
977 ResizeAttribute.End = ResizeAttribute.ResizeAttributeEnd
978 from tosa import SliceAttribute
979
980 if not hasattr(SliceAttribute, "Start"):
981 SliceAttribute.Start = SliceAttribute.SliceAttributeStart
982 SliceAttribute.AddStart = SliceAttribute.SliceAttributeAddStart
983 SliceAttribute.StartStartVector = (
984 SliceAttribute.SliceAttributeStartStartVector
985 )
986 SliceAttribute.AddSize = SliceAttribute.SliceAttributeAddSize
987 SliceAttribute.StartSizeVector = (
988 SliceAttribute.SliceAttributeStartSizeVector
989 )
990 SliceAttribute.End = SliceAttribute.SliceAttributeEnd
991 from tosa import TableAttribute
992
993 if not hasattr(TableAttribute, "Start"):
994 TableAttribute.Start = TableAttribute.TableAttributeStart
995 TableAttribute.AddTable = TableAttribute.TableAttributeAddTable
996 TableAttribute.StartTableVector = (
997 TableAttribute.TableAttributeStartTableVector
998 )
999 TableAttribute.End = TableAttribute.TableAttributeEnd
1000 from tosa import TileAttribute
1001
1002 if not hasattr(TileAttribute, "Start"):
1003 TileAttribute.Start = TileAttribute.TileAttributeStart
1004 TileAttribute.AddMultiples = TileAttribute.TileAttributeAddMultiples
1005 TileAttribute.StartMultiplesVector = (
1006 TileAttribute.TileAttributeStartMultiplesVector
1007 )
1008 TileAttribute.End = TileAttribute.TileAttributeEnd
1009 from tosa import TosaBasicBlock
1010
1011 if not hasattr(TosaBasicBlock, "Start"):
1012 TosaBasicBlock.Start = TosaBasicBlock.TosaBasicBlockStart
1013 TosaBasicBlock.AddName = TosaBasicBlock.TosaBasicBlockAddName
1014 TosaBasicBlock.AddOperators = TosaBasicBlock.TosaBasicBlockAddOperators
1015 TosaBasicBlock.StartOperatorsVector = (
1016 TosaBasicBlock.TosaBasicBlockStartOperatorsVector
1017 )
1018 TosaBasicBlock.AddTensors = TosaBasicBlock.TosaBasicBlockAddTensors
1019 TosaBasicBlock.StartTensorsVector = (
1020 TosaBasicBlock.TosaBasicBlockStartTensorsVector
1021 )
1022 TosaBasicBlock.AddInputs = TosaBasicBlock.TosaBasicBlockAddInputs
1023 TosaBasicBlock.StartInputsVector = (
1024 TosaBasicBlock.TosaBasicBlockStartInputsVector
1025 )
1026 TosaBasicBlock.AddOutputs = TosaBasicBlock.TosaBasicBlockAddOutputs
1027 TosaBasicBlock.StartOutputsVector = (
1028 TosaBasicBlock.TosaBasicBlockStartOutputsVector
1029 )
1030 TosaBasicBlock.End = TosaBasicBlock.TosaBasicBlockEnd
1031 from tosa import TosaGraph
1032
1033 if not hasattr(TosaGraph, "Start"):
1034 TosaGraph.Start = TosaGraph.TosaGraphStart
1035 TosaGraph.AddVersion = TosaGraph.TosaGraphAddVersion
1036 TosaGraph.AddBlocks = TosaGraph.TosaGraphAddBlocks
1037 TosaGraph.StartBlocksVector = TosaGraph.TosaGraphStartBlocksVector
1038 TosaGraph.End = TosaGraph.TosaGraphEnd
1039 from tosa import TosaOperator
1040
1041 if not hasattr(TosaOperator, "Start"):
1042 TosaOperator.Start = TosaOperator.TosaOperatorStart
1043 TosaOperator.AddOp = TosaOperator.TosaOperatorAddOp
1044 TosaOperator.AddAttributeType = TosaOperator.TosaOperatorAddAttributeType
1045 TosaOperator.AddAttribute = TosaOperator.TosaOperatorAddAttribute
1046 TosaOperator.AddInputs = TosaOperator.TosaOperatorAddInputs
1047 TosaOperator.StartInputsVector = TosaOperator.TosaOperatorStartInputsVector
1048 TosaOperator.AddOutputs = TosaOperator.TosaOperatorAddOutputs
1049 TosaOperator.StartOutputsVector = (
1050 TosaOperator.TosaOperatorStartOutputsVector
1051 )
1052 TosaOperator.AddQuantInfoType = TosaOperator.TosaOperatorAddQuantInfoType
1053 TosaOperator.AddQuantInfo = TosaOperator.TosaOperatorAddQuantInfo
1054 TosaOperator.End = TosaOperator.TosaOperatorEnd
1055 from tosa import TosaTensor
1056
1057 if not hasattr(TosaTensor, "Start"):
1058 TosaTensor.Start = TosaTensor.TosaTensorStart
1059 TosaTensor.AddName = TosaTensor.TosaTensorAddName
1060 TosaTensor.AddShape = TosaTensor.TosaTensorAddShape
1061 TosaTensor.StartShapeVector = TosaTensor.TosaTensorStartShapeVector
1062 TosaTensor.AddType = TosaTensor.TosaTensorAddType
1063 TosaTensor.AddData = TosaTensor.TosaTensorAddData
1064 TosaTensor.StartDataVector = TosaTensor.TosaTensorStartDataVector
1065 TosaTensor.End = TosaTensor.TosaTensorEnd
1066 from tosa import TransposeAttribute
1067
1068 if not hasattr(TransposeAttribute, "Start"):
1069 TransposeAttribute.Start = TransposeAttribute.TransposeAttributeStart
1070 TransposeAttribute.AddPerms = TransposeAttribute.TransposeAttributeAddPerms
1071 TransposeAttribute.StartPermsVector = (
1072 TransposeAttribute.TransposeAttributeStartPermsVector
1073 )
1074 TransposeAttribute.End = TransposeAttribute.TransposeAttributeEnd
1075 from tosa import TransposeConvAttribute
1076
1077 if not hasattr(TransposeConvAttribute, "Start"):
1078 TransposeConvAttribute.Start = (
1079 TransposeConvAttribute.TransposeConvAttributeStart
1080 )
1081 TransposeConvAttribute.AddOutpad = (
1082 TransposeConvAttribute.TransposeConvAttributeAddOutpad
1083 )
1084 TransposeConvAttribute.StartOutpadVector = (
1085 TransposeConvAttribute.TransposeConvAttributeStartOutpadVector
1086 )
1087 TransposeConvAttribute.AddStride = (
1088 TransposeConvAttribute.TransposeConvAttributeAddStride
1089 )
1090 TransposeConvAttribute.StartStrideVector = (
1091 TransposeConvAttribute.TransposeConvAttributeStartStrideVector
1092 )
1093 TransposeConvAttribute.AddDilation = (
1094 TransposeConvAttribute.TransposeConvAttributeAddDilation
1095 )
1096 TransposeConvAttribute.StartDilationVector = (
1097 TransposeConvAttribute.TransposeConvAttributeStartDilationVector
1098 )
1099 TransposeConvAttribute.AddOutputShape = (
1100 TransposeConvAttribute.TransposeConvAttributeAddOutputShape
1101 )
1102 TransposeConvAttribute.StartOutputShapeVector = (
1103 TransposeConvAttribute.TransposeConvAttributeStartOutputShapeVector
1104 )
1105 TransposeConvAttribute.End = (
1106 TransposeConvAttribute.TransposeConvAttributeEnd
1107 )
1108 from tosa import UnaryQuantInfo
1109
1110 if not hasattr(UnaryQuantInfo, "Start"):
1111 UnaryQuantInfo.Start = UnaryQuantInfo.UnaryQuantInfoStart
1112 UnaryQuantInfo.AddInputZp = UnaryQuantInfo.UnaryQuantInfoAddInputZp
1113 UnaryQuantInfo.AddOutputZp = UnaryQuantInfo.UnaryQuantInfoAddOutputZp
1114 UnaryQuantInfo.End = UnaryQuantInfo.UnaryQuantInfoEnd
1115 from tosa import Version
1116
1117 if not hasattr(Version, "Start"):
1118 Version.Start = Version.VersionStart
1119 Version.Add_major = Version.VersionAdd_major
1120 Version.Add_minor = Version.VersionAdd_minor
1121 Version.Add_patch = Version.VersionAdd_patch
1122 Version.Add_draft = Version.VersionAdd_draft
1123 Version.End = Version.VersionEnd
1124 from tosa import WhileLoopAttribute
1125
1126 if not hasattr(WhileLoopAttribute, "Start"):
1127 WhileLoopAttribute.Start = WhileLoopAttribute.WhileLoopAttributeStart
1128 WhileLoopAttribute.AddCondBranch = (
1129 WhileLoopAttribute.WhileLoopAttributeAddCondBranch
1130 )
1131 WhileLoopAttribute.AddBodyBranch = (
1132 WhileLoopAttribute.WhileLoopAttributeAddBodyBranch
1133 )
1134 WhileLoopAttribute.End = WhileLoopAttribute.WhileLoopAttributeEnd