|
| 1 | +From b3c8c3f779a7e051826f317598fb831fa9cfe923 Mon Sep 17 00:00:00 2001 |
| 2 | +From: =?UTF-8?q?Per=20=C3=85strand?= <per.astrand@arm.com> |
| 3 | +Date: Wed, 5 Feb 2025 12:30:09 +0100 |
| 4 | +Subject: [PATCH] Make TOSA serializer lib to be self contained |
| 5 | + |
| 6 | +--- |
| 7 | + CMakeLists.txt | 4 ++ |
| 8 | + python/serializer/tosa_serializer.py | 57 ++++++++++++++-------------- |
| 9 | + 2 files changed, 32 insertions(+), 29 deletions(-) |
| 10 | + |
| 11 | +diff --git a/CMakeLists.txt b/CMakeLists.txt |
| 12 | +index ac34b75..5e191aa 100644 |
| 13 | +--- a/CMakeLists.txt |
| 14 | ++++ b/CMakeLists.txt |
| 15 | +@@ -19,6 +19,8 @@ |
| 16 | + cmake_minimum_required(VERSION 3.13.4) |
| 17 | + project(TosaSerialization) |
| 18 | + |
| 19 | ++cmake_policy(SET CMP0077 NEW) |
| 20 | ++ |
| 21 | + set(CMAKE_CXX_STANDARD 14 CACHE STRING "C++ standard to conform to") |
| 22 | + set(CMAKE_CXX_STANDARD_REQUIRED YES) |
| 23 | + |
| 24 | +@@ -27,6 +29,8 @@ set(CMAKE_VERBOSE_MAKEFILE ON) |
| 25 | + option(BUILD_TESTS "Build test applications" ON) |
| 26 | + option(FLATBUFFERS_ROOT "Location where the flatbuffers 'include' and 'lib' folders to be found" Off) |
| 27 | + |
| 28 | ++message(STATUS "FLATBUFFERS_ROOT set to: ${FLATBUFFERS_ROOT}") |
| 29 | ++ |
| 30 | + include_directories(${PROJECT_SOURCE_DIR}/third_party/half/include) |
| 31 | + |
| 32 | + include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include) |
| 33 | +diff --git a/python/serializer/tosa_serializer.py b/python/serializer/tosa_serializer.py |
| 34 | +index 7bc75f0..d191997 100644 |
| 35 | +--- a/python/serializer/tosa_serializer.py |
| 36 | ++++ b/python/serializer/tosa_serializer.py |
| 37 | +@@ -14,12 +14,11 @@ |
| 38 | + |
| 39 | + import os |
| 40 | + import struct |
| 41 | +-import serializer.tosa_serializer as ts |
| 42 | + import json |
| 43 | + import flatbuffers |
| 44 | + import numpy as np |
| 45 | + from enum import IntEnum, unique |
| 46 | +-from tosa import ( |
| 47 | ++from ..tosa import ( |
| 48 | + TosaGraph, |
| 49 | + TosaRegion, |
| 50 | + TosaBasicBlock, |
| 51 | +@@ -27,8 +26,8 @@ from tosa import ( |
| 52 | + TosaOperator, |
| 53 | + Version, |
| 54 | + ) |
| 55 | +-import tosa.DType as TosaDType |
| 56 | +-import tosa.Op as TosaOp |
| 57 | ++from ..tosa import DType as TosaDType |
| 58 | ++from ..tosa import Op as TosaOp |
| 59 | + |
| 60 | + # Keep version number in sync with the version default value with schema/tosa.fbs |
| 61 | + TOSA_VERSION_MAJOR = 0 |
| 62 | +@@ -159,7 +158,7 @@ class TosaSerializerAttribute(TosaSerializerUnion): |
| 63 | + output_zp, |
| 64 | + accum_dtype, |
| 65 | + ): |
| 66 | +- from tosa import PoolAttribute as a, Attribute |
| 67 | ++ from ..tosa import PoolAttribute as a, Attribute |
| 68 | + |
| 69 | + self.utype = Attribute.Attribute().PoolAttribute |
| 70 | + |
| 71 | +@@ -172,7 +171,7 @@ class TosaSerializerAttribute(TosaSerializerUnion): |
| 72 | + self.ints.append((a.AddAccumDtype, accum_dtype)) |
| 73 | + |
| 74 | + def ConvAttribute(self, pad, stride, dilation, input_zp, weight_zp, local_bound): |
| 75 | +- from tosa import ConvAttribute as a, Attribute |
| 76 | ++ from ..tosa import ConvAttribute as a, Attribute |
| 77 | + |
| 78 | + self.utype = Attribute.Attribute().ConvAttribute |
| 79 | + self.optFcns = (a.Start, a.End) |
| 80 | +@@ -187,7 +186,7 @@ class TosaSerializerAttribute(TosaSerializerUnion): |
| 81 | + def TransposeConvAttribute( |
| 82 | + self, outpad, stride, output_shape, input_zp, weight_zp, local_bound |
| 83 | + ): |
| 84 | +- from tosa import TransposeConvAttribute as a, Attribute |
| 85 | ++ from ..tosa import TransposeConvAttribute as a, Attribute |
| 86 | + |
| 87 | + self.utype = Attribute.Attribute().TransposeConvAttribute |
| 88 | + self.optFcns = (a.Start, a.End) |
| 89 | +@@ -200,7 +199,7 @@ class TosaSerializerAttribute(TosaSerializerUnion): |
| 90 | + self.bools.append((a.AddLocalBound, local_bound)) |
| 91 | + |
| 92 | + def PadAttribute(self, serializer_builder, padding, pad_const_int, pad_const_fp): |
| 93 | +- from tosa import PadAttribute as a, Attribute |
| 94 | ++ from ..tosa import PadAttribute as a, Attribute |
| 95 | + |
| 96 | + self.utype = Attribute.Attribute().PadAttribute |
| 97 | + self.optFcns = (a.Start, a.End) |
| 98 | +@@ -210,14 +209,14 @@ class TosaSerializerAttribute(TosaSerializerUnion): |
| 99 | + |
| 100 | + # pad_const_fp attribute serialized as uint8 vector |
| 101 | + pad_const_float_as_bytes = struct.pack("<f", pad_const_fp) |
| 102 | +- serialized_pad_const_fp = ts.TosaSerializer.serializeUint8Vec( |
| 103 | ++ serialized_pad_const_fp = TosaSerializer.serializeUint8Vec( |
| 104 | + serializer_builder, pad_const_float_as_bytes |
| 105 | + ) |
| 106 | + |
| 107 | + self.floats.append((a.AddPadConstFp, serialized_pad_const_fp)) |
| 108 | + |
| 109 | + def AxisAttribute(self, axis): |
| 110 | +- from tosa import AxisAttribute as a, Attribute |
| 111 | ++ from ..tosa import AxisAttribute as a, Attribute |
| 112 | + |
| 113 | + self.utype = Attribute.Attribute().AxisAttribute |
| 114 | + self.optFcns = (a.Start, a.End) |
| 115 | +@@ -225,7 +224,7 @@ class TosaSerializerAttribute(TosaSerializerUnion): |
| 116 | + self.ints.append((a.AddAxis, axis)) |
| 117 | + |
| 118 | + def ReshapeAttribute(self, new_shape): |
| 119 | +- from tosa import ReshapeAttribute as a, Attribute |
| 120 | ++ from ..tosa import ReshapeAttribute as a, Attribute |
| 121 | + |
| 122 | + self.utype = Attribute.Attribute().ReshapeAttribute |
| 123 | + self.optFcns = (a.Start, a.End) |
| 124 | +@@ -233,7 +232,7 @@ class TosaSerializerAttribute(TosaSerializerUnion): |
| 125 | + self.intvecs.append((a.AddNewShape, new_shape)) |
| 126 | + |
| 127 | + def SliceAttribute(self, start, size): |
| 128 | +- from tosa import SliceAttribute as a, Attribute |
| 129 | ++ from ..tosa import SliceAttribute as a, Attribute |
| 130 | + |
| 131 | + self.utype = Attribute.Attribute().SliceAttribute |
| 132 | + self.optFcns = (a.Start, a.End) |
| 133 | +@@ -242,7 +241,7 @@ class TosaSerializerAttribute(TosaSerializerUnion): |
| 134 | + self.intvecs.append((a.AddSize, size)) |
| 135 | + |
| 136 | + def TileAttribute(self, multiples): |
| 137 | +- from tosa import TileAttribute as a, Attribute |
| 138 | ++ from ..tosa import TileAttribute as a, Attribute |
| 139 | + |
| 140 | + self.utype = Attribute.Attribute().TileAttribute |
| 141 | + self.optFcns = (a.Start, a.End) |
| 142 | +@@ -250,7 +249,7 @@ class TosaSerializerAttribute(TosaSerializerUnion): |
| 143 | + self.intvecs.append((a.AddMultiples, multiples)) |
| 144 | + |
| 145 | + def ResizeAttribute(self, scale, offset, border, mode): |
| 146 | +- from tosa import ResizeAttribute as a, Attribute |
| 147 | ++ from ..tosa import ResizeAttribute as a, Attribute |
| 148 | + |
| 149 | + self.utype = Attribute.Attribute().ResizeAttribute |
| 150 | + self.optFcns = (a.Start, a.End) |
| 151 | +@@ -261,7 +260,7 @@ class TosaSerializerAttribute(TosaSerializerUnion): |
| 152 | + self.ints.append((a.AddMode, mode)) |
| 153 | + |
| 154 | + def ClampAttribute(self, serializer_builder, minint, maxint, minfp, maxfp): |
| 155 | +- from tosa import ClampAttribute as a, Attribute |
| 156 | ++ from ..tosa import ClampAttribute as a, Attribute |
| 157 | + |
| 158 | + self.utype = Attribute.Attribute().ClampAttribute |
| 159 | + self.optFcns = (a.Start, a.End) |
| 160 | +@@ -272,10 +271,10 @@ class TosaSerializerAttribute(TosaSerializerUnion): |
| 161 | + # min/max float attributes serialized as uint8 vectors |
| 162 | + minfp_bytes = struct.pack("<f", minfp) |
| 163 | + maxfp_bytes = struct.pack("<f", maxfp) |
| 164 | +- serialized_minfp_bytes = ts.TosaSerializer.serializeUint8Vec( |
| 165 | ++ serialized_minfp_bytes = TosaSerializer.serializeUint8Vec( |
| 166 | + serializer_builder, minfp_bytes |
| 167 | + ) |
| 168 | +- serialized_maxfp_bytes = ts.TosaSerializer.serializeUint8Vec( |
| 169 | ++ serialized_maxfp_bytes = TosaSerializer.serializeUint8Vec( |
| 170 | + serializer_builder, maxfp_bytes |
| 171 | + ) |
| 172 | + |
| 173 | +@@ -294,7 +293,7 @@ class TosaSerializerAttribute(TosaSerializerUnion): |
| 174 | + input_unsigned, |
| 175 | + output_unsigned, |
| 176 | + ): |
| 177 | +- from tosa import RescaleAttribute as a, Attribute |
| 178 | ++ from ..tosa import RescaleAttribute as a, Attribute |
| 179 | + |
| 180 | + self.utype = Attribute.Attribute().RescaleAttribute |
| 181 | + self.optFcns = (a.Start, a.End) |
| 182 | +@@ -310,7 +309,7 @@ class TosaSerializerAttribute(TosaSerializerUnion): |
| 183 | + self.bools.append((a.AddOutputUnsigned, output_unsigned)) |
| 184 | + |
| 185 | + def MulAttribute(self, shift): |
| 186 | +- from tosa import MulAttribute as a, Attribute |
| 187 | ++ from ..tosa import MulAttribute as a, Attribute |
| 188 | + |
| 189 | + self.utype = Attribute.Attribute().MulAttribute |
| 190 | + self.optFcns = (a.Start, a.End) |
| 191 | +@@ -318,7 +317,7 @@ class TosaSerializerAttribute(TosaSerializerUnion): |
| 192 | + self.ints.append((a.AddShift, shift)) |
| 193 | + |
| 194 | + def ArithmeticRightShiftAttribute(self, round): |
| 195 | +- from tosa import ArithmeticRightShiftAttribute as a, Attribute |
| 196 | ++ from ..tosa import ArithmeticRightShiftAttribute as a, Attribute |
| 197 | + |
| 198 | + self.utype = Attribute.Attribute().ArithmeticRightShiftAttribute |
| 199 | + self.optFcns = ( |
| 200 | +@@ -329,7 +328,7 @@ class TosaSerializerAttribute(TosaSerializerUnion): |
| 201 | + self.bools.append((a.AddRound, round)) |
| 202 | + |
| 203 | + def CondIfAttribute(self, then_branch, else_branch): |
| 204 | +- from tosa import CondIfAttribute as a, Attribute |
| 205 | ++ from ..tosa import CondIfAttribute as a, Attribute |
| 206 | + |
| 207 | + self.utype = Attribute.Attribute().CondIfAttribute |
| 208 | + self.optFcns = (a.Start, a.End) |
| 209 | +@@ -338,7 +337,7 @@ class TosaSerializerAttribute(TosaSerializerUnion): |
| 210 | + self.strings.append((a.AddElseBranch, else_branch)) |
| 211 | + |
| 212 | + def WhileLoopAttribute(self, cond_branch, body_branch): |
| 213 | +- from tosa import WhileLoopAttribute as a, Attribute |
| 214 | ++ from ..tosa import WhileLoopAttribute as a, Attribute |
| 215 | + |
| 216 | + self.utype = Attribute.Attribute().WhileLoopAttribute |
| 217 | + self.optFcns = (a.Start, a.End) |
| 218 | +@@ -347,7 +346,7 @@ class TosaSerializerAttribute(TosaSerializerUnion): |
| 219 | + self.strings.append((a.AddBodyBranch, body_branch)) |
| 220 | + |
| 221 | + def TransposeAttribute(self, perms): |
| 222 | +- from tosa import TransposeAttribute as a, Attribute |
| 223 | ++ from ..tosa import TransposeAttribute as a, Attribute |
| 224 | + |
| 225 | + self.utype = Attribute.Attribute().TransposeAttribute |
| 226 | + self.optFcns = (a.Start, a.End) |
| 227 | +@@ -355,7 +354,7 @@ class TosaSerializerAttribute(TosaSerializerUnion): |
| 228 | + self.intvecs.append((a.AddPerms, perms)) |
| 229 | + |
| 230 | + def TableAttribute(self, table): |
| 231 | +- from tosa import TableAttribute as a, Attribute |
| 232 | ++ from ..tosa import TableAttribute as a, Attribute |
| 233 | + |
| 234 | + self.utype = Attribute.Attribute().TableAttribute |
| 235 | + self.optFcns = (a.Start, a.End) |
| 236 | +@@ -363,7 +362,7 @@ class TosaSerializerAttribute(TosaSerializerUnion): |
| 237 | + self.int16vecs.append((a.AddTable, table)) |
| 238 | + |
| 239 | + def MatMulAttribute(self, A_zp, B_zp): |
| 240 | +- from tosa import MatMulAttribute as a, Attribute |
| 241 | ++ from ..tosa import MatMulAttribute as a, Attribute |
| 242 | + |
| 243 | + self.utype = Attribute.Attribute().MatMulAttribute |
| 244 | + self.optFcns = (a.Start, a.End) |
| 245 | +@@ -372,7 +371,7 @@ class TosaSerializerAttribute(TosaSerializerUnion): |
| 246 | + self.ints.append((a.AddBZp, B_zp)) |
| 247 | + |
| 248 | + def FullyConnectedAttribute(self, input_zp, weight_zp): |
| 249 | +- from tosa import FullyConnectedAttribute as a, Attribute |
| 250 | ++ from ..tosa import FullyConnectedAttribute as a, Attribute |
| 251 | + |
| 252 | + self.utype = Attribute.Attribute().FullyConnectedAttribute |
| 253 | + self.optFcns = (a.Start, a.End) |
| 254 | +@@ -381,7 +380,7 @@ class TosaSerializerAttribute(TosaSerializerUnion): |
| 255 | + self.ints.append((a.AddWeightZp, weight_zp)) |
| 256 | + |
| 257 | + def NegateAttribute(self, input1_zp, output_zp): |
| 258 | +- from tosa import NegateAttribute as a, Attribute |
| 259 | ++ from ..tosa import NegateAttribute as a, Attribute |
| 260 | + |
| 261 | + self.utype = Attribute.Attribute().NegateAttribute |
| 262 | + self.optFcns = (a.Start, a.End) |
| 263 | +@@ -390,7 +389,7 @@ class TosaSerializerAttribute(TosaSerializerUnion): |
| 264 | + self.ints.append((a.AddOutputZp, output_zp)) |
| 265 | + |
| 266 | + def FFTAttribute(self, inverse, local_bound): |
| 267 | +- from tosa import FFTAttribute as a, Attribute |
| 268 | ++ from ..tosa import FFTAttribute as a, Attribute |
| 269 | + |
| 270 | + self.utype = Attribute.Attribute().FFTAttribute |
| 271 | + self.optFcns = (a.Start, a.End) |
| 272 | +@@ -399,7 +398,7 @@ class TosaSerializerAttribute(TosaSerializerUnion): |
| 273 | + self.bools.append((a.AddLocalBound, local_bound)) |
| 274 | + |
| 275 | + def RFFTAttribute(self, local_bound): |
| 276 | +- from tosa import RFFTAttribute as a, Attribute |
| 277 | ++ from ..tosa import RFFTAttribute as a, Attribute |
| 278 | + |
| 279 | + self.utype = Attribute.Attribute().RFFTAttribute |
| 280 | + self.optFcns = (a.Start, a.End) |
| 281 | +-- |
| 282 | +2.39.5 (Apple Git-154) |
| 283 | + |
0 commit comments