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