blob: 7b5cd1ab1f1d69d3daad73d911844a58cd66da19 [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
Eric Kunze7ffa1ff2022-06-01 17:26:48 -0700164 def TransposeConvAttribute(self, outpad, stride, output_shape):
Kevin Chengfea5a372021-10-11 18:38:47 +0000165 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
Eric Kunze4c3537d2022-06-13 17:21:48 -0700170 self.intvecs.append((a.AddOutPad, outpad))
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800171 self.intvecs.append((a.AddStride, stride))
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800172 self.intvecs.append((a.AddOutputShape, output_shape))
Kevin Chengfea5a372021-10-11 18:38:47 +0000173
Kevin Cheng38d214c2021-10-15 15:49:19 -0700174 def PadAttribute(self, padding, pad_const_int, pad_const_fp):
175 from tosa import PadAttribute as a, Attribute
Kevin Chengfea5a372021-10-11 18:38:47 +0000176
Kevin Cheng38d214c2021-10-15 15:49:19 -0700177 self.utype = Attribute.Attribute().PadAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800178 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000179
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800180 self.intvecs.append((a.AddPadding, padding))
181 self.ints.append((a.AddPadConstInt, pad_const_int))
182 self.floats.append((a.AddPadConstFp, pad_const_fp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000183
184 def AxisAttribute(self, axis):
185 from tosa import AxisAttribute as a, Attribute
186
187 self.utype = Attribute.Attribute().AxisAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800188 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000189
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800190 self.ints.append((a.AddAxis, axis))
Kevin Chengfea5a372021-10-11 18:38:47 +0000191
TatWai Chong7be71652022-05-10 17:26:20 -0700192 def ReshapeAttribute(self, new_shape):
Kevin Chengfea5a372021-10-11 18:38:47 +0000193 from tosa import ReshapeAttribute as a, Attribute
194
195 self.utype = Attribute.Attribute().ReshapeAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800196 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000197
TatWai Chong7be71652022-05-10 17:26:20 -0700198 self.intvecs.append((a.AddNewShape, new_shape))
Kevin Chengfea5a372021-10-11 18:38:47 +0000199
TatWai Chong7be71652022-05-10 17:26:20 -0700200 def SliceAttribute(self, start, size):
Kevin Chengfea5a372021-10-11 18:38:47 +0000201 from tosa import SliceAttribute as a, Attribute
202
203 self.utype = Attribute.Attribute().SliceAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800204 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000205
TatWai Chong7be71652022-05-10 17:26:20 -0700206 self.intvecs.append((a.AddStart, start))
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800207 self.intvecs.append((a.AddSize, size))
Kevin Chengfea5a372021-10-11 18:38:47 +0000208
209 def TileAttribute(self, multiples):
210 from tosa import TileAttribute as a, Attribute
211
212 self.utype = Attribute.Attribute().TileAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800213 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000214
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800215 self.intvecs.append((a.AddMultiples, multiples))
Kevin Chengfea5a372021-10-11 18:38:47 +0000216
217 def ResizeAttribute(
218 self, output_size, stride, offset, shift, stride_fp, offset_fp, mode
219 ):
220 from tosa import ResizeAttribute as a, Attribute
221
222 self.utype = Attribute.Attribute().ResizeAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800223 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000224
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800225 self.intvecs.append((a.AddOutputSize, output_size))
226 self.intvecs.append((a.AddStride, stride))
227 self.intvecs.append((a.AddOffset, offset))
228 self.ints.append((a.AddShift, shift))
229 self.fpvecs.append((a.AddStrideFp, stride_fp))
230 self.fpvecs.append((a.AddOffsetFp, offset_fp))
231 self.ints.append((a.AddMode, mode))
Kevin Chengfea5a372021-10-11 18:38:47 +0000232
233 def ClampAttribute(self, minint, maxint, minfp, maxfp):
234 from tosa import ClampAttribute as a, Attribute
235
236 self.utype = Attribute.Attribute().ClampAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800237 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000238
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800239 self.ints.append((a.AddMinInt, minint))
240 self.ints.append((a.AddMaxInt, maxint))
Kevin Chengfea5a372021-10-11 18:38:47 +0000241
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800242 self.ints.append((a.AddMinFp, minfp))
243 self.ints.append((a.AddMaxFp, maxfp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000244
245 def RescaleAttribute(
246 self, input_zp, output_zp, multiplier, shift, scale32, double_round, per_channel
247 ):
248 from tosa import RescaleAttribute as a, Attribute
249
250 self.utype = Attribute.Attribute().RescaleAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800251 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000252
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800253 self.ints.append((a.AddInputZp, input_zp))
254 self.ints.append((a.AddOutputZp, output_zp))
255 self.intvecs.append((a.AddMultiplier, multiplier))
256 self.intvecs.append((a.AddShift, shift))
257 self.bools.append((a.AddScale32, scale32))
258 self.bools.append((a.AddDoubleRound, double_round))
259 self.bools.append((a.AddPerChannel, per_channel))
Kevin Chengfea5a372021-10-11 18:38:47 +0000260
261 def MulAttribute(self, shift):
262 from tosa import MulAttribute as a, Attribute
263
264 self.utype = Attribute.Attribute().MulAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800265 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000266
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800267 self.ints.append((a.AddShift, shift))
Kevin Chengfea5a372021-10-11 18:38:47 +0000268
269 def ArithmeticRightShiftAttribute(self, round):
270 from tosa import ArithmeticRightShiftAttribute as a, Attribute
271
272 self.utype = Attribute.Attribute().ArithmeticRightShiftAttribute
273 self.optFcns = (
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800274 a.Start,
275 a.End,
Kevin Chengfea5a372021-10-11 18:38:47 +0000276 )
277
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800278 self.bools.append((a.AddRound, round))
Kevin Chengfea5a372021-10-11 18:38:47 +0000279
Kevin Chengfea5a372021-10-11 18:38:47 +0000280 def CondIfAttribute(self, then_branch, else_branch):
281 from tosa import CondIfAttribute as a, Attribute
282
283 self.utype = Attribute.Attribute().CondIfAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800284 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000285
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800286 self.strings.append((a.AddThenBranch, then_branch))
287 self.strings.append((a.AddElseBranch, else_branch))
Kevin Chengfea5a372021-10-11 18:38:47 +0000288
289 def WhileLoopAttribute(self, cond_branch, body_branch):
290 from tosa import WhileLoopAttribute as a, Attribute
291
292 self.utype = Attribute.Attribute().WhileLoopAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800293 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000294
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800295 self.strings.append((a.AddCondBranch, cond_branch))
296 self.strings.append((a.AddBodyBranch, body_branch))
Kevin Chengfea5a372021-10-11 18:38:47 +0000297
TatWai Chong7be71652022-05-10 17:26:20 -0700298 def TransposeAttribute(self, perms):
Kevin Cheng38d214c2021-10-15 15:49:19 -0700299 from tosa import TransposeAttribute as a, Attribute
300
301 self.utype = Attribute.Attribute().TransposeAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800302 self.optFcns = (a.Start, a.End)
Kevin Cheng38d214c2021-10-15 15:49:19 -0700303
TatWai Chong7be71652022-05-10 17:26:20 -0700304 self.intvecs.append((a.AddPerms, perms))
Kevin Cheng38d214c2021-10-15 15:49:19 -0700305
306 def TableAttribute(self, table):
307 from tosa import TableAttribute as a, Attribute
308
309 self.utype = Attribute.Attribute().TableAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800310 self.optFcns = (a.Start, a.End)
Kevin Cheng38d214c2021-10-15 15:49:19 -0700311
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800312 self.intvecs.append((a.AddTable, table))
Kevin Chengfea5a372021-10-11 18:38:47 +0000313
Jeremy Johnson9b225172021-12-14 16:34:47 +0000314
Kevin Chengfea5a372021-10-11 18:38:47 +0000315class TosaSerializerQuantInfo(TosaSerializerUnion):
Jeremy Johnson9b225172021-12-14 16:34:47 +0000316 """This class handles encapsulating all of the enumerated types for quantinfo"""
Kevin Chengfea5a372021-10-11 18:38:47 +0000317
318 def __init__(self):
319 super().__init__()
320
321 def ConvQuantInfo(self, input_zp, weight_zp):
322 from tosa import ConvQuantInfo as q, QuantInfo
323
324 self.utype = QuantInfo.QuantInfo().ConvQuantInfo
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800325 self.optFcns = (q.Start, q.End)
326 self.ints.append((q.AddInputZp, input_zp))
327 self.ints.append((q.AddWeightZp, weight_zp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000328
329 def UnaryQuantInfo(self, input_zp, output_zp):
330 from tosa import UnaryQuantInfo as q, QuantInfo
331
332 self.utype = QuantInfo.QuantInfo().UnaryQuantInfo
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800333 self.optFcns = (q.Start, q.End)
334 self.ints.append((q.AddInputZp, input_zp))
335 self.ints.append((q.AddOutputZp, output_zp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000336
337 def MatMulQuantInfo(self, a_zp, b_zp):
338 from tosa import MatMulQuantInfo as q, QuantInfo
339
340 self.utype = QuantInfo.QuantInfo().MatMulQuantInfo
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800341 self.optFcns = (q.Start, q.End)
342 self.ints.append((q.AddAZp, a_zp))
343 self.ints.append((q.AddBZp, b_zp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000344
345 def PadQuantInfo(self, input_zp):
346 from tosa import PadQuantInfo as q, QuantInfo
347
348 self.utype = QuantInfo.QuantInfo().PadQuantInfo
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800349 self.optFcns = (q.Start, q.End)
350 self.ints.append((q.AddInputZp, input_zp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000351
352
353class TosaSerializerTensor:
354 def __init__(
355 self,
356 name,
357 shape,
358 dtype,
359 data=None,
360 placeholderFilename=None,
361 ):
362 self.name = name
363
364 if isinstance(shape, np.ndarray):
365 shape = shape.astype(int).tolist()
366 shape = list(map(int, shape))
367
368 self.shape = shape
369 self.dtype = dtype
370
371 if isinstance(data, np.ndarray):
372 data = data.flatten().astype(int).tolist()
373 data = list(map(int, data))
374 self.data = data
375 elif isinstance(data, list):
376 data = list(map(int, data))
377 self.data = data
378 else:
379 self.data = None
380
381 # Filename for placeholder tensors. These get generated by the test generation
Jeremy Johnson9b225172021-12-14 16:34:47 +0000382 # process and are written to disk, but are considered input tensors by the
383 # network so they do not appear in the TOSA serialiazation. However, if we
384 # want to form a unit test around these input tensors, we can get the filename
385 # from here.
Kevin Chengfea5a372021-10-11 18:38:47 +0000386 self.placeholderFilename = placeholderFilename
387
388 def __str__(self):
389 str = "TosaSerializerTensor name: {} shape: {} dtype: {}".format(
390 self.name,
391 self.shape,
392 DTypeNames[self.dtype],
393 )
394 return str
395
396 def setDtype(self, dtype):
397 self.dtype = dtype
398
399 def serialize(self, builder):
400 fb_name = builder.CreateString(self.name)
401 fb_shapes = TosaSerializer.serializeInt32Vec(builder, self.shape)
402 if self.data:
403 u8_data = list()
404 # little endianess
405 if self.dtype == DType.BOOL:
406 for val in self.data:
407 val_u8 = np.uint8(val)
408 u8_data.append(val_u8)
409 elif self.dtype == DType.INT4:
410 in_size = len(self.data)
411 out_size = (in_size + 1) // 2
412 for i in range(out_size):
413 val_0 = self.data[2 * i]
414 if (2 * i + 1) < in_size:
415 val_1 = self.data[2 * i + 1]
416 else:
417 val_1 = 0
418 val_i8 = (val_0 & 0xF) | ((val_1 & 0xF) << 4)
419 val_u8 = np.uint8(val_i8)
420 u8_data.append(val_u8)
421 elif self.dtype == DType.INT8:
422 for val in self.data:
423 val_u8 = np.uint8(val)
424 u8_data.append(val_u8)
425 elif self.dtype == DType.INT16:
426 for val in self.data:
427 val_u16 = np.uint16(val)
428 b0 = val_u16 & ByteMask
429 b1 = (val_u16 >> np.uint16(8)) & ByteMask
430 u8_data.extend([b0, b1])
431 elif self.dtype == DType.INT32:
432 for val in self.data:
433 val_u32 = np.uint32(val)
434 b0 = val_u32 & ByteMask
435 b1 = (val_u32 >> np.uint32(8)) & ByteMask
436 b2 = (val_u32 >> np.uint32(16)) & ByteMask
Kevin Cheng6b078ca2021-10-13 23:12:50 -0700437 b3 = (val_u32 >> np.uint32(24)) & ByteMask
Kevin Chengfea5a372021-10-11 18:38:47 +0000438 u8_data.extend([b0, b1, b2, b3])
439 elif self.dtype == DType.INT48:
440 for val in self.data:
441 val_u64 = np.uint64(val)
442 b0 = val_u64 & ByteMask
443 b1 = (val_u64 >> np.uint64(8)) & ByteMask
444 b2 = (val_u64 >> np.uint64(16)) & ByteMask
445 b3 = (val_u64 >> np.uint64(24)) & ByteMask
446 b4 = (val_u64 >> np.uint64(32)) & ByteMask
447 b5 = (val_u64 >> np.uint64(40)) & ByteMask
448 u8_data.extend([b0, b1, b2, b3, b4, b5])
449 elif self.dtype == DType.FLOAT:
450 for val in self.data:
451 b = struct.pack("!f", val)
452 u8_data.extend([b[3], b[2], b[1], b[0]])
453 else:
454 raise Exception(
455 "unsupported data type {}".format(DTypeNames[self.dtype])
456 )
457 fb_data = TosaSerializer.serializeUint8Vec(builder, u8_data)
458
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800459 TosaTensor.Start(builder)
460 TosaTensor.AddName(builder, fb_name)
461 TosaTensor.AddShape(builder, fb_shapes)
462 TosaTensor.AddType(builder, self.dtype)
Kevin Chengfea5a372021-10-11 18:38:47 +0000463 if self.data:
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800464 TosaTensor.AddData(builder, fb_data)
Kevin Chengfea5a372021-10-11 18:38:47 +0000465
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800466 return TosaTensor.End(builder)
Kevin Chengfea5a372021-10-11 18:38:47 +0000467
468
469class TosaSerializerOperator:
470 def __init__(self, op, inputs, outputs, attributes=None, quantInfo=None):
471 self.op = op
472 self.attributes = attributes
473 self.inputs = TosaSerializer.toList(inputs)
474 self.outputs = TosaSerializer.toList(outputs)
475 self.quantInfo = quantInfo
476
477 def __str__(self):
478 str = "Op {}\n----\n".format(self.op)
479
480 for i in self.inputs:
481 str = str + " Input: {}\n".format(i)
482 for o in self.outputs:
483 str = str + " Output: {}\n".format(o)
484
485 return str
486
487 def serialize(self, builder):
488 fb_inputs = TosaSerializer.serializeStrVec(
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800489 builder, self.inputs, TosaOperator.StartInputsVector
Kevin Chengfea5a372021-10-11 18:38:47 +0000490 )
491 fb_outputs = TosaSerializer.serializeStrVec(
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800492 builder, self.outputs, TosaOperator.StartOutputsVector
Kevin Chengfea5a372021-10-11 18:38:47 +0000493 )
494 # Need to serialize quant_info and attributes enums still
495 if self.attributes is not None:
496 fb_attributes = self.attributes.serialize(builder)
497
498 if self.quantInfo is not None:
499 fb_qinfo = self.quantInfo.serialize(builder)
500
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800501 TosaOperator.Start(builder)
502 TosaOperator.AddOp(builder, self.op)
503 TosaOperator.AddInputs(builder, fb_inputs)
504 TosaOperator.AddOutputs(builder, fb_outputs)
Kevin Chengfea5a372021-10-11 18:38:47 +0000505 if self.attributes is not None:
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800506 TosaOperator.AddAttributeType(builder, self.attributes.utype)
507 TosaOperator.AddAttribute(builder, fb_attributes)
Kevin Chengfea5a372021-10-11 18:38:47 +0000508 if self.quantInfo is not None:
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800509 TosaOperator.AddQuantInfoType(builder, self.quantInfo.utype)
510 TosaOperator.AddQuantInfo(builder, fb_qinfo)
Kevin Chengfea5a372021-10-11 18:38:47 +0000511
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800512 return TosaOperator.End(builder)
Kevin Chengfea5a372021-10-11 18:38:47 +0000513
514
515class TosaSerializerBasicBlock:
516 def __init__(self, name):
517 self.name = name
518 self.operators = []
519
520 # Dict assures uniqueness, but allows us to look up by name
521 self.tensors = dict()
522
523 self.inputs = []
524 self.outputs = []
525
526 def addTensor(
527 self,
528 name,
529 shape,
530 dtype,
531 data=None,
532 placeholderFilename=None,
533 ):
Jeremy Johnson9b225172021-12-14 16:34:47 +0000534 if name not in self.tensors:
Kevin Chengfea5a372021-10-11 18:38:47 +0000535 self.tensors[name] = TosaSerializerTensor(
536 name, shape, dtype, data, placeholderFilename
537 )
538
539 return self.tensors[name]
540
541 def addInput(self, name):
542 self.inputs.append(name)
543
544 def addOutput(self, name):
545 self.outputs.append(name)
546
547 def addOperator(self, op, inputs, outputs, attributes=None, quant_info=None):
548 self.operators.append(
549 TosaSerializerOperator(op, inputs, outputs, attributes, quant_info)
550 )
551
552 def serialize(self, builder):
553 fb_name = builder.CreateString(self.name)
554 fbv_inputs = TosaSerializer.serializeStrVec(
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800555 builder, list(self.inputs), TosaBasicBlock.StartInputsVector
Kevin Chengfea5a372021-10-11 18:38:47 +0000556 )
557 fbv_outputs = TosaSerializer.serializeStrVec(
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800558 builder, list(self.outputs), TosaBasicBlock.StartOutputsVector
Kevin Chengfea5a372021-10-11 18:38:47 +0000559 )
560 fbv_tensors = TosaSerializer.serializeObjVec(
561 builder,
562 list(self.tensors.values()),
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800563 TosaBasicBlock.StartTensorsVector,
Kevin Chengfea5a372021-10-11 18:38:47 +0000564 )
565 fbv_operators = TosaSerializer.serializeObjVec(
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800566 builder, self.operators, TosaBasicBlock.StartOperatorsVector
Kevin Chengfea5a372021-10-11 18:38:47 +0000567 )
568
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800569 TosaBasicBlock.Start(builder)
570 TosaBasicBlock.AddName(builder, fb_name)
571 TosaBasicBlock.AddInputs(builder, fbv_inputs)
572 TosaBasicBlock.AddOutputs(builder, fbv_outputs)
573 TosaBasicBlock.AddTensors(builder, fbv_tensors)
574 TosaBasicBlock.AddOperators(builder, fbv_operators)
575 return TosaBasicBlock.End(builder)
Kevin Chengfea5a372021-10-11 18:38:47 +0000576
577
578@unique
579class TensorDir(IntEnum):
580 PLACEHOLDER = 0
581 CONST = 1
582 INTERMEDIATE = 2
583 RESULT = 3
584
585
586class TosaSerializer:
587 def __init__(self, pathPrefix):
Eric Kunzeae906de2022-05-30 22:40:47 -0700588 self.add_compat_methods()
Kevin Chengfea5a372021-10-11 18:38:47 +0000589 # Get the global TOSA version if not already defined
Kevin Chengfea5a372021-10-11 18:38:47 +0000590
591 self.builder = flatbuffers.Builder(0)
592
593 self.basicBlocks = []
594 self.startBasicBlock("main")
595 self.pathPrefix = pathPrefix
596
597 # Indicies used for adding/naming tensors
598 self.currInputIdx = 0
599 self.currConstIdx = 0
600 self.currLayerIdx = 1
601 self.currResultIdx = 0
602
603 # Is this an illegal test that is expected to fail?
Jeremy Johnson9b225172021-12-14 16:34:47 +0000604 self.expectedReturnCode = 0
Kevin Chengfea5a372021-10-11 18:38:47 +0000605 self.expectedFailure = False
606 self.expectedFailureDesc = ""
607
608 def __str__(self):
609 str = ""
610 for bb in self.basicBlocks:
611 str = str + bb.__str__()
612 return str
613
614 def addPlaceholder(self, shape, dtype, vals):
615 if not self.currBasicBlock:
616 raise Exception("addTensor called without valid basic block")
617
618 name = "input-{}".format(self.currInputIdx)
619 filename = "{}.npy".format(name)
620 self.currInputIdx = self.currInputIdx + 1
621
622 tens = self.currBasicBlock.addTensor(name, shape, dtype, None, filename)
623 # This is always an input to the block
624 self.currBasicBlock.addInput(name)
625
626 if vals is not None:
627 np.save(os.path.join(self.pathPrefix, filename), vals, False)
628
629 return tens
630
631 def addConst(self, shape, dtype, vals):
632 if not self.currBasicBlock:
633 raise Exception("addTensor called without valid basic block")
634
635 name = "const-{}".format(self.currInputIdx)
Kevin Chengfea5a372021-10-11 18:38:47 +0000636 self.currInputIdx = self.currInputIdx + 1
637
638 tens = self.currBasicBlock.addTensor(name, shape, dtype, vals)
639 # Add the operator now
Jeremy Johnson9b225172021-12-14 16:34:47 +0000640 self.currBasicBlock.addOperator(TosaOp.Op().CONST, [], name)
Kevin Chengfea5a372021-10-11 18:38:47 +0000641
642 return tens
643
644 def addIntermediate(self, shape, dtype):
645
646 if not self.currBasicBlock:
647 raise Exception("addTensor called without valid basic block")
648
649 name = "layer-{}".format(self.currLayerIdx)
650 self.currLayerIdx = self.currLayerIdx + 1
651
652 tens = self.currBasicBlock.addTensor(name, shape, dtype, None)
653
654 return tens
655
656 def addInputTensor(self, tensor):
657 self.currBasicBlock.addTensor(tensor.name, tensor.shape, tensor.dtype)
658 self.currBasicBlock.addInput(tensor.name)
659
660 def addOutputTensor(self, tensor):
661 self.currBasicBlock.addOutput(tensor.name)
662
663 def addOutput(self, shape, dtype):
664 if not self.currBasicBlock:
665 raise Exception("addTensor called without valid basic block")
666
667 name = "result-{}".format(self.currResultIdx)
668 self.currResultIdx = self.currResultIdx + 1
669
670 tens = self.currBasicBlock.addTensor(name, shape, dtype, None)
671 self.currBasicBlock.addOutput(name)
672 return tens
673
674 def addOperator(self, op, inputs, outputs, attributes=None, quant_info=None):
675
Jeremy Johnson9b225172021-12-14 16:34:47 +0000676 if op == TosaOp.Op().CONST:
Kevin Chengfea5a372021-10-11 18:38:47 +0000677 raise Exception("Use addConstTensor() to add CONST ops")
678
679 return self.currBasicBlock.addOperator(
680 op, inputs, outputs, attributes, quant_info
681 )
682
Jeremy Johnson9b225172021-12-14 16:34:47 +0000683 def setExpectedReturnCode(self, val, fail, desc=""):
Kevin Chengfea5a372021-10-11 18:38:47 +0000684
685 self.expectedReturnCode = val
686 self.expectedFailureDesc = desc
Jeremy Johnson9b225172021-12-14 16:34:47 +0000687 self.expectedFailure = fail
Kevin Chengfea5a372021-10-11 18:38:47 +0000688
689 def serialize(self):
690
691 builder = self.builder
692
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800693 Version.Start(builder)
694 Version.Add_major(builder, TOSA_VERSION[0])
695 Version.Add_minor(builder, TOSA_VERSION[1])
696 Version.Add_patch(builder, TOSA_VERSION[2])
697 Version.Add_draft(builder, TOSA_VERSION[3])
698 version = Version.End(builder)
Kevin Chengfea5a372021-10-11 18:38:47 +0000699
700 fbv_bb = TosaSerializer.serializeObjVec(
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800701 builder, self.basicBlocks, TosaGraph.StartBlocksVector
Kevin Chengfea5a372021-10-11 18:38:47 +0000702 )
703
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800704 TosaGraph.Start(builder)
705 TosaGraph.AddVersion(builder, version)
706 TosaGraph.AddBlocks(builder, fbv_bb)
707 graph = TosaGraph.End(builder)
Kevin Chengfea5a372021-10-11 18:38:47 +0000708
Eric Kunzee6596402022-06-09 21:27:36 +0000709 self.builder.Finish(graph, TOSA_GRAPH_IDENTIFIER)
Kevin Chengfea5a372021-10-11 18:38:47 +0000710 return self.builder.Output()
711
712 def writeJson(self, tosa_filename):
713 """Write a json test file so that it is fairly easy to pick up the test
714 and generate commands for third party tool"""
715 test_desc = dict()
716
717 test_desc["tosa_file"] = tosa_filename
718 ifm_name = []
719 ifm_file = []
720 ofm_name = []
721 ofm_file = []
722
723 for b in self.basicBlocks:
724 if b.name == "main":
725 for i in b.inputs:
726 ifm_name.append(i)
727 ifm_file.append(b.tensors[i].placeholderFilename)
728 for o in b.outputs:
729 ofm_name.append(o)
Jeremy Johnson9b225172021-12-14 16:34:47 +0000730 # Make up an OFM filename here. One isn't generated until the
731 # reference tool is run, so any name is a good name
Kevin Chengfea5a372021-10-11 18:38:47 +0000732 ofm_file.append("ref-{}.npy".format(o))
733
734 test_desc["ifm_name"] = ifm_name
735 test_desc["ifm_file"] = ifm_file
736 test_desc["ofm_name"] = ofm_name
737 test_desc["ofm_file"] = ofm_file
738 test_desc["expected_return_code"] = self.expectedReturnCode
739 test_desc["expected_failure"] = self.expectedFailure
740 if self.expectedFailureDesc:
741 test_desc["expected_failure_desc"] = self.expectedFailureDesc
742
743 return json.dumps(test_desc, indent=" ")
744
745 def startBasicBlock(self, name):
746 self.currBasicBlock = TosaSerializerBasicBlock(name)
747 self.basicBlocks.append(self.currBasicBlock)
748
749 @staticmethod
750 def serializeStrVec(builder, vec, start_fcn):
751 fb_strs = [builder.CreateString(i) for i in vec]
752 start_fcn(builder, len(fb_strs))
753 for s in fb_strs[::-1]:
754 builder.PrependUOffsetTRelative(s)
Eric Kunzeae906de2022-05-30 22:40:47 -0700755 try:
756 return builder.EndVector()
757 except TypeError:
758 return builder.EndVector(len(vec))
Kevin Chengfea5a372021-10-11 18:38:47 +0000759
760 @staticmethod
761 def serializeUint8Vec(builder, vec):
762 builder.StartVector(1, len(vec), 8)
763 for v in vec[::-1]:
764 builder.PrependUint8(v)
765 try:
766 return builder.EndVector()
767 except TypeError:
768 return builder.EndVector(len(vec))
769
770 @staticmethod
771 def serializeInt32Vec(builder, vec):
772 builder.StartVector(4, len(vec), 4)
773 for v in vec[::-1]:
774 builder.PrependInt32(v)
775 try:
776 return builder.EndVector()
777 except TypeError:
778 return builder.EndVector(len(vec))
779
780 @staticmethod
781 def serializeFpVec(builder, vec):
782 builder.StartVector(4, len(vec), 4)
783 for v in vec[::-1]:
784 builder.PrependFloat32(v)
785 try:
786 return builder.EndVector()
787 except TypeError:
788 return builder.EndVector(len(vec))
789
790 @staticmethod
791 def serializeObjVec(builder, vec, start_fcn):
792 serialized_vec = []
793 for v in vec[::-1]:
794 serialized_vec.append(v.serialize(builder))
795
796 start_fcn(builder, len(vec))
797 for v in serialized_vec:
798 builder.PrependUOffsetTRelative(v)
799 try:
800 return builder.EndVector()
801 except TypeError:
802 return builder.EndVector(len(vec))
803
804 @staticmethod
805 def toList(val):
806 if isinstance(val, list):
807 return val
808 else:
809 return [val]
Eric Kunzeae906de2022-05-30 22:40:47 -0700810
811 # Remove when switching to flatbuffers 2.0
812 # contains a mapping of the deprecated 1.12 method to the 2.0 version
813
814 def add_compat_methods(self):
815
816 from tosa import ArithmeticRightShiftAttribute
817
818 if not hasattr(ArithmeticRightShiftAttribute, "Start"):
819 ArithmeticRightShiftAttribute.Start = (
820 ArithmeticRightShiftAttribute.ArithmeticRightShiftAttributeStart
821 )
822 ArithmeticRightShiftAttribute.AddRound = (
823 ArithmeticRightShiftAttribute.ArithmeticRightShiftAttributeAddRound
824 )
825 ArithmeticRightShiftAttribute.End = (
826 ArithmeticRightShiftAttribute.ArithmeticRightShiftAttributeEnd
827 )
828 from tosa import AxisAttribute
829
830 if not hasattr(AxisAttribute, "Start"):
831 AxisAttribute.Start = AxisAttribute.AxisAttributeStart
832 AxisAttribute.AddAxis = AxisAttribute.AxisAttributeAddAxis
833 AxisAttribute.End = AxisAttribute.AxisAttributeEnd
834 from tosa import ClampAttribute
835
836 if not hasattr(ClampAttribute, "Start"):
837 ClampAttribute.Start = ClampAttribute.ClampAttributeStart
838 ClampAttribute.AddMinInt = ClampAttribute.ClampAttributeAddMinInt
839 ClampAttribute.AddMaxInt = ClampAttribute.ClampAttributeAddMaxInt
840 ClampAttribute.AddMinFp = ClampAttribute.ClampAttributeAddMinFp
841 ClampAttribute.AddMaxFp = ClampAttribute.ClampAttributeAddMaxFp
842 ClampAttribute.End = ClampAttribute.ClampAttributeEnd
843 from tosa import CondIfAttribute
844
845 if not hasattr(CondIfAttribute, "Start"):
846 CondIfAttribute.Start = CondIfAttribute.CondIfAttributeStart
847 CondIfAttribute.AddThenBranch = CondIfAttribute.CondIfAttributeAddThenBranch
848 CondIfAttribute.AddElseBranch = CondIfAttribute.CondIfAttributeAddElseBranch
849 CondIfAttribute.End = CondIfAttribute.CondIfAttributeEnd
850 from tosa import ConvAttribute
851
852 if not hasattr(ConvAttribute, "Start"):
853 ConvAttribute.Start = ConvAttribute.ConvAttributeStart
854 ConvAttribute.AddPad = ConvAttribute.ConvAttributeAddPad
855 ConvAttribute.StartPadVector = ConvAttribute.ConvAttributeStartPadVector
856 ConvAttribute.AddStride = ConvAttribute.ConvAttributeAddStride
857 ConvAttribute.StartStrideVector = (
858 ConvAttribute.ConvAttributeStartStrideVector
859 )
860 ConvAttribute.AddDilation = ConvAttribute.ConvAttributeAddDilation
861 ConvAttribute.StartDilationVector = (
862 ConvAttribute.ConvAttributeStartDilationVector
863 )
864 ConvAttribute.End = ConvAttribute.ConvAttributeEnd
865 from tosa import ConvQuantInfo
866
867 if not hasattr(ConvQuantInfo, "Start"):
868 ConvQuantInfo.Start = ConvQuantInfo.ConvQuantInfoStart
869 ConvQuantInfo.AddInputZp = ConvQuantInfo.ConvQuantInfoAddInputZp
870 ConvQuantInfo.AddWeightZp = ConvQuantInfo.ConvQuantInfoAddWeightZp
871 ConvQuantInfo.End = ConvQuantInfo.ConvQuantInfoEnd
872 from tosa import MatMulQuantInfo
873
874 if not hasattr(MatMulQuantInfo, "Start"):
875 MatMulQuantInfo.Start = MatMulQuantInfo.MatMulQuantInfoStart
876 MatMulQuantInfo.AddAZp = MatMulQuantInfo.MatMulQuantInfoAddAZp
877 MatMulQuantInfo.AddBZp = MatMulQuantInfo.MatMulQuantInfoAddBZp
878 MatMulQuantInfo.End = MatMulQuantInfo.MatMulQuantInfoEnd
879 from tosa import MulAttribute
880
881 if not hasattr(MulAttribute, "Start"):
882 MulAttribute.Start = MulAttribute.MulAttributeStart
883 MulAttribute.AddShift = MulAttribute.MulAttributeAddShift
884 MulAttribute.End = MulAttribute.MulAttributeEnd
885 from tosa import PadAttribute
886
887 if not hasattr(PadAttribute, "Start"):
888 PadAttribute.Start = PadAttribute.PadAttributeStart
889 PadAttribute.AddPadding = PadAttribute.PadAttributeAddPadding
890 PadAttribute.StartPaddingVector = (
891 PadAttribute.PadAttributeStartPaddingVector
892 )
893 PadAttribute.AddPadConstInt = PadAttribute.PadAttributeAddPadConstInt
894 PadAttribute.AddPadConstFp = PadAttribute.PadAttributeAddPadConstFp
895 PadAttribute.End = PadAttribute.PadAttributeEnd
896 from tosa import PadQuantInfo
897
898 if not hasattr(PadQuantInfo, "Start"):
899 PadQuantInfo.Start = PadQuantInfo.PadQuantInfoStart
900 PadQuantInfo.AddInputZp = PadQuantInfo.PadQuantInfoAddInputZp
901 PadQuantInfo.End = PadQuantInfo.PadQuantInfoEnd
902 from tosa import PoolAttribute
903
904 if not hasattr(PoolAttribute, "Start"):
905 PoolAttribute.Start = PoolAttribute.PoolAttributeStart
906 PoolAttribute.AddPad = PoolAttribute.PoolAttributeAddPad
907 PoolAttribute.StartPadVector = PoolAttribute.PoolAttributeStartPadVector
908 PoolAttribute.AddKernel = PoolAttribute.PoolAttributeAddKernel
909 PoolAttribute.StartKernelVector = (
910 PoolAttribute.PoolAttributeStartKernelVector
911 )
912 PoolAttribute.AddStride = PoolAttribute.PoolAttributeAddStride
913 PoolAttribute.StartStrideVector = (
914 PoolAttribute.PoolAttributeStartStrideVector
915 )
916 PoolAttribute.End = PoolAttribute.PoolAttributeEnd
917 from tosa import RescaleAttribute
918
919 if not hasattr(RescaleAttribute, "Start"):
920 RescaleAttribute.Start = RescaleAttribute.RescaleAttributeStart
921 RescaleAttribute.AddInputZp = RescaleAttribute.RescaleAttributeAddInputZp
922 RescaleAttribute.AddOutputZp = RescaleAttribute.RescaleAttributeAddOutputZp
923 RescaleAttribute.AddMultiplier = (
924 RescaleAttribute.RescaleAttributeAddMultiplier
925 )
926 RescaleAttribute.StartMultiplierVector = (
927 RescaleAttribute.RescaleAttributeStartMultiplierVector
928 )
929 RescaleAttribute.AddShift = RescaleAttribute.RescaleAttributeAddShift
930 RescaleAttribute.StartShiftVector = (
931 RescaleAttribute.RescaleAttributeStartShiftVector
932 )
933 RescaleAttribute.AddScale32 = RescaleAttribute.RescaleAttributeAddScale32
934 RescaleAttribute.AddDoubleRound = (
935 RescaleAttribute.RescaleAttributeAddDoubleRound
936 )
937 RescaleAttribute.AddPerChannel = (
938 RescaleAttribute.RescaleAttributeAddPerChannel
939 )
940 RescaleAttribute.End = RescaleAttribute.RescaleAttributeEnd
941 from tosa import ReshapeAttribute
942
943 if not hasattr(ReshapeAttribute, "Start"):
944 ReshapeAttribute.Start = ReshapeAttribute.ReshapeAttributeStart
945 ReshapeAttribute.AddNewShape = ReshapeAttribute.ReshapeAttributeAddNewShape
946 ReshapeAttribute.StartNewShapeVector = (
947 ReshapeAttribute.ReshapeAttributeStartNewShapeVector
948 )
949 ReshapeAttribute.End = ReshapeAttribute.ReshapeAttributeEnd
950 from tosa import ResizeAttribute
951
952 if not hasattr(ResizeAttribute, "Start"):
953 ResizeAttribute.Start = ResizeAttribute.ResizeAttributeStart
954 ResizeAttribute.AddOutputSize = ResizeAttribute.ResizeAttributeAddOutputSize
955 ResizeAttribute.StartOutputSizeVector = (
956 ResizeAttribute.ResizeAttributeStartOutputSizeVector
957 )
958 ResizeAttribute.AddStride = ResizeAttribute.ResizeAttributeAddStride
959 ResizeAttribute.StartStrideVector = (
960 ResizeAttribute.ResizeAttributeStartStrideVector
961 )
962 ResizeAttribute.AddOffset = ResizeAttribute.ResizeAttributeAddOffset
963 ResizeAttribute.StartOffsetVector = (
964 ResizeAttribute.ResizeAttributeStartOffsetVector
965 )
966 ResizeAttribute.AddShift = ResizeAttribute.ResizeAttributeAddShift
967 ResizeAttribute.AddStrideFp = ResizeAttribute.ResizeAttributeAddStrideFp
968 ResizeAttribute.StartStrideFpVector = (
969 ResizeAttribute.ResizeAttributeStartStrideFpVector
970 )
971 ResizeAttribute.AddOffsetFp = ResizeAttribute.ResizeAttributeAddOffsetFp
972 ResizeAttribute.StartOffsetFpVector = (
973 ResizeAttribute.ResizeAttributeStartOffsetFpVector
974 )
975 ResizeAttribute.AddMode = ResizeAttribute.ResizeAttributeAddMode
976 ResizeAttribute.End = ResizeAttribute.ResizeAttributeEnd
977 from tosa import SliceAttribute
978
979 if not hasattr(SliceAttribute, "Start"):
980 SliceAttribute.Start = SliceAttribute.SliceAttributeStart
981 SliceAttribute.AddStart = SliceAttribute.SliceAttributeAddStart
982 SliceAttribute.StartStartVector = (
983 SliceAttribute.SliceAttributeStartStartVector
984 )
985 SliceAttribute.AddSize = SliceAttribute.SliceAttributeAddSize
986 SliceAttribute.StartSizeVector = (
987 SliceAttribute.SliceAttributeStartSizeVector
988 )
989 SliceAttribute.End = SliceAttribute.SliceAttributeEnd
990 from tosa import TableAttribute
991
992 if not hasattr(TableAttribute, "Start"):
993 TableAttribute.Start = TableAttribute.TableAttributeStart
994 TableAttribute.AddTable = TableAttribute.TableAttributeAddTable
995 TableAttribute.StartTableVector = (
996 TableAttribute.TableAttributeStartTableVector
997 )
998 TableAttribute.End = TableAttribute.TableAttributeEnd
999 from tosa import TileAttribute
1000
1001 if not hasattr(TileAttribute, "Start"):
1002 TileAttribute.Start = TileAttribute.TileAttributeStart
1003 TileAttribute.AddMultiples = TileAttribute.TileAttributeAddMultiples
1004 TileAttribute.StartMultiplesVector = (
1005 TileAttribute.TileAttributeStartMultiplesVector
1006 )
1007 TileAttribute.End = TileAttribute.TileAttributeEnd
1008 from tosa import TosaBasicBlock
1009
1010 if not hasattr(TosaBasicBlock, "Start"):
1011 TosaBasicBlock.Start = TosaBasicBlock.TosaBasicBlockStart
1012 TosaBasicBlock.AddName = TosaBasicBlock.TosaBasicBlockAddName
1013 TosaBasicBlock.AddOperators = TosaBasicBlock.TosaBasicBlockAddOperators
1014 TosaBasicBlock.StartOperatorsVector = (
1015 TosaBasicBlock.TosaBasicBlockStartOperatorsVector
1016 )
1017 TosaBasicBlock.AddTensors = TosaBasicBlock.TosaBasicBlockAddTensors
1018 TosaBasicBlock.StartTensorsVector = (
1019 TosaBasicBlock.TosaBasicBlockStartTensorsVector
1020 )
1021 TosaBasicBlock.AddInputs = TosaBasicBlock.TosaBasicBlockAddInputs
1022 TosaBasicBlock.StartInputsVector = (
1023 TosaBasicBlock.TosaBasicBlockStartInputsVector
1024 )
1025 TosaBasicBlock.AddOutputs = TosaBasicBlock.TosaBasicBlockAddOutputs
1026 TosaBasicBlock.StartOutputsVector = (
1027 TosaBasicBlock.TosaBasicBlockStartOutputsVector
1028 )
1029 TosaBasicBlock.End = TosaBasicBlock.TosaBasicBlockEnd
1030 from tosa import TosaGraph
1031
1032 if not hasattr(TosaGraph, "Start"):
1033 TosaGraph.Start = TosaGraph.TosaGraphStart
1034 TosaGraph.AddVersion = TosaGraph.TosaGraphAddVersion
1035 TosaGraph.AddBlocks = TosaGraph.TosaGraphAddBlocks
1036 TosaGraph.StartBlocksVector = TosaGraph.TosaGraphStartBlocksVector
1037 TosaGraph.End = TosaGraph.TosaGraphEnd
1038 from tosa import TosaOperator
1039
1040 if not hasattr(TosaOperator, "Start"):
1041 TosaOperator.Start = TosaOperator.TosaOperatorStart
1042 TosaOperator.AddOp = TosaOperator.TosaOperatorAddOp
1043 TosaOperator.AddAttributeType = TosaOperator.TosaOperatorAddAttributeType
1044 TosaOperator.AddAttribute = TosaOperator.TosaOperatorAddAttribute
1045 TosaOperator.AddInputs = TosaOperator.TosaOperatorAddInputs
1046 TosaOperator.StartInputsVector = TosaOperator.TosaOperatorStartInputsVector
1047 TosaOperator.AddOutputs = TosaOperator.TosaOperatorAddOutputs
1048 TosaOperator.StartOutputsVector = (
1049 TosaOperator.TosaOperatorStartOutputsVector
1050 )
1051 TosaOperator.AddQuantInfoType = TosaOperator.TosaOperatorAddQuantInfoType
1052 TosaOperator.AddQuantInfo = TosaOperator.TosaOperatorAddQuantInfo
1053 TosaOperator.End = TosaOperator.TosaOperatorEnd
1054 from tosa import TosaTensor
1055
1056 if not hasattr(TosaTensor, "Start"):
1057 TosaTensor.Start = TosaTensor.TosaTensorStart
1058 TosaTensor.AddName = TosaTensor.TosaTensorAddName
1059 TosaTensor.AddShape = TosaTensor.TosaTensorAddShape
1060 TosaTensor.StartShapeVector = TosaTensor.TosaTensorStartShapeVector
1061 TosaTensor.AddType = TosaTensor.TosaTensorAddType
1062 TosaTensor.AddData = TosaTensor.TosaTensorAddData
1063 TosaTensor.StartDataVector = TosaTensor.TosaTensorStartDataVector
1064 TosaTensor.End = TosaTensor.TosaTensorEnd
1065 from tosa import TransposeAttribute
1066
1067 if not hasattr(TransposeAttribute, "Start"):
1068 TransposeAttribute.Start = TransposeAttribute.TransposeAttributeStart
1069 TransposeAttribute.AddPerms = TransposeAttribute.TransposeAttributeAddPerms
1070 TransposeAttribute.StartPermsVector = (
1071 TransposeAttribute.TransposeAttributeStartPermsVector
1072 )
1073 TransposeAttribute.End = TransposeAttribute.TransposeAttributeEnd
1074 from tosa import TransposeConvAttribute
1075
1076 if not hasattr(TransposeConvAttribute, "Start"):
1077 TransposeConvAttribute.Start = (
1078 TransposeConvAttribute.TransposeConvAttributeStart
1079 )
Eric Kunze4c3537d2022-06-13 17:21:48 -07001080 TransposeConvAttribute.AddOutPad = (
1081 TransposeConvAttribute.TransposeConvAttributeAddOutPad
Eric Kunzeae906de2022-05-30 22:40:47 -07001082 )
Eric Kunze4c3537d2022-06-13 17:21:48 -07001083 TransposeConvAttribute.StartOutPadVector = (
1084 TransposeConvAttribute.TransposeConvAttributeStartOutPadVector
Eric Kunzeae906de2022-05-30 22:40:47 -07001085 )
1086 TransposeConvAttribute.AddStride = (
1087 TransposeConvAttribute.TransposeConvAttributeAddStride
1088 )
1089 TransposeConvAttribute.StartStrideVector = (
1090 TransposeConvAttribute.TransposeConvAttributeStartStrideVector
1091 )
Eric Kunzeae906de2022-05-30 22:40:47 -07001092 TransposeConvAttribute.AddOutputShape = (
1093 TransposeConvAttribute.TransposeConvAttributeAddOutputShape
1094 )
1095 TransposeConvAttribute.StartOutputShapeVector = (
1096 TransposeConvAttribute.TransposeConvAttributeStartOutputShapeVector
1097 )
1098 TransposeConvAttribute.End = (
1099 TransposeConvAttribute.TransposeConvAttributeEnd
1100 )
1101 from tosa import UnaryQuantInfo
1102
1103 if not hasattr(UnaryQuantInfo, "Start"):
1104 UnaryQuantInfo.Start = UnaryQuantInfo.UnaryQuantInfoStart
1105 UnaryQuantInfo.AddInputZp = UnaryQuantInfo.UnaryQuantInfoAddInputZp
1106 UnaryQuantInfo.AddOutputZp = UnaryQuantInfo.UnaryQuantInfoAddOutputZp
1107 UnaryQuantInfo.End = UnaryQuantInfo.UnaryQuantInfoEnd
1108 from tosa import Version
1109
1110 if not hasattr(Version, "Start"):
1111 Version.Start = Version.VersionStart
1112 Version.Add_major = Version.VersionAdd_major
1113 Version.Add_minor = Version.VersionAdd_minor
1114 Version.Add_patch = Version.VersionAdd_patch
1115 Version.Add_draft = Version.VersionAdd_draft
1116 Version.End = Version.VersionEnd
1117 from tosa import WhileLoopAttribute
1118
1119 if not hasattr(WhileLoopAttribute, "Start"):
1120 WhileLoopAttribute.Start = WhileLoopAttribute.WhileLoopAttributeStart
1121 WhileLoopAttribute.AddCondBranch = (
1122 WhileLoopAttribute.WhileLoopAttributeAddCondBranch
1123 )
1124 WhileLoopAttribute.AddBodyBranch = (
1125 WhileLoopAttribute.WhileLoopAttributeAddBodyBranch
1126 )
1127 WhileLoopAttribute.End = WhileLoopAttribute.WhileLoopAttributeEnd