Jeremy Johnson | 9b22517 | 2021-12-14 16:34:47 +0000 | [diff] [blame] | 1 | # Copyright (c) 2020-2022, ARM Limited. |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 2 | # |
| 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 Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 15 | import os |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 16 | import json |
| 17 | import flatbuffers |
| 18 | import numpy as np |
| 19 | import struct |
Jeremy Johnson | 9b22517 | 2021-12-14 16:34:47 +0000 | [diff] [blame] | 20 | from enum import IntEnum, unique |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 21 | from tosa import ( |
| 22 | TosaGraph, |
| 23 | TosaBasicBlock, |
| 24 | TosaTensor, |
| 25 | TosaOperator, |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 26 | Version, |
| 27 | ) |
Jeremy Johnson | 9b22517 | 2021-12-14 16:34:47 +0000 | [diff] [blame] | 28 | import tosa.DType as TosaDType |
| 29 | import tosa.Op as TosaOp |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 30 | |
Kevin Cheng | e6563f5 | 2021-10-20 12:12:02 -0700 | [diff] [blame] | 31 | # Keep version number in sync with the version default value with schema/tosa.fbs |
Kevin Cheng | b97cb1d | 2021-10-14 11:53:39 -0700 | [diff] [blame] | 32 | TOSA_VERSION_MAJOR = 0 |
Eric Kunze | a687b61 | 2021-11-03 17:02:57 -0700 | [diff] [blame] | 33 | TOSA_VERSION_MINOR = 24 |
Kevin Cheng | b97cb1d | 2021-10-14 11:53:39 -0700 | [diff] [blame] | 34 | TOSA_VERSION_PATCH = 0 |
Eric Kunze | a687b61 | 2021-11-03 17:02:57 -0700 | [diff] [blame] | 35 | TOSA_VERSION_DRAFT = True |
Jeremy Johnson | 9b22517 | 2021-12-14 16:34:47 +0000 | [diff] [blame] | 36 | TOSA_VERSION = [ |
| 37 | TOSA_VERSION_MAJOR, |
| 38 | TOSA_VERSION_MINOR, |
| 39 | TOSA_VERSION_PATCH, |
| 40 | TOSA_VERSION_DRAFT, |
| 41 | ] |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 42 | # With the way flatc generates its python types, there is no programatic way |
| 43 | # to get string names for the integer types. Manually maintain a string table |
| 44 | # here. |
Jeremy Johnson | 9b22517 | 2021-12-14 16:34:47 +0000 | [diff] [blame] | 45 | DType = TosaDType.DType() |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 46 | DTypeNames = [ |
| 47 | "UNKNOWN", |
| 48 | "BOOL", |
| 49 | "UINT8", |
| 50 | "INT4", |
| 51 | "INT8", |
| 52 | "INT16", |
| 53 | "INT32", |
| 54 | "INT48", |
| 55 | "FLOAT", |
Jeremy Johnson | 4102773 | 2022-05-25 17:52:29 +0100 | [diff] [blame] | 56 | "UINT16", |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 57 | ] |
| 58 | |
| 59 | ByteMask = np.uint64(0xFF) |
| 60 | |
| 61 | |
| 62 | def dtype_str_to_val(name): |
| 63 | |
| 64 | for i in range(len(DTypeNames)): |
| 65 | if name.casefold() == DTypeNames[i].casefold(): |
| 66 | return i |
| 67 | raise Exception("Unable to parse DType name {}".format(name)) |
| 68 | |
| 69 | |
| 70 | class TosaSerializerUnion: |
| 71 | """This class handles encapsulating and serializing union types into flatbuffers""" |
| 72 | |
| 73 | def __init__(self): |
| 74 | |
Jeremy Johnson | 9b22517 | 2021-12-14 16:34:47 +0000 | [diff] [blame] | 75 | # A tuple of the start and end functions. |
| 76 | # Set by the options constructors below |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 77 | self.optFcns = None |
| 78 | |
Jeremy Johnson | 9b22517 | 2021-12-14 16:34:47 +0000 | [diff] [blame] | 79 | # The type from the tosa.Options enumeration. |
| 80 | # Set by the options constructors below. |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 81 | self.utype = None |
| 82 | |
| 83 | # Each of these lists is a tuple of the add function and the |
| 84 | # value being added. Set by the options constructors below. |
| 85 | self.ints = [] |
| 86 | self.bools = [] |
| 87 | self.floats = [] |
| 88 | self.strings = [] |
| 89 | self.intvecs = [] |
| 90 | self.fpvecs = [] |
| 91 | |
| 92 | def serialize(self, builder): |
| 93 | |
| 94 | # We have to build strings and vectors first |
| 95 | strList = [] |
| 96 | intVecList = [] |
| 97 | fpVecList = [] |
| 98 | |
| 99 | for fcn, val in self.strings: |
| 100 | strList.append((fcn, builder.CreateString(val))) |
| 101 | |
| 102 | for fcn, val in self.intvecs: |
| 103 | intVecList.append((fcn, TosaSerializer.serializeInt32Vec(builder, val))) |
| 104 | |
| 105 | for fcn, val in self.fpvecs: |
| 106 | fpVecList.append((fcn, TosaSerializer.serializeFpVec(builder, val))) |
| 107 | |
| 108 | startFcn, endFcn = self.optFcns |
| 109 | |
| 110 | # Then serialize the options object from the list of primitives and |
| 111 | # other serialized values |
| 112 | startFcn(builder) |
| 113 | for fcn, val in self.ints: |
| 114 | fcn(builder, val) |
| 115 | |
| 116 | for fcn, val in self.bools: |
| 117 | fcn(builder, val) |
| 118 | |
| 119 | for fcn, val in self.floats: |
| 120 | fcn(builder, val) |
| 121 | |
| 122 | for fcn, val in strList: |
| 123 | fcn(builder, val) |
| 124 | |
| 125 | for fcn, val in intVecList: |
| 126 | fcn(builder, val) |
| 127 | |
| 128 | for fcn, val in fpVecList: |
| 129 | fcn(builder, val) |
| 130 | |
| 131 | return endFcn(builder) |
| 132 | |
| 133 | |
| 134 | class TosaSerializerAttribute(TosaSerializerUnion): |
| 135 | """This class handles encapsulating all of the enumerated types for attributes""" |
| 136 | |
| 137 | def __init__(self): |
| 138 | super().__init__() |
| 139 | |
TatWai Chong | 7be7165 | 2022-05-10 17:26:20 -0700 | [diff] [blame] | 140 | def PoolAttribute(self, kernel, stride, pad): |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 141 | from tosa import PoolAttribute as a, Attribute |
| 142 | |
| 143 | self.utype = Attribute.Attribute().PoolAttribute |
| 144 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 145 | self.optFcns = (a.Start, a.End) |
TatWai Chong | 7be7165 | 2022-05-10 17:26:20 -0700 | [diff] [blame] | 146 | self.intvecs.append((a.AddPad, pad)) |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 147 | self.intvecs.append((a.AddKernel, kernel)) |
| 148 | self.intvecs.append((a.AddStride, stride)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 149 | |
TatWai Chong | 7be7165 | 2022-05-10 17:26:20 -0700 | [diff] [blame] | 150 | def ConvAttribute(self, pad, stride, dilation): |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 151 | from tosa import ConvAttribute as a, Attribute |
| 152 | |
| 153 | self.utype = Attribute.Attribute().ConvAttribute |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 154 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 155 | |
TatWai Chong | 7be7165 | 2022-05-10 17:26:20 -0700 | [diff] [blame] | 156 | self.intvecs.append((a.AddPad, pad)) |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 157 | self.intvecs.append((a.AddStride, stride)) |
| 158 | self.intvecs.append((a.AddDilation, dilation)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 159 | |
| 160 | def TransposeConvAttribute(self, outpad, stride, dilation, output_shape): |
| 161 | from tosa import TransposeConvAttribute as a, Attribute |
| 162 | |
| 163 | self.utype = Attribute.Attribute().TransposeConvAttribute |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 164 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 165 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 166 | self.intvecs.append((a.AddOutpad, outpad)) |
| 167 | self.intvecs.append((a.AddStride, stride)) |
| 168 | self.intvecs.append((a.AddDilation, dilation)) |
| 169 | self.intvecs.append((a.AddOutputShape, output_shape)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 170 | |
Kevin Cheng | 38d214c | 2021-10-15 15:49:19 -0700 | [diff] [blame] | 171 | def PadAttribute(self, padding, pad_const_int, pad_const_fp): |
| 172 | from tosa import PadAttribute as a, Attribute |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 173 | |
Kevin Cheng | 38d214c | 2021-10-15 15:49:19 -0700 | [diff] [blame] | 174 | self.utype = Attribute.Attribute().PadAttribute |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 175 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 176 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 177 | self.intvecs.append((a.AddPadding, padding)) |
| 178 | self.ints.append((a.AddPadConstInt, pad_const_int)) |
| 179 | self.floats.append((a.AddPadConstFp, pad_const_fp)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 180 | |
| 181 | def AxisAttribute(self, axis): |
| 182 | from tosa import AxisAttribute as a, Attribute |
| 183 | |
| 184 | self.utype = Attribute.Attribute().AxisAttribute |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 185 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 186 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 187 | self.ints.append((a.AddAxis, axis)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 188 | |
TatWai Chong | 7be7165 | 2022-05-10 17:26:20 -0700 | [diff] [blame] | 189 | def ReshapeAttribute(self, new_shape): |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 190 | from tosa import ReshapeAttribute as a, Attribute |
| 191 | |
| 192 | self.utype = Attribute.Attribute().ReshapeAttribute |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 193 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 194 | |
TatWai Chong | 7be7165 | 2022-05-10 17:26:20 -0700 | [diff] [blame] | 195 | self.intvecs.append((a.AddNewShape, new_shape)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 196 | |
TatWai Chong | 7be7165 | 2022-05-10 17:26:20 -0700 | [diff] [blame] | 197 | def SliceAttribute(self, start, size): |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 198 | from tosa import SliceAttribute as a, Attribute |
| 199 | |
| 200 | self.utype = Attribute.Attribute().SliceAttribute |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 201 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 202 | |
TatWai Chong | 7be7165 | 2022-05-10 17:26:20 -0700 | [diff] [blame] | 203 | self.intvecs.append((a.AddStart, start)) |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 204 | self.intvecs.append((a.AddSize, size)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 205 | |
| 206 | def TileAttribute(self, multiples): |
| 207 | from tosa import TileAttribute as a, Attribute |
| 208 | |
| 209 | self.utype = Attribute.Attribute().TileAttribute |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 210 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 211 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 212 | self.intvecs.append((a.AddMultiples, multiples)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 213 | |
| 214 | def ResizeAttribute( |
| 215 | self, output_size, stride, offset, shift, stride_fp, offset_fp, mode |
| 216 | ): |
| 217 | from tosa import ResizeAttribute as a, Attribute |
| 218 | |
| 219 | self.utype = Attribute.Attribute().ResizeAttribute |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 220 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 221 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 222 | self.intvecs.append((a.AddOutputSize, output_size)) |
| 223 | self.intvecs.append((a.AddStride, stride)) |
| 224 | self.intvecs.append((a.AddOffset, offset)) |
| 225 | self.ints.append((a.AddShift, shift)) |
| 226 | self.fpvecs.append((a.AddStrideFp, stride_fp)) |
| 227 | self.fpvecs.append((a.AddOffsetFp, offset_fp)) |
| 228 | self.ints.append((a.AddMode, mode)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 229 | |
| 230 | def ClampAttribute(self, minint, maxint, minfp, maxfp): |
| 231 | from tosa import ClampAttribute as a, Attribute |
| 232 | |
| 233 | self.utype = Attribute.Attribute().ClampAttribute |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 234 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 235 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 236 | self.ints.append((a.AddMinInt, minint)) |
| 237 | self.ints.append((a.AddMaxInt, maxint)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 238 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 239 | self.ints.append((a.AddMinFp, minfp)) |
| 240 | self.ints.append((a.AddMaxFp, maxfp)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 241 | |
| 242 | def RescaleAttribute( |
| 243 | self, input_zp, output_zp, multiplier, shift, scale32, double_round, per_channel |
| 244 | ): |
| 245 | from tosa import RescaleAttribute as a, Attribute |
| 246 | |
| 247 | self.utype = Attribute.Attribute().RescaleAttribute |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 248 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 249 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 250 | self.ints.append((a.AddInputZp, input_zp)) |
| 251 | self.ints.append((a.AddOutputZp, output_zp)) |
| 252 | self.intvecs.append((a.AddMultiplier, multiplier)) |
| 253 | self.intvecs.append((a.AddShift, shift)) |
| 254 | self.bools.append((a.AddScale32, scale32)) |
| 255 | self.bools.append((a.AddDoubleRound, double_round)) |
| 256 | self.bools.append((a.AddPerChannel, per_channel)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 257 | |
| 258 | def MulAttribute(self, shift): |
| 259 | from tosa import MulAttribute as a, Attribute |
| 260 | |
| 261 | self.utype = Attribute.Attribute().MulAttribute |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 262 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 263 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 264 | self.ints.append((a.AddShift, shift)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 265 | |
| 266 | def ArithmeticRightShiftAttribute(self, round): |
| 267 | from tosa import ArithmeticRightShiftAttribute as a, Attribute |
| 268 | |
| 269 | self.utype = Attribute.Attribute().ArithmeticRightShiftAttribute |
| 270 | self.optFcns = ( |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 271 | a.Start, |
| 272 | a.End, |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 273 | ) |
| 274 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 275 | self.bools.append((a.AddRound, round)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 276 | |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 277 | def CondIfAttribute(self, then_branch, else_branch): |
| 278 | from tosa import CondIfAttribute as a, Attribute |
| 279 | |
| 280 | self.utype = Attribute.Attribute().CondIfAttribute |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 281 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 282 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 283 | self.strings.append((a.AddThenBranch, then_branch)) |
| 284 | self.strings.append((a.AddElseBranch, else_branch)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 285 | |
| 286 | def WhileLoopAttribute(self, cond_branch, body_branch): |
| 287 | from tosa import WhileLoopAttribute as a, Attribute |
| 288 | |
| 289 | self.utype = Attribute.Attribute().WhileLoopAttribute |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 290 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 291 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 292 | self.strings.append((a.AddCondBranch, cond_branch)) |
| 293 | self.strings.append((a.AddBodyBranch, body_branch)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 294 | |
TatWai Chong | 7be7165 | 2022-05-10 17:26:20 -0700 | [diff] [blame] | 295 | def TransposeAttribute(self, perms): |
Kevin Cheng | 38d214c | 2021-10-15 15:49:19 -0700 | [diff] [blame] | 296 | from tosa import TransposeAttribute as a, Attribute |
| 297 | |
| 298 | self.utype = Attribute.Attribute().TransposeAttribute |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 299 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | 38d214c | 2021-10-15 15:49:19 -0700 | [diff] [blame] | 300 | |
TatWai Chong | 7be7165 | 2022-05-10 17:26:20 -0700 | [diff] [blame] | 301 | self.intvecs.append((a.AddPerms, perms)) |
Kevin Cheng | 38d214c | 2021-10-15 15:49:19 -0700 | [diff] [blame] | 302 | |
| 303 | def TableAttribute(self, table): |
| 304 | from tosa import TableAttribute as a, Attribute |
| 305 | |
| 306 | self.utype = Attribute.Attribute().TableAttribute |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 307 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | 38d214c | 2021-10-15 15:49:19 -0700 | [diff] [blame] | 308 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 309 | self.intvecs.append((a.AddTable, table)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 310 | |
Jeremy Johnson | 9b22517 | 2021-12-14 16:34:47 +0000 | [diff] [blame] | 311 | |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 312 | class TosaSerializerQuantInfo(TosaSerializerUnion): |
Jeremy Johnson | 9b22517 | 2021-12-14 16:34:47 +0000 | [diff] [blame] | 313 | """This class handles encapsulating all of the enumerated types for quantinfo""" |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 314 | |
| 315 | def __init__(self): |
| 316 | super().__init__() |
| 317 | |
| 318 | def ConvQuantInfo(self, input_zp, weight_zp): |
| 319 | from tosa import ConvQuantInfo as q, QuantInfo |
| 320 | |
| 321 | self.utype = QuantInfo.QuantInfo().ConvQuantInfo |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 322 | self.optFcns = (q.Start, q.End) |
| 323 | self.ints.append((q.AddInputZp, input_zp)) |
| 324 | self.ints.append((q.AddWeightZp, weight_zp)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 325 | |
| 326 | def UnaryQuantInfo(self, input_zp, output_zp): |
| 327 | from tosa import UnaryQuantInfo as q, QuantInfo |
| 328 | |
| 329 | self.utype = QuantInfo.QuantInfo().UnaryQuantInfo |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 330 | self.optFcns = (q.Start, q.End) |
| 331 | self.ints.append((q.AddInputZp, input_zp)) |
| 332 | self.ints.append((q.AddOutputZp, output_zp)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 333 | |
| 334 | def MatMulQuantInfo(self, a_zp, b_zp): |
| 335 | from tosa import MatMulQuantInfo as q, QuantInfo |
| 336 | |
| 337 | self.utype = QuantInfo.QuantInfo().MatMulQuantInfo |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 338 | self.optFcns = (q.Start, q.End) |
| 339 | self.ints.append((q.AddAZp, a_zp)) |
| 340 | self.ints.append((q.AddBZp, b_zp)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 341 | |
| 342 | def PadQuantInfo(self, input_zp): |
| 343 | from tosa import PadQuantInfo as q, QuantInfo |
| 344 | |
| 345 | self.utype = QuantInfo.QuantInfo().PadQuantInfo |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 346 | self.optFcns = (q.Start, q.End) |
| 347 | self.ints.append((q.AddInputZp, input_zp)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 348 | |
| 349 | |
| 350 | class TosaSerializerTensor: |
| 351 | def __init__( |
| 352 | self, |
| 353 | name, |
| 354 | shape, |
| 355 | dtype, |
| 356 | data=None, |
| 357 | placeholderFilename=None, |
| 358 | ): |
| 359 | self.name = name |
| 360 | |
| 361 | if isinstance(shape, np.ndarray): |
| 362 | shape = shape.astype(int).tolist() |
| 363 | shape = list(map(int, shape)) |
| 364 | |
| 365 | self.shape = shape |
| 366 | self.dtype = dtype |
| 367 | |
| 368 | if isinstance(data, np.ndarray): |
| 369 | data = data.flatten().astype(int).tolist() |
| 370 | data = list(map(int, data)) |
| 371 | self.data = data |
| 372 | elif isinstance(data, list): |
| 373 | data = list(map(int, data)) |
| 374 | self.data = data |
| 375 | else: |
| 376 | self.data = None |
| 377 | |
| 378 | # Filename for placeholder tensors. These get generated by the test generation |
Jeremy Johnson | 9b22517 | 2021-12-14 16:34:47 +0000 | [diff] [blame] | 379 | # process and are written to disk, but are considered input tensors by the |
| 380 | # network so they do not appear in the TOSA serialiazation. However, if we |
| 381 | # want to form a unit test around these input tensors, we can get the filename |
| 382 | # from here. |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 383 | self.placeholderFilename = placeholderFilename |
| 384 | |
| 385 | def __str__(self): |
| 386 | str = "TosaSerializerTensor name: {} shape: {} dtype: {}".format( |
| 387 | self.name, |
| 388 | self.shape, |
| 389 | DTypeNames[self.dtype], |
| 390 | ) |
| 391 | return str |
| 392 | |
| 393 | def setDtype(self, dtype): |
| 394 | self.dtype = dtype |
| 395 | |
| 396 | def serialize(self, builder): |
| 397 | fb_name = builder.CreateString(self.name) |
| 398 | fb_shapes = TosaSerializer.serializeInt32Vec(builder, self.shape) |
| 399 | if self.data: |
| 400 | u8_data = list() |
| 401 | # little endianess |
| 402 | if self.dtype == DType.BOOL: |
| 403 | for val in self.data: |
| 404 | val_u8 = np.uint8(val) |
| 405 | u8_data.append(val_u8) |
| 406 | elif self.dtype == DType.INT4: |
| 407 | in_size = len(self.data) |
| 408 | out_size = (in_size + 1) // 2 |
| 409 | for i in range(out_size): |
| 410 | val_0 = self.data[2 * i] |
| 411 | if (2 * i + 1) < in_size: |
| 412 | val_1 = self.data[2 * i + 1] |
| 413 | else: |
| 414 | val_1 = 0 |
| 415 | val_i8 = (val_0 & 0xF) | ((val_1 & 0xF) << 4) |
| 416 | val_u8 = np.uint8(val_i8) |
| 417 | u8_data.append(val_u8) |
| 418 | elif self.dtype == DType.INT8: |
| 419 | for val in self.data: |
| 420 | val_u8 = np.uint8(val) |
| 421 | u8_data.append(val_u8) |
| 422 | elif self.dtype == DType.INT16: |
| 423 | for val in self.data: |
| 424 | val_u16 = np.uint16(val) |
| 425 | b0 = val_u16 & ByteMask |
| 426 | b1 = (val_u16 >> np.uint16(8)) & ByteMask |
| 427 | u8_data.extend([b0, b1]) |
| 428 | elif self.dtype == DType.INT32: |
| 429 | for val in self.data: |
| 430 | val_u32 = np.uint32(val) |
| 431 | b0 = val_u32 & ByteMask |
| 432 | b1 = (val_u32 >> np.uint32(8)) & ByteMask |
| 433 | b2 = (val_u32 >> np.uint32(16)) & ByteMask |
Kevin Cheng | 6b078ca | 2021-10-13 23:12:50 -0700 | [diff] [blame] | 434 | b3 = (val_u32 >> np.uint32(24)) & ByteMask |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 435 | u8_data.extend([b0, b1, b2, b3]) |
| 436 | elif self.dtype == DType.INT48: |
| 437 | for val in self.data: |
| 438 | val_u64 = np.uint64(val) |
| 439 | b0 = val_u64 & ByteMask |
| 440 | b1 = (val_u64 >> np.uint64(8)) & ByteMask |
| 441 | b2 = (val_u64 >> np.uint64(16)) & ByteMask |
| 442 | b3 = (val_u64 >> np.uint64(24)) & ByteMask |
| 443 | b4 = (val_u64 >> np.uint64(32)) & ByteMask |
| 444 | b5 = (val_u64 >> np.uint64(40)) & ByteMask |
| 445 | u8_data.extend([b0, b1, b2, b3, b4, b5]) |
| 446 | elif self.dtype == DType.FLOAT: |
| 447 | for val in self.data: |
| 448 | b = struct.pack("!f", val) |
| 449 | u8_data.extend([b[3], b[2], b[1], b[0]]) |
| 450 | else: |
| 451 | raise Exception( |
| 452 | "unsupported data type {}".format(DTypeNames[self.dtype]) |
| 453 | ) |
| 454 | fb_data = TosaSerializer.serializeUint8Vec(builder, u8_data) |
| 455 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 456 | TosaTensor.Start(builder) |
| 457 | TosaTensor.AddName(builder, fb_name) |
| 458 | TosaTensor.AddShape(builder, fb_shapes) |
| 459 | TosaTensor.AddType(builder, self.dtype) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 460 | if self.data: |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 461 | TosaTensor.AddData(builder, fb_data) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 462 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 463 | return TosaTensor.End(builder) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 464 | |
| 465 | |
| 466 | class TosaSerializerOperator: |
| 467 | def __init__(self, op, inputs, outputs, attributes=None, quantInfo=None): |
| 468 | self.op = op |
| 469 | self.attributes = attributes |
| 470 | self.inputs = TosaSerializer.toList(inputs) |
| 471 | self.outputs = TosaSerializer.toList(outputs) |
| 472 | self.quantInfo = quantInfo |
| 473 | |
| 474 | def __str__(self): |
| 475 | str = "Op {}\n----\n".format(self.op) |
| 476 | |
| 477 | for i in self.inputs: |
| 478 | str = str + " Input: {}\n".format(i) |
| 479 | for o in self.outputs: |
| 480 | str = str + " Output: {}\n".format(o) |
| 481 | |
| 482 | return str |
| 483 | |
| 484 | def serialize(self, builder): |
| 485 | fb_inputs = TosaSerializer.serializeStrVec( |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 486 | builder, self.inputs, TosaOperator.StartInputsVector |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 487 | ) |
| 488 | fb_outputs = TosaSerializer.serializeStrVec( |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 489 | builder, self.outputs, TosaOperator.StartOutputsVector |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 490 | ) |
| 491 | # Need to serialize quant_info and attributes enums still |
| 492 | if self.attributes is not None: |
| 493 | fb_attributes = self.attributes.serialize(builder) |
| 494 | |
| 495 | if self.quantInfo is not None: |
| 496 | fb_qinfo = self.quantInfo.serialize(builder) |
| 497 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 498 | TosaOperator.Start(builder) |
| 499 | TosaOperator.AddOp(builder, self.op) |
| 500 | TosaOperator.AddInputs(builder, fb_inputs) |
| 501 | TosaOperator.AddOutputs(builder, fb_outputs) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 502 | if self.attributes is not None: |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 503 | TosaOperator.AddAttributeType(builder, self.attributes.utype) |
| 504 | TosaOperator.AddAttribute(builder, fb_attributes) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 505 | if self.quantInfo is not None: |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 506 | TosaOperator.AddQuantInfoType(builder, self.quantInfo.utype) |
| 507 | TosaOperator.AddQuantInfo(builder, fb_qinfo) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 508 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 509 | return TosaOperator.End(builder) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 510 | |
| 511 | |
| 512 | class TosaSerializerBasicBlock: |
| 513 | def __init__(self, name): |
| 514 | self.name = name |
| 515 | self.operators = [] |
| 516 | |
| 517 | # Dict assures uniqueness, but allows us to look up by name |
| 518 | self.tensors = dict() |
| 519 | |
| 520 | self.inputs = [] |
| 521 | self.outputs = [] |
| 522 | |
| 523 | def addTensor( |
| 524 | self, |
| 525 | name, |
| 526 | shape, |
| 527 | dtype, |
| 528 | data=None, |
| 529 | placeholderFilename=None, |
| 530 | ): |
Jeremy Johnson | 9b22517 | 2021-12-14 16:34:47 +0000 | [diff] [blame] | 531 | if name not in self.tensors: |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 532 | self.tensors[name] = TosaSerializerTensor( |
| 533 | name, shape, dtype, data, placeholderFilename |
| 534 | ) |
| 535 | |
| 536 | return self.tensors[name] |
| 537 | |
| 538 | def addInput(self, name): |
| 539 | self.inputs.append(name) |
| 540 | |
| 541 | def addOutput(self, name): |
| 542 | self.outputs.append(name) |
| 543 | |
| 544 | def addOperator(self, op, inputs, outputs, attributes=None, quant_info=None): |
| 545 | self.operators.append( |
| 546 | TosaSerializerOperator(op, inputs, outputs, attributes, quant_info) |
| 547 | ) |
| 548 | |
| 549 | def serialize(self, builder): |
| 550 | fb_name = builder.CreateString(self.name) |
| 551 | fbv_inputs = TosaSerializer.serializeStrVec( |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 552 | builder, list(self.inputs), TosaBasicBlock.StartInputsVector |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 553 | ) |
| 554 | fbv_outputs = TosaSerializer.serializeStrVec( |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 555 | builder, list(self.outputs), TosaBasicBlock.StartOutputsVector |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 556 | ) |
| 557 | fbv_tensors = TosaSerializer.serializeObjVec( |
| 558 | builder, |
| 559 | list(self.tensors.values()), |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 560 | TosaBasicBlock.StartTensorsVector, |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 561 | ) |
| 562 | fbv_operators = TosaSerializer.serializeObjVec( |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 563 | builder, self.operators, TosaBasicBlock.StartOperatorsVector |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 564 | ) |
| 565 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 566 | TosaBasicBlock.Start(builder) |
| 567 | TosaBasicBlock.AddName(builder, fb_name) |
| 568 | TosaBasicBlock.AddInputs(builder, fbv_inputs) |
| 569 | TosaBasicBlock.AddOutputs(builder, fbv_outputs) |
| 570 | TosaBasicBlock.AddTensors(builder, fbv_tensors) |
| 571 | TosaBasicBlock.AddOperators(builder, fbv_operators) |
| 572 | return TosaBasicBlock.End(builder) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 573 | |
| 574 | |
| 575 | @unique |
| 576 | class TensorDir(IntEnum): |
| 577 | PLACEHOLDER = 0 |
| 578 | CONST = 1 |
| 579 | INTERMEDIATE = 2 |
| 580 | RESULT = 3 |
| 581 | |
| 582 | |
| 583 | class TosaSerializer: |
| 584 | def __init__(self, pathPrefix): |
Eric Kunze | ae906de | 2022-05-30 22:40:47 -0700 | [diff] [blame^] | 585 | self.add_compat_methods() |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 586 | # Get the global TOSA version if not already defined |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 587 | |
| 588 | self.builder = flatbuffers.Builder(0) |
| 589 | |
| 590 | self.basicBlocks = [] |
| 591 | self.startBasicBlock("main") |
| 592 | self.pathPrefix = pathPrefix |
| 593 | |
| 594 | # Indicies used for adding/naming tensors |
| 595 | self.currInputIdx = 0 |
| 596 | self.currConstIdx = 0 |
| 597 | self.currLayerIdx = 1 |
| 598 | self.currResultIdx = 0 |
| 599 | |
| 600 | # Is this an illegal test that is expected to fail? |
Jeremy Johnson | 9b22517 | 2021-12-14 16:34:47 +0000 | [diff] [blame] | 601 | self.expectedReturnCode = 0 |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 602 | self.expectedFailure = False |
| 603 | self.expectedFailureDesc = "" |
| 604 | |
| 605 | def __str__(self): |
| 606 | str = "" |
| 607 | for bb in self.basicBlocks: |
| 608 | str = str + bb.__str__() |
| 609 | return str |
| 610 | |
| 611 | def addPlaceholder(self, shape, dtype, vals): |
| 612 | if not self.currBasicBlock: |
| 613 | raise Exception("addTensor called without valid basic block") |
| 614 | |
| 615 | name = "input-{}".format(self.currInputIdx) |
| 616 | filename = "{}.npy".format(name) |
| 617 | self.currInputIdx = self.currInputIdx + 1 |
| 618 | |
| 619 | tens = self.currBasicBlock.addTensor(name, shape, dtype, None, filename) |
| 620 | # This is always an input to the block |
| 621 | self.currBasicBlock.addInput(name) |
| 622 | |
| 623 | if vals is not None: |
| 624 | np.save(os.path.join(self.pathPrefix, filename), vals, False) |
| 625 | |
| 626 | return tens |
| 627 | |
| 628 | def addConst(self, shape, dtype, vals): |
| 629 | if not self.currBasicBlock: |
| 630 | raise Exception("addTensor called without valid basic block") |
| 631 | |
| 632 | name = "const-{}".format(self.currInputIdx) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 633 | self.currInputIdx = self.currInputIdx + 1 |
| 634 | |
| 635 | tens = self.currBasicBlock.addTensor(name, shape, dtype, vals) |
| 636 | # Add the operator now |
Jeremy Johnson | 9b22517 | 2021-12-14 16:34:47 +0000 | [diff] [blame] | 637 | self.currBasicBlock.addOperator(TosaOp.Op().CONST, [], name) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 638 | |
| 639 | return tens |
| 640 | |
| 641 | def addIntermediate(self, shape, dtype): |
| 642 | |
| 643 | if not self.currBasicBlock: |
| 644 | raise Exception("addTensor called without valid basic block") |
| 645 | |
| 646 | name = "layer-{}".format(self.currLayerIdx) |
| 647 | self.currLayerIdx = self.currLayerIdx + 1 |
| 648 | |
| 649 | tens = self.currBasicBlock.addTensor(name, shape, dtype, None) |
| 650 | |
| 651 | return tens |
| 652 | |
| 653 | def addInputTensor(self, tensor): |
| 654 | self.currBasicBlock.addTensor(tensor.name, tensor.shape, tensor.dtype) |
| 655 | self.currBasicBlock.addInput(tensor.name) |
| 656 | |
| 657 | def addOutputTensor(self, tensor): |
| 658 | self.currBasicBlock.addOutput(tensor.name) |
| 659 | |
| 660 | def addOutput(self, shape, dtype): |
| 661 | if not self.currBasicBlock: |
| 662 | raise Exception("addTensor called without valid basic block") |
| 663 | |
| 664 | name = "result-{}".format(self.currResultIdx) |
| 665 | self.currResultIdx = self.currResultIdx + 1 |
| 666 | |
| 667 | tens = self.currBasicBlock.addTensor(name, shape, dtype, None) |
| 668 | self.currBasicBlock.addOutput(name) |
| 669 | return tens |
| 670 | |
| 671 | def addOperator(self, op, inputs, outputs, attributes=None, quant_info=None): |
| 672 | |
Jeremy Johnson | 9b22517 | 2021-12-14 16:34:47 +0000 | [diff] [blame] | 673 | if op == TosaOp.Op().CONST: |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 674 | raise Exception("Use addConstTensor() to add CONST ops") |
| 675 | |
| 676 | return self.currBasicBlock.addOperator( |
| 677 | op, inputs, outputs, attributes, quant_info |
| 678 | ) |
| 679 | |
Jeremy Johnson | 9b22517 | 2021-12-14 16:34:47 +0000 | [diff] [blame] | 680 | def setExpectedReturnCode(self, val, fail, desc=""): |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 681 | |
| 682 | self.expectedReturnCode = val |
| 683 | self.expectedFailureDesc = desc |
Jeremy Johnson | 9b22517 | 2021-12-14 16:34:47 +0000 | [diff] [blame] | 684 | self.expectedFailure = fail |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 685 | |
| 686 | def serialize(self): |
| 687 | |
| 688 | builder = self.builder |
| 689 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 690 | Version.Start(builder) |
| 691 | Version.Add_major(builder, TOSA_VERSION[0]) |
| 692 | Version.Add_minor(builder, TOSA_VERSION[1]) |
| 693 | Version.Add_patch(builder, TOSA_VERSION[2]) |
| 694 | Version.Add_draft(builder, TOSA_VERSION[3]) |
| 695 | version = Version.End(builder) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 696 | |
| 697 | fbv_bb = TosaSerializer.serializeObjVec( |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 698 | builder, self.basicBlocks, TosaGraph.StartBlocksVector |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 699 | ) |
| 700 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 701 | TosaGraph.Start(builder) |
| 702 | TosaGraph.AddVersion(builder, version) |
| 703 | TosaGraph.AddBlocks(builder, fbv_bb) |
| 704 | graph = TosaGraph.End(builder) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 705 | |
| 706 | self.builder.Finish(graph) |
| 707 | return self.builder.Output() |
| 708 | |
| 709 | def writeJson(self, tosa_filename): |
| 710 | """Write a json test file so that it is fairly easy to pick up the test |
| 711 | and generate commands for third party tool""" |
| 712 | test_desc = dict() |
| 713 | |
| 714 | test_desc["tosa_file"] = tosa_filename |
| 715 | ifm_name = [] |
| 716 | ifm_file = [] |
| 717 | ofm_name = [] |
| 718 | ofm_file = [] |
| 719 | |
| 720 | for b in self.basicBlocks: |
| 721 | if b.name == "main": |
| 722 | for i in b.inputs: |
| 723 | ifm_name.append(i) |
| 724 | ifm_file.append(b.tensors[i].placeholderFilename) |
| 725 | for o in b.outputs: |
| 726 | ofm_name.append(o) |
Jeremy Johnson | 9b22517 | 2021-12-14 16:34:47 +0000 | [diff] [blame] | 727 | # Make up an OFM filename here. One isn't generated until the |
| 728 | # reference tool is run, so any name is a good name |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 729 | ofm_file.append("ref-{}.npy".format(o)) |
| 730 | |
| 731 | test_desc["ifm_name"] = ifm_name |
| 732 | test_desc["ifm_file"] = ifm_file |
| 733 | test_desc["ofm_name"] = ofm_name |
| 734 | test_desc["ofm_file"] = ofm_file |
| 735 | test_desc["expected_return_code"] = self.expectedReturnCode |
| 736 | test_desc["expected_failure"] = self.expectedFailure |
| 737 | if self.expectedFailureDesc: |
| 738 | test_desc["expected_failure_desc"] = self.expectedFailureDesc |
| 739 | |
| 740 | return json.dumps(test_desc, indent=" ") |
| 741 | |
| 742 | def startBasicBlock(self, name): |
| 743 | self.currBasicBlock = TosaSerializerBasicBlock(name) |
| 744 | self.basicBlocks.append(self.currBasicBlock) |
| 745 | |
| 746 | @staticmethod |
| 747 | def serializeStrVec(builder, vec, start_fcn): |
| 748 | fb_strs = [builder.CreateString(i) for i in vec] |
| 749 | start_fcn(builder, len(fb_strs)) |
| 750 | for s in fb_strs[::-1]: |
| 751 | builder.PrependUOffsetTRelative(s) |
Eric Kunze | ae906de | 2022-05-30 22:40:47 -0700 | [diff] [blame^] | 752 | try: |
| 753 | return builder.EndVector() |
| 754 | except TypeError: |
| 755 | return builder.EndVector(len(vec)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 756 | |
| 757 | @staticmethod |
| 758 | def serializeUint8Vec(builder, vec): |
| 759 | builder.StartVector(1, len(vec), 8) |
| 760 | for v in vec[::-1]: |
| 761 | builder.PrependUint8(v) |
| 762 | try: |
| 763 | return builder.EndVector() |
| 764 | except TypeError: |
| 765 | return builder.EndVector(len(vec)) |
| 766 | |
| 767 | @staticmethod |
| 768 | def serializeInt32Vec(builder, vec): |
| 769 | builder.StartVector(4, len(vec), 4) |
| 770 | for v in vec[::-1]: |
| 771 | builder.PrependInt32(v) |
| 772 | try: |
| 773 | return builder.EndVector() |
| 774 | except TypeError: |
| 775 | return builder.EndVector(len(vec)) |
| 776 | |
| 777 | @staticmethod |
| 778 | def serializeFpVec(builder, vec): |
| 779 | builder.StartVector(4, len(vec), 4) |
| 780 | for v in vec[::-1]: |
| 781 | builder.PrependFloat32(v) |
| 782 | try: |
| 783 | return builder.EndVector() |
| 784 | except TypeError: |
| 785 | return builder.EndVector(len(vec)) |
| 786 | |
| 787 | @staticmethod |
| 788 | def serializeObjVec(builder, vec, start_fcn): |
| 789 | serialized_vec = [] |
| 790 | for v in vec[::-1]: |
| 791 | serialized_vec.append(v.serialize(builder)) |
| 792 | |
| 793 | start_fcn(builder, len(vec)) |
| 794 | for v in serialized_vec: |
| 795 | builder.PrependUOffsetTRelative(v) |
| 796 | try: |
| 797 | return builder.EndVector() |
| 798 | except TypeError: |
| 799 | return builder.EndVector(len(vec)) |
| 800 | |
| 801 | @staticmethod |
| 802 | def toList(val): |
| 803 | if isinstance(val, list): |
| 804 | return val |
| 805 | else: |
| 806 | return [val] |
Eric Kunze | ae906de | 2022-05-30 22:40:47 -0700 | [diff] [blame^] | 807 | |
| 808 | # Remove when switching to flatbuffers 2.0 |
| 809 | # contains a mapping of the deprecated 1.12 method to the 2.0 version |
| 810 | |
| 811 | def add_compat_methods(self): |
| 812 | |
| 813 | from tosa import ArithmeticRightShiftAttribute |
| 814 | |
| 815 | if not hasattr(ArithmeticRightShiftAttribute, "Start"): |
| 816 | ArithmeticRightShiftAttribute.Start = ( |
| 817 | ArithmeticRightShiftAttribute.ArithmeticRightShiftAttributeStart |
| 818 | ) |
| 819 | ArithmeticRightShiftAttribute.AddRound = ( |
| 820 | ArithmeticRightShiftAttribute.ArithmeticRightShiftAttributeAddRound |
| 821 | ) |
| 822 | ArithmeticRightShiftAttribute.End = ( |
| 823 | ArithmeticRightShiftAttribute.ArithmeticRightShiftAttributeEnd |
| 824 | ) |
| 825 | from tosa import AxisAttribute |
| 826 | |
| 827 | if not hasattr(AxisAttribute, "Start"): |
| 828 | AxisAttribute.Start = AxisAttribute.AxisAttributeStart |
| 829 | AxisAttribute.AddAxis = AxisAttribute.AxisAttributeAddAxis |
| 830 | AxisAttribute.End = AxisAttribute.AxisAttributeEnd |
| 831 | from tosa import ClampAttribute |
| 832 | |
| 833 | if not hasattr(ClampAttribute, "Start"): |
| 834 | ClampAttribute.Start = ClampAttribute.ClampAttributeStart |
| 835 | ClampAttribute.AddMinInt = ClampAttribute.ClampAttributeAddMinInt |
| 836 | ClampAttribute.AddMaxInt = ClampAttribute.ClampAttributeAddMaxInt |
| 837 | ClampAttribute.AddMinFp = ClampAttribute.ClampAttributeAddMinFp |
| 838 | ClampAttribute.AddMaxFp = ClampAttribute.ClampAttributeAddMaxFp |
| 839 | ClampAttribute.End = ClampAttribute.ClampAttributeEnd |
| 840 | from tosa import CondIfAttribute |
| 841 | |
| 842 | if not hasattr(CondIfAttribute, "Start"): |
| 843 | CondIfAttribute.Start = CondIfAttribute.CondIfAttributeStart |
| 844 | CondIfAttribute.AddThenBranch = CondIfAttribute.CondIfAttributeAddThenBranch |
| 845 | CondIfAttribute.AddElseBranch = CondIfAttribute.CondIfAttributeAddElseBranch |
| 846 | CondIfAttribute.End = CondIfAttribute.CondIfAttributeEnd |
| 847 | from tosa import ConvAttribute |
| 848 | |
| 849 | if not hasattr(ConvAttribute, "Start"): |
| 850 | ConvAttribute.Start = ConvAttribute.ConvAttributeStart |
| 851 | ConvAttribute.AddPad = ConvAttribute.ConvAttributeAddPad |
| 852 | ConvAttribute.StartPadVector = ConvAttribute.ConvAttributeStartPadVector |
| 853 | ConvAttribute.AddStride = ConvAttribute.ConvAttributeAddStride |
| 854 | ConvAttribute.StartStrideVector = ( |
| 855 | ConvAttribute.ConvAttributeStartStrideVector |
| 856 | ) |
| 857 | ConvAttribute.AddDilation = ConvAttribute.ConvAttributeAddDilation |
| 858 | ConvAttribute.StartDilationVector = ( |
| 859 | ConvAttribute.ConvAttributeStartDilationVector |
| 860 | ) |
| 861 | ConvAttribute.End = ConvAttribute.ConvAttributeEnd |
| 862 | from tosa import ConvQuantInfo |
| 863 | |
| 864 | if not hasattr(ConvQuantInfo, "Start"): |
| 865 | ConvQuantInfo.Start = ConvQuantInfo.ConvQuantInfoStart |
| 866 | ConvQuantInfo.AddInputZp = ConvQuantInfo.ConvQuantInfoAddInputZp |
| 867 | ConvQuantInfo.AddWeightZp = ConvQuantInfo.ConvQuantInfoAddWeightZp |
| 868 | ConvQuantInfo.End = ConvQuantInfo.ConvQuantInfoEnd |
| 869 | from tosa import MatMulQuantInfo |
| 870 | |
| 871 | if not hasattr(MatMulQuantInfo, "Start"): |
| 872 | MatMulQuantInfo.Start = MatMulQuantInfo.MatMulQuantInfoStart |
| 873 | MatMulQuantInfo.AddAZp = MatMulQuantInfo.MatMulQuantInfoAddAZp |
| 874 | MatMulQuantInfo.AddBZp = MatMulQuantInfo.MatMulQuantInfoAddBZp |
| 875 | MatMulQuantInfo.End = MatMulQuantInfo.MatMulQuantInfoEnd |
| 876 | from tosa import MulAttribute |
| 877 | |
| 878 | if not hasattr(MulAttribute, "Start"): |
| 879 | MulAttribute.Start = MulAttribute.MulAttributeStart |
| 880 | MulAttribute.AddShift = MulAttribute.MulAttributeAddShift |
| 881 | MulAttribute.End = MulAttribute.MulAttributeEnd |
| 882 | from tosa import PadAttribute |
| 883 | |
| 884 | if not hasattr(PadAttribute, "Start"): |
| 885 | PadAttribute.Start = PadAttribute.PadAttributeStart |
| 886 | PadAttribute.AddPadding = PadAttribute.PadAttributeAddPadding |
| 887 | PadAttribute.StartPaddingVector = ( |
| 888 | PadAttribute.PadAttributeStartPaddingVector |
| 889 | ) |
| 890 | PadAttribute.AddPadConstInt = PadAttribute.PadAttributeAddPadConstInt |
| 891 | PadAttribute.AddPadConstFp = PadAttribute.PadAttributeAddPadConstFp |
| 892 | PadAttribute.End = PadAttribute.PadAttributeEnd |
| 893 | from tosa import PadQuantInfo |
| 894 | |
| 895 | if not hasattr(PadQuantInfo, "Start"): |
| 896 | PadQuantInfo.Start = PadQuantInfo.PadQuantInfoStart |
| 897 | PadQuantInfo.AddInputZp = PadQuantInfo.PadQuantInfoAddInputZp |
| 898 | PadQuantInfo.End = PadQuantInfo.PadQuantInfoEnd |
| 899 | from tosa import PoolAttribute |
| 900 | |
| 901 | if not hasattr(PoolAttribute, "Start"): |
| 902 | PoolAttribute.Start = PoolAttribute.PoolAttributeStart |
| 903 | PoolAttribute.AddPad = PoolAttribute.PoolAttributeAddPad |
| 904 | PoolAttribute.StartPadVector = PoolAttribute.PoolAttributeStartPadVector |
| 905 | PoolAttribute.AddKernel = PoolAttribute.PoolAttributeAddKernel |
| 906 | PoolAttribute.StartKernelVector = ( |
| 907 | PoolAttribute.PoolAttributeStartKernelVector |
| 908 | ) |
| 909 | PoolAttribute.AddStride = PoolAttribute.PoolAttributeAddStride |
| 910 | PoolAttribute.StartStrideVector = ( |
| 911 | PoolAttribute.PoolAttributeStartStrideVector |
| 912 | ) |
| 913 | PoolAttribute.End = PoolAttribute.PoolAttributeEnd |
| 914 | from tosa import RescaleAttribute |
| 915 | |
| 916 | if not hasattr(RescaleAttribute, "Start"): |
| 917 | RescaleAttribute.Start = RescaleAttribute.RescaleAttributeStart |
| 918 | RescaleAttribute.AddInputZp = RescaleAttribute.RescaleAttributeAddInputZp |
| 919 | RescaleAttribute.AddOutputZp = RescaleAttribute.RescaleAttributeAddOutputZp |
| 920 | RescaleAttribute.AddMultiplier = ( |
| 921 | RescaleAttribute.RescaleAttributeAddMultiplier |
| 922 | ) |
| 923 | RescaleAttribute.StartMultiplierVector = ( |
| 924 | RescaleAttribute.RescaleAttributeStartMultiplierVector |
| 925 | ) |
| 926 | RescaleAttribute.AddShift = RescaleAttribute.RescaleAttributeAddShift |
| 927 | RescaleAttribute.StartShiftVector = ( |
| 928 | RescaleAttribute.RescaleAttributeStartShiftVector |
| 929 | ) |
| 930 | RescaleAttribute.AddScale32 = RescaleAttribute.RescaleAttributeAddScale32 |
| 931 | RescaleAttribute.AddDoubleRound = ( |
| 932 | RescaleAttribute.RescaleAttributeAddDoubleRound |
| 933 | ) |
| 934 | RescaleAttribute.AddPerChannel = ( |
| 935 | RescaleAttribute.RescaleAttributeAddPerChannel |
| 936 | ) |
| 937 | RescaleAttribute.End = RescaleAttribute.RescaleAttributeEnd |
| 938 | from tosa import ReshapeAttribute |
| 939 | |
| 940 | if not hasattr(ReshapeAttribute, "Start"): |
| 941 | ReshapeAttribute.Start = ReshapeAttribute.ReshapeAttributeStart |
| 942 | ReshapeAttribute.AddNewShape = ReshapeAttribute.ReshapeAttributeAddNewShape |
| 943 | ReshapeAttribute.StartNewShapeVector = ( |
| 944 | ReshapeAttribute.ReshapeAttributeStartNewShapeVector |
| 945 | ) |
| 946 | ReshapeAttribute.End = ReshapeAttribute.ReshapeAttributeEnd |
| 947 | from tosa import ResizeAttribute |
| 948 | |
| 949 | if not hasattr(ResizeAttribute, "Start"): |
| 950 | ResizeAttribute.Start = ResizeAttribute.ResizeAttributeStart |
| 951 | ResizeAttribute.AddOutputSize = ResizeAttribute.ResizeAttributeAddOutputSize |
| 952 | ResizeAttribute.StartOutputSizeVector = ( |
| 953 | ResizeAttribute.ResizeAttributeStartOutputSizeVector |
| 954 | ) |
| 955 | ResizeAttribute.AddStride = ResizeAttribute.ResizeAttributeAddStride |
| 956 | ResizeAttribute.StartStrideVector = ( |
| 957 | ResizeAttribute.ResizeAttributeStartStrideVector |
| 958 | ) |
| 959 | ResizeAttribute.AddOffset = ResizeAttribute.ResizeAttributeAddOffset |
| 960 | ResizeAttribute.StartOffsetVector = ( |
| 961 | ResizeAttribute.ResizeAttributeStartOffsetVector |
| 962 | ) |
| 963 | ResizeAttribute.AddShift = ResizeAttribute.ResizeAttributeAddShift |
| 964 | ResizeAttribute.AddStrideFp = ResizeAttribute.ResizeAttributeAddStrideFp |
| 965 | ResizeAttribute.StartStrideFpVector = ( |
| 966 | ResizeAttribute.ResizeAttributeStartStrideFpVector |
| 967 | ) |
| 968 | ResizeAttribute.AddOffsetFp = ResizeAttribute.ResizeAttributeAddOffsetFp |
| 969 | ResizeAttribute.StartOffsetFpVector = ( |
| 970 | ResizeAttribute.ResizeAttributeStartOffsetFpVector |
| 971 | ) |
| 972 | ResizeAttribute.AddMode = ResizeAttribute.ResizeAttributeAddMode |
| 973 | ResizeAttribute.End = ResizeAttribute.ResizeAttributeEnd |
| 974 | from tosa import SliceAttribute |
| 975 | |
| 976 | if not hasattr(SliceAttribute, "Start"): |
| 977 | SliceAttribute.Start = SliceAttribute.SliceAttributeStart |
| 978 | SliceAttribute.AddStart = SliceAttribute.SliceAttributeAddStart |
| 979 | SliceAttribute.StartStartVector = ( |
| 980 | SliceAttribute.SliceAttributeStartStartVector |
| 981 | ) |
| 982 | SliceAttribute.AddSize = SliceAttribute.SliceAttributeAddSize |
| 983 | SliceAttribute.StartSizeVector = ( |
| 984 | SliceAttribute.SliceAttributeStartSizeVector |
| 985 | ) |
| 986 | SliceAttribute.End = SliceAttribute.SliceAttributeEnd |
| 987 | from tosa import TableAttribute |
| 988 | |
| 989 | if not hasattr(TableAttribute, "Start"): |
| 990 | TableAttribute.Start = TableAttribute.TableAttributeStart |
| 991 | TableAttribute.AddTable = TableAttribute.TableAttributeAddTable |
| 992 | TableAttribute.StartTableVector = ( |
| 993 | TableAttribute.TableAttributeStartTableVector |
| 994 | ) |
| 995 | TableAttribute.End = TableAttribute.TableAttributeEnd |
| 996 | from tosa import TileAttribute |
| 997 | |
| 998 | if not hasattr(TileAttribute, "Start"): |
| 999 | TileAttribute.Start = TileAttribute.TileAttributeStart |
| 1000 | TileAttribute.AddMultiples = TileAttribute.TileAttributeAddMultiples |
| 1001 | TileAttribute.StartMultiplesVector = ( |
| 1002 | TileAttribute.TileAttributeStartMultiplesVector |
| 1003 | ) |
| 1004 | TileAttribute.End = TileAttribute.TileAttributeEnd |
| 1005 | from tosa import TosaBasicBlock |
| 1006 | |
| 1007 | if not hasattr(TosaBasicBlock, "Start"): |
| 1008 | TosaBasicBlock.Start = TosaBasicBlock.TosaBasicBlockStart |
| 1009 | TosaBasicBlock.AddName = TosaBasicBlock.TosaBasicBlockAddName |
| 1010 | TosaBasicBlock.AddOperators = TosaBasicBlock.TosaBasicBlockAddOperators |
| 1011 | TosaBasicBlock.StartOperatorsVector = ( |
| 1012 | TosaBasicBlock.TosaBasicBlockStartOperatorsVector |
| 1013 | ) |
| 1014 | TosaBasicBlock.AddTensors = TosaBasicBlock.TosaBasicBlockAddTensors |
| 1015 | TosaBasicBlock.StartTensorsVector = ( |
| 1016 | TosaBasicBlock.TosaBasicBlockStartTensorsVector |
| 1017 | ) |
| 1018 | TosaBasicBlock.AddInputs = TosaBasicBlock.TosaBasicBlockAddInputs |
| 1019 | TosaBasicBlock.StartInputsVector = ( |
| 1020 | TosaBasicBlock.TosaBasicBlockStartInputsVector |
| 1021 | ) |
| 1022 | TosaBasicBlock.AddOutputs = TosaBasicBlock.TosaBasicBlockAddOutputs |
| 1023 | TosaBasicBlock.StartOutputsVector = ( |
| 1024 | TosaBasicBlock.TosaBasicBlockStartOutputsVector |
| 1025 | ) |
| 1026 | TosaBasicBlock.End = TosaBasicBlock.TosaBasicBlockEnd |
| 1027 | from tosa import TosaGraph |
| 1028 | |
| 1029 | if not hasattr(TosaGraph, "Start"): |
| 1030 | TosaGraph.Start = TosaGraph.TosaGraphStart |
| 1031 | TosaGraph.AddVersion = TosaGraph.TosaGraphAddVersion |
| 1032 | TosaGraph.AddBlocks = TosaGraph.TosaGraphAddBlocks |
| 1033 | TosaGraph.StartBlocksVector = TosaGraph.TosaGraphStartBlocksVector |
| 1034 | TosaGraph.End = TosaGraph.TosaGraphEnd |
| 1035 | from tosa import TosaOperator |
| 1036 | |
| 1037 | if not hasattr(TosaOperator, "Start"): |
| 1038 | TosaOperator.Start = TosaOperator.TosaOperatorStart |
| 1039 | TosaOperator.AddOp = TosaOperator.TosaOperatorAddOp |
| 1040 | TosaOperator.AddAttributeType = TosaOperator.TosaOperatorAddAttributeType |
| 1041 | TosaOperator.AddAttribute = TosaOperator.TosaOperatorAddAttribute |
| 1042 | TosaOperator.AddInputs = TosaOperator.TosaOperatorAddInputs |
| 1043 | TosaOperator.StartInputsVector = TosaOperator.TosaOperatorStartInputsVector |
| 1044 | TosaOperator.AddOutputs = TosaOperator.TosaOperatorAddOutputs |
| 1045 | TosaOperator.StartOutputsVector = ( |
| 1046 | TosaOperator.TosaOperatorStartOutputsVector |
| 1047 | ) |
| 1048 | TosaOperator.AddQuantInfoType = TosaOperator.TosaOperatorAddQuantInfoType |
| 1049 | TosaOperator.AddQuantInfo = TosaOperator.TosaOperatorAddQuantInfo |
| 1050 | TosaOperator.End = TosaOperator.TosaOperatorEnd |
| 1051 | from tosa import TosaTensor |
| 1052 | |
| 1053 | if not hasattr(TosaTensor, "Start"): |
| 1054 | TosaTensor.Start = TosaTensor.TosaTensorStart |
| 1055 | TosaTensor.AddName = TosaTensor.TosaTensorAddName |
| 1056 | TosaTensor.AddShape = TosaTensor.TosaTensorAddShape |
| 1057 | TosaTensor.StartShapeVector = TosaTensor.TosaTensorStartShapeVector |
| 1058 | TosaTensor.AddType = TosaTensor.TosaTensorAddType |
| 1059 | TosaTensor.AddData = TosaTensor.TosaTensorAddData |
| 1060 | TosaTensor.StartDataVector = TosaTensor.TosaTensorStartDataVector |
| 1061 | TosaTensor.End = TosaTensor.TosaTensorEnd |
| 1062 | from tosa import TransposeAttribute |
| 1063 | |
| 1064 | if not hasattr(TransposeAttribute, "Start"): |
| 1065 | TransposeAttribute.Start = TransposeAttribute.TransposeAttributeStart |
| 1066 | TransposeAttribute.AddPerms = TransposeAttribute.TransposeAttributeAddPerms |
| 1067 | TransposeAttribute.StartPermsVector = ( |
| 1068 | TransposeAttribute.TransposeAttributeStartPermsVector |
| 1069 | ) |
| 1070 | TransposeAttribute.End = TransposeAttribute.TransposeAttributeEnd |
| 1071 | from tosa import TransposeConvAttribute |
| 1072 | |
| 1073 | if not hasattr(TransposeConvAttribute, "Start"): |
| 1074 | TransposeConvAttribute.Start = ( |
| 1075 | TransposeConvAttribute.TransposeConvAttributeStart |
| 1076 | ) |
| 1077 | TransposeConvAttribute.AddOutpad = ( |
| 1078 | TransposeConvAttribute.TransposeConvAttributeAddOutpad |
| 1079 | ) |
| 1080 | TransposeConvAttribute.StartOutpadVector = ( |
| 1081 | TransposeConvAttribute.TransposeConvAttributeStartOutpadVector |
| 1082 | ) |
| 1083 | TransposeConvAttribute.AddStride = ( |
| 1084 | TransposeConvAttribute.TransposeConvAttributeAddStride |
| 1085 | ) |
| 1086 | TransposeConvAttribute.StartStrideVector = ( |
| 1087 | TransposeConvAttribute.TransposeConvAttributeStartStrideVector |
| 1088 | ) |
| 1089 | TransposeConvAttribute.AddDilation = ( |
| 1090 | TransposeConvAttribute.TransposeConvAttributeAddDilation |
| 1091 | ) |
| 1092 | TransposeConvAttribute.StartDilationVector = ( |
| 1093 | TransposeConvAttribute.TransposeConvAttributeStartDilationVector |
| 1094 | ) |
| 1095 | TransposeConvAttribute.AddOutputShape = ( |
| 1096 | TransposeConvAttribute.TransposeConvAttributeAddOutputShape |
| 1097 | ) |
| 1098 | TransposeConvAttribute.StartOutputShapeVector = ( |
| 1099 | TransposeConvAttribute.TransposeConvAttributeStartOutputShapeVector |
| 1100 | ) |
| 1101 | TransposeConvAttribute.End = ( |
| 1102 | TransposeConvAttribute.TransposeConvAttributeEnd |
| 1103 | ) |
| 1104 | from tosa import UnaryQuantInfo |
| 1105 | |
| 1106 | if not hasattr(UnaryQuantInfo, "Start"): |
| 1107 | UnaryQuantInfo.Start = UnaryQuantInfo.UnaryQuantInfoStart |
| 1108 | UnaryQuantInfo.AddInputZp = UnaryQuantInfo.UnaryQuantInfoAddInputZp |
| 1109 | UnaryQuantInfo.AddOutputZp = UnaryQuantInfo.UnaryQuantInfoAddOutputZp |
| 1110 | UnaryQuantInfo.End = UnaryQuantInfo.UnaryQuantInfoEnd |
| 1111 | from tosa import Version |
| 1112 | |
| 1113 | if not hasattr(Version, "Start"): |
| 1114 | Version.Start = Version.VersionStart |
| 1115 | Version.Add_major = Version.VersionAdd_major |
| 1116 | Version.Add_minor = Version.VersionAdd_minor |
| 1117 | Version.Add_patch = Version.VersionAdd_patch |
| 1118 | Version.Add_draft = Version.VersionAdd_draft |
| 1119 | Version.End = Version.VersionEnd |
| 1120 | from tosa import WhileLoopAttribute |
| 1121 | |
| 1122 | if not hasattr(WhileLoopAttribute, "Start"): |
| 1123 | WhileLoopAttribute.Start = WhileLoopAttribute.WhileLoopAttributeStart |
| 1124 | WhileLoopAttribute.AddCondBranch = ( |
| 1125 | WhileLoopAttribute.WhileLoopAttributeAddCondBranch |
| 1126 | ) |
| 1127 | WhileLoopAttribute.AddBodyBranch = ( |
| 1128 | WhileLoopAttribute.WhileLoopAttributeAddBodyBranch |
| 1129 | ) |
| 1130 | WhileLoopAttribute.End = WhileLoopAttribute.WhileLoopAttributeEnd |