blob: f4e146caf8b1eb18210373b33d3c7b9f74c34433 [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 Kunze24a68bb2022-09-08 23:54:21 +000033TOSA_VERSION_MINOR = 41
Kevin Chengb97cb1d2021-10-14 11:53:39 -070034TOSA_VERSION_PATCH = 0
Eric Kunze24a68bb2022-09-08 23:54:21 +000035TOSA_VERSION_DRAFT = True
Jeremy Johnson9b225172021-12-14 16:34:47 +000036TOSA_VERSION = [
37 TOSA_VERSION_MAJOR,
38 TOSA_VERSION_MINOR,
39 TOSA_VERSION_PATCH,
40 TOSA_VERSION_DRAFT,
41]
Eric Kunzee6596402022-06-09 21:27:36 +000042
43# File identifier needs to be kept in sync with schema/tosa.fbs
44TOSA_GRAPH_IDENTIFIER = b"\x54\x4F\x53\x41"
45
Kevin Chengfea5a372021-10-11 18:38:47 +000046# With the way flatc generates its python types, there is no programatic way
47# to get string names for the integer types. Manually maintain a string table
48# here.
Jeremy Johnson9b225172021-12-14 16:34:47 +000049DType = TosaDType.DType()
Kevin Chengfea5a372021-10-11 18:38:47 +000050DTypeNames = [
51 "UNKNOWN",
52 "BOOL",
53 "UINT8",
54 "INT4",
55 "INT8",
56 "INT16",
57 "INT32",
58 "INT48",
Jeremy Johnsone1072a92022-09-27 12:44:11 +010059 "FP32",
Jeremy Johnson41027732022-05-25 17:52:29 +010060 "UINT16",
James Ward485a11d2022-08-05 13:48:37 +010061 "FP16",
Kevin Chengfea5a372021-10-11 18:38:47 +000062]
63
64ByteMask = np.uint64(0xFF)
65
66
67def dtype_str_to_val(name):
68
69 for i in range(len(DTypeNames)):
70 if name.casefold() == DTypeNames[i].casefold():
71 return i
72 raise Exception("Unable to parse DType name {}".format(name))
73
74
75class TosaSerializerUnion:
76 """This class handles encapsulating and serializing union types into flatbuffers"""
77
78 def __init__(self):
79
Jeremy Johnson9b225172021-12-14 16:34:47 +000080 # A tuple of the start and end functions.
81 # Set by the options constructors below
Kevin Chengfea5a372021-10-11 18:38:47 +000082 self.optFcns = None
83
Jeremy Johnson9b225172021-12-14 16:34:47 +000084 # The type from the tosa.Options enumeration.
85 # Set by the options constructors below.
Kevin Chengfea5a372021-10-11 18:38:47 +000086 self.utype = None
87
88 # Each of these lists is a tuple of the add function and the
89 # value being added. Set by the options constructors below.
90 self.ints = []
91 self.bools = []
92 self.floats = []
93 self.strings = []
TatWai Chong49b1ca62022-06-10 01:49:13 -070094 self.int16vecs = []
Kevin Chengfea5a372021-10-11 18:38:47 +000095 self.intvecs = []
96 self.fpvecs = []
97
98 def serialize(self, builder):
99
100 # We have to build strings and vectors first
101 strList = []
102 intVecList = []
103 fpVecList = []
104
105 for fcn, val in self.strings:
106 strList.append((fcn, builder.CreateString(val)))
107
108 for fcn, val in self.intvecs:
109 intVecList.append((fcn, TosaSerializer.serializeInt32Vec(builder, val)))
110
TatWai Chong49b1ca62022-06-10 01:49:13 -0700111 for fcn, val in self.int16vecs:
112 intVecList.append((fcn, TosaSerializer.serializeInt16Vec(builder, val)))
113
Kevin Chengfea5a372021-10-11 18:38:47 +0000114 for fcn, val in self.fpvecs:
115 fpVecList.append((fcn, TosaSerializer.serializeFpVec(builder, val)))
116
117 startFcn, endFcn = self.optFcns
118
119 # Then serialize the options object from the list of primitives and
120 # other serialized values
121 startFcn(builder)
122 for fcn, val in self.ints:
123 fcn(builder, val)
124
125 for fcn, val in self.bools:
126 fcn(builder, val)
127
128 for fcn, val in self.floats:
129 fcn(builder, val)
130
131 for fcn, val in strList:
132 fcn(builder, val)
133
134 for fcn, val in intVecList:
135 fcn(builder, val)
136
137 for fcn, val in fpVecList:
138 fcn(builder, val)
139
140 return endFcn(builder)
141
142
143class TosaSerializerAttribute(TosaSerializerUnion):
144 """This class handles encapsulating all of the enumerated types for attributes"""
145
146 def __init__(self):
147 super().__init__()
148
James Ward485a11d2022-08-05 13:48:37 +0100149 def PoolAttribute(
150 self,
151 kernel,
152 stride,
153 pad,
154 input_zp,
155 output_zp,
156 accum_dtype,
157 ):
Kevin Chengfea5a372021-10-11 18:38:47 +0000158 from tosa import PoolAttribute as a, Attribute
159
160 self.utype = Attribute.Attribute().PoolAttribute
161
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800162 self.optFcns = (a.Start, a.End)
TatWai Chong7be71652022-05-10 17:26:20 -0700163 self.intvecs.append((a.AddPad, pad))
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800164 self.intvecs.append((a.AddKernel, kernel))
165 self.intvecs.append((a.AddStride, stride))
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000166 self.ints.append((a.AddInputZp, input_zp))
167 self.ints.append((a.AddOutputZp, output_zp))
James Ward485a11d2022-08-05 13:48:37 +0100168 self.ints.append((a.AddAccumDtype, accum_dtype))
Kevin Chengfea5a372021-10-11 18:38:47 +0000169
James Ward485a11d2022-08-05 13:48:37 +0100170 def ConvAttribute(self, pad, stride, dilation, input_zp, weight_zp, accum_dtype):
Kevin Chengfea5a372021-10-11 18:38:47 +0000171 from tosa import ConvAttribute as a, Attribute
172
173 self.utype = Attribute.Attribute().ConvAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800174 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000175
TatWai Chong7be71652022-05-10 17:26:20 -0700176 self.intvecs.append((a.AddPad, pad))
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800177 self.intvecs.append((a.AddStride, stride))
178 self.intvecs.append((a.AddDilation, dilation))
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000179 self.ints.append((a.AddInputZp, input_zp))
180 self.ints.append((a.AddWeightZp, weight_zp))
James Ward485a11d2022-08-05 13:48:37 +0100181 self.ints.append((a.AddAccumDtype, accum_dtype))
Kevin Chengfea5a372021-10-11 18:38:47 +0000182
James Ward485a11d2022-08-05 13:48:37 +0100183 def TransposeConvAttribute(
184 self, outpad, stride, output_shape, input_zp, weight_zp, accum_dtype
185 ):
Kevin Chengfea5a372021-10-11 18:38:47 +0000186 from tosa import TransposeConvAttribute as a, Attribute
187
188 self.utype = Attribute.Attribute().TransposeConvAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800189 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000190
Eric Kunze4c3537d2022-06-13 17:21:48 -0700191 self.intvecs.append((a.AddOutPad, outpad))
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800192 self.intvecs.append((a.AddStride, stride))
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800193 self.intvecs.append((a.AddOutputShape, output_shape))
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000194 self.ints.append((a.AddInputZp, input_zp))
195 self.ints.append((a.AddWeightZp, weight_zp))
James Ward485a11d2022-08-05 13:48:37 +0100196 self.ints.append((a.AddAccumDtype, accum_dtype))
Kevin Chengfea5a372021-10-11 18:38:47 +0000197
Kevin Cheng38d214c2021-10-15 15:49:19 -0700198 def PadAttribute(self, padding, pad_const_int, pad_const_fp):
199 from tosa import PadAttribute as a, Attribute
Kevin Chengfea5a372021-10-11 18:38:47 +0000200
Kevin Cheng38d214c2021-10-15 15:49:19 -0700201 self.utype = Attribute.Attribute().PadAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800202 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000203
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800204 self.intvecs.append((a.AddPadding, padding))
205 self.ints.append((a.AddPadConstInt, pad_const_int))
206 self.floats.append((a.AddPadConstFp, pad_const_fp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000207
208 def AxisAttribute(self, axis):
209 from tosa import AxisAttribute as a, Attribute
210
211 self.utype = Attribute.Attribute().AxisAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800212 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000213
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800214 self.ints.append((a.AddAxis, axis))
Kevin Chengfea5a372021-10-11 18:38:47 +0000215
TatWai Chong7be71652022-05-10 17:26:20 -0700216 def ReshapeAttribute(self, new_shape):
Kevin Chengfea5a372021-10-11 18:38:47 +0000217 from tosa import ReshapeAttribute as a, Attribute
218
219 self.utype = Attribute.Attribute().ReshapeAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800220 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000221
TatWai Chong7be71652022-05-10 17:26:20 -0700222 self.intvecs.append((a.AddNewShape, new_shape))
Kevin Chengfea5a372021-10-11 18:38:47 +0000223
TatWai Chong7be71652022-05-10 17:26:20 -0700224 def SliceAttribute(self, start, size):
Kevin Chengfea5a372021-10-11 18:38:47 +0000225 from tosa import SliceAttribute as a, Attribute
226
227 self.utype = Attribute.Attribute().SliceAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800228 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000229
TatWai Chong7be71652022-05-10 17:26:20 -0700230 self.intvecs.append((a.AddStart, start))
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800231 self.intvecs.append((a.AddSize, size))
Kevin Chengfea5a372021-10-11 18:38:47 +0000232
233 def TileAttribute(self, multiples):
234 from tosa import TileAttribute as a, Attribute
235
236 self.utype = Attribute.Attribute().TileAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800237 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000238
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800239 self.intvecs.append((a.AddMultiples, multiples))
Kevin Chengfea5a372021-10-11 18:38:47 +0000240
TatWai Chong49b1ca62022-06-10 01:49:13 -0700241 def ResizeAttribute(self, scale, offset, border, mode):
Kevin Chengfea5a372021-10-11 18:38:47 +0000242 from tosa import ResizeAttribute as a, Attribute
243
244 self.utype = Attribute.Attribute().ResizeAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800245 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000246
TatWai Chong49b1ca62022-06-10 01:49:13 -0700247 self.int16vecs.append((a.AddScale, scale))
248 self.int16vecs.append((a.AddOffset, offset))
249 self.int16vecs.append((a.AddBorder, border))
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800250 self.ints.append((a.AddMode, mode))
Kevin Chengfea5a372021-10-11 18:38:47 +0000251
252 def ClampAttribute(self, minint, maxint, minfp, maxfp):
253 from tosa import ClampAttribute as a, Attribute
254
255 self.utype = Attribute.Attribute().ClampAttribute
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.AddMinInt, minint))
259 self.ints.append((a.AddMaxInt, maxint))
Kevin Chengfea5a372021-10-11 18:38:47 +0000260
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800261 self.ints.append((a.AddMinFp, minfp))
262 self.ints.append((a.AddMaxFp, maxfp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000263
264 def RescaleAttribute(
265 self, input_zp, output_zp, multiplier, shift, scale32, double_round, per_channel
266 ):
267 from tosa import RescaleAttribute as a, Attribute
268
269 self.utype = Attribute.Attribute().RescaleAttribute
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.AddInputZp, input_zp))
273 self.ints.append((a.AddOutputZp, output_zp))
274 self.intvecs.append((a.AddMultiplier, multiplier))
275 self.intvecs.append((a.AddShift, shift))
276 self.bools.append((a.AddScale32, scale32))
277 self.bools.append((a.AddDoubleRound, double_round))
278 self.bools.append((a.AddPerChannel, per_channel))
Kevin Chengfea5a372021-10-11 18:38:47 +0000279
280 def MulAttribute(self, shift):
281 from tosa import MulAttribute as a, Attribute
282
283 self.utype = Attribute.Attribute().MulAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800284 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000285
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800286 self.ints.append((a.AddShift, shift))
Kevin Chengfea5a372021-10-11 18:38:47 +0000287
288 def ArithmeticRightShiftAttribute(self, round):
289 from tosa import ArithmeticRightShiftAttribute as a, Attribute
290
291 self.utype = Attribute.Attribute().ArithmeticRightShiftAttribute
292 self.optFcns = (
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800293 a.Start,
294 a.End,
Kevin Chengfea5a372021-10-11 18:38:47 +0000295 )
296
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800297 self.bools.append((a.AddRound, round))
Kevin Chengfea5a372021-10-11 18:38:47 +0000298
Kevin Chengfea5a372021-10-11 18:38:47 +0000299 def CondIfAttribute(self, then_branch, else_branch):
300 from tosa import CondIfAttribute as a, Attribute
301
302 self.utype = Attribute.Attribute().CondIfAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800303 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000304
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800305 self.strings.append((a.AddThenBranch, then_branch))
306 self.strings.append((a.AddElseBranch, else_branch))
Kevin Chengfea5a372021-10-11 18:38:47 +0000307
308 def WhileLoopAttribute(self, cond_branch, body_branch):
309 from tosa import WhileLoopAttribute as a, Attribute
310
311 self.utype = Attribute.Attribute().WhileLoopAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800312 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000313
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800314 self.strings.append((a.AddCondBranch, cond_branch))
315 self.strings.append((a.AddBodyBranch, body_branch))
Kevin Chengfea5a372021-10-11 18:38:47 +0000316
TatWai Chong7be71652022-05-10 17:26:20 -0700317 def TransposeAttribute(self, perms):
Kevin Cheng38d214c2021-10-15 15:49:19 -0700318 from tosa import TransposeAttribute as a, Attribute
319
320 self.utype = Attribute.Attribute().TransposeAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800321 self.optFcns = (a.Start, a.End)
Kevin Cheng38d214c2021-10-15 15:49:19 -0700322
TatWai Chong7be71652022-05-10 17:26:20 -0700323 self.intvecs.append((a.AddPerms, perms))
Kevin Cheng38d214c2021-10-15 15:49:19 -0700324
325 def TableAttribute(self, table):
326 from tosa import TableAttribute as a, Attribute
327
328 self.utype = Attribute.Attribute().TableAttribute
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800329 self.optFcns = (a.Start, a.End)
Kevin Cheng38d214c2021-10-15 15:49:19 -0700330
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800331 self.intvecs.append((a.AddTable, table))
Kevin Chengfea5a372021-10-11 18:38:47 +0000332
James Ward485a11d2022-08-05 13:48:37 +0100333 def MatMulAttribute(self, A_zp, B_zp, accum_dtype):
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000334 from tosa import MatMulAttribute as a, Attribute
Jeremy Johnson9b225172021-12-14 16:34:47 +0000335
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000336 self.utype = Attribute.Attribute().MatMulAttribute
337 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000338
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000339 self.ints.append((a.AddAZp, A_zp))
340 self.ints.append((a.AddBZp, B_zp))
James Ward485a11d2022-08-05 13:48:37 +0100341 self.ints.append((a.AddAccumDtype, accum_dtype))
Kevin Chengfea5a372021-10-11 18:38:47 +0000342
James Ward485a11d2022-08-05 13:48:37 +0100343 def FullyConnectedAttribute(self, input_zp, weight_zp, accum_dtype):
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000344 from tosa import FullyConnectedAttribute as a, Attribute
Kevin Chengfea5a372021-10-11 18:38:47 +0000345
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000346 self.utype = Attribute.Attribute().FullyConnectedAttribute
347 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000348
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000349 self.ints.append((a.AddInputZp, input_zp))
350 self.ints.append((a.AddWeightZp, weight_zp))
James Ward485a11d2022-08-05 13:48:37 +0100351 self.ints.append((a.AddAccumDtype, accum_dtype))
Kevin Chengfea5a372021-10-11 18:38:47 +0000352
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000353 def NegateAttribute(self, input1_zp, output_zp):
354 from tosa import NegateAttribute as a, Attribute
Kevin Chengfea5a372021-10-11 18:38:47 +0000355
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000356 self.utype = Attribute.Attribute().NegateAttribute
357 self.optFcns = (a.Start, a.End)
Kevin Chengfea5a372021-10-11 18:38:47 +0000358
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000359 self.ints.append((a.AddInput1Zp, input1_zp))
360 self.ints.append((a.AddOutputZp, output_zp))
Kevin Chengfea5a372021-10-11 18:38:47 +0000361
362
363class TosaSerializerTensor:
364 def __init__(
365 self,
366 name,
367 shape,
368 dtype,
369 data=None,
370 placeholderFilename=None,
371 ):
372 self.name = name
373
374 if isinstance(shape, np.ndarray):
375 shape = shape.astype(int).tolist()
376 shape = list(map(int, shape))
377
378 self.shape = shape
379 self.dtype = dtype
380
Jeremy Johnsone1072a92022-09-27 12:44:11 +0100381 if dtype == DType.FP32:
Jeremy Johnsonc92710d2022-09-15 12:16:07 +0100382 fntype = np.float32
James Ward485a11d2022-08-05 13:48:37 +0100383 elif dtype == DType.FP16:
384 fntype = np.float16
Jeremy Johnsonc92710d2022-09-15 12:16:07 +0100385 else:
386 fntype = int
387
Kevin Chengfea5a372021-10-11 18:38:47 +0000388 if isinstance(data, np.ndarray):
Jeremy Johnsonc92710d2022-09-15 12:16:07 +0100389 data = data.flatten().astype(fntype).tolist()
390 data = list(map(fntype, data))
Kevin Chengfea5a372021-10-11 18:38:47 +0000391 self.data = data
392 elif isinstance(data, list):
Jeremy Johnsonc92710d2022-09-15 12:16:07 +0100393 data = list(map(fntype, data))
Kevin Chengfea5a372021-10-11 18:38:47 +0000394 self.data = data
395 else:
396 self.data = None
397
398 # Filename for placeholder tensors. These get generated by the test generation
Jeremy Johnson9b225172021-12-14 16:34:47 +0000399 # process and are written to disk, but are considered input tensors by the
400 # network so they do not appear in the TOSA serialiazation. However, if we
401 # want to form a unit test around these input tensors, we can get the filename
402 # from here.
Kevin Chengfea5a372021-10-11 18:38:47 +0000403 self.placeholderFilename = placeholderFilename
404
405 def __str__(self):
406 str = "TosaSerializerTensor name: {} shape: {} dtype: {}".format(
407 self.name,
408 self.shape,
409 DTypeNames[self.dtype],
410 )
411 return str
412
413 def setDtype(self, dtype):
414 self.dtype = dtype
415
416 def serialize(self, builder):
417 fb_name = builder.CreateString(self.name)
418 fb_shapes = TosaSerializer.serializeInt32Vec(builder, self.shape)
419 if self.data:
420 u8_data = list()
421 # little endianess
422 if self.dtype == DType.BOOL:
423 for val in self.data:
424 val_u8 = np.uint8(val)
425 u8_data.append(val_u8)
426 elif self.dtype == DType.INT4:
427 in_size = len(self.data)
428 out_size = (in_size + 1) // 2
429 for i in range(out_size):
430 val_0 = self.data[2 * i]
431 if (2 * i + 1) < in_size:
432 val_1 = self.data[2 * i + 1]
433 else:
434 val_1 = 0
435 val_i8 = (val_0 & 0xF) | ((val_1 & 0xF) << 4)
436 val_u8 = np.uint8(val_i8)
437 u8_data.append(val_u8)
438 elif self.dtype == DType.INT8:
439 for val in self.data:
440 val_u8 = np.uint8(val)
441 u8_data.append(val_u8)
442 elif self.dtype == DType.INT16:
443 for val in self.data:
444 val_u16 = np.uint16(val)
445 b0 = val_u16 & ByteMask
446 b1 = (val_u16 >> np.uint16(8)) & ByteMask
447 u8_data.extend([b0, b1])
448 elif self.dtype == DType.INT32:
449 for val in self.data:
450 val_u32 = np.uint32(val)
451 b0 = val_u32 & ByteMask
452 b1 = (val_u32 >> np.uint32(8)) & ByteMask
453 b2 = (val_u32 >> np.uint32(16)) & ByteMask
Kevin Cheng6b078ca2021-10-13 23:12:50 -0700454 b3 = (val_u32 >> np.uint32(24)) & ByteMask
Kevin Chengfea5a372021-10-11 18:38:47 +0000455 u8_data.extend([b0, b1, b2, b3])
456 elif self.dtype == DType.INT48:
457 for val in self.data:
458 val_u64 = np.uint64(val)
459 b0 = val_u64 & ByteMask
460 b1 = (val_u64 >> np.uint64(8)) & ByteMask
461 b2 = (val_u64 >> np.uint64(16)) & ByteMask
462 b3 = (val_u64 >> np.uint64(24)) & ByteMask
463 b4 = (val_u64 >> np.uint64(32)) & ByteMask
464 b5 = (val_u64 >> np.uint64(40)) & ByteMask
465 u8_data.extend([b0, b1, b2, b3, b4, b5])
James Ward485a11d2022-08-05 13:48:37 +0100466 elif self.dtype == DType.FP16:
467 np_arr = np.array(self.data, dtype=np.float16)
468 u8_data.extend(np_arr.view(np.uint8))
Jeremy Johnsone1072a92022-09-27 12:44:11 +0100469 elif self.dtype == DType.FP32:
Kevin Chengfea5a372021-10-11 18:38:47 +0000470 for val in self.data:
471 b = struct.pack("!f", val)
472 u8_data.extend([b[3], b[2], b[1], b[0]])
James Ward485a11d2022-08-05 13:48:37 +0100473 elif self.dtype == TosaDType.DType:
474 # Serialize DType enum data as uint8 bytes
475 for val in self.data:
476 np_arr = np.array(self.data, dtype=np.uint32)
477 u8_data.extend(np_arr.view(np.uint8))
Kevin Chengfea5a372021-10-11 18:38:47 +0000478 else:
479 raise Exception(
480 "unsupported data type {}".format(DTypeNames[self.dtype])
481 )
482 fb_data = TosaSerializer.serializeUint8Vec(builder, u8_data)
483
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800484 TosaTensor.Start(builder)
485 TosaTensor.AddName(builder, fb_name)
486 TosaTensor.AddShape(builder, fb_shapes)
487 TosaTensor.AddType(builder, self.dtype)
Kevin Chengfea5a372021-10-11 18:38:47 +0000488 if self.data:
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800489 TosaTensor.AddData(builder, fb_data)
Kevin Chengfea5a372021-10-11 18:38:47 +0000490
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800491 return TosaTensor.End(builder)
Kevin Chengfea5a372021-10-11 18:38:47 +0000492
493
494class TosaSerializerOperator:
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000495 def __init__(self, op, inputs, outputs, attributes=None):
Kevin Chengfea5a372021-10-11 18:38:47 +0000496 self.op = op
497 self.attributes = attributes
498 self.inputs = TosaSerializer.toList(inputs)
499 self.outputs = TosaSerializer.toList(outputs)
Kevin Chengfea5a372021-10-11 18:38:47 +0000500
501 def __str__(self):
502 str = "Op {}\n----\n".format(self.op)
503
504 for i in self.inputs:
505 str = str + " Input: {}\n".format(i)
506 for o in self.outputs:
507 str = str + " Output: {}\n".format(o)
508
509 return str
510
511 def serialize(self, builder):
512 fb_inputs = TosaSerializer.serializeStrVec(
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800513 builder, self.inputs, TosaOperator.StartInputsVector
Kevin Chengfea5a372021-10-11 18:38:47 +0000514 )
515 fb_outputs = TosaSerializer.serializeStrVec(
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800516 builder, self.outputs, TosaOperator.StartOutputsVector
Kevin Chengfea5a372021-10-11 18:38:47 +0000517 )
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000518 # Need to serialize attributes enums still
Kevin Chengfea5a372021-10-11 18:38:47 +0000519 if self.attributes is not None:
520 fb_attributes = self.attributes.serialize(builder)
521
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800522 TosaOperator.Start(builder)
523 TosaOperator.AddOp(builder, self.op)
524 TosaOperator.AddInputs(builder, fb_inputs)
525 TosaOperator.AddOutputs(builder, fb_outputs)
Kevin Chengfea5a372021-10-11 18:38:47 +0000526 if self.attributes is not None:
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800527 TosaOperator.AddAttributeType(builder, self.attributes.utype)
528 TosaOperator.AddAttribute(builder, fb_attributes)
Kevin Chengfea5a372021-10-11 18:38:47 +0000529
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800530 return TosaOperator.End(builder)
Kevin Chengfea5a372021-10-11 18:38:47 +0000531
532
533class TosaSerializerBasicBlock:
534 def __init__(self, name):
535 self.name = name
536 self.operators = []
537
538 # Dict assures uniqueness, but allows us to look up by name
539 self.tensors = dict()
540
541 self.inputs = []
542 self.outputs = []
543
544 def addTensor(
545 self,
546 name,
547 shape,
548 dtype,
549 data=None,
550 placeholderFilename=None,
551 ):
Jeremy Johnson9b225172021-12-14 16:34:47 +0000552 if name not in self.tensors:
Kevin Chengfea5a372021-10-11 18:38:47 +0000553 self.tensors[name] = TosaSerializerTensor(
554 name, shape, dtype, data, placeholderFilename
555 )
556
557 return self.tensors[name]
558
559 def addInput(self, name):
560 self.inputs.append(name)
561
562 def addOutput(self, name):
563 self.outputs.append(name)
564
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000565 def addOperator(self, op, inputs, outputs, attributes=None):
566 self.operators.append(TosaSerializerOperator(op, inputs, outputs, attributes))
Kevin Chengfea5a372021-10-11 18:38:47 +0000567
568 def serialize(self, builder):
569 fb_name = builder.CreateString(self.name)
570 fbv_inputs = TosaSerializer.serializeStrVec(
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800571 builder, list(self.inputs), TosaBasicBlock.StartInputsVector
Kevin Chengfea5a372021-10-11 18:38:47 +0000572 )
573 fbv_outputs = TosaSerializer.serializeStrVec(
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800574 builder, list(self.outputs), TosaBasicBlock.StartOutputsVector
Kevin Chengfea5a372021-10-11 18:38:47 +0000575 )
576 fbv_tensors = TosaSerializer.serializeObjVec(
577 builder,
578 list(self.tensors.values()),
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800579 TosaBasicBlock.StartTensorsVector,
Kevin Chengfea5a372021-10-11 18:38:47 +0000580 )
581 fbv_operators = TosaSerializer.serializeObjVec(
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800582 builder, self.operators, TosaBasicBlock.StartOperatorsVector
Kevin Chengfea5a372021-10-11 18:38:47 +0000583 )
584
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800585 TosaBasicBlock.Start(builder)
586 TosaBasicBlock.AddName(builder, fb_name)
587 TosaBasicBlock.AddInputs(builder, fbv_inputs)
588 TosaBasicBlock.AddOutputs(builder, fbv_outputs)
589 TosaBasicBlock.AddTensors(builder, fbv_tensors)
590 TosaBasicBlock.AddOperators(builder, fbv_operators)
591 return TosaBasicBlock.End(builder)
Kevin Chengfea5a372021-10-11 18:38:47 +0000592
593
594@unique
595class TensorDir(IntEnum):
596 PLACEHOLDER = 0
597 CONST = 1
598 INTERMEDIATE = 2
599 RESULT = 3
600
601
602class TosaSerializer:
Jeremy Johnsonc92710d2022-09-15 12:16:07 +0100603 def __init__(self, pathPrefix, saveConstsToFile=False):
Eric Kunzeae906de2022-05-30 22:40:47 -0700604 self.add_compat_methods()
Kevin Chengfea5a372021-10-11 18:38:47 +0000605 # Get the global TOSA version if not already defined
Kevin Chengfea5a372021-10-11 18:38:47 +0000606
607 self.builder = flatbuffers.Builder(0)
608
609 self.basicBlocks = []
610 self.startBasicBlock("main")
611 self.pathPrefix = pathPrefix
612
Jeremy Johnsonc92710d2022-09-15 12:16:07 +0100613 # Enables inspection of constant data outside of graph
614 self.saveConstsToFile = saveConstsToFile
615
Kevin Chengfea5a372021-10-11 18:38:47 +0000616 # Indicies used for adding/naming tensors
617 self.currInputIdx = 0
618 self.currConstIdx = 0
619 self.currLayerIdx = 1
620 self.currResultIdx = 0
621
622 # Is this an illegal test that is expected to fail?
Jeremy Johnson9b225172021-12-14 16:34:47 +0000623 self.expectedReturnCode = 0
Kevin Chengfea5a372021-10-11 18:38:47 +0000624 self.expectedFailure = False
625 self.expectedFailureDesc = ""
626
627 def __str__(self):
628 str = ""
629 for bb in self.basicBlocks:
630 str = str + bb.__str__()
631 return str
632
633 def addPlaceholder(self, shape, dtype, vals):
634 if not self.currBasicBlock:
635 raise Exception("addTensor called without valid basic block")
636
637 name = "input-{}".format(self.currInputIdx)
638 filename = "{}.npy".format(name)
639 self.currInputIdx = self.currInputIdx + 1
640
641 tens = self.currBasicBlock.addTensor(name, shape, dtype, None, filename)
642 # This is always an input to the block
643 self.currBasicBlock.addInput(name)
644
645 if vals is not None:
646 np.save(os.path.join(self.pathPrefix, filename), vals, False)
647
648 return tens
649
650 def addConst(self, shape, dtype, vals):
651 if not self.currBasicBlock:
652 raise Exception("addTensor called without valid basic block")
653
654 name = "const-{}".format(self.currInputIdx)
Kevin Chengfea5a372021-10-11 18:38:47 +0000655 self.currInputIdx = self.currInputIdx + 1
656
657 tens = self.currBasicBlock.addTensor(name, shape, dtype, vals)
658 # Add the operator now
Jeremy Johnson9b225172021-12-14 16:34:47 +0000659 self.currBasicBlock.addOperator(TosaOp.Op().CONST, [], name)
Kevin Chengfea5a372021-10-11 18:38:47 +0000660
Jeremy Johnsonc92710d2022-09-15 12:16:07 +0100661 if self.saveConstsToFile:
662 filename = "{}.npy".format(name)
663 np.save(os.path.join(self.pathPrefix, filename), vals, False)
664
Kevin Chengfea5a372021-10-11 18:38:47 +0000665 return tens
666
667 def addIntermediate(self, shape, dtype):
668
669 if not self.currBasicBlock:
670 raise Exception("addTensor called without valid basic block")
671
672 name = "layer-{}".format(self.currLayerIdx)
673 self.currLayerIdx = self.currLayerIdx + 1
674
675 tens = self.currBasicBlock.addTensor(name, shape, dtype, None)
676
677 return tens
678
679 def addInputTensor(self, tensor):
680 self.currBasicBlock.addTensor(tensor.name, tensor.shape, tensor.dtype)
681 self.currBasicBlock.addInput(tensor.name)
682
683 def addOutputTensor(self, tensor):
684 self.currBasicBlock.addOutput(tensor.name)
685
686 def addOutput(self, shape, dtype):
687 if not self.currBasicBlock:
688 raise Exception("addTensor called without valid basic block")
689
690 name = "result-{}".format(self.currResultIdx)
691 self.currResultIdx = self.currResultIdx + 1
692
693 tens = self.currBasicBlock.addTensor(name, shape, dtype, None)
694 self.currBasicBlock.addOutput(name)
695 return tens
696
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000697 def addOperator(self, op, inputs, outputs, attributes=None):
Kevin Chengfea5a372021-10-11 18:38:47 +0000698
Jeremy Johnson9b225172021-12-14 16:34:47 +0000699 if op == TosaOp.Op().CONST:
Kevin Chengfea5a372021-10-11 18:38:47 +0000700 raise Exception("Use addConstTensor() to add CONST ops")
701
702 return self.currBasicBlock.addOperator(
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000703 op,
704 inputs,
705 outputs,
706 attributes,
Kevin Chengfea5a372021-10-11 18:38:47 +0000707 )
708
Jeremy Johnson9b225172021-12-14 16:34:47 +0000709 def setExpectedReturnCode(self, val, fail, desc=""):
Kevin Chengfea5a372021-10-11 18:38:47 +0000710
711 self.expectedReturnCode = val
712 self.expectedFailureDesc = desc
Jeremy Johnson9b225172021-12-14 16:34:47 +0000713 self.expectedFailure = fail
Kevin Chengfea5a372021-10-11 18:38:47 +0000714
715 def serialize(self):
716
717 builder = self.builder
718
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800719 Version.Start(builder)
720 Version.Add_major(builder, TOSA_VERSION[0])
721 Version.Add_minor(builder, TOSA_VERSION[1])
722 Version.Add_patch(builder, TOSA_VERSION[2])
723 Version.Add_draft(builder, TOSA_VERSION[3])
724 version = Version.End(builder)
Kevin Chengfea5a372021-10-11 18:38:47 +0000725
726 fbv_bb = TosaSerializer.serializeObjVec(
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800727 builder, self.basicBlocks, TosaGraph.StartBlocksVector
Kevin Chengfea5a372021-10-11 18:38:47 +0000728 )
729
Kevin Cheng49faa4e2021-11-08 16:59:18 -0800730 TosaGraph.Start(builder)
731 TosaGraph.AddVersion(builder, version)
732 TosaGraph.AddBlocks(builder, fbv_bb)
733 graph = TosaGraph.End(builder)
Kevin Chengfea5a372021-10-11 18:38:47 +0000734
Eric Kunzee6596402022-06-09 21:27:36 +0000735 self.builder.Finish(graph, TOSA_GRAPH_IDENTIFIER)
Kevin Chengfea5a372021-10-11 18:38:47 +0000736 return self.builder.Output()
737
738 def writeJson(self, tosa_filename):
739 """Write a json test file so that it is fairly easy to pick up the test
740 and generate commands for third party tool"""
741 test_desc = dict()
742
743 test_desc["tosa_file"] = tosa_filename
744 ifm_name = []
745 ifm_file = []
746 ofm_name = []
747 ofm_file = []
748
749 for b in self.basicBlocks:
750 if b.name == "main":
751 for i in b.inputs:
752 ifm_name.append(i)
753 ifm_file.append(b.tensors[i].placeholderFilename)
754 for o in b.outputs:
755 ofm_name.append(o)
Jeremy Johnson9b225172021-12-14 16:34:47 +0000756 # Make up an OFM filename here. One isn't generated until the
757 # reference tool is run, so any name is a good name
Kevin Chengfea5a372021-10-11 18:38:47 +0000758 ofm_file.append("ref-{}.npy".format(o))
759
760 test_desc["ifm_name"] = ifm_name
761 test_desc["ifm_file"] = ifm_file
762 test_desc["ofm_name"] = ofm_name
763 test_desc["ofm_file"] = ofm_file
764 test_desc["expected_return_code"] = self.expectedReturnCode
765 test_desc["expected_failure"] = self.expectedFailure
766 if self.expectedFailureDesc:
767 test_desc["expected_failure_desc"] = self.expectedFailureDesc
768
769 return json.dumps(test_desc, indent=" ")
770
771 def startBasicBlock(self, name):
772 self.currBasicBlock = TosaSerializerBasicBlock(name)
773 self.basicBlocks.append(self.currBasicBlock)
774
775 @staticmethod
776 def serializeStrVec(builder, vec, start_fcn):
777 fb_strs = [builder.CreateString(i) for i in vec]
778 start_fcn(builder, len(fb_strs))
779 for s in fb_strs[::-1]:
780 builder.PrependUOffsetTRelative(s)
Eric Kunzeae906de2022-05-30 22:40:47 -0700781 try:
782 return builder.EndVector()
783 except TypeError:
784 return builder.EndVector(len(vec))
Kevin Chengfea5a372021-10-11 18:38:47 +0000785
786 @staticmethod
787 def serializeUint8Vec(builder, vec):
788 builder.StartVector(1, len(vec), 8)
789 for v in vec[::-1]:
790 builder.PrependUint8(v)
791 try:
792 return builder.EndVector()
793 except TypeError:
794 return builder.EndVector(len(vec))
795
796 @staticmethod
TatWai Chong49b1ca62022-06-10 01:49:13 -0700797 def serializeInt16Vec(builder, vec):
798 builder.StartVector(2, len(vec), 4)
799 for v in vec[::-1]:
800 builder.PrependInt16(v)
801 try:
802 return builder.EndVector()
803 except TypeError:
804 return builder.EndVector(len(vec))
805
806 @staticmethod
Kevin Chengfea5a372021-10-11 18:38:47 +0000807 def serializeInt32Vec(builder, vec):
808 builder.StartVector(4, len(vec), 4)
809 for v in vec[::-1]:
810 builder.PrependInt32(v)
811 try:
812 return builder.EndVector()
813 except TypeError:
814 return builder.EndVector(len(vec))
815
816 @staticmethod
817 def serializeFpVec(builder, vec):
818 builder.StartVector(4, len(vec), 4)
819 for v in vec[::-1]:
820 builder.PrependFloat32(v)
821 try:
822 return builder.EndVector()
823 except TypeError:
824 return builder.EndVector(len(vec))
825
826 @staticmethod
827 def serializeObjVec(builder, vec, start_fcn):
828 serialized_vec = []
829 for v in vec[::-1]:
830 serialized_vec.append(v.serialize(builder))
831
832 start_fcn(builder, len(vec))
833 for v in serialized_vec:
834 builder.PrependUOffsetTRelative(v)
835 try:
836 return builder.EndVector()
837 except TypeError:
838 return builder.EndVector(len(vec))
839
840 @staticmethod
841 def toList(val):
842 if isinstance(val, list):
843 return val
844 else:
845 return [val]
Eric Kunzeae906de2022-05-30 22:40:47 -0700846
847 # Remove when switching to flatbuffers 2.0
848 # contains a mapping of the deprecated 1.12 method to the 2.0 version
849
850 def add_compat_methods(self):
851
852 from tosa import ArithmeticRightShiftAttribute
853
854 if not hasattr(ArithmeticRightShiftAttribute, "Start"):
855 ArithmeticRightShiftAttribute.Start = (
856 ArithmeticRightShiftAttribute.ArithmeticRightShiftAttributeStart
857 )
858 ArithmeticRightShiftAttribute.AddRound = (
859 ArithmeticRightShiftAttribute.ArithmeticRightShiftAttributeAddRound
860 )
861 ArithmeticRightShiftAttribute.End = (
862 ArithmeticRightShiftAttribute.ArithmeticRightShiftAttributeEnd
863 )
864 from tosa import AxisAttribute
865
866 if not hasattr(AxisAttribute, "Start"):
867 AxisAttribute.Start = AxisAttribute.AxisAttributeStart
868 AxisAttribute.AddAxis = AxisAttribute.AxisAttributeAddAxis
869 AxisAttribute.End = AxisAttribute.AxisAttributeEnd
870 from tosa import ClampAttribute
871
872 if not hasattr(ClampAttribute, "Start"):
873 ClampAttribute.Start = ClampAttribute.ClampAttributeStart
874 ClampAttribute.AddMinInt = ClampAttribute.ClampAttributeAddMinInt
875 ClampAttribute.AddMaxInt = ClampAttribute.ClampAttributeAddMaxInt
876 ClampAttribute.AddMinFp = ClampAttribute.ClampAttributeAddMinFp
877 ClampAttribute.AddMaxFp = ClampAttribute.ClampAttributeAddMaxFp
878 ClampAttribute.End = ClampAttribute.ClampAttributeEnd
879 from tosa import CondIfAttribute
880
881 if not hasattr(CondIfAttribute, "Start"):
882 CondIfAttribute.Start = CondIfAttribute.CondIfAttributeStart
883 CondIfAttribute.AddThenBranch = CondIfAttribute.CondIfAttributeAddThenBranch
884 CondIfAttribute.AddElseBranch = CondIfAttribute.CondIfAttributeAddElseBranch
885 CondIfAttribute.End = CondIfAttribute.CondIfAttributeEnd
886 from tosa import ConvAttribute
887
888 if not hasattr(ConvAttribute, "Start"):
889 ConvAttribute.Start = ConvAttribute.ConvAttributeStart
890 ConvAttribute.AddPad = ConvAttribute.ConvAttributeAddPad
891 ConvAttribute.StartPadVector = ConvAttribute.ConvAttributeStartPadVector
892 ConvAttribute.AddStride = ConvAttribute.ConvAttributeAddStride
893 ConvAttribute.StartStrideVector = (
894 ConvAttribute.ConvAttributeStartStrideVector
895 )
896 ConvAttribute.AddDilation = ConvAttribute.ConvAttributeAddDilation
897 ConvAttribute.StartDilationVector = (
898 ConvAttribute.ConvAttributeStartDilationVector
899 )
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000900 ConvAttribute.AddInputZp = ConvAttribute.ConvAttributeAddInputZp
901 ConvAttribute.AddWeightZp = ConvAttribute.ConvAttributeAddWeightZp
James Ward485a11d2022-08-05 13:48:37 +0100902 ConvAttribute.AddAccumDtype = ConvAttribute.ConvAttributeAddAccumDtype
Eric Kunzeae906de2022-05-30 22:40:47 -0700903 ConvAttribute.End = ConvAttribute.ConvAttributeEnd
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000904 from tosa import FullyConnectedAttribute
Eric Kunzeae906de2022-05-30 22:40:47 -0700905
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000906 if not hasattr(FullyConnectedAttribute, "Start"):
907 FullyConnectedAttribute.Start = (
908 FullyConnectedAttribute.FullyConnectedAttributeStart
909 )
910 FullyConnectedAttribute.AddInputZp = (
911 FullyConnectedAttribute.FullyConnectedAttributeAddInputZp
912 )
913 FullyConnectedAttribute.AddWeightZp = (
914 FullyConnectedAttribute.FullyConnectedAttributeAddWeightZp
915 )
James Ward485a11d2022-08-05 13:48:37 +0100916 FullyConnectedAttribute.AddAccumDtype = (
917 FullyConnectedAttribute.FullyConnectedAttributeAddAccumDtype
918 )
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000919 FullyConnectedAttribute.End = (
920 FullyConnectedAttribute.FullyConnectedAttributeEnd
921 )
922 from tosa import MatMulAttribute
Eric Kunzeae906de2022-05-30 22:40:47 -0700923
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000924 if not hasattr(MatMulAttribute, "Start"):
925 MatMulAttribute.Start = MatMulAttribute.MatMulAttributeStart
926 MatMulAttribute.AddAZp = MatMulAttribute.MatMulAttributeAddAZp
927 MatMulAttribute.AddBZp = MatMulAttribute.MatMulAttributeAddBZp
James Ward485a11d2022-08-05 13:48:37 +0100928 MatMulAttribute.AddAccumDtype = MatMulAttribute.MatMulAttributeAddAccumDtype
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000929 MatMulAttribute.End = MatMulAttribute.MatMulAttributeEnd
930 from tosa import PoolAttribute
931
932 if not hasattr(PoolAttribute, "Start"):
933 PoolAttribute.Start = PoolAttribute.PoolAttributeStart
934 PoolAttribute.AddPad = PoolAttribute.PoolAttributeAddPad
935 PoolAttribute.StartPadVector = PoolAttribute.PoolAttributeStartPadVector
936 PoolAttribute.AddKernel = PoolAttribute.PoolAttributeAddKernel
937 PoolAttribute.StartKernelVector = (
938 PoolAttribute.PoolAttributeStartKernelVector
939 )
940 PoolAttribute.AddStride = PoolAttribute.PoolAttributeAddStride
941 PoolAttribute.StartStrideVector = (
942 PoolAttribute.PoolAttributeStartStrideVector
943 )
James Ward485a11d2022-08-05 13:48:37 +0100944 PoolAttribute.AddAccumDtype = PoolAttribute.PoolAttributeAddAccumDtype
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000945 PoolAttribute.AddInputZp = PoolAttribute.PoolAttributeAddInputZp
946 PoolAttribute.AddOutputZp = PoolAttribute.PoolAttributeAddOutputZp
947 PoolAttribute.End = PoolAttribute.PoolAttributeEnd
Eric Kunzeae906de2022-05-30 22:40:47 -0700948 from tosa import MulAttribute
949
950 if not hasattr(MulAttribute, "Start"):
951 MulAttribute.Start = MulAttribute.MulAttributeStart
952 MulAttribute.AddShift = MulAttribute.MulAttributeAddShift
953 MulAttribute.End = MulAttribute.MulAttributeEnd
954 from tosa import PadAttribute
955
956 if not hasattr(PadAttribute, "Start"):
957 PadAttribute.Start = PadAttribute.PadAttributeStart
958 PadAttribute.AddPadding = PadAttribute.PadAttributeAddPadding
959 PadAttribute.StartPaddingVector = (
960 PadAttribute.PadAttributeStartPaddingVector
961 )
962 PadAttribute.AddPadConstInt = PadAttribute.PadAttributeAddPadConstInt
963 PadAttribute.AddPadConstFp = PadAttribute.PadAttributeAddPadConstFp
964 PadAttribute.End = PadAttribute.PadAttributeEnd
Eric Kunzeae906de2022-05-30 22:40:47 -0700965 from tosa import PoolAttribute
966
967 if not hasattr(PoolAttribute, "Start"):
968 PoolAttribute.Start = PoolAttribute.PoolAttributeStart
969 PoolAttribute.AddPad = PoolAttribute.PoolAttributeAddPad
970 PoolAttribute.StartPadVector = PoolAttribute.PoolAttributeStartPadVector
971 PoolAttribute.AddKernel = PoolAttribute.PoolAttributeAddKernel
972 PoolAttribute.StartKernelVector = (
973 PoolAttribute.PoolAttributeStartKernelVector
974 )
975 PoolAttribute.AddStride = PoolAttribute.PoolAttributeAddStride
976 PoolAttribute.StartStrideVector = (
977 PoolAttribute.PoolAttributeStartStrideVector
978 )
James Ward485a11d2022-08-05 13:48:37 +0100979 PoolAttribute.AddAccumDtype = PoolAttribute.PoolAttributeAddAccumDtype
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000980 PoolAttribute.AddInputZp = PoolAttribute.PoolAttributeAddInputZp
981 PoolAttribute.AddOutputZp = PoolAttribute.PoolAttributeAddOutputZp
Eric Kunzeae906de2022-05-30 22:40:47 -0700982 PoolAttribute.End = PoolAttribute.PoolAttributeEnd
983 from tosa import RescaleAttribute
984
985 if not hasattr(RescaleAttribute, "Start"):
986 RescaleAttribute.Start = RescaleAttribute.RescaleAttributeStart
987 RescaleAttribute.AddInputZp = RescaleAttribute.RescaleAttributeAddInputZp
988 RescaleAttribute.AddOutputZp = RescaleAttribute.RescaleAttributeAddOutputZp
989 RescaleAttribute.AddMultiplier = (
990 RescaleAttribute.RescaleAttributeAddMultiplier
991 )
992 RescaleAttribute.StartMultiplierVector = (
993 RescaleAttribute.RescaleAttributeStartMultiplierVector
994 )
995 RescaleAttribute.AddShift = RescaleAttribute.RescaleAttributeAddShift
996 RescaleAttribute.StartShiftVector = (
997 RescaleAttribute.RescaleAttributeStartShiftVector
998 )
999 RescaleAttribute.AddScale32 = RescaleAttribute.RescaleAttributeAddScale32
1000 RescaleAttribute.AddDoubleRound = (
1001 RescaleAttribute.RescaleAttributeAddDoubleRound
1002 )
1003 RescaleAttribute.AddPerChannel = (
1004 RescaleAttribute.RescaleAttributeAddPerChannel
1005 )
1006 RescaleAttribute.End = RescaleAttribute.RescaleAttributeEnd
1007 from tosa import ReshapeAttribute
1008
1009 if not hasattr(ReshapeAttribute, "Start"):
1010 ReshapeAttribute.Start = ReshapeAttribute.ReshapeAttributeStart
1011 ReshapeAttribute.AddNewShape = ReshapeAttribute.ReshapeAttributeAddNewShape
1012 ReshapeAttribute.StartNewShapeVector = (
1013 ReshapeAttribute.ReshapeAttributeStartNewShapeVector
1014 )
1015 ReshapeAttribute.End = ReshapeAttribute.ReshapeAttributeEnd
1016 from tosa import ResizeAttribute
1017
1018 if not hasattr(ResizeAttribute, "Start"):
1019 ResizeAttribute.Start = ResizeAttribute.ResizeAttributeStart
TatWai Chong49b1ca62022-06-10 01:49:13 -07001020 ResizeAttribute.AddScale = ResizeAttribute.ResizeAttributeAddScale
1021 ResizeAttribute.StartScaleVector = (
1022 ResizeAttribute.ResizeAttributeStartScaleVector
Eric Kunzeae906de2022-05-30 22:40:47 -07001023 )
1024 ResizeAttribute.AddOffset = ResizeAttribute.ResizeAttributeAddOffset
1025 ResizeAttribute.StartOffsetVector = (
1026 ResizeAttribute.ResizeAttributeStartOffsetVector
1027 )
TatWai Chong49b1ca62022-06-10 01:49:13 -07001028 ResizeAttribute.AddBorder = ResizeAttribute.ResizeAttributeAddBorder
1029 ResizeAttribute.StartBorderVector = (
1030 ResizeAttribute.ResizeAttributeStartBorderVector
Eric Kunzeae906de2022-05-30 22:40:47 -07001031 )
1032 ResizeAttribute.AddMode = ResizeAttribute.ResizeAttributeAddMode
1033 ResizeAttribute.End = ResizeAttribute.ResizeAttributeEnd
1034 from tosa import SliceAttribute
1035
1036 if not hasattr(SliceAttribute, "Start"):
1037 SliceAttribute.Start = SliceAttribute.SliceAttributeStart
1038 SliceAttribute.AddStart = SliceAttribute.SliceAttributeAddStart
1039 SliceAttribute.StartStartVector = (
1040 SliceAttribute.SliceAttributeStartStartVector
1041 )
1042 SliceAttribute.AddSize = SliceAttribute.SliceAttributeAddSize
1043 SliceAttribute.StartSizeVector = (
1044 SliceAttribute.SliceAttributeStartSizeVector
1045 )
1046 SliceAttribute.End = SliceAttribute.SliceAttributeEnd
1047 from tosa import TableAttribute
1048
1049 if not hasattr(TableAttribute, "Start"):
1050 TableAttribute.Start = TableAttribute.TableAttributeStart
1051 TableAttribute.AddTable = TableAttribute.TableAttributeAddTable
1052 TableAttribute.StartTableVector = (
1053 TableAttribute.TableAttributeStartTableVector
1054 )
1055 TableAttribute.End = TableAttribute.TableAttributeEnd
1056 from tosa import TileAttribute
1057
1058 if not hasattr(TileAttribute, "Start"):
1059 TileAttribute.Start = TileAttribute.TileAttributeStart
1060 TileAttribute.AddMultiples = TileAttribute.TileAttributeAddMultiples
1061 TileAttribute.StartMultiplesVector = (
1062 TileAttribute.TileAttributeStartMultiplesVector
1063 )
1064 TileAttribute.End = TileAttribute.TileAttributeEnd
1065 from tosa import TosaBasicBlock
1066
1067 if not hasattr(TosaBasicBlock, "Start"):
1068 TosaBasicBlock.Start = TosaBasicBlock.TosaBasicBlockStart
1069 TosaBasicBlock.AddName = TosaBasicBlock.TosaBasicBlockAddName
1070 TosaBasicBlock.AddOperators = TosaBasicBlock.TosaBasicBlockAddOperators
1071 TosaBasicBlock.StartOperatorsVector = (
1072 TosaBasicBlock.TosaBasicBlockStartOperatorsVector
1073 )
1074 TosaBasicBlock.AddTensors = TosaBasicBlock.TosaBasicBlockAddTensors
1075 TosaBasicBlock.StartTensorsVector = (
1076 TosaBasicBlock.TosaBasicBlockStartTensorsVector
1077 )
1078 TosaBasicBlock.AddInputs = TosaBasicBlock.TosaBasicBlockAddInputs
1079 TosaBasicBlock.StartInputsVector = (
1080 TosaBasicBlock.TosaBasicBlockStartInputsVector
1081 )
1082 TosaBasicBlock.AddOutputs = TosaBasicBlock.TosaBasicBlockAddOutputs
1083 TosaBasicBlock.StartOutputsVector = (
1084 TosaBasicBlock.TosaBasicBlockStartOutputsVector
1085 )
1086 TosaBasicBlock.End = TosaBasicBlock.TosaBasicBlockEnd
1087 from tosa import TosaGraph
1088
1089 if not hasattr(TosaGraph, "Start"):
1090 TosaGraph.Start = TosaGraph.TosaGraphStart
1091 TosaGraph.AddVersion = TosaGraph.TosaGraphAddVersion
1092 TosaGraph.AddBlocks = TosaGraph.TosaGraphAddBlocks
1093 TosaGraph.StartBlocksVector = TosaGraph.TosaGraphStartBlocksVector
1094 TosaGraph.End = TosaGraph.TosaGraphEnd
1095 from tosa import TosaOperator
1096
1097 if not hasattr(TosaOperator, "Start"):
1098 TosaOperator.Start = TosaOperator.TosaOperatorStart
1099 TosaOperator.AddOp = TosaOperator.TosaOperatorAddOp
1100 TosaOperator.AddAttributeType = TosaOperator.TosaOperatorAddAttributeType
1101 TosaOperator.AddAttribute = TosaOperator.TosaOperatorAddAttribute
1102 TosaOperator.AddInputs = TosaOperator.TosaOperatorAddInputs
1103 TosaOperator.StartInputsVector = TosaOperator.TosaOperatorStartInputsVector
1104 TosaOperator.AddOutputs = TosaOperator.TosaOperatorAddOutputs
1105 TosaOperator.StartOutputsVector = (
1106 TosaOperator.TosaOperatorStartOutputsVector
1107 )
Eric Kunzeae906de2022-05-30 22:40:47 -07001108 TosaOperator.End = TosaOperator.TosaOperatorEnd
1109 from tosa import TosaTensor
1110
1111 if not hasattr(TosaTensor, "Start"):
1112 TosaTensor.Start = TosaTensor.TosaTensorStart
1113 TosaTensor.AddName = TosaTensor.TosaTensorAddName
1114 TosaTensor.AddShape = TosaTensor.TosaTensorAddShape
1115 TosaTensor.StartShapeVector = TosaTensor.TosaTensorStartShapeVector
1116 TosaTensor.AddType = TosaTensor.TosaTensorAddType
1117 TosaTensor.AddData = TosaTensor.TosaTensorAddData
1118 TosaTensor.StartDataVector = TosaTensor.TosaTensorStartDataVector
1119 TosaTensor.End = TosaTensor.TosaTensorEnd
1120 from tosa import TransposeAttribute
1121
1122 if not hasattr(TransposeAttribute, "Start"):
1123 TransposeAttribute.Start = TransposeAttribute.TransposeAttributeStart
1124 TransposeAttribute.AddPerms = TransposeAttribute.TransposeAttributeAddPerms
1125 TransposeAttribute.StartPermsVector = (
1126 TransposeAttribute.TransposeAttributeStartPermsVector
1127 )
1128 TransposeAttribute.End = TransposeAttribute.TransposeAttributeEnd
1129 from tosa import TransposeConvAttribute
1130
1131 if not hasattr(TransposeConvAttribute, "Start"):
1132 TransposeConvAttribute.Start = (
1133 TransposeConvAttribute.TransposeConvAttributeStart
1134 )
Eric Kunze4c3537d2022-06-13 17:21:48 -07001135 TransposeConvAttribute.AddOutPad = (
1136 TransposeConvAttribute.TransposeConvAttributeAddOutPad
Eric Kunzeae906de2022-05-30 22:40:47 -07001137 )
Eric Kunze4c3537d2022-06-13 17:21:48 -07001138 TransposeConvAttribute.StartOutPadVector = (
1139 TransposeConvAttribute.TransposeConvAttributeStartOutPadVector
Eric Kunzeae906de2022-05-30 22:40:47 -07001140 )
1141 TransposeConvAttribute.AddStride = (
1142 TransposeConvAttribute.TransposeConvAttributeAddStride
1143 )
1144 TransposeConvAttribute.StartStrideVector = (
1145 TransposeConvAttribute.TransposeConvAttributeStartStrideVector
1146 )
Eric Kunzeae906de2022-05-30 22:40:47 -07001147 TransposeConvAttribute.AddOutputShape = (
1148 TransposeConvAttribute.TransposeConvAttributeAddOutputShape
1149 )
1150 TransposeConvAttribute.StartOutputShapeVector = (
1151 TransposeConvAttribute.TransposeConvAttributeStartOutputShapeVector
1152 )
Eric Kunzebdcc3fe2022-06-07 05:17:37 +00001153 TransposeConvAttribute.AddInputZp = (
1154 TransposeConvAttribute.TransposeConvAttributeAddInputZp
1155 )
1156 TransposeConvAttribute.AddWeightZp = (
1157 TransposeConvAttribute.TransposeConvAttributeAddWeightZp
1158 )
James Ward485a11d2022-08-05 13:48:37 +01001159 TransposeConvAttribute.AddAccumDtype = (
1160 TransposeConvAttribute.TransposeConvAttributeAddAccumDtype
1161 )
Eric Kunzeae906de2022-05-30 22:40:47 -07001162 TransposeConvAttribute.End = (
1163 TransposeConvAttribute.TransposeConvAttributeEnd
1164 )
Eric Kunzeae906de2022-05-30 22:40:47 -07001165 from tosa import Version
1166
1167 if not hasattr(Version, "Start"):
1168 Version.Start = Version.VersionStart
1169 Version.Add_major = Version.VersionAdd_major
1170 Version.Add_minor = Version.VersionAdd_minor
1171 Version.Add_patch = Version.VersionAdd_patch
1172 Version.Add_draft = Version.VersionAdd_draft
1173 Version.End = Version.VersionEnd
Eric Kunzebdcc3fe2022-06-07 05:17:37 +00001174 from tosa import MatMulAttribute
1175
1176 if not hasattr(MatMulAttribute, "Start"):
1177 MatMulAttribute.Start = MatMulAttribute.MatMulAttributeStart
1178 MatMulAttribute.AddAZp = MatMulAttribute.MatMulAttributeAddAZp
1179 MatMulAttribute.AddBZp = MatMulAttribute.MatMulAttributeAddBZp
1180 MatMulAttribute.End = MatMulAttribute.MatMulAttributeEnd
1181 from tosa import FullyConnectedAttribute
1182
1183 if not hasattr(FullyConnectedAttribute, "Start"):
1184 FullyConnectedAttribute.Start = (
1185 FullyConnectedAttribute.FullyConnectedAttributeStart
1186 )
1187 FullyConnectedAttribute.AddInputZp = (
1188 FullyConnectedAttribute.FullyConnectedAttributeAddInputZp
1189 )
1190 FullyConnectedAttribute.AddWeightZp = (
1191 FullyConnectedAttribute.FullyConnectedAttributeAddWeightZp
1192 )
1193 FullyConnectedAttribute.End = (
1194 FullyConnectedAttribute.FullyConnectedAttributeEnd
1195 )
1196 from tosa import NegateAttribute
1197
1198 if not hasattr(NegateAttribute, "Start"):
1199 NegateAttribute.Start = NegateAttribute.NegateAttributeStart
1200 NegateAttribute.AddInput1Zp = NegateAttribute.NegateAttributeAddInput1Zp
1201 NegateAttribute.AddOutputZp = NegateAttribute.NegateAttributeAddOutputZp
1202 NegateAttribute.End = NegateAttribute.NegateAttributeEnd
Eric Kunzeae906de2022-05-30 22:40:47 -07001203 from tosa import WhileLoopAttribute
1204
1205 if not hasattr(WhileLoopAttribute, "Start"):
1206 WhileLoopAttribute.Start = WhileLoopAttribute.WhileLoopAttributeStart
1207 WhileLoopAttribute.AddCondBranch = (
1208 WhileLoopAttribute.WhileLoopAttributeAddCondBranch
1209 )
1210 WhileLoopAttribute.AddBodyBranch = (
1211 WhileLoopAttribute.WhileLoopAttributeAddBodyBranch
1212 )
1213 WhileLoopAttribute.End = WhileLoopAttribute.WhileLoopAttributeEnd