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