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