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