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