blob: c723617e7d9f4e2c0339a6bd854b7d7870141e93 [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 Kunzeb2fdef22022-08-29 11:53:18 -070033TOSA_VERSION_MINOR = 40
Kevin Chengb97cb1d2021-10-14 11:53:39 -070034TOSA_VERSION_PATCH = 0
Eric Kunze011a3332022-08-30 21:12:01 +000035TOSA_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 = []
TatWai Chong49b1ca62022-06-10 01:49:13 -070093 self.int16vecs = []
Kevin Chengfea5a372021-10-11 18:38:47 +000094 self.intvecs = []
95 self.fpvecs = []
96
97 def serialize(self, builder):
98
99 # We have to build strings and vectors first
100 strList = []
101 intVecList = []
102 fpVecList = []
103
104 for fcn, val in self.strings:
105 strList.append((fcn, builder.CreateString(val)))
106
107 for fcn, val in self.intvecs:
108 intVecList.append((fcn, TosaSerializer.serializeInt32Vec(builder, val)))
109
TatWai Chong49b1ca62022-06-10 01:49:13 -0700110 for fcn, val in self.int16vecs:
111 intVecList.append((fcn, TosaSerializer.serializeInt16Vec(builder, val)))
112
Kevin Chengfea5a372021-10-11 18:38:47 +0000113 for fcn, val in self.fpvecs:
114 fpVecList.append((fcn, TosaSerializer.serializeFpVec(builder, val)))
115
116 startFcn, endFcn = self.optFcns
117
118 # Then serialize the options object from the list of primitives and
119 # other serialized values
120 startFcn(builder)
121 for fcn, val in self.ints:
122 fcn(builder, val)
123
124 for fcn, val in self.bools:
125 fcn(builder, val)
126
127 for fcn, val in self.floats:
128 fcn(builder, val)
129
130 for fcn, val in strList:
131 fcn(builder, val)
132
133 for fcn, val in intVecList:
134 fcn(builder, val)
135
136 for fcn, val in fpVecList:
137 fcn(builder, val)
138
139 return endFcn(builder)
140
141
142class TosaSerializerAttribute(TosaSerializerUnion):
143 """This class handles encapsulating all of the enumerated types for attributes"""
144
145 def __init__(self):
146 super().__init__()
147
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000148 def PoolAttribute(self, kernel, stride, pad, input_zp, output_zp):
Kevin Chengfea5a372021-10-11 18:38:47 +0000149 from tosa import PoolAttribute as a, Attribute
150
151 self.utype = Attribute.Attribute().PoolAttribute
152
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800153 self.optFcns = (a.Start, a.End)
TatWai Chong7be71652022-05-10 17:26:20 -0700154 self.intvecs.append((a.AddPad, pad))
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800155 self.intvecs.append((a.AddKernel, kernel))
156 self.intvecs.append((a.AddStride, stride))
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000157 self.ints.append((a.AddInputZp, input_zp))
158 self.ints.append((a.AddOutputZp, output_zp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000159
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000160 def ConvAttribute(self, pad, stride, dilation, input_zp, weight_zp):
Kevin Chengfea5a372021-10-11 18:38:47 +0000161 from tosa import ConvAttribute as a, Attribute
162
163 self.utype = Attribute.Attribute().ConvAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800164 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000165
TatWai Chong7be71652022-05-10 17:26:20 -0700166 self.intvecs.append((a.AddPad, pad))
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800167 self.intvecs.append((a.AddStride, stride))
168 self.intvecs.append((a.AddDilation, dilation))
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000169 self.ints.append((a.AddInputZp, input_zp))
170 self.ints.append((a.AddWeightZp, weight_zp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000171
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000172 def TransposeConvAttribute(self, outpad, stride, output_shape, input_zp, weight_zp):
Kevin Chengfea5a372021-10-11 18:38:47 +0000173 from tosa import TransposeConvAttribute as a, Attribute
174
175 self.utype = Attribute.Attribute().TransposeConvAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800176 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000177
Eric Kunze4c3537d2022-06-13 17:21:48 -0700178 self.intvecs.append((a.AddOutPad, outpad))
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800179 self.intvecs.append((a.AddStride, stride))
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800180 self.intvecs.append((a.AddOutputShape, output_shape))
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000181 self.ints.append((a.AddInputZp, input_zp))
182 self.ints.append((a.AddWeightZp, weight_zp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000183
Kevin Cheng38d214c2021-10-15 15:49:19 -0700184 def PadAttribute(self, padding, pad_const_int, pad_const_fp):
185 from tosa import PadAttribute as a, Attribute
Kevin Chengfea5a372021-10-11 18:38:47 +0000186
Kevin Cheng38d214c2021-10-15 15:49:19 -0700187 self.utype = Attribute.Attribute().PadAttribute
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.intvecs.append((a.AddPadding, padding))
191 self.ints.append((a.AddPadConstInt, pad_const_int))
192 self.floats.append((a.AddPadConstFp, pad_const_fp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000193
194 def AxisAttribute(self, axis):
195 from tosa import AxisAttribute as a, Attribute
196
197 self.utype = Attribute.Attribute().AxisAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800198 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000199
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800200 self.ints.append((a.AddAxis, axis))
Kevin Chengfea5a372021-10-11 18:38:47 +0000201
TatWai Chong7be71652022-05-10 17:26:20 -0700202 def ReshapeAttribute(self, new_shape):
Kevin Chengfea5a372021-10-11 18:38:47 +0000203 from tosa import ReshapeAttribute as a, Attribute
204
205 self.utype = Attribute.Attribute().ReshapeAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800206 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000207
TatWai Chong7be71652022-05-10 17:26:20 -0700208 self.intvecs.append((a.AddNewShape, new_shape))
Kevin Chengfea5a372021-10-11 18:38:47 +0000209
TatWai Chong7be71652022-05-10 17:26:20 -0700210 def SliceAttribute(self, start, size):
Kevin Chengfea5a372021-10-11 18:38:47 +0000211 from tosa import SliceAttribute as a, Attribute
212
213 self.utype = Attribute.Attribute().SliceAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800214 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000215
TatWai Chong7be71652022-05-10 17:26:20 -0700216 self.intvecs.append((a.AddStart, start))
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800217 self.intvecs.append((a.AddSize, size))
Kevin Chengfea5a372021-10-11 18:38:47 +0000218
219 def TileAttribute(self, multiples):
220 from tosa import TileAttribute as a, Attribute
221
222 self.utype = Attribute.Attribute().TileAttribute
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.AddMultiples, multiples))
Kevin Chengfea5a372021-10-11 18:38:47 +0000226
TatWai Chong49b1ca62022-06-10 01:49:13 -0700227 def ResizeAttribute(self, scale, offset, border, mode):
Kevin Chengfea5a372021-10-11 18:38:47 +0000228 from tosa import ResizeAttribute as a, Attribute
229
230 self.utype = Attribute.Attribute().ResizeAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800231 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000232
TatWai Chong49b1ca62022-06-10 01:49:13 -0700233 self.int16vecs.append((a.AddScale, scale))
234 self.int16vecs.append((a.AddOffset, offset))
235 self.int16vecs.append((a.AddBorder, border))
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800236 self.ints.append((a.AddMode, mode))
Kevin Chengfea5a372021-10-11 18:38:47 +0000237
238 def ClampAttribute(self, minint, maxint, minfp, maxfp):
239 from tosa import ClampAttribute as a, Attribute
240
241 self.utype = Attribute.Attribute().ClampAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800242 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000243
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800244 self.ints.append((a.AddMinInt, minint))
245 self.ints.append((a.AddMaxInt, maxint))
Kevin Chengfea5a372021-10-11 18:38:47 +0000246
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800247 self.ints.append((a.AddMinFp, minfp))
248 self.ints.append((a.AddMaxFp, maxfp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000249
250 def RescaleAttribute(
251 self, input_zp, output_zp, multiplier, shift, scale32, double_round, per_channel
252 ):
253 from tosa import RescaleAttribute as a, Attribute
254
255 self.utype = Attribute.Attribute().RescaleAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800256 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000257
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800258 self.ints.append((a.AddInputZp, input_zp))
259 self.ints.append((a.AddOutputZp, output_zp))
260 self.intvecs.append((a.AddMultiplier, multiplier))
261 self.intvecs.append((a.AddShift, shift))
262 self.bools.append((a.AddScale32, scale32))
263 self.bools.append((a.AddDoubleRound, double_round))
264 self.bools.append((a.AddPerChannel, per_channel))
Kevin Chengfea5a372021-10-11 18:38:47 +0000265
266 def MulAttribute(self, shift):
267 from tosa import MulAttribute as a, Attribute
268
269 self.utype = Attribute.Attribute().MulAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800270 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000271
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800272 self.ints.append((a.AddShift, shift))
Kevin Chengfea5a372021-10-11 18:38:47 +0000273
274 def ArithmeticRightShiftAttribute(self, round):
275 from tosa import ArithmeticRightShiftAttribute as a, Attribute
276
277 self.utype = Attribute.Attribute().ArithmeticRightShiftAttribute
278 self.optFcns = (
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800279 a.Start,
280 a.End,
Kevin Chengfea5a372021-10-11 18:38:47 +0000281 )
282
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800283 self.bools.append((a.AddRound, round))
Kevin Chengfea5a372021-10-11 18:38:47 +0000284
Kevin Chengfea5a372021-10-11 18:38:47 +0000285 def CondIfAttribute(self, then_branch, else_branch):
286 from tosa import CondIfAttribute as a, Attribute
287
288 self.utype = Attribute.Attribute().CondIfAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800289 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000290
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800291 self.strings.append((a.AddThenBranch, then_branch))
292 self.strings.append((a.AddElseBranch, else_branch))
Kevin Chengfea5a372021-10-11 18:38:47 +0000293
294 def WhileLoopAttribute(self, cond_branch, body_branch):
295 from tosa import WhileLoopAttribute as a, Attribute
296
297 self.utype = Attribute.Attribute().WhileLoopAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800298 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000299
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800300 self.strings.append((a.AddCondBranch, cond_branch))
301 self.strings.append((a.AddBodyBranch, body_branch))
Kevin Chengfea5a372021-10-11 18:38:47 +0000302
TatWai Chong7be71652022-05-10 17:26:20 -0700303 def TransposeAttribute(self, perms):
Kevin Cheng38d214c2021-10-15 15:49:19 -0700304 from tosa import TransposeAttribute as a, Attribute
305
306 self.utype = Attribute.Attribute().TransposeAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800307 self.optFcns = (a.Start, a.End)
Kevin Cheng38d214c2021-10-15 15:49:19 -0700308
TatWai Chong7be71652022-05-10 17:26:20 -0700309 self.intvecs.append((a.AddPerms, perms))
Kevin Cheng38d214c2021-10-15 15:49:19 -0700310
311 def TableAttribute(self, table):
312 from tosa import TableAttribute as a, Attribute
313
314 self.utype = Attribute.Attribute().TableAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800315 self.optFcns = (a.Start, a.End)
Kevin Cheng38d214c2021-10-15 15:49:19 -0700316
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800317 self.intvecs.append((a.AddTable, table))
Kevin Chengfea5a372021-10-11 18:38:47 +0000318
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000319 def MatMulAttribute(self, A_zp, B_zp):
320 from tosa import MatMulAttribute as a, Attribute
Jeremy Johnson9b225172021-12-14 16:34:47 +0000321
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000322 self.utype = Attribute.Attribute().MatMulAttribute
323 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000324
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000325 self.ints.append((a.AddAZp, A_zp))
326 self.ints.append((a.AddBZp, B_zp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000327
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000328 def FullyConnectedAttribute(self, input_zp, weight_zp):
329 from tosa import FullyConnectedAttribute as a, Attribute
Kevin Chengfea5a372021-10-11 18:38:47 +0000330
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000331 self.utype = Attribute.Attribute().FullyConnectedAttribute
332 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000333
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000334 self.ints.append((a.AddInputZp, input_zp))
335 self.ints.append((a.AddWeightZp, weight_zp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000336
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000337 def NegateAttribute(self, input1_zp, output_zp):
338 from tosa import NegateAttribute as a, Attribute
Kevin Chengfea5a372021-10-11 18:38:47 +0000339
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000340 self.utype = Attribute.Attribute().NegateAttribute
341 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000342
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000343 self.ints.append((a.AddInput1Zp, input1_zp))
344 self.ints.append((a.AddOutputZp, output_zp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000345
346
347class TosaSerializerTensor:
348 def __init__(
349 self,
350 name,
351 shape,
352 dtype,
353 data=None,
354 placeholderFilename=None,
355 ):
356 self.name = name
357
358 if isinstance(shape, np.ndarray):
359 shape = shape.astype(int).tolist()
360 shape = list(map(int, shape))
361
362 self.shape = shape
363 self.dtype = dtype
364
365 if isinstance(data, np.ndarray):
366 data = data.flatten().astype(int).tolist()
367 data = list(map(int, data))
368 self.data = data
369 elif isinstance(data, list):
370 data = list(map(int, data))
371 self.data = data
372 else:
373 self.data = None
374
375 # Filename for placeholder tensors. These get generated by the test generation
Jeremy Johnson9b225172021-12-14 16:34:47 +0000376 # process and are written to disk, but are considered input tensors by the
377 # network so they do not appear in the TOSA serialiazation. However, if we
378 # want to form a unit test around these input tensors, we can get the filename
379 # from here.
Kevin Chengfea5a372021-10-11 18:38:47 +0000380 self.placeholderFilename = placeholderFilename
381
382 def __str__(self):
383 str = "TosaSerializerTensor name: {} shape: {} dtype: {}".format(
384 self.name,
385 self.shape,
386 DTypeNames[self.dtype],
387 )
388 return str
389
390 def setDtype(self, dtype):
391 self.dtype = dtype
392
393 def serialize(self, builder):
394 fb_name = builder.CreateString(self.name)
395 fb_shapes = TosaSerializer.serializeInt32Vec(builder, self.shape)
396 if self.data:
397 u8_data = list()
398 # little endianess
399 if self.dtype == DType.BOOL:
400 for val in self.data:
401 val_u8 = np.uint8(val)
402 u8_data.append(val_u8)
403 elif self.dtype == DType.INT4:
404 in_size = len(self.data)
405 out_size = (in_size + 1) // 2
406 for i in range(out_size):
407 val_0 = self.data[2 * i]
408 if (2 * i + 1) < in_size:
409 val_1 = self.data[2 * i + 1]
410 else:
411 val_1 = 0
412 val_i8 = (val_0 & 0xF) | ((val_1 & 0xF) << 4)
413 val_u8 = np.uint8(val_i8)
414 u8_data.append(val_u8)
415 elif self.dtype == DType.INT8:
416 for val in self.data:
417 val_u8 = np.uint8(val)
418 u8_data.append(val_u8)
419 elif self.dtype == DType.INT16:
420 for val in self.data:
421 val_u16 = np.uint16(val)
422 b0 = val_u16 & ByteMask
423 b1 = (val_u16 >> np.uint16(8)) & ByteMask
424 u8_data.extend([b0, b1])
425 elif self.dtype == DType.INT32:
426 for val in self.data:
427 val_u32 = np.uint32(val)
428 b0 = val_u32 & ByteMask
429 b1 = (val_u32 >> np.uint32(8)) & ByteMask
430 b2 = (val_u32 >> np.uint32(16)) & ByteMask
Kevin Cheng6b078ca2021-10-13 23:12:50 -0700431 b3 = (val_u32 >> np.uint32(24)) & ByteMask
Kevin Chengfea5a372021-10-11 18:38:47 +0000432 u8_data.extend([b0, b1, b2, b3])
433 elif self.dtype == DType.INT48:
434 for val in self.data:
435 val_u64 = np.uint64(val)
436 b0 = val_u64 & ByteMask
437 b1 = (val_u64 >> np.uint64(8)) & ByteMask
438 b2 = (val_u64 >> np.uint64(16)) & ByteMask
439 b3 = (val_u64 >> np.uint64(24)) & ByteMask
440 b4 = (val_u64 >> np.uint64(32)) & ByteMask
441 b5 = (val_u64 >> np.uint64(40)) & ByteMask
442 u8_data.extend([b0, b1, b2, b3, b4, b5])
443 elif self.dtype == DType.FLOAT:
444 for val in self.data:
445 b = struct.pack("!f", val)
446 u8_data.extend([b[3], b[2], b[1], b[0]])
447 else:
448 raise Exception(
449 "unsupported data type {}".format(DTypeNames[self.dtype])
450 )
451 fb_data = TosaSerializer.serializeUint8Vec(builder, u8_data)
452
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800453 TosaTensor.Start(builder)
454 TosaTensor.AddName(builder, fb_name)
455 TosaTensor.AddShape(builder, fb_shapes)
456 TosaTensor.AddType(builder, self.dtype)
Kevin Chengfea5a372021-10-11 18:38:47 +0000457 if self.data:
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800458 TosaTensor.AddData(builder, fb_data)
Kevin Chengfea5a372021-10-11 18:38:47 +0000459
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800460 return TosaTensor.End(builder)
Kevin Chengfea5a372021-10-11 18:38:47 +0000461
462
463class TosaSerializerOperator:
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000464 def __init__(self, op, inputs, outputs, attributes=None):
Kevin Chengfea5a372021-10-11 18:38:47 +0000465 self.op = op
466 self.attributes = attributes
467 self.inputs = TosaSerializer.toList(inputs)
468 self.outputs = TosaSerializer.toList(outputs)
Kevin Chengfea5a372021-10-11 18:38:47 +0000469
470 def __str__(self):
471 str = "Op {}\n----\n".format(self.op)
472
473 for i in self.inputs:
474 str = str + " Input: {}\n".format(i)
475 for o in self.outputs:
476 str = str + " Output: {}\n".format(o)
477
478 return str
479
480 def serialize(self, builder):
481 fb_inputs = TosaSerializer.serializeStrVec(
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800482 builder, self.inputs, TosaOperator.StartInputsVector
Kevin Chengfea5a372021-10-11 18:38:47 +0000483 )
484 fb_outputs = TosaSerializer.serializeStrVec(
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800485 builder, self.outputs, TosaOperator.StartOutputsVector
Kevin Chengfea5a372021-10-11 18:38:47 +0000486 )
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000487 # Need to serialize attributes enums still
Kevin Chengfea5a372021-10-11 18:38:47 +0000488 if self.attributes is not None:
489 fb_attributes = self.attributes.serialize(builder)
490
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800491 TosaOperator.Start(builder)
492 TosaOperator.AddOp(builder, self.op)
493 TosaOperator.AddInputs(builder, fb_inputs)
494 TosaOperator.AddOutputs(builder, fb_outputs)
Kevin Chengfea5a372021-10-11 18:38:47 +0000495 if self.attributes is not None:
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800496 TosaOperator.AddAttributeType(builder, self.attributes.utype)
497 TosaOperator.AddAttribute(builder, fb_attributes)
Kevin Chengfea5a372021-10-11 18:38:47 +0000498
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800499 return TosaOperator.End(builder)
Kevin Chengfea5a372021-10-11 18:38:47 +0000500
501
502class TosaSerializerBasicBlock:
503 def __init__(self, name):
504 self.name = name
505 self.operators = []
506
507 # Dict assures uniqueness, but allows us to look up by name
508 self.tensors = dict()
509
510 self.inputs = []
511 self.outputs = []
512
513 def addTensor(
514 self,
515 name,
516 shape,
517 dtype,
518 data=None,
519 placeholderFilename=None,
520 ):
Jeremy Johnson9b225172021-12-14 16:34:47 +0000521 if name not in self.tensors:
Kevin Chengfea5a372021-10-11 18:38:47 +0000522 self.tensors[name] = TosaSerializerTensor(
523 name, shape, dtype, data, placeholderFilename
524 )
525
526 return self.tensors[name]
527
528 def addInput(self, name):
529 self.inputs.append(name)
530
531 def addOutput(self, name):
532 self.outputs.append(name)
533
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000534 def addOperator(self, op, inputs, outputs, attributes=None):
535 self.operators.append(TosaSerializerOperator(op, inputs, outputs, attributes))
Kevin Chengfea5a372021-10-11 18:38:47 +0000536
537 def serialize(self, builder):
538 fb_name = builder.CreateString(self.name)
539 fbv_inputs = TosaSerializer.serializeStrVec(
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800540 builder, list(self.inputs), TosaBasicBlock.StartInputsVector
Kevin Chengfea5a372021-10-11 18:38:47 +0000541 )
542 fbv_outputs = TosaSerializer.serializeStrVec(
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800543 builder, list(self.outputs), TosaBasicBlock.StartOutputsVector
Kevin Chengfea5a372021-10-11 18:38:47 +0000544 )
545 fbv_tensors = TosaSerializer.serializeObjVec(
546 builder,
547 list(self.tensors.values()),
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800548 TosaBasicBlock.StartTensorsVector,
Kevin Chengfea5a372021-10-11 18:38:47 +0000549 )
550 fbv_operators = TosaSerializer.serializeObjVec(
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800551 builder, self.operators, TosaBasicBlock.StartOperatorsVector
Kevin Chengfea5a372021-10-11 18:38:47 +0000552 )
553
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800554 TosaBasicBlock.Start(builder)
555 TosaBasicBlock.AddName(builder, fb_name)
556 TosaBasicBlock.AddInputs(builder, fbv_inputs)
557 TosaBasicBlock.AddOutputs(builder, fbv_outputs)
558 TosaBasicBlock.AddTensors(builder, fbv_tensors)
559 TosaBasicBlock.AddOperators(builder, fbv_operators)
560 return TosaBasicBlock.End(builder)
Kevin Chengfea5a372021-10-11 18:38:47 +0000561
562
563@unique
564class TensorDir(IntEnum):
565 PLACEHOLDER = 0
566 CONST = 1
567 INTERMEDIATE = 2
568 RESULT = 3
569
570
571class TosaSerializer:
572 def __init__(self, pathPrefix):
Eric Kunzeae906de2022-05-30 22:40:47 -0700573 self.add_compat_methods()
Kevin Chengfea5a372021-10-11 18:38:47 +0000574 # Get the global TOSA version if not already defined
Kevin Chengfea5a372021-10-11 18:38:47 +0000575
576 self.builder = flatbuffers.Builder(0)
577
578 self.basicBlocks = []
579 self.startBasicBlock("main")
580 self.pathPrefix = pathPrefix
581
582 # Indicies used for adding/naming tensors
583 self.currInputIdx = 0
584 self.currConstIdx = 0
585 self.currLayerIdx = 1
586 self.currResultIdx = 0
587
588 # Is this an illegal test that is expected to fail?
Jeremy Johnson9b225172021-12-14 16:34:47 +0000589 self.expectedReturnCode = 0
Kevin Chengfea5a372021-10-11 18:38:47 +0000590 self.expectedFailure = False
591 self.expectedFailureDesc = ""
592
593 def __str__(self):
594 str = ""
595 for bb in self.basicBlocks:
596 str = str + bb.__str__()
597 return str
598
599 def addPlaceholder(self, shape, dtype, vals):
600 if not self.currBasicBlock:
601 raise Exception("addTensor called without valid basic block")
602
603 name = "input-{}".format(self.currInputIdx)
604 filename = "{}.npy".format(name)
605 self.currInputIdx = self.currInputIdx + 1
606
607 tens = self.currBasicBlock.addTensor(name, shape, dtype, None, filename)
608 # This is always an input to the block
609 self.currBasicBlock.addInput(name)
610
611 if vals is not None:
612 np.save(os.path.join(self.pathPrefix, filename), vals, False)
613
614 return tens
615
616 def addConst(self, shape, dtype, vals):
617 if not self.currBasicBlock:
618 raise Exception("addTensor called without valid basic block")
619
620 name = "const-{}".format(self.currInputIdx)
Kevin Chengfea5a372021-10-11 18:38:47 +0000621 self.currInputIdx = self.currInputIdx + 1
622
623 tens = self.currBasicBlock.addTensor(name, shape, dtype, vals)
624 # Add the operator now
Jeremy Johnson9b225172021-12-14 16:34:47 +0000625 self.currBasicBlock.addOperator(TosaOp.Op().CONST, [], name)
Kevin Chengfea5a372021-10-11 18:38:47 +0000626
627 return tens
628
629 def addIntermediate(self, shape, dtype):
630
631 if not self.currBasicBlock:
632 raise Exception("addTensor called without valid basic block")
633
634 name = "layer-{}".format(self.currLayerIdx)
635 self.currLayerIdx = self.currLayerIdx + 1
636
637 tens = self.currBasicBlock.addTensor(name, shape, dtype, None)
638
639 return tens
640
641 def addInputTensor(self, tensor):
642 self.currBasicBlock.addTensor(tensor.name, tensor.shape, tensor.dtype)
643 self.currBasicBlock.addInput(tensor.name)
644
645 def addOutputTensor(self, tensor):
646 self.currBasicBlock.addOutput(tensor.name)
647
648 def addOutput(self, shape, dtype):
649 if not self.currBasicBlock:
650 raise Exception("addTensor called without valid basic block")
651
652 name = "result-{}".format(self.currResultIdx)
653 self.currResultIdx = self.currResultIdx + 1
654
655 tens = self.currBasicBlock.addTensor(name, shape, dtype, None)
656 self.currBasicBlock.addOutput(name)
657 return tens
658
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000659 def addOperator(self, op, inputs, outputs, attributes=None):
Kevin Chengfea5a372021-10-11 18:38:47 +0000660
Jeremy Johnson9b225172021-12-14 16:34:47 +0000661 if op == TosaOp.Op().CONST:
Kevin Chengfea5a372021-10-11 18:38:47 +0000662 raise Exception("Use addConstTensor() to add CONST ops")
663
664 return self.currBasicBlock.addOperator(
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000665 op,
666 inputs,
667 outputs,
668 attributes,
Kevin Chengfea5a372021-10-11 18:38:47 +0000669 )
670
Jeremy Johnson9b225172021-12-14 16:34:47 +0000671 def setExpectedReturnCode(self, val, fail, desc=""):
Kevin Chengfea5a372021-10-11 18:38:47 +0000672
673 self.expectedReturnCode = val
674 self.expectedFailureDesc = desc
Jeremy Johnson9b225172021-12-14 16:34:47 +0000675 self.expectedFailure = fail
Kevin Chengfea5a372021-10-11 18:38:47 +0000676
677 def serialize(self):
678
679 builder = self.builder
680
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800681 Version.Start(builder)
682 Version.Add_major(builder, TOSA_VERSION[0])
683 Version.Add_minor(builder, TOSA_VERSION[1])
684 Version.Add_patch(builder, TOSA_VERSION[2])
685 Version.Add_draft(builder, TOSA_VERSION[3])
686 version = Version.End(builder)
Kevin Chengfea5a372021-10-11 18:38:47 +0000687
688 fbv_bb = TosaSerializer.serializeObjVec(
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800689 builder, self.basicBlocks, TosaGraph.StartBlocksVector
Kevin Chengfea5a372021-10-11 18:38:47 +0000690 )
691
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800692 TosaGraph.Start(builder)
693 TosaGraph.AddVersion(builder, version)
694 TosaGraph.AddBlocks(builder, fbv_bb)
695 graph = TosaGraph.End(builder)
Kevin Chengfea5a372021-10-11 18:38:47 +0000696
Eric Kunzee6596402022-06-09 21:27:36 +0000697 self.builder.Finish(graph, TOSA_GRAPH_IDENTIFIER)
Kevin Chengfea5a372021-10-11 18:38:47 +0000698 return self.builder.Output()
699
700 def writeJson(self, tosa_filename):
701 """Write a json test file so that it is fairly easy to pick up the test
702 and generate commands for third party tool"""
703 test_desc = dict()
704
705 test_desc["tosa_file"] = tosa_filename
706 ifm_name = []
707 ifm_file = []
708 ofm_name = []
709 ofm_file = []
710
711 for b in self.basicBlocks:
712 if b.name == "main":
713 for i in b.inputs:
714 ifm_name.append(i)
715 ifm_file.append(b.tensors[i].placeholderFilename)
716 for o in b.outputs:
717 ofm_name.append(o)
Jeremy Johnson9b225172021-12-14 16:34:47 +0000718 # Make up an OFM filename here. One isn't generated until the
719 # reference tool is run, so any name is a good name
Kevin Chengfea5a372021-10-11 18:38:47 +0000720 ofm_file.append("ref-{}.npy".format(o))
721
722 test_desc["ifm_name"] = ifm_name
723 test_desc["ifm_file"] = ifm_file
724 test_desc["ofm_name"] = ofm_name
725 test_desc["ofm_file"] = ofm_file
726 test_desc["expected_return_code"] = self.expectedReturnCode
727 test_desc["expected_failure"] = self.expectedFailure
728 if self.expectedFailureDesc:
729 test_desc["expected_failure_desc"] = self.expectedFailureDesc
730
731 return json.dumps(test_desc, indent=" ")
732
733 def startBasicBlock(self, name):
734 self.currBasicBlock = TosaSerializerBasicBlock(name)
735 self.basicBlocks.append(self.currBasicBlock)
736
737 @staticmethod
738 def serializeStrVec(builder, vec, start_fcn):
739 fb_strs = [builder.CreateString(i) for i in vec]
740 start_fcn(builder, len(fb_strs))
741 for s in fb_strs[::-1]:
742 builder.PrependUOffsetTRelative(s)
Eric Kunzeae906de2022-05-30 22:40:47 -0700743 try:
744 return builder.EndVector()
745 except TypeError:
746 return builder.EndVector(len(vec))
Kevin Chengfea5a372021-10-11 18:38:47 +0000747
748 @staticmethod
749 def serializeUint8Vec(builder, vec):
750 builder.StartVector(1, len(vec), 8)
751 for v in vec[::-1]:
752 builder.PrependUint8(v)
753 try:
754 return builder.EndVector()
755 except TypeError:
756 return builder.EndVector(len(vec))
757
758 @staticmethod
TatWai Chong49b1ca62022-06-10 01:49:13 -0700759 def serializeInt16Vec(builder, vec):
760 builder.StartVector(2, len(vec), 4)
761 for v in vec[::-1]:
762 builder.PrependInt16(v)
763 try:
764 return builder.EndVector()
765 except TypeError:
766 return builder.EndVector(len(vec))
767
768 @staticmethod
Kevin Chengfea5a372021-10-11 18:38:47 +0000769 def serializeInt32Vec(builder, vec):
770 builder.StartVector(4, len(vec), 4)
771 for v in vec[::-1]:
772 builder.PrependInt32(v)
773 try:
774 return builder.EndVector()
775 except TypeError:
776 return builder.EndVector(len(vec))
777
778 @staticmethod
779 def serializeFpVec(builder, vec):
780 builder.StartVector(4, len(vec), 4)
781 for v in vec[::-1]:
782 builder.PrependFloat32(v)
783 try:
784 return builder.EndVector()
785 except TypeError:
786 return builder.EndVector(len(vec))
787
788 @staticmethod
789 def serializeObjVec(builder, vec, start_fcn):
790 serialized_vec = []
791 for v in vec[::-1]:
792 serialized_vec.append(v.serialize(builder))
793
794 start_fcn(builder, len(vec))
795 for v in serialized_vec:
796 builder.PrependUOffsetTRelative(v)
797 try:
798 return builder.EndVector()
799 except TypeError:
800 return builder.EndVector(len(vec))
801
802 @staticmethod
803 def toList(val):
804 if isinstance(val, list):
805 return val
806 else:
807 return [val]
Eric Kunzeae906de2022-05-30 22:40:47 -0700808
809 # Remove when switching to flatbuffers 2.0
810 # contains a mapping of the deprecated 1.12 method to the 2.0 version
811
812 def add_compat_methods(self):
813
814 from tosa import ArithmeticRightShiftAttribute
815
816 if not hasattr(ArithmeticRightShiftAttribute, "Start"):
817 ArithmeticRightShiftAttribute.Start = (
818 ArithmeticRightShiftAttribute.ArithmeticRightShiftAttributeStart
819 )
820 ArithmeticRightShiftAttribute.AddRound = (
821 ArithmeticRightShiftAttribute.ArithmeticRightShiftAttributeAddRound
822 )
823 ArithmeticRightShiftAttribute.End = (
824 ArithmeticRightShiftAttribute.ArithmeticRightShiftAttributeEnd
825 )
826 from tosa import AxisAttribute
827
828 if not hasattr(AxisAttribute, "Start"):
829 AxisAttribute.Start = AxisAttribute.AxisAttributeStart
830 AxisAttribute.AddAxis = AxisAttribute.AxisAttributeAddAxis
831 AxisAttribute.End = AxisAttribute.AxisAttributeEnd
832 from tosa import ClampAttribute
833
834 if not hasattr(ClampAttribute, "Start"):
835 ClampAttribute.Start = ClampAttribute.ClampAttributeStart
836 ClampAttribute.AddMinInt = ClampAttribute.ClampAttributeAddMinInt
837 ClampAttribute.AddMaxInt = ClampAttribute.ClampAttributeAddMaxInt
838 ClampAttribute.AddMinFp = ClampAttribute.ClampAttributeAddMinFp
839 ClampAttribute.AddMaxFp = ClampAttribute.ClampAttributeAddMaxFp
840 ClampAttribute.End = ClampAttribute.ClampAttributeEnd
841 from tosa import CondIfAttribute
842
843 if not hasattr(CondIfAttribute, "Start"):
844 CondIfAttribute.Start = CondIfAttribute.CondIfAttributeStart
845 CondIfAttribute.AddThenBranch = CondIfAttribute.CondIfAttributeAddThenBranch
846 CondIfAttribute.AddElseBranch = CondIfAttribute.CondIfAttributeAddElseBranch
847 CondIfAttribute.End = CondIfAttribute.CondIfAttributeEnd
848 from tosa import ConvAttribute
849
850 if not hasattr(ConvAttribute, "Start"):
851 ConvAttribute.Start = ConvAttribute.ConvAttributeStart
852 ConvAttribute.AddPad = ConvAttribute.ConvAttributeAddPad
853 ConvAttribute.StartPadVector = ConvAttribute.ConvAttributeStartPadVector
854 ConvAttribute.AddStride = ConvAttribute.ConvAttributeAddStride
855 ConvAttribute.StartStrideVector = (
856 ConvAttribute.ConvAttributeStartStrideVector
857 )
858 ConvAttribute.AddDilation = ConvAttribute.ConvAttributeAddDilation
859 ConvAttribute.StartDilationVector = (
860 ConvAttribute.ConvAttributeStartDilationVector
861 )
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000862 ConvAttribute.AddInputZp = ConvAttribute.ConvAttributeAddInputZp
863 ConvAttribute.AddWeightZp = ConvAttribute.ConvAttributeAddWeightZp
Eric Kunzeae906de2022-05-30 22:40:47 -0700864 ConvAttribute.End = ConvAttribute.ConvAttributeEnd
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000865 from tosa import FullyConnectedAttribute
Eric Kunzeae906de2022-05-30 22:40:47 -0700866
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000867 if not hasattr(FullyConnectedAttribute, "Start"):
868 FullyConnectedAttribute.Start = (
869 FullyConnectedAttribute.FullyConnectedAttributeStart
870 )
871 FullyConnectedAttribute.AddInputZp = (
872 FullyConnectedAttribute.FullyConnectedAttributeAddInputZp
873 )
874 FullyConnectedAttribute.AddWeightZp = (
875 FullyConnectedAttribute.FullyConnectedAttributeAddWeightZp
876 )
877 FullyConnectedAttribute.End = (
878 FullyConnectedAttribute.FullyConnectedAttributeEnd
879 )
880 from tosa import MatMulAttribute
Eric Kunzeae906de2022-05-30 22:40:47 -0700881
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000882 if not hasattr(MatMulAttribute, "Start"):
883 MatMulAttribute.Start = MatMulAttribute.MatMulAttributeStart
884 MatMulAttribute.AddAZp = MatMulAttribute.MatMulAttributeAddAZp
885 MatMulAttribute.AddBZp = MatMulAttribute.MatMulAttributeAddBZp
886 MatMulAttribute.End = MatMulAttribute.MatMulAttributeEnd
887 from tosa import PoolAttribute
888
889 if not hasattr(PoolAttribute, "Start"):
890 PoolAttribute.Start = PoolAttribute.PoolAttributeStart
891 PoolAttribute.AddPad = PoolAttribute.PoolAttributeAddPad
892 PoolAttribute.StartPadVector = PoolAttribute.PoolAttributeStartPadVector
893 PoolAttribute.AddKernel = PoolAttribute.PoolAttributeAddKernel
894 PoolAttribute.StartKernelVector = (
895 PoolAttribute.PoolAttributeStartKernelVector
896 )
897 PoolAttribute.AddStride = PoolAttribute.PoolAttributeAddStride
898 PoolAttribute.StartStrideVector = (
899 PoolAttribute.PoolAttributeStartStrideVector
900 )
901 PoolAttribute.AddInputZp = PoolAttribute.PoolAttributeAddInputZp
902 PoolAttribute.AddOutputZp = PoolAttribute.PoolAttributeAddOutputZp
903 PoolAttribute.End = PoolAttribute.PoolAttributeEnd
Eric Kunzeae906de2022-05-30 22:40:47 -0700904 from tosa import MulAttribute
905
906 if not hasattr(MulAttribute, "Start"):
907 MulAttribute.Start = MulAttribute.MulAttributeStart
908 MulAttribute.AddShift = MulAttribute.MulAttributeAddShift
909 MulAttribute.End = MulAttribute.MulAttributeEnd
910 from tosa import PadAttribute
911
912 if not hasattr(PadAttribute, "Start"):
913 PadAttribute.Start = PadAttribute.PadAttributeStart
914 PadAttribute.AddPadding = PadAttribute.PadAttributeAddPadding
915 PadAttribute.StartPaddingVector = (
916 PadAttribute.PadAttributeStartPaddingVector
917 )
918 PadAttribute.AddPadConstInt = PadAttribute.PadAttributeAddPadConstInt
919 PadAttribute.AddPadConstFp = PadAttribute.PadAttributeAddPadConstFp
920 PadAttribute.End = PadAttribute.PadAttributeEnd
Eric Kunzeae906de2022-05-30 22:40:47 -0700921 from tosa import PoolAttribute
922
923 if not hasattr(PoolAttribute, "Start"):
924 PoolAttribute.Start = PoolAttribute.PoolAttributeStart
925 PoolAttribute.AddPad = PoolAttribute.PoolAttributeAddPad
926 PoolAttribute.StartPadVector = PoolAttribute.PoolAttributeStartPadVector
927 PoolAttribute.AddKernel = PoolAttribute.PoolAttributeAddKernel
928 PoolAttribute.StartKernelVector = (
929 PoolAttribute.PoolAttributeStartKernelVector
930 )
931 PoolAttribute.AddStride = PoolAttribute.PoolAttributeAddStride
932 PoolAttribute.StartStrideVector = (
933 PoolAttribute.PoolAttributeStartStrideVector
934 )
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000935 PoolAttribute.AddInputZp = PoolAttribute.PoolAttributeAddInputZp
936 PoolAttribute.AddOutputZp = PoolAttribute.PoolAttributeAddOutputZp
Eric Kunzeae906de2022-05-30 22:40:47 -0700937 PoolAttribute.End = PoolAttribute.PoolAttributeEnd
938 from tosa import RescaleAttribute
939
940 if not hasattr(RescaleAttribute, "Start"):
941 RescaleAttribute.Start = RescaleAttribute.RescaleAttributeStart
942 RescaleAttribute.AddInputZp = RescaleAttribute.RescaleAttributeAddInputZp
943 RescaleAttribute.AddOutputZp = RescaleAttribute.RescaleAttributeAddOutputZp
944 RescaleAttribute.AddMultiplier = (
945 RescaleAttribute.RescaleAttributeAddMultiplier
946 )
947 RescaleAttribute.StartMultiplierVector = (
948 RescaleAttribute.RescaleAttributeStartMultiplierVector
949 )
950 RescaleAttribute.AddShift = RescaleAttribute.RescaleAttributeAddShift
951 RescaleAttribute.StartShiftVector = (
952 RescaleAttribute.RescaleAttributeStartShiftVector
953 )
954 RescaleAttribute.AddScale32 = RescaleAttribute.RescaleAttributeAddScale32
955 RescaleAttribute.AddDoubleRound = (
956 RescaleAttribute.RescaleAttributeAddDoubleRound
957 )
958 RescaleAttribute.AddPerChannel = (
959 RescaleAttribute.RescaleAttributeAddPerChannel
960 )
961 RescaleAttribute.End = RescaleAttribute.RescaleAttributeEnd
962 from tosa import ReshapeAttribute
963
964 if not hasattr(ReshapeAttribute, "Start"):
965 ReshapeAttribute.Start = ReshapeAttribute.ReshapeAttributeStart
966 ReshapeAttribute.AddNewShape = ReshapeAttribute.ReshapeAttributeAddNewShape
967 ReshapeAttribute.StartNewShapeVector = (
968 ReshapeAttribute.ReshapeAttributeStartNewShapeVector
969 )
970 ReshapeAttribute.End = ReshapeAttribute.ReshapeAttributeEnd
971 from tosa import ResizeAttribute
972
973 if not hasattr(ResizeAttribute, "Start"):
974 ResizeAttribute.Start = ResizeAttribute.ResizeAttributeStart
TatWai Chong49b1ca62022-06-10 01:49:13 -0700975 ResizeAttribute.AddScale = ResizeAttribute.ResizeAttributeAddScale
976 ResizeAttribute.StartScaleVector = (
977 ResizeAttribute.ResizeAttributeStartScaleVector
Eric Kunzeae906de2022-05-30 22:40:47 -0700978 )
979 ResizeAttribute.AddOffset = ResizeAttribute.ResizeAttributeAddOffset
980 ResizeAttribute.StartOffsetVector = (
981 ResizeAttribute.ResizeAttributeStartOffsetVector
982 )
TatWai Chong49b1ca62022-06-10 01:49:13 -0700983 ResizeAttribute.AddBorder = ResizeAttribute.ResizeAttributeAddBorder
984 ResizeAttribute.StartBorderVector = (
985 ResizeAttribute.ResizeAttributeStartBorderVector
Eric Kunzeae906de2022-05-30 22:40:47 -0700986 )
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