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 | b2fdef2 | 2022-08-29 11:53:18 -0700 | [diff] [blame] | 33 | TOSA_VERSION_MINOR = 40 |
Kevin Cheng | b97cb1d | 2021-10-14 11:53:39 -0700 | [diff] [blame] | 34 | TOSA_VERSION_PATCH = 0 |
Eric Kunze | 011a333 | 2022-08-30 21:12:01 +0000 | [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 = [] |
TatWai Chong | 49b1ca6 | 2022-06-10 01:49:13 -0700 | [diff] [blame] | 93 | self.int16vecs = [] |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 94 | self.intvecs = [] |
| 95 | self.fpvecs = [] |
| 96 | |
| 97 | def serialize(self, builder): |
| 98 | |
| 99 | # We have to build strings and vectors first |
| 100 | strList = [] |
| 101 | intVecList = [] |
| 102 | fpVecList = [] |
| 103 | |
| 104 | for fcn, val in self.strings: |
| 105 | strList.append((fcn, builder.CreateString(val))) |
| 106 | |
| 107 | for fcn, val in self.intvecs: |
| 108 | intVecList.append((fcn, TosaSerializer.serializeInt32Vec(builder, val))) |
| 109 | |
TatWai Chong | 49b1ca6 | 2022-06-10 01:49:13 -0700 | [diff] [blame] | 110 | for fcn, val in self.int16vecs: |
| 111 | intVecList.append((fcn, TosaSerializer.serializeInt16Vec(builder, val))) |
| 112 | |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 113 | for fcn, val in self.fpvecs: |
| 114 | fpVecList.append((fcn, TosaSerializer.serializeFpVec(builder, val))) |
| 115 | |
| 116 | startFcn, endFcn = self.optFcns |
| 117 | |
| 118 | # Then serialize the options object from the list of primitives and |
| 119 | # other serialized values |
| 120 | startFcn(builder) |
| 121 | for fcn, val in self.ints: |
| 122 | fcn(builder, val) |
| 123 | |
| 124 | for fcn, val in self.bools: |
| 125 | fcn(builder, val) |
| 126 | |
| 127 | for fcn, val in self.floats: |
| 128 | fcn(builder, val) |
| 129 | |
| 130 | for fcn, val in strList: |
| 131 | fcn(builder, val) |
| 132 | |
| 133 | for fcn, val in intVecList: |
| 134 | fcn(builder, val) |
| 135 | |
| 136 | for fcn, val in fpVecList: |
| 137 | fcn(builder, val) |
| 138 | |
| 139 | return endFcn(builder) |
| 140 | |
| 141 | |
| 142 | class TosaSerializerAttribute(TosaSerializerUnion): |
| 143 | """This class handles encapsulating all of the enumerated types for attributes""" |
| 144 | |
| 145 | def __init__(self): |
| 146 | super().__init__() |
| 147 | |
Eric Kunze | bdcc3fe | 2022-06-07 05:17:37 +0000 | [diff] [blame] | 148 | def PoolAttribute(self, kernel, stride, pad, input_zp, output_zp): |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 149 | from tosa import PoolAttribute as a, Attribute |
| 150 | |
| 151 | self.utype = Attribute.Attribute().PoolAttribute |
| 152 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 153 | self.optFcns = (a.Start, a.End) |
TatWai Chong | 7be7165 | 2022-05-10 17:26:20 -0700 | [diff] [blame] | 154 | self.intvecs.append((a.AddPad, pad)) |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 155 | self.intvecs.append((a.AddKernel, kernel)) |
| 156 | self.intvecs.append((a.AddStride, stride)) |
Eric Kunze | bdcc3fe | 2022-06-07 05:17:37 +0000 | [diff] [blame] | 157 | self.ints.append((a.AddInputZp, input_zp)) |
| 158 | self.ints.append((a.AddOutputZp, output_zp)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 159 | |
Eric Kunze | bdcc3fe | 2022-06-07 05:17:37 +0000 | [diff] [blame] | 160 | def ConvAttribute(self, pad, stride, dilation, input_zp, weight_zp): |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 161 | from tosa import ConvAttribute as a, Attribute |
| 162 | |
| 163 | self.utype = Attribute.Attribute().ConvAttribute |
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 | |
TatWai Chong | 7be7165 | 2022-05-10 17:26:20 -0700 | [diff] [blame] | 166 | self.intvecs.append((a.AddPad, pad)) |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 167 | self.intvecs.append((a.AddStride, stride)) |
| 168 | self.intvecs.append((a.AddDilation, dilation)) |
Eric Kunze | bdcc3fe | 2022-06-07 05:17:37 +0000 | [diff] [blame] | 169 | self.ints.append((a.AddInputZp, input_zp)) |
| 170 | self.ints.append((a.AddWeightZp, weight_zp)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 171 | |
Eric Kunze | bdcc3fe | 2022-06-07 05:17:37 +0000 | [diff] [blame] | 172 | def TransposeConvAttribute(self, outpad, stride, output_shape, input_zp, weight_zp): |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 173 | from tosa import TransposeConvAttribute as a, Attribute |
| 174 | |
| 175 | self.utype = Attribute.Attribute().TransposeConvAttribute |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 176 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 177 | |
Eric Kunze | 4c3537d | 2022-06-13 17:21:48 -0700 | [diff] [blame] | 178 | self.intvecs.append((a.AddOutPad, outpad)) |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 179 | self.intvecs.append((a.AddStride, stride)) |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 180 | self.intvecs.append((a.AddOutputShape, output_shape)) |
Eric Kunze | bdcc3fe | 2022-06-07 05:17:37 +0000 | [diff] [blame] | 181 | self.ints.append((a.AddInputZp, input_zp)) |
| 182 | self.ints.append((a.AddWeightZp, weight_zp)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 183 | |
Kevin Cheng | 38d214c | 2021-10-15 15:49:19 -0700 | [diff] [blame] | 184 | def PadAttribute(self, padding, pad_const_int, pad_const_fp): |
| 185 | from tosa import PadAttribute as a, Attribute |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 186 | |
Kevin Cheng | 38d214c | 2021-10-15 15:49:19 -0700 | [diff] [blame] | 187 | self.utype = Attribute.Attribute().PadAttribute |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 188 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 189 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 190 | self.intvecs.append((a.AddPadding, padding)) |
| 191 | self.ints.append((a.AddPadConstInt, pad_const_int)) |
| 192 | self.floats.append((a.AddPadConstFp, pad_const_fp)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 193 | |
| 194 | def AxisAttribute(self, axis): |
| 195 | from tosa import AxisAttribute as a, Attribute |
| 196 | |
| 197 | self.utype = Attribute.Attribute().AxisAttribute |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 198 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 199 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 200 | self.ints.append((a.AddAxis, axis)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 201 | |
TatWai Chong | 7be7165 | 2022-05-10 17:26:20 -0700 | [diff] [blame] | 202 | def ReshapeAttribute(self, new_shape): |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 203 | from tosa import ReshapeAttribute as a, Attribute |
| 204 | |
| 205 | self.utype = Attribute.Attribute().ReshapeAttribute |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 206 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 207 | |
TatWai Chong | 7be7165 | 2022-05-10 17:26:20 -0700 | [diff] [blame] | 208 | self.intvecs.append((a.AddNewShape, new_shape)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 209 | |
TatWai Chong | 7be7165 | 2022-05-10 17:26:20 -0700 | [diff] [blame] | 210 | def SliceAttribute(self, start, size): |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 211 | from tosa import SliceAttribute as a, Attribute |
| 212 | |
| 213 | self.utype = Attribute.Attribute().SliceAttribute |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 214 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 215 | |
TatWai Chong | 7be7165 | 2022-05-10 17:26:20 -0700 | [diff] [blame] | 216 | self.intvecs.append((a.AddStart, start)) |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 217 | self.intvecs.append((a.AddSize, size)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 218 | |
| 219 | def TileAttribute(self, multiples): |
| 220 | from tosa import TileAttribute as a, Attribute |
| 221 | |
| 222 | self.utype = Attribute.Attribute().TileAttribute |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 223 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 224 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 225 | self.intvecs.append((a.AddMultiples, multiples)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 226 | |
TatWai Chong | 49b1ca6 | 2022-06-10 01:49:13 -0700 | [diff] [blame] | 227 | def ResizeAttribute(self, scale, offset, border, mode): |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 228 | from tosa import ResizeAttribute as a, Attribute |
| 229 | |
| 230 | self.utype = Attribute.Attribute().ResizeAttribute |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 231 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 232 | |
TatWai Chong | 49b1ca6 | 2022-06-10 01:49:13 -0700 | [diff] [blame] | 233 | self.int16vecs.append((a.AddScale, scale)) |
| 234 | self.int16vecs.append((a.AddOffset, offset)) |
| 235 | self.int16vecs.append((a.AddBorder, border)) |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 236 | self.ints.append((a.AddMode, mode)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 237 | |
| 238 | def ClampAttribute(self, minint, maxint, minfp, maxfp): |
| 239 | from tosa import ClampAttribute as a, Attribute |
| 240 | |
| 241 | self.utype = Attribute.Attribute().ClampAttribute |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 242 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 243 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 244 | self.ints.append((a.AddMinInt, minint)) |
| 245 | self.ints.append((a.AddMaxInt, maxint)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 246 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 247 | self.ints.append((a.AddMinFp, minfp)) |
| 248 | self.ints.append((a.AddMaxFp, maxfp)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 249 | |
| 250 | def RescaleAttribute( |
| 251 | self, input_zp, output_zp, multiplier, shift, scale32, double_round, per_channel |
| 252 | ): |
| 253 | from tosa import RescaleAttribute as a, Attribute |
| 254 | |
| 255 | self.utype = Attribute.Attribute().RescaleAttribute |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 256 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 257 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 258 | self.ints.append((a.AddInputZp, input_zp)) |
| 259 | self.ints.append((a.AddOutputZp, output_zp)) |
| 260 | self.intvecs.append((a.AddMultiplier, multiplier)) |
| 261 | self.intvecs.append((a.AddShift, shift)) |
| 262 | self.bools.append((a.AddScale32, scale32)) |
| 263 | self.bools.append((a.AddDoubleRound, double_round)) |
| 264 | self.bools.append((a.AddPerChannel, per_channel)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 265 | |
| 266 | def MulAttribute(self, shift): |
| 267 | from tosa import MulAttribute as a, Attribute |
| 268 | |
| 269 | self.utype = Attribute.Attribute().MulAttribute |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 270 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 271 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 272 | self.ints.append((a.AddShift, shift)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 273 | |
| 274 | def ArithmeticRightShiftAttribute(self, round): |
| 275 | from tosa import ArithmeticRightShiftAttribute as a, Attribute |
| 276 | |
| 277 | self.utype = Attribute.Attribute().ArithmeticRightShiftAttribute |
| 278 | self.optFcns = ( |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 279 | a.Start, |
| 280 | a.End, |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 281 | ) |
| 282 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 283 | self.bools.append((a.AddRound, round)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 284 | |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 285 | def CondIfAttribute(self, then_branch, else_branch): |
| 286 | from tosa import CondIfAttribute as a, Attribute |
| 287 | |
| 288 | self.utype = Attribute.Attribute().CondIfAttribute |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 289 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 290 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 291 | self.strings.append((a.AddThenBranch, then_branch)) |
| 292 | self.strings.append((a.AddElseBranch, else_branch)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 293 | |
| 294 | def WhileLoopAttribute(self, cond_branch, body_branch): |
| 295 | from tosa import WhileLoopAttribute as a, Attribute |
| 296 | |
| 297 | self.utype = Attribute.Attribute().WhileLoopAttribute |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 298 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 299 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 300 | self.strings.append((a.AddCondBranch, cond_branch)) |
| 301 | self.strings.append((a.AddBodyBranch, body_branch)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 302 | |
TatWai Chong | 7be7165 | 2022-05-10 17:26:20 -0700 | [diff] [blame] | 303 | def TransposeAttribute(self, perms): |
Kevin Cheng | 38d214c | 2021-10-15 15:49:19 -0700 | [diff] [blame] | 304 | from tosa import TransposeAttribute as a, Attribute |
| 305 | |
| 306 | self.utype = Attribute.Attribute().TransposeAttribute |
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 | |
TatWai Chong | 7be7165 | 2022-05-10 17:26:20 -0700 | [diff] [blame] | 309 | self.intvecs.append((a.AddPerms, perms)) |
Kevin Cheng | 38d214c | 2021-10-15 15:49:19 -0700 | [diff] [blame] | 310 | |
| 311 | def TableAttribute(self, table): |
| 312 | from tosa import TableAttribute as a, Attribute |
| 313 | |
| 314 | self.utype = Attribute.Attribute().TableAttribute |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 315 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | 38d214c | 2021-10-15 15:49:19 -0700 | [diff] [blame] | 316 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 317 | self.intvecs.append((a.AddTable, table)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 318 | |
Eric Kunze | bdcc3fe | 2022-06-07 05:17:37 +0000 | [diff] [blame] | 319 | def MatMulAttribute(self, A_zp, B_zp): |
| 320 | from tosa import MatMulAttribute as a, Attribute |
Jeremy Johnson | 9b22517 | 2021-12-14 16:34:47 +0000 | [diff] [blame] | 321 | |
Eric Kunze | bdcc3fe | 2022-06-07 05:17:37 +0000 | [diff] [blame] | 322 | self.utype = Attribute.Attribute().MatMulAttribute |
| 323 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 324 | |
Eric Kunze | bdcc3fe | 2022-06-07 05:17:37 +0000 | [diff] [blame] | 325 | self.ints.append((a.AddAZp, A_zp)) |
| 326 | self.ints.append((a.AddBZp, B_zp)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 327 | |
Eric Kunze | bdcc3fe | 2022-06-07 05:17:37 +0000 | [diff] [blame] | 328 | def FullyConnectedAttribute(self, input_zp, weight_zp): |
| 329 | from tosa import FullyConnectedAttribute as a, Attribute |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 330 | |
Eric Kunze | bdcc3fe | 2022-06-07 05:17:37 +0000 | [diff] [blame] | 331 | self.utype = Attribute.Attribute().FullyConnectedAttribute |
| 332 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 333 | |
Eric Kunze | bdcc3fe | 2022-06-07 05:17:37 +0000 | [diff] [blame] | 334 | self.ints.append((a.AddInputZp, input_zp)) |
| 335 | self.ints.append((a.AddWeightZp, weight_zp)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 336 | |
Eric Kunze | bdcc3fe | 2022-06-07 05:17:37 +0000 | [diff] [blame] | 337 | def NegateAttribute(self, input1_zp, output_zp): |
| 338 | from tosa import NegateAttribute as a, Attribute |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 339 | |
Eric Kunze | bdcc3fe | 2022-06-07 05:17:37 +0000 | [diff] [blame] | 340 | self.utype = Attribute.Attribute().NegateAttribute |
| 341 | self.optFcns = (a.Start, a.End) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 342 | |
Eric Kunze | bdcc3fe | 2022-06-07 05:17:37 +0000 | [diff] [blame] | 343 | self.ints.append((a.AddInput1Zp, input1_zp)) |
| 344 | self.ints.append((a.AddOutputZp, output_zp)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 345 | |
| 346 | |
| 347 | class TosaSerializerTensor: |
| 348 | def __init__( |
| 349 | self, |
| 350 | name, |
| 351 | shape, |
| 352 | dtype, |
| 353 | data=None, |
| 354 | placeholderFilename=None, |
| 355 | ): |
| 356 | self.name = name |
| 357 | |
| 358 | if isinstance(shape, np.ndarray): |
| 359 | shape = shape.astype(int).tolist() |
| 360 | shape = list(map(int, shape)) |
| 361 | |
| 362 | self.shape = shape |
| 363 | self.dtype = dtype |
| 364 | |
| 365 | if isinstance(data, np.ndarray): |
| 366 | data = data.flatten().astype(int).tolist() |
| 367 | data = list(map(int, data)) |
| 368 | self.data = data |
| 369 | elif isinstance(data, list): |
| 370 | data = list(map(int, data)) |
| 371 | self.data = data |
| 372 | else: |
| 373 | self.data = None |
| 374 | |
| 375 | # Filename for placeholder tensors. These get generated by the test generation |
Jeremy Johnson | 9b22517 | 2021-12-14 16:34:47 +0000 | [diff] [blame] | 376 | # process and are written to disk, but are considered input tensors by the |
| 377 | # network so they do not appear in the TOSA serialiazation. However, if we |
| 378 | # want to form a unit test around these input tensors, we can get the filename |
| 379 | # from here. |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 380 | self.placeholderFilename = placeholderFilename |
| 381 | |
| 382 | def __str__(self): |
| 383 | str = "TosaSerializerTensor name: {} shape: {} dtype: {}".format( |
| 384 | self.name, |
| 385 | self.shape, |
| 386 | DTypeNames[self.dtype], |
| 387 | ) |
| 388 | return str |
| 389 | |
| 390 | def setDtype(self, dtype): |
| 391 | self.dtype = dtype |
| 392 | |
| 393 | def serialize(self, builder): |
| 394 | fb_name = builder.CreateString(self.name) |
| 395 | fb_shapes = TosaSerializer.serializeInt32Vec(builder, self.shape) |
| 396 | if self.data: |
| 397 | u8_data = list() |
| 398 | # little endianess |
| 399 | if self.dtype == DType.BOOL: |
| 400 | for val in self.data: |
| 401 | val_u8 = np.uint8(val) |
| 402 | u8_data.append(val_u8) |
| 403 | elif self.dtype == DType.INT4: |
| 404 | in_size = len(self.data) |
| 405 | out_size = (in_size + 1) // 2 |
| 406 | for i in range(out_size): |
| 407 | val_0 = self.data[2 * i] |
| 408 | if (2 * i + 1) < in_size: |
| 409 | val_1 = self.data[2 * i + 1] |
| 410 | else: |
| 411 | val_1 = 0 |
| 412 | val_i8 = (val_0 & 0xF) | ((val_1 & 0xF) << 4) |
| 413 | val_u8 = np.uint8(val_i8) |
| 414 | u8_data.append(val_u8) |
| 415 | elif self.dtype == DType.INT8: |
| 416 | for val in self.data: |
| 417 | val_u8 = np.uint8(val) |
| 418 | u8_data.append(val_u8) |
| 419 | elif self.dtype == DType.INT16: |
| 420 | for val in self.data: |
| 421 | val_u16 = np.uint16(val) |
| 422 | b0 = val_u16 & ByteMask |
| 423 | b1 = (val_u16 >> np.uint16(8)) & ByteMask |
| 424 | u8_data.extend([b0, b1]) |
| 425 | elif self.dtype == DType.INT32: |
| 426 | for val in self.data: |
| 427 | val_u32 = np.uint32(val) |
| 428 | b0 = val_u32 & ByteMask |
| 429 | b1 = (val_u32 >> np.uint32(8)) & ByteMask |
| 430 | b2 = (val_u32 >> np.uint32(16)) & ByteMask |
Kevin Cheng | 6b078ca | 2021-10-13 23:12:50 -0700 | [diff] [blame] | 431 | b3 = (val_u32 >> np.uint32(24)) & ByteMask |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 432 | u8_data.extend([b0, b1, b2, b3]) |
| 433 | elif self.dtype == DType.INT48: |
| 434 | for val in self.data: |
| 435 | val_u64 = np.uint64(val) |
| 436 | b0 = val_u64 & ByteMask |
| 437 | b1 = (val_u64 >> np.uint64(8)) & ByteMask |
| 438 | b2 = (val_u64 >> np.uint64(16)) & ByteMask |
| 439 | b3 = (val_u64 >> np.uint64(24)) & ByteMask |
| 440 | b4 = (val_u64 >> np.uint64(32)) & ByteMask |
| 441 | b5 = (val_u64 >> np.uint64(40)) & ByteMask |
| 442 | u8_data.extend([b0, b1, b2, b3, b4, b5]) |
| 443 | elif self.dtype == DType.FLOAT: |
| 444 | for val in self.data: |
| 445 | b = struct.pack("!f", val) |
| 446 | u8_data.extend([b[3], b[2], b[1], b[0]]) |
| 447 | else: |
| 448 | raise Exception( |
| 449 | "unsupported data type {}".format(DTypeNames[self.dtype]) |
| 450 | ) |
| 451 | fb_data = TosaSerializer.serializeUint8Vec(builder, u8_data) |
| 452 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 453 | TosaTensor.Start(builder) |
| 454 | TosaTensor.AddName(builder, fb_name) |
| 455 | TosaTensor.AddShape(builder, fb_shapes) |
| 456 | TosaTensor.AddType(builder, self.dtype) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 457 | if self.data: |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 458 | TosaTensor.AddData(builder, fb_data) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 459 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 460 | return TosaTensor.End(builder) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 461 | |
| 462 | |
| 463 | class TosaSerializerOperator: |
Eric Kunze | bdcc3fe | 2022-06-07 05:17:37 +0000 | [diff] [blame] | 464 | def __init__(self, op, inputs, outputs, attributes=None): |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 465 | self.op = op |
| 466 | self.attributes = attributes |
| 467 | self.inputs = TosaSerializer.toList(inputs) |
| 468 | self.outputs = TosaSerializer.toList(outputs) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 469 | |
| 470 | def __str__(self): |
| 471 | str = "Op {}\n----\n".format(self.op) |
| 472 | |
| 473 | for i in self.inputs: |
| 474 | str = str + " Input: {}\n".format(i) |
| 475 | for o in self.outputs: |
| 476 | str = str + " Output: {}\n".format(o) |
| 477 | |
| 478 | return str |
| 479 | |
| 480 | def serialize(self, builder): |
| 481 | fb_inputs = TosaSerializer.serializeStrVec( |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 482 | builder, self.inputs, TosaOperator.StartInputsVector |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 483 | ) |
| 484 | fb_outputs = TosaSerializer.serializeStrVec( |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 485 | builder, self.outputs, TosaOperator.StartOutputsVector |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 486 | ) |
Eric Kunze | bdcc3fe | 2022-06-07 05:17:37 +0000 | [diff] [blame] | 487 | # Need to serialize attributes enums still |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 488 | if self.attributes is not None: |
| 489 | fb_attributes = self.attributes.serialize(builder) |
| 490 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 491 | TosaOperator.Start(builder) |
| 492 | TosaOperator.AddOp(builder, self.op) |
| 493 | TosaOperator.AddInputs(builder, fb_inputs) |
| 494 | TosaOperator.AddOutputs(builder, fb_outputs) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 495 | if self.attributes is not None: |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 496 | TosaOperator.AddAttributeType(builder, self.attributes.utype) |
| 497 | TosaOperator.AddAttribute(builder, fb_attributes) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 498 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 499 | return TosaOperator.End(builder) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 500 | |
| 501 | |
| 502 | class TosaSerializerBasicBlock: |
| 503 | def __init__(self, name): |
| 504 | self.name = name |
| 505 | self.operators = [] |
| 506 | |
| 507 | # Dict assures uniqueness, but allows us to look up by name |
| 508 | self.tensors = dict() |
| 509 | |
| 510 | self.inputs = [] |
| 511 | self.outputs = [] |
| 512 | |
| 513 | def addTensor( |
| 514 | self, |
| 515 | name, |
| 516 | shape, |
| 517 | dtype, |
| 518 | data=None, |
| 519 | placeholderFilename=None, |
| 520 | ): |
Jeremy Johnson | 9b22517 | 2021-12-14 16:34:47 +0000 | [diff] [blame] | 521 | if name not in self.tensors: |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 522 | self.tensors[name] = TosaSerializerTensor( |
| 523 | name, shape, dtype, data, placeholderFilename |
| 524 | ) |
| 525 | |
| 526 | return self.tensors[name] |
| 527 | |
| 528 | def addInput(self, name): |
| 529 | self.inputs.append(name) |
| 530 | |
| 531 | def addOutput(self, name): |
| 532 | self.outputs.append(name) |
| 533 | |
Eric Kunze | bdcc3fe | 2022-06-07 05:17:37 +0000 | [diff] [blame] | 534 | def addOperator(self, op, inputs, outputs, attributes=None): |
| 535 | self.operators.append(TosaSerializerOperator(op, inputs, outputs, attributes)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 536 | |
| 537 | def serialize(self, builder): |
| 538 | fb_name = builder.CreateString(self.name) |
| 539 | fbv_inputs = TosaSerializer.serializeStrVec( |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 540 | builder, list(self.inputs), TosaBasicBlock.StartInputsVector |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 541 | ) |
| 542 | fbv_outputs = TosaSerializer.serializeStrVec( |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 543 | builder, list(self.outputs), TosaBasicBlock.StartOutputsVector |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 544 | ) |
| 545 | fbv_tensors = TosaSerializer.serializeObjVec( |
| 546 | builder, |
| 547 | list(self.tensors.values()), |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 548 | TosaBasicBlock.StartTensorsVector, |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 549 | ) |
| 550 | fbv_operators = TosaSerializer.serializeObjVec( |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 551 | builder, self.operators, TosaBasicBlock.StartOperatorsVector |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 552 | ) |
| 553 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 554 | TosaBasicBlock.Start(builder) |
| 555 | TosaBasicBlock.AddName(builder, fb_name) |
| 556 | TosaBasicBlock.AddInputs(builder, fbv_inputs) |
| 557 | TosaBasicBlock.AddOutputs(builder, fbv_outputs) |
| 558 | TosaBasicBlock.AddTensors(builder, fbv_tensors) |
| 559 | TosaBasicBlock.AddOperators(builder, fbv_operators) |
| 560 | return TosaBasicBlock.End(builder) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 561 | |
| 562 | |
| 563 | @unique |
| 564 | class TensorDir(IntEnum): |
| 565 | PLACEHOLDER = 0 |
| 566 | CONST = 1 |
| 567 | INTERMEDIATE = 2 |
| 568 | RESULT = 3 |
| 569 | |
| 570 | |
| 571 | class TosaSerializer: |
| 572 | def __init__(self, pathPrefix): |
Eric Kunze | ae906de | 2022-05-30 22:40:47 -0700 | [diff] [blame] | 573 | self.add_compat_methods() |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 574 | # Get the global TOSA version if not already defined |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 575 | |
| 576 | self.builder = flatbuffers.Builder(0) |
| 577 | |
| 578 | self.basicBlocks = [] |
| 579 | self.startBasicBlock("main") |
| 580 | self.pathPrefix = pathPrefix |
| 581 | |
| 582 | # Indicies used for adding/naming tensors |
| 583 | self.currInputIdx = 0 |
| 584 | self.currConstIdx = 0 |
| 585 | self.currLayerIdx = 1 |
| 586 | self.currResultIdx = 0 |
| 587 | |
| 588 | # Is this an illegal test that is expected to fail? |
Jeremy Johnson | 9b22517 | 2021-12-14 16:34:47 +0000 | [diff] [blame] | 589 | self.expectedReturnCode = 0 |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 590 | self.expectedFailure = False |
| 591 | self.expectedFailureDesc = "" |
| 592 | |
| 593 | def __str__(self): |
| 594 | str = "" |
| 595 | for bb in self.basicBlocks: |
| 596 | str = str + bb.__str__() |
| 597 | return str |
| 598 | |
| 599 | def addPlaceholder(self, shape, dtype, vals): |
| 600 | if not self.currBasicBlock: |
| 601 | raise Exception("addTensor called without valid basic block") |
| 602 | |
| 603 | name = "input-{}".format(self.currInputIdx) |
| 604 | filename = "{}.npy".format(name) |
| 605 | self.currInputIdx = self.currInputIdx + 1 |
| 606 | |
| 607 | tens = self.currBasicBlock.addTensor(name, shape, dtype, None, filename) |
| 608 | # This is always an input to the block |
| 609 | self.currBasicBlock.addInput(name) |
| 610 | |
| 611 | if vals is not None: |
| 612 | np.save(os.path.join(self.pathPrefix, filename), vals, False) |
| 613 | |
| 614 | return tens |
| 615 | |
| 616 | def addConst(self, shape, dtype, vals): |
| 617 | if not self.currBasicBlock: |
| 618 | raise Exception("addTensor called without valid basic block") |
| 619 | |
| 620 | name = "const-{}".format(self.currInputIdx) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 621 | self.currInputIdx = self.currInputIdx + 1 |
| 622 | |
| 623 | tens = self.currBasicBlock.addTensor(name, shape, dtype, vals) |
| 624 | # Add the operator now |
Jeremy Johnson | 9b22517 | 2021-12-14 16:34:47 +0000 | [diff] [blame] | 625 | self.currBasicBlock.addOperator(TosaOp.Op().CONST, [], name) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 626 | |
| 627 | return tens |
| 628 | |
| 629 | def addIntermediate(self, shape, dtype): |
| 630 | |
| 631 | if not self.currBasicBlock: |
| 632 | raise Exception("addTensor called without valid basic block") |
| 633 | |
| 634 | name = "layer-{}".format(self.currLayerIdx) |
| 635 | self.currLayerIdx = self.currLayerIdx + 1 |
| 636 | |
| 637 | tens = self.currBasicBlock.addTensor(name, shape, dtype, None) |
| 638 | |
| 639 | return tens |
| 640 | |
| 641 | def addInputTensor(self, tensor): |
| 642 | self.currBasicBlock.addTensor(tensor.name, tensor.shape, tensor.dtype) |
| 643 | self.currBasicBlock.addInput(tensor.name) |
| 644 | |
| 645 | def addOutputTensor(self, tensor): |
| 646 | self.currBasicBlock.addOutput(tensor.name) |
| 647 | |
| 648 | def addOutput(self, shape, dtype): |
| 649 | if not self.currBasicBlock: |
| 650 | raise Exception("addTensor called without valid basic block") |
| 651 | |
| 652 | name = "result-{}".format(self.currResultIdx) |
| 653 | self.currResultIdx = self.currResultIdx + 1 |
| 654 | |
| 655 | tens = self.currBasicBlock.addTensor(name, shape, dtype, None) |
| 656 | self.currBasicBlock.addOutput(name) |
| 657 | return tens |
| 658 | |
Eric Kunze | bdcc3fe | 2022-06-07 05:17:37 +0000 | [diff] [blame] | 659 | def addOperator(self, op, inputs, outputs, attributes=None): |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 660 | |
Jeremy Johnson | 9b22517 | 2021-12-14 16:34:47 +0000 | [diff] [blame] | 661 | if op == TosaOp.Op().CONST: |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 662 | raise Exception("Use addConstTensor() to add CONST ops") |
| 663 | |
| 664 | return self.currBasicBlock.addOperator( |
Eric Kunze | bdcc3fe | 2022-06-07 05:17:37 +0000 | [diff] [blame] | 665 | op, |
| 666 | inputs, |
| 667 | outputs, |
| 668 | attributes, |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 669 | ) |
| 670 | |
Jeremy Johnson | 9b22517 | 2021-12-14 16:34:47 +0000 | [diff] [blame] | 671 | def setExpectedReturnCode(self, val, fail, desc=""): |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 672 | |
| 673 | self.expectedReturnCode = val |
| 674 | self.expectedFailureDesc = desc |
Jeremy Johnson | 9b22517 | 2021-12-14 16:34:47 +0000 | [diff] [blame] | 675 | self.expectedFailure = fail |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 676 | |
| 677 | def serialize(self): |
| 678 | |
| 679 | builder = self.builder |
| 680 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 681 | Version.Start(builder) |
| 682 | Version.Add_major(builder, TOSA_VERSION[0]) |
| 683 | Version.Add_minor(builder, TOSA_VERSION[1]) |
| 684 | Version.Add_patch(builder, TOSA_VERSION[2]) |
| 685 | Version.Add_draft(builder, TOSA_VERSION[3]) |
| 686 | version = Version.End(builder) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 687 | |
| 688 | fbv_bb = TosaSerializer.serializeObjVec( |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 689 | builder, self.basicBlocks, TosaGraph.StartBlocksVector |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 690 | ) |
| 691 | |
Kevin Cheng | 49faa4e | 2021-11-08 16:59:18 -0800 | [diff] [blame] | 692 | TosaGraph.Start(builder) |
| 693 | TosaGraph.AddVersion(builder, version) |
| 694 | TosaGraph.AddBlocks(builder, fbv_bb) |
| 695 | graph = TosaGraph.End(builder) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 696 | |
Eric Kunze | e659640 | 2022-06-09 21:27:36 +0000 | [diff] [blame] | 697 | self.builder.Finish(graph, TOSA_GRAPH_IDENTIFIER) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 698 | return self.builder.Output() |
| 699 | |
| 700 | def writeJson(self, tosa_filename): |
| 701 | """Write a json test file so that it is fairly easy to pick up the test |
| 702 | and generate commands for third party tool""" |
| 703 | test_desc = dict() |
| 704 | |
| 705 | test_desc["tosa_file"] = tosa_filename |
| 706 | ifm_name = [] |
| 707 | ifm_file = [] |
| 708 | ofm_name = [] |
| 709 | ofm_file = [] |
| 710 | |
| 711 | for b in self.basicBlocks: |
| 712 | if b.name == "main": |
| 713 | for i in b.inputs: |
| 714 | ifm_name.append(i) |
| 715 | ifm_file.append(b.tensors[i].placeholderFilename) |
| 716 | for o in b.outputs: |
| 717 | ofm_name.append(o) |
Jeremy Johnson | 9b22517 | 2021-12-14 16:34:47 +0000 | [diff] [blame] | 718 | # Make up an OFM filename here. One isn't generated until the |
| 719 | # reference tool is run, so any name is a good name |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 720 | ofm_file.append("ref-{}.npy".format(o)) |
| 721 | |
| 722 | test_desc["ifm_name"] = ifm_name |
| 723 | test_desc["ifm_file"] = ifm_file |
| 724 | test_desc["ofm_name"] = ofm_name |
| 725 | test_desc["ofm_file"] = ofm_file |
| 726 | test_desc["expected_return_code"] = self.expectedReturnCode |
| 727 | test_desc["expected_failure"] = self.expectedFailure |
| 728 | if self.expectedFailureDesc: |
| 729 | test_desc["expected_failure_desc"] = self.expectedFailureDesc |
| 730 | |
| 731 | return json.dumps(test_desc, indent=" ") |
| 732 | |
| 733 | def startBasicBlock(self, name): |
| 734 | self.currBasicBlock = TosaSerializerBasicBlock(name) |
| 735 | self.basicBlocks.append(self.currBasicBlock) |
| 736 | |
| 737 | @staticmethod |
| 738 | def serializeStrVec(builder, vec, start_fcn): |
| 739 | fb_strs = [builder.CreateString(i) for i in vec] |
| 740 | start_fcn(builder, len(fb_strs)) |
| 741 | for s in fb_strs[::-1]: |
| 742 | builder.PrependUOffsetTRelative(s) |
Eric Kunze | ae906de | 2022-05-30 22:40:47 -0700 | [diff] [blame] | 743 | try: |
| 744 | return builder.EndVector() |
| 745 | except TypeError: |
| 746 | return builder.EndVector(len(vec)) |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 747 | |
| 748 | @staticmethod |
| 749 | def serializeUint8Vec(builder, vec): |
| 750 | builder.StartVector(1, len(vec), 8) |
| 751 | for v in vec[::-1]: |
| 752 | builder.PrependUint8(v) |
| 753 | try: |
| 754 | return builder.EndVector() |
| 755 | except TypeError: |
| 756 | return builder.EndVector(len(vec)) |
| 757 | |
| 758 | @staticmethod |
TatWai Chong | 49b1ca6 | 2022-06-10 01:49:13 -0700 | [diff] [blame] | 759 | def serializeInt16Vec(builder, vec): |
| 760 | builder.StartVector(2, len(vec), 4) |
| 761 | for v in vec[::-1]: |
| 762 | builder.PrependInt16(v) |
| 763 | try: |
| 764 | return builder.EndVector() |
| 765 | except TypeError: |
| 766 | return builder.EndVector(len(vec)) |
| 767 | |
| 768 | @staticmethod |
Kevin Cheng | fea5a37 | 2021-10-11 18:38:47 +0000 | [diff] [blame] | 769 | def serializeInt32Vec(builder, vec): |
| 770 | builder.StartVector(4, len(vec), 4) |
| 771 | for v in vec[::-1]: |
| 772 | builder.PrependInt32(v) |
| 773 | try: |
| 774 | return builder.EndVector() |
| 775 | except TypeError: |
| 776 | return builder.EndVector(len(vec)) |
| 777 | |
| 778 | @staticmethod |
| 779 | def serializeFpVec(builder, vec): |
| 780 | builder.StartVector(4, len(vec), 4) |
| 781 | for v in vec[::-1]: |
| 782 | builder.PrependFloat32(v) |
| 783 | try: |
| 784 | return builder.EndVector() |
| 785 | except TypeError: |
| 786 | return builder.EndVector(len(vec)) |
| 787 | |
| 788 | @staticmethod |
| 789 | def serializeObjVec(builder, vec, start_fcn): |
| 790 | serialized_vec = [] |
| 791 | for v in vec[::-1]: |
| 792 | serialized_vec.append(v.serialize(builder)) |
| 793 | |
| 794 | start_fcn(builder, len(vec)) |
| 795 | for v in serialized_vec: |
| 796 | builder.PrependUOffsetTRelative(v) |
| 797 | try: |
| 798 | return builder.EndVector() |
| 799 | except TypeError: |
| 800 | return builder.EndVector(len(vec)) |
| 801 | |
| 802 | @staticmethod |
| 803 | def toList(val): |
| 804 | if isinstance(val, list): |
| 805 | return val |
| 806 | else: |
| 807 | return [val] |
Eric Kunze | ae906de | 2022-05-30 22:40:47 -0700 | [diff] [blame] | 808 | |
| 809 | # Remove when switching to flatbuffers 2.0 |
| 810 | # contains a mapping of the deprecated 1.12 method to the 2.0 version |
| 811 | |
| 812 | def add_compat_methods(self): |
| 813 | |
| 814 | from tosa import ArithmeticRightShiftAttribute |
| 815 | |
| 816 | if not hasattr(ArithmeticRightShiftAttribute, "Start"): |
| 817 | ArithmeticRightShiftAttribute.Start = ( |
| 818 | ArithmeticRightShiftAttribute.ArithmeticRightShiftAttributeStart |
| 819 | ) |
| 820 | ArithmeticRightShiftAttribute.AddRound = ( |
| 821 | ArithmeticRightShiftAttribute.ArithmeticRightShiftAttributeAddRound |
| 822 | ) |
| 823 | ArithmeticRightShiftAttribute.End = ( |
| 824 | ArithmeticRightShiftAttribute.ArithmeticRightShiftAttributeEnd |
| 825 | ) |
| 826 | from tosa import AxisAttribute |
| 827 | |
| 828 | if not hasattr(AxisAttribute, "Start"): |
| 829 | AxisAttribute.Start = AxisAttribute.AxisAttributeStart |
| 830 | AxisAttribute.AddAxis = AxisAttribute.AxisAttributeAddAxis |
| 831 | AxisAttribute.End = AxisAttribute.AxisAttributeEnd |
| 832 | from tosa import ClampAttribute |
| 833 | |
| 834 | if not hasattr(ClampAttribute, "Start"): |
| 835 | ClampAttribute.Start = ClampAttribute.ClampAttributeStart |
| 836 | ClampAttribute.AddMinInt = ClampAttribute.ClampAttributeAddMinInt |
| 837 | ClampAttribute.AddMaxInt = ClampAttribute.ClampAttributeAddMaxInt |
| 838 | ClampAttribute.AddMinFp = ClampAttribute.ClampAttributeAddMinFp |
| 839 | ClampAttribute.AddMaxFp = ClampAttribute.ClampAttributeAddMaxFp |
| 840 | ClampAttribute.End = ClampAttribute.ClampAttributeEnd |
| 841 | from tosa import CondIfAttribute |
| 842 | |
| 843 | if not hasattr(CondIfAttribute, "Start"): |
| 844 | CondIfAttribute.Start = CondIfAttribute.CondIfAttributeStart |
| 845 | CondIfAttribute.AddThenBranch = CondIfAttribute.CondIfAttributeAddThenBranch |
| 846 | CondIfAttribute.AddElseBranch = CondIfAttribute.CondIfAttributeAddElseBranch |
| 847 | CondIfAttribute.End = CondIfAttribute.CondIfAttributeEnd |
| 848 | from tosa import ConvAttribute |
| 849 | |
| 850 | if not hasattr(ConvAttribute, "Start"): |
| 851 | ConvAttribute.Start = ConvAttribute.ConvAttributeStart |
| 852 | ConvAttribute.AddPad = ConvAttribute.ConvAttributeAddPad |
| 853 | ConvAttribute.StartPadVector = ConvAttribute.ConvAttributeStartPadVector |
| 854 | ConvAttribute.AddStride = ConvAttribute.ConvAttributeAddStride |
| 855 | ConvAttribute.StartStrideVector = ( |
| 856 | ConvAttribute.ConvAttributeStartStrideVector |
| 857 | ) |
| 858 | ConvAttribute.AddDilation = ConvAttribute.ConvAttributeAddDilation |
| 859 | ConvAttribute.StartDilationVector = ( |
| 860 | ConvAttribute.ConvAttributeStartDilationVector |
| 861 | ) |
Eric Kunze | bdcc3fe | 2022-06-07 05:17:37 +0000 | [diff] [blame] | 862 | ConvAttribute.AddInputZp = ConvAttribute.ConvAttributeAddInputZp |
| 863 | ConvAttribute.AddWeightZp = ConvAttribute.ConvAttributeAddWeightZp |
Eric Kunze | ae906de | 2022-05-30 22:40:47 -0700 | [diff] [blame] | 864 | ConvAttribute.End = ConvAttribute.ConvAttributeEnd |
Eric Kunze | bdcc3fe | 2022-06-07 05:17:37 +0000 | [diff] [blame] | 865 | from tosa import FullyConnectedAttribute |
Eric Kunze | ae906de | 2022-05-30 22:40:47 -0700 | [diff] [blame] | 866 | |
Eric Kunze | bdcc3fe | 2022-06-07 05:17:37 +0000 | [diff] [blame] | 867 | if not hasattr(FullyConnectedAttribute, "Start"): |
| 868 | FullyConnectedAttribute.Start = ( |
| 869 | FullyConnectedAttribute.FullyConnectedAttributeStart |
| 870 | ) |
| 871 | FullyConnectedAttribute.AddInputZp = ( |
| 872 | FullyConnectedAttribute.FullyConnectedAttributeAddInputZp |
| 873 | ) |
| 874 | FullyConnectedAttribute.AddWeightZp = ( |
| 875 | FullyConnectedAttribute.FullyConnectedAttributeAddWeightZp |
| 876 | ) |
| 877 | FullyConnectedAttribute.End = ( |
| 878 | FullyConnectedAttribute.FullyConnectedAttributeEnd |
| 879 | ) |
| 880 | from tosa import MatMulAttribute |
Eric Kunze | ae906de | 2022-05-30 22:40:47 -0700 | [diff] [blame] | 881 | |
Eric Kunze | bdcc3fe | 2022-06-07 05:17:37 +0000 | [diff] [blame] | 882 | if not hasattr(MatMulAttribute, "Start"): |
| 883 | MatMulAttribute.Start = MatMulAttribute.MatMulAttributeStart |
| 884 | MatMulAttribute.AddAZp = MatMulAttribute.MatMulAttributeAddAZp |
| 885 | MatMulAttribute.AddBZp = MatMulAttribute.MatMulAttributeAddBZp |
| 886 | MatMulAttribute.End = MatMulAttribute.MatMulAttributeEnd |
| 887 | from tosa import PoolAttribute |
| 888 | |
| 889 | if not hasattr(PoolAttribute, "Start"): |
| 890 | PoolAttribute.Start = PoolAttribute.PoolAttributeStart |
| 891 | PoolAttribute.AddPad = PoolAttribute.PoolAttributeAddPad |
| 892 | PoolAttribute.StartPadVector = PoolAttribute.PoolAttributeStartPadVector |
| 893 | PoolAttribute.AddKernel = PoolAttribute.PoolAttributeAddKernel |
| 894 | PoolAttribute.StartKernelVector = ( |
| 895 | PoolAttribute.PoolAttributeStartKernelVector |
| 896 | ) |
| 897 | PoolAttribute.AddStride = PoolAttribute.PoolAttributeAddStride |
| 898 | PoolAttribute.StartStrideVector = ( |
| 899 | PoolAttribute.PoolAttributeStartStrideVector |
| 900 | ) |
| 901 | PoolAttribute.AddInputZp = PoolAttribute.PoolAttributeAddInputZp |
| 902 | PoolAttribute.AddOutputZp = PoolAttribute.PoolAttributeAddOutputZp |
| 903 | PoolAttribute.End = PoolAttribute.PoolAttributeEnd |
Eric Kunze | ae906de | 2022-05-30 22:40:47 -0700 | [diff] [blame] | 904 | from tosa import MulAttribute |
| 905 | |
| 906 | if not hasattr(MulAttribute, "Start"): |
| 907 | MulAttribute.Start = MulAttribute.MulAttributeStart |
| 908 | MulAttribute.AddShift = MulAttribute.MulAttributeAddShift |
| 909 | MulAttribute.End = MulAttribute.MulAttributeEnd |
| 910 | from tosa import PadAttribute |
| 911 | |
| 912 | if not hasattr(PadAttribute, "Start"): |
| 913 | PadAttribute.Start = PadAttribute.PadAttributeStart |
| 914 | PadAttribute.AddPadding = PadAttribute.PadAttributeAddPadding |
| 915 | PadAttribute.StartPaddingVector = ( |
| 916 | PadAttribute.PadAttributeStartPaddingVector |
| 917 | ) |
| 918 | PadAttribute.AddPadConstInt = PadAttribute.PadAttributeAddPadConstInt |
| 919 | PadAttribute.AddPadConstFp = PadAttribute.PadAttributeAddPadConstFp |
| 920 | PadAttribute.End = PadAttribute.PadAttributeEnd |
Eric Kunze | ae906de | 2022-05-30 22:40:47 -0700 | [diff] [blame] | 921 | from tosa import PoolAttribute |
| 922 | |
| 923 | if not hasattr(PoolAttribute, "Start"): |
| 924 | PoolAttribute.Start = PoolAttribute.PoolAttributeStart |
| 925 | PoolAttribute.AddPad = PoolAttribute.PoolAttributeAddPad |
| 926 | PoolAttribute.StartPadVector = PoolAttribute.PoolAttributeStartPadVector |
| 927 | PoolAttribute.AddKernel = PoolAttribute.PoolAttributeAddKernel |
| 928 | PoolAttribute.StartKernelVector = ( |
| 929 | PoolAttribute.PoolAttributeStartKernelVector |
| 930 | ) |
| 931 | PoolAttribute.AddStride = PoolAttribute.PoolAttributeAddStride |
| 932 | PoolAttribute.StartStrideVector = ( |
| 933 | PoolAttribute.PoolAttributeStartStrideVector |
| 934 | ) |
Eric Kunze | bdcc3fe | 2022-06-07 05:17:37 +0000 | [diff] [blame] | 935 | PoolAttribute.AddInputZp = PoolAttribute.PoolAttributeAddInputZp |
| 936 | PoolAttribute.AddOutputZp = PoolAttribute.PoolAttributeAddOutputZp |
Eric Kunze | ae906de | 2022-05-30 22:40:47 -0700 | [diff] [blame] | 937 | PoolAttribute.End = PoolAttribute.PoolAttributeEnd |
| 938 | from tosa import RescaleAttribute |
| 939 | |
| 940 | if not hasattr(RescaleAttribute, "Start"): |
| 941 | RescaleAttribute.Start = RescaleAttribute.RescaleAttributeStart |
| 942 | RescaleAttribute.AddInputZp = RescaleAttribute.RescaleAttributeAddInputZp |
| 943 | RescaleAttribute.AddOutputZp = RescaleAttribute.RescaleAttributeAddOutputZp |
| 944 | RescaleAttribute.AddMultiplier = ( |
| 945 | RescaleAttribute.RescaleAttributeAddMultiplier |
| 946 | ) |
| 947 | RescaleAttribute.StartMultiplierVector = ( |
| 948 | RescaleAttribute.RescaleAttributeStartMultiplierVector |
| 949 | ) |
| 950 | RescaleAttribute.AddShift = RescaleAttribute.RescaleAttributeAddShift |
| 951 | RescaleAttribute.StartShiftVector = ( |
| 952 | RescaleAttribute.RescaleAttributeStartShiftVector |
| 953 | ) |
| 954 | RescaleAttribute.AddScale32 = RescaleAttribute.RescaleAttributeAddScale32 |
| 955 | RescaleAttribute.AddDoubleRound = ( |
| 956 | RescaleAttribute.RescaleAttributeAddDoubleRound |
| 957 | ) |
| 958 | RescaleAttribute.AddPerChannel = ( |
| 959 | RescaleAttribute.RescaleAttributeAddPerChannel |
| 960 | ) |
| 961 | RescaleAttribute.End = RescaleAttribute.RescaleAttributeEnd |
| 962 | from tosa import ReshapeAttribute |
| 963 | |
| 964 | if not hasattr(ReshapeAttribute, "Start"): |
| 965 | ReshapeAttribute.Start = ReshapeAttribute.ReshapeAttributeStart |
| 966 | ReshapeAttribute.AddNewShape = ReshapeAttribute.ReshapeAttributeAddNewShape |
| 967 | ReshapeAttribute.StartNewShapeVector = ( |
| 968 | ReshapeAttribute.ReshapeAttributeStartNewShapeVector |
| 969 | ) |
| 970 | ReshapeAttribute.End = ReshapeAttribute.ReshapeAttributeEnd |
| 971 | from tosa import ResizeAttribute |
| 972 | |
| 973 | if not hasattr(ResizeAttribute, "Start"): |
| 974 | ResizeAttribute.Start = ResizeAttribute.ResizeAttributeStart |
TatWai Chong | 49b1ca6 | 2022-06-10 01:49:13 -0700 | [diff] [blame] | 975 | ResizeAttribute.AddScale = ResizeAttribute.ResizeAttributeAddScale |
| 976 | ResizeAttribute.StartScaleVector = ( |
| 977 | ResizeAttribute.ResizeAttributeStartScaleVector |
Eric Kunze | ae906de | 2022-05-30 22:40:47 -0700 | [diff] [blame] | 978 | ) |
| 979 | ResizeAttribute.AddOffset = ResizeAttribute.ResizeAttributeAddOffset |
| 980 | ResizeAttribute.StartOffsetVector = ( |
| 981 | ResizeAttribute.ResizeAttributeStartOffsetVector |
| 982 | ) |
TatWai Chong | 49b1ca6 | 2022-06-10 01:49:13 -0700 | [diff] [blame] | 983 | ResizeAttribute.AddBorder = ResizeAttribute.ResizeAttributeAddBorder |
| 984 | ResizeAttribute.StartBorderVector = ( |
| 985 | ResizeAttribute.ResizeAttributeStartBorderVector |
Eric Kunze | ae906de | 2022-05-30 22:40:47 -0700 | [diff] [blame] | 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 |