erik.andersson@arm.com | 460c689 | 2021-02-24 14:38:09 +0100 | [diff] [blame] | 1 | # Copyright (C) 2020-2021 Arm Limited or its affiliates. All rights reserved. |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 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 |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 28 | from .operation import TensorIndices |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 29 | from .tflite import AbsOptions |
| 30 | from .tflite import AddNOptions |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 31 | from .tflite import AddOptions |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 32 | from .tflite import ArgMaxOptions |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 33 | from .tflite import ArgMinOptions |
Rickard Bolin | 2de898a | 2021-12-20 08:35:23 +0000 | [diff] [blame] | 34 | from .tflite import AssignVariableOptions |
Jacob Bohlin | 8daf6b7 | 2020-09-15 16:28:35 +0200 | [diff] [blame] | 35 | from .tflite import BatchMatMulOptions |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 36 | from .tflite import BatchToSpaceNDOptions |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 37 | from .tflite import BidirectionalSequenceLSTMOptions |
| 38 | from .tflite import BidirectionalSequenceRNNOptions |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 39 | from .tflite import BroadcastToOptions |
| 40 | from .tflite import CallOnceOptions |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 41 | from .tflite import CallOptions |
| 42 | from .tflite import CastOptions |
| 43 | from .tflite import ConcatEmbeddingsOptions |
| 44 | from .tflite import ConcatenationOptions |
| 45 | from .tflite import Conv2DOptions |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 46 | from .tflite import Conv3DOptions |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 47 | from .tflite import CosOptions |
Tim Hall | 42abec1 | 2021-02-04 21:31:57 +0000 | [diff] [blame] | 48 | from .tflite import CumsumOptions |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 49 | from .tflite import DensifyOptions |
| 50 | from .tflite import DepthToSpaceOptions |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 51 | from .tflite import DepthwiseConv2DOptions |
| 52 | from .tflite import DequantizeOptions |
| 53 | from .tflite import DivOptions |
| 54 | from .tflite import EmbeddingLookupSparseOptions |
| 55 | from .tflite import EqualOptions |
| 56 | from .tflite import ExpandDimsOptions |
| 57 | from .tflite import ExpOptions |
| 58 | from .tflite import FakeQuantOptions |
| 59 | from .tflite import FillOptions |
| 60 | from .tflite import FloorDivOptions |
| 61 | from .tflite import FloorModOptions |
| 62 | from .tflite import FullyConnectedOptions |
| 63 | from .tflite import GatherNdOptions |
| 64 | from .tflite import GatherOptions |
| 65 | from .tflite import GreaterEqualOptions |
| 66 | from .tflite import GreaterOptions |
Louis Verhaard | 678645b | 2020-06-15 15:22:47 +0200 | [diff] [blame] | 67 | from .tflite import HardSwishOptions |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 68 | from .tflite import HashtableFindOptions |
| 69 | from .tflite import HashtableImportOptions |
| 70 | from .tflite import HashtableOptions |
| 71 | from .tflite import HashtableSizeOptions |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 72 | from .tflite import IfOptions |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 73 | from .tflite import L2NormOptions |
| 74 | from .tflite import LeakyReluOptions |
| 75 | from .tflite import LessEqualOptions |
| 76 | from .tflite import LessOptions |
| 77 | from .tflite import LocalResponseNormalizationOptions |
| 78 | from .tflite import LogicalAndOptions |
| 79 | from .tflite import LogicalNotOptions |
| 80 | from .tflite import LogicalOrOptions |
| 81 | from .tflite import LogSoftmaxOptions |
| 82 | from .tflite import LSHProjectionOptions |
| 83 | from .tflite import LSTMOptions |
| 84 | from .tflite import MatrixDiagOptions |
| 85 | from .tflite import MatrixSetDiagOptions |
| 86 | from .tflite import MaximumMinimumOptions |
| 87 | from .tflite import MirrorPadOptions |
| 88 | from .tflite import MulOptions |
| 89 | from .tflite import NegOptions |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 90 | from .tflite import NonMaxSuppressionV4Options |
| 91 | from .tflite import NonMaxSuppressionV5Options |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 92 | from .tflite import NotEqualOptions |
| 93 | from .tflite import OneHotOptions |
| 94 | from .tflite import PackOptions |
| 95 | from .tflite import PadOptions |
| 96 | from .tflite import PadV2Options |
| 97 | from .tflite import Pool2DOptions |
| 98 | from .tflite import PowOptions |
| 99 | from .tflite import QuantizeOptions |
Rickard Bolin | 2de898a | 2021-12-20 08:35:23 +0000 | [diff] [blame] | 100 | from .tflite import RandomOptions |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 101 | from .tflite import RangeOptions |
| 102 | from .tflite import RankOptions |
Rickard Bolin | 2de898a | 2021-12-20 08:35:23 +0000 | [diff] [blame] | 103 | from .tflite import ReadVariableOptions |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 104 | from .tflite import ReducerOptions |
| 105 | from .tflite import ReshapeOptions |
| 106 | from .tflite import ResizeBilinearOptions |
| 107 | from .tflite import ResizeNearestNeighborOptions |
| 108 | from .tflite import ReverseSequenceOptions |
| 109 | from .tflite import ReverseV2Options |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 110 | from .tflite import Rfft2dOptions |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 111 | from .tflite import RNNOptions |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 112 | from .tflite import ScatterNdOptions |
| 113 | from .tflite import SegmentSumOptions |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 114 | from .tflite import SelectOptions |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 115 | from .tflite import SelectV2Options |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 116 | from .tflite import SequenceRNNOptions |
| 117 | from .tflite import ShapeOptions |
| 118 | from .tflite import SkipGramOptions |
| 119 | from .tflite import SliceOptions |
| 120 | from .tflite import SoftmaxOptions |
| 121 | from .tflite import SpaceToBatchNDOptions |
| 122 | from .tflite import SpaceToDepthOptions |
| 123 | from .tflite import SparseToDenseOptions |
| 124 | from .tflite import SplitOptions |
| 125 | from .tflite import SplitVOptions |
| 126 | from .tflite import SquaredDifferenceOptions |
| 127 | from .tflite import SquareOptions |
| 128 | from .tflite import SqueezeOptions |
| 129 | from .tflite import StridedSliceOptions |
| 130 | from .tflite import SubOptions |
| 131 | from .tflite import SVDFOptions |
| 132 | from .tflite import TileOptions |
| 133 | from .tflite import TopKV2Options |
| 134 | from .tflite import TransposeConvOptions |
| 135 | from .tflite import TransposeOptions |
| 136 | from .tflite import UnidirectionalSequenceLSTMOptions |
| 137 | from .tflite import UniqueOptions |
| 138 | from .tflite import UnpackOptions |
Rickard Bolin | 2de898a | 2021-12-20 08:35:23 +0000 | [diff] [blame] | 139 | from .tflite import VarHandleOptions |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 140 | from .tflite import WhereOptions |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 141 | from .tflite import WhileOptions |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 142 | from .tflite import ZerosLikeOptions |
| 143 | from .tflite.ActivationFunctionType import ActivationFunctionType |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 144 | from .tflite.BuiltinOperator import BuiltinOperator |
| 145 | from .tflite.BuiltinOptions import BuiltinOptions |
| 146 | from .tflite.Padding import Padding |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 147 | from .tflite.TensorType import TensorType |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 148 | |
| 149 | |
| 150 | def inverse_map(map): |
| 151 | return {v: k for k, v in map.items()} |
| 152 | |
| 153 | |
| 154 | datatype_map = { |
| 155 | TensorType.UINT8: DataType.uint8, |
| 156 | TensorType.INT8: DataType.int8, |
| 157 | TensorType.INT16: DataType.int16, |
| 158 | TensorType.INT32: DataType.int32, |
| 159 | TensorType.INT64: DataType.int64, |
| 160 | TensorType.FLOAT16: DataType.float16, |
| 161 | TensorType.FLOAT32: DataType.float32, |
Jacob Bohlin | 8daf6b7 | 2020-09-15 16:28:35 +0200 | [diff] [blame] | 162 | TensorType.FLOAT64: DataType.float64, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 163 | TensorType.STRING: DataType.string, |
| 164 | TensorType.BOOL: DataType.bool, |
Jacob Bohlin | f767b93 | 2020-08-13 15:32:45 +0200 | [diff] [blame] | 165 | TensorType.COMPLEX64: DataType.complex64, |
Jacob Bohlin | 8daf6b7 | 2020-09-15 16:28:35 +0200 | [diff] [blame] | 166 | TensorType.COMPLEX128: DataType.complex128, |
Dwight Lidman | f53b332 | 2021-10-08 11:58:08 +0200 | [diff] [blame] | 167 | TensorType.UINT64: DataType.uint64, |
| 168 | TensorType.RESOURCE: DataType.resource, |
| 169 | TensorType.VARIANT: DataType.variant, |
| 170 | TensorType.UINT32: DataType.uint32, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | datatype_inv_map = inverse_map(datatype_map) |
| 174 | datatype_inv_map[DataType.quint8] = TensorType.UINT8 |
| 175 | |
| 176 | datatype_inv_map[DataType.qint8] = TensorType.INT8 |
| 177 | datatype_inv_map[DataType.qint16] = TensorType.INT16 |
| 178 | datatype_inv_map[DataType.qint32] = TensorType.INT32 |
| 179 | |
| 180 | |
| 181 | datatype_map_numpy = { |
| 182 | TensorType.UINT8: np.uint8, |
Dwight Lidman | f53b332 | 2021-10-08 11:58:08 +0200 | [diff] [blame] | 183 | TensorType.UINT32: np.uint32, |
| 184 | TensorType.UINT64: np.uint64, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 185 | TensorType.INT8: np.int8, |
| 186 | TensorType.INT16: np.int16, |
| 187 | TensorType.INT32: np.int32, |
| 188 | TensorType.INT64: np.int64, |
| 189 | TensorType.FLOAT16: np.float16, |
| 190 | TensorType.FLOAT32: np.float32, |
Jacob Bohlin | 8daf6b7 | 2020-09-15 16:28:35 +0200 | [diff] [blame] | 191 | TensorType.FLOAT64: np.float64, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 192 | TensorType.BOOL: np.bool, |
Jacob Bohlin | f767b93 | 2020-08-13 15:32:45 +0200 | [diff] [blame] | 193 | TensorType.COMPLEX64: np.complex64, |
Jacob Bohlin | 8daf6b7 | 2020-09-15 16:28:35 +0200 | [diff] [blame] | 194 | TensorType.COMPLEX128: np.complex128, |
Louis Verhaard | f4e12be | 2020-12-18 14:23:06 +0100 | [diff] [blame] | 195 | TensorType.STRING: np.uint8, |
Dwight Lidman | f53b332 | 2021-10-08 11:58:08 +0200 | [diff] [blame] | 196 | TensorType.RESOURCE: None, # experimental type |
| 197 | TensorType.VARIANT: None, # experimental type |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | |
| 201 | builtin_options_map = { |
| 202 | BuiltinOptions.Conv2DOptions: Conv2DOptions.Conv2DOptions, |
| 203 | BuiltinOptions.DepthwiseConv2DOptions: DepthwiseConv2DOptions.DepthwiseConv2DOptions, |
| 204 | BuiltinOptions.ConcatEmbeddingsOptions: ConcatEmbeddingsOptions.ConcatEmbeddingsOptions, |
| 205 | BuiltinOptions.LSHProjectionOptions: LSHProjectionOptions.LSHProjectionOptions, |
| 206 | BuiltinOptions.Pool2DOptions: Pool2DOptions.Pool2DOptions, |
| 207 | BuiltinOptions.SVDFOptions: SVDFOptions.SVDFOptions, |
| 208 | BuiltinOptions.RNNOptions: RNNOptions.RNNOptions, |
| 209 | BuiltinOptions.FullyConnectedOptions: FullyConnectedOptions.FullyConnectedOptions, |
| 210 | BuiltinOptions.SoftmaxOptions: SoftmaxOptions.SoftmaxOptions, |
| 211 | BuiltinOptions.ConcatenationOptions: ConcatenationOptions.ConcatenationOptions, |
| 212 | BuiltinOptions.AddOptions: AddOptions.AddOptions, |
| 213 | BuiltinOptions.L2NormOptions: L2NormOptions.L2NormOptions, |
| 214 | BuiltinOptions.LocalResponseNormalizationOptions: LocalResponseNormalizationOptions.LocalResponseNormalizationOptions, # noqa: E501 |
| 215 | BuiltinOptions.LSTMOptions: LSTMOptions.LSTMOptions, |
| 216 | BuiltinOptions.ResizeBilinearOptions: ResizeBilinearOptions.ResizeBilinearOptions, |
| 217 | BuiltinOptions.CallOptions: CallOptions.CallOptions, |
| 218 | BuiltinOptions.ReshapeOptions: ReshapeOptions.ReshapeOptions, |
| 219 | BuiltinOptions.SkipGramOptions: SkipGramOptions.SkipGramOptions, |
| 220 | BuiltinOptions.SpaceToDepthOptions: SpaceToDepthOptions.SpaceToDepthOptions, |
| 221 | BuiltinOptions.EmbeddingLookupSparseOptions: EmbeddingLookupSparseOptions.EmbeddingLookupSparseOptions, |
| 222 | BuiltinOptions.MulOptions: MulOptions.MulOptions, |
| 223 | BuiltinOptions.PadOptions: PadOptions.PadOptions, |
| 224 | BuiltinOptions.GatherOptions: GatherOptions.GatherOptions, |
| 225 | BuiltinOptions.BatchToSpaceNDOptions: BatchToSpaceNDOptions.BatchToSpaceNDOptions, |
| 226 | BuiltinOptions.SpaceToBatchNDOptions: SpaceToBatchNDOptions.SpaceToBatchNDOptions, |
| 227 | BuiltinOptions.TransposeOptions: TransposeOptions.TransposeOptions, |
| 228 | BuiltinOptions.ReducerOptions: ReducerOptions.ReducerOptions, |
| 229 | BuiltinOptions.SubOptions: SubOptions.SubOptions, |
| 230 | BuiltinOptions.DivOptions: DivOptions.DivOptions, |
| 231 | BuiltinOptions.SqueezeOptions: SqueezeOptions.SqueezeOptions, |
| 232 | BuiltinOptions.SequenceRNNOptions: SequenceRNNOptions.SequenceRNNOptions, |
| 233 | BuiltinOptions.StridedSliceOptions: StridedSliceOptions.StridedSliceOptions, |
| 234 | BuiltinOptions.ExpOptions: ExpOptions.ExpOptions, |
| 235 | BuiltinOptions.TopKV2Options: TopKV2Options.TopKV2Options, |
| 236 | BuiltinOptions.SplitOptions: SplitOptions.SplitOptions, |
| 237 | BuiltinOptions.LogSoftmaxOptions: LogSoftmaxOptions.LogSoftmaxOptions, |
| 238 | BuiltinOptions.CastOptions: CastOptions.CastOptions, |
| 239 | BuiltinOptions.DequantizeOptions: DequantizeOptions.DequantizeOptions, |
| 240 | BuiltinOptions.MaximumMinimumOptions: MaximumMinimumOptions.MaximumMinimumOptions, |
| 241 | BuiltinOptions.ArgMaxOptions: ArgMaxOptions.ArgMaxOptions, |
| 242 | BuiltinOptions.LessOptions: LessOptions.LessOptions, |
| 243 | BuiltinOptions.NegOptions: NegOptions.NegOptions, |
| 244 | BuiltinOptions.PadV2Options: PadV2Options.PadV2Options, |
| 245 | BuiltinOptions.GreaterOptions: GreaterOptions.GreaterOptions, |
| 246 | BuiltinOptions.GreaterEqualOptions: GreaterEqualOptions.GreaterEqualOptions, |
| 247 | BuiltinOptions.LessEqualOptions: LessEqualOptions.LessEqualOptions, |
| 248 | BuiltinOptions.SelectOptions: SelectOptions.SelectOptions, |
| 249 | BuiltinOptions.SliceOptions: SliceOptions.SliceOptions, |
| 250 | BuiltinOptions.TransposeConvOptions: TransposeConvOptions.TransposeConvOptions, |
| 251 | BuiltinOptions.SparseToDenseOptions: SparseToDenseOptions.SparseToDenseOptions, |
| 252 | BuiltinOptions.TileOptions: TileOptions.TileOptions, |
| 253 | BuiltinOptions.ExpandDimsOptions: ExpandDimsOptions.ExpandDimsOptions, |
| 254 | BuiltinOptions.EqualOptions: EqualOptions.EqualOptions, |
| 255 | BuiltinOptions.NotEqualOptions: NotEqualOptions.NotEqualOptions, |
| 256 | BuiltinOptions.ShapeOptions: ShapeOptions.ShapeOptions, |
| 257 | BuiltinOptions.PowOptions: PowOptions.PowOptions, |
| 258 | BuiltinOptions.ArgMinOptions: ArgMinOptions.ArgMinOptions, |
| 259 | BuiltinOptions.FakeQuantOptions: FakeQuantOptions.FakeQuantOptions, |
| 260 | BuiltinOptions.PackOptions: PackOptions.PackOptions, |
| 261 | BuiltinOptions.LogicalOrOptions: LogicalOrOptions.LogicalOrOptions, |
| 262 | BuiltinOptions.OneHotOptions: OneHotOptions.OneHotOptions, |
| 263 | BuiltinOptions.LogicalAndOptions: LogicalAndOptions.LogicalAndOptions, |
| 264 | BuiltinOptions.LogicalNotOptions: LogicalNotOptions.LogicalNotOptions, |
| 265 | BuiltinOptions.UnpackOptions: UnpackOptions.UnpackOptions, |
| 266 | BuiltinOptions.FloorDivOptions: FloorDivOptions.FloorDivOptions, |
| 267 | BuiltinOptions.SquareOptions: SquareOptions.SquareOptions, |
| 268 | BuiltinOptions.ZerosLikeOptions: ZerosLikeOptions.ZerosLikeOptions, |
| 269 | BuiltinOptions.FillOptions: FillOptions.FillOptions, |
| 270 | BuiltinOptions.BidirectionalSequenceLSTMOptions: BidirectionalSequenceLSTMOptions.BidirectionalSequenceLSTMOptions, |
| 271 | BuiltinOptions.BidirectionalSequenceRNNOptions: BidirectionalSequenceRNNOptions.BidirectionalSequenceRNNOptions, |
| 272 | BuiltinOptions.UnidirectionalSequenceLSTMOptions: UnidirectionalSequenceLSTMOptions.UnidirectionalSequenceLSTMOptions, # noqa: E501 |
| 273 | BuiltinOptions.FloorModOptions: FloorModOptions.FloorModOptions, |
| 274 | BuiltinOptions.RangeOptions: RangeOptions.RangeOptions, |
| 275 | BuiltinOptions.ResizeNearestNeighborOptions: ResizeNearestNeighborOptions.ResizeNearestNeighborOptions, |
| 276 | BuiltinOptions.LeakyReluOptions: LeakyReluOptions.LeakyReluOptions, |
| 277 | BuiltinOptions.SquaredDifferenceOptions: SquaredDifferenceOptions.SquaredDifferenceOptions, |
| 278 | BuiltinOptions.MirrorPadOptions: MirrorPadOptions.MirrorPadOptions, |
| 279 | BuiltinOptions.AbsOptions: AbsOptions.AbsOptions, |
| 280 | BuiltinOptions.SplitVOptions: SplitVOptions.SplitVOptions, |
| 281 | BuiltinOptions.UniqueOptions: UniqueOptions.UniqueOptions, |
| 282 | BuiltinOptions.ReverseV2Options: ReverseV2Options.ReverseV2Options, |
| 283 | BuiltinOptions.AddNOptions: AddNOptions.AddNOptions, |
| 284 | BuiltinOptions.GatherNdOptions: GatherNdOptions.GatherNdOptions, |
| 285 | BuiltinOptions.CosOptions: CosOptions.CosOptions, |
| 286 | BuiltinOptions.WhereOptions: WhereOptions.WhereOptions, |
| 287 | BuiltinOptions.RankOptions: RankOptions.RankOptions, |
| 288 | BuiltinOptions.ReverseSequenceOptions: ReverseSequenceOptions.ReverseSequenceOptions, |
| 289 | BuiltinOptions.MatrixDiagOptions: MatrixDiagOptions.MatrixDiagOptions, |
| 290 | BuiltinOptions.QuantizeOptions: QuantizeOptions.QuantizeOptions, |
| 291 | BuiltinOptions.MatrixSetDiagOptions: MatrixSetDiagOptions.MatrixSetDiagOptions, |
| 292 | BuiltinOptions.DensifyOptions: DensifyOptions.DensifyOptions, |
| 293 | BuiltinOptions.DepthToSpaceOptions: DepthToSpaceOptions.DepthToSpaceOptions, |
Louis Verhaard | 678645b | 2020-06-15 15:22:47 +0200 | [diff] [blame] | 294 | BuiltinOptions.HardSwishOptions: HardSwishOptions.HardSwishOptions, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 295 | BuiltinOptions.IfOptions: IfOptions.IfOptions, |
| 296 | BuiltinOptions.NonMaxSuppressionV4Options: NonMaxSuppressionV4Options.NonMaxSuppressionV4Options, |
| 297 | BuiltinOptions.NonMaxSuppressionV5Options: NonMaxSuppressionV5Options.NonMaxSuppressionV5Options, |
| 298 | BuiltinOptions.ScatterNdOptions: ScatterNdOptions.ScatterNdOptions, |
| 299 | BuiltinOptions.SegmentSumOptions: SegmentSumOptions.SegmentSumOptions, |
| 300 | BuiltinOptions.SelectV2Options: SelectV2Options.SelectV2Options, |
| 301 | BuiltinOptions.WhileOptions: WhileOptions.WhileOptions, |
Jacob Bohlin | 8daf6b7 | 2020-09-15 16:28:35 +0200 | [diff] [blame] | 302 | BuiltinOptions.BatchMatMulOptions: BatchMatMulOptions.BatchMatMulOptions, |
Tim Hall | 42abec1 | 2021-02-04 21:31:57 +0000 | [diff] [blame] | 303 | BuiltinOptions.CumsumOptions: CumsumOptions.CumsumOptions, |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 304 | BuiltinOptions.CallOnceOptions: CallOnceOptions.CallOnceOptions, |
| 305 | BuiltinOptions.BroadcastToOptions: BroadcastToOptions.BroadcastToOptions, |
| 306 | BuiltinOptions.Rfft2dOptions: Rfft2dOptions.Rfft2dOptions, |
| 307 | BuiltinOptions.Conv3DOptions: Conv3DOptions.Conv3DOptions, |
| 308 | BuiltinOptions.HashtableOptions: HashtableOptions.HashtableOptions, |
| 309 | BuiltinOptions.HashtableFindOptions: HashtableFindOptions.HashtableFindOptions, |
| 310 | BuiltinOptions.HashtableImportOptions: HashtableImportOptions.HashtableImportOptions, |
| 311 | BuiltinOptions.HashtableSizeOptions: HashtableSizeOptions.HashtableSizeOptions, |
Rickard Bolin | 2de898a | 2021-12-20 08:35:23 +0000 | [diff] [blame] | 312 | BuiltinOptions.VarHandleOptions: VarHandleOptions.VarHandleOptions, |
| 313 | BuiltinOptions.ReadVariableOptions: ReadVariableOptions.ReadVariableOptions, |
| 314 | BuiltinOptions.AssignVariableOptions: AssignVariableOptions.AssignVariableOptions, |
| 315 | BuiltinOptions.RandomOptions: RandomOptions.RandomOptions, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 316 | } |
| 317 | |
Rickard Bolin | 2de898a | 2021-12-20 08:35:23 +0000 | [diff] [blame] | 318 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 319 | builtin_options_inv_map = inverse_map(builtin_options_map) |
| 320 | |
| 321 | |
| 322 | def underscore_to_camel_case(s): |
| 323 | return "".join(x.title() for x in s.split("_")) |
| 324 | |
| 325 | |
| 326 | def padding_deserialize(x): |
| 327 | return padding_map[x] |
| 328 | |
| 329 | |
| 330 | def padding_serialize(builder, x): |
| 331 | return padding_inv_map[x] |
| 332 | |
| 333 | |
| 334 | def activation_deserialize(x): |
| 335 | return activation_function_map[x] |
| 336 | |
| 337 | |
| 338 | def activation_serialize(builder, x): |
| 339 | return activation_function_inv_map[x] |
| 340 | |
| 341 | |
| 342 | def datatype_deserialize(x): |
| 343 | return datatype_map[x] |
| 344 | |
| 345 | |
| 346 | def datatype_serialize(builder, x): |
| 347 | return datatype_inv_map[x] |
| 348 | |
| 349 | |
| 350 | def identity(x): |
| 351 | return x |
| 352 | |
| 353 | |
| 354 | def identity_serialize(builder, x): |
| 355 | return x |
| 356 | |
| 357 | |
| 358 | def write_byte_vector(builder, v): |
| 359 | builder.StartVector(1, len(v), 1) |
| 360 | for e in v[::-1]: |
| 361 | builder.PrependByte(e) |
| 362 | return builder.EndVector(len(v)) |
| 363 | |
| 364 | |
| 365 | def write_int_vector(builder, v): |
| 366 | builder.StartVector(4, len(v), 4) |
| 367 | for e in v[::-1]: |
| 368 | builder.PrependInt32(e) |
| 369 | return builder.EndVector(len(v)) |
| 370 | |
| 371 | |
| 372 | class OptionsSerializer: |
Michael McGeagh | 6f72526 | 2020-12-03 15:21:36 +0000 | [diff] [blame] | 373 | def __init__(self, name, members=None): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 374 | self.name = name |
| 375 | self.module = globals()[self.name] |
| 376 | self.cls = getattr(self.module, self.name) |
| 377 | self.builtin_opt_type = builtin_options_inv_map[self.cls] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 378 | self.members = [] |
Michael McGeagh | 6f72526 | 2020-12-03 15:21:36 +0000 | [diff] [blame] | 379 | if members is not None: |
| 380 | for mem in members: |
| 381 | deserialize = identity |
| 382 | serialize = identity_serialize |
| 383 | is_vector = False |
| 384 | if isinstance(mem, tuple): |
| 385 | if len(mem) == 3: |
| 386 | mem, deserialize, serialize = mem |
| 387 | elif len(mem) == 2: |
| 388 | mem, is_vector = mem |
| 389 | deserialize = tuple |
| 390 | serialize = write_int_vector |
| 391 | else: |
| 392 | assert 0 |
| 393 | underscore_mem = mem |
| 394 | camelcase_mem = underscore_to_camel_case(mem) |
| 395 | self.members.append((underscore_mem, camelcase_mem, deserialize, serialize, is_vector)) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 396 | |
Tim Hall | c8310b1 | 2020-06-17 14:53:11 +0100 | [diff] [blame] | 397 | def deserialize(self, op_data): |
| 398 | builtin_options = op_data.BuiltinOptions() |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 399 | attrs = {} |
Tim Hall | c8310b1 | 2020-06-17 14:53:11 +0100 | [diff] [blame] | 400 | if builtin_options: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 401 | tfattrs = self.cls() |
Tim Hall | c8310b1 | 2020-06-17 14:53:11 +0100 | [diff] [blame] | 402 | tfattrs.Init(builtin_options.Bytes, builtin_options.Pos) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 403 | for underscore_mem, camelcase_mem, deserialize, serialize, is_vector in self.members: |
| 404 | fun = camelcase_mem |
| 405 | if is_vector: |
| 406 | fun += "AsNumpy" |
| 407 | |
Fredrik Svedberg | 4bb989b | 2020-11-16 15:32:25 +0100 | [diff] [blame] | 408 | attr = getattr(tfattrs, fun)() |
| 409 | try: |
| 410 | attrs[underscore_mem] = deserialize(attr) |
| 411 | except TypeError: |
| 412 | print("Warning: {0} could not read attribute '{1}'.".format(self.name, underscore_mem)) |
| 413 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 414 | return attrs |
| 415 | |
| 416 | def serialize(self, builder, attrs): |
| 417 | ser_attrs = [] |
| 418 | for underscore_mem, camelcase_mem, deserialize, serialize, is_vector in self.members: |
| 419 | a = serialize(builder, attrs[underscore_mem]) |
| 420 | ser_attrs.append((camelcase_mem, a)) |
| 421 | |
| 422 | getattr(self.module, self.name + "Start")(builder) |
| 423 | |
| 424 | for camelcase_mem, a in ser_attrs: |
| 425 | getattr(self.module, self.name + "Add" + camelcase_mem)(builder, a) |
| 426 | |
| 427 | return getattr(self.module, self.name + "End")(builder), None |
| 428 | |
| 429 | |
| 430 | class CustomOptionsSerializer: |
Tim Hall | c8310b1 | 2020-06-17 14:53:11 +0100 | [diff] [blame] | 431 | CUSTOM_OPTIONS_NPU_OP = [0x01, 0x04, 0x01] # NpuOp=1, FlexbufferFormat.UINT8=4, byte length=1 |
| 432 | CUSTOM_OPTIONS_FORMAT_DEFAULT = 0 |
| 433 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 434 | def __init__(self): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 435 | self.custom_opt_format = 0 |
| 436 | |
Tim Hall | c8310b1 | 2020-06-17 14:53:11 +0100 | [diff] [blame] | 437 | def deserialize(self, op_data): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 438 | attrs = {} |
Tim Hall | c8310b1 | 2020-06-17 14:53:11 +0100 | [diff] [blame] | 439 | custom_options = op_data.CustomOptionsAsNumpy() |
| 440 | attrs["custom_options"] = custom_options |
| 441 | attrs["custom_options_format"] = op_data.CustomOptionsFormat() |
| 442 | |
| 443 | if np.array_equal(custom_options, self.CUSTOM_OPTIONS_NPU_OP): |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 444 | attrs["custom_type"] = CustomType.ExistingNpuOp |
Tim Hall | c8310b1 | 2020-06-17 14:53:11 +0100 | [diff] [blame] | 445 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 446 | return attrs |
| 447 | |
| 448 | def serialize(self, builder, attrs): |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 449 | custom_type = attrs.get("custom_type", CustomType.ThirdPartyOp) |
Tim Hall | c8310b1 | 2020-06-17 14:53:11 +0100 | [diff] [blame] | 450 | 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] | 451 | |
| 452 | # Set NPU op custom options for the TensorFlow Lite custom operator |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 453 | if custom_type == CustomType.NpuOp: |
Tim Hall | c8310b1 | 2020-06-17 14:53:11 +0100 | [diff] [blame] | 454 | custom_options = self.CUSTOM_OPTIONS_NPU_OP |
| 455 | else: |
| 456 | custom_options = attrs.get("custom_options", []) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 457 | |
Tim Hall | c8310b1 | 2020-06-17 14:53:11 +0100 | [diff] [blame] | 458 | custom_options_bytes = struct.pack("<{0}B".format(len(custom_options)), *custom_options) |
| 459 | custom_offset = write_byte_vector(builder, custom_options_bytes) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 460 | |
| 461 | return None, custom_offset |
| 462 | |
| 463 | |
| 464 | padding_map = { |
Michael McGeagh | 1689548 | 2020-12-14 15:51:20 +0000 | [diff] [blame] | 465 | Padding.SAME: opPad.SAME, |
| 466 | Padding.VALID: opPad.VALID, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | padding_inv_map = inverse_map(padding_map) |
| 470 | |
| 471 | |
| 472 | activation_function_map = { |
| 473 | ActivationFunctionType.NONE: None, |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 474 | ActivationFunctionType.RELU: Op.Relu, |
| 475 | ActivationFunctionType.RELU_N1_TO_1: Op.ReluN1To1, |
| 476 | ActivationFunctionType.RELU6: Op.Relu6, |
| 477 | ActivationFunctionType.TANH: Op.Tanh, |
| 478 | ActivationFunctionType.SIGN_BIT: Op.SignBit, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | activation_function_inv_map = inverse_map(activation_function_map) |
| 482 | |
| 483 | fused_act = ("fused_activation_function", activation_deserialize, activation_serialize) |
| 484 | padding = ("padding", padding_deserialize, padding_serialize) |
| 485 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 486 | is_int_vec = True |
| 487 | |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 488 | TFLITE_NO_INDICES = TensorIndices([], [], []) |
| 489 | TFLITE_IFM_INDICES = TensorIndices([0], [], []) |
| 490 | TFLITE_IFM_WEIGHTS_INDICES = TensorIndices([0], [1], []) |
| 491 | TFLITE_IFM_WEIGHTS_BIAS_INDICES = TensorIndices([0], [1], [2]) |
| 492 | TFLITE_IFM_IFM2_INDICES = TensorIndices([0, 1], [], []) |
| 493 | TFLITE_CONV2D_BACKPROP_INDICES = TensorIndices([2], [1], [3]) |
| 494 | TFLITE_TRANSPOSE_CONV_INDICES = TensorIndices([0], [1], [3]) |
| 495 | TFLITE_CONCAT_INDICES = TensorIndices([1, 2], [], []) |
| 496 | TFLITE_SPLIT_IFM_INDICES = TensorIndices([1], [], []) |
| 497 | TFLITE_BLOCK_LSTM_INDICES = TensorIndices([3], [4], []) |
| 498 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 499 | builtin_operator_map = { |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 500 | BuiltinOperator.ADD: ( |
| 501 | Op.Add, |
| 502 | OptionsSerializer("AddOptions", (fused_act, "pot_scale_int16")), |
| 503 | TFLITE_IFM_IFM2_INDICES, |
| 504 | ), |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 505 | BuiltinOperator.AVERAGE_POOL_2D: ( |
| 506 | Op.AvgPool, |
| 507 | OptionsSerializer( |
| 508 | "Pool2DOptions", ("filter_height", "filter_width", fused_act, padding, "stride_h", "stride_w") |
| 509 | ), |
| 510 | TFLITE_IFM_INDICES, |
| 511 | ), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 512 | BuiltinOperator.CONCATENATION: ( |
| 513 | Op.ConcatTFLite, |
| 514 | OptionsSerializer("ConcatenationOptions", ("axis", fused_act)), |
| 515 | TFLITE_CONCAT_INDICES, |
| 516 | ), |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 517 | BuiltinOperator.CONV_2D: ( |
| 518 | Op.Conv2DBias, |
| 519 | OptionsSerializer( |
| 520 | "Conv2DOptions", ("dilation_h_factor", "dilation_w_factor", fused_act, padding, "stride_h", "stride_w") |
| 521 | ), |
| 522 | TFLITE_IFM_WEIGHTS_BIAS_INDICES, |
| 523 | ), |
| 524 | BuiltinOperator.DEPTHWISE_CONV_2D: ( |
| 525 | Op.DepthwiseConv2DBias, |
| 526 | OptionsSerializer( |
| 527 | "DepthwiseConv2DOptions", |
| 528 | ("depth_multiplier", "dilation_h_factor", "dilation_w_factor", fused_act, padding, "stride_h", "stride_w"), |
| 529 | ), |
| 530 | TFLITE_IFM_WEIGHTS_BIAS_INDICES, |
| 531 | ), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 532 | BuiltinOperator.DEPTH_TO_SPACE: ( |
| 533 | Op.DepthToSpace, |
| 534 | OptionsSerializer("DepthToSpaceOptions", ("block_size",)), |
| 535 | TFLITE_NO_INDICES, |
| 536 | ), |
| 537 | BuiltinOperator.DEQUANTIZE: (Op.Dequantize, OptionsSerializer("DequantizeOptions"), TFLITE_IFM_INDICES), |
| 538 | BuiltinOperator.EMBEDDING_LOOKUP: (Op.EmbeddingLookup, None, TFLITE_NO_INDICES), |
| 539 | BuiltinOperator.FLOOR: (Op.Floor, None, TFLITE_NO_INDICES), |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 540 | BuiltinOperator.FULLY_CONNECTED: ( |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 541 | Op.FullyConnected, |
erik.andersson@arm.com | 0cbb166 | 2021-02-22 15:47:07 +0100 | [diff] [blame] | 542 | OptionsSerializer( |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 543 | "FullyConnectedOptions", ("asymmetric_quantize_inputs", fused_act, "keep_num_dims", "weights_format") |
erik.andersson@arm.com | 0cbb166 | 2021-02-22 15:47:07 +0100 | [diff] [blame] | 544 | ), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 545 | TFLITE_IFM_WEIGHTS_BIAS_INDICES, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 546 | ), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 547 | BuiltinOperator.HASHTABLE_LOOKUP: (Op.HashtableLookup, None, TFLITE_NO_INDICES), |
| 548 | BuiltinOperator.L2_NORMALIZATION: (Op.L2Norm, OptionsSerializer("L2NormOptions", (fused_act,)), TFLITE_NO_INDICES), |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 549 | BuiltinOperator.L2_POOL_2D: ( |
| 550 | Op.L2Pool2D, |
| 551 | OptionsSerializer( |
| 552 | "Pool2DOptions", ("filter_height", "filter_width", fused_act, padding, "stride_h", "stride_w") |
| 553 | ), |
| 554 | TFLITE_NO_INDICES, |
| 555 | ), |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 556 | BuiltinOperator.LOCAL_RESPONSE_NORMALIZATION: ( |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 557 | Op.LRN, |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 558 | OptionsSerializer("LocalResponseNormalizationOptions", ("alpha", "beta", "bias", "radius")), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 559 | TFLITE_NO_INDICES, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 560 | ), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 561 | BuiltinOperator.LOGISTIC: (Op.Sigmoid, None, TFLITE_IFM_INDICES), |
| 562 | BuiltinOperator.LSH_PROJECTION: ( |
| 563 | Op.LSHProjection, |
| 564 | OptionsSerializer("LSHProjectionOptions", ("type",)), |
| 565 | TFLITE_NO_INDICES, |
| 566 | ), |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 567 | BuiltinOperator.LSTM: ( |
| 568 | Op.Lstm, |
| 569 | OptionsSerializer( |
| 570 | "LSTMOptions", ("asymmetric_quantize_inputs", "cell_clip", fused_act, "kernel_type", "proj_clip") |
| 571 | ), |
| 572 | TFLITE_IFM_WEIGHTS_INDICES, |
| 573 | ), |
| 574 | BuiltinOperator.MAX_POOL_2D: ( |
| 575 | Op.MaxPool, |
| 576 | OptionsSerializer( |
| 577 | "Pool2DOptions", ("filter_height", "filter_width", fused_act, padding, "stride_h", "stride_w") |
| 578 | ), |
| 579 | TFLITE_IFM_INDICES, |
| 580 | ), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 581 | BuiltinOperator.MUL: (Op.Mul, OptionsSerializer("MulOptions", (fused_act,)), TFLITE_IFM_IFM2_INDICES), |
| 582 | BuiltinOperator.RELU: (Op.Relu, None, TFLITE_IFM_INDICES), |
| 583 | BuiltinOperator.RELU_N1_TO_1: (Op.ReluN1To1, None, TFLITE_IFM_INDICES), |
| 584 | BuiltinOperator.RELU6: (Op.Relu6, None, TFLITE_IFM_INDICES), |
| 585 | BuiltinOperator.RESHAPE: ( |
| 586 | Op.Reshape, |
Dwight Lidman | cfb4262 | 2021-08-11 12:28:51 +0200 | [diff] [blame] | 587 | OptionsSerializer("ReshapeOptions", (("new_shape", is_int_vec),)), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 588 | TFLITE_IFM_INDICES, |
| 589 | ), |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 590 | BuiltinOperator.RESIZE_BILINEAR: ( |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 591 | Op.ResizeBilinear, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 592 | OptionsSerializer("ResizeBilinearOptions", ("align_corners", "half_pixel_centers")), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 593 | TFLITE_IFM_INDICES, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 594 | ), |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 595 | BuiltinOperator.RNN: ( |
| 596 | Op.Rnn, |
| 597 | OptionsSerializer("RNNOptions", ("asymmetric_quantize_inputs", fused_act)), |
| 598 | TFLITE_IFM_WEIGHTS_INDICES, |
| 599 | ), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 600 | BuiltinOperator.SOFTMAX: (Op.Softmax, OptionsSerializer("SoftmaxOptions", ("beta",)), TFLITE_IFM_INDICES), |
| 601 | BuiltinOperator.SPACE_TO_DEPTH: ( |
| 602 | Op.SpaceToDepth, |
| 603 | OptionsSerializer("SpaceToDepthOptions", ("block_size",)), |
| 604 | TFLITE_NO_INDICES, |
| 605 | ), |
Jacob Bohlin | 8daf6b7 | 2020-09-15 16:28:35 +0200 | [diff] [blame] | 606 | BuiltinOperator.SVDF: ( |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 607 | Op.Svdf, |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 608 | OptionsSerializer("SVDFOptions", ("asymmetric_quantize_inputs", fused_act, "rank")), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 609 | TFLITE_NO_INDICES, |
Jacob Bohlin | 8daf6b7 | 2020-09-15 16:28:35 +0200 | [diff] [blame] | 610 | ), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 611 | BuiltinOperator.TANH: (Op.Tanh, None, TFLITE_IFM_INDICES), |
Dwight Lidman | d2a972d | 2020-08-18 14:08:10 +0200 | [diff] [blame] | 612 | BuiltinOperator.CONCAT_EMBEDDINGS: ( |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 613 | Op.ConcatEmbeddings, |
Dwight Lidman | d2a972d | 2020-08-18 14:08:10 +0200 | [diff] [blame] | 614 | OptionsSerializer( |
| 615 | "ConcatEmbeddingsOptions", |
Jacob Bohlin | 8daf6b7 | 2020-09-15 16:28:35 +0200 | [diff] [blame] | 616 | ( |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 617 | "embedding_dim_per_channel", |
| 618 | "embedding_dim_per_channel_as_numpy", |
| 619 | "embedding_dim_per_channel_is_none", |
| 620 | "embedding_dim_per_channel_length", |
Jacob Bohlin | 8daf6b7 | 2020-09-15 16:28:35 +0200 | [diff] [blame] | 621 | "num_channels", |
| 622 | "num_columns_per_channel", |
| 623 | "num_columns_per_channel_as_numpy", |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 624 | "num_columns_per_channel_is_none", |
| 625 | "num_columns_per_channel_length", |
Jacob Bohlin | 8daf6b7 | 2020-09-15 16:28:35 +0200 | [diff] [blame] | 626 | ), |
Dwight Lidman | d2a972d | 2020-08-18 14:08:10 +0200 | [diff] [blame] | 627 | ), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 628 | TFLITE_NO_INDICES, |
Dwight Lidman | d2a972d | 2020-08-18 14:08:10 +0200 | [diff] [blame] | 629 | ), |
| 630 | BuiltinOperator.SKIP_GRAM: ( |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 631 | Op.SkipGram, |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 632 | OptionsSerializer("SkipGramOptions", ("include_all_ngrams", "max_skip_size", "ngram_size")), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 633 | TFLITE_NO_INDICES, |
Dwight Lidman | d2a972d | 2020-08-18 14:08:10 +0200 | [diff] [blame] | 634 | ), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 635 | BuiltinOperator.CALL: (Op.Call, OptionsSerializer("CallOptions", ("subgraph",)), TFLITE_NO_INDICES), |
Dwight Lidman | d2a972d | 2020-08-18 14:08:10 +0200 | [diff] [blame] | 636 | BuiltinOperator.EMBEDDING_LOOKUP_SPARSE: ( |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 637 | Op.EmbeddingLookupSparse, |
Jacob Bohlin | 8daf6b7 | 2020-09-15 16:28:35 +0200 | [diff] [blame] | 638 | OptionsSerializer("EmbeddingLookupSparseOptions", ("combiner",)), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 639 | TFLITE_NO_INDICES, |
Dwight Lidman | d2a972d | 2020-08-18 14:08:10 +0200 | [diff] [blame] | 640 | ), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 641 | BuiltinOperator.PAD: (Op.Pad, OptionsSerializer("PadOptions"), TFLITE_IFM_INDICES), |
| 642 | BuiltinOperator.UNIDIRECTIONAL_SEQUENCE_RNN: ( |
| 643 | Op.UnidirectionalSequenceRnn, |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 644 | OptionsSerializer("SequenceRNNOptions", ("asymmetric_quantize_inputs", fused_act, "time_major")), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 645 | TFLITE_IFM_WEIGHTS_INDICES, |
| 646 | ), |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 647 | BuiltinOperator.GATHER: ( |
| 648 | Op.GatherV2, |
| 649 | OptionsSerializer("GatherOptions", ("axis", "batch_dims")), |
| 650 | TFLITE_NO_INDICES, |
| 651 | ), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 652 | BuiltinOperator.BATCH_TO_SPACE_ND: ( |
| 653 | Op.BatchToSpaceND, |
| 654 | OptionsSerializer("BatchToSpaceNDOptions"), |
| 655 | TFLITE_NO_INDICES, |
| 656 | ), |
| 657 | BuiltinOperator.SPACE_TO_BATCH_ND: ( |
| 658 | Op.SpaceToBatchND, |
| 659 | OptionsSerializer("SpaceToBatchNDOptions"), |
| 660 | TFLITE_NO_INDICES, |
| 661 | ), |
James Ward | 6bf1613 | 2021-09-08 11:14:20 +0100 | [diff] [blame] | 662 | BuiltinOperator.TRANSPOSE: (Op.Transpose, OptionsSerializer("TransposeOptions"), TFLITE_IFM_IFM2_INDICES), |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 663 | BuiltinOperator.MEAN: (Op.Mean, OptionsSerializer("ReducerOptions", ("keep_dims",)), TFLITE_IFM_INDICES), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 664 | BuiltinOperator.SUB: ( |
| 665 | Op.Sub, |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 666 | OptionsSerializer("SubOptions", (fused_act, "pot_scale_int16")), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 667 | TFLITE_IFM_IFM2_INDICES, |
| 668 | ), |
| 669 | BuiltinOperator.DIV: (Op.Div, OptionsSerializer("DivOptions", (fused_act,)), TFLITE_NO_INDICES), |
| 670 | BuiltinOperator.SQUEEZE: ( |
| 671 | Op.Squeeze, |
Jonas Ohlsson | d857507 | 2022-03-30 10:30:25 +0200 | [diff] [blame] | 672 | OptionsSerializer( |
| 673 | "SqueezeOptions", |
| 674 | (("squeeze_dims", is_int_vec),), |
| 675 | ), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 676 | TFLITE_IFM_INDICES, |
| 677 | ), |
| 678 | BuiltinOperator.UNIDIRECTIONAL_SEQUENCE_LSTM: ( |
| 679 | Op.UnidirectionalSequenceLstm, |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 680 | OptionsSerializer( |
| 681 | "UnidirectionalSequenceLSTMOptions", |
| 682 | ("asymmetric_quantize_inputs", "cell_clip", fused_act, "proj_clip", "time_major"), |
| 683 | ), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 684 | TFLITE_IFM_WEIGHTS_INDICES, |
| 685 | ), |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 686 | BuiltinOperator.STRIDED_SLICE: ( |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 687 | Op.StridedSlice, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 688 | OptionsSerializer( |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 689 | "StridedSliceOptions", ("begin_mask", "ellipsis_mask", "end_mask", "new_axis_mask", "shrink_axis_mask") |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 690 | ), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 691 | TFLITE_IFM_INDICES, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 692 | ), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 693 | BuiltinOperator.BIDIRECTIONAL_SEQUENCE_RNN: ( |
| 694 | Op.BidirectionalSequenceRnn, |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 695 | OptionsSerializer( |
| 696 | "BidirectionalSequenceRNNOptions", ("asymmetric_quantize_inputs", fused_act, "merge_outputs", "time_major") |
| 697 | ), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 698 | TFLITE_IFM_WEIGHTS_INDICES, |
| 699 | ), |
James Ward | 6bf1613 | 2021-09-08 11:14:20 +0100 | [diff] [blame] | 700 | BuiltinOperator.EXP: (Op.Exp, OptionsSerializer("ExpOptions"), TFLITE_NO_INDICES), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 701 | BuiltinOperator.TOPK_V2: (Op.TopKV2, OptionsSerializer("TopKV2Options"), TFLITE_NO_INDICES), |
| 702 | BuiltinOperator.SPLIT: (Op.Split, OptionsSerializer("SplitOptions", ("num_splits",)), TFLITE_SPLIT_IFM_INDICES), |
| 703 | BuiltinOperator.LOG_SOFTMAX: (Op.LogSoftmax, OptionsSerializer("LogSoftmaxOptions"), TFLITE_NO_INDICES), |
| 704 | BuiltinOperator.DELEGATE: (Op.Delegate, None, TFLITE_NO_INDICES), |
| 705 | BuiltinOperator.BIDIRECTIONAL_SEQUENCE_LSTM: ( |
| 706 | Op.BidirectionalSequenceLstm, |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 707 | OptionsSerializer( |
| 708 | "BidirectionalSequenceLSTMOptions", |
| 709 | ("asymmetric_quantize_inputs", "cell_clip", fused_act, "merge_outputs", "proj_clip", "time_major"), |
| 710 | ), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 711 | TFLITE_IFM_WEIGHTS_INDICES, |
| 712 | ), |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 713 | BuiltinOperator.CAST: ( |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 714 | Op.Cast, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 715 | OptionsSerializer( |
| 716 | "CastOptions", |
| 717 | ( |
| 718 | ("in_data_type", datatype_deserialize, datatype_serialize), |
| 719 | ("out_data_type", datatype_deserialize, datatype_serialize), |
| 720 | ), |
| 721 | ), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 722 | TFLITE_NO_INDICES, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 723 | ), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 724 | BuiltinOperator.PRELU: (Op.Prelu, None, TFLITE_NO_INDICES), |
| 725 | BuiltinOperator.MAXIMUM: (Op.Maximum, OptionsSerializer("MaximumMinimumOptions"), TFLITE_IFM_IFM2_INDICES), |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 726 | BuiltinOperator.ARG_MAX: ( |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 727 | Op.ArgMax, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 728 | OptionsSerializer("ArgMaxOptions", (("output_type", datatype_deserialize, datatype_serialize),)), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 729 | TFLITE_NO_INDICES, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 730 | ), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 731 | BuiltinOperator.MINIMUM: (Op.Minimum, OptionsSerializer("MaximumMinimumOptions"), TFLITE_IFM_IFM2_INDICES), |
| 732 | BuiltinOperator.LESS: (Op.Less, OptionsSerializer("LessOptions"), TFLITE_NO_INDICES), |
| 733 | BuiltinOperator.NEG: (Op.Neg, OptionsSerializer("NegOptions"), TFLITE_NO_INDICES), |
| 734 | BuiltinOperator.PADV2: (Op.PadV2, OptionsSerializer("PadV2Options"), TFLITE_NO_INDICES), |
| 735 | BuiltinOperator.GREATER: (Op.Greater, OptionsSerializer("GreaterOptions"), TFLITE_NO_INDICES), |
| 736 | BuiltinOperator.GREATER_EQUAL: (Op.GreaterEqual, OptionsSerializer("GreaterEqualOptions"), TFLITE_NO_INDICES), |
| 737 | BuiltinOperator.LESS_EQUAL: (Op.LessEqual, OptionsSerializer("LessEqualOptions"), TFLITE_NO_INDICES), |
| 738 | BuiltinOperator.SELECT: (Op.Select, OptionsSerializer("SelectOptions"), TFLITE_NO_INDICES), |
| 739 | BuiltinOperator.SLICE: (Op.Slice, OptionsSerializer("SliceOptions"), TFLITE_IFM_INDICES), |
| 740 | BuiltinOperator.SIN: (Op.Sin, None, TFLITE_NO_INDICES), |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 741 | BuiltinOperator.TRANSPOSE_CONV: ( |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 742 | Op.Conv2DBackpropInput, |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 743 | OptionsSerializer("TransposeConvOptions", (padding, "stride_h", "stride_w")), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 744 | TFLITE_CONV2D_BACKPROP_INDICES, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 745 | ), |
| 746 | BuiltinOperator.SPARSE_TO_DENSE: ( |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 747 | Op.SparseToDense, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 748 | OptionsSerializer("SparseToDenseOptions", ("validate_indices",)), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 749 | TFLITE_NO_INDICES, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 750 | ), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 751 | BuiltinOperator.TILE: (Op.Tile, OptionsSerializer("TileOptions"), TFLITE_NO_INDICES), |
| 752 | BuiltinOperator.EXPAND_DIMS: (Op.ExpandDims, OptionsSerializer("ExpandDimsOptions"), TFLITE_IFM_INDICES), |
| 753 | BuiltinOperator.EQUAL: (Op.Equal, OptionsSerializer("EqualOptions"), TFLITE_NO_INDICES), |
| 754 | BuiltinOperator.NOT_EQUAL: (Op.NotEqual, OptionsSerializer("NotEqualOptions"), TFLITE_NO_INDICES), |
| 755 | BuiltinOperator.LOG: (Op.Log, None, TFLITE_NO_INDICES), |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 756 | BuiltinOperator.SUM: (Op.Sum, OptionsSerializer("ReducerOptions", ("keep_dims",)), TFLITE_NO_INDICES), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 757 | BuiltinOperator.SQRT: (Op.Sqrt, None, TFLITE_NO_INDICES), |
| 758 | BuiltinOperator.RSQRT: (Op.Rsqrt, None, TFLITE_NO_INDICES), |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 759 | BuiltinOperator.SHAPE: ( |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 760 | Op.Shape, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 761 | OptionsSerializer("ShapeOptions", (("out_type", datatype_deserialize, datatype_serialize),)), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 762 | TFLITE_NO_INDICES, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 763 | ), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 764 | BuiltinOperator.POW: (Op.Pow, OptionsSerializer("PowOptions"), TFLITE_NO_INDICES), |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 765 | BuiltinOperator.ARG_MIN: ( |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 766 | Op.ArgMin, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 767 | OptionsSerializer("ArgMinOptions", (("output_type", datatype_deserialize, datatype_serialize),)), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 768 | TFLITE_NO_INDICES, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 769 | ), |
| 770 | BuiltinOperator.FAKE_QUANT: ( |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 771 | Op.FakeQuantWithMinMaxArgs, |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 772 | OptionsSerializer("FakeQuantOptions", ("max", "min", "narrow_range", "num_bits")), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 773 | TFLITE_NO_INDICES, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 774 | ), |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 775 | BuiltinOperator.REDUCE_PROD: (Op.Prod, OptionsSerializer("ReducerOptions", ("keep_dims",)), TFLITE_NO_INDICES), |
| 776 | BuiltinOperator.REDUCE_MAX: (Op.Max, OptionsSerializer("ReducerOptions", ("keep_dims",)), TFLITE_NO_INDICES), |
| 777 | BuiltinOperator.PACK: (Op.Pack, OptionsSerializer("PackOptions", ("axis", "values_count")), TFLITE_IFM_INDICES), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 778 | BuiltinOperator.LOGICAL_OR: (Op.LogicalOr, OptionsSerializer("LogicalOrOptions"), TFLITE_NO_INDICES), |
| 779 | BuiltinOperator.ONE_HOT: (Op.OneHot, OptionsSerializer("OneHotOptions", ("axis",)), TFLITE_NO_INDICES), |
| 780 | BuiltinOperator.LOGICAL_AND: (Op.LogicalAnd, OptionsSerializer("LogicalAndOptions"), TFLITE_NO_INDICES), |
| 781 | BuiltinOperator.LOGICAL_NOT: (Op.LogicalNot, OptionsSerializer("LogicalNotOptions"), TFLITE_NO_INDICES), |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 782 | BuiltinOperator.UNPACK: (Op.Unpack, OptionsSerializer("UnpackOptions", ("axis", "num")), TFLITE_IFM_INDICES), |
| 783 | BuiltinOperator.REDUCE_MIN: (Op.Min, OptionsSerializer("ReducerOptions", ("keep_dims",)), TFLITE_NO_INDICES), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 784 | BuiltinOperator.FLOOR_DIV: (Op.FloorDiv, OptionsSerializer("FloorDivOptions"), TFLITE_NO_INDICES), |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 785 | BuiltinOperator.REDUCE_ANY: (Op.Any, OptionsSerializer("ReducerOptions", ("keep_dims",)), TFLITE_NO_INDICES), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 786 | BuiltinOperator.SQUARE: (Op.Square, OptionsSerializer("SquareOptions"), TFLITE_NO_INDICES), |
| 787 | BuiltinOperator.ZEROS_LIKE: (Op.ZerosLike, OptionsSerializer("ZerosLikeOptions"), TFLITE_NO_INDICES), |
| 788 | BuiltinOperator.FILL: (Op.Fill, OptionsSerializer("FillOptions"), TFLITE_NO_INDICES), |
| 789 | BuiltinOperator.FLOOR_MOD: (Op.FloorMod, OptionsSerializer("FloorModOptions"), TFLITE_NO_INDICES), |
| 790 | BuiltinOperator.RANGE: (Op.Range, OptionsSerializer("RangeOptions"), TFLITE_NO_INDICES), |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 791 | BuiltinOperator.RESIZE_NEAREST_NEIGHBOR: ( |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 792 | Op.ResizeNearestNeighbor, |
Jacob Bohlin | 8daf6b7 | 2020-09-15 16:28:35 +0200 | [diff] [blame] | 793 | OptionsSerializer("ResizeNearestNeighborOptions", ("align_corners", "half_pixel_centers")), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 794 | TFLITE_NO_INDICES, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 795 | ), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 796 | BuiltinOperator.LEAKY_RELU: (Op.LeakyRelu, OptionsSerializer("LeakyReluOptions", ("alpha",)), TFLITE_IFM_INDICES), |
| 797 | BuiltinOperator.SQUARED_DIFFERENCE: ( |
| 798 | Op.SquaredDifference, |
| 799 | OptionsSerializer("SquaredDifferenceOptions"), |
| 800 | TFLITE_NO_INDICES, |
| 801 | ), |
| 802 | BuiltinOperator.MIRROR_PAD: (Op.MirrorPad, OptionsSerializer("MirrorPadOptions", ("mode",)), TFLITE_NO_INDICES), |
| 803 | BuiltinOperator.ABS: (Op.Abs, OptionsSerializer("AbsOptions"), TFLITE_IFM_INDICES), |
| 804 | BuiltinOperator.SPLIT_V: (Op.SplitV, OptionsSerializer("SplitVOptions", ("num_splits",)), TFLITE_IFM_INDICES), |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 805 | BuiltinOperator.UNIQUE: ( |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 806 | Op.Unique, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 807 | OptionsSerializer("UniqueOptions", (("idx_out_type", datatype_deserialize, datatype_serialize),)), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 808 | TFLITE_NO_INDICES, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 809 | ), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 810 | BuiltinOperator.CEIL: (Op.Ceil, None, TFLITE_NO_INDICES), |
| 811 | BuiltinOperator.REVERSE_V2: (Op.ReverseV2, OptionsSerializer("ReverseV2Options"), TFLITE_NO_INDICES), |
| 812 | BuiltinOperator.ADD_N: (Op.AddN, OptionsSerializer("AddNOptions"), TFLITE_NO_INDICES), |
| 813 | BuiltinOperator.GATHER_ND: (Op.GatherNd, OptionsSerializer("GatherNdOptions"), TFLITE_NO_INDICES), |
| 814 | BuiltinOperator.COS: (Op.Cos, OptionsSerializer("CosOptions"), TFLITE_NO_INDICES), |
| 815 | BuiltinOperator.WHERE: (Op.Where, OptionsSerializer("WhereOptions"), TFLITE_NO_INDICES), |
| 816 | BuiltinOperator.RANK: (Op.Rank, OptionsSerializer("RankOptions"), TFLITE_NO_INDICES), |
| 817 | BuiltinOperator.ELU: (Op.Elu, None, TFLITE_NO_INDICES), |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 818 | BuiltinOperator.REVERSE_SEQUENCE: ( |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 819 | Op.ReverseSequence, |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 820 | OptionsSerializer("ReverseSequenceOptions", ("batch_dim", "seq_dim")), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 821 | TFLITE_NO_INDICES, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 822 | ), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 823 | BuiltinOperator.MATRIX_DIAG: (Op.MatrixDiag, OptionsSerializer("MatrixDiagOptions"), TFLITE_NO_INDICES), |
| 824 | BuiltinOperator.QUANTIZE: (Op.Quantize, OptionsSerializer("QuantizeOptions"), TFLITE_IFM_INDICES), |
| 825 | BuiltinOperator.MATRIX_SET_DIAG: (Op.MatrixSetDiag, OptionsSerializer("MatrixSetDiagOptions"), TFLITE_NO_INDICES), |
| 826 | BuiltinOperator.ROUND: (Op.Round, None, TFLITE_NO_INDICES), |
| 827 | BuiltinOperator.HARD_SWISH: (Op.HardSwish, OptionsSerializer("HardSwishOptions"), TFLITE_IFM_INDICES), |
| 828 | BuiltinOperator.IF: ( |
| 829 | Op.If, |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 830 | OptionsSerializer("IfOptions", ("else_subgraph_index", "then_subgraph_index")), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 831 | TFLITE_NO_INDICES, |
| 832 | ), |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 833 | BuiltinOperator.WHILE: ( |
| 834 | Op.While, |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 835 | OptionsSerializer("WhileOptions", ("body_subgraph_index", "cond_subgraph_index")), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 836 | TFLITE_NO_INDICES, |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 837 | ), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 838 | BuiltinOperator.NON_MAX_SUPPRESSION_V4: ( |
| 839 | Op.NonMaxSuppressionV4, |
| 840 | OptionsSerializer("NonMaxSuppressionV4Options"), |
| 841 | TFLITE_NO_INDICES, |
| 842 | ), |
| 843 | BuiltinOperator.NON_MAX_SUPPRESSION_V5: ( |
| 844 | Op.NonMaxSuppressionV5, |
| 845 | OptionsSerializer("NonMaxSuppressionV5Options"), |
| 846 | TFLITE_NO_INDICES, |
| 847 | ), |
| 848 | BuiltinOperator.SCATTER_ND: (Op.ScatterNd, OptionsSerializer("ScatterNdOptions"), TFLITE_NO_INDICES), |
| 849 | BuiltinOperator.SELECT_V2: (Op.SelectV2, OptionsSerializer("SelectV2Options"), TFLITE_NO_INDICES), |
| 850 | BuiltinOperator.DENSIFY: (Op.Densify, OptionsSerializer("DensifyOptions"), TFLITE_NO_INDICES), |
| 851 | BuiltinOperator.SEGMENT_SUM: (Op.SegmentSum, OptionsSerializer("SegmentSumOptions"), TFLITE_NO_INDICES), |
| 852 | BuiltinOperator.BATCH_MATMUL: ( |
| 853 | Op.BatchMatMul, |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 854 | OptionsSerializer("BatchMatMulOptions", ("adj_x", "adj_y", "asymmetric_quantize_inputs")), |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 855 | TFLITE_NO_INDICES, |
| 856 | ), |
| 857 | BuiltinOperator.CUMSUM: ( |
| 858 | Op.Cumsum, |
| 859 | OptionsSerializer("CumsumOptions", ("exclusive", "reverse")), |
| 860 | TFLITE_NO_INDICES, |
| 861 | ), |
Dwight Lidman | 8a12da1 | 2021-07-19 13:43:05 +0200 | [diff] [blame] | 862 | BuiltinOperator.CALL_ONCE: ( |
| 863 | Op.CallOnce, |
| 864 | OptionsSerializer("CallOnceOptions", ("init_subgraph_index",)), |
| 865 | TFLITE_NO_INDICES, |
| 866 | ), |
| 867 | BuiltinOperator.BROADCAST_TO: (Op.BroadcastTo, OptionsSerializer("BroadcastToOptions"), TFLITE_NO_INDICES), |
| 868 | BuiltinOperator.RFFT2D: (Op.Rfft2D, OptionsSerializer("Rfft2dOptions"), TFLITE_NO_INDICES), |
| 869 | BuiltinOperator.CONV_3D: ( |
| 870 | Op.Conv3D, |
| 871 | OptionsSerializer( |
| 872 | "Conv3DOptions", |
| 873 | ( |
| 874 | "dilation_d_factor", |
| 875 | "dilation_h_factor", |
| 876 | "dilation_w_factor", |
| 877 | fused_act, |
| 878 | padding, |
| 879 | "stride_d", |
| 880 | "stride_h", |
| 881 | "stride_w", |
| 882 | ), |
| 883 | ), |
| 884 | TFLITE_NO_INDICES, |
| 885 | ), |
| 886 | BuiltinOperator.IMAG: (Op.Imag, None, TFLITE_NO_INDICES), |
| 887 | BuiltinOperator.REAL: (Op.Real, None, TFLITE_NO_INDICES), |
| 888 | BuiltinOperator.COMPLEX_ABS: (Op.ComplexAbs, None, TFLITE_NO_INDICES), |
| 889 | BuiltinOperator.HASHTABLE: ( |
| 890 | Op.Hashtable, |
| 891 | OptionsSerializer("HashtableOptions", ("key_dtype", "table_id", "value_dtype")), |
| 892 | TFLITE_NO_INDICES, |
| 893 | ), |
| 894 | BuiltinOperator.HASHTABLE_FIND: (Op.HashtableFind, OptionsSerializer("HashtableFindOptions"), TFLITE_NO_INDICES), |
| 895 | BuiltinOperator.HASHTABLE_IMPORT: ( |
| 896 | Op.HashtableImport, |
| 897 | OptionsSerializer("HashtableImportOptions"), |
| 898 | TFLITE_NO_INDICES, |
| 899 | ), |
| 900 | BuiltinOperator.HASHTABLE_SIZE: (Op.HashtableSize, OptionsSerializer("HashtableSizeOptions"), TFLITE_NO_INDICES), |
| 901 | BuiltinOperator.REDUCE_ALL: (Op.ReduceAll, OptionsSerializer("ReducerOptions", ("keep_dims",)), TFLITE_NO_INDICES), |
| 902 | BuiltinOperator.CONV_3D_TRANSPOSE: ( |
| 903 | Op.Conv3DTranspose, |
| 904 | OptionsSerializer( |
| 905 | "Conv3DOptions", |
| 906 | ( |
| 907 | "dilation_d_factor", |
| 908 | "dilation_h_factor", |
| 909 | "dilation_w_factor", |
| 910 | fused_act, |
| 911 | padding, |
| 912 | "stride_d", |
| 913 | "stride_h", |
| 914 | "stride_w", |
| 915 | ), |
| 916 | ), |
| 917 | TFLITE_NO_INDICES, |
| 918 | ), |
Rickard Bolin | 2de898a | 2021-12-20 08:35:23 +0000 | [diff] [blame] | 919 | BuiltinOperator.VAR_HANDLE: ( |
| 920 | Op.VarHandle, |
Jonas Ohlsson | d857507 | 2022-03-30 10:30:25 +0200 | [diff] [blame] | 921 | OptionsSerializer( |
| 922 | "VarHandleOptions", |
| 923 | ( |
| 924 | "container", |
| 925 | "shared_name", |
| 926 | ), |
| 927 | ), |
Rickard Bolin | 2de898a | 2021-12-20 08:35:23 +0000 | [diff] [blame] | 928 | TFLITE_NO_INDICES, |
| 929 | ), |
| 930 | BuiltinOperator.READ_VARIABLE: (Op.ReadVariable, OptionsSerializer("ReadVariableOptions"), TFLITE_NO_INDICES), |
| 931 | BuiltinOperator.ASSIGN_VARIABLE: (Op.AssignVariable, OptionsSerializer("AssignVariableOptions"), TFLITE_NO_INDICES), |
| 932 | BuiltinOperator.BROADCAST_ARGS: (Op.BroadcastArgs, None, TFLITE_NO_INDICES), |
| 933 | BuiltinOperator.RANDOM_STANDARD_NORMAL: ( |
| 934 | Op.RandomStandardNormal, |
Jonas Ohlsson | d857507 | 2022-03-30 10:30:25 +0200 | [diff] [blame] | 935 | OptionsSerializer( |
| 936 | "RandomOptions", |
| 937 | ( |
| 938 | "seed", |
| 939 | "seed2", |
| 940 | ), |
| 941 | ), |
Rickard Bolin | 2de898a | 2021-12-20 08:35:23 +0000 | [diff] [blame] | 942 | TFLITE_NO_INDICES, |
| 943 | ), |
Tim Hall | b94bcd4 | 2021-09-07 14:24:51 +0100 | [diff] [blame] | 944 | BuiltinOperator.CUSTOM: (Op.Custom, CustomOptionsSerializer(), TFLITE_NO_INDICES), |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 945 | } |
| 946 | |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 947 | builtin_operator_inv_map = {v[0]: (k, v[1], v[2]) for k, v in builtin_operator_map.items()} |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 948 | |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 949 | builtin_operator_inv_map[Op.CustomNpuOp] = (BuiltinOperator.CUSTOM, CustomOptionsSerializer(), TFLITE_NO_INDICES) |
Michael McGeagh | 219ec07 | 2020-11-09 11:11:26 +0000 | [diff] [blame] | 950 | |
Michael McGeagh | 837dc1b | 2020-11-10 12:38:25 +0000 | [diff] [blame] | 951 | BUILTIN_OPERATOR_UNKNOWN = "UNKNOWN" |
| 952 | |
Tim Hall | a3fe665 | 2022-03-03 17:43:16 +0000 | [diff] [blame] | 953 | builtin_operator_name_map = {v: k for k, v in vars(BuiltinOperator).items()} |
Michael McGeagh | 837dc1b | 2020-11-10 12:38:25 +0000 | [diff] [blame] | 954 | |
Michael McGeagh | 219ec07 | 2020-11-09 11:11:26 +0000 | [diff] [blame] | 955 | |
Tim Hall | a3fe665 | 2022-03-03 17:43:16 +0000 | [diff] [blame] | 956 | def optype_to_builtintype(op_type: Op): |
Michael McGeagh | 219ec07 | 2020-11-09 11:11:26 +0000 | [diff] [blame] | 957 | if op_type in builtin_operator_inv_map: |
Tim Hall | a3fe665 | 2022-03-03 17:43:16 +0000 | [diff] [blame] | 958 | return builtin_operator_name_map[builtin_operator_inv_map[op_type][0]] |
Michael McGeagh | 219ec07 | 2020-11-09 11:11:26 +0000 | [diff] [blame] | 959 | else: |
Michael McGeagh | 837dc1b | 2020-11-10 12:38:25 +0000 | [diff] [blame] | 960 | return BUILTIN_OPERATOR_UNKNOWN |