blob: a07068a1cc0555618516540eec278452f33eabab [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 Kunzebdcc3fe2022-06-07 05:17:37 +000033TOSA_VERSION_MINOR = 30
Kevin Chengb97cb1d2021-10-14 11:53:39 -070034TOSA_VERSION_PATCH = 0
Eric Kunze8fcef212022-06-16 15:45:39 -070035TOSA_VERSION_DRAFT = False
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
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000144 def PoolAttribute(self, kernel, stride, pad, input_zp, output_zp):
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))
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000153 self.ints.append((a.AddInputZp, input_zp))
154 self.ints.append((a.AddOutputZp, output_zp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000155
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000156 def ConvAttribute(self, pad, stride, dilation, input_zp, weight_zp):
Kevin Chengfea5a372021-10-11 18:38:47 +0000157 from tosa import ConvAttribute as a, Attribute
158
159 self.utype = Attribute.Attribute().ConvAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800160 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000161
TatWai Chong7be71652022-05-10 17:26:20 -0700162 self.intvecs.append((a.AddPad, pad))
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800163 self.intvecs.append((a.AddStride, stride))
164 self.intvecs.append((a.AddDilation, dilation))
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000165 self.ints.append((a.AddInputZp, input_zp))
166 self.ints.append((a.AddWeightZp, weight_zp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000167
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000168 def TransposeConvAttribute(self, outpad, stride, output_shape, input_zp, weight_zp):
Kevin Chengfea5a372021-10-11 18:38:47 +0000169 from tosa import TransposeConvAttribute as a, Attribute
170
171 self.utype = Attribute.Attribute().TransposeConvAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800172 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000173
Eric Kunze4c3537d2022-06-13 17:21:48 -0700174 self.intvecs.append((a.AddOutPad, outpad))
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800175 self.intvecs.append((a.AddStride, stride))
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800176 self.intvecs.append((a.AddOutputShape, output_shape))
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000177 self.ints.append((a.AddInputZp, input_zp))
178 self.ints.append((a.AddWeightZp, weight_zp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000179
Kevin Cheng38d214c2021-10-15 15:49:19 -0700180 def PadAttribute(self, padding, pad_const_int, pad_const_fp):
181 from tosa import PadAttribute as a, Attribute
Kevin Chengfea5a372021-10-11 18:38:47 +0000182
Kevin Cheng38d214c2021-10-15 15:49:19 -0700183 self.utype = Attribute.Attribute().PadAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800184 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000185
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800186 self.intvecs.append((a.AddPadding, padding))
187 self.ints.append((a.AddPadConstInt, pad_const_int))
188 self.floats.append((a.AddPadConstFp, pad_const_fp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000189
190 def AxisAttribute(self, axis):
191 from tosa import AxisAttribute as a, Attribute
192
193 self.utype = Attribute.Attribute().AxisAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800194 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000195
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800196 self.ints.append((a.AddAxis, axis))
Kevin Chengfea5a372021-10-11 18:38:47 +0000197
TatWai Chong7be71652022-05-10 17:26:20 -0700198 def ReshapeAttribute(self, new_shape):
Kevin Chengfea5a372021-10-11 18:38:47 +0000199 from tosa import ReshapeAttribute as a, Attribute
200
201 self.utype = Attribute.Attribute().ReshapeAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800202 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000203
TatWai Chong7be71652022-05-10 17:26:20 -0700204 self.intvecs.append((a.AddNewShape, new_shape))
Kevin Chengfea5a372021-10-11 18:38:47 +0000205
TatWai Chong7be71652022-05-10 17:26:20 -0700206 def SliceAttribute(self, start, size):
Kevin Chengfea5a372021-10-11 18:38:47 +0000207 from tosa import SliceAttribute as a, Attribute
208
209 self.utype = Attribute.Attribute().SliceAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800210 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000211
TatWai Chong7be71652022-05-10 17:26:20 -0700212 self.intvecs.append((a.AddStart, start))
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800213 self.intvecs.append((a.AddSize, size))
Kevin Chengfea5a372021-10-11 18:38:47 +0000214
215 def TileAttribute(self, multiples):
216 from tosa import TileAttribute as a, Attribute
217
218 self.utype = Attribute.Attribute().TileAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800219 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000220
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800221 self.intvecs.append((a.AddMultiples, multiples))
Kevin Chengfea5a372021-10-11 18:38:47 +0000222
223 def ResizeAttribute(
224 self, output_size, stride, offset, shift, stride_fp, offset_fp, mode
225 ):
226 from tosa import ResizeAttribute as a, Attribute
227
228 self.utype = Attribute.Attribute().ResizeAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800229 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000230
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800231 self.intvecs.append((a.AddOutputSize, output_size))
232 self.intvecs.append((a.AddStride, stride))
233 self.intvecs.append((a.AddOffset, offset))
234 self.ints.append((a.AddShift, shift))
235 self.fpvecs.append((a.AddStrideFp, stride_fp))
236 self.fpvecs.append((a.AddOffsetFp, offset_fp))
237 self.ints.append((a.AddMode, mode))
Kevin Chengfea5a372021-10-11 18:38:47 +0000238
239 def ClampAttribute(self, minint, maxint, minfp, maxfp):
240 from tosa import ClampAttribute as a, Attribute
241
242 self.utype = Attribute.Attribute().ClampAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800243 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000244
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800245 self.ints.append((a.AddMinInt, minint))
246 self.ints.append((a.AddMaxInt, maxint))
Kevin Chengfea5a372021-10-11 18:38:47 +0000247
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800248 self.ints.append((a.AddMinFp, minfp))
249 self.ints.append((a.AddMaxFp, maxfp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000250
251 def RescaleAttribute(
252 self, input_zp, output_zp, multiplier, shift, scale32, double_round, per_channel
253 ):
254 from tosa import RescaleAttribute as a, Attribute
255
256 self.utype = Attribute.Attribute().RescaleAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800257 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000258
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800259 self.ints.append((a.AddInputZp, input_zp))
260 self.ints.append((a.AddOutputZp, output_zp))
261 self.intvecs.append((a.AddMultiplier, multiplier))
262 self.intvecs.append((a.AddShift, shift))
263 self.bools.append((a.AddScale32, scale32))
264 self.bools.append((a.AddDoubleRound, double_round))
265 self.bools.append((a.AddPerChannel, per_channel))
Kevin Chengfea5a372021-10-11 18:38:47 +0000266
267 def MulAttribute(self, shift):
268 from tosa import MulAttribute as a, Attribute
269
270 self.utype = Attribute.Attribute().MulAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800271 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000272
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800273 self.ints.append((a.AddShift, shift))
Kevin Chengfea5a372021-10-11 18:38:47 +0000274
275 def ArithmeticRightShiftAttribute(self, round):
276 from tosa import ArithmeticRightShiftAttribute as a, Attribute
277
278 self.utype = Attribute.Attribute().ArithmeticRightShiftAttribute
279 self.optFcns = (
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800280 a.Start,
281 a.End,
Kevin Chengfea5a372021-10-11 18:38:47 +0000282 )
283
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800284 self.bools.append((a.AddRound, round))
Kevin Chengfea5a372021-10-11 18:38:47 +0000285
Kevin Chengfea5a372021-10-11 18:38:47 +0000286 def CondIfAttribute(self, then_branch, else_branch):
287 from tosa import CondIfAttribute as a, Attribute
288
289 self.utype = Attribute.Attribute().CondIfAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800290 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000291
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800292 self.strings.append((a.AddThenBranch, then_branch))
293 self.strings.append((a.AddElseBranch, else_branch))
Kevin Chengfea5a372021-10-11 18:38:47 +0000294
295 def WhileLoopAttribute(self, cond_branch, body_branch):
296 from tosa import WhileLoopAttribute as a, Attribute
297
298 self.utype = Attribute.Attribute().WhileLoopAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800299 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000300
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800301 self.strings.append((a.AddCondBranch, cond_branch))
302 self.strings.append((a.AddBodyBranch, body_branch))
Kevin Chengfea5a372021-10-11 18:38:47 +0000303
TatWai Chong7be71652022-05-10 17:26:20 -0700304 def TransposeAttribute(self, perms):
Kevin Cheng38d214c2021-10-15 15:49:19 -0700305 from tosa import TransposeAttribute as a, Attribute
306
307 self.utype = Attribute.Attribute().TransposeAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800308 self.optFcns = (a.Start, a.End)
Kevin Cheng38d214c2021-10-15 15:49:19 -0700309
TatWai Chong7be71652022-05-10 17:26:20 -0700310 self.intvecs.append((a.AddPerms, perms))
Kevin Cheng38d214c2021-10-15 15:49:19 -0700311
312 def TableAttribute(self, table):
313 from tosa import TableAttribute as a, Attribute
314
315 self.utype = Attribute.Attribute().TableAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800316 self.optFcns = (a.Start, a.End)
Kevin Cheng38d214c2021-10-15 15:49:19 -0700317
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800318 self.intvecs.append((a.AddTable, table))
Kevin Chengfea5a372021-10-11 18:38:47 +0000319
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000320 def MatMulAttribute(self, A_zp, B_zp):
321 from tosa import MatMulAttribute as a, Attribute
Jeremy Johnson9b225172021-12-14 16:34:47 +0000322
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000323 self.utype = Attribute.Attribute().MatMulAttribute
324 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000325
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000326 self.ints.append((a.AddAZp, A_zp))
327 self.ints.append((a.AddBZp, B_zp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000328
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000329 def FullyConnectedAttribute(self, input_zp, weight_zp):
330 from tosa import FullyConnectedAttribute as a, Attribute
Kevin Chengfea5a372021-10-11 18:38:47 +0000331
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000332 self.utype = Attribute.Attribute().FullyConnectedAttribute
333 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000334
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000335 self.ints.append((a.AddInputZp, input_zp))
336 self.ints.append((a.AddWeightZp, weight_zp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000337
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000338 def NegateAttribute(self, input1_zp, output_zp):
339 from tosa import NegateAttribute as a, Attribute
Kevin Chengfea5a372021-10-11 18:38:47 +0000340
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000341 self.utype = Attribute.Attribute().NegateAttribute
342 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000343
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000344 self.ints.append((a.AddInput1Zp, input1_zp))
345 self.ints.append((a.AddOutputZp, output_zp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000346
347
348class TosaSerializerTensor:
349 def __init__(
350 self,
351 name,
352 shape,
353 dtype,
354 data=None,
355 placeholderFilename=None,
356 ):
357 self.name = name
358
359 if isinstance(shape, np.ndarray):
360 shape = shape.astype(int).tolist()
361 shape = list(map(int, shape))
362
363 self.shape = shape
364 self.dtype = dtype
365
366 if isinstance(data, np.ndarray):
367 data = data.flatten().astype(int).tolist()
368 data = list(map(int, data))
369 self.data = data
370 elif isinstance(data, list):
371 data = list(map(int, data))
372 self.data = data
373 else:
374 self.data = None
375
376 # Filename for placeholder tensors. These get generated by the test generation
Jeremy Johnson9b225172021-12-14 16:34:47 +0000377 # process and are written to disk, but are considered input tensors by the
378 # network so they do not appear in the TOSA serialiazation. However, if we
379 # want to form a unit test around these input tensors, we can get the filename
380 # from here.
Kevin Chengfea5a372021-10-11 18:38:47 +0000381 self.placeholderFilename = placeholderFilename
382
383 def __str__(self):
384 str = "TosaSerializerTensor name: {} shape: {} dtype: {}".format(
385 self.name,
386 self.shape,
387 DTypeNames[self.dtype],
388 )
389 return str
390
391 def setDtype(self, dtype):
392 self.dtype = dtype
393
394 def serialize(self, builder):
395 fb_name = builder.CreateString(self.name)
396 fb_shapes = TosaSerializer.serializeInt32Vec(builder, self.shape)
397 if self.data:
398 u8_data = list()
399 # little endianess
400 if self.dtype == DType.BOOL:
401 for val in self.data:
402 val_u8 = np.uint8(val)
403 u8_data.append(val_u8)
404 elif self.dtype == DType.INT4:
405 in_size = len(self.data)
406 out_size = (in_size + 1) // 2
407 for i in range(out_size):
408 val_0 = self.data[2 * i]
409 if (2 * i + 1) < in_size:
410 val_1 = self.data[2 * i + 1]
411 else:
412 val_1 = 0
413 val_i8 = (val_0 & 0xF) | ((val_1 & 0xF) << 4)
414 val_u8 = np.uint8(val_i8)
415 u8_data.append(val_u8)
416 elif self.dtype == DType.INT8:
417 for val in self.data:
418 val_u8 = np.uint8(val)
419 u8_data.append(val_u8)
420 elif self.dtype == DType.INT16:
421 for val in self.data:
422 val_u16 = np.uint16(val)
423 b0 = val_u16 & ByteMask
424 b1 = (val_u16 >> np.uint16(8)) & ByteMask
425 u8_data.extend([b0, b1])
426 elif self.dtype == DType.INT32:
427 for val in self.data:
428 val_u32 = np.uint32(val)
429 b0 = val_u32 & ByteMask
430 b1 = (val_u32 >> np.uint32(8)) & ByteMask
431 b2 = (val_u32 >> np.uint32(16)) & ByteMask
Kevin Cheng6b078ca2021-10-13 23:12:50 -0700432 b3 = (val_u32 >> np.uint32(24)) & ByteMask
Kevin Chengfea5a372021-10-11 18:38:47 +0000433 u8_data.extend([b0, b1, b2, b3])
434 elif self.dtype == DType.INT48:
435 for val in self.data:
436 val_u64 = np.uint64(val)
437 b0 = val_u64 & ByteMask
438 b1 = (val_u64 >> np.uint64(8)) & ByteMask
439 b2 = (val_u64 >> np.uint64(16)) & ByteMask
440 b3 = (val_u64 >> np.uint64(24)) & ByteMask
441 b4 = (val_u64 >> np.uint64(32)) & ByteMask
442 b5 = (val_u64 >> np.uint64(40)) & ByteMask
443 u8_data.extend([b0, b1, b2, b3, b4, b5])
444 elif self.dtype == DType.FLOAT:
445 for val in self.data:
446 b = struct.pack("!f", val)
447 u8_data.extend([b[3], b[2], b[1], b[0]])
448 else:
449 raise Exception(
450 "unsupported data type {}".format(DTypeNames[self.dtype])
451 )
452 fb_data = TosaSerializer.serializeUint8Vec(builder, u8_data)
453
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800454 TosaTensor.Start(builder)
455 TosaTensor.AddName(builder, fb_name)
456 TosaTensor.AddShape(builder, fb_shapes)
457 TosaTensor.AddType(builder, self.dtype)
Kevin Chengfea5a372021-10-11 18:38:47 +0000458 if self.data:
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800459 TosaTensor.AddData(builder, fb_data)
Kevin Chengfea5a372021-10-11 18:38:47 +0000460
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800461 return TosaTensor.End(builder)
Kevin Chengfea5a372021-10-11 18:38:47 +0000462
463
464class TosaSerializerOperator:
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000465 def __init__(self, op, inputs, outputs, attributes=None):
Kevin Chengfea5a372021-10-11 18:38:47 +0000466 self.op = op
467 self.attributes = attributes
468 self.inputs = TosaSerializer.toList(inputs)
469 self.outputs = TosaSerializer.toList(outputs)
Kevin Chengfea5a372021-10-11 18:38:47 +0000470
471 def __str__(self):
472 str = "Op {}\n----\n".format(self.op)
473
474 for i in self.inputs:
475 str = str + " Input: {}\n".format(i)
476 for o in self.outputs:
477 str = str + " Output: {}\n".format(o)
478
479 return str
480
481 def serialize(self, builder):
482 fb_inputs = TosaSerializer.serializeStrVec(
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800483 builder, self.inputs, TosaOperator.StartInputsVector
Kevin Chengfea5a372021-10-11 18:38:47 +0000484 )
485 fb_outputs = TosaSerializer.serializeStrVec(
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800486 builder, self.outputs, TosaOperator.StartOutputsVector
Kevin Chengfea5a372021-10-11 18:38:47 +0000487 )
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000488 # Need to serialize attributes enums still
Kevin Chengfea5a372021-10-11 18:38:47 +0000489 if self.attributes is not None:
490 fb_attributes = self.attributes.serialize(builder)
491
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800492 TosaOperator.Start(builder)
493 TosaOperator.AddOp(builder, self.op)
494 TosaOperator.AddInputs(builder, fb_inputs)
495 TosaOperator.AddOutputs(builder, fb_outputs)
Kevin Chengfea5a372021-10-11 18:38:47 +0000496 if self.attributes is not None:
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800497 TosaOperator.AddAttributeType(builder, self.attributes.utype)
498 TosaOperator.AddAttribute(builder, fb_attributes)
Kevin Chengfea5a372021-10-11 18:38:47 +0000499
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800500 return TosaOperator.End(builder)
Kevin Chengfea5a372021-10-11 18:38:47 +0000501
502
503class TosaSerializerBasicBlock:
504 def __init__(self, name):
505 self.name = name
506 self.operators = []
507
508 # Dict assures uniqueness, but allows us to look up by name
509 self.tensors = dict()
510
511 self.inputs = []
512 self.outputs = []
513
514 def addTensor(
515 self,
516 name,
517 shape,
518 dtype,
519 data=None,
520 placeholderFilename=None,
521 ):
Jeremy Johnson9b225172021-12-14 16:34:47 +0000522 if name not in self.tensors:
Kevin Chengfea5a372021-10-11 18:38:47 +0000523 self.tensors[name] = TosaSerializerTensor(
524 name, shape, dtype, data, placeholderFilename
525 )
526
527 return self.tensors[name]
528
529 def addInput(self, name):
530 self.inputs.append(name)
531
532 def addOutput(self, name):
533 self.outputs.append(name)
534
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000535 def addOperator(self, op, inputs, outputs, attributes=None):
536 self.operators.append(TosaSerializerOperator(op, inputs, outputs, attributes))
Kevin Chengfea5a372021-10-11 18:38:47 +0000537
538 def serialize(self, builder):
539 fb_name = builder.CreateString(self.name)
540 fbv_inputs = TosaSerializer.serializeStrVec(
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800541 builder, list(self.inputs), TosaBasicBlock.StartInputsVector
Kevin Chengfea5a372021-10-11 18:38:47 +0000542 )
543 fbv_outputs = TosaSerializer.serializeStrVec(
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800544 builder, list(self.outputs), TosaBasicBlock.StartOutputsVector
Kevin Chengfea5a372021-10-11 18:38:47 +0000545 )
546 fbv_tensors = TosaSerializer.serializeObjVec(
547 builder,
548 list(self.tensors.values()),
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800549 TosaBasicBlock.StartTensorsVector,
Kevin Chengfea5a372021-10-11 18:38:47 +0000550 )
551 fbv_operators = TosaSerializer.serializeObjVec(
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800552 builder, self.operators, TosaBasicBlock.StartOperatorsVector
Kevin Chengfea5a372021-10-11 18:38:47 +0000553 )
554
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800555 TosaBasicBlock.Start(builder)
556 TosaBasicBlock.AddName(builder, fb_name)
557 TosaBasicBlock.AddInputs(builder, fbv_inputs)
558 TosaBasicBlock.AddOutputs(builder, fbv_outputs)
559 TosaBasicBlock.AddTensors(builder, fbv_tensors)
560 TosaBasicBlock.AddOperators(builder, fbv_operators)
561 return TosaBasicBlock.End(builder)
Kevin Chengfea5a372021-10-11 18:38:47 +0000562
563
564@unique
565class TensorDir(IntEnum):
566 PLACEHOLDER = 0
567 CONST = 1
568 INTERMEDIATE = 2
569 RESULT = 3
570
571
572class TosaSerializer:
573 def __init__(self, pathPrefix):
Eric Kunzeae906de2022-05-30 22:40:47 -0700574 self.add_compat_methods()
Kevin Chengfea5a372021-10-11 18:38:47 +0000575 # Get the global TOSA version if not already defined
Kevin Chengfea5a372021-10-11 18:38:47 +0000576
577 self.builder = flatbuffers.Builder(0)
578
579 self.basicBlocks = []
580 self.startBasicBlock("main")
581 self.pathPrefix = pathPrefix
582
583 # Indicies used for adding/naming tensors
584 self.currInputIdx = 0
585 self.currConstIdx = 0
586 self.currLayerIdx = 1
587 self.currResultIdx = 0
588
589 # Is this an illegal test that is expected to fail?
Jeremy Johnson9b225172021-12-14 16:34:47 +0000590 self.expectedReturnCode = 0
Kevin Chengfea5a372021-10-11 18:38:47 +0000591 self.expectedFailure = False
592 self.expectedFailureDesc = ""
593
594 def __str__(self):
595 str = ""
596 for bb in self.basicBlocks:
597 str = str + bb.__str__()
598 return str
599
600 def addPlaceholder(self, shape, dtype, vals):
601 if not self.currBasicBlock:
602 raise Exception("addTensor called without valid basic block")
603
604 name = "input-{}".format(self.currInputIdx)
605 filename = "{}.npy".format(name)
606 self.currInputIdx = self.currInputIdx + 1
607
608 tens = self.currBasicBlock.addTensor(name, shape, dtype, None, filename)
609 # This is always an input to the block
610 self.currBasicBlock.addInput(name)
611
612 if vals is not None:
613 np.save(os.path.join(self.pathPrefix, filename), vals, False)
614
615 return tens
616
617 def addConst(self, shape, dtype, vals):
618 if not self.currBasicBlock:
619 raise Exception("addTensor called without valid basic block")
620
621 name = "const-{}".format(self.currInputIdx)
Kevin Chengfea5a372021-10-11 18:38:47 +0000622 self.currInputIdx = self.currInputIdx + 1
623
624 tens = self.currBasicBlock.addTensor(name, shape, dtype, vals)
625 # Add the operator now
Jeremy Johnson9b225172021-12-14 16:34:47 +0000626 self.currBasicBlock.addOperator(TosaOp.Op().CONST, [], name)
Kevin Chengfea5a372021-10-11 18:38:47 +0000627
628 return tens
629
630 def addIntermediate(self, shape, dtype):
631
632 if not self.currBasicBlock:
633 raise Exception("addTensor called without valid basic block")
634
635 name = "layer-{}".format(self.currLayerIdx)
636 self.currLayerIdx = self.currLayerIdx + 1
637
638 tens = self.currBasicBlock.addTensor(name, shape, dtype, None)
639
640 return tens
641
642 def addInputTensor(self, tensor):
643 self.currBasicBlock.addTensor(tensor.name, tensor.shape, tensor.dtype)
644 self.currBasicBlock.addInput(tensor.name)
645
646 def addOutputTensor(self, tensor):
647 self.currBasicBlock.addOutput(tensor.name)
648
649 def addOutput(self, shape, dtype):
650 if not self.currBasicBlock:
651 raise Exception("addTensor called without valid basic block")
652
653 name = "result-{}".format(self.currResultIdx)
654 self.currResultIdx = self.currResultIdx + 1
655
656 tens = self.currBasicBlock.addTensor(name, shape, dtype, None)
657 self.currBasicBlock.addOutput(name)
658 return tens
659
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000660 def addOperator(self, op, inputs, outputs, attributes=None):
Kevin Chengfea5a372021-10-11 18:38:47 +0000661
Jeremy Johnson9b225172021-12-14 16:34:47 +0000662 if op == TosaOp.Op().CONST:
Kevin Chengfea5a372021-10-11 18:38:47 +0000663 raise Exception("Use addConstTensor() to add CONST ops")
664
665 return self.currBasicBlock.addOperator(
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000666 op,
667 inputs,
668 outputs,
669 attributes,
Kevin Chengfea5a372021-10-11 18:38:47 +0000670 )
671
Jeremy Johnson9b225172021-12-14 16:34:47 +0000672 def setExpectedReturnCode(self, val, fail, desc=""):
Kevin Chengfea5a372021-10-11 18:38:47 +0000673
674 self.expectedReturnCode = val
675 self.expectedFailureDesc = desc
Jeremy Johnson9b225172021-12-14 16:34:47 +0000676 self.expectedFailure = fail
Kevin Chengfea5a372021-10-11 18:38:47 +0000677
678 def serialize(self):
679
680 builder = self.builder
681
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800682 Version.Start(builder)
683 Version.Add_major(builder, TOSA_VERSION[0])
684 Version.Add_minor(builder, TOSA_VERSION[1])
685 Version.Add_patch(builder, TOSA_VERSION[2])
686 Version.Add_draft(builder, TOSA_VERSION[3])
687 version = Version.End(builder)
Kevin Chengfea5a372021-10-11 18:38:47 +0000688
689 fbv_bb = TosaSerializer.serializeObjVec(
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800690 builder, self.basicBlocks, TosaGraph.StartBlocksVector
Kevin Chengfea5a372021-10-11 18:38:47 +0000691 )
692
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800693 TosaGraph.Start(builder)
694 TosaGraph.AddVersion(builder, version)
695 TosaGraph.AddBlocks(builder, fbv_bb)
696 graph = TosaGraph.End(builder)
Kevin Chengfea5a372021-10-11 18:38:47 +0000697
Eric Kunzee6596402022-06-09 21:27:36 +0000698 self.builder.Finish(graph, TOSA_GRAPH_IDENTIFIER)
Kevin Chengfea5a372021-10-11 18:38:47 +0000699 return self.builder.Output()
700
701 def writeJson(self, tosa_filename):
702 """Write a json test file so that it is fairly easy to pick up the test
703 and generate commands for third party tool"""
704 test_desc = dict()
705
706 test_desc["tosa_file"] = tosa_filename
707 ifm_name = []
708 ifm_file = []
709 ofm_name = []
710 ofm_file = []
711
712 for b in self.basicBlocks:
713 if b.name == "main":
714 for i in b.inputs:
715 ifm_name.append(i)
716 ifm_file.append(b.tensors[i].placeholderFilename)
717 for o in b.outputs:
718 ofm_name.append(o)
Jeremy Johnson9b225172021-12-14 16:34:47 +0000719 # Make up an OFM filename here. One isn't generated until the
720 # reference tool is run, so any name is a good name
Kevin Chengfea5a372021-10-11 18:38:47 +0000721 ofm_file.append("ref-{}.npy".format(o))
722
723 test_desc["ifm_name"] = ifm_name
724 test_desc["ifm_file"] = ifm_file
725 test_desc["ofm_name"] = ofm_name
726 test_desc["ofm_file"] = ofm_file
727 test_desc["expected_return_code"] = self.expectedReturnCode
728 test_desc["expected_failure"] = self.expectedFailure
729 if self.expectedFailureDesc:
730 test_desc["expected_failure_desc"] = self.expectedFailureDesc
731
732 return json.dumps(test_desc, indent=" ")
733
734 def startBasicBlock(self, name):
735 self.currBasicBlock = TosaSerializerBasicBlock(name)
736 self.basicBlocks.append(self.currBasicBlock)
737
738 @staticmethod
739 def serializeStrVec(builder, vec, start_fcn):
740 fb_strs = [builder.CreateString(i) for i in vec]
741 start_fcn(builder, len(fb_strs))
742 for s in fb_strs[::-1]:
743 builder.PrependUOffsetTRelative(s)
Eric Kunzeae906de2022-05-30 22:40:47 -0700744 try:
745 return builder.EndVector()
746 except TypeError:
747 return builder.EndVector(len(vec))
Kevin Chengfea5a372021-10-11 18:38:47 +0000748
749 @staticmethod
750 def serializeUint8Vec(builder, vec):
751 builder.StartVector(1, len(vec), 8)
752 for v in vec[::-1]:
753 builder.PrependUint8(v)
754 try:
755 return builder.EndVector()
756 except TypeError:
757 return builder.EndVector(len(vec))
758
759 @staticmethod
760 def serializeInt32Vec(builder, vec):
761 builder.StartVector(4, len(vec), 4)
762 for v in vec[::-1]:
763 builder.PrependInt32(v)
764 try:
765 return builder.EndVector()
766 except TypeError:
767 return builder.EndVector(len(vec))
768
769 @staticmethod
770 def serializeFpVec(builder, vec):
771 builder.StartVector(4, len(vec), 4)
772 for v in vec[::-1]:
773 builder.PrependFloat32(v)
774 try:
775 return builder.EndVector()
776 except TypeError:
777 return builder.EndVector(len(vec))
778
779 @staticmethod
780 def serializeObjVec(builder, vec, start_fcn):
781 serialized_vec = []
782 for v in vec[::-1]:
783 serialized_vec.append(v.serialize(builder))
784
785 start_fcn(builder, len(vec))
786 for v in serialized_vec:
787 builder.PrependUOffsetTRelative(v)
788 try:
789 return builder.EndVector()
790 except TypeError:
791 return builder.EndVector(len(vec))
792
793 @staticmethod
794 def toList(val):
795 if isinstance(val, list):
796 return val
797 else:
798 return [val]
Eric Kunzeae906de2022-05-30 22:40:47 -0700799
800 # Remove when switching to flatbuffers 2.0
801 # contains a mapping of the deprecated 1.12 method to the 2.0 version
802
803 def add_compat_methods(self):
804
805 from tosa import ArithmeticRightShiftAttribute
806
807 if not hasattr(ArithmeticRightShiftAttribute, "Start"):
808 ArithmeticRightShiftAttribute.Start = (
809 ArithmeticRightShiftAttribute.ArithmeticRightShiftAttributeStart
810 )
811 ArithmeticRightShiftAttribute.AddRound = (
812 ArithmeticRightShiftAttribute.ArithmeticRightShiftAttributeAddRound
813 )
814 ArithmeticRightShiftAttribute.End = (
815 ArithmeticRightShiftAttribute.ArithmeticRightShiftAttributeEnd
816 )
817 from tosa import AxisAttribute
818
819 if not hasattr(AxisAttribute, "Start"):
820 AxisAttribute.Start = AxisAttribute.AxisAttributeStart
821 AxisAttribute.AddAxis = AxisAttribute.AxisAttributeAddAxis
822 AxisAttribute.End = AxisAttribute.AxisAttributeEnd
823 from tosa import ClampAttribute
824
825 if not hasattr(ClampAttribute, "Start"):
826 ClampAttribute.Start = ClampAttribute.ClampAttributeStart
827 ClampAttribute.AddMinInt = ClampAttribute.ClampAttributeAddMinInt
828 ClampAttribute.AddMaxInt = ClampAttribute.ClampAttributeAddMaxInt
829 ClampAttribute.AddMinFp = ClampAttribute.ClampAttributeAddMinFp
830 ClampAttribute.AddMaxFp = ClampAttribute.ClampAttributeAddMaxFp
831 ClampAttribute.End = ClampAttribute.ClampAttributeEnd
832 from tosa import CondIfAttribute
833
834 if not hasattr(CondIfAttribute, "Start"):
835 CondIfAttribute.Start = CondIfAttribute.CondIfAttributeStart
836 CondIfAttribute.AddThenBranch = CondIfAttribute.CondIfAttributeAddThenBranch
837 CondIfAttribute.AddElseBranch = CondIfAttribute.CondIfAttributeAddElseBranch
838 CondIfAttribute.End = CondIfAttribute.CondIfAttributeEnd
839 from tosa import ConvAttribute
840
841 if not hasattr(ConvAttribute, "Start"):
842 ConvAttribute.Start = ConvAttribute.ConvAttributeStart
843 ConvAttribute.AddPad = ConvAttribute.ConvAttributeAddPad
844 ConvAttribute.StartPadVector = ConvAttribute.ConvAttributeStartPadVector
845 ConvAttribute.AddStride = ConvAttribute.ConvAttributeAddStride
846 ConvAttribute.StartStrideVector = (
847 ConvAttribute.ConvAttributeStartStrideVector
848 )
849 ConvAttribute.AddDilation = ConvAttribute.ConvAttributeAddDilation
850 ConvAttribute.StartDilationVector = (
851 ConvAttribute.ConvAttributeStartDilationVector
852 )
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000853 ConvAttribute.AddInputZp = ConvAttribute.ConvAttributeAddInputZp
854 ConvAttribute.AddWeightZp = ConvAttribute.ConvAttributeAddWeightZp
Eric Kunzeae906de2022-05-30 22:40:47 -0700855 ConvAttribute.End = ConvAttribute.ConvAttributeEnd
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000856 from tosa import FullyConnectedAttribute
Eric Kunzeae906de2022-05-30 22:40:47 -0700857
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000858 if not hasattr(FullyConnectedAttribute, "Start"):
859 FullyConnectedAttribute.Start = (
860 FullyConnectedAttribute.FullyConnectedAttributeStart
861 )
862 FullyConnectedAttribute.AddInputZp = (
863 FullyConnectedAttribute.FullyConnectedAttributeAddInputZp
864 )
865 FullyConnectedAttribute.AddWeightZp = (
866 FullyConnectedAttribute.FullyConnectedAttributeAddWeightZp
867 )
868 FullyConnectedAttribute.End = (
869 FullyConnectedAttribute.FullyConnectedAttributeEnd
870 )
871 from tosa import MatMulAttribute
Eric Kunzeae906de2022-05-30 22:40:47 -0700872
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000873 if not hasattr(MatMulAttribute, "Start"):
874 MatMulAttribute.Start = MatMulAttribute.MatMulAttributeStart
875 MatMulAttribute.AddAZp = MatMulAttribute.MatMulAttributeAddAZp
876 MatMulAttribute.AddBZp = MatMulAttribute.MatMulAttributeAddBZp
877 MatMulAttribute.End = MatMulAttribute.MatMulAttributeEnd
878 from tosa import PoolAttribute
879
880 if not hasattr(PoolAttribute, "Start"):
881 PoolAttribute.Start = PoolAttribute.PoolAttributeStart
882 PoolAttribute.AddPad = PoolAttribute.PoolAttributeAddPad
883 PoolAttribute.StartPadVector = PoolAttribute.PoolAttributeStartPadVector
884 PoolAttribute.AddKernel = PoolAttribute.PoolAttributeAddKernel
885 PoolAttribute.StartKernelVector = (
886 PoolAttribute.PoolAttributeStartKernelVector
887 )
888 PoolAttribute.AddStride = PoolAttribute.PoolAttributeAddStride
889 PoolAttribute.StartStrideVector = (
890 PoolAttribute.PoolAttributeStartStrideVector
891 )
892 PoolAttribute.AddInputZp = PoolAttribute.PoolAttributeAddInputZp
893 PoolAttribute.AddOutputZp = PoolAttribute.PoolAttributeAddOutputZp
894 PoolAttribute.End = PoolAttribute.PoolAttributeEnd
Eric Kunzeae906de2022-05-30 22:40:47 -0700895 from tosa import MulAttribute
896
897 if not hasattr(MulAttribute, "Start"):
898 MulAttribute.Start = MulAttribute.MulAttributeStart
899 MulAttribute.AddShift = MulAttribute.MulAttributeAddShift
900 MulAttribute.End = MulAttribute.MulAttributeEnd
901 from tosa import PadAttribute
902
903 if not hasattr(PadAttribute, "Start"):
904 PadAttribute.Start = PadAttribute.PadAttributeStart
905 PadAttribute.AddPadding = PadAttribute.PadAttributeAddPadding
906 PadAttribute.StartPaddingVector = (
907 PadAttribute.PadAttributeStartPaddingVector
908 )
909 PadAttribute.AddPadConstInt = PadAttribute.PadAttributeAddPadConstInt
910 PadAttribute.AddPadConstFp = PadAttribute.PadAttributeAddPadConstFp
911 PadAttribute.End = PadAttribute.PadAttributeEnd
Eric Kunzeae906de2022-05-30 22:40:47 -0700912 from tosa import PoolAttribute
913
914 if not hasattr(PoolAttribute, "Start"):
915 PoolAttribute.Start = PoolAttribute.PoolAttributeStart
916 PoolAttribute.AddPad = PoolAttribute.PoolAttributeAddPad
917 PoolAttribute.StartPadVector = PoolAttribute.PoolAttributeStartPadVector
918 PoolAttribute.AddKernel = PoolAttribute.PoolAttributeAddKernel
919 PoolAttribute.StartKernelVector = (
920 PoolAttribute.PoolAttributeStartKernelVector
921 )
922 PoolAttribute.AddStride = PoolAttribute.PoolAttributeAddStride
923 PoolAttribute.StartStrideVector = (
924 PoolAttribute.PoolAttributeStartStrideVector
925 )
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000926 PoolAttribute.AddInputZp = PoolAttribute.PoolAttributeAddInputZp
927 PoolAttribute.AddOutputZp = PoolAttribute.PoolAttributeAddOutputZp
Eric Kunzeae906de2022-05-30 22:40:47 -0700928 PoolAttribute.End = PoolAttribute.PoolAttributeEnd
929 from tosa import RescaleAttribute
930
931 if not hasattr(RescaleAttribute, "Start"):
932 RescaleAttribute.Start = RescaleAttribute.RescaleAttributeStart
933 RescaleAttribute.AddInputZp = RescaleAttribute.RescaleAttributeAddInputZp
934 RescaleAttribute.AddOutputZp = RescaleAttribute.RescaleAttributeAddOutputZp
935 RescaleAttribute.AddMultiplier = (
936 RescaleAttribute.RescaleAttributeAddMultiplier
937 )
938 RescaleAttribute.StartMultiplierVector = (
939 RescaleAttribute.RescaleAttributeStartMultiplierVector
940 )
941 RescaleAttribute.AddShift = RescaleAttribute.RescaleAttributeAddShift
942 RescaleAttribute.StartShiftVector = (
943 RescaleAttribute.RescaleAttributeStartShiftVector
944 )
945 RescaleAttribute.AddScale32 = RescaleAttribute.RescaleAttributeAddScale32
946 RescaleAttribute.AddDoubleRound = (
947 RescaleAttribute.RescaleAttributeAddDoubleRound
948 )
949 RescaleAttribute.AddPerChannel = (
950 RescaleAttribute.RescaleAttributeAddPerChannel
951 )
952 RescaleAttribute.End = RescaleAttribute.RescaleAttributeEnd
953 from tosa import ReshapeAttribute
954
955 if not hasattr(ReshapeAttribute, "Start"):
956 ReshapeAttribute.Start = ReshapeAttribute.ReshapeAttributeStart
957 ReshapeAttribute.AddNewShape = ReshapeAttribute.ReshapeAttributeAddNewShape
958 ReshapeAttribute.StartNewShapeVector = (
959 ReshapeAttribute.ReshapeAttributeStartNewShapeVector
960 )
961 ReshapeAttribute.End = ReshapeAttribute.ReshapeAttributeEnd
962 from tosa import ResizeAttribute
963
964 if not hasattr(ResizeAttribute, "Start"):
965 ResizeAttribute.Start = ResizeAttribute.ResizeAttributeStart
966 ResizeAttribute.AddOutputSize = ResizeAttribute.ResizeAttributeAddOutputSize
967 ResizeAttribute.StartOutputSizeVector = (
968 ResizeAttribute.ResizeAttributeStartOutputSizeVector
969 )
970 ResizeAttribute.AddStride = ResizeAttribute.ResizeAttributeAddStride
971 ResizeAttribute.StartStrideVector = (
972 ResizeAttribute.ResizeAttributeStartStrideVector
973 )
974 ResizeAttribute.AddOffset = ResizeAttribute.ResizeAttributeAddOffset
975 ResizeAttribute.StartOffsetVector = (
976 ResizeAttribute.ResizeAttributeStartOffsetVector
977 )
978 ResizeAttribute.AddShift = ResizeAttribute.ResizeAttributeAddShift
979 ResizeAttribute.AddStrideFp = ResizeAttribute.ResizeAttributeAddStrideFp
980 ResizeAttribute.StartStrideFpVector = (
981 ResizeAttribute.ResizeAttributeStartStrideFpVector
982 )
983 ResizeAttribute.AddOffsetFp = ResizeAttribute.ResizeAttributeAddOffsetFp
984 ResizeAttribute.StartOffsetFpVector = (
985 ResizeAttribute.ResizeAttributeStartOffsetFpVector
986 )
987 ResizeAttribute.AddMode = ResizeAttribute.ResizeAttributeAddMode
988 ResizeAttribute.End = ResizeAttribute.ResizeAttributeEnd
989 from tosa import SliceAttribute
990
991 if not hasattr(SliceAttribute, "Start"):
992 SliceAttribute.Start = SliceAttribute.SliceAttributeStart
993 SliceAttribute.AddStart = SliceAttribute.SliceAttributeAddStart
994 SliceAttribute.StartStartVector = (
995 SliceAttribute.SliceAttributeStartStartVector
996 )
997 SliceAttribute.AddSize = SliceAttribute.SliceAttributeAddSize
998 SliceAttribute.StartSizeVector = (
999 SliceAttribute.SliceAttributeStartSizeVector
1000 )
1001 SliceAttribute.End = SliceAttribute.SliceAttributeEnd
1002 from tosa import TableAttribute
1003
1004 if not hasattr(TableAttribute, "Start"):
1005 TableAttribute.Start = TableAttribute.TableAttributeStart
1006 TableAttribute.AddTable = TableAttribute.TableAttributeAddTable
1007 TableAttribute.StartTableVector = (
1008 TableAttribute.TableAttributeStartTableVector
1009 )
1010 TableAttribute.End = TableAttribute.TableAttributeEnd
1011 from tosa import TileAttribute
1012
1013 if not hasattr(TileAttribute, "Start"):
1014 TileAttribute.Start = TileAttribute.TileAttributeStart
1015 TileAttribute.AddMultiples = TileAttribute.TileAttributeAddMultiples
1016 TileAttribute.StartMultiplesVector = (
1017 TileAttribute.TileAttributeStartMultiplesVector
1018 )
1019 TileAttribute.End = TileAttribute.TileAttributeEnd
1020 from tosa import TosaBasicBlock
1021
1022 if not hasattr(TosaBasicBlock, "Start"):
1023 TosaBasicBlock.Start = TosaBasicBlock.TosaBasicBlockStart
1024 TosaBasicBlock.AddName = TosaBasicBlock.TosaBasicBlockAddName
1025 TosaBasicBlock.AddOperators = TosaBasicBlock.TosaBasicBlockAddOperators
1026 TosaBasicBlock.StartOperatorsVector = (
1027 TosaBasicBlock.TosaBasicBlockStartOperatorsVector
1028 )
1029 TosaBasicBlock.AddTensors = TosaBasicBlock.TosaBasicBlockAddTensors
1030 TosaBasicBlock.StartTensorsVector = (
1031 TosaBasicBlock.TosaBasicBlockStartTensorsVector
1032 )
1033 TosaBasicBlock.AddInputs = TosaBasicBlock.TosaBasicBlockAddInputs
1034 TosaBasicBlock.StartInputsVector = (
1035 TosaBasicBlock.TosaBasicBlockStartInputsVector
1036 )
1037 TosaBasicBlock.AddOutputs = TosaBasicBlock.TosaBasicBlockAddOutputs
1038 TosaBasicBlock.StartOutputsVector = (
1039 TosaBasicBlock.TosaBasicBlockStartOutputsVector
1040 )
1041 TosaBasicBlock.End = TosaBasicBlock.TosaBasicBlockEnd
1042 from tosa import TosaGraph
1043
1044 if not hasattr(TosaGraph, "Start"):
1045 TosaGraph.Start = TosaGraph.TosaGraphStart
1046 TosaGraph.AddVersion = TosaGraph.TosaGraphAddVersion
1047 TosaGraph.AddBlocks = TosaGraph.TosaGraphAddBlocks
1048 TosaGraph.StartBlocksVector = TosaGraph.TosaGraphStartBlocksVector
1049 TosaGraph.End = TosaGraph.TosaGraphEnd
1050 from tosa import TosaOperator
1051
1052 if not hasattr(TosaOperator, "Start"):
1053 TosaOperator.Start = TosaOperator.TosaOperatorStart
1054 TosaOperator.AddOp = TosaOperator.TosaOperatorAddOp
1055 TosaOperator.AddAttributeType = TosaOperator.TosaOperatorAddAttributeType
1056 TosaOperator.AddAttribute = TosaOperator.TosaOperatorAddAttribute
1057 TosaOperator.AddInputs = TosaOperator.TosaOperatorAddInputs
1058 TosaOperator.StartInputsVector = TosaOperator.TosaOperatorStartInputsVector
1059 TosaOperator.AddOutputs = TosaOperator.TosaOperatorAddOutputs
1060 TosaOperator.StartOutputsVector = (
1061 TosaOperator.TosaOperatorStartOutputsVector
1062 )
Eric Kunzeae906de2022-05-30 22:40:47 -07001063 TosaOperator.End = TosaOperator.TosaOperatorEnd
1064 from tosa import TosaTensor
1065
1066 if not hasattr(TosaTensor, "Start"):
1067 TosaTensor.Start = TosaTensor.TosaTensorStart
1068 TosaTensor.AddName = TosaTensor.TosaTensorAddName
1069 TosaTensor.AddShape = TosaTensor.TosaTensorAddShape
1070 TosaTensor.StartShapeVector = TosaTensor.TosaTensorStartShapeVector
1071 TosaTensor.AddType = TosaTensor.TosaTensorAddType
1072 TosaTensor.AddData = TosaTensor.TosaTensorAddData
1073 TosaTensor.StartDataVector = TosaTensor.TosaTensorStartDataVector
1074 TosaTensor.End = TosaTensor.TosaTensorEnd
1075 from tosa import TransposeAttribute
1076
1077 if not hasattr(TransposeAttribute, "Start"):
1078 TransposeAttribute.Start = TransposeAttribute.TransposeAttributeStart
1079 TransposeAttribute.AddPerms = TransposeAttribute.TransposeAttributeAddPerms
1080 TransposeAttribute.StartPermsVector = (
1081 TransposeAttribute.TransposeAttributeStartPermsVector
1082 )
1083 TransposeAttribute.End = TransposeAttribute.TransposeAttributeEnd
1084 from tosa import TransposeConvAttribute
1085
1086 if not hasattr(TransposeConvAttribute, "Start"):
1087 TransposeConvAttribute.Start = (
1088 TransposeConvAttribute.TransposeConvAttributeStart
1089 )
Eric Kunze4c3537d2022-06-13 17:21:48 -07001090 TransposeConvAttribute.AddOutPad = (
1091 TransposeConvAttribute.TransposeConvAttributeAddOutPad
Eric Kunzeae906de2022-05-30 22:40:47 -07001092 )
Eric Kunze4c3537d2022-06-13 17:21:48 -07001093 TransposeConvAttribute.StartOutPadVector = (
1094 TransposeConvAttribute.TransposeConvAttributeStartOutPadVector
Eric Kunzeae906de2022-05-30 22:40:47 -07001095 )
1096 TransposeConvAttribute.AddStride = (
1097 TransposeConvAttribute.TransposeConvAttributeAddStride
1098 )
1099 TransposeConvAttribute.StartStrideVector = (
1100 TransposeConvAttribute.TransposeConvAttributeStartStrideVector
1101 )
Eric Kunzeae906de2022-05-30 22:40:47 -07001102 TransposeConvAttribute.AddOutputShape = (
1103 TransposeConvAttribute.TransposeConvAttributeAddOutputShape
1104 )
1105 TransposeConvAttribute.StartOutputShapeVector = (
1106 TransposeConvAttribute.TransposeConvAttributeStartOutputShapeVector
1107 )
Eric Kunzebdcc3fe2022-06-07 05:17:37 +00001108 TransposeConvAttribute.AddInputZp = (
1109 TransposeConvAttribute.TransposeConvAttributeAddInputZp
1110 )
1111 TransposeConvAttribute.AddWeightZp = (
1112 TransposeConvAttribute.TransposeConvAttributeAddWeightZp
1113 )
Eric Kunzeae906de2022-05-30 22:40:47 -07001114 TransposeConvAttribute.End = (
1115 TransposeConvAttribute.TransposeConvAttributeEnd
1116 )
Eric Kunzeae906de2022-05-30 22:40:47 -07001117 from tosa import Version
1118
1119 if not hasattr(Version, "Start"):
1120 Version.Start = Version.VersionStart
1121 Version.Add_major = Version.VersionAdd_major
1122 Version.Add_minor = Version.VersionAdd_minor
1123 Version.Add_patch = Version.VersionAdd_patch
1124 Version.Add_draft = Version.VersionAdd_draft
1125 Version.End = Version.VersionEnd
Eric Kunzebdcc3fe2022-06-07 05:17:37 +00001126 from tosa import MatMulAttribute
1127
1128 if not hasattr(MatMulAttribute, "Start"):
1129 MatMulAttribute.Start = MatMulAttribute.MatMulAttributeStart
1130 MatMulAttribute.AddAZp = MatMulAttribute.MatMulAttributeAddAZp
1131 MatMulAttribute.AddBZp = MatMulAttribute.MatMulAttributeAddBZp
1132 MatMulAttribute.End = MatMulAttribute.MatMulAttributeEnd
1133 from tosa import FullyConnectedAttribute
1134
1135 if not hasattr(FullyConnectedAttribute, "Start"):
1136 FullyConnectedAttribute.Start = (
1137 FullyConnectedAttribute.FullyConnectedAttributeStart
1138 )
1139 FullyConnectedAttribute.AddInputZp = (
1140 FullyConnectedAttribute.FullyConnectedAttributeAddInputZp
1141 )
1142 FullyConnectedAttribute.AddWeightZp = (
1143 FullyConnectedAttribute.FullyConnectedAttributeAddWeightZp
1144 )
1145 FullyConnectedAttribute.End = (
1146 FullyConnectedAttribute.FullyConnectedAttributeEnd
1147 )
1148 from tosa import NegateAttribute
1149
1150 if not hasattr(NegateAttribute, "Start"):
1151 NegateAttribute.Start = NegateAttribute.NegateAttributeStart
1152 NegateAttribute.AddInput1Zp = NegateAttribute.NegateAttributeAddInput1Zp
1153 NegateAttribute.AddOutputZp = NegateAttribute.NegateAttributeAddOutputZp
1154 NegateAttribute.End = NegateAttribute.NegateAttributeEnd
Eric Kunzeae906de2022-05-30 22:40:47 -07001155 from tosa import WhileLoopAttribute
1156
1157 if not hasattr(WhileLoopAttribute, "Start"):
1158 WhileLoopAttribute.Start = WhileLoopAttribute.WhileLoopAttributeStart
1159 WhileLoopAttribute.AddCondBranch = (
1160 WhileLoopAttribute.WhileLoopAttributeAddCondBranch
1161 )
1162 WhileLoopAttribute.AddBodyBranch = (
1163 WhileLoopAttribute.WhileLoopAttributeAddBodyBranch
1164 )
1165 WhileLoopAttribute.End = WhileLoopAttribute.WhileLoopAttributeEnd