Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame^] | 1 | # Copyright (C) 2020 Arm Limited or its affiliates. All rights reserved. |
| 2 | # |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the License); you may |
| 6 | # not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | |
| 18 | # Description: |
| 19 | # TensorFlow Lite mapping functions used by both reader and writer. |
| 20 | # Contains a mapping from the various TensorFlow Lite enums and options structs, generated by the FlatBuffer code |
| 21 | # generator, to Vela's internal format. |
| 22 | |
| 23 | import numpy as np |
| 24 | import struct |
| 25 | |
| 26 | from .data_type import DataType |
| 27 | |
| 28 | from .tflite.TensorType import TensorType |
| 29 | from .tflite.BuiltinOperator import BuiltinOperator |
| 30 | from .tflite.BuiltinOptions import BuiltinOptions |
| 31 | |
| 32 | |
| 33 | from .tflite.Padding import Padding |
| 34 | from .tflite.ActivationFunctionType import ActivationFunctionType |
| 35 | |
| 36 | from .tflite import Conv2DOptions |
| 37 | from .tflite import DepthwiseConv2DOptions |
| 38 | from .tflite import ConcatEmbeddingsOptions |
| 39 | from .tflite import LSHProjectionOptions |
| 40 | from .tflite import Pool2DOptions |
| 41 | from .tflite import SVDFOptions |
| 42 | from .tflite import RNNOptions |
| 43 | from .tflite import FullyConnectedOptions |
| 44 | from .tflite import SoftmaxOptions |
| 45 | from .tflite import ConcatenationOptions |
| 46 | from .tflite import AddOptions |
| 47 | from .tflite import L2NormOptions |
| 48 | from .tflite import LocalResponseNormalizationOptions |
| 49 | from .tflite import LSTMOptions |
| 50 | from .tflite import ResizeBilinearOptions |
| 51 | from .tflite import CallOptions |
| 52 | from .tflite import ReshapeOptions |
| 53 | from .tflite import SkipGramOptions |
| 54 | from .tflite import SpaceToDepthOptions |
| 55 | from .tflite import EmbeddingLookupSparseOptions |
| 56 | from .tflite import MulOptions |
| 57 | from .tflite import PadOptions |
| 58 | from .tflite import GatherOptions |
| 59 | from .tflite import BatchToSpaceNDOptions |
| 60 | from .tflite import SpaceToBatchNDOptions |
| 61 | from .tflite import TransposeOptions |
| 62 | from .tflite import ReducerOptions |
| 63 | from .tflite import SubOptions |
| 64 | from .tflite import DivOptions |
| 65 | from .tflite import SqueezeOptions |
| 66 | from .tflite import SequenceRNNOptions |
| 67 | from .tflite import StridedSliceOptions |
| 68 | from .tflite import ExpOptions |
| 69 | from .tflite import TopKV2Options |
| 70 | from .tflite import SplitOptions |
| 71 | from .tflite import LogSoftmaxOptions |
| 72 | from .tflite import CastOptions |
| 73 | from .tflite import DequantizeOptions |
| 74 | from .tflite import MaximumMinimumOptions |
| 75 | from .tflite import ArgMaxOptions |
| 76 | from .tflite import LessOptions |
| 77 | from .tflite import NegOptions |
| 78 | from .tflite import PadV2Options |
| 79 | from .tflite import GreaterOptions |
| 80 | from .tflite import GreaterEqualOptions |
| 81 | from .tflite import LessEqualOptions |
| 82 | from .tflite import SelectOptions |
| 83 | from .tflite import SliceOptions |
| 84 | from .tflite import TransposeConvOptions |
| 85 | from .tflite import SparseToDenseOptions |
| 86 | from .tflite import TileOptions |
| 87 | from .tflite import ExpandDimsOptions |
| 88 | from .tflite import EqualOptions |
| 89 | from .tflite import NotEqualOptions |
| 90 | from .tflite import ShapeOptions |
| 91 | from .tflite import PowOptions |
| 92 | from .tflite import ArgMinOptions |
| 93 | from .tflite import FakeQuantOptions |
| 94 | from .tflite import PackOptions |
| 95 | from .tflite import LogicalOrOptions |
| 96 | from .tflite import OneHotOptions |
| 97 | from .tflite import LogicalAndOptions |
| 98 | from .tflite import LogicalNotOptions |
| 99 | from .tflite import UnpackOptions |
| 100 | from .tflite import FloorDivOptions |
| 101 | from .tflite import SquareOptions |
| 102 | from .tflite import ZerosLikeOptions |
| 103 | from .tflite import FillOptions |
| 104 | from .tflite import BidirectionalSequenceLSTMOptions |
| 105 | from .tflite import BidirectionalSequenceRNNOptions |
| 106 | from .tflite import UnidirectionalSequenceLSTMOptions |
| 107 | from .tflite import FloorModOptions |
| 108 | from .tflite import RangeOptions |
| 109 | from .tflite import ResizeNearestNeighborOptions |
| 110 | from .tflite import LeakyReluOptions |
| 111 | from .tflite import SquaredDifferenceOptions |
| 112 | from .tflite import MirrorPadOptions |
| 113 | from .tflite import AbsOptions |
| 114 | from .tflite import SplitVOptions |
| 115 | from .tflite import UniqueOptions |
| 116 | from .tflite import ReverseV2Options |
| 117 | from .tflite import AddNOptions |
| 118 | from .tflite import GatherNdOptions |
| 119 | from .tflite import CosOptions |
| 120 | from .tflite import WhereOptions |
| 121 | from .tflite import RankOptions |
| 122 | from .tflite import ReverseSequenceOptions |
| 123 | from .tflite import MatrixDiagOptions |
| 124 | from .tflite import QuantizeOptions |
| 125 | from .tflite import MatrixSetDiagOptions |
| 126 | from .tflite import DensifyOptions |
| 127 | from .tflite import DepthToSpaceOptions |
| 128 | from .tflite import IfOptions |
| 129 | from .tflite import NonMaxSuppressionV4Options |
| 130 | from .tflite import NonMaxSuppressionV5Options |
| 131 | from .tflite import ScatterNdOptions |
| 132 | from .tflite import SegmentSumOptions |
| 133 | from .tflite import SelectV2Options |
| 134 | from .tflite import WhileOptions |
| 135 | |
| 136 | |
| 137 | def inverse_map(map): |
| 138 | return {v: k for k, v in map.items()} |
| 139 | |
| 140 | |
| 141 | datatype_map = { |
| 142 | TensorType.UINT8: DataType.uint8, |
| 143 | TensorType.INT8: DataType.int8, |
| 144 | TensorType.INT16: DataType.int16, |
| 145 | TensorType.INT32: DataType.int32, |
| 146 | TensorType.INT64: DataType.int64, |
| 147 | TensorType.FLOAT16: DataType.float16, |
| 148 | TensorType.FLOAT32: DataType.float32, |
| 149 | TensorType.STRING: DataType.string, |
| 150 | TensorType.BOOL: DataType.bool, |
| 151 | # no TensorType.COMPLEX64 for now |
| 152 | } |
| 153 | |
| 154 | datatype_inv_map = inverse_map(datatype_map) |
| 155 | datatype_inv_map[DataType.quint8] = TensorType.UINT8 |
| 156 | |
| 157 | datatype_inv_map[DataType.qint8] = TensorType.INT8 |
| 158 | datatype_inv_map[DataType.qint16] = TensorType.INT16 |
| 159 | datatype_inv_map[DataType.qint32] = TensorType.INT32 |
| 160 | |
| 161 | |
| 162 | datatype_map_numpy = { |
| 163 | TensorType.UINT8: np.uint8, |
| 164 | TensorType.INT8: np.int8, |
| 165 | TensorType.INT16: np.int16, |
| 166 | TensorType.INT32: np.int32, |
| 167 | TensorType.INT64: np.int64, |
| 168 | TensorType.FLOAT16: np.float16, |
| 169 | TensorType.FLOAT32: np.float32, |
| 170 | TensorType.BOOL: np.bool, |
| 171 | } |
| 172 | |
| 173 | |
| 174 | builtin_options_map = { |
| 175 | BuiltinOptions.Conv2DOptions: Conv2DOptions.Conv2DOptions, |
| 176 | BuiltinOptions.DepthwiseConv2DOptions: DepthwiseConv2DOptions.DepthwiseConv2DOptions, |
| 177 | BuiltinOptions.ConcatEmbeddingsOptions: ConcatEmbeddingsOptions.ConcatEmbeddingsOptions, |
| 178 | BuiltinOptions.LSHProjectionOptions: LSHProjectionOptions.LSHProjectionOptions, |
| 179 | BuiltinOptions.Pool2DOptions: Pool2DOptions.Pool2DOptions, |
| 180 | BuiltinOptions.SVDFOptions: SVDFOptions.SVDFOptions, |
| 181 | BuiltinOptions.RNNOptions: RNNOptions.RNNOptions, |
| 182 | BuiltinOptions.FullyConnectedOptions: FullyConnectedOptions.FullyConnectedOptions, |
| 183 | BuiltinOptions.SoftmaxOptions: SoftmaxOptions.SoftmaxOptions, |
| 184 | BuiltinOptions.ConcatenationOptions: ConcatenationOptions.ConcatenationOptions, |
| 185 | BuiltinOptions.AddOptions: AddOptions.AddOptions, |
| 186 | BuiltinOptions.L2NormOptions: L2NormOptions.L2NormOptions, |
| 187 | BuiltinOptions.LocalResponseNormalizationOptions: LocalResponseNormalizationOptions.LocalResponseNormalizationOptions, # noqa: E501 |
| 188 | BuiltinOptions.LSTMOptions: LSTMOptions.LSTMOptions, |
| 189 | BuiltinOptions.ResizeBilinearOptions: ResizeBilinearOptions.ResizeBilinearOptions, |
| 190 | BuiltinOptions.CallOptions: CallOptions.CallOptions, |
| 191 | BuiltinOptions.ReshapeOptions: ReshapeOptions.ReshapeOptions, |
| 192 | BuiltinOptions.SkipGramOptions: SkipGramOptions.SkipGramOptions, |
| 193 | BuiltinOptions.SpaceToDepthOptions: SpaceToDepthOptions.SpaceToDepthOptions, |
| 194 | BuiltinOptions.EmbeddingLookupSparseOptions: EmbeddingLookupSparseOptions.EmbeddingLookupSparseOptions, |
| 195 | BuiltinOptions.MulOptions: MulOptions.MulOptions, |
| 196 | BuiltinOptions.PadOptions: PadOptions.PadOptions, |
| 197 | BuiltinOptions.GatherOptions: GatherOptions.GatherOptions, |
| 198 | BuiltinOptions.BatchToSpaceNDOptions: BatchToSpaceNDOptions.BatchToSpaceNDOptions, |
| 199 | BuiltinOptions.SpaceToBatchNDOptions: SpaceToBatchNDOptions.SpaceToBatchNDOptions, |
| 200 | BuiltinOptions.TransposeOptions: TransposeOptions.TransposeOptions, |
| 201 | BuiltinOptions.ReducerOptions: ReducerOptions.ReducerOptions, |
| 202 | BuiltinOptions.SubOptions: SubOptions.SubOptions, |
| 203 | BuiltinOptions.DivOptions: DivOptions.DivOptions, |
| 204 | BuiltinOptions.SqueezeOptions: SqueezeOptions.SqueezeOptions, |
| 205 | BuiltinOptions.SequenceRNNOptions: SequenceRNNOptions.SequenceRNNOptions, |
| 206 | BuiltinOptions.StridedSliceOptions: StridedSliceOptions.StridedSliceOptions, |
| 207 | BuiltinOptions.ExpOptions: ExpOptions.ExpOptions, |
| 208 | BuiltinOptions.TopKV2Options: TopKV2Options.TopKV2Options, |
| 209 | BuiltinOptions.SplitOptions: SplitOptions.SplitOptions, |
| 210 | BuiltinOptions.LogSoftmaxOptions: LogSoftmaxOptions.LogSoftmaxOptions, |
| 211 | BuiltinOptions.CastOptions: CastOptions.CastOptions, |
| 212 | BuiltinOptions.DequantizeOptions: DequantizeOptions.DequantizeOptions, |
| 213 | BuiltinOptions.MaximumMinimumOptions: MaximumMinimumOptions.MaximumMinimumOptions, |
| 214 | BuiltinOptions.ArgMaxOptions: ArgMaxOptions.ArgMaxOptions, |
| 215 | BuiltinOptions.LessOptions: LessOptions.LessOptions, |
| 216 | BuiltinOptions.NegOptions: NegOptions.NegOptions, |
| 217 | BuiltinOptions.PadV2Options: PadV2Options.PadV2Options, |
| 218 | BuiltinOptions.GreaterOptions: GreaterOptions.GreaterOptions, |
| 219 | BuiltinOptions.GreaterEqualOptions: GreaterEqualOptions.GreaterEqualOptions, |
| 220 | BuiltinOptions.LessEqualOptions: LessEqualOptions.LessEqualOptions, |
| 221 | BuiltinOptions.SelectOptions: SelectOptions.SelectOptions, |
| 222 | BuiltinOptions.SliceOptions: SliceOptions.SliceOptions, |
| 223 | BuiltinOptions.TransposeConvOptions: TransposeConvOptions.TransposeConvOptions, |
| 224 | BuiltinOptions.SparseToDenseOptions: SparseToDenseOptions.SparseToDenseOptions, |
| 225 | BuiltinOptions.TileOptions: TileOptions.TileOptions, |
| 226 | BuiltinOptions.ExpandDimsOptions: ExpandDimsOptions.ExpandDimsOptions, |
| 227 | BuiltinOptions.EqualOptions: EqualOptions.EqualOptions, |
| 228 | BuiltinOptions.NotEqualOptions: NotEqualOptions.NotEqualOptions, |
| 229 | BuiltinOptions.ShapeOptions: ShapeOptions.ShapeOptions, |
| 230 | BuiltinOptions.PowOptions: PowOptions.PowOptions, |
| 231 | BuiltinOptions.ArgMinOptions: ArgMinOptions.ArgMinOptions, |
| 232 | BuiltinOptions.FakeQuantOptions: FakeQuantOptions.FakeQuantOptions, |
| 233 | BuiltinOptions.PackOptions: PackOptions.PackOptions, |
| 234 | BuiltinOptions.LogicalOrOptions: LogicalOrOptions.LogicalOrOptions, |
| 235 | BuiltinOptions.OneHotOptions: OneHotOptions.OneHotOptions, |
| 236 | BuiltinOptions.LogicalAndOptions: LogicalAndOptions.LogicalAndOptions, |
| 237 | BuiltinOptions.LogicalNotOptions: LogicalNotOptions.LogicalNotOptions, |
| 238 | BuiltinOptions.UnpackOptions: UnpackOptions.UnpackOptions, |
| 239 | BuiltinOptions.FloorDivOptions: FloorDivOptions.FloorDivOptions, |
| 240 | BuiltinOptions.SquareOptions: SquareOptions.SquareOptions, |
| 241 | BuiltinOptions.ZerosLikeOptions: ZerosLikeOptions.ZerosLikeOptions, |
| 242 | BuiltinOptions.FillOptions: FillOptions.FillOptions, |
| 243 | BuiltinOptions.BidirectionalSequenceLSTMOptions: BidirectionalSequenceLSTMOptions.BidirectionalSequenceLSTMOptions, |
| 244 | BuiltinOptions.BidirectionalSequenceRNNOptions: BidirectionalSequenceRNNOptions.BidirectionalSequenceRNNOptions, |
| 245 | BuiltinOptions.UnidirectionalSequenceLSTMOptions: UnidirectionalSequenceLSTMOptions.UnidirectionalSequenceLSTMOptions, # noqa: E501 |
| 246 | BuiltinOptions.FloorModOptions: FloorModOptions.FloorModOptions, |
| 247 | BuiltinOptions.RangeOptions: RangeOptions.RangeOptions, |
| 248 | BuiltinOptions.ResizeNearestNeighborOptions: ResizeNearestNeighborOptions.ResizeNearestNeighborOptions, |
| 249 | BuiltinOptions.LeakyReluOptions: LeakyReluOptions.LeakyReluOptions, |
| 250 | BuiltinOptions.SquaredDifferenceOptions: SquaredDifferenceOptions.SquaredDifferenceOptions, |
| 251 | BuiltinOptions.MirrorPadOptions: MirrorPadOptions.MirrorPadOptions, |
| 252 | BuiltinOptions.AbsOptions: AbsOptions.AbsOptions, |
| 253 | BuiltinOptions.SplitVOptions: SplitVOptions.SplitVOptions, |
| 254 | BuiltinOptions.UniqueOptions: UniqueOptions.UniqueOptions, |
| 255 | BuiltinOptions.ReverseV2Options: ReverseV2Options.ReverseV2Options, |
| 256 | BuiltinOptions.AddNOptions: AddNOptions.AddNOptions, |
| 257 | BuiltinOptions.GatherNdOptions: GatherNdOptions.GatherNdOptions, |
| 258 | BuiltinOptions.CosOptions: CosOptions.CosOptions, |
| 259 | BuiltinOptions.WhereOptions: WhereOptions.WhereOptions, |
| 260 | BuiltinOptions.RankOptions: RankOptions.RankOptions, |
| 261 | BuiltinOptions.ReverseSequenceOptions: ReverseSequenceOptions.ReverseSequenceOptions, |
| 262 | BuiltinOptions.MatrixDiagOptions: MatrixDiagOptions.MatrixDiagOptions, |
| 263 | BuiltinOptions.QuantizeOptions: QuantizeOptions.QuantizeOptions, |
| 264 | BuiltinOptions.MatrixSetDiagOptions: MatrixSetDiagOptions.MatrixSetDiagOptions, |
| 265 | BuiltinOptions.DensifyOptions: DensifyOptions.DensifyOptions, |
| 266 | BuiltinOptions.DepthToSpaceOptions: DepthToSpaceOptions.DepthToSpaceOptions, |
| 267 | BuiltinOptions.IfOptions: IfOptions.IfOptions, |
| 268 | BuiltinOptions.NonMaxSuppressionV4Options: NonMaxSuppressionV4Options.NonMaxSuppressionV4Options, |
| 269 | BuiltinOptions.NonMaxSuppressionV5Options: NonMaxSuppressionV5Options.NonMaxSuppressionV5Options, |
| 270 | BuiltinOptions.ScatterNdOptions: ScatterNdOptions.ScatterNdOptions, |
| 271 | BuiltinOptions.SegmentSumOptions: SegmentSumOptions.SegmentSumOptions, |
| 272 | BuiltinOptions.SelectV2Options: SelectV2Options.SelectV2Options, |
| 273 | BuiltinOptions.WhileOptions: WhileOptions.WhileOptions, |
| 274 | } |
| 275 | |
| 276 | builtin_options_inv_map = inverse_map(builtin_options_map) |
| 277 | |
| 278 | |
| 279 | def underscore_to_camel_case(s): |
| 280 | return "".join(x.title() for x in s.split("_")) |
| 281 | |
| 282 | |
| 283 | def padding_deserialize(x): |
| 284 | return padding_map[x] |
| 285 | |
| 286 | |
| 287 | def padding_serialize(builder, x): |
| 288 | return padding_inv_map[x] |
| 289 | |
| 290 | |
| 291 | def activation_deserialize(x): |
| 292 | return activation_function_map[x] |
| 293 | |
| 294 | |
| 295 | def activation_serialize(builder, x): |
| 296 | return activation_function_inv_map[x] |
| 297 | |
| 298 | |
| 299 | def datatype_deserialize(x): |
| 300 | return datatype_map[x] |
| 301 | |
| 302 | |
| 303 | def datatype_serialize(builder, x): |
| 304 | return datatype_inv_map[x] |
| 305 | |
| 306 | |
| 307 | def identity(x): |
| 308 | return x |
| 309 | |
| 310 | |
| 311 | def identity_serialize(builder, x): |
| 312 | return x |
| 313 | |
| 314 | |
| 315 | def write_byte_vector(builder, v): |
| 316 | builder.StartVector(1, len(v), 1) |
| 317 | for e in v[::-1]: |
| 318 | builder.PrependByte(e) |
| 319 | return builder.EndVector(len(v)) |
| 320 | |
| 321 | |
| 322 | def write_int_vector(builder, v): |
| 323 | builder.StartVector(4, len(v), 4) |
| 324 | for e in v[::-1]: |
| 325 | builder.PrependInt32(e) |
| 326 | return builder.EndVector(len(v)) |
| 327 | |
| 328 | |
| 329 | class OptionsSerializer: |
| 330 | def __init__(self, name, members=[]): |
| 331 | self.name = name |
| 332 | self.module = globals()[self.name] |
| 333 | self.cls = getattr(self.module, self.name) |
| 334 | self.builtin_opt_type = builtin_options_inv_map[self.cls] |
| 335 | self.custom_opt_format = 0 |
| 336 | self.members = [] |
| 337 | for mem in members: |
| 338 | deserialize = identity |
| 339 | serialize = identity_serialize |
| 340 | is_vector = False |
| 341 | if isinstance(mem, tuple): |
| 342 | if len(mem) == 3: |
| 343 | mem, deserialize, serialize = mem |
| 344 | elif len(mem) == 2: |
| 345 | mem, is_vector = mem |
| 346 | deserialize = tuple |
| 347 | serialize = write_int_vector |
| 348 | else: |
| 349 | assert 0 |
| 350 | underscore_mem = mem |
| 351 | camelcase_mem = underscore_to_camel_case(mem) |
| 352 | self.members.append((underscore_mem, camelcase_mem, deserialize, serialize, is_vector)) |
| 353 | |
| 354 | def deserialize(self, builtin_data, custom_data): |
| 355 | attrs = {} |
| 356 | if builtin_data: |
| 357 | tfattrs = self.cls() |
| 358 | tfattrs.Init(builtin_data.Bytes, builtin_data.Pos) |
| 359 | for underscore_mem, camelcase_mem, deserialize, serialize, is_vector in self.members: |
| 360 | fun = camelcase_mem |
| 361 | if is_vector: |
| 362 | fun += "AsNumpy" |
| 363 | |
| 364 | a = deserialize(getattr(tfattrs, fun)()) |
| 365 | attrs[underscore_mem] = a |
| 366 | return attrs |
| 367 | |
| 368 | def serialize(self, builder, attrs): |
| 369 | ser_attrs = [] |
| 370 | for underscore_mem, camelcase_mem, deserialize, serialize, is_vector in self.members: |
| 371 | a = serialize(builder, attrs[underscore_mem]) |
| 372 | ser_attrs.append((camelcase_mem, a)) |
| 373 | |
| 374 | getattr(self.module, self.name + "Start")(builder) |
| 375 | |
| 376 | for camelcase_mem, a in ser_attrs: |
| 377 | getattr(self.module, self.name + "Add" + camelcase_mem)(builder, a) |
| 378 | |
| 379 | return getattr(self.module, self.name + "End")(builder), None |
| 380 | |
| 381 | |
| 382 | class CustomOptionsSerializer: |
| 383 | def __init__(self): |
| 384 | self.builtin_opt_type = 0 |
| 385 | self.custom_opt_format = 0 |
| 386 | |
| 387 | def deserialize(self, builtin_data, custom_data): |
| 388 | attrs = {} |
| 389 | attrs["custom_options"] = custom_data |
| 390 | return attrs |
| 391 | |
| 392 | def serialize(self, builder, attrs): |
| 393 | |
| 394 | custom_opts = attrs.get("custom_options", []) |
| 395 | custom_data = [] |
| 396 | |
| 397 | # Set NPU op custom options for the TensorFlow Lite custom operator |
| 398 | if custom_opts["type"] == "NpuOp": |
| 399 | custom_data = [0x01, 0x04, 0x01] # NpuOp=1, FlexbufferFormat.UINT8=4, byte length=1 |
| 400 | |
| 401 | custom_data_bytes = struct.pack("<{0}B".format(len(custom_data)), *custom_data) |
| 402 | custom_offset = write_byte_vector(builder, custom_data_bytes) |
| 403 | |
| 404 | return None, custom_offset |
| 405 | |
| 406 | |
| 407 | padding_map = { |
| 408 | Padding.SAME: b"SAME", |
| 409 | Padding.VALID: b"VALID", |
| 410 | } |
| 411 | |
| 412 | padding_inv_map = inverse_map(padding_map) |
| 413 | |
| 414 | |
| 415 | activation_function_map = { |
| 416 | ActivationFunctionType.NONE: None, |
| 417 | ActivationFunctionType.RELU: "Relu", |
| 418 | ActivationFunctionType.RELU_N1_TO_1: "ReluN1To1", |
| 419 | ActivationFunctionType.RELU6: "Relu6", |
| 420 | ActivationFunctionType.TANH: "Tanh", |
| 421 | ActivationFunctionType.SIGN_BIT: "SignBit", |
| 422 | } |
| 423 | |
| 424 | activation_function_inv_map = inverse_map(activation_function_map) |
| 425 | |
| 426 | fused_act = ("fused_activation_function", activation_deserialize, activation_serialize) |
| 427 | padding = ("padding", padding_deserialize, padding_serialize) |
| 428 | |
| 429 | pool2d_opts = OptionsSerializer( |
| 430 | "Pool2DOptions", (padding, "stride_w", "stride_h", "filter_width", "filter_height", fused_act,) |
| 431 | ) |
| 432 | |
| 433 | depthwise_opts = OptionsSerializer( |
| 434 | "DepthwiseConv2DOptions", |
| 435 | (padding, "stride_w", "stride_h", "depth_multiplier", fused_act, "dilation_w_factor", "dilation_h_factor",), |
| 436 | ) |
| 437 | |
| 438 | conv2d_opts = OptionsSerializer( |
| 439 | "Conv2DOptions", (padding, "stride_w", "stride_h", fused_act, "dilation_w_factor", "dilation_h_factor",) |
| 440 | ) |
| 441 | |
| 442 | lstm_opts = OptionsSerializer("LSTMOptions", (fused_act, "cell_clip", "proj_clip", "kernel_type")) |
| 443 | |
| 444 | unidir_seq_lstm_opts = OptionsSerializer( |
| 445 | "UnidirectionalSequenceLSTMOptions", (fused_act, "cell_clip", "proj_clip", "time_major") |
| 446 | ) |
| 447 | |
| 448 | bidir_seq_lstm_opts = OptionsSerializer( |
| 449 | "BidirectionalSequenceLSTMOptions", (fused_act, "cell_clip", "proj_clip", "merge_outputs", "time_major") |
| 450 | ) |
| 451 | |
| 452 | rnn_opts = OptionsSerializer("RNNOptions", (fused_act,)) |
| 453 | |
| 454 | seq_rnn_opts = OptionsSerializer("SequenceRNNOptions", ("time_major", fused_act,)) |
| 455 | |
| 456 | bidir_seq_rnn_opts = OptionsSerializer("BidirectionalSequenceRNNOptions", ("time_major", fused_act, "merge_outputs",)) |
| 457 | |
| 458 | |
| 459 | reducer_opts = OptionsSerializer("ReducerOptions", ("keep_dims",)) |
| 460 | |
| 461 | is_int_vec = True |
| 462 | |
| 463 | custom_prefix = "Custom_" |
| 464 | |
| 465 | builtin_operator_map = { |
| 466 | BuiltinOperator.ADD: ("AddAct", OptionsSerializer("AddOptions", (fused_act,))), |
| 467 | BuiltinOperator.AVERAGE_POOL_2D: ("AvgPoolAct", pool2d_opts), |
| 468 | BuiltinOperator.CONCATENATION: ("ConcatTFLite", OptionsSerializer("ConcatenationOptions", ("axis", fused_act))), |
| 469 | BuiltinOperator.CONV_2D: ("Conv2DBiasAct", conv2d_opts), |
| 470 | BuiltinOperator.DEPTHWISE_CONV_2D: ("DepthwiseConv2dBiasAct", depthwise_opts), |
| 471 | BuiltinOperator.DEPTH_TO_SPACE: ("DepthToSpace", OptionsSerializer("DepthToSpaceOptions", ("block_size",))), |
| 472 | BuiltinOperator.DEQUANTIZE: ("Dequantize", OptionsSerializer("DequantizeOptions")), |
| 473 | BuiltinOperator.EMBEDDING_LOOKUP: (None, None), |
| 474 | BuiltinOperator.FLOOR: ("Floor", None), |
| 475 | BuiltinOperator.FULLY_CONNECTED: ( |
| 476 | "FullyConnectedAct", |
| 477 | OptionsSerializer("FullyConnectedOptions", (fused_act, "weights_format")), |
| 478 | ), |
| 479 | BuiltinOperator.HASHTABLE_LOOKUP: (None, None), |
| 480 | # BuiltinOperator.L2_NORMALIZATION : "L2NormAct", |
| 481 | BuiltinOperator.L2_POOL_2D: (None, pool2d_opts), |
| 482 | BuiltinOperator.LOCAL_RESPONSE_NORMALIZATION: ( |
| 483 | "LRN", |
| 484 | OptionsSerializer("LocalResponseNormalizationOptions", ("radius", "bias", "alpha", "beta")), |
| 485 | ), |
| 486 | BuiltinOperator.LOGISTIC: ("Sigmoid", None), |
| 487 | # BuiltinOperator.LSH_PROJECTION : "", |
| 488 | BuiltinOperator.LSTM: ("LstmAct", lstm_opts), |
| 489 | BuiltinOperator.MAX_POOL_2D: ("MaxPool", pool2d_opts), |
| 490 | BuiltinOperator.MUL: ("MulAct", OptionsSerializer("MulOptions", (fused_act,))), |
| 491 | BuiltinOperator.RELU: ("Relu", None), |
| 492 | BuiltinOperator.RELU_N1_TO_1: (None, None), |
| 493 | BuiltinOperator.RELU6: ("Relu6", None), |
| 494 | BuiltinOperator.RESHAPE: ("Reshape", OptionsSerializer("ReshapeOptions", (("new_shape", is_int_vec),))), |
| 495 | BuiltinOperator.RESIZE_BILINEAR: ( |
| 496 | "ResizeBilinear", |
| 497 | OptionsSerializer("ResizeBilinearOptions", ("align_corners", "half_pixel_centers")), |
| 498 | ), |
| 499 | BuiltinOperator.RNN: ("RnnAct", rnn_opts), |
| 500 | BuiltinOperator.SOFTMAX: ("Softmax", OptionsSerializer("SoftmaxOptions", ("beta",))), |
| 501 | BuiltinOperator.SPACE_TO_DEPTH: ("SpaceToDepth", OptionsSerializer("SpaceToDepthOptions", ("block_size",))), |
| 502 | BuiltinOperator.SVDF: ("SvdfAct", OptionsSerializer("SVDFOptions", ("rank", fused_act))), |
| 503 | BuiltinOperator.TANH: ("Tanh", None), |
| 504 | # BuiltinOperator.CONCAT_EMBEDDINGS : "", |
| 505 | # BuiltinOperator.SKIP_GRAM : "", |
| 506 | # BuiltinOperator.CALL : "", |
| 507 | BuiltinOperator.EMBEDDING_LOOKUP_SPARSE: (None, OptionsSerializer("EmbeddingLookupSparseOptions", ("combiner",))), |
| 508 | BuiltinOperator.PAD: ("Pad", OptionsSerializer("PadOptions")), |
| 509 | BuiltinOperator.UNIDIRECTIONAL_SEQUENCE_RNN: ("UnidirectionalSequenceRnnAct", seq_rnn_opts), |
| 510 | BuiltinOperator.GATHER: ("GatherV2", OptionsSerializer("GatherOptions", ("axis",))), |
| 511 | BuiltinOperator.BATCH_TO_SPACE_ND: ("BatchToSpaceND", OptionsSerializer("BatchToSpaceNDOptions")), |
| 512 | BuiltinOperator.SPACE_TO_BATCH_ND: ("SpaceToBatchND", OptionsSerializer("SpaceToBatchNDOptions")), |
| 513 | BuiltinOperator.TRANSPOSE: ("Transpose", OptionsSerializer("TransposeOptions")), |
| 514 | BuiltinOperator.MEAN: ("Mean", None), |
| 515 | BuiltinOperator.SUB: ("SubAct", OptionsSerializer("SubOptions", (fused_act,))), |
| 516 | BuiltinOperator.DIV: ("DivAct", OptionsSerializer("DivOptions", (fused_act,))), |
| 517 | BuiltinOperator.SQUEEZE: ("Squeeze", OptionsSerializer("SqueezeOptions", (("squeeze_dims", is_int_vec),))), |
| 518 | BuiltinOperator.UNIDIRECTIONAL_SEQUENCE_LSTM: ("UnidirectionalSequenceLstmAct", unidir_seq_lstm_opts), |
| 519 | BuiltinOperator.STRIDED_SLICE: ( |
| 520 | "StridedSlice", |
| 521 | OptionsSerializer( |
| 522 | "StridedSliceOptions", ("begin_mask", "end_mask", "ellipsis_mask", "new_axis_mask", "shrink_axis_mask") |
| 523 | ), |
| 524 | ), |
| 525 | BuiltinOperator.BIDIRECTIONAL_SEQUENCE_RNN: ("BidirectionalSequenceRnnAct", bidir_seq_rnn_opts), |
| 526 | BuiltinOperator.EXP: ("Exp", OptionsSerializer("ExpOptions")), |
| 527 | BuiltinOperator.TOPK_V2: ("TopKV2", OptionsSerializer("TopKV2Options")), |
| 528 | BuiltinOperator.SPLIT: ("Split", OptionsSerializer("SplitOptions", ("num_splits",))), |
| 529 | BuiltinOperator.LOG_SOFTMAX: ("LogSoftmax", OptionsSerializer("LogSoftmaxOptions")), |
| 530 | # BuiltinOperator.DELEGATE : "", |
| 531 | BuiltinOperator.BIDIRECTIONAL_SEQUENCE_LSTM: ("BidirectionalSequenceLstmAct", bidir_seq_lstm_opts), |
| 532 | BuiltinOperator.CAST: ( |
| 533 | "Cast", |
| 534 | OptionsSerializer( |
| 535 | "CastOptions", |
| 536 | ( |
| 537 | ("in_data_type", datatype_deserialize, datatype_serialize), |
| 538 | ("out_data_type", datatype_deserialize, datatype_serialize), |
| 539 | ), |
| 540 | ), |
| 541 | ), |
| 542 | # BuiltinOperator.PRELU : "", |
| 543 | BuiltinOperator.MAXIMUM: ("Maximum", OptionsSerializer("MaximumMinimumOptions")), |
| 544 | BuiltinOperator.ARG_MAX: ( |
| 545 | "ArgMax", |
| 546 | OptionsSerializer("ArgMaxOptions", (("output_type", datatype_deserialize, datatype_serialize),)), |
| 547 | ), |
| 548 | BuiltinOperator.MINIMUM: ("Minimum", OptionsSerializer("MaximumMinimumOptions")), |
| 549 | BuiltinOperator.LESS: ("Less", None), |
| 550 | BuiltinOperator.NEG: ("Neg", None), |
| 551 | BuiltinOperator.PADV2: ("PadV2", None), |
| 552 | BuiltinOperator.GREATER: ("Greater", None), |
| 553 | BuiltinOperator.GREATER_EQUAL: ("GreaterEqual", None), |
| 554 | BuiltinOperator.LESS_EQUAL: ("LessEqual", None), |
| 555 | BuiltinOperator.SELECT: ("Select", None), |
| 556 | BuiltinOperator.SLICE: ("Slice", None), |
| 557 | BuiltinOperator.SIN: ("Sin", None), |
| 558 | BuiltinOperator.TRANSPOSE_CONV: ( |
| 559 | "Conv2DBackpropInput", |
| 560 | OptionsSerializer("TransposeConvOptions", (padding, "stride_w", "stride_h")), |
| 561 | ), |
| 562 | BuiltinOperator.SPARSE_TO_DENSE: ( |
| 563 | "SparseToDense", |
| 564 | OptionsSerializer("SparseToDenseOptions", ("validate_indices",)), |
| 565 | ), |
| 566 | BuiltinOperator.TILE: ("Tile", OptionsSerializer("TileOptions")), |
| 567 | BuiltinOperator.EXPAND_DIMS: ("ExpandDims", None), |
| 568 | BuiltinOperator.EQUAL: ("Equal", None), |
| 569 | BuiltinOperator.NOT_EQUAL: ("NotEqual", None), |
| 570 | BuiltinOperator.LOG: ("Log", None), |
| 571 | BuiltinOperator.SUM: ("Sum", None), |
| 572 | BuiltinOperator.SQRT: ("Sqrt", None), |
| 573 | BuiltinOperator.RSQRT: ("Rsqrt", None), |
| 574 | BuiltinOperator.SHAPE: ( |
| 575 | "Shape", |
| 576 | OptionsSerializer("ShapeOptions", (("out_type", datatype_deserialize, datatype_serialize),)), |
| 577 | ), |
| 578 | BuiltinOperator.POW: "Pow", |
| 579 | BuiltinOperator.ARG_MIN: ( |
| 580 | "ArgMin", |
| 581 | OptionsSerializer("ArgMinOptions", (("output_type", datatype_deserialize, datatype_serialize),)), |
| 582 | ), |
| 583 | BuiltinOperator.FAKE_QUANT: ( |
| 584 | "FakeQuantWithMinMaxArgs", |
| 585 | OptionsSerializer("FakeQuantOptions", ("min", "max", "num_bits", "narrow_range")), |
| 586 | ), |
| 587 | BuiltinOperator.REDUCE_PROD: ("Prod", reducer_opts), |
| 588 | BuiltinOperator.REDUCE_MAX: ("Max", reducer_opts), |
| 589 | BuiltinOperator.PACK: ("Pack", OptionsSerializer("PackOptions", ("values_count", "axis"))), |
| 590 | BuiltinOperator.LOGICAL_OR: ("LogicalOr", None), |
| 591 | BuiltinOperator.ONE_HOT: ("OneHot", OptionsSerializer("OneHotOptions", ("axis",))), |
| 592 | BuiltinOperator.LOGICAL_AND: ("LogicalAnd", None), |
| 593 | BuiltinOperator.LOGICAL_NOT: ("LogicalNot", None), |
| 594 | BuiltinOperator.UNPACK: ("Unpack", OptionsSerializer("UnpackOptions", ("num", "axis"))), |
| 595 | BuiltinOperator.REDUCE_MIN: ("Min", reducer_opts), |
| 596 | BuiltinOperator.FLOOR_DIV: ("FloorDiv", None), |
| 597 | BuiltinOperator.REDUCE_ANY: ("Any", reducer_opts), |
| 598 | BuiltinOperator.SQUARE: ("Square", None), |
| 599 | BuiltinOperator.ZEROS_LIKE: ("ZerosLike", None), |
| 600 | BuiltinOperator.FILL: ("Fill", None), |
| 601 | BuiltinOperator.FLOOR_MOD: ("FloorMod", None), |
| 602 | BuiltinOperator.RANGE: ("Range", None), |
| 603 | BuiltinOperator.RESIZE_NEAREST_NEIGHBOR: ( |
| 604 | "ResizeNearestNeighbor", |
| 605 | OptionsSerializer("ResizeNearestNeighborOptions", ("align_corners",)), |
| 606 | ), |
| 607 | BuiltinOperator.LEAKY_RELU: ("LeakyRelu", OptionsSerializer("LeakyReluOptions", ("alpha",))), |
| 608 | BuiltinOperator.SQUARED_DIFFERENCE: ("SquaredDifference", None), |
| 609 | BuiltinOperator.MIRROR_PAD: ("MirrorPad", OptionsSerializer("MirrorPadOptions", ("mode",))), |
| 610 | BuiltinOperator.ABS: ("Abs", None), |
| 611 | BuiltinOperator.SPLIT_V: ("SplitV", OptionsSerializer("SplitVOptions", ("num_splits",))), |
| 612 | BuiltinOperator.UNIQUE: ( |
| 613 | "Unique", |
| 614 | OptionsSerializer("UniqueOptions", (("idx_out_type", datatype_deserialize, datatype_serialize),)), |
| 615 | ), |
| 616 | BuiltinOperator.CEIL: ("Ceil", None), |
| 617 | BuiltinOperator.REVERSE_V2: ("ReverseV2", None), |
| 618 | BuiltinOperator.ADD_N: ("AddN", None), |
| 619 | BuiltinOperator.GATHER_ND: ("GatherNd", None), |
| 620 | BuiltinOperator.COS: ("Cos", None), |
| 621 | BuiltinOperator.WHERE: ("Where", None), |
| 622 | BuiltinOperator.RANK: ("Rank", None), |
| 623 | BuiltinOperator.ELU: ("Elu", None), |
| 624 | BuiltinOperator.REVERSE_SEQUENCE: ( |
| 625 | "ReverseSequence", |
| 626 | OptionsSerializer("ReverseSequenceOptions", ("seq_dim", "batch_dim")), |
| 627 | ), |
| 628 | BuiltinOperator.MATRIX_DIAG: ("MatrixDiag", None), |
| 629 | BuiltinOperator.QUANTIZE: ("Quantize", None), |
| 630 | BuiltinOperator.MATRIX_SET_DIAG: ("MatrixSetDiag", None), |
| 631 | BuiltinOperator.IF: ("If", OptionsSerializer("IfOptions", ("then_subgraph_index", "else_subgraph_index"))), |
| 632 | BuiltinOperator.WHILE: ("While", OptionsSerializer("WhileOptions", ("cond_subgraph_index", "body_subgraph_index"))), |
| 633 | BuiltinOperator.NON_MAX_SUPPRESSION_V4: ("NonMaxSuppressionV4", OptionsSerializer("NonMaxSuppressionV4Options")), |
| 634 | BuiltinOperator.NON_MAX_SUPPRESSION_V5: ("NonMaxSuppressionV5", OptionsSerializer("NonMaxSuppressionV5Options")), |
| 635 | BuiltinOperator.SCATTER_ND: ("ScatterNd", OptionsSerializer("ScatterNdOptions")), |
| 636 | BuiltinOperator.SELECT_V2: ("SelectV2", OptionsSerializer("SelectV2Options")), |
| 637 | BuiltinOperator.DENSIFY: ("Densify", OptionsSerializer("DensifyOptions")), |
| 638 | BuiltinOperator.SEGMENT_SUM: ("SegmentSum", OptionsSerializer("SegmentSumOptions")), |
| 639 | BuiltinOperator.CUSTOM: (custom_prefix, CustomOptionsSerializer()), |
| 640 | } |
| 641 | |
| 642 | builtin_operator_inv_map = {v[0]: (k, v[1]) for k, v in builtin_operator_map.items()} |
| 643 | |
| 644 | builtin_operator_inv_map["NpuOp"] = (BuiltinOperator.CUSTOM, CustomOptionsSerializer()) |