diff --git a/code/components/tflite-lib/CMakeLists.txt b/code/components/tflite-lib/CMakeLists.txt deleted file mode 100644 index aaf56231..00000000 --- a/code/components/tflite-lib/CMakeLists.txt +++ /dev/null @@ -1,72 +0,0 @@ -## TODO: GLOB is not a good way to collect files. Use explicit file list instead - -cmake_minimum_required(VERSION 3.5) - -set(tflite_dir "${CMAKE_CURRENT_SOURCE_DIR}/tensorflow/lite") -set(tfmicro_dir "${tflite_dir}/micro") -set(tfmicro_frontend_dir "${tflite_dir}/experimental/microfrontend/lib") -set(tfmicro_kernels_dir "${tfmicro_dir}/kernels") - -file(GLOB srcs_micro - "${tfmicro_dir}/*.cc" - "${tfmicro_dir}/*.c") - -file(GLOB src_micro_frontend - "${tfmicro_frontend_dir}/*.c" - "${tfmicro_frontend_dir}/*.cc") -file(GLOB srcs_kernels - "${tfmicro_kernels_dir}/*.c" - "${tfmicro_kernels_dir}/*.cc") - -# remove sources which will be provided by esp_nn -list(REMOVE_ITEM srcs_kernels - "${tfmicro_kernels_dir}/add.cc" - "${tfmicro_kernels_dir}/conv.cc" - "${tfmicro_kernels_dir}/depthwise_conv.cc" - "${tfmicro_kernels_dir}/fully_connected.cc" - "${tfmicro_kernels_dir}/mul.cc" - "${tfmicro_kernels_dir}/pooling.cc" - "${tfmicro_kernels_dir}/softmax.cc") - -FILE(GLOB esp_nn_kernels - "${tfmicro_kernels_dir}/esp_nn/*.cc") - -set(lib_srcs - "${srcs_micro}" - "${srcs_kernels}" - "${esp_nn_kernels}" - "${src_micro_frontend}" - "${tflite_dir}/kernels/kernel_util.cc" - "${tflite_dir}/micro/memory_planner/greedy_memory_planner.cc" - "${tflite_dir}/micro/memory_planner/linear_memory_planner.cc" - "${tflite_dir}/micro/arena_allocator/non_persistent_arena_buffer_allocator.cc" - "${tflite_dir}/micro/arena_allocator/persistent_arena_buffer_allocator.cc" - "${tflite_dir}/micro/arena_allocator/recording_single_arena_buffer_allocator.cc" - "${tflite_dir}/micro/arena_allocator/single_arena_buffer_allocator.cc" - "${tflite_dir}/c/common.cc" - "${tflite_dir}/core/api/error_reporter.cc" - "${tflite_dir}/core/api/flatbuffer_conversions.cc" - "${tflite_dir}/core/api/op_resolver.cc" - "${tflite_dir}/core/api/tensor_utils.cc" - "${tflite_dir}/kernels/internal/quantization_util.cc" - "${tflite_dir}/schema/schema_utils.cc") - -idf_component_register( - SRCS "${lib_srcs}" - INCLUDE_DIRS "." "third_party/gemmlowp" - "third_party/flatbuffers/include" - "third_party/ruy" - "third_party/kissfft" - REQUIRES "esp-nn") - -# Reduce the level of paranoia to be able to compile TF sources -target_compile_options(${COMPONENT_LIB} PRIVATE - -Wno-maybe-uninitialized - -Wno-missing-field-initializers - -DESP_NN # enables ESP-NN optimizations by Espressif - -Wno-type-limits) - -target_compile_options(${COMPONENT_LIB} PRIVATE -fno-unwind-tables -ffunction-sections -fdata-sections -fmessage-length=0 -DTF_LITE_STATIC_MEMORY -DTF_LITE_DISABLE_X86_NEON -O3 -Wsign-compare -Wdouble-promotion -Wshadow -Wunused-variable -Wmissing-field-initializers -Wunused-function -Wswitch -Wvla -Wall -Wextra -Wstrict-aliasing -Wno-unused-parameter -Wno-nonnull) -target_compile_options(${COMPONENT_LIB} PRIVATE $<$: -std=c++11 -fno-rtti -fno-exceptions -fno-threadsafe-statics -fno-unwind-tables -ffunction-sections -fdata-sections -fmessage-length=0 -DTF_LITE_STATIC_MEMORY -DTF_LITE_DISABLE_X86_NEON -O3 -Werror -Wsign-compare -Wdouble-promotion -Wshadow -Wunused-variable -Wmissing-field-initializers -Wunused-function -Wswitch -Wvla -Wall -Wextra -Wstrict-aliasing -Wno-unused-parameter -Wno-return-type -Wno-strict-aliasing -std=gnu++14 >) -target_compile_options(${COMPONENT_LIB} INTERFACE $<$>:-DTF_LITE_STATIC_MEMORY>) -target_link_libraries(${COMPONENT_LIB} PRIVATE -lm) diff --git a/code/components/tflite-lib/tensorflow/lite/builtin_op_data.h b/code/components/tflite-lib/tensorflow/lite/builtin_op_data.h deleted file mode 100644 index b9d42845..00000000 --- a/code/components/tflite-lib/tensorflow/lite/builtin_op_data.h +++ /dev/null @@ -1,22 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -// Compatibility shim for new location of interface definitions. - -#ifndef TENSORFLOW_LITE_BUILTIN_OP_DATA_H_ -#define TENSORFLOW_LITE_BUILTIN_OP_DATA_H_ - -#include "tensorflow/lite/c/builtin_op_data.h" - -#endif // TENSORFLOW_LITE_BUILTIN_OP_DATA_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/builtin_ops.h b/code/components/tflite-lib/tensorflow/lite/builtin_ops.h deleted file mode 100644 index 01156c39..00000000 --- a/code/components/tflite-lib/tensorflow/lite/builtin_ops.h +++ /dev/null @@ -1,193 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_BUILTIN_OPS_H_ -#define TENSORFLOW_LITE_BUILTIN_OPS_H_ - -// DO NOT EDIT MANUALLY: This file is automatically generated by -// `schema/builtin_ops_header/generator.cc`. - -#ifdef __cplusplus -extern "C" { -#endif // __cplusplus - -// The enum for builtin operators. -// Note: CUSTOM, DELEGATE, and PLACEHOLDER_FOR_GREATER_OP_CODES are 3 special -// ops which are not real built-in ops. -typedef enum { - kTfLiteBuiltinAdd = 0, - kTfLiteBuiltinAveragePool2d = 1, - kTfLiteBuiltinConcatenation = 2, - kTfLiteBuiltinConv2d = 3, - kTfLiteBuiltinDepthwiseConv2d = 4, - kTfLiteBuiltinDepthToSpace = 5, - kTfLiteBuiltinDequantize = 6, - kTfLiteBuiltinEmbeddingLookup = 7, - kTfLiteBuiltinFloor = 8, - kTfLiteBuiltinFullyConnected = 9, - kTfLiteBuiltinHashtableLookup = 10, - kTfLiteBuiltinL2Normalization = 11, - kTfLiteBuiltinL2Pool2d = 12, - kTfLiteBuiltinLocalResponseNormalization = 13, - kTfLiteBuiltinLogistic = 14, - kTfLiteBuiltinLshProjection = 15, - kTfLiteBuiltinLstm = 16, - kTfLiteBuiltinMaxPool2d = 17, - kTfLiteBuiltinMul = 18, - kTfLiteBuiltinRelu = 19, - kTfLiteBuiltinReluN1To1 = 20, - kTfLiteBuiltinRelu6 = 21, - kTfLiteBuiltinReshape = 22, - kTfLiteBuiltinResizeBilinear = 23, - kTfLiteBuiltinRnn = 24, - kTfLiteBuiltinSoftmax = 25, - kTfLiteBuiltinSpaceToDepth = 26, - kTfLiteBuiltinSvdf = 27, - kTfLiteBuiltinTanh = 28, - kTfLiteBuiltinConcatEmbeddings = 29, - kTfLiteBuiltinSkipGram = 30, - kTfLiteBuiltinCall = 31, - kTfLiteBuiltinCustom = 32, - kTfLiteBuiltinEmbeddingLookupSparse = 33, - kTfLiteBuiltinPad = 34, - kTfLiteBuiltinUnidirectionalSequenceRnn = 35, - kTfLiteBuiltinGather = 36, - kTfLiteBuiltinBatchToSpaceNd = 37, - kTfLiteBuiltinSpaceToBatchNd = 38, - kTfLiteBuiltinTranspose = 39, - kTfLiteBuiltinMean = 40, - kTfLiteBuiltinSub = 41, - kTfLiteBuiltinDiv = 42, - kTfLiteBuiltinSqueeze = 43, - kTfLiteBuiltinUnidirectionalSequenceLstm = 44, - kTfLiteBuiltinStridedSlice = 45, - kTfLiteBuiltinBidirectionalSequenceRnn = 46, - kTfLiteBuiltinExp = 47, - kTfLiteBuiltinTopkV2 = 48, - kTfLiteBuiltinSplit = 49, - kTfLiteBuiltinLogSoftmax = 50, - kTfLiteBuiltinDelegate = 51, - kTfLiteBuiltinBidirectionalSequenceLstm = 52, - kTfLiteBuiltinCast = 53, - kTfLiteBuiltinPrelu = 54, - kTfLiteBuiltinMaximum = 55, - kTfLiteBuiltinArgMax = 56, - kTfLiteBuiltinMinimum = 57, - kTfLiteBuiltinLess = 58, - kTfLiteBuiltinNeg = 59, - kTfLiteBuiltinPadv2 = 60, - kTfLiteBuiltinGreater = 61, - kTfLiteBuiltinGreaterEqual = 62, - kTfLiteBuiltinLessEqual = 63, - kTfLiteBuiltinSelect = 64, - kTfLiteBuiltinSlice = 65, - kTfLiteBuiltinSin = 66, - kTfLiteBuiltinTransposeConv = 67, - kTfLiteBuiltinSparseToDense = 68, - kTfLiteBuiltinTile = 69, - kTfLiteBuiltinExpandDims = 70, - kTfLiteBuiltinEqual = 71, - kTfLiteBuiltinNotEqual = 72, - kTfLiteBuiltinLog = 73, - kTfLiteBuiltinSum = 74, - kTfLiteBuiltinSqrt = 75, - kTfLiteBuiltinRsqrt = 76, - kTfLiteBuiltinShape = 77, - kTfLiteBuiltinPow = 78, - kTfLiteBuiltinArgMin = 79, - kTfLiteBuiltinFakeQuant = 80, - kTfLiteBuiltinReduceProd = 81, - kTfLiteBuiltinReduceMax = 82, - kTfLiteBuiltinPack = 83, - kTfLiteBuiltinLogicalOr = 84, - kTfLiteBuiltinOneHot = 85, - kTfLiteBuiltinLogicalAnd = 86, - kTfLiteBuiltinLogicalNot = 87, - kTfLiteBuiltinUnpack = 88, - kTfLiteBuiltinReduceMin = 89, - kTfLiteBuiltinFloorDiv = 90, - kTfLiteBuiltinReduceAny = 91, - kTfLiteBuiltinSquare = 92, - kTfLiteBuiltinZerosLike = 93, - kTfLiteBuiltinFill = 94, - kTfLiteBuiltinFloorMod = 95, - kTfLiteBuiltinRange = 96, - kTfLiteBuiltinResizeNearestNeighbor = 97, - kTfLiteBuiltinLeakyRelu = 98, - kTfLiteBuiltinSquaredDifference = 99, - kTfLiteBuiltinMirrorPad = 100, - kTfLiteBuiltinAbs = 101, - kTfLiteBuiltinSplitV = 102, - kTfLiteBuiltinUnique = 103, - kTfLiteBuiltinCeil = 104, - kTfLiteBuiltinReverseV2 = 105, - kTfLiteBuiltinAddN = 106, - kTfLiteBuiltinGatherNd = 107, - kTfLiteBuiltinCos = 108, - kTfLiteBuiltinWhere = 109, - kTfLiteBuiltinRank = 110, - kTfLiteBuiltinElu = 111, - kTfLiteBuiltinReverseSequence = 112, - kTfLiteBuiltinMatrixDiag = 113, - kTfLiteBuiltinQuantize = 114, - kTfLiteBuiltinMatrixSetDiag = 115, - kTfLiteBuiltinRound = 116, - kTfLiteBuiltinHardSwish = 117, - kTfLiteBuiltinIf = 118, - kTfLiteBuiltinWhile = 119, - kTfLiteBuiltinNonMaxSuppressionV4 = 120, - kTfLiteBuiltinNonMaxSuppressionV5 = 121, - kTfLiteBuiltinScatterNd = 122, - kTfLiteBuiltinSelectV2 = 123, - kTfLiteBuiltinDensify = 124, - kTfLiteBuiltinSegmentSum = 125, - kTfLiteBuiltinBatchMatmul = 126, - kTfLiteBuiltinPlaceholderForGreaterOpCodes = 127, - kTfLiteBuiltinCumsum = 128, - kTfLiteBuiltinCallOnce = 129, - kTfLiteBuiltinBroadcastTo = 130, - kTfLiteBuiltinRfft2d = 131, - kTfLiteBuiltinConv3d = 132, - kTfLiteBuiltinImag = 133, - kTfLiteBuiltinReal = 134, - kTfLiteBuiltinComplexAbs = 135, - kTfLiteBuiltinHashtable = 136, - kTfLiteBuiltinHashtableFind = 137, - kTfLiteBuiltinHashtableImport = 138, - kTfLiteBuiltinHashtableSize = 139, - kTfLiteBuiltinReduceAll = 140, - kTfLiteBuiltinConv3dTranspose = 141, - kTfLiteBuiltinVarHandle = 142, - kTfLiteBuiltinReadVariable = 143, - kTfLiteBuiltinAssignVariable = 144, - kTfLiteBuiltinBroadcastArgs = 145, - kTfLiteBuiltinRandomStandardNormal = 146, - kTfLiteBuiltinBucketize = 147, - kTfLiteBuiltinRandomUniform = 148, - kTfLiteBuiltinMultinomial = 149, - kTfLiteBuiltinGelu = 150, - kTfLiteBuiltinDynamicUpdateSlice = 151, - kTfLiteBuiltinRelu0To1 = 152, - kTfLiteBuiltinUnsortedSegmentProd = 153, - kTfLiteBuiltinUnsortedSegmentMax = 154, - kTfLiteBuiltinUnsortedSegmentSum = 155, - kTfLiteBuiltinAtan2 = 156, - kTfLiteBuiltinUnsortedSegmentMin = 157, -} TfLiteBuiltinOperator; - -#ifdef __cplusplus -} // extern "C" -#endif // __cplusplus -#endif // TENSORFLOW_LITE_BUILTIN_OPS_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/c/builtin_op_data.h b/code/components/tflite-lib/tensorflow/lite/c/builtin_op_data.h deleted file mode 100644 index 7f160972..00000000 --- a/code/components/tflite-lib/tensorflow/lite/c/builtin_op_data.h +++ /dev/null @@ -1,525 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_C_BUILTIN_OP_DATA_H_ -#define TENSORFLOW_LITE_C_BUILTIN_OP_DATA_H_ - -#include - -#include "tensorflow/lite/c/common.h" - -#ifdef __cplusplus -extern "C" { -#endif // __cplusplus - -// TfLiteReshapeParams can't have dynamic data so we fix the maximum possible -// number of dimensions. -#define TFLITE_RESHAPE_PARAMS_MAX_DIMENSION_COUNT 8 - -// TODO(aselle): Consider using "if this then that" for testing. - -// Useful placeholder to put in otherwise empty structs to avoid size warnings. -typedef struct { - char dummy; -} EmptyStructPlaceholder; - -// IMPORTANT: All new members of structs must be added at the end to ensure -// backwards compatibility. - -// Possible padding types (for convolutions) -typedef enum { - kTfLitePaddingUnknown = 0, - kTfLitePaddingSame, - kTfLitePaddingValid, -} TfLitePadding; - -typedef enum { - kTfLiteMirrorPaddingUnknown = 0, - kTfLiteMirrorPaddingReflect, - kTfLiteMirrorPaddingSymmetric, -} TfLiteMirrorPaddingMode; - -// TODO(b/130259536): We should move this out of builtin_op_data. -typedef struct { - int width; - int height; - int width_offset; - int height_offset; -} TfLitePaddingValues; - -typedef struct { - TfLiteMirrorPaddingMode mode; -} TfLiteMirrorPaddingParams; - -// Possible fused activation functions. -typedef enum { - kTfLiteActNone = 0, - kTfLiteActRelu, - kTfLiteActReluN1To1, // min(max(-1, x), 1) - kTfLiteActRelu6, // min(max(0, x), 6) - kTfLiteActTanh, - kTfLiteActSignBit, - kTfLiteActSigmoid, -} TfLiteFusedActivation; - -typedef struct { - // Parameters for CONV_2D version 1. - TfLitePadding padding; - int stride_width; - int stride_height; - TfLiteFusedActivation activation; - - // Parameters for CONV_2D version 2. - // Note: Version 2 supports dilation values not equal to 1. - int dilation_width_factor; - int dilation_height_factor; -} TfLiteConvParams; - -typedef struct { - TfLitePadding padding; - int stride_width; - int stride_height; - int stride_depth; - int dilation_width_factor; - int dilation_height_factor; - int dilation_depth_factor; - TfLiteFusedActivation activation; -} TfLiteConv3DParams; - -typedef TfLiteConv3DParams TfLiteConv3DTransposeParams; - -typedef struct { - TfLitePadding padding; - int stride_width; - int stride_height; - int filter_width; - int filter_height; - TfLiteFusedActivation activation; - struct { - TfLitePaddingValues padding; - } computed; -} TfLitePoolParams; - -typedef struct { - // Parameters for DepthwiseConv version 1 or above. - TfLitePadding padding; - int stride_width; - int stride_height; - // `depth_multiplier` is redundant. It's used by CPU kernels in - // TensorFlow 2.0 or below, but ignored in versions above. - // - // The information can be deduced from the shape of input and the shape of - // weights. Since the TFLiteConverter toolchain doesn't support partially - // specified shapes, relying on `depth_multiplier` stops us from supporting - // graphs with dynamic shape tensors. - // - // Note: Some of the delegates (e.g. NNAPI, GPU) are still relying on this - // field. - int depth_multiplier; - TfLiteFusedActivation activation; - // Parameters for DepthwiseConv version 2 or above. - int dilation_width_factor; - int dilation_height_factor; -} TfLiteDepthwiseConvParams; - -typedef struct { - int rank; - TfLiteFusedActivation activation; - - // Parameter for SVDF version 4. - bool asymmetric_quantize_inputs; -} TfLiteSVDFParams; - -typedef struct { - TfLiteFusedActivation activation; - - // Parameter for RNN version 3. - bool asymmetric_quantize_inputs; -} TfLiteRNNParams; - -typedef struct { - bool time_major; - TfLiteFusedActivation activation; - - // Parameter for Sequence RNN version 3. - bool asymmetric_quantize_inputs; -} TfLiteSequenceRNNParams; - -typedef struct { - bool time_major; - TfLiteFusedActivation activation; - bool merge_outputs; - - // Parameter for Bidirectional RNN verison 3. - bool asymmetric_quantize_inputs; -} TfLiteBidirectionalSequenceRNNParams; - -typedef enum { - kTfLiteFullyConnectedWeightsFormatDefault = 0, - kTfLiteFullyConnectedWeightsFormatShuffled4x16Int8 = 1, -} TfLiteFullyConnectedWeightsFormat; - -typedef struct { - // Parameters for FullyConnected version 1 or above. - TfLiteFusedActivation activation; - - // Parameters for FullyConnected version 2 or above. - TfLiteFullyConnectedWeightsFormat weights_format; - - // Parameters for FullyConnected version 5 or above. - // If set to true, then the number of dimensions in the input and the output - // tensors are the same. Furthermore, all but the last dimension of the input - // and output shapes will be equal. - bool keep_num_dims; - - // Parameters for FullyConnected version 7 or above. - // If set to true and the weights are quantized, then non constant inputs - // are quantized at evaluation time with asymmetric quantization. - bool asymmetric_quantize_inputs; -} TfLiteFullyConnectedParams; - -typedef enum { - kTfLiteLshProjectionUnknown = 0, - kTfLiteLshProjectionSparse = 1, - kTfLiteLshProjectionDense = 2, -} TfLiteLSHProjectionType; - -typedef struct { - TfLiteLSHProjectionType type; -} TfLiteLSHProjectionParams; - -typedef struct { - float beta; -} TfLiteSoftmaxParams; - -typedef struct { - int axis; - TfLiteFusedActivation activation; -} TfLiteConcatenationParams; - -typedef struct { - TfLiteFusedActivation activation; - // Parameter added for the version 4. - bool pot_scale_int16; -} TfLiteAddParams; - -typedef struct { - EmptyStructPlaceholder placeholder; -} TfLiteSpaceToBatchNDParams; - -typedef struct { - EmptyStructPlaceholder placeholder; -} TfLiteBatchToSpaceNDParams; - -typedef struct { - bool adj_x; - bool adj_y; - // Parameters for BatchMatMul version 4 or above. - // If set to true and the weights are quantized, then non constant inputs - // are quantized at evaluation time with asymmetric quantization. - bool asymmetric_quantize_inputs; -} TfLiteBatchMatMulParams; - -typedef struct { - TfLiteFusedActivation activation; -} TfLiteMulParams; - -typedef struct { - TfLiteFusedActivation activation; - // Parameter added for the version 5. - bool pot_scale_int16; -} TfLiteSubParams; - -typedef struct { - TfLiteFusedActivation activation; -} TfLiteDivParams; - -typedef struct { - TfLiteFusedActivation activation; -} TfLiteL2NormParams; - -typedef struct { - int radius; - float bias; - float alpha; - float beta; -} TfLiteLocalResponseNormParams; - -typedef enum { - kTfLiteLSTMFullKernel = 0, - kTfLiteLSTMBasicKernel -} TfLiteLSTMKernelType; - -typedef struct { - // Parameters for LSTM version 1. - TfLiteFusedActivation activation; - float cell_clip; - float proj_clip; - - // Parameters for LSTM version 2. - // kTfLiteLSTMBasicKernel is only supported in version 2 or above. - TfLiteLSTMKernelType kernel_type; - - // Parameters for LSTM version 4. - bool asymmetric_quantize_inputs; -} TfLiteLSTMParams; - -typedef struct { - // Parameters needed for the underlying LSTM. - TfLiteFusedActivation activation; - float cell_clip; - float proj_clip; - - // If set to true then the first dimension is time, otherwise batch. - bool time_major; - - // Parameter for unidirectional sequence RNN version 3. - bool asymmetric_quantize_inputs; -} TfLiteUnidirectionalSequenceLSTMParams; - -typedef struct { - // Parameters supported by version 1: - // Parameters inherited for the LSTM kernel. - TfLiteFusedActivation activation; - float cell_clip; - float proj_clip; - - // If true, store the outputs of both directions in the first output. - bool merge_outputs; - - // Parameters supported by version 2: - // If set to true then the first dimension is time, otherwise batch. - bool time_major; - - // Parameters supported by version 4: - // If set to true, then hybrid ops use asymmetric quantization for inputs. - bool asymmetric_quantize_inputs; -} TfLiteBidirectionalSequenceLSTMParams; - -typedef struct { - bool align_corners; - // half_pixel_centers assumes pixels are of half the actual dimensions, and - // yields more accurate resizes. Corresponds to the same argument for the - // original TensorFlow op in TF2.0. - bool half_pixel_centers; -} TfLiteResizeBilinearParams; - -typedef struct { - bool align_corners; - bool half_pixel_centers; -} TfLiteResizeNearestNeighborParams; - -typedef struct { - EmptyStructPlaceholder placeholder; -} TfLitePadParams; - -typedef struct { - EmptyStructPlaceholder placeholder; -} TfLitePadV2Params; - -typedef struct { - // These fields are only used in old models for backward compatibility. - // In the current implementation, we use the 2nd input of the op as the shape, - // and these fields are unused. - int shape[TFLITE_RESHAPE_PARAMS_MAX_DIMENSION_COUNT]; - int num_dimensions; -} TfLiteReshapeParams; - -typedef struct { - int ngram_size; - int max_skip_size; - bool include_all_ngrams; -} TfLiteSkipGramParams; - -typedef struct { - int block_size; -} TfLiteSpaceToDepthParams; - -typedef struct { - int block_size; -} TfLiteDepthToSpaceParams; - -typedef struct { - TfLiteType in_data_type; - TfLiteType out_data_type; -} TfLiteCastParams; - -typedef enum { - kTfLiteCombinerTypeSum = 0, - kTfLiteCombinerTypeMean = 1, - kTfLiteCombinerTypeSqrtn = 2, -} TfLiteCombinerType; - -typedef struct { - TfLiteCombinerType combiner; -} TfLiteEmbeddingLookupSparseParams; - -typedef struct { - int axis; - int batch_dims; -} TfLiteGatherParams; - -typedef struct { - EmptyStructPlaceholder placeholder; -} TfLiteTransposeParams; - -typedef struct { - bool keep_dims; -} TfLiteReducerParams; - -typedef struct { - int num_splits; -} TfLiteSplitParams; - -typedef struct { - int num_splits; -} TfLiteSplitVParams; - -typedef struct { - // TODO(ahentz): We can't have dynamic data in this struct, at least not yet. - // For now we will fix the maximum possible number of dimensions. - int squeeze_dims[8]; - int num_squeeze_dims; -} TfLiteSqueezeParams; - -typedef struct { - int begin_mask; - int end_mask; - int ellipsis_mask; - int new_axis_mask; - int shrink_axis_mask; -} TfLiteStridedSliceParams; - -typedef struct { - TfLiteType output_type; -} TfLiteArgMaxParams; - -typedef struct { - TfLiteType output_type; -} TfLiteArgMinParams; - -typedef struct { - TfLitePadding padding; - int stride_width; - int stride_height; -} TfLiteTransposeConvParams; - -typedef struct { - bool validate_indices; -} TfLiteSparseToDenseParams; - -typedef struct { - TfLiteType out_type; -} TfLiteShapeParams; - -typedef struct { - EmptyStructPlaceholder placeholder; -} TfLiteRankParams; - -typedef struct { - // Parameters supported by version 1: - float min; - float max; - int num_bits; - - // Parameters supported by version 2: - bool narrow_range; -} TfLiteFakeQuantParams; - -typedef struct { - int values_count; - int axis; -} TfLitePackParams; - -typedef struct { - int axis; -} TfLiteOneHotParams; - -typedef struct { - int num; - int axis; -} TfLiteUnpackParams; - -typedef struct { - float alpha; -} TfLiteLeakyReluParams; - -typedef struct { - TfLiteType index_out_type; -} TfLiteUniqueParams; - -typedef struct { - int seq_dim; - int batch_dim; -} TfLiteReverseSequenceParams; - -typedef struct { - EmptyStructPlaceholder placeholder; -} TfLiteMatrixDiagParams; - -typedef struct { - EmptyStructPlaceholder placeholder; -} TfLiteMatrixSetDiagParams; - -typedef struct { - int then_subgraph_index; - int else_subgraph_index; -} TfLiteIfParams; - -typedef struct { - int cond_subgraph_index; - int body_subgraph_index; -} TfLiteWhileParams; - -typedef struct { - bool exclusive; - bool reverse; -} TfLiteCumsumParams; - -typedef struct { - int init_subgraph_index; -} TfLiteCallOnceParams; - -typedef struct { - int table_id; - TfLiteType key_dtype; - TfLiteType value_dtype; -} TfLiteHashtableParams; - -typedef struct { - const char* container; - const char* shared_name; -} TfLiteVarHandleParams; - -typedef struct { - int seed; - int seed2; -} TfLiteRandomParams; - -typedef struct { - int num_boundaries; - // This points to the memory stored in the model (flatbuffer), - // and is not owned. - const float* boundaries; -} TfLiteBucketizeParams; - -typedef struct { - bool approximate; -} TfLiteGeluParams; - -#ifdef __cplusplus -} // extern "C" -#endif // __cplusplus - -#endif // TENSORFLOW_LITE_C_BUILTIN_OP_DATA_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/c/c_api_types.h b/code/components/tflite-lib/tensorflow/lite/c/c_api_types.h deleted file mode 100644 index d947213b..00000000 --- a/code/components/tflite-lib/tensorflow/lite/c/c_api_types.h +++ /dev/null @@ -1,130 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -// This file declares types used by the pure C inference API defined in c_api.h, -// some of which are also used in the C++ and C kernel and interpreter APIs. - -#ifndef TENSORFLOW_LITE_C_C_API_TYPES_H_ -#define TENSORFLOW_LITE_C_C_API_TYPES_H_ - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -// Define TFL_CAPI_EXPORT macro to export a function properly with a shared -// library. -#ifdef SWIG -#define TFL_CAPI_EXPORT -#elif defined(TFL_STATIC_LIBRARY_BUILD) -#define TFL_CAPI_EXPORT -#else // not definded TFL_STATIC_LIBRARY_BUILD -#if defined(_WIN32) -#ifdef TFL_COMPILE_LIBRARY -#define TFL_CAPI_EXPORT __declspec(dllexport) -#else -#define TFL_CAPI_EXPORT __declspec(dllimport) -#endif // TFL_COMPILE_LIBRARY -#else -#define TFL_CAPI_EXPORT __attribute__((visibility("default"))) -#endif // _WIN32 -#endif // SWIG - -// Note that new error status values may be added in future in order to -// indicate more fine-grained internal states, therefore, applications should -// not rely on status values being members of the enum. -typedef enum TfLiteStatus { - kTfLiteOk = 0, - - // Generally referring to an error in the runtime (i.e. interpreter) - kTfLiteError = 1, - - // Generally referring to an error from a TfLiteDelegate itself. - kTfLiteDelegateError = 2, - - // Generally referring to an error in applying a delegate due to - // incompatibility between runtime and delegate, e.g., this error is returned - // when trying to apply a TF Lite delegate onto a model graph that's already - // immutable. - kTfLiteApplicationError = 3, - - // Generally referring to serialized delegate data not being found. - // See tflite::delegates::Serialization. - kTfLiteDelegateDataNotFound = 4, - - // Generally referring to data-writing issues in delegate serialization. - // See tflite::delegates::Serialization. - kTfLiteDelegateDataWriteError = 5, - - // Generally referring to data-reading issues in delegate serialization. - // See tflite::delegates::Serialization. - kTfLiteDelegateDataReadError = 6, - - // Generally referring to issues when the TF Lite model has ops that cannot be - // resolved at runtime. This could happen when the specific op is not - // registered or built with the TF Lite framework. - kTfLiteUnresolvedOps = 7, -} TfLiteStatus; - -// Types supported by tensor -typedef enum { - kTfLiteNoType = 0, - kTfLiteFloat32 = 1, - kTfLiteInt32 = 2, - kTfLiteUInt8 = 3, - kTfLiteInt64 = 4, - kTfLiteString = 5, - kTfLiteBool = 6, - kTfLiteInt16 = 7, - kTfLiteComplex64 = 8, - kTfLiteInt8 = 9, - kTfLiteFloat16 = 10, - kTfLiteFloat64 = 11, - kTfLiteComplex128 = 12, - kTfLiteUInt64 = 13, - kTfLiteResource = 14, - kTfLiteVariant = 15, - kTfLiteUInt32 = 16, - kTfLiteUInt16 = 17, -} TfLiteType; - -// Legacy. Will be deprecated in favor of TfLiteAffineQuantization. -// If per-layer quantization is specified this field will still be populated in -// addition to TfLiteAffineQuantization. -// Parameters for asymmetric quantization. Quantized values can be converted -// back to float using: -// real_value = scale * (quantized_value - zero_point) -typedef struct TfLiteQuantizationParams { - float scale; - int32_t zero_point; -} TfLiteQuantizationParams; - -// -------------------------------------------------------------------------- -// Opaque types used by c_api.h, c_api_opaque.h and common.h. - -// TfLiteOpaqueContext is an opaque version of TfLiteContext; -typedef struct TfLiteOpaqueContext TfLiteOpaqueContext; - -// TfLiteOpaqueNode is an opaque version of TfLiteNode; -typedef struct TfLiteOpaqueNode TfLiteOpaqueNode; - -// TfLiteOpaqueTensor is an opaque version of TfLiteTensor; -typedef struct TfLiteOpaqueTensor TfLiteOpaqueTensor; - -#ifdef __cplusplus -} // extern C -#endif -#endif // TENSORFLOW_LITE_C_C_API_TYPES_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/c/common.cc b/code/components/tflite-lib/tensorflow/lite/c/common.cc deleted file mode 100644 index ae5c44b5..00000000 --- a/code/components/tflite-lib/tensorflow/lite/c/common.cc +++ /dev/null @@ -1,286 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/c/common.h" - -#include "tensorflow/lite/c/c_api_types.h" -#ifdef TF_LITE_TENSORFLOW_PROFILER -#include "tensorflow/lite/tensorflow_profiler_logger.h" -#endif - -#ifndef TF_LITE_STATIC_MEMORY -#include -#include -#endif // TF_LITE_STATIC_MEMORY - -extern "C" { - -size_t TfLiteIntArrayGetSizeInBytes(int size) { - static TfLiteIntArray dummy; - - size_t computed_size = sizeof(dummy) + sizeof(dummy.data[0]) * size; -#if defined(_MSC_VER) - // Context for why this is needed is in http://b/189926408#comment21 - computed_size -= sizeof(dummy.data[0]); -#endif - return computed_size; -} - -int TfLiteIntArrayEqual(const TfLiteIntArray* a, const TfLiteIntArray* b) { - if (a == b) return 1; - if (a == nullptr || b == nullptr) return 0; - return TfLiteIntArrayEqualsArray(a, b->size, b->data); -} - -int TfLiteIntArrayEqualsArray(const TfLiteIntArray* a, int b_size, - const int b_data[]) { - if (a == nullptr) return (b_size == 0); - if (a->size != b_size) return 0; - int i = 0; - for (; i < a->size; i++) - if (a->data[i] != b_data[i]) return 0; - return 1; -} - -#ifndef TF_LITE_STATIC_MEMORY - -TfLiteIntArray* TfLiteIntArrayCreate(int size) { - size_t alloc_size = TfLiteIntArrayGetSizeInBytes(size); - if (alloc_size <= 0) return nullptr; - TfLiteIntArray* ret = (TfLiteIntArray*)malloc(alloc_size); - if (!ret) return ret; - ret->size = size; - return ret; -} - -TfLiteIntArray* TfLiteIntArrayCopy(const TfLiteIntArray* src) { - if (!src) return nullptr; - TfLiteIntArray* ret = TfLiteIntArrayCreate(src->size); - if (ret) { - memcpy(ret->data, src->data, src->size * sizeof(int)); - } - return ret; -} - -void TfLiteIntArrayFree(TfLiteIntArray* a) { free(a); } - -#endif // TF_LITE_STATIC_MEMORY - -int TfLiteFloatArrayGetSizeInBytes(int size) { - static TfLiteFloatArray dummy; - - int computed_size = sizeof(dummy) + sizeof(dummy.data[0]) * size; -#if defined(_MSC_VER) - // Context for why this is needed is in http://b/189926408#comment21 - computed_size -= sizeof(dummy.data[0]); -#endif - return computed_size; -} - -#ifndef TF_LITE_STATIC_MEMORY - -TfLiteFloatArray* TfLiteFloatArrayCreate(int size) { - TfLiteFloatArray* ret = - (TfLiteFloatArray*)malloc(TfLiteFloatArrayGetSizeInBytes(size)); - ret->size = size; - return ret; -} - -void TfLiteFloatArrayFree(TfLiteFloatArray* a) { free(a); } - -void TfLiteTensorDataFree(TfLiteTensor* t) { - if (t->allocation_type == kTfLiteDynamic || - t->allocation_type == kTfLitePersistentRo) { - if (t->data.raw) { -#ifdef TF_LITE_TENSORFLOW_PROFILER - tflite::OnTfLiteTensorDealloc(t); -#endif - free(t->data.raw); - } - } - t->data.raw = nullptr; -} - -void TfLiteQuantizationFree(TfLiteQuantization* quantization) { - if (quantization->type == kTfLiteAffineQuantization) { - TfLiteAffineQuantization* q_params = - (TfLiteAffineQuantization*)(quantization->params); - if (q_params->scale) { - TfLiteFloatArrayFree(q_params->scale); - q_params->scale = nullptr; - } - if (q_params->zero_point) { - TfLiteIntArrayFree(q_params->zero_point); - q_params->zero_point = nullptr; - } - free(q_params); - } - quantization->params = nullptr; - quantization->type = kTfLiteNoQuantization; -} - -void TfLiteSparsityFree(TfLiteSparsity* sparsity) { - if (sparsity == nullptr) { - return; - } - - if (sparsity->traversal_order) { - TfLiteIntArrayFree(sparsity->traversal_order); - sparsity->traversal_order = nullptr; - } - - if (sparsity->block_map) { - TfLiteIntArrayFree(sparsity->block_map); - sparsity->block_map = nullptr; - } - - if (sparsity->dim_metadata) { - int i = 0; - for (; i < sparsity->dim_metadata_size; i++) { - TfLiteDimensionMetadata metadata = sparsity->dim_metadata[i]; - if (metadata.format == kTfLiteDimSparseCSR) { - TfLiteIntArrayFree(metadata.array_segments); - metadata.array_segments = nullptr; - TfLiteIntArrayFree(metadata.array_indices); - metadata.array_indices = nullptr; - } - } - free(sparsity->dim_metadata); - sparsity->dim_metadata = nullptr; - } - - free(sparsity); -} - -void TfLiteTensorFree(TfLiteTensor* t) { - TfLiteTensorDataFree(t); - if (t->dims) TfLiteIntArrayFree(t->dims); - t->dims = nullptr; - - if (t->dims_signature) { - TfLiteIntArrayFree((TfLiteIntArray*)t->dims_signature); - } - t->dims_signature = nullptr; - - TfLiteQuantizationFree(&t->quantization); - TfLiteSparsityFree(t->sparsity); - t->sparsity = nullptr; -} - -void TfLiteTensorReset(TfLiteType type, const char* name, TfLiteIntArray* dims, - TfLiteQuantizationParams quantization, char* buffer, - size_t size, TfLiteAllocationType allocation_type, - const void* allocation, bool is_variable, - TfLiteTensor* tensor) { - TfLiteTensorFree(tensor); - tensor->type = type; - tensor->name = name; - tensor->dims = dims; - tensor->params = quantization; - tensor->data.raw = buffer; - tensor->bytes = size; - tensor->allocation_type = allocation_type; - tensor->allocation = allocation; - tensor->is_variable = is_variable; - - tensor->quantization.type = kTfLiteNoQuantization; - tensor->quantization.params = nullptr; -} - -TfLiteStatus TfLiteTensorCopy(const TfLiteTensor* src, TfLiteTensor* dst) { - if (!src || !dst) return kTfLiteOk; - if (src->bytes != dst->bytes) return kTfLiteError; - if (src == dst) return kTfLiteOk; - - dst->type = src->type; - if (dst->dims) TfLiteIntArrayFree(dst->dims); - dst->dims = TfLiteIntArrayCopy(src->dims); - memcpy(dst->data.raw, src->data.raw, src->bytes); - dst->buffer_handle = src->buffer_handle; - dst->data_is_stale = src->data_is_stale; - dst->delegate = src->delegate; - - return kTfLiteOk; -} - -void TfLiteTensorRealloc(size_t num_bytes, TfLiteTensor* tensor) { - if (tensor->allocation_type != kTfLiteDynamic && - tensor->allocation_type != kTfLitePersistentRo) { - return; - } - // TODO(b/145340303): Tensor data should be aligned. - if (!tensor->data.raw) { - tensor->data.raw = (char*)malloc(num_bytes); -#ifdef TF_LITE_TENSORFLOW_PROFILER - tflite::OnTfLiteTensorAlloc(tensor, num_bytes); -#endif - } else if (num_bytes > tensor->bytes) { -#ifdef TF_LITE_TENSORFLOW_PROFILER - tflite::OnTfLiteTensorDealloc(tensor); -#endif - tensor->data.raw = (char*)realloc(tensor->data.raw, num_bytes); -#ifdef TF_LITE_TENSORFLOW_PROFILER - tflite::OnTfLiteTensorAlloc(tensor, num_bytes); -#endif - } - tensor->bytes = num_bytes; -} -#endif // TF_LITE_STATIC_MEMORY - -const char* TfLiteTypeGetName(TfLiteType type) { - switch (type) { - case kTfLiteNoType: - return "NOTYPE"; - case kTfLiteFloat32: - return "FLOAT32"; - case kTfLiteUInt16: - return "UINT16"; - case kTfLiteInt16: - return "INT16"; - case kTfLiteInt32: - return "INT32"; - case kTfLiteUInt32: - return "UINT32"; - case kTfLiteUInt8: - return "UINT8"; - case kTfLiteInt8: - return "INT8"; - case kTfLiteInt64: - return "INT64"; - case kTfLiteUInt64: - return "UINT64"; - case kTfLiteBool: - return "BOOL"; - case kTfLiteComplex64: - return "COMPLEX64"; - case kTfLiteComplex128: - return "COMPLEX128"; - case kTfLiteString: - return "STRING"; - case kTfLiteFloat16: - return "FLOAT16"; - case kTfLiteFloat64: - return "FLOAT64"; - case kTfLiteResource: - return "RESOURCE"; - case kTfLiteVariant: - return "VARIANT"; - } - return "Unknown type"; -} - -TfLiteDelegate TfLiteDelegateCreate() { return TfLiteDelegate{}; } - -} // extern "C" diff --git a/code/components/tflite-lib/tensorflow/lite/c/common.h b/code/components/tflite-lib/tensorflow/lite/c/common.h deleted file mode 100644 index cc856f9a..00000000 --- a/code/components/tflite-lib/tensorflow/lite/c/common.h +++ /dev/null @@ -1,1022 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -// This file defines common C types and APIs for implementing operations, -// delegates and other constructs in TensorFlow Lite. The actual operations and -// delegates can be defined using C++, but the interface between the interpreter -// and the operations are C. -// -// Summary of abstractions -// TF_LITE_ENSURE - Self-sufficient error checking -// TfLiteStatus - Status reporting -// TfLiteIntArray - stores tensor shapes (dims), -// TfLiteContext - allows an op to access the tensors -// TfLiteTensor - tensor (a multidimensional array) -// TfLiteNode - a single node or operation -// TfLiteRegistration - the implementation of a conceptual operation. -// TfLiteDelegate - allows delegation of nodes to alternative backends. -// -// Some abstractions in this file are created and managed by Interpreter. -// -// NOTE: The order of values in these structs are "semi-ABI stable". New values -// should be added only to the end of structs and never reordered. - -#ifndef TENSORFLOW_LITE_C_COMMON_H_ -#define TENSORFLOW_LITE_C_COMMON_H_ - -#include -#include -#include - -#include "tensorflow/lite/c/c_api_types.h" // IWYU pragma: export - -#ifdef __cplusplus -extern "C" { -#endif // __cplusplus - -// The list of external context types known to TF Lite. This list exists solely -// to avoid conflicts and to ensure ops can share the external contexts they -// need. Access to the external contexts is controlled by one of the -// corresponding support files. -typedef enum TfLiteExternalContextType { - kTfLiteEigenContext = 0, // include eigen_support.h to use. - kTfLiteGemmLowpContext = 1, // include gemm_support.h to use. - kTfLiteEdgeTpuContext = 2, // Placeholder for Edge TPU support. - kTfLiteCpuBackendContext = 3, // include cpu_backend_context.h to use. - kTfLiteMaxExternalContexts = 4 -} TfLiteExternalContextType; - -// Forward declare so dependent structs and methods can reference these types -// prior to the struct definitions. -struct TfLiteContext; -struct TfLiteDelegate; -struct TfLiteRegistration; - -// An external context is a collection of information unrelated to the TF Lite -// framework, but useful to a subset of the ops. TF Lite knows very little -// about the actual contexts, but it keeps a list of them, and is able to -// refresh them if configurations like the number of recommended threads -// change. -typedef struct TfLiteExternalContext { - TfLiteExternalContextType type; - TfLiteStatus (*Refresh)(struct TfLiteContext* context); -} TfLiteExternalContext; - -#define kTfLiteOptionalTensor (-1) - -// Fixed size list of integers. Used for dimensions and inputs/outputs tensor -// indices -typedef struct TfLiteIntArray { - int size; - -#if defined(_MSC_VER) - // Context for why this is needed is in http://b/189926408#comment21 - int data[1]; -#elif (!defined(__clang__) && defined(__GNUC__) && __GNUC__ == 6 && \ - __GNUC_MINOR__ >= 1) || \ - defined(HEXAGON) || \ - (defined(__clang__) && __clang_major__ == 7 && __clang_minor__ == 1) - // gcc 6.1+ have a bug where flexible members aren't properly handled - // https://github.com/google/re2/commit/b94b7cd42e9f02673cd748c1ac1d16db4052514c - int data[0]; -#else - int data[]; -#endif -} TfLiteIntArray; - -// Given the size (number of elements) in a TfLiteIntArray, calculate its size -// in bytes. -size_t TfLiteIntArrayGetSizeInBytes(int size); - -#ifndef TF_LITE_STATIC_MEMORY -// Create a array of a given `size` (uninitialized entries). -// This returns a pointer, that you must free using TfLiteIntArrayFree(). -TfLiteIntArray* TfLiteIntArrayCreate(int size); -#endif - -// Check if two intarrays are equal. Returns 1 if they are equal, 0 otherwise. -int TfLiteIntArrayEqual(const TfLiteIntArray* a, const TfLiteIntArray* b); - -// Check if an intarray equals an array. Returns 1 if equals, 0 otherwise. -int TfLiteIntArrayEqualsArray(const TfLiteIntArray* a, int b_size, - const int b_data[]); - -#ifndef TF_LITE_STATIC_MEMORY -// Create a copy of an array passed as `src`. -// You are expected to free memory with TfLiteIntArrayFree -TfLiteIntArray* TfLiteIntArrayCopy(const TfLiteIntArray* src); - -// Free memory of array `a`. -void TfLiteIntArrayFree(TfLiteIntArray* a); -#endif // TF_LITE_STATIC_MEMORY - -// Fixed size list of floats. Used for per-channel quantization. -typedef struct TfLiteFloatArray { - int size; -#if defined(_MSC_VER) - // Context for why this is needed is in http://b/189926408#comment21 - float data[1]; -#elif (!defined(__clang__) && defined(__GNUC__) && __GNUC__ == 6 && \ - __GNUC_MINOR__ >= 1) || \ - defined(HEXAGON) || \ - (defined(__clang__) && __clang_major__ == 7 && __clang_minor__ == 1) - // gcc 6.1+ have a bug where flexible members aren't properly handled - // https://github.com/google/re2/commit/b94b7cd42e9f02673cd748c1ac1d16db4052514c - float data[0]; -#else - float data[]; -#endif -} TfLiteFloatArray; - -// Given the size (number of elements) in a TfLiteFloatArray, calculate its size -// in bytes. -int TfLiteFloatArrayGetSizeInBytes(int size); - -#ifndef TF_LITE_STATIC_MEMORY -// Create a array of a given `size` (uninitialized entries). -// This returns a pointer, that you must free using TfLiteFloatArrayFree(). -TfLiteFloatArray* TfLiteFloatArrayCreate(int size); - -// Free memory of array `a`. -void TfLiteFloatArrayFree(TfLiteFloatArray* a); -#endif // TF_LITE_STATIC_MEMORY - -// Since we must not depend on any libraries, define a minimal subset of -// error macros while avoiding names that have pre-conceived meanings like -// assert and check. - -// Try to make all reporting calls through TF_LITE_KERNEL_LOG rather than -// calling the context->ReportError function directly, so that message strings -// can be stripped out if the binary size needs to be severely optimized. -#ifndef TF_LITE_STRIP_ERROR_STRINGS -#define TF_LITE_KERNEL_LOG(context, ...) \ - do { \ - (context)->ReportError((context), __VA_ARGS__); \ - } while (false) - -#define TF_LITE_MAYBE_KERNEL_LOG(context, ...) \ - do { \ - if ((context) != nullptr) { \ - (context)->ReportError((context), __VA_ARGS__); \ - } \ - } while (false) -#else // TF_LITE_STRIP_ERROR_STRINGS -#define ARGS_UNUSED(...) (void)sizeof(#__VA_ARGS__) -#define TF_LITE_KERNEL_LOG(context, ...) ARGS_UNUSED(__VA_ARGS__) -#define TF_LITE_MAYBE_KERNEL_LOG(context, ...) ARGS_UNUSED(__VA_ARGS__) -#endif // TF_LITE_STRIP_ERROR_STRINGS - -// Check whether value is true, and if not return kTfLiteError from -// the current function (and report the error string msg). -#define TF_LITE_ENSURE_MSG(context, value, msg) \ - do { \ - if (!(value)) { \ - TF_LITE_KERNEL_LOG((context), __FILE__ " " msg); \ - return kTfLiteError; \ - } \ - } while (0) - -// Check whether the value `a` is true, and if not return kTfLiteError from -// the current function, while also reporting the location of the error. -#define TF_LITE_ENSURE(context, a) \ - do { \ - if (!(a)) { \ - TF_LITE_KERNEL_LOG((context), "%s:%d %s was not true.", __FILE__, \ - __LINE__, #a); \ - return kTfLiteError; \ - } \ - } while (0) - -#define TF_LITE_ENSURE_STATUS(a) \ - do { \ - const TfLiteStatus s = (a); \ - if (s != kTfLiteOk) { \ - return s; \ - } \ - } while (0) - -// Check whether the value `a == b` is true, and if not return kTfLiteError from -// the current function, while also reporting the location of the error. -// `a` and `b` may be evaluated more than once, so no side effects or -// extremely expensive computations should be done. -// NOTE: Use TF_LITE_ENSURE_TYPES_EQ if comparing TfLiteTypes. -#define TF_LITE_ENSURE_EQ(context, a, b) \ - do { \ - if ((a) != (b)) { \ - TF_LITE_KERNEL_LOG((context), "%s:%d %s != %s (%d != %d)", __FILE__, \ - __LINE__, #a, #b, (a), (b)); \ - return kTfLiteError; \ - } \ - } while (0) - -#define TF_LITE_ENSURE_TYPES_EQ(context, a, b) \ - do { \ - if ((a) != (b)) { \ - TF_LITE_KERNEL_LOG((context), "%s:%d %s != %s (%s != %s)", __FILE__, \ - __LINE__, #a, #b, TfLiteTypeGetName(a), \ - TfLiteTypeGetName(b)); \ - return kTfLiteError; \ - } \ - } while (0) - -#define TF_LITE_ENSURE_NEAR(context, a, b, epsilon) \ - do { \ - auto delta = ((a) > (b)) ? ((a) - (b)) : ((b) - (a)); \ - if (delta > epsilon) { \ - TF_LITE_KERNEL_LOG((context), "%s:%d %s not near %s (%f != %f)", \ - __FILE__, __LINE__, #a, #b, static_cast(a), \ - static_cast(b)); \ - return kTfLiteError; \ - } \ - } while (0) - -#define TF_LITE_ENSURE_OK(context, status) \ - do { \ - const TfLiteStatus s = (status); \ - if ((s) != kTfLiteOk) { \ - return s; \ - } \ - } while (0) - -// Single-precision complex data type compatible with the C99 definition. -typedef struct TfLiteComplex64 { - float re, im; // real and imaginary parts, respectively. -} TfLiteComplex64; - -// Double-precision complex data type compatible with the C99 definition. -typedef struct TfLiteComplex128 { - double re, im; // real and imaginary parts, respectively. -} TfLiteComplex128; - -// Half precision data type compatible with the C99 definition. -typedef struct TfLiteFloat16 { - uint16_t data; -} TfLiteFloat16; - -// Return the name of a given type, for error reporting purposes. -const char* TfLiteTypeGetName(TfLiteType type); - -// SupportedQuantizationTypes. -typedef enum TfLiteQuantizationType { - // No quantization. - kTfLiteNoQuantization = 0, - // Affine quantization (with support for per-channel quantization). - // Corresponds to TfLiteAffineQuantization. - kTfLiteAffineQuantization = 1, -} TfLiteQuantizationType; - -// Structure specifying the quantization used by the tensor, if-any. -typedef struct TfLiteQuantization { - // The type of quantization held by params. - TfLiteQuantizationType type; - // Holds an optional reference to a quantization param structure. The actual - // type depends on the value of the `type` field (see the comment there for - // the values and corresponding types). - void* params; -} TfLiteQuantization; - -// Parameters for asymmetric quantization across a dimension (i.e per output -// channel quantization). -// quantized_dimension specifies which dimension the scales and zero_points -// correspond to. -// For a particular value in quantized_dimension, quantized values can be -// converted back to float using: -// real_value = scale * (quantized_value - zero_point) -typedef struct TfLiteAffineQuantization { - TfLiteFloatArray* scale; - TfLiteIntArray* zero_point; - int32_t quantized_dimension; -} TfLiteAffineQuantization; - -/* A union of pointers that points to memory for a given tensor. */ -typedef union TfLitePtrUnion { - /* Do not access these members directly, if possible, use - * GetTensorData(tensor) instead, otherwise only access .data, as other - * members are deprecated. */ - int32_t* i32; - uint32_t* u32; - int64_t* i64; - uint64_t* u64; - float* f; - TfLiteFloat16* f16; - double* f64; - char* raw; - const char* raw_const; - uint8_t* uint8; - bool* b; - int16_t* i16; - uint16_t* ui16; - TfLiteComplex64* c64; - TfLiteComplex128* c128; - int8_t* int8; - /* Only use this member. */ - void* data; -} TfLitePtrUnion; - -// Memory allocation strategies. -// * kTfLiteMmapRo: Read-only memory-mapped data, or data externally allocated. -// * kTfLiteArenaRw: Arena allocated with no guarantees about persistence, -// and available during eval. -// * kTfLiteArenaRwPersistent: Arena allocated but persistent across eval, and -// only available during eval. -// * kTfLiteDynamic: Allocated during eval, or for string tensors. -// * kTfLitePersistentRo: Allocated and populated during prepare. This is -// useful for tensors that can be computed during prepare and treated -// as constant inputs for downstream ops (also in prepare). -// * kTfLiteCustom: Custom memory allocation provided by the user. See -// TfLiteCustomAllocation below. -typedef enum TfLiteAllocationType { - kTfLiteMemNone = 0, - kTfLiteMmapRo, - kTfLiteArenaRw, - kTfLiteArenaRwPersistent, - kTfLiteDynamic, - kTfLitePersistentRo, - kTfLiteCustom, -} TfLiteAllocationType; - -// The delegates should use zero or positive integers to represent handles. -// -1 is reserved from unallocated status. -typedef int TfLiteBufferHandle; -enum { - kTfLiteNullBufferHandle = -1, -}; - -// Storage format of each dimension in a sparse tensor. -typedef enum TfLiteDimensionType { - kTfLiteDimDense = 0, - kTfLiteDimSparseCSR, -} TfLiteDimensionType; - -// Metadata to encode each dimension in a sparse tensor. -typedef struct TfLiteDimensionMetadata { - TfLiteDimensionType format; - int dense_size; - TfLiteIntArray* array_segments; - TfLiteIntArray* array_indices; -} TfLiteDimensionMetadata; - -// Parameters used to encode a sparse tensor. For detailed explanation of each -// field please refer to lite/schema/schema.fbs. -typedef struct TfLiteSparsity { - TfLiteIntArray* traversal_order; - TfLiteIntArray* block_map; - TfLiteDimensionMetadata* dim_metadata; - int dim_metadata_size; -} TfLiteSparsity; - -// Defines a custom memory allocation not owned by the runtime. -// `data` should be aligned to kDefaultTensorAlignment defined in -// lite/util.h. (Currently 64 bytes) -// NOTE: See Interpreter.SetCustomAllocationForTensor for details on usage. -typedef struct TfLiteCustomAllocation { - void* data; - size_t bytes; -} TfLiteCustomAllocation; - -// The flags used in `Interpreter::SetCustomAllocationForTensor`. -// Note that this is a bitmask, so the values should be 1, 2, 4, 8, ...etc. -typedef enum TfLiteCustomAllocationFlags { - kTfLiteCustomAllocationFlagsNone = 0, - // Skips checking whether allocation.data points to an aligned buffer as - // expected by the TFLite runtime. - // NOTE: Setting this flag can cause crashes when calling Invoke(). - // Use with caution. - kTfLiteCustomAllocationFlagsSkipAlignCheck = 1, -} TfLiteCustomAllocationFlags; - -// A tensor in the interpreter system which is a wrapper around a buffer of -// data including a dimensionality (or NULL if not currently defined). -#ifndef TF_LITE_STATIC_MEMORY -typedef struct TfLiteTensor { - // The data type specification for data stored in `data`. This affects - // what member of `data` union should be used. - TfLiteType type; - // A union of data pointers. The appropriate type should be used for a typed - // tensor based on `type`. - TfLitePtrUnion data; - // A pointer to a structure representing the dimensionality interpretation - // that the buffer should have. NOTE: the product of elements of `dims` - // and the element datatype size should be equal to `bytes` below. - TfLiteIntArray* dims; - // Quantization information. - TfLiteQuantizationParams params; - // How memory is mapped - // kTfLiteMmapRo: Memory mapped read only. - // i.e. weights - // kTfLiteArenaRw: Arena allocated read write memory - // (i.e. temporaries, outputs). - TfLiteAllocationType allocation_type; - // The number of bytes required to store the data of this Tensor. I.e. - // (bytes of each element) * dims[0] * ... * dims[n-1]. For example, if - // type is kTfLiteFloat32 and dims = {3, 2} then - // bytes = sizeof(float) * 3 * 2 = 4 * 3 * 2 = 24. - size_t bytes; - - // An opaque pointer to a tflite::MMapAllocation - const void* allocation; - - // Null-terminated name of this tensor. - const char* name; - - // The delegate which knows how to handle `buffer_handle`. - // WARNING: This is an experimental interface that is subject to change. - struct TfLiteDelegate* delegate; - - // An integer buffer handle that can be handled by `delegate`. - // The value is valid only when delegate is not null. - // WARNING: This is an experimental interface that is subject to change. - TfLiteBufferHandle buffer_handle; - - // If the delegate uses its own buffer (e.g. GPU memory), the delegate is - // responsible to set data_is_stale to true. - // `delegate->CopyFromBufferHandle` can be called to copy the data from - // delegate buffer. - // WARNING: This is an // experimental interface that is subject to change. - bool data_is_stale; - - // True if the tensor is a variable. - bool is_variable; - - // Quantization information. Replaces params field above. - TfLiteQuantization quantization; - - // Parameters used to encode a sparse tensor. - // This is optional. The field is NULL if a tensor is dense. - // WARNING: This is an experimental interface that is subject to change. - TfLiteSparsity* sparsity; - - // Optional. Encodes shapes with unknown dimensions with -1. This field is - // only populated when unknown dimensions exist in a read-write tensor (i.e. - // an input or output tensor). (e.g. `dims` contains [1, 1, 1, 3] and - // `dims_signature` contains [1, -1, -1, 3]). Note that this field only - // exists when TF_LITE_STATIC_MEMORY is not defined. - const TfLiteIntArray* dims_signature; -} TfLiteTensor; - -// A structure representing an instance of a node. -// This structure only exhibits the inputs, outputs, user defined data and some -// node properties (like statefulness), not other features like the type. -typedef struct TfLiteNode { - // Inputs to this node expressed as indices into the simulator's tensors. - TfLiteIntArray* inputs; - - // Outputs to this node expressed as indices into the simulator's tensors. - TfLiteIntArray* outputs; - - // intermediate tensors to this node expressed as indices into the simulator's - // tensors. - TfLiteIntArray* intermediates; - - // Temporary tensors uses during the computations. This usually contains no - // tensors, but ops are allowed to change that if they need scratch space of - // any sort. - TfLiteIntArray* temporaries; - - // Opaque data provided by the node implementer through `Registration.init`. - void* user_data; - - // Opaque data provided to the node if the node is a builtin. This is usually - // a structure defined in builtin_op_data.h - void* builtin_data; - - // Custom initial data. This is the opaque data provided in the flatbuffer. - // WARNING: This is an experimental interface that is subject to change. - const void* custom_initial_data; - int custom_initial_data_size; - - // The pointer to the delegate. This is non-null only when the node is - // created by calling `interpreter.ModifyGraphWithDelegate`. - // WARNING: This is an experimental interface that is subject to change. - struct TfLiteDelegate* delegate; - - // Whether this op might have side effect (e.g. stateful op). - bool might_have_side_effect; -} TfLiteNode; -#else // defined(TF_LITE_STATIC_MEMORY)? -// NOTE: This flag is opt-in only at compile time. -// -// Specific reduced TfLiteTensor struct for TF Micro runtime. This struct -// contains only the minimum fields required to initialize and prepare a micro -// inference graph. The fields in this struct have been ordered from -// largest-to-smallest for optimal struct sizeof. -// -// This struct does not use: -// - allocation -// - buffer_handle -// - data_is_stale -// - delegate -// - dims_signature -// - name -// - sparsity -typedef struct TfLiteTensor { - // TODO(b/155784997): Consider consolidating these quantization fields: - // Quantization information. Replaces params field above. - TfLiteQuantization quantization; - - // Quantization information. - TfLiteQuantizationParams params; - - // A union of data pointers. The appropriate type should be used for a typed - // tensor based on `type`. - TfLitePtrUnion data; - - // A pointer to a structure representing the dimensionality interpretation - // that the buffer should have. NOTE: the product of elements of `dims` - // and the element datatype size should be equal to `bytes` below. - TfLiteIntArray* dims; - - // The number of bytes required to store the data of this Tensor. I.e. - // (bytes of each element) * dims[0] * ... * dims[n-1]. For example, if - // type is kTfLiteFloat32 and dims = {3, 2} then - // bytes = sizeof(float) * 3 * 2 = 4 * 3 * 2 = 24. - size_t bytes; - - // The data type specification for data stored in `data`. This affects - // what member of `data` union should be used. - TfLiteType type; - - // How memory is mapped - // kTfLiteMmapRo: Memory mapped read only. - // i.e. weights - // kTfLiteArenaRw: Arena allocated read write memory - // (i.e. temporaries, outputs). - TfLiteAllocationType allocation_type; - - // True if the tensor is a variable. - bool is_variable; -} TfLiteTensor; - -// Specific reduced TfLiteNode struct for TF Micro runtime. This struct contains -// only the minimum fields required to represent a node. -// -// This struct does not use: -// - delegate -// - intermediates -// - temporaries -typedef struct TfLiteNode { - // Inputs to this node expressed as indices into the simulator's tensors. - TfLiteIntArray* inputs; - - // Outputs to this node expressed as indices into the simulator's tensors. - TfLiteIntArray* outputs; - - // intermediate tensors to this node expressed as indices into the simulator's - // tensors. - TfLiteIntArray* intermediates; - - // Opaque data provided by the node implementer through `Registration.init`. - void* user_data; - - // Opaque data provided to the node if the node is a builtin. This is usually - // a structure defined in builtin_op_data.h - void* builtin_data; - - // Custom initial data. This is the opaque data provided in the flatbuffer. - // WARNING: This is an experimental interface that is subject to change. - const void* custom_initial_data; - int custom_initial_data_size; -} TfLiteNode; -#endif // TF_LITE_STATIC_MEMORY - -// Light-weight tensor struct for TF Micro runtime. Provides the minimal amount -// of information required for a kernel to run during TfLiteRegistration::Eval. -// TODO(b/160955687): Move this field into TF_LITE_STATIC_MEMORY when TFLM -// builds with this flag by default internally. -typedef struct TfLiteEvalTensor { - // A union of data pointers. The appropriate type should be used for a typed - // tensor based on `type`. - TfLitePtrUnion data; - - // A pointer to a structure representing the dimensionality interpretation - // that the buffer should have. - TfLiteIntArray* dims; - - // The data type specification for data stored in `data`. This affects - // what member of `data` union should be used. - TfLiteType type; -} TfLiteEvalTensor; - -#ifndef TF_LITE_STATIC_MEMORY -// Free data memory of tensor `t`. -void TfLiteTensorDataFree(TfLiteTensor* t); - -// Free quantization data. -void TfLiteQuantizationFree(TfLiteQuantization* quantization); - -// Free sparsity parameters. -void TfLiteSparsityFree(TfLiteSparsity* sparsity); - -// Free memory of tensor `t`. -void TfLiteTensorFree(TfLiteTensor* t); - -// Set all of a tensor's fields (and free any previously allocated data). -void TfLiteTensorReset(TfLiteType type, const char* name, TfLiteIntArray* dims, - TfLiteQuantizationParams quantization, char* buffer, - size_t size, TfLiteAllocationType allocation_type, - const void* allocation, bool is_variable, - TfLiteTensor* tensor); - -// Copies the contents of 'src' in 'dst'. -// Function does nothing if either 'src' or 'dst' is passed as nullptr and -// return kTfLiteOk. -// Returns kTfLiteError if 'src' and 'dst' doesn't have matching data size. -// Note function copies contents, so it won't create new data pointer -// or change allocation type. -// All Tensor related properties will be copied from 'src' to 'dst' like -// quantization, sparsity, ... -TfLiteStatus TfLiteTensorCopy(const TfLiteTensor* src, TfLiteTensor* dst); - -// Resize the allocated data of a (dynamic) tensor. Tensors with allocation -// types other than kTfLiteDynamic will be ignored. -void TfLiteTensorRealloc(size_t num_bytes, TfLiteTensor* tensor); -#endif // TF_LITE_STATIC_MEMORY - -// WARNING: This is an experimental interface that is subject to change. -// -// Currently, TfLiteDelegateParams has to be allocated in a way that it's -// trivially destructable. It will be stored as `builtin_data` field in -// `TfLiteNode` of the delegate node. -// -// See also the `CreateDelegateParams` function in `interpreter.cc` details. -typedef struct TfLiteDelegateParams { - struct TfLiteDelegate* delegate; - TfLiteIntArray* nodes_to_replace; - TfLiteIntArray* input_tensors; - TfLiteIntArray* output_tensors; -} TfLiteDelegateParams; - -typedef struct TfLiteContext { - // Number of tensors in the context. - size_t tensors_size; - - // The execution plan contains a list of the node indices in execution - // order. execution_plan->size is the current number of nodes. And, - // execution_plan->data[0] is the first node that needs to be run. - // TfLiteDelegates can traverse the current execution plan by iterating - // through each member of this array and using GetNodeAndRegistration() to - // access details about a node. i.e. - // - // TfLiteIntArray* execution_plan; - // TF_LITE_ENSURE_STATUS(context->GetExecutionPlan(context, &execution_plan)); - // for (int exec_index = 0; exec_index < execution_plan->size; exec_index++) { - // int node_index = execution_plan->data[exec_index]; - // TfLiteNode* node; - // TfLiteRegistration* reg; - // context->GetNodeAndRegistration(context, node_index, &node, ®); - // } - // Note: the memory pointed by '`*execution_plan` is OWNED by TfLite runtime. - // Future calls to GetExecutionPlan invalidates earlier outputs. The following - // code snippet shows the issue of such an invocation pattern. After calling - // CheckNode, subsequent access to `plan_1st` is undefined. - // - // void CheckNode(const TfLiteNode* node) { - // ... - // TfLiteIntArray* plan_2nd; - // TF_LITE_ENSURE_STATUS(context->GetExecutionPlan(context, &plan_2nd)); - // ... - // } - // - // TfLiteIntArray* plan_1st; - // TF_LITE_ENSURE_STATUS(context->GetExecutionPlan(context, &plan_1st)); - // for (int exec_index = 0; exec_index < plan_1st->size; exec_index++) { - // int node_index = plan_1st->data[exec_index]; - // TfLiteNode* node; - // TfLiteRegistration* reg; - // context->GetNodeAndRegistration(context, node_index, &node, ®); - // CheckNode(node); - // } - // - // WARNING: This is an experimental interface that is subject to change. - TfLiteStatus (*GetExecutionPlan)(struct TfLiteContext* context, - TfLiteIntArray** execution_plan); - - // An array of tensors in the interpreter context (of length `tensors_size`) - TfLiteTensor* tensors; - - // opaque full context ptr (an opaque c++ data structure) - void* impl_; - - // Request memory pointer be resized. Updates dimensions on the tensor. - // NOTE: ResizeTensor takes ownership of newSize. - TfLiteStatus (*ResizeTensor)(struct TfLiteContext*, TfLiteTensor* tensor, - TfLiteIntArray* new_size); - // Request that an error be reported with format string msg. - void (*ReportError)(struct TfLiteContext*, const char* msg, ...); - - // Add `tensors_to_add` tensors, preserving pre-existing Tensor entries. If - // non-null, the value pointed to by `first_new_tensor_index` will be set to - // the index of the first new tensor. - TfLiteStatus (*AddTensors)(struct TfLiteContext*, int tensors_to_add, - int* first_new_tensor_index); - - // Get a Tensor node by node_index. - // WARNING: This is an experimental interface that is subject to change. - TfLiteStatus (*GetNodeAndRegistration)( - struct TfLiteContext*, int node_index, TfLiteNode** node, - struct TfLiteRegistration** registration); - - // Replace ops with one or more stub delegate operations. This function - // does not take ownership of `nodes_to_replace`. - TfLiteStatus (*ReplaceNodeSubsetsWithDelegateKernels)( - struct TfLiteContext*, struct TfLiteRegistration registration, - const TfLiteIntArray* nodes_to_replace, struct TfLiteDelegate* delegate); - - // Number of threads that are recommended to subsystems like gemmlowp and - // eigen. - int recommended_num_threads; - - // Access external contexts by type. - // WARNING: This is an experimental interface that is subject to change. - TfLiteExternalContext* (*GetExternalContext)(struct TfLiteContext*, - TfLiteExternalContextType); - // Set the value of a external context. Does not take ownership of the - // pointer. - // WARNING: This is an experimental interface that is subject to change. - void (*SetExternalContext)(struct TfLiteContext*, TfLiteExternalContextType, - TfLiteExternalContext*); - - // Flag for allowing float16 precision for FP32 calculation. - // default: false. - // WARNING: This is an experimental API and subject to change. - bool allow_fp32_relax_to_fp16; - - // Pointer to the op-level profiler, if set; nullptr otherwise. - void* profiler; - - // Allocate persistent buffer which has the same life time as the interpreter. - // Returns nullptr on failure. - // The memory is allocated from heap for TFL, and from tail in TFLM. - // This method is only available in Init or Prepare stage. - // WARNING: This is an experimental interface that is subject to change. - void* (*AllocatePersistentBuffer)(struct TfLiteContext* ctx, size_t bytes); - - // Allocate a buffer which will be deallocated right after invoke phase. - // The memory is allocated from heap in TFL, and from volatile arena in TFLM. - // This method is only available in invoke stage. - // NOTE: If possible use RequestScratchBufferInArena method to avoid memory - // allocation during inference time. - // WARNING: This is an experimental interface that is subject to change. - TfLiteStatus (*AllocateBufferForEval)(struct TfLiteContext* ctx, size_t bytes, - void** ptr); - - // Request a scratch buffer in the arena through static memory planning. - // This method is only available in Prepare stage and the buffer is allocated - // by the interpreter between Prepare and Eval stage. In Eval stage, - // GetScratchBuffer API can be used to fetch the address. - // WARNING: This is an experimental interface that is subject to change. - TfLiteStatus (*RequestScratchBufferInArena)(struct TfLiteContext* ctx, - size_t bytes, int* buffer_idx); - - // Get the scratch buffer pointer. - // This method is only available in Eval stage. - // WARNING: This is an experimental interface that is subject to change. - void* (*GetScratchBuffer)(struct TfLiteContext* ctx, int buffer_idx); - - // Resize the memory pointer of the `tensor`. This method behaves the same as - // `ResizeTensor`, except that it makes a copy of the shape array internally - // so the shape array could be deallocated right afterwards. - // WARNING: This is an experimental interface that is subject to change. - TfLiteStatus (*ResizeTensorExplicit)(struct TfLiteContext* ctx, - TfLiteTensor* tensor, int dims, - const int* shape); - - // This method provides a preview of post-delegation partitioning. Each - // TfLiteDelegateParams in the referenced array corresponds to one instance of - // the delegate kernel. - // Example usage: - // - // TfLiteIntArray* nodes_to_replace = ...; - // TfLiteDelegateParams* params_array; - // int num_partitions = 0; - // TF_LITE_ENSURE_STATUS(context->PreviewDelegatePartitioning( - // context, delegate, nodes_to_replace, ¶ms_array, &num_partitions)); - // for (int idx = 0; idx < num_partitions; idx++) { - // const auto& partition_params = params_array[idx]; - // ... - // } - // - // NOTE: The context owns the memory referenced by partition_params_array. It - // will be cleared with another call to PreviewDelegateParitioning, or after - // TfLiteDelegateParams::Prepare returns. - // - // WARNING: This is an experimental interface that is subject to change. - TfLiteStatus (*PreviewDelegatePartitioning)( - struct TfLiteContext* context, const TfLiteIntArray* nodes_to_replace, - TfLiteDelegateParams** partition_params_array, int* num_partitions); - - // Returns a TfLiteTensor struct for a given index. - // WARNING: This is an experimental interface that is subject to change. - // WARNING: This method may not be available on all platforms. - TfLiteTensor* (*GetTensor)(const struct TfLiteContext* context, - int tensor_idx); - - // Returns a TfLiteEvalTensor struct for a given index. - // WARNING: This is an experimental interface that is subject to change. - // WARNING: This method may not be available on all platforms. - TfLiteEvalTensor* (*GetEvalTensor)(const struct TfLiteContext* context, - int tensor_idx); - - // Retrieves named metadata buffer from the TFLite model. - // Returns kTfLiteOk if metadata is successfully obtained from the flatbuffer - // Model: that is, there exists a `metadata` entry with given `name` string. - // (see TFLite's schema.fbs). - // The corresponding `buffer` information is populated in `ptr` & `bytes`. - // The data from `ptr` is valid for the lifetime of the Interpreter. - // - // WARNING: This is an experimental interface that is subject to change. - TfLiteStatus (*GetModelMetadata)(const struct TfLiteContext* context, - const char* name, const char** ptr, - size_t* bytes); -} TfLiteContext; - -// `TfLiteRegistrationExternal` is an external version of `TfLiteRegistration` -// for C API which doesn't use internal types (such as `TfLiteContext`) but only -// uses stable API types (such as `TfLiteOpaqueContext`). The purpose of each -// field is the exactly the same as with `TfLiteRegistration`. -typedef struct TfLiteRegistrationExternal TfLiteRegistrationExternal; - -typedef struct TfLiteRegistration { - // Initializes the op from serialized data. - // Called only *once* for the lifetime of the op, so any one-time allocations - // should be made here (unless they depend on tensor sizes). - // - // If a built-in op: - // `buffer` is the op's params data (TfLiteLSTMParams*). - // `length` is zero. - // If custom op: - // `buffer` is the op's `custom_options`. - // `length` is the size of the buffer. - // - // Returns a type-punned (i.e. void*) opaque data (e.g. a primitive pointer - // or an instance of a struct). - // - // The returned pointer will be stored with the node in the `user_data` field, - // accessible within prepare and invoke functions below. - // NOTE: if the data is already in the desired format, simply implement this - // function to return `nullptr` and implement the free function to be a no-op. - void* (*init)(TfLiteContext* context, const char* buffer, size_t length); - - // The pointer `buffer` is the data previously returned by an init invocation. - void (*free)(TfLiteContext* context, void* buffer); - - // prepare is called when the inputs this node depends on have been resized. - // context->ResizeTensor() can be called to request output tensors to be - // resized. - // Can be called multiple times for the lifetime of the op. - // - // Returns kTfLiteOk on success. - TfLiteStatus (*prepare)(TfLiteContext* context, TfLiteNode* node); - - // Execute the node (should read node->inputs and output to node->outputs). - // Returns kTfLiteOk on success. - TfLiteStatus (*invoke)(TfLiteContext* context, TfLiteNode* node); - - // profiling_string is called during summarization of profiling information - // in order to group executions together. Providing a value here will cause a - // given op to appear multiple times is the profiling report. This is - // particularly useful for custom ops that can perform significantly - // different calculations depending on their `user-data`. - const char* (*profiling_string)(const TfLiteContext* context, - const TfLiteNode* node); - - // Builtin codes. If this kernel refers to a builtin this is the code - // of the builtin. This is so we can do marshaling to other frameworks like - // NN API. - // Note: It is the responsibility of the registration binder to set this - // properly. - int32_t builtin_code; - - // Custom op name. If the op is a builtin, this will be null. - // Note: It is the responsibility of the registration binder to set this - // properly. - // WARNING: This is an experimental interface that is subject to change. - const char* custom_name; - - // The version of the op. - // Note: It is the responsibility of the registration binder to set this - // properly. - int version; - - // The external version of `TfLiteRegistration`. Since we can't use internal - // types (such as `TfLiteContext`) for C API to maintain ABI stability. - // C API user will provide `TfLiteRegistrationExternal` to implement custom - // ops. We keep it inside of `TfLiteRegistration` and use it to route - // callbacks properly. - TfLiteRegistrationExternal* registration_external; -} TfLiteRegistration; - -// Old version of `TfLiteRegistration` to maintain binary backward -// compatibility. -// WARNING: This structure is deprecated / not an official part of the API. -// It should be only used for binary backward compatibility. -typedef struct TfLiteRegistration_V1 { - void* (*init)(TfLiteContext* context, const char* buffer, size_t length); - void (*free)(TfLiteContext* context, void* buffer); - TfLiteStatus (*prepare)(TfLiteContext* context, TfLiteNode* node); - TfLiteStatus (*invoke)(TfLiteContext* context, TfLiteNode* node); - const char* (*profiling_string)(const TfLiteContext* context, - const TfLiteNode* node); - int32_t builtin_code; - const char* custom_name; - int version; -} TfLiteRegistration_V1; - -// The flags used in `TfLiteDelegate`. Note that this is a bitmask, so the -// values should be 1, 2, 4, 8, ...etc. -typedef enum TfLiteDelegateFlags { - kTfLiteDelegateFlagsNone = 0, - // The flag is set if the delegate can handle dynamic sized tensors. - // For example, the output shape of a `Resize` op with non-constant shape - // can only be inferred when the op is invoked. - // In this case, the Delegate is responsible for calling - // `SetTensorToDynamic` to mark the tensor as a dynamic tensor, and calling - // `ResizeTensor` when invoking the op. - // - // If the delegate isn't capable to handle dynamic tensors, this flag need - // to be set to false. - kTfLiteDelegateFlagsAllowDynamicTensors = 1, - - // This flag can be used by delegates (that allow dynamic tensors) to ensure - // applicable tensor shapes are automatically propagated in the case of tensor - // resizing. - // This means that non-dynamic (allocation_type != kTfLiteDynamic) I/O tensors - // of a delegate kernel will have correct shapes before its Prepare() method - // is called. The runtime leverages TFLite builtin ops in the original - // execution plan to propagate shapes. - // - // A few points to note: - // 1. This requires kTfLiteDelegateFlagsAllowDynamicTensors. If that flag is - // false, this one is redundant since the delegate kernels are re-initialized - // every time tensors are resized. - // 2. Enabling this flag adds some overhead to AllocateTensors(), since extra - // work is required to prepare the original execution plan. - // 3. This flag requires that the original execution plan only have ops with - // valid registrations (and not 'dummy' custom ops like with Flex). - // WARNING: This feature is experimental and subject to change. - kTfLiteDelegateFlagsRequirePropagatedShapes = 2 -} TfLiteDelegateFlags; - -// WARNING: This is an experimental interface that is subject to change. -typedef struct TfLiteDelegate { - // Data that delegate needs to identify itself. This data is owned by the - // delegate. The delegate is owned in the user code, so the delegate is - // responsible for doing this when it is destroyed. - void* data_; - - // Invoked by ModifyGraphWithDelegate. This prepare is called, giving the - // delegate a view of the current graph through TfLiteContext*. It typically - // will look at the nodes and call ReplaceNodeSubsetsWithDelegateKernels() - // to ask the TensorFlow lite runtime to create macro-nodes to represent - // delegated subgraphs of the original graph. - TfLiteStatus (*Prepare)(TfLiteContext* context, - struct TfLiteDelegate* delegate); - - // Copy the data from delegate buffer handle into raw memory of the given - // 'tensor'. Note that the delegate is allowed to allocate the raw bytes as - // long as it follows the rules for kTfLiteDynamic tensors, in which case this - // cannot be null. - TfLiteStatus (*CopyFromBufferHandle)(TfLiteContext* context, - struct TfLiteDelegate* delegate, - TfLiteBufferHandle buffer_handle, - TfLiteTensor* tensor); - - // Copy the data from raw memory of the given 'tensor' to delegate buffer - // handle. This can be null if the delegate doesn't use its own buffer. - TfLiteStatus (*CopyToBufferHandle)(TfLiteContext* context, - struct TfLiteDelegate* delegate, - TfLiteBufferHandle buffer_handle, - TfLiteTensor* tensor); - - // Free the Delegate Buffer Handle. Note: This only frees the handle, but - // this doesn't release the underlying resource (e.g. textures). The - // resources are either owned by application layer or the delegate. - // This can be null if the delegate doesn't use its own buffer. - void (*FreeBufferHandle)(TfLiteContext* context, - struct TfLiteDelegate* delegate, - TfLiteBufferHandle* handle); - - // Bitmask flags. See the comments in `TfLiteDelegateFlags`. - int64_t flags; -} TfLiteDelegate; - -// Build a 'null' delegate, with all the fields properly set to their default -// values. -TfLiteDelegate TfLiteDelegateCreate(void); - -#ifdef __cplusplus -} // extern "C" -#endif // __cplusplus -#endif // TENSORFLOW_LITE_C_COMMON_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/context_util.h b/code/components/tflite-lib/tensorflow/lite/context_util.h deleted file mode 100644 index 7c8a5abd..00000000 --- a/code/components/tflite-lib/tensorflow/lite/context_util.h +++ /dev/null @@ -1,51 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -// This provides a few C++ helpers that are useful for manipulating C structures -// in C++. -#ifndef TENSORFLOW_LITE_CONTEXT_UTIL_H_ -#define TENSORFLOW_LITE_CONTEXT_UTIL_H_ - -#include - -#include "tensorflow/lite/c/common.h" - -namespace tflite { - -// Provide a range iterable wrapper for TfLiteIntArray* (C lists that TfLite -// C api uses. Can't use the google array_view, since we can't depend on even -// absl for embedded device reasons. -class TfLiteIntArrayView { - public: - // Construct a view of a TfLiteIntArray*. Note, `int_array` should be non-null - // and this view does not take ownership of it. - explicit TfLiteIntArrayView(const TfLiteIntArray* int_array) - : int_array_(int_array) {} - - TfLiteIntArrayView(const TfLiteIntArrayView&) = default; - TfLiteIntArrayView& operator=(const TfLiteIntArrayView& rhs) = default; - - typedef const int* const_iterator; - const_iterator begin() const { return int_array_->data; } - const_iterator end() const { return &int_array_->data[int_array_->size]; } - size_t size() const { return end() - begin(); } - int operator[](size_t pos) const { return int_array_->data[pos]; } - - private: - const TfLiteIntArray* int_array_; -}; - -} // namespace tflite - -#endif // TENSORFLOW_LITE_CONTEXT_UTIL_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/core/api/error_reporter.cc b/code/components/tflite-lib/tensorflow/lite/core/api/error_reporter.cc deleted file mode 100644 index 7070eaa5..00000000 --- a/code/components/tflite-lib/tensorflow/lite/core/api/error_reporter.cc +++ /dev/null @@ -1,38 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/core/api/error_reporter.h" -#include - -namespace tflite { - -int ErrorReporter::Report(const char* format, ...) { - va_list args; - va_start(args, format); - int code = Report(format, args); - va_end(args); - return code; -} - -// TODO(aselle): Make the name of ReportError on context the same, so -// we can use the ensure functions w/o a context and w/ a reporter. -int ErrorReporter::ReportError(void*, const char* format, ...) { - va_list args; - va_start(args, format); - int code = Report(format, args); - va_end(args); - return code; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/core/api/error_reporter.h b/code/components/tflite-lib/tensorflow/lite/core/api/error_reporter.h deleted file mode 100644 index 05839a61..00000000 --- a/code/components/tflite-lib/tensorflow/lite/core/api/error_reporter.h +++ /dev/null @@ -1,59 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_CORE_API_ERROR_REPORTER_H_ -#define TENSORFLOW_LITE_CORE_API_ERROR_REPORTER_H_ - -#include - -namespace tflite { - -/// A functor that reports error to supporting system. Invoked similar to -/// printf. -/// -/// Usage: -/// ErrorReporter foo; -/// foo.Report("test %d", 5); -/// or -/// va_list args; -/// foo.Report("test %d", args); // where args is va_list -/// -/// Subclass ErrorReporter to provide another reporting destination. -/// For example, if you have a GUI program, you might redirect to a buffer -/// that drives a GUI error log box. -class ErrorReporter { - public: - virtual ~ErrorReporter() {} - virtual int Report(const char* format, va_list args) = 0; - int Report(const char* format, ...); - int ReportError(void*, const char* format, ...); -}; - -} // namespace tflite - -// You should not make bare calls to the error reporter, instead use the -// TF_LITE_REPORT_ERROR macro, since this allows message strings to be -// stripped when the binary size has to be optimized. If you are looking to -// reduce binary size, define TF_LITE_STRIP_ERROR_STRINGS when compiling and -// every call will be stubbed out, taking no memory. -#ifndef TF_LITE_STRIP_ERROR_STRINGS -#define TF_LITE_REPORT_ERROR(reporter, ...) \ - do { \ - static_cast(reporter)->Report(__VA_ARGS__); \ - } while (false) -#else // TF_LITE_STRIP_ERROR_STRINGS -#define TF_LITE_REPORT_ERROR(reporter, ...) -#endif // TF_LITE_STRIP_ERROR_STRINGS - -#endif // TENSORFLOW_LITE_CORE_API_ERROR_REPORTER_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/core/api/flatbuffer_conversions.cc b/code/components/tflite-lib/tensorflow/lite/core/api/flatbuffer_conversions.cc deleted file mode 100644 index 1ecefa47..00000000 --- a/code/components/tflite-lib/tensorflow/lite/core/api/flatbuffer_conversions.cc +++ /dev/null @@ -1,2457 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/core/api/flatbuffer_conversions.h" - -#include -#include -#include - -#include "flatbuffers/flatbuffers.h" // from @flatbuffers -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/core/api/error_reporter.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/schema/schema_generated.h" - -namespace tflite { - -namespace { - -// Utility class for safely allocating POD data. This is useful for avoiding -// leaks in cases where op params are allocated but fail to propagate to the -// parsed op data (e.g., when model parameters are invalid). -class SafeBuiltinDataAllocator { - public: - class BuiltinDataDeleter { - public: - explicit BuiltinDataDeleter(BuiltinDataAllocator* allocator) - : allocator_(allocator) {} - - void operator()(void* data) { allocator_->Deallocate(data); } - - private: - BuiltinDataAllocator* allocator_; - }; - - template - using BuiltinDataPtr = std::unique_ptr; - - explicit SafeBuiltinDataAllocator(BuiltinDataAllocator* allocator) - : allocator_(allocator) {} - - template - BuiltinDataPtr Allocate() { - return BuiltinDataPtr(allocator_->AllocatePOD(), - BuiltinDataDeleter(allocator_)); - } - - private: - BuiltinDataAllocator* allocator_; -}; - -// All the Parse functions take some pointers as params and this function has -// the common DCHECKs to catch if any of those are nullptr. -void CheckParsePointerParams(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data) { - TFLITE_DCHECK(op != nullptr); - TFLITE_DCHECK(error_reporter != nullptr); - TFLITE_DCHECK(allocator != nullptr); - TFLITE_DCHECK(builtin_data != nullptr); -} - -// Copies the contents from the flatbuffer int vector `flatbuffer` into the -// int array `buffer`. `flat_vector` and `buffer` represent the same -// configuration operation for a given operation. -TfLiteStatus FlatBufferIntVectorToArray( - int max_size_of_buffer, const flatbuffers::Vector* flat_vector, - int* buffer, ErrorReporter* error_reporter, const char* op_name) { - if (!flat_vector) { - TF_LITE_REPORT_ERROR(error_reporter, - "Input array not provided for operation '%s'.\n", - op_name); - return kTfLiteError; - } else { - size_t num_dimensions = flat_vector->size(); - if (num_dimensions > max_size_of_buffer / sizeof(int)) { - TF_LITE_REPORT_ERROR( - error_reporter, - "Found too many dimensions in the input array of operation '%s'.\n", - op_name); - return kTfLiteError; - } else { - for (size_t i = 0; i < num_dimensions; ++i) { - buffer[i] = flat_vector->Get(i); - } - } - } - return kTfLiteOk; -} - -// Converts the flatbuffer activation to what is used at runtime. -TfLiteFusedActivation ConvertActivation(ActivationFunctionType activation) { - switch (activation) { - case ActivationFunctionType_NONE: - return kTfLiteActNone; - case ActivationFunctionType_RELU: - return kTfLiteActRelu; - case ActivationFunctionType_RELU_N1_TO_1: - return kTfLiteActReluN1To1; - case ActivationFunctionType_RELU6: - return kTfLiteActRelu6; - case ActivationFunctionType_TANH: - return kTfLiteActTanh; - case ActivationFunctionType_SIGN_BIT: - return kTfLiteActSignBit; - } - return kTfLiteActNone; -} - -// Converts the flatbuffer padding enum to what is used at runtime. -TfLitePadding ConvertPadding(Padding padding) { - switch (padding) { - case Padding_SAME: - return kTfLitePaddingSame; - case Padding_VALID: - return kTfLitePaddingValid; - } - return kTfLitePaddingUnknown; -} - -// Converts the flatbuffer mirror padding enum to what is used at runtime. -TfLiteMirrorPaddingMode ConvertMirrorPadding(MirrorPadMode padding) { - switch (padding) { - case MirrorPadMode_REFLECT: - return kTfLiteMirrorPaddingReflect; - case MirrorPadMode_SYMMETRIC: - return kTfLiteMirrorPaddingSymmetric; - } - return kTfLiteMirrorPaddingUnknown; -} - -#ifndef TF_LITE_STATIC_MEMORY -TfLiteStatus ParseOpDataTfLite(const Operator* op, BuiltinOperator op_type, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data) { - auto parseLSHProjectionType = [](LSHProjectionType type) { - switch (type) { - case LSHProjectionType_SPARSE: - return kTfLiteLshProjectionSparse; - case LSHProjectionType_DENSE: - return kTfLiteLshProjectionDense; - default: - return kTfLiteLshProjectionUnknown; - } - }; - auto parseCombinerType = [](CombinerType type) { - switch (type) { - case CombinerType_MEAN: - return kTfLiteCombinerTypeMean; - case CombinerType_SQRTN: - return kTfLiteCombinerTypeSqrtn; - case CombinerType_SUM: - default: - return kTfLiteCombinerTypeSum; - } - }; - - SafeBuiltinDataAllocator safe_allocator(allocator); - *builtin_data = nullptr; - switch (op_type) { - case BuiltinOperator_ABS: { - return ParseAbs(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_ADD: { - return ParseAdd(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_ADD_N: { - return ParseAddN(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_ARG_MAX: { - return ParseArgMax(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_ARG_MIN: { - return ParseArgMin(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_ASSIGN_VARIABLE: { - return ParseAssignVariable(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_AVERAGE_POOL_2D: { - return ParsePool(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_BATCH_MATMUL: { - return ParseBatchMatMul(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_BATCH_TO_SPACE_ND: { - return ParseBatchToSpaceNd(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_BROADCAST_ARGS: { - return ParseBroadcastArgs(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_BROADCAST_TO: { - return ParseBroadcastTo(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_CALL_ONCE: { - return ParseCallOnce(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_CEIL: { - return ParseCeil(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_CONCATENATION: { - return ParseConcatenation(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_CONV_2D: { - return ParseConv2D(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_CUMSUM: { - return ParseCumsum(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_DEPTH_TO_SPACE: { - return ParseDepthToSpace(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_DEPTHWISE_CONV_2D: { - return ParseDepthwiseConv2D(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_DEQUANTIZE: { - return ParseDequantize(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_DIV: { - return ParseDiv(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_ELU: { - return ParseElu(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_EXP: { - return ParseExp(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_EXPAND_DIMS: { - return ParseExpandDims(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_FILL: { - return ParseFill(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_FLOOR: { - return ParseFloor(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_FLOOR_DIV: { - return ParseFloorDiv(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_FLOOR_MOD: { - return ParseFloorMod(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_FULLY_CONNECTED: { - return ParseFullyConnected(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_GATHER_ND: { - return ParseGatherNd(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_GREATER: { - return ParseGreater(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_GREATER_EQUAL: { - return ParseGreaterEqual(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_HARD_SWISH: { - return ParseHardSwish(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_L2_NORMALIZATION: { - return ParseL2Normalization(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_L2_POOL_2D: { - return ParsePool(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_LEAKY_RELU: { - return ParseLeakyRelu(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_LESS: { - return ParseLess(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_LESS_EQUAL: { - return ParseLessEqual(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_LOG: { - return ParseLog(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_LOGICAL_AND: { - return ParseLogicalAnd(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_LOGICAL_NOT: { - return ParseLogicalNot(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_LOGICAL_OR: { - return ParseLogicalOr(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_LOGISTIC: { - return ParseLogistic(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_LOG_SOFTMAX: { - return ParseLogSoftmax(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_LSTM: { - return ParseLSTM(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_MAXIMUM: { - return ParseMaximum(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_MAX_POOL_2D: { - return ParsePool(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_MIRROR_PAD: { - return ParseMirrorPad(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_MEAN: { - return ParseReducer(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_MINIMUM: { - return ParseMinimum(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_MUL: { - return ParseMul(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_NEG: { - return ParseNeg(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_NOT_EQUAL: { - return ParseNotEqual(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_PACK: { - return ParsePack(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_PAD: { - return ParsePad(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_PADV2: { - return ParsePadV2(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_POW: { - return ParsePow(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_PRELU: { - return ParsePrelu(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_QUANTIZE: { - return ParseQuantize(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_READ_VARIABLE: { - return ParseReadVariable(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_REDUCE_ANY: { - return ParseReducer(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_REDUCE_ALL: { - return ParseReducer(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_REDUCE_MAX: { - return ParseReducer(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_REDUCE_MIN: { - return ParseReducer(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_REDUCE_PROD: { - return ParseReducer(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_RELU: { - return ParseRelu(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_RELU6: { - return ParseRelu6(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_RESHAPE: { - return ParseReshape(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_RESIZE_BILINEAR: { - return ParseResizeBilinear(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_RESIZE_NEAREST_NEIGHBOR: { - return ParseResizeNearestNeighbor(op, error_reporter, allocator, - builtin_data); - } - - case BuiltinOperator_ROUND: { - return ParseRound(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_RSQRT: { - return ParseRsqrt(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_SHAPE: { - return ParseShape(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_SIN: { - return ParseSin(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_SOFTMAX: { - return ParseSoftmax(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_SPACE_TO_BATCH_ND: { - return ParseSpaceToBatchNd(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_SPACE_TO_DEPTH: { - return ParseSpaceToDepth(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_SPLIT: { - return ParseSplit(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_SPLIT_V: { - return ParseSplitV(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_SQRT: { - return ParseSqrt(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_SQUARE: { - return ParseSquare(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_SQUARED_DIFFERENCE: { - return ParseSquaredDifference(op, error_reporter, allocator, - builtin_data); - } - - case BuiltinOperator_SQUEEZE: { - return ParseSqueeze(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_STRIDED_SLICE: { - return ParseStridedSlice(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_SUB: { - return ParseSub(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_SUM: { - return ParseReducer(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_SVDF: { - return ParseSvdf(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_TANH: { - return ParseTanh(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_TRANSPOSE_CONV: { - return ParseTransposeConv(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_UNPACK: { - return ParseUnpack(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_VAR_HANDLE: { - return ParseVarHandle(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_ZEROS_LIKE: { - return ParseZerosLike(op, error_reporter, allocator, builtin_data); - } - - case BuiltinOperator_CAST: { - return ParseCast(op, error_reporter, allocator, builtin_data); - } - case BuiltinOperator_LSH_PROJECTION: { - auto params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - if (const auto* lshParams = - op->builtin_options_as_LSHProjectionOptions()) { - params->type = parseLSHProjectionType(lshParams->type()); - } - *builtin_data = params.release(); - return kTfLiteOk; - } - case BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_RNN: { - auto params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - if (const auto* sequence_rnn_params = - op->builtin_options_as_SequenceRNNOptions()) { - params->activation = - ConvertActivation(sequence_rnn_params->fused_activation_function()); - params->time_major = sequence_rnn_params->time_major(); - params->asymmetric_quantize_inputs = - sequence_rnn_params->asymmetric_quantize_inputs(); - } - *builtin_data = params.release(); - return kTfLiteOk; - } - case BuiltinOperator_BIDIRECTIONAL_SEQUENCE_RNN: { - auto params = - safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - if (const auto* bidi_sequence_rnn_params = - op->builtin_options_as_BidirectionalSequenceRNNOptions()) { - params->activation = ConvertActivation( - bidi_sequence_rnn_params->fused_activation_function()); - params->time_major = bidi_sequence_rnn_params->time_major(); - params->merge_outputs = bidi_sequence_rnn_params->merge_outputs(); - params->asymmetric_quantize_inputs = - bidi_sequence_rnn_params->asymmetric_quantize_inputs(); - } - *builtin_data = params.release(); - return kTfLiteOk; - } - case BuiltinOperator_RNN: { - auto params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - if (const auto* rnn_params = op->builtin_options_as_RNNOptions()) { - params->activation = - ConvertActivation(rnn_params->fused_activation_function()); - params->asymmetric_quantize_inputs = - rnn_params->asymmetric_quantize_inputs(); - } - *builtin_data = params.release(); - return kTfLiteOk; - } - case BuiltinOperator_EMBEDDING_LOOKUP_SPARSE: { - auto params = - safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - if (const auto* embedding_params = - op->builtin_options_as_EmbeddingLookupSparseOptions()) { - params->combiner = parseCombinerType(embedding_params->combiner()); - } - *builtin_data = params.release(); - return kTfLiteOk; - } - - case BuiltinOperator_HASHTABLE_LOOKUP: - // no-op. - return kTfLiteOk; - - case BuiltinOperator_LOCAL_RESPONSE_NORMALIZATION: { - auto params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - if (const auto* schema_params = - op->builtin_options_as_LocalResponseNormalizationOptions()) { - params->radius = schema_params->radius(); - params->bias = schema_params->bias(); - params->alpha = schema_params->alpha(); - params->beta = schema_params->beta(); - } - *builtin_data = params.release(); - return kTfLiteOk; - } - case BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM: { - return ParseUnidirectionalSequenceLSTM(op, error_reporter, allocator, - builtin_data); - } - case BuiltinOperator_BIDIRECTIONAL_SEQUENCE_LSTM: { - auto params = - safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - if (const auto* bidi_lstm_params = - op->builtin_options_as_BidirectionalSequenceLSTMOptions()) { - params->activation = - ConvertActivation(bidi_lstm_params->fused_activation_function()); - params->cell_clip = bidi_lstm_params->cell_clip(); - params->proj_clip = bidi_lstm_params->proj_clip(); - params->merge_outputs = bidi_lstm_params->merge_outputs(); - params->time_major = bidi_lstm_params->time_major(); - params->asymmetric_quantize_inputs = - bidi_lstm_params->asymmetric_quantize_inputs(); - } - *builtin_data = params.release(); - return kTfLiteOk; - } - case BuiltinOperator_SKIP_GRAM: { - auto params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - if (const auto* skip_gram_params = - op->builtin_options_as_SkipGramOptions()) { - params->ngram_size = skip_gram_params->ngram_size(); - params->max_skip_size = skip_gram_params->max_skip_size(); - params->include_all_ngrams = skip_gram_params->include_all_ngrams(); - } - *builtin_data = params.release(); - return kTfLiteOk; - } - - case BuiltinOperator_GATHER: { - return ParseGather(op, error_reporter, allocator, builtin_data); - } - case BuiltinOperator_SPARSE_TO_DENSE: { - auto params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - if (const auto* sparse_to_dense_params = - op->builtin_options_as_SparseToDenseOptions()) { - params->validate_indices = sparse_to_dense_params->validate_indices(); - } - *builtin_data = params.release(); - return kTfLiteOk; - } - case BuiltinOperator_DELEGATE: { - TF_LITE_REPORT_ERROR(error_reporter, - "DELEGATE op shouldn't exist in model."); - return kTfLiteError; - } - case BuiltinOperator_FAKE_QUANT: { - auto params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - if (const auto* schema_params = - op->builtin_options_as_FakeQuantOptions()) { - params->min = schema_params->min(); - params->max = schema_params->max(); - params->num_bits = schema_params->num_bits(); - params->narrow_range = schema_params->narrow_range(); - } - *builtin_data = params.release(); - return kTfLiteOk; - } - case BuiltinOperator_ONE_HOT: { - auto params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - if (const auto* schema_params = op->builtin_options_as_OneHotOptions()) { - params->axis = schema_params->axis(); - } - *builtin_data = params.release(); - return kTfLiteOk; - } - case BuiltinOperator_UNIQUE: { - auto params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - const auto* unique_params = op->builtin_options_as_UniqueOptions(); - if (unique_params != nullptr) { - params->index_out_type = - unique_params->idx_out_type() == tflite::TensorType_INT64 - ? TfLiteType::kTfLiteInt64 - : TfLiteType::kTfLiteInt32; - } - *builtin_data = params.release(); - return kTfLiteOk; - } - case BuiltinOperator_REVERSE_SEQUENCE: { - auto params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - if (const auto* reverse_seq_params = - op->builtin_options_as_ReverseSequenceOptions()) { - params->seq_dim = reverse_seq_params->seq_dim(); - params->batch_dim = reverse_seq_params->batch_dim(); - } - *builtin_data = params.release(); - return kTfLiteOk; - } - case BuiltinOperator_IF: { - auto params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - if (const auto* if_params = op->builtin_options_as_IfOptions()) { - params->then_subgraph_index = if_params->then_subgraph_index(); - params->else_subgraph_index = if_params->else_subgraph_index(); - } - *builtin_data = params.release(); - return kTfLiteOk; - } - case BuiltinOperator_WHILE: { - auto params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - if (const auto* while_params = op->builtin_options_as_WhileOptions()) { - params->cond_subgraph_index = while_params->cond_subgraph_index(); - params->body_subgraph_index = while_params->body_subgraph_index(); - } - *builtin_data = params.release(); - return kTfLiteOk; - } - case BuiltinOperator_CONV_3D: - case BuiltinOperator_CONV_3D_TRANSPOSE: { - auto params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - if (const auto* conv3d_params = op->builtin_options_as_Conv3DOptions()) { - params->padding = ConvertPadding(conv3d_params->padding()); - params->activation = - ConvertActivation(conv3d_params->fused_activation_function()); - params->stride_depth = conv3d_params->stride_d(); - params->stride_height = conv3d_params->stride_h(); - params->stride_width = conv3d_params->stride_w(); - params->dilation_depth_factor = conv3d_params->dilation_d_factor(); - params->dilation_height_factor = conv3d_params->dilation_h_factor(); - params->dilation_width_factor = conv3d_params->dilation_w_factor(); - } - *builtin_data = params.release(); - return kTfLiteOk; - } - case BuiltinOperator_HASHTABLE: { - auto params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - if (const auto* hashtable_params = - op->builtin_options_as_HashtableOptions()) { - params->table_id = hashtable_params->table_id(); - TF_LITE_ENSURE_STATUS(ConvertTensorType( - hashtable_params->key_dtype(), ¶ms->key_dtype, error_reporter)); - TF_LITE_ENSURE_STATUS(ConvertTensorType(hashtable_params->value_dtype(), - ¶ms->value_dtype, - error_reporter)); - } - *builtin_data = params.release(); - return kTfLiteOk; - } - case BuiltinOperator_MULTINOMIAL: { - auto params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - if (const auto* multinomial_params = - op->builtin_options_as_RandomOptions()) { - params->seed = multinomial_params->seed(); - params->seed2 = multinomial_params->seed2(); - } - *builtin_data = params.release(); - return kTfLiteOk; - } - case BuiltinOperator_RANDOM_STANDARD_NORMAL: { - auto params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - if (const auto* random_std_normal_params = - op->builtin_options_as_RandomOptions()) { - params->seed = random_std_normal_params->seed(); - params->seed2 = random_std_normal_params->seed2(); - } - *builtin_data = params.release(); - return kTfLiteOk; - } - case BuiltinOperator_BUCKETIZE: { - auto params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - if (const auto* bucketize_params = - op->builtin_options_as_BucketizeOptions()) { - const flatbuffers::Vector* boundaries = - bucketize_params->boundaries(); - if (boundaries == nullptr) { - TF_LITE_REPORT_ERROR( - error_reporter, - "boundaries array not provided for operation 'bucketize'.\n"); - return kTfLiteError; - } - params->num_boundaries = boundaries->size(); - if (boundaries->data() == nullptr) { - TF_LITE_REPORT_ERROR(error_reporter, - "boundaries.data() returned nullptr for " - "operation 'bucketize'.\n"); - return kTfLiteError; - } - params->boundaries = boundaries->data(); - } - *builtin_data = params.release(); - return kTfLiteOk; - } - case BuiltinOperator_RANDOM_UNIFORM: { - auto params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - if (const auto* random_uniform_params = - op->builtin_options_as_RandomOptions()) { - params->seed = random_uniform_params->seed(); - params->seed2 = random_uniform_params->seed2(); - } - *builtin_data = params.release(); - return kTfLiteOk; - } - case BuiltinOperator_GELU: { - auto params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - if (const auto* gelu_params = op->builtin_options_as_GeluOptions()) { - params->approximate = gelu_params->approximate(); - } - *builtin_data = params.release(); - return kTfLiteOk; - } - // Below are the ops with no builtin_data structure. - // TODO(aselle): Implement call in BuiltinOptions, but nullptrs are - // ok for now, since there is no call implementation either. - case BuiltinOperator_CALL: - case BuiltinOperator_COMPLEX_ABS: - case BuiltinOperator_CONCAT_EMBEDDINGS: - case BuiltinOperator_COS: - case BuiltinOperator_CUSTOM: - case BuiltinOperator_DENSIFY: - case BuiltinOperator_DYNAMIC_UPDATE_SLICE: - case BuiltinOperator_EMBEDDING_LOOKUP: - case BuiltinOperator_EQUAL: - case BuiltinOperator_HASHTABLE_FIND: - case BuiltinOperator_HASHTABLE_IMPORT: - case BuiltinOperator_HASHTABLE_SIZE: - case BuiltinOperator_IMAG: - case BuiltinOperator_MATRIX_DIAG: - case BuiltinOperator_MATRIX_SET_DIAG: - case BuiltinOperator_NON_MAX_SUPPRESSION_V4: - case BuiltinOperator_NON_MAX_SUPPRESSION_V5: - case BuiltinOperator_RELU_N1_TO_1: - case BuiltinOperator_RELU_0_TO_1: - case BuiltinOperator_SCATTER_ND: - case BuiltinOperator_SELECT: - case BuiltinOperator_SELECT_V2: - case BuiltinOperator_SLICE: - case BuiltinOperator_TILE: - case BuiltinOperator_TOPK_V2: - case BuiltinOperator_TRANSPOSE: - case BuiltinOperator_RANGE: - case BuiltinOperator_RANK: - case BuiltinOperator_REAL: - case BuiltinOperator_RFFT2D: - case BuiltinOperator_SEGMENT_SUM: - case BuiltinOperator_REVERSE_V2: - case BuiltinOperator_UNSORTED_SEGMENT_MAX: - case BuiltinOperator_UNSORTED_SEGMENT_MIN: - case BuiltinOperator_UNSORTED_SEGMENT_PROD: - case BuiltinOperator_UNSORTED_SEGMENT_SUM: - case BuiltinOperator_ATAN2: - case BuiltinOperator_WHERE: - return kTfLiteOk; - case BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES: - return kTfLiteError; - } - return kTfLiteError; -} // NOLINT[readability/fn_size] -#endif // !defined(TF_LITE_STATIC_MEMORY) -} // namespace - -TfLiteStatus ConvertTensorType(TensorType tensor_type, TfLiteType* type, - ErrorReporter* error_reporter) { - switch (tensor_type) { - case TensorType_FLOAT16: - *type = kTfLiteFloat16; - return kTfLiteOk; - case TensorType_FLOAT32: - *type = kTfLiteFloat32; - return kTfLiteOk; - case TensorType_FLOAT64: - *type = kTfLiteFloat64; - return kTfLiteOk; - case TensorType_INT16: - *type = kTfLiteInt16; - return kTfLiteOk; - case TensorType_UINT16: - *type = kTfLiteUInt16; - return kTfLiteOk; - case TensorType_INT32: - *type = kTfLiteInt32; - return kTfLiteOk; - case TensorType_UINT32: - *type = kTfLiteUInt32; - return kTfLiteOk; - case TensorType_UINT8: - *type = kTfLiteUInt8; - return kTfLiteOk; - case TensorType_INT8: - *type = kTfLiteInt8; - return kTfLiteOk; - case TensorType_INT64: - *type = kTfLiteInt64; - return kTfLiteOk; - case TensorType_UINT64: - *type = kTfLiteUInt64; - return kTfLiteOk; - case TensorType_STRING: - *type = kTfLiteString; - return kTfLiteOk; - case TensorType_BOOL: - *type = kTfLiteBool; - return kTfLiteOk; - case TensorType_COMPLEX64: - *type = kTfLiteComplex64; - return kTfLiteOk; - case TensorType_COMPLEX128: - *type = kTfLiteComplex128; - return kTfLiteOk; - case TensorType_RESOURCE: - *type = kTfLiteResource; - return kTfLiteOk; - case TensorType_VARIANT: - *type = kTfLiteVariant; - return kTfLiteOk; - default: - *type = kTfLiteNoType; - TF_LITE_REPORT_ERROR(error_reporter, - "Unsupported data type %d in tensor\n", tensor_type); - return kTfLiteError; - } -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseAbs(const Operator*, ErrorReporter*, BuiltinDataAllocator*, - void**) { - return kTfLiteOk; -} - -TfLiteStatus ParseAdd(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const AddOptions* schema_params = op->builtin_options_as_AddOptions(); - - if (schema_params != nullptr) { - params->activation = - ConvertActivation(schema_params->fused_activation_function()); - params->pot_scale_int16 = schema_params->pot_scale_int16(); - } else { - // TODO(b/157480169): We should either return kTfLiteError or fill in some - // reasonable defaults in the params struct. We are not doing so until we - // better undertand the ramifications of changing the legacy behavior. - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -TfLiteStatus ParseAddN(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data) { - return kTfLiteOk; -} - -TfLiteStatus ParseArgMax(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const ArgMaxOptions* schema_params = op->builtin_options_as_ArgMaxOptions(); - - if (schema_params != nullptr) { - TF_LITE_ENSURE_STATUS(ConvertTensorType( - schema_params->output_type(), ¶ms->output_type, error_reporter)); - } else { - // TODO(b/157480169): We should either return kTfLiteError or fill in some - // reasonable defaults in the params struct. We are not doing so until we - // better undertand the ramifications of changing the legacy behavior. - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -TfLiteStatus ParseArgMin(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const ArgMinOptions* schema_params = op->builtin_options_as_ArgMinOptions(); - - if (schema_params != nullptr) { - TF_LITE_ENSURE_STATUS(ConvertTensorType( - schema_params->output_type(), ¶ms->output_type, error_reporter)); - } else { - // TODO(b/157480169): We should either return kTfLiteError or fill in some - // reasonable defaults in the params struct. We are not doing so until we - // better undertand the ramifications of changing the legacy behavior. - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseAssignVariable(const Operator*, ErrorReporter*, - BuiltinDataAllocator*, void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseBatchMatMul(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - auto params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - if (const auto* bmm_params = op->builtin_options_as_BatchMatMulOptions()) { - params->adj_x = bmm_params->adj_x(); - params->adj_y = bmm_params->adj_y(); - params->asymmetric_quantize_inputs = - bmm_params->asymmetric_quantize_inputs(); - } - *builtin_data = params.release(); - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseBatchToSpaceNd(const Operator*, ErrorReporter*, - BuiltinDataAllocator*, void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseBroadcastArgs(const Operator*, ErrorReporter*, - BuiltinDataAllocator*, void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseBroadcastTo(const Operator*, ErrorReporter*, - BuiltinDataAllocator*, void**) { - return kTfLiteOk; -} - -TfLiteStatus ParseCallOnce(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const CallOnceOptions* schema_params = - op->builtin_options_as_CallOnceOptions(); - - if (schema_params != nullptr) { - params->init_subgraph_index = schema_params->init_subgraph_index(); - - } else { - // TODO(b/157480169): We should either return kTfLiteError or fill in some - // reasonable defaults in the params struct. We are not doing so until we - // better undertand the ramifications of changing the legacy behavior. - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseCast(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - auto params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - if (const auto* schema_params = op->builtin_options_as_CastOptions()) { - TF_LITE_ENSURE_STATUS(ConvertTensorType( - schema_params->in_data_type(), ¶ms->in_data_type, error_reporter)); - TF_LITE_ENSURE_STATUS(ConvertTensorType(schema_params->out_data_type(), - ¶ms->out_data_type, - error_reporter)); - } - *builtin_data = params.release(); - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseCeil(const Operator*, ErrorReporter*, BuiltinDataAllocator*, - void**) { - return kTfLiteOk; -} - -TfLiteStatus ParseConcatenation(const Operator* op, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const ConcatenationOptions* schema_params = - op->builtin_options_as_ConcatenationOptions(); - - if (schema_params != nullptr) { - params->activation = - ConvertActivation(schema_params->fused_activation_function()); - params->axis = schema_params->axis(); - } else { - // TODO(b/157480169): We should either return kTfLiteError or fill in some - // reasonable defaults in the params struct. We are not doing so until we - // better undertand the ramifications of changing the legacy behavior. - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -TfLiteStatus ParseConv2D(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const Conv2DOptions* schema_params = op->builtin_options_as_Conv2DOptions(); - - if (schema_params != nullptr) { - params->padding = ConvertPadding(schema_params->padding()); - params->stride_width = schema_params->stride_w(); - params->stride_height = schema_params->stride_h(); - params->activation = - ConvertActivation(schema_params->fused_activation_function()); - - params->dilation_width_factor = schema_params->dilation_w_factor(); - params->dilation_height_factor = schema_params->dilation_h_factor(); - } else { - // TODO(b/157480169): We should either return kTfLiteError or fill in some - // reasonable defaults in the params struct. We are not doing so until we - // better undertand the ramifications of changing the legacy behavior. - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseCumsum(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - auto params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - if (const auto* cumsum_params = op->builtin_options_as_CumsumOptions()) { - params->exclusive = cumsum_params->exclusive(); - params->reverse = cumsum_params->reverse(); - } - *builtin_data = params.release(); - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseCos(const Operator*, ErrorReporter*, BuiltinDataAllocator*, - void**) { - return kTfLiteOk; -} - -TfLiteStatus ParseDepthToSpace(const Operator* op, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const auto* schema_params = op->builtin_options_as_DepthToSpaceOptions(); - if (schema_params != nullptr) { - params->block_size = schema_params->block_size(); - } else { - // TODO(b/157480169): We should either return kTfLiteError or fill in some - // reasonable defaults in the params struct. We are not doing so until we - // better undertand the ramifications of changing the legacy behavior. - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -TfLiteStatus ParseDepthwiseConv2D(const Operator* op, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const DepthwiseConv2DOptions* schema_params = - op->builtin_options_as_DepthwiseConv2DOptions(); - - if (schema_params != nullptr) { - params->padding = ConvertPadding(schema_params->padding()); - params->stride_width = schema_params->stride_w(); - params->stride_height = schema_params->stride_h(); - params->depth_multiplier = schema_params->depth_multiplier(); - params->activation = - ConvertActivation(schema_params->fused_activation_function()); - - params->dilation_width_factor = schema_params->dilation_w_factor(); - params->dilation_height_factor = schema_params->dilation_h_factor(); - } else { - // TODO(b/157480169): We should either return kTfLiteError or fill in some - // reasonable defaults in the params struct. We are not doing so until we - // better undertand the ramifications of changing the legacy behavior. - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseDequantize(const Operator*, ErrorReporter*, - BuiltinDataAllocator*, void**) { - return kTfLiteOk; -} - -TfLiteStatus ParseDiv(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - auto params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - if (const auto* schema_params = op->builtin_options_as_DivOptions()) { - params->activation = - ConvertActivation(schema_params->fused_activation_function()); - } - *builtin_data = params.release(); - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseElu(const Operator*, ErrorReporter*, BuiltinDataAllocator*, - void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseEqual(const Operator*, ErrorReporter*, BuiltinDataAllocator*, - void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseExp(const Operator*, ErrorReporter*, BuiltinDataAllocator*, - void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseExpandDims(const Operator*, ErrorReporter*, - BuiltinDataAllocator*, void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseFill(const Operator*, ErrorReporter*, BuiltinDataAllocator*, - void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseFloor(const Operator*, ErrorReporter*, BuiltinDataAllocator*, - void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseFloorDiv(const Operator*, ErrorReporter*, - BuiltinDataAllocator*, void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseFloorMod(const Operator*, ErrorReporter*, - BuiltinDataAllocator*, void**) { - return kTfLiteOk; -} - -TfLiteStatus ParseFullyConnected(const Operator* op, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const FullyConnectedOptions* schema_params = - op->builtin_options_as_FullyConnectedOptions(); - - if (schema_params != nullptr) { - params->activation = - ConvertActivation(schema_params->fused_activation_function()); - params->keep_num_dims = schema_params->keep_num_dims(); - params->asymmetric_quantize_inputs = - schema_params->asymmetric_quantize_inputs(); - - switch (schema_params->weights_format()) { - case FullyConnectedOptionsWeightsFormat_DEFAULT: - params->weights_format = kTfLiteFullyConnectedWeightsFormatDefault; - break; - case FullyConnectedOptionsWeightsFormat_SHUFFLED4x16INT8: - params->weights_format = - kTfLiteFullyConnectedWeightsFormatShuffled4x16Int8; - break; - default: - TF_LITE_REPORT_ERROR(error_reporter, - "Unhandled fully-connected weights format."); - return kTfLiteError; - } - } else { - // TODO(b/157480169): We should either return kTfLiteError or fill in some - // reasonable defaults in the params struct. We are not doing so until we - // better undertand the ramifications of changing the legacy behavior. - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseGather(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - auto params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - params->axis = 0; - params->batch_dims = 0; - if (const auto* gather_params = op->builtin_options_as_GatherOptions()) { - params->axis = gather_params->axis(); - params->batch_dims = gather_params->batch_dims(); - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseGatherNd(const Operator*, ErrorReporter*, - BuiltinDataAllocator*, void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseGreater(const Operator*, ErrorReporter*, - BuiltinDataAllocator*, void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseGreaterEqual(const Operator*, ErrorReporter*, - BuiltinDataAllocator*, void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseHardSwish(const Operator*, ErrorReporter*, - BuiltinDataAllocator*, void**) { - return kTfLiteOk; -} - -TfLiteStatus ParseIf(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const IfOptions* schema_params = op->builtin_options_as_IfOptions(); - - if (schema_params != nullptr) { - params->then_subgraph_index = schema_params->then_subgraph_index(); - params->else_subgraph_index = schema_params->else_subgraph_index(); - } else { - // TODO(b/157480169): We should either return kTfLiteError or fill in some - // reasonable defaults in the params struct. We are not doing so until we - // better undertand the ramifications of changing the legacy behavior. - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -TfLiteStatus ParseL2Normalization(const Operator* op, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const L2NormOptions* schema_params = op->builtin_options_as_L2NormOptions(); - - if (schema_params != nullptr) { - params->activation = - ConvertActivation(schema_params->fused_activation_function()); - } else { - // TODO(b/157480169): We should either return kTfLiteError or fill in some - // reasonable defaults in the params struct. We are not doing so until we - // better undertand the ramifications of changing the legacy behavior. - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -TfLiteStatus ParseLeakyRelu(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - auto params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - if (const auto* leaky_relu_params = - op->builtin_options_as_LeakyReluOptions()) { - params->alpha = leaky_relu_params->alpha(); - } - *builtin_data = params.release(); - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseLess(const Operator*, ErrorReporter*, BuiltinDataAllocator*, - void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseLessEqual(const Operator*, ErrorReporter*, - BuiltinDataAllocator*, void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseLog(const Operator*, ErrorReporter*, BuiltinDataAllocator*, - void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseLogicalAnd(const Operator*, ErrorReporter*, - BuiltinDataAllocator*, void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseLogicalNot(const Operator*, ErrorReporter*, - BuiltinDataAllocator*, void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseLogicalOr(const Operator*, ErrorReporter*, - BuiltinDataAllocator*, void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseLogistic(const Operator*, ErrorReporter*, - BuiltinDataAllocator*, void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseLogSoftmax(const Operator*, ErrorReporter*, - BuiltinDataAllocator*, void**) { - return kTfLiteOk; -} - -TfLiteStatus ParseLSTM(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - auto params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - if (const auto* lstm_params = op->builtin_options_as_LSTMOptions()) { - params->activation = - ConvertActivation(lstm_params->fused_activation_function()); - params->cell_clip = lstm_params->cell_clip(); - params->proj_clip = lstm_params->proj_clip(); - switch (lstm_params->kernel_type()) { - case LSTMKernelType_FULL: - params->kernel_type = kTfLiteLSTMFullKernel; - break; - case LSTMKernelType_BASIC: - params->kernel_type = kTfLiteLSTMBasicKernel; - break; - default: - TF_LITE_REPORT_ERROR(error_reporter, "Unhandled LSTM kernel type: %d", - lstm_params->kernel_type()); - return kTfLiteError; - } - params->asymmetric_quantize_inputs = - lstm_params->asymmetric_quantize_inputs(); - } else { - TF_LITE_REPORT_ERROR(error_reporter, "No valid LSTM builtin options exist"); - return kTfLiteError; - } - *builtin_data = params.release(); - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseMaximum(const Operator*, ErrorReporter*, - BuiltinDataAllocator*, void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseMinimum(const Operator*, ErrorReporter*, - BuiltinDataAllocator*, void**) { - return kTfLiteOk; -} - -TfLiteStatus ParseMirrorPad(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const MirrorPadOptions* schema_params = - op->builtin_options_as_MirrorPadOptions(); - - if (schema_params != nullptr) { - params->mode = ConvertMirrorPadding(schema_params->mode()); - } else { - // TODO(b/157480169): We should either return kTfLiteError or fill in some - // reasonable defaults in the params struct. We are not doing so until we - // better undertand the ramifications of changing the legacy behavior. - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -TfLiteStatus ParseMul(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const MulOptions* schema_params = op->builtin_options_as_MulOptions(); - - if (schema_params != nullptr) { - params->activation = - ConvertActivation(schema_params->fused_activation_function()); - } else { - // TODO(b/157480169): We should either return kTfLiteError or fill in some - // reasonable defaults in the params struct. We are not doing so until we - // better undertand the ramifications of changing the legacy behavior. - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseNeg(const Operator*, ErrorReporter*, BuiltinDataAllocator*, - void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseNotEqual(const Operator*, ErrorReporter*, - BuiltinDataAllocator*, void**) { - return kTfLiteOk; -} - -TfLiteStatus ParsePack(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const PackOptions* schema_params = op->builtin_options_as_PackOptions(); - - if (schema_params != nullptr) { - params->values_count = schema_params->values_count(); - params->axis = schema_params->axis(); - } else { - // TODO(b/157480169): We should either return kTfLiteError or fill in some - // reasonable defaults in the params struct. We are not doing so until we - // better undertand the ramifications of changing the legacy behavior. - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParsePad(const Operator*, ErrorReporter*, BuiltinDataAllocator*, - void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParsePadV2(const Operator*, ErrorReporter*, BuiltinDataAllocator*, - void**) { - return kTfLiteOk; -} - -TfLiteStatus ParsePool(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const Pool2DOptions* schema_params = op->builtin_options_as_Pool2DOptions(); - - if (schema_params != nullptr) { - params->padding = ConvertPadding(schema_params->padding()); - params->stride_width = schema_params->stride_w(); - params->stride_height = schema_params->stride_h(); - params->filter_width = schema_params->filter_width(); - params->filter_height = schema_params->filter_height(); - params->activation = - ConvertActivation(schema_params->fused_activation_function()); - } else { - // TODO(b/157480169): We should either return kTfLiteError or fill in some - // reasonable defaults in the params struct. We are not doing so until we - // better undertand the ramifications of changing the legacy behavior. - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParsePow(const Operator*, ErrorReporter*, BuiltinDataAllocator*, - void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParsePrelu(const Operator*, ErrorReporter*, BuiltinDataAllocator*, - void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseQuantize(const Operator*, ErrorReporter*, - BuiltinDataAllocator*, void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseReadVariable(const Operator*, ErrorReporter*, - BuiltinDataAllocator*, void**) { - return kTfLiteOk; -} - -TfLiteStatus ParseReducer(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const ReducerOptions* schema_params = op->builtin_options_as_ReducerOptions(); - - if (schema_params != nullptr) { - params->keep_dims = schema_params->keep_dims(); - } else { - // TODO(b/157480169): We should either return kTfLiteError or fill in some - // reasonable defaults in the params struct. We are not doing so until we - // better undertand the ramifications of changing the legacy behavior. - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseRelu(const Operator*, ErrorReporter*, BuiltinDataAllocator*, - void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseRelu6(const Operator*, ErrorReporter*, BuiltinDataAllocator*, - void**) { - return kTfLiteOk; -} - -TfLiteStatus ParseReshape(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const ReshapeOptions* schema_params = op->builtin_options_as_ReshapeOptions(); - - if (schema_params != nullptr) { - const flatbuffers::Vector* new_shape = schema_params->new_shape(); - if (new_shape != nullptr) { - TF_LITE_ENSURE_STATUS( - FlatBufferIntVectorToArray(sizeof(params->shape), new_shape, - params->shape, error_reporter, "reshape")); - params->num_dimensions = new_shape->size(); - } else { - // TODO(b/157480169) TODO(b/147203660): We should either return - // kTfLiteError or fill in some reasonable defaults in the params struct. - // We are not doing so until we better undertand the ramifications of - // changing the legacy behavior. - } - } else { - // TODO(b/157480169): We should either return kTfLiteError or fill in some - // reasonable defaults in the params struct. We are not doing so until we - // better undertand the ramifications of changing the legacy behavior. - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -TfLiteStatus ParseResizeBilinear(const Operator* op, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const ResizeBilinearOptions* schema_params = - op->builtin_options_as_ResizeBilinearOptions(); - - if (schema_params != nullptr) { - params->align_corners = schema_params->align_corners(); - params->half_pixel_centers = schema_params->half_pixel_centers(); - } else { - params->align_corners = false; - params->half_pixel_centers = false; - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -TfLiteStatus ParseResizeNearestNeighbor(const Operator* op, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const ResizeNearestNeighborOptions* schema_params = - op->builtin_options_as_ResizeNearestNeighborOptions(); - - if (schema_params != nullptr) { - params->align_corners = schema_params->align_corners(); - params->half_pixel_centers = schema_params->half_pixel_centers(); - } else { - params->align_corners = false; - params->half_pixel_centers = false; - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseRound(const Operator*, ErrorReporter*, BuiltinDataAllocator*, - void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseRsqrt(const Operator*, ErrorReporter*, BuiltinDataAllocator*, - void**) { - return kTfLiteOk; -} - -TfLiteStatus ParseShape(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data) { - SafeBuiltinDataAllocator safe_allocator(allocator); - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const ShapeOptions* schema_params = op->builtin_options_as_ShapeOptions(); - - if (schema_params != nullptr) { - TF_LITE_ENSURE_STATUS(ConvertTensorType(schema_params->out_type(), - ¶ms->out_type, error_reporter)); - } else { - // TODO(b/157480169): We should either return kTfLiteError or fill in some - // reasonable defaults in the params struct. We are not doing so until we - // better undertand the ramifications of changing the legacy behavior. - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseSin(const Operator*, ErrorReporter*, BuiltinDataAllocator*, - void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseSlice(const Operator*, ErrorReporter*, BuiltinDataAllocator*, - void**) { - return kTfLiteOk; -} - -TfLiteStatus ParseSoftmax(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const SoftmaxOptions* schema_params = op->builtin_options_as_SoftmaxOptions(); - - if (schema_params != nullptr) { - params->beta = schema_params->beta(); - } else { - // TODO(b/157480169): We should either return kTfLiteError or fill in some - // reasonable defaults in the params struct. We are not doing so until we - // better undertand the ramifications of changing the legacy behavior. - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseSpaceToBatchNd(const Operator*, ErrorReporter*, - BuiltinDataAllocator*, void**) { - return kTfLiteOk; -} - -TfLiteStatus ParseSpaceToDepth(const Operator* op, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const auto* schema_params = op->builtin_options_as_SpaceToDepthOptions(); - if (schema_params != nullptr) { - params->block_size = schema_params->block_size(); - } else { - // TODO(b/157480169): We should either return kTfLiteError or fill in some - // reasonable defaults in the params struct. We are not doing so until we - // better undertand the ramifications of changing the legacy behavior. - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -TfLiteStatus ParseSplit(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const SplitOptions* schema_params = op->builtin_options_as_SplitOptions(); - - if (schema_params != nullptr) { - params->num_splits = schema_params->num_splits(); - } else { - // TODO(b/157480169): We should either return kTfLiteError or fill in some - // reasonable defaults in the params struct. We are not doing so until we - // better undertand the ramifications of changing the legacy behavior. - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -TfLiteStatus ParseSplitV(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - SafeBuiltinDataAllocator safe_allocator(allocator); - - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const SplitVOptions* schema_params = op->builtin_options_as_SplitVOptions(); - - if (schema_params != nullptr) { - params->num_splits = schema_params->num_splits(); - } else { - // TODO(b/157480169): We should either return kTfLiteError or fill in some - // reasonable defaults in the params struct. We are not doing so until we - // better undertand the ramifications of changing the legacy behavior. - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -TfLiteStatus ParseUnidirectionalSequenceLSTM(const Operator* op, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - SafeBuiltinDataAllocator safe_allocator(allocator); - auto params = - safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - if (const auto* seq_lstm_params = - op->builtin_options_as_UnidirectionalSequenceLSTMOptions()) { - params->activation = - ConvertActivation(seq_lstm_params->fused_activation_function()); - params->cell_clip = seq_lstm_params->cell_clip(); - params->proj_clip = seq_lstm_params->proj_clip(); - params->time_major = seq_lstm_params->time_major(); - params->asymmetric_quantize_inputs = - seq_lstm_params->asymmetric_quantize_inputs(); - } - *builtin_data = params.release(); - return kTfLiteOk; -} - -TfLiteStatus ParseSqueeze(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - SafeBuiltinDataAllocator safe_allocator(allocator); - - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const SqueezeOptions* schema_params = op->builtin_options_as_SqueezeOptions(); - - if (schema_params != nullptr) { - const auto* squeeze_dims = schema_params->squeeze_dims(); - if (squeeze_dims != nullptr) { - TF_LITE_ENSURE_STATUS(FlatBufferIntVectorToArray( - sizeof(params->squeeze_dims), squeeze_dims, params->squeeze_dims, - error_reporter, "squeeze")); - params->num_squeeze_dims = squeeze_dims->size(); - } else { - params->num_squeeze_dims = 0; - } - } else { - // TODO(b/157480169): We should either return kTfLiteError or fill in some - // reasonable defaults in the params struct. We are not doing so until we - // better undertand the ramifications of changing the legacy behavior. - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseSqrt(const Operator*, ErrorReporter*, BuiltinDataAllocator*, - void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseSquare(const Operator*, ErrorReporter*, BuiltinDataAllocator*, - void**) { - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseSquaredDifference(const Operator*, ErrorReporter*, - BuiltinDataAllocator*, void**) { - return kTfLiteOk; -} - -TfLiteStatus ParseStridedSlice(const Operator* op, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const StridedSliceOptions* schema_params = - op->builtin_options_as_StridedSliceOptions(); - - if (schema_params != nullptr) { - params->begin_mask = schema_params->begin_mask(); - params->end_mask = schema_params->end_mask(); - params->ellipsis_mask = schema_params->ellipsis_mask(); - params->new_axis_mask = schema_params->new_axis_mask(); - params->shrink_axis_mask = schema_params->shrink_axis_mask(); - } else { - // TODO(b/157480169): We should either return kTfLiteError or fill in some - // reasonable defaults in the params struct. We are not doing so until we - // better undertand the ramifications of changing the legacy behavior. - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -TfLiteStatus ParseSub(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const SubOptions* schema_params = op->builtin_options_as_SubOptions(); - - if (schema_params != nullptr) { - params->activation = - ConvertActivation(schema_params->fused_activation_function()); - params->pot_scale_int16 = schema_params->pot_scale_int16(); - } else { - // TODO(b/157480169): We should either return kTfLiteError or fill in some - // reasonable defaults in the params struct. We are not doing so until we - // better undertand the ramifications of changing the legacy behavior. - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -TfLiteStatus ParseSvdf(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const SVDFOptions* schema_params = op->builtin_options_as_SVDFOptions(); - if (schema_params != nullptr) { - params->rank = schema_params->rank(); - params->activation = - ConvertActivation(schema_params->fused_activation_function()); - params->asymmetric_quantize_inputs = - schema_params->asymmetric_quantize_inputs(); - } else { - // TODO(b/157480169): We should either return kTfLiteError or fill in some - // reasonable defaults in the params struct. We are not doing so until we - // better undertand the ramifications of changing the legacy behavior. - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseTanh(const Operator*, ErrorReporter*, BuiltinDataAllocator*, - void**) { - return kTfLiteOk; -} -// -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseTranspose(const Operator*, ErrorReporter*, - BuiltinDataAllocator*, void**) { - return kTfLiteOk; -} - -TfLiteStatus ParseTransposeConv(const Operator* op, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - const TransposeConvOptions* transpose_conv_params = - op->builtin_options_as_TransposeConvOptions(); - if (transpose_conv_params != nullptr) { - params->padding = ConvertPadding(transpose_conv_params->padding()); - params->stride_width = transpose_conv_params->stride_w(); - params->stride_height = transpose_conv_params->stride_h(); - } else { - // TODO(b/157480169): We should either return kTfLiteError or fill in some - // reasonable defaults in the params struct. We are not doing so until we - // better undertand the ramifications of changing the legacy behavior. - } - *builtin_data = params.release(); - return kTfLiteOk; -} - -TfLiteStatus ParseUnpack(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const UnpackOptions* schema_params = op->builtin_options_as_UnpackOptions(); - - if (schema_params != nullptr) { - params->num = schema_params->num(); - params->axis = schema_params->axis(); - } else { - // TODO(b/157480169): We should either return kTfLiteError or fill in some - // reasonable defaults in the params struct. We are not doing so until we - // better undertand the ramifications of changing the legacy behavior. - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -TfLiteStatus ParseVarHandle(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const VarHandleOptions* schema_params = - op->builtin_options_as_VarHandleOptions(); - - if (schema_params != nullptr) { - if (schema_params->container()) { - params->container = schema_params->container()->c_str(); - } - if (schema_params->shared_name()) { - params->shared_name = schema_params->shared_name()->c_str(); - } - } else { - // TODO(b/157480169): We should either return kTfLiteError or fill in some - // reasonable defaults in the params struct. We are not doing so until we - // better undertand the ramifications of changing the legacy behavior. - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -TfLiteStatus ParseWhile(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data) { - CheckParsePointerParams(op, error_reporter, allocator, builtin_data); - - SafeBuiltinDataAllocator safe_allocator(allocator); - std::unique_ptr - params = safe_allocator.Allocate(); - TF_LITE_ENSURE(error_reporter, params != nullptr); - - const WhileOptions* schema_params = op->builtin_options_as_WhileOptions(); - - if (schema_params != nullptr) { - params->cond_subgraph_index = schema_params->cond_subgraph_index(); - params->body_subgraph_index = schema_params->body_subgraph_index(); - } else { - // TODO(b/157480169): We should either return kTfLiteError or fill in some - // reasonable defaults in the params struct. We are not doing so until we - // better undertand the ramifications of changing the legacy behavior. - } - - *builtin_data = params.release(); - return kTfLiteOk; -} - -// We have this parse function instead of directly returning kTfLiteOk from the -// switch-case in ParseOpData because this function is used as part of the -// selective registration for the OpResolver implementation in micro. -TfLiteStatus ParseZerosLike(const Operator*, ErrorReporter*, - BuiltinDataAllocator*, void**) { - return kTfLiteOk; -} - -TfLiteStatus ParseOpData(const Operator* op, BuiltinOperator op_type, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data) { -// TODO(b/145762662): It would be preferable to have the build graph for TF Lite -// Micro not have the ParseOpData function at all. This would require splitting -// the current file into two separate files, one of which defines the -// ParseOpData function and the other that defines the operator specific parse -// functions (e.g. ParseAdd). -// -// Such a split was attempted but was not worth the effort at the time because -// of the following reasons: -// * We could either duplicate the functions and the SafeBuiltinDataAllocator -// class in the anonymous namespace of this file, or attempt to make a common -// library with these helper functions and class. -// * Making a common library with a separate build target was not feasible as -// it introduced circular dependencies due to the ErrorReporter and a common -// .cc and .h within the same api build target the also cause circular -// dependencies due to the BuiltinDataAllocator class. -// * If all the builtin operators were to have their own parse functions, or we -// were ok with some amount of code duplication, then this split of the .cc -// files would be a lot more feasible. -#ifdef TF_LITE_STATIC_MEMORY - TF_LITE_REPORT_ERROR( - error_reporter, - "ParseOpData is unsupported on TfLiteMicro, please use the operator " - "specific parse functions (e.g. ParseAdd etc.).\n"); - return kTfLiteError; -#else - return ParseOpDataTfLite(op, op_type, error_reporter, allocator, - builtin_data); -#endif -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/core/api/flatbuffer_conversions.h b/code/components/tflite-lib/tensorflow/lite/core/api/flatbuffer_conversions.h deleted file mode 100644 index ed317b81..00000000 --- a/code/components/tflite-lib/tensorflow/lite/core/api/flatbuffer_conversions.h +++ /dev/null @@ -1,408 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_CORE_API_FLATBUFFER_CONVERSIONS_H_ -#define TENSORFLOW_LITE_CORE_API_FLATBUFFER_CONVERSIONS_H_ - -// These functions transform codes and data structures that are defined in the -// flatbuffer serialization format into in-memory values that are used by the -// runtime API and interpreter. - -#include -#include -#include - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/core/api/error_reporter.h" -#include "tensorflow/lite/schema/schema_generated.h" - -namespace tflite { - -// Interface class for builtin data allocations. -class BuiltinDataAllocator { - public: - virtual void* Allocate(size_t size, size_t alignment_hint) = 0; - virtual void Deallocate(void* data) = 0; - - // Allocate a structure, but make sure it is a POD structure that doesn't - // require constructors to run. The reason we do this, is that Interpreter's C - // extension part will take ownership so destructors will not be run during - // deallocation. - template - T* AllocatePOD() { - // TODO(b/154346074): Change this to is_trivially_destructible when all - // platform targets support that properly. - static_assert(std::is_pod::value, "Builtin data structure must be POD."); - void* allocated_memory = this->Allocate(sizeof(T), alignof(T)); - return new (allocated_memory) T(); - } - - virtual ~BuiltinDataAllocator() {} -}; - -// Parse the appropriate data out of the op. -// -// This handles builtin data explicitly as there are flatbuffer schemas. -// If it returns kTfLiteOk, it passes the data out with `builtin_data`. The -// calling function has to pass in an allocator object, and this allocator -// will be called to reserve space for the output data. If the calling -// function's allocator reserves memory on the heap, then it's the calling -// function's responsibility to free it. -// If it returns kTfLiteError, `builtin_data` will be `nullptr`. -TfLiteStatus ParseOpData(const Operator* op, BuiltinOperator op_type, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -// Converts the tensor data type used in the flat buffer to the representation -// used by the runtime. -TfLiteStatus ConvertTensorType(TensorType tensor_type, TfLiteType* type, - ErrorReporter* error_reporter); - -TfLiteStatus ParseAbs(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseAdd(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseAddN(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseArgMax(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseArgMin(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseAssignVariable(const Operator* op, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseBatchMatMul(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseBatchToSpaceNd(const Operator* op, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseBroadcastArgs(const Operator* op, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseBroadcastTo(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseCallOnce(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseCeil(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseCast(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseConcatenation(const Operator* op, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseConv2D(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseCos(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseCumsum(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseDepthToSpace(const Operator* op, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseDepthwiseConv2D(const Operator* op, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseDequantize(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseDiv(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseElu(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseEqual(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseExp(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseExpandDims(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseFill(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseFloor(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseFloorDiv(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseFloorMod(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseFullyConnected(const Operator* op, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseGather(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseGatherNd(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseGreater(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseGreaterEqual(const Operator* op, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseHardSwish(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseIf(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseL2Normalization(const Operator* op, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseLeakyRelu(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseLess(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseLessEqual(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseLog(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseLogicalAnd(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseLogicalNot(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseLogicalOr(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseLogistic(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseLogSoftmax(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseLSTM(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseMaximum(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseMinimum(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseMirrorPad(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseMul(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseNeg(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseNotEqual(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParsePack(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParsePad(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParsePadV2(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParsePool(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParsePow(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParsePrelu(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseQuantize(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseReadVariable(const Operator* op, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseReducer(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseRelu(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseRelu6(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseReshape(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseResizeBilinear(const Operator* op, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseResizeNearestNeighbor(const Operator* op, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseRound(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseRsqrt(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseShape(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseSin(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseSlice(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseSoftmax(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseSpaceToBatchNd(const Operator* op, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseSpaceToDepth(const Operator* op, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseSplit(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseSplitV(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseSqueeze(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseSqrt(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseSquare(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseSquaredDifference(const Operator* op, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseStridedSlice(const Operator* op, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseSub(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseSvdf(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseTanh(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseTranspose(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseTransposeConv(const Operator* op, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseUnpack(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseUnidirectionalSequenceLSTM(const Operator* op, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseVarHandle(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -TfLiteStatus ParseWhile(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, void** builtin_data); - -TfLiteStatus ParseZerosLike(const Operator* op, ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - -} // namespace tflite - -#endif // TENSORFLOW_LITE_CORE_API_FLATBUFFER_CONVERSIONS_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/core/api/op_resolver.cc b/code/components/tflite-lib/tensorflow/lite/core/api/op_resolver.cc deleted file mode 100644 index 6f7e4c2a..00000000 --- a/code/components/tflite-lib/tensorflow/lite/core/api/op_resolver.cc +++ /dev/null @@ -1,68 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/core/api/op_resolver.h" - -#include "flatbuffers/flatbuffers.h" // from @flatbuffers -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/core/api/error_reporter.h" -#include "tensorflow/lite/schema/schema_utils.h" - -namespace tflite { - -TfLiteStatus GetRegistrationFromOpCode( - const OperatorCode* opcode, const OpResolver& op_resolver, - ErrorReporter* error_reporter, const TfLiteRegistration** registration) { - TfLiteStatus status = kTfLiteOk; - *registration = nullptr; - auto builtin_code = GetBuiltinCode(opcode); - int version = opcode->version(); - - if (builtin_code > BuiltinOperator_MAX) { - TF_LITE_REPORT_ERROR( - error_reporter, - "Op builtin_code out of range: %d. Are you using old TFLite binary " - "with newer model?", - builtin_code); - status = kTfLiteError; - } else if (builtin_code != BuiltinOperator_CUSTOM) { - *registration = op_resolver.FindOp(builtin_code, version); - if (*registration == nullptr) { - TF_LITE_REPORT_ERROR( - error_reporter, - "Didn't find op for builtin opcode '%s' version '%d'. " - "An older version of this builtin might be supported. " - "Are you using an old TFLite binary with a newer model?\n", - EnumNameBuiltinOperator(builtin_code), version); - status = kTfLiteError; - } - } else if (!opcode->custom_code()) { - TF_LITE_REPORT_ERROR( - error_reporter, - "Operator with CUSTOM builtin_code has no custom_code.\n"); - status = kTfLiteError; - } else { - const char* name = opcode->custom_code()->c_str(); - *registration = op_resolver.FindOp(name, version); - if (*registration == nullptr) { - // Do not report error for unresolved custom op, we do the final check - // while preparing ops. - status = kTfLiteError; - } - } - return status; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/core/api/op_resolver.h b/code/components/tflite-lib/tensorflow/lite/core/api/op_resolver.h deleted file mode 100644 index cec1f2dd..00000000 --- a/code/components/tflite-lib/tensorflow/lite/core/api/op_resolver.h +++ /dev/null @@ -1,140 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_CORE_API_OP_RESOLVER_H_ -#define TENSORFLOW_LITE_CORE_API_OP_RESOLVER_H_ - -#include -#include -#include - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/core/api/error_reporter.h" -#include "tensorflow/lite/schema/schema_generated.h" - -// Opaque type similar to TfLiteDelegate / TfLiteOpaqueDelegate. -// This is used for cases (e.g. when using "TF Lite with Google Play Services") -// where the TF Lite runtime might be built using a newer (or older) -// version of the TF Lite sources than the app, and hence might have a -// different definition of the TfLiteDelegate type. TF Lite APIs use -// TfLiteOpaqueDelegate rather than TfLiteDelegate when they want to -// refer to a delegate defined with that potentially different version -// of the TfLiteDelegate type. -struct TfLiteOpaqueDelegateStruct; - -namespace tflite { - -/// Abstract interface that returns TfLiteRegistrations given op codes or custom -/// op names. This is the mechanism that ops being referenced in the flatbuffer -/// model are mapped to executable function pointers (TfLiteRegistrations). -class OpResolver { - public: - /// Finds the op registration for a builtin operator by enum code. - virtual const TfLiteRegistration* FindOp(tflite::BuiltinOperator op, - int version) const = 0; - /// Finds the op registration of a custom operator by op name. - virtual const TfLiteRegistration* FindOp(const char* op, - int version) const = 0; - - // Represents a sequence of delegates. - using TfLiteDelegatePtrVector = - std::vector>; - - // Returns optional delegates for resolving and handling ops in the flatbuffer - // model. This may be used in addition to the standard TfLiteRegistration - // lookup for graph resolution. - // WARNING: This API is deprecated, GetDelegateCreators is preferred. - virtual TfLiteDelegatePtrVector GetDelegates(int num_threads) const { - return {}; - } - - // Represents a function that creates a TfLite delegate instance. - using TfLiteDelegateCreator = - std::function( - int /*num_threads*/)>; - - // Represents a sequence of delegate creator functions. - using TfLiteDelegateCreators = std::vector; - - // Returns a vector of delegate creators to create optional delegates for - // resolving and handling ops in the flatbuffer model. This may be used in - // addition to the standard TfLiteRegistration lookup for graph resolution. - // - // Note that this method is not used (will not be called) if you are using - // TF Lite in Google Play Services; the GetOpaqueDelegateCreators method - // (see below) is used for that case. - virtual TfLiteDelegateCreators GetDelegateCreators() const { return {}; } - - // TODO(b/202712825): it would be nice if we could avoid the need for separate - // "opaque" types & methods for use only with TF Lite in Google Play Services. - - // Represents an opaque delegate instance. - // WARNING: Experimental interface, subject to change. - using TfLiteOpaqueDelegatePtr = - std::unique_ptr; - - // Represents a function that creates an opaque delegate instance. - // WARNING: Experimental interface, subject to change. - using TfLiteOpaqueDelegateCreator = - std::function; - - // Represents a sequence of opaque delegate creator functions. - // WARNING: Experimental interface, subject to change. - using TfLiteOpaqueDelegateCreators = std::vector; - - // Returns a vector of opaque delegate creators to create optional opaque - // delegates for resolving and handling ops in the flatbuffer model. This may - // be used in addition to the standard TfLiteRegistration lookup for graph - // resolution. - // - // Note that this method will be called only if you are using TF Lite in - // Google Play Services; if you are using regular TF Lite, GetDelegateCreators - // (see above) is used instead. - // - // WARNING: Experimental interface, subject to change. - virtual TfLiteOpaqueDelegateCreators GetOpaqueDelegateCreators() const { - return {}; - } - - virtual ~OpResolver() {} - - private: - /// Returns true if this OpResolver may contain any "user defined" ops. - /// By "user defined" ops, we mean any op definitions other than those - /// contained in tflite::ops::builtin::BuiltinOpResolver. - /// - /// If this method returns true, it doesn't necessarily mean that the - /// OpResolver contains a user-defined op, just that the absence of - /// user-defined ops can't be guaranteed. - /// - /// Note that "user-defined" ops are not the same as "custom" ops; - /// BuiltinOpResolver may support certain "custom" ops, in addition to - /// "builtin" ops, and may not support all of the "builtin" op enum values. - virtual bool MayContainUserDefinedOps() const { return true; } - - friend class OpResolverInternal; -}; - -// Handles the logic for converting between an OperatorCode structure extracted -// from a flatbuffer and information about a registered operator -// implementation. -TfLiteStatus GetRegistrationFromOpCode(const OperatorCode* opcode, - const OpResolver& op_resolver, - ErrorReporter* error_reporter, - const TfLiteRegistration** registration); - -} // namespace tflite - -#endif // TENSORFLOW_LITE_CORE_API_OP_RESOLVER_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/core/api/tensor_utils.cc b/code/components/tflite-lib/tensorflow/lite/core/api/tensor_utils.cc deleted file mode 100644 index 3aac16b6..00000000 --- a/code/components/tflite-lib/tensorflow/lite/core/api/tensor_utils.cc +++ /dev/null @@ -1,50 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/core/api/tensor_utils.h" - -#include - -#include "tensorflow/lite/c/common.h" - -namespace tflite { - -TfLiteStatus ResetVariableTensor(TfLiteTensor* tensor) { - if (!tensor->is_variable) { - return kTfLiteOk; - } - // TODO(b/115961645): Implement - If a variable tensor has a buffer, reset it - // to the value of the buffer. - int value = 0; - if (tensor->type == kTfLiteInt8) { - value = tensor->params.zero_point; - } - // TODO(b/139446230): Provide a platform header to better handle these - // specific scenarios. -#if __ANDROID__ || defined(__x86_64__) || defined(__i386__) || \ - defined(__i386) || defined(__x86__) || defined(__X86__) || \ - defined(_X86_) || defined(_M_IX86) || defined(_M_X64) - memset(tensor->data.raw, value, tensor->bytes); -#else - char* raw_ptr = tensor->data.raw; - for (size_t i = 0; i < tensor->bytes; ++i) { - *raw_ptr = value; - raw_ptr++; - } -#endif - return kTfLiteOk; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/core/api/tensor_utils.h b/code/components/tflite-lib/tensorflow/lite/core/api/tensor_utils.h deleted file mode 100644 index 9f1cf94a..00000000 --- a/code/components/tflite-lib/tensorflow/lite/core/api/tensor_utils.h +++ /dev/null @@ -1,28 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_CORE_API_TENSOR_UTILS_H_ -#define TENSORFLOW_LITE_CORE_API_TENSOR_UTILS_H_ - -#include "tensorflow/lite/c/common.h" - -namespace tflite { - -// Resets a variable tensor to the default value. -TfLiteStatus ResetVariableTensor(TfLiteTensor* tensor); - -} // namespace tflite - -#endif // TENSORFLOW_LITE_CORE_API_TENSOR_UTILS_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/bits.h b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/bits.h deleted file mode 100644 index 04b3ba6f..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/bits.h +++ /dev/null @@ -1,102 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_BITS_H_ -#define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_BITS_H_ - -#ifdef __cplusplus -#include - -extern "C" { -#endif - -static inline int CountLeadingZeros32Slow(uint64_t n) { - int zeroes = 28; - if (n >> 16) zeroes -= 16, n >>= 16; - if (n >> 8) zeroes -= 8, n >>= 8; - if (n >> 4) zeroes -= 4, n >>= 4; - return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[n] + zeroes; -} - -static inline int CountLeadingZeros32(uint32_t n) { -#if defined(_MSC_VER) - unsigned long result = 0; // NOLINT(runtime/int) - if (_BitScanReverse(&result, n)) { - return 31 - result; - } - return 32; -#elif defined(__GNUC__) - - // Handle 0 as a special case because __builtin_clz(0) is undefined. - if (n == 0) { - return 32; - } - return __builtin_clz(n); -#else - return CountLeadingZeros32Slow(n); -#endif -} - -static inline int MostSignificantBit32(uint32_t n) { - return 32 - CountLeadingZeros32(n); -} - -static inline int CountLeadingZeros64Slow(uint64_t n) { - int zeroes = 60; - if (n >> 32) zeroes -= 32, n >>= 32; - if (n >> 16) zeroes -= 16, n >>= 16; - if (n >> 8) zeroes -= 8, n >>= 8; - if (n >> 4) zeroes -= 4, n >>= 4; - return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[n] + zeroes; -} - -static inline int CountLeadingZeros64(uint64_t n) { -#if defined(_MSC_VER) && defined(_M_X64) - // MSVC does not have __builtin_clzll. Use _BitScanReverse64. - unsigned long result = 0; // NOLINT(runtime/int) - if (_BitScanReverse64(&result, n)) { - return 63 - result; - } - return 64; -#elif defined(_MSC_VER) - // MSVC does not have __builtin_clzll. Compose two calls to _BitScanReverse - unsigned long result = 0; // NOLINT(runtime/int) - if ((n >> 32) && _BitScanReverse(&result, n >> 32)) { - return 31 - result; - } - if (_BitScanReverse(&result, n)) { - return 63 - result; - } - return 64; -#elif defined(__GNUC__) - - // Handle 0 as a special case because __builtin_clzll(0) is undefined. - if (n == 0) { - return 64; - } - return __builtin_clzll(n); -#else - return CountLeadingZeros64Slow(n); -#endif -} - -static inline int MostSignificantBit64(uint64_t n) { - return 64 - CountLeadingZeros64(n); -} - -#ifdef __cplusplus -} // extern "C" -#endif - -#endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_BITS_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/fft.cc b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/fft.cc deleted file mode 100644 index bcdd9cc0..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/fft.cc +++ /dev/null @@ -1,52 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/experimental/microfrontend/lib/fft.h" - -#include - -#include "tensorflow/lite/experimental/microfrontend/lib/kiss_fft_int16.h" - -void FftCompute(struct FftState* state, const int16_t* input, - int input_scale_shift) { - const size_t input_size = state->input_size; - const size_t fft_size = state->fft_size; - - int16_t* fft_input = state->input; - // First, scale the input by the given shift. - size_t i; - for (i = 0; i < input_size; ++i) { - fft_input[i] = static_cast(static_cast(input[i]) - << input_scale_shift); - } - // Zero out whatever else remains in the top part of the input. - for (; i < fft_size; ++i) { - fft_input[i] = 0; - } - - // Apply the FFT. - kissfft_fixed16::kiss_fftr( - reinterpret_cast(state->scratch), - state->input, - reinterpret_cast(state->output)); -} - -void FftInit(struct FftState* state) { - // All the initialization is done in FftPopulateState() -} - -void FftReset(struct FftState* state) { - memset(state->input, 0, state->fft_size * sizeof(*state->input)); - memset(state->output, 0, (state->fft_size / 2 + 1) * sizeof(*state->output)); -} diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/fft.h b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/fft.h deleted file mode 100644 index aaffa69d..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/fft.h +++ /dev/null @@ -1,50 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FFT_H_ -#define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FFT_H_ - -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -struct complex_int16_t { - int16_t real; - int16_t imag; -}; - -struct FftState { - int16_t* input; - struct complex_int16_t* output; - size_t fft_size; - size_t input_size; - void* scratch; - size_t scratch_size; -}; - -void FftCompute(struct FftState* state, const int16_t* input, - int input_scale_shift); - -void FftInit(struct FftState* state); - -void FftReset(struct FftState* state); - -#ifdef __cplusplus -} // extern "C" -#endif - -#endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FFT_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/fft_util.cc b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/fft_util.cc deleted file mode 100644 index ed3dc8fb..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/fft_util.cc +++ /dev/null @@ -1,70 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/experimental/microfrontend/lib/fft_util.h" - -#include - -#include "tensorflow/lite/experimental/microfrontend/lib/kiss_fft_int16.h" - -int FftPopulateState(struct FftState* state, size_t input_size) { - state->input_size = input_size; - state->fft_size = 1; - while (state->fft_size < state->input_size) { - state->fft_size <<= 1; - } - - state->input = reinterpret_cast( - malloc(state->fft_size * sizeof(*state->input))); - if (state->input == nullptr) { - fprintf(stderr, "Failed to alloc fft input buffer\n"); - return 0; - } - - state->output = reinterpret_cast( - malloc((state->fft_size / 2 + 1) * sizeof(*state->output) * 2)); - if (state->output == nullptr) { - fprintf(stderr, "Failed to alloc fft output buffer\n"); - return 0; - } - - // Ask kissfft how much memory it wants. - size_t scratch_size = 0; - kissfft_fixed16::kiss_fftr_cfg kfft_cfg = kissfft_fixed16::kiss_fftr_alloc( - state->fft_size, 0, nullptr, &scratch_size); - if (kfft_cfg != nullptr) { - fprintf(stderr, "Kiss memory sizing failed.\n"); - return 0; - } - state->scratch = malloc(scratch_size); - if (state->scratch == nullptr) { - fprintf(stderr, "Failed to alloc fft scratch buffer\n"); - return 0; - } - state->scratch_size = scratch_size; - // Let kissfft configure the scratch space we just allocated - kfft_cfg = kissfft_fixed16::kiss_fftr_alloc(state->fft_size, 0, - state->scratch, &scratch_size); - if (kfft_cfg != state->scratch) { - fprintf(stderr, "Kiss memory preallocation strategy failed.\n"); - return 0; - } - return 1; -} - -void FftFreeStateContents(struct FftState* state) { - free(state->input); - free(state->output); - free(state->scratch); -} diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/fft_util.h b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/fft_util.h deleted file mode 100644 index 6a471301..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/fft_util.h +++ /dev/null @@ -1,34 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FFT_UTIL_H_ -#define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FFT_UTIL_H_ - -#include "tensorflow/lite/experimental/microfrontend/lib/fft.h" - -#ifdef __cplusplus -extern "C" { -#endif - -// Prepares and FFT for the given input size. -int FftPopulateState(struct FftState* state, size_t input_size); - -// Frees any allocated buffers. -void FftFreeStateContents(struct FftState* state); - -#ifdef __cplusplus -} // extern "C" -#endif - -#endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FFT_UTIL_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/filterbank.c b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/filterbank.c deleted file mode 100644 index 80f8738f..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/filterbank.c +++ /dev/null @@ -1,134 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/experimental/microfrontend/lib/filterbank.h" - -#include - -#include "tensorflow/lite/experimental/microfrontend/lib/bits.h" - -void FilterbankConvertFftComplexToEnergy(struct FilterbankState* state, - struct complex_int16_t* fft_output, - int32_t* energy) { - const int end_index = state->end_index; - int i; - energy += state->start_index; - fft_output += state->start_index; - for (i = state->start_index; i < end_index; ++i) { - const int32_t real = fft_output->real; - const int32_t imag = fft_output->imag; - fft_output++; - const uint32_t mag_squared = (real * real) + (imag * imag); - *energy++ = mag_squared; - } -} - -void FilterbankAccumulateChannels(struct FilterbankState* state, - const int32_t* energy) { - uint64_t* work = state->work; - uint64_t weight_accumulator = 0; - uint64_t unweight_accumulator = 0; - - const int16_t* channel_frequency_starts = state->channel_frequency_starts; - const int16_t* channel_weight_starts = state->channel_weight_starts; - const int16_t* channel_widths = state->channel_widths; - - int num_channels_plus_1 = state->num_channels + 1; - int i; - for (i = 0; i < num_channels_plus_1; ++i) { - const int32_t* magnitudes = energy + *channel_frequency_starts++; - const int16_t* weights = state->weights + *channel_weight_starts; - const int16_t* unweights = state->unweights + *channel_weight_starts++; - const int width = *channel_widths++; - int j; - for (j = 0; j < width; ++j) { - weight_accumulator += *weights++ * ((uint64_t)*magnitudes); - unweight_accumulator += *unweights++ * ((uint64_t)*magnitudes); - ++magnitudes; - } - *work++ = weight_accumulator; - weight_accumulator = unweight_accumulator; - unweight_accumulator = 0; - } -} - -static uint16_t Sqrt32(uint32_t num) { - if (num == 0) { - return 0; - } - uint32_t res = 0; - int max_bit_number = 32 - MostSignificantBit32(num); - max_bit_number |= 1; - uint32_t bit = 1U << (31 - max_bit_number); - int iterations = (31 - max_bit_number) / 2 + 1; - while (iterations--) { - if (num >= res + bit) { - num -= res + bit; - res = (res >> 1U) + bit; - } else { - res >>= 1U; - } - bit >>= 2U; - } - // Do rounding - if we have the bits. - if (num > res && res != 0xFFFF) { - ++res; - } - return res; -} - -static uint32_t Sqrt64(uint64_t num) { - // Take a shortcut and just use 32 bit operations if the upper word is all - // clear. This will cause a slight off by one issue for numbers close to 2^32, - // but it probably isn't going to matter (and gives us a big performance win). - if ((num >> 32) == 0) { - return Sqrt32((uint32_t)num); - } - uint64_t res = 0; - int max_bit_number = 64 - MostSignificantBit64(num); - max_bit_number |= 1; - uint64_t bit = 1ULL << (63 - max_bit_number); - int iterations = (63 - max_bit_number) / 2 + 1; - while (iterations--) { - if (num >= res + bit) { - num -= res + bit; - res = (res >> 1U) + bit; - } else { - res >>= 1U; - } - bit >>= 2U; - } - // Do rounding - if we have the bits. - if (num > res && res != 0xFFFFFFFFLL) { - ++res; - } - return res; -} - -uint32_t* FilterbankSqrt(struct FilterbankState* state, int scale_down_shift) { - const int num_channels = state->num_channels; - const uint64_t* work = state->work + 1; - // Reuse the work buffer since we're fine clobbering it at this point to hold - // the output. - uint32_t* output = (uint32_t*)state->work; - int i; - for (i = 0; i < num_channels; ++i) { - *output++ = Sqrt64(*work++) >> scale_down_shift; - } - return (uint32_t*)state->work; -} - -void FilterbankReset(struct FilterbankState* state) { - memset(state->work, 0, (state->num_channels + 1) * sizeof(*state->work)); -} diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/filterbank.h b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/filterbank.h deleted file mode 100644 index 1e6d3885..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/filterbank.h +++ /dev/null @@ -1,63 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FILTERBANK_H_ -#define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FILTERBANK_H_ - -#include -#include - -#include "tensorflow/lite/experimental/microfrontend/lib/fft.h" - -#define kFilterbankBits 12 - -#ifdef __cplusplus -extern "C" { -#endif - -struct FilterbankState { - int num_channels; - int start_index; - int end_index; - int16_t* channel_frequency_starts; - int16_t* channel_weight_starts; - int16_t* channel_widths; - int16_t* weights; - int16_t* unweights; - uint64_t* work; -}; - -// Converts the relevant complex values of an FFT output into energy (the -// square magnitude). -void FilterbankConvertFftComplexToEnergy(struct FilterbankState* state, - struct complex_int16_t* fft_output, - int32_t* energy); - -// Computes the mel-scale filterbank on the given energy array. Output is cached -// internally - to fetch it, you need to call FilterbankSqrt. -void FilterbankAccumulateChannels(struct FilterbankState* state, - const int32_t* energy); - -// Applies an integer square root to the 64 bit intermediate values of the -// filterbank, and returns a pointer to them. Memory will be invalidated the -// next time FilterbankAccumulateChannels is called. -uint32_t* FilterbankSqrt(struct FilterbankState* state, int scale_down_shift); - -void FilterbankReset(struct FilterbankState* state); - -#ifdef __cplusplus -} // extern "C" -#endif - -#endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FILTERBANK_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/filterbank_util.c b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/filterbank_util.c deleted file mode 100644 index f18ebf54..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/filterbank_util.c +++ /dev/null @@ -1,220 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/experimental/microfrontend/lib/filterbank_util.h" - -#include -#include -#include - -#define kFilterbankIndexAlignment 4 -#define kFilterbankChannelBlockSize 4 - -void FilterbankFillConfigWithDefaults(struct FilterbankConfig* config) { - config->num_channels = 32; - config->lower_band_limit = 125.0f; - config->upper_band_limit = 7500.0f; - config->output_scale_shift = 7; -} - -static float FreqToMel(float freq) { return 1127.0 * log1p(freq / 700.0); } - -static void CalculateCenterFrequencies(const int num_channels, - const float lower_frequency_limit, - const float upper_frequency_limit, - float* center_frequencies) { - assert(lower_frequency_limit >= 0.0f); - assert(upper_frequency_limit > lower_frequency_limit); - - const float mel_low = FreqToMel(lower_frequency_limit); - const float mel_hi = FreqToMel(upper_frequency_limit); - const float mel_span = mel_hi - mel_low; - const float mel_spacing = mel_span / ((float)num_channels); - int i; - for (i = 0; i < num_channels; ++i) { - center_frequencies[i] = mel_low + (mel_spacing * (i + 1)); - } -} - -static void QuantizeFilterbankWeights(const float float_weight, int16_t* weight, - int16_t* unweight) { - *weight = floor(float_weight * (1 << kFilterbankBits) + 0.5); - *unweight = floor((1.0 - float_weight) * (1 << kFilterbankBits) + 0.5); -} - -int FilterbankPopulateState(const struct FilterbankConfig* config, - struct FilterbankState* state, int sample_rate, - int spectrum_size) { - state->num_channels = config->num_channels; - const int num_channels_plus_1 = config->num_channels + 1; - - // How should we align things to index counts given the byte alignment? - const int index_alignment = - (kFilterbankIndexAlignment < sizeof(int16_t) - ? 1 - : kFilterbankIndexAlignment / sizeof(int16_t)); - - state->channel_frequency_starts = - malloc(num_channels_plus_1 * sizeof(*state->channel_frequency_starts)); - state->channel_weight_starts = - malloc(num_channels_plus_1 * sizeof(*state->channel_weight_starts)); - state->channel_widths = - malloc(num_channels_plus_1 * sizeof(*state->channel_widths)); - state->work = malloc(num_channels_plus_1 * sizeof(*state->work)); - - float* center_mel_freqs = - malloc(num_channels_plus_1 * sizeof(*center_mel_freqs)); - int16_t* actual_channel_starts = - malloc(num_channels_plus_1 * sizeof(*actual_channel_starts)); - int16_t* actual_channel_widths = - malloc(num_channels_plus_1 * sizeof(*actual_channel_widths)); - - if (state->channel_frequency_starts == NULL || - state->channel_weight_starts == NULL || state->channel_widths == NULL || - center_mel_freqs == NULL || actual_channel_starts == NULL || - actual_channel_widths == NULL) { - free(center_mel_freqs); - free(actual_channel_starts); - free(actual_channel_widths); - fprintf(stderr, "Failed to allocate channel buffers\n"); - return 0; - } - - CalculateCenterFrequencies(num_channels_plus_1, config->lower_band_limit, - config->upper_band_limit, center_mel_freqs); - - // Always exclude DC. - const float hz_per_sbin = 0.5 * sample_rate / ((float)spectrum_size - 1); - state->start_index = 1.5 + config->lower_band_limit / hz_per_sbin; - state->end_index = 0; // Initialized to zero here, but actually set below. - - // For each channel, we need to figure out what frequencies belong to it, and - // how much padding we need to add so that we can efficiently multiply the - // weights and unweights for accumulation. To simplify the multiplication - // logic, all channels will have some multiplication to do (even if there are - // no frequencies that accumulate to that channel) - they will be directed to - // a set of zero weights. - int chan_freq_index_start = state->start_index; - int weight_index_start = 0; - int needs_zeros = 0; - - int chan; - for (chan = 0; chan < num_channels_plus_1; ++chan) { - // Keep jumping frequencies until we overshoot the bound on this channel. - int freq_index = chan_freq_index_start; - while (FreqToMel((freq_index)*hz_per_sbin) <= center_mel_freqs[chan]) { - ++freq_index; - } - - const int width = freq_index - chan_freq_index_start; - actual_channel_starts[chan] = chan_freq_index_start; - actual_channel_widths[chan] = width; - - if (width == 0) { - // This channel doesn't actually get anything from the frequencies, it's - // always zero. We need then to insert some 'zero' weights into the - // output, and just redirect this channel to do a single multiplication at - // this point. For simplicity, the zeros are placed at the beginning of - // the weights arrays, so we have to go and update all the other - // weight_starts to reflect this shift (but only once). - state->channel_frequency_starts[chan] = 0; - state->channel_weight_starts[chan] = 0; - state->channel_widths[chan] = kFilterbankChannelBlockSize; - if (!needs_zeros) { - needs_zeros = 1; - int j; - for (j = 0; j < chan; ++j) { - state->channel_weight_starts[j] += kFilterbankChannelBlockSize; - } - weight_index_start += kFilterbankChannelBlockSize; - } - } else { - // How far back do we need to go to ensure that we have the proper - // alignment? - const int aligned_start = - (chan_freq_index_start / index_alignment) * index_alignment; - const int aligned_width = (chan_freq_index_start - aligned_start + width); - const int padded_width = - (((aligned_width - 1) / kFilterbankChannelBlockSize) + 1) * - kFilterbankChannelBlockSize; - - state->channel_frequency_starts[chan] = aligned_start; - state->channel_weight_starts[chan] = weight_index_start; - state->channel_widths[chan] = padded_width; - weight_index_start += padded_width; - } - chan_freq_index_start = freq_index; - } - - // Allocate the two arrays to store the weights - weight_index_start contains - // the index of what would be the next set of weights that we would need to - // add, so that's how many weights we need to allocate. - state->weights = calloc(weight_index_start, sizeof(*state->weights)); - state->unweights = calloc(weight_index_start, sizeof(*state->unweights)); - - // If the alloc failed, we also need to nuke the arrays. - if (state->weights == NULL || state->unweights == NULL) { - free(center_mel_freqs); - free(actual_channel_starts); - free(actual_channel_widths); - fprintf(stderr, "Failed to allocate weights or unweights\n"); - return 0; - } - - // Next pass, compute all the weights. Since everything has been memset to - // zero, we only need to fill in the weights that correspond to some frequency - // for a channel. - const float mel_low = FreqToMel(config->lower_band_limit); - for (chan = 0; chan < num_channels_plus_1; ++chan) { - int frequency = actual_channel_starts[chan]; - const int num_frequencies = actual_channel_widths[chan]; - const int frequency_offset = - frequency - state->channel_frequency_starts[chan]; - const int weight_start = state->channel_weight_starts[chan]; - const float denom_val = (chan == 0) ? mel_low : center_mel_freqs[chan - 1]; - - int j; - for (j = 0; j < num_frequencies; ++j, ++frequency) { - const float weight = - (center_mel_freqs[chan] - FreqToMel(frequency * hz_per_sbin)) / - (center_mel_freqs[chan] - denom_val); - - // Make the float into an integer for the weights (and unweights). - const int weight_index = weight_start + frequency_offset + j; - QuantizeFilterbankWeights(weight, state->weights + weight_index, - state->unweights + weight_index); - } - if (frequency > state->end_index) { - state->end_index = frequency; - } - } - - free(center_mel_freqs); - free(actual_channel_starts); - free(actual_channel_widths); - if (state->end_index >= spectrum_size) { - fprintf(stderr, "Filterbank end_index is above spectrum size.\n"); - return 0; - } - return 1; -} - -void FilterbankFreeStateContents(struct FilterbankState* state) { - free(state->channel_frequency_starts); - free(state->channel_weight_starts); - free(state->channel_widths); - free(state->weights); - free(state->unweights); - free(state->work); -} diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/filterbank_util.h b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/filterbank_util.h deleted file mode 100644 index 781d1024..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/filterbank_util.h +++ /dev/null @@ -1,50 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FILTERBANK_UTIL_H_ -#define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FILTERBANK_UTIL_H_ - -#include "tensorflow/lite/experimental/microfrontend/lib/filterbank.h" - -#ifdef __cplusplus -extern "C" { -#endif - -struct FilterbankConfig { - // number of frequency channel buckets for filterbank - int num_channels; - // maximum frequency to include - float upper_band_limit; - // minimum frequency to include - float lower_band_limit; - // unused - int output_scale_shift; -}; - -// Fills the frontendConfig with "sane" defaults. -void FilterbankFillConfigWithDefaults(struct FilterbankConfig* config); - -// Allocates any buffers. -int FilterbankPopulateState(const struct FilterbankConfig* config, - struct FilterbankState* state, int sample_rate, - int spectrum_size); - -// Frees any allocated buffers. -void FilterbankFreeStateContents(struct FilterbankState* state); - -#ifdef __cplusplus -} // extern "C" -#endif - -#endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FILTERBANK_UTIL_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/frontend.c b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/frontend.c deleted file mode 100644 index 9de2a879..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/frontend.c +++ /dev/null @@ -1,72 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/experimental/microfrontend/lib/frontend.h" - -#include "tensorflow/lite/experimental/microfrontend/lib/bits.h" - -struct FrontendOutput FrontendProcessSamples(struct FrontendState* state, - const int16_t* samples, - size_t num_samples, - size_t* num_samples_read) { - struct FrontendOutput output; - output.values = NULL; - output.size = 0; - - // Try to apply the window - if it fails, return and wait for more data. - if (!WindowProcessSamples(&state->window, samples, num_samples, - num_samples_read)) { - return output; - } - - // Apply the FFT to the window's output (and scale it so that the fixed point - // FFT can have as much resolution as possible). - int input_shift = - 15 - MostSignificantBit32(state->window.max_abs_output_value); - FftCompute(&state->fft, state->window.output, input_shift); - - // We can re-ruse the fft's output buffer to hold the energy. - int32_t* energy = (int32_t*)state->fft.output; - - FilterbankConvertFftComplexToEnergy(&state->filterbank, state->fft.output, - energy); - - FilterbankAccumulateChannels(&state->filterbank, energy); - uint32_t* scaled_filterbank = FilterbankSqrt(&state->filterbank, input_shift); - - // Apply noise reduction. - NoiseReductionApply(&state->noise_reduction, scaled_filterbank); - - if (state->pcan_gain_control.enable_pcan) { - PcanGainControlApply(&state->pcan_gain_control, scaled_filterbank); - } - - // Apply the log and scale. - int correction_bits = - MostSignificantBit32(state->fft.fft_size) - 1 - (kFilterbankBits / 2); - uint16_t* logged_filterbank = - LogScaleApply(&state->log_scale, scaled_filterbank, - state->filterbank.num_channels, correction_bits); - - output.size = state->filterbank.num_channels; - output.values = logged_filterbank; - return output; -} - -void FrontendReset(struct FrontendState* state) { - WindowReset(&state->window); - FftReset(&state->fft); - FilterbankReset(&state->filterbank); - NoiseReductionReset(&state->noise_reduction); -} diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/frontend.h b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/frontend.h deleted file mode 100644 index 883df5fd..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/frontend.h +++ /dev/null @@ -1,64 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FRONTEND_H_ -#define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FRONTEND_H_ - -#include -#include - -#include "tensorflow/lite/experimental/microfrontend/lib/fft.h" -#include "tensorflow/lite/experimental/microfrontend/lib/filterbank.h" -#include "tensorflow/lite/experimental/microfrontend/lib/log_scale.h" -#include "tensorflow/lite/experimental/microfrontend/lib/noise_reduction.h" -#include "tensorflow/lite/experimental/microfrontend/lib/pcan_gain_control.h" -#include "tensorflow/lite/experimental/microfrontend/lib/window.h" - -#ifdef __cplusplus -extern "C" { -#endif - -struct FrontendState { - struct WindowState window; - struct FftState fft; - struct FilterbankState filterbank; - struct NoiseReductionState noise_reduction; - struct PcanGainControlState pcan_gain_control; - struct LogScaleState log_scale; -}; - -struct FrontendOutput { - const uint16_t* values; - size_t size; -}; - -// Main entry point to processing frontend samples. Updates num_samples_read to -// contain the number of samples that have been consumed from the input array. -// Returns a struct containing the generated output. If not enough samples were -// added to generate a feature vector, the returned size will be 0 and the -// values pointer will be NULL. Note that the output pointer will be invalidated -// as soon as FrontendProcessSamples is called again, so copy the contents -// elsewhere if you need to use them later. -struct FrontendOutput FrontendProcessSamples(struct FrontendState* state, - const int16_t* samples, - size_t num_samples, - size_t* num_samples_read); - -void FrontendReset(struct FrontendState* state); - -#ifdef __cplusplus -} // extern "C" -#endif - -#endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FRONTEND_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/frontend_util.c b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/frontend_util.c deleted file mode 100644 index 27224f6d..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/frontend_util.c +++ /dev/null @@ -1,85 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/experimental/microfrontend/lib/frontend_util.h" - -#include -#include - -#include "tensorflow/lite/experimental/microfrontend/lib/bits.h" - -void FrontendFillConfigWithDefaults(struct FrontendConfig* config) { - WindowFillConfigWithDefaults(&config->window); - FilterbankFillConfigWithDefaults(&config->filterbank); - NoiseReductionFillConfigWithDefaults(&config->noise_reduction); - PcanGainControlFillConfigWithDefaults(&config->pcan_gain_control); - LogScaleFillConfigWithDefaults(&config->log_scale); -} - -int FrontendPopulateState(const struct FrontendConfig* config, - struct FrontendState* state, int sample_rate) { - memset(state, 0, sizeof(*state)); - - if (!WindowPopulateState(&config->window, &state->window, sample_rate)) { - fprintf(stderr, "Failed to populate window state\n"); - return 0; - } - - if (!FftPopulateState(&state->fft, state->window.size)) { - fprintf(stderr, "Failed to populate fft state\n"); - return 0; - } - FftInit(&state->fft); - - if (!FilterbankPopulateState(&config->filterbank, &state->filterbank, - sample_rate, state->fft.fft_size / 2 + 1)) { - fprintf(stderr, "Failed to populate filterbank state\n"); - return 0; - } - - if (!NoiseReductionPopulateState(&config->noise_reduction, - &state->noise_reduction, - state->filterbank.num_channels)) { - fprintf(stderr, "Failed to populate noise reduction state\n"); - return 0; - } - - int input_correction_bits = - MostSignificantBit32(state->fft.fft_size) - 1 - (kFilterbankBits / 2); - if (!PcanGainControlPopulateState( - &config->pcan_gain_control, &state->pcan_gain_control, - state->noise_reduction.estimate, state->filterbank.num_channels, - state->noise_reduction.smoothing_bits, input_correction_bits)) { - fprintf(stderr, "Failed to populate pcan gain control state\n"); - return 0; - } - - if (!LogScalePopulateState(&config->log_scale, &state->log_scale)) { - fprintf(stderr, "Failed to populate log scale state\n"); - return 0; - } - - FrontendReset(state); - - // All good, return a true value. - return 1; -} - -void FrontendFreeStateContents(struct FrontendState* state) { - WindowFreeStateContents(&state->window); - FftFreeStateContents(&state->fft); - FilterbankFreeStateContents(&state->filterbank); - NoiseReductionFreeStateContents(&state->noise_reduction); - PcanGainControlFreeStateContents(&state->pcan_gain_control); -} diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/frontend_util.h b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/frontend_util.h deleted file mode 100644 index 895ce6cd..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/frontend_util.h +++ /dev/null @@ -1,52 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FRONTEND_UTIL_H_ -#define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FRONTEND_UTIL_H_ - -#include "tensorflow/lite/experimental/microfrontend/lib/fft_util.h" -#include "tensorflow/lite/experimental/microfrontend/lib/filterbank_util.h" -#include "tensorflow/lite/experimental/microfrontend/lib/frontend.h" -#include "tensorflow/lite/experimental/microfrontend/lib/log_scale_util.h" -#include "tensorflow/lite/experimental/microfrontend/lib/noise_reduction_util.h" -#include "tensorflow/lite/experimental/microfrontend/lib/pcan_gain_control_util.h" -#include "tensorflow/lite/experimental/microfrontend/lib/window_util.h" - -#ifdef __cplusplus -extern "C" { -#endif - -struct FrontendConfig { - struct WindowConfig window; - struct FilterbankConfig filterbank; - struct NoiseReductionConfig noise_reduction; - struct PcanGainControlConfig pcan_gain_control; - struct LogScaleConfig log_scale; -}; - -// Fills the frontendConfig with "sane" defaults. -void FrontendFillConfigWithDefaults(struct FrontendConfig* config); - -// Allocates any buffers. -int FrontendPopulateState(const struct FrontendConfig* config, - struct FrontendState* state, int sample_rate); - -// Frees any allocated buffers. -void FrontendFreeStateContents(struct FrontendState* state); - -#ifdef __cplusplus -} // extern "C" -#endif - -#endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FRONTEND_UTIL_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/kiss_fft_common.h b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/kiss_fft_common.h deleted file mode 100644 index 33556dab..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/kiss_fft_common.h +++ /dev/null @@ -1,48 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_KISS_FFT_COMMON_H_ -#define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_KISS_FFT_COMMON_H_ - -// This header file should be included in all variants of kiss_fft_$type.{h,cc} -// so that their sub-included source files do not mistakenly wrap libc header -// files within their kissfft_$type namespaces. -// E.g, This header avoids kissfft_int16.h containing: -// namespace kiss_fft_int16 { -// #include "kiss_fft.h" -// } -// where kiss_fft_.h contains: -// #include -// -// TRICK: By including the following header files here, their preprocessor -// header guards prevent them being re-defined inside of the kiss_fft_$type -// namespaces declared within the kiss_fft_$type.{h,cc} sources. -// Note that the original kiss_fft*.h files are untouched since they -// may be used in libraries that include them directly. - -#include -#include -#include -#include -#include - -#ifdef FIXED_POINT -#include -#endif - -#ifdef USE_SIMD -#include -#endif -#endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_KISS_FFT_COMMON_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/kiss_fft_int16.cc b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/kiss_fft_int16.cc deleted file mode 100644 index 55457f4d..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/kiss_fft_int16.cc +++ /dev/null @@ -1,8 +0,0 @@ -#include "tensorflow/lite/experimental/microfrontend/lib/kiss_fft_common.h" - -#define FIXED_POINT 16 -namespace kissfft_fixed16 { -#include "kiss_fft.c" -#include "tools/kiss_fftr.c" -} // namespace kissfft_fixed16 -#undef FIXED_POINT diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/kiss_fft_int16.h b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/kiss_fft_int16.h deleted file mode 100644 index beee99aa..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/kiss_fft_int16.h +++ /dev/null @@ -1,33 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_KISS_FFT_INT16_H_ -#define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_KISS_FFT_INT16_H_ - -#include "tensorflow/lite/experimental/microfrontend/lib/kiss_fft_common.h" - -// Wrap 16-bit kiss fft in its own namespace. Enables us to link an application -// with different kiss fft resultions (16/32 bit interger, float, double) -// without getting a linker error. -#define FIXED_POINT 16 -namespace kissfft_fixed16 { -#include "kiss_fft.h" -#include "tools/kiss_fftr.h" -} // namespace kissfft_fixed16 -#undef FIXED_POINT -#undef kiss_fft_scalar -#undef KISS_FFT_H - -#endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_KISS_FFT_INT16_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/log_lut.c b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/log_lut.c deleted file mode 100644 index f59618e0..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/log_lut.c +++ /dev/null @@ -1,30 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/experimental/microfrontend/lib/log_lut.h" -const uint16_t kLogLut[] -#ifndef _MSC_VER - __attribute__((aligned(4))) -#endif // _MSV_VER - = {0, 224, 442, 654, 861, 1063, 1259, 1450, 1636, 1817, 1992, 2163, - 2329, 2490, 2646, 2797, 2944, 3087, 3224, 3358, 3487, 3611, 3732, 3848, - 3960, 4068, 4172, 4272, 4368, 4460, 4549, 4633, 4714, 4791, 4864, 4934, - 5001, 5063, 5123, 5178, 5231, 5280, 5326, 5368, 5408, 5444, 5477, 5507, - 5533, 5557, 5578, 5595, 5610, 5622, 5631, 5637, 5640, 5641, 5638, 5633, - 5626, 5615, 5602, 5586, 5568, 5547, 5524, 5498, 5470, 5439, 5406, 5370, - 5332, 5291, 5249, 5203, 5156, 5106, 5054, 5000, 4944, 4885, 4825, 4762, - 4697, 4630, 4561, 4490, 4416, 4341, 4264, 4184, 4103, 4020, 3935, 3848, - 3759, 3668, 3575, 3481, 3384, 3286, 3186, 3084, 2981, 2875, 2768, 2659, - 2549, 2437, 2323, 2207, 2090, 1971, 1851, 1729, 1605, 1480, 1353, 1224, - 1094, 963, 830, 695, 559, 421, 282, 142, 0, 0}; diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/log_lut.h b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/log_lut.h deleted file mode 100644 index b2448a32..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/log_lut.h +++ /dev/null @@ -1,40 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_LOG_LUT_H_ -#define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_LOG_LUT_H_ - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -// Number of segments in the log lookup table. The table will be kLogSegments+1 -// in length (with some padding). -#define kLogSegments 128 -#define kLogSegmentsLog2 7 - -// Scale used by lookup table. -#define kLogScale 65536 -#define kLogScaleLog2 16 -#define kLogCoeff 45426 - -extern const uint16_t kLogLut[]; - -#ifdef __cplusplus -} // extern "C" -#endif - -#endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_LOG_LUT_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/log_scale.c b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/log_scale.c deleted file mode 100644 index c27a50a6..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/log_scale.c +++ /dev/null @@ -1,83 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/experimental/microfrontend/lib/log_scale.h" - -#include "tensorflow/lite/experimental/microfrontend/lib/bits.h" -#include "tensorflow/lite/experimental/microfrontend/lib/log_lut.h" - -#define kuint16max 0x0000FFFF - -// The following functions implement integer logarithms of various sizes. The -// approximation is calculated according to method described in -// www.inti.gob.ar/electronicaeinformatica/instrumentacion/utic/ -// publicaciones/SPL2007/Log10-spl07.pdf -// It first calculates log2 of the input and then converts it to natural -// logarithm. - -static uint32_t Log2FractionPart(const uint32_t x, const uint32_t log2x) { - // Part 1 - int32_t frac = x - (1LL << log2x); - if (log2x < kLogScaleLog2) { - frac <<= kLogScaleLog2 - log2x; - } else { - frac >>= log2x - kLogScaleLog2; - } - // Part 2 - const uint32_t base_seg = frac >> (kLogScaleLog2 - kLogSegmentsLog2); - const uint32_t seg_unit = - (((uint32_t)1) << kLogScaleLog2) >> kLogSegmentsLog2; - - const int32_t c0 = kLogLut[base_seg]; - const int32_t c1 = kLogLut[base_seg + 1]; - const int32_t seg_base = seg_unit * base_seg; - const int32_t rel_pos = ((c1 - c0) * (frac - seg_base)) >> kLogScaleLog2; - return frac + c0 + rel_pos; -} - -static uint32_t Log(const uint32_t x, const uint32_t scale_shift) { - const uint32_t integer = MostSignificantBit32(x) - 1; - const uint32_t fraction = Log2FractionPart(x, integer); - const uint32_t log2 = (integer << kLogScaleLog2) + fraction; - const uint32_t round = kLogScale / 2; - const uint32_t loge = (((uint64_t)kLogCoeff) * log2 + round) >> kLogScaleLog2; - // Finally scale to our output scale - const uint32_t loge_scaled = ((loge << scale_shift) + round) >> kLogScaleLog2; - return loge_scaled; -} - -uint16_t* LogScaleApply(struct LogScaleState* state, uint32_t* signal, - int signal_size, int correction_bits) { - const int scale_shift = state->scale_shift; - uint16_t* output = (uint16_t*)signal; - uint16_t* ret = output; - int i; - for (i = 0; i < signal_size; ++i) { - uint32_t value = *signal++; - if (state->enable_log) { - if (correction_bits < 0) { - value >>= -correction_bits; - } else { - value <<= correction_bits; - } - if (value > 1) { - value = Log(value, scale_shift); - } else { - value = 0; - } - } - *output++ = (value < kuint16max) ? value : kuint16max; - } - return ret; -} diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/log_scale.h b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/log_scale.h deleted file mode 100644 index a383f32f..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/log_scale.h +++ /dev/null @@ -1,39 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_LOG_SCALE_H_ -#define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_LOG_SCALE_H_ - -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -struct LogScaleState { - int enable_log; - int scale_shift; -}; - -// Applies a fixed point logarithm to the signal and converts it to 16 bit. Note -// that the signal array will be modified. -uint16_t* LogScaleApply(struct LogScaleState* state, uint32_t* signal, - int signal_size, int correction_bits); - -#ifdef __cplusplus -} // extern "C" -#endif - -#endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_LOG_SCALE_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/log_scale_util.c b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/log_scale_util.c deleted file mode 100644 index 0e3dd1d1..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/log_scale_util.c +++ /dev/null @@ -1,27 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/experimental/microfrontend/lib/log_scale_util.h" - -void LogScaleFillConfigWithDefaults(struct LogScaleConfig* config) { - config->enable_log = 1; - config->scale_shift = 6; -} - -int LogScalePopulateState(const struct LogScaleConfig* config, - struct LogScaleState* state) { - state->enable_log = config->enable_log; - state->scale_shift = config->scale_shift; - return 1; -} diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/log_scale_util.h b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/log_scale_util.h deleted file mode 100644 index 11f7d9ee..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/log_scale_util.h +++ /dev/null @@ -1,45 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_LOG_SCALE_UTIL_H_ -#define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_LOG_SCALE_UTIL_H_ - -#include -#include - -#include "tensorflow/lite/experimental/microfrontend/lib/log_scale.h" - -#ifdef __cplusplus -extern "C" { -#endif - -struct LogScaleConfig { - // set to false (0) to disable this module - int enable_log; - // scale results by 2^(scale_shift) - int scale_shift; -}; - -// Populates the LogScaleConfig with "sane" default values. -void LogScaleFillConfigWithDefaults(struct LogScaleConfig* config); - -// Allocates any buffers. -int LogScalePopulateState(const struct LogScaleConfig* config, - struct LogScaleState* state); - -#ifdef __cplusplus -} // extern "C" -#endif - -#endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_LOG_SCALE_UTIL_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/noise_reduction.c b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/noise_reduction.c deleted file mode 100644 index 16b30e66..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/noise_reduction.c +++ /dev/null @@ -1,51 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/experimental/microfrontend/lib/noise_reduction.h" - -#include - -void NoiseReductionApply(struct NoiseReductionState* state, uint32_t* signal) { - int i; - for (i = 0; i < state->num_channels; ++i) { - const uint32_t smoothing = - ((i & 1) == 0) ? state->even_smoothing : state->odd_smoothing; - const uint32_t one_minus_smoothing = (1 << kNoiseReductionBits) - smoothing; - - // Update the estimate of the noise. - const uint32_t signal_scaled_up = signal[i] << state->smoothing_bits; - uint32_t estimate = - (((uint64_t)signal_scaled_up * smoothing) + - ((uint64_t)state->estimate[i] * one_minus_smoothing)) >> - kNoiseReductionBits; - state->estimate[i] = estimate; - - // Make sure that we can't get a negative value for the signal - estimate. - if (estimate > signal_scaled_up) { - estimate = signal_scaled_up; - } - - const uint32_t floor = - ((uint64_t)signal[i] * state->min_signal_remaining) >> - kNoiseReductionBits; - const uint32_t subtracted = - (signal_scaled_up - estimate) >> state->smoothing_bits; - const uint32_t output = subtracted > floor ? subtracted : floor; - signal[i] = output; - } -} - -void NoiseReductionReset(struct NoiseReductionState* state) { - memset(state->estimate, 0, sizeof(*state->estimate) * state->num_channels); -} diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/noise_reduction.h b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/noise_reduction.h deleted file mode 100644 index 46d3f52e..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/noise_reduction.h +++ /dev/null @@ -1,46 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_NOISE_REDUCTION_H_ -#define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_NOISE_REDUCTION_H_ - -#define kNoiseReductionBits 14 - -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -struct NoiseReductionState { - int smoothing_bits; - uint16_t even_smoothing; - uint16_t odd_smoothing; - uint16_t min_signal_remaining; - int num_channels; - uint32_t* estimate; -}; - -// Removes stationary noise from each channel of the signal using a low pass -// filter. -void NoiseReductionApply(struct NoiseReductionState* state, uint32_t* signal); - -void NoiseReductionReset(struct NoiseReductionState* state); - -#ifdef __cplusplus -} // extern "C" -#endif - -#endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_NOISE_REDUCTION_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/noise_reduction_util.c b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/noise_reduction_util.c deleted file mode 100644 index a6c9234e..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/noise_reduction_util.c +++ /dev/null @@ -1,45 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/experimental/microfrontend/lib/noise_reduction_util.h" - -#include - -void NoiseReductionFillConfigWithDefaults(struct NoiseReductionConfig* config) { - config->smoothing_bits = 10; - config->even_smoothing = 0.025; - config->odd_smoothing = 0.06; - config->min_signal_remaining = 0.05; -} - -int NoiseReductionPopulateState(const struct NoiseReductionConfig* config, - struct NoiseReductionState* state, - int num_channels) { - state->smoothing_bits = config->smoothing_bits; - state->odd_smoothing = config->odd_smoothing * (1 << kNoiseReductionBits); - state->even_smoothing = config->even_smoothing * (1 << kNoiseReductionBits); - state->min_signal_remaining = - config->min_signal_remaining * (1 << kNoiseReductionBits); - state->num_channels = num_channels; - state->estimate = calloc(state->num_channels, sizeof(*state->estimate)); - if (state->estimate == NULL) { - fprintf(stderr, "Failed to alloc estimate buffer\n"); - return 0; - } - return 1; -} - -void NoiseReductionFreeStateContents(struct NoiseReductionState* state) { - free(state->estimate); -} diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/noise_reduction_util.h b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/noise_reduction_util.h deleted file mode 100644 index fa555391..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/noise_reduction_util.h +++ /dev/null @@ -1,50 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_NOISE_REDUCTION_UTIL_H_ -#define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_NOISE_REDUCTION_UTIL_H_ - -#include "tensorflow/lite/experimental/microfrontend/lib/noise_reduction.h" - -#ifdef __cplusplus -extern "C" { -#endif - -struct NoiseReductionConfig { - // scale the signal up by 2^(smoothing_bits) before reduction - int smoothing_bits; - // smoothing coefficient for even-numbered channels - float even_smoothing; - // smoothing coefficient for odd-numbered channels - float odd_smoothing; - // fraction of signal to preserve (1.0 disables this module) - float min_signal_remaining; -}; - -// Populates the NoiseReductionConfig with "sane" default values. -void NoiseReductionFillConfigWithDefaults(struct NoiseReductionConfig* config); - -// Allocates any buffers. -int NoiseReductionPopulateState(const struct NoiseReductionConfig* config, - struct NoiseReductionState* state, - int num_channels); - -// Frees any allocated buffers. -void NoiseReductionFreeStateContents(struct NoiseReductionState* state); - -#ifdef __cplusplus -} // extern "C" -#endif - -#endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_NOISE_REDUCTION_UTIL_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/pcan_gain_control.c b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/pcan_gain_control.c deleted file mode 100644 index 22d58767..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/pcan_gain_control.c +++ /dev/null @@ -1,56 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/experimental/microfrontend/lib/pcan_gain_control.h" - -#include "tensorflow/lite/experimental/microfrontend/lib/bits.h" - -int16_t WideDynamicFunction(const uint32_t x, const int16_t* lut) { - if (x <= 2) { - return lut[x]; - } - - const int16_t interval = MostSignificantBit32(x); - lut += 4 * interval - 6; - - const int16_t frac = - ((interval < 11) ? (x << (11 - interval)) : (x >> (interval - 11))) & - 0x3FF; - - int32_t result = ((int32_t)lut[2] * frac) >> 5; - result += (int32_t)((uint32_t)lut[1] << 5); - result *= frac; - result = (result + (1 << 14)) >> 15; - result += lut[0]; - return (int16_t)result; -} - -uint32_t PcanShrink(const uint32_t x) { - if (x < (2 << kPcanSnrBits)) { - return (x * x) >> (2 + 2 * kPcanSnrBits - kPcanOutputBits); - } else { - return (x >> (kPcanSnrBits - kPcanOutputBits)) - (1 << kPcanOutputBits); - } -} - -void PcanGainControlApply(struct PcanGainControlState* state, - uint32_t* signal) { - int i; - for (i = 0; i < state->num_channels; ++i) { - const uint32_t gain = - WideDynamicFunction(state->noise_estimate[i], state->gain_lut); - const uint32_t snr = ((uint64_t)signal[i] * gain) >> state->snr_shift; - signal[i] = PcanShrink(snr); - } -} diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/pcan_gain_control.h b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/pcan_gain_control.h deleted file mode 100644 index 3f6222be..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/pcan_gain_control.h +++ /dev/null @@ -1,47 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_PCAN_GAIN_CONTROL_H_ -#define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_PCAN_GAIN_CONTROL_H_ - -#include -#include - -#define kPcanSnrBits 12 -#define kPcanOutputBits 6 - -#ifdef __cplusplus -extern "C" { -#endif - -// Details at https://research.google/pubs/pub45911.pdf -struct PcanGainControlState { - int enable_pcan; - uint32_t* noise_estimate; - int num_channels; - int16_t* gain_lut; - int32_t snr_shift; -}; - -int16_t WideDynamicFunction(const uint32_t x, const int16_t* lut); - -uint32_t PcanShrink(const uint32_t x); - -void PcanGainControlApply(struct PcanGainControlState* state, uint32_t* signal); - -#ifdef __cplusplus -} // extern "C" -#endif - -#endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_PCAN_GAIN_CONTROL_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/pcan_gain_control_util.c b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/pcan_gain_control_util.c deleted file mode 100644 index e850d439..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/pcan_gain_control_util.c +++ /dev/null @@ -1,92 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/experimental/microfrontend/lib/pcan_gain_control_util.h" - -#include -#include - -#define kint16max 0x00007FFF - -void PcanGainControlFillConfigWithDefaults( - struct PcanGainControlConfig* config) { - config->enable_pcan = 0; - config->strength = 0.95; - config->offset = 80.0; - config->gain_bits = 21; -} - -int16_t PcanGainLookupFunction(const struct PcanGainControlConfig* config, - int32_t input_bits, uint32_t x) { - const float x_as_float = ((float)x) / ((uint32_t)1 << input_bits); - const float gain_as_float = - ((uint32_t)1 << config->gain_bits) * - powf(x_as_float + config->offset, -config->strength); - - if (gain_as_float > kint16max) { - return kint16max; - } - return (int16_t)(gain_as_float + 0.5f); -} - -int PcanGainControlPopulateState(const struct PcanGainControlConfig* config, - struct PcanGainControlState* state, - uint32_t* noise_estimate, - const int num_channels, - const uint16_t smoothing_bits, - const int32_t input_correction_bits) { - state->enable_pcan = config->enable_pcan; - if (!state->enable_pcan) { - return 1; - } - state->noise_estimate = noise_estimate; - state->num_channels = num_channels; - state->gain_lut = malloc(kWideDynamicFunctionLUTSize * sizeof(int16_t)); - if (state->gain_lut == NULL) { - fprintf(stderr, "Failed to allocate gain LUT\n"); - return 0; - } - state->snr_shift = config->gain_bits - input_correction_bits - kPcanSnrBits; - - const int32_t input_bits = smoothing_bits - input_correction_bits; - state->gain_lut[0] = PcanGainLookupFunction(config, input_bits, 0); - state->gain_lut[1] = PcanGainLookupFunction(config, input_bits, 1); - state->gain_lut -= 6; - int interval; - for (interval = 2; interval <= kWideDynamicFunctionBits; ++interval) { - const uint32_t x0 = (uint32_t)1 << (interval - 1); - const uint32_t x1 = x0 + (x0 >> 1); - const uint32_t x2 = - (interval == kWideDynamicFunctionBits) ? x0 + (x0 - 1) : 2 * x0; - - const int16_t y0 = PcanGainLookupFunction(config, input_bits, x0); - const int16_t y1 = PcanGainLookupFunction(config, input_bits, x1); - const int16_t y2 = PcanGainLookupFunction(config, input_bits, x2); - - const int32_t diff1 = (int32_t)y1 - y0; - const int32_t diff2 = (int32_t)y2 - y0; - const int32_t a1 = 4 * diff1 - diff2; - const int32_t a2 = diff2 - a1; - - state->gain_lut[4 * interval] = y0; - state->gain_lut[4 * interval + 1] = (int16_t)a1; - state->gain_lut[4 * interval + 2] = (int16_t)a2; - } - state->gain_lut += 6; - return 1; -} - -void PcanGainControlFreeStateContents(struct PcanGainControlState* state) { - free(state->gain_lut); -} diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/pcan_gain_control_util.h b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/pcan_gain_control_util.h deleted file mode 100644 index d4bfaa2e..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/pcan_gain_control_util.h +++ /dev/null @@ -1,57 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_PCAN_GAIN_CONTROL_UTIL_H_ -#define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_PCAN_GAIN_CONTROL_UTIL_H_ - -#include "tensorflow/lite/experimental/microfrontend/lib/pcan_gain_control.h" - -#define kWideDynamicFunctionBits 32 -#define kWideDynamicFunctionLUTSize (4 * kWideDynamicFunctionBits - 3) - -#ifdef __cplusplus -extern "C" { -#endif - -struct PcanGainControlConfig { - // set to false (0) to disable this module - int enable_pcan; - // gain normalization exponent (0.0 disables, 1.0 full strength) - float strength; - // positive value added in the normalization denominator - float offset; - // number of fractional bits in the gain - int gain_bits; -}; - -void PcanGainControlFillConfigWithDefaults( - struct PcanGainControlConfig* config); - -int16_t PcanGainLookupFunction(const struct PcanGainControlConfig* config, - int32_t input_bits, uint32_t x); - -int PcanGainControlPopulateState(const struct PcanGainControlConfig* config, - struct PcanGainControlState* state, - uint32_t* noise_estimate, - const int num_channels, - const uint16_t smoothing_bits, - const int32_t input_correction_bits); - -void PcanGainControlFreeStateContents(struct PcanGainControlState* state); - -#ifdef __cplusplus -} // extern "C" -#endif - -#endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_PCAN_GAIN_CONTROL_UTIL_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/window.c b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/window.c deleted file mode 100644 index 10da6762..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/window.c +++ /dev/null @@ -1,70 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/experimental/microfrontend/lib/window.h" - -#include - -int WindowProcessSamples(struct WindowState* state, const int16_t* samples, - size_t num_samples, size_t* num_samples_read) { - const int size = state->size; - - // Copy samples from the samples buffer over to our local input. - size_t max_samples_to_copy = state->size - state->input_used; - if (max_samples_to_copy > num_samples) { - max_samples_to_copy = num_samples; - } - memcpy(state->input + state->input_used, samples, - max_samples_to_copy * sizeof(*samples)); - *num_samples_read = max_samples_to_copy; - state->input_used += max_samples_to_copy; - - if (state->input_used < state->size) { - // We don't have enough samples to compute a window. - return 0; - } - - // Apply the window to the input. - const int16_t* coefficients = state->coefficients; - const int16_t* input = state->input; - int16_t* output = state->output; - int i; - int16_t max_abs_output_value = 0; - for (i = 0; i < size; ++i) { - int16_t new_value = - (((int32_t)*input++) * *coefficients++) >> kFrontendWindowBits; - *output++ = new_value; - if (new_value < 0) { - new_value = -new_value; - } - if (new_value > max_abs_output_value) { - max_abs_output_value = new_value; - } - } - // Shuffle the input down by the step size, and update how much we have used. - memmove(state->input, state->input + state->step, - sizeof(*state->input) * (state->size - state->step)); - state->input_used -= state->step; - state->max_abs_output_value = max_abs_output_value; - - // Indicate that the output buffer is valid for the next stage. - return 1; -} - -void WindowReset(struct WindowState* state) { - memset(state->input, 0, state->size * sizeof(*state->input)); - memset(state->output, 0, state->size * sizeof(*state->output)); - state->input_used = 0; - state->max_abs_output_value = 0; -} diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/window.h b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/window.h deleted file mode 100644 index bad81514..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/window.h +++ /dev/null @@ -1,49 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_WINDOW_H_ -#define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_WINDOW_H_ - -#include -#include - -#define kFrontendWindowBits 12 - -#ifdef __cplusplus -extern "C" { -#endif - -struct WindowState { - size_t size; - int16_t* coefficients; - size_t step; - - int16_t* input; - size_t input_used; - int16_t* output; - int16_t max_abs_output_value; -}; - -// Applies a window to the samples coming in, stepping forward at the given -// rate. -int WindowProcessSamples(struct WindowState* state, const int16_t* samples, - size_t num_samples, size_t* num_samples_read); - -void WindowReset(struct WindowState* state); - -#ifdef __cplusplus -} // extern "C" -#endif - -#endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_WINDOW_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/window_util.c b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/window_util.c deleted file mode 100644 index eee6e7b5..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/window_util.c +++ /dev/null @@ -1,73 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/experimental/microfrontend/lib/window_util.h" - -#include -#include -#include -#include - -// Some platforms don't have M_PI -#ifndef M_PI -#define M_PI 3.14159265358979323846 -#endif - -void WindowFillConfigWithDefaults(struct WindowConfig* config) { - config->size_ms = 25; - config->step_size_ms = 10; -} - -int WindowPopulateState(const struct WindowConfig* config, - struct WindowState* state, int sample_rate) { - state->size = config->size_ms * sample_rate / 1000; - state->step = config->step_size_ms * sample_rate / 1000; - - state->coefficients = malloc(state->size * sizeof(*state->coefficients)); - if (state->coefficients == NULL) { - fprintf(stderr, "Failed to allocate window coefficients\n"); - return 0; - } - - // Populate the window values. - const float arg = M_PI * 2.0 / ((float)state->size); - int i; - for (i = 0; i < state->size; ++i) { - float float_value = 0.5 - (0.5 * cos(arg * (i + 0.5))); - // Scale it to fixed point and round it. - state->coefficients[i] = - floor(float_value * (1 << kFrontendWindowBits) + 0.5); - } - - state->input_used = 0; - state->input = malloc(state->size * sizeof(*state->input)); - if (state->input == NULL) { - fprintf(stderr, "Failed to allocate window input\n"); - return 0; - } - - state->output = malloc(state->size * sizeof(*state->output)); - if (state->output == NULL) { - fprintf(stderr, "Failed to allocate window output\n"); - return 0; - } - - return 1; -} - -void WindowFreeStateContents(struct WindowState* state) { - free(state->coefficients); - free(state->input); - free(state->output); -} diff --git a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/window_util.h b/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/window_util.h deleted file mode 100644 index 68e4de9e..00000000 --- a/code/components/tflite-lib/tensorflow/lite/experimental/microfrontend/lib/window_util.h +++ /dev/null @@ -1,45 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_WINDOW_UTIL_H_ -#define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_WINDOW_UTIL_H_ - -#include "tensorflow/lite/experimental/microfrontend/lib/window.h" - -#ifdef __cplusplus -extern "C" { -#endif - -struct WindowConfig { - // length of window frame in milliseconds - size_t size_ms; - // length of step for next frame in milliseconds - size_t step_size_ms; -}; - -// Populates the WindowConfig with "sane" default values. -void WindowFillConfigWithDefaults(struct WindowConfig* config); - -// Allocates any buffers. -int WindowPopulateState(const struct WindowConfig* config, - struct WindowState* state, int sample_rate); - -// Frees any allocated buffers. -void WindowFreeStateContents(struct WindowState* state); - -#ifdef __cplusplus -} // extern "C" -#endif - -#endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_WINDOW_UTIL_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/common.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/common.h deleted file mode 100644 index 205294fd..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/common.h +++ /dev/null @@ -1,1180 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_COMMON_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_COMMON_H_ - -#include -#ifndef ALLOW_SLOW_GENERIC_DEPTHWISECONV_FALLBACK -#ifdef GEMMLOWP_ALLOW_SLOW_SCALAR_FALLBACK -#define ALLOW_SLOW_GENERIC_DEPTHWISECONV_FALLBACK -#endif -#endif - -#include - -#include "fixedpoint/fixedpoint.h" -#include "tensorflow/lite/kernels/internal/cppmath.h" -#include "tensorflow/lite/kernels/internal/optimized/neon_check.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -constexpr int kReverseShift = -1; - -inline void GetActivationMinMax(FusedActivationFunctionType ac, - float* output_activation_min, - float* output_activation_max) { - switch (ac) { - case FusedActivationFunctionType::kNone: - *output_activation_min = std::numeric_limits::lowest(); - *output_activation_max = std::numeric_limits::max(); - break; - case FusedActivationFunctionType::kRelu: - *output_activation_min = 0.f; - *output_activation_max = std::numeric_limits::max(); - break; - case FusedActivationFunctionType::kRelu1: - *output_activation_min = -1.f; - *output_activation_max = 1.f; - break; - case FusedActivationFunctionType::kRelu6: - *output_activation_min = 0.f; - *output_activation_max = 6.f; - break; - } -} - -template -inline T ActivationFunctionWithMinMax(T x, T output_activation_min, - T output_activation_max) { - using std::max; - using std::min; - return min(max(x, output_activation_min), output_activation_max); -} - -// Legacy function, left for compatibility only. -template -float ActivationFunction(float x) { - float output_activation_min, output_activation_max; - GetActivationMinMax(Ac, &output_activation_min, &output_activation_max); - return ActivationFunctionWithMinMax(x, output_activation_min, - output_activation_max); -} - -inline void BiasAndClamp(float clamp_min, float clamp_max, int bias_size, - const float* bias_data, int array_size, - float* array_data) { - if (bias_size == 0) return; - // Note: see b/132215220: in May 2019 we thought it would be OK to replace - // this with the Eigen one-liner: - // return (array.colwise() + bias).cwiseMin(clamp_max).cwiseMin(clamp_max). - // This turned out to severely regress performance: +4ms (i.e. 8%) on - // MobileNet v2 / 1.0 / 224. So we keep custom NEON code for now. - TFLITE_DCHECK_EQ((array_size % bias_size), 0); -#ifdef USE_NEON - float* array_ptr = array_data; - float* array_end_ptr = array_ptr + array_size; - const auto clamp_min_vec = vdupq_n_f32(clamp_min); - const auto clamp_max_vec = vdupq_n_f32(clamp_max); - for (; array_ptr != array_end_ptr; array_ptr += bias_size) { - int i = 0; - for (; i <= bias_size - 16; i += 16) { - auto b0 = vld1q_f32(bias_data + i); - auto b1 = vld1q_f32(bias_data + i + 4); - auto b2 = vld1q_f32(bias_data + i + 8); - auto b3 = vld1q_f32(bias_data + i + 12); - auto a0 = vld1q_f32(array_ptr + i); - auto a1 = vld1q_f32(array_ptr + i + 4); - auto a2 = vld1q_f32(array_ptr + i + 8); - auto a3 = vld1q_f32(array_ptr + i + 12); - auto x0 = vaddq_f32(a0, b0); - auto x1 = vaddq_f32(a1, b1); - auto x2 = vaddq_f32(a2, b2); - auto x3 = vaddq_f32(a3, b3); - x0 = vmaxq_f32(clamp_min_vec, x0); - x1 = vmaxq_f32(clamp_min_vec, x1); - x2 = vmaxq_f32(clamp_min_vec, x2); - x3 = vmaxq_f32(clamp_min_vec, x3); - x0 = vminq_f32(clamp_max_vec, x0); - x1 = vminq_f32(clamp_max_vec, x1); - x2 = vminq_f32(clamp_max_vec, x2); - x3 = vminq_f32(clamp_max_vec, x3); - vst1q_f32(array_ptr + i, x0); - vst1q_f32(array_ptr + i + 4, x1); - vst1q_f32(array_ptr + i + 8, x2); - vst1q_f32(array_ptr + i + 12, x3); - } - for (; i <= bias_size - 4; i += 4) { - auto b = vld1q_f32(bias_data + i); - auto a = vld1q_f32(array_ptr + i); - auto x = vaddq_f32(a, b); - x = vmaxq_f32(clamp_min_vec, x); - x = vminq_f32(clamp_max_vec, x); - vst1q_f32(array_ptr + i, x); - } - for (; i < bias_size; i++) { - array_ptr[i] = ActivationFunctionWithMinMax(array_ptr[i] + bias_data[i], - clamp_min, clamp_max); - } - } -#else // not NEON - for (int array_offset = 0; array_offset < array_size; - array_offset += bias_size) { - for (int i = 0; i < bias_size; i++) { - array_data[array_offset + i] = ActivationFunctionWithMinMax( - array_data[array_offset + i] + bias_data[i], clamp_min, clamp_max); - } - } -#endif -} - -// Single-rounding MultiplyByQuantizedMultiplier -#if TFLITE_SINGLE_ROUNDING -inline int32_t MultiplyByQuantizedMultiplier(int32_t x, - int32_t quantized_multiplier, - int shift) { - TFLITE_DCHECK(quantized_multiplier >= 0); - TFLITE_DCHECK(shift >= -31 && shift <= 30); - - const int64_t total_shift = 31 - shift; - const int64_t round = static_cast(1) << (total_shift - 1); - int64_t result = x * static_cast(quantized_multiplier) + round; - result = result >> total_shift; - - TFLITE_DCHECK(result >= std::numeric_limits::min() && - result <= std::numeric_limits::max()); - return static_cast(result); -} - -inline int32_t MultiplyByQuantizedMultiplierSmallerThanOneExp( - int32_t x, int32_t quantized_multiplier, int shift) { - TFLITE_DCHECK_LE(shift, 0); - return MultiplyByQuantizedMultiplier(x, quantized_multiplier, shift); -} - -inline int32_t MultiplyByQuantizedMultiplierGreaterThanOne( - int32_t x, int32_t quantized_multiplier, int shift) { - TFLITE_DCHECK_GE(shift, 0); - return MultiplyByQuantizedMultiplier(x, quantized_multiplier, shift); -} - -inline int32_t MultiplyByQuantizedMultiplier(int64_t x, - int32_t quantized_multiplier, - int shift) { - // Inputs: - // - quantized_multiplier has fixed point at bit 31 - // - shift is -31 to +7 (negative for right shift) - // - // Assumptions: The following input ranges are assumed - // - quantize_scale>=0 (the usual range is (1<<30) to (1>>31)-1) - // - scaling is chosen so final scaled result fits in int32_t - // - input x is in the range -(1<<47) <= x < (1<<47) - TFLITE_DCHECK(quantized_multiplier >= 0); - TFLITE_DCHECK(shift >= -31 && shift < 8); - TFLITE_DCHECK(x >= -(static_cast(1) << 47) && - x < (static_cast(1) << 47)); - - const int32_t reduced_multiplier = - (quantized_multiplier < 0x7FFF0000) - ? ((quantized_multiplier + (1 << 15)) >> 16) - : 0x7FFF; - const int64_t total_shift = 15 - shift; - const int64_t round = static_cast(1) << (total_shift - 1); - int64_t result = x * static_cast(reduced_multiplier) + round; - result = result >> total_shift; - - TFLITE_DCHECK(result >= std::numeric_limits::min() && - result <= std::numeric_limits::max()); - return static_cast(result); -} - -#ifdef USE_NEON -inline int32x4x4_t MultiplyByQuantizedMultiplier4Rows( - int32x4x4_t input_val, int32_t quantized_multiplier, int shift) { - TFLITE_DCHECK(quantized_multiplier >= 0); - - const int right_shift = std::min(-1, shift); - const int left_shift = shift - right_shift; - - const int32x4_t multiplier_dup = vdupq_n_s32(quantized_multiplier); - const int32x4_t left_shift_dup = vdupq_n_s32(left_shift); - const int32x4_t right_shift_dup = vdupq_n_s32(right_shift); - - int32x4x4_t result; - result.val[0] = vrshlq_s32( - vqdmulhq_s32(vshlq_s32(input_val.val[0], left_shift_dup), multiplier_dup), - right_shift_dup); - - result.val[1] = vrshlq_s32( - vqdmulhq_s32(vshlq_s32(input_val.val[1], left_shift_dup), multiplier_dup), - right_shift_dup); - - result.val[2] = vrshlq_s32( - vqdmulhq_s32(vshlq_s32(input_val.val[2], left_shift_dup), multiplier_dup), - right_shift_dup); - - result.val[3] = vrshlq_s32( - vqdmulhq_s32(vshlq_s32(input_val.val[3], left_shift_dup), multiplier_dup), - right_shift_dup); - - return result; -} -#endif // USE_NEON -// Double-rounding MultiplyByQuantizedMultiplier -#else -inline int32_t MultiplyByQuantizedMultiplierSmallerThanOneExp( - int32_t x, int32_t quantized_multiplier, int left_shift) { - using gemmlowp::RoundingDivideByPOT; - using gemmlowp::SaturatingRoundingDoublingHighMul; - return RoundingDivideByPOT( - SaturatingRoundingDoublingHighMul(x, quantized_multiplier), -left_shift); -} - -inline int32_t MultiplyByQuantizedMultiplierGreaterThanOne( - int32_t x, int32_t quantized_multiplier, int left_shift) { - using gemmlowp::SaturatingRoundingDoublingHighMul; - return SaturatingRoundingDoublingHighMul(x * (1 << left_shift), - quantized_multiplier); -} - -inline int32_t MultiplyByQuantizedMultiplier(int32_t x, - int32_t quantized_multiplier, - int shift) { - using gemmlowp::RoundingDivideByPOT; - using gemmlowp::SaturatingRoundingDoublingHighMul; - int left_shift = shift > 0 ? shift : 0; - int right_shift = shift > 0 ? 0 : -shift; - return RoundingDivideByPOT(SaturatingRoundingDoublingHighMul( - x * (1 << left_shift), quantized_multiplier), - right_shift); -} - -inline int32_t MultiplyByQuantizedMultiplier(int64_t x, - int32_t quantized_multiplier, - int shift) { - // Inputs: - // - quantized_multiplier has fixed point at bit 31 - // - shift is -31 to +7 (negative for right shift) - // - // Assumptions: The following input ranges are assumed - // - quantize_scale>=0 (the usual range is (1<<30) to (1>>31)-1) - // - scaling is chosen so final scaled result fits in int32_t - // - input x is in the range -(1<<47) <= x < (1<<47) - assert(quantized_multiplier >= 0); - assert(shift >= -31 && shift < 8); - assert(x >= -(static_cast(1) << 47) && - x < (static_cast(1) << 47)); - - int32_t reduced_multiplier = (quantized_multiplier < 0x7FFF0000) - ? ((quantized_multiplier + (1 << 15)) >> 16) - : 0x7FFF; - int total_shift = 15 - shift; - x = (x * (int64_t)reduced_multiplier) + ((int64_t)1 << (total_shift - 1)); - int32_t result = x >> total_shift; - return result; -} - -#ifdef USE_NEON -// Round uses ARM's rounding shift right. -inline int32x4x4_t MultiplyByQuantizedMultiplier4Rows( - int32x4x4_t input_val, int32_t quantized_multiplier, int shift) { - const int left_shift = std::max(shift, 0); - const int right_shift = std::min(shift, 0); - int32x4x4_t result; - - int32x4_t multiplier_dup = vdupq_n_s32(quantized_multiplier); - int32x4_t left_shift_dup = vdupq_n_s32(left_shift); - int32x4_t right_shift_dup = vdupq_n_s32(right_shift); - - result.val[0] = - vrshlq_s32(vqrdmulhq_s32(vshlq_s32(input_val.val[0], left_shift_dup), - multiplier_dup), - right_shift_dup); - - result.val[1] = - vrshlq_s32(vqrdmulhq_s32(vshlq_s32(input_val.val[1], left_shift_dup), - multiplier_dup), - right_shift_dup); - - result.val[2] = - vrshlq_s32(vqrdmulhq_s32(vshlq_s32(input_val.val[2], left_shift_dup), - multiplier_dup), - right_shift_dup); - - result.val[3] = - vrshlq_s32(vqrdmulhq_s32(vshlq_s32(input_val.val[3], left_shift_dup), - multiplier_dup), - right_shift_dup); - - return result; -} -#endif // USE_NEON -#endif // TFLITE_SINGLE_ROUNDING - -template -int CountLeadingZeros(T integer_input) { - static_assert(std::is_unsigned::value, - "Only unsigned integer types handled."); -#if defined(__GNUC__) - return integer_input ? __builtin_clz(integer_input) - : std::numeric_limits::digits; -#else - if (integer_input == 0) { - return std::numeric_limits::digits; - } - - const T one_in_leading_positive = static_cast(1) - << (std::numeric_limits::digits - 1); - int leading_zeros = 0; - while (integer_input < one_in_leading_positive) { - integer_input <<= 1; - ++leading_zeros; - } - return leading_zeros; -#endif -} - -template -inline int CountLeadingSignBits(T integer_input) { - static_assert(std::is_signed::value, "Only signed integer types handled."); -#if defined(__GNUC__) && !defined(__clang__) - return integer_input ? __builtin_clrsb(integer_input) - : std::numeric_limits::digits; -#else - using U = typename std::make_unsigned::type; - return integer_input >= 0 - ? CountLeadingZeros(static_cast(integer_input)) - 1 - : integer_input != std::numeric_limits::min() - ? CountLeadingZeros(2 * static_cast(-integer_input) - 1) - : 0; -#endif -} - -// Use "count leading zeros" helper functions to do a fast Floor(log_2(x)). -template -inline Integer FloorLog2(Integer n) { - static_assert(std::is_integral::value, ""); - static_assert(std::is_signed::value, ""); - static_assert(sizeof(Integer) == 4 || sizeof(Integer) == 8, ""); - TFLITE_CHECK_GT(n, 0); - if (sizeof(Integer) == 4) { - return 30 - CountLeadingSignBits(n); - } else { - return 62 - CountLeadingSignBits(n); - } -} - -// The size of the LUT depends on the type of input. For int8 inputs a simple -// 256 entries LUT is used. For int16 inputs the high 9 bits are used for -// indexing and the 7 remaining bits are used for interpolation. We thus use a -// 513-entries LUT for int16 cases, 512 for the 9-bit indexing and 1 extra entry -// to interpolate the last value. -template -constexpr int lut_size() { - static_assert(std::is_same::value || - std::is_same::value, - "Only LUTs with int8 or int16 inputs are supported."); - return std::is_same::value ? 256 : 513; -} - -// Generate a LUT for 'func' which can be used to approximate functions like -// exp, log, ... -// -// - func: the function to build the LUT for (e.g exp(x)) -// - input_min, input_max: range of the func inputs -// - output_min, output_max: range of the func outputs -// - lut: pointer to the LUT table to fill, the table must be of size -// lut_size() -template -inline void gen_lut(FloatT (*func)(FloatT), FloatT input_min, FloatT input_max, - FloatT output_min, FloatT output_max, LutOutT* lut) { - static_assert(std::is_same::value || - std::is_same::value, - "Only LUTs with int8 or int16 inputs are supported."); - static_assert(std::is_same::value || - std::is_same::value, - "Only LUTs with int8 or int16 outputs are supported."); - static_assert(std::is_floating_point::value, - "FloatT must be a floating-point type."); - - const int nb_steps = std::is_same::value ? 256 : 512; - const FloatT step = (input_max - input_min) / nb_steps; - const FloatT half_step = step / 2; - const FloatT output_scaling_inv = - static_cast(std::numeric_limits::max() - - std::numeric_limits::min() + 1) / - (output_max - output_min); - const FloatT table_min = - static_cast(std::numeric_limits::min()); - const FloatT table_max = - static_cast(std::numeric_limits::max()); - - for (int i = 0; i < nb_steps; i++) { - const FloatT val = func(input_min + i * step); - const FloatT val_midpoint = func(input_min + i * step + half_step); - const FloatT val_next = func(input_min + (i + 1) * step); - - const FloatT sample_val = TfLiteRound(val * output_scaling_inv); - const FloatT midpoint_interp_val = - TfLiteRound((val_next * output_scaling_inv + - TfLiteRound(val * output_scaling_inv)) / - 2); - const FloatT midpoint_val = TfLiteRound(val_midpoint * output_scaling_inv); - const FloatT midpoint_err = midpoint_interp_val - midpoint_val; - const FloatT bias = TfLiteRound(midpoint_err / 2); - - lut[i] = static_cast(std::min( - std::max(sample_val - bias, table_min), table_max)); - } - - const bool with_extra_interpolation_value = - std::is_same::value; - if (with_extra_interpolation_value) { - lut[nb_steps] = static_cast(std::min( - std::max(TfLiteRound(func(input_max) * output_scaling_inv), - table_min), - table_max)); - } -} - -// LUT must have 513 values -template -inline LutOutT lut_lookup_with_interpolation(int16_t value, - const LutOutT* lut) { - static_assert(std::is_same::value || - std::is_same::value, - "Only LUTs with int8 or int16 outputs are supported."); - // 512 base values, lut[513] is only used to calculate the slope - const uint16_t index = static_cast(256 + (value >> 7)); - assert(index < 512 && "LUT index out of range."); - const int16_t offset = value & 0x7f; - - // Base and slope are Q0.x - const LutOutT base = lut[index]; - const LutOutT slope = lut[index + 1] - lut[index]; - - // Q0.x * Q0.7 = Q0.(x + 7) - // Round and convert from Q0.(x + 7) to Q0.x - const int delta = (slope * offset + 64) >> 7; - - // Q0.15 + Q0.15 - return static_cast(base + delta); -} - -// int16_t -> int16_t table lookup with interpolation -// LUT must have 513 values -inline int16_t lut_lookup(int16_t value, const int16_t* lut) { - return lut_lookup_with_interpolation(value, lut); -} - -// int16_t -> int8_t table lookup with interpolation -// LUT must have 513 values -inline int8_t lut_lookup(int16_t value, const int8_t* lut) { - return lut_lookup_with_interpolation(value, lut); -} - -// int8_t -> int8_t table lookup without interpolation -// LUT must have 256 values -inline int8_t lut_lookup(int8_t value, const int8_t* lut) { - return lut[128 + value]; -} - -// int8_t -> int16_t table lookup without interpolation -// LUT must have 256 values -inline int16_t lut_lookup(int8_t value, const int16_t* lut) { - return lut[128 + value]; -} - -// Table of sigmoid(i/24) at 0.16 format - 256 elements. - -// We use combined sigmoid and tanh look-up table, since -// tanh(x) = 2*sigmoid(2*x) -1. -// Both functions are symmetric, so the LUT table is only needed -// for the absolute value of the input. -static const uint16_t sigmoid_table_uint16[256] = { - 32768, 33451, 34133, 34813, 35493, 36169, 36843, 37513, 38180, 38841, 39498, - 40149, 40794, 41432, 42064, 42688, 43304, 43912, 44511, 45102, 45683, 46255, - 46817, 47369, 47911, 48443, 48964, 49475, 49975, 50464, 50942, 51409, 51865, - 52311, 52745, 53169, 53581, 53983, 54374, 54755, 55125, 55485, 55834, 56174, - 56503, 56823, 57133, 57433, 57724, 58007, 58280, 58544, 58800, 59048, 59288, - 59519, 59743, 59959, 60168, 60370, 60565, 60753, 60935, 61110, 61279, 61441, - 61599, 61750, 61896, 62036, 62172, 62302, 62428, 62549, 62666, 62778, 62886, - 62990, 63090, 63186, 63279, 63368, 63454, 63536, 63615, 63691, 63765, 63835, - 63903, 63968, 64030, 64090, 64148, 64204, 64257, 64308, 64357, 64405, 64450, - 64494, 64536, 64576, 64614, 64652, 64687, 64721, 64754, 64786, 64816, 64845, - 64873, 64900, 64926, 64950, 64974, 64997, 65019, 65039, 65060, 65079, 65097, - 65115, 65132, 65149, 65164, 65179, 65194, 65208, 65221, 65234, 65246, 65258, - 65269, 65280, 65291, 65301, 65310, 65319, 65328, 65337, 65345, 65352, 65360, - 65367, 65374, 65381, 65387, 65393, 65399, 65404, 65410, 65415, 65420, 65425, - 65429, 65433, 65438, 65442, 65445, 65449, 65453, 65456, 65459, 65462, 65465, - 65468, 65471, 65474, 65476, 65479, 65481, 65483, 65485, 65488, 65489, 65491, - 65493, 65495, 65497, 65498, 65500, 65501, 65503, 65504, 65505, 65507, 65508, - 65509, 65510, 65511, 65512, 65513, 65514, 65515, 65516, 65517, 65517, 65518, - 65519, 65520, 65520, 65521, 65522, 65522, 65523, 65523, 65524, 65524, 65525, - 65525, 65526, 65526, 65526, 65527, 65527, 65528, 65528, 65528, 65529, 65529, - 65529, 65529, 65530, 65530, 65530, 65530, 65531, 65531, 65531, 65531, 65531, - 65532, 65532, 65532, 65532, 65532, 65532, 65533, 65533, 65533, 65533, 65533, - 65533, 65533, 65533, 65534, 65534, 65534, 65534, 65534, 65534, 65534, 65534, - 65534, 65534, 65535}; - -// TODO(b/77858996): Add these to gemmlowp. -template -IntegerType SaturatingAddNonGemmlowp(IntegerType a, IntegerType b) { - static_assert(std::is_same::value, "unimplemented"); - return a; -} - -template <> -inline std::int32_t SaturatingAddNonGemmlowp(std::int32_t a, std::int32_t b) { - std::int64_t a64 = a; - std::int64_t b64 = b; - std::int64_t sum = a64 + b64; - return static_cast(std::min( - static_cast(std::numeric_limits::max()), - std::max( - static_cast(std::numeric_limits::min()), - sum))); -} - -template -gemmlowp::FixedPoint SaturatingAddNonGemmlowp( - gemmlowp::FixedPoint a, - gemmlowp::FixedPoint b) { - return gemmlowp::FixedPoint::FromRaw( - SaturatingAddNonGemmlowp(a.raw(), b.raw())); -} - -template -IntegerType SaturatingSub(IntegerType a, IntegerType b) { - static_assert(std::is_same::value, "unimplemented"); - return a; -} - -template <> -inline std::int16_t SaturatingSub(std::int16_t a, std::int16_t b) { - std::int32_t a32 = a; - std::int32_t b32 = b; - std::int32_t diff = a32 - b32; - return static_cast( - std::min(static_cast(32767), - std::max(static_cast(-32768), diff))); -} - -template <> -inline std::int32_t SaturatingSub(std::int32_t a, std::int32_t b) { - std::int64_t a64 = a; - std::int64_t b64 = b; - std::int64_t diff = a64 - b64; - return static_cast(std::min( - static_cast(std::numeric_limits::max()), - std::max( - static_cast(std::numeric_limits::min()), - diff))); -} - -template -gemmlowp::FixedPoint SaturatingSub( - gemmlowp::FixedPoint a, - gemmlowp::FixedPoint b) { - return gemmlowp::FixedPoint::FromRaw( - SaturatingSub(a.raw(), b.raw())); -} -// End section to be moved to gemmlowp. - -template -IntegerType SaturatingRoundingMultiplyByPOTParam(IntegerType x, int exponent) { - if (exponent == 0) { - return x; - } - using ScalarIntegerType = - typename gemmlowp::FixedPointRawTypeTraits::ScalarRawType; - const IntegerType min = - gemmlowp::Dup(std::numeric_limits::min()); - const IntegerType max = - gemmlowp::Dup(std::numeric_limits::max()); - const int ScalarIntegerTypeBits = 8 * sizeof(ScalarIntegerType); - - const std::int32_t threshold = - ((1 << (ScalarIntegerTypeBits - 1 - exponent)) - 1); - const IntegerType positive_mask = - gemmlowp::MaskIfGreaterThan(x, gemmlowp::Dup(threshold)); - const IntegerType negative_mask = - gemmlowp::MaskIfLessThan(x, gemmlowp::Dup(-threshold)); - - IntegerType result = gemmlowp::ShiftLeft(x, exponent); - result = gemmlowp::SelectUsingMask(positive_mask, max, result); - result = gemmlowp::SelectUsingMask(negative_mask, min, result); - return result; -} - -// If we want to leave IntegerBits fixed, then multiplication -// by a power of two has to be saturating/rounding, not exact anymore. -template -gemmlowp::FixedPoint -SaturatingRoundingMultiplyByPOTParam( - gemmlowp::FixedPoint a, int exponent) { - return gemmlowp::FixedPoint::FromRaw( - SaturatingRoundingMultiplyByPOTParam(a.raw(), exponent)); -} - -// Convert int32_t multiplier to int16_t with rounding. -inline void DownScaleInt32ToInt16Multiplier(int32_t multiplier_int32_t, - int16_t* multiplier_int16_t) { - TFLITE_DCHECK_GE(multiplier_int32_t, 0); - static constexpr int32_t kRoundingOffset = 1 << 15; - if (multiplier_int32_t >= - std::numeric_limits::max() - kRoundingOffset) { - *multiplier_int16_t = std::numeric_limits::max(); - return; - } - const int32_t result = (multiplier_int32_t + kRoundingOffset) >> 16; - TFLITE_DCHECK_LE(result << 16, multiplier_int32_t + kRoundingOffset); - TFLITE_DCHECK_GT(result << 16, multiplier_int32_t - kRoundingOffset); - *multiplier_int16_t = result; - TFLITE_DCHECK_EQ(*multiplier_int16_t, result); -} - -// Minimum output bits to accommodate log of maximum input range. It actually -// does not matter if one considers, say, [-64,64] or [-64,64). -// -// For example, run this through Octave: -// [0:127; ... -// ceil(log(abs( log(2.^(0:127))+1 ))/log(2)); ... -// ceil(log(abs( log(2.^(0:127))+1 ))/log(2))] -constexpr int min_log_x_output_bits(int input_bits) { - return input_bits > 90 ? 7 - : input_bits > 44 ? 6 - : input_bits > 21 ? 5 - : input_bits > 10 ? 4 - : input_bits > 4 ? 3 - : input_bits > 1 ? 2 - : 1; -} - -// Although currently the name of this function says that it cannot handle -// values less than 1, in practice it can handle as low as 1/x_max, where -// x_max is the largest representable input. In other words, the output range -// is symmetric. -template -inline gemmlowp::FixedPoint -log_x_for_x_greater_than_or_equal_to_1_impl( - gemmlowp::FixedPoint input_val) { - // assert(__builtin_clz(0u) >= std::numeric_limits::digits - 1); - // assert(__builtin_clz(0u) <= std::numeric_limits::digits); - using FixedPoint0 = gemmlowp::FixedPoint; - // The reason for accumulating the result with an extra bit of headroom is - // that z_pow_2_adj * log_2 might be saturated, and adding num_scaled * - // recip_denom will otherwise introduce an error. - static constexpr int kAccumIntegerBits = OutputIntegerBits + 1; - using FixedPointAccum = gemmlowp::FixedPoint; - - const FixedPoint0 log_2 = GEMMLOWP_CHECKED_FIXEDPOINT_CONSTANT( - FixedPoint0, 1488522236, std::log(2.0)); - const FixedPoint0 sqrt_sqrt_half = GEMMLOWP_CHECKED_FIXEDPOINT_CONSTANT( - FixedPoint0, 1805811301, std::sqrt(std::sqrt(0.5))); - const FixedPoint0 sqrt_half = GEMMLOWP_CHECKED_FIXEDPOINT_CONSTANT( - FixedPoint0, 1518500250, std::sqrt(0.5)); - const FixedPoint0 one_quarter = - GEMMLOWP_CHECKED_FIXEDPOINT_CONSTANT(FixedPoint0, 536870912, 1.0 / 4.0); - - const FixedPoint0 alpha_n = GEMMLOWP_CHECKED_FIXEDPOINT_CONSTANT( - FixedPoint0, 117049297, 11.0 / 240.0 * std::sqrt(std::sqrt(2.0))); - const FixedPoint0 alpha_d = GEMMLOWP_CHECKED_FIXEDPOINT_CONSTANT( - FixedPoint0, 127690142, 1.0 / 20.0 * std::sqrt(std::sqrt(2.0))); - const FixedPoint0 alpha_i = GEMMLOWP_CHECKED_FIXEDPOINT_CONSTANT( - FixedPoint0, 1057819769, - 2.0 / std::sqrt(std::sqrt(2.0)) - std::sqrt(std::sqrt(2.0))); - const FixedPoint0 alpha_f = GEMMLOWP_CHECKED_FIXEDPOINT_CONSTANT( - FixedPoint0, 638450708, 1.0 / 4.0 * std::sqrt(std::sqrt(2.0))); - - const FixedPointAccum shifted_quarter = - gemmlowp::Rescale(one_quarter); - - // Reinterpret the input value as Q0.31, because we will figure out the - // required shift "ourselves" instead of using, say, Rescale. - FixedPoint0 z_a = FixedPoint0::FromRaw(input_val.raw()); - // z_a_pow_2 = input_integer_bits - z_a_headroom; - int z_a_headroom_plus_1 = CountLeadingZeros(static_cast(z_a.raw())); - FixedPoint0 r_a_tmp = - SaturatingRoundingMultiplyByPOTParam(z_a, (z_a_headroom_plus_1 - 1)); - const int32_t r_a_raw = - SaturatingRoundingMultiplyByPOTParam((r_a_tmp * sqrt_half).raw(), 1); - // z_pow_2_adj = max(z_pow_2_a - 0.75, z_pow_2_b - 0.25); - // z_pow_2_adj = max(InputIntegerBits - z_a_headroom_plus_1 + 0.25, - // InputIntegerBits - z_b_headroom - 0.25); - const FixedPointAccum z_a_pow_2_adj = SaturatingAddNonGemmlowp( - FixedPointAccum::FromRaw(SaturatingRoundingMultiplyByPOTParam( - static_cast(InputIntegerBits - z_a_headroom_plus_1), - 31 - kAccumIntegerBits)), - shifted_quarter); - - // z_b is treated like z_a, but premultiplying by sqrt(0.5). - FixedPoint0 z_b = z_a * sqrt_half; - int z_b_headroom = CountLeadingZeros(static_cast(z_b.raw())) - 1; - const int32_t r_b_raw = - SaturatingRoundingMultiplyByPOTParam(z_a.raw(), z_b_headroom); - const FixedPointAccum z_b_pow_2_adj = SaturatingSub( - FixedPointAccum::FromRaw(SaturatingRoundingMultiplyByPOTParam( - static_cast(InputIntegerBits - z_b_headroom), - 31 - kAccumIntegerBits)), - shifted_quarter); - - const FixedPoint0 r = FixedPoint0::FromRaw(std::min(r_a_raw, r_b_raw)); - const FixedPointAccum z_pow_2_adj = FixedPointAccum::FromRaw( - std::max(z_a_pow_2_adj.raw(), z_b_pow_2_adj.raw())); - - const FixedPoint0 p = gemmlowp::RoundingHalfSum(r, sqrt_sqrt_half); - FixedPoint0 q = r - sqrt_sqrt_half; - q = q + q; - - const FixedPoint0 common_sq = q * q; - const FixedPoint0 num = q * r + q * common_sq * alpha_n; - const FixedPoint0 denom_minus_one_0 = - p * (alpha_i + q + alpha_d * common_sq) + alpha_f * q; - const FixedPoint0 recip_denom = - one_over_one_plus_x_for_x_in_0_1(denom_minus_one_0); - - const FixedPointAccum num_scaled = gemmlowp::Rescale(num); - return gemmlowp::Rescale(z_pow_2_adj * log_2 + - num_scaled * recip_denom); -} - -template -inline gemmlowp::FixedPoint -log_x_for_x_greater_than_or_equal_to_1( - gemmlowp::FixedPoint input_val) { - static_assert( - OutputIntegerBits >= min_log_x_output_bits(InputIntegerBits), - "Output integer bits must be sufficient to accommodate logs of inputs."); - return log_x_for_x_greater_than_or_equal_to_1_impl( - input_val); -} - -inline int32_t GetReciprocal(int32_t x, int x_integer_digits, - int* num_bits_over_unit) { - int headroom_plus_one = CountLeadingZeros(static_cast(x)); - // This is the number of bits to the left of the binary point above 1.0. - // Consider x=1.25. In that case shifted_scale=0.8 and - // no later adjustment will be needed. - *num_bits_over_unit = x_integer_digits - headroom_plus_one; - const int32_t shifted_sum_minus_one = - static_cast((static_cast(x) << headroom_plus_one) - - (static_cast(1) << 31)); - - gemmlowp::FixedPoint shifted_scale = - gemmlowp::one_over_one_plus_x_for_x_in_0_1( - gemmlowp::FixedPoint::FromRaw(shifted_sum_minus_one)); - return shifted_scale.raw(); -} - -inline void GetInvSqrtQuantizedMultiplierExp(int32_t input, int reverse_shift, - int32_t* output_inv_sqrt, - int* output_shift) { - TFLITE_DCHECK_GE(input, 0); - if (input <= 1) { - // Handle the input value 1 separately to avoid overflow in that case - // in the general computation below (b/143972021). Also handle 0 as if it - // were a 1. 0 is an invalid input here (divide by zero) and 1 is a valid - // but rare/unrealistic input value. We can expect both to occur in some - // incompletely trained models, but probably not in fully trained models. - *output_inv_sqrt = std::numeric_limits::max(); - *output_shift = 0; - return; - } - TFLITE_DCHECK_GT(input, 1); - *output_shift = 11; - while (input >= (1 << 29)) { - input /= 4; - ++*output_shift; - } - const unsigned max_left_shift_bits = - CountLeadingZeros(static_cast(input)) - 1; - const unsigned max_left_shift_bit_pairs = max_left_shift_bits / 2; - const unsigned left_shift_bit_pairs = max_left_shift_bit_pairs - 1; - *output_shift -= left_shift_bit_pairs; - input <<= 2 * left_shift_bit_pairs; - TFLITE_DCHECK_GE(input, (1 << 27)); - TFLITE_DCHECK_LT(input, (1 << 29)); - using gemmlowp::FixedPoint; - using gemmlowp::Rescale; - using gemmlowp::SaturatingRoundingMultiplyByPOT; - // Using 3 integer bits gives us enough room for the internal arithmetic in - // this Newton-Raphson iteration. - using F3 = FixedPoint; - using F0 = FixedPoint; - const F3 fixedpoint_input = F3::FromRaw(input >> 1); - const F3 fixedpoint_half_input = - SaturatingRoundingMultiplyByPOT<-1>(fixedpoint_input); - const F3 fixedpoint_half_three = - GEMMLOWP_CHECKED_FIXEDPOINT_CONSTANT(F3, (1 << 28) + (1 << 27), 1.5); - // Newton-Raphson iteration - // Naive unoptimized starting guess: x = 1 - F3 x = F3::One(); - // Naive unoptimized number of iterations: 5 - for (int i = 0; i < 5; i++) { - const F3 x3 = Rescale<3>(x * x * x); - x = Rescale<3>(fixedpoint_half_three * x - fixedpoint_half_input * x3); - } - const F0 fixedpoint_half_sqrt_2 = - GEMMLOWP_CHECKED_FIXEDPOINT_CONSTANT(F0, 1518500250, std::sqrt(2.) / 2.); - x = x * fixedpoint_half_sqrt_2; - *output_inv_sqrt = x.raw(); - if (*output_shift < 0) { - *output_inv_sqrt <<= -*output_shift; - *output_shift = 0; - } - // Convert right shift (right is positive) to left shift. - *output_shift *= reverse_shift; -} - -// DO NOT USE THIS STRUCT FOR NEW FUNCTIONALITY BEYOND IMPLEMENTING -// BROADCASTING. -// -// NdArrayDesc describes the shape and memory layout of an N-dimensional -// rectangular array of numbers. -// -// NdArrayDesc is basically identical to Dims defined in types.h. -// However, as Dims is to be deprecated, this class exists as an adaptor -// to enable simple unoptimized implementations of element-wise broadcasting -// operations. -template -struct NdArrayDesc { - // The "extent" of each dimension. Indices along dimension d must be in the - // half-open interval [0, extents[d]). - int extents[N]; - - // The number of *elements* (not bytes) between consecutive indices of each - // dimension. - int strides[N]; -}; - -// DO NOT USE THIS FUNCTION FOR NEW FUNCTIONALITY BEYOND IMPLEMENTING -// BROADCASTING. -// -// Same as Offset(), except takes as NdArrayDesc instead of Dims. -inline int SubscriptToIndex(const NdArrayDesc<4>& desc, int i0, int i1, int i2, - int i3) { - TFLITE_DCHECK(i0 >= 0 && i0 < desc.extents[0]); - TFLITE_DCHECK(i1 >= 0 && i1 < desc.extents[1]); - TFLITE_DCHECK(i2 >= 0 && i2 < desc.extents[2]); - TFLITE_DCHECK(i3 >= 0 && i3 < desc.extents[3]); - return i0 * desc.strides[0] + i1 * desc.strides[1] + i2 * desc.strides[2] + - i3 * desc.strides[3]; -} - -inline int SubscriptToIndex(const NdArrayDesc<5>& desc, int indexes[5]) { - return indexes[0] * desc.strides[0] + indexes[1] * desc.strides[1] + - indexes[2] * desc.strides[2] + indexes[3] * desc.strides[3] + - indexes[4] * desc.strides[4]; -} - -inline int SubscriptToIndex(const NdArrayDesc<8>& desc, int indexes[8]) { - return indexes[0] * desc.strides[0] + indexes[1] * desc.strides[1] + - indexes[2] * desc.strides[2] + indexes[3] * desc.strides[3] + - indexes[4] * desc.strides[4] + indexes[5] * desc.strides[5] + - indexes[6] * desc.strides[6] + indexes[7] * desc.strides[7]; -} - -// Given the dimensions of the operands for an element-wise binary broadcast, -// adjusts them so that they can be directly iterated over with simple loops. -// Returns the adjusted dims as instances of NdArrayDesc in 'desc0_out' and -// 'desc1_out'. 'desc0_out' and 'desc1_out' cannot be nullptr. -// -// This function assumes that the two input shapes are compatible up to -// broadcasting and the shorter one has already been prepended with 1s to be the -// same length. E.g., if shape0 is (1, 16, 16, 64) and shape1 is (1, 64), -// shape1 must already have been prepended to be (1, 1, 1, 64). Recall that -// Dims refer to shapes in reverse order. In this case, input0_dims will be -// (64, 16, 16, 1) and input1_dims will be (64, 1, 1, 1). -// -// When two shapes are compatible up to broadcasting, for each dimension d, -// the input extents are either equal, or one of them is 1. -// -// This function performs the following for each dimension d: -// - If the extents are equal, then do nothing since the loop that walks over -// both of the input arrays is correct. -// - Otherwise, one (and only one) of the extents must be 1. Say extent0 is 1 -// and extent1 is e1. Then set extent0 to e1 and stride0 *to 0*. This allows -// array0 to be referenced *at any index* in dimension d and still access the -// same slice. -template -inline void NdArrayDescsForElementwiseBroadcast(const Dims& input0_dims, - const Dims& input1_dims, - NdArrayDesc* desc0_out, - NdArrayDesc* desc1_out) { - TFLITE_DCHECK(desc0_out != nullptr); - TFLITE_DCHECK(desc1_out != nullptr); - - // Copy dims to desc. - for (int i = 0; i < N; ++i) { - desc0_out->extents[i] = input0_dims.sizes[i]; - desc0_out->strides[i] = input0_dims.strides[i]; - desc1_out->extents[i] = input1_dims.sizes[i]; - desc1_out->strides[i] = input1_dims.strides[i]; - } - - // Walk over each dimension. If the extents are equal do nothing. - // Otherwise, set the desc with extent 1 to have extent equal to the other and - // stride 0. - for (int i = 0; i < N; ++i) { - const int extent0 = ArraySize(input0_dims, i); - const int extent1 = ArraySize(input1_dims, i); - if (extent0 != extent1) { - if (extent0 == 1) { - desc0_out->strides[i] = 0; - desc0_out->extents[i] = extent1; - } else { - TFLITE_DCHECK_EQ(extent1, 1); - desc1_out->strides[i] = 0; - desc1_out->extents[i] = extent0; - } - } - } -} - -// Copies dims to desc, calculating strides. -template -inline void CopyDimsToDesc(const RuntimeShape& input_shape, - NdArrayDesc* desc_out) { - int desc_stride = 1; - for (int i = N - 1; i >= 0; --i) { - desc_out->extents[i] = input_shape.Dims(i); - desc_out->strides[i] = desc_stride; - desc_stride *= input_shape.Dims(i); - } -} - -template -inline void NdArrayDescsForElementwiseBroadcast( - const RuntimeShape& input0_shape, const RuntimeShape& input1_shape, - NdArrayDesc* desc0_out, NdArrayDesc* desc1_out) { - TFLITE_DCHECK(desc0_out != nullptr); - TFLITE_DCHECK(desc1_out != nullptr); - - auto extended_input0_shape = RuntimeShape::ExtendedShape(N, input0_shape); - auto extended_input1_shape = RuntimeShape::ExtendedShape(N, input1_shape); - - // Copy dims to desc, calculating strides. - CopyDimsToDesc(extended_input0_shape, desc0_out); - CopyDimsToDesc(extended_input1_shape, desc1_out); - - // Walk over each dimension. If the extents are equal do nothing. - // Otherwise, set the desc with extent 1 to have extent equal to the other and - // stride 0. - for (int i = 0; i < N; ++i) { - const int extent0 = extended_input0_shape.Dims(i); - const int extent1 = extended_input1_shape.Dims(i); - if (extent0 != extent1) { - if (extent0 == 1) { - desc0_out->strides[i] = 0; - desc0_out->extents[i] = extent1; - } else { - TFLITE_DCHECK_EQ(extent1, 1); - desc1_out->strides[i] = 0; - desc1_out->extents[i] = extent0; - } - } - } -} - -template -inline void NdArrayDescsForElementwiseBroadcast( - const RuntimeShape& input0_shape, const RuntimeShape& input1_shape, - const RuntimeShape& input2_shape, NdArrayDesc* desc0_out, - NdArrayDesc* desc1_out, NdArrayDesc* desc2_out) { - TFLITE_DCHECK(desc0_out != nullptr); - TFLITE_DCHECK(desc1_out != nullptr); - TFLITE_DCHECK(desc2_out != nullptr); - - auto extended_input0_shape = RuntimeShape::ExtendedShape(N, input0_shape); - auto extended_input1_shape = RuntimeShape::ExtendedShape(N, input1_shape); - auto extended_input2_shape = RuntimeShape::ExtendedShape(N, input2_shape); - - // Copy dims to desc, calculating strides. - CopyDimsToDesc(extended_input0_shape, desc0_out); - CopyDimsToDesc(extended_input1_shape, desc1_out); - CopyDimsToDesc(extended_input2_shape, desc2_out); - - // Walk over each dimension. If the extents are equal do nothing. - // Otherwise, set the desc with extent 1 to have extent equal to the other and - // stride 0. - for (int i = 0; i < N; ++i) { - const int extent0 = extended_input0_shape.Dims(i); - const int extent1 = extended_input1_shape.Dims(i); - const int extent2 = extended_input2_shape.Dims(i); - - int extent = extent0; - if (extent1 != 1) extent = extent1; - if (extent2 != 1) extent = extent2; - - TFLITE_DCHECK(extent0 == 1 || extent0 == extent); - TFLITE_DCHECK(extent1 == 1 || extent1 == extent); - TFLITE_DCHECK(extent2 == 1 || extent2 == extent); - - if (!(extent0 == extent1 && extent1 == extent2)) { - if (extent0 == 1) { - desc0_out->strides[i] = 0; - desc0_out->extents[i] = extent; - } - if (extent1 == 1) { - desc1_out->strides[i] = 0; - desc1_out->extents[i] = extent; - } - if (extent2 == 1) { - desc2_out->strides[i] = 0; - desc2_out->extents[i] = extent; - } - } - } -} - -// Detailed implementation of NDOpsHelper, the indexes must be a zero array. -// This implementation is equivalent to N nested loops. Ex, if N=4, it can be -// re-writen as: -// for (int b = 0; b < output.extents[0]; ++b) { -// for (int y = 0; y < output.extents[1]; ++y) { -// for (int x = 0; x < output.extents[2]; ++x) { -// for (int c = 0; c < output.extents[3]; ++c) { -// calc({b,y,x,c}); -// } -// } -// } -// } -template -typename std::enable_if::type NDOpsHelperImpl( - const NdArrayDesc& output, const Calc& calc, int indexes[N]) { - for (indexes[DIM] = 0; indexes[DIM] < output.extents[DIM]; ++indexes[DIM]) { - NDOpsHelperImpl(output, calc, indexes); - } -} - -template -typename std::enable_if::type NDOpsHelperImpl( - const NdArrayDesc& output, const Calc& calc, int indexes[N]) { - for (indexes[DIM] = 0; indexes[DIM] < output.extents[DIM]; ++indexes[DIM]) { - calc(indexes); - } -} - -// Execute the calc function in the innermost iteration based on the shape of -// the output. The calc function should take a single argument of type int[N]. -template -inline void NDOpsHelper(const NdArrayDesc& output, const Calc& calc) { - int indexes[N] = {0}; - NDOpsHelperImpl(output, calc, indexes); -} -// Copied from gemmlowp::RoundDown when we dropped direct dependency on -// gemmlowp. -// -// Returns the runtime argument rounded down to the nearest multiple of -// the fixed Modulus. -template -Integer RoundDown(Integer i) { - return i - (i % Modulus); -} - -// Copied from gemmlowp::RoundUp when we dropped direct dependency on -// gemmlowp. -// -// Returns the runtime argument rounded up to the nearest multiple of -// the fixed Modulus. -template -Integer RoundUp(Integer i) { - return RoundDown(i + Modulus - 1); -} - -// Copied from gemmlowp::CeilQuotient when we dropped direct dependency on -// gemmlowp. -// -// Returns the quotient a / b rounded up ('ceil') to the nearest integer. -template -Integer CeilQuotient(Integer a, Integer b) { - return (a + b - 1) / b; -} - -// This function is a copy of gemmlowp::HowManyThreads, copied when we dropped -// the direct dependency of internal/optimized/ on gemmlowp. -// -// It computes a reasonable number of threads to use for a GEMM of shape -// (rows, cols, depth). -// -// TODO(b/131910176): get rid of this function by switching each call site -// to its own more sensible logic for its own workload. -template -inline int LegacyHowManyThreads(int max_num_threads, int rows, int cols, - int depth) { - // Early-exit in the default case where multi-threading is disabled. - if (max_num_threads == 1) { - return 1; - } - - // Ensure that each thread has KernelRows rows to process, if at all possible. - int thread_count = std::min(max_num_threads, rows / KernelRows); - - // Limit the number of threads according to the overall size of the problem. - if (thread_count > 1) { - // Empirically determined value. - static constexpr std::uint64_t min_cubic_size_per_thread = 64 * 1024; - - // We can only multiply two out of three sizes without risking overflow - const std::uint64_t cubic_size = - std::uint64_t(rows) * std::uint64_t(cols) * std::uint64_t(depth); - - thread_count = std::min( - thread_count, static_cast(cubic_size / min_cubic_size_per_thread)); - } - - if (thread_count < 1) { - thread_count = 1; - } - - assert(thread_count > 0 && thread_count <= max_num_threads); - return thread_count; -} - -template -void optimized_ops_preload_l1_stream(const T* ptr) { -#ifdef __GNUC__ - // builtin offered by GCC-compatible compilers including clang - __builtin_prefetch(ptr, /* 0 means read */ 0, /* 0 means no locality */ 0); -#else - (void)ptr; -#endif -} - -template -void optimized_ops_preload_l1_keep(const T* ptr) { -#ifdef __GNUC__ - // builtin offered by GCC-compatible compilers including clang - __builtin_prefetch(ptr, /* 0 means read */ 0, /* 3 means high locality */ 3); -#else - (void)ptr; -#endif -} - -template -void optimized_ops_prefetch_write_l1_keep(const T* ptr) { -#ifdef __GNUC__ - // builtin offered by GCC-compatible compilers including clang - __builtin_prefetch(ptr, /* 1 means write */ 1, /* 3 means high locality */ 3); -#else - (void)ptr; -#endif -} - -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_COMMON_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/compatibility.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/compatibility.h deleted file mode 100644 index 7ba66ed8..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/compatibility.h +++ /dev/null @@ -1,122 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_COMPATIBILITY_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_COMPATIBILITY_H_ - -#include - -#include "tensorflow/lite/kernels/op_macros.h" - -#ifndef TFLITE_DCHECK -#define TFLITE_DCHECK(condition) (condition) ? (void)0 : TFLITE_ASSERT_FALSE -#endif - -#ifndef TFLITE_DCHECK_EQ -#define TFLITE_DCHECK_EQ(x, y) ((x) == (y)) ? (void)0 : TFLITE_ASSERT_FALSE -#endif - -#ifndef TFLITE_DCHECK_NE -#define TFLITE_DCHECK_NE(x, y) ((x) != (y)) ? (void)0 : TFLITE_ASSERT_FALSE -#endif - -#ifndef TFLITE_DCHECK_GE -#define TFLITE_DCHECK_GE(x, y) ((x) >= (y)) ? (void)0 : TFLITE_ASSERT_FALSE -#endif - -#ifndef TFLITE_DCHECK_GT -#define TFLITE_DCHECK_GT(x, y) ((x) > (y)) ? (void)0 : TFLITE_ASSERT_FALSE -#endif - -#ifndef TFLITE_DCHECK_LE -#define TFLITE_DCHECK_LE(x, y) ((x) <= (y)) ? (void)0 : TFLITE_ASSERT_FALSE -#endif - -#ifndef TFLITE_DCHECK_LT -#define TFLITE_DCHECK_LT(x, y) ((x) < (y)) ? (void)0 : TFLITE_ASSERT_FALSE -#endif - -// TODO(ahentz): Clean up: We should stick to the DCHECK versions. -#ifndef TFLITE_CHECK -#define TFLITE_CHECK(condition) (condition) ? (void)0 : TFLITE_ABORT -#endif - -#ifndef TFLITE_CHECK_EQ -#define TFLITE_CHECK_EQ(x, y) ((x) == (y)) ? (void)0 : TFLITE_ABORT -#endif - -#ifndef TFLITE_CHECK_NE -#define TFLITE_CHECK_NE(x, y) ((x) != (y)) ? (void)0 : TFLITE_ABORT -#endif - -#ifndef TFLITE_CHECK_GE -#define TFLITE_CHECK_GE(x, y) ((x) >= (y)) ? (void)0 : TFLITE_ABORT -#endif - -#ifndef TFLITE_CHECK_GT -#define TFLITE_CHECK_GT(x, y) ((x) > (y)) ? (void)0 : TFLITE_ABORT -#endif - -#ifndef TFLITE_CHECK_LE -#define TFLITE_CHECK_LE(x, y) ((x) <= (y)) ? (void)0 : TFLITE_ABORT -#endif - -#ifndef TFLITE_CHECK_LT -#define TFLITE_CHECK_LT(x, y) ((x) < (y)) ? (void)0 : TFLITE_ABORT -#endif - -#ifndef TF_LITE_STATIC_MEMORY -// TODO(b/162019032): Consider removing these type-aliases. -using int8 = std::int8_t; -using uint8 = std::uint8_t; -using int16 = std::int16_t; -using uint16 = std::uint16_t; -using int32 = std::int32_t; -using uint32 = std::uint32_t; -#endif // !defined(TF_LITE_STATIC_MEMORY) - -// Allow for cross-compiler usage of function signatures - currently used for -// specifying named RUY profiler regions in templated methods. -#if defined(_MSC_VER) -#define TFLITE_PRETTY_FUNCTION __FUNCSIG__ -#elif defined(__GNUC__) -#define TFLITE_PRETTY_FUNCTION __PRETTY_FUNCTION__ -#else -#define TFLITE_PRETTY_FUNCTION __func__ -#endif - -// TFLITE_DEPRECATED() -// -// Duplicated from absl/base/macros.h to avoid pulling in that library. -// Marks a deprecated class, struct, enum, function, method and variable -// declarations. The macro argument is used as a custom diagnostic message (e.g. -// suggestion of a better alternative). -// -// Example: -// -// class TFLITE_DEPRECATED("Use Bar instead") Foo {...}; -// TFLITE_DEPRECATED("Use Baz instead") void Bar() {...} -// -// Every usage of a deprecated entity will trigger a warning when compiled with -// clang's `-Wdeprecated-declarations` option. This option is turned off by -// default, but the warnings will be reported by clang-tidy. -#if defined(__clang__) && __cplusplus >= 201103L -#define TFLITE_DEPRECATED(message) __attribute__((deprecated(message))) -#endif - -#ifndef TFLITE_DEPRECATED -#define TFLITE_DEPRECATED(message) -#endif - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_COMPATIBILITY_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/cppmath.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/cppmath.h deleted file mode 100644 index c97cc31d..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/cppmath.h +++ /dev/null @@ -1,40 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_CPPMATH_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_CPPMATH_H_ - -#include - -namespace tflite { - -#if defined(TF_LITE_USE_GLOBAL_CMATH_FUNCTIONS) || \ - (defined(__ANDROID__) && !defined(__NDK_MAJOR__)) || defined(__ZEPHYR__) -#define TF_LITE_GLOBAL_STD_PREFIX -#else -#define TF_LITE_GLOBAL_STD_PREFIX std -#endif - -#define DECLARE_STD_GLOBAL_SWITCH1(tf_name, std_name) \ - template \ - inline T tf_name(const T x) { \ - return TF_LITE_GLOBAL_STD_PREFIX::std_name(x); \ - } - -DECLARE_STD_GLOBAL_SWITCH1(TfLiteRound, round); -DECLARE_STD_GLOBAL_SWITCH1(TfLiteExpm1, expm1); - -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_CPPMATH_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/max.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/max.h deleted file mode 100644 index c1810027..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/max.h +++ /dev/null @@ -1,35 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_MAX_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_MAX_H_ - -#include - -namespace tflite { - -#if defined(TF_LITE_USE_GLOBAL_MAX) || defined(__ZEPHYR__) -inline float TfLiteMax(const float& x, const float& y) { - return std::max(x, y); -} -#else -template -inline T TfLiteMax(const T& x, const T& y) { - return std::fmax(x, y); -} -#endif - -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_MAX_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/min.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/min.h deleted file mode 100644 index 62035dcc..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/min.h +++ /dev/null @@ -1,35 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_MIN_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_MIN_H_ - -#include - -namespace tflite { - -#if defined(TF_LITE_USE_GLOBAL_MIN) || defined(__ZEPHYR__) -inline float TfLiteMin(const float& x, const float& y) { - return std::min(x, y); -} -#else -template -inline T TfLiteMin(const T& x, const T& y) { - return std::fmin(x, y); -} -#endif - -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_MIN_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/optimized/neon_check.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/optimized/neon_check.h deleted file mode 100644 index 7df1129d..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/optimized/neon_check.h +++ /dev/null @@ -1,20 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_OPTIMIZED_NEON_CHECK_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_OPTIMIZED_NEON_CHECK_H_ - -// TFLM does not need to utilize any Neon optimizations. - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_OPTIMIZED_NEON_CHECK_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/portable_tensor.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/portable_tensor.h deleted file mode 100644 index 4d71c967..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/portable_tensor.h +++ /dev/null @@ -1,122 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_PORTABLE_TENSOR_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_PORTABLE_TENSOR_H_ - -#include - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -inline RuntimeShape GetTensorShape(std::vector data) { - return RuntimeShape(data.size(), data.data()); -} - -// A list of tensors in a format that can be used by kernels like split and -// concatenation. -template -class VectorOfTensors { - public: - // Build with the tensors in 'tensor_list'. - VectorOfTensors(const TfLiteContext& context, - const TfLiteIntArray& tensor_list) { - int num_tensors = tensor_list.size; - - all_data_.reserve(num_tensors); - all_shape_.reserve(num_tensors); - all_shape_ptr_.reserve(num_tensors); - - for (int i = 0; i < num_tensors; ++i) { - TfLiteTensor* t = &context.tensors[tensor_list.data[i]]; - all_data_.push_back(GetTensorData(t)); - all_shape_.push_back(GetTensorShape(t)); - } - - // Taking the pointer from inside a std::vector is only OK if the vector is - // never modified, so we populate all_shape in the previous loop and then we - // are free to grab iterators here. - for (int i = 0; i < num_tensors; ++i) { - all_shape_ptr_.push_back(&all_shape_[i]); - } - } - // Return a pointer to the data pointers of all tensors in the list. For - // example: - // float* const* f = v.data(); - // f[0][1] is the second element of the first tensor. - T* const* data() const { return all_data_.data(); } - - // Return a pointer the shape pointers of all tensors in the list. For - // example: - // const RuntimeShape* const* d = v.dims(); - // dims[1] are the dimensions of the second tensor in the list. - const RuntimeShape* const* shapes() const { return all_shape_ptr_.data(); } - - private: - std::vector all_data_; - std::vector all_shape_; - std::vector all_shape_ptr_; -}; - -// A list of quantized tensors in a format that can be used by kernels like -// split and concatenation. -class VectorOfQuantizedTensors : public VectorOfTensors { - public: - // Build with the tensors in 'tensor_list'. - VectorOfQuantizedTensors(const TfLiteContext& context, - const TfLiteIntArray& tensor_list) - : VectorOfTensors(context, tensor_list) { - for (int i = 0; i < tensor_list.size; ++i) { - TfLiteTensor* t = &context.tensors[tensor_list.data[i]]; - zero_point_.push_back(t->params.zero_point); - scale_.push_back(t->params.scale); - } - } - - const float* scale() const { return scale_.data(); } - const int32_t* zero_point() const { return zero_point_.data(); } - - private: - std::vector zero_point_; - std::vector scale_; -}; - -// Writes randomly accessed values from `input` sequentially into `output`. -template -class SequentialTensorWriter { - public: - SequentialTensorWriter(const TfLiteTensor* input, TfLiteTensor* output) { - input_data_ = GetTensorData(input); - output_ptr_ = GetTensorData(output); - } - SequentialTensorWriter(const T* input_data, T* output_data) - : input_data_(input_data), output_ptr_(output_data) {} - - void Write(int position) { *output_ptr_++ = input_data_[position]; } - void WriteN(int position, int len) { - memcpy(output_ptr_, &input_data_[position], sizeof(T) * len); - output_ptr_ += len; - } - - private: - const T* input_data_; - T* output_ptr_; -}; - -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_PORTABLE_TENSOR_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/portable_tensor_utils.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/portable_tensor_utils.h deleted file mode 100644 index 122a0dc2..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/portable_tensor_utils.h +++ /dev/null @@ -1,484 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_PORTABLE_TENSOR_UTILS_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_PORTABLE_TENSOR_UTILS_H_ - -#include -#include -#include - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" - -#if defined(_MSC_VER) -#define __restrict__ __restrict -#endif - -namespace tflite { - -namespace tensor_utils { - -// Multiplies a matrix with a scalar and reduce the result on each row to a -// scalar. -// Parameters: -// - matrix: matrix of size n_row * n_col -// - scalar: the scalar that is multiplied to each element in the matrix -// - n_row: the row count of the matrix -// - n_col: the column count of the matrix -// - output: the 32bit output -// Note: We do not need saturation because the int8 * int8 is safe from overflow -// in (2^31-1) / (2^14) = 131072, which is bigger than the n_row. Non-zero -// initial output value is not exceptionally large. -void MatrixScalarMultiplyAccumulate(const int8_t* matrix, int32_t scalar, - int32_t n_row, int32_t n_col, - int32_t* output); - -// Add another vector for each batch in the batch vector. -template -void VectorBatchVectorAdd(const T* vector, int v_size, int n_batch, - T* batch_vector) { - for (int b = 0; b < n_batch; b++) { - for (int i = 0; i < v_size; ++i) { - batch_vector[i] += vector[i]; - } - batch_vector += v_size; - } -} - -// Cwise product of two vectors. -template -inline void VectorVectorCwiseProduct(const T* vector1, const T* vector2, - int v_size, T* result) { - for (int v = 0; v < v_size; v++) { - *result++ = *vector1++ * *vector2++; - } -} - -// Cwise product of a vector and a batch-vector. -template -inline void VectorBatchVectorCwiseProduct(const T* vector, int v_size, - const T* batch_vector, int n_batch, - T* result) { - for (int b = 0; b < n_batch; b++) { - VectorVectorCwiseProduct(vector, batch_vector, v_size, result); - // Update the pointers. - result += v_size; - batch_vector += v_size; - } -} - -// Cwise product and accumulate of two vectors. Since it's a MAC operation, the -// assumption here is that result array is initialized to valid values. -template -inline void VectorVectorCwiseProductAccumulate(const T* __restrict__ vector1, - const T* __restrict__ vector2, - int v_size, - T* __restrict__ result) { - for (int v = 0; v < v_size; v++) { - *result++ += *vector1++ * *vector2++; - } -} - -// Cwise product and accumulate of a vector and a batch-vector. Since it's a MAC -// operation, the assumption here is that result array is initialized to valid -// values. -template -inline void VectorBatchVectorCwiseProductAccumulate(const T* vector, int v_size, - const T* batch_vector, - int n_batch, T* result) { - for (int b = 0; b < n_batch; b++) { - VectorVectorCwiseProductAccumulate(vector, batch_vector, v_size, result); - // Update the pointers. - result += v_size; - batch_vector += v_size; - } -} - -// Batch vector initialization with another vector. -template -void VectorBatchVectorAssign(const T* vector, int v_size, int n_batch, - T* batch_vector) { - for (int b = 0; b < n_batch; b++) { - std::copy_n(vector, v_size, batch_vector + b * v_size); - } -} - -// Checks if all entries of vector are zero for float. -bool IsZeroVector(const float* vector, int v_size); - -// Checks if all entries of vector are zero for int8. -bool IsZeroVector(const int8_t* vector, int v_size); - -// Quantizes a buffer of floating point values using a symmetric quantization -// (i.e. linear quantization without an offset) to 8-bit signed integers. -// It also outputs the range (min, max) of the floating point buffer, and the -// scaling factor used to quantize the values. -void SymmetricQuantizeFloats(const float* values, const int size, - int8_t* quantized_values, float* min_value, - float* max_value, float* scaling_factor); - -// Quantizes a buffer of floating point values using a symmetric quantization -// (i.e. linear quantization without an offset) to 8-bit signed integers. -// It uses the range (min, max) provided to the function to calculate the -// appropriate scaling factor to quantize the values. -void SymmetricQuantizeFloats(const float* values, const int size, - int8_t* quantized_values, float min_value, - float max_value, float* scaling_factor); - -void AsymmetricQuantizeFloats(const float* values, const int size, - int8_t* quantized_values, float* scaling_factor, - int32_t* offset); - -// Helper function to quantize floats. -// float_data_ptr input float vectors -// n_batch number of input vectors -// n_data size of a single input vector -// quantized_data_ptr (out) vector with quantized data -// scaling_factors (out) scaling factors (one per vector) -// zero_points (out) zero points (one per vector) -// do_asymmetric controls if the quantization should be asymmetric. -inline void BatchQuantizeFloats(const float* float_data_ptr, int n_batch, - int n_data, int8_t* quantized_data_ptr, - float* scaling_factors, int32_t* zero_points, - bool do_asymmetric) { - for (int b = 0; b < n_batch; ++b) { - const int offset = b * n_data; - if (do_asymmetric) { - tensor_utils::AsymmetricQuantizeFloats( - float_data_ptr + offset, n_data, quantized_data_ptr + offset, - &scaling_factors[b], &zero_points[b]); - } else { - float unused_min, unused_max; - tensor_utils::SymmetricQuantizeFloats( - float_data_ptr + offset, n_data, quantized_data_ptr + offset, - &unused_min, &unused_max, &scaling_factors[b]); - } - } -} - -// Multiplies a matrix by a "batched" vector (i.e. a matrix with a batch -// dimension composed by input vectors independent from each other). The result -// of the multiplication is accumulated to the passed result buffer. -// More specifically, for a matrix M of shape [n, i] and a batched-vector -// of shape [i, batch] it will first compute the product of shape [n, batch]. -// This product will be accumulated to the result buffer. -void MatrixBatchVectorMultiplyAccumulate(const float* matrix, int m_rows, - int m_cols, const float* vector, - int n_batch, float* result); - -// Same as the function above, but the matrix is a sparse tensor with block -// pattern 1x4. -// This function assumes that m_cols is a multiple of the block size (4 in this -// case) so that there's no incomplete block. -void SparseMatrixBatchVectorMultiplyAccumulate1x4( - const float* __restrict__ matrix, const int32_t* __restrict__ segments, - const int32_t* __restrict__ indices, int m_rows, int m_cols, - const float* __restrict__ vector, int n_batch, float* __restrict__ result); - -// Same as the function above, but the matrix is stored in block compressed -// sparse row format with block pattern 1x16 which consists of two arrays: -// 1. A matrix array stores non-zero blocks of the matrix in row major. -// 2. A ledger array stores nrows groups, one group per row. Each group starts -// with an integer representing the number of non-zero blocks for the -// corresponding row and follows with column indexes of the first element -// of each non-zero block. -// This function assumes that -// 1. m_cols is a multiple of 16 so that all blocks are full blocks. -// 2. m_cols < 254 * 16 so that block index can be represented by uint8. -void SparseMatrixBatchVectorMultiplyAccumulate( - const float* __restrict__ matrix, const uint8_t* __restrict__ ledger, - int m_rows, int m_cols, const float* __restrict__ vector, int n_batch, - float* __restrict__ result); - -// Same as the function above, but for values quantized using symmetric -// quantization (e.g. by calling SymmetricQuantizeFloats). -// The passed scaling factors is a buffer of the quantization scaling factors -// that will be used to dequentize the products into the final result buffer. -// These scaling factors are the multiplication of the matrix scaling factor -// by the vector's scaling factor, one per batch (i.e. this allows quantizing -// each batch in the batch-vector matrix independently). -void MatrixBatchVectorMultiplyAccumulate( - const int8_t* __restrict__ matrix, const int m_rows, const int m_cols, - const int8_t* __restrict__ vectors, - const float* __restrict__ scaling_factors, int n_batch, - float* __restrict__ result); - -// Same as the function above except that vector values -// are quantized with asymmetric quantization per-batch and the matrix -// is quantized per row. -void MatrixBatchVectorMultiplyAccumulate( - const int8_t* __restrict__ matrix, const int m_rows, const int m_cols, - const int8_t* __restrict__ vectors, - const float* __restrict__ scaling_factors, int n_batch, - float* __restrict__ result, const float* __restrict__ per_channel_scale, - const int32_t* __restrict__ input_offset); - -// Same as the function above, but the matrix is a sparse tensor with block -// pattern 1x16. -// This function assumes that m_cols is a multiple of the block size (16 in this -// case) so that there's no incomplete block. Also, it assumes all offsets of -// input, output and filter are zero. -void SparseMatrixBatchVectorMultiplyAccumulate1x16( - const int8_t* __restrict__ matrix, const int32_t* __restrict__ segments, - const int32_t* __restrict__ indices, int m_rows, int m_cols, - const int8_t* __restrict__ vector, const int32_t* __restrict__ bias_vector, - int n_batch, const int32_t input_offset, const int32_t output_multiplier, - const int32_t output_shift, const int32_t output_offset, - const int32_t output_activation_min, const int32_t output_activation_max, - int8_t* __restrict__ result); - -// Same as the function above, but the matrix is stored in block compressed -// sparse row format with block pattern 1x16 which consists of two arrays: -// 1. A matrix array stores non-zero blocks of the matrix in row major. -// 2. A ledger array stores nrows groups, one group per row. Each group starts -// with an integer representing the number of non-zero blocks for the -// corresponding row followed by column index of the first element of -// each non-zero block. -// This function assumes that -// 1. m_cols is a multiple of 16 so that all blocks are full blocks. -// 2. m_cols < 254 * 16 so that block index can be represented by uint8. -void SparseMatrixBatchVectorMultiplyAccumulate( - const int8_t* __restrict__ matrix, const uint8_t* __restrict__ ledger, - const int m_rows, const int m_cols, const int8_t* __restrict__ vectors, - const float* __restrict__ scaling_factors, int n_batch, - float* __restrict__ result); - -// Same as the above 8, 8, 8 integer matmul except for the presence of zero -// point and non-accumulative. -// TODO(b/148688698): remove this function by folding zero point calculation in -// prepare() function. -void MatrixBatchVectorMultiply(const int8_t* input, int32_t input_zeropoint, - const int8_t* input_to_gate_weights, - int32_t input_to_gate_effective_scale_a, - int32_t input_to_gate_effective_scale_b, - int32_t n_batch, int32_t n_input, int32_t n_cell, - int8_t* gate_output, int8_t gate_output_zp); - -// Same as above but has 16 bit and 8 bit input and 8 bit output. -// Used in projection when hidden is 16bit. -void MatrixBatchVectorMultiply(const int16_t* hidden, - const int8_t* hidden_to_output_weights, - int32_t proj_effective_scale_a, - int32_t proj_effective_scale_b, - const int32_t* gate_bias, int32_t n_batch, - int32_t n_hidden, int32_t n_output, - int32_t output_zp, int8_t* proj_output); - -// Apply Layer Normalization (https://arxiv.org/abs/1607.06450) to a Quantized -// vector. -// Parameters: -// - input: batch vector of size n_batch * n_input; 16 bit. -// - layer_norm_weights: the quantized layer normalization weights. -// - bias: the bias for the layer normalization. -// - layer_norm_scale_a: multiplier for scale factor. -// - layer_norm_scale_b: shift for scale factor. -// - variance_limit: the guard to make sure the inverse does not overflow. -// - n_batch: the number of batches. -// - n_input: the size for input and output. -// - output: the 16 bit output -void ApplyLayerNorm(const int16_t* input, const int16_t* layer_norm_weights, - const int32_t* bias, int32_t layer_norm_scale_a, - int32_t layer_norm_scale_b, int32_t variance_limit, - int n_batch, int n_input, int16_t* output); - -// Same as above but the internal calculation is done in float. -void ApplyLayerNormFloat(const int16_t* input, - const int16_t* layer_norm_weights, - int32_t layer_norm_scale_a, int32_t layer_norm_scale_b, - const int32_t* bias, int n_batch, int n_input, - int16_t* output); - -// Apply Sigmoid to a quantized vector. -// Parameters: -// - input: batch vector of size n_batch * n_input; 16 bit. -// - n_batch: the number of batches. -// - n_input: the size for input and output. -// - output: the 16 bit output -// The input is in Q3.12 format and the output is in Q0.15 format. -void ApplySigmoid(const int16_t* input, int32_t n_batch, int32_t n_input, - int16_t* output); - -// Same as above but the internal calcualtion is float. -void ApplySigmoidFloat(const int16_t* input, int32_t n_batch, int32_t n_input, - int16_t* output); - -// Apply Tanh to a quantized vector. -// Parameters: -// - integer_bits: the integer bits of the input. -// Currently supports 0, 1, 2, 3, 4, 5, 6. -// - input: batch vector of size n_batch * n_input; 16 bit. -// - n_batch: the number of batches. -// - n_input: the size for input and output. -// - output: the 16 bit output -// The input is in Qm.15-m format and the output is in Q0.15 format. -void ApplyTanh(int32_t intger_bits, const int16_t* input, int32_t n_batch, - int32_t n_input, int16_t* output); - -// Apply Tanh to a quantized vector. Tbe internal calculation is in float. -// - Input has 2^(integer_bits) as scale. -// - Output has Q0.15 as scale. -void ApplyTanhFloat(const int16_t* input, int32_t n_batch, int32_t n_input, - int32_t integer_bits, int16_t* output); - -// Element-wise multiplication of two quantized vectors. -// Parameters: -// - input_1: batch vector of size n_batch * n_input; 16 bit. -// - input_2: batch vector of size n_batch * n_input; 16 bit. -// - n_batch: the number of batches. -// - n_input: the size for input and output. -// - shift: the shift needed to produce the output. -// - output: the 16 bit output of size n_batch * n_input. -// Output does not need to be initialized. -void CwiseMul(const int16_t* input_1, const int16_t* input_2, int n_batch, - int n_input, int shift, int16_t* output); - -// Element-wise multiplication of two quantized vectors. -// Parameters: -// - input_1: batch vector of size n_batch * n_input; 16 bit. -// - input_2: batch vector of size n_batch * n_input; 16 bit. -// - n_batch: the number of batches. -// - n_input: the size for input and output. -// - shift: the shift needed to produce the output. -// - output: the 8 bit output of size n_batch * n_input. -// Output does not need to be initialized. -void CwiseMul(const int16_t* input_1, const int16_t* input_2, int n_batch, - int n_input, int shift, int8_t* output); - -// Element-wise multiplication of two quantized vectors with rescaling. -// Parameters: -// - input_1: batch vector of size n_batch * n_input; 16 bit. -// - input_2: batch vector of size n_batch * n_input; 16 bit. -// - multiplier: the multiplier part of scale. -// - shift: the shift part of scale. -// - n_batch: the number of batches. -// - n_input: the size for input and output. -// - output: the 8 bit output of size n_batch * n_input. -// - output_zp: the zero point of output. -// Output does not need to be initialized. -// Multiplier ("m") and shift ("s") are connected to scale ("s") with s = m * -// 2^(s - 31). -void CwiseMul(const int16_t* input_1, const int16_t* input_2, - int32_t multiplier, int32_t shift, int32_t n_batch, - int32_t n_input, int32_t output_zp, int8_t* output); - -// Element-wise saturating addition of two quantized vectors without rescaling. -// Parameters: -// - input_1: batch vector of size n_batch * n_input; 16 bit. -// - input_2: batch vector of size n_batch * n_input; 16 bit. -// - n_batch: the number of batches. -// - n_input: the size for input and output. -// - output: the 8 bit output of size n_batch * n_input. -// Output does not need to be initialized. -void CwiseAdd(const int16_t* input_1, const int16_t* input_2, int n_batch, - int n_input, int16_t* output); - -// Element-wise in-place clipping of a vector. Overloaded for float, int16_t, -// int8_t. Parameters: -// - vector: vector of size v_size. -// - v_size: the size of the vector. -// - clipping_value: the value used for clipping. -void CwiseClipping(float* vector, const int v_size, const float clipping_value); -void CwiseClipping(int16_t* vector, const int v_size, - const int16_t clipping_value); -void CwiseClipping(int8_t* vector, const int v_size, - const int8_t clipping_value); - -// Dot product of two vectors. -float VectorVectorDotProduct(const float* vector1, const float* vector2, - int v_size); - -// Dot product of two batch vectors of size n_batch * v_size: -// vector1 = [x_1_1, x_1_2, ..., x_1_vsize, -// x_2_1, x_2_2, ..., x_2_vsize, -// ... -// x_nbatch_1,..., x_nbatch_vsize] -// vector2 = [y_1_1, y_1_2, ..., y_1_vsize, -// y_2_1, y_2_2, ..., y_2_vsize, -// ... -// y_nbatch_1,..., y_nbatch_vsize] -// Then result will be a vector of n_batch size starting from 'result': -// [x_1_1 * y_1_1 + x_1_2 * y_1_2 + ... + x_1_vsize * y_1_vsize, -// x_2_1 * y_2_1 + x_2_2 * y_2_2 + ... + x_2_vsize * y_2_vsize, -// ... -// x_nbatch_1 * y_nbatch_1 + ... + x_nbatch_vsize * y_nbatch_vsize] -template -inline void BatchVectorBatchVectorDotProduct(const T* vector1, const T* vector2, - int v_size, int n_batch, - T* result) { - for (int b = 0; b < n_batch; b++) { - result[b] = VectorVectorDotProduct(vector1, vector2, v_size); - vector1 += v_size; - vector2 += v_size; - } -} - -// Same as above but input is 16bit and output is 32bit. -void BatchVectorBatchVectorDotProduct(const int16_t* vector1, - const int16_t* vector2, int v_size, - int n_batch, int32_t* result); - -// Same as above, but inputs are 16bit integer and output is 16bit integer. -void VectorBatchVectorCwiseProductAccumulate(const int16_t* vector, int v_size, - const int16_t* batch_vector, - int n_batch, int32_t multiplier, - int shift, int16_t* result); - -// Compute "1.0f - elements of vector" (used in CIFG). -void Sub1Vector(const float* vector, int v_size, float* result); - -// Compute "1.0f - elements of vector" (used in CIFG) for int16 input. -// "vector" has range [0, 32767] because it is the output of sigmoid function. -void Sub1Vector(const int16_t* vector, int v_size, int16_t* result); - -// Multiply all elements of vector with a scalar. -void VectorScalarMultiply(const int8_t* vector, int v_size, float scale, - float* result); - -// Reduce-sum on a float input vector: -// input_vector: float pointer to input vector. -// output_vector: float pointer to vector. -// output_size: output vector size. -// reduction_size: number of consecutive elements from input vector which are -// added to get one element of output. -void ReductionSumVector(const float* input_vector, float* output_vector, - int output_size, int reduction_size); - -// Same as above but input/output is 32 bit integer. -void ReductionSumVector(const int32_t* input_vector, int32_t* output_vector, - int output_size, int reduction_size); - -// Same as above but input is 8 bit integer. -void ReductionSumVector(const int8_t* input_vector, int32_t* output_vector, - int output_size, int reduction_size); - -// Layer norm for each batch. -void MeanStddevNormalization(const float* input_vector, float* output_vector, - int v_size, int n_batch); - -// Saturate Add with rescale on both inputs. -void TwoGateSaturatingAdd(const int8_t* input, int8_t input_zp, - const int8_t* recurrent, int8_t recurrent_zp, - int32_t input_effective_scale_a, - int32_t input_effective_scale_b, - int32_t recurrent_effective_scale_a, - int32_t recurrent_effective_scale_b, int32_t n_batch, - int32_t n_cell, int16_t* output); - -} // namespace tensor_utils - -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_PORTABLE_TENSOR_UTILS_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/quantization_util.cc b/code/components/tflite-lib/tensorflow/lite/kernels/internal/quantization_util.cc deleted file mode 100644 index 62045d67..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/quantization_util.cc +++ /dev/null @@ -1,416 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/kernels/internal/quantization_util.h" - -#include -#include -#include - -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/internal/cppmath.h" - -namespace tflite { - -namespace { -// These constants are used to manipulate the binary representation of doubles. -// Double-precision binary64 floating point format is: -// Bit | 63 | 62-52 | 51-0 | -// | Sign | Exponent | Fraction | -// To avoid 64-bit integers as much as possible, I break this into high and -// low 32-bit chunks. High is: -// Bit | 31 | 30-20 | 19-0 | -// | Sign | Exponent | High Fraction | -// Low is: -// Bit | 31-0 | -// | Low Fraction | -// We then access the components through logical bit-wise operations to -// extract the parts needed, with the positions and masks derived from the -// layout shown above. -constexpr uint64_t kSignMask = 0x8000000000000000LL; -constexpr uint64_t kExponentMask = 0x7ff0000000000000LL; -constexpr int32_t kExponentShift = 52; -constexpr int32_t kExponentBias = 1023; -constexpr uint32_t kExponentIsBadNum = 0x7ff; -constexpr uint64_t kFractionMask = 0x000fffffffc00000LL; -constexpr uint32_t kFractionShift = 22; -constexpr uint32_t kFractionRoundingMask = 0x003fffff; -constexpr uint32_t kFractionRoundingThreshold = 0x00200000; -} // namespace - -void QuantizeMultiplier(double double_multiplier, int32_t* quantized_multiplier, - int* shift) { -#if TFLITE_SINGLE_ROUNDING - // Single-rounding MultiplyByQuantizedMultiplier only supports positive - // multipliers. - // TFLITE_DCHECK(double_multiplier >= 0); -#endif - if (double_multiplier == 0.) { - *quantized_multiplier = 0; - *shift = 0; - return; - } -#ifdef TFLITE_EMULATE_FLOAT - // If we're trying to avoid the use of floating-point instructions (for - // example on microcontrollers) then use an alternative implementation - // that only requires integer and bitwise operations. To enable this, you - // need to set the define during the build process for your platform. - int64_t q_fixed = IntegerFrExp(double_multiplier, shift); -#else // TFLITE_EMULATE_FLOAT - const double q = std::frexp(double_multiplier, shift); - auto q_fixed = static_cast(TfLiteRound(q * (1LL << 31))); -#endif // TFLITE_EMULATE_FLOAT - TFLITE_CHECK(q_fixed <= (1LL << 31)); - if (q_fixed == (1LL << 31)) { - q_fixed /= 2; - ++*shift; - } - TFLITE_CHECK_LE(q_fixed, std::numeric_limits::max()); - // A shift amount smaller than -31 would cause all bits to be shifted out - // and thus all results would be zero. We implement that instead with - // q_fixed==0, so as to avoid hitting issues with right-shift - // operations with shift amounts greater than 31. Note that this happens - // roughly when abs(double_multiplier) < 2^-31 and the present handling means - // that we're effectively flushing tiny double_multiplier's to zero. - // We could conceivably handle values in the range (roughly) [32, 63] - // as 'denormals' i.e. (shift==0, q_fixed < 2^30). In that point of view - // the present handling is just doing 'flush denormals to zero'. We could - // reconsider and actually generate nonzero denormals if a need arises. - if (*shift < -31) { - *shift = 0; - q_fixed = 0; - } -#if TFLITE_SINGLE_ROUNDING - // Single-rounding MultiplyByQuantizedMultiplier doesn't support a shift > 30, - // saturate it. - if (*shift > 30) { - *shift = 30; - q_fixed = (1LL << 31) - 1; - } -#endif - *quantized_multiplier = static_cast(q_fixed); -} - -void QuantizeMultiplierGreaterThanOne(double double_multiplier, - int32_t* quantized_multiplier, - int* left_shift) { - TFLITE_CHECK_GT(double_multiplier, 1.); - QuantizeMultiplier(double_multiplier, quantized_multiplier, left_shift); - TFLITE_CHECK_GE(*left_shift, 0); -} - -void QuantizeMultiplierSmallerThanOneExp(double double_multiplier, - int32_t* quantized_multiplier, - int* left_shift) { - TFLITE_CHECK_LT(double_multiplier, 1.); - TFLITE_CHECK_GT(double_multiplier, 0.); - int shift; - QuantizeMultiplier(double_multiplier, quantized_multiplier, &shift); - TFLITE_CHECK_LE(shift, 0); - *left_shift = shift; -} - -int64_t IntegerFrExp(double input, int* shift) { - // Make sure our assumptions about the double layout hold. - TFLITE_CHECK_EQ(8, sizeof(double)); - - // We want to access the bits of the input double value directly, which is - // tricky to do safely, so use a union to handle the casting. - union { - double double_value; - uint64_t double_as_uint; - } cast_union; - cast_union.double_value = input; - const uint64_t u = cast_union.double_as_uint; - - // If the bitfield is all zeros apart from the sign bit, this is a normalized - // zero value, so return standard values for this special case. - if ((u & ~kSignMask) == 0) { - *shift = 0; - return 0; - } - - // Deal with NaNs and Infs, which are always indicated with a fixed pattern in - // the exponent, and distinguished by whether the fractions are zero or - // non-zero. - const uint32_t exponent_part = ((u & kExponentMask) >> kExponentShift); - if (exponent_part == kExponentIsBadNum) { - *shift = std::numeric_limits::max(); - if (u & kFractionMask) { - // NaN, so just return zero (with the exponent set to INT_MAX). - return 0; - } else { - // Infinity, so return +/- INT_MAX. - if (u & kSignMask) { - return std::numeric_limits::min(); - } else { - return std::numeric_limits::max(); - } - } - } - - // The shift is fairly easy to extract from the high bits of the double value, - // just by masking it out and applying a bias. The std::frexp() implementation - // always returns values between 0.5 and 1.0 though, whereas the exponent - // assumes 1.0 to 2.0 is the standard range, so I add on one to match that - // interface. - *shift = (exponent_part - kExponentBias) + 1; - - // There's an implicit high bit in the double format definition, so make sure - // we include that at the top, and then reconstruct the rest of the fractional - // value from the remaining fragments. - int64_t fraction = 0x40000000 + ((u & kFractionMask) >> kFractionShift); - - // We're cutting off some bits at the bottom, so to exactly match the standard - // frexp implementation here we'll apply rounding by adding one to the least - // significant bit of the result if the discarded portion is over half of the - // maximum. - if ((u & kFractionRoundingMask) > kFractionRoundingThreshold) { - fraction += 1; - } - // Negate the fraction if the sign bit was set. - if (u & kSignMask) { - fraction *= -1; - } - - return fraction; -} - -double DoubleFromFractionAndShift(int64_t fraction, int shift) { - union { - double double_value; - uint64_t double_as_uint; - } result; - - // Detect NaNs and infinities. - if (shift == std::numeric_limits::max()) { - if (fraction == 0) { - return std::numeric_limits::quiet_NaN(); - } else if (fraction > 0) { - return std::numeric_limits::infinity(); - } else { - return -std::numeric_limits::infinity(); - } - } - - // Return a normalized zero for a zero fraction. - if (fraction == 0) { - result.double_as_uint = 0; - return result.double_value; - } - - bool is_negative = (fraction < 0); - int64_t encoded_fraction = is_negative ? -fraction : fraction; - int64_t encoded_shift = (shift - 1); - while (encoded_fraction < 0x40000000) { - encoded_fraction *= 2; - encoded_shift -= 1; - } - while (encoded_fraction > 0x80000000) { - encoded_fraction /= 2; - encoded_shift += 1; - } - encoded_fraction -= 0x40000000; - if (encoded_shift < -1022) { - encoded_shift = -1023; - } else if (encoded_shift > 1022) { - encoded_shift = 1023; - } - encoded_shift += kExponentBias; - uint64_t encoded_sign = is_negative ? kSignMask : 0; - result.double_as_uint = encoded_sign | (encoded_shift << kExponentShift) | - (encoded_fraction << kFractionShift); - return result.double_value; -} - -double IntegerDoubleMultiply(double a, double b) { - int a_shift; - const int64_t a_fraction = IntegerFrExp(a, &a_shift); - int b_shift; - const int64_t b_fraction = IntegerFrExp(b, &b_shift); - // Detect NaNs and infinities. - if (a_shift == std::numeric_limits::max() || - (b_shift == std::numeric_limits::max())) { - return std::numeric_limits::quiet_NaN(); - } - const int result_shift = a_shift + b_shift + 1; - const int64_t result_fraction = (a_fraction * b_fraction) >> 32; - return DoubleFromFractionAndShift(result_fraction, result_shift); -} - -int IntegerDoubleCompare(double a, double b) { - int a_shift; - const int64_t a_fraction = IntegerFrExp(a, &a_shift); - int b_shift; - const int64_t b_fraction = IntegerFrExp(b, &b_shift); - - // Detect NaNs and infinities. - if (a_shift == std::numeric_limits::max() || - (b_shift == std::numeric_limits::max())) { - return 1; - } - - if ((a_fraction == 0) && (b_fraction < 0)) { - return 1; - } else if ((a_fraction < 0) && (b_fraction == 0)) { - return -1; - } else if (a_shift < b_shift) { - return -1; - } else if (a_shift > b_shift) { - return 1; - } else if (a_fraction < b_fraction) { - return -1; - } else if (a_fraction > b_fraction) { - return 1; - } else { - return 0; - } -} - -void PreprocessSoftmaxScaling(double beta, double input_scale, - int input_integer_bits, - int32_t* quantized_multiplier, int* left_shift) { - // If the overall multiplier (input and beta) is large, then exp() of an - // input difference of 1 scaled by this will be large. In other words, we - // can cap the multiplier and know that, when it is used, the output will be - // (round to) zero wherever the input is not at the maximum value. - - // If the overall scale is less than one, and input_integer_bits=0, then the - // result is double equivalent of Q0.31 (actually with more precision). Thus - // this generates a Q(input_integer_bits).(31-input_integer_bits) - // representation. -#if TFLITE_SINGLE_ROUNDING - const double max_real_multiplier = (1LL << 30) - 1.0; -#else - const double max_real_multiplier = (1LL << 31) - 1.0; -#endif - -#ifdef TFLITE_EMULATE_FLOAT - const double input_beta = IntegerDoubleMultiply(beta, input_scale); - int shift; - int64_t fraction = IntegerFrExp(input_beta, &shift); - shift += (31 - input_integer_bits); - double input_beta_real_multiplier = - DoubleFromFractionAndShift(fraction, shift); - if (IntegerDoubleCompare(input_beta_real_multiplier, max_real_multiplier) > - 0) { - input_beta_real_multiplier = max_real_multiplier; - } -#else // TFLITE_EMULATE_FLOAT - const double input_beta_real_multiplier = - std::min(beta * input_scale * (1 << (31 - input_integer_bits)), - max_real_multiplier); -#endif // TFLITE_EMULATE_FLOAT - - QuantizeMultiplierGreaterThanOne(input_beta_real_multiplier, - quantized_multiplier, left_shift); -} - -void PreprocessLogSoftmaxScalingExp(double beta, double input_scale, - int input_integer_bits, - int32_t* quantized_multiplier, - int* left_shift, - int32_t* reverse_scaling_divisor, - int* reverse_scaling_left_shift) { - PreprocessSoftmaxScaling(beta, input_scale, input_integer_bits, - quantized_multiplier, left_shift); - - // Also calculate what amounts to the inverse scaling factor for the input. - const double real_reverse_scaling_divisor = - (1 << (31 - *left_shift)) / static_cast(*quantized_multiplier); - tflite::QuantizeMultiplierSmallerThanOneExp(real_reverse_scaling_divisor, - reverse_scaling_divisor, - reverse_scaling_left_shift); -} - -int CalculateInputRadius(int input_integer_bits, int input_left_shift, - int total_signed_bits) { -#ifdef TFLITE_EMULATE_FLOAT - int64_t result = (1 << input_integer_bits) - 1; - result <<= (total_signed_bits - input_integer_bits); - result >>= input_left_shift; - return result; -#else // TFLITE_EMULATE_FLOAT - const double max_input_rescaled = - 1.0 * ((1 << input_integer_bits) - 1) * - (1LL << (total_signed_bits - input_integer_bits)) / - (1LL << input_left_shift); - // Tighten bound using floor. Suppose that we could use the exact value. - // After scaling the difference, the result would be at the maximum. Thus we - // must ensure that our value has lower magnitude. - return static_cast(std::floor(max_input_rescaled)); -#endif // TFLITE_EMULATE_FLOAT -} - -void NudgeQuantizationRange(const float min, const float max, - const int quant_min, const int quant_max, - float* nudged_min, float* nudged_max, - float* nudged_scale) { - // This code originates from tensorflow/core/kernels/fake_quant_ops_functor.h. - const float quant_min_float = static_cast(quant_min); - const float quant_max_float = static_cast(quant_max); - *nudged_scale = (max - min) / (quant_max_float - quant_min_float); - const float zero_point_from_min = quant_min_float - min / *nudged_scale; - uint16_t nudged_zero_point; - if (zero_point_from_min < quant_min_float) { - nudged_zero_point = static_cast(quant_min); - } else if (zero_point_from_min > quant_max_float) { - nudged_zero_point = static_cast(quant_max); - } else { - nudged_zero_point = static_cast(TfLiteRound(zero_point_from_min)); - } - *nudged_min = (quant_min_float - nudged_zero_point) * (*nudged_scale); - *nudged_max = (quant_max_float - nudged_zero_point) * (*nudged_scale); -} - -void FakeQuantizeArray(const float nudged_scale, const float nudged_min, - const float nudged_max, const float* input_data, - float* output_data, const float size) { - // This code originates from tensorflow/core/kernels/fake_quant_ops_functor.h. - const float inv_nudged_scale = 1.0f / nudged_scale; - - for (int i = 0; i < size; i++) { - const float src_val = input_data[i]; - const float clamped = std::min(nudged_max, std::max(nudged_min, src_val)); - const float clamped_shifted = clamped - nudged_min; - const float dst_val = - TfLiteRound(clamped_shifted * inv_nudged_scale) * nudged_scale + - nudged_min; - output_data[i] = dst_val; - } -} - -bool CheckedLog2(const float x, int* log2_result) { - // Using TfLiteRound instead of std::round and std::log instead of - // std::log2 to work around these functions being missing in a toolchain - // used in some TensorFlow tests as of May 2018. - const float x_log2 = std::log(x) * (1.0f / std::log(2.0f)); - const float x_log2_rounded = TfLiteRound(x_log2); - const float x_log2_fracpart = x_log2 - x_log2_rounded; - - *log2_result = static_cast(x_log2_rounded); - return std::abs(x_log2_fracpart) < 1e-3f; -} - -void QuantizeMultiplierArray(const double* effective_scales, size_t size, - int32_t* effective_scale_significand, - int* effective_shift) { - for (size_t i = 0; i < size; ++i) { - QuantizeMultiplier(effective_scales[i], &effective_scale_significand[i], - &effective_shift[i]); - } -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/quantization_util.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/quantization_util.h deleted file mode 100644 index 0ee914b0..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/quantization_util.h +++ /dev/null @@ -1,292 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_QUANTIZATION_UTIL_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_QUANTIZATION_UTIL_H_ - -#include -#include -#include - -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/internal/cppmath.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -// Given the min and max values of a float array, return -// reasonable quantization parameters to use for this array. -template -QuantizationParams ChooseQuantizationParams(double rmin, double rmax, - bool narrow_range) { - const T qmin = std::numeric_limits::min() + (narrow_range ? 1 : 0); - const T qmax = std::numeric_limits::max(); - const double qmin_double = qmin; - const double qmax_double = qmax; - // 0 should always be a representable value. Let's assume that the initial - // min,max range contains 0. - TFLITE_CHECK_LE(rmin, 0.); - TFLITE_CHECK_GE(rmax, 0.); - if (rmin == rmax) { - // Special case where the min,max range is a point. Should be {0}. - TFLITE_CHECK_EQ(rmin, 0.); - TFLITE_CHECK_EQ(rmax, 0.); - QuantizationParams quantization_params; - quantization_params.zero_point = 0; - quantization_params.scale = 0.; - return quantization_params; - } - - // General case. - // - // First determine the scale. - const double scale = (rmax - rmin) / (qmax_double - qmin_double); - - // Zero-point computation. - // First the initial floating-point computation. The zero-point can be - // determined from solving an affine equation for any known pair - // (real value, corresponding quantized value). - // We know two such pairs: (rmin, qmin) and (rmax, qmax). - // The arithmetic error on the zero point computed from either pair - // will be roughly machine_epsilon * (sum of absolute values of terms) - // so we want to use the variant that adds the smaller terms. - const double zero_point_from_min = qmin_double - rmin / scale; - const double zero_point_from_max = qmax_double - rmax / scale; - const double zero_point_from_min_error = - std::abs(qmin_double) + std::abs(rmin / scale); - const double zero_point_from_max_error = - std::abs(qmax_double) + std::abs(rmax / scale); - - const double zero_point_double = - zero_point_from_min_error < zero_point_from_max_error - ? zero_point_from_min - : zero_point_from_max; - - // Now we need to nudge the zero point to be an integer - // (our zero points are integer, and this is motivated by the requirement - // to be able to represent the real value "0" exactly as a quantized value, - // which is required in multiple places, for example in Im2col with SAME - // padding). - T nudged_zero_point = 0; - if (zero_point_double < qmin_double) { - nudged_zero_point = qmin; - } else if (zero_point_double > qmax_double) { - nudged_zero_point = qmax; - } else { - nudged_zero_point = static_cast(round(zero_point_double)); - } - // The zero point should always be in the range of quantized value, - // [qmin, qmax]. - TFLITE_CHECK_GE(nudged_zero_point, qmin); - TFLITE_CHECK_LE(nudged_zero_point, qmax); - - // Finally, store the result nudged quantization params. - QuantizationParams quantization_params; - quantization_params.zero_point = nudged_zero_point; - quantization_params.scale = scale; - return quantization_params; -} - -template -QuantizationParams ChooseQuantizationParams(double rmin, double rmax) { - return ChooseQuantizationParams(rmin, rmax, false); -} - -// Converts a floating-point number to an integer. For all inputs x where -// static_cast(x) is legal according to the C++ standard, the result -// is identical to that cast (i.e. the result is x with its fractional part -// truncated whenever that is representable as IntOut). -// -// static_cast would cause undefined behavior for the following cases, which -// have well-defined behavior for this function: -// -// 1. If x is NaN, the result is zero. -// -// 2. If the truncated form of x is above the representable range of IntOut, -// the result is std::numeric_limits::max(). -// -// 3. If the truncated form of x is below the representable range of IntOut, -// the result is std::numeric_limits::min(). -// -// Note that cases #2 and #3 cover infinities as well as finite numbers. -// -// The range of FloatIn must include the range of IntOut, otherwise -// the results are undefined. -// TODO(sfeuz): Replace by absl::SafeCast once available. -template -IntOut SafeCast(FloatIn x) { - static_assert(!std::numeric_limits::is_integer, - "FloatIn is integer"); - static_assert(std::numeric_limits::is_integer, - "IntOut is not integer"); - static_assert(std::numeric_limits::radix == 2, "IntOut is base 2"); - - // Special case NaN, for which the logic below doesn't work. - if (std::isnan(x)) { - return 0; - } - - // Negative values all clip to zero for unsigned results. - if (!std::numeric_limits::is_signed && x < 0) { - return 0; - } - - // Handle infinities. - if (std::isinf(x)) { - return x < 0 ? std::numeric_limits::min() - : std::numeric_limits::max(); - } - - // Set exp such that x == f * 2^exp for some f with |f| in [0.5, 1.0), - // unless x is zero in which case exp == 0. Note that this implies that the - // magnitude of x is strictly less than 2^exp. - int exp = 0; - std::frexp(x, &exp); - - // Let N be the number of non-sign bits in the representation of IntOut. If - // the magnitude of x is strictly less than 2^N, the truncated version of x - // is representable as IntOut. The only representable integer for which this - // is not the case is kMin for signed types (i.e. -2^N), but that is covered - // by the fall-through below. - if (exp <= std::numeric_limits::digits) { - return x; - } - - // Handle numbers with magnitude >= 2^N. - return x < 0 ? std::numeric_limits::min() - : std::numeric_limits::max(); -} - -// Decompose a double multiplier into a Q0.31 int32 representation of its -// significand, and shift representation of NEGATIVE its exponent --- -// this is intended as a RIGHT-shift. -// -// Restricted to the case where the multiplier < 1 (and non-negative). -void QuantizeMultiplierSmallerThanOneExp(double double_multiplier, - int32_t* quantized_multiplier, - int* left_shift); - -// Decompose a double multiplier into a Q0.31 int32 representation of its -// significand, and shift representation of its exponent. -// -// Restricted to the case where the multiplier > 1. -void QuantizeMultiplierGreaterThanOne(double double_multiplier, - int32_t* quantized_multiplier, - int* left_shift); - -// Decompose a double multiplier into a Q0.31 int32 representation of its -// significand, and shift representation of its exponent. -// -// Handles an arbitrary positive multiplier. The 'shift' output-value is -// basically the 'floating-point exponent' of the multiplier: -// Negative for a right-shift (when the multiplier is <1), positive for a -// left-shift (when the multiplier is >1) -void QuantizeMultiplier(double double_multiplier, int32_t* quantized_multiplier, - int* shift); - -// Splits a double input value into a returned fraction, and a shift value from -// the exponent, using only bitwise and integer operations to support -// microcontrollers and other environments without floating-point support. -// -// This is designed to be a replacement for how std::frexp() is used within the -// QuantizeMultiplier() function, and so has a different signature than the -// standard version, returning a 64-bit integer rather than a double. This -// result has a maximum value of 1<<31, with the fraction expressed as a -// proportion of that maximum. -// -// std::frexp() returns NaNs and infinities unmodified, but since we're -// returning integers that can't represent those values, instead we return -// a shift of std::numeric_limits::max() for all bad numbers, with an int64 -// result of 0 for NaNs, std:numeric_limits::max() for +INFINITY, and -// std::numeric_limits::min() for -INFINITY. Denormalized inputs will -// result in return values that end up truncating some bits at the end, -// reflecting the loss of precision inherent in denormalization. -int64_t IntegerFrExp(double input, int* shift); - -// Converts an integer fraction in the format produced by IntegerFrExp (where -// 0x40000000 is 1.0) and an exponent shift (between -1022 and +1022) into an -// IEEE binary64 double format result. The implementation uses only integer and -// bitwise operators, so no floating point hardware support or emulation is -// needed. This is here so quantized operations can run non-time-critical -// preparation calculations on microcontrollers and other platforms without -// float support. -double DoubleFromFractionAndShift(int64_t fraction, int shift); - -// Performs a multiplication of two numbers in double format, using only integer -// and bitwise instructions. This is aimed at supporting housekeeping functions -// for quantized operations on microcontrollers without floating-point hardware. -double IntegerDoubleMultiply(double a, double b); - -// Returns -1 if a is less than b, 0 if a and b are equal, and +1 if a is -// greater than b. It is implemented using only integer and logical instructions -// so that it can be easily run on microcontrollers for quantized operations. -int IntegerDoubleCompare(double a, double b); - -// This first creates a multiplier in a double equivalent of -// Q(input_integer_bits).(31-input_integer_bits) representation, with extra -// precision in the double's fractional bits. It then splits the result into -// significand and exponent. -void PreprocessSoftmaxScaling(double beta, double input_scale, - int input_integer_bits, - int32_t* quantized_multiplier, int* left_shift); -// Like PreprocessSoftmaxScaling, but inverse scaling factors also calculated. -void PreprocessLogSoftmaxScalingExp(double beta, double input_scale, - int input_integer_bits, - int32_t* quantized_multiplier, - int* left_shift, - int32_t* reverse_scaling_divisor, - int* reverse_scaling_left_shift); -// Calculate the largest input that will result in a within-bounds intermediate -// result within MultiplyByQuantizedMultiplierGreaterThanOne. In other words, -// it must not overflow before we reduce the value by multiplication by the -// input multiplier. The negative radius is used as the minimum difference in -// Softmax. -int CalculateInputRadius(int input_integer_bits, int input_left_shift, - int total_signed_bits = 31); - -// Nudges a min/max quantization range to ensure zero is zero. -// Gymnastics with nudged zero point is to ensure that real zero maps to -// an integer, which is required for e.g. zero-padding in convolutional layers. -// Outputs nudged_min, nudged_max, nudged_scale. -void NudgeQuantizationRange(const float min, const float max, - const int quant_min, const int quant_max, - float* nudged_min, float* nudged_max, - float* nudged_scale); - -// Fake quantizes (quantizes and dequantizes) input_data using the scale, -// nudged_min, and nudged_max from NudgeQuantizationRange. This matches the code -// in TensorFlow's FakeQuantizeWithMinMaxVarsFunctor. -void FakeQuantizeArray(const float nudged_scale, const float nudged_min, - const float nudged_max, const float* input_data, - float* output_data, const float size); - -// If x is approximately a power of two (with any positive or negative -// exponent), stores that exponent (i.e. log2(x)) in *log2_result, otherwise -// returns false. -bool CheckedLog2(const float x, int* log2_result); - -// Decomposes an array of double multipliers into a Q0.31 int32 representation -// of its significand, and shift representation of its exponent. -// -// Handles an arbitrary multiplier. The 'shift' output-value is -// basically the 'floating-point exponent' of the multiplier: -// Negative for a right-shift (when the multiplier is <1), positive for a -// left-shift (when the multiplier is >1) -void QuantizeMultiplierArray(const double* effective_scales, size_t size, - int32_t* effective_scale_significand, - int* effective_shift); - -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_QUANTIZATION_UTIL_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/add.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/add.h deleted file mode 100644 index 1f521316..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/add.h +++ /dev/null @@ -1,400 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_ADD_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_ADD_H_ - -#include -#include - -#include "fixedpoint/fixedpoint.h" -#include "tensorflow/lite/kernels/internal/common.h" - -namespace tflite { - -namespace reference_ops { - -template -inline void Add(const ArithmeticParams& params, - const RuntimeShape& input1_shape, const T* input1_data, - const RuntimeShape& input2_shape, const T* input2_data, - const RuntimeShape& output_shape, T* output_data) { - T activation_min, activation_max; - GetActivationParams(params, &activation_min, &activation_max); - - const int flat_size = - MatchingElementsSize(input1_shape, input2_shape, output_shape); - for (int i = 0; i < flat_size; ++i) { - output_data[i] = ActivationFunctionWithMinMax( - input1_data[i] + input2_data[i], activation_min, activation_max); - } -} - -// Element-wise add that can often be used for inner loop of broadcast add as -// well as the non-broadcast add. - -// This function is used for 8-bit as well as for 16-bit, but the accumulator -// is 32-bit for both cases. The overflow does not happen due to the -// choice of the shift (20 or 15, accordingly - see add.cc for more comments). -template -inline void AddElementwise(int size, const ArithmeticParams& params, - const T* input1_data, const T* input2_data, - T* output_data) { - TFLITE_DCHECK_GT(params.input1_offset, -std::numeric_limits::max()); - TFLITE_DCHECK_GT(params.input2_offset, -std::numeric_limits::max()); - TFLITE_DCHECK_LT(params.input1_offset, std::numeric_limits::max()); - TFLITE_DCHECK_LT(params.input2_offset, std::numeric_limits::max()); - - for (int i = 0; i < size; ++i) { - const int32_t input1_val = params.input1_offset + input1_data[i]; - const int32_t input2_val = params.input2_offset + input2_data[i]; - const int32_t shifted_input1_val = input1_val * (1 << params.left_shift); - const int32_t shifted_input2_val = input2_val * (1 << params.left_shift); - const int32_t scaled_input1_val = - MultiplyByQuantizedMultiplierSmallerThanOneExp( - shifted_input1_val, params.input1_multiplier, params.input1_shift); - const int32_t scaled_input2_val = - MultiplyByQuantizedMultiplierSmallerThanOneExp( - shifted_input2_val, params.input2_multiplier, params.input2_shift); - const int32_t raw_sum = scaled_input1_val + scaled_input2_val; - const int32_t raw_output = - MultiplyByQuantizedMultiplierSmallerThanOneExp( - raw_sum, params.output_multiplier, params.output_shift) + - params.output_offset; - const int32_t clamped_output = - std::min(params.quantized_activation_max, - std::max(params.quantized_activation_min, raw_output)); - output_data[i] = static_cast(clamped_output); - } -} - -// Scalar-broadcast add that can be used for inner loop of more general -// broadcast add, so that, for example, scalar-broadcast with batch will still -// be fast. -inline void AddScalarBroadcast(int size, const ArithmeticParams& params, - uint8_t input1_data, const uint8_t* input2_data, - uint8_t* output_data) { - TFLITE_DCHECK_GT(params.input1_offset, -256); - TFLITE_DCHECK_GT(params.input2_offset, -256); - TFLITE_DCHECK_LT(params.input1_offset, 256); - TFLITE_DCHECK_LT(params.input2_offset, 256); - - const int32_t input1_val = params.input1_offset + input1_data; - const int32_t shifted_input1_val = input1_val * (1 << params.left_shift); - const int32_t scaled_input1_val = - MultiplyByQuantizedMultiplierSmallerThanOneExp( - shifted_input1_val, params.input1_multiplier, params.input1_shift); - for (int i = 0; i < size; ++i) { - const int32_t input2_val = params.input2_offset + input2_data[i]; - const int32_t shifted_input2_val = input2_val * (1 << params.left_shift); - const int32_t scaled_input2_val = - MultiplyByQuantizedMultiplierSmallerThanOneExp( - shifted_input2_val, params.input2_multiplier, params.input2_shift); - const int32_t raw_sum = scaled_input1_val + scaled_input2_val; - const int32_t raw_output = - MultiplyByQuantizedMultiplierSmallerThanOneExp( - raw_sum, params.output_multiplier, params.output_shift) + - params.output_offset; - const int32_t clamped_output = - std::min(params.quantized_activation_max, - std::max(params.quantized_activation_min, raw_output)); - output_data[i] = static_cast(clamped_output); - } -} - -inline void Add(const ArithmeticParams& params, - const RuntimeShape& input1_shape, const uint8_t* input1_data, - const RuntimeShape& input2_shape, const uint8_t* input2_data, - const RuntimeShape& output_shape, uint8_t* output_data) { - TFLITE_DCHECK_LE(params.quantized_activation_min, - params.quantized_activation_max); - const int flat_size = - MatchingElementsSize(input1_shape, input2_shape, output_shape); - - TFLITE_DCHECK_GT(params.input1_offset, -256); - TFLITE_DCHECK_GT(params.input2_offset, -256); - TFLITE_DCHECK_LT(params.input1_offset, 256); - TFLITE_DCHECK_LT(params.input2_offset, 256); - AddElementwise(flat_size, params, input1_data, input2_data, output_data); -} - -inline void AddGeneralParamScale(const ArithmeticParams& params, - const RuntimeShape& input1_shape, - const int16_t* input1_data, - const RuntimeShape& input2_shape, - const int16_t* input2_data, - const RuntimeShape& output_shape, - int16_t* output_data) { - TFLITE_DCHECK_LE(params.quantized_activation_min, - params.quantized_activation_max); - const int flat_size = - MatchingElementsSize(input1_shape, input2_shape, output_shape); - - int max_value = std::numeric_limits::max(); - - TFLITE_DCHECK_GT(params.input1_offset, -max_value); - TFLITE_DCHECK_GT(params.input2_offset, -max_value); - TFLITE_DCHECK_LT(params.input1_offset, max_value); - TFLITE_DCHECK_LT(params.input2_offset, max_value); - AddElementwise(flat_size, params, input1_data, input2_data, output_data); -} - -inline void Add(const ArithmeticParams& params, - const RuntimeShape& input1_shape, const int16_t* input1_data, - const RuntimeShape& input2_shape, const int16_t* input2_data, - const RuntimeShape& output_shape, int16_t* output_data, - bool pot_scale = true) { - if (!pot_scale) { - AddGeneralParamScale(params, input1_shape, input1_data, input2_shape, - input2_data, output_shape, output_data); - return; - } - - TFLITE_DCHECK_LE(params.quantized_activation_min, - params.quantized_activation_max); - - const int input1_shift = params.input1_shift; - const int flat_size = - MatchingElementsSize(input1_shape, input2_shape, output_shape); - const int16_t output_activation_min = params.quantized_activation_min; - const int16_t output_activation_max = params.quantized_activation_max; - - TFLITE_DCHECK(input1_shift == 0 || params.input2_shift == 0); - TFLITE_DCHECK_LE(input1_shift, 0); - TFLITE_DCHECK_LE(params.input2_shift, 0); - const int16_t* not_shift_input = - input1_shift == 0 ? input1_data : input2_data; - const int16_t* shift_input = input1_shift == 0 ? input2_data : input1_data; - const int input_right_shift = - input1_shift == 0 ? -params.input2_shift : -input1_shift; - - for (int i = 0; i < flat_size; i++) { - // F0 uses 0 integer bits, range [-1, 1]. - using F0 = gemmlowp::FixedPoint; - - F0 input_ready_scaled = F0::FromRaw(not_shift_input[i]); - F0 scaled_input = F0::FromRaw( - gemmlowp::RoundingDivideByPOT(shift_input[i], input_right_shift)); - F0 result = gemmlowp::SaturatingAdd(scaled_input, input_ready_scaled); - const int16_t raw_output = result.raw(); - const int16_t clamped_output = std::min( - output_activation_max, std::max(output_activation_min, raw_output)); - output_data[i] = clamped_output; - } -} - -template -inline typename std::enable_if::value, void>::type -BroadcastAdd4DSlow(const ArithmeticParams& params, - const RuntimeShape& input1_shape, const T* input1_data, - const RuntimeShape& input2_shape, const T* input2_data, - const RuntimeShape& output_shape, T* output_data) { - NdArrayDesc<4> desc1; - NdArrayDesc<4> desc2; - NdArrayDescsForElementwiseBroadcast(input1_shape, input2_shape, &desc1, - &desc2); - const RuntimeShape extended_output_shape = - RuntimeShape::ExtendedShape(4, output_shape); - - T activation_min, activation_max; - GetActivationParams(params, &activation_min, &activation_max); - - // In Tensorflow, the dimensions are canonically named (batch_number, row, - // col, channel), with extents (batches, height, width, depth), with the - // trailing dimension changing most rapidly (channels has the smallest stride, - // typically 1 element). - // - // In generated C code, we store arrays with the dimensions reversed. The - // first dimension has smallest stride. - // - // We name our variables by their Tensorflow convention, but generate C code - // nesting loops such that the innermost loop has the smallest stride for the - // best cache behavior. - for (int b = 0; b < extended_output_shape.Dims(0); ++b) { - for (int y = 0; y < extended_output_shape.Dims(1); ++y) { - for (int x = 0; x < extended_output_shape.Dims(2); ++x) { - for (int c = 0; c < extended_output_shape.Dims(3); ++c) { - output_data[Offset(extended_output_shape, b, y, x, c)] = - ActivationFunctionWithMinMax( - input1_data[SubscriptToIndex(desc1, b, y, x, c)] + - input2_data[SubscriptToIndex(desc2, b, y, x, c)], - activation_min, activation_max); - } - } - } - } -} - -// This function is used for 8-bit as well as for 16-bit, but the accumulator -// is 32-bit for both cases. The overflow does not happen due to the -// choice of the shift (20 or 15, accordingly - see add.cc for more comments). -template -inline typename std::enable_if::value, void>::type -BroadcastAdd4DSlow(const ArithmeticParams& params, - const RuntimeShape& input1_shape, const T* input1_data, - const RuntimeShape& input2_shape, const T* input2_data, - const RuntimeShape& output_shape, T* output_data) { - NdArrayDesc<4> desc1; - NdArrayDesc<4> desc2; - NdArrayDescsForElementwiseBroadcast(input1_shape, input2_shape, &desc1, - &desc2); - const RuntimeShape extended_output_shape = - RuntimeShape::ExtendedShape(4, output_shape); - - // In Tensorflow, the dimensions are canonically named (batch_number, row, - // col, channel), with extents (batches, height, width, depth), with the - // trailing dimension changing most rapidly (channels has the smallest stride, - // typically 1 element). - // - // In generated C code, we store arrays with the dimensions reversed. The - // first dimension has smallest stride. - // - // We name our variables by their Tensorflow convention, but generate C code - // nesting loops such that the innermost loop has the smallest stride for the - // best cache behavior. - for (int b = 0; b < extended_output_shape.Dims(0); ++b) { - for (int y = 0; y < extended_output_shape.Dims(1); ++y) { - for (int x = 0; x < extended_output_shape.Dims(2); ++x) { - for (int c = 0; c < extended_output_shape.Dims(3); ++c) { - const int32_t input1_val = - params.input1_offset + - input1_data[SubscriptToIndex(desc1, b, y, x, c)]; - const int32_t input2_val = - params.input2_offset + - input2_data[SubscriptToIndex(desc2, b, y, x, c)]; - const int32_t shifted_input1_val = - input1_val * (1 << params.left_shift); - const int32_t shifted_input2_val = - input2_val * (1 << params.left_shift); - const int32_t scaled_input1_val = - MultiplyByQuantizedMultiplierSmallerThanOneExp( - shifted_input1_val, params.input1_multiplier, - params.input1_shift); - const int32_t scaled_input2_val = - MultiplyByQuantizedMultiplierSmallerThanOneExp( - shifted_input2_val, params.input2_multiplier, - params.input2_shift); - const int32_t raw_sum = scaled_input1_val + scaled_input2_val; - const int32_t raw_output = - MultiplyByQuantizedMultiplierSmallerThanOneExp( - raw_sum, params.output_multiplier, params.output_shift) + - params.output_offset; - const int32_t clamped_output = - std::min(params.quantized_activation_max, - std::max(params.quantized_activation_min, raw_output)); - output_data[Offset(extended_output_shape, b, y, x, c)] = - static_cast(clamped_output); - } - } - } - } -} - -inline void BroadcastAddFivefold(const ArithmeticParams& unswitched_params, - const RuntimeShape& unswitched_input1_shape, - const uint8_t* unswitched_input1_data, - const RuntimeShape& unswitched_input2_shape, - const uint8_t* unswitched_input2_data, - const RuntimeShape& output_shape, - uint8_t* output_data) { - ArithmeticParams switched_params = unswitched_params; - switched_params.input1_offset = unswitched_params.input2_offset; - switched_params.input1_multiplier = unswitched_params.input2_multiplier; - switched_params.input1_shift = unswitched_params.input2_shift; - switched_params.input2_offset = unswitched_params.input1_offset; - switched_params.input2_multiplier = unswitched_params.input1_multiplier; - switched_params.input2_shift = unswitched_params.input1_shift; - - const bool use_unswitched = - unswitched_params.broadcast_category == - tflite::BroadcastableOpCategory::kFirstInputBroadcastsFast; - - const ArithmeticParams& params = - use_unswitched ? unswitched_params : switched_params; - const uint8_t* input1_data = - use_unswitched ? unswitched_input1_data : unswitched_input2_data; - const uint8_t* input2_data = - use_unswitched ? unswitched_input2_data : unswitched_input1_data; - - // Fivefold nested loops. The second input resets its position for each - // iteration of the second loop. The first input resets its position at the - // beginning of the fourth loop. The innermost loop is an elementwise add of - // sections of the arrays. - uint8_t* output_data_ptr = output_data; - const uint8_t* input1_data_ptr = input1_data; - const uint8_t* input2_data_reset = input2_data; - // In the fivefold pattern, y0, y2 and y4 are not broadcast, and so shared - // between input shapes. y3 for input 1 is always broadcast, and so the - // dimension there is 1, whereas optionally y1 might be broadcast for input 2. - // Put another way, - // input1.shape.FlatSize = y0 * y1 * y2 * y4, - // input2.shape.FlatSize = y0 * y2 * y3 * y4. - int y0 = params.broadcast_shape[0]; - int y1 = params.broadcast_shape[1]; - int y2 = params.broadcast_shape[2]; - int y3 = params.broadcast_shape[3]; - int y4 = params.broadcast_shape[4]; - if (y4 > 1) { - // General fivefold pattern, with y4 > 1 so there is a non-broadcast inner - // dimension. - for (int i0 = 0; i0 < y0; ++i0) { - const uint8_t* input2_data_ptr; - for (int i1 = 0; i1 < y1; ++i1) { - input2_data_ptr = input2_data_reset; - for (int i2 = 0; i2 < y2; ++i2) { - for (int i3 = 0; i3 < y3; ++i3) { - AddElementwise(y4, params, input1_data_ptr, input2_data_ptr, - output_data_ptr); - input2_data_ptr += y4; - output_data_ptr += y4; - } - // We have broadcast y4 of input1 data y3 times, and now move on. - input1_data_ptr += y4; - } - } - // We have broadcast y2*y3*y4 of input2 data y1 times, and now move on. - input2_data_reset = input2_data_ptr; - } - } else { - // Special case of y4 == 1, in which the innermost loop is a single element - // and can be combined with the next (y3) as an inner broadcast. - // - // Note that this handles the case of pure scalar broadcast when - // y0 == y1 == y2 == 1. With low overhead it handles cases such as scalar - // broadcast with batch (as y2 > 1). - // - // NOTE The process is the same as the above general case except simplified - // for y4 == 1 and the loop over y3 is contained within the - // AddScalarBroadcast function. - for (int i0 = 0; i0 < y0; ++i0) { - const uint8_t* input2_data_ptr; - for (int i1 = 0; i1 < y1; ++i1) { - input2_data_ptr = input2_data_reset; - for (int i2 = 0; i2 < y2; ++i2) { - AddScalarBroadcast(y3, params, *input1_data_ptr, input2_data_ptr, - output_data_ptr); - input2_data_ptr += y3; - output_data_ptr += y3; - input1_data_ptr += 1; - } - } - input2_data_reset = input2_data_ptr; - } - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_ADD_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/add_n.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/add_n.h deleted file mode 100644 index b6b5882d..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/add_n.h +++ /dev/null @@ -1,86 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_ADD_N_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_ADD_N_H_ - -#include -#include - -#include "tensorflow/lite/kernels/internal/common.h" - -namespace tflite { -namespace reference_ops { - -// T is expected to be either float or int. -template -inline void AddN(const RuntimeShape& input_shape, const size_t num_inputs, - const T* const* input_data, T* output_data) { - // All inputs and output should have the same shape, this is checked during - // Prepare stage. - const size_t size = input_shape.FlatSize(); - for (size_t i = 0; i < size; ++i) { - T x = 0; - for (size_t j = 0; j < num_inputs; ++j) { - x += input_data[j][i]; - } - output_data[i] = x; - } -} - -inline void AddN(const ArithmeticParams& params, - const RuntimeShape& input_shape, const size_t num_inputs, - const int8_t* const* input_data, int8_t* output_data) { - TFLITE_DCHECK_LE(params.quantized_activation_min, - params.quantized_activation_max); - // Input offset is negative input zero point. Activation tensors are - // asymmetric quantized so they span the full int8 range. - // All inputs should have same zero-point and scale, this is checked during - // Prepare stage. - TFLITE_DCHECK_GE(-params.input1_offset, std::numeric_limits::min()); - TFLITE_DCHECK_LE(-params.input1_offset, std::numeric_limits::max()); - - // All inputs and output should have the same shape, this is checked during - // Prepare stage. - const size_t size = input_shape.FlatSize(); - for (size_t i = 0; i < size; ++i) { - // accumulate in scaled_x before clamping to avoid overflow - const int32_t x = params.input1_offset; // x = 0 - const int32_t shifted_x = x * (1 << params.left_shift); - int32_t scaled_x = MultiplyByQuantizedMultiplierSmallerThanOneExp( - shifted_x, params.input1_multiplier, params.input1_shift); - - for (size_t j = 0; j < num_inputs; ++j) { - const int32_t y = params.input1_offset + input_data[j][i]; - const int32_t shifted_y = y * (1 << params.left_shift); - int32_t scaled_y = MultiplyByQuantizedMultiplierSmallerThanOneExp( - shifted_y, params.input1_multiplier, params.input1_shift); - scaled_x += scaled_y; - } - - const int32_t raw_output = - MultiplyByQuantizedMultiplierSmallerThanOneExp( - scaled_x, params.output_multiplier, params.output_shift) + - params.output_offset; - const int32_t clamped_output = - std::min(params.quantized_activation_max, - std::max(params.quantized_activation_min, raw_output)); - output_data[i] = static_cast(clamped_output); - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_ADD_N_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/arg_min_max.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/arg_min_max.h deleted file mode 100644 index 8154fbf7..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/arg_min_max.h +++ /dev/null @@ -1,88 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_ARG_MIN_MAX_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_ARG_MIN_MAX_H_ - -#include - -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -namespace reference_ops { - -template -std::function GetComparefunction(bool is_arg_max) { - if (is_arg_max) { - return std::greater(); - } else { - return std::less(); - } -} - -template -void ArgMinMax(const RuntimeShape& input1_shape, const T1* input1_data, - const T3* input2_data, const RuntimeShape& output_shape, - T2* output_data, const Cmp& cmp) { - TFLITE_DCHECK_GT(input1_shape.DimensionsCount(), 0); - TFLITE_DCHECK_EQ(input1_shape.DimensionsCount() - 1, - output_shape.DimensionsCount()); - int axis = input2_data[0]; - if (axis < 0) { - axis += input1_shape.DimensionsCount(); - } - const int axis_size = input1_shape.Dims(axis); - - int outer_size = 1; - for (int i = 0; i < axis; ++i) { - TFLITE_DCHECK_EQ(input1_shape.Dims(i), output_shape.Dims(i)); - outer_size *= input1_shape.Dims(i); - } - - int inner_size = 1; - const int dims_count = input1_shape.DimensionsCount(); - for (int i = axis + 1; i < dims_count; ++i) { - TFLITE_DCHECK_EQ(input1_shape.Dims(i), output_shape.Dims(i - 1)); - inner_size *= input1_shape.Dims(i); - } - for (int outer = 0; outer < outer_size; ++outer) { - for (int inner = 0; inner < inner_size; ++inner) { - auto min_max_value = input1_data[outer * axis_size * inner_size + inner]; - T2 min_max_index = 0; - for (int i = 1; i < axis_size; ++i) { - const auto& curr_value = - input1_data[(outer * axis_size + i) * inner_size + inner]; - if (cmp(curr_value, min_max_value)) { - min_max_value = curr_value; - min_max_index = static_cast(i); - } - } - output_data[outer * inner_size + inner] = min_max_index; - } - } -} - -template -void ArgMinMax(const RuntimeShape& input1_shape, const T1* input1_data, - const T3* input2_data, const RuntimeShape& output_shape, - T2* output_data, const bool is_arg_max) { - ArgMinMax(input1_shape, input1_data, input2_data, output_shape, output_data, - GetComparefunction(is_arg_max)); -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_ARG_MIN_MAX_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/batch_matmul.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/batch_matmul.h deleted file mode 100644 index 767ad6ab..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/batch_matmul.h +++ /dev/null @@ -1,275 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_BATCH_MATMUL_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_BATCH_MATMUL_H_ - -#include -#include - -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/internal/portable_tensor_utils.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { -namespace reference_ops { -namespace batch_matmul { - -// Determine which dimension is the broadcast dimension. -inline int broadcast_dim(int lhs_dim, int rhs_dim) { - if (lhs_dim == rhs_dim) return lhs_dim; - if (lhs_dim == 1) return rhs_dim; - TFLITE_DCHECK_EQ(rhs_dim, 1); - return lhs_dim; -} - -// Compute the "extent" for iterating on this dimension. -// If we are broadcasting, then don't advance (i.e return 0). -inline int extent(const RuntimeShape& shape, int x) { - if (shape.Dims(x) == 1) { - return 0; - } - int prod = 1; - for (int i = x + 1; i < shape.DimensionsCount(); ++i) { - prod *= shape.Dims(i); - } - return prod; -} - -} // namespace batch_matmul - -template -inline void BatchMatMul(const RuntimeShape& lhs_shape, const Ta* lhs_data, - const RuntimeShape& rhs_shape, const Tb* rhs_data, - const RuntimeShape& output_shape, Tout* output_data) { - const RuntimeShape extended_lhs_shape = - RuntimeShape::ExtendedShape(5, lhs_shape); - const RuntimeShape extended_rhs_shape = - RuntimeShape::ExtendedShape(5, rhs_shape); - - const int batch_dim0 = batch_matmul::broadcast_dim( - extended_lhs_shape.Dims(0), extended_rhs_shape.Dims(0)); - const int batch_dim1 = batch_matmul::broadcast_dim( - extended_lhs_shape.Dims(1), extended_rhs_shape.Dims(1)); - const int batch_dim2 = batch_matmul::broadcast_dim( - extended_lhs_shape.Dims(2), extended_rhs_shape.Dims(2)); - - const int lhs_ext0 = batch_matmul::extent(extended_lhs_shape, 0); - const int lhs_ext1 = batch_matmul::extent(extended_lhs_shape, 1); - const int lhs_ext2 = batch_matmul::extent(extended_lhs_shape, 2); - const int rhs_ext0 = batch_matmul::extent(extended_rhs_shape, 0); - const int rhs_ext1 = batch_matmul::extent(extended_rhs_shape, 1); - const int rhs_ext2 = batch_matmul::extent(extended_rhs_shape, 2); - - // Set params for each matrix multiply. - const int lhs_rows = extended_lhs_shape.Dims(3); - const int rhs_cols = extended_rhs_shape.Dims(4); - const int accum_depth = extended_lhs_shape.Dims(4); - - for (int b0 = 0; b0 < batch_dim0; ++b0) { - const Ta* lhs_ptr0 = lhs_data + (b0 * lhs_ext0); - const Tb* rhs_ptr0 = rhs_data + (b0 * rhs_ext0); - for (int b1 = 0; b1 < batch_dim1; ++b1) { - const Ta* lhs_ptr1 = lhs_ptr0 + b1 * lhs_ext1; - const Tb* rhs_ptr1 = rhs_ptr0 + b1 * rhs_ext1; - for (int b2 = 0; b2 < batch_dim2; ++b2) { - const Ta* lhs_ptr2 = lhs_ptr1 + b2 * lhs_ext2; - const Tb* rhs_ptr2 = rhs_ptr1 + b2 * rhs_ext2; - Tout* out_ptr = output_data + ((b0 * batch_dim1 * batch_dim2) + - b1 * batch_dim2 + b2) * - lhs_rows * rhs_cols; - for (int j = 0; j < rhs_cols; ++j) { - for (int i = 0; i < lhs_rows; ++i) { - Tout total = 0; - for (int k = 0; k < accum_depth; ++k) { - total += static_cast(lhs_ptr2[accum_depth * i + k]) * - static_cast(rhs_ptr2[j * accum_depth + k]); - } - int idx = lhs_rows * j + i; - out_ptr[idx] = total; - } - } - } - } - } -} - -inline void BatchMatMul(const RuntimeShape& lhs_shape, const int8_t* lhs_data, - const RuntimeShape& rhs_shape, const int8_t* rhs_data, - const float* scaling_factors, - const int32_t* input_offset, int32_t* row_sums, - const RuntimeShape& output_shape, float* output_data, - bool* compute_row_sums) { - const RuntimeShape extended_lhs_shape = - RuntimeShape::ExtendedShape(5, lhs_shape); - const RuntimeShape extended_rhs_shape = - RuntimeShape::ExtendedShape(5, rhs_shape); - - const int batch_dim0 = batch_matmul::broadcast_dim( - extended_lhs_shape.Dims(0), extended_rhs_shape.Dims(0)); - const int batch_dim1 = batch_matmul::broadcast_dim( - extended_lhs_shape.Dims(1), extended_rhs_shape.Dims(1)); - const int batch_dim2 = batch_matmul::broadcast_dim( - extended_lhs_shape.Dims(2), extended_rhs_shape.Dims(2)); - - const int lhs_ext0 = batch_matmul::extent(extended_lhs_shape, 0); - const int lhs_ext1 = batch_matmul::extent(extended_lhs_shape, 1); - const int lhs_ext2 = batch_matmul::extent(extended_lhs_shape, 2); - const int rhs_ext0 = batch_matmul::extent(extended_rhs_shape, 0); - const int rhs_ext1 = batch_matmul::extent(extended_rhs_shape, 1); - const int rhs_ext2 = batch_matmul::extent(extended_rhs_shape, 2); - - // Set params for each matrix multiply. - const int lhs_rows = extended_lhs_shape.Dims(3); - const int rhs_cols = extended_rhs_shape.Dims(4); - const int accum_depth = extended_lhs_shape.Dims(4); - - const int ioff_ext0 = rhs_ext0 == 0 ? 0 : rhs_cols; - const int ioff_ext1 = rhs_ext1 == 0 ? 0 : rhs_cols; - const int ioff_ext2 = rhs_ext2 == 0 ? 0 : rhs_cols; - const int woff_ext0 = lhs_ext0 == 0 ? 0 : lhs_rows; - const int woff_ext1 = lhs_ext1 == 0 ? 0 : lhs_rows; - const int woff_ext2 = lhs_ext2 == 0 ? 0 : lhs_rows; - - if (!compute_row_sums || *compute_row_sums) { - int num_weights_matrices = 1; - for (int i = 1; i < extended_lhs_shape.DimensionsCount() - 2; ++i) { - num_weights_matrices *= extended_lhs_shape.Dims(i); - } - tensor_utils::ReductionSumVector( - lhs_data, row_sums, num_weights_matrices * lhs_rows, accum_depth); - if (compute_row_sums) { - *compute_row_sums = false; - } - } - - for (int b0 = 0; b0 < batch_dim0; ++b0) { - const int8_t* lhs_ptr0 = lhs_data + (b0 * lhs_ext0); - const int8_t* rhs_ptr0 = rhs_data + (b0 * rhs_ext0); - const int32_t* ioff_ptr0 = input_offset + (b0 * ioff_ext0); - const float* scale_ptr0 = scaling_factors + (b0 * ioff_ext0); - const int32_t* woff_ptr0 = row_sums + (b0 * woff_ext0); - for (int b1 = 0; b1 < batch_dim1; ++b1) { - const int8_t* lhs_ptr1 = lhs_ptr0 + b1 * lhs_ext1; - const int8_t* rhs_ptr1 = rhs_ptr0 + b1 * rhs_ext1; - const int32_t* ioff_ptr1 = ioff_ptr0 + (b1 * ioff_ext1); - const float* scale_ptr1 = scale_ptr0 + (b1 * ioff_ext1); - const int32_t* woff_ptr1 = woff_ptr0 + (b1 * woff_ext1); - for (int b2 = 0; b2 < batch_dim2; ++b2) { - const int8_t* lhs_ptr2 = lhs_ptr1 + b2 * lhs_ext2; - const int8_t* rhs_ptr2 = rhs_ptr1 + b2 * rhs_ext2; - const int32_t* ioff_ptr2 = ioff_ptr1 + (b2 * ioff_ext2); - const float* scale_ptr2 = scale_ptr1 + (b2 * ioff_ext2); - const int32_t* woff_ptr2 = woff_ptr1 + (b2 * woff_ext2); - float* out_ptr = output_data + ((b0 * batch_dim1 * batch_dim2) + - b1 * batch_dim2 + b2) * - lhs_rows * rhs_cols; - for (int j = 0; j < rhs_cols; ++j) { - const float batch_scaling_factor = scale_ptr2[j]; - const float batch_offset = static_cast(ioff_ptr2[j]); - for (int i = 0; i < lhs_rows; ++i) { - int32_t total = 0; - for (int k = 0; k < accum_depth; ++k) { - total += - lhs_ptr2[accum_depth * i + k] * rhs_ptr2[j * accum_depth + k]; - } - int32_t row_sum = woff_ptr2[i]; - total -= row_sum * batch_offset; - int idx = lhs_rows * j + i; - out_ptr[idx] += batch_scaling_factor * total; - } - } - } - } - } -} - -template -inline void BatchMatMul(const FullyConnectedParams& params, - const RuntimeShape& lhs_shape, const T* lhs_data, - const RuntimeShape& rhs_shape, const T* rhs_data, - const RuntimeShape& output_shape, T* output_data) { - const RuntimeShape extended_lhs_shape = - RuntimeShape::ExtendedShape(5, lhs_shape); - const RuntimeShape extended_rhs_shape = - RuntimeShape::ExtendedShape(5, rhs_shape); - - const int batch_dim0 = batch_matmul::broadcast_dim( - extended_lhs_shape.Dims(0), extended_rhs_shape.Dims(0)); - const int batch_dim1 = batch_matmul::broadcast_dim( - extended_lhs_shape.Dims(1), extended_rhs_shape.Dims(1)); - const int batch_dim2 = batch_matmul::broadcast_dim( - extended_lhs_shape.Dims(2), extended_rhs_shape.Dims(2)); - - const int lhs_ext0 = batch_matmul::extent(extended_lhs_shape, 0); - const int lhs_ext1 = batch_matmul::extent(extended_lhs_shape, 1); - const int lhs_ext2 = batch_matmul::extent(extended_lhs_shape, 2); - const int rhs_ext0 = batch_matmul::extent(extended_rhs_shape, 0); - const int rhs_ext1 = batch_matmul::extent(extended_rhs_shape, 1); - const int rhs_ext2 = batch_matmul::extent(extended_rhs_shape, 2); - - // Set params for each matrix multiply. - const int lhs_rows = extended_lhs_shape.Dims(3); - const int rhs_cols = extended_rhs_shape.Dims(4); - const int accum_depth = extended_lhs_shape.Dims(4); - - const int32_t input_offset = params.input_offset; - const int32_t filter_offset = params.weights_offset; - const int32_t output_offset = params.output_offset; - const int32_t output_multiplier = params.output_multiplier; - const int output_shift = params.output_shift; - const int32_t output_activation_min = params.quantized_activation_min; - const int32_t output_activation_max = params.quantized_activation_max; - TFLITE_DCHECK_LE(output_activation_min, output_activation_max); - - for (int b0 = 0; b0 < batch_dim0; ++b0) { - const T* lhs_ptr0 = lhs_data + (b0 * lhs_ext0); - const T* rhs_ptr0 = rhs_data + (b0 * rhs_ext0); - for (int b1 = 0; b1 < batch_dim1; ++b1) { - const T* lhs_ptr1 = lhs_ptr0 + b1 * lhs_ext1; - const T* rhs_ptr1 = rhs_ptr0 + b1 * rhs_ext1; - for (int b2 = 0; b2 < batch_dim2; ++b2) { - const T* lhs_ptr2 = lhs_ptr1 + b2 * lhs_ext2; - const T* rhs_ptr2 = rhs_ptr1 + b2 * rhs_ext2; - T* out_ptr = output_data + - ((b0 * batch_dim1 * batch_dim2) + b1 * batch_dim2 + b2) * - lhs_rows * rhs_cols; - - for (int j = 0; j < rhs_cols; ++j) { - for (int i = 0; i < lhs_rows; ++i) { - AccumT total = 0; - for (int k = 0; k < accum_depth; ++k) { - AccumT lhs_val = lhs_ptr2[accum_depth * i + k]; - AccumT rhs_val = rhs_ptr2[accum_depth * j + k]; - total += (lhs_val + filter_offset) * (rhs_val + input_offset); - } - int32_t total_scaled = MultiplyByQuantizedMultiplier( - total, output_multiplier, output_shift); - total_scaled += output_offset; - total_scaled = std::max(total_scaled, output_activation_min); - total_scaled = std::min(total_scaled, output_activation_max); - const int idx = lhs_rows * j + i; - out_ptr[idx] = static_cast(total_scaled); - } - } - } - } - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_BATCH_MATMUL_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/batch_to_space_nd.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/batch_to_space_nd.h deleted file mode 100644 index cda46a26..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/batch_to_space_nd.h +++ /dev/null @@ -1,101 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_BATCH_TO_SPACE_ND_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_BATCH_TO_SPACE_ND_H_ - -#include - -#include "ruy/profiler/instrumentation.h" // from @ruy -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { -namespace reference_ops { - -// TODO(b/135760455): Move this method anonymous namespace in a cc file. -inline RuntimeShape ExtendShapeBatchToSpace(const RuntimeShape& shape) { - if (shape.DimensionsCount() == 4) { - return shape; - } - RuntimeShape new_shape(4, 1); - new_shape.SetDim(0, shape.Dims(0)); - new_shape.SetDim(1, shape.Dims(1)); - new_shape.SetDim(3, shape.Dims(2)); - return new_shape; -} - -template -inline void BatchToSpaceND(const RuntimeShape& unextended_input1_shape, - const T* input1_data, - const RuntimeShape& unextended_input2_shape, - const int32_t* block_shape_data, - const RuntimeShape& unextended_input3_shape, - const int32_t* crops_data, - const RuntimeShape& unextended_output_shape, - T* output_data) { - ruy::profiler::ScopeLabel label("BatchToSpaceND"); - TFLITE_DCHECK_GE(unextended_input1_shape.DimensionsCount(), 3); - TFLITE_DCHECK_LE(unextended_input1_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(unextended_input1_shape.DimensionsCount(), - unextended_output_shape.DimensionsCount()); - - const RuntimeShape input1_shape = - ExtendShapeBatchToSpace(unextended_input1_shape); - const RuntimeShape output_shape = - ExtendShapeBatchToSpace(unextended_output_shape); - - const int output_width = output_shape.Dims(2); - const int output_height = output_shape.Dims(1); - const int output_batch_size = output_shape.Dims(0); - - const int depth = input1_shape.Dims(3); - const int input_width = input1_shape.Dims(2); - const int input_height = input1_shape.Dims(1); - const int input_batch_size = input1_shape.Dims(0); - - const int block_shape_height = block_shape_data[0]; - const int block_shape_width = - unextended_input1_shape.DimensionsCount() == 4 ? block_shape_data[1] : 1; - const int crops_top = crops_data[0]; - const int crops_left = - unextended_input1_shape.DimensionsCount() == 4 ? crops_data[2] : 0; - for (int in_batch = 0; in_batch < input_batch_size; ++in_batch) { - const int out_batch = in_batch % output_batch_size; - const int spatial_offset = in_batch / output_batch_size; - for (int in_h = 0; in_h < input_height; ++in_h) { - const int out_h = in_h * block_shape_height + - spatial_offset / block_shape_width - crops_top; - if (out_h < 0 || out_h >= output_height) { - continue; - } - for (int in_w = 0; in_w < input_width; ++in_w) { - const int out_w = in_w * block_shape_width + - spatial_offset % block_shape_width - crops_left; - - if (out_w < 0 || out_w >= output_width) { - continue; - } - T* out = output_data + Offset(output_shape, out_batch, out_h, out_w, 0); - const T* in = - input1_data + Offset(input1_shape, in_batch, in_h, in_w, 0); - memcpy(out, in, depth * sizeof(T)); - } - } - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_BATCH_TO_SPACE_ND_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/binary_function.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/binary_function.h deleted file mode 100644 index 0b124af8..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/binary_function.h +++ /dev/null @@ -1,91 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_BINARY_FUNCTION_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_BINARY_FUNCTION_H_ - -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -namespace reference_ops { - -// Also appears to duplicate MinimumMaximum. -// -// R: Result type. T1: Input 1 type. T2: Input 2 type. -template -inline void BroadcastBinaryFunction4DSlow( - const RuntimeShape& unextended_input1_shape, const T1* input1_data, - const RuntimeShape& unextended_input2_shape, const T2* input2_data, - const RuntimeShape& unextended_output_shape, R* output_data, - R (*func)(T1, T2)) { - TFLITE_DCHECK_LE(unextended_input1_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(unextended_input2_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(unextended_output_shape.DimensionsCount(), 4); - const RuntimeShape output_shape = - RuntimeShape::ExtendedShape(4, unextended_output_shape); - - NdArrayDesc<4> desc1; - NdArrayDesc<4> desc2; - NdArrayDescsForElementwiseBroadcast(unextended_input1_shape, - unextended_input2_shape, &desc1, &desc2); - - const int* dims_data = - reinterpret_cast(output_shape.DimsDataUpTo5D()); - for (int b = 0; b < output_shape.Dims(0); ++b) { - int out_idx_b = b * dims_data[1]; - int in_idx1_b = desc1.strides[0] * b; - int in_idx2_b = desc2.strides[0] * b; - for (int y = 0; y < output_shape.Dims(1); ++y) { - int out_idx_y = (out_idx_b + y) * dims_data[2]; - int in_idx1_y = in_idx1_b + desc1.strides[1] * y; - int in_idx2_y = in_idx2_b + desc2.strides[1] * y; - for (int x = 0; x < output_shape.Dims(2); ++x) { - int out_idx_x = (out_idx_y + x) * dims_data[3]; - int in1_idx = in_idx1_y + desc1.strides[2] * x; - int in2_idx = in_idx2_y + desc2.strides[2] * x; - for (int c = 0; c < output_shape.Dims(3); ++c) { - auto out_idx = out_idx_x + c; - auto in1_val = input1_data[in1_idx]; - auto in2_val = input2_data[in2_idx]; - output_data[out_idx] = func(in1_val, in2_val); - in1_idx += desc1.strides[3]; - in2_idx += desc2.strides[3]; - } - } - } - } -} - -// R: Result type. T1: Input 1 type. T2: Input 2 type. -template -inline void BinaryFunction(const RuntimeShape& input1_shape, - const T1* input1_data, - const RuntimeShape& input2_shape, - const T2* input2_data, - const RuntimeShape& output_shape, R* output_data, - R (*func)(T1, T2)) { - const int flat_size = - MatchingFlatSize(input1_shape, input2_shape, output_shape); - for (int i = 0; i < flat_size; ++i) { - output_data[i] = func(input1_data[i], input2_data[i]); - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_BINARY_FUNCTION_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/broadcast_args.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/broadcast_args.h deleted file mode 100644 index d93c316d..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/broadcast_args.h +++ /dev/null @@ -1,56 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_BROADCAST_ARGS_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_BROADCAST_ARGS_H_ - -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { -namespace reference_ops { - -template -void BroadcastArgs(const RuntimeShape& input1_shape, const T* input1_data, - const RuntimeShape& input2_shape, const T* input2_data, - const RuntimeShape& output_shape, T* output_data) { - // Gets data at the backward index i of the shape tensor. Returns 1 if the - // index is out of range. - auto get_shape_data = [](const RuntimeShape& shape, const T* data, - int backward_idx) -> T { - int forward_idx = shape.FlatSize() - 1 - backward_idx; - if (forward_idx < 0) return 1; - return data[forward_idx]; - }; - - int output_num_elements = output_shape.FlatSize(); - for (int i = 0; i < output_num_elements; ++i) { - int backward_i = output_num_elements - 1 - i; - int shape1_i = get_shape_data(input1_shape, input1_data, i); - int shape2_i = get_shape_data(input2_shape, input2_data, i); - if (shape1_i == 1) { - output_data[backward_i] = shape2_i; - } else if (shape2_i == 1) { - output_data[backward_i] = shape1_i; - } else { - TFLITE_CHECK_EQ(shape1_i, shape2_i); - output_data[backward_i] = shape1_i; - } - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_BROADCAST_ARGS_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/broadcast_to.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/broadcast_to.h deleted file mode 100644 index f106b2b5..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/broadcast_to.h +++ /dev/null @@ -1,97 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_BROADCAST_TO_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_BROADCAST_TO_H_ - -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/kernel_util.h" - -namespace tflite { -namespace reference_ops { -template -void BroadcastImpl(const NdArrayDesc& input_desc, const char* input_data, - const NdArrayDesc& output_desc, char* output_data, - int indexes[N], int dim, const int last_broadcasting_dim, - const int type_size) { - // Copy data from input to output. - if (dim == last_broadcasting_dim) { - int copy_size = output_desc.strides[dim] * type_size; - const char* data_src = - input_data + SubscriptToIndex(input_desc, indexes) * type_size; - char* data_dst = - output_data + SubscriptToIndex(output_desc, indexes) * type_size; - for (int i = 0; i < output_desc.extents[dim]; ++i, data_dst += copy_size) { - memcpy(data_dst, data_src, copy_size); - } - return; - } - - // Recursive call to find the next broadcasting. - for (indexes[dim] = 0; indexes[dim] < input_desc.extents[dim]; - ++indexes[dim]) { - BroadcastImpl(input_desc, input_data, output_desc, output_data, indexes, - dim + 1, last_broadcasting_dim, type_size); - } - - // Duplicate data in output tensor. - indexes[dim] = 0; - if (input_desc.extents[dim] != output_desc.extents[dim]) { - int copy_size = output_desc.strides[dim] * type_size; - char* data_src = - output_data + SubscriptToIndex(output_desc, indexes) * type_size; - char* data_dst = data_src + copy_size; - for (int i = 1; i < output_desc.extents[dim]; ++i, data_dst += copy_size) { - memcpy(data_dst, data_src, copy_size); - } - } -} - -template -inline void BroadcastTo(const RuntimeShape& unextended_input_shape, - const char* input_data, - const RuntimeShape& unextended_output_shape, - char* output_data, TfLiteType data_type) { - NdArrayDesc input_desc; - NdArrayDesc output_desc; - CopyDimsToDesc(RuntimeShape::ExtendedShape(N, unextended_input_shape), - &input_desc); - CopyDimsToDesc(RuntimeShape::ExtendedShape(N, unextended_output_shape), - &output_desc); - - // Get the last dimension has broadcasting. At this dimension, the data is - // copied from input tensor to output tensor. - int last_broadcast_dim = -1; - for (int i = N - 1; i >= 0; --i) { - if (input_desc.extents[i] != output_desc.extents[i]) { - last_broadcast_dim = i; - break; - } - } - - // If non-broadcasting, just copy data from input to output tensor. - if (last_broadcast_dim == -1) { - memcpy(output_data, input_data, - unextended_input_shape.FlatSize() * TfLiteTypeGetSize(data_type)); - return; - } - - // Broadcasting using memcpy. - int indexes[N] = {0}; - BroadcastImpl(input_desc, input_data, output_desc, output_data, indexes, 0, - last_broadcast_dim, TfLiteTypeGetSize(data_type)); -} -} // namespace reference_ops -} // namespace tflite -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_BROADCAST_TO_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/ceil.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/ceil.h deleted file mode 100644 index 66d1dc35..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/ceil.h +++ /dev/null @@ -1,37 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_CEIL_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_CEIL_H_ - -#include - -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -namespace reference_ops { - -inline void Ceil(const RuntimeShape& input_shape, const float* input_data, - const RuntimeShape& output_shape, float* output_data) { - const int flat_size = MatchingFlatSize(input_shape, output_shape); - - for (int i = 0; i < flat_size; ++i) { - output_data[i] = std::ceil(input_data[i]); - } -} - -} // namespace reference_ops -} // namespace tflite -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_CEIL_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/comparisons.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/comparisons.h deleted file mode 100644 index 6344bdc7..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/comparisons.h +++ /dev/null @@ -1,280 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_COMPARISONS_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_COMPARISONS_H_ - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -namespace reference_ops { - -template -inline bool EqualFn(T lhs, T rhs) { - return lhs == rhs; -} - -template -inline bool NotEqualFn(T lhs, T rhs) { - return lhs != rhs; -} - -template -inline bool GreaterFn(T lhs, T rhs) { - return lhs > rhs; -} -template -inline bool GreaterEqualFn(T lhs, T rhs) { - return lhs >= rhs; -} -template -inline bool LessFn(T lhs, T rhs) { - return lhs < rhs; -} -template -inline bool LessEqualFn(T lhs, T rhs) { - return lhs <= rhs; -} - -template -using ComparisonFn = bool (*)(T, T); - -template F> -inline void ComparisonImpl( - const ComparisonParams& op_params, const RuntimeShape& input1_shape, - const T* input1_data, const RuntimeShape& input2_shape, - const T* input2_data, const RuntimeShape& output_shape, bool* output_data) { - const int64_t flatsize = - MatchingFlatSize(input1_shape, input2_shape, output_shape); - for (int64_t i = 0; i < flatsize; ++i) { - output_data[i] = F(input1_data[i], input2_data[i]); - } -} - -template F> -inline void Comparison(const ComparisonParams& op_params, - const RuntimeShape& input1_shape, - const float* input1_data, - const RuntimeShape& input2_shape, - const float* input2_data, - const RuntimeShape& output_shape, bool* output_data) { - ComparisonImpl(op_params, input1_shape, input1_data, input2_shape, - input2_data, output_shape, output_data); -} - -template F> -inline void ComparisonWithScaling( - const ComparisonParams& op_params, const RuntimeShape& input1_shape, - const T* input1_data, const RuntimeShape& input2_shape, - const T* input2_data, const RuntimeShape& output_shape, bool* output_data) { - int left_shift = op_params.left_shift; - int32_t input1_offset = op_params.input1_offset; - int32_t input1_multiplier = op_params.input1_multiplier; - int input1_shift = op_params.input1_shift; - int32_t input2_offset = op_params.input2_offset; - int32_t input2_multiplier = op_params.input2_multiplier; - int input2_shift = op_params.input2_shift; - - const int64_t flatsize = - MatchingFlatSize(input1_shape, input2_shape, output_shape); - for (int64_t i = 0; i < flatsize; ++i) { - const int32_t input1_val = input1_offset + input1_data[i]; - const int32_t input2_val = input2_offset + input2_data[i]; - const int32_t shifted_input1_val = input1_val * (1 << left_shift); - const int32_t shifted_input2_val = input2_val * (1 << left_shift); - const int32_t scaled_input1_val = - MultiplyByQuantizedMultiplierSmallerThanOneExp( - shifted_input1_val, input1_multiplier, input1_shift); - const int32_t scaled_input2_val = - MultiplyByQuantizedMultiplierSmallerThanOneExp( - shifted_input2_val, input2_multiplier, input2_shift); - output_data[i] = F(scaled_input1_val, scaled_input2_val); - } -} - -struct BroadcastComparison4DSlowCommon { - const RuntimeShape output_shape; - NdArrayDesc<4> desc1; - NdArrayDesc<4> desc2; -}; - -inline BroadcastComparison4DSlowCommon BroadcastComparison4DSlowPreprocess( - const RuntimeShape& unextended_input1_shape, - const RuntimeShape& unextended_input2_shape, - const RuntimeShape& unextended_output_shape) { - TFLITE_DCHECK_LE(unextended_input1_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(unextended_input2_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(unextended_output_shape.DimensionsCount(), 4); - NdArrayDesc<4> desc1; - NdArrayDesc<4> desc2; - NdArrayDescsForElementwiseBroadcast(unextended_input1_shape, - unextended_input2_shape, &desc1, &desc2); - return {RuntimeShape::ExtendedShape(4, unextended_output_shape), desc1, - desc2}; -} - -template F> -inline void BroadcastComparison4DSlowImpl( - const ComparisonParams& op_params, - const RuntimeShape& unextended_input1_shape, const T* input1_data, - const RuntimeShape& unextended_input2_shape, const T* input2_data, - const RuntimeShape& unextended_output_shape, bool* output_data) { - const BroadcastComparison4DSlowCommon dims = - BroadcastComparison4DSlowPreprocess(unextended_input1_shape, - unextended_input2_shape, - unextended_output_shape); - - for (int b = 0; b < dims.output_shape.Dims(0); ++b) { - for (int y = 0; y < dims.output_shape.Dims(1); ++y) { - for (int x = 0; x < dims.output_shape.Dims(2); ++x) { - for (int c = 0; c < dims.output_shape.Dims(3); ++c) { - output_data[Offset(dims.output_shape, b, y, x, c)] = - F(input1_data[SubscriptToIndex(dims.desc1, b, y, x, c)], - input2_data[SubscriptToIndex(dims.desc2, b, y, x, c)]); - } - } - } - } -} - -template F> -inline void BroadcastComparison4DSlow(const ComparisonParams& op_params, - const RuntimeShape& input1_shape, - const float* input1_data, - const RuntimeShape& input2_shape, - const float* input2_data, - const RuntimeShape& output_shape, - bool* output_data) { - BroadcastComparison4DSlowImpl(op_params, input1_shape, input1_data, - input2_shape, input2_data, - output_shape, output_data); -} - -template F> -inline void BroadcastComparison4DSlowWithScaling( - const ComparisonParams& op_params, - const RuntimeShape& unextended_input1_shape, const T* input1_data, - const RuntimeShape& unextended_input2_shape, const T* input2_data, - const RuntimeShape& unextended_output_shape, bool* output_data) { - const BroadcastComparison4DSlowCommon dims = - BroadcastComparison4DSlowPreprocess(unextended_input1_shape, - unextended_input2_shape, - unextended_output_shape); - - int left_shift = op_params.left_shift; - int32_t input1_offset = op_params.input1_offset; - int32_t input1_multiplier = op_params.input1_multiplier; - int input1_shift = op_params.input1_shift; - int32_t input2_offset = op_params.input2_offset; - int32_t input2_multiplier = op_params.input2_multiplier; - int input2_shift = op_params.input2_shift; - - for (int b = 0; b < dims.output_shape.Dims(0); ++b) { - for (int y = 0; y < dims.output_shape.Dims(1); ++y) { - for (int x = 0; x < dims.output_shape.Dims(2); ++x) { - for (int c = 0; c < dims.output_shape.Dims(3); ++c) { - const int32_t input1_val = - input1_offset + - input1_data[SubscriptToIndex(dims.desc1, b, y, x, c)]; - const int32_t input2_val = - input2_offset + - input2_data[SubscriptToIndex(dims.desc2, b, y, x, c)]; - const int32_t shifted_input1_val = input1_val * (1 << left_shift); - const int32_t shifted_input2_val = input2_val * (1 << left_shift); - const int32_t scaled_input1_val = - MultiplyByQuantizedMultiplierSmallerThanOneExp( - shifted_input1_val, input1_multiplier, input1_shift); - const int32_t scaled_input2_val = - MultiplyByQuantizedMultiplierSmallerThanOneExp( - shifted_input2_val, input2_multiplier, input2_shift); - output_data[Offset(dims.output_shape, b, y, x, c)] = - F(scaled_input1_val, scaled_input2_val); - } - } - } - } -} - -#define TFLITE_COMPARISON_OP(name) \ - inline void name(const ComparisonParams& op_params, \ - const RuntimeShape& input1_shape, const float* input1_data, \ - const RuntimeShape& input2_shape, const float* input2_data, \ - const RuntimeShape& output_shape, bool* output_data) { \ - Comparison(op_params, input1_shape, input1_data, input2_shape, \ - input2_data, output_shape, output_data); \ - } \ - template \ - inline void name##NoScaling( \ - const ComparisonParams& op_params, const RuntimeShape& input1_shape, \ - const T* input1_data, const RuntimeShape& input2_shape, \ - const T* input2_data, const RuntimeShape& output_shape, \ - bool* output_data) { \ - ComparisonImpl(op_params, input1_shape, input1_data, \ - input2_shape, input2_data, output_shape, \ - output_data); \ - } \ - template \ - inline void name##WithScaling( \ - const ComparisonParams& op_params, const RuntimeShape& input1_shape, \ - const T* input1_data, const RuntimeShape& input2_shape, \ - const T* input2_data, const RuntimeShape& output_shape, \ - bool* output_data) { \ - ComparisonWithScaling(op_params, input1_shape, input1_data, \ - input2_shape, input2_data, \ - output_shape, output_data); \ - } \ - template \ - inline void Broadcast4DSlow##name##NoScaling( \ - const ComparisonParams& op_params, const RuntimeShape& input1_shape, \ - const T* input1_data, const RuntimeShape& input2_shape, \ - const T* input2_data, const RuntimeShape& output_shape, \ - bool* output_data) { \ - BroadcastComparison4DSlowImpl( \ - op_params, input1_shape, input1_data, input2_shape, input2_data, \ - output_shape, output_data); \ - } \ - inline void Broadcast4DSlow##name( \ - const ComparisonParams& op_params, const RuntimeShape& input1_shape, \ - const float* input1_data, const RuntimeShape& input2_shape, \ - const float* input2_data, const RuntimeShape& output_shape, \ - bool* output_data) { \ - BroadcastComparison4DSlow(op_params, input1_shape, input1_data, \ - input2_shape, input2_data, \ - output_shape, output_data); \ - } \ - template \ - inline void Broadcast4DSlow##name##WithScaling( \ - const ComparisonParams& op_params, const RuntimeShape& input1_shape, \ - const T* input1_data, const RuntimeShape& input2_shape, \ - const T* input2_data, const RuntimeShape& output_shape, \ - bool* output_data) { \ - BroadcastComparison4DSlowWithScaling( \ - op_params, input1_shape, input1_data, input2_shape, input2_data, \ - output_shape, output_data); \ - } -TFLITE_COMPARISON_OP(Equal); -TFLITE_COMPARISON_OP(NotEqual); -TFLITE_COMPARISON_OP(Greater); -TFLITE_COMPARISON_OP(GreaterEqual); -TFLITE_COMPARISON_OP(Less); -TFLITE_COMPARISON_OP(LessEqual); -#undef TFLITE_COMPARISON_OP - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_COMPARISONS_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/concatenation.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/concatenation.h deleted file mode 100644 index 9d2ecbec..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/concatenation.h +++ /dev/null @@ -1,141 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_CONCATENATION_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_CONCATENATION_H_ - -#include - -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/internal/cppmath.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { -namespace reference_ops { - -template -inline void Concatenation(const ConcatenationParams& params, - const RuntimeShape* const* input_shapes, - const Scalar* const* input_data, - const RuntimeShape& output_shape, - Scalar* output_data) { - int axis = params.axis; - int inputs_count = params.inputs_count; - const int concat_dimensions = output_shape.DimensionsCount(); - TFLITE_DCHECK_LT(axis, concat_dimensions); - - int64_t concat_size = 0; - for (int i = 0; i < inputs_count; i++) { - TFLITE_DCHECK_EQ(input_shapes[i]->DimensionsCount(), concat_dimensions); - for (int j = 0; j < concat_dimensions; j++) { - if (j != axis) { - MatchingDim(*input_shapes[i], j, output_shape, j); - } - } - concat_size += input_shapes[i]->Dims(axis); - } - TFLITE_DCHECK_EQ(concat_size, output_shape.Dims(axis)); - int64_t outer_size = 1; - for (int i = 0; i < axis; ++i) { - outer_size *= output_shape.Dims(i); - } - // For all input arrays, - // FlatSize() = outer_size * Dims(axis) * base_inner_size; - int64_t base_inner_size = 1; - for (int i = axis + 1; i < concat_dimensions; ++i) { - base_inner_size *= output_shape.Dims(i); - } - - Scalar* output_ptr = output_data; - for (int k = 0; k < outer_size; k++) { - for (int i = 0; i < inputs_count; ++i) { - const int copy_size = input_shapes[i]->Dims(axis) * base_inner_size; - const Scalar* input_ptr = input_data[i] + k * copy_size; - memcpy(output_ptr, input_ptr, copy_size * sizeof(Scalar)); - output_ptr += copy_size; - } - } -} - -// TODO(b/174275780): The quantized implementation of concatentation isn't fully -// quantized as it takes scale as a floating point value. This should be fixed -// when optimizng this routine further. -inline void ConcatenationWithScaling(const ConcatenationParams& params, - const RuntimeShape* const* input_shapes, - const uint8_t* const* input_data, - const RuntimeShape& output_shape, - uint8_t* output_data) { - int axis = params.axis; - const int32_t* input_zeropoint = params.input_zeropoint; - const float* input_scale = params.input_scale; - int inputs_count = params.inputs_count; - const int32_t output_zeropoint = params.output_zeropoint; - const float output_scale = params.output_scale; - - const int concat_dimensions = output_shape.DimensionsCount(); - TFLITE_DCHECK_LT(axis, concat_dimensions); - - int64_t concat_size = 0; - for (int i = 0; i < inputs_count; i++) { - TFLITE_DCHECK_EQ(input_shapes[i]->DimensionsCount(), concat_dimensions); - for (int j = 0; j < concat_dimensions; j++) { - if (j != axis) { - MatchingDim(*input_shapes[i], j, output_shape, j); - } - } - concat_size += input_shapes[i]->Dims(axis); - } - TFLITE_DCHECK_EQ(concat_size, output_shape.Dims(axis)); - int64_t outer_size = 1; - for (int i = 0; i < axis; ++i) { - outer_size *= output_shape.Dims(i); - } - // For all input arrays, - // FlatSize() = outer_size * Dims(axis) * base_inner_size; - int64_t base_inner_size = 1; - for (int i = axis + 1; i < concat_dimensions; ++i) { - base_inner_size *= output_shape.Dims(i); - } - - const float inverse_output_scale = 1.f / output_scale; - uint8_t* output_ptr = output_data; - for (int k = 0; k < outer_size; k++) { - for (int i = 0; i < inputs_count; ++i) { - const int copy_size = input_shapes[i]->Dims(axis) * base_inner_size; - const uint8_t* input_ptr = input_data[i] + k * copy_size; - if (input_zeropoint[i] == output_zeropoint && - input_scale[i] == output_scale) { - memcpy(output_ptr, input_ptr, copy_size); - } else { - const float scale = input_scale[i] * inverse_output_scale; - const float bias = -input_zeropoint[i] * scale; - for (int j = 0; j < copy_size; ++j) { - const int32_t value = static_cast(tflite::TfLiteRound( - input_ptr[j] * scale + bias)) + - output_zeropoint; - output_ptr[j] = static_cast( - std::max(std::min(255, value), 0)); - } - } - output_ptr += copy_size; - } - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_CONCATENATION_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/conv.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/conv.h deleted file mode 100644 index 3a53e06e..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/conv.h +++ /dev/null @@ -1,287 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_CONV_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_CONV_H_ - -#include - -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -namespace reference_ops { - -inline void Conv(const ConvParams& params, const RuntimeShape& input_shape, - const float* input_data, const RuntimeShape& filter_shape, - const float* filter_data, const RuntimeShape& bias_shape, - const float* bias_data, const RuntimeShape& output_shape, - float* output_data, const RuntimeShape& im2col_shape, - float* im2col_data) { - const int stride_width = params.stride_width; - const int stride_height = params.stride_height; - const int dilation_width_factor = params.dilation_width_factor; - const int dilation_height_factor = params.dilation_height_factor; - const int pad_width = params.padding_values.width; - const int pad_height = params.padding_values.height; - const float output_activation_min = params.float_activation_min; - const float output_activation_max = params.float_activation_max; - TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(filter_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4); - - (void)im2col_data; // only used in optimized code. - (void)im2col_shape; // only used in optimized code. - const int batches = MatchingDim(input_shape, 0, output_shape, 0); - const int input_depth = input_shape.Dims(3); - const int output_depth = MatchingDim(filter_shape, 0, output_shape, 3); - if (bias_data) { - TFLITE_DCHECK_EQ(bias_shape.FlatSize(), output_depth); - } - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - const int filter_height = filter_shape.Dims(1); - const int filter_width = filter_shape.Dims(2); - const int filter_input_depth = filter_shape.Dims(3); - const int groups = input_depth / filter_input_depth; - TFLITE_DCHECK_EQ(input_depth % filter_input_depth, 0); - const int filters_per_group = output_depth / groups; - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - - for (int batch = 0; batch < batches; ++batch) { - for (int out_y = 0; out_y < output_height; ++out_y) { - const int in_y_origin = (out_y * stride_height) - pad_height; - for (int out_x = 0; out_x < output_width; ++out_x) { - const int in_x_origin = (out_x * stride_width) - pad_width; - for (int out_channel = 0; out_channel < output_depth; ++out_channel) { - auto group = out_channel / filters_per_group; - float total = 0.f; - for (int filter_y = 0; filter_y < filter_height; ++filter_y) { - const int in_y = in_y_origin + dilation_height_factor * filter_y; - for (int filter_x = 0; filter_x < filter_width; ++filter_x) { - const int in_x = in_x_origin + dilation_width_factor * filter_x; - - // Zero padding by omitting the areas outside the image. - const bool is_point_inside_image = - (in_x >= 0) && (in_x < input_width) && (in_y >= 0) && - (in_y < input_height); - - if (!is_point_inside_image) { - continue; - } - for (int in_channel = 0; in_channel < filter_input_depth; - ++in_channel) { - float input_value = - input_data[Offset(input_shape, batch, in_y, in_x, - in_channel + group * filter_input_depth)]; - float filter_value = filter_data[Offset( - filter_shape, out_channel, filter_y, filter_x, in_channel)]; - total += (input_value * filter_value); - } - } - } - float bias_value = 0.0f; - if (bias_data) { - bias_value = bias_data[out_channel]; - } - output_data[Offset(output_shape, batch, out_y, out_x, out_channel)] = - ActivationFunctionWithMinMax(total + bias_value, - output_activation_min, - output_activation_max); - } - } - } - } -} - -inline void Conv(const ConvParams& params, const RuntimeShape& input_shape, - const uint8_t* input_data, const RuntimeShape& filter_shape, - const uint8_t* filter_data, const RuntimeShape& bias_shape, - const int32_t* bias_data, const RuntimeShape& output_shape, - uint8_t* output_data, const RuntimeShape& im2col_shape, - uint8_t* im2col_data, void* cpu_backend_context) { - (void)cpu_backend_context; // only used in optimized code. - (void)im2col_data; // only used in optimized code. - (void)im2col_shape; // only used in optimized code. - const int stride_width = params.stride_width; - const int stride_height = params.stride_height; - const int dilation_width_factor = params.dilation_width_factor; - const int dilation_height_factor = params.dilation_height_factor; - const int pad_width = params.padding_values.width; - const int pad_height = params.padding_values.height; - const int32_t input_offset = params.input_offset; - const int32_t filter_offset = params.weights_offset; - const int32_t output_offset = params.output_offset; - const int32_t output_multiplier = params.output_multiplier; - const int output_shift = params.output_shift; - const int32_t output_activation_min = params.quantized_activation_min; - const int32_t output_activation_max = params.quantized_activation_max; - TFLITE_DCHECK_LE(output_activation_min, output_activation_max); - - TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(filter_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4); - const int batches = MatchingDim(input_shape, 0, output_shape, 0); - const int input_depth = input_shape.Dims(3); - const int output_depth = MatchingDim(filter_shape, 0, output_shape, 3); - if (bias_data) { - TFLITE_DCHECK_EQ(bias_shape.FlatSize(), output_depth); - } - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - const int filter_height = filter_shape.Dims(1); - const int filter_width = filter_shape.Dims(2); - const int filter_input_depth = filter_shape.Dims(3); - const int groups = input_depth / filter_input_depth; - TFLITE_DCHECK_EQ(input_depth % filter_input_depth, 0); - const int filters_per_group = output_depth / groups; - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - for (int batch = 0; batch < batches; ++batch) { - for (int out_y = 0; out_y < output_height; ++out_y) { - const int in_y_origin = (out_y * stride_height) - pad_height; - for (int out_x = 0; out_x < output_width; ++out_x) { - const int in_x_origin = (out_x * stride_width) - pad_width; - for (int out_channel = 0; out_channel < output_depth; ++out_channel) { - auto group = out_channel / filters_per_group; - int32_t acc = 0; - for (int filter_y = 0; filter_y < filter_height; ++filter_y) { - const int in_y = in_y_origin + dilation_height_factor * filter_y; - for (int filter_x = 0; filter_x < filter_width; ++filter_x) { - const int in_x = in_x_origin + dilation_width_factor * filter_x; - - // Zero padding by omitting the areas outside the image. - const bool is_point_inside_image = - (in_x >= 0) && (in_x < input_width) && (in_y >= 0) && - (in_y < input_height); - - if (!is_point_inside_image) { - continue; - } - - for (int in_channel = 0; in_channel < filter_input_depth; - ++in_channel) { - int32_t input_val = - input_data[Offset(input_shape, batch, in_y, in_x, - in_channel + group * filter_input_depth)]; - int32_t filter_val = filter_data[Offset( - filter_shape, out_channel, filter_y, filter_x, in_channel)]; - acc += - (filter_val + filter_offset) * (input_val + input_offset); - } - } - } - if (bias_data) { - acc += bias_data[out_channel]; - } - acc = MultiplyByQuantizedMultiplier(acc, output_multiplier, - output_shift); - acc += output_offset; - acc = std::max(acc, output_activation_min); - acc = std::min(acc, output_activation_max); - output_data[Offset(output_shape, batch, out_y, out_x, out_channel)] = - static_cast(acc); - } - } - } - } -} - -inline void HybridConvPerChannel( - const ConvParams& params, float* scaling_factors_ptr, - const RuntimeShape& input_shape, const int8_t* input_data, - const RuntimeShape& filter_shape, const int8_t* filter_data, - const RuntimeShape& bias_shape, const float* bias_data, - const RuntimeShape& output_shape, float* output_data, - const RuntimeShape& im2col_shape, int8_t* im2col_data, - const float* per_channel_scale, int32_t* input_offset) { - (void)im2col_data; // only used in optimized code. - (void)im2col_shape; // only used in optimized code. - const int stride_width = params.stride_width; - const int stride_height = params.stride_height; - const int dilation_width_factor = params.dilation_width_factor; - const int dilation_height_factor = params.dilation_height_factor; - const int pad_width = params.padding_values.width; - const int pad_height = params.padding_values.height; - const float output_activation_min = params.float_activation_min; - const float output_activation_max = params.float_activation_max; - TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(filter_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4); - const int batches = MatchingDim(input_shape, 0, output_shape, 0); - const int input_depth = input_shape.Dims(3); - const int output_depth = MatchingDim(filter_shape, 0, output_shape, 3); - if (bias_data) { - TFLITE_DCHECK_EQ(bias_shape.FlatSize(), output_depth); - } - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - const int filter_height = filter_shape.Dims(1); - const int filter_width = filter_shape.Dims(2); - const int filter_input_depth = filter_shape.Dims(3); - const int groups = input_depth / filter_input_depth; - TFLITE_DCHECK_EQ(input_depth % filter_input_depth, 0); - const int filters_per_group = output_depth / groups; - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - for (int batch = 0; batch < batches; ++batch) { - for (int out_y = 0; out_y < output_height; ++out_y) { - for (int out_x = 0; out_x < output_width; ++out_x) { - for (int out_channel = 0; out_channel < output_depth; ++out_channel) { - auto group = out_channel / filters_per_group; - const int in_x_origin = (out_x * stride_width) - pad_width; - const int in_y_origin = (out_y * stride_height) - pad_height; - int32_t acc = 0; - for (int filter_y = 0; filter_y < filter_height; ++filter_y) { - for (int filter_x = 0; filter_x < filter_width; ++filter_x) { - for (int in_channel = 0; in_channel < filter_input_depth; - ++in_channel) { - const int in_x = in_x_origin + dilation_width_factor * filter_x; - const int in_y = - in_y_origin + dilation_height_factor * filter_y; - // If the location is outside the bounds of the input image, - // use zero as a default value. - if ((in_x >= 0) && (in_x < input_width) && (in_y >= 0) && - (in_y < input_height)) { - int32_t input_val = input_data[Offset( - input_shape, batch, in_y, in_x, - in_channel + group * filter_input_depth)]; - int32_t filter_val = - filter_data[Offset(filter_shape, out_channel, filter_y, - filter_x, in_channel)]; - acc += filter_val * (input_val - input_offset[batch]); - } - } - } - } - float acc_float = - acc * per_channel_scale[out_channel] * scaling_factors_ptr[batch]; - if (bias_data) { - acc_float += bias_data[out_channel]; - } - output_data[Offset(output_shape, batch, out_y, out_x, out_channel)] = - ActivationFunctionWithMinMax(acc_float, output_activation_min, - output_activation_max); - } - } - } - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_CONV_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/cumsum.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/cumsum.h deleted file mode 100644 index 7cbc87c0..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/cumsum.h +++ /dev/null @@ -1,175 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_CUMSUM_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_CUMSUM_H_ - -#include -#include -#include - -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" - -namespace tflite { -namespace reference_ops { - -template -inline void CumSum(const T* input_data, const RuntimeShape& shape, int32_t axis, - bool exclusive, bool reverse, T* output_data) { - const int32_t rank = shape.DimensionsCount(); - TFLITE_DCHECK_GE(rank, 1); - TFLITE_DCHECK_GE(axis, 0); - TFLITE_DCHECK_LT(axis, rank); - - size_t inner = 1; - size_t outer = 1; - size_t depth = 1; - for (int32_t i = 0; i < rank; i++) { - if (i < axis) - inner *= shape.Dims(i); - else if (i > axis) - outer *= shape.Dims(i); - else - depth = shape.Dims(i); - } - - for (size_t outer_index = 0; outer_index < outer; outer_index++) { - size_t outer_index_adj; - if (reverse) - outer_index_adj = (outer - 1) - outer_index; - else - outer_index_adj = outer_index; - for (size_t inner_index = 0; inner_index < inner; inner_index++) { - T accumulator = 0; - size_t inner_index_adj; - if (reverse) - inner_index_adj = (inner - 1) - inner_index; - else - inner_index_adj = inner_index; - for (size_t depth_index = 0; depth_index < depth; depth_index++) { - size_t depth_index_adj; - if (reverse) - depth_index_adj = (depth - 1) - depth_index; - else - depth_index_adj = depth_index; - - size_t index = outer_index_adj; - index += inner_index_adj * depth * outer; - index += depth_index_adj * outer; - - if (exclusive) { - output_data[index] = accumulator; - accumulator += input_data[index]; - } else { - accumulator += input_data[index]; - output_data[index] = accumulator; - } - } - } - } -} - -// -// Quantized INT8 CUMSUM -// -inline void CumSum(const ArithmeticParams& params, const int8_t* input_data, - const RuntimeShape& shape, int32_t axis, bool exclusive, - bool reverse, int8_t* output_data) { - TFLITE_DCHECK_LE(params.quantized_activation_min, - params.quantized_activation_max); - // Input offset is negative input zero point. Activation tensors are - // asymmetric quantized so they span the full int8 range. - // All inputs should have same zero-point and scale, this is checked during - // Prepare stage. - TFLITE_DCHECK_GE(-params.input1_offset, std::numeric_limits::min()); - TFLITE_DCHECK_LE(-params.input1_offset, std::numeric_limits::max()); - - const int32_t rank = shape.DimensionsCount(); - TFLITE_DCHECK_GE(rank, 1); - TFLITE_DCHECK_GE(axis, 0); - TFLITE_DCHECK_LT(axis, rank); - - size_t inner = 1; - size_t outer = 1; - size_t depth = 1; - for (int32_t i = 0; i < rank; i++) { - if (i < axis) - inner *= shape.Dims(i); - else if (i > axis) - outer *= shape.Dims(i); - else - depth = shape.Dims(i); - } - - for (size_t outer_index = 0; outer_index < outer; outer_index++) { - size_t outer_index_adj; - if (reverse) - outer_index_adj = (outer - 1) - outer_index; - else - outer_index_adj = outer_index; - for (size_t inner_index = 0; inner_index < inner; inner_index++) { - int32_t accumulator = params.input1_offset; // accumulator = 0 - accumulator *= (1 << params.left_shift); - accumulator = MultiplyByQuantizedMultiplierSmallerThanOneExp( - accumulator, params.input1_multiplier, params.input1_shift); - - size_t inner_index_adj; - if (reverse) - inner_index_adj = (inner - 1) - inner_index; - else - inner_index_adj = inner_index; - - for (size_t depth_index = 0; depth_index < depth; depth_index++) { - size_t depth_index_adj; - if (reverse) - depth_index_adj = (depth - 1) - depth_index; - else - depth_index_adj = depth_index; - - size_t index = outer_index_adj; - index += inner_index_adj * depth * outer; - index += depth_index_adj * outer; - - const int32_t y = params.input1_offset + input_data[index]; - const int32_t shifted_y = y * (1 << params.left_shift); - const int32_t scaled_y = MultiplyByQuantizedMultiplierSmallerThanOneExp( - shifted_y, params.input1_multiplier, params.input1_shift); - - int32_t scaled_output; - if (exclusive) { - scaled_output = accumulator; - accumulator += scaled_y; - } else { - accumulator += scaled_y; - scaled_output = accumulator; - } - - const int32_t raw_output = - MultiplyByQuantizedMultiplierSmallerThanOneExp( - scaled_output, params.output_multiplier, params.output_shift) + - params.output_offset; - const int32_t clamped_output = - std::min(params.quantized_activation_max, - std::max(params.quantized_activation_min, raw_output)); - output_data[index] = static_cast(clamped_output); - } - } - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_CUMSUM_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/depth_to_space.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/depth_to_space.h deleted file mode 100644 index 23cff285..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/depth_to_space.h +++ /dev/null @@ -1,79 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_DEPTH_TO_SPACE_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_DEPTH_TO_SPACE_H_ - -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { -namespace reference_ops { - -template -inline void DepthToSpace(const tflite::DepthToSpaceParams& op_params, - const RuntimeShape& unextended_input_shape, - const T* input_data, - const RuntimeShape& unextended_output_shape, - T* output_data) { - TFLITE_DCHECK_LE(unextended_input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(unextended_output_shape.DimensionsCount(), 4); - const RuntimeShape input_shape = - RuntimeShape::ExtendedShape(4, unextended_input_shape); - const RuntimeShape output_shape = - RuntimeShape::ExtendedShape(4, unextended_output_shape); - - const int input_depth = input_shape.Dims(3); - const int input_width = input_shape.Dims(2); - const int input_height = input_shape.Dims(1); - const int input_batch = input_shape.Dims(0); - - const int output_depth = output_shape.Dims(3); - const int output_width = output_shape.Dims(2); - const int output_height = output_shape.Dims(1); - const int output_batch = output_shape.Dims(0); - - const int32_t block_size = op_params.block_size; - - TFLITE_DCHECK_EQ(input_width * block_size, output_width); - TFLITE_DCHECK_EQ(input_height * block_size, output_height); - TFLITE_DCHECK_EQ(input_depth, output_depth * block_size * block_size); - TFLITE_DCHECK_EQ(input_batch, output_batch); - - for (int out_b = 0; out_b < output_batch; ++out_b) { - for (int out_h = 0; out_h < output_height; ++out_h) { - for (int out_w = 0; out_w < output_width; ++out_w) { - for (int out_d = 0; out_d < output_depth; ++out_d) { - const int in_d = - out_d + ((out_h % block_size) * block_size + out_w % block_size) * - output_depth; - - const int in_w = out_w / block_size; - const int in_h = out_h / block_size; - const int in_b = out_b; - - const int input_index = Offset(input_shape, in_b, in_h, in_w, in_d); - const int output_index = - Offset(output_shape, out_b, out_h, out_w, out_d); - - output_data[output_index] = input_data[input_index]; - } - } - } - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_DEPTH_TO_SPACE_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/depthwiseconv_float.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/depthwiseconv_float.h deleted file mode 100644 index 0cecb16b..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/depthwiseconv_float.h +++ /dev/null @@ -1,100 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_DEPTHWISECONV_FLOAT_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_DEPTHWISECONV_FLOAT_H_ - -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { -namespace reference_ops { - -inline void DepthwiseConv( - const DepthwiseParams& params, const RuntimeShape& input_shape, - const float* input_data, const RuntimeShape& filter_shape, - const float* filter_data, const RuntimeShape& bias_shape, - const float* bias_data, const RuntimeShape& output_shape, - float* output_data) { - const int stride_width = params.stride_width; - const int stride_height = params.stride_height; - const int dilation_width_factor = params.dilation_width_factor; - const int dilation_height_factor = params.dilation_height_factor; - const int pad_width = params.padding_values.width; - const int pad_height = params.padding_values.height; - const int depth_multiplier = params.depth_multiplier; - const float output_activation_min = params.float_activation_min; - const float output_activation_max = params.float_activation_max; - TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(filter_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4); - - const int batches = MatchingDim(input_shape, 0, output_shape, 0); - const int output_depth = MatchingDim(filter_shape, 3, output_shape, 3); - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - const int input_depth = input_shape.Dims(3); - const int filter_height = filter_shape.Dims(1); - const int filter_width = filter_shape.Dims(2); - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - TFLITE_DCHECK_EQ(output_depth, input_depth * depth_multiplier); - TFLITE_DCHECK_EQ(bias_shape.FlatSize(), output_depth); - - for (int b = 0; b < batches; ++b) { - for (int out_y = 0; out_y < output_height; ++out_y) { - for (int out_x = 0; out_x < output_width; ++out_x) { - for (int ic = 0; ic < input_depth; ++ic) { - for (int m = 0; m < depth_multiplier; m++) { - const int oc = m + ic * depth_multiplier; - const int in_x_origin = (out_x * stride_width) - pad_width; - const int in_y_origin = (out_y * stride_height) - pad_height; - float total = 0.f; - for (int filter_y = 0; filter_y < filter_height; ++filter_y) { - for (int filter_x = 0; filter_x < filter_width; ++filter_x) { - const int in_x = in_x_origin + dilation_width_factor * filter_x; - const int in_y = - in_y_origin + dilation_height_factor * filter_y; - // If the location is outside the bounds of the input image, - // use zero as a default value. - if ((in_x >= 0) && (in_x < input_width) && (in_y >= 0) && - (in_y < input_height)) { - float input_value = - input_data[Offset(input_shape, b, in_y, in_x, ic)]; - float filter_value = filter_data[Offset( - filter_shape, 0, filter_y, filter_x, oc)]; - total += (input_value * filter_value); - } - } - } - float bias_value = 0.0f; - if (bias_data) { - bias_value = bias_data[oc]; - } - output_data[Offset(output_shape, b, out_y, out_x, oc)] = - ActivationFunctionWithMinMax(total + bias_value, - output_activation_min, - output_activation_max); - } - } - } - } - } -} - -} // end namespace reference_ops -} // end namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_DEPTHWISECONV_FLOAT_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/depthwiseconv_uint8.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/depthwiseconv_uint8.h deleted file mode 100644 index d4fba139..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/depthwiseconv_uint8.h +++ /dev/null @@ -1,319 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_DEPTHWISECONV_UINT8_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_DEPTHWISECONV_UINT8_H_ - -#include - -#include "fixedpoint/fixedpoint.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -// Used in tests and template parameters to control which version of depthwise -// convolution is called. Primarily for reference code, and specializations -// forced in tests. -enum class DepthwiseConvImplementation { - // Run all tests against kUseStandardEntry even if also testing another - // kernel, since we need to be sure that the main DepthwiseConv() function in - // optimized_ops.h dispatches to a correctly-executing kernel. - kNone = 0, // The "default" option: use the normal - // DepthwiseConv kernel (entry) function. - kUseGenericKernel, // Forced use of generic kernel. - kUseNeon3x3, // 3x3 kernel that uses NEON when available. - kUseNeon3x3DotProduct, // 3x3 kernel that uses dot-product enabled NEON - // when available. - kUseCModel3x3DotProduct, // 3x3 kernel, reference C model that is intended - // to match overall design NEON code. - kUseUnwound3x3DotProduct, // 3x3 kernel, reference C model with unwound loops - // and some arrays. - kUseIntrinsics3x3DotProduct, // 3x3 kernel using NEON intrinsics. -}; - -// Category of depthwise convolution output rounding. -enum class DepthwiseConvOutputRounding { - kNone = 0, // Invalid: specific method must be specified. - kAwayFromZero, // Original method: exact halves rounded away from zero. - kUpward, // Halves towards +infinity: adds 0.5 before truncate. - // This is where a future kNearestEven would be placed. -}; - -// Category of depthwise convolution depth multiplication. -enum class DepthwiseConvDepthMultiplication { - kNoMultiplication = 0, // Depth multiplier = 1. - kUnitInputDepth, // Input depth = 1, output depth = depth multiplier. -}; - -namespace reference_ops { -namespace depthwise_conv { - -template -inline int32_t DepthwiseConvRound(int32_t x, int32_t quantized_multiplier, - int shift) { - TFLITE_DCHECK_NE(output_rounding, DepthwiseConvOutputRounding::kNone); - return MultiplyByQuantizedMultiplier(x, quantized_multiplier, shift); -} - -// Single-rounding MultiplyByQuantizedMultiplier -#if TFLITE_SINGLE_ROUNDING -template <> -inline int32_t DepthwiseConvRound( - int32_t x, int32_t quantized_multiplier, int shift) { - using gemmlowp::RoundingDivideByPOT; - using gemmlowp::SaturatingRoundingDoublingHighMul; - int left_shift = shift > 0 ? shift : 0; - int right_shift = shift > 0 ? 0 : -shift; - return RoundingDivideByPOT(SaturatingRoundingDoublingHighMul( - x * (1 << left_shift), quantized_multiplier), - right_shift); -} - -template <> -inline int32_t DepthwiseConvRound( - int32_t x, int32_t quantized_multiplier, int shift) { - return MultiplyByQuantizedMultiplier(x, quantized_multiplier, shift); -} -// Double-rounding MultiplyByQuantizedMultiplier -#else -template <> -inline int32_t DepthwiseConvRound( - int32_t x, int32_t quantized_multiplier, int shift) { - return MultiplyByQuantizedMultiplier(x, quantized_multiplier, shift); -} - -template <> -inline int32_t DepthwiseConvRound( - int32_t x, int32_t quantized_multiplier, int shift) { - using gemmlowp::SaturatingRoundingDoublingHighMul; - const int left_shift = shift > 0 ? shift : 0; - const int right_shift = shift > 0 ? 0 : -shift; - const int rounding_offset = right_shift > 0 ? 1 << (right_shift - 1) : 0; - return (SaturatingRoundingDoublingHighMul(x * (1 << left_shift), - quantized_multiplier) + - rounding_offset) >> - right_shift; -} -#endif // TFLITE_SINGLE_ROUNDING - -template -struct DepthwiseConvBasicKernel { - static inline void Run( - const DepthwiseParams& params, const RuntimeShape& input_shape, - const uint8_t* input_data, const RuntimeShape& filter_shape, - const uint8_t* filter_data, const RuntimeShape& bias_shape, - const int32_t* bias_data, const RuntimeShape& output_shape, - uint8_t* output_data) { - const int stride_width = params.stride_width; - const int stride_height = params.stride_height; - const int dilation_width_factor = params.dilation_width_factor; - const int dilation_height_factor = params.dilation_height_factor; - const int pad_width = params.padding_values.width; - const int pad_height = params.padding_values.height; - const int depth_multiplier = params.depth_multiplier; - const int32_t output_activation_min = params.quantized_activation_min; - const int32_t output_activation_max = params.quantized_activation_max; - const int32_t input_offset = params.input_offset; - const int32_t filter_offset = params.weights_offset; - const int32_t output_offset = params.output_offset; - const int32_t output_multiplier = params.output_multiplier; - const int output_shift = params.output_shift; - TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(filter_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4); - - TFLITE_DCHECK_LE(output_activation_min, output_activation_max); - const int batches = MatchingDim(input_shape, 0, output_shape, 0); - const int output_depth = MatchingDim(filter_shape, 3, output_shape, 3); - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - const int input_depth = input_shape.Dims(3); - const int filter_height = filter_shape.Dims(1); - const int filter_width = filter_shape.Dims(2); - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - TFLITE_DCHECK_EQ(output_depth, input_depth * depth_multiplier); - TFLITE_DCHECK_EQ(bias_shape.FlatSize(), output_depth); - - for (int b = 0; b < batches; ++b) { - for (int out_y = 0; out_y < output_height; ++out_y) { - for (int out_x = 0; out_x < output_width; ++out_x) { - for (int ic = 0; ic < input_depth; ++ic) { - for (int m = 0; m < depth_multiplier; m++) { - const int oc = m + ic * depth_multiplier; - const int in_x_origin = (out_x * stride_width) - pad_width; - const int in_y_origin = (out_y * stride_height) - pad_height; - int32_t acc = 0; - for (int filter_y = 0; filter_y < filter_height; ++filter_y) { - for (int filter_x = 0; filter_x < filter_width; ++filter_x) { - const int in_x = - in_x_origin + dilation_width_factor * filter_x; - const int in_y = - in_y_origin + dilation_height_factor * filter_y; - // If the location is outside the bounds of the input image, - // use zero as a default value. - if ((in_x >= 0) && (in_x < input_width) && (in_y >= 0) && - (in_y < input_height)) { - int32_t input_val = - input_data[Offset(input_shape, b, in_y, in_x, ic)]; - int32_t filter_val = filter_data[Offset( - filter_shape, 0, filter_y, filter_x, oc)]; - acc += (filter_val + filter_offset) * - (input_val + input_offset); - } - } - } - if (bias_data) { - acc += bias_data[oc]; - } - acc = DepthwiseConvRound(acc, output_multiplier, - output_shift); - acc += output_offset; - acc = std::max(acc, output_activation_min); - acc = std::min(acc, output_activation_max); - output_data[Offset(output_shape, b, out_y, out_x, oc)] = - static_cast(acc); - } - } - } - } - } - } - - // TODO(b/148596273): Reconcile reference versions, perhaps with common - // MultiplyByQuantizedMultiplier or DepthwiseConvRound function. - static inline void RunPerChannel( - const DepthwiseParams& params, const RuntimeShape& input_shape, - const int8_t* input_data, const RuntimeShape& filter_shape, - const int8_t* filter_data, const RuntimeShape& bias_shape, - const int32_t* bias_data, const RuntimeShape& output_shape, - int8_t* output_data) { - // Get parameters. - // TODO(b/141565753): Re-introduce ScopedProfilingLabel on Micro. - const int stride_width = params.stride_width; - const int stride_height = params.stride_height; - const int dilation_width_factor = params.dilation_width_factor; - const int dilation_height_factor = params.dilation_height_factor; - const int pad_width = params.padding_values.width; - const int pad_height = params.padding_values.height; - const int depth_multiplier = params.depth_multiplier; - const int32_t input_offset = params.input_offset; - const int32_t output_offset = params.output_offset; - const int32_t output_activation_min = params.quantized_activation_min; - const int32_t output_activation_max = params.quantized_activation_max; - const int32_t* output_multiplier = params.output_multiplier_per_channel; - const int32_t* output_shift = params.output_shift_per_channel; - - // Check dimensions of the tensors. - TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(filter_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4); - - TFLITE_DCHECK_LE(output_activation_min, output_activation_max); - const int batches = MatchingDim(input_shape, 0, output_shape, 0); - const int output_depth = MatchingDim(filter_shape, 3, output_shape, 3); - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - const int input_depth = input_shape.Dims(3); - const int filter_height = filter_shape.Dims(1); - const int filter_width = filter_shape.Dims(2); - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - TFLITE_DCHECK_EQ(output_depth, input_depth * depth_multiplier); - TFLITE_DCHECK_EQ(bias_shape.FlatSize(), output_depth); - - for (int batch = 0; batch < batches; ++batch) { - for (int out_y = 0; out_y < output_height; ++out_y) { - for (int out_x = 0; out_x < output_width; ++out_x) { - for (int in_channel = 0; in_channel < input_depth; ++in_channel) { - for (int m = 0; m < depth_multiplier; ++m) { - const int output_channel = m + in_channel * depth_multiplier; - const int in_x_origin = (out_x * stride_width) - pad_width; - const int in_y_origin = (out_y * stride_height) - pad_height; - int32_t acc = 0; - for (int filter_y = 0; filter_y < filter_height; ++filter_y) { - for (int filter_x = 0; filter_x < filter_width; ++filter_x) { - const int in_x = - in_x_origin + dilation_width_factor * filter_x; - const int in_y = - in_y_origin + dilation_height_factor * filter_y; - // Zero padding by omitting the areas outside the image. - const bool is_point_inside_image = - (in_x >= 0) && (in_x < input_width) && (in_y >= 0) && - (in_y < input_height); - if (is_point_inside_image) { - int32_t input_val = input_data[Offset( - input_shape, batch, in_y, in_x, in_channel)]; - int32_t filter_val = filter_data[Offset( - filter_shape, 0, filter_y, filter_x, output_channel)]; - // Accumulate with 32 bits accumulator. - // In the nudging process during model quantization, we - // force real value of 0.0 be represented by a quantized - // value. This guarantees that the input_offset is a int8_t, - // even though it is represented using int32_t. int32_t += - // int8_t - // * (int8_t - int8_t) so the highest value we can get from - // each accumulation is [-127, 127] * ([-128, 127] - - // [-128, 127]), which is [-32512, 32512]. log2(32512) - // = 14.98, which means we can accumulate at least 2^16 - // multiplications without overflow. The accumulator is - // applied to a filter so the accumulation logic will hold - // as long as the filter size (filter_y * filter_x * - // in_channel) does not exceed 2^16, which is the case in - // all the models we have seen so far. - acc += filter_val * (input_val + input_offset); - } - } - } - if (bias_data) { - acc += bias_data[output_channel]; - } - acc = DepthwiseConvRound( - acc, output_multiplier[output_channel], - output_shift[output_channel]); - acc += output_offset; - acc = std::max(acc, output_activation_min); - acc = std::min(acc, output_activation_max); - output_data[Offset(output_shape, batch, out_y, out_x, - output_channel)] = static_cast(acc); - } - } - } - } - } - } -}; - -} // namespace depthwise_conv - -inline void DepthwiseConv( - const DepthwiseParams& params, const RuntimeShape& input_shape, - const uint8_t* input_data, const RuntimeShape& filter_shape, - const uint8_t* filter_data, const RuntimeShape& bias_shape, - const int32_t* bias_data, const RuntimeShape& output_shape, - uint8_t* output_data) { - return depthwise_conv::DepthwiseConvBasicKernel< - DepthwiseConvOutputRounding::kAwayFromZero>::Run(params, input_shape, - input_data, filter_shape, - filter_data, bias_shape, - bias_data, output_shape, - output_data); -} - -} // namespace reference_ops -} // end namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_DEPTHWISECONV_UINT8_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/dequantize.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/dequantize.h deleted file mode 100644 index b90951f9..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/dequantize.h +++ /dev/null @@ -1,78 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_DEQUANTIZE_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_DEQUANTIZE_H_ - -#include - -#include - -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -namespace reference_ops { - -// Dequantizes into a float without rounding. -template -inline void Dequantize(const tflite::DequantizationParams& op_params, - const RuntimeShape& input_shape, - const InputT* input_data, - const RuntimeShape& output_shape, OutputT* output_data) { - int32_t zero_point = op_params.zero_point; - const double scale = op_params.scale; - const int flat_size = MatchingFlatSize(input_shape, output_shape); - - for (int i = 0; i < flat_size; i++) { - const int32_t val = input_data[i]; - const OutputT result = static_cast(scale * (val - zero_point)); - output_data[i] = result; - } -} - -// Dequantizes per-channel quantized tensor to float. -template -inline void PerChannelDequantize( - const tflite::PerChannelDequantizationParams& op_params, - const RuntimeShape& input_shape, const T* input_data, - const RuntimeShape& output_shape, float* output_data) { - // Ensure flat size is same. - MatchingFlatSize(input_shape, output_shape); - - const int32_t* zero_point = op_params.zero_point; - const float* scale = op_params.scale; - const int32_t quantized_dimension = op_params.quantized_dimension; - const int32_t num_dims = input_shape.DimensionsCount(); - const int32_t* dims_data = input_shape.DimsData(); - std::vector current_dim(num_dims, 0); - - do { - size_t offset = - ReducedOutputOffset(num_dims, reinterpret_cast(dims_data), - current_dim.data(), 0, nullptr); - const int channel = current_dim[quantized_dimension]; - const int32_t val = input_data[offset]; - const float result = - static_cast(scale[channel] * (val - zero_point[channel])); - output_data[offset] = result; - } while (NextIndex(num_dims, reinterpret_cast(dims_data), - current_dim.data())); -} - -} // namespace reference_ops - -} // namespace tflite -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_DEQUANTIZE_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/div.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/div.h deleted file mode 100644 index df8da1b1..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/div.h +++ /dev/null @@ -1,247 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_DIV_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_DIV_H_ - -#include - -#include "tensorflow/lite/kernels/internal/common.h" - -namespace tflite { - -namespace reference_ops { - -template -inline void DivCheckArithmeticParams(const ArithmeticParams& params) { - TFLITE_DCHECK_LE(params.quantized_activation_min, - params.quantized_activation_max); - // Input offset is negative input zero point. Activation tensors are - // asymmetric quantized so they span the full int8 range. - constexpr int32_t max_value = - static_cast(std::numeric_limits::max()); - TFLITE_DCHECK_GE(params.input1_offset, -max_value); - TFLITE_DCHECK_LE(params.input1_offset, max_value); - TFLITE_DCHECK_GE(params.input2_offset, -max_value); - TFLITE_DCHECK_LE(params.input2_offset, max_value); - TFLITE_DCHECK_GE(params.output_offset, -max_value); - TFLITE_DCHECK_LE(params.output_offset, max_value); -} - -// Element-wise div that can often be used for inner loop of broadcast Div as -// well as the non-broadcast Div. -template -inline void DivElementwise(int size, const ArithmeticParams& params, - const T* input1_data, const T* input2_data, - T* output_data) { - DivCheckArithmeticParams(params); - - for (int i = 0; i < size; ++i) { - int32_t input1_val = params.input1_offset + input1_data[i]; - int32_t input2_val = params.input2_offset + input2_data[i]; - TFLITE_DCHECK_NE(input2_val, 0); - if (input2_val < 0) { - // Invert signs to avoid a negative input2_val as input2_inv needs to be - // positive to be used as multiplier of MultiplyByQuantizedMultiplier. - input1_val = -input1_val; - input2_val = -input2_val; - } - int recip_shift; - const int32_t input2_inv = GetReciprocal(input2_val, 31, &recip_shift); - const int headroom = CountLeadingSignBits(input1_val); - const int32_t unscaled_quotient = - MultiplyByQuantizedMultiplierGreaterThanOne(input1_val, input2_inv, - headroom); - const int total_shift = params.output_shift - recip_shift - headroom; - const int32_t unclamped_result = - params.output_offset + - MultiplyByQuantizedMultiplierSmallerThanOneExp( - unscaled_quotient, params.output_multiplier, total_shift); - const int32_t clamped_output = - std::min(params.quantized_activation_max, - std::max(params.quantized_activation_min, unclamped_result)); - output_data[i] = static_cast(clamped_output); - } -} - -inline void Div(const ArithmeticParams& params, - const RuntimeShape& input1_shape, const uint8_t* input1_data, - const RuntimeShape& input2_shape, const uint8_t* input2_data, - const RuntimeShape& output_shape, uint8_t* output_data) { - TFLITE_DCHECK_LE(params.quantized_activation_min, - params.quantized_activation_max); - const int flat_size = - MatchingElementsSize(input1_shape, input2_shape, output_shape); - - DivElementwise(flat_size, params, input1_data, input2_data, output_data); -} - -inline void Div(const ArithmeticParams& params, - const RuntimeShape& input1_shape, const int8_t* input1_data, - const RuntimeShape& input2_shape, const int8_t* input2_data, - const RuntimeShape& output_shape, int8_t* output_data) { - TFLITE_DCHECK_LE(params.quantized_activation_min, - params.quantized_activation_max); - const int flat_size = - MatchingElementsSize(input1_shape, input2_shape, output_shape); - - DivElementwise(flat_size, params, input1_data, input2_data, output_data); -} - -template -inline void BroadcastDivSlowQuantized( - const ArithmeticParams& params, const RuntimeShape& unextended_input1_shape, - const T* input1_data, const RuntimeShape& unextended_input2_shape, - const T* input2_data, const RuntimeShape& unextended_output_shape, - T* output_data) { - TFLITE_DCHECK_LE(unextended_input1_shape.DimensionsCount(), N); - TFLITE_DCHECK_LE(unextended_input2_shape.DimensionsCount(), N); - TFLITE_DCHECK_LE(unextended_output_shape.DimensionsCount(), N); - - NdArrayDesc desc1; - NdArrayDesc desc2; - NdArrayDesc output_desc; - NdArrayDescsForElementwiseBroadcast(unextended_input1_shape, - unextended_input2_shape, &desc1, &desc2); - CopyDimsToDesc(RuntimeShape::ExtendedShape(N, unextended_output_shape), - &output_desc); - - DivCheckArithmeticParams(params); - - auto div_func = [&](int indexes[N]) { - int32_t input1_val = - params.input1_offset + input1_data[SubscriptToIndex(desc1, indexes)]; - int32_t input2_val = - params.input2_offset + input2_data[SubscriptToIndex(desc2, indexes)]; - TFLITE_DCHECK_NE(input2_val, 0); - if (input2_val < 0) { - // Invert signs to avoid a negative input2_val as input2_inv needs to be - // positive to be used as multiplier of MultiplyByQuantizedMultiplier. - input1_val = -input1_val; - input2_val = -input2_val; - } - int recip_shift; - const int32_t input2_inv = GetReciprocal(input2_val, 31, &recip_shift); - const int headroom = CountLeadingSignBits(input1_val); - const int32_t unscaled_quotient = - MultiplyByQuantizedMultiplierGreaterThanOne(input1_val, input2_inv, - headroom); - const int total_shift = params.output_shift - recip_shift - headroom; - const int32_t unclamped_result = - params.output_offset + - MultiplyByQuantizedMultiplierSmallerThanOneExp( - unscaled_quotient, params.output_multiplier, total_shift); - const int32_t clamped_output = - std::min(params.quantized_activation_max, - std::max(params.quantized_activation_min, unclamped_result)); - output_data[SubscriptToIndex(output_desc, indexes)] = - static_cast(clamped_output); - }; - NDOpsHelper(output_desc, div_func); -} - -template -inline void BroadcastDivSlow(const ArithmeticParams& params, - const RuntimeShape& unextended_input1_shape, - const uint8_t* input1_data, - const RuntimeShape& unextended_input2_shape, - const uint8_t* input2_data, - const RuntimeShape& unextended_output_shape, - uint8_t* output_data) { - BroadcastDivSlowQuantized( - params, unextended_input1_shape, input1_data, unextended_input2_shape, - input2_data, unextended_output_shape, output_data); -} - -template -inline void BroadcastDivSlow(const ArithmeticParams& params, - const RuntimeShape& unextended_input1_shape, - const int8_t* input1_data, - const RuntimeShape& unextended_input2_shape, - const int8_t* input2_data, - const RuntimeShape& unextended_output_shape, - int8_t* output_data) { - BroadcastDivSlowQuantized( - params, unextended_input1_shape, input1_data, unextended_input2_shape, - input2_data, unextended_output_shape, output_data); -} - -// TODO(jiawen): We can implement BroadcastDiv on buffers of arbitrary -// dimensionality if the runtime code does a single loop over one dimension -// that handles broadcasting as the base case. The code generator would then -// generate max(D1, D2) nested for loops. -template -void BroadcastDivSlow(const ArithmeticParams& params, - const RuntimeShape& unextended_input1_shape, - const T* input1_data, - const RuntimeShape& unextended_input2_shape, - const T* input2_data, - const RuntimeShape& unextended_output_shape, - T* output_data) { - T output_activation_min; - T output_activation_max; - GetActivationParams(params, &output_activation_min, &output_activation_max); - - TFLITE_DCHECK_LE(unextended_input1_shape.DimensionsCount(), N); - TFLITE_DCHECK_LE(unextended_input2_shape.DimensionsCount(), N); - TFLITE_DCHECK_LE(unextended_output_shape.DimensionsCount(), N); - - NdArrayDesc desc1; - NdArrayDesc desc2; - NdArrayDesc output_desc; - NdArrayDescsForElementwiseBroadcast(unextended_input1_shape, - unextended_input2_shape, &desc1, &desc2); - CopyDimsToDesc(RuntimeShape::ExtendedShape(N, unextended_output_shape), - &output_desc); - - // In Tensorflow, the dimensions are canonically named (batch_number, row, - // col, channel), with extents (batches, height, width, depth), with the - // trailing dimension changing most rapidly (channels has the smallest - // stride, typically 1 element). - // - // In generated C code, we store arrays with the dimensions reversed. The - // first dimension has smallest stride. - - auto div_func = [&](int indexes[N]) { - output_data[SubscriptToIndex(output_desc, indexes)] = - ActivationFunctionWithMinMax( - input1_data[SubscriptToIndex(desc1, indexes)] / - input2_data[SubscriptToIndex(desc2, indexes)], - output_activation_min, output_activation_max); - }; - NDOpsHelper(output_desc, div_func); -} - -template -inline void Div(const ArithmeticParams& params, - const RuntimeShape& input1_shape, const T* input1_data, - const RuntimeShape& input2_shape, const T* input2_data, - const RuntimeShape& output_shape, T* output_data) { - T output_activation_min; - T output_activation_max; - GetActivationParams(params, &output_activation_min, &output_activation_max); - - const int flat_size = - MatchingElementsSize(input1_shape, input2_shape, output_shape); - for (int i = 0; i < flat_size; ++i) { - output_data[i] = ActivationFunctionWithMinMax( - input1_data[i] / input2_data[i], output_activation_min, - output_activation_max); - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_DIV_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/elu.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/elu.h deleted file mode 100644 index 3dc93589..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/elu.h +++ /dev/null @@ -1,37 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_ELU_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_ELU_H_ - -#include "tensorflow/lite/kernels/internal/cppmath.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -namespace reference_ops { - -inline void Elu(const RuntimeShape& input_shape, const float* input_data, - const RuntimeShape& output_shape, float* output_data) { - const int flat_size = MatchingFlatSize(input_shape, output_shape); - for (int i = 0; i < flat_size; ++i) { - const float val = input_data[i]; - output_data[i] = val < 0.0f ? TfLiteExpm1(val) : val; - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_ELU_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/exp.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/exp.h deleted file mode 100644 index 134ee13f..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/exp.h +++ /dev/null @@ -1,38 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_EXP_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_EXP_H_ - -#include - -#include "ruy/profiler/instrumentation.h" // from @ruy -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { -namespace reference_ops { - -template -inline void Exp(const T* input_data, const size_t num_elements, - T* output_data) { - ruy::profiler::ScopeLabel label("Exp"); - for (size_t idx = 0; idx < num_elements; ++idx) { - output_data[idx] = std::exp(input_data[idx]); - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_EXP_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/fill.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/fill.h deleted file mode 100644 index 16630e61..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/fill.h +++ /dev/null @@ -1,38 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FILL_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FILL_H_ - -#include - -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { -namespace reference_ops { - -template -void Fill(const RuntimeShape& value_shape, const T* value_data, - const RuntimeShape& output_shape, T* output_data) { - TFLITE_DCHECK_EQ(value_shape.DimensionsCount(), 0); - const int flat_size = output_shape.FlatSize(); - for (int i = 0; i < flat_size; ++i) { - output_data[i] = *value_data; - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FILL_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/floor.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/floor.h deleted file mode 100644 index 0693fd42..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/floor.h +++ /dev/null @@ -1,39 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FLOOR_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FLOOR_H_ - -#include - -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -namespace reference_ops { - -inline void Floor(const RuntimeShape& input_shape, const float* input_data, - const RuntimeShape& output_shape, float* output_data) { - const int flat_size = MatchingFlatSize(input_shape, output_shape); - - for (int i = 0; i < flat_size; i++) { - int offset = i; - output_data[offset] = std::floor(input_data[offset]); - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FLOOR_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/floor_div.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/floor_div.h deleted file mode 100644 index e75d473c..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/floor_div.h +++ /dev/null @@ -1,35 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FLOOR_DIV_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FLOOR_DIV_H_ - -#include -#include - -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { -namespace reference_ops { - -template -T FloorDiv(T input1, T input2) { - return std::floor(std::divides()(static_cast(input1), - static_cast(input2))); -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FLOOR_DIV_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/floor_mod.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/floor_mod.h deleted file mode 100644 index 20ce18b7..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/floor_mod.h +++ /dev/null @@ -1,44 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FLOOR_MOD_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FLOOR_MOD_H_ - -#include -#include - -namespace tflite { - -namespace reference_ops { - -template -T FloorMod(T input1, T input2) { - struct FloatMod { - float operator()(const float lhs, const float rhs) const { - return std::fmod(lhs, rhs); - } - }; - using ModFunc = typename std::conditional::value, - std::modulus, FloatMod>::type; - ModFunc mod_func; - T trunc_mod = mod_func(input1, input2); - return (trunc_mod != 0) && ((input2 < 0) != (trunc_mod < 0)) - ? (trunc_mod + input2) - : trunc_mod; -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FLOOR_MOD_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/fully_connected.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/fully_connected.h deleted file mode 100644 index ba51cbcf..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/fully_connected.h +++ /dev/null @@ -1,323 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FULLY_CONNECTED_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FULLY_CONNECTED_H_ - -#include - -#include "ruy/profiler/instrumentation.h" // from @ruy -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/cppmath.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { -namespace reference_ops { - -inline void FullyConnected( - const FullyConnectedParams& params, const RuntimeShape& input_shape, - const float* input_data, const RuntimeShape& weights_shape, - const float* weights_data, const RuntimeShape& bias_shape, - const float* bias_data, const RuntimeShape& output_shape, - float* output_data) { - const float output_activation_min = params.float_activation_min; - const float output_activation_max = params.float_activation_max; - // TODO(b/62193649): This really should be: - // const int batches = ArraySize(output_dims, 1); - // but the current --variable_batch hack consists in overwriting the 3rd - // dimension with the runtime batch size, as we don't keep track for each - // array of which dimension is the batch dimension in it. - const int output_dims_count = output_shape.DimensionsCount(); - const int weights_dims_count = weights_shape.DimensionsCount(); - const int batches = FlatSizeSkipDim(output_shape, output_dims_count - 1); - const int output_depth = MatchingDim(weights_shape, weights_dims_count - 2, - output_shape, output_dims_count - 1); - const int accum_depth = weights_shape.Dims(weights_dims_count - 1); - for (int b = 0; b < batches; ++b) { - for (int out_c = 0; out_c < output_depth; ++out_c) { - float total = 0.f; - for (int d = 0; d < accum_depth; ++d) { - total += input_data[b * accum_depth + d] * - weights_data[out_c * accum_depth + d]; - } - float bias_value = 0.0f; - if (bias_data) { - bias_value = bias_data[out_c]; - } - output_data[out_c + output_depth * b] = ActivationFunctionWithMinMax( - total + bias_value, output_activation_min, output_activation_max); - } - } -} - -inline void FullyConnected( - const FullyConnectedParams& params, const RuntimeShape& input_shape, - const uint8_t* input_data, const RuntimeShape& filter_shape, - const uint8_t* filter_data, const RuntimeShape& bias_shape, - const int32_t* bias_data, const RuntimeShape& output_shape, - uint8_t* output_data) { - const int32_t input_offset = params.input_offset; - const int32_t filter_offset = params.weights_offset; - const int32_t output_offset = params.output_offset; - const int32_t output_multiplier = params.output_multiplier; - const int output_shift = params.output_shift; - const int32_t output_activation_min = params.quantized_activation_min; - const int32_t output_activation_max = params.quantized_activation_max; - TFLITE_DCHECK_GE(filter_shape.DimensionsCount(), 2); - TFLITE_DCHECK_GE(output_shape.DimensionsCount(), 1); - - TFLITE_DCHECK_LE(output_activation_min, output_activation_max); - // TODO(b/62193649): This really should be: - // const int batches = ArraySize(output_dims, 1); - // but the current --variable_batch hack consists in overwriting the 3rd - // dimension with the runtime batch size, as we don't keep track for each - // array of which dimension is the batch dimension in it. - const int output_dim_count = output_shape.DimensionsCount(); - const int filter_dim_count = filter_shape.DimensionsCount(); - const int batches = FlatSizeSkipDim(output_shape, output_dim_count - 1); - const int output_depth = MatchingDim(filter_shape, filter_dim_count - 2, - output_shape, output_dim_count - 1); - const int accum_depth = filter_shape.Dims(filter_dim_count - 1); - for (int b = 0; b < batches; ++b) { - for (int out_c = 0; out_c < output_depth; ++out_c) { - int32_t acc = 0; - for (int d = 0; d < accum_depth; ++d) { - int32_t input_val = input_data[b * accum_depth + d]; - int32_t filter_val = filter_data[out_c * accum_depth + d]; - acc += (filter_val + filter_offset) * (input_val + input_offset); - } - if (bias_data) { - acc += bias_data[out_c]; - } - acc = MultiplyByQuantizedMultiplier(acc, output_multiplier, output_shift); - acc += output_offset; - acc = std::max(acc, output_activation_min); - acc = std::min(acc, output_activation_max); - output_data[out_c + output_depth * b] = static_cast(acc); - } - } -} - -inline void FullyConnected( - const FullyConnectedParams& params, const RuntimeShape& input_shape, - const uint8_t* input_data, const RuntimeShape& filter_shape, - const uint8_t* filter_data, const RuntimeShape& bias_shape, - const int32_t* bias_data, const RuntimeShape& output_shape, - int16_t* output_data) { - const int32_t input_offset = params.input_offset; - const int32_t filter_offset = params.weights_offset; - const int32_t output_offset = params.output_offset; - const int32_t output_multiplier = params.output_multiplier; - const int output_shift = params.output_shift; - const int32_t output_activation_min = params.quantized_activation_min; - const int32_t output_activation_max = params.quantized_activation_max; - - TFLITE_DCHECK_LE(output_activation_min, output_activation_max); - TFLITE_DCHECK_EQ(output_offset, 0); - // TODO(b/62193649): This really should be: - // const int batches = ArraySize(output_dims, 1); - // but the current --variable_batch hack consists in overwriting the 3rd - // dimension with the runtime batch size, as we don't keep track for each - // array of which dimension is the batch dimension in it. - const int output_dim_count = output_shape.DimensionsCount(); - const int filter_dim_count = filter_shape.DimensionsCount(); - const int batches = FlatSizeSkipDim(output_shape, output_dim_count - 1); - const int output_depth = MatchingDim(filter_shape, filter_dim_count - 2, - output_shape, output_dim_count - 1); - const int accum_depth = filter_shape.Dims(filter_dim_count - 1); - for (int b = 0; b < batches; ++b) { - for (int out_c = 0; out_c < output_depth; ++out_c) { - // Internal accumulation. - // Initialize accumulator with the bias-value. - int32_t accum = bias_data[out_c]; - // Accumulation loop. - for (int d = 0; d < accum_depth; ++d) { - int16_t input_val = input_data[b * accum_depth + d] + input_offset; - int16_t filter_val = - filter_data[out_c * accum_depth + d] + filter_offset; - accum += filter_val * input_val; - } - // Down-scale the final int32_t accumulator to the scale used by our - // (16-bit, typically 3 integer bits) fixed-point format. The quantized - // multiplier and shift here have been pre-computed offline - // (e.g. by toco). - accum = - MultiplyByQuantizedMultiplier(accum, output_multiplier, output_shift); - // Saturate, cast to int16_t, and store to output array. - accum = std::max(accum, output_activation_min - output_offset); - accum = std::min(accum, output_activation_max - output_offset); - accum += output_offset; - output_data[out_c + output_depth * b] = accum; - } - } -} - -inline void ShuffledFullyConnected( - const FullyConnectedParams& params, const RuntimeShape& input_shape, - const uint8_t* input_data, const RuntimeShape& weights_shape, - const uint8_t* shuffled_weights_data, const RuntimeShape& bias_shape, - const int32_t* bias_data, const RuntimeShape& output_shape, - int16_t* output_data, uint8_t* shuffled_input_workspace_data) { - const int32_t output_multiplier = params.output_multiplier; - const int output_shift = params.output_shift; - const int32_t output_activation_min = params.quantized_activation_min; - const int32_t output_activation_max = params.quantized_activation_max; - TFLITE_DCHECK_LE(output_activation_min, output_activation_max); - - TFLITE_DCHECK_GE(input_shape.DimensionsCount(), 1); - TFLITE_DCHECK_GE(weights_shape.DimensionsCount(), 2); - TFLITE_DCHECK_GE(output_shape.DimensionsCount(), 1); - // TODO(b/62193649): This really should be: - // const int batches = ArraySize(output_dims, 1); - // but the current --variable_batch hack consists in overwriting the 3rd - // dimension with the runtime batch size, as we don't keep track for each - // array of which dimension is the batch dimension in it. - const int output_dim_count = output_shape.DimensionsCount(); - const int weights_dim_count = weights_shape.DimensionsCount(); - const int batches = FlatSizeSkipDim(output_shape, output_dim_count - 1); - const int output_depth = MatchingDim(weights_shape, weights_dim_count - 2, - output_shape, output_dim_count - 1); - const int accum_depth = weights_shape.Dims(weights_dim_count - 1); - TFLITE_DCHECK((accum_depth % 16) == 0); - TFLITE_DCHECK((output_depth % 4) == 0); - - // Shuffling and xoring of input activations into the workspace buffer - uint8_t* shuffled_input_workspace_ptr = shuffled_input_workspace_data; - if (batches == 1) { - for (int i = 0; i < accum_depth; i++) { - shuffled_input_workspace_data[i] = input_data[i] ^ 0x80; - } - } else if (batches == 4) { - for (int c = 0; c < accum_depth; c += 16) { - for (int b = 0; b < 4; b++) { - const uint8_t* src_data_ptr = input_data + b * accum_depth + c; - for (int j = 0; j < 16; j++) { - uint8_t src_val = *src_data_ptr++; - // Flip the sign bit, so that the kernel will only need to - // reinterpret these uint8_t values as int8_t, getting for free the - // subtraction of the zero_point value 128. - uint8_t dst_val = src_val ^ 0x80; - *shuffled_input_workspace_ptr++ = dst_val; - } - } - } - } else { - TFLITE_DCHECK(false); - return; - } - - // Actual computation - if (batches == 1) { - int16_t* output_ptr = output_data; - // Shuffled weights have had their sign bit (0x80) pre-flipped (xor'd) - // so that just reinterpreting them as int8_t values is equivalent to - // subtracting 128 from them, thus implementing for free the subtraction of - // the zero_point value 128. - const int8_t* shuffled_weights_ptr = - reinterpret_cast(shuffled_weights_data); - // Likewise, we preshuffled and pre-xored the input data above. - const int8_t* shuffled_input_data = - reinterpret_cast(shuffled_input_workspace_data); - for (int c = 0; c < output_depth; c += 4) { - // Internal accumulation. - // Initialize accumulator with the bias-value. - int32_t accum[4] = {0}; - // Accumulation loop. - for (int d = 0; d < accum_depth; d += 16) { - for (int i = 0; i < 4; i++) { - for (int j = 0; j < 16; j++) { - int8_t input_val = shuffled_input_data[d + j]; - int8_t weights_val = *shuffled_weights_ptr++; - accum[i] += weights_val * input_val; - } - } - } - for (int i = 0; i < 4; i++) { - // Add bias value - int32_t acc = accum[i] + bias_data[c + i]; - // Down-scale the final int32_t accumulator to the scale used by our - // (16-bit, typically 3 integer bits) fixed-point format. The quantized - // multiplier and shift here have been pre-computed offline - // (e.g. by toco). - acc = - MultiplyByQuantizedMultiplier(acc, output_multiplier, output_shift); - // Saturate, cast to int16_t, and store to output array. - acc = std::max(acc, output_activation_min); - acc = std::min(acc, output_activation_max); - output_ptr[c + i] = acc; - } - } - } else if (batches == 4) { - int16_t* output_ptr = output_data; - // Shuffled weights have had their sign bit (0x80) pre-flipped (xor'd) - // so that just reinterpreting them as int8_t values is equivalent to - // subtracting 128 from them, thus implementing for free the subtraction of - // the zero_point value 128. - const int8_t* shuffled_weights_ptr = - reinterpret_cast(shuffled_weights_data); - // Likewise, we preshuffled and pre-xored the input data above. - const int8_t* shuffled_input_data = - reinterpret_cast(shuffled_input_workspace_data); - for (int c = 0; c < output_depth; c += 4) { - const int8_t* shuffled_input_ptr = shuffled_input_data; - // Accumulation loop. - // Internal accumulation. - // Initialize accumulator with the bias-value. - int32_t accum[4][4]; - for (int i = 0; i < 4; i++) { - for (int b = 0; b < 4; b++) { - accum[i][b] = 0; - } - } - for (int d = 0; d < accum_depth; d += 16) { - for (int i = 0; i < 4; i++) { - for (int b = 0; b < 4; b++) { - for (int j = 0; j < 16; j++) { - int8_t input_val = shuffled_input_ptr[16 * b + j]; - int8_t weights_val = shuffled_weights_ptr[16 * i + j]; - accum[i][b] += weights_val * input_val; - } - } - } - shuffled_input_ptr += 64; - shuffled_weights_ptr += 64; - } - for (int i = 0; i < 4; i++) { - for (int b = 0; b < 4; b++) { - // Add bias value - int32_t acc = accum[i][b] + bias_data[c + i]; - // Down-scale the final int32_t accumulator to the scale used by our - // (16-bit, typically 3 integer bits) fixed-point format. The - // quantized multiplier and shift here have been pre-computed offline - // (e.g. by toco). - acc = MultiplyByQuantizedMultiplier(acc, output_multiplier, - output_shift); - // Saturate, cast to int16_t, and store to output array. - acc = std::max(acc, output_activation_min); - acc = std::min(acc, output_activation_max); - output_ptr[b * output_depth + c + i] = acc; - } - } - } - } else { - TFLITE_DCHECK(false); - return; - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FULLY_CONNECTED_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/hard_swish.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/hard_swish.h deleted file mode 100644 index d9fe32e9..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/hard_swish.h +++ /dev/null @@ -1,168 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_ACTIVATIONS_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_ACTIVATIONS_H_ - -#include - -#include "ruy/profiler/instrumentation.h" // from @ruy -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { -namespace reference_ops { - -inline int16_t SaturatingLeftShift(int16_t value, int amount) { - int64_t result = static_cast(value) * (1 << amount); - result = std::min(result, std::numeric_limits::max()); - result = std::max(result, std::numeric_limits::min()); - return result; -} - -// Similar to ARM instruction SQDMULH. -// Similar to gemmlowp::SaturatingRoundingDoublingHighMul except -// rounding to zero instead of to nearest (SQRDMULH). -inline std::int16_t SaturatingDoublingHighMul(std::int16_t a, std::int16_t b) { - bool overflow = a == b && a == std::numeric_limits::min(); - std::int32_t a_32(a); - std::int32_t b_32(b); - std::int32_t ab_32 = a_32 * b_32; - std::int16_t ab_x2_high16 = static_cast((ab_32) / (1 << 15)); - return overflow ? std::numeric_limits::max() : ab_x2_high16; -} - -template -inline void HardSwish(const RuntimeShape& input_shape, const T* input_data, - const RuntimeShape& output_shape, T* output_data) { - ruy::profiler::ScopeLabel label("ReferenceHardSwish/Float"); - auto matching_size = MatchingFlatSize(input_shape, output_shape); - const T* in_end = input_data + matching_size; - for (; input_data < in_end; input_data++, output_data++) { - const float in = *input_data; - *output_data = - in * std::min(static_cast(6), std::max(static_cast(0), in + 3)) / - 6; - } -} - -template -inline void HardSwish(const HardSwishParams& params, - const RuntimeShape& input_shape, const T* input_data, - const RuntimeShape& output_shape, T* output_data) { - ruy::profiler::ScopeLabel label("ReferenceHardSwish/Quantized"); - - const int flat_size = MatchingFlatSize(input_shape, output_shape); - - for (int i = 0; i < flat_size; i++) { - const int16_t input_value = input_data[i] - params.input_zero_point; - // Left-shift as much as we can without overflow/saturation to put - // significant bits in the high bits of our 16-bit fixedpoint values, so - // that fixed-point approximate computations below are as accurate as - // possible. - const int16_t input_value_on_hires_input_scale = input_value * (1 << 7); - // Compute the input value on essentially the output scale, just not - // right-shifted yet. This is the value that we'll use in the (x >= +3) - // case, and that in the general case we'll multiply against the "relu-ish" - // fixed-point multiplier in [0, 1]. - const int16_t input_value_on_preshift_output_scale = - gemmlowp::SaturatingRoundingDoublingHighMul( - input_value_on_hires_input_scale, - params.output_multiplier_fixedpoint_int16); - // Now compute the "relu-ish multiplier". In the (-3 <= x <= +3) case, that - // is just an affine rescaling of x from [-3, 3] to [0, 1]. In the general - // case, it is just that plus saturation at the boundaries of [-3, 3]. - // First, we rescale from [-3, 3] to [-1, 1], saturating. - // That is done by rescaling the input value with a fixed-point multiplier - // (reluish_multiplier_fixedpoint) and bit-shift such that we represent - // that input value on the scale where the real value 3.0f is represented - // by the quantized value 32768. (+32768 is actually not representable as - // int16_t, so this saturates at +32767, and that is seen empirically to be - // a negligible contribution to numerical error/bias). - // - // This code is careful to correctly implement any magnitude of multiplier, - // involving either a right shift or a left shift, with correct saturation - // behavior in the left-shift case. This forces this code to be more - // complicated, but is necessary for real applications: a partially - // trained quantized MobileNet v3-small model that motivated this code - // exhibits some large [min, max] range boundaries, of the order of - // magnitude of 10 or 100 depending on layers. - // - // The next few lines are basically just an ordinary - // MultiplyByQuantizedMultiplier, except that we are more careful here - // about the fine details of saturation when left-shifting, because here - // overflow in left-shift is a common case, not an anomaly as - // MultiplyByQuantizedMultiplier assumes. - int16_t reluish_value = input_value_on_hires_input_scale; - // Shift left, saturating, as much as we can while ensuring that this - // saturation will not contribute to the result. That is, left shift amount - // reduced by 1. - if (params.reluish_multiplier_exponent > 0) { - reluish_value = SaturatingLeftShift( - reluish_value, params.reluish_multiplier_exponent - 1); - } - // Apply the fixed-point multiplier, dividing the value by a divisor - // ranging in [1, 2]. - reluish_value = gemmlowp::SaturatingRoundingDoublingHighMul( - reluish_value, params.reluish_multiplier_fixedpoint_int16); - // Apply the last bit of left-shift. Thus, in the left-shifting case, if - // any saturation affects the result, it is happening here --- any - // saturation having occurred above is overwritten here, not affecting the - // result. - if (params.reluish_multiplier_exponent > 0) { - reluish_value = SaturatingLeftShift(reluish_value, 1); - } - // Shift right, in the right-shifting case. - if (params.reluish_multiplier_exponent < 0) { - reluish_value = gemmlowp::RoundingDivideByPOT( - reluish_value, -params.reluish_multiplier_exponent); - } - // At this point we have rescaled the value into a 16bit fixedpoint - // reluish_value in [-1, 1]. - // We now convert that to a 16bit fixedpoint value in [0, 1]. - reluish_value = (reluish_value + (1 << 15)) >> 1; - // Use of SaturatingDoublingHighMul here is important to cancel the biases - // from the above SaturatingRoundingDoublingHighMul. - // - // On a partially trained MobileNet-v3-small, - // - // | bias on | ImageNet - // | quantized | Top-1 - // Operation used here | values | accuracy (50k) - // --------------------------------------+------------+----------- - // SaturatingDoublingHighMul | -0.0024 | 58.920 - // SaturatingRoundingDoublingHighMul | -0.0067 | 58.064 - // - // In activations_test, this is covered by this testcase: - // QuantizedActivationsOpTest.HardSwishBias - // - const int16_t preshift_output_value = SaturatingDoublingHighMul( - reluish_value, input_value_on_preshift_output_scale); - // We were so far operating on the pre-shift output scale. Now we finally - // apply that output shift, arriving at the final output scale. - int16_t output_value = gemmlowp::RoundingDivideByPOT( - preshift_output_value, -params.output_multiplier_exponent); - output_value += params.output_zero_point; - output_value = - std::min(output_value, std::numeric_limits::max()); - output_value = - std::max(output_value, std::numeric_limits::min()); - output_data[i] = output_value; - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_CONV_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/add.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/add.h deleted file mode 100644 index 8d9b318c..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/add.h +++ /dev/null @@ -1,145 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_ADD_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_ADD_H_ - -#include -#include - -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { -namespace reference_integer_ops { - -inline void CheckArithmeticParams(const ArithmeticParams& params) { - TFLITE_DCHECK_LE(params.quantized_activation_min, - params.quantized_activation_max); - // Input offset is negative input zero point. Activation tensors are - // asymmetric quantized so they span the full int8 range. - TFLITE_DCHECK_GE(-params.input1_offset, std::numeric_limits::min()); - TFLITE_DCHECK_GE(-params.input2_offset, std::numeric_limits::min()); - TFLITE_DCHECK_LE(-params.input1_offset, std::numeric_limits::max()); - TFLITE_DCHECK_LE(-params.input2_offset, std::numeric_limits::max()); -} - -inline void ElementWise( - int size, const ArithmeticParams& params, const int8_t* input1_data, - const int8_t* input2_data, int8_t* output_data, - void (*check_arithmetic_params)(const ArithmeticParams&), - int8_t (*binary_func)(int8_t, int8_t, const ArithmeticParams&)) { - CheckArithmeticParams(params); - for (int i = 0; i < size; ++i) { - output_data[i] = binary_func(input1_data[i], input2_data[i], params); - } -} - -inline void BroadcastBinaryFunction4DSlow( - const ArithmeticParams& params, const RuntimeShape& input1_shape, - const int8_t* input1_data, const RuntimeShape& input2_shape, - const int8_t* input2_data, const RuntimeShape& output_shape, - int8_t* output_data, - void (*check_arithmetic_params)(const ArithmeticParams&), - int8_t (*binary_func)(int8_t, int8_t, const ArithmeticParams&)) { - NdArrayDesc<4> desc1; - NdArrayDesc<4> desc2; - NdArrayDescsForElementwiseBroadcast(input1_shape, input2_shape, &desc1, - &desc2); - const RuntimeShape extended_output_shape = - RuntimeShape::ExtendedShape(4, output_shape); - - // In Tensorflow, the dimensions are canonically named (batch_number, row, - // col, channel), with extents (batches, height, width, depth), with the - // trailing dimension changing most rapidly (channels has the smallest stride, - // typically 1 element). - // - // In generated C code, we store arrays with the dimensions reversed. The - // first dimension has smallest stride. - // - // We name our variables by their Tensorflow convention, but generate C code - // nesting loops such that the innermost loop has the smallest stride for the - // best cache behavior. - for (int b = 0; b < extended_output_shape.Dims(0); ++b) { - for (int y = 0; y < extended_output_shape.Dims(1); ++y) { - for (int x = 0; x < extended_output_shape.Dims(2); ++x) { - for (int c = 0; c < extended_output_shape.Dims(3); ++c) { - output_data[Offset(extended_output_shape, b, y, x, c)] = binary_func( - input1_data[SubscriptToIndex(desc1, b, y, x, c)], - input2_data[SubscriptToIndex(desc2, b, y, x, c)], params); - } - } - } - } -} - -inline int8_t AddFunc(int8_t x, int8_t y, const ArithmeticParams& params) { - const int32_t input1_val = params.input1_offset + x; - const int32_t input2_val = params.input2_offset + y; - const int32_t shifted_input1_val = input1_val * (1 << params.left_shift); - const int32_t shifted_input2_val = input2_val * (1 << params.left_shift); - const int32_t scaled_input1_val = - MultiplyByQuantizedMultiplierSmallerThanOneExp( - shifted_input1_val, params.input1_multiplier, params.input1_shift); - const int32_t scaled_input2_val = - MultiplyByQuantizedMultiplierSmallerThanOneExp( - shifted_input2_val, params.input2_multiplier, params.input2_shift); - const int32_t raw_sum = scaled_input1_val + scaled_input2_val; - const int32_t raw_output = - MultiplyByQuantizedMultiplierSmallerThanOneExp( - raw_sum, params.output_multiplier, params.output_shift) + - params.output_offset; - const int32_t clamped_output = - std::min(params.quantized_activation_max, - std::max(params.quantized_activation_min, raw_output)); - return static_cast(clamped_output); -} - -// Element-wise add that can often be used for inner loop of broadcast add as -// well as the non-broadcast add. -inline void AddElementwise(int size, const ArithmeticParams& params, - const int8_t* input1_data, const int8_t* input2_data, - int8_t* output_data) { - ElementWise(size, params, input1_data, input2_data, output_data, - CheckArithmeticParams, AddFunc); -} - -inline void Add(const ArithmeticParams& params, - const RuntimeShape& input1_shape, const int8_t* input1_data, - const RuntimeShape& input2_shape, const int8_t* input2_data, - const RuntimeShape& output_shape, int8_t* output_data) { - CheckArithmeticParams(params); - - const int flat_size = - MatchingElementsSize(input1_shape, input2_shape, output_shape); - - AddElementwise(flat_size, params, input1_data, input2_data, output_data); -} - -inline void BroadcastAdd4DSlow(const ArithmeticParams& params, - const RuntimeShape& input1_shape, - const int8_t* input1_data, - const RuntimeShape& input2_shape, - const int8_t* input2_data, - const RuntimeShape& output_shape, - int8_t* output_data) { - BroadcastBinaryFunction4DSlow(params, input1_shape, input1_data, input2_shape, - input2_data, output_shape, output_data, - CheckArithmeticParams, AddFunc); -} - -} // namespace reference_integer_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_ADD_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/conv.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/conv.h deleted file mode 100644 index ffd4978e..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/conv.h +++ /dev/null @@ -1,238 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_CONV_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_CONV_H_ - -#include - -#include "tensorflow/lite/kernels/internal/common.h" - -namespace tflite { -namespace reference_integer_ops { - -// Fixed-point per-channel-quantization convolution reference kernel. -inline void ConvPerChannel( - const ConvParams& params, const int32_t* output_multiplier, - const int32_t* output_shift, const RuntimeShape& input_shape, - const int8_t* input_data, const RuntimeShape& filter_shape, - const int8_t* filter_data, const RuntimeShape& bias_shape, - const int32_t* bias_data, const RuntimeShape& output_shape, - int8_t* output_data) { - // Get parameters. - const int32_t input_offset = params.input_offset; // r = s(q - Z) - const int stride_width = params.stride_width; - const int stride_height = params.stride_height; - const int dilation_width_factor = params.dilation_width_factor; - const int dilation_height_factor = params.dilation_height_factor; - const int pad_width = params.padding_values.width; - const int pad_height = params.padding_values.height; - const int32_t output_offset = params.output_offset; - - // Set min and max value of the output. - const int32_t output_activation_min = params.quantized_activation_min; - const int32_t output_activation_max = params.quantized_activation_max; - - // Consistency check. - TFLITE_DCHECK_LE(output_activation_min, output_activation_max); - TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(filter_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4); - const int batches = MatchingDim(input_shape, 0, output_shape, 0); - const int input_depth = input_shape.Dims(3); - const int output_depth = MatchingDim(filter_shape, 0, output_shape, 3); - if (bias_data) { - TFLITE_DCHECK_EQ(bias_shape.FlatSize(), output_depth); - } - - // Check dimensions of the tensors. - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - const int filter_height = filter_shape.Dims(1); - const int filter_width = filter_shape.Dims(2); - const int filter_input_depth = filter_shape.Dims(3); - const int groups = input_depth / filter_input_depth; - TFLITE_DCHECK_EQ(input_depth % filter_input_depth, 0); - const int filters_per_group = output_depth / groups; - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - for (int batch = 0; batch < batches; ++batch) { - for (int out_y = 0; out_y < output_height; ++out_y) { - const int in_y_origin = (out_y * stride_height) - pad_height; - for (int out_x = 0; out_x < output_width; ++out_x) { - const int in_x_origin = (out_x * stride_width) - pad_width; - for (int out_channel = 0; out_channel < output_depth; ++out_channel) { - auto group = out_channel / filters_per_group; - int32_t acc = 0; - for (int filter_y = 0; filter_y < filter_height; ++filter_y) { - const int in_y = in_y_origin + dilation_height_factor * filter_y; - for (int filter_x = 0; filter_x < filter_width; ++filter_x) { - const int in_x = in_x_origin + dilation_width_factor * filter_x; - - // Zero padding by omitting the areas outside the image. - const bool is_point_inside_image = - (in_x >= 0) && (in_x < input_width) && (in_y >= 0) && - (in_y < input_height); - - if (!is_point_inside_image) { - continue; - } - - for (int in_channel = 0; in_channel < filter_input_depth; - ++in_channel) { - int32_t input_val = - input_data[Offset(input_shape, batch, in_y, in_x, - in_channel + group * filter_input_depth)]; - int32_t filter_val = filter_data[Offset( - filter_shape, out_channel, filter_y, filter_x, in_channel)]; - // Accumulate with 32 bits accumulator. - // In the nudging process during model quantization, we force - // real value of 0.0 be represented by a quantized value. This - // guarantees that the input_offset is a int8_t, even though - // it is represented using int32_t. int32_t += int8_t * - // (int8_t - int8_t) so the highest value we can get from each - // accumulation is [-127, 127] * ([-128, 127] - - // [-128, 127]), which is [-32512, 32512]. log2(32512) - // = 14.98, which means we can accumulate at least 2^16 - // multiplications without overflow. The accumulator is - // applied to a filter so the accumulation logic will hold as - // long as the filter size (filter_y * filter_x * in_channel) - // does not exceed 2^16, which is the case in all the models - // we have seen so far. - // TODO(b/174275578): Add a check to make sure the - // accumulator depth is smaller than 2^16. - acc += filter_val * (input_val + input_offset); - } - } - } - - if (bias_data) { - acc += bias_data[out_channel]; - } - acc = MultiplyByQuantizedMultiplier( - acc, output_multiplier[out_channel], output_shift[out_channel]); - acc += output_offset; - acc = std::max(acc, output_activation_min); - acc = std::min(acc, output_activation_max); - output_data[Offset(output_shape, batch, out_y, out_x, out_channel)] = - static_cast(acc); - } - } - } - } -} - -// Fixed-point per-channel-quantization convolution reference kernel. -// 16-bit data and 8-bit filter -template -inline void ConvPerChannel( - const ConvParams& params, const int32_t* output_multiplier, - const int32_t* output_shift, const RuntimeShape& input_shape, - const int16_t* input_data, const RuntimeShape& filter_shape, - const int8_t* filter_data, const RuntimeShape& bias_shape, - const AccumScalar* bias_data, const RuntimeShape& output_shape, - int16_t* output_data) { - // Get parameters. - const int stride_width = params.stride_width; - const int stride_height = params.stride_height; - const int dilation_width_factor = params.dilation_width_factor; - const int dilation_height_factor = params.dilation_height_factor; - const int pad_width = params.padding_values.width; - const int pad_height = params.padding_values.height; - - // Set min and max value of the output. - const int32_t output_activation_min = params.quantized_activation_min; - const int32_t output_activation_max = params.quantized_activation_max; - - // Consistency check. - TFLITE_DCHECK_LE(output_activation_min, output_activation_max); - TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(filter_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4); - const int batches = MatchingDim(input_shape, 0, output_shape, 0); - const int input_depth = input_shape.Dims(3); - const int output_depth = MatchingDim(filter_shape, 0, output_shape, 3); - if (bias_data) { - TFLITE_DCHECK_EQ(bias_shape.FlatSize(), output_depth); - } - - // Check dimensions of the tensors. - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - const int filter_height = filter_shape.Dims(1); - const int filter_width = filter_shape.Dims(2); - const int filter_input_depth = filter_shape.Dims(3); - const int groups = input_depth / filter_input_depth; - TFLITE_DCHECK_EQ(input_depth % filter_input_depth, 0); - const int filters_per_group = output_depth / groups; - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - for (int batch = 0; batch < batches; ++batch) { - for (int out_y = 0; out_y < output_height; ++out_y) { - const int in_y_origin = (out_y * stride_height) - pad_height; - for (int out_x = 0; out_x < output_width; ++out_x) { - const int in_x_origin = (out_x * stride_width) - pad_width; - for (int out_channel = 0; out_channel < output_depth; ++out_channel) { - auto group = out_channel / filters_per_group; - AccumScalar acc = 0; - for (int filter_y = 0; filter_y < filter_height; ++filter_y) { - const int in_y = in_y_origin + dilation_height_factor * filter_y; - for (int filter_x = 0; filter_x < filter_width; ++filter_x) { - const int in_x = in_x_origin + dilation_width_factor * filter_x; - - // Zero padding by omitting the areas outside the image. - const bool is_point_inside_image = - (in_x >= 0) && (in_x < input_width) && (in_y >= 0) && - (in_y < input_height); - - if (!is_point_inside_image) { - continue; - } - - for (int in_channel = 0; in_channel < filter_input_depth; - ++in_channel) { - int32_t input_val = - input_data[Offset(input_shape, batch, in_y, in_x, - in_channel + group * filter_input_depth)]; - int32_t filter_val = filter_data[Offset( - filter_shape, out_channel, filter_y, filter_x, in_channel)]; - // Accumulate with 64 bits accumulator. - // int64_t += int8_t * int16_t so the highest value we can - // get from each accumulation is [-127, 127] * ([-32768, - // 32767] - - // [-32768, 32767]), which is [-8322945, 8322945]. - // log2(8322945) = 22.99. - acc += filter_val * input_val; - } - } - } - if (bias_data) { - acc += bias_data[out_channel]; - } - int32_t scaled_acc = MultiplyByQuantizedMultiplier( - acc, output_multiplier[out_channel], output_shift[out_channel]); - scaled_acc = std::max(scaled_acc, output_activation_min); - scaled_acc = std::min(scaled_acc, output_activation_max); - output_data[Offset(output_shape, batch, out_y, out_x, out_channel)] = - static_cast(scaled_acc); - } - } - } - } -} - -} // namespace reference_integer_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_CONV_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/depthwise_conv.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/depthwise_conv.h deleted file mode 100644 index 7676fce0..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/depthwise_conv.h +++ /dev/null @@ -1,291 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_DEPTHWISE_CONV_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_DEPTHWISE_CONV_H_ - -#include - -#include "tensorflow/lite/kernels/internal/common.h" - -namespace tflite { -namespace reference_integer_ops { -inline void DepthwiseConvPerChannel( - const DepthwiseParams& params, const int32_t* output_multiplier, - const int32_t* output_shift, const RuntimeShape& input_shape, - const int8_t* input_data, const RuntimeShape& filter_shape, - const int8_t* filter_data, const RuntimeShape& bias_shape, - const int32_t* bias_data, const RuntimeShape& output_shape, - int8_t* output_data) { - // Get parameters. - // TODO(b/141565753): Re-introduce ScopedProfilingLabel on Micro. - const int stride_width = params.stride_width; - const int stride_height = params.stride_height; - const int dilation_width_factor = params.dilation_width_factor; - const int dilation_height_factor = params.dilation_height_factor; - const int pad_width = params.padding_values.width; - const int pad_height = params.padding_values.height; - const int depth_multiplier = params.depth_multiplier; - const int32_t input_offset = params.input_offset; - const int32_t output_offset = params.output_offset; - const int32_t output_activation_min = params.quantized_activation_min; - const int32_t output_activation_max = params.quantized_activation_max; - - // Check dimensions of the tensors. - TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(filter_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4); - - TFLITE_DCHECK_LE(output_activation_min, output_activation_max); - const int batches = MatchingDim(input_shape, 0, output_shape, 0); - const int output_depth = MatchingDim(filter_shape, 3, output_shape, 3); - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - const int input_depth = input_shape.Dims(3); - const int filter_height = filter_shape.Dims(1); - const int filter_width = filter_shape.Dims(2); - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - TFLITE_DCHECK_EQ(output_depth, input_depth * depth_multiplier); - TFLITE_DCHECK_EQ(bias_shape.FlatSize(), output_depth); - - for (int batch = 0; batch < batches; ++batch) { - for (int out_y = 0; out_y < output_height; ++out_y) { - for (int out_x = 0; out_x < output_width; ++out_x) { - for (int in_channel = 0; in_channel < input_depth; ++in_channel) { - for (int m = 0; m < depth_multiplier; ++m) { - const int output_channel = m + in_channel * depth_multiplier; - const int in_x_origin = (out_x * stride_width) - pad_width; - const int in_y_origin = (out_y * stride_height) - pad_height; - int32_t acc = 0; - for (int filter_y = 0; filter_y < filter_height; ++filter_y) { - for (int filter_x = 0; filter_x < filter_width; ++filter_x) { - const int in_x = in_x_origin + dilation_width_factor * filter_x; - const int in_y = - in_y_origin + dilation_height_factor * filter_y; - // Zero padding by omitting the areas outside the image. - const bool is_point_inside_image = - (in_x >= 0) && (in_x < input_width) && (in_y >= 0) && - (in_y < input_height); - if (is_point_inside_image) { - int32_t input_val = input_data[Offset( - input_shape, batch, in_y, in_x, in_channel)]; - int32_t filter_val = filter_data[Offset( - filter_shape, 0, filter_y, filter_x, output_channel)]; - // Accumulate with 32 bits accumulator. - // In the nudging process during model quantization, we force - // real value of 0.0 be represented by a quantized value. This - // guarantees that the input_offset is a int8_t, even though - // it is represented using int32_t. int32_t += int8_t * - // (int8_t - int8_t) so the highest value we can get from each - // accumulation is [-127, 127] * ([-128, 127] - - // [-128, 127]), which is [-32512, 32512]. log2(32512) - // = 14.98, which means we can accumulate at least 2^16 - // multiplications without overflow. The accumulator is - // applied to a filter so the accumulation logic will hold as - // long as the filter size (filter_y * filter_x * in_channel) - // does not exceed 2^16, which is the case in all the models - // we have seen so far. - // TODO(b/174275578): Add a check to make sure the - // accumulator depth is smaller than 2^16. - acc += filter_val * (input_val + input_offset); - } - } - } - if (bias_data) { - acc += bias_data[output_channel]; - } - acc = MultiplyByQuantizedMultiplier( - acc, output_multiplier[output_channel], - output_shift[output_channel]); - acc += output_offset; - acc = std::max(acc, output_activation_min); - acc = std::min(acc, output_activation_max); - output_data[Offset(output_shape, batch, out_y, out_x, - output_channel)] = static_cast(acc); - } - } - } - } - } -} - -inline void DepthwiseConvPerChannel( - const DepthwiseParams& params, const int32_t* output_multiplier, - const int32_t* output_shift, const RuntimeShape& input_shape, - const int16_t* input_data, const RuntimeShape& filter_shape, - const int8_t* filter_data, const RuntimeShape& bias_shape, - const std::int64_t* bias_data, const RuntimeShape& output_shape, - int16_t* output_data) { - // Get parameters. - const int stride_width = params.stride_width; - const int stride_height = params.stride_height; - const int dilation_width_factor = params.dilation_width_factor; - const int dilation_height_factor = params.dilation_height_factor; - const int pad_width = params.padding_values.width; - const int pad_height = params.padding_values.height; - const int depth_multiplier = params.depth_multiplier; - const int32_t output_activation_min = params.quantized_activation_min; - const int32_t output_activation_max = params.quantized_activation_max; - - // Check dimensions of the tensors. - TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(filter_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4); - - TFLITE_DCHECK_LE(output_activation_min, output_activation_max); - const int batches = MatchingDim(input_shape, 0, output_shape, 0); - const int output_depth = MatchingDim(filter_shape, 3, output_shape, 3); - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - const int input_depth = input_shape.Dims(3); - const int filter_height = filter_shape.Dims(1); - const int filter_width = filter_shape.Dims(2); - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - TFLITE_DCHECK_EQ(output_depth, input_depth * depth_multiplier); - TFLITE_DCHECK_EQ(bias_shape.FlatSize(), output_depth); - - for (int batch = 0; batch < batches; ++batch) { - for (int out_y = 0; out_y < output_height; ++out_y) { - for (int out_x = 0; out_x < output_width; ++out_x) { - for (int in_channel = 0; in_channel < input_depth; ++in_channel) { - for (int m = 0; m < depth_multiplier; ++m) { - const int output_channel = m + in_channel * depth_multiplier; - const int in_x_origin = (out_x * stride_width) - pad_width; - const int in_y_origin = (out_y * stride_height) - pad_height; - std::int64_t acc = 0; - for (int filter_y = 0; filter_y < filter_height; ++filter_y) { - for (int filter_x = 0; filter_x < filter_width; ++filter_x) { - const int in_x = in_x_origin + dilation_width_factor * filter_x; - const int in_y = - in_y_origin + dilation_height_factor * filter_y; - // Zero padding by omitting the areas outside the image. - const bool is_point_inside_image = - (in_x >= 0) && (in_x < input_width) && (in_y >= 0) && - (in_y < input_height); - if (is_point_inside_image) { - int32_t input_val = input_data[Offset( - input_shape, batch, in_y, in_x, in_channel)]; - int32_t filter_val = filter_data[Offset( - filter_shape, 0, filter_y, filter_x, output_channel)]; - // Accumulate with 64 bits accumulator. - // We assume maximum of 2^16 accumulations as with the 8-bit - // case so actually the value in the accumulator should not - // exceed 40 bits - acc += static_cast(filter_val) * - static_cast(input_val); - } - } - } - if (bias_data) { - acc += bias_data[output_channel]; - } - int32_t scaled_acc = MultiplyByQuantizedMultiplier( - acc, output_multiplier[output_channel], - output_shift[output_channel]); - scaled_acc = std::max(scaled_acc, output_activation_min); - scaled_acc = std::min(scaled_acc, output_activation_max); - output_data[Offset(output_shape, batch, out_y, out_x, - output_channel)] = - static_cast(scaled_acc); - } - } - } - } - } -} - -inline void DepthwiseConvHybridPerChannel( - const DepthwiseParams& params, float* scaling_factors_ptr, - const RuntimeShape& input_shape, const int8_t* input_data, - const RuntimeShape& filter_shape, const int8_t* filter_data, - const RuntimeShape& bias_shape, const float* bias_data, - const RuntimeShape& output_shape, float* output_data, - const float* per_channel_scale, int32_t* input_offset) { - const int stride_width = params.stride_width; - const int stride_height = params.stride_height; - const int dilation_width_factor = params.dilation_width_factor; - const int dilation_height_factor = params.dilation_height_factor; - const int pad_width = params.padding_values.width; - const int pad_height = params.padding_values.height; - const int depth_multiplier = params.depth_multiplier; - const float output_activation_min = params.float_activation_min; - const float output_activation_max = params.float_activation_max; - // Check dimensions of the tensors. - TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(filter_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4); - - const int batches = MatchingDim(input_shape, 0, output_shape, 0); - const int output_depth = MatchingDim(filter_shape, 3, output_shape, 3); - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - const int input_depth = input_shape.Dims(3); - const int filter_height = filter_shape.Dims(1); - const int filter_width = filter_shape.Dims(2); - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - const int bias_depth = bias_shape.FlatSize(); - TFLITE_DCHECK_EQ(output_depth, input_depth * depth_multiplier); - TFLITE_DCHECK_EQ(bias_depth, output_depth); - - for (int batch = 0; batch < batches; ++batch) { - for (int out_y = 0; out_y < output_height; ++out_y) { - for (int out_x = 0; out_x < output_width; ++out_x) { - for (int in_channel = 0; in_channel < input_depth; ++in_channel) { - for (int m = 0; m < depth_multiplier; ++m) { - const int output_channel = m + in_channel * depth_multiplier; - const int in_x_origin = (out_x * stride_width) - pad_width; - const int in_y_origin = (out_y * stride_height) - pad_height; - int32_t acc = 0; - for (int filter_y = 0; filter_y < filter_height; ++filter_y) { - for (int filter_x = 0; filter_x < filter_width; ++filter_x) { - const int in_x = in_x_origin + dilation_width_factor * filter_x; - const int in_y = - in_y_origin + dilation_height_factor * filter_y; - // Zero padding by omitting the areas outside the image. - const bool is_point_inside_image = - (in_x >= 0) && (in_x < input_width) && (in_y >= 0) && - (in_y < input_height); - if (is_point_inside_image) { - int32_t input_val = input_data[Offset( - input_shape, batch, in_y, in_x, in_channel)]; - int32_t filter_val = filter_data[Offset( - filter_shape, 0, filter_y, filter_x, output_channel)]; - acc += filter_val * (input_val - input_offset[batch]); - } - } - } - float acc_float = static_cast(acc); - acc_float *= - per_channel_scale[output_channel] * scaling_factors_ptr[batch]; - if (bias_data && output_channel < bias_depth) { - acc_float += bias_data[output_channel]; - } - output_data[Offset(output_shape, batch, out_y, out_x, - output_channel)] = - ActivationFunctionWithMinMax(acc_float, output_activation_min, - output_activation_max); - } - } - } - } - } -} - -} // namespace reference_integer_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_DEPTHWISE_CONV_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/fully_connected.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/fully_connected.h deleted file mode 100644 index 634f0bff..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/fully_connected.h +++ /dev/null @@ -1,201 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_FULLY_CONNECTED_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_FULLY_CONNECTED_H_ - -#include - -#include "tensorflow/lite/kernels/internal/common.h" - -namespace tflite { -namespace reference_integer_ops { - -// For per-channel functions, since it is defined in quantization spec that -// weights are symmetric -// (https://www.tensorflow.org/lite/performance/quantization_spec#symmetric_vs_asymmetric), -// zero_point (params.weights_offset) is always 0. -// However, for per-tensor functions, params.weights_offset is still applied for -// backward compatibility. - -inline void FullyConnectedPerChannel( - const FullyConnectedParams& params, const int32_t* output_multiplier, - const int* output_shift, const RuntimeShape& input_shape, - const int8_t* input_data, const RuntimeShape& filter_shape, - const int8_t* filter_data, const RuntimeShape& bias_shape, - const int32_t* bias_data, const RuntimeShape& output_shape, - int8_t* output_data) { - const int32_t input_offset = params.input_offset; - const int32_t output_offset = params.output_offset; - const int32_t output_activation_min = params.quantized_activation_min; - const int32_t output_activation_max = params.quantized_activation_max; - TFLITE_DCHECK_GE(filter_shape.DimensionsCount(), 2); - TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 2); - - TFLITE_DCHECK_LE(output_activation_min, output_activation_max); - const int filter_dim_count = filter_shape.DimensionsCount(); - const int batches = output_shape.Dims(0); - const int output_depth = output_shape.Dims(1); - TFLITE_DCHECK_LE(output_depth, filter_shape.Dims(filter_dim_count - 2)); - const int accum_depth = filter_shape.Dims(filter_dim_count - 1); - for (int b = 0; b < batches; ++b) { - for (int out_c = 0; out_c < output_depth; ++out_c) { - int32_t acc = 0; - for (int d = 0; d < accum_depth; ++d) { - int32_t input_val = input_data[b * accum_depth + d]; - int32_t filter_val = filter_data[out_c * accum_depth + d]; - acc += filter_val * (input_val + input_offset); - } - if (bias_data) { - acc += bias_data[out_c]; - } - acc = MultiplyByQuantizedMultiplier(acc, output_multiplier[out_c], - output_shift[out_c]); - acc += output_offset; - acc = std::max(acc, output_activation_min); - acc = std::min(acc, output_activation_max); - output_data[out_c + output_depth * b] = static_cast(acc); - } - } -} - -template -inline void FullyConnectedPerChannel( - const FullyConnectedParams& params, const int32_t* output_multiplier, - const int* output_shift, const RuntimeShape& input_shape, - const int16_t* input_data, const RuntimeShape& filter_shape, - const int8_t* filter_data, const RuntimeShape& bias_shape, - const AccumScalar* bias_data, const RuntimeShape& output_shape, - int16_t* output_data) { - const int32_t output_activation_min = params.quantized_activation_min; - const int32_t output_activation_max = params.quantized_activation_max; - TFLITE_DCHECK_GE(filter_shape.DimensionsCount(), 2); - TFLITE_DCHECK_GE(output_shape.DimensionsCount(), 1); - - TFLITE_DCHECK_LE(output_activation_min, output_activation_max); - const int filter_dim_count = filter_shape.DimensionsCount(); - const int output_dim_count = output_shape.DimensionsCount(); - const int batches = FlatSizeSkipDim(output_shape, output_dim_count - 1); - const int output_depth = output_shape.Dims(output_dim_count - 1); - TFLITE_DCHECK_LE(output_depth, filter_shape.Dims(filter_dim_count - 2)); - const int accum_depth = filter_shape.Dims(filter_dim_count - 1); - for (int b = 0; b < batches; ++b) { - for (int out_c = 0; out_c < output_depth; ++out_c) { - AccumScalar acc = 0; - for (int d = 0; d < accum_depth; ++d) { - int32_t input_val = input_data[b * accum_depth + d]; - int32_t filter_val = filter_data[out_c * accum_depth + d]; - acc += filter_val * input_val; - } - if (bias_data) { - acc += bias_data[out_c]; - } - int32_t acc_scaled = MultiplyByQuantizedMultiplier( - acc, output_multiplier[out_c], output_shift[out_c]); - acc_scaled = std::max(acc_scaled, output_activation_min); - acc_scaled = std::min(acc_scaled, output_activation_max); - output_data[out_c + output_depth * b] = static_cast(acc_scaled); - } - } -} - -inline void FullyConnected( - const FullyConnectedParams& params, const RuntimeShape& input_shape, - const int8_t* input_data, const RuntimeShape& filter_shape, - const int8_t* filter_data, const RuntimeShape& bias_shape, - const int32_t* bias_data, const RuntimeShape& output_shape, - int8_t* output_data) { - const int32_t input_offset = params.input_offset; - const int32_t filter_offset = params.weights_offset; - const int32_t output_offset = params.output_offset; - const int32_t output_multiplier = params.output_multiplier; - const int output_shift = params.output_shift; - const int32_t output_activation_min = params.quantized_activation_min; - const int32_t output_activation_max = params.quantized_activation_max; - TFLITE_DCHECK_GE(filter_shape.DimensionsCount(), 2); - TFLITE_DCHECK_GE(output_shape.DimensionsCount(), 1); - - TFLITE_DCHECK_LE(output_activation_min, output_activation_max); - const int filter_dim_count = filter_shape.DimensionsCount(); - const int output_dim_count = output_shape.DimensionsCount(); - const int batches = FlatSizeSkipDim(output_shape, output_dim_count - 1); - const int output_depth = output_shape.Dims(output_dim_count - 1); - TFLITE_DCHECK_LE(output_depth, filter_shape.Dims(filter_dim_count - 2)); - const int accum_depth = filter_shape.Dims(filter_dim_count - 1); - for (int b = 0; b < batches; ++b) { - for (int out_c = 0; out_c < output_depth; ++out_c) { - int32_t acc = 0; - for (int d = 0; d < accum_depth; ++d) { - int32_t input_val = input_data[b * accum_depth + d]; - int32_t filter_val = filter_data[out_c * accum_depth + d]; - acc += (filter_val + filter_offset) * (input_val + input_offset); - } - if (bias_data) { - acc += bias_data[out_c]; - } - acc = MultiplyByQuantizedMultiplier(acc, output_multiplier, output_shift); - acc += output_offset; - acc = std::max(acc, output_activation_min); - acc = std::min(acc, output_activation_max); - output_data[out_c + output_depth * b] = static_cast(acc); - } - } -} - -template -inline void FullyConnected( - const FullyConnectedParams& params, const RuntimeShape& input_shape, - const int16_t* input_data, const RuntimeShape& filter_shape, - const int8_t* filter_data, const RuntimeShape& bias_shape, - const AccumScalar* bias_data, const RuntimeShape& output_shape, - int16_t* output_data) { - const int32_t filter_offset = params.weights_offset; - const int32_t output_multiplier = params.output_multiplier; - const int output_shift = params.output_shift; - const int32_t output_activation_min = params.quantized_activation_min; - const int32_t output_activation_max = params.quantized_activation_max; - TFLITE_DCHECK_GE(filter_shape.DimensionsCount(), 2); - TFLITE_DCHECK_GE(output_shape.DimensionsCount(), 1); - - TFLITE_DCHECK_LE(output_activation_min, output_activation_max); - const int filter_dim_count = filter_shape.DimensionsCount(); - const int output_dim_count = output_shape.DimensionsCount(); - const int batches = FlatSizeSkipDim(output_shape, output_dim_count - 1); - const int output_depth = output_shape.Dims(output_dim_count - 1); - TFLITE_DCHECK_LE(output_depth, filter_shape.Dims(filter_dim_count - 2)); - const int accum_depth = filter_shape.Dims(filter_dim_count - 1); - for (int b = 0; b < batches; ++b) { - for (int out_c = 0; out_c < output_depth; ++out_c) { - AccumScalar acc = 0; - for (int d = 0; d < accum_depth; ++d) { - int32_t input_val = input_data[b * accum_depth + d]; - int32_t filter_val = filter_data[out_c * accum_depth + d]; - acc += (filter_val + filter_offset) * input_val; - } - if (bias_data) { - acc += bias_data[out_c]; - } - int32_t acc_scaled = - MultiplyByQuantizedMultiplier(acc, output_multiplier, output_shift); - acc_scaled = std::max(acc_scaled, output_activation_min); - acc_scaled = std::min(acc_scaled, output_activation_max); - output_data[out_c + output_depth * b] = static_cast(acc_scaled); - } - } -} - -} // namespace reference_integer_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_FULLY_CONNECTED_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/l2normalization.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/l2normalization.h deleted file mode 100644 index 164a8367..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/l2normalization.h +++ /dev/null @@ -1,67 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_L2NORMALIZATION_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_L2NORMALIZATION_H_ - -#include - -#include "tensorflow/lite/kernels/internal/common.h" - -namespace tflite { -namespace reference_integer_ops { - -inline void L2Normalization(int32_t input_zero_point, int32_t outer_size, - int32_t depth, const int8_t* input_data, - int8_t* output_data) { - static constexpr int8_t kMinInt8 = std::numeric_limits::min(); - static constexpr int8_t kMaxInt8 = std::numeric_limits::max(); - // The output scale must be in sync with Prepare(). - // Output is in 1/128 scale so the actual output range is nudged from [-1, 1] - // to [-1, 127/128]. - static constexpr int32_t kOutputScale = 7; - for (int outer_index = 0; outer_index < outer_size; ++outer_index) { - // int32_t = (int8_t - int8_t) ^ 2. - // ([-128, 127] - [-128, 127]) ^ 2 = [0, (2^8 - 1)^2] so the accumulator is - // safe from overflowing in at least 2^16 steps. - int32_t acc = 0; - for (int inner_index = 0; inner_index < depth; ++inner_index) { - int32_t input = - input_data[depth * outer_index + inner_index] - input_zero_point; - acc += input * input; - } - int32_t inv_l2norm_multiplier; - int inv_l2norm_shift; - GetInvSqrtQuantizedMultiplierExp(acc, kReverseShift, &inv_l2norm_multiplier, - &inv_l2norm_shift); - - for (int inner_index = 0; inner_index < depth; ++inner_index) { - int32_t input = - input_data[depth * outer_index + inner_index] - input_zero_point; - - // Rescale and downcast. Rescale is folded into the division. - int32_t output_in_q24 = MultiplyByQuantizedMultiplier( - input, inv_l2norm_multiplier, inv_l2norm_shift + kOutputScale); - output_in_q24 = - std::min(static_cast(kMaxInt8), - std::max(static_cast(kMinInt8), output_in_q24)); - output_data[depth * outer_index + inner_index] = - static_cast(output_in_q24); - } - } -} -} // namespace reference_integer_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_L2NORMALIZATION_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/logistic.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/logistic.h deleted file mode 100644 index 16eff133..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/logistic.h +++ /dev/null @@ -1,121 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_LOGISTIC_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_LOGISTIC_H_ - -#include -#include - -#include "tensorflow/lite/kernels/internal/common.h" - -namespace tflite { -namespace reference_integer_ops { - -inline void Logistic(int32_t input_zero_point, int32_t input_range_radius, - int32_t input_multiplier, int32_t input_left_shift, - int32_t input_size, const int8_t* input_data, - int8_t* output_data) { - // Integer bits must be in sync with Prepare() function. - static constexpr int32_t kInputIntegerBits = 4; - static constexpr int32_t kOutputIntegerBits = 8; - static constexpr int8_t kMinInt8 = std::numeric_limits::min(); - static constexpr int8_t kMaxInt8 = std::numeric_limits::max(); - static constexpr int32_t kOutputZeroPoint = -128; - - for (int i = 0; i < input_size; ++i) { - const int32_t input = - static_cast(input_data[i]) - input_zero_point; - if (input <= -input_range_radius) { - output_data[i] = kMinInt8; - } else if (input >= input_range_radius) { - output_data[i] = kMaxInt8; - } else { - const int32_t input_in_q4 = MultiplyByQuantizedMultiplier( - input, input_multiplier, input_left_shift); - using FixedPoint4 = gemmlowp::FixedPoint; - const int32_t output_in_q0 = - gemmlowp::logistic(FixedPoint4::FromRaw(input_in_q4)).raw(); - - // Rescale and downcast. - using gemmlowp::RoundingDivideByPOT; - int32_t output_in_q23 = - RoundingDivideByPOT(output_in_q0, 31 - kOutputIntegerBits); - output_in_q23 = std::min(std::max(output_in_q23 + kOutputZeroPoint, - static_cast(kMinInt8)), - static_cast(kMaxInt8)); - output_data[i] = static_cast(output_in_q23); - } - } -} - -inline void Logistic(int32_t input_multiplier, int32_t input_left_shift, - int32_t input_size, const int16_t* ptr_input_data, - int16_t* ptr_output_data) { - // We use the LUT for sigmoid and take into account, that - // tanh(x) = 2*sigmoid(2*x) - 1 - - // We scale by 3/4 to expand range [-8,8]->[-10.7,10.7]. - // In case of general parameter scale, multiplier 3 is taken into account - // in TanhPrepare function and it is included in - // input_multiplier already. - - TFLITE_DCHECK_GE(input_left_shift, 0); - if (input_multiplier == 0) { // power of two case - input_multiplier = 3 << input_left_shift; - input_left_shift = 0; - } - - int32_t round = (input_left_shift > 0) ? 1 << (input_left_shift - 1) : 0; - - for (int i = 0; i < input_size; ++i, ptr_input_data++, ptr_output_data++) { - int32_t input_data = - ((*ptr_input_data) * input_multiplier + round) >> input_left_shift; - - // We do interpolation on unsigned values. - uint32_t abs_input_data = abs(input_data); - - // We divide by 2 power of 9, because - // we need to divide by 2 in power of 7 for - // the input conversion + 1/4 from the scale above. - - // Define uh as uint32_t type not to make this function overflow. - uint32_t uh = abs_input_data >> 9; - uint32_t result; - - if (uh >= 255) { - // Saturate to maximum. - result = 0x7FFF << 10; - } else { - uint32_t ua = sigmoid_table_uint16[uh]; - uint32_t ub = sigmoid_table_uint16[uh + 1]; - uint32_t ut = abs_input_data & 0x1ff; - // Interpolation is done using the fractional bit. - result = (ua << 9) + ut * (ub - ua); - } - - result = (input_data >= 0) ? (result + (1 << 9)) - : ((1 << (16 + 9)) - result + (1 << 9) - 1); - - // Back to 16-bit. - result >>= 10; - - *ptr_output_data = result; - } -} - -} // namespace reference_integer_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_LOGISTIC_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/mean.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/mean.h deleted file mode 100644 index 09d37b72..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/mean.h +++ /dev/null @@ -1,79 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_MEAN_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_MEAN_H_ - -#include - -#include "tensorflow/lite/kernels/internal/common.h" - -namespace tflite { -namespace reference_integer_ops { - -template -inline void Mean(const tflite::MeanParams& op_params, int32_t multiplier, - int32_t shift, const RuntimeShape& unextended_input_shape, - const integer_type* input_data, int32_t input_zero_point, - const RuntimeShape& unextended_output_shape, - integer_type* output_data, int32_t output_zero_point) { - // Current implementation only supports dimension equals 4 and simultaneous - // reduction over width and height. - TFLITE_CHECK_EQ(unextended_input_shape.DimensionsCount(), 4); - TFLITE_CHECK_LE(unextended_output_shape.DimensionsCount(), 4); - const RuntimeShape input_shape = - RuntimeShape::ExtendedShape(4, unextended_input_shape); - const RuntimeShape output_shape = - RuntimeShape::ExtendedShape(4, unextended_output_shape); - const int output_batch = output_shape.Dims(0); - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - const int output_depth = output_shape.Dims(3); - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - const int num_elements_in_axis = input_width * input_height; - - TFLITE_CHECK_EQ(op_params.axis_count, 2); - TFLITE_CHECK((op_params.axis[0] == 1 && op_params.axis[1] == 2) || - (op_params.axis[0] == 2 && op_params.axis[1] == 1)); - TFLITE_CHECK_EQ(output_height, 1); - TFLITE_CHECK_EQ(output_width, 1); - - static constexpr int32_t kMinInt = std::numeric_limits::min(); - static constexpr int32_t kMaxInt = std::numeric_limits::max(); - - for (int out_b = 0; out_b < output_batch; ++out_b) { - for (int out_d = 0; out_d < output_depth; ++out_d) { - int32_t acc = 0; - for (int in_h = 0; in_h < input_height; ++in_h) { - for (int in_w = 0; in_w < input_width; ++in_w) { - acc += input_data[Offset(input_shape, out_b, in_h, in_w, out_d)] - - input_zero_point; - } - } - acc = MultiplyByQuantizedMultiplier(acc, multiplier, shift); - acc = acc > 0 ? (acc + num_elements_in_axis / 2) / num_elements_in_axis - : (acc - num_elements_in_axis / 2) / num_elements_in_axis; - acc += output_zero_point; - acc = std::min(std::max(acc, kMinInt), kMaxInt); - output_data[Offset(output_shape, out_b, 0, 0, out_d)] = - static_cast(acc); - } - } -} - -} // namespace reference_integer_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_MEAN_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/mul.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/mul.h deleted file mode 100644 index 22e89740..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/mul.h +++ /dev/null @@ -1,133 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_MUL_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_MUL_H_ - -#include - -#include "fixedpoint/fixedpoint.h" -#include "ruy/profiler/instrumentation.h" // from @ruy -#include "tensorflow/lite/kernels/internal/common.h" - -namespace tflite { -namespace reference_integer_ops { - -template -inline void MulElementwise(int size, const ArithmeticParams& params, - const T* input1_data, const T* input2_data, - T* output_data) { - for (int i = 0; i < size; ++i) { - const int32_t input1_val = params.input1_offset + input1_data[i]; - const int32_t input2_val = params.input2_offset + input2_data[i]; - const int32_t unclamped_result = - params.output_offset + - MultiplyByQuantizedMultiplier(input1_val * input2_val, - params.output_multiplier, - params.output_shift); - const int32_t clamped_output = - std::min(params.quantized_activation_max, - std::max(params.quantized_activation_min, unclamped_result)); - output_data[i] = static_cast(clamped_output); - } -} - -template -inline void Mul(const ArithmeticParams& params, - const RuntimeShape& input1_shape, const T* input1_data, - const RuntimeShape& input2_shape, const T* input2_data, - const RuntimeShape& output_shape, T* output_data) { - TFLITE_DCHECK_LE(params.quantized_activation_min, - params.quantized_activation_max); - ruy::profiler::ScopeLabel label("Mul/8bit"); - const int flat_size = - MatchingElementsSize(input1_shape, input2_shape, output_shape); - - MulElementwise(flat_size, params, input1_data, input2_data, output_data); -} - -// Mul with 16 bit inputs and int8_t outputs. -inline void Mul(const ArithmeticParams& params, - const RuntimeShape& input1_shape, const int16_t* input1_data, - const RuntimeShape& input2_shape, const int16_t* input2_data, - const RuntimeShape& output_shape, int8_t* output_data) { - ruy::profiler::ScopeLabel label("Mul/Int16Int8"); - int32_t output_offset = params.output_offset; - int32_t output_activation_min = params.quantized_activation_min; - int32_t output_activation_max = params.quantized_activation_max; - TFLITE_DCHECK_LE(output_activation_min, output_activation_max); - - const int flat_size = - MatchingElementsSize(input1_shape, input2_shape, output_shape); - - for (int i = 0; i < flat_size; i++) { - // F0 uses 0 integer bits, range [-1, 1]. - using F0 = gemmlowp::FixedPoint; - - F0 unclamped_result = - F0::FromRaw(input1_data[i]) * F0::FromRaw(input2_data[i]); - int16_t rescaled_result = - gemmlowp::RoundingDivideByPOT(unclamped_result.raw(), 8); - int16_t clamped_result = std::min( - output_activation_max - output_offset, rescaled_result); - clamped_result = std::max(output_activation_min - output_offset, - clamped_result); - output_data[i] = output_offset + clamped_result; - } -} - -template -inline void BroadcastMul4DSlow( - const ArithmeticParams& params, const RuntimeShape& input1_shape, - const T* input1_data, const RuntimeShape& input2_shape, - const T* input2_data, const RuntimeShape& output_shape, T* output_data) { - ruy::profiler::ScopeLabel label("BroadcastMul4DSlow"); - - NdArrayDesc<4> desc1; - NdArrayDesc<4> desc2; - // The input shapes are extended as part of NdArrayDesc initialization. - NdArrayDescsForElementwiseBroadcast(input1_shape, input2_shape, &desc1, - &desc2); - const RuntimeShape extended_output_shape = - RuntimeShape::ExtendedShape(4, output_shape); - - for (int b = 0; b < extended_output_shape.Dims(0); ++b) { - for (int y = 0; y < extended_output_shape.Dims(1); ++y) { - for (int x = 0; x < extended_output_shape.Dims(2); ++x) { - for (int c = 0; c < extended_output_shape.Dims(3); ++c) { - const int32_t input1_val = - params.input1_offset + - input1_data[SubscriptToIndex(desc1, b, y, x, c)]; - const int32_t input2_val = - params.input2_offset + - input2_data[SubscriptToIndex(desc2, b, y, x, c)]; - const int32_t unclamped_result = - params.output_offset + - MultiplyByQuantizedMultiplier(input1_val * input2_val, - params.output_multiplier, - params.output_shift); - const int32_t clamped_output = std::min( - params.quantized_activation_max, - std::max(params.quantized_activation_min, unclamped_result)); - output_data[Offset(extended_output_shape, b, y, x, c)] = - static_cast(clamped_output); - } - } - } - } -} - -} // namespace reference_integer_ops -} // namespace tflite -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_MUL_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/pooling.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/pooling.h deleted file mode 100644 index 4dc31d9e..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/pooling.h +++ /dev/null @@ -1,264 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_POOLING_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_POOLING_H_ - -#include -#include - -#include "tensorflow/lite/kernels/internal/common.h" - -namespace tflite { -namespace reference_integer_ops { - -inline bool AveragePool(const PoolParams& params, - const RuntimeShape& input_shape, - const int8_t* input_data, - const RuntimeShape& output_shape, int8_t* output_data) { - TFLITE_DCHECK_LE(params.quantized_activation_min, - params.quantized_activation_max); - TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4); - const int batches = MatchingDim(input_shape, 0, output_shape, 0); - const int depth = MatchingDim(input_shape, 3, output_shape, 3); - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - const int stride_height = params.stride_height; - const int stride_width = params.stride_width; - for (int batch = 0; batch < batches; ++batch) { - for (int out_y = 0; out_y < output_height; ++out_y) { - for (int out_x = 0; out_x < output_width; ++out_x) { - for (int channel = 0; channel < depth; ++channel) { - const int in_x_origin = - (out_x * stride_width) - params.padding_values.width; - const int in_y_origin = - (out_y * stride_height) - params.padding_values.height; - // Compute the boundaries of the filter region clamped so as to - // ensure that the filter window fits in the input array. - const int filter_x_start = std::max(0, -in_x_origin); - const int filter_x_end = - std::min(params.filter_width, input_width - in_x_origin); - const int filter_y_start = std::max(0, -in_y_origin); - const int filter_y_end = - std::min(params.filter_height, input_height - in_y_origin); - int32_t acc = 0; - int filter_count = 0; - for (int filter_y = filter_y_start; filter_y < filter_y_end; - ++filter_y) { - for (int filter_x = filter_x_start; filter_x < filter_x_end; - ++filter_x) { - const int in_x = in_x_origin + filter_x; - const int in_y = in_y_origin + filter_y; - acc += - input_data[Offset(input_shape, batch, in_y, in_x, channel)]; - filter_count++; - } - } - if (filter_count == 0) return false; - // Round to the closest integer value. - acc = acc > 0 ? (acc + filter_count / 2) / filter_count - : (acc - filter_count / 2) / filter_count; - acc = std::max(acc, params.quantized_activation_min); - acc = std::min(acc, params.quantized_activation_max); - output_data[Offset(output_shape, batch, out_y, out_x, channel)] = - static_cast(acc); - } - } - } - } - return true; -} - -inline void MaxPool(const PoolParams& params, const RuntimeShape& input_shape, - const int8_t* input_data, const RuntimeShape& output_shape, - int8_t* output_data) { - TFLITE_DCHECK_LE(params.quantized_activation_min, - params.quantized_activation_max); - TFLITE_DCHECK_GE(params.quantized_activation_min, - std::numeric_limits::min()); - TFLITE_DCHECK_LE(params.quantized_activation_max, - std::numeric_limits::max()); - TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4); - const int batches = MatchingDim(input_shape, 0, output_shape, 0); - const int depth = MatchingDim(input_shape, 3, output_shape, 3); - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - const int stride_height = params.stride_height; - const int stride_width = params.stride_width; - for (int batch = 0; batch < batches; ++batch) { - for (int out_y = 0; out_y < output_height; ++out_y) { - for (int out_x = 0; out_x < output_width; ++out_x) { - for (int channel = 0; channel < depth; ++channel) { - const int in_x_origin = - (out_x * stride_width) - params.padding_values.width; - const int in_y_origin = - (out_y * stride_height) - params.padding_values.height; - // Compute the boundaries of the filter region clamped so as to - // ensure that the filter window fits in the input array. - const int filter_x_start = std::max(0, -in_x_origin); - const int filter_x_end = - std::min(params.filter_width, input_width - in_x_origin); - const int filter_y_start = std::max(0, -in_y_origin); - const int filter_y_end = - std::min(params.filter_height, input_height - in_y_origin); - int8_t max = std::numeric_limits::lowest(); - for (int filter_y = filter_y_start; filter_y < filter_y_end; - ++filter_y) { - for (int filter_x = filter_x_start; filter_x < filter_x_end; - ++filter_x) { - const int in_x = in_x_origin + filter_x; - const int in_y = in_y_origin + filter_y; - max = std::max( - max, - input_data[Offset(input_shape, batch, in_y, in_x, channel)]); - } - } - max = std::max(max, params.quantized_activation_min); - max = std::min(max, params.quantized_activation_max); - output_data[Offset(output_shape, batch, out_y, out_x, channel)] = - static_cast(max); - } - } - } - } -} - -inline bool AveragePool(const PoolParams& params, - const RuntimeShape& input_shape, - const int16_t* input_data, - const RuntimeShape& output_shape, - int16_t* output_data) { - TFLITE_DCHECK_LE(params.quantized_activation_min, - params.quantized_activation_max); - TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4); - const int batches = MatchingDim(input_shape, 0, output_shape, 0); - const int depth = MatchingDim(input_shape, 3, output_shape, 3); - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - const int stride_height = params.stride_height; - const int stride_width = params.stride_width; - for (int batch = 0; batch < batches; ++batch) { - for (int out_y = 0; out_y < output_height; ++out_y) { - for (int out_x = 0; out_x < output_width; ++out_x) { - for (int channel = 0; channel < depth; ++channel) { - const int in_x_origin = - (out_x * stride_width) - params.padding_values.width; - const int in_y_origin = - (out_y * stride_height) - params.padding_values.height; - // Compute the boundaries of the filter region clamped so as to - // ensure that the filter window fits in the input array. - const int filter_x_start = std::max(0, -in_x_origin); - const int filter_x_end = - std::min(params.filter_width, input_width - in_x_origin); - const int filter_y_start = std::max(0, -in_y_origin); - const int filter_y_end = - std::min(params.filter_height, input_height - in_y_origin); - int32_t acc = 0; - int filter_count = 0; - for (int filter_y = filter_y_start; filter_y < filter_y_end; - ++filter_y) { - for (int filter_x = filter_x_start; filter_x < filter_x_end; - ++filter_x) { - const int in_x = in_x_origin + filter_x; - const int in_y = in_y_origin + filter_y; - acc += - input_data[Offset(input_shape, batch, in_y, in_x, channel)]; - filter_count++; - } - } - if (filter_count == 0) return false; - // Round to the closest integer value. - acc = acc > 0 ? (acc + filter_count / 2) / filter_count - : (acc - filter_count / 2) / filter_count; - acc = std::max(acc, params.quantized_activation_min); - acc = std::min(acc, params.quantized_activation_max); - output_data[Offset(output_shape, batch, out_y, out_x, channel)] = - static_cast(acc); - } - } - } - } - return true; -} - -inline void MaxPool(const PoolParams& params, const RuntimeShape& input_shape, - const int16_t* input_data, const RuntimeShape& output_shape, - int16_t* output_data) { - TFLITE_DCHECK_LE(params.quantized_activation_min, - params.quantized_activation_max); - TFLITE_DCHECK_GE(params.quantized_activation_min, - std::numeric_limits::min()); - TFLITE_DCHECK_LE(params.quantized_activation_max, - std::numeric_limits::max()); - TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4); - const int batches = MatchingDim(input_shape, 0, output_shape, 0); - const int depth = MatchingDim(input_shape, 3, output_shape, 3); - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - const int stride_height = params.stride_height; - const int stride_width = params.stride_width; - for (int batch = 0; batch < batches; ++batch) { - for (int out_y = 0; out_y < output_height; ++out_y) { - for (int out_x = 0; out_x < output_width; ++out_x) { - for (int channel = 0; channel < depth; ++channel) { - const int in_x_origin = - (out_x * stride_width) - params.padding_values.width; - const int in_y_origin = - (out_y * stride_height) - params.padding_values.height; - // Compute the boundaries of the filter region clamped so as to - // ensure that the filter window fits in the input array. - const int filter_x_start = std::max(0, -in_x_origin); - const int filter_x_end = - std::min(params.filter_width, input_width - in_x_origin); - const int filter_y_start = std::max(0, -in_y_origin); - const int filter_y_end = - std::min(params.filter_height, input_height - in_y_origin); - int16_t max = std::numeric_limits::lowest(); - for (int filter_y = filter_y_start; filter_y < filter_y_end; - ++filter_y) { - for (int filter_x = filter_x_start; filter_x < filter_x_end; - ++filter_x) { - const int in_x = in_x_origin + filter_x; - const int in_y = in_y_origin + filter_y; - max = std::max( - max, - input_data[Offset(input_shape, batch, in_y, in_x, channel)]); - } - } - max = std::max(max, params.quantized_activation_min); - max = std::min(max, params.quantized_activation_max); - output_data[Offset(output_shape, batch, out_y, out_x, channel)] = - static_cast(max); - } - } - } - } -} - -} // namespace reference_integer_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_POOLING_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/tanh.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/tanh.h deleted file mode 100644 index 7b1e003b..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/tanh.h +++ /dev/null @@ -1,117 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_TANH_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_TANH_H_ - -#include -#include - -#include "fixedpoint/fixedpoint.h" -#include "tensorflow/lite/kernels/internal/common.h" - -namespace tflite { -namespace reference_integer_ops { - -inline void Tanh(int32_t input_zero_point, int32_t input_range_radius, - int32_t input_multiplier, int32_t input_shift, - const RuntimeShape& input_shape, const int8_t* input_data, - const RuntimeShape& output_shape, int8_t* output_data) { - // Integer bits must be in sync with Prepare() function. - static constexpr int32_t kInputIntegerBits = 4; - static constexpr int32_t kOutputScale = 7; - static constexpr int32_t kMinInt8 = std::numeric_limits::min(); - static constexpr int32_t kMaxInt8 = std::numeric_limits::max(); - using F4 = gemmlowp::FixedPoint; - - const int flat_size = MatchingFlatSize(input_shape, output_shape); - - for (int i = 0; i < flat_size; ++i) { - const int32_t input = - static_cast(input_data[i]) - input_zero_point; - if (input <= -input_range_radius) { - output_data[i] = kMinInt8; - } else if (input >= input_range_radius) { - output_data[i] = kMaxInt8; - } else { - const int32_t input_in_q4 = - MultiplyByQuantizedMultiplier(input, input_multiplier, input_shift); - const int32_t output_in_q0 = - gemmlowp::tanh(F4::FromRaw(input_in_q4)).raw(); - - // Rescale and downcast. - using gemmlowp::RoundingDivideByPOT; - int32_t output_in_q24 = - RoundingDivideByPOT(output_in_q0, 31 - kOutputScale); - output_in_q24 = std::min(std::max(output_in_q24, kMinInt8), kMaxInt8); - output_data[i] = static_cast(output_in_q24); - } - } -} - -inline void Tanh(int32_t input_multiplier, int32_t input_left_shift, - const RuntimeShape& input_shape, const int16_t* ptr_input_data, - const RuntimeShape& output_shape, int16_t* ptr_output_data) { - // We use the LUT for sigmoid and take into account, that - // tanh(x) = 2*sigmoid(2*x) - 1 - - // We scale by 3/4 to expand range [-8,8]->[-10.7,10.7]. - // In case of general parameter scale, multiplier 3 is taken into account - // in TanhPrepare function and it is included in - // input_multiplier already. - - if (input_multiplier == 0) { // power of two case - input_multiplier = 3 << input_left_shift; - input_left_shift = 0; - } - - int32_t round = (input_left_shift > 0) ? 1 << (input_left_shift - 1) : 0; - - int flat_size = MatchingFlatSize(input_shape, output_shape); - - for (int i = 0; i < flat_size; ++i, ptr_input_data++, ptr_output_data++) { - int32_t input_data = - ((*ptr_input_data) * input_multiplier + round) >> input_left_shift; - - uint32_t abs_input_data = abs(input_data); - uint32_t uh = abs_input_data >> 8; - int32_t result; - - if (uh >= 255) { - // Saturate to maximum. - result = 0xFFFF << 8; - } else { - uint32_t ua = sigmoid_table_uint16[uh]; - uint32_t ub = sigmoid_table_uint16[uh + 1]; - - uint8_t ut = abs_input_data & 0xFF; - - result = (ua << 8) + ut * (ub - ua); - } - - result = (input_data >= 0) - ? (result - (1 << (14 + 9)) + (1 << (9 - 2))) - : (-result + (1 << (14 + 9)) + (1 << (9 - 2)) - 1); - - // Convert back to 16-bit. - result >>= (9 - 1); - - *ptr_output_data = result; - } -} - -} // namespace reference_integer_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_TANH_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/transpose_conv.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/transpose_conv.h deleted file mode 100644 index 92919a71..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/integer_ops/transpose_conv.h +++ /dev/null @@ -1,224 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_TRANSPOSE_CONV_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_TRANSPOSE_CONV_H_ - -#include - -#include "tensorflow/lite/kernels/internal/common.h" - -namespace tflite { -namespace reference_integer_ops { - -// Fixed-point per-channel-quantization transpose convolution reference kernel. -inline void TransposeConv( - const ConvParams& params, const int32_t* output_multiplier, - const int32_t* output_shift, const RuntimeShape& input_shape, - const int8_t* input_data, const RuntimeShape& filter_shape, - const int8_t* filter_data, const RuntimeShape& bias_shape, - const int32_t* bias_data, const RuntimeShape& output_shape, - int8_t* output_data, const RuntimeShape& im2col_shape, int8_t* im2col_data, - int32_t* scratch_buffer) { - const int stride_width = params.stride_width; - const int stride_height = params.stride_height; - const int pad_width = params.padding_values.width; - const int pad_height = params.padding_values.height; - TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(filter_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4); - (void)im2col_data; // only used in optimized code. - (void)im2col_shape; // only used in optimized code. - - const int batches = MatchingDim(input_shape, 0, output_shape, 0); - const int input_depth = MatchingDim(input_shape, 3, filter_shape, 3); - const int output_depth = MatchingDim(filter_shape, 0, output_shape, 3); - if (bias_data) { - TFLITE_DCHECK_EQ(bias_shape.FlatSize(), output_depth); - } - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - const int filter_height = filter_shape.Dims(1); - const int filter_width = filter_shape.Dims(2); - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - const int32_t input_offset = params.input_offset; - const int32_t output_offset = params.output_offset; - const int32_t output_activation_min = std::numeric_limits::min(); - const int32_t output_activation_max = std::numeric_limits::max(); - TFLITE_DCHECK_LE(output_activation_min, output_activation_max); - - const int num_elements = output_shape.FlatSize(); - // We need to initialize scratch_buffer to all 0s, as we apply the same - // 'scatter' based trick as in float version. - memset(scratch_buffer, 0, num_elements * sizeof(int32_t)); - - // Loop through input elements one at a time. - for (int batch = 0; batch < batches; ++batch) { - for (int in_y = 0; in_y < input_height; ++in_y) { - for (int in_x = 0; in_x < input_width; ++in_x) { - for (int in_channel = 0; in_channel < input_depth; ++in_channel) { - // Loop through the output elements it will influence. - const int out_x_origin = (in_x * stride_width) - pad_width; - const int out_y_origin = (in_y * stride_height) - pad_height; - for (int filter_y = 0; filter_y < filter_height; ++filter_y) { - for (int filter_x = 0; filter_x < filter_width; ++filter_x) { - for (int out_channel = 0; out_channel < output_depth; - ++out_channel) { - // Compute output element location. - const int out_x = out_x_origin + filter_x; - const int out_y = out_y_origin + filter_y; - // We cannot accumulate out of bounds. - if ((out_x >= 0) && (out_x < output_width) && (out_y >= 0) && - (out_y < output_height)) { - const int8_t input_value = input_data[Offset( - input_shape, batch, in_y, in_x, in_channel)]; - const int8_t filter_value = - filter_data[Offset(filter_shape, out_channel, filter_y, - filter_x, in_channel)]; - scratch_buffer[Offset(output_shape, batch, out_y, out_x, - out_channel)] += - (input_value + input_offset) * filter_value; - } - } - } - } - } - } - } - } - - for (int batch = 0; batch < batches; ++batch) { - for (int out_y = 0; out_y < output_height; ++out_y) { - for (int out_x = 0; out_x < output_width; ++out_x) { - for (int out_channel = 0; out_channel < output_depth; ++out_channel) { - int32_t acc = scratch_buffer[Offset(output_shape, batch, out_y, out_x, - out_channel)]; - if (bias_data) { - acc += bias_data[out_channel]; - } - acc = MultiplyByQuantizedMultiplier( - acc, output_multiplier[out_channel], output_shift[out_channel]); - acc += output_offset; - acc = std::max(acc, output_activation_min); - acc = std::min(acc, output_activation_max); - output_data[Offset(output_shape, batch, out_y, out_x, out_channel)] = - static_cast(acc); - } - } - } - } -} - -// int16_t input (zero_point=0), int8_t filter, int32 or int64 accumulator -template -inline void TransposeConv( - const ConvParams& params, const int32_t* output_multiplier, - const int32_t* output_shift, const RuntimeShape& input_shape, - const int16_t* input_data, const RuntimeShape& filter_shape, - const int8_t* filter_data, const RuntimeShape& bias_shape, - const Scalar* bias_data, const RuntimeShape& output_shape, - int16_t* output_data, const RuntimeShape& im2col_shape, int8_t* im2col_data, - Scalar* scratch_buffer) { - const int stride_width = params.stride_width; - const int stride_height = params.stride_height; - const int pad_width = params.padding_values.width; - const int pad_height = params.padding_values.height; - TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(filter_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4); - (void)im2col_data; // only used in optimized code. - (void)im2col_shape; // only used in optimized code. - - const int batches = MatchingDim(input_shape, 0, output_shape, 0); - const int input_depth = MatchingDim(input_shape, 3, filter_shape, 3); - const int output_depth = MatchingDim(filter_shape, 0, output_shape, 3); - if (bias_data) { - TFLITE_DCHECK_EQ(bias_shape.FlatSize(), output_depth); - } - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - const int filter_height = filter_shape.Dims(1); - const int filter_width = filter_shape.Dims(2); - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - const int32_t output_activation_min = std::numeric_limits::min(); - const int32_t output_activation_max = std::numeric_limits::max(); - TFLITE_DCHECK_LE(output_activation_min, output_activation_max); - - const int num_elements = output_shape.FlatSize(); - // We need to initialize scratch_buffer to all 0s, as we apply the same - // 'scatter' based trick as in float version. - memset(scratch_buffer, 0, num_elements * sizeof(Scalar)); - - // Loop through input elements one at a time. - for (int batch = 0; batch < batches; ++batch) { - for (int in_y = 0; in_y < input_height; ++in_y) { - for (int in_x = 0; in_x < input_width; ++in_x) { - for (int in_channel = 0; in_channel < input_depth; ++in_channel) { - // Loop through the output elements it will influence. - const int out_x_origin = (in_x * stride_width) - pad_width; - const int out_y_origin = (in_y * stride_height) - pad_height; - for (int filter_y = 0; filter_y < filter_height; ++filter_y) { - for (int filter_x = 0; filter_x < filter_width; ++filter_x) { - for (int out_channel = 0; out_channel < output_depth; - ++out_channel) { - // Compute output element location. - const int out_x = out_x_origin + filter_x; - const int out_y = out_y_origin + filter_y; - // We cannot accumulate out of bounds. - if ((out_x >= 0) && (out_x < output_width) && (out_y >= 0) && - (out_y < output_height)) { - const int32_t input_value = input_data[Offset( - input_shape, batch, in_y, in_x, in_channel)]; - const int32_t filter_value = - filter_data[Offset(filter_shape, out_channel, filter_y, - filter_x, in_channel)]; - scratch_buffer[Offset(output_shape, batch, out_y, out_x, - out_channel)] += - input_value * filter_value; - } - } - } - } - } - } - } - } - - for (int batch = 0; batch < batches; ++batch) { - for (int out_y = 0; out_y < output_height; ++out_y) { - for (int out_x = 0; out_x < output_width; ++out_x) { - for (int out_channel = 0; out_channel < output_depth; ++out_channel) { - Scalar acc = scratch_buffer[Offset(output_shape, batch, out_y, out_x, - out_channel)]; - if (bias_data) { - acc += bias_data[out_channel]; - } - int32_t scaled_acc = MultiplyByQuantizedMultiplier( - acc, output_multiplier[out_channel], output_shift[out_channel]); - scaled_acc = std::max(scaled_acc, output_activation_min); - scaled_acc = std::min(scaled_acc, output_activation_max); - output_data[Offset(output_shape, batch, out_y, out_x, out_channel)] = - static_cast(scaled_acc); - } - } - } - } -} - -} // namespace reference_integer_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_TRANSPOSE_CONV_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/l2normalization.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/l2normalization.h deleted file mode 100644 index 7587d2b5..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/l2normalization.h +++ /dev/null @@ -1,90 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_L2NORMALIZATION_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_L2NORMALIZATION_H_ - -#include -#include - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -namespace reference_ops { - -inline void L2Normalization(const tflite::L2NormalizationParams& op_params, - const RuntimeShape& input_shape, - const float* input_data, - const RuntimeShape& output_shape, - float* output_data, float epsilon = 1e-6) { - const int trailing_dim = input_shape.DimensionsCount() - 1; - const int outer_size = - MatchingFlatSizeSkipDim(input_shape, trailing_dim, output_shape); - const int depth = - MatchingDim(input_shape, trailing_dim, output_shape, trailing_dim); - for (int i = 0; i < outer_size; ++i) { - float squared_l2_norm = 0; - for (int c = 0; c < depth; ++c) { - const float val = input_data[depth * i + c]; - squared_l2_norm += val * val; - } - float l2_norm = std::sqrt(squared_l2_norm); - l2_norm = std::max(l2_norm, epsilon); - for (int c = 0; c < depth; ++c) { - output_data[depth * i + c] = input_data[depth * i + c] / l2_norm; - } - } -} - -inline void L2Normalization(const tflite::L2NormalizationParams& op_params, - const RuntimeShape& input_shape, - const uint8_t* input_data, - const RuntimeShape& output_shape, - uint8_t* output_data) { - const int trailing_dim = input_shape.DimensionsCount() - 1; - const int depth = - MatchingDim(input_shape, trailing_dim, output_shape, trailing_dim); - const int outer_size = - MatchingFlatSizeSkipDim(input_shape, trailing_dim, output_shape); - const int32_t input_zero_point = op_params.input_zero_point; - - for (int i = 0; i < outer_size; ++i) { - int32_t square_l2_norm = 0; - for (int c = 0; c < depth; c++) { - int32_t diff = input_data[depth * i + c] - input_zero_point; - square_l2_norm += diff * diff; - } - int32_t inv_l2norm_multiplier; - int inv_l2norm_shift; - GetInvSqrtQuantizedMultiplierExp(square_l2_norm, kReverseShift, - &inv_l2norm_multiplier, &inv_l2norm_shift); - for (int c = 0; c < depth; c++) { - int32_t diff = input_data[depth * i + c] - input_zero_point; - int32_t rescaled_diff = MultiplyByQuantizedMultiplierSmallerThanOneExp( - 128 * diff, inv_l2norm_multiplier, inv_l2norm_shift); - int32_t unclamped_output_val = 128 + rescaled_diff; - int32_t output_val = - std::min(static_cast(255), - std::max(static_cast(0), unclamped_output_val)); - output_data[depth * i + c] = static_cast(output_val); - } - } -} - -} // namespace reference_ops -} // namespace tflite -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_L2NORMALIZATION_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/leaky_relu.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/leaky_relu.h deleted file mode 100644 index 06f691ab..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/leaky_relu.h +++ /dev/null @@ -1,69 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_LEAKY_RELU_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_LEAKY_RELU_H_ - -#include -#include - -#include "tensorflow/lite/kernels/internal/common.h" - -namespace tflite { -namespace reference_ops { - -inline void LeakyRelu(const tflite::LeakyReluParams& params, - const RuntimeShape& input_shape, const float* input_data, - const RuntimeShape& output_shape, float* output_data) { - const int flat_size = MatchingFlatSize(input_shape, output_shape); - for (int i = 0; i < flat_size; ++i) { - const float val = input_data[i]; - // Note that alpha might be > 1 or < 0, so we don't use std::max here. - output_data[i] = val > 0 ? val : val * params.alpha; - } -} - -template -inline void QuantizeLeakyRelu(const LeakyReluParams& params, - const RuntimeShape& input_shape, - const T* input_data, - const RuntimeShape& output_shape, - T* output_data) { - const int flat_size = MatchingFlatSize(input_shape, output_shape); - static const int32_t quantized_min = std::numeric_limits::min(); - static const int32_t quantized_max = std::numeric_limits::max(); - for (int i = 0; i < flat_size; ++i) { - const int32_t input_value = input_data[i] - params.input_offset; - int32_t unclamped_output; - if (input_value >= 0) { - unclamped_output = params.output_offset + - MultiplyByQuantizedMultiplier( - input_value, params.output_multiplier_identity, - params.output_shift_identity); - } else { - unclamped_output = params.output_offset + - MultiplyByQuantizedMultiplier( - input_value, params.output_multiplier_alpha, - params.output_shift_alpha); - } - const T clamped_output = - std::min(quantized_max, std::max(quantized_min, unclamped_output)); - output_data[i] = static_cast(clamped_output); - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_LEAKY_RELU_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/log_softmax.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/log_softmax.h deleted file mode 100644 index 394dd3a9..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/log_softmax.h +++ /dev/null @@ -1,256 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_LOG_SOFTMAX_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_LOG_SOFTMAX_H_ - -#include -#include -#include - -#include "fixedpoint/fixedpoint.h" -#include "tensorflow/lite/kernels/internal/common.h" - -namespace tflite { -namespace reference_ops { - -inline void LogSoftmax(const SoftmaxParams& params, - const RuntimeShape& input_shape, const float* input_data, - const RuntimeShape& output_shape, float* output_data) { - const int trailing_dim = input_shape.DimensionsCount() - 1; - const int outer_size = - MatchingFlatSizeSkipDim(input_shape, trailing_dim, output_shape); - const int depth = - MatchingDim(input_shape, trailing_dim, output_shape, trailing_dim); - - for (int i = 0; i < outer_size; ++i) { - // Find max element value which we'll use to ensure numerical stability - // taking advantage of the following equality: - // log(exp(x[i])/sum(exp(x[i]))) == log(exp(x[i]+C)/sum(exp(x[i]+C))) - float max = std::numeric_limits::lowest(); - for (int c = 0; c < depth; ++c) { - max = std::max(max, input_data[i * depth + c]); - } - - // Compute sum. - float sum = 0.f; - for (int c = 0; c < depth; ++c) { - sum += std::exp(input_data[i * depth + c] - max); - } - - // Compute result. - const float log_sum = std::log(sum); - for (int c = 0; c < depth; ++c) { - output_data[i * depth + c] = input_data[i * depth + c] - max - log_sum; - } - } -} - -inline void LogSoftmax(const SoftmaxParams& params, - const RuntimeShape& input_shape, - const uint8_t* input_data, - const RuntimeShape& output_shape, uint8_t* output_data) { - const int32_t input_multiplier = params.input_multiplier; - const int32_t input_left_shift = params.input_left_shift; - const int32_t reverse_scaling_divisor = params.reverse_scaling_divisor; - const int32_t reverse_scaling_right_shift = - params.reverse_scaling_right_shift; - const int diff_min = params.diff_min; - // The representation chosen for the input to the exp() function is Q5.26. - // We need to leave extra space since values that we skip might be as large - // as -32 before multiplying by input_beta_multiplier, and therefore as - // large as -16 afterwards. Note that exp(-8) is definitely not - // insignificant to accumulation, but exp(-16) definitely is. - static constexpr int kScaledDiffIntegerBits = 5; - static constexpr int kAccumulationIntegerBits = 12; - static constexpr int kOutputIntegerBits = 4; - using FixedPointScaledDiff = - gemmlowp::FixedPoint; - using FixedPointAccum = - gemmlowp::FixedPoint; - - const int trailing_dim = input_shape.DimensionsCount() - 1; - const int outer_size = - MatchingFlatSizeSkipDim(input_shape, trailing_dim, output_shape); - const int depth = - MatchingDim(input_shape, trailing_dim, output_shape, trailing_dim); - - for (int i = 0; i < outer_size; ++i) { - uint8_t max_in_row = 0; - for (int c = 0; c < depth; ++c) { - max_in_row = std::max(max_in_row, input_data[i * depth + c]); - } - - FixedPointAccum sum_of_exps = FixedPointAccum::Zero(); - for (int c = 0; c < depth; ++c) { - int32_t input_diff = - static_cast(input_data[i * depth + c]) - max_in_row; - if (input_diff >= diff_min) { - const int32_t input_diff_rescaled = - MultiplyByQuantizedMultiplierGreaterThanOne( - input_diff, input_multiplier, input_left_shift); - const FixedPointScaledDiff scaled_diff_f8 = - FixedPointScaledDiff::FromRaw(input_diff_rescaled); - sum_of_exps = sum_of_exps + gemmlowp::Rescale( - exp_on_negative_values(scaled_diff_f8)); - } - } - - const int32_t fixed_log_sum_of_exps = - log_x_for_x_greater_than_or_equal_to_1( - sum_of_exps) - .raw(); - - // rescaled_diff_min is smallest representable in - // Q(kScaledDiffIntegerBits).(31-kScaledDiffIntegerBits) plus the - // log-sub-exps that will be subtracted in the loop. - // - // The thresholds diff_min, etc are negative. - const int rescaled_diff_min = - fixed_log_sum_of_exps + std::numeric_limits::lowest(); - const int adjusted_diff_min = - std::max(static_cast( - diff_min - 1), // Note use of > below instead of >= above. - MultiplyByQuantizedMultiplierSmallerThanOneExp( - rescaled_diff_min, reverse_scaling_divisor, - -reverse_scaling_right_shift)); - - for (int c = 0; c < depth; ++c) { - int32_t input_diff = - static_cast(input_data[i * depth + c]) - max_in_row; - if (input_diff > adjusted_diff_min) { - const int32_t input_diff_rescaled = - MultiplyByQuantizedMultiplierGreaterThanOne( - input_diff, input_multiplier, input_left_shift); - int32_t unsat_output = - gemmlowp::RoundingDivideByPOT( - (input_diff_rescaled - fixed_log_sum_of_exps), - 31 - kScaledDiffIntegerBits - kOutputIntegerBits) + - 255; - - output_data[i * depth + c] = static_cast( - std::max(std::min(unsat_output, static_cast(255)), - static_cast(0))); - } else { - // Set output to smallest value. - output_data[i * depth + c] = 0; - } - } - } -} - -template -inline void LogSoftmaxQuantized(const SoftmaxParams& params, - const size_t outer_size, const size_t depth, - const RuntimeShape& input_shape, - const T* input_data, - const RuntimeShape& output_shape, - T* output_data) { - const int32_t input_multiplier = params.input_multiplier; - const int32_t input_left_shift = params.input_left_shift; - const int32_t reverse_scaling_divisor = params.reverse_scaling_divisor; - const int32_t reverse_scaling_right_shift = - params.reverse_scaling_right_shift; - const int diff_min = params.diff_min; - - static constexpr T kMinT8 = std::numeric_limits::min(); - static constexpr T kMaxT8 = std::numeric_limits::max(); - static constexpr int32_t kMinInt32 = std::numeric_limits::min(); - - // All IntegerBits must agree with Prepare function. - // Input is chosen as Q5.26 so exp(-1 * 2^5 * 2^-1) = exp(-16) is negligible. - static constexpr int kInputIntegerBits = 5; - static constexpr int kAccumulationIntegerBits = 12; - static constexpr int kOutputIntegerBits = 4; - using F5 = gemmlowp::FixedPoint; - using F12 = gemmlowp::FixedPoint; - - for (size_t outer_index = 0; outer_index < outer_size; ++outer_index) { - T max_in_row = kMinT8; - for (size_t inner_index = 0; inner_index < depth; ++inner_index) { - max_in_row = - std::max(max_in_row, input_data[outer_index * depth + inner_index]); - } - - // Accumulator "sum_of_exps_in_q12" is safe from overflowing in 2^12 steps. - F12 sum_of_exps_in_q12 = F12::FromRaw(0); - for (size_t inner_index = 0; inner_index < depth; ++inner_index) { - int32_t input_diff = - static_cast(input_data[outer_index * depth + inner_index]) - - max_in_row; - if (input_diff >= diff_min) { - const int32_t input_diff_in_q5 = MultiplyByQuantizedMultiplier( - input_diff, input_multiplier, input_left_shift); - sum_of_exps_in_q12 = - sum_of_exps_in_q12 + - gemmlowp::Rescale( - exp_on_negative_values(F5::FromRaw(input_diff_in_q5))); - } - } - - const int32_t log_sum_of_exps_in_q5 = - log_x_for_x_greater_than_or_equal_to_1( - sum_of_exps_in_q12) - .raw(); - - // Potentially reduced the valid range. shifted_log_sum_of_exps_in_q5 is - // smallest representable in Q5.26 plus the log_sum_of_exps. - const int32_t shifted_log_sum_of_exps_in_q5 = - log_sum_of_exps_in_q5 + kMinInt32; - const int32_t adjusted_diff_min = - std::max(static_cast(diff_min - 1), - MultiplyByQuantizedMultiplier(shifted_log_sum_of_exps_in_q5, - reverse_scaling_divisor, - -reverse_scaling_right_shift)); - - for (size_t inner_index = 0; inner_index < depth; ++inner_index) { - int32_t input_diff = - static_cast(input_data[outer_index * depth + inner_index]) - - max_in_row; - // Note use of > below instead of >= above. - if (input_diff > adjusted_diff_min) { - const int32_t input_diff_in_q5 = MultiplyByQuantizedMultiplier( - input_diff, input_multiplier, input_left_shift); - - // Rescale and downcast. - int32_t output_in_q27 = - gemmlowp::RoundingDivideByPOT( - (input_diff_in_q5 - log_sum_of_exps_in_q5), - 31 - kInputIntegerBits - kOutputIntegerBits) + - kMaxT8; - - output_in_q27 = - std::max(std::min(output_in_q27, static_cast(kMaxT8)), - static_cast(kMinT8)); - output_data[outer_index * depth + inner_index] = - static_cast(output_in_q27); - } else { - output_data[outer_index * depth + inner_index] = kMinT8; - } - } - } -} - -inline void LogSoftmax(const SoftmaxParams& params, const size_t outer_size, - const size_t depth, const RuntimeShape& input_shape, - const int8_t* input_data, - const RuntimeShape& output_shape, int8_t* output_data) { - LogSoftmaxQuantized(params, outer_size, depth, input_shape, input_data, - output_shape, output_data); -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_LOG_SOFTMAX_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/logistic.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/logistic.h deleted file mode 100644 index 64b7133b..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/logistic.h +++ /dev/null @@ -1,132 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_LOGISTIC_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_LOGISTIC_H_ - -#include - -#include "fixedpoint/fixedpoint.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/cppmath.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/types.h" -#include "tensorflow/lite/kernels/op_macros.h" - -namespace tflite { -namespace reference_ops { - -inline void Logistic(const RuntimeShape& input_shape, const float* input_data, - const RuntimeShape& output_shape, float* output_data) { - const float cutoff_upper = 16.619047164916992188f; - const float cutoff_lower = -9.f; - - const int flat_size = MatchingFlatSize(input_shape, output_shape); - - // Rational for using approximation in reference kernel. - // 0. This approximation gives enough precision for float. - // 1. This works around an issue on an embedded chipset where exp() does not - // return correctly as expected - exp(x) should return inf when overflown - // not 1.701417 IEEE 754 defines representation for inf. - // 2. This will speed up calculation and is matching the behavior in the - // optimized kernels. (check the definition of scalar_logistic_op) - - for (int i = 0; i < flat_size; i++) { - float val = input_data[i]; - float result; - if (val > cutoff_upper) { - result = 1.0f; - } else if (val < cutoff_lower) { - result = std::exp(val); - } else { - result = 1.f / (1.f + std::exp(-val)); - } - output_data[i] = result; - } -} - -// Convenience version that allows, for example, generated-code calls to be -// uniform between data types. -inline void Logistic(const LogisticParams&, const RuntimeShape& input_shape, - const float* input_data, const RuntimeShape& output_shape, - float* output_data) { - // Drop params: not needed. - Logistic(input_shape, input_data, output_shape, output_data); -} - -inline void Logistic(const LogisticParams& params, - const RuntimeShape& input_shape, const int16_t* input_data, - const RuntimeShape& output_shape, int16_t* output_data) { - const int flat_size = MatchingFlatSize(input_shape, output_shape); - - for (int i = 0; i < flat_size; i++) { - // F0 uses 0 integer bits, range [-1, 1]. - // This is the return type of math functions such as tanh, logistic, - // whose range is in [-1, 1]. - using F0 = gemmlowp::FixedPoint; - // F3 uses 3 integer bits, range [-8, 8], the input range expected here. - using F3 = gemmlowp::FixedPoint; - - const F3 input = F3::FromRaw(input_data[i]); - F0 output = gemmlowp::logistic(input); - output_data[i] = output.raw(); - } -} - -// Quantized int8_t logistic activation. Cheats by dequantizing and -// requantizing around the floating point logistic method. This implementation -// is slow on platforms without a floating point unit. - -// TODO(b/141211002): Delete this int8_t implementation once we can reuse the -// approach used in TFLite for int8_t Logistic. -inline void Logistic(const RuntimeShape& input_shape, const int8_t* input_data, - float input_scale, int input_zero_point, - const RuntimeShape& output_shape, int8_t* output_data, - float output_scale, int output_zero_point) { - const float cutoff_upper = 16.619047164916992188f; - const float cutoff_lower = -9.f; - - const int flat_size = MatchingFlatSize(input_shape, output_shape); - - // Rational for using approximation in reference kernel. - // 0. This approximation gives enough precision for float. - // 1. This works around an issue on an embedded chipset where exp() does not - // return correctly as expected - exp(x) should return inf when overflown - // not 1.701417 IEEE 754 defines representation for inf. - // 2. This will speed up calculation and is matching the behavior in the - // optimized kernels. (check the definition of scalar_logistic_op) - - for (int i = 0; i < flat_size; i++) { - // Dequantize. - float val = - static_cast((input_data[i] - input_zero_point) * input_scale); - float result; - if (val > cutoff_upper) { - result = 1.0f; - } else if (val < cutoff_lower) { - result = std::exp(val); - } else { - result = 1.f / (1.f + std::exp(-val)); - } - // Requantize - int8_t output = - static_cast(result / output_scale + output_zero_point); - output_data[i] = output; - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_LOGISTIC_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/lstm_cell.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/lstm_cell.h deleted file mode 100644 index 17b113eb..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/lstm_cell.h +++ /dev/null @@ -1,422 +0,0 @@ -/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_LSTM_CELL_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_LSTM_CELL_H_ - -#include -#include -#include - -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/reference/concatenation.h" -#include "tensorflow/lite/kernels/internal/reference/fully_connected.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { -namespace reference_ops { - -inline void LstmCell( - const LstmCellParams& params, const RuntimeShape& unextended_input_shape, - const float* input_data, const RuntimeShape& unextended_prev_activ_shape, - const float* prev_activ_data, const RuntimeShape& weights_shape, - const float* weights_data, const RuntimeShape& unextended_bias_shape, - const float* bias_data, const RuntimeShape& unextended_prev_state_shape, - const float* prev_state_data, - const RuntimeShape& unextended_output_state_shape, float* output_state_data, - const RuntimeShape& unextended_output_activ_shape, float* output_activ_data, - const RuntimeShape& unextended_concat_temp_shape, float* concat_temp_data, - const RuntimeShape& unextended_activ_temp_shape, float* activ_temp_data) { - TFLITE_DCHECK_LE(unextended_input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(unextended_prev_activ_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(unextended_bias_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(unextended_prev_state_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(unextended_output_state_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(unextended_output_activ_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(unextended_concat_temp_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(unextended_activ_temp_shape.DimensionsCount(), 4); - const RuntimeShape input_shape = - RuntimeShape::ExtendedShape(4, unextended_input_shape); - const RuntimeShape prev_activ_shape = - RuntimeShape::ExtendedShape(4, unextended_prev_activ_shape); - const RuntimeShape bias_shape = - RuntimeShape::ExtendedShape(4, unextended_bias_shape); - const RuntimeShape prev_state_shape = - RuntimeShape::ExtendedShape(4, unextended_prev_state_shape); - const RuntimeShape output_state_shape = - RuntimeShape::ExtendedShape(4, unextended_output_state_shape); - const RuntimeShape output_activ_shape = - RuntimeShape::ExtendedShape(4, unextended_output_activ_shape); - const RuntimeShape concat_temp_shape = - RuntimeShape::ExtendedShape(4, unextended_concat_temp_shape); - const RuntimeShape activ_temp_shape = - RuntimeShape::ExtendedShape(4, unextended_activ_temp_shape); - TFLITE_DCHECK_GE(weights_shape.DimensionsCount(), 2); - - const int weights_dim_count = weights_shape.DimensionsCount(); - const int batches = - MatchingDim(input_shape, 0, prev_activ_shape, 0, prev_state_shape, 0, - output_state_shape, 0, output_activ_shape, 0); - const int height = - MatchingDim(input_shape, 1, prev_activ_shape, 1, prev_state_shape, 1, - output_state_shape, 1, output_activ_shape, 1); - const int width = - MatchingDim(input_shape, 2, prev_activ_shape, 2, prev_state_shape, 2, - output_state_shape, 2, output_activ_shape, 2); - const int input_depth = input_shape.Dims(3); - const int prev_activ_depth = prev_activ_shape.Dims(3); - const int total_input_depth = prev_activ_depth + input_depth; - TFLITE_DCHECK_EQ(weights_shape.Dims(weights_dim_count - 1), - total_input_depth); - TFLITE_DCHECK_EQ(FlatSizeSkipDim(bias_shape, 3), 1); - const int intern_activ_depth = - MatchingDim(weights_shape, weights_dim_count - 2, bias_shape, 3); - TFLITE_DCHECK_EQ(weights_shape.FlatSize(), - intern_activ_depth * total_input_depth); - TFLITE_DCHECK_EQ(intern_activ_depth % 4, 0); - const int output_depth = - MatchingDim(prev_state_shape, 3, prev_activ_shape, 3, output_state_shape, - 3, output_activ_shape, 3); - TFLITE_DCHECK_EQ(output_depth, intern_activ_depth / 4); - - // Concatenate prev_activ and input data together - float const* concat_input_arrays_data[2] = {input_data, prev_activ_data}; - const RuntimeShape* concat_input_arrays_shapes[2] = {&input_shape, - &prev_activ_shape}; - tflite::ConcatenationParams concat_params; - concat_params.axis = 3; - concat_params.inputs_count = 2; - Concatenation(concat_params, concat_input_arrays_shapes, - concat_input_arrays_data, concat_temp_shape, concat_temp_data); - - // Fully connected - tflite::FullyConnectedParams fc_params; - fc_params.float_activation_min = std::numeric_limits::lowest(); - fc_params.float_activation_max = std::numeric_limits::max(); - FullyConnected(fc_params, concat_temp_shape, concat_temp_data, weights_shape, - weights_data, bias_shape, bias_data, activ_temp_shape, - activ_temp_data); - - // Memory state update (the LSTM "guts") - for (int b = 0; b < batches; ++b) { - for (int w = 0; w < width; ++w) { - for (int h = 0; h < height; ++h) { - for (int c = 0; c < output_depth; ++c) { - const float input_gate = - 1.f / - (1.f + std::exp(-activ_temp_data[Offset(activ_temp_shape, b, h, w, - 0 * output_depth + c)])); - const float new_input = std::tanh(activ_temp_data[Offset( - activ_temp_shape, b, h, w, 1 * output_depth + c)]); - const float forget_gate = - 1.f / - (1.f + std::exp(-activ_temp_data[Offset(activ_temp_shape, b, h, w, - 2 * output_depth + c)])); - const float output_gate = - 1.f / - (1.f + std::exp(-activ_temp_data[Offset(activ_temp_shape, b, h, w, - 3 * output_depth + c)])); - const float new_state = - input_gate * new_input + - forget_gate * - prev_state_data[Offset(prev_state_shape, b, h, w, c)]; - output_state_data[Offset(output_state_shape, b, h, w, c)] = new_state; - output_activ_data[Offset(output_activ_shape, b, h, w, c)] = - output_gate * std::tanh(new_state); - } - } - } - } -} - -// Quantized LSTM cell implementation. -// The quantization of the input, output arrays is as follows: -// - The input activations are quantized as uint8 on the interval -// [-1, 127/128]. -// The rationale for that is that is the natural interval for output -// activations (see next point) and these need to be concatenated together. -// We could accommodate different ranges by re-scaling, but we empirically -// found that setting the input activations range to be [-1, 127/128] in the -// first place, removing the need for re-scaling, greatly improves accuracy. -// - The output activations are quantized as uint8 on the interval -// [-1, 127/128]. -// The rationale for that is that the definition of a LSTM cell makes them -// intrinsically constrained in [-1, 1]; tweaking that to [-1, 127/128] -// makes for simpler, more accurate fixed-point arithmetic. -// - The output-at-previous-timestep state array is obviously quantized as -// the output activations. -// - The internal LSTM memory (not the output-at-previous-timestep, the other -// internal state array) is int16-quantized and may use any power-of-two, -// symmetric range i.e. [-2^N, 2^N * 32767/32768] for any N, which we call -// StateIntegerBits below, see the below discussion of that template -// parameter ("The StateIntegerBits template parameter"). -// - The output of the internal fully-connected node is int16-quantized -// on the interval [-8, 8 * 32767/32768], the rationale for which is -// explained just below ("Why [-8, 8] for fully-connected output?"). -// -// -// === The StateIntegerBits template parameter === -// -// The StateIntegerBits template parameter controls the fixed-point format used -// to represent the internal memory of the LSTM cell (not the -// output-at-previous-timestep, the other internal state array). It's currently -// a template parameter so that the model can control that. The most typical -// value for StateIntegerBits is 4. Other plausible values are anywhere between -// 3 and 5. We might eventually standardize on a single supported value, e.g. 4, -// and drop that template parameter. The reason why it can't be a runtime -// parameter is that this controls the fixed-point format used, i.e. we need to -// generate actually different code based on it. In particular, we generate code -// for a fixed-point tanh() implementation for that format, which internally -// uses a fixed-point exp() implementation, which internally uses a -// barrel-shifter with a number of steps that depends on StateIntegerBits. -// Another consequence of that is that a higher value of StateIntegerBits -// results in a more expensive implementation (more barrel shifter steps -// needed). -// -// -// === Why [-8, 8] for fully-connected output? === -// -// This array is only fed to Logistic and Tanh functions, for which -// the quantized implementation will want to use fixed-point arithmetic, -// requiring a power-of-two representation interval. Thus, we should right -// away quantize this array to a power-of-two interval; otherwise, -// implementation will need to rescale that, losing any benefit that a tighter -// representation interval might otherwise yield, while introducing some -// numerical error and computational overhead. -// -// Now, Logistic and Tanh -// are nearly constant (nearly equal to their horizontal asymptotes) -// outside of a small bounded interval around 0: -// -// Logistic(4) = 1 - 1.8e-2 Tanh(4) = 1 - 6.7e-4 -// Logistic(8) = 1 - 3.4e-4 Tanh(8) = 1 - 2.3e-7 -// Logistic(16) = 1 - 1.1e-7 Tanh(16) = 1 - 2.5e-14 -// -// From this, we see that clamping to [-4, 4] would be too inaccurate -// (the error of 1.8e-2 on Logistic would be felt even in 8bit precision) -// while clamping to [-16, 16] would make no difference even in float32. -// However, for a fixed-point implementation in 16-bit integers, using 5 -// integer bits to represent the [-16, 16] range would leave only 11 -// fractional bits, giving an increment of 2^-11 = 4.9e-4 between consecutive -// representable values. Notice that is higher than the -// worst-case clamping error with clamping to [-8, 8]: 3.4e-4 for Logistic. -// Using [-8, 8] thus seems like the better compromise overall, enjoying -// an increment of 2.4e-4 between representable values and a worst-case -// clamping error of 3.4e-4, both better than the increment of 4.9e-4 with -// [-16, 16]. -// -// Moreover, all other things being equal, it is nice to choose the narrower -// representation range, as that makes the implementation of fixed-point -// math functions a little cheaper (each integer bit requires an additional -// barrel-shifter atep in the implementation of exp(-x)). That is further -// reason to prefer [-8, 8] over [-16, 16]. The choice of [-16, 16] would make -// sense for 32-bit float or 32-bit fixed-point quantization, but we are -// aiming for 16-bit fixed-point quantization of these internal nodes here. -// -template -inline void LstmCell(const LstmCellParams& params, - const RuntimeShape& unextended_input_shape, - const uint8_t* input_data_uint8, - const RuntimeShape& unextended_prev_activ_shape, - const uint8_t* prev_activ_data_uint8, - const RuntimeShape& weights_shape, - const uint8_t* weights_data_uint8, - const RuntimeShape& unextended_bias_shape, - const int32_t* bias_data_int32, - const RuntimeShape& unextended_prev_state_shape, - const int16_t* prev_state_data_int16, - const RuntimeShape& unextended_output_state_shape, - int16_t* output_state_data_int16, - const RuntimeShape& unextended_output_activ_shape, - uint8_t* output_activ_data_uint8, - const RuntimeShape& unextended_concat_temp_shape, - uint8_t* concat_temp_data_uint8, - const RuntimeShape& unextended_activ_temp_shape, - int16_t* activ_temp_data_int16, void* gemmlowp_context) { - (void)gemmlowp_context; // only used in optimized code. - int32_t weights_zero_point = params.weights_zero_point; - int32_t accum_multiplier = params.accum_multiplier; - int accum_shift = params.accum_shift; - TFLITE_DCHECK_LE(unextended_input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(unextended_prev_activ_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(unextended_bias_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(unextended_prev_state_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(unextended_output_state_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(unextended_output_activ_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(unextended_concat_temp_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(unextended_activ_temp_shape.DimensionsCount(), 4); - const RuntimeShape input_shape = - RuntimeShape::ExtendedShape(4, unextended_input_shape); - const RuntimeShape prev_activ_shape = - RuntimeShape::ExtendedShape(4, unextended_prev_activ_shape); - const RuntimeShape bias_shape = - RuntimeShape::ExtendedShape(4, unextended_bias_shape); - const RuntimeShape prev_state_shape = - RuntimeShape::ExtendedShape(4, unextended_prev_state_shape); - const RuntimeShape output_state_shape = - RuntimeShape::ExtendedShape(4, unextended_output_state_shape); - const RuntimeShape output_activ_shape = - RuntimeShape::ExtendedShape(4, unextended_output_activ_shape); - const RuntimeShape concat_temp_shape = - RuntimeShape::ExtendedShape(4, unextended_concat_temp_shape); - const RuntimeShape activ_temp_shape = - RuntimeShape::ExtendedShape(4, unextended_activ_temp_shape); - TFLITE_DCHECK_GE(weights_shape.DimensionsCount(), 2); - - // Gather dimensions information, and perform consistency checks. - const int weights_dim_count = weights_shape.DimensionsCount(); - const int outer_size = MatchingFlatSizeSkipDim( - input_shape, 3, prev_activ_shape, prev_state_shape, output_state_shape, - output_activ_shape); - const int input_depth = input_shape.Dims(3); - const int prev_activ_depth = prev_activ_shape.Dims(3); - const int total_input_depth = prev_activ_depth + input_depth; - TFLITE_DCHECK_EQ(weights_shape.Dims(weights_dim_count - 1), - total_input_depth); - const int intern_activ_depth = - MatchingDim(weights_shape, weights_dim_count - 2, bias_shape, 3); - TFLITE_DCHECK_EQ(weights_shape.FlatSize(), - intern_activ_depth * total_input_depth); - TFLITE_DCHECK_EQ(FlatSizeSkipDim(bias_shape, 3), 1); - TFLITE_DCHECK_EQ(intern_activ_depth % 4, 0); - const int output_depth = - MatchingDim(prev_state_shape, 3, prev_activ_shape, 3, output_state_shape, - 3, output_activ_shape, 3); - TFLITE_DCHECK_EQ(output_depth, intern_activ_depth / 4); - const int fc_batches = FlatSizeSkipDim(activ_temp_shape, 3); - const int fc_output_depth = - MatchingDim(weights_shape, weights_dim_count - 2, activ_temp_shape, 3); - const int fc_accum_depth = total_input_depth; - TFLITE_DCHECK_EQ(fc_output_depth, 4 * output_depth); - - // Depth-concatenate prev_activ and input data together. - uint8_t const* concat_input_arrays_data[2] = {input_data_uint8, - prev_activ_data_uint8}; - const RuntimeShape* concat_input_arrays_shapes[2] = {&input_shape, - &prev_activ_shape}; - tflite::ConcatenationParams concat_params; - concat_params.axis = 3; - concat_params.inputs_count = 2; - Concatenation(concat_params, concat_input_arrays_shapes, - concat_input_arrays_data, concat_temp_shape, - concat_temp_data_uint8); - - // Implementation of the fully connected node inside the LSTM cell. - // The operands are 8-bit integers, the accumulators are internally 32bit - // integers, and the output is 16-bit fixed-point with 3 integer bits so - // the output range is [-2^3, 2^3] == [-8, 8]. The rationale for that - // is explained in the function comment above. - for (int b = 0; b < fc_batches; ++b) { - for (int out_c = 0; out_c < fc_output_depth; ++out_c) { - // Internal accumulation. - // Initialize accumulator with the bias-value. - int32_t accum = bias_data_int32[out_c]; - // Accumulation loop. - for (int d = 0; d < fc_accum_depth; ++d) { - int16_t input_val = - concat_temp_data_uint8[b * fc_accum_depth + d] - 128; - int16_t weights_val = - weights_data_uint8[out_c * fc_accum_depth + d] - weights_zero_point; - accum += input_val * weights_val; - } - // Down-scale the final int32 accumulator to the scale used by our - // (16-bit, using 3 integer bits) fixed-point format. The quantized - // multiplier and shift here have been pre-computed offline - // (e.g. by toco). - accum = - MultiplyByQuantizedMultiplier(accum, accum_multiplier, accum_shift); - // Saturate, cast to int16, and store to the temporary activations array. - accum = std::max(-32768, std::min(32767, accum)); - activ_temp_data_int16[out_c + fc_output_depth * b] = accum; - } - } - - // Rest of the LSTM cell: tanh and logistic math functions, and some adds - // and muls, all done in 16-bit fixed-point. - for (int b = 0; b < outer_size; ++b) { - for (int c = 0; c < output_depth; ++c) { - // Define the fixed-point data types that we will use here. All use - // int16 as the underlying integer type i.e. all are 16-bit fixed-point. - // They only differ by the number of integral vs. fractional bits, - // determining the range of values that they can represent. - // - // F0 uses 0 integer bits, range [-1, 1]. - // This is the return type of math functions such as tanh, logistic, - // whose range is in [-1, 1]. - using F0 = gemmlowp::FixedPoint; - // F3 uses 3 integer bits, range [-8, 8]. - // This is the range of the previous fully-connected node's output, - // which is our input here. - using F3 = gemmlowp::FixedPoint; - // FS uses StateIntegerBits integer bits, range [-2^StateIntegerBits, - // 2^StateIntegerBits]. It's used to represent the internal state, whose - // number of integer bits is currently dictated by the model. See comment - // on the StateIntegerBits template parameter above. - using FS = gemmlowp::FixedPoint; - // Implementation of input gate, using fixed-point logistic function. - F3 input_gate_input = F3::FromRaw( - activ_temp_data_int16[b * fc_output_depth + 0 * output_depth + c]); - F0 input_gate_output = gemmlowp::logistic(input_gate_input); - // Implementation of input modulation gate, using fixed-point tanh - // function. - F3 input_modulation_gate_input = F3::FromRaw( - activ_temp_data_int16[b * fc_output_depth + 1 * output_depth + c]); - F0 input_modulation_gate_output = - gemmlowp::tanh(input_modulation_gate_input); - // Implementation of forget gate, using fixed-point logistic function. - F3 forget_gate_input = F3::FromRaw( - activ_temp_data_int16[b * fc_output_depth + 2 * output_depth + c]); - F0 forget_gate_output = gemmlowp::logistic(forget_gate_input); - // Implementation of output gate, using fixed-point logistic function. - F3 output_gate_input = F3::FromRaw( - activ_temp_data_int16[b * fc_output_depth + 3 * output_depth + c]); - F0 output_gate_output = gemmlowp::logistic(output_gate_input); - // Implementation of internal multiplication nodes, still in fixed-point. - F0 input_times_input_modulation = - input_gate_output * input_modulation_gate_output; - FS prev_state = FS::FromRaw(prev_state_data_int16[b * output_depth + c]); - FS prev_state_times_forget_state = forget_gate_output * prev_state; - // Implementation of internal addition node, saturating. - FS new_state = gemmlowp::SaturatingAdd( - gemmlowp::Rescale(input_times_input_modulation), - prev_state_times_forget_state); - // Implementation of last internal Tanh node, still in fixed-point. - // Since a Tanh fixed-point implementation is specialized for a given - // number or integer bits, and each specialization can have a substantial - // code size, and we already used above a Tanh on an input with 3 integer - // bits, and per the table in the above function comment there is no - // significant accuracy to be lost by clamping to [-8, +8] for a - // 3-integer-bits representation, let us just do that. This helps people - // porting this to targets where code footprint must be minimized. - F3 new_state_f3 = gemmlowp::Rescale<3>(new_state); - F0 output_activ_int16 = output_gate_output * gemmlowp::tanh(new_state_f3); - // Store the new internal state back to memory, as 16-bit integers. - // Note: here we store the original value with StateIntegerBits, not - // the rescaled 3-integer-bits value fed to tanh. - output_state_data_int16[b * output_depth + c] = new_state.raw(); - // Down-scale the output activations to 8-bit integers, saturating, - // and store back to memory. - int16_t rescaled_output_activ = - gemmlowp::RoundingDivideByPOT(output_activ_int16.raw(), 8); - int16_t clamped_output_activ = std::max( - -128, std::min(127, rescaled_output_activ)); - output_activ_data_uint8[b * output_depth + c] = - 128 + clamped_output_activ; - } - } -} - -} // namespace reference_ops -} // namespace tflite -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_LSTM_CELL_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/maximum_minimum.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/maximum_minimum.h deleted file mode 100644 index cd11b419..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/maximum_minimum.h +++ /dev/null @@ -1,64 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_MAXIMUM_MINIMUM_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_MAXIMUM_MINIMUM_H_ - -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { -namespace reference_ops { - -template -void MaximumMinimumBroadcastSlow(const RuntimeShape& unextended_input1_shape, - const T* input1_data, - const RuntimeShape& unextended_input2_shape, - const T* input2_data, - const RuntimeShape& unextended_output_shape, - T* output_data, Op op) { - // Uses element-wise calculation if broadcast is not required. - if (unextended_input1_shape == unextended_input2_shape) { - const int flat_size = - MatchingElementsSize(unextended_input1_shape, unextended_input2_shape, - unextended_output_shape); - for (int i = 0; i < flat_size; ++i) { - output_data[i] = op(input1_data[i], input2_data[i]); - } - } else { - TFLITE_DCHECK_LE(unextended_input1_shape.DimensionsCount(), N); - TFLITE_DCHECK_LE(unextended_input2_shape.DimensionsCount(), N); - TFLITE_DCHECK_LE(unextended_output_shape.DimensionsCount(), N); - - NdArrayDesc desc1; - NdArrayDesc desc2; - NdArrayDesc output_desc; - NdArrayDescsForElementwiseBroadcast( - unextended_input1_shape, unextended_input2_shape, &desc1, &desc2); - CopyDimsToDesc(RuntimeShape::ExtendedShape(N, unextended_output_shape), - &output_desc); - - auto maxmin_func = [&](int indexes[N]) { - output_data[SubscriptToIndex(output_desc, indexes)] = - op(input1_data[SubscriptToIndex(desc1, indexes)], - input2_data[SubscriptToIndex(desc2, indexes)]); - }; - NDOpsHelper(output_desc, maxmin_func); - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_MAXIMUM_MINIMUM_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/mul.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/mul.h deleted file mode 100644 index b977104c..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/mul.h +++ /dev/null @@ -1,168 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_MUL_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_MUL_H_ - -#include - -#include "tensorflow/lite/kernels/internal/common.h" - -namespace tflite { - -namespace reference_ops { - -// Element-wise mul that can often be used for inner loop of broadcast Mul as -// well as the non-broadcast Mul. -inline void MulElementwise(int size, const ArithmeticParams& params, - const uint8_t* input1_data, - const uint8_t* input2_data, uint8_t* output_data) { - for (int i = 0; i < size; ++i) { - const int32_t input1_val = params.input1_offset + input1_data[i]; - const int32_t input2_val = params.input2_offset + input2_data[i]; - const int32_t unclamped_result = - params.output_offset + - MultiplyByQuantizedMultiplier(input1_val * input2_val, - params.output_multiplier, - params.output_shift); - const int32_t clamped_output = - std::min(params.quantized_activation_max, - std::max(params.quantized_activation_min, unclamped_result)); - output_data[i] = static_cast(clamped_output); - } -} - -template -inline void Mul(const ArithmeticParams& params, - const RuntimeShape& input1_shape, const T* input1_data, - const RuntimeShape& input2_shape, const T* input2_data, - const RuntimeShape& output_shape, T* output_data) { - T output_activation_min; - T output_activation_max; - GetActivationParams(params, &output_activation_min, &output_activation_max); - - const int flat_size = - MatchingExtendedShapeFlatSize(input1_shape, input2_shape, output_shape); - for (int i = 0; i < flat_size; ++i) { - output_data[i] = ActivationFunctionWithMinMax( - input1_data[i] * input2_data[i], output_activation_min, - output_activation_max); - } -} - -inline void Mul(const ArithmeticParams& params, - const RuntimeShape& input1_shape, const uint8_t* input1_data, - const RuntimeShape& input2_shape, const uint8_t* input2_data, - const RuntimeShape& output_shape, uint8_t* output_data) { - TFLITE_DCHECK_LE(params.quantized_activation_min, - params.quantized_activation_max); - const int flat_size = - MatchingExtendedShapeFlatSize(input1_shape, input2_shape, output_shape); - - MulElementwise(flat_size, params, input1_data, input2_data, output_data); -} - -inline void BroadcastMul4DSlow(const ArithmeticParams& params, - const RuntimeShape& input1_shape, - const uint8_t* input1_data, - const RuntimeShape& input2_shape, - const uint8_t* input2_data, - const RuntimeShape& output_shape, - uint8_t* output_data) { - NdArrayDesc<4> desc1; - NdArrayDesc<4> desc2; - NdArrayDescsForElementwiseBroadcast(input1_shape, input2_shape, &desc1, - &desc2); - const RuntimeShape extended_output_shape = - RuntimeShape::ExtendedShape(4, output_shape); - - for (int b = 0; b < extended_output_shape.Dims(0); ++b) { - for (int y = 0; y < extended_output_shape.Dims(1); ++y) { - for (int x = 0; x < extended_output_shape.Dims(2); ++x) { - for (int c = 0; c < extended_output_shape.Dims(3); ++c) { - const int32_t input1_val = - params.input1_offset + - input1_data[SubscriptToIndex(desc1, b, y, x, c)]; - const int32_t input2_val = - params.input2_offset + - input2_data[SubscriptToIndex(desc2, b, y, x, c)]; - const int32_t unclamped_result = - params.output_offset + - MultiplyByQuantizedMultiplier(input1_val * input2_val, - params.output_multiplier, - params.output_shift); - const int32_t clamped_output = std::min( - params.quantized_activation_max, - std::max(params.quantized_activation_min, unclamped_result)); - output_data[Offset(extended_output_shape, b, y, x, c)] = - static_cast(clamped_output); - } - } - } - } -} - -template -void BroadcastMul4DSlow(const ArithmeticParams& params, - const RuntimeShape& unextended_input1_shape, - const T* input1_data, - const RuntimeShape& unextended_input2_shape, - const T* input2_data, - const RuntimeShape& unextended_output_shape, - T* output_data) { - T output_activation_min; - T output_activation_max; - GetActivationParams(params, &output_activation_min, &output_activation_max); - - TFLITE_DCHECK_LE(unextended_input1_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(unextended_input2_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(unextended_output_shape.DimensionsCount(), 4); - const RuntimeShape output_shape = - RuntimeShape::ExtendedShape(4, unextended_output_shape); - - NdArrayDesc<4> desc1; - NdArrayDesc<4> desc2; - NdArrayDescsForElementwiseBroadcast(unextended_input1_shape, - unextended_input2_shape, &desc1, &desc2); - - // In Tensorflow, the dimensions are canonically named (batch_number, row, - // col, channel), with extents (batches, height, width, depth), with the - // trailing dimension changing most rapidly (channels has the smallest stride, - // typically 1 element). - // - // In generated C code, we store arrays with the dimensions reversed. The - // first dimension has smallest stride. - // - // We name our variables by their Tensorflow convention, but generate C code - // nesting loops such that the innermost loop has the smallest stride for the - // best cache behavior. - for (int b = 0; b < output_shape.Dims(0); ++b) { - for (int y = 0; y < output_shape.Dims(1); ++y) { - for (int x = 0; x < output_shape.Dims(2); ++x) { - for (int c = 0; c < output_shape.Dims(3); ++c) { - output_data[Offset(output_shape, b, y, x, c)] = - ActivationFunctionWithMinMax( - input1_data[SubscriptToIndex(desc1, b, y, x, c)] * - input2_data[SubscriptToIndex(desc2, b, y, x, c)], - output_activation_min, output_activation_max); - } - } - } - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_MUL_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/neg.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/neg.h deleted file mode 100644 index e127883f..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/neg.h +++ /dev/null @@ -1,37 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_NEG_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_NEG_H_ - -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -namespace reference_ops { - -template -inline void Negate(const RuntimeShape& input_shape, const T* input_data, - const RuntimeShape& output_shape, T* output_data) { - const int flat_size = MatchingFlatSize(input_shape, output_shape); - - for (int i = 0; i < flat_size; ++i) { - output_data[i] = -input_data[i]; - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_NEG_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/pad.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/pad.h deleted file mode 100644 index 27589445..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/pad.h +++ /dev/null @@ -1,169 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_PAD_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_PAD_H_ - -#include - -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -namespace reference_ops { - -// TFLite Pad supports activation tensors with up to 5 dimensions. -constexpr int PadKernelMaxDimensionCount() { return 5; } - -// There are two versions of pad: Pad and PadV2. In PadV2 there is a second -// scalar input that provides the padding value. Therefore pad_value_ptr can be -// equivalent to a simple input1_data. For Pad, it should point to a zero -// value. -// -// Note that two typenames are required, so that T=P=int32_t is considered a -// specialization distinct from P=int32_t. -template -inline void PadImpl(const tflite::PadParams& op_params, - const RuntimeShape& input_shape, const T* input_data, - const P* pad_value_ptr, const RuntimeShape& output_shape, - T* output_data) { - const RuntimeShape ext_input_shape = - RuntimeShape::ExtendedShape(PadKernelMaxDimensionCount(), input_shape); - const RuntimeShape ext_output_shape = - RuntimeShape::ExtendedShape(PadKernelMaxDimensionCount(), output_shape); - TFLITE_DCHECK_LE(op_params.left_padding_count, PadKernelMaxDimensionCount()); - TFLITE_DCHECK_LE(op_params.right_padding_count, PadKernelMaxDimensionCount()); - - // Runtime calls are currently fixed at 5 dimensions. Copy inputs so we can - // pad them to 5 dims (yes, we are "padding the padding"). - int left_padding_copy[PadKernelMaxDimensionCount()]; - for (int i = 0; i < PadKernelMaxDimensionCount(); i++) { - left_padding_copy[i] = 0; - } - for (int i = 0; i < op_params.left_padding_count; ++i) { - left_padding_copy[i + PadKernelMaxDimensionCount() - - op_params.left_padding_count] = op_params.left_padding[i]; - } - int right_padding_copy[PadKernelMaxDimensionCount()]; - for (int i = 0; i < PadKernelMaxDimensionCount(); i++) { - right_padding_copy[i] = 0; - } - for (int i = 0; i < op_params.right_padding_count; ++i) { - right_padding_copy[i + PadKernelMaxDimensionCount() - - op_params.right_padding_count] = - op_params.right_padding[i]; - } - - const int output_batch = ext_output_shape.Dims(0); - const int output_plane = ext_output_shape.Dims(1); - const int output_height = ext_output_shape.Dims(2); - const int output_width = ext_output_shape.Dims(3); - const int output_depth = ext_output_shape.Dims(4); - - const int left_b_padding = left_padding_copy[0]; - const int left_p_padding = left_padding_copy[1]; - const int left_h_padding = left_padding_copy[2]; - const int left_w_padding = left_padding_copy[3]; - const int left_d_padding = left_padding_copy[4]; - - const int right_b_padding = right_padding_copy[0]; - const int right_p_padding = right_padding_copy[1]; - const int right_h_padding = right_padding_copy[2]; - const int right_w_padding = right_padding_copy[3]; - const int right_d_padding = right_padding_copy[4]; - - const T pad_value = *pad_value_ptr; - - const T* in_ptr = input_data; - T* out_ptr = output_data; - for (int out_b = 0; out_b < output_batch; ++out_b) { - for (int out_p = 0; out_p < output_plane; ++out_p) { - for (int out_h = 0; out_h < output_height; ++out_h) { - for (int out_w = 0; out_w < output_width; ++out_w) { - for (int out_d = 0; out_d < output_depth; ++out_d) { - if (out_b < left_b_padding || - out_b >= output_batch - right_b_padding || - out_p < left_p_padding || - out_p >= output_plane - right_p_padding || - out_h < left_h_padding || - out_h >= output_height - right_h_padding || - out_w < left_w_padding || - out_w >= output_width - right_w_padding || - out_d < left_d_padding || - out_d >= output_depth - right_d_padding) { - *out_ptr++ = pad_value; - } else { - *out_ptr++ = *in_ptr++; - } - } - } - } - } - } -} - -template -inline void Pad(const tflite::PadParams& op_params, - const RuntimeShape& input_shape, const T* input_data, - const P* pad_value_ptr, const RuntimeShape& output_shape, - T* output_data) { - PadImpl(op_params, input_shape, input_data, pad_value_ptr, output_shape, - output_data); -} - -// The second (pad-value) input can be int32_t when, say, the first is uint8_t. -template -inline void Pad(const tflite::PadParams& op_params, - const RuntimeShape& input_shape, const T* input_data, - const int32_t* pad_value_ptr, const RuntimeShape& output_shape, - T* output_data) { - const T converted_pad_value = static_cast(*pad_value_ptr); - PadImpl(op_params, input_shape, input_data, &converted_pad_value, - output_shape, output_data); -} - -// This version avoids conflicting template matching. -template <> -inline void Pad(const tflite::PadParams& op_params, - const RuntimeShape& input_shape, const int32_t* input_data, - const int32_t* pad_value_ptr, const RuntimeShape& output_shape, - int32_t* output_data) { - PadImpl(op_params, input_shape, input_data, pad_value_ptr, output_shape, - output_data); -} - -template -inline void PadImageStyle(const tflite::PadParams& op_params, - const RuntimeShape& input_shape, const T* input_data, - const P* pad_value_ptr, - const RuntimeShape& output_shape, T* output_data) { - Pad(op_params, input_shape, input_data, pad_value_ptr, output_shape, - output_data); -} - -template -inline void PadImageStyle(const tflite::PadParams& op_params, - const RuntimeShape& input_shape, - const float* input_data, const P* pad_value_ptr, - const RuntimeShape& output_shape, - float* output_data) { - Pad(op_params, input_shape, input_data, pad_value_ptr, output_shape, - output_data); -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_PAD_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/pooling.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/pooling.h deleted file mode 100644 index fe17484c..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/pooling.h +++ /dev/null @@ -1,303 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_POOLING_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_POOLING_H_ - -#include - -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/cppmath.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { -namespace reference_ops { - -inline bool AveragePool(const PoolParams& params, - const RuntimeShape& input_shape, - const float* input_data, - const RuntimeShape& output_shape, float* output_data) { - TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4); - const int batches = MatchingDim(input_shape, 0, output_shape, 0); - const int depth = MatchingDim(input_shape, 3, output_shape, 3); - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - const int stride_height = params.stride_height; - const int stride_width = params.stride_width; - for (int batch = 0; batch < batches; ++batch) { - for (int out_y = 0; out_y < output_height; ++out_y) { - for (int out_x = 0; out_x < output_width; ++out_x) { - for (int channel = 0; channel < depth; ++channel) { - const int in_x_origin = - (out_x * stride_width) - params.padding_values.width; - const int in_y_origin = - (out_y * stride_height) - params.padding_values.height; - // Compute the boundaries of the filter region clamped so as to - // ensure that the filter window fits in the input array. - const int filter_x_start = std::max(0, -in_x_origin); - const int filter_x_end = - std::min(params.filter_width, input_width - in_x_origin); - const int filter_y_start = std::max(0, -in_y_origin); - const int filter_y_end = - std::min(params.filter_height, input_height - in_y_origin); - float total = 0.f; - float filter_count = 0; - for (int filter_y = filter_y_start; filter_y < filter_y_end; - ++filter_y) { - for (int filter_x = filter_x_start; filter_x < filter_x_end; - ++filter_x) { - const int in_x = in_x_origin + filter_x; - const int in_y = in_y_origin + filter_y; - total += - input_data[Offset(input_shape, batch, in_y, in_x, channel)]; - filter_count++; - } - } - if (filter_count == 0) return false; - const float average = total / filter_count; - output_data[Offset(output_shape, batch, out_y, out_x, channel)] = - ActivationFunctionWithMinMax(average, params.float_activation_min, - params.float_activation_max); - } - } - } - } - return true; -} - -inline bool AveragePool(const PoolParams& params, - const RuntimeShape& input_shape, - const uint8_t* input_data, - const RuntimeShape& output_shape, - uint8_t* output_data) { - TFLITE_DCHECK_LE(params.quantized_activation_min, - params.quantized_activation_max); - TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4); - const int batches = MatchingDim(input_shape, 0, output_shape, 0); - const int depth = MatchingDim(input_shape, 3, output_shape, 3); - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - const int stride_height = params.stride_height; - const int stride_width = params.stride_width; - for (int batch = 0; batch < batches; ++batch) { - for (int out_y = 0; out_y < output_height; ++out_y) { - for (int out_x = 0; out_x < output_width; ++out_x) { - for (int channel = 0; channel < depth; ++channel) { - const int in_x_origin = - (out_x * stride_width) - params.padding_values.width; - const int in_y_origin = - (out_y * stride_height) - params.padding_values.height; - // Compute the boundaries of the filter region clamped so as to - // ensure that the filter window fits in the input array. - const int filter_x_start = std::max(0, -in_x_origin); - const int filter_x_end = - std::min(params.filter_width, input_width - in_x_origin); - const int filter_y_start = std::max(0, -in_y_origin); - const int filter_y_end = - std::min(params.filter_height, input_height - in_y_origin); - int32_t acc = 0; - int filter_count = 0; - for (int filter_y = filter_y_start; filter_y < filter_y_end; - ++filter_y) { - for (int filter_x = filter_x_start; filter_x < filter_x_end; - ++filter_x) { - const int in_x = in_x_origin + filter_x; - const int in_y = in_y_origin + filter_y; - acc += - input_data[Offset(input_shape, batch, in_y, in_x, channel)]; - filter_count++; - } - } - if (filter_count == 0) return false; - acc = (acc + filter_count / 2) / filter_count; - acc = std::max(acc, params.quantized_activation_min); - acc = std::min(acc, params.quantized_activation_max); - output_data[Offset(output_shape, batch, out_y, out_x, channel)] = - static_cast(acc); - } - } - } - } - return true; -} - -inline void L2Pool(const PoolParams& params, const RuntimeShape& input_shape, - const float* input_data, const RuntimeShape& output_shape, - float* output_data) { - TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4); - const int batches = MatchingDim(input_shape, 0, output_shape, 0); - const int depth = MatchingDim(input_shape, 3, output_shape, 3); - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - const int stride_height = params.stride_height; - const int stride_width = params.stride_width; - for (int batch = 0; batch < batches; ++batch) { - for (int out_y = 0; out_y < output_height; ++out_y) { - for (int out_x = 0; out_x < output_width; ++out_x) { - for (int channel = 0; channel < depth; ++channel) { - const int in_x_origin = - (out_x * stride_width) - params.padding_values.width; - const int in_y_origin = - (out_y * stride_height) - params.padding_values.height; - // Compute the boundaries of the filter region clamped so as to - // ensure that the filter window fits in the input array. - const int filter_x_start = std::max(0, -in_x_origin); - const int filter_x_end = - std::min(params.filter_width, input_width - in_x_origin); - const int filter_y_start = std::max(0, -in_y_origin); - const int filter_y_end = - std::min(params.filter_height, input_height - in_y_origin); - float sum_squares = 0.f; - int filter_count = 0; - for (int filter_y = filter_y_start; filter_y < filter_y_end; - ++filter_y) { - for (int filter_x = filter_x_start; filter_x < filter_x_end; - ++filter_x) { - const int in_x = in_x_origin + filter_x; - const int in_y = in_y_origin + filter_y; - const float val = - input_data[Offset(input_shape, batch, in_y, in_x, channel)]; - sum_squares += val * val; - filter_count++; - } - } - const float l2pool_result = std::sqrt(sum_squares / filter_count); - output_data[Offset(output_shape, batch, out_y, out_x, channel)] = - ActivationFunctionWithMinMax(l2pool_result, - params.float_activation_min, - params.float_activation_max); - } - } - } - } -} - -inline void MaxPool(const PoolParams& params, const RuntimeShape& input_shape, - const float* input_data, const RuntimeShape& output_shape, - float* output_data) { - TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4); - const int batches = MatchingDim(input_shape, 0, output_shape, 0); - const int depth = MatchingDim(input_shape, 3, output_shape, 3); - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - const int stride_height = params.stride_height; - const int stride_width = params.stride_width; - for (int batch = 0; batch < batches; ++batch) { - for (int out_y = 0; out_y < output_height; ++out_y) { - for (int out_x = 0; out_x < output_width; ++out_x) { - for (int channel = 0; channel < depth; ++channel) { - const int in_x_origin = - (out_x * stride_width) - params.padding_values.width; - const int in_y_origin = - (out_y * stride_height) - params.padding_values.height; - // Compute the boundaries of the filter region clamped so as to - // ensure that the filter window fits in the input array. - const int filter_x_start = std::max(0, -in_x_origin); - const int filter_x_end = - std::min(params.filter_width, input_width - in_x_origin); - const int filter_y_start = std::max(0, -in_y_origin); - const int filter_y_end = - std::min(params.filter_height, input_height - in_y_origin); - float max = std::numeric_limits::lowest(); - for (int filter_y = filter_y_start; filter_y < filter_y_end; - ++filter_y) { - for (int filter_x = filter_x_start; filter_x < filter_x_end; - ++filter_x) { - const int in_x = in_x_origin + filter_x; - const int in_y = in_y_origin + filter_y; - max = std::max( - max, - input_data[Offset(input_shape, batch, in_y, in_x, channel)]); - } - } - output_data[Offset(output_shape, batch, out_y, out_x, channel)] = - ActivationFunctionWithMinMax(max, params.float_activation_min, - params.float_activation_max); - } - } - } - } -} - -inline void MaxPool(const PoolParams& params, const RuntimeShape& input_shape, - const uint8_t* input_data, const RuntimeShape& output_shape, - uint8_t* output_data) { - TFLITE_DCHECK_LE(params.quantized_activation_min, - params.quantized_activation_max); - TFLITE_DCHECK_GE(params.quantized_activation_min, 0); - TFLITE_DCHECK_LE(params.quantized_activation_max, 255); - TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4); - const int batches = MatchingDim(input_shape, 0, output_shape, 0); - const int depth = MatchingDim(input_shape, 3, output_shape, 3); - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - const int stride_height = params.stride_height; - const int stride_width = params.stride_width; - for (int batch = 0; batch < batches; ++batch) { - for (int out_y = 0; out_y < output_height; ++out_y) { - for (int out_x = 0; out_x < output_width; ++out_x) { - for (int channel = 0; channel < depth; ++channel) { - const int in_x_origin = - (out_x * stride_width) - params.padding_values.width; - const int in_y_origin = - (out_y * stride_height) - params.padding_values.height; - // Compute the boundaries of the filter region clamped so as to - // ensure that the filter window fits in the input array. - const int filter_x_start = std::max(0, -in_x_origin); - const int filter_x_end = - std::min(params.filter_width, input_width - in_x_origin); - const int filter_y_start = std::max(0, -in_y_origin); - const int filter_y_end = - std::min(params.filter_height, input_height - in_y_origin); - uint8_t max = 0; - for (int filter_y = filter_y_start; filter_y < filter_y_end; - ++filter_y) { - for (int filter_x = filter_x_start; filter_x < filter_x_end; - ++filter_x) { - const int in_x = in_x_origin + filter_x; - const int in_y = in_y_origin + filter_y; - max = std::max( - max, - input_data[Offset(input_shape, batch, in_y, in_x, channel)]); - } - } - max = std::max(max, params.quantized_activation_min); - max = std::min(max, params.quantized_activation_max); - output_data[Offset(output_shape, batch, out_y, out_x, channel)] = - static_cast(max); - } - } - } - } -} -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_POOLING_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/portable_tensor_utils.cc b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/portable_tensor_utils.cc deleted file mode 100644 index 4684be64..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/portable_tensor_utils.cc +++ /dev/null @@ -1,809 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include -#include -#include -#include -#include -#include - -#include "fixedpoint/fixedpoint.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/internal/cppmath.h" -#include "tensorflow/lite/kernels/internal/reference/portable_tensor_utils_impl.h" - -#if defined(_MSC_VER) -#define __restrict__ __restrict -#endif - -namespace tflite { -namespace tensor_utils { - -namespace { -const int32_t kInt16Max = std::numeric_limits::max(); -const int32_t kInt16Min = std::numeric_limits::min(); -} // namespace - -void PortableSymmetricQuantizeFloats(const float* values, const int size, - int8_t* quantized_values, float* min_value, - float* max_value, float* scaling_factor) { - auto minmax = std::minmax_element(values, values + size); - *min_value = *minmax.first; - *max_value = *minmax.second; - - PortableSymmetricQuantizeFloats(values, size, quantized_values, *min_value, - *max_value, scaling_factor); -} - -void PortableSymmetricQuantizeFloats(const float* values, const int size, - int8_t* quantized_values, float min_value, - float max_value, float* scaling_factor) { - const int32_t kScale = 127; - const float range = std::max(std::abs(min_value), std::abs(max_value)); - if (range == 0) { - memset(quantized_values, 0, size * sizeof(int8_t)); - *scaling_factor = 1; - return; - } - *scaling_factor = range / kScale; - const float scaling_factor_inv = kScale / range; - for (int i = 0; i < size; ++i) { - const int32_t quantized_value = - static_cast(TfLiteRound(values[i] * scaling_factor_inv)); - // Clamp: just in case some odd numeric offset. - quantized_values[i] = static_cast( - std::min(kScale, std::max(-kScale, quantized_value))); - } -} - -void PortableAsymmetricQuantizeFloats(const float* values, const int size, - int8_t* quantized_values, - float* scaling_factor, int32_t* offset) { - const int32_t kMinScale = -128; - const int32_t kMaxScale = 127; - const double qmin_double = kMinScale; - const double qmax_double = kMaxScale; - const auto minmax = std::minmax_element(values, values + size); - const double rmin = static_cast(std::min(0.0f, *minmax.first)); - const double rmax = static_cast(std::max(0.0f, *minmax.second)); - if (rmin == rmax) { - memset(quantized_values, 0, size * sizeof(int8_t)); - *scaling_factor = 1; - *offset = 0; - return; - } else { - double scale = (rmax - rmin) / (qmax_double - qmin_double); - const double zero_point_from_min = qmin_double - rmin / scale; - const double zero_point_from_max = qmax_double - rmax / scale; - const double zero_point_from_min_error = - std::abs(qmin_double) + std::abs(rmin / scale); - const double zero_point_from_max_error = - std::abs(qmax_double) + std::abs(rmax / scale); - const double zero_point_double = - zero_point_from_min_error < zero_point_from_max_error - ? zero_point_from_min - : zero_point_from_max; - int8_t nudged_zero_point = 0; - if (zero_point_double <= qmin_double) { - nudged_zero_point = kMinScale; - } else if (zero_point_double >= qmax_double) { - nudged_zero_point = kMaxScale; - } else { - nudged_zero_point = static_cast(round(zero_point_double)); - } - *scaling_factor = scale; - *offset = nudged_zero_point; - } - const float scaling_factor_inv = 1.0f / *scaling_factor; - for (int i = 0; i < size; ++i) { - const int32_t quantized_value = static_cast( - TfLiteRound(*offset + values[i] * scaling_factor_inv)); - quantized_values[i] = - std::min(kMaxScale, std::max(kMinScale, quantized_value)); - } -} - -void PortableMatrixBatchVectorMultiplyAccumulate(const float* matrix, - int m_rows, int m_cols, - const float* vector, - int n_batch, float* result) { - float* result_in_batch = result; - for (int b = 0; b < n_batch; b++) { - const float* matrix_ptr = matrix; - for (int r = 0; r < m_rows; r++) { - float dot_prod = 0.0f; - const float* vector_in_batch = vector + b * m_cols; - for (int c = 0; c < m_cols; c++) { - dot_prod += *matrix_ptr++ * *vector_in_batch++; - } - *result_in_batch += dot_prod; - ++result_in_batch; - } - } -} - -void PortableMatrixBatchVectorMultiplyAccumulate( - const int8_t* __restrict__ matrix, const int m_rows, const int m_cols, - const int8_t* __restrict__ vectors, const float* scaling_factors, - int n_batch, float* __restrict__ result) { - for (int batch = 0; batch < n_batch; ++batch, vectors += m_cols) { - const float batch_scaling_factor = scaling_factors[batch]; - // Get the address of the first row. - const int8_t* row_ptr = matrix; - for (int row = 0; row < m_rows; ++row) { - // Initialize the dot product sum for the row to 0. - int32_t dotprod = 0; -#if defined(__GNUC__) - // Prefetch the row to cache. - __builtin_prefetch(row_ptr, 0 /* prefetch for read */, - 3 /* temporal locality */); -#endif - for (int col = 0; col < m_cols; ++col, ++row_ptr) { - dotprod += (*row_ptr) * (vectors[col]); - } // for col - *result += dotprod * batch_scaling_factor; - ++result; - } // for row - } // for batch -} - -void PortableMatrixBatchVectorMultiplyAccumulate( - const int8_t* __restrict__ matrix, const int m_rows, const int m_cols, - const int8_t* __restrict__ vectors, const float* scaling_factors, - int n_batch, float* __restrict__ result, const float* per_channel_scale, - const int32_t* input_offset, int32_t* scratch, int32_t* row_sums, - bool* compute_row_sums, CpuBackendContext* context) { - if (input_offset == nullptr) { - PortableMatrixBatchVectorMultiplyAccumulate( - matrix, m_rows, m_cols, vectors, scaling_factors, n_batch, result); - return; - } - if (!compute_row_sums || *compute_row_sums) { - PortableReductionSumVector(matrix, row_sums, m_rows, m_cols); - if (compute_row_sums) { - *compute_row_sums = false; - } - } - - for (int batch = 0; batch < n_batch; ++batch, vectors += m_cols) { - const float batch_scaling_factor = scaling_factors[batch]; - const int32_t batch_offset = input_offset[batch]; - const int8_t* row_ptr = matrix; - for (int row = 0; row < m_rows; ++row) { - int32_t dotprod = 0; - float scale = batch_scaling_factor; - if (per_channel_scale) { - scale *= per_channel_scale[row]; - } -#if defined(__GNUC__) - // Prefetch the row to cache. - __builtin_prefetch(row_ptr, 0 /* prefetch for read */, - 3 /* temporal locality */); -#endif - for (int col = 0; col < m_cols; ++col, ++row_ptr) { - dotprod += (*row_ptr) * vectors[col]; - } // for col - dotprod -= row_sums[row] * batch_offset; - *result += dotprod * scale; - ++result; - } // for row - } // for batch -} - -void PortableSparseMatrixBatchVectorMultiplyAccumulate1x4( - const float* __restrict__ matrix, const int32_t* __restrict__ segments, - const int32_t* __restrict__ indices, int m_rows, int m_cols, - const float* __restrict__ vector, int n_batch, float* __restrict__ result) { - const int kBlockSize = 4; - TFLITE_DCHECK_EQ(m_cols % kBlockSize, 0); - for (int batch = 0; batch < n_batch; batch++) { - const float* matrix_ptr = matrix; - for (int row = 0; row < m_rows; row++) { - float dot_prod = 0.0f; - const float* vector_in_batch = vector + batch * m_cols; - for (int i = segments[row]; i < segments[row + 1]; i++) { - const int block_start_index = indices[i] * kBlockSize; - const float* vector_block_in_batch_ptr = - vector_in_batch + block_start_index; - for (int c = 0; c < kBlockSize; c++) { - dot_prod += *matrix_ptr++ * *vector_block_in_batch_ptr++; - } - } - result[batch * m_rows + row] += dot_prod; - } - } -} - -void PortableSparseMatrixBatchVectorMultiplyAccumulate1x16( - const int8_t* __restrict__ matrix, const int32_t* __restrict__ segments, - const int32_t* __restrict__ indices, int m_rows, int m_cols, - const int8_t* __restrict__ vector, const int32_t* __restrict__ bias_vector, - int n_batch, const int32_t input_offset, const int32_t output_multiplier, - const int32_t output_shift, const int32_t output_offset, - const int32_t output_activation_min, const int32_t output_activation_max, - int8_t* __restrict__ result) { - const int kBlockSize = 16; - TFLITE_DCHECK_EQ(m_cols % kBlockSize, 0); - for (int batch = 0; batch < n_batch; ++batch) { - const int8_t* matrix_ptr = matrix; - for (int row = 0; row < m_rows; ++row) { - int32_t dot_prod = 0; - const int8_t* vector_in_batch = vector + batch * m_cols; - for (int i = segments[row]; i < segments[row + 1]; ++i) { - const int block_start_index = indices[i] * kBlockSize; - const int8_t* vector_block_in_batch_ptr = - vector_in_batch + block_start_index; - for (int c = 0; c < kBlockSize; c++) { - dot_prod += *matrix_ptr * *vector_block_in_batch_ptr++; - dot_prod += *matrix_ptr++ * input_offset; - } - } - const int32_t bias_value = bias_vector != nullptr ? bias_vector[row] : 0; - dot_prod = MultiplyByQuantizedMultiplier(dot_prod + bias_value, - output_multiplier, output_shift); - dot_prod += output_offset; - result[batch * m_rows + row] = - static_cast(ActivationFunctionWithMinMax( - dot_prod, output_activation_min, output_activation_max)); - } - } -} - -void PortableSparseMatrixBatchVectorMultiplyAccumulate( - const float* __restrict__ matrix, const uint8_t* __restrict__ ledger, - int m_rows, int m_cols, const float* __restrict__ vector, int n_batch, - float* __restrict__ result) { - const int kBlockSize = 16; - TFLITE_DCHECK_EQ( // NOLINT - m_cols % kBlockSize, 0); - for (int batch = 0; batch < n_batch; batch++) { - const float* matrix_ptr = matrix; - const uint8_t* ledger_ptr = ledger; - for (int row = 0; row < m_rows; row++) { - float dot_prod = 0.0f; - int num_nonzero_blocks = *ledger_ptr++; - if (num_nonzero_blocks > 0) { - const float* vector_in_batch = vector + batch * m_cols; - for (int i = 0; i < num_nonzero_blocks; i++) { - const int block_start_index = *ledger_ptr++ * kBlockSize; - const float* vector_block_in_batch_ptr = - vector_in_batch + block_start_index; - for (int c = 0; c < kBlockSize; c++) { - dot_prod += *matrix_ptr++ * *vector_block_in_batch_ptr++; - } - } - } - result[batch * m_rows + row] += dot_prod; - } - } -} - -void PortableSparseMatrixBatchVectorMultiplyAccumulate( - const int8_t* __restrict__ matrix, const uint8_t* ledger, const int m_rows, - const int m_cols, const int8_t* __restrict__ vectors, - const float* scaling_factors, int n_batch, float* __restrict__ result) { - static const int kBlockSize = 16; - TFLITE_DCHECK_EQ( // NOLINT - m_cols % kBlockSize, 0); - for (int batch = 0; batch < n_batch; ++batch, vectors += m_cols) { - const float batch_scaling_factor = scaling_factors[batch]; - const uint8_t* ledger_ptr = ledger; - // Get the address of the first row. - const int8_t* row_ptr = matrix; - for (int row = 0; row < m_rows; ++row) { - // Initialize the dot product sum for the row to 0. - int32_t dotprod = 0; -#if defined(__GNUC__) - // Prefetch the row to cache. - __builtin_prefetch(row_ptr, 0 /* prefetch for read */, - 3 /* temporal locality */); -#endif - int num_nonzero_blocks = *ledger_ptr++; - for (int i = 0; i < num_nonzero_blocks; i++) { - const int block_start_index = *ledger_ptr++ * kBlockSize; - const int8_t* vector_block_ptr = vectors + block_start_index; - for (int c = 0; c < kBlockSize; c++) { - dotprod += (*row_ptr++) * (*vector_block_ptr++); - } // for block - } // for num_nonzero_blocks - result[batch * m_rows + row] += dotprod * batch_scaling_factor; - } // for row - } // for batch -} - -template -void PortableMatrixBatchVectorMultiplyAccumulateImpl( - const int8_t* input, const int32_t* bias, - const int8_t* input_to_gate_weights, int32_t multiplier, int32_t shift, - int32_t n_batch, int32_t n_input, int32_t n_output, int32_t output_zp, - T* output) { - const int16_t output_max = std::numeric_limits::max(); - const int16_t output_min = std::numeric_limits::min(); - for (int batch = 0; batch < n_batch; ++batch) { - for (int row = 0; row < n_output; ++row) { - int32_t acc = bias[row]; - for (int col = 0; col < n_input; ++col) { - int8_t input_val = input[batch * n_input + col]; - int8_t weights_val = input_to_gate_weights[row * n_input + col]; - acc += input_val * weights_val; - } - acc = MultiplyByQuantizedMultiplier(acc, multiplier, shift); - acc += output_zp; - acc += output[batch * n_output + row]; - if (acc > output_max) { - acc = output_max; - } - if (acc < output_min) { - acc = output_min; - } - output[batch * n_output + row] = static_cast(acc); - } - } -} - -void PortableMatrixBatchVectorMultiplyAccumulate( - const int8_t* input, const int32_t* bias, - const int8_t* input_to_gate_weights, int32_t multiplier, int32_t shift, - int32_t n_batch, int32_t n_input, int32_t n_output, int32_t output_zp, - int32_t* scratch, int16_t* output, CpuBackendContext* context) { - PortableMatrixBatchVectorMultiplyAccumulateImpl( - input, bias, input_to_gate_weights, multiplier, shift, n_batch, n_input, - n_output, output_zp, output); -} - -void PortableMatrixBatchVectorMultiplyAccumulate( - const int8_t* input, const int32_t* bias, - const int8_t* input_to_gate_weights, int32_t multiplier, int32_t shift, - int32_t n_batch, int32_t n_input, int32_t n_output, int32_t output_zp, - int32_t* scratch, int8_t* output, CpuBackendContext* context) { - PortableMatrixBatchVectorMultiplyAccumulateImpl( - input, bias, input_to_gate_weights, multiplier, shift, n_batch, n_input, - n_output, output_zp, output); -} - -void PortableMatrixBatchVectorMultiply(const int8_t* input, - int32_t input_zeropoint, - const int8_t* input_to_gate_weights, - int32_t input_to_gate_effective_scale_a, - int32_t input_to_gate_effective_scale_b, - int32_t n_batch, int32_t n_input, - int32_t n_cell, int8_t* gate_output, - int8_t gate_output_zp) { - const int32_t int8_max = std::numeric_limits::max(); - const int32_t int8_min = std::numeric_limits::min(); - for (int batch = 0; batch < n_batch; ++batch) { - for (int row = 0; row < n_cell; ++row) { - int32_t acc = 0; - for (int col = 0; col < n_input; ++col) { - int32_t input_val = input[batch * n_input + col]; - int8_t weights_val = input_to_gate_weights[row * n_input + col]; - acc += (input_val - input_zeropoint) * weights_val; - } - acc = MultiplyByQuantizedMultiplier(acc, input_to_gate_effective_scale_a, - input_to_gate_effective_scale_b); - acc += gate_output_zp; - if (acc > int8_max) { - acc = int8_max; - } - if (acc < int8_min) { - acc = int8_min; - } - gate_output[batch * n_cell + row] = static_cast(acc); - } - } -} - -void PortableMatrixBatchVectorMultiply( - const int16_t* hidden, const int8_t* hidden_to_output_weights, - int32_t proj_effective_scale_a, int32_t proj_effective_scale_b, - const int32_t* gate_bias, int32_t n_batch, int32_t n_hidden, - int32_t n_output, int32_t output_zp, int8_t* proj_output) { - const int16_t int8_max = std::numeric_limits::max(); - const int16_t int8_min = std::numeric_limits::min(); - for (int batch = 0; batch < n_batch; ++batch) { - for (int row = 0; row < n_output; ++row) { - int64_t acc = gate_bias[row]; - for (int col = 0; col < n_hidden; ++col) { - int16_t input_val = hidden[batch * n_hidden + col]; - int8_t weights_val = hidden_to_output_weights[row * n_hidden + col]; - int64_t curr = acc; - acc += input_val * weights_val; - if (input_val * weights_val > 0 && acc < curr) { - acc = std::numeric_limits::max(); - } - if (input_val * weights_val < 0 && acc > curr) { - acc = std::numeric_limits::min(); - } - } - acc = MultiplyByQuantizedMultiplier(acc, proj_effective_scale_a, - proj_effective_scale_b); - acc += output_zp; - if (acc > int8_max) { - acc = int8_max; - } - if (acc < int8_min) { - acc = int8_min; - } - proj_output[batch * n_output + row] = acc; - } - } -} - -void PortableApplyLayerNorm(const int16_t* input, - const int16_t* layer_norm_weights, - const int32_t* bias, int32_t layer_norm_scale_a, - int32_t layer_norm_scale_b, int32_t variance_limit, - int n_batch, int n_input, int16_t* output) { - // The square of std::pow(2, 10), which is the extra factor that makes sure - // normalized values has enough resolution. - static const int kTwoToPower20 = 1 << 20; - for (int i = 0; i < n_batch; ++i) { - int64_t sum = 0; - int64_t sum_sq = 0; - for (int j = 0; j < n_input; ++j) { - const int32_t index = i * n_input + j; - int32_t val = static_cast(input[index]); - sum += val; - sum_sq += val * val; - } - int32_t mean = - static_cast(static_cast(sum) * 1024 / n_input); - // TODO(b/173994730): Avoids overflow but only works for POT n_input. - int32_t temp = kTwoToPower20 / n_input; - int64_t variance = - sum_sq * temp - static_cast(mean) * static_cast(mean); - int32_t variance2 = static_cast(variance / kTwoToPower20); - if (variance2 < 1) { - variance2 = variance_limit; - } - int32_t stddev_inverse_a; - int stddev_inverse_b; - GetInvSqrtQuantizedMultiplierExp(variance2, /*reverse_shift*/ -1, - &stddev_inverse_a, &stddev_inverse_b); - - for (int j = 0; j < n_input; ++j) { - const int32_t index = i * n_input + j; - int32_t val = static_cast(input[index]); - int32_t shifted = 1024 * val - mean; - int32_t rescaled = MultiplyByQuantizedMultiplier( - shifted, stddev_inverse_a, stddev_inverse_b); - // TODO(jianlijianli): Saturate this. - int64_t val3 = rescaled * layer_norm_weights[j] + bias[j]; - int32_t val4 = - static_cast((val3 > 0 ? val3 + 512 : val3 - 512) / 1024); - int32_t val5 = MultiplyByQuantizedMultiplier(val4, layer_norm_scale_a, - layer_norm_scale_b + 12); - val5 = std::min(std::max(kInt16Min, val5), kInt16Max); - output[index] = static_cast(val5); - } - } -} - -void PortableApplyLayerNormFloat(const int16_t* input, - const int16_t* layer_norm_weights, - int32_t layer_norm_scale_a, - int32_t layer_norm_scale_b, - const int32_t* bias, int n_batch, int n_input, - int16_t* output) { - const int32_t int16_max = std::numeric_limits::max(); - const int32_t int16_min = std::numeric_limits::min(); - const float layer_norm_scale = - layer_norm_scale_a * - std::pow(2.0, static_cast(layer_norm_scale_b - 31)); - const float bias_scale = - static_cast(std::pow(2.0, -10)) * layer_norm_scale; - - for (int batch = 0; batch < n_batch; ++batch) { - float sum = 0.0f; - float sum_sq = 0.0f; - for (int i = 0; i < n_input; ++i) { - const int index = batch * n_input + i; - const float value = static_cast(input[index]); - sum += value; - sum_sq += value * value; - } - const float mean = sum / n_input; - float stddev_inv = 0.0f; - const float variance = sum_sq / n_input - mean * mean; - if (variance == 0) { - stddev_inv = 1.0f / std::sqrt(1e-8f); - } else { - stddev_inv = 1.0f / std::sqrt(variance); - } - for (int i = 0; i < n_input; ++i) { - const int index = batch * n_input + i; - const float normalized_value = - (static_cast(input[index]) - mean) * stddev_inv; - const float weighted_normalized_value = - normalized_value * layer_norm_weights[i] * layer_norm_scale + - bias[i] * bias_scale; - const int32_t quant_output = static_cast(round( - weighted_normalized_value * static_cast(std::pow(2, 12)))); - output[index] = std::min(int16_max, std::max(int16_min, quant_output)); - } - } -} - -void PortableMatrixScalarMultiplyAccumulate(const int8_t* matrix, - int32_t scalar, int32_t n_row, - int32_t n_col, int32_t* output) { - for (int i = 0; i < n_row; ++i) { - int32_t row_sum = 0; - for (int j = 0; j < n_col; ++j) { - row_sum += *matrix++; - } - output[i] += row_sum * scalar; - } -} - -void PortableApplySigmoid(const int16_t* input, int32_t n_batch, - int32_t n_input, int16_t* output) { - for (int batch = 0; batch < n_batch; ++batch) { - for (int c = 0; c < n_input; c++) { - using F3 = gemmlowp::FixedPoint; - using F0 = gemmlowp::FixedPoint; - const int index = batch * n_input + c; - F3 sigmoid_input = F3::FromRaw(input[index]); - F0 sigmoid_output = gemmlowp::logistic(sigmoid_input); - output[index] = sigmoid_output.raw(); - } - } -} - -void PortableApplySigmoidFloat(const int16_t* input, int32_t n_batch, - int32_t n_input, int16_t* output) { - const int32_t int16_max = std::numeric_limits::max(); - const int32_t int16_min = std::numeric_limits::min(); - for (int batch = 0; batch < n_batch; ++batch) { - for (int i = 0; i < n_input; ++i) { - const int index = batch * n_input + i; - const float float_input = - input[index] * static_cast(std::pow(2, -12)); - const float float_output = 1.0f / (1.0f + std::exp(-float_input)); - const int32_t quant_output = static_cast( - float_output * static_cast(std::pow(2, 15))); - const int32_t quant_output_clamped = - std::min(int16_max, std::max(int16_min, quant_output)); - output[index] = static_cast(quant_output_clamped); - } - } -} - -template -void PortableApplyTanhImpl(const int16_t* input, int32_t n_batch, - int32_t n_input, int16_t* output) { - using FX = gemmlowp::FixedPoint; - using F0 = gemmlowp::FixedPoint; - for (int batch = 0; batch < n_batch; ++batch) { - for (int i = 0; i < n_input; ++i) { - const int index = batch * n_input + i; - FX tanh_input = FX::FromRaw(input[index]); - F0 tanh_output = gemmlowp::tanh(tanh_input); - output[index] = tanh_output.raw(); - } - } -} - -void PortableApplyTanh(int32_t integer_bits, const int16_t* input, - int32_t n_batch, int32_t n_input, int16_t* output) { - assert(integer_bits <= 6); -#define DISPATCH_TANH(i) \ - case i: \ - PortableApplyTanhImpl(input, n_batch, n_input, output); \ - break; - switch (integer_bits) { - DISPATCH_TANH(0); - DISPATCH_TANH(1); - DISPATCH_TANH(2); - DISPATCH_TANH(3); - DISPATCH_TANH(4); - DISPATCH_TANH(5); - DISPATCH_TANH(6); - default: - return; - } -#undef DISPATCH_TANH -} - -void PortableApplyTanhFloat(const int16_t* input, int32_t n_batch, - int32_t n_input, int32_t integer_bits, - int16_t* output) { - const int32_t int16_max = std::numeric_limits::max(); - const int32_t int16_min = std::numeric_limits::min(); - const double two = 2.0; - for (int batch = 0; batch < n_batch; ++batch) { - for (int i = 0; i < n_input; ++i) { - const int index = batch * n_input + i; - const float float_input = - input[index] * std::pow(two, static_cast(integer_bits)); - const float float_output = std::tanh(float_input); - const int32_t quant_output = static_cast( - float_output * static_cast(std::pow(2, 15))); - const int32_t quant_output_clamped = - std::min(int16_max, std::max(int16_min, quant_output)); - output[index] = static_cast(quant_output_clamped); - } - } -} - -void PortableCwiseMul(const int16_t* input_1, const int16_t* input_2, - int n_batch, int n_input, int shift, int16_t* output) { - for (int batch = 0; batch < n_batch; ++batch) { - for (int i = 0; i < n_input; ++i) { - const int index = batch * n_input + i; - const int16_t a = input_1[index]; - const int16_t b = input_2[index]; - const int32_t value = static_cast(a) * static_cast(b); - output[index] = - static_cast(gemmlowp::RoundingDivideByPOT(value, shift)); - } - } -} - -void PortableCwiseMul(const int16_t* input_1, const int16_t* input_2, - int32_t multiplier, int32_t shift, int32_t n_batch, - int32_t n_input, int32_t output_zp, int8_t* output) { - for (int batch = 0; batch < n_batch; ++batch) { - for (int i = 0; i < n_input; ++i) { - const int index = batch * n_input + i; - const int16_t a = input_1[index]; - const int16_t b = input_2[index]; - int32_t value = static_cast(a) * static_cast(b); - value = MultiplyByQuantizedMultiplier(value, multiplier, shift); - value -= output_zp; - value = std::min(std::max(static_cast(-128), value), - static_cast(127)); - - output[index] = static_cast(value); - } - } -} - -void PortableCwiseAdd(const int16_t* input_1, const int16_t* input_2, - int n_batch, int n_input, int16_t* output) { - for (int batch = 0; batch < n_batch; ++batch) { - for (int i = 0; i < n_input; ++i) { - const int index = batch * n_input + i; - int32_t sum = input_1[index] + input_2[index]; - const int32_t sum_clamped = std::min(kInt16Max, std::max(kInt16Min, sum)); - output[index] = static_cast(sum_clamped); - } - } -} - -float PortableVectorVectorDotProduct(const float* vector1, const float* vector2, - int v_size) { - float result = 0.0; - for (int v = 0; v < v_size; v++) { - result += *vector1++ * *vector2++; - } - return result; -} - -namespace { -inline int32_t VectorVectorDotProduct(const int16_t* vector1, - const int16_t* vector2, int v_size) { - int32_t result = 0; - for (int v = 0; v < v_size; v++) { - result += *vector1++ * *vector2++; - } - return result; -} -} // namespace - -void PortableBatchVectorBatchVectorDotProduct(const int16_t* vector1, - const int16_t* vector2, - int v_size, int n_batch, - int32_t* result) { - for (int b = 0; b < n_batch; b++) { - result[b] = VectorVectorDotProduct(vector1, vector2, v_size); - vector1 += v_size; - vector2 += v_size; - } -} - -void PortableVectorBatchVectorCwiseProductAccumulate( - const int16_t* vector, int v_size, const int16_t* batch_vector, int n_batch, - int32_t multiplier, int shift, int16_t* result) { - for (int b = 0; b < n_batch; b++) { - for (int v = 0; v < v_size; v++) { - int32_t prod = vector[v] * *batch_vector++; - prod = MultiplyByQuantizedMultiplier(prod, multiplier, shift); - int32_t output = prod + *result; - output = std::max(std::min(static_cast(32767), output), - static_cast(-32768)); - *result++ = output; - } - } -} - -void PortableSub1Vector(const float* vector, int v_size, float* result) { - for (int v = 0; v < v_size; v++) { - *result++ = 1.0f - *vector++; - } -} - -void PortableSub1Vector(const int16_t* vector, int v_size, int16_t* result) { - static const int16_t kOne = 32767; - for (int v = 0; v < v_size; v++) { - *result++ = kOne - *vector++; - } -} - -void PortableVectorScalarMultiply(const int8_t* vector, const int v_size, - const float scale, float* result) { - for (int v = 0; v < v_size; ++v) { - *result++ = scale * *vector++; - } -} - -void PortableMeanStddevNormalization(const float* __restrict__ input_vector, - float* __restrict__ output_vector, - int v_size, int n_batch) { - for (int batch = 0; batch < n_batch; ++batch) { - float sum = 0.0f; - for (int i = 0; i < v_size; ++i) { - sum += input_vector[i]; - } - const float mean = sum / v_size; - float sum_diff_sq = 0.0f; - for (int i = 0; i < v_size; ++i) { - const float diff = input_vector[i] - mean; - sum_diff_sq += diff * diff; - } - const float variance = sum_diff_sq / v_size; - constexpr float kNormalizationConstant = 1e-8f; - const float stddev_inv = - 1.0f / std::sqrt(variance + kNormalizationConstant); - for (int i = 0; i < v_size; ++i) { - output_vector[i] = (input_vector[i] - mean) * stddev_inv; - } - input_vector += v_size; - output_vector += v_size; - } -} - -void PortableTwoGateSaturatingAdd(const int8_t* input, int8_t input_zp, - const int8_t* recurrent, int8_t recurrent_zp, - int32_t input_effective_scale_a, - int32_t input_effective_scale_b, - int32_t recurrent_effective_scale_a, - int32_t recurrent_effective_scale_b, - int32_t n_batch, int32_t n_cell, - int16_t* output) { - const int32_t int16_max = std::numeric_limits::max(); - const int32_t int16_min = std::numeric_limits::min(); - for (int i = 0; i < n_batch * n_cell; ++i) { - int32_t x = static_cast(input[i]) - static_cast(input_zp); - int32_t h = - static_cast(recurrent[i]) - static_cast(recurrent_zp); - int32_t x_scaled = MultiplyByQuantizedMultiplier(x, input_effective_scale_a, - input_effective_scale_b); - int32_t h_scaled = MultiplyByQuantizedMultiplier( - h, recurrent_effective_scale_a, recurrent_effective_scale_b); - int32_t y = h_scaled + x_scaled; - if (y > int16_max) { - y = int16_max; - } - if (y < int16_min) { - y = int16_min; - } - output[i] = static_cast(y); - } -} - -} // namespace tensor_utils -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/portable_tensor_utils.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/portable_tensor_utils.h deleted file mode 100644 index 0416db09..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/portable_tensor_utils.h +++ /dev/null @@ -1,333 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_PORTABLE_TENSOR_UTILS_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_PORTABLE_TENSOR_UTILS_H_ - -#include "tensorflow/lite/kernels/internal/reference/portable_tensor_utils_impl.h" - -#if defined(_MSC_VER) -#define __restrict__ __restrict -#endif - -namespace tflite { -namespace tensor_utils { - -// Check if all entries of a vector are zero for float. -bool IsZeroVector(const float* vector, int v_size) { - return PortableIsZeroVector(vector, v_size); -} - -// Check if all entries of a vector are zero for int8_t. -bool IsZeroVector(const int8_t* vector, int v_size) { - return PortableIsZeroVector(vector, v_size); -} - -void SymmetricQuantizeFloats(const float* values, const int size, - int8_t* quantized_values, float* min, float* max, - float* scaling_factor) { - PortableSymmetricQuantizeFloats(values, size, quantized_values, min, max, - scaling_factor); -} - -void SymmetricQuantizeFloats(const float* values, const int size, - int8_t* quantized_values, float min_value, - float max_value, float* scaling_factor) { - PortableSymmetricQuantizeFloats(values, size, quantized_values, min_value, - max_value, scaling_factor); -} - -void AsymmetricQuantizeFloats(const float* values, const int size, - int8_t* quantized_values, float* scaling_factor, - int32_t* offset) { - PortableAsymmetricQuantizeFloats(values, size, quantized_values, - scaling_factor, offset); -} - -void MatrixBatchVectorMultiplyAccumulate(const float* matrix, int m_rows, - int m_cols, const float* vector, - int n_batch, float* result) { - PortableMatrixBatchVectorMultiplyAccumulate(matrix, m_rows, m_cols, vector, - n_batch, result); -} - -void MatrixBatchVectorMultiplyAccumulate(const int8_t* __restrict__ matrix, - const int m_rows, const int m_cols, - const int8_t* __restrict__ vector, - const float* scaling_factors, - int n_batch, - float* __restrict__ result) { - PortableMatrixBatchVectorMultiplyAccumulate(matrix, m_rows, m_cols, vector, - scaling_factors, n_batch, result); -} - -void MatrixBatchVectorMultiplyAccumulate( - const int8_t* __restrict__ matrix, const int m_rows, const int m_cols, - const int8_t* __restrict__ vectors, const float* scaling_factors, - int n_batch, float* __restrict__ result, const float* per_channel_scale, - const int32_t* input_offset, int32_t* scratch, int32_t* row_sums, - bool* compute_row_sums, CpuBackendContext* context) { - PortableMatrixBatchVectorMultiplyAccumulate( - matrix, m_rows, m_cols, vectors, scaling_factors, n_batch, result, - per_channel_scale, input_offset, scratch, row_sums, compute_row_sums, - context); -} - -void MatrixBatchVectorMultiplyAccumulate(const int8_t* __restrict__ matrix, - const int m_rows, const int m_cols, - const int8_t* __restrict__ vector, - const float* scaling_factors, - int n_batch, int32_t* scratch, - float* __restrict__ result, - CpuBackendContext* context) { - PortableMatrixBatchVectorMultiplyAccumulate(matrix, m_rows, m_cols, vector, - scaling_factors, n_batch, result); -} - -void SparseMatrixBatchVectorMultiplyAccumulate1x4( - const float* __restrict__ matrix, const int32_t* __restrict__ segments, - const int32_t* __restrict__ indices, int m_rows, int m_cols, - const float* __restrict__ vector, int n_batch, float* __restrict__ result) { - PortableSparseMatrixBatchVectorMultiplyAccumulate1x4( - matrix, segments, indices, m_rows, m_cols, vector, n_batch, result); -} - -void SparseMatrixBatchVectorMultiplyAccumulate( - const float* __restrict__ matrix, const uint8_t* __restrict__ ledger, - int m_rows, int m_cols, const float* __restrict__ vector, int n_batch, - float* __restrict__ result) { - PortableSparseMatrixBatchVectorMultiplyAccumulate( - matrix, ledger, m_rows, m_cols, vector, n_batch, result); -} - -void SparseMatrixBatchVectorMultiplyAccumulate1x16( - const int8_t* __restrict__ matrix, const int32_t* __restrict__ segments, - const int32_t* __restrict__ indices, int m_rows, int m_cols, - const int8_t* __restrict__ vector, const int32_t* __restrict__ bias_vector, - int n_batch, const int32_t input_offset, const int32_t output_multiplier, - const int32_t output_shift, const int32_t output_offset, - const int32_t output_activation_min, const int32_t output_activation_max, - - int8_t* __restrict__ result) { - PortableSparseMatrixBatchVectorMultiplyAccumulate1x16( - matrix, segments, indices, m_rows, m_cols, vector, bias_vector, n_batch, - input_offset, output_multiplier, output_shift, output_offset, - output_activation_min, output_activation_max, result); -} - -void SparseMatrixBatchVectorMultiplyAccumulate( - const int8_t* __restrict__ matrix, const uint8_t* ledger, const int m_rows, - const int m_cols, const int8_t* __restrict__ vectors, - const float* scaling_factors, int n_batch, float* __restrict__ result) { - PortableSparseMatrixBatchVectorMultiplyAccumulate( - matrix, ledger, m_rows, m_cols, vectors, scaling_factors, n_batch, - result); -} - -void MatrixBatchVectorMultiplyAccumulate( - const int8_t* input, const int32_t* bias, - const int8_t* input_to_gate_weights, int32_t multiplier, int32_t shift, - int32_t n_batch, int32_t n_input, int32_t n_output, int32_t output_zp, - int32_t* scratch, int16_t* output, CpuBackendContext* context) { - PortableMatrixBatchVectorMultiplyAccumulate( - input, bias, input_to_gate_weights, multiplier, shift, n_batch, n_input, - n_output, output_zp, scratch, output, context); -} - -void MatrixBatchVectorMultiplyAccumulate( - const int8_t* input, const int32_t* bias, - const int8_t* input_to_gate_weights, int32_t multiplier, int32_t shift, - int32_t n_batch, int32_t n_input, int32_t n_output, int32_t output_zp, - int32_t* scratch, int8_t* output, CpuBackendContext* context) { - PortableMatrixBatchVectorMultiplyAccumulate( - input, bias, input_to_gate_weights, multiplier, shift, n_batch, n_input, - n_output, output_zp, scratch, output, context); -} - -void MatrixScalarMultiplyAccumulate(const int8_t* matrix, int32_t scalar, - int32_t n_row, int32_t n_col, - int32_t* output) { - PortableMatrixScalarMultiplyAccumulate(matrix, scalar, n_row, n_col, output); -} - -void MatrixBatchVectorMultiply(const int8_t* input, int32_t input_zeropoint, - const int8_t* input_to_gate_weights, - int32_t input_to_gate_effective_scale_a, - int32_t input_to_gate_effective_scale_b, - int32_t n_batch, int32_t n_input, int32_t n_cell, - int8_t* gate_output, int8_t gate_output_zp) { - PortableMatrixBatchVectorMultiply( - input, input_zeropoint, input_to_gate_weights, - input_to_gate_effective_scale_a, input_to_gate_effective_scale_b, n_batch, - n_input, n_cell, gate_output, gate_output_zp); -} - -void MatrixBatchVectorMultiply(const int16_t* hidden, - const int8_t* hidden_to_output_weights, - int32_t proj_effective_scale_a, - int32_t proj_effective_scale_b, - const int32_t* gate_bias, int32_t n_batch, - int32_t n_hidden, int32_t n_output, - int32_t output_zp, int8_t* proj_output) { - PortableMatrixBatchVectorMultiply(hidden, hidden_to_output_weights, - proj_effective_scale_a, - proj_effective_scale_b, gate_bias, n_batch, - n_hidden, n_output, output_zp, proj_output); -} - -void ApplyLayerNorm(const int16_t* input, const int16_t* layer_norm_weights, - const int32_t* bias, int32_t layer_norm_scale_a, - int32_t layer_norm_scale_b, int32_t variance_limit, - int n_batch, int n_input, int16_t* output) { - PortableApplyLayerNorm(input, layer_norm_weights, bias, layer_norm_scale_a, - layer_norm_scale_b, variance_limit, n_batch, n_input, - output); -} - -void ApplyLayerNormFloat(const int16_t* input, - const int16_t* layer_norm_weights, - int32_t layer_norm_scale_a, int32_t layer_norm_scale_b, - const int32_t* bias, int n_batch, int n_input, - int16_t* output) { - PortableApplyLayerNormFloat(input, layer_norm_weights, layer_norm_scale_a, - layer_norm_scale_b, bias, n_batch, n_input, - output); -} - -void ApplySigmoid(const int16_t* input, int32_t n_batch, int32_t n_input, - int16_t* output) { - PortableApplySigmoid(input, n_batch, n_input, output); -} - -void ApplySigmoidFloat(const int16_t* input, int32_t n_batch, int32_t n_input, - int16_t* output) { - PortableApplySigmoidFloat(input, n_batch, n_input, output); -} - -void ApplyTanh(int32_t integer_bits, const int16_t* input, int32_t n_batch, - int32_t n_input, int16_t* output) { - PortableApplyTanh(integer_bits, input, n_batch, n_input, output); -} - -void ApplyTanhFloat(const int16_t* input, int32_t n_batch, int32_t n_input, - int32_t integer_bits, int16_t* output) { - PortableApplyTanhFloat(input, n_batch, n_input, integer_bits, output); -} - -void CwiseMul(const int16_t* input_1, const int16_t* input_2, int n_batch, - int n_input, int shift, int16_t* output) { - PortableCwiseMul(input_1, input_2, n_batch, n_input, shift, output); -} - -void CwiseMul(const int16_t* input_1, const int16_t* input_2, - int32_t multiplier, int32_t shift, int32_t n_batch, - int32_t n_input, int32_t output_zp, int8_t* output) { - PortableCwiseMul(input_1, input_2, multiplier, shift, n_batch, n_input, - output_zp, output); -} - -void CwiseAdd(const int16_t* input_1, const int16_t* input_2, int n_batch, - int n_input, int16_t* output) { - PortableCwiseAdd(input_1, input_2, n_batch, n_input, output); -} - -void CwiseClipping(float* vector, const int v_size, - const float clipping_value) { - PortableCwiseClipping(vector, v_size, clipping_value); -} - -void CwiseClipping(int16_t* vector, const int v_size, - const int16_t clipping_value) { - PortableCwiseClipping(vector, v_size, clipping_value); -} - -void CwiseClipping(int8_t* vector, const int v_size, - const int8_t clipping_value) { - PortableCwiseClipping(vector, v_size, clipping_value); -} - -void VectorBatchVectorCwiseProductAccumulate(const int16_t* vector, int v_size, - const int16_t* batch_vector, - int n_batch, int32_t multiplier, - int shift, int16_t* result) { - PortableVectorBatchVectorCwiseProductAccumulate( - vector, v_size, batch_vector, n_batch, multiplier, shift, result); -} - -float VectorVectorDotProduct(const float* vector1, const float* vector2, - int v_size) { - return PortableVectorVectorDotProduct(vector1, vector2, v_size); -} - -void BatchVectorBatchVectorDotProduct(const int16_t* vector1, - const int16_t* vector2, int v_size, - int n_batch, int32_t* result) { - PortableBatchVectorBatchVectorDotProduct(vector1, vector2, v_size, n_batch, - result); -} - -void Sub1Vector(const float* vector, int v_size, float* result) { - PortableSub1Vector(vector, v_size, result); -} - -void Sub1Vector(const int16_t* vector, int v_size, int16_t* result) { - PortableSub1Vector(vector, v_size, result); -} - -// Multiply all elements of vector with a scalar. -void VectorScalarMultiply(const int8_t* vector, int v_size, float scale, - float* result) { - PortableVectorScalarMultiply(vector, v_size, scale, result); -} - -void ReductionSumVector(const float* input_vector, float* output_vector, - int output_size, int reduction_size) { - PortableReductionSumVector(input_vector, output_vector, output_size, - reduction_size); -} - -void ReductionSumVector(const int32_t* input_vector, int32_t* output_vector, - int output_size, int reduction_size) { - PortableReductionSumVector(input_vector, output_vector, output_size, - reduction_size); -} - -void ReductionSumVector(const int8_t* input_vector, int32_t* output_vector, - int output_size, int reduction_size) { - PortableReductionSumVector(input_vector, output_vector, output_size, - reduction_size); -} - -void MeanStddevNormalization(const float* input_vector, float* output_vector, - int v_size, int n_batch) { - PortableMeanStddevNormalization(input_vector, output_vector, v_size, n_batch); -} - -void TwoGateSaturatingAdd(const int8_t* input, int8_t input_zp, - const int8_t* recurrent, int8_t recurrent_zp, - int32_t input_effective_scale_a, - int32_t input_effective_scale_b, - int32_t recurrent_effective_scale_a, - int32_t recurrent_effective_scale_b, int32_t n_batch, - int32_t n_cell, int16_t* output) { - PortableTwoGateSaturatingAdd( - input, input_zp, recurrent, recurrent_zp, input_effective_scale_a, - input_effective_scale_b, recurrent_effective_scale_a, - recurrent_effective_scale_b, n_batch, n_cell, output); -} - -} // namespace tensor_utils -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_PORTABLE_TENSOR_UTILS_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/portable_tensor_utils_impl.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/portable_tensor_utils_impl.h deleted file mode 100644 index 6c404d5e..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/portable_tensor_utils_impl.h +++ /dev/null @@ -1,244 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_PORTABLE_TENSOR_UTILS_IMPL_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_PORTABLE_TENSOR_UTILS_IMPL_H_ - -#include -#include - -#if defined(_MSC_VER) -#define __restrict__ __restrict -#endif - -namespace tflite { - -// Not all backends support CpuBackendContext usage, so forward declare to avoid -// pulling in its implementation. -class CpuBackendContext; - -namespace tensor_utils { - -template -bool PortableIsZeroVector(const T* vector, int v_size) { - for (int i = 0; i < v_size; ++i) { - if (vector[i] != 0) { - return false; - } - } - return true; -} - -void PortableSymmetricQuantizeFloats(const float* values, const int size, - int8_t* quantized_values, float* min_value, - float* max_value, float* scaling_factor); - -void PortableSymmetricQuantizeFloats(const float* values, const int size, - int8_t* quantized_values, float min_value, - float max_value, float* scaling_factor); - -void PortableAsymmetricQuantizeFloats(const float* values, const int size, - int8_t* quantized_values, - float* scaling_factor, int32_t* offset); - -// Multiply a matrix by a batch vector, and store results in a batch-size -// vector. -void PortableMatrixBatchVectorMultiplyAccumulate(const float* matrix, - int m_rows, int m_cols, - const float* vector, - int n_batch, float* result); - -void PortableMatrixBatchVectorMultiplyAccumulate( - const int8_t* __restrict__ matrix, const int m_rows, const int m_cols, - const int8_t* __restrict__ vectors, const float* scaling_factors, - int n_batch, float* __restrict__ result); - -void PortableMatrixBatchVectorMultiplyAccumulate( - const int8_t* __restrict__ matrix, const int m_rows, const int m_cols, - const int8_t* __restrict__ vectors, const float* scaling_factors, - int n_batch, float* __restrict__ result, const float* per_channel_scale, - const int32_t* input_offset, int32_t* scratch, int32_t* row_sums, - bool* compute_row_sums, CpuBackendContext* context); - -void PortableMatrixBatchVectorMultiplyAccumulate( - const int8_t* __restrict__ matrix, const int m_rows, const int m_cols, - const int8_t* __restrict__ vector, const float* scaling_factors, - int n_batch, int32_t* scratch, float* __restrict__ result, - CpuBackendContext* context); - -void PortableSparseMatrixBatchVectorMultiplyAccumulate1x4( - const float* __restrict__ matrix, const int32_t* __restrict__ segments, - const int32_t* __restrict__ indices, int m_rows, int m_cols, - const float* __restrict__ vector, int n_batch, float* __restrict__ result); - -void PortableSparseMatrixBatchVectorMultiplyAccumulate( - const float* __restrict__ matrix, const uint8_t* __restrict__ ledger, - int m_rows, int m_cols, const float* __restrict__ vector, int n_batch, - float* __restrict__ result); - -void PortableSparseMatrixBatchVectorMultiplyAccumulate1x16( - const int8_t* __restrict__ matrix, const int32_t* __restrict__ segments, - const int32_t* __restrict__ indices, int m_rows, int m_cols, - const int8_t* __restrict__ vector, const int32_t* __restrict__ bias_vector, - int n_batch, const int32_t input_offset, const int32_t output_multiplier, - const int32_t output_shift, const int32_t output_offset, - const int32_t output_activation_min, const int32_t output_activation_max, - int8_t* __restrict__ result); - -void PortableSparseMatrixBatchVectorMultiplyAccumulate( - const int8_t* __restrict__ matrix, const uint8_t* ledger, const int m_rows, - const int m_cols, const int8_t* __restrict__ vectors, - const float* scaling_factors, int n_batch, float* __restrict__ result); - -// Dot product of two vectors. -float PortableVectorVectorDotProduct(const float* vector1, const float* vector2, - int v_size); - -void PortableBatchVectorBatchVectorDotProduct(const int16_t* vector1, - const int16_t* vector2, - int v_size, int n_batch, - int32_t* result); - -void PortableVectorBatchVectorCwiseProductAccumulate( - const int16_t* vector, int v_size, const int16_t* batch_vector, int n_batch, - int32_t multiplier, int shift, int16_t* result); - -void PortableMatrixBatchVectorMultiplyAccumulate( - const int8_t* input, const int32_t* bias, - const int8_t* input_to_gate_weights, int32_t multiplier, int32_t shift, - int32_t n_batch, int32_t n_input, int32_t n_output, int32_t output_zp, - int32_t* scratch, int16_t* output, CpuBackendContext* context); - -void PortableMatrixBatchVectorMultiplyAccumulate( - const int8_t* input, const int32_t* bias, - const int8_t* input_to_gate_weights, int32_t multiplier, int32_t shift, - int32_t n_batch, int32_t n_input, int32_t n_output, int32_t output_zp, - int32_t* scratch, int8_t* output, CpuBackendContext* context); - -void PortableMatrixBatchVectorMultiply(const int8_t* input, - int32_t input_zeropoint, - const int8_t* input_to_gate_weights, - int32_t input_to_gate_effective_scale_a, - int32_t input_to_gate_effective_scale_b, - int32_t n_batch, int32_t n_input, - int32_t n_cell, int8_t* gate_output, - int8_t gate_output_zp); - -void PortableMatrixBatchVectorMultiply( - const int16_t* hidden, const int8_t* hidden_to_output_weights, - int32_t proj_effective_scale_a, int32_t proj_effective_scale_b, - const int32_t* gate_bias, int32_t n_batch, int32_t n_hidden, - int32_t n_output, int32_t output_zp, int8_t* proj_output); - -void PortableMatrixScalarMultiplyAccumulate(const int8_t* matrix, - int32_t scalar, int32_t n_row, - int32_t n_col, int32_t* output); - -void PortableApplyLayerNorm(const int16_t* input, - const int16_t* layer_norm_weights, - const int32_t* bias, int32_t layer_norm_scale_a, - int32_t layer_norm_scale_b, int32_t variance_limit, - int n_batch, int n_input, int16_t* output); - -void PortableApplyLayerNormFloat(const int16_t* input, - const int16_t* layer_norm_weights, - int32_t layer_norm_scale_a, - int32_t layer_norm_scale_b, - const int32_t* bias, int n_batch, int n_input, - int16_t* output); - -void PortableApplySigmoid(const int16_t* input, int32_t n_batch, - int32_t n_input, int16_t* output); - -void PortableApplySigmoidFloat(const int16_t* input, int32_t n_batch, - int32_t n_input, int16_t* output); - -void PortableApplyTanh(int32_t integer_bits, const int16_t* input, - int32_t n_batch, int32_t n_input, int16_t* output); - -void PortableApplyTanhFloat(const int16_t* input, int32_t n_batch, - int32_t n_input, int32_t integer_bits, - int16_t* output); - -void PortableCwiseMul(const int16_t* input_1, const int16_t* input_2, - int n_batch, int n_input, int shift, int16_t* output); - -void PortableCwiseMul(const int16_t* input_1, const int16_t* input_2, - int32_t multiplier, int32_t shift, int32_t n_batch, - int32_t n_input, int32_t output_zp, int8_t* output); - -void PortableCwiseAdd(const int16_t* input_1, const int16_t* input_2, - int n_batch, int n_input, int16_t* output); - -template -void PortableCwiseClipping(T* vector, const int v_size, - const T& clipping_value) { - for (int i = 0; i < v_size; i++) { - vector[i] = std::max(std::min(clipping_value, vector[i]), - static_cast(-clipping_value)); - } -} - -// Batch vector initialization with another vector. -void PortableVectorBatchVectorAssign(const float* vector, int v_size, - int n_batch, float* batch_vector); - -// Compute "1.0f - elements of vector" (used in CIFG). -void PortableSub1Vector(const float* vector, int v_size, float* result); - -void PortableSub1Vector(const int16_t* vector, int v_size, int16_t* result); - -// Multiply all elements of vector with a scalar. -void PortableVectorScalarMultiply(const int8_t* vector, int v_size, float scale, - float* result); - -// Reduce-sum on a vector: -// input_vector: pointer to input vector. -// output_vector: pointer to vector. -// output_size: output vector size. -// reduction_size: number of consecutive elements from input vector which are -// added to get one element of output. -template -void PortableReductionSumVector(const INPUT* input_vector, - OUTPUT* output_vector, int output_size, - int reduction_size) { - for (int o = 0; o < output_size; o++) { - OUTPUT result = 0; - for (int r = 0; r < reduction_size; r++) { - result += input_vector[r]; - } - output_vector[o] = result; - input_vector += reduction_size; - } -} - -// Layer norm for each batch. -void PortableMeanStddevNormalization(const float* __restrict__ input_vector, - float* __restrict__ output_vector, - int v_size, int n_batch); - -// Saturate Add. -void PortableTwoGateSaturatingAdd(const int8_t* input, int8_t input_zp, - const int8_t* recurrent, int8_t recurrent_zp, - int32_t input_effective_scale_a, - int32_t input_effective_scale_b, - int32_t recurrent_effective_scale_a, - int32_t recurrent_effective_scale_b, - int32_t n_batch, int32_t n_cell, - int16_t* output); - -} // namespace tensor_utils -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_PORTABLE_TENSOR_UTILS_IMPL_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/prelu.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/prelu.h deleted file mode 100644 index aa9901d6..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/prelu.h +++ /dev/null @@ -1,111 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_PRELU_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_PRELU_H_ - -#include - -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -namespace reference_ops { - -// Broadcast prelu to output_shape for quantized uint8_t/int8_t data. -template -inline void BroadcastPrelu4DSlow( - const PreluParams& params, const RuntimeShape& input_shape, - const T* input_data, const RuntimeShape& alpha_shape, const T* alpha_data, - const RuntimeShape& output_shape, T* output_data) { - TFLITE_DCHECK_LE(input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(alpha_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(output_shape.DimensionsCount(), 4); - const RuntimeShape extended_output_shape = - RuntimeShape::ExtendedShape(4, output_shape); - NdArrayDesc<4> desc1; - NdArrayDesc<4> desc2; - NdArrayDescsForElementwiseBroadcast(input_shape, alpha_shape, &desc1, &desc2); - - for (int b = 0; b < extended_output_shape.Dims(0); ++b) { - for (int y = 0; y < extended_output_shape.Dims(1); ++y) { - for (int x = 0; x < extended_output_shape.Dims(2); ++x) { - for (int c = 0; c < extended_output_shape.Dims(3); ++c) { - int output_index = Offset(extended_output_shape, b, y, x, c); - int input_index = SubscriptToIndex(desc1, b, y, x, c); - const int32_t input_value = - params.input_offset + input_data[input_index]; - int32_t output_value; - if (input_value >= 0) { - output_value = MultiplyByQuantizedMultiplier( - input_value, params.output_multiplier_1, params.output_shift_1); - } else { - auto alpha_index = SubscriptToIndex(desc2, b, y, x, c); - const int32_t alpha_value = - params.alpha_offset + alpha_data[alpha_index]; - - output_value = MultiplyByQuantizedMultiplier( - input_value * alpha_value, params.output_multiplier_2, - params.output_shift_2); - } - output_value += params.output_offset; - - const int32_t quantized_min = std::numeric_limits::min(); - const int32_t quantized_max = std::numeric_limits::max(); - const int32_t clamped_output = - std::min(quantized_max, std::max(quantized_min, output_value)); - output_data[output_index] = static_cast(clamped_output); - } - } - } - } -} - -template -inline void Prelu(const PreluParams& params, const RuntimeShape& input_shape, - const T* input_data, const RuntimeShape& alpha_shape, - const T* alpha_data, const RuntimeShape& output_shape, - T* output_data) { - const int32_t quantized_min = std::numeric_limits::min(); - const int32_t quantized_max = std::numeric_limits::max(); - - const int flat_size = - MatchingElementsSize(input_shape, alpha_shape, output_shape); - for (int i = 0; i < flat_size; ++i) { - const int32_t input_value = params.input_offset + input_data[i]; - int32_t output_value; - if (input_value >= 0) { - output_value = MultiplyByQuantizedMultiplier( - input_value, params.output_multiplier_1, params.output_shift_1); - } else { - const int32_t alpha_value = params.alpha_offset + alpha_data[i]; - - output_value = MultiplyByQuantizedMultiplier(input_value * alpha_value, - params.output_multiplier_2, - params.output_shift_2); - } - output_value += params.output_offset; - - const int32_t clamped_output = - std::min(quantized_max, std::max(quantized_min, output_value)); - output_data[i] = static_cast(clamped_output); - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_PRELU_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/process_broadcast_shapes.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/process_broadcast_shapes.h deleted file mode 100644 index bda27693..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/process_broadcast_shapes.h +++ /dev/null @@ -1,140 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_PROCESS_BROADCAST_SHAPES_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_PROCESS_BROADCAST_SHAPES_H_ - -#include - -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -namespace reference_ops { - -// Consolidates dimensions in broadcast inputs, checks for five-fold pattern. -// -// For example, if sequence of dimensions of one input is -// ..., 1, 3, 1, 7, 9, 5,... and the other is ..., 2, 3, 1, 7, 1, 1, ... -// we can consolidate these as -// ..., 1, 3*7, 9*5, ... and 2, 3*7, 1. -// -// The category is updated in the less-frequent case of shapes that are -// not suited to a fivefold-loop broadcast. -// -// Falls back to generic pattern when it does not know how to process properly. -// -// Returns true iff there is some sort of broadcast, which includes five-fold -// patterns and falling back to generic broadcast. -inline bool ProcessBroadcastShapes(const RuntimeShape& shape0, - const RuntimeShape& shape1, - tflite::ArithmeticParams* params) { - const int dims_count = - std::max(shape0.DimensionsCount(), shape1.DimensionsCount()); - - params->broadcast_category = BroadcastableOpCategory::kGenericBroadcast; - RuntimeShape scalar_shape(dims_count, 1); - - auto extended_shape0 = RuntimeShape::ExtendedShape(dims_count, shape0); - auto extended_shape1 = RuntimeShape::ExtendedShape(dims_count, shape1); - - // Check for "exact" match, implicitly accepting any scalar shapes. - if (extended_shape0 == extended_shape1) { - params->broadcast_category = BroadcastableOpCategory::kNonBroadcast; - return false; - } - - for (int i = dims_count - 1; i >= 0; --i) { - if (extended_shape0.Dims(i) == extended_shape1.Dims(i)) { - continue; - } else if (extended_shape0.Dims(i) == 1) { - params->broadcast_category = - BroadcastableOpCategory::kFirstInputBroadcastsFast; - break; - } else if (extended_shape1.Dims(i) == 1) { - params->broadcast_category = - BroadcastableOpCategory::kSecondInputBroadcastsFast; - break; - } else { - // This case is erroneous: there is a dimension that does not match and - // is not a broadcast from one shape to the other. - params->broadcast_category = BroadcastableOpCategory::kGenericBroadcast; - return true; - } - } - - if (params->broadcast_category != - BroadcastableOpCategory::kFirstInputBroadcastsFast && - params->broadcast_category != - BroadcastableOpCategory::kSecondInputBroadcastsFast) { - // This is unreachable because at least one else clause in the above loop - // must be reached. - TFLITE_DCHECK(false); - params->broadcast_category = BroadcastableOpCategory::kNonBroadcast; - return false; - } - - // From this point it is assumed contractually that corresponding dimensions - // in shape0 and shape1 are either (a) equal or (b) one or other equals 1. - const bool swap_inputs = params->broadcast_category == - BroadcastableOpCategory::kSecondInputBroadcastsFast; - const RuntimeShape* shape_a = - swap_inputs ? &extended_shape1 : &extended_shape0; - const RuntimeShape* shape_b = - swap_inputs ? &extended_shape0 : &extended_shape1; - - int i = dims_count - 1; - params->broadcast_shape[0] = 1; - params->broadcast_shape[1] = 1; - params->broadcast_shape[2] = 1; - params->broadcast_shape[3] = 1; - params->broadcast_shape[4] = 1; - // y_0 is greedy: include dims if both or neither equal 1: in other words, - // test for equality rather than (shape_a->Dims(i) != 1). - while (i >= 0 && shape_a->Dims(i) == shape_b->Dims(i)) { - params->broadcast_shape[4] *= shape_b->Dims(i); - --i; - } - // Here either input_a or input_b has dim of 1 (if i >= 0). If it is input_b - // that has the unit dimension, the next two loops are not entered. - while (i >= 0 && shape_a->Dims(i) == 1) { - params->broadcast_shape[3] *= shape_b->Dims(i); - --i; - } - while (i >= 0 && shape_a->Dims(i) == shape_b->Dims(i)) { - params->broadcast_shape[2] *= shape_a->Dims(i); - --i; - } - // Here either input_a or input_b has dim of 1 (if i >= 0). - while (i >= 0 && shape_b->Dims(i) == 1) { - params->broadcast_shape[1] *= shape_a->Dims(i); - --i; - } - while (i >= 0 && shape_a->Dims(i) == shape_b->Dims(i)) { - params->broadcast_shape[0] *= shape_b->Dims(i); - --i; - } - - // Rarer case is when the broadcast dimensions cannot be handled by a fivefold - // loop. - if (i >= 0) { - params->broadcast_category = BroadcastableOpCategory::kGenericBroadcast; - } - return true; -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_PROCESS_BROADCAST_SHAPES_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/quantize.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/quantize.h deleted file mode 100644 index f304b641..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/quantize.h +++ /dev/null @@ -1,89 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_QUANTIZE_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_QUANTIZE_H_ - -#include -#include -#include - -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/internal/cppmath.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -namespace reference_ops { - -template -inline void AffineQuantize(const tflite::QuantizationParams& op_params, - const RuntimeShape& input_shape, - const InputT* input_data, - const RuntimeShape& output_shape, - OutputT* output_data) { - const int32_t zero_point = op_params.zero_point; - const double scale = op_params.scale; - const int flat_size = MatchingFlatSize(input_shape, output_shape); - static constexpr int32_t min_val = std::numeric_limits::min(); - static constexpr int32_t max_val = std::numeric_limits::max(); - - for (int i = 0; i < flat_size; i++) { - const InputT val = input_data[i]; - int32_t unclamped = - static_cast(TfLiteRound(val / static_cast(scale))) + - zero_point; - int32_t clamped = std::min(std::max(unclamped, min_val), max_val); - output_data[i] = clamped; - } -} - -// Quantizes per-channel. -template -inline void PerChannelQuantize( - const tflite::PerChannelQuantizationParams& op_params, - const RuntimeShape& input_shape, const InputT* input_data, - const RuntimeShape& output_shape, OutputT* output_data) { - // Ensure flat size is same. - MatchingFlatSize(input_shape, output_shape); - - const int32_t* zero_point = op_params.zero_point; - const float* scale = op_params.scale; - const int32_t quantized_dimension = op_params.quantized_dimension; - const int32_t num_dims = input_shape.DimensionsCount(); - const int32_t* dims_data = input_shape.DimsData(); - std::vector current_dim(num_dims, 0); - static constexpr int32_t min_val = std::numeric_limits::min(); - static constexpr int32_t max_val = std::numeric_limits::max(); - - do { - size_t offset = - ReducedOutputOffset(num_dims, reinterpret_cast(dims_data), - current_dim.data(), 0, nullptr); - const InputT val = input_data[offset]; - const int channel = current_dim[quantized_dimension]; - int32_t unclamped = static_cast(TfLiteRound( - val / static_cast(scale[channel]))) + - zero_point[channel]; - int32_t clamped = std::min(std::max(unclamped, min_val), max_val); - output_data[offset] = static_cast(clamped); - } while (NextIndex(num_dims, reinterpret_cast(dims_data), - current_dim.data())); -} - -} // namespace reference_ops - -} // namespace tflite -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_QUANTIZE_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/reduce.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/reduce.h deleted file mode 100644 index 341b3a08..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/reduce.h +++ /dev/null @@ -1,526 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_REDUCE_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_REDUCE_H_ - -#include - -#include "ruy/profiler/instrumentation.h" // from @ruy -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/cppmath.h" -#include "tensorflow/lite/kernels/internal/max.h" -#include "tensorflow/lite/kernels/internal/min.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/types.h" - -// Check if the reduction at index is the first one along the dimensions given -// in axis. -inline bool IsFirstReduction(const int* index, const int num_axis, - const int* axis) { - if (num_axis == 0) { - return true; - } - - TFLITE_DCHECK(index != nullptr); - TFLITE_DCHECK(axis != nullptr); - for (int axis_idx = 0; axis_idx < num_axis; ++axis_idx) { - if (index[axis[axis_idx]] != 0) { - return false; - } - } - - return true; -} - -namespace tflite { - -namespace reference_ops { - -// A generic reduce method that can be used for reduce_sum, reduce_mean, etc. -// This method iterates through input data and reduce elements along the -// dimensions given in axis. -template -inline bool Reduce(const In* input_data, const int* input_dims, - const int* output_dims, const int input_num_dims, - const int output_num_dims, const int* axis, - const int num_axis, int* input_iter, - Out reducer(Out current, const In in), Out* output_data) { - // Reset input iterator. - for (int idx = 0; idx < input_num_dims; ++idx) { - input_iter[idx] = 0; - } - // Iterate through input_data. - do { - size_t input_offset = - ReducedOutputOffset(input_num_dims, input_dims, input_iter, 0, nullptr); - size_t output_offset = ReducedOutputOffset(input_num_dims, input_dims, - input_iter, num_axis, axis); - output_data[output_offset] = - reducer(output_data[output_offset], input_data[input_offset]); - } while (NextIndex(input_num_dims, input_dims, input_iter)); - return true; -} - -// Similar to above Reduce function but takes two reducer functions. -// The 'reducer_first' is called with the first value of the reduction, -// 'reducer_next' is then called for all the others. -template -inline bool Reduce(const In* input_data, const int* input_dims, - const int* output_dims, const int input_num_dims, - const int output_num_dims, const int* axis, - const int num_axis, int* input_iter, - const std::function& reducer_first, - const std::function& reducer_next, - Out* output_data) { - // Reset input iterator. - for (int idx = 0; idx < input_num_dims; ++idx) { - input_iter[idx] = 0; - } - // Iterate through input_data. - do { - size_t input_offset = - ReducedOutputOffset(input_num_dims, input_dims, input_iter, 0, nullptr); - size_t output_offset = ReducedOutputOffset(input_num_dims, input_dims, - input_iter, num_axis, axis); - if (IsFirstReduction(input_iter, num_axis, axis)) { - output_data[output_offset] = reducer_first(input_data[input_offset]); - } else { - output_data[output_offset] = - reducer_next(output_data[output_offset], input_data[input_offset]); - } - } while (NextIndex(input_num_dims, input_dims, input_iter)); - return true; -} - -// This method parses the input 'axis' to remove duplicates and handle negative -// values, and returns a valid 'out_axis' -inline bool ResolveAxis(const int num_dims, const int* axis, - const int64_t num_axis, int* out_axis, - int* out_num_axis) { - *out_num_axis = 0; // Just in case. - // Short-circuit axis resolution for scalars; the axis will go unused. - if (num_dims == 0) { - return true; - } - // o(n^2) is fine since out_num_axis should be really small, mostly <= 4 - for (int64_t idx = 0; idx < num_axis; ++idx) { - // Handle negative index. A positive index 'p_idx' can be represented as a - // negative index 'n_idx' as: n_idx = p_idx-num_dims - // eg: For num_dims=3, [0, 1, 2] is the same as [-3, -2, -1] */ - int current = axis[idx] < 0 ? (axis[idx] + num_dims) : axis[idx]; - TFLITE_DCHECK(current >= 0 && current < num_dims); - if (current < 0 || current >= num_dims) { - return false; - } - bool is_dup = false; - for (int j = 0; j < *out_num_axis; ++j) { - if (out_axis[j] == current) { - is_dup = true; - break; - } - } - if (!is_dup) { - out_axis[*out_num_axis] = current; - *out_num_axis += 1; - } - } - return true; -} - -// This method expects that output_data has been initialized. -template -inline bool ReduceSumImpl(const In* input_data, const int* input_dims, - const int* output_dims, const int input_num_dims, - const int output_num_dims, const int* axis, - const int num_axis, int* input_iter, - Out* output_data) { - auto reducer = [](const Out current, const In in) -> Out { - const Out actual_in = static_cast(in); - return current + actual_in; - }; - return Reduce(input_data, input_dims, output_dims, input_num_dims, - output_num_dims, axis, num_axis, input_iter, reducer, - output_data); -} - -template -inline bool InitTensorDataForReduce(const int* dims, const int num_dims, - const T init_value, T* data) { - size_t num_elements = 1; - for (int idx = 0; idx < num_dims; ++idx) { - size_t current = static_cast(dims[idx]); - // Overflow prevention. - if (current > 0 && - num_elements > std::numeric_limits::max() / current) { - return false; - } - num_elements *= current; - } - for (size_t idx = 0; idx < num_elements; ++idx) { - data[idx] = init_value; - } - return true; -} - -// Computes the generic value (i.e., sum/max/min/prod) of elements across -// dimensions given in axis. It needs to pass in init_value and reducer. -template -inline bool ReduceGeneric(const T* input_data, const int* input_dims, - const int input_num_dims, T* output_data, - const int* output_dims, const int output_num_dims, - const int* axis, const int64_t num_axis_dimensions, - bool keep_dims, int* temp_index, int* resolved_axis, - T init_value, - T reducer(const T current, const T in)) { - // Reset output data. - if (!InitTensorDataForReduce(output_dims, output_num_dims, init_value, - output_data)) { - return false; - } - - // Return early when input shape has zero dim. This is done after initializing - // data for output tensor because there are cases that the input tensor is - // empty but output tensor is not. In that case, output tensor should be - // filled with init_value. - for (int i = 0; i < input_num_dims; ++i) { - if (input_dims[i] == 0) return true; - } - - // Resolve axis. - int num_resolved_axis = 0; - if (!ResolveAxis(input_num_dims, axis, num_axis_dimensions, resolved_axis, - &num_resolved_axis)) { - return false; - } - - return Reduce(input_data, input_dims, output_dims, input_num_dims, - output_num_dims, resolved_axis, num_resolved_axis, - temp_index, reducer, output_data); -} - -// Computes the mean of elements across dimensions given in axis. -// It does so in two stages, first calculates the sum of elements along the axis -// then divides it by the number of element in axis. -template -inline bool Mean(const T* input_data, const int* input_dims, - const int input_num_dims, T* output_data, - const int* output_dims, const int output_num_dims, - const int* axis, const int num_axis_dimensions, bool keep_dims, - int* temp_index, int* resolved_axis, U* temp_sum) { - ruy::profiler::ScopeLabel label("Mean"); - // Reset output data. - size_t num_outputs = 1; - for (int idx = 0; idx < output_num_dims; ++idx) { - size_t current = static_cast(output_dims[idx]); - // Overflow prevention. - if (num_outputs > std::numeric_limits::max() / current) { - return false; - } - num_outputs *= current; - } - for (size_t idx = 0; idx < num_outputs; ++idx) { - output_data[idx] = T(); - temp_sum[idx] = U(); - } - - // Resolve axis. - int num_resolved_axis = 0; - if (!ResolveAxis(input_num_dims, axis, num_axis_dimensions, resolved_axis, - &num_resolved_axis)) { - return false; - } - - if (!ReduceSumImpl(input_data, input_dims, output_dims, input_num_dims, - output_num_dims, resolved_axis, num_resolved_axis, - temp_index, temp_sum)) { - return false; - } - - // Calculate mean by dividing output_data by num of aggregated element. - size_t num_elements_in_axis = 1; - for (int idx = 0; idx < num_resolved_axis; ++idx) { - size_t current = static_cast(input_dims[resolved_axis[idx]]); - // Overflow prevention. - if (current > (std::numeric_limits::max() / num_elements_in_axis)) { - return false; - } - num_elements_in_axis *= current; - } - - if (num_elements_in_axis > 0) { - for (size_t idx = 0; idx < num_outputs; ++idx) { - output_data[idx] = - static_cast(temp_sum[idx] / static_cast(num_elements_in_axis)); - } - } - return true; -} - -template -inline void Mean(const tflite::MeanParams& op_params, - const RuntimeShape& unextended_input_shape, - const T* input_data, - const RuntimeShape& unextended_output_shape, T* output_data) { - ruy::profiler::ScopeLabel label("Mean4D"); - - // Current implementation only supports dimension equals 4 and simultaneous - // reduction over width and height. - TFLITE_CHECK_EQ(unextended_input_shape.DimensionsCount(), 4); - TFLITE_CHECK_LE(unextended_output_shape.DimensionsCount(), 4); - const RuntimeShape input_shape = - RuntimeShape::ExtendedShape(4, unextended_input_shape); - const RuntimeShape output_shape = - RuntimeShape::ExtendedShape(4, unextended_output_shape); - - const int output_batch = output_shape.Dims(0); - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - const int output_depth = output_shape.Dims(3); - - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - - TFLITE_CHECK_EQ(op_params.axis_count, 2); - TFLITE_CHECK((op_params.axis[0] == 1 && op_params.axis[1] == 2) || - (op_params.axis[0] == 2 && op_params.axis[1] == 1)); - TFLITE_CHECK_EQ(output_height, 1); - TFLITE_CHECK_EQ(output_width, 1); - - for (int out_b = 0; out_b < output_batch; ++out_b) { - for (int out_d = 0; out_d < output_depth; ++out_d) { - float value = 0; - for (int in_h = 0; in_h < input_height; ++in_h) { - for (int in_w = 0; in_w < input_width; ++in_w) { - value += input_data[Offset(input_shape, out_b, in_h, in_w, out_d)]; - } - } - output_data[Offset(output_shape, out_b, 0, 0, out_d)] = - value / (input_width * input_height); - } - } -} - -inline void Mean(const tflite::MeanParams& op_params, - const RuntimeShape& unextended_input_shape, - const uint8_t* input_data, int32_t input_zero_point, - float input_scale, const RuntimeShape& unextended_output_shape, - uint8_t* output_data, int32_t output_zero_point, - float output_scale) { - ruy::profiler::ScopeLabel label("Mean4D/Uint8"); - - // Current implementation only supports dimension equals 4 and simultaneous - // reduction over width and height. - TFLITE_CHECK_EQ(unextended_input_shape.DimensionsCount(), 4); - TFLITE_CHECK_LE(unextended_output_shape.DimensionsCount(), 4); - const RuntimeShape input_shape = - RuntimeShape::ExtendedShape(4, unextended_input_shape); - const RuntimeShape output_shape = - RuntimeShape::ExtendedShape(4, unextended_output_shape); - const int output_batch = output_shape.Dims(0); - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - const int output_depth = output_shape.Dims(3); - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - const float num_elements_in_axis = input_width * input_height; - - TFLITE_CHECK_EQ(op_params.axis_count, 2); - TFLITE_CHECK((op_params.axis[0] == 1 && op_params.axis[1] == 2) || - (op_params.axis[0] == 2 && op_params.axis[1] == 1)); - TFLITE_CHECK_EQ(output_height, 1); - TFLITE_CHECK_EQ(output_width, 1); - - constexpr int32_t kMinValue = std::numeric_limits::min(); - constexpr int32_t kMaxValue = std::numeric_limits::max(); - - float temp = input_zero_point * input_scale / output_scale; - temp = temp > 0 ? temp + 0.5f : temp - 0.5f; - int32_t bias = output_zero_point - static_cast(temp); - double real_scale = - static_cast(input_scale / (num_elements_in_axis * output_scale)); - - int32_t multiplier; - int shift; - QuantizeMultiplier(real_scale, &multiplier, &shift); - for (int out_b = 0; out_b < output_batch; ++out_b) { - for (int out_d = 0; out_d < output_depth; ++out_d) { - int32_t acc = 0; - for (int in_h = 0; in_h < input_height; ++in_h) { - for (int in_w = 0; in_w < input_width; ++in_w) { - acc += input_data[Offset(input_shape, out_b, in_h, in_w, out_d)]; - } - } - acc = MultiplyByQuantizedMultiplier(acc, multiplier, shift); - acc += bias; - acc = std::min(std::max(acc, kMinValue), kMaxValue); - output_data[Offset(output_shape, out_b, 0, 0, out_d)] = - static_cast(acc); - } - } -} - -// Computes the mean of elements across dimensions given in axis. -// It does so in two stages, first calculates the sum of elements along the axis -// then divides it by the number of element in axis for quantized values. -template -inline bool QuantizedMeanOrSum(const T* input_data, int32_t input_zero_point, - float input_scale, const int* input_dims, - const int input_num_dims, T* output_data, - int32_t output_zero_point, float output_scale, - const int* output_dims, - const int output_num_dims, const int* axis, - const int num_axis_dimensions, bool keep_dims, - int* temp_index, int* resolved_axis, U* temp_sum, - bool compute_sum) { - const bool uint8_case = std::is_same::value; - const bool int16_case = std::is_same::value; - if (uint8_case) { - ruy::profiler::ScopeLabel label(compute_sum ? "Sum/Uint8" : "Mean/Uint8"); - } else if (int16_case) { - ruy::profiler::ScopeLabel label(compute_sum ? "Sum/Int16" : "Mean/Int16"); - } else { - ruy::profiler::ScopeLabel label(compute_sum ? "Sum/Int8" : "Mean/Int8"); - } - // Reset output data. - size_t num_outputs = 1; - for (int idx = 0; idx < output_num_dims; ++idx) { - size_t current = static_cast(output_dims[idx]); - // Overflow prevention. - if (num_outputs > std::numeric_limits::max() / current) { - return false; - } - num_outputs *= current; - } - for (size_t idx = 0; idx < num_outputs; ++idx) { - output_data[idx] = T(); - temp_sum[idx] = U(); - } - - // Return early when input shape has zero dim. This is done after initializing - // data for output tensor because there are cases that the input tensor is - // empty but output tensor is not. In that case, output tensor should be - // filled with init_value. - for (int i = 0; i < input_num_dims; ++i) { - if (input_dims[i] == 0) return true; - } - - // Resolve axis. - int num_resolved_axis = 0; - if (!ResolveAxis(input_num_dims, axis, num_axis_dimensions, resolved_axis, - &num_resolved_axis)) { - return false; - } - - if (!ReduceSumImpl(input_data, input_dims, output_dims, input_num_dims, - output_num_dims, resolved_axis, num_resolved_axis, - temp_index, temp_sum)) { - return false; - } - - // Calculate mean by dividing output_data by num of aggregated element. - size_t num_elements_in_axis = 1; - for (int idx = 0; idx < num_resolved_axis; ++idx) { - size_t current = static_cast(input_dims[resolved_axis[idx]]); - // Overflow prevention. - if (current > (std::numeric_limits::max() / num_elements_in_axis)) { - return false; - } - num_elements_in_axis *= current; - } - - if (num_elements_in_axis > 0) { - const float scale = input_scale / output_scale; - if (compute_sum) { - // TODO(b/116341117): Eliminate float and do this completely in 8bit. - const float bias = -input_zero_point * scale * num_elements_in_axis; - for (size_t idx = 0; idx < num_outputs; ++idx) { - const U value = - static_cast(TfLiteRound(temp_sum[idx] * scale + bias)) + - output_zero_point; - output_data[idx] = static_cast(value); - } - } else { - const float bias = -input_zero_point * scale; - for (size_t idx = 0; idx < num_outputs; ++idx) { - float float_mean = static_cast(temp_sum[idx]) / - static_cast(num_elements_in_axis); - float result = TfLiteMin( - TfLiteRound(float_mean * scale + bias) + output_zero_point, - static_cast(std::numeric_limits::max())); - result = TfLiteMax(result, - static_cast(std::numeric_limits::min())); - output_data[idx] = static_cast(result); - } - } - } - return true; -} - -template -inline bool QuantizedReduceProd(const T* input_data, int32_t input_zero_point, - const RuntimeShape& input_shape, T* output_data, - int32_t output_zero_point, - const RuntimeShape& output_shape, - const int* axis, - const int64_t num_axis_dimensions, - bool keep_dims, int* temp_index, - int* resolved_axis, int32_t* temp_prod, - int32_t scaling_multiplier, int scaling_shift) { - const int32_t kMinValue = std::numeric_limits::min(); - const int32_t kMaxValue = std::numeric_limits::max(); - - // Resolve axis. - int num_resolved_axis = 0; - if (!ResolveAxis(input_shape.DimensionsCount(), axis, num_axis_dimensions, - resolved_axis, &num_resolved_axis)) { - return false; - } - - // Calculate the reduced product by rescaling each multiplication step to - // avoid an overflow. - auto reducer_first = [&](T in) -> int32_t { return in - input_zero_point; }; - - auto reducer_next = [&](int32_t current, T in) -> int32_t { - const int64_t result = - static_cast(current) * (in - input_zero_point); - return MultiplyByQuantizedMultiplier(result, scaling_multiplier, - scaling_shift); - }; - - if (!Reduce( - input_data, input_shape.DimsData(), output_shape.DimsData(), - input_shape.DimensionsCount(), output_shape.DimensionsCount(), - resolved_axis, num_resolved_axis, temp_index, reducer_first, - reducer_next, temp_prod)) { - return false; - } - - for (int i = 0; i < output_shape.FlatSize(); i++) { - int32_t result = - MultiplyByQuantizedMultiplier(static_cast(temp_prod[i]), - scaling_multiplier, scaling_shift) + - output_zero_point; - result = std::min(std::max(result, kMinValue), kMaxValue); - output_data[i] = static_cast(result); - } - - return true; -} - -} // namespace reference_ops - -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_REDUCE_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/requantize.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/requantize.h deleted file mode 100644 index f35f6fc8..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/requantize.h +++ /dev/null @@ -1,70 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_REQUANTIZE_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_REQUANTIZE_H_ - -#include - -#include "ruy/profiler/instrumentation.h" // from @ruy -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { -namespace reference_ops { - -template -inline void Requantize(const input_type* input_data, int32_t size, - int32_t effective_scale_multiplier, - int32_t effective_scale_shift, int32_t input_zeropoint, - int32_t output_zeropoint, output_type* output_data) { - ruy::profiler::ScopeLabel label("Requantize"); - const bool same_scale = - (effective_scale_multiplier == 1 << 30 && effective_scale_shift == 1); - if (same_scale) { - const bool mixed_type_int8_uint8 = - std::is_same::value && - std::is_same::value; - const bool mixed_type_uint8_int8 = - std::is_same::value && - std::is_same::value; - const int32_t zero_point_diff = input_zeropoint - output_zeropoint; - // Fast path to do requantization for the case when just a shift of 128 is - // needed. - if ((mixed_type_int8_uint8 && zero_point_diff == -128) || - (mixed_type_uint8_int8 && zero_point_diff == 128)) { - for (int i = 0; i < size; ++i) { - output_data[i] = input_data[i] ^ 0x80; - } - return; - } - } - static constexpr int32_t kMinOutput = std::numeric_limits::min(); - static constexpr int32_t kMaxOutput = std::numeric_limits::max(); - for (int i = 0; i < size; ++i) { - const int32_t input = input_data[i] - input_zeropoint; - const int32_t output = - MultiplyByQuantizedMultiplier(input, effective_scale_multiplier, - effective_scale_shift) + - output_zeropoint; - const int32_t clamped_output = - std::max(std::min(output, kMaxOutput), kMinOutput); - output_data[i] = static_cast(clamped_output); - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_REQUANTIZE_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/resize_bilinear.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/resize_bilinear.h deleted file mode 100644 index b5edadb9..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/resize_bilinear.h +++ /dev/null @@ -1,228 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_RESIZE_BILINEAR_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_RESIZE_BILINEAR_H_ - -#include -#include -#include -#include - -#include "tensorflow/lite/kernels/internal/cppmath.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { -namespace reference_ops { - -inline void ComputeInterpolationValues(const float value, const float scale, - const bool half_pixel_centers, - int32_t input_size, float* scaled_value, - int32_t* lower_bound, - int32_t* upper_bound) { - if (half_pixel_centers) { - *scaled_value = (value + 0.5f) * scale - 0.5f; - } else { - *scaled_value = value * scale; - } - float scaled_value_floor = std::floor(*scaled_value); - *lower_bound = std::max(static_cast(scaled_value_floor), - static_cast(0)); - *upper_bound = - std::min(static_cast(std::ceil(*scaled_value)), input_size - 1); -} - -template -inline void ResizeBilinear(const tflite::ResizeBilinearParams& op_params, - const RuntimeShape& unextended_input_shape, - const T* input_data, - const RuntimeShape& unextended_output_size_shape, - const int32_t* output_size_data, - const RuntimeShape& unextended_output_shape, - T* output_data) { - // If half_pixel_centers is True, align_corners must be False. - TFLITE_DCHECK(!op_params.half_pixel_centers || !op_params.align_corners); - TFLITE_DCHECK_LE(unextended_input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(unextended_output_size_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(unextended_output_shape.DimensionsCount(), 4); - const RuntimeShape input_shape = - RuntimeShape::ExtendedShape(4, unextended_input_shape); - const RuntimeShape output_size_shape = - RuntimeShape::ExtendedShape(4, unextended_output_size_shape); - const RuntimeShape output_shape = - RuntimeShape::ExtendedShape(4, unextended_output_shape); - - int32_t batches = MatchingDim(input_shape, 0, output_shape, 0); - int32_t input_height = input_shape.Dims(1); - int32_t input_width = input_shape.Dims(2); - int32_t depth = MatchingDim(input_shape, 3, output_shape, 3); - - TFLITE_DCHECK_EQ(output_size_shape.Dims(0), 1); - TFLITE_DCHECK_EQ(output_size_shape.Dims(1), 1); - TFLITE_DCHECK_EQ(output_size_shape.Dims(2), 1); - TFLITE_DCHECK_EQ(output_size_shape.Dims(3), 2); - int32_t output_height = - output_size_data[Offset(output_size_shape, 0, 0, 0, 0)]; - int32_t output_width = - output_size_data[Offset(output_size_shape, 0, 0, 0, 1)]; - - float height_scale = static_cast(input_height) / output_height; - float width_scale = static_cast(input_width) / output_width; - if (op_params.align_corners && output_height > 1) { - height_scale = static_cast(input_height - 1) / (output_height - 1); - } - if (op_params.align_corners && output_width > 1) { - width_scale = static_cast(input_width - 1) / (output_width - 1); - } - const float rounding_offset = std::numeric_limits::is_integer ? .5f : .0f; - - for (int b = 0; b < batches; ++b) { - for (int y = 0; y < output_height; ++y) { - float input_y; - int32_t y0, y1; - ComputeInterpolationValues(y, height_scale, op_params.half_pixel_centers, - input_height, &input_y, &y0, &y1); - for (int x = 0; x < output_width; ++x) { - float input_x; - int32_t x0, x1; - ComputeInterpolationValues(x, width_scale, op_params.half_pixel_centers, - input_width, &input_x, &x0, &x1); - for (int c = 0; c < depth; ++c) { - T interpolation = - static_cast(input_data[Offset(input_shape, b, y0, x0, c)] * - (1 - (input_y - y0)) * (1 - (input_x - x0)) + - input_data[Offset(input_shape, b, y1, x0, c)] * - (input_y - y0) * (1 - (input_x - x0)) + - input_data[Offset(input_shape, b, y0, x1, c)] * - (1 - (input_y - y0)) * (input_x - x0) + - input_data[Offset(input_shape, b, y1, x1, c)] * - (input_y - y0) * (input_x - x0) + - rounding_offset); - output_data[Offset(output_shape, b, y, x, c)] = interpolation; - } - } - } - } -} - -inline void ComputeInterpolationValuesInteger( - const int32_t value, const int32_t scale_10, const bool half_pixel_centers, - int32_t input_size, int32_t* scaled_value, int32_t* lower_bound, - int32_t* upper_bound) { - if (half_pixel_centers) { - *scaled_value = value * scale_10 + scale_10 / 2 - (1 << 9); - } else { - *scaled_value = value * scale_10; - } - constexpr int32_t zero = 0; - *lower_bound = std::max(*scaled_value / (1 << 10), zero); - *upper_bound = - std::min((*scaled_value + (1 << 10) - 1) / (1 << 10), input_size - 1); -} - -// Same as above but doesn't use any floating-point for the resize -template -inline void ResizeBilinearInteger( - const tflite::ResizeBilinearParams& op_params, - const RuntimeShape& unextended_input_shape, const T* input_data, - const RuntimeShape& unextended_output_size_shape, - const int32_t* output_size_data, - const RuntimeShape& unextended_output_shape, T* output_data) { - // If half_pixel_centers is True, align_corners must be False. - TFLITE_DCHECK(!op_params.half_pixel_centers || !op_params.align_corners); - TFLITE_DCHECK_LE(unextended_input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(unextended_output_size_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(unextended_output_shape.DimensionsCount(), 4); - const RuntimeShape input_shape = - RuntimeShape::ExtendedShape(4, unextended_input_shape); - const RuntimeShape output_size_shape = - RuntimeShape::ExtendedShape(4, unextended_output_size_shape); - const RuntimeShape output_shape = - RuntimeShape::ExtendedShape(4, unextended_output_shape); - - const int32_t batches = MatchingDim(input_shape, 0, output_shape, 0); - const int32_t input_height = input_shape.Dims(1); - const int32_t input_width = input_shape.Dims(2); - const int32_t depth = MatchingDim(input_shape, 3, output_shape, 3); - - TFLITE_DCHECK_EQ(output_size_shape.Dims(0), 1); - TFLITE_DCHECK_EQ(output_size_shape.Dims(1), 1); - TFLITE_DCHECK_EQ(output_size_shape.Dims(2), 1); - TFLITE_DCHECK_EQ(output_size_shape.Dims(3), 2); - const int32_t output_height = - output_size_data[Offset(output_size_shape, 0, 0, 0, 0)]; - const int32_t output_width = - output_size_data[Offset(output_size_shape, 0, 0, 0, 1)]; - - int32_t height_scale_10 = - ((1 << 10) * input_height + output_height / 2) / output_height; - int32_t width_scale_10 = - ((1 << 10) * input_width + output_width / 2) / output_width; - if (op_params.align_corners && output_height > 1) { - height_scale_10 = - ((1 << 10) * (input_height - 1) + (output_height - 1) / 2) / - (output_height - 1); - } - if (op_params.align_corners && output_width > 1) { - width_scale_10 = ((1 << 10) * (input_width - 1) + (output_width - 1) / 2) / - (output_width - 1); - } - - for (int b = 0; b < batches; ++b) { - for (int y = 0; y < output_height; ++y) { - int32_t input_y, y0, y1; - ComputeInterpolationValuesInteger(y, height_scale_10, - op_params.half_pixel_centers, - input_height, &input_y, &y0, &y1); - for (int x = 0; x < output_width; ++x) { - int32_t input_x, x0, x1; - ComputeInterpolationValuesInteger(x, width_scale_10, - op_params.half_pixel_centers, - input_width, &input_x, &x0, &x1); - for (int c = 0; c < depth; ++c) { - const int64_t output_20_ll = - static_cast( - input_data[Offset(input_shape, b, y0, x0, c)]) * - ((1 << 10) - (input_y - (1 << 10) * y0)) * - ((1 << 10) - (input_x - (1 << 10) * x0)); - const int64_t output_20_lu = - static_cast( - input_data[Offset(input_shape, b, y1, x0, c)]) * - (input_y - (1 << 10) * y0) * - ((1 << 10) - (input_x - (1 << 10) * x0)); - const int64_t output_20_rl = - static_cast( - input_data[Offset(input_shape, b, y0, x1, c)]) * - ((1 << 10) - (input_y - (1 << 10) * y0)) * - (input_x - (1 << 10) * x0); - const int64_t output_20_ru = - static_cast( - input_data[Offset(input_shape, b, y1, x1, c)]) * - (input_y - (1 << 10) * y0) * (input_x - (1 << 10) * x0); - const int64_t output_20 = - output_20_ll + output_20_lu + output_20_rl + output_20_ru; - const int64_t round = (output_20 > 0) ? (1 << 19) : -(1 << 19); - const T interpolation = - static_cast((output_20 + round) / (1 << 20)); - output_data[Offset(output_shape, b, y, x, c)] = interpolation; - } - } - } - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_RESIZE_BILINEAR_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/resize_nearest_neighbor.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/resize_nearest_neighbor.h deleted file mode 100644 index bf0b757e..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/resize_nearest_neighbor.h +++ /dev/null @@ -1,102 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_RESIZE_NEAREST_NEIGHBOR_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_RESIZE_NEAREST_NEIGHBOR_H_ - -#include -#include - -#include "tensorflow/lite/kernels/internal/cppmath.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -namespace reference_ops { - -inline int32_t GetNearestNeighbor(const int input_value, - const int32_t input_size, - const int32_t output_size, - const bool align_corners, - const bool half_pixel_centers) { - const float scale = - (align_corners && output_size > 1) - ? (input_size - 1) / static_cast(output_size - 1) - : input_size / static_cast(output_size); - const float offset = half_pixel_centers ? 0.5f : 0.0f; - int32_t output_value = std::min( - align_corners - ? static_cast(TfLiteRound((input_value + offset) * scale)) - : static_cast(std::floor((input_value + offset) * scale)), - input_size - 1); - if (half_pixel_centers) { - output_value = std::max(static_cast(0), output_value); - } - return output_value; -} - -template -inline void ResizeNearestNeighbor( - const tflite::ResizeNearestNeighborParams& op_params, - const RuntimeShape& unextended_input_shape, const T* input_data, - const RuntimeShape& output_size_shape, const int32_t* output_size_data, - const RuntimeShape& unextended_output_shape, T* output_data) { - TFLITE_DCHECK_LE(unextended_input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(unextended_output_shape.DimensionsCount(), 4); - - const RuntimeShape input_shape = - RuntimeShape::ExtendedShape(4, unextended_input_shape); - const RuntimeShape output_shape = - RuntimeShape::ExtendedShape(4, unextended_output_shape); - - int32_t batches = MatchingDim(input_shape, 0, output_shape, 0); - int32_t input_height = input_shape.Dims(1); - int32_t input_width = input_shape.Dims(2); - int32_t depth = MatchingDim(input_shape, 3, output_shape, 3); - - // The Tensorflow version of this op allows resize on the width and height - // axis only. - TFLITE_DCHECK_EQ(output_size_shape.FlatSize(), 2); - int32_t output_height = output_size_data[0]; - int32_t output_width = output_size_data[1]; - - const int col_offset = input_shape.Dims(3); - const int row_offset = input_shape.Dims(2) * col_offset; - const int batch_offset = input_shape.Dims(1) * row_offset; - - const T* input_ptr = input_data; - T* output_ptr = output_data; - for (int b = 0; b < batches; ++b) { - for (int y = 0; y < output_height; ++y) { - int32_t in_y = GetNearestNeighbor(y, input_height, output_height, - op_params.align_corners, - op_params.half_pixel_centers); - const T* y_input_ptr = input_ptr + in_y * row_offset; - for (int x = 0; x < output_width; ++x) { - int32_t in_x = GetNearestNeighbor(x, input_width, output_width, - op_params.align_corners, - op_params.half_pixel_centers); - const T* x_input_ptr = y_input_ptr + in_x * col_offset; - memcpy(output_ptr, x_input_ptr, depth * sizeof(T)); - output_ptr += depth; - } - } - input_ptr += batch_offset; - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_RESIZE_NEAREST_NEIGHBOR_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/round.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/round.h deleted file mode 100644 index 9bd8f3f2..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/round.h +++ /dev/null @@ -1,51 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_ROUND_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_ROUND_H_ - -#include - -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -namespace reference_ops { - -inline float RoundToNearest(float value) { - auto floor_val = std::floor(value); - auto diff = value - floor_val; - if ((diff < 0.5f) || - ((diff == 0.5f) && (static_cast(floor_val) % 2 == 0))) { - return floor_val; - } else { - return floor_val = floor_val + 1.0f; - } -} - -inline void Round(const RuntimeShape& input_shape, const float* input_data, - const RuntimeShape& output_shape, float* output_data) { - const int flat_size = MatchingFlatSize(input_shape, output_shape); - for (int i = 0; i < flat_size; ++i) { - // Note that this implementation matches that of tensorFlow tf.round - // and corresponds to the bankers rounding method. - // cfenv (for fesetround) is not yet supported universally on Android, so - // using a work around. - output_data[i] = RoundToNearest(input_data[i]); - } -} - -} // namespace reference_ops -} // namespace tflite -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_ROUND_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/slice.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/slice.h deleted file mode 100644 index cb73ea0d..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/slice.h +++ /dev/null @@ -1,80 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_SLICE_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_SLICE_H_ - -#include "tensorflow/lite/kernels/internal/portable_tensor.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -namespace reference_ops { - -template -inline void Slice(const tflite::SliceParams& op_params, - const RuntimeShape& input_shape, - const RuntimeShape& output_shape, - SequentialTensorWriter* writer) { - const RuntimeShape ext_shape = RuntimeShape::ExtendedShape(5, input_shape); - TFLITE_DCHECK_LE(op_params.begin_count, 5); - TFLITE_DCHECK_LE(op_params.size_count, 5); - const int begin_count = op_params.begin_count; - const int size_count = op_params.size_count; - // We front-pad the begin and size vectors. - int start[5]; - int stop[5]; - for (int i = 0; i < 5; ++i) { - int padded_i = 5 - i; - start[i] = - begin_count < padded_i ? 0 : op_params.begin[begin_count - padded_i]; - stop[i] = - (size_count < padded_i || op_params.size[size_count - padded_i] == -1) - ? ext_shape.Dims(i) - : start[i] + op_params.size[size_count - padded_i]; - } - - for (int i0 = start[0]; i0 < stop[0]; ++i0) { - for (int i1 = start[1]; i1 < stop[1]; ++i1) { - for (int i2 = start[2]; i2 < stop[2]; ++i2) { - for (int i3 = start[3]; i3 < stop[3]; ++i3) { - for (int i4 = start[4]; i4 < stop[4]; ++i4) { - writer->Write(Offset(ext_shape, i0, i1, i2, i3, i4)); - } - } - } - } - } -} - -template -inline void Slice(const tflite::SliceParams& op_params, - const RuntimeShape& input_shape, const T* input_data, - const RuntimeShape& output_shape, T* output_data) { - SequentialTensorWriter writer(input_data, output_data); - return Slice(op_params, input_shape, output_shape, &writer); -} - -template -inline void Slice(const tflite::SliceParams& op_params, - const RuntimeShape& input_shape, const TfLiteTensor* input, - const RuntimeShape& output_shape, TfLiteTensor* output) { - SequentialTensorWriter writer(input, output); - return Slice(op_params, input_shape, output_shape, &writer); -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_SLICE_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/softmax.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/softmax.h deleted file mode 100644 index 9f4b6398..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/softmax.h +++ /dev/null @@ -1,233 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_SOFTMAX_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_SOFTMAX_H_ - -#include -#include - -#include "fixedpoint/fixedpoint.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/cppmath.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/types.h" -#include "tensorflow/lite/kernels/op_macros.h" - -namespace tflite { -namespace reference_ops { - -inline void Softmax(const SoftmaxParams& params, - const RuntimeShape& input_shape, const float* input_data, - const RuntimeShape& output_shape, float* output_data) { - const int trailing_dim = input_shape.DimensionsCount() - 1; - const int outer_size = - MatchingFlatSizeSkipDim(input_shape, trailing_dim, output_shape); - const int depth = - MatchingDim(input_shape, trailing_dim, output_shape, trailing_dim); - - for (int i = 0; i < outer_size; ++i) { - // Find max element value which we'll use to ensure numerical stability - // taking advantage of the following equality: - // exp(x[i])/sum(exp(x[i])) == exp(x[i]+C)/sum(exp(x[i]+C)) - float max = std::numeric_limits::lowest(); - for (int c = 0; c < depth; ++c) { - max = std::max(max, input_data[i * depth + c]); - } - - // Compute sum. - float sum = 0.f; - for (int c = 0; c < depth; ++c) { - const float exp_c = std::exp((input_data[i * depth + c] - max) * - static_cast(params.beta)); - output_data[i * depth + c] = exp_c; - sum += exp_c; - } - - // Compute result. - for (int c = 0; c < depth; ++c) { - output_data[i * depth + c] = output_data[i * depth + c] / sum; - } - } -} - -// Quantized softmax with int8_t/uint8_t input and int8_t/uint8_t/int16_t -// output. -template -inline void Softmax(const SoftmaxParams& params, - const RuntimeShape& input_shape, const InputT* input_data, - const RuntimeShape& output_shape, OutputT* output_data) { - const int32_t input_beta_multiplier = params.input_multiplier; - const int32_t input_beta_left_shift = params.input_left_shift; - const int diff_min = params.diff_min; - // The representation chosen for the input to the exp() function is Q5.26. - // We need to leave extra space since values that we skip might be as large as - // -32 before multiplying by input_beta_multiplier, and therefore as large as - // -16 afterwards. Note that exp(-8) is definitely not insignificant to - // accumulation, but exp(-16) definitely is. - static const int kScaledDiffIntegerBits = 5; - static const int kAccumulationIntegerBits = 12; - using FixedPointScaledDiff = - gemmlowp::FixedPoint; - using FixedPointAccum = - gemmlowp::FixedPoint; - using FixedPoint0 = gemmlowp::FixedPoint; - - const int trailing_dim = input_shape.DimensionsCount() - 1; - const int outer_size = - MatchingFlatSizeSkipDim(input_shape, trailing_dim, output_shape); - const int depth = - MatchingDim(input_shape, trailing_dim, output_shape, trailing_dim); - - for (int i = 0; i < outer_size; ++i) { - InputT max_in_row = std::numeric_limits::min(); - for (int c = 0; c < depth; ++c) { - max_in_row = std::max(max_in_row, input_data[i * depth + c]); - } - - FixedPointAccum sum_of_exps = FixedPointAccum::Zero(); - for (int c = 0; c < depth; ++c) { - int32_t input_diff = - static_cast(input_data[i * depth + c]) - max_in_row; - if (input_diff >= diff_min) { - const int32_t input_diff_rescaled = - MultiplyByQuantizedMultiplierGreaterThanOne( - input_diff, input_beta_multiplier, input_beta_left_shift); - const FixedPointScaledDiff scaled_diff_f8 = - FixedPointScaledDiff::FromRaw(input_diff_rescaled); - sum_of_exps = sum_of_exps + gemmlowp::Rescale( - exp_on_negative_values(scaled_diff_f8)); - } - } - - int num_bits_over_unit; - FixedPoint0 shifted_scale = FixedPoint0::FromRaw(GetReciprocal( - sum_of_exps.raw(), kAccumulationIntegerBits, &num_bits_over_unit)); - - for (int c = 0; c < depth; ++c) { - int32_t input_diff = - static_cast(input_data[i * depth + c]) - max_in_row; - if (input_diff >= diff_min) { - const int32_t input_diff_rescaled = - MultiplyByQuantizedMultiplierGreaterThanOne( - input_diff, input_beta_multiplier, input_beta_left_shift); - const FixedPointScaledDiff scaled_diff_f8 = - FixedPointScaledDiff::FromRaw(input_diff_rescaled); - - FixedPoint0 exp_in_0 = exp_on_negative_values(scaled_diff_f8); - int32_t unsat_output = gemmlowp::RoundingDivideByPOT( - (shifted_scale * exp_in_0).raw(), - num_bits_over_unit + 31 - (sizeof(OutputT) * 8)); - - const int32_t shifted_output = - unsat_output + - static_cast(std::numeric_limits::min()); - - output_data[i * depth + c] = static_cast(std::max( - std::min(shifted_output, - static_cast(std::numeric_limits::max())), - static_cast(std::numeric_limits::min()))); - } else { - output_data[i * depth + c] = std::numeric_limits::min(); - } - } - } -} - -// Computes exp(input - max_input) -inline int16_t SoftMaxCalculateExp(const SoftmaxParams& params, - const int16_t* input_data, const int depth, - int16_t max_in_row, int i, int c) { - int32_t input_diff = input_data[i * depth + c] - max_in_row; - // scale the input_diff such that [-65535, 0] correspond to [-10.0, 0.0] - // exp lut generated with range [-10, 0], as exp(-10) is negligible. - int32_t scaled_diff = MultiplyByQuantizedMultiplier( - input_diff, params.input_multiplier, params.input_left_shift); - // recenter to [-32768, 32767] - int32_t sym_scaled_diff = scaled_diff + 32767; - int16_t sat_sym_scaled_diff = - std::min(std::max(sym_scaled_diff, static_cast(-32768)), - static_cast(32767)); - // apply the exp() LUT activation function - return lut_lookup(sat_sym_scaled_diff, params.exp_lut); -} -// Quantized softmax with int16_t input and int16_t output. -inline void SoftmaxInt16(const SoftmaxParams& params, - const RuntimeShape& input_shape, - const int16_t* input_data, - const RuntimeShape& output_shape, - int16_t* output_data) { - const int trailing_dim = input_shape.DimensionsCount() - 1; - const int outer_size = - MatchingFlatSizeSkipDim(input_shape, trailing_dim, output_shape); - const int depth = - MatchingDim(input_shape, trailing_dim, output_shape, trailing_dim); - - for (int i = 0; i < outer_size; ++i) { - // Find the largest element - int16_t max_in_row = std::numeric_limits::min(); - for (int c = 0; c < depth; ++c) { - max_in_row = std::max(max_in_row, input_data[i * depth + c]); - } - - // This loops computes the exp values and their sum. We will need the exp - // values later on in the function so we cache them in the output_data - // buffer. This is an optimization done to avoid calculating the exp values - // twice making use of the output_data buffer as scratch memory. - int32_t sum_of_exps = 0; // Q16.15 fixed point format. - int16_t* exp_results_Q015 = output_data + i * depth; - for (int c = 0; c < depth; ++c) { - exp_results_Q015[c] = - SoftMaxCalculateExp(params, input_data, depth, max_in_row, i, c); - sum_of_exps += exp_results_Q015[c]; - } - - // Compute the reciprocal 1/sum_of_exps - uint8_t headroom_plus_one = - CountLeadingZeros(static_cast(sum_of_exps)); - int32_t shifted_sum = - ((static_cast(sum_of_exps) << (headroom_plus_one - 1)) + - (1 << 13)) >> - 14; - // since the LUT computes 1/(1 + x) we need to first compute x = (sum - 1). - // also, the LUT expects a symmetrical input, so we must also recenter x - // from [0, 65535] to [-32768, 32767]. - int32_t sym_shifted_sum = shifted_sum + (-((1 << 15) + (1 << 16))); - int16_t sat_sym_shifted_sum = static_cast( - std::min(std::max(sym_shifted_sum, static_cast(-32768)), - static_cast(32767))); - // apply 1/(1 + x) LUT activation function - int16_t reciprocal_scale_Q015 = - lut_lookup(sat_sym_shifted_sum, params.one_over_one_plus_x_lut); - - // Rescale the exp_result with reciprocal - // range of output is [0, 32767] correspond to [0.0, 1.0] - for (int c = 0; c < depth; ++c) { - uint8_t right_shift = 31 - headroom_plus_one; - int64_t round = 1 << (right_shift - 1); - int32_t result = (static_cast(exp_results_Q015[c]) * - static_cast(reciprocal_scale_Q015) + - round) >> - right_shift; - output_data[i * depth + c] = static_cast( - std::min(std::max(result, static_cast(0)), - static_cast(32767))); - } - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_SOFTMAX_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/space_to_batch_nd.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/space_to_batch_nd.h deleted file mode 100644 index 7f844152..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/space_to_batch_nd.h +++ /dev/null @@ -1,109 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_SPACE_TO_BATCH_ND_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_SPACE_TO_BATCH_ND_H_ - -#include - -#include "ruy/profiler/instrumentation.h" // from @ruy -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { -namespace reference_ops { - -// TODO(b/135760455): Move this method anonymous namespace in a cc file. -inline RuntimeShape ExtendShapeSpaceToBatch(const RuntimeShape& shape) { - if (shape.DimensionsCount() == 4) { - return shape; - } - RuntimeShape new_shape(4, 1); - new_shape.SetDim(0, shape.Dims(0)); - new_shape.SetDim(1, shape.Dims(1)); - new_shape.SetDim(3, shape.Dims(2)); - return new_shape; -} - -template -inline void SpaceToBatchND(const SpaceToBatchParams& params, - const RuntimeShape& unextended_input1_shape, - const T* input1_data, - const RuntimeShape& unextended_input2_shape, - const int32_t* block_shape_data, - const RuntimeShape& unextended_input3_shape, - const int32_t* paddings_data, - const RuntimeShape& unextended_output_shape, - T* output_data) { - ruy::profiler::ScopeLabel label("SpaceToBatchND"); - TFLITE_DCHECK_GE(unextended_input1_shape.DimensionsCount(), 3); - TFLITE_DCHECK_LE(unextended_input1_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(unextended_input1_shape.DimensionsCount(), - unextended_output_shape.DimensionsCount()); - - // Extends the input/output shape from 3D to 4D if needed, NHC -> NH1C. - const RuntimeShape input1_shape = - ExtendShapeSpaceToBatch(unextended_input1_shape); - const RuntimeShape output_shape = - ExtendShapeSpaceToBatch(unextended_output_shape); - - const int depth = input1_shape.Dims(3); - const int input_width = input1_shape.Dims(2); - const int input_height = input1_shape.Dims(1); - const int input_batch_size = input1_shape.Dims(0); - - const int output_width = output_shape.Dims(2); - const int output_height = output_shape.Dims(1); - const int output_batch_size = output_shape.Dims(0); - - const int block_shape_height = block_shape_data[0]; - const int block_shape_width = - unextended_input1_shape.DimensionsCount() == 4 ? block_shape_data[1] : 1; - const int padding_top = paddings_data[0]; - const int padding_left = - unextended_input1_shape.DimensionsCount() == 4 ? paddings_data[2] : 0; - - // For uint8 quantized, the correct padding "zero value" is the output offset. - const int32_t pad_value = params.output_offset; - for (int out_b = 0; out_b < output_batch_size; ++out_b) { - int input_batch = out_b % input_batch_size; - int shift_w = (out_b / input_batch_size) % block_shape_width; - int shift_h = (out_b / input_batch_size) / block_shape_width; - for (int out_h = 0; out_h < output_height; ++out_h) { - for (int out_w = 0; out_w < output_width; ++out_w) { - T* out = output_data + Offset(output_shape, out_b, out_h, out_w, 0); - if (out_h * block_shape_height + shift_h < padding_top || - out_h * block_shape_height + shift_h >= - padding_top + input_height || - out_w * block_shape_width + shift_w < padding_left || - out_w * block_shape_width + shift_w >= padding_left + input_width) { - // This may not execute correctly when pad_value != 0 and T != uint8. - memset(out, pad_value, depth * sizeof(T)); - } else { - const T* in = - input1_data + - Offset(input1_shape, input_batch, - (out_h * block_shape_height + shift_h) - padding_top, - (out_w * block_shape_width + shift_w) - padding_left, 0); - memcpy(out, in, depth * sizeof(T)); - } - } - } - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_SPACE_TO_BATCH_ND_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/space_to_depth.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/space_to_depth.h deleted file mode 100644 index 7ad46549..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/space_to_depth.h +++ /dev/null @@ -1,80 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_SPACE_TO_DEPTH_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_SPACE_TO_DEPTH_H_ - -#include - -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { -namespace reference_ops { - -template -inline void SpaceToDepth(const tflite::SpaceToDepthParams& op_params, - const RuntimeShape& unextended_input_shape, - const T* input_data, - const RuntimeShape& unextended_output_shape, - T* output_data) { - TFLITE_DCHECK_LE(unextended_input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(unextended_output_shape.DimensionsCount(), 4); - const RuntimeShape input_shape = - RuntimeShape::ExtendedShape(4, unextended_input_shape); - const RuntimeShape output_shape = - RuntimeShape::ExtendedShape(4, unextended_output_shape); - - const int input_depth = input_shape.Dims(3); - const int input_width = input_shape.Dims(2); - const int input_height = input_shape.Dims(1); - const int input_batch = input_shape.Dims(0); - - const int output_depth = output_shape.Dims(3); - const int output_width = output_shape.Dims(2); - const int output_height = output_shape.Dims(1); - const int output_batch = output_shape.Dims(0); - - const int32_t block_size = op_params.block_size; - - TFLITE_DCHECK_EQ(input_width, output_width * block_size); - TFLITE_DCHECK_EQ(input_height, output_height * block_size); - TFLITE_DCHECK_EQ(input_depth * block_size * block_size, output_depth); - TFLITE_DCHECK_EQ(input_batch, output_batch); - - for (int in_b = 0; in_b < input_batch; ++in_b) { - for (int in_h = 0; in_h < input_height; ++in_h) { - for (int in_w = 0; in_w < input_width; ++in_w) { - for (int in_d = 0; in_d < input_depth; ++in_d) { - const int out_d = - in_d + ((in_h % block_size) * block_size + in_w % block_size) * - input_depth; - const int out_w = in_w / block_size; - const int out_h = in_h / block_size; - const int out_b = in_b; - - const int input_index = Offset(input_shape, in_b, in_h, in_w, in_d); - const int output_index = - Offset(output_shape, out_b, out_h, out_w, out_d); - - output_data[output_index] = input_data[input_index]; - } - } - } - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_SPACE_TO_DEPTH_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/strided_slice.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/strided_slice.h deleted file mode 100644 index 40dc2e91..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/strided_slice.h +++ /dev/null @@ -1,121 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_STRIDED_SLICE_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_STRIDED_SLICE_H_ - -#include "ruy/profiler/instrumentation.h" // from @ruy -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/internal/portable_tensor.h" -#include "tensorflow/lite/kernels/internal/strided_slice_logic.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -namespace reference_ops { - -template -inline void StridedSlice(const tflite::StridedSliceParams& op_params, - const RuntimeShape& unextended_input_shape, - const RuntimeShape& unextended_output_shape, - SequentialTensorWriter* writer) { - using strided_slice::LoopCondition; - using strided_slice::StartForAxis; - using strided_slice::StopForAxis; - - ruy::profiler::ScopeLabel label("StridedSlice"); - - // Note that the output_shape is not used herein. - tflite::StridedSliceParams params_copy = op_params; - - TFLITE_DCHECK_LE(unextended_input_shape.DimensionsCount(), 5); - TFLITE_DCHECK_LE(unextended_output_shape.DimensionsCount(), 5); - const RuntimeShape input_shape = - RuntimeShape::ExtendedShape(5, unextended_input_shape); - const RuntimeShape output_shape = - RuntimeShape::ExtendedShape(5, unextended_output_shape); - - // Reverse and pad to 5 dimensions because that is what the runtime code - // requires (ie. all shapes must be 5D and are given backwards). - strided_slice::StridedSlicePadIndices(¶ms_copy, 5); - - const int start_0 = StartForAxis(params_copy, input_shape, 0); - const int stop_0 = StopForAxis(params_copy, input_shape, 0, start_0); - const int start_1 = StartForAxis(params_copy, input_shape, 1); - const int stop_1 = StopForAxis(params_copy, input_shape, 1, start_1); - const int start_2 = StartForAxis(params_copy, input_shape, 2); - const int stop_2 = StopForAxis(params_copy, input_shape, 2, start_2); - const int start_3 = StartForAxis(params_copy, input_shape, 3); - const int stop_3 = StopForAxis(params_copy, input_shape, 3, start_3); - const int start_4 = StartForAxis(params_copy, input_shape, 4); - const int stop_4 = StopForAxis(params_copy, input_shape, 4, start_4); - - for (int offset_0 = start_0 * input_shape.Dims(1), - end_0 = stop_0 * input_shape.Dims(1), - step_0 = params_copy.strides[0] * input_shape.Dims(1); - !LoopCondition(offset_0, end_0, params_copy.strides[0]); - offset_0 += step_0) { - for (int offset_1 = (offset_0 + start_1) * input_shape.Dims(2), - end_1 = (offset_0 + stop_1) * input_shape.Dims(2), - step_1 = params_copy.strides[1] * input_shape.Dims(2); - !LoopCondition(offset_1, end_1, params_copy.strides[1]); - offset_1 += step_1) { - for (int offset_2 = (offset_1 + start_2) * input_shape.Dims(3), - end_2 = (offset_1 + stop_2) * input_shape.Dims(3), - step_2 = params_copy.strides[2] * input_shape.Dims(3); - !LoopCondition(offset_2, end_2, params_copy.strides[2]); - offset_2 += step_2) { - for (int offset_3 = (offset_2 + start_3) * input_shape.Dims(4), - end_3 = (offset_2 + stop_3) * input_shape.Dims(4), - step_3 = params_copy.strides[3] * input_shape.Dims(4); - !LoopCondition(offset_3, end_3, params_copy.strides[3]); - offset_3 += step_3) { - for (int offset_4 = offset_3 + start_4, end_4 = offset_3 + stop_4; - !LoopCondition(offset_4, end_4, params_copy.strides[4]); - offset_4 += params_copy.strides[4]) { - writer->Write(offset_4); - } - } - } - } - } -} - -template -inline void StridedSlice(const tflite::StridedSliceParams& op_params, - const RuntimeShape& unextended_input_shape, - const T* input_data, - const RuntimeShape& unextended_output_shape, - T* output_data) { - SequentialTensorWriter writer(input_data, output_data); - StridedSlice(op_params, unextended_input_shape, unextended_output_shape, - &writer); -} - -template -inline void StridedSlice(const tflite::StridedSliceParams& op_params, - const RuntimeShape& unextended_input_shape, - const TfLiteTensor* input, - const RuntimeShape& unextended_output_shape, - TfLiteTensor* output) { - SequentialTensorWriter writer(input, output); - StridedSlice(op_params, unextended_input_shape, unextended_output_shape, - &writer); -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_STRIDED_SLICE_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/sub.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/sub.h deleted file mode 100644 index d0ebc95a..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/sub.h +++ /dev/null @@ -1,479 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_SUB_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_SUB_H_ - -#include - -#include -#include - -#include "ruy/profiler/instrumentation.h" // from @ruy -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -namespace reference_ops { - -inline void SubNonBroadcast(const ArithmeticParams& params, - const RuntimeShape& input1_shape, - const float* input1_data, - const RuntimeShape& input2_shape, - const float* input2_data, - const RuntimeShape& output_shape, - float* output_data) { - const int flat_size = - MatchingElementsSize(input1_shape, input2_shape, output_shape); - for (int i = 0; i < flat_size; ++i) { - output_data[i] = ActivationFunctionWithMinMax( - input1_data[i] - input2_data[i], params.float_activation_min, - params.float_activation_max); - } -} - -inline void SubNonBroadcast(const ArithmeticParams& params, - const RuntimeShape& input1_shape, - const int32_t* input1_data, - const RuntimeShape& input2_shape, - const int32_t* input2_data, - const RuntimeShape& output_shape, - int32_t* output_data) { - const int flat_size = - MatchingElementsSize(input1_shape, input2_shape, output_shape); - for (int i = 0; i < flat_size; ++i) { - output_data[i] = ActivationFunctionWithMinMax( - input1_data[i] - input2_data[i], params.quantized_activation_min, - params.quantized_activation_max); - } -} - -// TODO(b/151345304): We can implement BroadcastSub on buffers of arbitrary -// dimensionality if the runtime code does a single loop over one dimension -// that handles broadcasting as the base case. The code generator would then -// generate max(D1, D2) nested for loops. -template -inline void BroadcastSubSlow(const ArithmeticParams& params, - const RuntimeShape& input1_shape, - const float* input1_data, - const RuntimeShape& input2_shape, - const float* input2_data, - const RuntimeShape& output_shape, - float* output_data) { - ruy::profiler::ScopeLabel label("BroadcastSubSlow/float"); - TFLITE_DCHECK_LE(input1_shape.DimensionsCount(), N); - TFLITE_DCHECK_LE(input2_shape.DimensionsCount(), N); - TFLITE_DCHECK_LE(output_shape.DimensionsCount(), N); - NdArrayDesc desc1; - NdArrayDesc desc2; - NdArrayDesc output_desc; - NdArrayDescsForElementwiseBroadcast(input1_shape, input2_shape, &desc1, - &desc2); - CopyDimsToDesc(RuntimeShape::ExtendedShape(N, output_shape), &output_desc); - - // In Tensorflow, the dimensions are canonically named (batch_number, row, - // col, channel), with extents (batches, height, width, depth), with the - // trailing dimension changing most rapidly (channels has the smallest stride, - // typically 1 element). - // - // In generated C code, we store arrays with the dimensions reversed. The - // first dimension has smallest stride. - // - // We name our variables by their Tensorflow convention, but generate C code - // nesting loops such that the innermost loop has the smallest stride for the - // best cache behavior. - auto sub_func = [&](int indexes[N]) { - output_data[SubscriptToIndex(output_desc, indexes)] = - ActivationFunctionWithMinMax( - input1_data[SubscriptToIndex(desc1, indexes)] - - input2_data[SubscriptToIndex(desc2, indexes)], - params.float_activation_min, params.float_activation_max); - }; - NDOpsHelper(output_desc, sub_func); -} - -template -inline void BroadcastSubSlow(const ArithmeticParams& params, - const RuntimeShape& input1_shape, - const int32_t* input1_data, - const RuntimeShape& input2_shape, - const int32_t* input2_data, - const RuntimeShape& output_shape, - int32_t* output_data) { - ruy::profiler::ScopeLabel label("BroadcastSubSlow/int32_t"); - TFLITE_DCHECK_LE(input1_shape.DimensionsCount(), N); - TFLITE_DCHECK_LE(input2_shape.DimensionsCount(), N); - TFLITE_DCHECK_LE(output_shape.DimensionsCount(), N); - NdArrayDesc desc1; - NdArrayDesc desc2; - NdArrayDesc output_desc; - NdArrayDescsForElementwiseBroadcast(input1_shape, input2_shape, &desc1, - &desc2); - CopyDimsToDesc(RuntimeShape::ExtendedShape(N, output_shape), &output_desc); - - // In Tensorflow, the dimensions are canonically named (batch_number, row, - // col, channel), with extents (batches, height, width, depth), with the - // trailing dimension changing most rapidly (channels has the smallest stride, - // typically 1 element). - // - // In generated C code, we store arrays with the dimensions reversed. The - // first dimension has smallest stride. - // - // We name our variables by their Tensorflow convention, but generate C code - // nesting loops such that the innermost loop has the smallest stride for the - // best cache behavior. - auto sub_func = [&](int indexes[N]) { - output_data[SubscriptToIndex(output_desc, indexes)] = - ActivationFunctionWithMinMax( - input1_data[SubscriptToIndex(desc1, indexes)] - - input2_data[SubscriptToIndex(desc2, indexes)], - params.quantized_activation_min, params.quantized_activation_max); - }; - NDOpsHelper(output_desc, sub_func); -} - -template -void BroadcastSubSlow(const ArithmeticParams& params, - const RuntimeShape& input1_shape, - const int64_t* input1_data, - const RuntimeShape& input2_shape, - const int64_t* input2_data, - const RuntimeShape& output_shape, int64_t* output_data) { - ruy::profiler::ScopeLabel label("BroadcastSubSlow/int64_t"); - TFLITE_DCHECK_LE(input1_shape.DimensionsCount(), N); - TFLITE_DCHECK_LE(input2_shape.DimensionsCount(), N); - TFLITE_DCHECK_LE(output_shape.DimensionsCount(), N); - NdArrayDesc desc1; - NdArrayDesc desc2; - NdArrayDesc output_desc; - NdArrayDescsForElementwiseBroadcast(input1_shape, input2_shape, &desc1, - &desc2); - CopyDimsToDesc(RuntimeShape::ExtendedShape(N, output_shape), &output_desc); - - // In Tensorflow, the dimensions are canonically named (batch_number, row, - // col, channel), with extents (batches, height, width, depth), with the - // trailing dimension changing most rapidly (channels has the smallest stride, - // typically 1 element). - // - // In generated C code, we store arrays with the dimensions reversed. The - // first dimension has smallest stride. - // - // We name our variables by their Tensorflow convention, but generate C code - // nesting loops such that the innermost loop has the smallest stride for the - // best cache behavior. - auto sub_func = [&](int indexes[N]) { - output_data[SubscriptToIndex(output_desc, indexes)] = - ActivationFunctionWithMinMax( - input1_data[SubscriptToIndex(desc1, indexes)] - - input2_data[SubscriptToIndex(desc2, indexes)], - params.int64_activation_min, params.int64_activation_max); - }; - NDOpsHelper(output_desc, sub_func); -} - -template -void BroadcastSubSlow(const ArithmeticParams& params, - const RuntimeShape& input1_shape, const T* input1_data, - const RuntimeShape& input2_shape, const T* input2_data, - const RuntimeShape& output_shape, T* output_data) { - ruy::profiler::ScopeLabel label("BroadcastSubSlow/templated"); - TFLITE_DCHECK_LE(input1_shape.DimensionsCount(), N); - TFLITE_DCHECK_LE(input2_shape.DimensionsCount(), N); - TFLITE_DCHECK_LE(output_shape.DimensionsCount(), N); - NdArrayDesc desc1; - NdArrayDesc desc2; - NdArrayDesc output_desc; - NdArrayDescsForElementwiseBroadcast(input1_shape, input2_shape, &desc1, - &desc2); - CopyDimsToDesc(RuntimeShape::ExtendedShape(N, output_shape), &output_desc); - - // In Tensorflow, the dimensions are canonically named (batch_number, row, - // col, channel), with extents (batches, height, width, depth), with the - // trailing dimension changing most rapidly (channels has the smallest stride, - // typically 1 element). - // - // In generated C code, we store arrays with the dimensions reversed. The - // first dimension has smallest stride. - // - // We name our variables by their Tensorflow convention, but generate C code - // nesting loops such that the innermost loop has the smallest stride for the - // best cache behavior. - auto sub_func = [&](int indexes[N]) { - output_data[SubscriptToIndex(output_desc, indexes)] = - ActivationFunctionWithMinMax( - input1_data[SubscriptToIndex(desc1, indexes)] - - input2_data[SubscriptToIndex(desc2, indexes)], - params.quantized_activation_min, params.quantized_activation_max); - }; - NDOpsHelper(output_desc, sub_func); -} - -template -inline void BroadcastSub16POTSlow(const ArithmeticParams& params, - const RuntimeShape& input1_shape, - const int16_t* input1_data, - const RuntimeShape& input2_shape, - const int16_t* input2_data, - const RuntimeShape& output_shape, - int16_t* output_data) { - ruy::profiler::ScopeLabel label("BroadcastSub16POTSlow/int16_t"); - NdArrayDesc desc1; - NdArrayDesc desc2; - NdArrayDesc output_desc; - NdArrayDescsForElementwiseBroadcast(input1_shape, input2_shape, &desc1, - &desc2); - CopyDimsToDesc(RuntimeShape::ExtendedShape(N, output_shape), &output_desc); - - // In Tensorflow, the dimensions are canonically named (batch_number, row, - // col, channel), with extents (batches, height, width, depth), with the - // trailing dimension changing most rapidly (channels has the smallest stride, - // typically 1 element). - // - // In generated C code, we store arrays with the dimensions reversed. The - // first dimension has smallest stride. - // - // We name our variables by their Tensorflow convention, but generate C code - // nesting loops such that the innermost loop has the smallest stride for the - // best cache behavior. - auto sub_func = [&](int indexes[N]) { - const int32_t input1_val = input1_data[SubscriptToIndex(desc1, indexes)]; - const int32_t input2_val = input2_data[SubscriptToIndex(desc2, indexes)]; - const int32_t scaled_input1_val = - gemmlowp::RoundingDivideByPOT(input1_val, -params.input1_shift); - const int32_t scaled_input2_val = - gemmlowp::RoundingDivideByPOT(input2_val, -params.input2_shift); - const int32_t raw_output = scaled_input1_val - scaled_input2_val; - const int32_t clamped_output = - std::min(params.quantized_activation_max, - std::max(params.quantized_activation_min, raw_output)); - output_data[SubscriptToIndex(output_desc, indexes)] = - static_cast(clamped_output); - }; - NDOpsHelper(output_desc, sub_func); -} - -template -void BroadcastQuantSubSlow(const ArithmeticParams& params, - const RuntimeShape& input1_shape, - const T* input1_data, - const RuntimeShape& input2_shape, - const T* input2_data, - const RuntimeShape& output_shape, T* output_data) { - ruy::profiler::ScopeLabel label("BroadcastQuantSubSlow/T"); - TFLITE_DCHECK_LE(input1_shape.DimensionsCount(), N); - TFLITE_DCHECK_LE(input2_shape.DimensionsCount(), N); - TFLITE_DCHECK_LE(output_shape.DimensionsCount(), N); - NdArrayDesc desc1; - NdArrayDesc desc2; - NdArrayDesc output_desc; - NdArrayDescsForElementwiseBroadcast(input1_shape, input2_shape, &desc1, - &desc2); - CopyDimsToDesc(RuntimeShape::ExtendedShape(N, output_shape), &output_desc); - - // In Tensorflow, the dimensions are canonically named (batch_number, row, - // col, channel), with extents (batches, height, width, depth), with the - // trailing dimension changing most rapidly (channels has the smallest stride, - // typically 1 element). - // - // In generated C code, we store arrays with the dimensions reversed. The - // first dimension has smallest stride. - // - // We name our variables by their Tensorflow convention, but generate C code - // nesting loops such that the innermost loop has the smallest stride for the - // best cache behavior. - auto sub_func = [&](int indexes[N]) { - const int32_t input1_val = - params.input1_offset + input1_data[SubscriptToIndex(desc1, indexes)]; - const int32_t input2_val = - params.input2_offset + input2_data[SubscriptToIndex(desc2, indexes)]; - const int32_t shifted_input1_val = input1_val * (1 << params.left_shift); - const int32_t shifted_input2_val = input2_val * (1 << params.left_shift); - const int32_t scaled_input1_val = - MultiplyByQuantizedMultiplierSmallerThanOneExp( - shifted_input1_val, params.input1_multiplier, params.input1_shift); - const int32_t scaled_input2_val = - MultiplyByQuantizedMultiplierSmallerThanOneExp( - shifted_input2_val, params.input2_multiplier, params.input2_shift); - const int32_t raw_sub = scaled_input1_val - scaled_input2_val; - const int32_t raw_output = - MultiplyByQuantizedMultiplierSmallerThanOneExp( - raw_sub, params.output_multiplier, params.output_shift) + - params.output_offset; - const int32_t clamped_output = - std::min(params.quantized_activation_max, - std::max(params.quantized_activation_min, raw_output)); - output_data[SubscriptToIndex(output_desc, indexes)] = - static_cast(clamped_output); - }; - NDOpsHelper(output_desc, sub_func); -} - -// Element-wise add that can often be used for inner loop of broadcast add as -// well as the non-broadcast add. -template -inline void SubElementwise(int size, const ArithmeticParams& params, - const T* input1_data, const T* input2_data, - T* output_data) { - for (int i = 0; i < size; ++i) { - const int32_t input1_val = params.input1_offset + input1_data[i]; - const int32_t input2_val = params.input2_offset + input2_data[i]; - const int32_t shifted_input1_val = input1_val * (1 << params.left_shift); - const int32_t shifted_input2_val = input2_val * (1 << params.left_shift); - const int32_t scaled_input1_val = - MultiplyByQuantizedMultiplierSmallerThanOneExp( - shifted_input1_val, params.input1_multiplier, params.input1_shift); - const int32_t scaled_input2_val = - MultiplyByQuantizedMultiplierSmallerThanOneExp( - shifted_input2_val, params.input2_multiplier, params.input2_shift); - const int32_t raw_sub = scaled_input1_val - scaled_input2_val; - const int32_t raw_output = - MultiplyByQuantizedMultiplierSmallerThanOneExp( - raw_sub, params.output_multiplier, params.output_shift) + - params.output_offset; - const int32_t clamped_output = - std::min(params.quantized_activation_max, - std::max(params.quantized_activation_min, raw_output)); - output_data[i] = static_cast(clamped_output); - } -} - -inline void Sub(const ArithmeticParams& params, - const RuntimeShape& input1_shape, const uint8_t* input1_data, - const RuntimeShape& input2_shape, const uint8_t* input2_data, - const RuntimeShape& output_shape, uint8_t* output_data) { - TFLITE_DCHECK_LE(params.quantized_activation_min, - params.quantized_activation_max); - const int flat_size = - MatchingElementsSize(input1_shape, input2_shape, output_shape); - - TFLITE_DCHECK_GT(params.input1_offset, -256); - TFLITE_DCHECK_GT(params.input2_offset, -256); - TFLITE_DCHECK_LT(params.input1_offset, 256); - TFLITE_DCHECK_LT(params.input2_offset, 256); - SubElementwise(flat_size, params, input1_data, input2_data, output_data); -} - -inline void Sub(const ArithmeticParams& params, - const RuntimeShape& input1_shape, const int8_t* input1_data, - const RuntimeShape& input2_shape, const int8_t* input2_data, - const RuntimeShape& output_shape, int8_t* output_data) { - TFLITE_DCHECK_LE(params.quantized_activation_min, - params.quantized_activation_max); - - const int flat_size = - MatchingElementsSize(input1_shape, input2_shape, output_shape); - - TFLITE_DCHECK_GE(params.input1_offset, -128); - TFLITE_DCHECK_GE(params.input2_offset, -128); - // offset = -quantization_params.zero_point in PrepareGeneralSubOp(). - // So it's maximum can be 128 not 127. - TFLITE_DCHECK_LE(params.input1_offset, 128); - TFLITE_DCHECK_LE(params.input2_offset, 128); - SubElementwise(flat_size, params, input1_data, input2_data, output_data); -} - -inline void Sub(const ArithmeticParams& params, - const RuntimeShape& input1_shape, const int16_t* input1_data, - const RuntimeShape& input2_shape, const int16_t* input2_data, - const RuntimeShape& output_shape, int16_t* output_data) { - TFLITE_DCHECK_LE(params.quantized_activation_min, - params.quantized_activation_max); - - const int flat_size = - MatchingElementsSize(input1_shape, input2_shape, output_shape); - - TFLITE_DCHECK_EQ(params.input1_offset, 0); - TFLITE_DCHECK_EQ(params.input2_offset, 0); - SubElementwise(flat_size, params, input1_data, input2_data, output_data); -} - -template -void Sub(const ArithmeticParams& params, const RuntimeShape& input1_shape, - const T* input1_data, const RuntimeShape& input2_shape, - const T* input2_data, const RuntimeShape& output_shape, - T* output_data) { - NdArrayDesc<4> desc1; - NdArrayDesc<4> desc2; - NdArrayDescsForElementwiseBroadcast(input1_shape, input2_shape, &desc1, - &desc2); - const RuntimeShape extended_output_shape = - RuntimeShape::ExtendedShape(4, output_shape); - - // In Tensorflow, the dimensions are canonically named (batch_number, row, - // col, channel), with extents (batches, height, width, depth), with the - // trailing dimension changing most rapidly (channels has the smallest stride, - // typically 1 element). - // - // In generated C code, we store arrays with the dimensions reversed. The - // first dimension has smallest stride. - // - // We name our variables by their Tensorflow convention, but generate C code - // nesting loops such that the innermost loop has the smallest stride for the - // best cache behavior. - for (int b = 0; b < extended_output_shape.Dims(0); ++b) { - for (int y = 0; y < extended_output_shape.Dims(1); ++y) { - for (int x = 0; x < extended_output_shape.Dims(2); ++x) { - for (int c = 0; c < extended_output_shape.Dims(3); ++c) { - output_data[Offset(extended_output_shape, b, y, x, c)] = - input1_data[SubscriptToIndex(desc1, b, y, x, c)] - - input2_data[SubscriptToIndex(desc2, b, y, x, c)]; - } - } - } - } -} - -inline void SetActivationMinMax(const ArithmeticParams& params, - int32_t* activation_min, - int32_t* activation_max) { - *activation_min = params.quantized_activation_min; - *activation_max = params.quantized_activation_max; -} - -inline void SetActivationMinMax(const ArithmeticParams& params, - float* activation_min, float* activation_max) { - *activation_min = params.float_activation_min; - *activation_max = params.float_activation_max; -} - -inline void SetActivationMinMax(const ArithmeticParams& params, - int64_t* activation_min, - int64_t* activation_max) { - *activation_min = params.int64_activation_min; - *activation_max = params.int64_activation_max; -} - -template -inline void SubWithActivation( - const ArithmeticParams& params, const RuntimeShape& input1_shape, - const T* input1_data, const RuntimeShape& input2_shape, - const T* input2_data, const RuntimeShape& output_shape, T* output_data) { - ruy::profiler::ScopeLabel label("SubWithActivation"); - const int flat_size = - MatchingElementsSize(input1_shape, input2_shape, output_shape); - T activation_min, activation_max; - SetActivationMinMax(params, &activation_min, &activation_max); - - for (int i = 0; i < flat_size; ++i) { - output_data[i] = ActivationFunctionWithMinMax( - input1_data[i] - input2_data[i], activation_min, activation_max); - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_SUB_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/tanh.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/tanh.h deleted file mode 100644 index 3a05c474..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/tanh.h +++ /dev/null @@ -1,129 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_TANH_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_TANH_H_ - -#include - -#include "fixedpoint/fixedpoint.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/cppmath.h" -#include "tensorflow/lite/kernels/internal/types.h" -#include "tensorflow/lite/kernels/op_macros.h" - -namespace tflite { -namespace reference_ops { - -inline void Tanh(const RuntimeShape& input_shape, const float* input_data, - const RuntimeShape& output_shape, float* output_data) { - const int flat_size = MatchingFlatSize(input_shape, output_shape); - - for (int i = 0; i < flat_size; i++) { - float val = input_data[i]; - float result = std::tanh(val); - output_data[i] = result; - } -} - -// Convenience version that allows, for example, generated-code calls to be -// uniform between data types. -inline void Tanh(const TanhParams&, const RuntimeShape& input_shape, - const float* input_data, const RuntimeShape& output_shape, - float* output_data) { - // Drop params: not needed. - Tanh(input_shape, input_data, output_shape, output_data); -} - -inline void Tanh(const TanhParams& params, const RuntimeShape& input_shape, - const int16_t* input_data, const RuntimeShape& output_shape, - int16_t* output_data) { - const int input_left_shift = params.input_left_shift; - // Support for shifts is limited until we have a parameterized version of - // SaturatingRoundingMultiplyByPOT(). - TFLITE_DCHECK_GE(input_left_shift, 0); - TFLITE_DCHECK_LE(input_left_shift, 1); - - const int flat_size = MatchingFlatSize(input_shape, output_shape); - - // F0 uses 0 integer bits, range [-1, 1]. - // This is the return type of math functions such as tanh, logistic, - // whose range is in [-1, 1]. - using F0 = gemmlowp::FixedPoint; - // F3 uses 3 integer bits, range [-8, 8], the input range expected here. - using F3 = gemmlowp::FixedPoint; - - if (input_left_shift == 0) { - for (int i = 0; i < flat_size; i++) { - F3 input = F3::FromRaw(input_data[i]); - F0 output = gemmlowp::tanh(input); - output_data[i] = output.raw(); - } - } else { - for (int i = 0; i < flat_size; i++) { - F3 input = F3::FromRaw( - gemmlowp::SaturatingRoundingMultiplyByPOT<1>(input_data[i])); - F0 output = gemmlowp::tanh(input); - output_data[i] = output.raw(); - } - } -} - -inline void Tanh(const TanhParams& params, const RuntimeShape& input_shape, - const uint8_t* input_data, const RuntimeShape& output_shape, - uint8_t* output_data) { - const int32_t input_zero_point = params.input_zero_point; - const int32_t input_range_radius = params.input_range_radius; - const int32_t input_multiplier = params.input_multiplier; - const int input_left_shift = params.input_left_shift; - const int32_t output_zero_point = 128; - const int flat_size = MatchingFlatSize(input_shape, output_shape); - - for (int i = 0; i < flat_size; i++) { - const uint8_t input_val_u8 = input_data[i]; - const int32_t input_val_centered = - static_cast(input_val_u8) - input_zero_point; - uint8_t output_val; - if (input_val_centered <= -input_range_radius) { - output_val = 0; - } else if (input_val_centered >= input_range_radius) { - output_val = 255; - } else { - const int32_t input_val_rescaled = - MultiplyByQuantizedMultiplierGreaterThanOne( - input_val_centered, input_multiplier, input_left_shift); - using FixedPoint4 = gemmlowp::FixedPoint; - using FixedPoint0 = gemmlowp::FixedPoint; - const FixedPoint4 input_val_f4 = FixedPoint4::FromRaw(input_val_rescaled); - const FixedPoint0 output_val_f0 = gemmlowp::tanh(input_val_f4); - // Convert from Q0.31 to Q24.7. - using gemmlowp::RoundingDivideByPOT; - int32_t output_val_s32 = RoundingDivideByPOT(output_val_f0.raw(), 24); - output_val_s32 += output_zero_point; - if (output_val_s32 == 256) { - output_val_s32 = 255; - } - // Reinterpret as Q0.7, encoded in uint8_t. - TFLITE_DCHECK_GE(output_val_s32, 0); - TFLITE_DCHECK_LE(output_val_s32, 255); - output_val = static_cast(output_val_s32); - } - output_data[i] = output_val; - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_TANH_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/transpose.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/transpose.h deleted file mode 100644 index 96aa4cca..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/transpose.h +++ /dev/null @@ -1,111 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_TRANSPOSE_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_TRANSPOSE_H_ - -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -namespace reference_ops { - -template -void TransposeImpl(const TransposeParams& params, - const RuntimeShape& unextended_input_shape, - const T* input_data, - const RuntimeShape& unextended_output_shape, - T* output_data) { - const int unextended_input_size = unextended_input_shape.DimensionsCount(); - const int unextended_output_size = unextended_output_shape.DimensionsCount(); - TFLITE_DCHECK_LE(unextended_input_size, N); - TFLITE_DCHECK_LE(unextended_output_size, N); - TFLITE_DCHECK_EQ(unextended_output_size, params.perm_count); - const int input_ext_size = N - unextended_input_size; - const int output_ext_size = N - unextended_output_size; - NdArrayDesc input_desc; - NdArrayDesc output_desc; - CopyDimsToDesc(RuntimeShape::ExtendedShape(N, unextended_input_shape), - &input_desc); - CopyDimsToDesc(RuntimeShape::ExtendedShape(N, unextended_output_shape), - &output_desc); - - // The perm data is extended to match the output, each index incremented by - // the amount of front padding of the input shape. - int extended_perm[N]; - for (int i = 0; i < N; ++i) { - extended_perm[i] = i < output_ext_size - ? i - : params.perm[i - output_ext_size] + input_ext_size; - } - - // Permutes the input shape so we don't need to permute the indexes inside - // the loop. Check to make sure output_dims is matching input_dims. - NdArrayDesc perm_input_desc; - for (int k = 0; k < N; ++k) { - TFLITE_DCHECK_EQ(input_desc.extents[extended_perm[k]], - output_desc.extents[k]); - perm_input_desc.extents[k] = input_desc.extents[extended_perm[k]]; - perm_input_desc.strides[k] = input_desc.strides[extended_perm[k]]; - } - - // Naive transpose loop (iterate on output index and compute input index). - auto tranpose_func = [&](int indexes[N]) { - output_data[SubscriptToIndex(output_desc, indexes)] = - input_data[SubscriptToIndex(perm_input_desc, indexes)]; - }; - NDOpsHelper(output_desc, tranpose_func); -} - -template -void Transpose(const TransposeParams& params, - const RuntimeShape& unextended_input_shape, const T* input_data, - const RuntimeShape& unextended_output_shape, T* output_data) { - // Transpose kernel only does rearranging values not numeric evaluations on - // each cell. It's safe to implement per size of scalar type and this trick - // keeps the total code size in a reasonable range. - switch (sizeof(T)) { - case 1: - TransposeImpl(params, unextended_input_shape, - reinterpret_cast(input_data), - unextended_output_shape, - reinterpret_cast(output_data)); - break; - case 2: - TransposeImpl(params, unextended_input_shape, - reinterpret_cast(input_data), - unextended_output_shape, - reinterpret_cast(output_data)); - break; - - case 4: - TransposeImpl(params, unextended_input_shape, - reinterpret_cast(input_data), - unextended_output_shape, - reinterpret_cast(output_data)); - break; - case 8: - TransposeImpl(params, unextended_input_shape, - reinterpret_cast(input_data), - unextended_output_shape, - reinterpret_cast(output_data)); - break; - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_TRANSPOSE_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/transpose_conv.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/transpose_conv.h deleted file mode 100644 index ac91f379..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/reference/transpose_conv.h +++ /dev/null @@ -1,219 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_TRANSPOSE_CONV_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_TRANSPOSE_CONV_H_ - -#include - -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -namespace reference_ops { - -inline void TransposeConv( - const ConvParams& params, const RuntimeShape& input_shape, - const float* input_data, const RuntimeShape& filter_shape, - const float* filter_data, const RuntimeShape& bias_shape, - const float* bias_data, const RuntimeShape& output_shape, - float* output_data, const RuntimeShape& im2col_shape, float* im2col_data) { - const int stride_width = params.stride_width; - const int stride_height = params.stride_height; - const int pad_width = params.padding_values.width; - const int pad_height = params.padding_values.height; - TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(filter_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4); - (void)im2col_data; // only used in optimized code. - (void)im2col_shape; // only used in optimized code. - - const int batches = MatchingDim(input_shape, 0, output_shape, 0); - const int input_depth = MatchingDim(input_shape, 3, filter_shape, 3); - const int output_depth = MatchingDim(filter_shape, 0, output_shape, 3); - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - const int filter_height = filter_shape.Dims(1); - const int filter_width = filter_shape.Dims(2); - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - if (bias_data) { - TFLITE_DCHECK_EQ(bias_shape.FlatSize(), output_depth); - } - - // Although transpose convolution simplifies to convolution with transposed - // weights for strides of 1, non-unitary striding complicates matters. To - // keep this reference implementation as clear as possible, we use a - // "scatter" access pattern, where we loop through all the input elements, - // computing their influence on the output, rather than looping through the - // output elements in the typical "gather" access pattern of a conv. We - // therefore must initialize the output array to zero. - const int num_elements = output_shape.FlatSize(); - for (int i = 0; i < num_elements; i++) { - output_data[i] = 0.0f; - } - - // Loop through input elements one at a time. - for (int batch = 0; batch < batches; ++batch) { - for (int in_y = 0; in_y < input_height; ++in_y) { - for (int in_x = 0; in_x < input_width; ++in_x) { - for (int in_channel = 0; in_channel < input_depth; ++in_channel) { - // Loop through the output elements it will influence - const int out_x_origin = (in_x * stride_width) - pad_width; - const int out_y_origin = (in_y * stride_height) - pad_height; - for (int filter_y = 0; filter_y < filter_height; ++filter_y) { - for (int filter_x = 0; filter_x < filter_width; ++filter_x) { - for (int out_channel = 0; out_channel < output_depth; - ++out_channel) { - // Compute output element location - const int out_x = out_x_origin + filter_x; - const int out_y = out_y_origin + filter_y; - // We cannot accumulate out of bounds - if ((out_x >= 0) && (out_x < output_width) && (out_y >= 0) && - (out_y < output_height)) { - float input_value = input_data[Offset( - input_shape, batch, in_y, in_x, in_channel)]; - float filter_value = - filter_data[Offset(filter_shape, out_channel, filter_y, - filter_x, in_channel)]; - output_data[Offset(output_shape, batch, out_y, out_x, - out_channel)] += - input_value * filter_value; - } - } - } - } - } - } - } - } - if (bias_data) { - for (int batch = 0; batch < batches; ++batch) { - for (int out_y = 0; out_y < output_height; ++out_y) { - for (int out_x = 0; out_x < output_width; ++out_x) { - for (int out_channel = 0; out_channel < output_depth; ++out_channel) { - output_data[Offset(output_shape, batch, out_y, out_x, - out_channel)] += bias_data[out_channel]; - } - } - } - } - } -} - -inline void TransposeConv( - const ConvParams& params, const RuntimeShape& input_shape, - const uint8_t* input_data, const RuntimeShape& filter_shape, - const uint8_t* filter_data, const RuntimeShape& bias_shape, - const int32_t* bias_data, const RuntimeShape& output_shape, - uint8_t* output_data, const RuntimeShape& im2col_shape, - uint8_t* im2col_data, int32_t* scratch_buffer) { - const int stride_width = params.stride_width; - const int stride_height = params.stride_height; - const int pad_width = params.padding_values.width; - const int pad_height = params.padding_values.height; - TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(filter_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4); - (void)im2col_data; // only used in optimized code. - (void)im2col_shape; // only used in optimized code. - - const int batches = MatchingDim(input_shape, 0, output_shape, 0); - const int input_depth = MatchingDim(input_shape, 3, filter_shape, 3); - const int output_depth = MatchingDim(filter_shape, 0, output_shape, 3); - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - const int filter_height = filter_shape.Dims(1); - const int filter_width = filter_shape.Dims(2); - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - const int32_t input_offset = params.input_offset; - const int32_t filter_offset = params.weights_offset; - const int32_t output_offset = params.output_offset; - const int32_t output_multiplier = params.output_multiplier; - const int output_shift = params.output_shift; - const int32_t output_activation_min = params.quantized_activation_min; - const int32_t output_activation_max = params.quantized_activation_max; - TFLITE_DCHECK_LE(output_activation_min, output_activation_max); - if (bias_data) { - TFLITE_DCHECK_EQ(bias_shape.FlatSize(), output_depth); - } - - const int num_elements = output_shape.FlatSize(); - // We need to initialize scratch_buffer to all 0s, as we apply the same - // 'scatter' based trick as in float version. - memset(scratch_buffer, 0, num_elements * sizeof(int32_t)); - - // Loop through input elements one at a time. - for (int batch = 0; batch < batches; ++batch) { - for (int in_y = 0; in_y < input_height; ++in_y) { - for (int in_x = 0; in_x < input_width; ++in_x) { - for (int in_channel = 0; in_channel < input_depth; ++in_channel) { - // Loop through the output elements it will influence. - const int out_x_origin = (in_x * stride_width) - pad_width; - const int out_y_origin = (in_y * stride_height) - pad_height; - for (int filter_y = 0; filter_y < filter_height; ++filter_y) { - for (int filter_x = 0; filter_x < filter_width; ++filter_x) { - for (int out_channel = 0; out_channel < output_depth; - ++out_channel) { - // Compute output element location. - const int out_x = out_x_origin + filter_x; - const int out_y = out_y_origin + filter_y; - // We cannot accumulate out of bounds. - if ((out_x >= 0) && (out_x < output_width) && (out_y >= 0) && - (out_y < output_height)) { - uint8_t input_value = input_data[Offset( - input_shape, batch, in_y, in_x, in_channel)]; - uint8_t filter_value = - filter_data[Offset(filter_shape, out_channel, filter_y, - filter_x, in_channel)]; - scratch_buffer[Offset(output_shape, batch, out_y, out_x, - out_channel)] += - (input_value + input_offset) * - (filter_value + filter_offset); - } - } - } - } - } - } - } - } - for (int batch = 0; batch < batches; ++batch) { - for (int out_y = 0; out_y < output_height; ++out_y) { - for (int out_x = 0; out_x < output_width; ++out_x) { - for (int out_channel = 0; out_channel < output_depth; ++out_channel) { - int32_t acc = scratch_buffer[Offset(output_shape, batch, out_y, out_x, - out_channel)]; - if (bias_data) { - acc += bias_data[out_channel]; - } - int32_t scaled_acc = MultiplyByQuantizedMultiplier( - acc, output_multiplier, output_shift); - scaled_acc += output_offset; - scaled_acc = std::max(scaled_acc, output_activation_min); - scaled_acc = std::min(scaled_acc, output_activation_max); - output_data[Offset(output_shape, batch, out_y, out_x, out_channel)] = - static_cast(scaled_acc); - } - } - } - } -} - -} // namespace reference_ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_TRANSPOSE_CONV_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/runtime_shape.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/runtime_shape.h deleted file mode 100644 index c2678b57..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/runtime_shape.h +++ /dev/null @@ -1,158 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_RUNTIME_SHAPE_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_RUNTIME_SHAPE_H_ - -namespace tflite { - -template -struct Dims { - int sizes[N]; - int strides[N]; -}; - -class RuntimeShape { - public: - RuntimeShape& operator=(RuntimeShape const&) = delete; - - // RuntimeShape in TFLM supports up to 5 dimensions. - // The name kMaxSmallSize comes from the same file of the upstream - // tensorflow lite repo and need to be kept the same for max reuse. - static constexpr int kMaxSmallSize = 5; - - RuntimeShape() : size_(0) {} - - explicit RuntimeShape(int dimensions_count) : size_(dimensions_count) {} - - RuntimeShape(int shape_size, int32_t value) : size_(shape_size) { - for (int i = 0; i < shape_size; ++i) { - SetDim(i, value); - } - } - - RuntimeShape(int dimensions_count, const int32_t* dims_data) - : size_(dimensions_count) { - ReplaceWith(dimensions_count, dims_data); - } - - bool operator==(const RuntimeShape& comp) const { - return this->size_ == comp.size_ && - std::memcmp(DimsData(), comp.DimsData(), size_ * sizeof(int32_t)) == - 0; - } - - ~RuntimeShape() {} - - int32_t DimensionsCount() const { return size_; } - int32_t Dims(int i) const { - TFLITE_DCHECK_GE(i, 0); - TFLITE_DCHECK_LT(i, size_); - return dims_[i]; - } - void SetDim(int i, int32_t val) { - TFLITE_DCHECK_GE(i, 0); - TFLITE_DCHECK_LT(i, size_); - dims_[i] = val; - } - - static RuntimeShape ExtendedShape(int new_shape_size, - const RuntimeShape& shape) { - return RuntimeShape(new_shape_size, shape, 1); - } - int32_t* DimsData() { return dims_; } - const int32_t* DimsData() const { return dims_; } - const int32_t* DimsDataUpTo5D() const { return dims_; } - - void ReplaceWith(int dimensions_count, const int32_t* dims_data) { - size_ = dimensions_count; - int32_t* dst_dims = DimsData(); - std::memcpy(dst_dims, dims_data, dimensions_count * sizeof(int32_t)); - } - - // Returns the total count of elements, that is the size when flattened into a - // vector. - int FlatSize() const { - int buffer_size = 1; - const int* dims_data = reinterpret_cast(DimsData()); - for (int i = 0; i < size_; i++) { - buffer_size *= dims_data[i]; - } - return buffer_size; - } - - private: - // For use only by ExtendedShape(), written to guarantee (return-value) copy - // elision in C++17. - // This creates a shape padded to the desired size with the specified value. - RuntimeShape(int new_shape_size, const RuntimeShape& shape, int pad_value) - : size_(new_shape_size) { - // If the following check fails, it is likely because a 4D-only kernel is - // being used with an array of larger dimension count. - TFLITE_CHECK_GE(new_shape_size, shape.DimensionsCount()); - const int size_increase = new_shape_size - shape.DimensionsCount(); - for (int i = 0; i < size_increase; ++i) { - SetDim(i, pad_value); - } - std::memcpy(DimsData() + size_increase, shape.DimsData(), - sizeof(int32_t) * shape.DimensionsCount()); - } - - int32_t size_; - union { - int32_t dims_[kMaxSmallSize]; - }; -}; - -// Since tensors with '0' in their shape are valid in TF, these offset functions -// allow that as long as the corresponding index is also 0. It is upto the -// calling ops to ensure that they perform verification checks on tensor shapes -// if they don't support a particular behavior. - -inline int Offset(const RuntimeShape& shape, int i0, int i1, int i2, int i3) { - TFLITE_DCHECK_EQ(shape.DimensionsCount(), 4); - const int* dims_data = reinterpret_cast(shape.DimsData()); - TFLITE_DCHECK((dims_data[0] == 0 && i0 == 0) || - (i0 >= 0 && i0 < dims_data[0])); - TFLITE_DCHECK((dims_data[1] == 0 && i1 == 0) || - (i1 >= 0 && i1 < dims_data[1])); - TFLITE_DCHECK((dims_data[2] == 0 && i2 == 0) || - (i2 >= 0 && i2 < dims_data[2])); - TFLITE_DCHECK((dims_data[3] == 0 && i3 == 0) || - (i3 >= 0 && i3 < dims_data[3])); - return ((i0 * dims_data[1] + i1) * dims_data[2] + i2) * dims_data[3] + i3; -} - -inline int Offset(const RuntimeShape& shape, int i0, int i1, int i2, int i3, - int i4) { - TFLITE_DCHECK_EQ(shape.DimensionsCount(), 5); - const int* dims_data = reinterpret_cast(shape.DimsData()); - TFLITE_DCHECK((dims_data[0] == 0 && i0 == 0) || - (i0 >= 0 && i0 < dims_data[0])); - TFLITE_DCHECK((dims_data[1] == 0 && i1 == 0) || - (i1 >= 0 && i1 < dims_data[1])); - TFLITE_DCHECK((dims_data[2] == 0 && i2 == 0) || - (i2 >= 0 && i2 < dims_data[2])); - TFLITE_DCHECK((dims_data[3] == 0 && i3 == 0) || - (i3 >= 0 && i3 < dims_data[3])); - TFLITE_DCHECK((dims_data[4] == 0 && i4 == 0) || - (i4 >= 0 && i4 < dims_data[4])); - return (((i0 * dims_data[1] + i1) * dims_data[2] + i2) * dims_data[3] + i3) * - dims_data[4] + - i4; -} - -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_RUNTIME_SHAPE_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/strided_slice_logic.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/strided_slice_logic.h deleted file mode 100644 index bfe84050..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/strided_slice_logic.h +++ /dev/null @@ -1,211 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_STRIDED_SLICE_LOGIC_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_STRIDED_SLICE_LOGIC_H_ - -#include -#include - -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { -namespace strided_slice { - -// Use until std::clamp() is available from C++17. -inline int Clamp(const int v, const int lo, const int hi) { - TFLITE_DCHECK(!(hi < lo)); - if (hi < v) return hi; - if (v < lo) return lo; - return v; -} - -inline void StridedSlicePadIndices(tflite::StridedSliceParams* p, - int dim_count) { - // Add indices and mask bits to fully include extra dimensions - TFLITE_CHECK_LE(dim_count, 5); - TFLITE_CHECK_GE(dim_count, p->start_indices_count); - TFLITE_CHECK_EQ(p->start_indices_count, p->stop_indices_count); - TFLITE_CHECK_EQ(p->stop_indices_count, p->strides_count); - - const int pad_count = dim_count - p->start_indices_count; - - // Pad indices at start, so move arrays by pad_count. - for (int i = p->start_indices_count - 1; i >= 0; --i) { - p->strides[i + pad_count] = p->strides[i]; - p->start_indices[i + pad_count] = p->start_indices[i]; - p->stop_indices[i + pad_count] = p->stop_indices[i]; - } - for (int i = 0; i < pad_count; ++i) { - p->start_indices[i] = 0; - p->stop_indices[i] = 1; - p->strides[i] = 1; - } - - // Pad masks with 0s or 1s as required. - p->shrink_axis_mask <<= pad_count; - p->ellipsis_mask <<= pad_count; - p->new_axis_mask <<= pad_count; - p->begin_mask <<= pad_count; - p->end_mask <<= pad_count; - p->begin_mask |= (1 << pad_count) - 1; - p->end_mask |= (1 << pad_count) - 1; - - p->start_indices_count = dim_count; - p->stop_indices_count = dim_count; - p->strides_count = dim_count; -} - -// Return the index for the first element along that axis. This index will be a -// positive integer between [0, axis_size] (or [-1, axis_size -1] if stride < 0) -// that can be used to index directly into the data. -inline int StartForAxis(const tflite::StridedSliceParams& params, - const RuntimeShape& input_shape, int axis) { - const auto begin_mask = params.begin_mask; - const auto* start_indices = params.start_indices; - const auto* strides = params.strides; - const int axis_size = input_shape.Dims(axis); - if (axis_size == 0) { - return 0; - } - // Begin with the specified index. - int start = start_indices[axis]; - - // begin_mask override - if (begin_mask & 1 << axis) { - if (strides[axis] > 0) { - // Forward iteration - use the first element. These values will get - // clamped below (Note: We could have set them to 0 and axis_size-1, but - // use lowest() and max() to maintain symmetry with StopForAxis()) - start = std::numeric_limits::lowest(); - } else { - // Backward iteration - use the last element. - start = std::numeric_limits::max(); - } - } - - // Handle negative indices - if (start < 0) { - start += axis_size; - } - - // Clamping - if (strides[axis] > 0) { - // Forward iteration - start = Clamp(start, 0, axis_size); - } else { - // Backward iteration - start = Clamp(start, -1, axis_size - 1); - } - - return start; -} - -// Return the "real" index for the end of iteration along that axis. This is an -// "end" in the traditional C sense, in that it points to one past the last -// element. ie. So if you were iterating through all elements of a 1D array of -// size 4, this function would return 4 as the stop, because it is one past the -// "real" indices of 0, 1, 2 & 3. -inline int StopForAxis(const tflite::StridedSliceParams& params, - const RuntimeShape& input_shape, int axis, - int start_for_axis) { - const auto end_mask = params.end_mask; - const auto shrink_axis_mask = params.shrink_axis_mask; - const auto* stop_indices = params.stop_indices; - const auto* strides = params.strides; - const int axis_size = input_shape.Dims(axis); - if (axis_size == 0) { - return 0; - } - - // Begin with the specified index - const bool shrink_axis = shrink_axis_mask & (1 << axis); - int stop = stop_indices[axis]; - - // When shrinking an axis, the end position does not matter (and can be - // incorrect when negative indexing is used, see Issue #19260). Always use - // start_for_axis + 1 to generate a length 1 slice, since start_for_axis has - // already been adjusted for negative indices. - if (shrink_axis) { - return start_for_axis + 1; - } - - // end_mask override - if (end_mask & (1 << axis)) { - if (strides[axis] > 0) { - // Forward iteration - use the last element. These values will get - // clamped below - stop = std::numeric_limits::max(); - } else { - // Backward iteration - use the first element. - stop = std::numeric_limits::lowest(); - } - } - - // Handle negative indices - if (stop < 0) { - stop += axis_size; - } - - // Clamping - // Because the end index points one past the last element, we need slightly - // different clamping ranges depending on the direction. - if (strides[axis] > 0) { - // Forward iteration - stop = Clamp(stop, 0, axis_size); - } else { - // Backward iteration - stop = Clamp(stop, -1, axis_size - 1); - } - - return stop; -} - -inline bool LoopCondition(int index, int stop, int stride) { - // True when we have reached the end of an axis and should loop. - return stride > 0 ? index >= stop : index <= stop; -} - -inline tflite::StridedSliceParams BuildStridedSliceParams( - int begin_mask, int end_mask, int shrink_axis_mask, - const std::vector& start_indices, const std::vector& stop_indices, - const std::vector& strides) { - tflite::StridedSliceParams op_params; - const int dims_count = start_indices.size(); - - op_params.start_indices_count = dims_count; - op_params.stop_indices_count = dims_count; - op_params.strides_count = dims_count; - for (int i = 0; i < dims_count; ++i) { - op_params.start_indices[i] = start_indices[i]; - op_params.stop_indices[i] = stop_indices[i]; - op_params.strides[i] = strides[i]; - } - - op_params.begin_mask = begin_mask; - op_params.ellipsis_mask = 0; - op_params.end_mask = end_mask; - op_params.new_axis_mask = 0; - op_params.shrink_axis_mask = shrink_axis_mask; - - return op_params; -} - -} // namespace strided_slice - -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_STRIDED_SLICE_LOGIC_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/tensor_ctypes.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/tensor_ctypes.h deleted file mode 100644 index f1d3e17f..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/tensor_ctypes.h +++ /dev/null @@ -1,47 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_TENSOR_CTYPES_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_TENSOR_CTYPES_H_ - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -template -inline T* GetTensorData(TfLiteTensor* tensor) { - return tensor != nullptr ? reinterpret_cast(tensor->data.raw) : nullptr; -} - -template -inline const T* GetTensorData(const TfLiteTensor* tensor) { - return tensor != nullptr ? reinterpret_cast(tensor->data.raw) - : nullptr; -} - -inline RuntimeShape GetTensorShape(const TfLiteTensor* tensor) { - if (tensor == nullptr) { - return RuntimeShape(); - } - - TfLiteIntArray* dims = tensor->dims; - const int dims_size = dims->size; - const int32_t* dims_data = reinterpret_cast(dims->data); - return RuntimeShape(dims_size, dims_data); -} - -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_TENSOR_CTYPES_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/internal/types.h b/code/components/tflite-lib/tensorflow/lite/kernels/internal/types.h deleted file mode 100644 index c44ba48e..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/internal/types.h +++ /dev/null @@ -1,1065 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_TYPES_H_ -#define TENSORFLOW_LITE_KERNELS_INTERNAL_TYPES_H_ - -#include -#include -#include -#include - -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/internal/runtime_shape.h" - -namespace tflite { - -enum class FusedActivationFunctionType : uint8_t { - kNone, - kRelu6, - kRelu1, - kRelu -}; -enum class PaddingType : uint8_t { kNone, kSame, kValid }; - -struct PaddingValues { - int16_t width; - int16_t height; - // offset is used for calculating "remaining" padding, for example, `width` - // is 1 and `width_offset` is 1, so padding_left is 1 while padding_right is - // 1 + 1 = 2. - int16_t width_offset; - // Same as width_offset except it's over the height dimension. - int16_t height_offset; -}; - -struct Padding3DValues { - int16_t width; - int16_t height; - int16_t depth; - // offset is used for calculating "remaining" padding, for example, `width` - // is 1 and `width_offset` is 1, so padding_left is 1 while padding_right is - // 1 + 1 = 2. - int16_t width_offset; - // Same as width_offset except it's over the height dimension. - int16_t height_offset; - // Same as width_offset except it's over the depth dimension. - int16_t depth_offset; -}; - -// This enumeration allows for non-default formats for the weights array -// of a fully-connected operator, allowing the use of special optimized -// runtime paths. -enum class FullyConnectedWeightsFormat : uint8_t { - // Default format (flat 2D layout, the inner contiguous dimension - // is input_depth, the outer non-contiguous dimension is output_depth) - kDefault, - // Summary: optimized layout for fast CPU runtime implementation, - // aimed specifically at ARM CPUs at the moment, and specialized for - // 8-bit quantized layers. - // - // The use case we're concerned with here is: 8-bit quantization, - // large weights matrix that doesn't fit in cache (e.g. 4096x2048 in - // a key application that drove this), very small batch size (e.g. 1 -- 4). - // - // Even with 8-bit quantization of weights, the performance of memory - // accesses to the weights can become the dominant issue when - // the batch size is small, so each weight value is used in only a few - // arithmetic ops, i.e. the fully-connected node has a low arithmetic - // intensity. The specific issues that arise are of three kinds: - // (1) One may, ideally, max out DRAM bandwidth, i.e. be truly memory - // bound. That's the "good" issue to run into. - // (2) One may run into sub-optimal pre-fetching: the data hasn't been - // prefetched into the cache by the time we need it. - // (3) One may run into cache aliasing: multiple values that are - // pre-fetched, alias each other in the L1 cache (which typically - // has only 4-way set associativity in ARM CPUs) and thus evict - // each other before we get to using them. - // - // The point of this shuffling is to avoid issues (2) and (3) so that - // we get as fast as possible given only the hard constraint (1). - // This is achieved by turning the difficulty into a solution: the - // difficulty, that each value loaded from memory is used only in - // one kernel iteration, making this operation memory-intensive, hints at - // the solution, of shuffling the weights so that they are stored in the - // exact order as the kernel needs to load them, so that the memory - // accesses made by the kernel are trivial. This solves (2) because the - // trivial memory access pattern allows the CPU's automatic prefetching - // to perform very well (no need even for preload instructions), and this - // solves (3) because the values being loaded concurrently are now - // contiguous in the address space, thus don't alias each other in the cache. - // - // On ARM, we typically want our kernel to process a 4x16 block of weights - // at a time, because: - // - 16 is the number of bytes in a NEON register. - // - 4 is how many rows we need to handle concurrently in the kernel in - // order to have sufficient mutual independence of instructions to - // maximize arithmetic throughput. - // - // Finally, the 'Int8' part in the name refers to the fact that this - // weights format has each weights value encoded as a signed int8_t value, - // even if the data type of the weights buffer is uint8_t. This is intended - // to save runtime kernels the effort to have to XOR the top bit of these - // bytes before using them in signed arithmetic, see this file for more - // explanations on the 'signed int8_t trick' in matrix multiplication kernels: - // - // tensorflow/lite/toco/graph_transformations/ensure_uint8_weights_safe_for_fast_int8_kernels.cc - // - kShuffled4x16Int8, -}; - -// Quantization parameters, determining the mapping of quantized values -// to real values (i.e. determining how quantized values are mathematically -// interpreted). -// -// The correspondence is as follows: -// -// real_value = scale * (quantized_value - zero_point); -// -// In other words, zero_point designates which quantized value corresponds to -// the real 0 value, and scale designates the difference between the real values -// corresponding to consecutive quantized values differing by 1. -struct QuantizationParams { - int32_t zero_point = 0; - double scale = 0.0; -}; - -inline bool operator==(const QuantizationParams& qp1, - const QuantizationParams& qp2) { - return qp1.zero_point == qp2.zero_point && qp1.scale == qp2.scale; -} - -// Quantization parameters for each channel, determining the mapping of -// quantized values to real values. See QuantizationParams for a single set of -// parameters per tensor. This has one parameters set per each channel. -// -// The correspondence is as follows: -// -// real_value = scale[channel] * (quantized_value - zero_point[channel]); -// -struct PerChannelQuantizationParams { - // The following members typically point to the corresponding members of a - // TfLiteAffineQuantization struct. - const float* scale; - const int32_t* zero_point; - int32_t quantized_dimension; -}; - -// Gets next index to iterate through a multidimensional array. -inline bool NextIndex(const int num_dims, const int* dims, int* current) { - if (num_dims == 0) { - return false; - } - TFLITE_DCHECK(dims != nullptr); - TFLITE_DCHECK(current != nullptr); - int carry = 1; - for (int idx = num_dims - 1; idx >= 0; --idx) { - int current_val = current[idx] + carry; - TFLITE_DCHECK_GE(dims[idx], current_val); - if (dims[idx] == current_val) { - current[idx] = 0; - } else { - current[idx] = current_val; - carry = 0; - break; - } - } - return (carry == 0); -} - -// Gets offset of index if reducing on axis. When reducing, the flattened offset -// will not change, if the input index changes on the given axis. For example, -// if you have a 3D tensor and you are reducing to 2D by eliminating axis 0, -// then index (0, 1, 2) and index (1, 1, 2) will map to the same flattened -// offset. -// TODO(kanlig): uses Dims to represent dimensions. -inline size_t ReducedOutputOffset(const int num_dims, const int* dims, - const int* index, const int num_axis, - const int* axis) { - if (num_dims == 0) { - return 0; - } - TFLITE_DCHECK(dims != nullptr); - TFLITE_DCHECK(index != nullptr); - size_t offset = 0; - for (int idx = 0; idx < num_dims; ++idx) { - // if we need to skip this axis - bool is_axis = false; - if (axis != nullptr) { - for (int axis_idx = 0; axis_idx < num_axis; ++axis_idx) { - if (idx == axis[axis_idx]) { - is_axis = true; - break; - } - } - } - if (!is_axis) { - offset = offset * static_cast(dims[idx]) + - static_cast(index[idx]); - } - } - return offset; -} - -// Since tensors with '0' in their shape are valid in TF, these offset functions -// allow that as long as the corresponding index is also 0. It is upto the -// calling ops to ensure that they perform verification checks on tensor shapes -// if they don't support a particular behavior. - -inline int Offset(const Dims<4>& dims, int i0, int i1, int i2, int i3) { - TFLITE_DCHECK((i0 == 0 && dims.sizes[0] == 0) || - (i0 >= 0 && i0 < dims.sizes[0])); - TFLITE_DCHECK((i1 == 0 && dims.sizes[1] == 0) || - (i1 >= 0 && i1 < dims.sizes[1])); - TFLITE_DCHECK((i2 == 0 && dims.sizes[2] == 0) || - (i2 >= 0 && i2 < dims.sizes[2])); - TFLITE_DCHECK((i3 == 0 && dims.sizes[3] == 0) || - (i3 >= 0 && i3 < dims.sizes[3])); - return i0 * dims.strides[0] + i1 * dims.strides[1] + i2 * dims.strides[2] + - i3 * dims.strides[3]; -} - -inline int Offset(const Dims<4>& dims, int* index) { - return Offset(dims, index[0], index[1], index[2], index[3]); -} - -// Get array size, DCHECKing that the dim index is in range. -// -// Note that this will be phased out with Dims<4>, since RuntimeShape::Dims() -// already performs this check. -template -int ArraySize(const Dims& array, int index) { - TFLITE_DCHECK(index >= 0 && index < N); - return array.sizes[index]; -} - -// Get common array size, DCHECKing that they all agree. -template -int MatchingArraySize(const ArrayType1& array1, int index1, - const ArrayType2& array2, int index2) { - TFLITE_DCHECK_EQ(ArraySize(array1, index1), ArraySize(array2, index2)); - return ArraySize(array1, index1); -} - -template -int MatchingArraySize(const ArrayType1& array1, int index1, - const ArrayType2& array2, int index2, Args... args) { - TFLITE_DCHECK_EQ(ArraySize(array1, index1), ArraySize(array2, index2)); - return MatchingArraySize(array1, index1, args...); -} - -// Get common shape dim, DCHECKing that they all agree. -inline int MatchingDim(const RuntimeShape& shape1, int index1, - const RuntimeShape& shape2, int index2) { - TFLITE_DCHECK_EQ(shape1.Dims(index1), shape2.Dims(index2)); - return std::min(shape1.Dims(index1), shape2.Dims(index2)); -} - -template -int MatchingDim(const RuntimeShape& shape1, int index1, - const RuntimeShape& shape2, int index2, Args... args) { - TFLITE_DCHECK_EQ(shape1.Dims(index1), shape2.Dims(index2)); - return MatchingDim(shape1, index1, args...); -} - -// Will be phased out with Dims<4>, replaced by RuntimeShape::FlatSize(). -template -inline int FlatSize(const Dims& dims) { - int flat_size = 1; - for (int i = 0; i < N; ++i) { - flat_size *= dims.sizes[i]; - } - return flat_size; -} - -TFLITE_DEPRECATED("Prefer FlatSize.") -inline int RequiredBufferSizeForDims(const Dims<4>& dims) { - return FlatSize(dims); -} - -inline int MatchingElementsSize(const RuntimeShape& shape, - const RuntimeShape& check_shape_0) { - const int size_1 = shape.FlatSize(); - const int size_2 = check_shape_0.FlatSize(); - TFLITE_CHECK_EQ(size_1, size_2); - return size_1; -} - -inline int MatchingElementsSize(const RuntimeShape& shape, - const RuntimeShape& check_shape_0, - const RuntimeShape& check_shape_1) { - const int size_1 = shape.FlatSize(); - const int size_2 = check_shape_0.FlatSize(); - const int size_3 = check_shape_1.FlatSize(); - TFLITE_CHECK_EQ(size_1, size_2); - TFLITE_CHECK_EQ(size_2, size_3); - return size_1; -} - -// Flat size calculation, checking that dimensions match with one or more other -// arrays. -inline int MatchingFlatSize(const RuntimeShape& shape, - const RuntimeShape& check_shape_0) { - TFLITE_DCHECK_EQ(shape.DimensionsCount(), check_shape_0.DimensionsCount()); - const int dims_count = shape.DimensionsCount(); - for (int i = 0; i < dims_count; ++i) { - TFLITE_DCHECK_EQ(shape.Dims(i), check_shape_0.Dims(i)); - } - return shape.FlatSize(); -} - -inline int MatchingFlatSize(const RuntimeShape& shape, - const RuntimeShape& check_shape_0, - const RuntimeShape& check_shape_1) { - TFLITE_DCHECK_EQ(shape.DimensionsCount(), check_shape_0.DimensionsCount()); - const int dims_count = shape.DimensionsCount(); - for (int i = 0; i < dims_count; ++i) { - TFLITE_DCHECK_EQ(shape.Dims(i), check_shape_0.Dims(i)); - } - return MatchingFlatSize(shape, check_shape_1); -} - -inline int MatchingFlatSize(const RuntimeShape& shape, - const RuntimeShape& check_shape_0, - const RuntimeShape& check_shape_1, - const RuntimeShape& check_shape_2) { - TFLITE_DCHECK_EQ(shape.DimensionsCount(), check_shape_0.DimensionsCount()); - const int dims_count = shape.DimensionsCount(); - for (int i = 0; i < dims_count; ++i) { - TFLITE_DCHECK_EQ(shape.Dims(i), check_shape_0.Dims(i)); - } - return MatchingFlatSize(shape, check_shape_1, check_shape_2); -} - -inline int MatchingFlatSize(const RuntimeShape& shape, - const RuntimeShape& check_shape_0, - const RuntimeShape& check_shape_1, - const RuntimeShape& check_shape_2, - const RuntimeShape& check_shape_3) { - TFLITE_DCHECK_EQ(shape.DimensionsCount(), check_shape_0.DimensionsCount()); - const int dims_count = shape.DimensionsCount(); - for (int i = 0; i < dims_count; ++i) { - TFLITE_DCHECK_EQ(shape.Dims(i), check_shape_0.Dims(i)); - } - return MatchingFlatSize(shape, check_shape_1, check_shape_2, check_shape_3); -} - -// Flat size calculation, checking that dimensions match with one or more other -// arrays. -template -inline int MatchingFlatSize(const Dims& dims, const Dims& check_dims_0) { - for (int i = 0; i < N; ++i) { - TFLITE_DCHECK_EQ(ArraySize(dims, i), ArraySize(check_dims_0, i)); - } - return FlatSize(dims); -} - -template -inline int MatchingFlatSize(const Dims& dims, const Dims& check_dims_0, - const Dims& check_dims_1) { - for (int i = 0; i < N; ++i) { - TFLITE_DCHECK_EQ(ArraySize(dims, i), ArraySize(check_dims_0, i)); - } - return MatchingFlatSize(dims, check_dims_1); -} - -template -inline int MatchingFlatSize(const Dims& dims, const Dims& check_dims_0, - const Dims& check_dims_1, - const Dims& check_dims_2) { - for (int i = 0; i < N; ++i) { - TFLITE_DCHECK_EQ(ArraySize(dims, i), ArraySize(check_dims_0, i)); - } - return MatchingFlatSize(dims, check_dims_1, check_dims_2); -} - -template -inline int MatchingFlatSize(const Dims& dims, const Dims& check_dims_0, - const Dims& check_dims_1, - const Dims& check_dims_2, - const Dims& check_dims_3) { - for (int i = 0; i < N; ++i) { - TFLITE_DCHECK_EQ(ArraySize(dims, i), ArraySize(check_dims_0, i)); - } - return MatchingFlatSize(dims, check_dims_1, check_dims_2, check_dims_3); -} - -// Flat size calculation, checking if their extended shapes match. -inline int MatchingExtendedShapeFlatSize(const RuntimeShape& shape, - const RuntimeShape& check_shape_0) { - const int shape_dims = shape.DimensionsCount(); - const int check_shape_0_dims = check_shape_0.DimensionsCount(); - const int min_dims = std::min(shape_dims, check_shape_0_dims); - - for (int i = 0; i < min_dims; ++i) { - TFLITE_DCHECK_EQ(shape.Dims(shape_dims - 1 - i), - check_shape_0.Dims(check_shape_0_dims - 1 - i)); - } - for (int i = min_dims; i < shape_dims; ++i) { - TFLITE_DCHECK_EQ(shape.Dims(shape_dims - 1 - i), 1); - } - for (int i = min_dims; i < check_shape_0_dims; ++i) { - TFLITE_DCHECK_EQ(check_shape_0.Dims(check_shape_0_dims - 1 - i), 1); - } - return shape.FlatSize(); -} - -inline int MatchingExtendedShapeFlatSize(const RuntimeShape& shape, - const RuntimeShape& check_shape_0, - const RuntimeShape& check_shape_1) { - const int flat_size = MatchingExtendedShapeFlatSize(shape, check_shape_0); - TFLITE_DCHECK_EQ(MatchingExtendedShapeFlatSize(shape, check_shape_1), - flat_size); - return flat_size; -} - -inline int MatchingExtendedShapeFlatSize(const RuntimeShape& shape, - const RuntimeShape& check_shape_0, - const RuntimeShape& check_shape_1, - const RuntimeShape& check_shape_2) { - const int flat_size = MatchingExtendedShapeFlatSize(shape, check_shape_0); - TFLITE_DCHECK_EQ( - MatchingExtendedShapeFlatSize(shape, check_shape_1, check_shape_2), - flat_size); - return flat_size; -} - -inline int MatchingExtendedShapeFlatSize(const RuntimeShape& shape, - const RuntimeShape& check_shape_0, - const RuntimeShape& check_shape_1, - const RuntimeShape& check_shape_2, - const RuntimeShape& check_shape_3) { - const int flat_size = MatchingExtendedShapeFlatSize(shape, check_shape_0); - TFLITE_DCHECK_EQ(MatchingExtendedShapeFlatSize(shape, check_shape_1, - check_shape_2, check_shape_3), - flat_size); - return flat_size; -} - -// Data is required to be contiguous, and so many operators can use either the -// full array flat size or the flat size with one dimension skipped (commonly -// the depth). -template -inline int FlatSizeSkipDim(const Dims& dims, int skip_dim) { - TFLITE_DCHECK(skip_dim >= 0 && skip_dim < N); - int flat_size = 1; - for (int i = 0; i < N; ++i) { - flat_size *= (i == skip_dim) ? 1 : dims.sizes[i]; - } - return flat_size; -} - -// A combination of MatchingFlatSize() and FlatSizeSkipDim(). -template -inline int MatchingFlatSizeSkipDim(const Dims& dims, int skip_dim, - const Dims& check_dims_0) { - for (int i = 0; i < N; ++i) { - if (i != skip_dim) { - TFLITE_DCHECK_EQ(ArraySize(dims, i), ArraySize(check_dims_0, i)); - } - } - return FlatSizeSkipDim(dims, skip_dim); -} - -template -inline int MatchingFlatSizeSkipDim(const Dims& dims, int skip_dim, - const Dims& check_dims_0, - const Dims& check_dims_1) { - for (int i = 0; i < N; ++i) { - if (i != skip_dim) { - TFLITE_DCHECK_EQ(ArraySize(dims, i), ArraySize(check_dims_0, i)); - } - } - return MatchingFlatSizeSkipDim(dims, skip_dim, check_dims_1); -} - -template -inline int MatchingFlatSizeSkipDim(const Dims& dims, int skip_dim, - const Dims& check_dims_0, - const Dims& check_dims_1, - const Dims& check_dims_2) { - for (int i = 0; i < N; ++i) { - if (i != skip_dim) { - TFLITE_DCHECK_EQ(ArraySize(dims, i), ArraySize(check_dims_0, i)); - } - } - return MatchingFlatSizeSkipDim(dims, skip_dim, check_dims_1, check_dims_2); -} - -template -inline int MatchingFlatSizeSkipDim(const Dims& dims, int skip_dim, - const Dims& check_dims_0, - const Dims& check_dims_1, - const Dims& check_dims_2, - const Dims& check_dims_3) { - for (int i = 0; i < N; ++i) { - if (i != skip_dim) { - TFLITE_DCHECK_EQ(ArraySize(dims, i), ArraySize(check_dims_0, i)); - } - } - return MatchingFlatSizeSkipDim(dims, skip_dim, check_dims_1, check_dims_2, - check_dims_3); -} - -// Data is required to be contiguous, and so many operators can use either the -// full array flat size or the flat size with one dimension skipped (commonly -// the depth). -inline int FlatSizeSkipDim(const RuntimeShape& shape, int skip_dim) { - const int dims_count = shape.DimensionsCount(); - TFLITE_DCHECK(skip_dim >= 0 && skip_dim < dims_count); - const auto* dims_data = shape.DimsData(); - int flat_size = 1; - for (int i = 0; i < dims_count; ++i) { - flat_size *= (i == skip_dim) ? 1 : dims_data[i]; - } - return flat_size; -} - -// A combination of MatchingFlatSize() and FlatSizeSkipDim(). -inline int MatchingFlatSizeSkipDim(const RuntimeShape& shape, int skip_dim, - const RuntimeShape& check_shape_0) { - const int dims_count = shape.DimensionsCount(); - for (int i = 0; i < dims_count; ++i) { - if (i != skip_dim) { - TFLITE_DCHECK_EQ(shape.Dims(i), check_shape_0.Dims(i)); - } - } - return FlatSizeSkipDim(shape, skip_dim); -} - -inline int MatchingFlatSizeSkipDim(const RuntimeShape& shape, int skip_dim, - const RuntimeShape& check_shape_0, - const RuntimeShape& check_shape_1) { - const int dims_count = shape.DimensionsCount(); - for (int i = 0; i < dims_count; ++i) { - if (i != skip_dim) { - TFLITE_DCHECK_EQ(shape.Dims(i), check_shape_0.Dims(i)); - } - } - return MatchingFlatSizeSkipDim(shape, skip_dim, check_shape_1); -} - -inline int MatchingFlatSizeSkipDim(const RuntimeShape& shape, int skip_dim, - const RuntimeShape& check_shape_0, - const RuntimeShape& check_shape_1, - const RuntimeShape& check_shape_2) { - const int dims_count = shape.DimensionsCount(); - for (int i = 0; i < dims_count; ++i) { - if (i != skip_dim) { - TFLITE_DCHECK_EQ(shape.Dims(i), check_shape_0.Dims(i)); - } - } - return MatchingFlatSizeSkipDim(shape, skip_dim, check_shape_1, check_shape_2); -} - -inline int MatchingFlatSizeSkipDim(const RuntimeShape& shape, int skip_dim, - const RuntimeShape& check_shape_0, - const RuntimeShape& check_shape_1, - const RuntimeShape& check_shape_2, - const RuntimeShape& check_shape_3) { - const int dims_count = shape.DimensionsCount(); - for (int i = 0; i < dims_count; ++i) { - if (i != skip_dim) { - TFLITE_DCHECK_EQ(shape.Dims(i), check_shape_0.Dims(i)); - } - } - return MatchingFlatSizeSkipDim(shape, skip_dim, check_shape_1, check_shape_2, - check_shape_3); -} - -template -bool IsPackedWithoutStrides(const Dims& dims) { - int expected_stride = 1; - for (int d = 0; d < N; d++) { - if (dims.strides[d] != expected_stride) return false; - expected_stride *= dims.sizes[d]; - } - return true; -} - -template -void ComputeStrides(Dims* dims) { - dims->strides[0] = 1; - for (int d = 1; d < N; d++) { - dims->strides[d] = dims->strides[d - 1] * dims->sizes[d - 1]; - } -} - -enum class BroadcastableOpCategory : uint8_t { - kNone, - kNonBroadcast, // Matching input shapes. - kFirstInputBroadcastsFast, // Fivefold nested loops. - kSecondInputBroadcastsFast, // Fivefold nested loops. - kGenericBroadcast, // Fall-back. -}; - -struct MinMax { - float min; - float max; -}; -static_assert(sizeof(MinMax) == 8, ""); - -struct ActivationParams { - FusedActivationFunctionType activation_type; - // uint8_t, etc, activation params. - int32_t quantized_activation_min; - int32_t quantized_activation_max; -}; - -struct ReluParams : public ActivationParams { - int32_t input_offset; - int32_t output_offset; - int32_t output_multiplier; - int output_shift; -}; - -// Styles of resizing op usages. For example, kImageStyle can be used with a Pad -// op for pattern-specific optimization. -enum class ResizingCategory : uint8_t { - kNone, - kImageStyle, // 4D, operating on inner dimensions, say {0, a, b, 0}. - kGenericResize, -}; - -// For Add, Sub, Mul ops. -struct ArithmeticParams { - // Shape dependent / common to data / op types. - BroadcastableOpCategory broadcast_category; - // uint8_t inference params. - int32_t input1_offset; - int32_t input2_offset; - int32_t output_offset; - int32_t output_multiplier; - int output_shift; - // Add / Sub, not Mul, uint8_t inference params. - int left_shift; - int32_t input1_multiplier; - int input1_shift; - int32_t input2_multiplier; - int input2_shift; - - // TODO(b/158622529): Union the following activation params. - // uint8_t, etc, activation params. - int32_t quantized_activation_min; - int32_t quantized_activation_max; - // float activation params. - float float_activation_min; - float float_activation_max; - // int64_t activation params. - int64_t int64_activation_min; - int64_t int64_activation_max; - - // Processed output dimensions. - // Let input "a" be the one that broadcasts in the faster-changing dimension. - // Then, after coalescing, for shapes {a0, a1, a2, a3, a4} and - // {b0, b1, b2, b3, b4}, - // broadcast_shape[4] = b0 = a0. - // broadcast_shape[3] = b1; a1 = 1. - // broadcast_shape[2] = b2 = a2. - // broadcast_shape[1] = a3; b3 = 1. - // broadcast_shape[0] = b4 = a4. - int broadcast_shape[5]; -}; - -struct ConcatenationParams { - int8_t axis; - const int32_t* input_zeropoint; - const float* input_scale; - uint16_t inputs_count; - int32_t output_zeropoint; - float output_scale; -}; - -struct ComparisonParams { - // uint8_t inference params. - int left_shift; - int32_t input1_offset; - int32_t input1_multiplier; - int input1_shift; - int32_t input2_offset; - int32_t input2_multiplier; - int input2_shift; - // Shape dependent / common to inference types. - bool is_broadcast; -}; - -struct ConvParams { - PaddingType padding_type; - PaddingValues padding_values; - // TODO(starka): This was just "stride", so check that width+height is OK. - int16_t stride_width; - int16_t stride_height; - int16_t dilation_width_factor; - int16_t dilation_height_factor; - // uint8_t inference params. - // TODO(b/65838351): Use smaller types if appropriate. - int32_t input_offset; - int32_t weights_offset; - int32_t output_offset; - int32_t output_multiplier; - int output_shift; - // uint8_t, etc, activation params. - int32_t quantized_activation_min; - int32_t quantized_activation_max; - // float activation params. - float float_activation_min; - float float_activation_max; -}; - -struct Conv3DParams { - Padding3DValues padding_values; - int stride_width; - int stride_height; - int stride_depth; - int dilation_width; - int dilation_height; - int dilation_depth; - // float activation params. - float float_activation_min; - float float_activation_max; -}; - -typedef Conv3DParams Conv3DTransposeParams; - -struct DepthToSpaceParams { - int32_t block_size; -}; - -struct DepthwiseParams { - PaddingType padding_type; - PaddingValues padding_values; - int16_t stride_width; - int16_t stride_height; - int16_t dilation_width_factor; - int16_t dilation_height_factor; - int16_t depth_multiplier; - // uint8_t inference params. - // TODO(b/65838351): Use smaller types if appropriate. - int32_t input_offset; - int32_t weights_offset; - int32_t output_offset; - int32_t output_multiplier; - int output_shift; - // uint8_t, etc, activation params. - int32_t quantized_activation_min; - int32_t quantized_activation_max; - // float activation params. - float float_activation_min; - float float_activation_max; - const int32_t* output_multiplier_per_channel; - const int32_t* output_shift_per_channel; -}; - -struct DequantizationParams { - double scale; - int32_t zero_point; -}; - -struct PerChannelDequantizationParams { - const float* scale; - const int32_t* zero_point; - int32_t quantized_dimension; -}; - -struct FakeQuantParams { - MinMax minmax; - int32_t num_bits; -}; - -struct FullyConnectedParams { - // uint8_t inference params. - // TODO(b/65838351): Use smaller types if appropriate. - int32_t input_offset; - int32_t weights_offset; - int32_t output_offset; - int32_t output_multiplier; - int output_shift; - // uint8_t, etc, activation params. - int32_t quantized_activation_min; - int32_t quantized_activation_max; - // float activation params. - float float_activation_min; - float float_activation_max; - // Mark the operands as cacheable if they are unchanging, e.g. weights. - bool lhs_cacheable; - bool rhs_cacheable; - FullyConnectedWeightsFormat weights_format; -}; - -struct GatherParams { - int16_t axis; - int16_t batch_dims; -}; - -struct L2NormalizationParams { - // uint8_t inference params. - int32_t input_zero_point; -}; - -struct LocalResponseNormalizationParams { - int32_t range; - double bias; - double alpha; - double beta; -}; - -struct HardSwishParams { - // zero_point of the input activations. - int16_t input_zero_point; - // zero_point of the output activations. - int16_t output_zero_point; - // 16bit fixed-point component of the multiplier to apply to go from the - // "high-res input scale", which is the input scale multiplied by 2^7, to the - // "relu-ish scale", which 3.0/32768. - // See the implementation of HardSwishPrepare. - int16_t reluish_multiplier_fixedpoint_int16; - // exponent/bit-shift component of the aforementioned multiplier. - int reluish_multiplier_exponent; - // 16bit fixed-point component of the multiplier to apply to go from the - // "high-res input scale", which is the input scale multiplied by 2^7, to the - // output scale. - // See the implementation of HardSwishPrepare. - int16_t output_multiplier_fixedpoint_int16; - // exponent/bit-shift component of the aforementioned multiplier. - int output_multiplier_exponent; -}; - -struct LogisticParams { - // uint8_t inference params. - int32_t input_zero_point; - int32_t input_range_radius; - int32_t input_multiplier; - int input_left_shift; -}; - -struct LstmCellParams { - int32_t weights_zero_point; - int32_t accum_multiplier; - int accum_shift; - int state_integer_bits; -}; - -struct MeanParams { - int8_t axis_count; - int16_t axis[4]; -}; - -struct PackParams { - int8_t axis; - const int32_t* input_zeropoint; - const float* input_scale; - uint16_t inputs_count; - int32_t output_zeropoint; - float output_scale; -}; - -struct PadParams { - int8_t left_padding_count; - int32_t left_padding[5]; - int8_t right_padding_count; - int32_t right_padding[5]; - ResizingCategory resizing_category; -}; - -struct PreluParams { - int32_t input_offset; - int32_t alpha_offset; - int32_t output_offset; - int32_t output_multiplier_1; - int output_shift_1; - int32_t output_multiplier_2; - int output_shift_2; -}; - -struct PoolParams { - FusedActivationFunctionType activation; - PaddingType padding_type; - PaddingValues padding_values; - int stride_height; - int stride_width; - int filter_height; - int filter_width; - // uint8_t, etc, activation params. - int32_t quantized_activation_min; - int32_t quantized_activation_max; - // float activation params. - float float_activation_min; - float float_activation_max; -}; - -struct ReshapeParams { - int8_t shape_count; - int32_t shape[4]; -}; - -struct ResizeBilinearParams { - bool align_corners; - // half_pixel_centers assumes pixels are of half the actual dimensions, and - // yields more accurate resizes. Corresponds to the same argument for the - // original TensorFlow op in TF2.0. - bool half_pixel_centers; -}; - -struct ResizeNearestNeighborParams { - bool align_corners; - bool half_pixel_centers; -}; - -struct SliceParams { - int8_t begin_count; - int32_t begin[5]; - int8_t size_count; - int32_t size[5]; -}; - -struct SoftmaxParams { - // beta is not really used (not a Tensorflow parameter) and not implemented - // for LogSoftmax. - double beta; - // uint8_t inference params. Used even when beta defaults to 1.0. - int32_t input_multiplier; - int32_t input_left_shift; - // Reverse scaling is only used by LogSoftmax. - int32_t reverse_scaling_divisor; - int32_t reverse_scaling_right_shift; - int diff_min; - int32_t zero_point; - float scale; - float* table; - // int16 LUT for exp(x), where x uniform distributed between [-10.0 , 0.0] - int16_t* exp_lut; - // int16 LUT for 1 / (1 + x), where x uniform distributed between [0.0 , 1.0] - int16_t* one_over_one_plus_x_lut; - uint8_t* uint8_table1; - uint8_t* uint8_table2; -}; - -struct SpaceToBatchParams { - // "Zero" padding for uint8_t means padding with the output offset. - int32_t output_offset; -}; - -struct SpaceToDepthParams { - int32_t block_size; -}; - -struct SplitParams { - // Graphs that split into, say, 2000 nodes are encountered. The indices in - // OperatorEdges are of type uint16_t. - uint16_t num_split; - int16_t axis; -}; - -struct SqueezeParams { - int8_t squeeze_dims_count; - int32_t squeeze_dims[4]; -}; - -struct StridedSliceParams { - int8_t start_indices_count; - int32_t start_indices[5]; - int8_t stop_indices_count; - int32_t stop_indices[5]; - int8_t strides_count; - int32_t strides[5]; - - uint16_t begin_mask; - uint16_t ellipsis_mask; - uint16_t end_mask; - uint16_t new_axis_mask; - uint16_t shrink_axis_mask; -}; - -struct TanhParams { - int32_t input_zero_point; - int32_t input_range_radius; - int32_t input_multiplier; - int input_left_shift; -}; - -struct TransposeParams { - int8_t perm_count; - int32_t perm[5]; -}; - -struct UnpackParams { - uint16_t num_split; - int16_t axis; -}; - -struct LeakyReluParams { - float alpha; - int32_t input_offset; - int32_t output_offset; - int32_t output_multiplier_alpha; - int32_t output_shift_alpha; - int32_t output_multiplier_identity; - int32_t output_shift_identity; -}; - -template -inline void SetActivationParams(float min, float max, P* params) { - params->float_activation_min = min; - params->float_activation_max = max; -} - -template -inline void SetActivationParams(int32_t min, int32_t max, P* params) { - params->quantized_activation_min = min; - params->quantized_activation_max = max; -} - -template -inline void SetActivationParams(int64_t min, int64_t max, P* params) { - params->int64_activation_min = min; - params->int64_activation_max = max; -} - -template -inline void GetActivationParams(const P& params, int32_t* min, int32_t* max) { - *min = params.quantized_activation_min; - *max = params.quantized_activation_max; -} - -template -inline void GetActivationParams(const P& params, float* min, float* max) { - *min = params.float_activation_min; - *max = params.float_activation_max; -} - -template -inline void GetActivationParams(const P& params, int64_t* min, int64_t* max) { - *min = params.int64_activation_min; - *max = params.int64_activation_max; -} - -// Type trait to check of given type has size smaller than 4 bytes. -template -struct is_small_integer - : public std::integral_constant::value || - std::is_same::value || - std::is_same::value || - std::is_same::value> {}; - -// Type trait to check of given type is int32 or int64. -template -struct is_int32_or_int64 - : public std::integral_constant::value || - std::is_same::value> { -}; - -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_TYPES_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/kernel_util.cc b/code/components/tflite-lib/tensorflow/lite/kernels/kernel_util.cc deleted file mode 100644 index 10b37ed3..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/kernel_util.cc +++ /dev/null @@ -1,593 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/kernels/kernel_util.h" - -#include -#include - -#include -#include -#include -#include -#ifndef TF_LITE_STATIC_MEMORY -#include -#endif // TF_LITE_STATIC_MEMORY - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/context_util.h" -#include "tensorflow/lite/kernels/internal/cppmath.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" - -#if defined(__APPLE__) -#include "TargetConditionals.h" -#endif - -namespace tflite { - -namespace { - -// Assumes tensor_index is a valid index (in bounds) -inline TfLiteTensor* GetTensorAtIndex(const TfLiteContext* context, - int tensor_index) { - if (context->tensors != nullptr) { - return &context->tensors[tensor_index]; - } else { - return context->GetTensor(context, tensor_index); - } -} - -// Validate in a single place to reduce binary size -inline TfLiteStatus ValidateTensorIndexingSafe(const TfLiteContext* context, - int index, int max_size, - const int* tensor_indices, - int* tensor_index) { - if (index < 0 || index >= max_size) { - TF_LITE_KERNEL_LOG(const_cast(context), - "Invalid tensor index %d (not in [0, %d))\n", index, - max_size); - return kTfLiteError; - } - if (tensor_indices[index] == kTfLiteOptionalTensor) { - TF_LITE_KERNEL_LOG(const_cast(context), - "Tensor at index %d was optional but was expected\n", - index); - return kTfLiteError; - } - - *tensor_index = tensor_indices[index]; - return kTfLiteOk; -} - -// Same as above but returns -1 for invalid inputs instead of status + logging -// error. -inline int ValidateTensorIndexing(const TfLiteContext* context, int index, - int max_size, const int* tensor_indices) { - if (index >= 0 && index < max_size) { - const int tensor_index = tensor_indices[index]; - if (tensor_index != kTfLiteOptionalTensor) { - return tensor_index; - } - } - return -1; -} - -inline TfLiteTensor* GetMutableInput(const TfLiteContext* context, - const TfLiteNode* node, int index) { - const int tensor_index = ValidateTensorIndexing( - context, index, node->inputs->size, node->inputs->data); - if (tensor_index < 0) { - return nullptr; - } - return GetTensorAtIndex(context, tensor_index); -} - -inline TfLiteStatus GetMutableInputSafe(const TfLiteContext* context, - const TfLiteNode* node, int index, - const TfLiteTensor** tensor) { - int tensor_index; - TF_LITE_ENSURE_OK( - context, ValidateTensorIndexingSafe(context, index, node->inputs->size, - node->inputs->data, &tensor_index)); - *tensor = GetTensorAtIndex(context, tensor_index); - return kTfLiteOk; -} - -} // anonymous namespace. - -const TfLiteTensor* GetInput(const TfLiteContext* context, - const TfLiteNode* node, int index) { - return GetMutableInput(context, node, index); -} - -TfLiteStatus GetInputSafe(const TfLiteContext* context, const TfLiteNode* node, - int index, const TfLiteTensor** tensor) { - return GetMutableInputSafe(context, node, index, tensor); -} - -TfLiteTensor* GetVariableInput(TfLiteContext* context, const TfLiteNode* node, - int index) { - TfLiteTensor* tensor = GetMutableInput(context, node, index); - if (tensor == nullptr) return nullptr; - return tensor->is_variable ? tensor : nullptr; -} - -TfLiteTensor* GetOutput(TfLiteContext* context, const TfLiteNode* node, - int index) { - const int tensor_index = ValidateTensorIndexing( - context, index, node->outputs->size, node->outputs->data); - if (tensor_index < 0) { - return nullptr; - } - return GetTensorAtIndex(context, tensor_index); -} - -TfLiteStatus GetOutputSafe(const TfLiteContext* context, const TfLiteNode* node, - int index, TfLiteTensor** tensor) { - int tensor_index; - TF_LITE_ENSURE_OK( - context, ValidateTensorIndexingSafe(context, index, node->outputs->size, - node->outputs->data, &tensor_index)); - *tensor = GetTensorAtIndex(context, tensor_index); - return kTfLiteOk; -} - -const TfLiteTensor* GetOptionalInputTensor(const TfLiteContext* context, - const TfLiteNode* node, int index) { - return GetInput(context, node, index); -} - -#ifndef TF_LITE_STATIC_MEMORY -TfLiteTensor* GetTemporary(TfLiteContext* context, const TfLiteNode* node, - int index) { - const int tensor_index = ValidateTensorIndexing( - context, index, node->temporaries->size, node->temporaries->data); - if (tensor_index < 0) { - return nullptr; - } - return GetTensorAtIndex(context, tensor_index); -} - -TfLiteStatus GetTemporarySafe(const TfLiteContext* context, - const TfLiteNode* node, int index, - TfLiteTensor** tensor) { - int tensor_index; - TF_LITE_ENSURE_OK(context, ValidateTensorIndexingSafe( - context, index, node->temporaries->size, - node->temporaries->data, &tensor_index)); - *tensor = GetTensorAtIndex(context, tensor_index); - return kTfLiteOk; -} - -const TfLiteTensor* GetIntermediates(TfLiteContext* context, - const TfLiteNode* node, int index) { - const int tensor_index = ValidateTensorIndexing( - context, index, node->intermediates->size, node->intermediates->data); - if (tensor_index < 0) { - return nullptr; - } - return GetTensorAtIndex(context, tensor_index); -} - -TfLiteStatus GetIntermediatesSafe(const TfLiteContext* context, - const TfLiteNode* node, int index, - TfLiteTensor** tensor) { - int tensor_index; - TF_LITE_ENSURE_OK(context, ValidateTensorIndexingSafe( - context, index, node->intermediates->size, - node->intermediates->data, &tensor_index)); - *tensor = GetTensorAtIndex(context, tensor_index); - return kTfLiteOk; -} -#endif // TF_LITE_STATIC_MEMORY - -// Per-axis -TfLiteStatus PopulateConvolutionQuantizationParams( - TfLiteContext* context, const TfLiteTensor* input, - const TfLiteTensor* filter, const TfLiteTensor* bias, TfLiteTensor* output, - const TfLiteFusedActivation& activation, int32_t* multiplier, int* shift, - int32_t* output_activation_min, int32_t* output_activation_max, - int32_t* per_channel_multiplier, int32_t* per_channel_shift) { - const auto* affine_quantization = - reinterpret_cast(filter->quantization.params); - return PopulateConvolutionQuantizationParams( - context, input, filter, bias, output, activation, multiplier, shift, - output_activation_min, output_activation_max, per_channel_multiplier, - per_channel_shift, affine_quantization->scale->size); -} - -// Per-axis & per-tensor -TfLiteStatus PopulateConvolutionQuantizationParams( - TfLiteContext* context, const TfLiteTensor* input, - const TfLiteTensor* filter, const TfLiteTensor* bias, TfLiteTensor* output, - const TfLiteFusedActivation& activation, int32_t* multiplier, int* shift, - int32_t* output_activation_min, int32_t* output_activation_max, - int32_t* per_channel_multiplier, int32_t* per_channel_shift, - int num_channels) { - TF_LITE_ENSURE_EQ(context, input->quantization.type, - kTfLiteAffineQuantization); - TF_LITE_ENSURE_EQ(context, filter->quantization.type, - kTfLiteAffineQuantization); - // TODO(jianlijianli): Enable bias type check and bias scale == input scale - // * filter scale for each channel in affine quantization once bias - // quantization is properly populated. - // TF_LITE_ENSURE_EQ(context, bias->quantization.type, - // kTfLiteAffineQuantization); - - // Check data type. - const auto* affine_quantization = - reinterpret_cast(filter->quantization.params); - TF_LITE_ENSURE(context, affine_quantization); - TF_LITE_ENSURE(context, affine_quantization->scale); - const bool is_per_channel = affine_quantization->scale->size > 1; - if (is_per_channel) { - // Currently only Int8/Int16 is supported for per channel quantization. - TF_LITE_ENSURE(context, - input->type == kTfLiteInt8 || input->type == kTfLiteInt16); - TF_LITE_ENSURE_EQ(context, filter->type, kTfLiteInt8); - TF_LITE_ENSURE_EQ(context, affine_quantization->scale->size, num_channels); - TF_LITE_ENSURE_EQ( - context, num_channels, - filter->dims->data[affine_quantization->quantized_dimension]); - } - - // Populate multiplier and shift using affine quantization. - const float input_scale = input->params.scale; - const float output_scale = output->params.scale; - const float* filter_scales = affine_quantization->scale->data; - for (int i = 0; i < num_channels; ++i) { - // If per-tensor quantization parameter is specified, broadcast it along the - // quantization dimension (channels_out). - const float scale = is_per_channel ? filter_scales[i] : filter_scales[0]; - const double filter_scale = static_cast(scale); - const double effective_output_scale = static_cast(input_scale) * - filter_scale / - static_cast(output_scale); - int32_t significand; - int channel_shift; - QuantizeMultiplier(effective_output_scale, &significand, &channel_shift); - per_channel_multiplier[i] = significand; - per_channel_shift[i] = channel_shift; - } - - // Populate scalar quantization parameters. - // This check on legacy quantization parameters is kept only for backward - // compatibility. - if (input->type == kTfLiteUInt8) { - // Check bias scale == input scale * filter scale. - double real_multiplier = 0.0; - TF_LITE_ENSURE_STATUS(GetQuantizedConvolutionMultipler( - context, input, filter, bias, output, &real_multiplier)); - int exponent; - - // Populate quantization parameters with multiplier and shift. - QuantizeMultiplier(real_multiplier, multiplier, &exponent); - *shift = -exponent; - } - if (input->type == kTfLiteInt8 || input->type == kTfLiteUInt8 || - input->type == kTfLiteInt16) { - TF_LITE_ENSURE_STATUS(CalculateActivationRangeQuantized( - context, activation, output, output_activation_min, - output_activation_max)); - } - return kTfLiteOk; -} - -TfLiteStatus GetQuantizedConvolutionMultipler(TfLiteContext* context, - const TfLiteTensor* input, - const TfLiteTensor* filter, - const TfLiteTensor* bias, - TfLiteTensor* output, - double* multiplier) { - const double input_product_scale = static_cast(input->params.scale) * - static_cast(filter->params.scale); - // The following conditions must be guaranteed by the training pipeline. - if (bias) { - const double bias_scale = static_cast(bias->params.scale); - // Here we're making sure the input_product_scale & bias_scale are about the - // same. Since we have: - // (output - output_zp) * output_scale = - // input_product_scale * input_product + bias * bias_scale ---- (0) - // - // (0) equals: - // (input_product + bias) * input_product_scale ----- (1) - // + - // bias * (bias_scale - input_product_scale) ------ (2) - // - // For the real kernel computation, we're doing (1), so we really need to - // make sure (2) has minimum impact on the output, so: - // bias * (bias_scale - input_product_scale) / output_scale should be - // a small number for an integer. - // Since normally bias should be within a small range. - // We should expect (bias_scale - input_product_scale) / output_scale to - // be a small number like 0.02. - const double scale_diff = std::abs(input_product_scale - bias_scale); - const double output_scale = static_cast(output->params.scale); - - TF_LITE_ENSURE(context, scale_diff / output_scale <= 0.02); - } - return GetQuantizedConvolutionMultipler(context, input, filter, output, - multiplier); -} - -TfLiteStatus GetQuantizedConvolutionMultipler(TfLiteContext* context, - const TfLiteTensor* input, - const TfLiteTensor* filter, - TfLiteTensor* output, - double* multiplier) { - const double input_product_scale = - static_cast(input->params.scale * filter->params.scale); - TF_LITE_ENSURE(context, input_product_scale >= 0); - *multiplier = input_product_scale / static_cast(output->params.scale); - - return kTfLiteOk; -} - -namespace { - -inline TfLiteStatus Quantize(TfLiteContext* context, float scale, - int32_t zero_point, float f, int32_t& q) { - const float tmp = TfLiteRound(f / scale); - const bool no_integer_overflow_from_quantization = - (tmp >= static_cast(std::numeric_limits::min()) && - tmp <= static_cast(std::numeric_limits::max())); - TF_LITE_ENSURE(context, no_integer_overflow_from_quantization); - q = zero_point + static_cast(tmp); - return kTfLiteOk; -} - -TfLiteStatus CalculateActivationRangeQuantizedImpl( - TfLiteContext* context, TfLiteFusedActivation activation, int32_t qmin, - int32_t qmax, TfLiteTensor* output, int32_t* act_min, int32_t* act_max) { - const auto scale = output->params.scale; - const auto zero_point = output->params.zero_point; - - int32_t tmp_q; - if (activation == kTfLiteActRelu) { - TF_LITE_ENSURE_OK(context, - Quantize(context, scale, zero_point, 0.0, tmp_q)); - *act_min = std::max(qmin, tmp_q); - *act_max = qmax; - } else if (activation == kTfLiteActRelu6) { - TF_LITE_ENSURE_OK(context, - Quantize(context, scale, zero_point, 0.0, tmp_q)); - *act_min = std::max(qmin, tmp_q); - TF_LITE_ENSURE_OK(context, - Quantize(context, scale, zero_point, 6.0, tmp_q)); - *act_max = std::min(qmax, tmp_q); - } else if (activation == kTfLiteActReluN1To1) { - TF_LITE_ENSURE_OK(context, - Quantize(context, scale, zero_point, -1.0, tmp_q)); - *act_min = std::max(qmin, tmp_q); - TF_LITE_ENSURE_OK(context, - Quantize(context, scale, zero_point, 1.0, tmp_q)); - *act_max = std::min(qmax, tmp_q); - } else { - *act_min = qmin; - *act_max = qmax; - } - return kTfLiteOk; -} -} // namespace - -TfLiteStatus CalculateActivationRangeQuantized(TfLiteContext* context, - TfLiteFusedActivation activation, - TfLiteTensor* output, - int32_t* act_min, - int32_t* act_max) { - int32_t qmin = 0; - int32_t qmax = 0; - if (output->type == kTfLiteUInt8) { - qmin = std::numeric_limits::min(); - qmax = std::numeric_limits::max(); - } else if (output->type == kTfLiteInt8) { - qmin = std::numeric_limits::min(); - qmax = std::numeric_limits::max(); - } else if (output->type == kTfLiteInt16) { - qmin = std::numeric_limits::min(); - qmax = std::numeric_limits::max(); - } else { - TF_LITE_ENSURE(context, false); - } - - return CalculateActivationRangeQuantizedImpl(context, activation, qmin, qmax, - output, act_min, act_max); -} - -bool HaveSameShapes(const TfLiteTensor* input1, const TfLiteTensor* input2) { - return TfLiteIntArrayEqual(input1->dims, input2->dims); -} - -#ifndef TF_LITE_STATIC_MEMORY -TfLiteStatus GetOutputShapeFromInput(TfLiteContext* context, - const TfLiteTensor* input, - TfLiteIntArray** output_shape) { - if (NumDimensions(input) != 1) { - TF_LITE_KERNEL_LOG(const_cast(context), - "Invalid %dD input tensor (must be a 1D tensor).", - NumDimensions(input)); - return kTfLiteError; - } - const int output_dims = SizeOfDimension(input, 0); - std::unique_ptr shape( - TfLiteIntArrayCreate(output_dims), TfLiteIntArrayFree); - for (int i = 0; i < output_dims; i++) { - shape->data[i] = input->data.i32[i]; - } - *output_shape = shape.release(); - return kTfLiteOk; -} - -// TODO(b/172067338): Having this function be part of TF_LITE_STATIC_MEMORY -// build results in a 6KB size increase, even though the function is unsused for -// that build. What appears to be happening is that while the linker drops the -// unsused function, the string library that gets pulled in is not dropped, -// resulting in the increased binary size. -const std::string GetShapeDebugString(const TfLiteIntArray* shape) { - std::string str; - for (int d = 0; d < shape->size; ++d) { - if (str.empty()) - str = "[" + std::to_string(shape->data[d]); - else - // Don't add space after "," to make the output consistent with - // tensorflow::shape_inference::InferenceContext::DebugString() - str += "," + std::to_string(shape->data[d]); - } - if (str.empty()) { - str = "[]"; - } else { - str += "]"; - } - return str; -} - -TfLiteStatus CalculateShapeForBroadcast(TfLiteContext* context, - const TfLiteTensor* input1, - const TfLiteTensor* input2, - TfLiteIntArray** output_shape) { - const int dims1 = NumDimensions(input1); - const int dims2 = NumDimensions(input2); - const int out_dims = std::max(dims1, dims2); - - std::unique_ptr shape( - TfLiteIntArrayCreate(out_dims), TfLiteIntArrayFree); - for (int i = 0; i < out_dims; ++i) { - const int d1 = i >= dims1 ? 1 : SizeOfDimension(input1, dims1 - i - 1); - const int d2 = i >= dims2 ? 1 : SizeOfDimension(input2, dims2 - i - 1); - if (!(d1 == d2 || d1 == 1 || d2 == 1)) { - TF_LITE_KERNEL_LOG(context, - "Given shapes, %s and %s, are not broadcastable.", - GetShapeDebugString(input1->dims).c_str(), - GetShapeDebugString(input2->dims).c_str()); - return kTfLiteError; - } - - if (d1 == 0 || d2 == 0) { - shape->data[out_dims - i - 1] = 0; - } else { - shape->data[out_dims - i - 1] = std::max(d1, d2); - } - } - *output_shape = shape.release(); - return kTfLiteOk; -} - -TfLiteStatus CalculateShapeForBroadcast(TfLiteContext* context, - const TfLiteTensor* input1, - const TfLiteTensor* input2, - const TfLiteTensor* input3, - TfLiteIntArray** output_shape) { - const int dims1 = NumDimensions(input1); - const int dims2 = NumDimensions(input2); - const int dims3 = NumDimensions(input3); - const int out_dims = std::max(std::max(dims1, dims2), dims3); - std::unique_ptr shape( - TfLiteIntArrayCreate(out_dims), TfLiteIntArrayFree); - for (int i = 0; i < out_dims; ++i) { - const int d1 = i >= dims1 ? 1 : SizeOfDimension(input1, dims1 - i - 1); - const int d2 = i >= dims2 ? 1 : SizeOfDimension(input2, dims2 - i - 1); - const int d3 = i >= dims3 ? 1 : SizeOfDimension(input3, dims3 - i - 1); - const int min_value = std::min(std::min(d1, d2), d3); - int max_value = std::max(std::max(d1, d2), d3); - // If one dimention is 0, others must be 0 or 1. - if (min_value == 0) max_value = 0; - if (!(d1 == 1 || d1 == max_value) || !(d2 == 1 || d2 == max_value) || - !(d3 == 1 || d3 == max_value)) { - TF_LITE_KERNEL_LOG(context, - "Given shapes, %s, %s and %s, are not broadcastable.", - GetShapeDebugString(input1->dims).c_str(), - GetShapeDebugString(input2->dims).c_str(), - GetShapeDebugString(input3->dims).c_str()); - return kTfLiteError; - } - shape->data[out_dims - i - 1] = max_value; - } - *output_shape = shape.release(); - return kTfLiteOk; -} -#endif // TF_LITE_STATIC_MEMORY - -// Size of string is not constant, return 0 in such case. -int TfLiteTypeGetSize(TfLiteType type) { - switch (type) { - case kTfLiteUInt8: - static_assert(sizeof(uint8_t) == 1, ""); - return 1; - case kTfLiteInt8: - static_assert(sizeof(int8_t) == 1, ""); - return 1; - case kTfLiteBool: - return sizeof(bool); - case kTfLiteUInt16: - static_assert(sizeof(uint16_t) == 2, ""); - return 2; - case kTfLiteInt16: - static_assert(sizeof(int16_t) == 2, ""); - return 2; - case kTfLiteFloat16: - static_assert(sizeof(int16_t) == 2, ""); - return 2; - case kTfLiteFloat32: - static_assert(sizeof(float) == 4, ""); - return 4; - case kTfLiteInt32: - static_assert(sizeof(int32_t) == 4, ""); - return 4; - case kTfLiteUInt32: - static_assert(sizeof(uint32_t) == 4, ""); - return 4; - case kTfLiteInt64: - static_assert(sizeof(int64_t) == 8, ""); - return 8; - case kTfLiteUInt64: - static_assert(sizeof(uint64_t) == 8, ""); - return 8; - case kTfLiteFloat64: - static_assert(sizeof(double) == 8, ""); - return 8; - case kTfLiteComplex64: - static_assert(sizeof(std::complex) == 8, ""); - return 8; - case kTfLiteComplex128: - static_assert(sizeof(std::complex) == 16, ""); - return 16; - default: - return 0; - } -} - -bool IsMobilePlatform() { -#if defined(ANDROID) || defined(__ANDROID__) - return true; -#elif defined(__APPLE__) -#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE - return true; -#endif -#endif - return false; -} - -bool HasUnspecifiedDimension(const TfLiteTensor* tensor) { -#ifndef TF_LITE_STATIC_MEMORY - if (tensor->dims_signature) { - for (int i : TfLiteIntArrayView(tensor->dims_signature)) { - if (i == -1) return true; - } - } -#endif // TF_LITE_STATIC_MEMORY - return false; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/kernel_util.h b/code/components/tflite-lib/tensorflow/lite/kernels/kernel_util.h deleted file mode 100644 index 06874422..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/kernel_util.h +++ /dev/null @@ -1,330 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_KERNEL_UTIL_H_ -#define TENSORFLOW_LITE_KERNELS_KERNEL_UTIL_H_ - -#include - -#include -#ifndef TF_LITE_STATIC_MEMORY -#include -#endif // TF_LITE_STATIC_MEMORY - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" - -namespace tflite { - -// A fair number of functions in this header have historically been inline. -// It is ok to change functions to not be inline if the latency with -// benchmark_model for MobileNet + MobileBERT is unaffected. If such a change is -// made, move the newly non-inlined function declarations to the top of this -// header file. - -// Note: You must check if result is not null: -// -// TfLiteTensor* my_tensor = GetInput(context, node, kMyTensorIdx); -// TF_LITE_ENSURE(context, my_tensor != nullptr); -// -// This is because the index might point to the optional tensor constant -// (kTfLiteOptionalTensor) in which case there is no tensor to return. -const TfLiteTensor* GetInput(const TfLiteContext* context, - const TfLiteNode* node, int index); - -// Same as `GetInput` but returns boolean and uses output argument for tensor. -// -// TfLiteTensor* my_tensor; -// TF_LITE_ENSURE_OK(context, -// GetInputSafe(context, node, kMyTensorIdx, &my_tensor)); -// // can use my_tensor directly from here onwards, it is not nullptr -// -// Should be used in cases where the binary size is too large. -TfLiteStatus GetInputSafe(const TfLiteContext* context, const TfLiteNode* node, - int index, const TfLiteTensor** tensor); - -// Note: You must check if result is not null: -// -// TfLiteTensor* my_tensor = GetVariableInput(context, node, kMyTensorIdx); -// TF_LITE_ENSURE(context, my_tensor != nullptr); -// -// This is because the index might point to the optional tensor constant -// (kTfLiteOptionalTensor) in which case there is no tensor to return. -TfLiteTensor* GetVariableInput(TfLiteContext* context, const TfLiteNode* node, - int index); - -// Note: You must check if result is not null: -// -// TfLiteTensor* my_tensor = GetOutput(context, node, kMyTensorIdx); -// TF_LITE_ENSURE(context, my_tensor != nullptr); -// -// This is because the index might point to the optional tensor constant -// (kTfLiteOptionalTensor) in which case there is no tensor to return. -TfLiteTensor* GetOutput(TfLiteContext* context, const TfLiteNode* node, - int index); - -// Same as `GetOutput` but returns boolean and uses output argument for tensor. -// -// TfLiteTensor* my_tensor; -// TF_LITE_ENSURE_OK(context, -// GetOutputSafe(context, node, kMyTensorIdx, &my_tensor)); -// // can use my_tensor directly from here onwards, it is not nullptr -// -// Should be used in cases where the binary size is too large. -TfLiteStatus GetOutputSafe(const TfLiteContext* context, const TfLiteNode* node, - int index, TfLiteTensor** tensor); - -// Note: You must check if result is not null: -// -// TfLiteTensor* my_tensor = GetOptionalInputTensor(context, node, kIdx); -// TF_LITE_ENSURE(context, my_tensor != nullptr); -// -// This is because the index might point to the optional tensor constant -// (kTfLiteOptionalTensor) in which case there is no tensor to return. -// -// Deprecated. GetInput has the same functionality. -const TfLiteTensor* GetOptionalInputTensor(const TfLiteContext* context, - const TfLiteNode* node, int index); - -#ifndef TF_LITE_STATIC_MEMORY -// Note: You must check if result is not null: -// -// TfLiteTensor* my_tensor = GetTemporary(context, node, kMyTensorIdx); -// TF_LITE_ENSURE(context, my_tensor != nullptr); -// -// This is because the index might point to the optional tensor constant -// (kTfLiteOptionalTensor) in which case there is no tensor to return. -TfLiteTensor* GetTemporary(TfLiteContext* context, const TfLiteNode* node, - int index); - -// Same as `GetTemporary` but returns boolean and uses output argument for -// tensor. -// -// TfLiteTensor* my_tensor; -// TF_LITE_ENSURE_OK(context, -// GetTemporarySafe(context, node, kMyTensorIdx, -// &my_tensor)); -// // can use my_tensor directly from here onwards, it is not nullptr -// -// Should be used in cases where the binary size is too large. -TfLiteStatus GetTemporarySafe(const TfLiteContext* context, - const TfLiteNode* node, int index, - TfLiteTensor** tensor); - -// Note: You must check if result is not null: -// -// TfLiteTensor* my_tensor = GetIntermediates(context, node, kMyTensorIdx); -// TF_LITE_ENSURE(context, my_tensor != nullptr); -// -// This is because the index might point to the optional tensor constant -// (kTfLiteOptionalTensor) in which case there is no tensor to return. -const TfLiteTensor* GetIntermediates(TfLiteContext* context, - const TfLiteNode* node, int index); - -// Same as `GetIntermediates` but returns boolean and uses output argument for -// tensor. -// -// TfLiteTensor* my_tensor; -// TF_LITE_ENSURE_OK(context, -// GetIntermediatesSafe(context, node, kMyTensorIdx, -// &my_tensor)); -// // can use my_tensor directly from here onwards, it is not nullptr -// -// Should be used in cases where the binary size is too large. -TfLiteStatus GetIntermediatesSafe(const TfLiteContext* context, - const TfLiteNode* node, int index, - TfLiteTensor** tensor); -#endif // TF_LITE_STATIC_MEMORY - -inline int NumDimensions(const TfLiteTensor* t) { return t->dims->size; } -inline int SizeOfDimension(const TfLiteTensor* t, int dim) { - return t->dims->data[dim]; -} - -inline int NumInputs(const TfLiteNode* node) { - return node->inputs == nullptr ? 0 : node->inputs->size; -} -inline int NumOutputs(const TfLiteNode* node) { - return node->outputs == nullptr ? 0 : node->outputs->size; -} - -#ifndef TF_LITE_STATIC_MEMORY -inline int NumIntermediates(const TfLiteNode* node) { - return node->intermediates->size; -} -#endif // TF_LITE_STATIC_MEMORY - -inline int64_t NumElements(const TfLiteIntArray* dims) { - int64_t count = 1; - for (int i = 0; i < dims->size; ++i) { - count *= dims->data[i]; - } - return count; -} - -inline int64_t NumElements(const TfLiteTensor* t) { - return NumElements(t->dims); -} - -inline int64_t NumElements(const int* dims, int num_dims) { - int64_t count = 1; - for (int i = 0; i < num_dims; ++i) { - count *= dims[i]; - } - return count; -} - -// Determines whether tensor is constant. -// TODO(b/138199592): Introduce new query which checks for constant OR -// persistent-read-only, which would be useful for most tensor kernels that -// are potentially dynamic based on the input tensor value availability at the -// time of prepare. -inline bool IsConstantTensor(const TfLiteTensor* tensor) { - return tensor->allocation_type == kTfLiteMmapRo; -} - -inline bool IsConstantOrPersistentTensor(const TfLiteTensor* tensor) { - return IsConstantTensor(tensor) || - (tensor->allocation_type == kTfLitePersistentRo); -} - -// Determines whether tensor is dynamic. Note that a tensor can be non-const and -// not dynamic. This function specifically checks for a dynamic tensor. -inline bool IsDynamicTensor(const TfLiteTensor* tensor) { - return tensor->allocation_type == kTfLiteDynamic; -} - -// Sets tensor to dynamic. -inline void SetTensorToDynamic(TfLiteTensor* tensor) { - if (tensor->allocation_type != kTfLiteDynamic) { - tensor->allocation_type = kTfLiteDynamic; - tensor->data.raw = nullptr; - } -} - -// Sets tensor to persistent and read-only. -inline void SetTensorToPersistentRo(TfLiteTensor* tensor) { - if (tensor->allocation_type != kTfLitePersistentRo) { - tensor->allocation_type = kTfLitePersistentRo; - tensor->data.raw = nullptr; - } -} - -// Determines whether it is a hybrid op - one that has float inputs and -// quantized weights. -inline bool IsHybridOp(const TfLiteTensor* input, const TfLiteTensor* weight) { - return ((weight->type == kTfLiteUInt8 || weight->type == kTfLiteInt8) && - input->type == kTfLiteFloat32); -} - -// Check dimensionality match and populate OpData for Conv and DepthwiseConv. -TfLiteStatus PopulateConvolutionQuantizationParams( - TfLiteContext* context, const TfLiteTensor* input, - const TfLiteTensor* filter, const TfLiteTensor* bias, TfLiteTensor* output, - const TfLiteFusedActivation& activation, int32_t* multiplier, int* shift, - int32_t* output_activation_min, int32_t* output_activation_max, - int32_t* per_channel_multiplier, int32_t* per_channel_shift); - -TfLiteStatus PopulateConvolutionQuantizationParams( - TfLiteContext* context, const TfLiteTensor* input, - const TfLiteTensor* filter, const TfLiteTensor* bias, TfLiteTensor* output, - const TfLiteFusedActivation& activation, int32_t* multiplier, int* shift, - int32_t* output_activation_min, int32_t* output_activation_max, - int32_t* per_channel_multiplier, int32_t* per_channel_shift, - int num_channels); - -// Calculates the multiplication factor for a quantized convolution (or -// quantized depthwise convolution) involving the given tensors. Returns an -// error if the scales of the tensors are not compatible. -TfLiteStatus GetQuantizedConvolutionMultipler(TfLiteContext* context, - const TfLiteTensor* input, - const TfLiteTensor* filter, - const TfLiteTensor* bias, - TfLiteTensor* output, - double* multiplier); - -TfLiteStatus GetQuantizedConvolutionMultipler(TfLiteContext* context, - const TfLiteTensor* input, - const TfLiteTensor* filter, - TfLiteTensor* output, - double* multiplier); - -// Calculates the useful quantized range of an activation layer given its -// activation tensor. -TfLiteStatus CalculateActivationRangeQuantized(TfLiteContext* context, - TfLiteFusedActivation activation, - TfLiteTensor* output, - int32_t* act_min, - int32_t* act_max); - -// Calculates the useful range of an activation layer given its activation -// tensor.a -template -void CalculateActivationRange(TfLiteFusedActivation activation, - T* activation_min, T* activation_max) { - if (activation == kTfLiteActRelu) { - *activation_min = 0; - *activation_max = std::numeric_limits::max(); - } else if (activation == kTfLiteActRelu6) { - *activation_min = 0; - *activation_max = 6; - } else if (activation == kTfLiteActReluN1To1) { - *activation_min = -1; - *activation_max = 1; - } else { - *activation_min = std::numeric_limits::lowest(); - *activation_max = std::numeric_limits::max(); - } -} - -// Return true if the given tensors have the same shape. -bool HaveSameShapes(const TfLiteTensor* input1, const TfLiteTensor* input2); - -#if !defined(TF_LITE_STATIC_MEMORY) -// Gets the output shape from the input tensor. -TfLiteStatus GetOutputShapeFromInput(TfLiteContext* context, - const TfLiteTensor* input, - TfLiteIntArray** output_shape); - -const std::string GetShapeDebugString(const TfLiteIntArray* shape); - -#endif // !defined(TF_LITE_STATIC_MEMORY) - -// Calculates the output_shape that is necessary for element-wise operations -// with broadcasting involving the two input tensors. -TfLiteStatus CalculateShapeForBroadcast(TfLiteContext* context, - const TfLiteTensor* input1, - const TfLiteTensor* input2, - TfLiteIntArray** output_shape); - -// Calculates the output_shape that is necessary for element-wise operations -// with broadcasting involving the three input tensors. -TfLiteStatus CalculateShapeForBroadcast(TfLiteContext* context, - const TfLiteTensor* input1, - const TfLiteTensor* input2, - const TfLiteTensor* input3, - TfLiteIntArray** output_shape); - -// Return the size of given type in bytes. Return 0 in case of string. -int TfLiteTypeGetSize(TfLiteType type); - -// Whether the current platform is mobile (Android or iOS). -bool IsMobilePlatform(); - -// Returns whether there is unspecified dimension in the tensor's dim signature. -bool HasUnspecifiedDimension(const TfLiteTensor* tensor); - -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_KERNEL_UTIL_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/op_macros.h b/code/components/tflite-lib/tensorflow/lite/kernels/op_macros.h deleted file mode 100644 index 4255d253..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/op_macros.h +++ /dev/null @@ -1,38 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_OP_MACROS_H_ -#define TENSORFLOW_LITE_KERNELS_OP_MACROS_H_ - -#include "tensorflow/lite/micro/debug_log.h" - -#if !defined(TF_LITE_MCU_DEBUG_LOG) -#include -#define TFLITE_ABORT abort() -#else -inline void AbortImpl() { - DebugLog("HALTED\n"); - while (1) { - } -} -#define TFLITE_ABORT AbortImpl(); -#endif - -#if defined(NDEBUG) -#define TFLITE_ASSERT_FALSE (static_cast(0)) -#else -#define TFLITE_ASSERT_FALSE TFLITE_ABORT -#endif - -#endif // TENSORFLOW_LITE_KERNELS_OP_MACROS_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/kernels/padding.h b/code/components/tflite-lib/tensorflow/lite/kernels/padding.h deleted file mode 100644 index d9cca3ea..00000000 --- a/code/components/tflite-lib/tensorflow/lite/kernels/padding.h +++ /dev/null @@ -1,115 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_KERNELS_PADDING_H_ -#define TENSORFLOW_LITE_KERNELS_PADDING_H_ - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -inline int ComputePadding(int stride, int dilation_rate, int in_size, - int filter_size, int out_size) { - int effective_filter_size = (filter_size - 1) * dilation_rate + 1; - int padding = ((out_size - 1) * stride + effective_filter_size - in_size) / 2; - return padding > 0 ? padding : 0; -} - -// It's not guaranteed that padding is symmetric. It's important to keep -// offset for algorithms need all paddings. -inline int ComputePaddingWithOffset(int stride, int dilation_rate, int in_size, - int filter_size, int out_size, - int* offset) { - int effective_filter_size = (filter_size - 1) * dilation_rate + 1; - int total_padding = - ((out_size - 1) * stride + effective_filter_size - in_size); - total_padding = total_padding > 0 ? total_padding : 0; - *offset = total_padding % 2; - return total_padding / 2; -} - -// Matching GetWindowedOutputSize in TensorFlow. -inline int ComputeOutSize(TfLitePadding padding, int image_size, - int filter_size, int stride, int dilation_rate = 1) { - int effective_filter_size = (filter_size - 1) * dilation_rate + 1; - - // TODO(b/186448822): This uses 0 since the function has no other way to - // report error case - if (stride == 0) return 0; - - switch (padding) { - case kTfLitePaddingSame: - return (image_size + stride - 1) / stride; - case kTfLitePaddingValid: - return (image_size + stride - effective_filter_size) / stride; - default: - return 0; - } -} - -inline TfLitePaddingValues ComputePaddingHeightWidth( - int stride_height, int stride_width, int dilation_rate_height, - int dilation_rate_width, int in_height, int in_width, int filter_height, - int filter_width, TfLitePadding padding, int* out_height, int* out_width) { - *out_width = ComputeOutSize(padding, in_width, filter_width, stride_width, - dilation_rate_width); - *out_height = ComputeOutSize(padding, in_height, filter_height, stride_height, - dilation_rate_height); - - TfLitePaddingValues padding_values; - int offset = 0; - padding_values.height = - ComputePaddingWithOffset(stride_height, dilation_rate_height, in_height, - filter_height, *out_height, &offset); - padding_values.height_offset = offset; - padding_values.width = - ComputePaddingWithOffset(stride_width, dilation_rate_width, in_width, - filter_width, *out_width, &offset); - padding_values.width_offset = offset; - return padding_values; -} - -inline Padding3DValues ComputePadding3DValues( - int stride_height, int stride_width, int stride_depth, - int dilation_rate_height, int dilation_rate_width, int dilation_rate_depth, - int in_height, int in_width, int in_depth, int filter_height, - int filter_width, int filter_depth, TfLitePadding padding, int* out_height, - int* out_width, int* out_depth) { - *out_width = ComputeOutSize(padding, in_width, filter_width, stride_width, - dilation_rate_width); - *out_height = ComputeOutSize(padding, in_height, filter_height, stride_height, - dilation_rate_height); - *out_depth = ComputeOutSize(padding, in_depth, filter_depth, stride_depth, - dilation_rate_depth); - - Padding3DValues padding_values; - int offset = 0; - padding_values.depth = - ComputePaddingWithOffset(stride_depth, dilation_rate_depth, in_depth, - filter_depth, *out_depth, &offset); - padding_values.depth_offset = offset; - padding_values.height = - ComputePaddingWithOffset(stride_height, dilation_rate_height, in_height, - filter_height, *out_height, &offset); - padding_values.height_offset = offset; - padding_values.width = - ComputePaddingWithOffset(stride_width, dilation_rate_width, in_width, - filter_width, *out_width, &offset); - padding_values.width_offset = offset; - return padding_values; -} -} // namespace tflite - -#endif // TENSORFLOW_LITE_KERNELS_PADDING_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/all_ops_resolver.cc b/code/components/tflite-lib/tensorflow/lite/micro/all_ops_resolver.cc deleted file mode 100644 index abbe34e7..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/all_ops_resolver.cc +++ /dev/null @@ -1,119 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/all_ops_resolver.h" - -#include "tensorflow/lite/micro/kernels/micro_ops.h" - -namespace tflite { - -AllOpsResolver::AllOpsResolver() { - // Please keep this list of Builtin Operators in alphabetical order. - AddAbs(); - AddAdd(); - AddAddN(); - AddArgMax(); - AddArgMin(); - AddAssignVariable(); - AddAveragePool2D(); - AddBatchToSpaceNd(); - AddBroadcastArgs(); - AddBroadcastTo(); - AddCallOnce(); - AddCast(); - AddCeil(); - AddCircularBuffer(); - AddConcatenation(); - AddConv2D(); - AddCos(); - AddCumSum(); - AddDepthToSpace(); - AddDepthwiseConv2D(); - AddDequantize(); - AddDetectionPostprocess(); - AddDiv(); - AddElu(); - AddEqual(); - AddEthosU(); - AddExp(); - AddExpandDims(); - AddFill(); - AddFloor(); - AddFloorDiv(); - AddFloorMod(); - AddFullyConnected(); - AddGather(); - AddGatherNd(); - AddGreater(); - AddGreaterEqual(); - AddHardSwish(); - AddIf(); - AddL2Normalization(); - AddL2Pool2D(); - AddLeakyRelu(); - AddLess(); - AddLessEqual(); - AddLog(); - AddLogicalAnd(); - AddLogicalNot(); - AddLogicalOr(); - AddLogistic(); - AddMaxPool2D(); - AddMaximum(); - AddMean(); - AddMinimum(); - AddMirrorPad(); - AddMul(); - AddNeg(); - AddNotEqual(); - AddPack(); - AddPad(); - AddPadV2(); - AddPrelu(); - AddQuantize(); - AddReadVariable(); - AddReduceMax(); - AddRelu(); - AddRelu6(); - AddReshape(); - AddResizeBilinear(); - AddResizeNearestNeighbor(); - AddRound(); - AddRsqrt(); - AddShape(); - AddSin(); - AddSlice(); - AddSoftmax(); - AddSpaceToBatchNd(); - AddSpaceToDepth(); - AddSplit(); - AddSplitV(); - AddSqrt(); - AddSquare(); - AddSqueeze(); - AddStridedSlice(); - AddSub(); - AddSum(); - AddSvdf(); - AddTanh(); - AddTranspose(); - AddTransposeConv(); - AddUnpack(); - AddVarHandle(); - AddWhile(); - AddZerosLike(); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/all_ops_resolver.h b/code/components/tflite-lib/tensorflow/lite/micro/all_ops_resolver.h deleted file mode 100644 index 391b4f08..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/all_ops_resolver.h +++ /dev/null @@ -1,38 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_MICRO_ALL_OPS_RESOLVER_H_ -#define TENSORFLOW_LITE_MICRO_ALL_OPS_RESOLVER_H_ - -#include "tensorflow/lite/micro/compatibility.h" -#include "tensorflow/lite/micro/micro_mutable_op_resolver.h" - -namespace tflite { - -// The magic number in the template parameter is the maximum number of ops that -// can be added to AllOpsResolver. It can be increased if needed. And most -// applications that care about the memory footprint will want to directly use -// MicroMutableOpResolver and have an application specific template parameter. -// The examples directory has sample code for this. -class AllOpsResolver : public MicroMutableOpResolver<128> { - public: - AllOpsResolver(); - - private: - TF_LITE_REMOVE_VIRTUAL_DELETE -}; - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_ALL_OPS_RESOLVER_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/arena_allocator/ibuffer_allocator.h b/code/components/tflite-lib/tensorflow/lite/micro/arena_allocator/ibuffer_allocator.h deleted file mode 100644 index b92d6b2d..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/arena_allocator/ibuffer_allocator.h +++ /dev/null @@ -1,100 +0,0 @@ -/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_MICRO_ARENA_ALLOCATOR_IBUFFER_ALLOCATOR_H_ -#define TENSORFLOW_LITE_MICRO_ARENA_ALLOCATOR_IBUFFER_ALLOCATOR_H_ - -#include -#include - -#include "tensorflow/lite/c/c_api_types.h" - -namespace tflite { -// Interface classes that the TFLM framework relies on to get buffers it needs. -// There are two types of buffers that the TFLM framework requires: persistent -// and non-persistent. Persistent buffers, once allocated, are never freed by -// the TFLM framework. Non-persist buffers can be allocated and deallocated by -// the TFLM framework. This file defines two interfaces classes that TFLM -// framework will rely on to manage these buffers. - -// Interface class for managing persistent buffers. -class IPersistentBufferAllocator { - public: - IPersistentBufferAllocator() {} - virtual ~IPersistentBufferAllocator() {} - - // Allocates persistent memory. The persistent buffer is never freed. - virtual uint8_t* AllocatePersistentBuffer(size_t size, size_t alignment) = 0; - - // Returns the size of all persistent allocations in bytes. - virtual size_t GetPersistentUsedBytes() const = 0; -}; - -// Interface class for managing non-persistent buffers. -// The default non-persistent buffers are temp buffers that are not resizable. -// Support of at least one resizable buffer is required. -class INonPersistentBufferAllocator { - public: - INonPersistentBufferAllocator() {} - virtual ~INonPersistentBufferAllocator() {} - - // Allocates a temporary buffer. This buffer is not resizable. - virtual uint8_t* AllocateTemp(size_t size, size_t alignment) = 0; - - // Signals that a temporary buffer is no longer needed. - virtual void DeallocateTemp(uint8_t* buf) = 0; - - // Returns true if all temporary buffers are already deallocated. - virtual bool IsAllTempDeallocated() = 0; - - // Signals that all temporary allocations can be reclaimed. TFLM calls this - // API when it knows that all temporary buffers that it requested has been - // deallocated. The goal of API is to facilitate implementations of - // INonPersistentBufferAllocator can reuse buffer with some reasonable - // complexity. - virtual TfLiteStatus ResetTempAllocations() = 0; - - // Returns a buffer that is resizable viable ResizeBuffer(). - virtual uint8_t* AllocateResizableBuffer(size_t size, size_t alignment) = 0; - - // Resizes a buffer that is previously returned by the - // AllocateResizableBuffer. - virtual TfLiteStatus ResizeBuffer(uint8_t* resizable_buf, size_t size, - size_t alignment) = 0; - - // Frees up the memory occupied by the resizable buffer. - virtual TfLiteStatus DeallocateResizableBuffer(uint8_t* resizable_buf) = 0; - - // Returns a pointer pointing to the start of the overlay memory, which is - // used for activation tensors and scratch buffers by kernels at Invoke stage. - virtual uint8_t* GetOverlayMemoryAddress() const = 0; - - // Reserves the size of the overlay memory. This overlay is reserved for the - // kernels at Invoke stage. This is referred to as the overlay because before - // Invoket state, the same memory can be used for temp buffers. The layout of - // the memory is planned by the memory planner separately at Invoke stage. - virtual TfLiteStatus ReserveNonPersistentOverlayMemory(size_t size, - size_t alignment) = 0; - - // Returns the size of non-persistent buffer in use. - virtual size_t GetNonPersistentUsedBytes() const = 0; - - // Returns the number of bytes available with a given alignment. This number - // takes in account any temporary allocations. - virtual size_t GetAvailableMemory(size_t alignment) const = 0; -}; - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_ARENA_ALLOCATOR_IBUFFER_ALLOCATOR_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/arena_allocator/non_persistent_arena_buffer_allocator.cc b/code/components/tflite-lib/tensorflow/lite/micro/arena_allocator/non_persistent_arena_buffer_allocator.cc deleted file mode 100644 index 6389da40..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/arena_allocator/non_persistent_arena_buffer_allocator.cc +++ /dev/null @@ -1,170 +0,0 @@ -/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/micro/arena_allocator/non_persistent_arena_buffer_allocator.h" - -#include "tensorflow/lite/micro/memory_helpers.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" - -namespace tflite { - -NonPersistentArenaBufferAllocator::NonPersistentArenaBufferAllocator( - uint8_t* buffer, size_t buffer_size) - : buffer_head_(buffer), - buffer_tail_(buffer + buffer_size), - head_temp_(buffer), - next_temp_(buffer) {} - -NonPersistentArenaBufferAllocator::~NonPersistentArenaBufferAllocator() {} - -// Allocates a temporary buffer. This buffer is not resizable. -uint8_t* NonPersistentArenaBufferAllocator::AllocateTemp(size_t size, - size_t alignment) { - uint8_t* const aligned_result = AlignPointerUp(next_temp_, alignment); - const size_t available_memory = buffer_tail_ - aligned_result; - if (available_memory < size) { - MicroPrintf( - "Failed to allocate temp memory. Requested: %u, " - "available %u, missing: %u", - size, available_memory, size - available_memory); - return nullptr; - } - next_temp_ = aligned_result + size; - temp_buffer_ptr_check_sum_ ^= reinterpret_cast(aligned_result); - temp_buffer_count_++; - return aligned_result; -} - -// Signals that a temporary buffer is no longer needed. -void NonPersistentArenaBufferAllocator::DeallocateTemp(uint8_t* temp_buf) { - temp_buffer_ptr_check_sum_ ^= reinterpret_cast(temp_buf); - temp_buffer_count_--; -} - -// Returns true if all temporary buffers are already deallocated. -bool NonPersistentArenaBufferAllocator::IsAllTempDeallocated() { - if (temp_buffer_count_ != 0 || temp_buffer_ptr_check_sum_ != 0) { - MicroPrintf( - "Number of allocated temp buffers: %d. Checksum passing status: %d", - temp_buffer_count_, !temp_buffer_ptr_check_sum_); - return false; - } - return true; -} - -// Signals that all temporary allocations can be reclaimed. TFLM calls this -// API when it knows that all temporary buffers that it requested has been -// deallocated. The goal of API is to facilitate implementations of -// INonPersistentBufferAllocator can reuse buffer with some reasonable -// complexity. -TfLiteStatus NonPersistentArenaBufferAllocator::ResetTempAllocations() { - if (!IsAllTempDeallocated()) { - MicroPrintf( - "All temp buffers must be freed before calling ResetTempAllocations()"); - return kTfLiteError; - } - next_temp_ = head_temp_; - return kTfLiteOk; -} - -// Returns a buffer that is resizable viable ResizeBuffer(). -uint8_t* NonPersistentArenaBufferAllocator::AllocateResizableBuffer( - size_t size, size_t alignment) { - // Only supports one resizable buffer, which starts at the buffer head. - uint8_t* expected_resizable_buf = AlignPointerUp(buffer_head_, alignment); - - if (resizable_buffer_allocated_) { - MicroPrintf( - "Cannot allocate a new resizable buffer when one is already allocated"); - return nullptr; - } - - if (ResizeBuffer(expected_resizable_buf, size, alignment) == kTfLiteOk) { - resizable_buffer_allocated_ = true; - return expected_resizable_buf; - } - return nullptr; -} - -// Resizes a buffer that is previously returned by the AllocateResizableBuffer. -// Note that ResizeBuffer(old_resizable_buf, 0, 1) effectively deallocates -// a previous allocated resizable buffer. -TfLiteStatus NonPersistentArenaBufferAllocator::ResizeBuffer( - uint8_t* resizable_buf, size_t size, size_t alignment) { - // Only supports one resizable buffer, which starts at the buffer head. - uint8_t* expect_resizable_buf = AlignPointerUp(buffer_head_, alignment); - if (resizable_buf != expect_resizable_buf) { - MicroPrintf("Internal error: buffer is not resizable"); - return kTfLiteError; - } - if (head_temp_ != next_temp_) { - MicroPrintf("ResetTempAllocations() is not called before ResizeBuffer()."); - return kTfLiteError; - } - - const size_t available_memory = buffer_tail_ - expect_resizable_buf; - if (available_memory < size) { - MicroPrintf( - "Failed to resize buffer. Requested: %u, available %u, missing: %u", - size, available_memory, size - available_memory); - return kTfLiteError; - } - head_temp_ = expect_resizable_buf + size; - next_temp_ = head_temp_; - - return kTfLiteOk; -} - -// Frees up the memory occupied by the resizable buffer. -TfLiteStatus NonPersistentArenaBufferAllocator::DeallocateResizableBuffer( - uint8_t* resizable_buf) { - TfLiteStatus status = ResizeBuffer(resizable_buf, 0, 1); - if (status == kTfLiteOk) { - resizable_buffer_allocated_ = false; - } - return status; -} - -// Returns a pointer pointing to the start of the overlay memory, which is -// used for activation tensors and scratch buffers by kernels at Invoke stage. -uint8_t* NonPersistentArenaBufferAllocator::GetOverlayMemoryAddress() const { - return buffer_head_; -} - -// Reserves the size of the overlay memory. This overlay is reserved for the -// kernels at Invoke stage. This is referred to as the overlay because before -// Invoket state, the same memory can be used for temp buffers. The layout of -// the memory is planned by the memory planner separately at Invoke stage. -TfLiteStatus -NonPersistentArenaBufferAllocator::ReserveNonPersistentOverlayMemory( - size_t size, size_t alignment) { - uint8_t* expect_resizable_buf = AlignPointerUp(buffer_head_, alignment); - return ResizeBuffer(expect_resizable_buf, size, alignment); -} - -// Returns the size of non-persistent buffer in use. -size_t NonPersistentArenaBufferAllocator::GetNonPersistentUsedBytes() const { - return (next_temp_ - buffer_head_); -} - -// Returns the number of bytes available with a given alignment. This number -// takes in account any temporary allocations. -size_t NonPersistentArenaBufferAllocator::GetAvailableMemory( - size_t alignment) const { - uint8_t* const aligned_temp = AlignPointerUp(next_temp_, alignment); - uint8_t* const aligned_tail = AlignPointerDown(buffer_tail_, alignment); - return aligned_tail - aligned_temp; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/arena_allocator/non_persistent_arena_buffer_allocator.h b/code/components/tflite-lib/tensorflow/lite/micro/arena_allocator/non_persistent_arena_buffer_allocator.h deleted file mode 100644 index 9eb4efeb..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/arena_allocator/non_persistent_arena_buffer_allocator.h +++ /dev/null @@ -1,105 +0,0 @@ -/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_MICRO_ARENA_ALLOCATOR_NON_PERSISTENT_ARENA_BUFFER_ALLOCATOR_H_ -#define TENSORFLOW_LITE_MICRO_ARENA_ALLOCATOR_NON_PERSISTENT_ARENA_BUFFER_ALLOCATOR_H_ - -#include -#include - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/core/api/error_reporter.h" -#include "tensorflow/lite/micro/arena_allocator/ibuffer_allocator.h" -#include "tensorflow/lite/micro/compatibility.h" - -namespace tflite { - -// Implement INonPersistentBufferAllocator on an arena that is dedicated for -// non-persistent buffers. -class NonPersistentArenaBufferAllocator : public INonPersistentBufferAllocator { - public: - NonPersistentArenaBufferAllocator(uint8_t* buffer, size_t buffer_size); - virtual ~NonPersistentArenaBufferAllocator(); - - // Allocates a temporary buffer. This buffer is not resizable. - uint8_t* AllocateTemp(size_t size, size_t alignment) override; - - // Signals that a temporary buffer is no longer needed. - void DeallocateTemp(uint8_t* buf) override; - - // Returns true if all temporary buffers are already deallocated. - bool IsAllTempDeallocated() override; - - // Signals that all temporary allocations can be reclaimed. TFLM calls this - // API when it knows that all temporary buffers that it requested has been - // deallocated. - TfLiteStatus ResetTempAllocations() override; - - // Returns a buffer that is resizable viable ResizeBuffer(). - uint8_t* AllocateResizableBuffer(size_t size, size_t alignment) override; - - // Resizes a buffer that is previously returned by the - // AllocateResizableBuffer. - TfLiteStatus ResizeBuffer(uint8_t* resizable_buf, size_t size, - size_t alignment) override; - - // Frees up the memory occupied by the resizable buffer. - TfLiteStatus DeallocateResizableBuffer(uint8_t* resizable_buf) override; - - // Returns a pointer pointing to the start of the overlay memory, which is - // used for activation tensors and scratch buffers by kernels at Invoke stage. - uint8_t* GetOverlayMemoryAddress() const override; - - // Reserves the size of the overlay memory. This overlay is reserved for the - // kernels at Invoke stage. This is referred to as the overlay because before - // Invoket state, the same memory can be used for temp buffers. The layout of - // the memory is planned by the memory planner separately at Invoke stage. - TfLiteStatus ReserveNonPersistentOverlayMemory(size_t size, - size_t alignment) override; - - // Returns the size of non-persistent buffer in use. - size_t GetNonPersistentUsedBytes() const override; - - // Returns the number of bytes available with a given alignment. This number - // takes in account any temporary allocations. - size_t GetAvailableMemory(size_t alignment) const override; - - TF_LITE_REMOVE_VIRTUAL_DELETE - - private: - // The memory arena that this allocator manages. - uint8_t* const buffer_head_; - uint8_t* const buffer_tail_; - - // The whole region is split into two parts: - // buffer_head_ to head_temp_ - 1 belongs to the only resizable buffer. - // head_temp_ to buffer_tail_ can be used for (non-resizable) temp buffers. - uint8_t* head_temp_; - - // next_temp_ points to the next available temp buffer allocation address and - // its range is between head_temp_ and buffer_tail_ - uint8_t* next_temp_; - - // XOR Check sum for outstanding temp buffers. - // If all temp buffers are deallocated OR no temp buffers are allocated, - // temp_buffer_ptr_check_sum_ == nullptr. - intptr_t temp_buffer_ptr_check_sum_ = 0; - // Count of outstanding temp buffers. - int temp_buffer_count_ = 0; - bool resizable_buffer_allocated_ = false; -}; - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_ARENA_ALLOCATOR_NON_PERSISTENT_ARENA_BUFFER_ALLOCATOR_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/arena_allocator/persistent_arena_buffer_allocator.cc b/code/components/tflite-lib/tensorflow/lite/micro/arena_allocator/persistent_arena_buffer_allocator.cc deleted file mode 100644 index 0ccc8fb1..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/arena_allocator/persistent_arena_buffer_allocator.cc +++ /dev/null @@ -1,52 +0,0 @@ -/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/micro/arena_allocator/persistent_arena_buffer_allocator.h" - -#include "tensorflow/lite/micro/memory_helpers.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" - -namespace tflite { - -PersistentArenaBufferAllocator::PersistentArenaBufferAllocator( - uint8_t* buffer, size_t buffer_size) - : buffer_head_(buffer), - buffer_tail_(buffer + buffer_size), - tail_temp_(buffer_tail_) {} - -PersistentArenaBufferAllocator::~PersistentArenaBufferAllocator() {} - -uint8_t* PersistentArenaBufferAllocator::AllocatePersistentBuffer( - size_t size, size_t alignment) { - uint8_t* const aligned_result = - AlignPointerDown(tail_temp_ - size, alignment); - if (aligned_result < buffer_head_) { -#ifndef TF_LITE_STRIP_ERROR_STRINGS - const size_t missing_memory = buffer_head_ - aligned_result; - MicroPrintf( - "Failed to allocate tail memory. Requested: %u, " - "available %u, missing: %u", - size, size - missing_memory, missing_memory); -#endif - return nullptr; - } - tail_temp_ = aligned_result; - return aligned_result; -} - -size_t PersistentArenaBufferAllocator::GetPersistentUsedBytes() const { - return buffer_tail_ - tail_temp_; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/arena_allocator/persistent_arena_buffer_allocator.h b/code/components/tflite-lib/tensorflow/lite/micro/arena_allocator/persistent_arena_buffer_allocator.h deleted file mode 100644 index 70de408f..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/arena_allocator/persistent_arena_buffer_allocator.h +++ /dev/null @@ -1,59 +0,0 @@ -/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_MICRO_ARENA_ALLOCATOR_PERSISTENT_ARENA_BUFFER_ALLOCATOR_H_ -#define TENSORFLOW_LITE_MICRO_ARENA_ALLOCATOR_PERSISTENT_ARENA_BUFFER_ALLOCATOR_H_ - -#include -#include - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/core/api/error_reporter.h" -#include "tensorflow/lite/micro/arena_allocator/ibuffer_allocator.h" -#include "tensorflow/lite/micro/compatibility.h" - -namespace tflite { - -// PersistentArenaBufferAllocator is an implementatation of -// IPersistentBufferAllocator interface on an arena that is dedicated for -// persistent buffers. -class PersistentArenaBufferAllocator : public IPersistentBufferAllocator { - public: - PersistentArenaBufferAllocator(uint8_t* buffer, size_t buffer_size); - virtual ~PersistentArenaBufferAllocator(); - - // Allocates persistent memory. The persistent buffer is never freed. - // Returns nullptr if errors occured. - uint8_t* AllocatePersistentBuffer(size_t size, size_t alignment) override; - - // Returns the size of all persistent allocations in bytes. - size_t GetPersistentUsedBytes() const override; - - TF_LITE_REMOVE_VIRTUAL_DELETE - private: - // The memory arena that this allocator manages. - uint8_t* const buffer_head_; - uint8_t* const buffer_tail_; - - // The whole region is split into two parts: - // tail_temp_ to buffer_tail_ contains allocated buffers; - // buffer_head_ to tail_temp_ - 1 belongs to still available spaces. - // So in essence, the allocated region grows from the bottom and emulates - // SingleArenaBufferAllocator's persistent part. - uint8_t* tail_temp_; -}; - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_ARENA_ALLOCATOR_PERSISTENT_ARENA_BUFFER_ALLOCATOR_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/arena_allocator/recording_single_arena_buffer_allocator.cc b/code/components/tflite-lib/tensorflow/lite/micro/arena_allocator/recording_single_arena_buffer_allocator.cc deleted file mode 100644 index 0f24a0b5..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/arena_allocator/recording_single_arena_buffer_allocator.cc +++ /dev/null @@ -1,87 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/arena_allocator/recording_single_arena_buffer_allocator.h" - -#include - -#include "tensorflow/lite/kernels/internal/compatibility.h" - -namespace tflite { - -RecordingSingleArenaBufferAllocator::RecordingSingleArenaBufferAllocator( - ErrorReporter* error_reporter, uint8_t* buffer_head, size_t buffer_size) - : SingleArenaBufferAllocator(error_reporter, buffer_head, buffer_size), - requested_head_bytes_(0), - requested_tail_bytes_(0), - used_bytes_(0), - alloc_count_(0) {} - -RecordingSingleArenaBufferAllocator::~RecordingSingleArenaBufferAllocator() {} - -RecordingSingleArenaBufferAllocator* -RecordingSingleArenaBufferAllocator::Create(ErrorReporter* error_reporter, - uint8_t* buffer_head, - size_t buffer_size) { - TFLITE_DCHECK(error_reporter != nullptr); - TFLITE_DCHECK(buffer_head != nullptr); - RecordingSingleArenaBufferAllocator tmp = RecordingSingleArenaBufferAllocator( - error_reporter, buffer_head, buffer_size); - - uint8_t* allocator_buffer = tmp.AllocatePersistentBuffer( - sizeof(RecordingSingleArenaBufferAllocator), - alignof(RecordingSingleArenaBufferAllocator)); - // Use the default copy constructor to populate internal states. - return new (allocator_buffer) RecordingSingleArenaBufferAllocator(tmp); -} - -size_t RecordingSingleArenaBufferAllocator::GetRequestedBytes() const { - return requested_head_bytes_ + requested_tail_bytes_; -} - -size_t RecordingSingleArenaBufferAllocator::GetUsedBytes() const { - return used_bytes_; -} - -size_t RecordingSingleArenaBufferAllocator::GetAllocatedCount() const { - return alloc_count_; -} - -TfLiteStatus RecordingSingleArenaBufferAllocator::ResizeBuffer( - uint8_t* resizable_buf, size_t size, size_t alignment) { - const uint8_t* previous_head = head(); - TfLiteStatus status = - SingleArenaBufferAllocator::ResizeBuffer(resizable_buf, size, alignment); - if (status == kTfLiteOk) { - used_bytes_ += head() - previous_head; - requested_head_bytes_ = size; - } - return status; -} - -uint8_t* RecordingSingleArenaBufferAllocator::AllocatePersistentBuffer( - size_t size, size_t alignment) { - const uint8_t* previous_tail = tail(); - uint8_t* result = - SingleArenaBufferAllocator::AllocatePersistentBuffer(size, alignment); - if (result != nullptr) { - used_bytes_ += previous_tail - tail(); - requested_tail_bytes_ += size; - alloc_count_++; - } - return result; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/arena_allocator/recording_single_arena_buffer_allocator.h b/code/components/tflite-lib/tensorflow/lite/micro/arena_allocator/recording_single_arena_buffer_allocator.h deleted file mode 100644 index 3cec561e..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/arena_allocator/recording_single_arena_buffer_allocator.h +++ /dev/null @@ -1,64 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_ARENA_ALLOCATOR_RECORDING_SINGLE_ARENA_BUFFER_ALLOCATOR_H_ -#define TENSORFLOW_LITE_MICRO_ARENA_ALLOCATOR_RECORDING_SINGLE_ARENA_BUFFER_ALLOCATOR_H_ - -#include "tensorflow/lite/micro/arena_allocator/single_arena_buffer_allocator.h" -#include "tensorflow/lite/micro/compatibility.h" - -namespace tflite { - -// Utility class used to log allocations of a SingleArenaBufferAllocator. Should -// only be used in debug/evaluation settings or unit tests to evaluate -// allocation usage. -class RecordingSingleArenaBufferAllocator : public SingleArenaBufferAllocator { - public: - RecordingSingleArenaBufferAllocator(ErrorReporter* error_reporter, - uint8_t* buffer_head, size_t buffer_size); - // TODO(b/157615197): Cleanup constructors/destructor and use factory - // functions. - ~RecordingSingleArenaBufferAllocator() override; - - static RecordingSingleArenaBufferAllocator* Create( - ErrorReporter* error_reporter, uint8_t* buffer_head, size_t buffer_size); - - // Returns the number of bytes requested from the head or tail. - size_t GetRequestedBytes() const; - - // Returns the number of bytes actually allocated from the head or tail. This - // value will be >= to the number of requested bytes due to padding and - // alignment. - size_t GetUsedBytes() const; - - // Returns the number of alloc calls from the head or tail. - size_t GetAllocatedCount() const; - - TfLiteStatus ResizeBuffer(uint8_t* resizable_buf, size_t size, - size_t alignment) override; - uint8_t* AllocatePersistentBuffer(size_t size, size_t alignment) override; - - private: - size_t requested_head_bytes_; - size_t requested_tail_bytes_; - size_t used_bytes_; - size_t alloc_count_; - - TF_LITE_REMOVE_VIRTUAL_DELETE -}; - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_ARENA_ALLOCATOR_RECORDING_SINGLE_ARENA_BUFFER_ALLOCATOR_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/arena_allocator/single_arena_buffer_allocator.cc b/code/components/tflite-lib/tensorflow/lite/micro/arena_allocator/single_arena_buffer_allocator.cc deleted file mode 100644 index 15d512bd..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/arena_allocator/single_arena_buffer_allocator.cc +++ /dev/null @@ -1,209 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/arena_allocator/single_arena_buffer_allocator.h" - -#include -#include -#include - -#include "tensorflow/lite/c/c_api_types.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/core/api/error_reporter.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/memory_helpers.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" - -namespace tflite { - -SingleArenaBufferAllocator::SingleArenaBufferAllocator( - ErrorReporter* error_reporter, uint8_t* buffer_head, uint8_t* buffer_tail) - : -#if !defined(TF_LITE_STRIP_ERROR_STRINGS) - error_reporter_(error_reporter), -#endif - buffer_head_(buffer_head), - buffer_tail_(buffer_tail), - head_(buffer_head), - tail_(buffer_tail), - temp_(buffer_head_) { -} - -SingleArenaBufferAllocator::SingleArenaBufferAllocator( - ErrorReporter* error_reporter, uint8_t* buffer, size_t buffer_size) - : SingleArenaBufferAllocator(error_reporter, buffer, buffer + buffer_size) { -} - -/* static */ -SingleArenaBufferAllocator* SingleArenaBufferAllocator::Create( - ErrorReporter* error_reporter, uint8_t* buffer_head, size_t buffer_size) { - TFLITE_DCHECK(error_reporter != nullptr); - TFLITE_DCHECK(buffer_head != nullptr); - SingleArenaBufferAllocator tmp = - SingleArenaBufferAllocator(error_reporter, buffer_head, buffer_size); - - // Allocate enough bytes from the buffer to create a - // SingleArenaBufferAllocator. The new instance will use the current adjusted - // tail buffer from the tmp allocator instance. - uint8_t* allocator_buffer = tmp.AllocatePersistentBuffer( - sizeof(SingleArenaBufferAllocator), alignof(SingleArenaBufferAllocator)); - // Use the default copy constructor to populate internal states. - return new (allocator_buffer) SingleArenaBufferAllocator(tmp); -} - -SingleArenaBufferAllocator::~SingleArenaBufferAllocator() {} - -uint8_t* SingleArenaBufferAllocator::AllocateResizableBuffer(size_t size, - size_t alignment) { - // Only supports one resizable buffer, which starts at the buffer head. - uint8_t* expect_resizable_buf = AlignPointerUp(buffer_head_, alignment); - if (ResizeBuffer(expect_resizable_buf, size, alignment) == kTfLiteOk) { - return expect_resizable_buf; - } - return nullptr; -} - -TfLiteStatus SingleArenaBufferAllocator::DeallocateResizableBuffer( - uint8_t* resizable_buf) { - return ResizeBuffer(resizable_buf, 0, 1); -} - -TfLiteStatus SingleArenaBufferAllocator::ReserveNonPersistentOverlayMemory( - size_t size, size_t alignment) { - uint8_t* expect_resizable_buf = AlignPointerUp(buffer_head_, alignment); - return ResizeBuffer(expect_resizable_buf, size, alignment); -} - -TfLiteStatus SingleArenaBufferAllocator::ResizeBuffer(uint8_t* resizable_buf, - size_t size, - size_t alignment) { - // Only supports one resizable buffer, which starts at the buffer head. - uint8_t* expect_resizable_buf = AlignPointerUp(buffer_head_, alignment); - if (head_ != temp_ || resizable_buf != expect_resizable_buf) { - TF_LITE_REPORT_ERROR( - error_reporter_, - "Internal error: either buffer is not resizable or " - "ResetTempAllocations() is not called before ResizeBuffer()."); - return kTfLiteError; - } - - uint8_t* const aligned_result = AlignPointerUp(buffer_head_, alignment); - const size_t available_memory = tail_ - aligned_result; - if (available_memory < size) { - TF_LITE_REPORT_ERROR( - error_reporter_, - "Failed to resize buffer. Requested: %u, available %u, missing: %u", - size, available_memory, size - available_memory); - return kTfLiteError; - } - head_ = aligned_result + size; - temp_ = head_; - - return kTfLiteOk; -} - -uint8_t* SingleArenaBufferAllocator::AllocatePersistentBuffer( - size_t size, size_t alignment) { - uint8_t* const aligned_result = AlignPointerDown(tail_ - size, alignment); - if (aligned_result < head_) { -#ifndef TF_LITE_STRIP_ERROR_STRINGS - const size_t missing_memory = head_ - aligned_result; - TF_LITE_REPORT_ERROR(error_reporter_, - "Failed to allocate tail memory. Requested: %u, " - "available %u, missing: %u", - size, size - missing_memory, missing_memory); -#endif - return nullptr; - } - tail_ = aligned_result; - return aligned_result; -} - -uint8_t* SingleArenaBufferAllocator::AllocateTemp(size_t size, - size_t alignment) { - uint8_t* const aligned_result = AlignPointerUp(temp_, alignment); - const size_t available_memory = tail_ - aligned_result; - if (available_memory < size) { - TF_LITE_REPORT_ERROR(error_reporter_, - "Failed to allocate temp memory. Requested: %u, " - "available %u, missing: %u", - size, available_memory, size - available_memory); - return nullptr; - } - temp_ = aligned_result + size; - temp_buffer_ptr_check_sum_ ^= (reinterpret_cast(aligned_result)); - temp_buffer_count_++; - return aligned_result; -} - -void SingleArenaBufferAllocator::DeallocateTemp(uint8_t* temp_buf) { - temp_buffer_ptr_check_sum_ ^= (reinterpret_cast(temp_buf)); - temp_buffer_count_--; -} - -bool SingleArenaBufferAllocator::IsAllTempDeallocated() { - if (temp_buffer_count_ != 0 || temp_buffer_ptr_check_sum_ != 0) { - MicroPrintf( - "Number of allocated temp buffers: %d. Checksum passing status: %d", - temp_buffer_count_, !temp_buffer_ptr_check_sum_); - return false; - } - return true; -} - -TfLiteStatus SingleArenaBufferAllocator::ResetTempAllocations() { - // TODO(b/209453859): enable error check based on IsAllTempDeallocated after - // all AllocateTemp have been paird with DeallocateTemp - if (!IsAllTempDeallocated()) { - MicroPrintf( - "All temp buffers must be freed before calling ResetTempAllocations()"); - return kTfLiteError; - } - temp_ = head_; - return kTfLiteOk; -} - -uint8_t* SingleArenaBufferAllocator::GetOverlayMemoryAddress() const { - return buffer_head_; -} - -size_t SingleArenaBufferAllocator::GetNonPersistentUsedBytes() const { - return std::max(head_ - buffer_head_, temp_ - buffer_head_); -} - -size_t SingleArenaBufferAllocator::GetPersistentUsedBytes() const { - return buffer_tail_ - tail_; -} - -size_t SingleArenaBufferAllocator::GetAvailableMemory(size_t alignment) const { - uint8_t* const aligned_temp = AlignPointerUp(temp_, alignment); - uint8_t* const aligned_tail = AlignPointerDown(tail_, alignment); - return aligned_tail - aligned_temp; -} - -size_t SingleArenaBufferAllocator::GetUsedBytes() const { - return GetPersistentUsedBytes() + GetNonPersistentUsedBytes(); -} - -size_t SingleArenaBufferAllocator::GetBufferSize() const { - return buffer_tail_ - buffer_head_; -} - -uint8_t* SingleArenaBufferAllocator::head() const { return head_; } - -uint8_t* SingleArenaBufferAllocator::tail() const { return tail_; } - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/arena_allocator/single_arena_buffer_allocator.h b/code/components/tflite-lib/tensorflow/lite/micro/arena_allocator/single_arena_buffer_allocator.h deleted file mode 100644 index d3be1f23..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/arena_allocator/single_arena_buffer_allocator.h +++ /dev/null @@ -1,152 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_ARENA_ALLOCATOR_SINGLE_ARENA_BUFFER_ALLOCATOR_H_ -#define TENSORFLOW_LITE_MICRO_ARENA_ALLOCATOR_SINGLE_ARENA_BUFFER_ALLOCATOR_H_ - -#include -#include - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/core/api/error_reporter.h" -#include "tensorflow/lite/micro/arena_allocator/ibuffer_allocator.h" -#include "tensorflow/lite/micro/compatibility.h" - -namespace tflite { - -// TODO(petewarden): This allocator never frees up or reuses any memory, even -// though we have enough information about lifetimes of the tensors to do so. -// This makes it pretty wasteful, so we should use a more intelligent method. -class SingleArenaBufferAllocator : public INonPersistentBufferAllocator, - public IPersistentBufferAllocator { - public: - // TODO(b/157615197): Cleanup constructors/destructor and use factory - // functions. - SingleArenaBufferAllocator(ErrorReporter* error_reporter, - uint8_t* buffer_head, uint8_t* buffer_tail); - SingleArenaBufferAllocator(ErrorReporter* error_reporter, uint8_t* buffer, - size_t buffer_size); - virtual ~SingleArenaBufferAllocator(); - - // Creates a new SingleArenaBufferAllocator from a given buffer head and size. - static SingleArenaBufferAllocator* Create(ErrorReporter* error_reporter, - uint8_t* buffer_head, - size_t buffer_size); - - // Resizes a buffer that is previously returned by the - // AllocateResizableBuffer. In current implementation, it Adjusts the head - // (lowest address and moving upwards) memory allocation to a given size. - // Calls to this method will also invalidate all temporary allocation values - // (it sets the location of temp space at the end of the head section). This - // call will fail if a chain of allocations through AllocateTemp() have not - // been cleaned up with a call to ResetTempAllocations(). - virtual TfLiteStatus ResizeBuffer(uint8_t* resizable_buf, size_t size, - size_t alignment) override; - - // Returns a buffer that is resizable viable ResizeBuffer(). Only one - // resizable buffer is currently supported. - virtual uint8_t* AllocateResizableBuffer(size_t size, - size_t alignment) override; - - // Frees up the memory occupied by the resizable buffer - virtual TfLiteStatus DeallocateResizableBuffer( - uint8_t* resizable_buf) override; - - // Reserves the non-persistent memory that is planned by the memory planner. - virtual TfLiteStatus ReserveNonPersistentOverlayMemory( - size_t size, size_t alignment) override; - - // Allocates persistent memory starting at the tail of the arena (highest - // address and moving downwards). - virtual uint8_t* AllocatePersistentBuffer(size_t size, - size_t alignment) override; - - // Allocates a temporary buffer from the head of the arena (lowest address and - // moving upwards) but does not update the actual head allocation size or - // position. The returned buffer is guaranteed until either - // ResetTempAllocations() is called or another call to AllocateFromHead(). - // Repeat calls to this function will create a chain of temp allocations. All - // calls to AllocateTemp() must end with a call to ResetTempAllocations(). If - // AllocateFromHead() is called before a call to ResetTempAllocations(), it - // will fail with an error message. - virtual uint8_t* AllocateTemp(size_t size, size_t alignment) override; - - // Signals that a temporary buffer is no longer needed. This is currently for - // book-keeping purpose and the memory region are not immediately available - // for re-use. The deallocated memory region are only reclaimed after - // ResetTempAllocations is called as it is right now. - virtual void DeallocateTemp(uint8_t* buf) override; - - // Returns true if all temporary buffers are already deallocated. - virtual bool IsAllTempDeallocated() override; - - // Resets a chain of temporary allocations back to the current head of the - // arena (lowest address). - virtual TfLiteStatus ResetTempAllocations() override; - - // Returns a pointer to the buffer currently assigned to the head section. - // This buffer is set by calling SetHeadSize(). - uint8_t* GetOverlayMemoryAddress() const override; - - // Returns the size of the head section in bytes. - size_t GetNonPersistentUsedBytes() const override; - - // Returns the size of all allocations in the tail section in bytes. - size_t GetPersistentUsedBytes() const override; - - // Returns the number of bytes available with a given alignment. This number - // takes in account any temporary allocations. - size_t GetAvailableMemory(size_t alignment) const override; - - // Returns the number of used bytes in the allocator. This number takes in - // account any temporary allocations. - size_t GetUsedBytes() const; - - TF_LITE_REMOVE_VIRTUAL_DELETE - - protected: - // Returns a pointer to the current end of the head buffer. - uint8_t* head() const; - - // Returns a pointer to the current end of the tail buffer. - uint8_t* tail() const; - - private: - size_t GetBufferSize() const; - -#if !defined(TF_LITE_STRIP_ERROR_STRINGS) - ErrorReporter* error_reporter_; -#endif - uint8_t* buffer_head_; - uint8_t* buffer_tail_; - uint8_t* head_; - uint8_t* tail_; - uint8_t* temp_; - - // The combination of the checksum of outstanding temporary buffer pointers - // AND the count of outstanding temporary buffer provide a low cost mechanism - // to audit temporary buffers' allocation and deallocation. - // - // XOR Check sum for outstanding temp buffers. - // If all temp buffers are deallocated OR no temp buffers are allocated, - // temp_buffer_ptr_check_sum_ == nullptr. - intptr_t temp_buffer_ptr_check_sum_ = 0; - // Count of outstanding temp buffers. - int temp_buffer_count_ = 0; -}; - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_ARENA_ALLOCATOR_SINGLE_ARENA_BUFFER_ALLOCATOR_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/compatibility.h b/code/components/tflite-lib/tensorflow/lite/micro/compatibility.h deleted file mode 100644 index 49acb28f..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/compatibility.h +++ /dev/null @@ -1,32 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_MICRO_COMPATIBILITY_H_ -#define TENSORFLOW_LITE_MICRO_COMPATIBILITY_H_ - -// C++ will automatically create class-specific delete operators for virtual -// objects, which by default call the global delete function. For embedded -// applications we want to avoid this, and won't be calling new/delete on these -// objects, so we need to override the default implementation with one that does -// nothing to avoid linking in ::delete(). -// This macro needs to be included in all subclasses of a virtual base class in -// the private section. -#ifdef TF_LITE_STATIC_MEMORY -#define TF_LITE_REMOVE_VIRTUAL_DELETE \ - void operator delete(void* p) {} -#else -#define TF_LITE_REMOVE_VIRTUAL_DELETE -#endif - -#endif // TENSORFLOW_LITE_MICRO_COMPATIBILITY_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/debug_log.cc b/code/components/tflite-lib/tensorflow/lite/micro/debug_log.cc deleted file mode 100644 index 46ca253a..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/debug_log.cc +++ /dev/null @@ -1,50 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -// Reference implementation of the DebugLog() function that's required for a -// platform to support the TensorFlow Lite for Microcontrollers library. This is -// the only function that's absolutely required to be available on a target -// device, since it's used for communicating test results back to the host so -// that we can verify the implementation is working correctly. -// It's designed to be as easy as possible to supply an implementation though. -// On platforms that have a POSIX stack or C library, it can be written as a -// single call to `fprintf(stderr, "%s", s)` to output a string to the error -// stream of the console, but if there's no OS or C library available, there's -// almost always an equivalent way to write out a string to some serial -// interface that can be used instead. For example on Arm M-series MCUs, calling -// the `bkpt #0xAB` assembler instruction will output the string in r1 to -// whatever debug serial connection is available. If you're running mbed, you -// can do the same by creating `Serial pc(USBTX, USBRX)` and then calling -// `pc.printf("%s", s)`. -// To add an equivalent function for your own platform, create your own -// implementation file, and place it in a subfolder with named after the OS -// you're targeting. For example, see the Cortex M bare metal version in -// tensorflow/lite/micro/bluepill/debug_log.cc or the mbed one on -// tensorflow/lite/micro/mbed/debug_log.cc. - -#include "tensorflow/lite/micro/debug_log.h" - -#ifndef TF_LITE_STRIP_ERROR_STRINGS -#include -#endif - -extern "C" void DebugLog(const char* s) { -#ifndef TF_LITE_STRIP_ERROR_STRINGS - // Reusing TF_LITE_STRIP_ERROR_STRINGS to disable DebugLog completely to get - // maximum reduction in binary size. This is because we have DebugLog calls - // via TF_LITE_CHECK that are not stubbed out by TF_LITE_REPORT_ERROR. - fprintf(stderr, "%s", s); -#endif -} diff --git a/code/components/tflite-lib/tensorflow/lite/micro/debug_log.h b/code/components/tflite-lib/tensorflow/lite/micro/debug_log.h deleted file mode 100644 index c2840d0f..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/debug_log.h +++ /dev/null @@ -1,31 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_MICRO_DEBUG_LOG_H_ -#define TENSORFLOW_LITE_MICRO_DEBUG_LOG_H_ - -#ifdef __cplusplus -extern "C" { -#endif // __cplusplus - -// This function should be implemented by each target platform, and provide a -// way for strings to be output to some text stream. For more information, see -// tensorflow/lite/micro/debug_log.cc. -void DebugLog(const char* s); - -#ifdef __cplusplus -} // extern "C" -#endif // __cplusplus - -#endif // TENSORFLOW_LITE_MICRO_DEBUG_LOG_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/fake_micro_context.cc b/code/components/tflite-lib/tensorflow/lite/micro/fake_micro_context.cc deleted file mode 100644 index 2403c6b1..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/fake_micro_context.cc +++ /dev/null @@ -1,107 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/fake_micro_context.h" - -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/micro/arena_allocator/single_arena_buffer_allocator.h" -#include "tensorflow/lite/micro/micro_allocator.h" -#include "tensorflow/lite/micro/micro_arena_constants.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" - -namespace tflite { -namespace { -// Dummy static variables to allow creation of dummy MicroAllocator. -// All tests are guarateed to run serially. -static constexpr int KDummyTensorArenaSize = 256; -static uint8_t dummy_tensor_arena[KDummyTensorArenaSize]; -} // namespace - -FakeMicroContext::FakeMicroContext(TfLiteTensor* tensors, - SingleArenaBufferAllocator* allocator, - MicroGraph* micro_graph) - : MicroContext( - MicroAllocator::Create(dummy_tensor_arena, KDummyTensorArenaSize, - GetMicroErrorReporter()), - nullptr, micro_graph), - tensors_(tensors), - allocator_(allocator) {} - -TfLiteTensor* FakeMicroContext::AllocateTempTfLiteTensor(int tensor_index) { - allocated_tensor_count_++; - return &tensors_[tensor_index]; -} - -void FakeMicroContext::DeallocateTempTfLiteTensor(TfLiteTensor* tensor) { - allocated_tensor_count_--; -} - -bool FakeMicroContext::IsAllTempTfLiteTensorDeallocated() { - return !allocated_tensor_count_; -} - -TfLiteEvalTensor* FakeMicroContext::GetEvalTensor(int tensor_index) { - TfLiteEvalTensor* eval_tensor = - reinterpret_cast(allocator_->AllocateTemp( - sizeof(TfLiteEvalTensor), alignof(TfLiteEvalTensor))); - TFLITE_DCHECK(eval_tensor != nullptr); - - // In unit tests, the TfLiteTensor pointer contains the source of truth for - // buffers and values: - eval_tensor->data = tensors_[tensor_index].data; - eval_tensor->dims = tensors_[tensor_index].dims; - eval_tensor->type = tensors_[tensor_index].type; - return eval_tensor; -} - -void* FakeMicroContext::AllocatePersistentBuffer(size_t bytes) { - // FakeMicroContext use SingleArenaBufferAllocator, which does not - // automatically apply the buffer alignment like MicroAllocator. The buffer - // alignment is potentially wasteful but allows the fake_micro_context to work - // correctly with optimized kernels. - return allocator_->AllocatePersistentBuffer(bytes, - MicroArenaBufferAlignment()); -} - -TfLiteStatus FakeMicroContext::RequestScratchBufferInArena(size_t bytes, - int* buffer_index) { - TFLITE_DCHECK(buffer_index != nullptr); - - if (scratch_buffer_count_ == kNumScratchBuffers_) { - MicroPrintf("Exceeded the maximum number of scratch tensors allowed (%d).", - kNumScratchBuffers_); - return kTfLiteError; - } - - // For tests, we allocate scratch buffers from the tail and keep them around - // for the lifetime of model. This means that the arena size in the tests will - // be more than what we would have if the scratch buffers could share memory. - scratch_buffers_[scratch_buffer_count_] = - allocator_->AllocatePersistentBuffer(bytes, MicroArenaBufferAlignment()); - TFLITE_DCHECK(scratch_buffers_[scratch_buffer_count_] != nullptr); - - *buffer_index = scratch_buffer_count_++; - return kTfLiteOk; -} - -void* FakeMicroContext::GetScratchBuffer(int buffer_index) { - TFLITE_DCHECK(scratch_buffer_count_ <= kNumScratchBuffers_); - if (buffer_index >= scratch_buffer_count_) { - return nullptr; - } - return scratch_buffers_[buffer_index]; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/fake_micro_context.h b/code/components/tflite-lib/tensorflow/lite/micro/fake_micro_context.h deleted file mode 100644 index 31b39d38..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/fake_micro_context.h +++ /dev/null @@ -1,56 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_FAKE_MICRO_CONTEXT_H_ -#define TENSORFLOW_LITE_MICRO_FAKE_MICRO_CONTEXT_H_ - -#include "tensorflow/lite/micro/micro_context.h" -#include "tensorflow/lite/micro/micro_graph.h" - -namespace tflite { -// A fake of MicroContext for kernel util tests. -class FakeMicroContext : public MicroContext { - public: - FakeMicroContext(TfLiteTensor* tensors, SingleArenaBufferAllocator* allocator, - MicroGraph* micro_graph); - - void* AllocatePersistentBuffer(size_t bytes) override; - TfLiteStatus RequestScratchBufferInArena(size_t bytes, - int* buffer_index) override; - void* GetScratchBuffer(int buffer_index) override; - - TfLiteTensor* AllocateTempTfLiteTensor(int tensor_index) override; - void DeallocateTempTfLiteTensor(TfLiteTensor* tensor) override; - bool IsAllTempTfLiteTensorDeallocated(); - - TfLiteEvalTensor* GetEvalTensor(int tensor_index) override; - - private: - static constexpr int kNumScratchBuffers_ = 12; - - int scratch_buffer_count_ = 0; - uint8_t* scratch_buffers_[kNumScratchBuffers_]; - - TfLiteTensor* tensors_; - int allocated_tensor_count_ = 0; - - SingleArenaBufferAllocator* allocator_; - - TF_LITE_REMOVE_VIRTUAL_DELETE -}; - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_FAKE_MICRO_CONTEXT_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/flatbuffer_utils.cc b/code/components/tflite-lib/tensorflow/lite/micro/flatbuffer_utils.cc deleted file mode 100644 index 9996172b..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/flatbuffer_utils.cc +++ /dev/null @@ -1,84 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/flatbuffer_utils.h" - -namespace tflite { - -FlexbufferWrapper::FlexbufferWrapper(const uint8_t* buffer, size_t size) - : flexbuffers::Vector(flexbuffers::GetRoot(buffer, size).AsVector()) {} - -int64_t FlexbufferWrapper::ElementAsInt64(size_t i) const { - const uint8_t* elem = data_ + i * byte_width_; - return ::flexbuffers::ReadInt64(elem, byte_width_); -} - -uint64_t FlexbufferWrapper::ElementAsUInt64(size_t i) const { - const uint8_t* elem = data_ + i * byte_width_; - return ::flexbuffers::ReadUInt64(elem, byte_width_); -} - -int32_t FlexbufferWrapper::ElementAsInt32(size_t i) const { - return static_cast(ElementAsInt64(i)); -} - -bool FlexbufferWrapper::ElementAsBool(size_t i) const { - return static_cast(ElementAsUInt64(i)); -} - -double FlexbufferWrapper::ElementAsDouble(size_t i) const { - const uint8_t* elem = data_ + i * byte_width_; - return ::flexbuffers::ReadDouble(elem, byte_width_); -} - -float FlexbufferWrapper::ElementAsFloat(size_t i) const { - return static_cast(FlexbufferWrapper::ElementAsDouble(i)); -} - -// TODO(b/192589496): Ops must always be there. Remove this function when fixed -uint32_t NumSubgraphOperators(const SubGraph* subgraph) { - if (subgraph->operators() != nullptr) { - return subgraph->operators()->size(); - } else { - return 0; - } -} -// TODO(b/192589496): Ops must always be there. Remove this function when fixed -uint32_t NumSubgraphOperators(const Model* model, int subgraph_idx) { - const SubGraph* subgraph = model->subgraphs()->Get(subgraph_idx); - return NumSubgraphOperators(subgraph); -} - -TfLiteIntArray* FlatBufferVectorToTfLiteTypeArray( - const flatbuffers::Vector* flatbuffer_array) { - // On little-endian machines, TfLiteIntArray happens to have the same memory - // layout as flatbuffers:Vector, so we can reinterpret_cast the - // flatbuffer vector and avoid a copy and malloc. - // TODO(b/188459715): audit this usage of const_cast. - return const_cast( - reinterpret_cast(flatbuffer_array)); -} - -TfLiteFloatArray* FlatBufferVectorToTfLiteTypeArray( - const flatbuffers::Vector* flatbuffer_array) { - // On little-endian machines, TfLiteFloatArray happens to have the same memory - // layout as flatbuffers:Vector, so we can reinterpret_cast the - // flatbuffer vector and avoid a copy and malloc. - // TODO(b/188459715): audit this usage of const_cast. - return const_cast( - reinterpret_cast(flatbuffer_array)); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/flatbuffer_utils.h b/code/components/tflite-lib/tensorflow/lite/micro/flatbuffer_utils.h deleted file mode 100644 index b4e0cdc2..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/flatbuffer_utils.h +++ /dev/null @@ -1,65 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef THIRD_PARTY_TFLITE_MICRO_TENSORFLOW_LITE_MICRO_FLATBUFFER_UTILS_H_ -#define THIRD_PARTY_TFLITE_MICRO_TENSORFLOW_LITE_MICRO_FLATBUFFER_UTILS_H_ - -#include "flatbuffers/flatbuffers.h" -#include "flatbuffers/flexbuffers.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/schema/schema_generated.h" - -namespace tflite { -// Kernels use flexbuffers::Map to pack their init parameters in a tflite file, -// with the parameter names as map keys and the parameter values as the -// corresponding map values. -// Accessing the map values using the flexbuffers:Map class is inline heavy, -// which can cause the code size to bloat beyond what's reasonable for a micro -// application. Use this class instead, when possible. -// FlexbufferWrapper takes advantage of the following properties of -// flexbuffers::Map: -// 1. It can be viewed as a flexbuffers::Vector of the values. -// 2. The values in the vector are ordered alphabetically by their keys. -// 3. All integer and Boolean values are stored as 64-bit numbers. -// 4. All floating point values are stored as double precision numbers. -// The properties are mentioned in the flexbuffers docs, but we rely on -// a unit test to catch design changes. -class FlexbufferWrapper : public flexbuffers::Vector { - public: - // Construct with a serialized flexbuffer 'buffer' of 'size' bytes - explicit FlexbufferWrapper(const uint8_t* buffer, size_t size); - int64_t ElementAsInt64(size_t i) const; - uint64_t ElementAsUInt64(size_t i) const; - int32_t ElementAsInt32(size_t i) const; - bool ElementAsBool(size_t i) const; - double ElementAsDouble(size_t i) const; - float ElementAsFloat(size_t i) const; -}; - -// Return the number of operators in a subgraph tflite -uint32_t NumSubgraphOperators(const SubGraph* subgraph); -uint32_t NumSubgraphOperators(const Model* model, int subgraph_idx); - -// Converts a flatbuffer array to a TfLiteArray. -// TODO(b/188459715): These function convert a const input to a non-const via a -// const_cast. It is unclear exactly why this is required. -TfLiteIntArray* FlatBufferVectorToTfLiteTypeArray( - const flatbuffers::Vector* flatbuffer_array); -TfLiteFloatArray* FlatBufferVectorToTfLiteTypeArray( - const flatbuffers::Vector* flatbuffer_array); - -} // namespace tflite - -#endif // THIRD_PARTY_TFLITE_MICRO_TENSORFLOW_LITE_MICRO_FLATBUFFER_UTILS_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/activation_utils.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/activation_utils.h deleted file mode 100644 index 95ecc26d..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/activation_utils.h +++ /dev/null @@ -1,57 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_ACTIVATION_UTILS_H_ -#define TENSORFLOW_LITE_MICRO_KERNELS_ACTIVATION_UTILS_H_ - -#include -#include - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/kernels/internal/cppmath.h" -#include "tensorflow/lite/kernels/internal/max.h" -#include "tensorflow/lite/kernels/internal/min.h" - -namespace tflite { -namespace ops { -namespace micro { - -// Returns the floating point value for a fused activation: -inline float ActivationValFloat(TfLiteFusedActivation act, float a) { - switch (act) { - case kTfLiteActNone: - return a; - case kTfLiteActRelu: - return TfLiteMax(0.0f, a); - case kTfLiteActReluN1To1: - return TfLiteMax(-1.0f, TfLiteMin(a, 1.0f)); - case kTfLiteActRelu6: - return TfLiteMax(0.0f, TfLiteMin(a, 6.0f)); - case kTfLiteActTanh: - return std::tanh(a); - case kTfLiteActSignBit: - return std::signbit(a); - case kTfLiteActSigmoid: - return 1.0f / (1.0f + std::exp(-a)); - } - return 0.0f; // To indicate an unsupported activation (i.e. when a new fused - // activation is added to the enum and not handled here). -} - -} // namespace micro -} // namespace ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_KERNELS_ACTIVATION_UTILS_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/activations.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/activations.cc deleted file mode 100644 index e0b79631..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/activations.cc +++ /dev/null @@ -1,120 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/kernels/activations.h" - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/internal/types.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" -#include "tensorflow/lite/micro/micro_utils.h" - -namespace tflite { -namespace { - -void* ReluInit(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(ReluOpData)); -} - -TfLiteStatus ReluEval(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - const ReluOpData& data = *(static_cast(node->user_data)); - - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kActivationsInputTensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kActivationsOutputTensor); - - switch (input->type) { - case kTfLiteFloat32: { - ReluFloat(tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - - return kTfLiteOk; - } - case kTfLiteInt8: { - tflite::ReluQuantized(data, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorData(output)); - return kTfLiteOk; - } - default: { - MicroPrintf("Only float32 is supported currently, got %s", - TfLiteTypeGetName(input->type)); - return kTfLiteError; - } - } -} - -void* Relu6Init(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(Relu6OpData)); -} - -TfLiteStatus Relu6Eval(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - const Relu6OpData& data = *(static_cast(node->user_data)); - - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kActivationsInputTensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kActivationsOutputTensor); - - switch (input->type) { - case kTfLiteFloat32: { - Relu6Float(tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - - return kTfLiteOk; - } - case kTfLiteInt8: { - Relu6Quantized(data.zero_int8, data.six_int8, - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - return kTfLiteOk; - } - default: { - MicroPrintf("Only float32 is supported currently, got %s", - TfLiteTypeGetName(input->type)); - return kTfLiteError; - } - } -} - -} // namespace - -TfLiteRegistration Register_RELU() { - return tflite::micro::RegisterOp(ReluInit, ReluPrepare, ReluEval); -} - -TfLiteRegistration Register_RELU6() { - return tflite::micro::RegisterOp(Relu6Init, Relu6Prepare, Relu6Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/activations.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/activations.h deleted file mode 100644 index e953f0e0..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/activations.h +++ /dev/null @@ -1,63 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_ACTIVATIONS_H_ -#define TENSORFLOW_LITE_MICRO_KERNELS_ACTIVATIONS_H_ - -#include - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -extern const int kActivationsInputTensor; -extern const int kActivationsOutputTensor; - -struct ReluOpData { - ReluParams params; -}; - -struct Relu6OpData { - int8_t six_int8; - int8_t zero_int8; -}; - -void ReluQuantized(const ReluOpData& data, const RuntimeShape& input_shape, - const RuntimeShape& output_shape, const int8_t* input_data, - int8_t* output_data); - -template -void CalculateReluOpData(const TfLiteTensor* input, TfLiteTensor* output, - ReluOpData* data); - -void ReluFloat(const RuntimeShape& input_shape, const float* input_data, - const RuntimeShape& output_shape, float* output_data); - -void Relu6Float(const RuntimeShape& input_shape, const float* input_data, - const RuntimeShape& output_shape, float* output_data); - -void Relu6Quantized(int8_t lower, int8_t upper, const RuntimeShape& input_shape, - const int8_t* input_data, const RuntimeShape& output_shape, - int8_t* output_data); - -TfLiteStatus ReluPrepare(TfLiteContext* context, TfLiteNode* node); - -TfLiteStatus Relu6Prepare(TfLiteContext* context, TfLiteNode* node); - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_KERNELS_ACTIVATIONS_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/activations_common.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/activations_common.cc deleted file mode 100644 index 2ec3a1bf..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/activations_common.cc +++ /dev/null @@ -1,158 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include -#include - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/internal/types.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/kernels/activations.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/micro_utils.h" - -namespace tflite { - -const int kActivationsInputTensor = 0; -const int kActivationsOutputTensor = 0; - -void ReluQuantized(const ReluOpData& data, const RuntimeShape& input_shape, - const RuntimeShape& output_shape, const int8_t* input_data, - int8_t* output_data) { - const int flat_size = MatchingFlatSize(input_shape, output_shape); - for (int i = 0; i < flat_size; ++i) { - const int32_t val = static_cast(input_data[i]); - int32_t clamped = - data.params.output_offset + - MultiplyByQuantizedMultiplier(val - data.params.input_offset, - data.params.output_multiplier, - data.params.output_shift); - clamped = std::max(data.params.quantized_activation_min, clamped); - clamped = std::min(data.params.quantized_activation_max, clamped); - output_data[i] = static_cast(clamped); - } -} - -template -void CalculateReluOpData(const TfLiteTensor* input, TfLiteTensor* output, - ReluOpData* data) { - float act_min = 0.0; - float act_max = std::numeric_limits::infinity(); - double real_multiplier = - static_cast(input->params.scale / output->params.scale); - - const RuntimeShape input_shape = GetTensorShape(input); - const RuntimeShape output_shape = GetTensorShape(output); - - QuantizeMultiplier(real_multiplier, &data->params.output_multiplier, - &data->params.output_shift); - - data->params.quantized_activation_min = std::max( - static_cast(std::numeric_limits::min()), - output->params.zero_point + - static_cast(roundf(act_min / output->params.scale))); - data->params.quantized_activation_max = - act_max == std::numeric_limits::infinity() - ? static_cast(std::numeric_limits::max()) - : std::min(static_cast(std::numeric_limits::max()), - output->params.zero_point + - static_cast( - roundf(act_max / output->params.scale))); - data->params.input_offset = input->params.zero_point; - data->params.output_offset = output->params.zero_point; -} - -void ReluFloat(const RuntimeShape& input_shape, const float* input_data, - const RuntimeShape& output_shape, float* output_data) { - const int flat_size = MatchingFlatSize(input_shape, output_shape); - for (int i = 0; i < flat_size; ++i) { - const float val = input_data[i]; - const float lower = 0.0f; - const float clamped = val < lower ? lower : val; - output_data[i] = clamped; - } -} - -void Relu6Float(const RuntimeShape& input_shape, const float* input_data, - const RuntimeShape& output_shape, float* output_data) { - const int flat_size = MatchingFlatSize(input_shape, output_shape); - for (int i = 0; i < flat_size; ++i) { - const float val = input_data[i]; - const float upper = 6.0f; - const float lower = 0.0f; - const float clamped = val > upper ? upper : val < lower ? lower : val; - output_data[i] = clamped; - } -} - -void Relu6Quantized(int8_t lower, int8_t upper, const RuntimeShape& input_shape, - const int8_t* input_data, const RuntimeShape& output_shape, - int8_t* output_data) { - const int flat_size = MatchingFlatSize(input_shape, output_shape); - for (int i = 0; i < flat_size; ++i) { - const int8_t val = input_data[i]; - const int8_t clamped = val > upper ? upper : val < lower ? lower : val; - output_data[i] = clamped; - } -} - -TfLiteStatus ReluPrepare(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - ReluOpData* data = static_cast(node->user_data); - - MicroContext* micro_context = GetMicroContext(context); - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kActivationsInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kActivationsOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - - if (input->type == kTfLiteInt8) { - CalculateReluOpData(input, output, data); - } - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(output); - - return kTfLiteOk; -} - -TfLiteStatus Relu6Prepare(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - Relu6OpData* data = static_cast(node->user_data); - - MicroContext* micro_context = GetMicroContext(context); - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kActivationsInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - - if (input->type == kTfLiteInt8) { - data->six_int8 = FloatToQuantizedType(6.0f, input->params.scale, - input->params.zero_point); - data->zero_int8 = input->params.zero_point; - } - - micro_context->DeallocateTempTfLiteTensor(input); - - return kTfLiteOk; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/add.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/add.cc deleted file mode 100644 index f75db4e5..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/add.cc +++ /dev/null @@ -1,165 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/kernels/internal/reference/add.h" - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/integer_ops/add.h" -#include "tensorflow/lite/kernels/internal/reference/process_broadcast_shapes.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/kernels/add.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/memory_helpers.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" - -namespace tflite { - -void EvalAdd(TfLiteContext* context, TfLiteNode* node, TfLiteAddParams* params, - const OpDataAdd* data, const TfLiteEvalTensor* input1, - const TfLiteEvalTensor* input2, TfLiteEvalTensor* output) { - tflite::ArithmeticParams op_params; - SetActivationParams(data->output_activation_min_f32, - data->output_activation_max_f32, &op_params); - if (data->requires_broadcast) { - reference_ops::BroadcastAdd4DSlow( - op_params, tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } else { - reference_ops::Add(op_params, tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } -} - -TfLiteStatus EvalAddQuantized(TfLiteContext* context, TfLiteNode* node, - TfLiteAddParams* params, const OpDataAdd* data, - const TfLiteEvalTensor* input1, - const TfLiteEvalTensor* input2, - TfLiteEvalTensor* output) { - tflite::ArithmeticParams op_params; - op_params.left_shift = data->left_shift; - op_params.input1_offset = data->input1_offset; - op_params.input1_multiplier = data->input1_multiplier; - op_params.input1_shift = data->input1_shift; - op_params.input2_offset = data->input2_offset; - op_params.input2_multiplier = data->input2_multiplier; - op_params.input2_shift = data->input2_shift; - op_params.output_offset = data->output_offset; - op_params.output_multiplier = data->output_multiplier; - op_params.output_shift = data->output_shift; - SetActivationParams(data->output_activation_min, data->output_activation_max, - &op_params); - bool need_broadcast = reference_ops::ProcessBroadcastShapes( - tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorShape(input2), &op_params); - - switch (output->type) { - case kTfLiteInt8: { - if (need_broadcast) { - reference_integer_ops::BroadcastAdd4DSlow( - op_params, tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } else { - reference_integer_ops::Add( - op_params, tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } - break; - } - case kTfLiteInt16: { - if (need_broadcast) { - reference_ops::BroadcastAdd4DSlow( - op_params, tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } else { - reference_ops::Add(op_params, tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output), - false); - } - break; - } - default: - MicroPrintf("Type %s (%d) not supported.", - TfLiteTypeGetName(output->type), output->type); - return kTfLiteError; - } - - return kTfLiteOk; -} - -void* AddInit(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(OpDataAdd)); -} - -TfLiteStatus AddEval(TfLiteContext* context, TfLiteNode* node) { - auto* params = reinterpret_cast(node->builtin_data); - - TFLITE_DCHECK(node->user_data != nullptr); - const OpDataAdd* data = static_cast(node->user_data); - - const TfLiteEvalTensor* input1 = - tflite::micro::GetEvalInput(context, node, kAddInputTensor1); - const TfLiteEvalTensor* input2 = - tflite::micro::GetEvalInput(context, node, kAddInputTensor2); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kAddOutputTensor); - - if (output->type == kTfLiteFloat32) { - EvalAdd(context, node, params, data, input1, input2, output); - } else if (output->type == kTfLiteInt8 || output->type == kTfLiteInt16) { - TF_LITE_ENSURE_OK(context, EvalAddQuantized(context, node, params, data, - input1, input2, output)); - } else { - MicroPrintf("Type %s (%d) not supported.", TfLiteTypeGetName(output->type), - output->type); - return kTfLiteError; - } - - return kTfLiteOk; -} - -TfLiteRegistration Register_ADD() { - return tflite::micro::RegisterOp(AddInit, AddPrepare, AddEval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/add.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/add.h deleted file mode 100644 index 88526153..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/add.h +++ /dev/null @@ -1,64 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_ADD_H_ -#define TENSORFLOW_LITE_MICRO_KERNELS_ADD_H_ - -#include - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" - -namespace tflite { - -extern const int kAddInputTensor1; -extern const int kAddInputTensor2; -extern const int kAddOutputTensor; - -struct OpDataAdd { - bool requires_broadcast; - - // These fields are used in both the general 8-bit -> 8bit quantized path, - // and the special 16-bit -> 16bit quantized path - int input1_shift; - int input2_shift; - int32_t output_activation_min; - int32_t output_activation_max; - - // These fields are used only in the general 8-bit -> 8bit quantized path - int32_t input1_multiplier; - int32_t input2_multiplier; - int32_t output_multiplier; - int output_shift; - int left_shift; - int32_t input1_offset; - int32_t input2_offset; - int32_t output_offset; - - // Used only for float evals: - float output_activation_min_f32; - float output_activation_max_f32; -}; - -TfLiteStatus CalculateOpDataAdd(TfLiteContext* context, TfLiteAddParams* params, - const TfLiteTensor* input1, - const TfLiteTensor* input2, - TfLiteTensor* output, OpDataAdd* data); - -TfLiteStatus AddPrepare(TfLiteContext* context, TfLiteNode* node); - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_KERNELS_ADD_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/add_common.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/add_common.cc deleted file mode 100644 index b285b800..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/add_common.cc +++ /dev/null @@ -1,106 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/add.h" -#include "tensorflow/lite/kernels/internal/reference/integer_ops/add.h" -#include "tensorflow/lite/kernels/internal/reference/process_broadcast_shapes.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/kernels/add.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/memory_helpers.h" - -namespace tflite { - -const int kAddInputTensor1 = 0; -const int kAddInputTensor2 = 1; -const int kAddOutputTensor = 0; - -TfLiteStatus CalculateOpDataAdd(TfLiteContext* context, TfLiteAddParams* params, - const TfLiteTensor* input1, - const TfLiteTensor* input2, - TfLiteTensor* output, OpDataAdd* data) { - data->requires_broadcast = !HaveSameShapes(input1, input2); - - if (output->type == kTfLiteInt8 || output->type == kTfLiteInt16) { - // 8bit -> 8bit general quantized path, with general rescalings - data->input1_offset = -input1->params.zero_point; - data->input2_offset = -input2->params.zero_point; - data->output_offset = output->params.zero_point; - data->left_shift = (output->type == kTfLiteInt16) ? 15 : 20; - const double twice_max_input_scale = - 2 * static_cast( - std::max(input1->params.scale, input2->params.scale)); - const double real_input1_multiplier = - static_cast(input1->params.scale) / twice_max_input_scale; - const double real_input2_multiplier = - static_cast(input2->params.scale) / twice_max_input_scale; - const double real_output_multiplier = - twice_max_input_scale / - ((1 << data->left_shift) * static_cast(output->params.scale)); - - QuantizeMultiplierSmallerThanOneExp( - real_input1_multiplier, &data->input1_multiplier, &data->input1_shift); - - QuantizeMultiplierSmallerThanOneExp( - real_input2_multiplier, &data->input2_multiplier, &data->input2_shift); - - QuantizeMultiplierSmallerThanOneExp( - real_output_multiplier, &data->output_multiplier, &data->output_shift); - - TF_LITE_ENSURE_STATUS(CalculateActivationRangeQuantized( - context, params->activation, output, &data->output_activation_min, - &data->output_activation_max)); - } else if (output->type == kTfLiteFloat32) { - CalculateActivationRange(params->activation, - &data->output_activation_min_f32, - &data->output_activation_max_f32); - } - - return kTfLiteOk; -} - -TfLiteStatus AddPrepare(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - TFLITE_DCHECK(node->builtin_data != nullptr); - - MicroContext* micro_context = GetMicroContext(context); - TfLiteTensor* input1 = - micro_context->AllocateTempInputTensor(node, kAddInputTensor1); - TF_LITE_ENSURE(context, input1 != nullptr); - TfLiteTensor* input2 = - micro_context->AllocateTempInputTensor(node, kAddInputTensor2); - TF_LITE_ENSURE(context, input2 != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kAddOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - - OpDataAdd* data = static_cast(node->user_data); - auto* params = reinterpret_cast(node->builtin_data); - - TF_LITE_ENSURE_STATUS( - CalculateOpDataAdd(context, params, input1, input2, output, data)); - - micro_context->DeallocateTempTfLiteTensor(input1); - micro_context->DeallocateTempTfLiteTensor(input2); - micro_context->DeallocateTempTfLiteTensor(output); - return kTfLiteOk; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/add_n.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/add_n.cc deleted file mode 100644 index ce064687..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/add_n.cc +++ /dev/null @@ -1,214 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/kernels/internal/reference/add_n.h" - -#include - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { -namespace { - -constexpr int kInputTensor0 = 0; -constexpr int kOutputTensor = 0; - -constexpr int kAddNIntegerShift = 20; - -// only used with INT8 tensors -struct OpData { - int32_t output_activation_min; - int32_t output_activation_max; - int32_t input_offset; - int32_t output_offset; - int32_t input_multiplier; - int32_t output_multiplier; - int input_shift; - int output_shift; - int left_shift; - int scratch_index; -}; - -TfLiteStatus CalculateOpData(TfLiteContext* context, TfLiteNode* node) { - int num_inputs = NumInputs(node); - TF_LITE_ENSURE(context, num_inputs >= 2); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - - MicroContext* micro_context = GetMicroContext(context); - TfLiteTensor* input_tensor_first = - micro_context->AllocateTempInputTensor(node, kInputTensor0); - TF_LITE_ENSURE(context, input_tensor_first != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - - // Check that all tensors have the same shape and type. - TF_LITE_ENSURE_TYPES_EQ(context, output->type, input_tensor_first->type); - for (int i = kInputTensor0 + 1; i < num_inputs; ++i) { - TfLiteTensor* input = micro_context->AllocateTempInputTensor(node, i); - TF_LITE_ENSURE(context, input != nullptr); - TF_LITE_ENSURE(context, HaveSameShapes(input_tensor_first, input)); - TF_LITE_ENSURE_TYPES_EQ(context, input_tensor_first->type, input->type); - - // Check that all INT8 input tensors have the same zero-point and scale. - if (input_tensor_first->type == kTfLiteInt8) { - TF_LITE_ENSURE(context, input_tensor_first->params.zero_point == - input->params.zero_point); - TF_LITE_ENSURE(context, - input_tensor_first->params.scale == input->params.scale); - } - - micro_context->DeallocateTempTfLiteTensor(input); - } - - if (output->type == kTfLiteFloat32) { - // Allocate scratch buffer space for pointer to each tensor's data - // and store the scratch buffer index in the node's user_data - int scratch_index; - size_t scratch_size = sizeof(float*) * num_inputs; - TF_LITE_ENSURE_OK(context, context->RequestScratchBufferInArena( - context, scratch_size, &scratch_index)); - node->user_data = - reinterpret_castuser_data)>(scratch_index); - } else if (output->type == kTfLiteInt8) { - node->user_data = - context->AllocatePersistentBuffer(context, sizeof(OpData)); - OpData* data = static_cast(node->user_data); - - // Allocate scratch buffer space for pointer to each tensor's data - // and store the scratch buffer index in OpData - size_t scratch_size = sizeof(int8_t*) * num_inputs; - TF_LITE_ENSURE_OK( - context, context->RequestScratchBufferInArena(context, scratch_size, - &data->scratch_index)); - - // 8bit -> 8bit general quantized path, with general rescalings - data->input_offset = -input_tensor_first->params.zero_point; - data->output_offset = output->params.zero_point; - data->left_shift = kAddNIntegerShift; - const double twice_max_input_scale = - 2 * static_cast(input_tensor_first->params.scale); - const double real_input_multiplier = - static_cast(input_tensor_first->params.scale) / - twice_max_input_scale; - const double real_output_multiplier = - twice_max_input_scale / - ((1 << data->left_shift) * static_cast(output->params.scale)); - - QuantizeMultiplierSmallerThanOneExp( - real_input_multiplier, &data->input_multiplier, &data->input_shift); - - QuantizeMultiplierSmallerThanOneExp( - real_output_multiplier, &data->output_multiplier, &data->output_shift); - - TF_LITE_ENSURE_STATUS(CalculateActivationRangeQuantized( - context, kTfLiteActNone, output, &data->output_activation_min, - &data->output_activation_max)); - } else { - TF_LITE_KERNEL_LOG(context, "ADD_N only supports FLOAT32 and INT8, got %s.", - TfLiteTypeGetName(output->type)); - return kTfLiteError; - } - - micro_context->DeallocateTempTfLiteTensor(input_tensor_first); - micro_context->DeallocateTempTfLiteTensor(output); - - return kTfLiteOk; -} - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - return CalculateOpData(context, node); -} - -template -inline const T** CopyInputsToScratchBuffer(TfLiteContext* context, - TfLiteNode* node, - const int scratch_index) { - int num_inputs = NumInputs(node); - void* scratch_buffer = context->GetScratchBuffer(context, scratch_index); - const T** all_inputs = static_cast(scratch_buffer); - for (int i = 0; i < num_inputs; i++) { - const TfLiteEvalTensor* next_input = - tflite::micro::GetEvalInput(context, node, kInputTensor0 + i); - all_inputs[i] = tflite::micro::GetTensorData(next_input); - } - - return all_inputs; -} - -template -void EvalAddN(TfLiteContext* context, TfLiteNode* node, - TfLiteEvalTensor* output) { - int num_inputs = NumInputs(node); - - int scratch_index = - static_cast(reinterpret_cast(node->user_data)); - const T** all_inputs = - CopyInputsToScratchBuffer(context, node, scratch_index); - - reference_ops::AddN(tflite::micro::GetTensorShape(output), num_inputs, - all_inputs, tflite::micro::GetTensorData(output)); -} - -template -void EvalAddNQuantized(TfLiteContext* context, TfLiteNode* node, - TfLiteEvalTensor* output) { - int num_inputs = NumInputs(node); - - OpData* data = static_cast(node->user_data); - const T** all_inputs = - CopyInputsToScratchBuffer(context, node, data->scratch_index); - - ArithmeticParams params; - params.left_shift = data->left_shift; - params.input1_offset = data->input_offset; - params.input1_multiplier = data->input_multiplier; - params.input1_shift = data->input_shift; - params.output_offset = data->output_offset; - params.output_multiplier = data->output_multiplier; - params.output_shift = data->output_shift; - SetActivationParams(data->output_activation_min, data->output_activation_max, - ¶ms); - - reference_ops::AddN(params, tflite::micro::GetTensorShape(output), num_inputs, - all_inputs, tflite::micro::GetTensorData(output)); -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - if (output->type == kTfLiteFloat32) { - EvalAddN(context, node, output); - } else if (output->type == kTfLiteInt8) { - EvalAddNQuantized(context, node, output); - } else { - TF_LITE_KERNEL_LOG(context, "ADD_N only supports FLOAT32 and INT8, got %s.", - TfLiteTypeGetName(output->type)); - return kTfLiteError; - } - return kTfLiteOk; -} - -} // namespace - -TfLiteRegistration Register_ADD_N() { - return tflite::micro::RegisterOp(nullptr, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/arg_min_max.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/arg_min_max.cc deleted file mode 100644 index a8aa5a48..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/arg_min_max.cc +++ /dev/null @@ -1,116 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/kernels/internal/reference/arg_min_max.h" - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/micro_utils.h" - -namespace tflite { -namespace ops { -namespace micro { -namespace arg_min_max { - -constexpr int kInputTensor = 0; -constexpr int kAxis = 1; -constexpr int kOutputTensor = 0; - -template -inline void ArgMinMaxHelper(const RuntimeShape& input1_shape, - const T1* input1_data, const T3* input2_data, - const RuntimeShape& output_shape, T2* output_data, - bool is_arg_max) { - if (is_arg_max) { - reference_ops::ArgMinMax(input1_shape, input1_data, input2_data, - output_shape, output_data, micro::Greater()); - } else { - reference_ops::ArgMinMax(input1_shape, input1_data, input2_data, - output_shape, output_data, micro::Less()); - } -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node, bool is_arg_max) { - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kInputTensor); - const TfLiteEvalTensor* axis = - tflite::micro::GetEvalInput(context, node, kAxis); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - -#define TF_LITE_ARG_MIN_MAX(data_type, axis_type, output_type) \ - ArgMinMaxHelper(tflite::micro::GetTensorShape(input), \ - tflite::micro::GetTensorData(input), \ - tflite::micro::GetTensorData(axis), \ - tflite::micro::GetTensorShape(output), \ - tflite::micro::GetTensorData(output), \ - is_arg_max) - if (axis->type == kTfLiteInt32) { - if (output->type == kTfLiteInt32) { - switch (input->type) { - case kTfLiteFloat32: - TF_LITE_ARG_MIN_MAX(float, int32_t, int32_t); - break; - case kTfLiteInt8: - TF_LITE_ARG_MIN_MAX(int8_t, int32_t, int32_t); - break; - default: - TF_LITE_KERNEL_LOG(context, - "Only float32, uint8_t and int8_t are " - "supported currently, got %s.", - TfLiteTypeGetName(input->type)); - return kTfLiteError; - } - } else { - TF_LITE_KERNEL_LOG(context, - "Only int32_t are supported currently, got %s.", - TfLiteTypeGetName(output->type)); - return kTfLiteError; - } - } else { - TF_LITE_KERNEL_LOG(context, "Only int32_t are supported currently, got %s.", - TfLiteTypeGetName(axis->type)); - return kTfLiteError; - } - -#undef TF_LITE_ARG_MIN_MAX - - return kTfLiteOk; -} - -TfLiteStatus ArgMinEval(TfLiteContext* context, TfLiteNode* node) { - return Eval(context, node, false); -} - -TfLiteStatus ArgMaxEval(TfLiteContext* context, TfLiteNode* node) { - return Eval(context, node, true); -} - -} // namespace arg_min_max - -TfLiteRegistration Register_ARG_MAX() { - return tflite::micro::RegisterOp(nullptr, nullptr, arg_min_max::ArgMaxEval); -} - -TfLiteRegistration Register_ARG_MIN() { - return tflite::micro::RegisterOp(nullptr, nullptr, arg_min_max::ArgMinEval); -} - -} // namespace micro -} // namespace ops -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/assign_variable.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/assign_variable.cc deleted file mode 100644 index a770d0aa..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/assign_variable.cc +++ /dev/null @@ -1,101 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include - -#include - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/memory_helpers.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" -#include "tensorflow/lite/micro/micro_graph.h" -#include "tensorflow/lite/micro/micro_resource_variable.h" -#include "tensorflow/lite/schema/schema_generated.h" - -namespace tflite { - -namespace { - -constexpr int kInputVariableId = 0; -constexpr int kInputValue = 1; - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - TF_LITE_ENSURE_EQ(context, NumInputs(node), 2); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 0); - - // This must be a TfLiteEvalTensor despite this being in Prepare, because - // CreateTensor allocates a temp tensor from the flatbuffer, which does not - // contain the correct ID generated within the VAR_HANDLE op. EvalTensors are - // all allocated during StartModelAllocation which happens before - // init/prepare, and VAR_HANDLE Prepare() references its own op_data in the - // TfLiteEvalTensor, so reading the ID here is valid. - const TfLiteEvalTensor* input_resource_id_tensor = - tflite::micro::GetEvalInput(context, node, kInputVariableId); - TFLITE_DCHECK(input_resource_id_tensor != nullptr); - TF_LITE_ENSURE(context, (input_resource_id_tensor->type == kTfLiteResource || - input_resource_id_tensor->type == kTfLiteInt32)); - TF_LITE_ENSURE_EQ(context, NumElements(input_resource_id_tensor->dims), 1); - - tflite::MicroContext* micro_context = tflite::GetMicroContext(context); - TfLiteTensor* input_value = - micro_context->AllocateTempInputTensor(node, kInputValue); - TFLITE_DCHECK(input_value != nullptr); - - MicroGraph& graph_info = micro_context->graph(); - - MicroResourceVariables* resources = graph_info.GetResourceVariables(); - TF_LITE_ENSURE_OK(context, - resources->Allocate(input_resource_id_tensor->data.i32[0], - context, input_value)); - - micro_context->DeallocateTempTfLiteTensor(input_value); - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input_id = - tflite::micro::GetEvalInput(context, node, kInputVariableId); - TFLITE_DCHECK(input_id != nullptr); - - const TfLiteEvalTensor* input_value = - tflite::micro::GetEvalInput(context, node, kInputValue); - TFLITE_DCHECK(input_value != nullptr); - - tflite::MicroContext* micro_context = tflite::GetMicroContext(context); - MicroGraph& graph_info = micro_context->graph(); - - MicroResourceVariables* resources = graph_info.GetResourceVariables(); - if (resources == nullptr) { - MicroPrintf( - "ASSIGN_VARIABLE requires resource variables. Please create " - "ResourceVariables and pass it to the interpreter."); - return kTfLiteError; - } - TF_LITE_ENSURE_OK(context, - resources->Assign(input_id->data.i32[0], input_value)); - return kTfLiteOk; -} - -} // namespace. - -TfLiteRegistration Register_ASSIGN_VARIABLE() { - return tflite::micro::RegisterOp(nullptr, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/batch_to_space_nd.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/batch_to_space_nd.cc deleted file mode 100644 index be82d942..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/batch_to_space_nd.cc +++ /dev/null @@ -1,111 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/kernels/internal/reference/batch_to_space_nd.h" - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/micro_utils.h" - -namespace tflite { - -namespace { - -constexpr int kInputTensor = 0; -constexpr int kBlockShapeTensor = 1; -constexpr int kCropsTensor = 2; -constexpr int kOutputTensor = 0; - -// Currently, only 3D NHC and 4D NHWC input/output op_context are supported. -// In case of 3D input, it will be extended to 3D NHWC by adding W=1. -// The 4D array need to have exactly 2 spatial dimensions. -// TODO(b/149952582): Support arbitrary dimension in SpaceToBatchND. -const int kInputOutputMinDimensionNum = 3; -const int kInputOutputMaxDimensionNum = 4; - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - TF_LITE_ENSURE_EQ(context, NumInputs(node), 3); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - - MicroContext* micro_context = GetMicroContext(context); - - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kInputTensor); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - TF_LITE_ENSURE(context, input != nullptr && output != nullptr); - - TF_LITE_ENSURE(context, NumDimensions(input) >= kInputOutputMinDimensionNum); - TF_LITE_ENSURE(context, NumDimensions(output) >= kInputOutputMinDimensionNum); - TF_LITE_ENSURE(context, NumDimensions(input) <= kInputOutputMaxDimensionNum); - TF_LITE_ENSURE(context, NumDimensions(output) <= kInputOutputMaxDimensionNum); - TF_LITE_ENSURE_TYPES_EQ(context, input->type, output->type); - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(output); - - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kInputTensor); - const TfLiteEvalTensor* block_shape = - tflite::micro::GetEvalInput(context, node, kBlockShapeTensor); - const TfLiteEvalTensor* crops = - tflite::micro::GetEvalInput(context, node, kCropsTensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - - switch (input->type) { // Already know in/out types are same. - case kTfLiteFloat32: - reference_ops::BatchToSpaceND( - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(block_shape), - tflite::micro::GetTensorData(block_shape), - tflite::micro::GetTensorShape(crops), - tflite::micro::GetTensorData(crops), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - case kTfLiteInt8: - reference_ops::BatchToSpaceND( - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(block_shape), - tflite::micro::GetTensorData(block_shape), - tflite::micro::GetTensorShape(crops), - tflite::micro::GetTensorData(crops), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - default: - TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.", - TfLiteTypeGetName(input->type), input->type); - return kTfLiteError; - } - return kTfLiteOk; -} - -} // namespace. - -TfLiteRegistration Register_BATCH_TO_SPACE_ND() { - return tflite::micro::RegisterOp(nullptr, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/broadcast_args.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/broadcast_args.cc deleted file mode 100644 index be2672ec..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/broadcast_args.cc +++ /dev/null @@ -1,91 +0,0 @@ -/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/kernels/internal/reference/broadcast_args.h" - -#include - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/micro_context.h" - -namespace tflite { -namespace { -constexpr int kShape1Tensor = 0; -constexpr int kShape2Tensor = 1; -constexpr int kOutputTensor = 0; - -TfLiteStatus BroadcastArgsPrepare(TfLiteContext* context, TfLiteNode* node) { - TF_LITE_ENSURE(context, NumInputs(node) == 2); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - - MicroContext* micro_context = GetMicroContext(context); - TfLiteTensor* shape1 = - micro_context->AllocateTempInputTensor(node, kShape1Tensor); - TfLiteTensor* shape2 = - micro_context->AllocateTempInputTensor(node, kShape2Tensor); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - - TF_LITE_ENSURE(context, - shape1->type == kTfLiteInt32 || shape1->type == kTfLiteInt64); - TF_LITE_ENSURE_EQ(context, shape1->type, shape2->type); - TF_LITE_ENSURE_EQ(context, shape1->type, output->type); - - // Ensures the shapes are 1D tensor. - TF_LITE_ENSURE_EQ(context, NumDimensions(shape1), 1); - TF_LITE_ENSURE_EQ(context, NumDimensions(shape2), 1); - - // Ensure the shape of the output tensor is compatible - TF_LITE_ENSURE_EQ(context, NumDimensions(output), 1); - - micro_context->DeallocateTempTfLiteTensor(shape1); - micro_context->DeallocateTempTfLiteTensor(shape2); - micro_context->DeallocateTempTfLiteTensor(output); - - return kTfLiteOk; -} - -TfLiteStatus BroadcastArgsEval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* shape1 = - micro::GetEvalInput(context, node, kShape1Tensor); - const TfLiteEvalTensor* shape2 = - micro::GetEvalInput(context, node, kShape2Tensor); - TfLiteEvalTensor* output = micro::GetEvalOutput(context, node, kOutputTensor); - - if (output->type == kTfLiteInt32) { - reference_ops::BroadcastArgs( - micro::GetTensorShape(shape1), micro::GetTensorData(shape1), - micro::GetTensorShape(shape2), micro::GetTensorData(shape2), - micro::GetTensorShape(output), micro::GetTensorData(output)); - } else { - reference_ops::BroadcastArgs( - micro::GetTensorShape(shape1), micro::GetTensorData(shape1), - micro::GetTensorShape(shape2), micro::GetTensorData(shape2), - micro::GetTensorShape(output), micro::GetTensorData(output)); - } - - return kTfLiteOk; -} - -} // namespace - -TfLiteRegistration Register_BROADCAST_ARGS() { - return tflite::micro::RegisterOp(nullptr, BroadcastArgsPrepare, - BroadcastArgsEval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/broadcast_to.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/broadcast_to.cc deleted file mode 100644 index 63a14db2..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/broadcast_to.cc +++ /dev/null @@ -1,123 +0,0 @@ -/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/kernels/internal/reference/broadcast_to.h" - -#include - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/micro_context.h" - -namespace tflite { - -namespace { -constexpr int kInputTensor = 0; -constexpr int kShapeTensor = 1; -constexpr int kOutputTensor = 0; -// Support a maximum of 5 dimensions in TFLM. -constexpr int kMaxDims = 5; - -TfLiteStatus ValidateOutputTensor(TfLiteContext* context, TfLiteTensor* input, - TfLiteTensor* shape, TfLiteTensor* output) { - // Ensures the shape is 1D tensor. - TF_LITE_ENSURE_EQ(context, NumDimensions(shape), 1); - - // Ensure output dims is not less than input dims. - int input_num_dims = NumDimensions(input); - int output_num_dims = NumDimensions(output); - int shape_num_dims = SizeOfDimension(shape, 0); - TF_LITE_ENSURE_MSG(context, output_num_dims == shape_num_dims, - "Output must match with the expected shape dimension."); - TF_LITE_ENSURE_MSG(context, input_num_dims <= output_num_dims, - "Output shape must be broadcastable from input shape."); - TF_LITE_ENSURE_MSG(context, output_num_dims <= kMaxDims, - "BroadcastTo only supports 1-5D tensor."); - - // Check if output shape is broadcastable from input shape. - auto get_shape_data = [shape](int i) -> int32_t { - if (shape->type == kTfLiteInt32) { - return GetTensorData(shape)[i]; - } else { - return GetTensorData(shape)[i]; - } - }; - - int extending_dims = output_num_dims - input_num_dims; - for (int idx = 0; idx < input_num_dims; ++idx) { - TF_LITE_ENSURE_MSG( - context, - (SizeOfDimension(input, idx) == 1 || - SizeOfDimension(input, idx) == get_shape_data(extending_dims + idx)), - "Output shape must be broadcastable from input shape."); - } - - // Validating the shape of the output tensor. - tflite::RuntimeShape output_shape = tflite::GetTensorShape(output); - for (int idx = 0; idx < output_num_dims; ++idx) { - TF_LITE_ENSURE(context, output_shape.Dims(idx) == get_shape_data(idx)); - } - return kTfLiteOk; -} - -TfLiteStatus BroadcastToPrepare(TfLiteContext* context, TfLiteNode* node) { - TF_LITE_ENSURE(context, NumInputs(node) == 2); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - MicroContext* micro_context = GetMicroContext(context); - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kInputTensor); - TfLiteTensor* shape = - micro_context->AllocateTempInputTensor(node, kShapeTensor); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - - TF_LITE_ENSURE_MSG(context, (NumDimensions(input) <= kMaxDims), - "BroadcastTo only supports 1-5D tensor."); - - TF_LITE_ENSURE(context, - shape->type == kTfLiteInt32 || shape->type == kTfLiteInt64); - TF_LITE_ENSURE_EQ(context, input->type, output->type); - - // Does not support String type due to its variable size. This limitation is - // the same as TFLite. - TF_LITE_ENSURE(context, input->type != kTfLiteString); - - TF_LITE_ENSURE_STATUS(ValidateOutputTensor(context, input, shape, output)); - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(shape); - micro_context->DeallocateTempTfLiteTensor(output); - return kTfLiteOk; -} - -TfLiteStatus BroadcastToEval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input = - micro::GetEvalInput(context, node, kInputTensor); - TfLiteEvalTensor* output = micro::GetEvalOutput(context, node, kOutputTensor); - - // BroadcastTo op support upto 5 dims, different from 8 dims in TFLite. - reference_ops::BroadcastTo( - micro::GetTensorShape(input), input->data.raw, - micro::GetTensorShape(output), output->data.raw, input->type); - return kTfLiteOk; -} -} // namespace - -TfLiteRegistration Register_BROADCAST_TO() { - return tflite::micro::RegisterOp(nullptr, BroadcastToPrepare, - BroadcastToEval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/call_once.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/call_once.cc deleted file mode 100644 index 200242b2..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/call_once.cc +++ /dev/null @@ -1,88 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include - -#include - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/memory_helpers.h" -#include "tensorflow/lite/micro/micro_context.h" -#include "tensorflow/lite/micro/micro_graph.h" -#include "tensorflow/lite/schema/schema_generated.h" - -namespace tflite { - -namespace { - -struct OpData { - int init_subgraph_index; - bool has_run; -}; - -void* Init(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(OpData)); -} - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - OpData* op_data = reinterpret_cast(node->user_data); - const auto* params = - reinterpret_cast(node->builtin_data); - op_data->init_subgraph_index = params->init_subgraph_index; - op_data->has_run = false; - - TF_LITE_ENSURE(context, NumInputs(node) == 0); - TF_LITE_ENSURE(context, NumOutputs(node) == 0); - - tflite::MicroContext* micro_context = tflite::GetMicroContext(context); - MicroGraph& graph_info = micro_context->graph(); - - TF_LITE_ENSURE(context, - op_data->init_subgraph_index < graph_info.NumSubgraphs()); - - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - OpData* op_data = reinterpret_cast(node->user_data); - - // Call once only runs one time then is a no-op for every subsequent call. - if (op_data->has_run) { - return kTfLiteOk; - } - - tflite::MicroContext* micro_context = tflite::GetMicroContext(context); - MicroGraph& graph_info = micro_context->graph(); - - TF_LITE_ENSURE_OK(context, - graph_info.InvokeSubgraph(op_data->init_subgraph_index)); - - op_data->has_run = true; - - return kTfLiteOk; -} - -} // namespace. - -TfLiteRegistration Register_CALL_ONCE() { - return tflite::micro::RegisterOp(Init, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/cast.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/cast.cc deleted file mode 100644 index a1f4516b..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/cast.cc +++ /dev/null @@ -1,114 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" - -namespace tflite { -namespace { - -constexpr int kInputTensor = 0; -constexpr int kOutputTensor = 0; - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - TF_LITE_ENSURE_EQ(context, NumInputs(node), 1); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - - MicroContext* micro_context = GetMicroContext(context); - - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(output); - - return kTfLiteOk; -} - -template -void copyCast(const FromT* in, ToT* out, int num_elements) { - std::transform(in, in + num_elements, out, - [](FromT a) { return static_cast(a); }); -} - -template -TfLiteStatus copyToTensor(TfLiteContext* context, const FromT* in, - TfLiteEvalTensor* out, int num_elements) { - switch (out->type) { - case kTfLiteInt8: - copyCast(in, out->data.int8, num_elements); - break; - case kTfLiteInt16: - copyCast(in, out->data.i16, num_elements); - break; - case kTfLiteInt32: - copyCast(in, out->data.i32, num_elements); - break; - case kTfLiteFloat32: - copyCast(in, tflite::micro::GetTensorData(out), num_elements); - break; - default: - // Unsupported type. - MicroPrintf("Output type %s (%d) not supported.", - TfLiteTypeGetName(out->type), out->type); - } - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kInputTensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - int num_elements = MatchingFlatSize(tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorShape(output)); - - switch (input->type) { - case kTfLiteInt8: - return copyToTensor(context, input->data.int8, output, num_elements); - case kTfLiteInt16: - return copyToTensor(context, tflite::micro::GetTensorData(input), - output, num_elements); - case kTfLiteInt32: - return copyToTensor(context, tflite::micro::GetTensorData(input), - output, num_elements); - case kTfLiteUInt32: - return copyToTensor(context, - tflite::micro::GetTensorData(input), output, - num_elements); - case kTfLiteFloat32: - return copyToTensor(context, tflite::micro::GetTensorData(input), - output, num_elements); - default: - // Unsupported type. - MicroPrintf("Input type %s (%d) not supported.", - TfLiteTypeGetName(input->type), input->type); - } - return kTfLiteOk; -} -} // namespace - -TfLiteRegistration Register_CAST() { - return tflite::micro::RegisterOp(nullptr, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/ceil.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/ceil.cc deleted file mode 100644 index a390a735..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/ceil.cc +++ /dev/null @@ -1,75 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/kernels/internal/reference/ceil.h" - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { -namespace ops { -namespace micro { -namespace ceil { - -constexpr int kInputTensor = 0; -constexpr int kOutputTensor = 0; - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - TF_LITE_ENSURE_EQ(context, NumInputs(node), 1); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - TF_LITE_ENSURE_TYPES_EQ(context, input->type, kTfLiteFloat32); - TF_LITE_ENSURE_TYPES_EQ(context, output->type, input->type); - TF_LITE_ENSURE_EQ(context, output->bytes, input->bytes); - TF_LITE_ENSURE_EQ(context, output->dims->size, input->dims->size); - for (int i = 0; i < output->dims->size; ++i) { - TF_LITE_ENSURE_EQ(context, output->dims->data[i], input->dims->data[i]); - } - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(output); - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kInputTensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - - reference_ops::Ceil(tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - - return kTfLiteOk; -} -} // namespace ceil - -TfLiteRegistration Register_CEIL() { - return tflite::micro::RegisterOp(nullptr, ceil::Prepare, ceil::Eval); -} - -} // namespace micro -} // namespace ops -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/circular_buffer.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/circular_buffer.cc deleted file mode 100644 index 399d1648..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/circular_buffer.cc +++ /dev/null @@ -1,116 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/kernels/circular_buffer.h" - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/flatbuffer_utils.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -/* - * The circular buffer custom operator is used to implement strided streaming - * convolutions on TFLite Micro. Each time this operator is invoked, it checks - * whether or not to run, based on a predetermined stride in time. If the op - * runs, it inserts the input into the end of the output buffer and shifts the - * output values towards the start of the buffer. It discards the oldest value - * in the output buffer. - * - * Input: [, , , ] - * - * After shifting: - * Output: [, , , ] - * - * We make some assumptions in this custom operator: - * - Input shape must be [1, 1, 1, depth] - * - Output shape must be [1, num_slots, 1, depth] - * - Input and output types must match. - * - Input and output quantization params must be identical. - */ -namespace tflite { - -void* CircularBufferInit(TfLiteContext* context, const char* buffer, - size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - OpDataCircularBuffer* op_data = static_cast( - context->AllocatePersistentBuffer(context, sizeof(OpDataCircularBuffer))); - - if (buffer != nullptr && length > 0) { - const uint8_t* buffer_t = reinterpret_cast(buffer); - tflite::FlexbufferWrapper wrapper(buffer_t, length); - op_data->cycles_max = wrapper.ElementAsInt32(kCircularBufferCyclesMaxIndex); - } else { - op_data->cycles_max = 0; - } - - return op_data; -} - -// Shifts buffer over by the output depth, and write new input to end of buffer. -// num_slots is the number of samples stored in the output buffer. -// depth is the size of each sample. -void EvalInt8(const int8_t* input, int num_slots, int depth, int8_t* output) { - memmove(output, &output[depth], (num_slots - 1) * depth); - memcpy(&output[(num_slots - 1) * depth], input, depth); -} - -TfLiteStatus CircularBufferEval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kCircularBufferInputTensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kCircularBufferOutputTensor); - - TFLITE_DCHECK(node->user_data != nullptr); - OpDataCircularBuffer* data = - reinterpret_cast(node->user_data); - - int num_slots = output->dims->data[1]; - int depth = output->dims->data[2] * output->dims->data[3]; - - if (input->type == kTfLiteInt8) { - EvalInt8(tflite::micro::GetTensorData(input), num_slots, depth, - tflite::micro::GetTensorData(output)); - } else { - TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.", - TfLiteTypeGetName(input->type), input->type); - return kTfLiteError; - } - - if (--data->cycles_until_run != 0) { - // Signal the interpreter to end current run if the delay before op invoke - // has not been reached. - // TODO(b/149795762): Add kTfLiteAbort to TfLiteStatus enum. - return static_cast(kTfLiteAbort); - } - - data->cycles_until_run = data->cycles_max; - - return kTfLiteOk; -} - -TfLiteRegistration* Register_CIRCULAR_BUFFER() { - static TfLiteRegistration r = tflite::micro::RegisterOp( - CircularBufferInit, CircularBufferPrepare, CircularBufferEval); - return &r; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/circular_buffer.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/circular_buffer.h deleted file mode 100644 index 51adf746..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/circular_buffer.h +++ /dev/null @@ -1,48 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_CIRCULAR_BUFFER_H_ -#define TENSORFLOW_LITE_MICRO_KERNELS_CIRCULAR_BUFFER_H_ - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" - -namespace tflite { - -// The CircularBuffer op has one input and one output tensor. -extern const int kCircularBufferInputTensor; -extern const int kCircularBufferOutputTensor; - -// Indices into the init flexbuffer's vector. -// The parameter's name is in the comment that follows. -// Elements in the vectors are ordered alphabetically by parameter name. -extern const int kCircularBufferCyclesMaxIndex; // 'cycles_max' - -// TODO(b/149795762): Add this to TfLiteStatus enum. -extern const TfLiteStatus kTfLiteAbort; - -// These fields control the stride period of a strided streaming model. This op -// returns kTfLiteAbort until cycles_until_run-- is zero. At this time, -// cycles_until_run is reset to cycles_max. -struct OpDataCircularBuffer { - int cycles_until_run; - int cycles_max; -}; - -TfLiteStatus CircularBufferPrepare(TfLiteContext* context, TfLiteNode* node); - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_KERNELS_CIRCULAR_BUFFER_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/circular_buffer_common.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/circular_buffer_common.cc deleted file mode 100644 index 81db6e65..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/circular_buffer_common.cc +++ /dev/null @@ -1,97 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/flatbuffer_utils.h" -#include "tensorflow/lite/micro/kernels/circular_buffer.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { - -// The CircularBuffer op has one input and one output tensor. -const int kCircularBufferInputTensor = 0; -const int kCircularBufferOutputTensor = 0; - -// Indices into the init flexbuffer's vector. -// The parameter's name is in the comment that follows. -// Elements in the vectors are ordered alphabetically by parameter name. -const int kCircularBufferCyclesMaxIndex = 0; // 'cycles_max' - -// TODO(b/149795762): Add this to TfLiteStatus enum. -const TfLiteStatus kTfLiteAbort = static_cast(-9); - -TfLiteStatus CircularBufferPrepare(TfLiteContext* context, TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kCircularBufferInputTensor); - TfLiteTensor* output = micro_context->AllocateTempOutputTensor( - node, kCircularBufferOutputTensor); - - TFLITE_DCHECK(node->user_data != nullptr); - OpDataCircularBuffer* op_data = - static_cast(node->user_data); - - TF_LITE_ENSURE(context, input != nullptr); - TF_LITE_ENSURE(context, output != nullptr); - TF_LITE_ENSURE_EQ(context, input->dims->data[0], output->dims->data[0]); - TF_LITE_ENSURE_EQ(context, 1, input->dims->data[1]); - TF_LITE_ENSURE_EQ(context, input->dims->data[2], output->dims->data[2]); - TF_LITE_ENSURE_EQ(context, output->dims->data[3], input->dims->data[3]); - - TF_LITE_ENSURE_TYPES_EQ(context, input->type, output->type); - - // The circular buffer custom operator currently only supports int8. - TF_LITE_ENSURE_TYPES_EQ(context, input->type, kTfLiteInt8); - - if (op_data->cycles_max <= 0) { - // The last circular buffer layer simply accumulates outputs, and does not - // run periodically. - // TODO(b/150001379): Move this special case logic to the tflite flatbuffer. - static int cb_prepare_count = 0; - cb_prepare_count++; - // These checks specifically work for the only two streaming models - // supported on TFLM. They use the shape of the output tensor along with the - // layer number to determine if the circular buffer period should be 1 or 2. - - // These models are outlined int the following documents: - // https://docs.google.com/document/d/1lc_G2ZFhjiKFo02UHjBaljye1xsL0EkfybkaVELEE3Q/edit?usp=sharing - // https://docs.google.com/document/d/1pGc42PuWyrk-Jy1-9qeqtggvsmHr1ifz8Lmqfpr2rKA/edit?usp=sharing - if (output->dims->data[1] == 5 || output->dims->data[1] == 13 || - output->dims->data[1] == 25 || - (cb_prepare_count == 5 && output->dims->data[2] == 2 && - output->dims->data[3] == 96)) { - op_data->cycles_max = 1; - cb_prepare_count = 0; - } else { - op_data->cycles_max = 2; - } - } - op_data->cycles_until_run = op_data->cycles_max; - node->user_data = op_data; - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(output); - - return kTfLiteOk; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/circular_buffer_flexbuffers_generated_data.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/circular_buffer_flexbuffers_generated_data.h deleted file mode 100644 index 2fbf4fe9..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/circular_buffer_flexbuffers_generated_data.h +++ /dev/null @@ -1,22 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_FLEXBUFFERS_GENERATED_DATA_H -#define TENSORFLOW_LITE_MICRO_KERNELS_FLEXBUFFERS_GENERATED_DATA_H - -extern const int g_gen_data_size_circular_buffer_config; -extern const unsigned char g_gen_data_circular_buffer_config[]; - -#endif diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/comparisons.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/comparisons.cc deleted file mode 100644 index cff15e4d..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/comparisons.cc +++ /dev/null @@ -1,617 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/kernels/internal/reference/comparisons.h" - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { -namespace ops { -namespace micro { -namespace comparisons { -namespace { - -struct OpData { - ComparisonParams params; -}; - -constexpr int kInputTensor1 = 0; -constexpr int kInputTensor2 = 1; -constexpr int kOutputTensor = 0; - -TfLiteStatus EqualEval(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - const OpData* data = static_cast(node->user_data); - - const TfLiteEvalTensor* input1 = - tflite::micro::GetEvalInput(context, node, kInputTensor1); - const TfLiteEvalTensor* input2 = - tflite::micro::GetEvalInput(context, node, kInputTensor2); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - - RuntimeShape input1_shape = tflite::micro::GetTensorShape(input1); - RuntimeShape input2_shape = tflite::micro::GetTensorShape(input2); - RuntimeShape output_shape = tflite::micro::GetTensorShape(output); - bool* output_data = tflite::micro::GetTensorData(output); - - bool requires_broadcast = !tflite::micro::HaveSameShapes(input1, input2); - switch (input1->type) { - case kTfLiteBool: - requires_broadcast - ? reference_ops::Broadcast4DSlowEqualNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data) - : reference_ops::EqualNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data); - break; - case kTfLiteFloat32: - requires_broadcast - ? reference_ops::Broadcast4DSlowEqualNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data) - : reference_ops::EqualNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data); - break; - case kTfLiteInt32: - requires_broadcast - ? reference_ops::Broadcast4DSlowEqualNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data) - : reference_ops::EqualNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data); - break; - case kTfLiteInt64: - requires_broadcast - ? reference_ops::Broadcast4DSlowEqualNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data) - : reference_ops::EqualNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data); - break; - case kTfLiteInt8: - requires_broadcast - ? reference_ops::Broadcast4DSlowEqualWithScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data) - : reference_ops::EqualWithScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data); - break; - default: - TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.", - TfLiteTypeGetName(input1->type), input1->type); - return kTfLiteError; - } - return kTfLiteOk; -} - -// TODO(renjieliu): Refactor the logic to avoid duplications. -TfLiteStatus NotEqualEval(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - const OpData* data = static_cast(node->user_data); - - const TfLiteEvalTensor* input1 = - tflite::micro::GetEvalInput(context, node, kInputTensor1); - const TfLiteEvalTensor* input2 = - tflite::micro::GetEvalInput(context, node, kInputTensor2); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - - RuntimeShape input1_shape = tflite::micro::GetTensorShape(input1); - RuntimeShape input2_shape = tflite::micro::GetTensorShape(input2); - RuntimeShape output_shape = tflite::micro::GetTensorShape(output); - bool* output_data = tflite::micro::GetTensorData(output); - - bool requires_broadcast = !tflite::micro::HaveSameShapes(input1, input2); - switch (input1->type) { - case kTfLiteBool: - requires_broadcast - ? reference_ops::Broadcast4DSlowNotEqualNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data) - : reference_ops::NotEqualNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data); - break; - case kTfLiteFloat32: - requires_broadcast - ? reference_ops::Broadcast4DSlowNotEqualNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data) - : reference_ops::NotEqualNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data); - break; - case kTfLiteInt32: - requires_broadcast - ? reference_ops::Broadcast4DSlowNotEqualNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data) - : reference_ops::NotEqualNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data); - break; - case kTfLiteInt64: - requires_broadcast - ? reference_ops::Broadcast4DSlowNotEqualNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data) - : reference_ops::NotEqualNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data); - break; - case kTfLiteInt8: - requires_broadcast - ? reference_ops::Broadcast4DSlowNotEqualWithScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data) - : reference_ops::NotEqualWithScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data); - break; - default: - TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.", - TfLiteTypeGetName(input1->type), input1->type); - return kTfLiteError; - } - return kTfLiteOk; -} - -TfLiteStatus GreaterEval(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - const OpData* data = static_cast(node->user_data); - - const TfLiteEvalTensor* input1 = - tflite::micro::GetEvalInput(context, node, kInputTensor1); - const TfLiteEvalTensor* input2 = - tflite::micro::GetEvalInput(context, node, kInputTensor2); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - - RuntimeShape input1_shape = tflite::micro::GetTensorShape(input1); - RuntimeShape input2_shape = tflite::micro::GetTensorShape(input2); - RuntimeShape output_shape = tflite::micro::GetTensorShape(output); - bool* output_data = tflite::micro::GetTensorData(output); - - bool requires_broadcast = !tflite::micro::HaveSameShapes(input1, input2); - switch (input1->type) { - case kTfLiteFloat32: - requires_broadcast - ? reference_ops::Broadcast4DSlowGreaterNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data) - : reference_ops::GreaterNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data); - break; - case kTfLiteInt32: - requires_broadcast - ? reference_ops::Broadcast4DSlowGreaterNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data) - : reference_ops::GreaterNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data); - break; - case kTfLiteInt64: - requires_broadcast - ? reference_ops::Broadcast4DSlowGreaterNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data) - : reference_ops::GreaterNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data); - break; - case kTfLiteInt8: - requires_broadcast - ? reference_ops::Broadcast4DSlowGreaterWithScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data) - : reference_ops::GreaterWithScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data); - break; - default: - TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.", - TfLiteTypeGetName(input1->type), input1->type); - return kTfLiteError; - } - return kTfLiteOk; -} - -TfLiteStatus GreaterEqualEval(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - const OpData* data = static_cast(node->user_data); - - const TfLiteEvalTensor* input1 = - tflite::micro::GetEvalInput(context, node, kInputTensor1); - const TfLiteEvalTensor* input2 = - tflite::micro::GetEvalInput(context, node, kInputTensor2); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - - RuntimeShape input1_shape = tflite::micro::GetTensorShape(input1); - RuntimeShape input2_shape = tflite::micro::GetTensorShape(input2); - RuntimeShape output_shape = tflite::micro::GetTensorShape(output); - bool* output_data = tflite::micro::GetTensorData(output); - - bool requires_broadcast = !tflite::micro::HaveSameShapes(input1, input2); - switch (input1->type) { - case kTfLiteFloat32: - requires_broadcast - ? reference_ops::Broadcast4DSlowGreaterEqualNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data) - : reference_ops::GreaterEqualNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data); - break; - case kTfLiteInt32: - requires_broadcast - ? reference_ops::Broadcast4DSlowGreaterEqualNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data) - : reference_ops::GreaterEqualNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data); - break; - case kTfLiteInt64: - requires_broadcast - ? reference_ops::Broadcast4DSlowGreaterEqualNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data) - : reference_ops::GreaterEqualNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data); - break; - case kTfLiteInt8: - requires_broadcast - ? reference_ops::Broadcast4DSlowGreaterEqualWithScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data) - : reference_ops::GreaterEqualWithScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data); - break; - default: - TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.", - TfLiteTypeGetName(input1->type), input1->type); - return kTfLiteError; - } - return kTfLiteOk; -} - -TfLiteStatus LessEval(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - const OpData* data = static_cast(node->user_data); - - const TfLiteEvalTensor* input1 = - tflite::micro::GetEvalInput(context, node, kInputTensor1); - const TfLiteEvalTensor* input2 = - tflite::micro::GetEvalInput(context, node, kInputTensor2); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - - RuntimeShape input1_shape = tflite::micro::GetTensorShape(input1); - RuntimeShape input2_shape = tflite::micro::GetTensorShape(input2); - RuntimeShape output_shape = tflite::micro::GetTensorShape(output); - bool* output_data = tflite::micro::GetTensorData(output); - - bool requires_broadcast = !tflite::micro::HaveSameShapes(input1, input2); - switch (input1->type) { - case kTfLiteFloat32: - requires_broadcast - ? reference_ops::Broadcast4DSlowLessNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data) - : reference_ops::LessNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data); - break; - case kTfLiteInt32: - requires_broadcast - ? reference_ops::Broadcast4DSlowLessNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data) - : reference_ops::LessNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data); - break; - case kTfLiteInt64: - requires_broadcast - ? reference_ops::Broadcast4DSlowLessNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data) - : reference_ops::LessNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data); - break; - case kTfLiteInt8: - requires_broadcast - ? reference_ops::Broadcast4DSlowLessWithScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data) - : reference_ops::LessWithScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data); - break; - default: - TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.", - TfLiteTypeGetName(input1->type), input1->type); - return kTfLiteError; - } - return kTfLiteOk; -} - -TfLiteStatus LessEqualEval(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - const OpData* data = static_cast(node->user_data); - - const TfLiteEvalTensor* input1 = - tflite::micro::GetEvalInput(context, node, kInputTensor1); - const TfLiteEvalTensor* input2 = - tflite::micro::GetEvalInput(context, node, kInputTensor2); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - - RuntimeShape input1_shape = tflite::micro::GetTensorShape(input1); - RuntimeShape input2_shape = tflite::micro::GetTensorShape(input2); - RuntimeShape output_shape = tflite::micro::GetTensorShape(output); - bool* output_data = tflite::micro::GetTensorData(output); - - bool requires_broadcast = !tflite::micro::HaveSameShapes(input1, input2); - switch (input1->type) { - case kTfLiteFloat32: - requires_broadcast - ? reference_ops::Broadcast4DSlowLessEqualNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data) - : reference_ops::LessEqualNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data); - break; - case kTfLiteInt32: - requires_broadcast - ? reference_ops::Broadcast4DSlowLessEqualNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data) - : reference_ops::LessEqualNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data); - break; - case kTfLiteInt64: - requires_broadcast - ? reference_ops::Broadcast4DSlowLessEqualNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data) - : reference_ops::LessEqualNoScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data); - break; - case kTfLiteInt8: - requires_broadcast - ? reference_ops::Broadcast4DSlowLessEqualWithScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data) - : reference_ops::LessEqualWithScaling( - data->params, input1_shape, - tflite::micro::GetTensorData(input1), input2_shape, - tflite::micro::GetTensorData(input2), output_shape, - output_data); - break; - default: - TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.", - TfLiteTypeGetName(input1->type), input1->type); - return kTfLiteError; - } - return kTfLiteOk; -} - -} // namespace - -void* Init(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(OpData)); -} - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - OpData* data = static_cast(node->user_data); - - MicroContext* micro_context = GetMicroContext(context); - - TfLiteTensor* input1 = - micro_context->AllocateTempInputTensor(node, kInputTensor1); - TF_LITE_ENSURE(context, input1 != nullptr); - TfLiteTensor* input2 = - micro_context->AllocateTempInputTensor(node, kInputTensor2); - TF_LITE_ENSURE(context, input2 != nullptr); - - if (input1->type == kTfLiteInt8) { - auto input1_offset = -input1->params.zero_point; - auto input2_offset = -input2->params.zero_point; - const int kLeftShift = 8; - - int32_t input1_multiplier; - int input1_shift; - QuantizeMultiplierSmallerThanOneExp( - static_cast(input1->params.scale), &input1_multiplier, - &input1_shift); - int32_t input2_multiplier; - int input2_shift; - QuantizeMultiplierSmallerThanOneExp( - static_cast(input2->params.scale), &input2_multiplier, - &input2_shift); - - data->params.left_shift = kLeftShift; - data->params.input1_offset = input1_offset; - data->params.input1_multiplier = input1_multiplier; - data->params.input1_shift = input1_shift; - data->params.input2_offset = input2_offset; - data->params.input2_multiplier = input2_multiplier; - data->params.input2_shift = input2_shift; - } - - micro_context->DeallocateTempTfLiteTensor(input1); - micro_context->DeallocateTempTfLiteTensor(input2); - - return kTfLiteOk; -} - -} // namespace comparisons - -TfLiteRegistration Register_EQUAL() { - return tflite::micro::RegisterOp(comparisons::Init, comparisons::Prepare, - comparisons::EqualEval); -} - -TfLiteRegistration Register_NOT_EQUAL() { - return tflite::micro::RegisterOp(comparisons::Init, comparisons::Prepare, - comparisons::NotEqualEval); -} - -TfLiteRegistration Register_GREATER() { - return tflite::micro::RegisterOp(comparisons::Init, comparisons::Prepare, - comparisons::GreaterEval); -} - -TfLiteRegistration Register_GREATER_EQUAL() { - return tflite::micro::RegisterOp(comparisons::Init, comparisons::Prepare, - comparisons::GreaterEqualEval); -} - -TfLiteRegistration Register_LESS() { - return tflite::micro::RegisterOp(comparisons::Init, comparisons::Prepare, - comparisons::LessEval); -} - -TfLiteRegistration Register_LESS_EQUAL() { - return tflite::micro::RegisterOp(comparisons::Init, comparisons::Prepare, - comparisons::LessEqualEval); -} - -} // namespace micro -} // namespace ops -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/concatenation.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/concatenation.cc deleted file mode 100644 index 34622c22..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/concatenation.cc +++ /dev/null @@ -1,261 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/kernels/internal/reference/concatenation.h" - -#include - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/portable_tensor.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/internal/types.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { -namespace ops { -namespace micro { -namespace concatenation { - -constexpr int kMaxInputNum = 10; // Maximum number of input tensors -constexpr int kOutputTensor = 0; - -struct OpData { - ConcatenationParams params; -}; - -// Handles negative axis index, coerces to positive index value. -inline int CalculatePositiveAxis(int axis, const TfLiteTensor* output_tensor) { - if (axis >= 0) { - return axis; - } else { - return NumDimensions(output_tensor) + axis; - } -} - -// The following functions are helpers to get tensor data in the format that the -// reference op implementation expects. They provide the same functionality as -// class VectorOfTensors and class VectorOfQuantizedTensors in TFLite. - -// Gets shapes from a list of tensors. -inline void GetAllInputTensorShapes(const TfLiteContext* context, - const TfLiteNode* node, - RuntimeShape all_shapes[kMaxInputNum]) { - TFLITE_DCHECK(context != nullptr); - TFLITE_DCHECK(node != nullptr); - for (int i = 0; i < node->inputs->size; ++i) { - const TfLiteEvalTensor* t = tflite::micro::GetEvalInput(context, node, i); - RuntimeShape shape = tflite::micro::GetTensorShape(t); - all_shapes[i].ReplaceWith(shape.DimensionsCount(), shape.DimsData()); - } -} - -// Get shape pointers from a list of shapes. -inline void GetShapesPointers(const RuntimeShape* shapes, size_t num, - const RuntimeShape* pointers[]) { - for (size_t i = 0; i < num; ++i) { - pointers[i] = &shapes[i]; - } -} - -// Gets data pointers from a list of tensors. -template -inline void GetAllInputTensorData(const TfLiteContext* context, - const TfLiteNode* node, - T* all_data[kMaxInputNum]) { - TFLITE_DCHECK(context != nullptr); - TFLITE_DCHECK(node != nullptr); - for (int i = 0; i < node->inputs->size; ++i) { - const TfLiteEvalTensor* t = tflite::micro::GetEvalInput(context, node, i); - all_data[i] = tflite::micro::GetTensorData(t); - } -} - -template -void EvalUnquantized(TfLiteContext* context, TfLiteNode* node) { - // Collect the shapes and data pointer of input tensors - RuntimeShape inputs_shape[kMaxInputNum]; - const RuntimeShape* inputs_shape_ptr[kMaxInputNum]; - const data_type* inputs_data[kMaxInputNum]; - GetAllInputTensorShapes(context, node, inputs_shape); - GetShapesPointers(inputs_shape, node->inputs->size, inputs_shape_ptr); - GetAllInputTensorData(context, node, inputs_data); - - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - - TFLITE_DCHECK(node->user_data != nullptr); - const OpData* data = static_cast(node->user_data); - - reference_ops::Concatenation(data->params, inputs_shape_ptr, inputs_data, - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); -} - -void* Init(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(OpData)); -} - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - // This function only checks the types. Additional shape validations are - // performed in the reference implementation called during Eval(). - const TfLiteConcatenationParams* params = - reinterpret_cast(node->builtin_data); - - MicroContext* micro_context = GetMicroContext(context); - - TfLiteTensor* input_tensor = micro_context->AllocateTempInputTensor(node, 0); - TF_LITE_ENSURE(context, input_tensor != nullptr); - TfLiteType input_type = input_tensor->type; - TfLiteTensor* output_tensor = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - TF_LITE_ENSURE(context, output_tensor != nullptr); - TfLiteType output_type = output_tensor->type; - - micro_context->DeallocateTempTfLiteTensor(input_tensor); - micro_context->DeallocateTempTfLiteTensor(output_tensor); - - // Check activation and input type - TF_LITE_ENSURE_EQ(context, params->activation, kTfLiteActNone); - TF_LITE_ENSURE(context, - input_type == kTfLiteFloat32 || input_type == kTfLiteInt8 || - input_type == kTfLiteInt16 || input_type == kTfLiteInt32 || - input_type == kTfLiteInt64); - - // Output type must match input type - TF_LITE_ENSURE_EQ(context, output_type, input_type); - - // This implementation does not support large number of input tensors - const int num_inputs = NumInputs(node); - TF_LITE_ENSURE(context, num_inputs <= kMaxInputNum); - - // Shapes with dimensions >4 are not yet supported with static allocation. - for (int i = 0; i < num_inputs; ++i) { - TfLiteTensor* input = micro_context->AllocateTempInputTensor(node, i); - TF_LITE_ENSURE(context, input != nullptr); - int num_dimensions = NumDimensions(input); - - if (num_dimensions > RuntimeShape::kMaxSmallSize) { - TF_LITE_KERNEL_LOG( - context, - "Op Concatenation does not currently support num dimensions > %d " - "Tensor has %d dimensions.", - RuntimeShape::kMaxSmallSize, num_dimensions); - return kTfLiteError; - } - micro_context->DeallocateTempTfLiteTensor(input); - } - - // Calculate OpData. - TFLITE_DCHECK(node->user_data != nullptr); - OpData* data = static_cast(node->user_data); - - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - - switch (output_type) { // Already know in/outtypes are same. - case kTfLiteFloat32: - case kTfLiteInt16: - case kTfLiteInt32: - case kTfLiteInt64: { - data->params.axis = CalculatePositiveAxis(params->axis, output); - data->params.inputs_count = node->inputs->size; - break; - } - case kTfLiteInt8: { - data->params.axis = CalculatePositiveAxis(params->axis, output); - data->params.inputs_count = node->inputs->size; - - float* input_scales = - reinterpret_cast(context->AllocatePersistentBuffer( - context, node->inputs->size * sizeof(float))); - - int32_t* input_zero_points = - reinterpret_cast(context->AllocatePersistentBuffer( - context, node->inputs->size * sizeof(int32_t))); - - // Allocate persistent scale and zeropoint buffers. - // Store input scale and zero point values in OpParams: - for (int i = 0; i < node->inputs->size; ++i) { - TfLiteTensor* t = micro_context->AllocateTempInputTensor(node, i); - TF_LITE_ENSURE(context, t != nullptr); - input_scales[i] = t->params.scale; - input_zero_points[i] = t->params.zero_point; - micro_context->DeallocateTempTfLiteTensor(t); - } - - data->params.input_scale = input_scales; - data->params.input_zeropoint = input_zero_points; - data->params.output_zeropoint = output->params.zero_point; - data->params.output_scale = output->params.scale; - break; - } - default: - TF_LITE_KERNEL_LOG( - context, "Op Concatenation does not currently support Type '%s'.", - TfLiteTypeGetName(output_type)); - return kTfLiteError; - } - - micro_context->DeallocateTempTfLiteTensor(output); - - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* output_tensor = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - TF_LITE_ENSURE(context, output_tensor != nullptr); - TfLiteType output_type = output_tensor->type; - - switch (output_type) { // Already know in/outtypes are same. - case kTfLiteFloat32: - EvalUnquantized(context, node); - break; - case kTfLiteInt32: - EvalUnquantized(context, node); - break; - case kTfLiteInt8: - EvalUnquantized(context, node); - break; - case kTfLiteInt64: - EvalUnquantized(context, node); - break; - case kTfLiteInt16: - EvalUnquantized(context, node); - break; - - default: - TF_LITE_KERNEL_LOG( - context, "Op Concatenation does not currently support Type '%s'.", - TfLiteTypeGetName(output_type)); - return kTfLiteError; - } - - return kTfLiteOk; -} - -} // namespace concatenation - -TfLiteRegistration Register_CONCATENATION() { - return tflite::micro::RegisterOp(concatenation::Init, concatenation::Prepare, - concatenation::Eval); -} - -} // namespace micro -} // namespace ops -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/conv.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/conv.cc deleted file mode 100644 index 87ea92e6..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/conv.cc +++ /dev/null @@ -1,141 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/kernels/conv.h" - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/conv.h" -#include "tensorflow/lite/kernels/internal/reference/integer_ops/conv.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/padding.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" - -namespace tflite { -namespace { - -void* Init(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(OpDataConv)); -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kConvInputTensor); - const TfLiteEvalTensor* filter = - tflite::micro::GetEvalInput(context, node, kConvWeightsTensor); - const TfLiteEvalTensor* bias = - (NumInputs(node) == 3) - ? tflite::micro::GetEvalInput(context, node, kConvBiasTensor) - : nullptr; - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kConvOutputTensor); - - TFLITE_DCHECK(node->builtin_data != nullptr); - const auto& params = - *(reinterpret_cast(node->builtin_data)); - TFLITE_DCHECK(node->user_data != nullptr); - const auto& data = *(static_cast(node->user_data)); - - TF_LITE_ENSURE_EQ(context, input->type, output->type); - TF_LITE_ENSURE_MSG( - context, - input->type == filter->type || - (input->type == kTfLiteInt16 && filter->type == kTfLiteInt8), - "Hybrid models are not supported on TFLite Micro."); - - switch (input->type) { // Already know in/out types are same. - case kTfLiteFloat32: { - tflite::reference_ops::Conv( - ConvParamsFloat(params, data), tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(filter), - tflite::micro::GetTensorData(filter), - tflite::micro::GetTensorShape(bias), - tflite::micro::GetOptionalTensorData(bias), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output), - tflite::micro::GetTensorShape(nullptr), nullptr); - break; - } - case kTfLiteInt16: { - switch (bias->type) { - case kTfLiteInt32: { - reference_integer_ops::ConvPerChannel( - ConvParamsQuantized(params, data), - data.per_channel_output_multiplier, data.per_channel_output_shift, - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(filter), - tflite::micro::GetTensorData(filter), - tflite::micro::GetTensorShape(bias), - tflite::micro::GetOptionalTensorData(bias), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - } - case kTfLiteInt64: { - reference_integer_ops::ConvPerChannel( - ConvParamsQuantized(params, data), - data.per_channel_output_multiplier, data.per_channel_output_shift, - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(filter), - tflite::micro::GetTensorData(filter), - tflite::micro::GetTensorShape(bias), - tflite::micro::GetOptionalTensorData(bias), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - } - default: - MicroPrintf("Bias type %s (%d) not supported.", - TfLiteTypeGetName(bias->type), bias->type); - return kTfLiteError; - } - break; - } - case kTfLiteInt8: { - reference_integer_ops::ConvPerChannel( - ConvParamsQuantized(params, data), data.per_channel_output_multiplier, - data.per_channel_output_shift, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(filter), - tflite::micro::GetTensorData(filter), - tflite::micro::GetTensorShape(bias), - tflite::micro::GetOptionalTensorData(bias), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - } - default: - MicroPrintf("Type %s (%d) not supported.", TfLiteTypeGetName(input->type), - input->type); - return kTfLiteError; - } - return kTfLiteOk; -} - -} // namespace - -TfLiteRegistration Register_CONV_2D() { - return tflite::micro::RegisterOp(Init, ConvPrepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/conv.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/conv.h deleted file mode 100644 index 06b35e1e..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/conv.h +++ /dev/null @@ -1,112 +0,0 @@ -/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_CONV_H_ -#define TENSORFLOW_LITE_MICRO_KERNELS_CONV_H_ - -#include - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -struct OpDataConv { - TfLitePaddingValues padding; - - // Cached tensor zero point values for quantized operations. - int32_t input_zero_point; - int32_t filter_zero_point; - int32_t output_zero_point; - - // The scaling factor from input to output (aka the 'real multiplier') can - // be represented as a fixed point multiplier plus a left shift. - int32_t output_multiplier; - int output_shift; - - // Per channel output multiplier and shift. - int32_t* per_channel_output_multiplier; - int32_t* per_channel_output_shift; - - // The range of the fused activation layer. For example for kNone and - // uint8_t these would be 0 and 255. - int32_t output_activation_min; - int32_t output_activation_max; -}; - -extern const int kConvInputTensor; -extern const int kConvWeightsTensor; -extern const int kConvBiasTensor; -extern const int kConvOutputTensor; -extern const int kConvQuantizedDimension; - -// Returns a ConvParams struct with all the parameters needed for a -// float computation. -ConvParams ConvParamsFloat(const TfLiteConvParams& params, - const OpDataConv& data); - -// Returns a ConvParams struct with all the parameters needed for a -// quantized computation. -ConvParams ConvParamsQuantized(const TfLiteConvParams& params, - const OpDataConv& data); - -TfLiteStatus CalculateOpDataConv(TfLiteContext* context, TfLiteNode* node, - const TfLiteConvParams& params, int width, - int height, int filter_width, - int filter_height, int out_width, - int out_height, const TfLiteType data_type, - OpDataConv* data); - -TfLiteStatus ConvPrepare(TfLiteContext* context, TfLiteNode* node); - -// This is the most generic TfLiteRegistration. The actual supported types may -// still be target dependent. The only requirement is that every implementation -// (reference or optimized) must define this function. -TfLiteRegistration Register_CONV_2D(); - -#if defined(XTENSA) -// Returns a TfLiteRegistration struct for kernel variant that only supports -// int8 activations and int8 weights and always calls the reference -// implementation. -TfLiteRegistration Register_CONV_2D_INT8REF(); -#else -inline TfLiteRegistration Register_CONV_2D_INT8REF() { - return Register_CONV_2D(); -} -#endif - -#if defined(CMSIS_NN) -// Returns a TfLiteRegistration struct for kernel variant that only supports -// int8 activations and int8 weights and uses the latency optimized -// implementations. -TfLiteRegistration Register_CONV_2D_INT8(); - -// Returns a TfLiteRegistration struct for kernel variant that only supports -// int16 activations and int8 weights and uses the latency optimized -// implementations. -TfLiteRegistration Register_CONV_2D_INT16(); - -#else -inline TfLiteRegistration Register_CONV_2D_INT8() { return Register_CONV_2D(); } - -inline TfLiteRegistration Register_CONV_2D_INT16() { - return Register_CONV_2D(); -} -#endif - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_KERNELS_CONV_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/conv_common.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/conv_common.cc deleted file mode 100644 index 7115f7ba..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/conv_common.cc +++ /dev/null @@ -1,197 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/conv.h" -#include "tensorflow/lite/kernels/internal/reference/integer_ops/conv.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/padding.h" -#include "tensorflow/lite/micro/kernels/conv.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { - -const int kConvInputTensor = 0; -const int kConvWeightsTensor = 1; -const int kConvBiasTensor = 2; -const int kConvOutputTensor = 0; - -// Conv is quantized along dimension 0: -// https://www.tensorflow.org/lite/performance/quantization_spec -const int kConvQuantizedDimension = 0; - -// Returns a ConvParams struct with all the parameters needed for a -// float computation. -ConvParams ConvParamsFloat(const TfLiteConvParams& params, - const OpDataConv& data) { - ConvParams op_params; - CalculateActivationRange(params.activation, &op_params.float_activation_min, - &op_params.float_activation_max); - op_params.padding_type = tflite::micro::RuntimePaddingType(params.padding); - op_params.padding_values.width = data.padding.width; - op_params.padding_values.height = data.padding.height; - op_params.stride_width = params.stride_width; - op_params.stride_height = params.stride_height; - op_params.dilation_width_factor = params.dilation_width_factor; - op_params.dilation_height_factor = params.dilation_height_factor; - return op_params; -} - -// Returns a ConvParams struct with all the parameters needed for a -// quantized computation. -ConvParams ConvParamsQuantized(const TfLiteConvParams& params, - const OpDataConv& data) { - ConvParams op_params; - op_params.input_offset = -data.input_zero_point; - op_params.weights_offset = -data.filter_zero_point; - op_params.output_offset = data.output_zero_point; - op_params.output_multiplier = data.output_multiplier; - op_params.output_shift = -data.output_shift; - op_params.padding_type = tflite::micro::RuntimePaddingType(params.padding); - op_params.padding_values.height = data.padding.height; - op_params.padding_values.width = data.padding.width; - op_params.stride_height = params.stride_height; - op_params.stride_width = params.stride_width; - op_params.dilation_height_factor = params.dilation_height_factor; - op_params.dilation_width_factor = params.dilation_width_factor; - op_params.quantized_activation_min = data.output_activation_min; - op_params.quantized_activation_max = data.output_activation_max; - return op_params; -} - -TfLiteStatus CalculateOpDataConv(TfLiteContext* context, TfLiteNode* node, - const TfLiteConvParams& params, int width, - int height, int filter_width, - int filter_height, int out_width, - int out_height, const TfLiteType data_type, - OpDataConv* data) { - bool has_bias = node->inputs->size == 3; - // Check number of inputs/outputs - TF_LITE_ENSURE(context, has_bias || node->inputs->size == 2); - TF_LITE_ENSURE_EQ(context, node->outputs->size, 1); - - // Matching GetWindowedOutputSize in TensorFlow. - auto padding = params.padding; - data->padding = ComputePaddingHeightWidth( - params.stride_height, params.stride_width, params.dilation_height_factor, - params.dilation_width_factor, height, width, filter_height, filter_width, - padding, &out_height, &out_width); - - MicroContext* micro_context = GetMicroContext(context); - - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kConvInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* filter = - micro_context->AllocateTempInputTensor(node, kConvWeightsTensor); - TF_LITE_ENSURE(context, filter != nullptr); - TfLiteTensor* bias = - micro_context->AllocateTempInputTensor(node, kConvBiasTensor); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kConvOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - - // Note that quantized inference requires that all tensors have their - // parameters set. This is usually done during quantized training. - if (data_type != kTfLiteFloat32) { - int output_channels = filter->dims->data[kConvQuantizedDimension]; - - TF_LITE_ENSURE_STATUS(tflite::PopulateConvolutionQuantizationParams( - context, input, filter, bias, output, params.activation, - &data->output_multiplier, &data->output_shift, - &data->output_activation_min, &data->output_activation_max, - data->per_channel_output_multiplier, data->per_channel_output_shift, - output_channels)); - } - - data->input_zero_point = input->params.zero_point; - data->filter_zero_point = filter->params.zero_point; - data->output_zero_point = output->params.zero_point; - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(filter); - micro_context->DeallocateTempTfLiteTensor(output); - micro_context->DeallocateTempTfLiteTensor(bias); - - return kTfLiteOk; -} - -TfLiteStatus ConvPrepare(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - TFLITE_DCHECK(node->builtin_data != nullptr); - - OpDataConv* data = static_cast(node->user_data); - const auto& params = - *(static_cast(node->builtin_data)); - MicroContext* micro_context = GetMicroContext(context); - - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kConvOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kConvInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* filter = - micro_context->AllocateTempInputTensor(node, kConvWeightsTensor); - TF_LITE_ENSURE(context, filter != nullptr); - - const int input_width = input->dims->data[2]; - const int input_height = input->dims->data[1]; - const int filter_width = filter->dims->data[2]; - const int filter_height = filter->dims->data[1]; - const int output_width = output->dims->data[2]; - const int output_height = output->dims->data[1]; - - // Dynamically allocate per-channel quantization parameters. - const int num_channels = filter->dims->data[kConvQuantizedDimension]; - data->per_channel_output_multiplier = - static_cast(context->AllocatePersistentBuffer( - context, num_channels * sizeof(int32_t))); - data->per_channel_output_shift = - static_cast(context->AllocatePersistentBuffer( - context, num_channels * sizeof(int32_t))); - - // All per-channel quantized tensors need valid zero point and scale arrays. - if (input->type == kTfLiteInt8 || input->type == kTfLiteInt16) { - TF_LITE_ENSURE_EQ(context, filter->quantization.type, - kTfLiteAffineQuantization); - - const auto* affine_quantization = - static_cast(filter->quantization.params); - TFLITE_DCHECK(affine_quantization != nullptr); - TFLITE_DCHECK(affine_quantization->scale != nullptr); - TFLITE_DCHECK(affine_quantization->zero_point != nullptr); - - TF_LITE_ENSURE(context, - affine_quantization->scale->size == 1 || - affine_quantization->scale->size == - filter->dims->data[kConvQuantizedDimension]); - } - - TF_LITE_ENSURE_STATUS(CalculateOpDataConv( - context, node, params, input_width, input_height, filter_width, - filter_height, output_width, output_height, input->type, data)); - - micro_context->DeallocateTempTfLiteTensor(filter); - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(output); - - return kTfLiteOk; -} -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/conv_test.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/conv_test.h deleted file mode 100644 index 47ba8ac4..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/conv_test.h +++ /dev/null @@ -1,113 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_CONV_TEST_H_ -#define TENSORFLOW_LITE_MICRO_KERNELS_CONV_TEST_H_ - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/micro/kernels/kernel_runner.h" -#include "tensorflow/lite/micro/kernels/micro_ops.h" -#include "tensorflow/lite/micro/test_helpers.h" -#include "tensorflow/lite/micro/testing/micro_test.h" - -namespace tflite { -namespace testing { - -TfLiteStatus InvokeConv(TfLiteTensor* tensors, int tensors_size, - int output_length, TfLiteConvParams* conv_params, - TfLiteRegistration registration, float* output_data); - -TfLiteStatus InvokeConv(TfLiteTensor* tensors, int tensors_size, - int output_length, TfLiteConvParams* conv_params, - TfLiteRegistration registration, int8_t* output_data); - -TfLiteStatus InvokeConv(TfLiteTensor* tensors, int tensors_size, - int output_length, TfLiteConvParams* conv_params, - TfLiteRegistration registration, uint8_t* output_data); - -TfLiteStatus ValidateConvGoldens(TfLiteTensor* tensors, int tensors_size, - const float* expected_output_data, - int output_length, - TfLiteConvParams* conv_params, - TfLiteRegistration registration, - float* output_data, float tolerance = 1e-5); - -TfLiteStatus ValidateConvGoldens(TfLiteTensor* tensors, int tensors_size, - const int8_t* expected_output_data, - int output_length, - TfLiteConvParams* conv_params, - TfLiteRegistration registration, - int8_t* output_data, float tolerance = 1e-5); - -TfLiteStatus ValidateConvGoldens(TfLiteTensor* tensors, int tensors_size, - const uint8_t* expected_output_data, - int output_length, - TfLiteConvParams* conv_params, - TfLiteRegistration registration, - uint8_t* output_data, float tolerance = 1e-5); - -TfLiteStatus TestConvFloat(int* input_dims_data, const float* input_data, - int* filter_dims_data, const float* filter_data, - int* bias_dims_data, const float* bias_data, - int* output_dims_data, - const float* expected_output_data, - TfLiteConvParams* conv_params, - TfLiteRegistration registration, float* output_data); - -TfLiteStatus TestConvQuantizedPerLayer( - int* input_dims_data, const float* input_data, uint8_t* input_quantized, - float input_scale, int* filter_dims_data, const float* filter_data, - uint8_t* filter_quantized, float filter_scale, int* bias_dims_data, - const float* bias_data, int32_t* bias_quantized, int* output_dims_data, - const float* expected_output_data, uint8_t* expected_output_quantized, - float output_scale, TfLiteConvParams* conv_params, - TfLiteRegistration registration, uint8_t* output_data); - -TfLiteStatus TestConvQuantizedPerChannel( - int* input_dims_data, const float* input_data, int8_t* input_quantized, - float input_scale, int input_zero_point, int* filter_dims_data, - const float* filter_data, int8_t* filter_data_quantized, - int* bias_dims_data, const float* bias_data, int32_t* bias_data_quantized, - float* bias_scales, int* bias_zero_points, int* output_dims_data, - const float* expected_output_data, int8_t* expected_output_data_quantized, - float output_scale, int output_zero_point, TfLiteConvParams* conv_params, - TfLiteRegistration registration, int8_t* output_data); - -TfLiteStatus TestConvQuantizedPerChannel( - int* input_dims_data, const float* input_data, int16_t* input_quantized, - float input_scale, int input_zero_point, int* filter_dims_data, - const float* filter_data, int8_t* filter_data_quantized, - int* bias_dims_data, const float* bias_data, - std::int64_t* bias_data_quantized, float* bias_scales, - int* bias_zero_points, int* output_dims_data, - const float* expected_output_data, int16_t* expected_output_data_quantized, - float output_scale, int output_zero_point, TfLiteConvParams* conv_params, - TfLiteRegistration registration, int16_t* output_data); - -TfLiteStatus TestConvQuantizedPerChannel( - int* input_dims_data, const float* input_data, int16_t* input_quantized, - float input_scale, int input_zero_point, int* filter_dims_data, - const float* filter_data, int8_t* filter_data_quantized, - int* bias_dims_data, const float* bias_data, int32_t* bias_data_quantized, - float* bias_scales, int* bias_zero_points, int* output_dims_data, - const float* expected_output_data, int16_t* expected_output_data_quantized, - float output_scale, int output_zero_point, TfLiteConvParams* conv_params, - TfLiteRegistration registration, int16_t* output_data); - -} // namespace testing -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_KERNELS_CONV_TEST_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/cumsum.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/cumsum.cc deleted file mode 100644 index eedc61fd..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/cumsum.cc +++ /dev/null @@ -1,175 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/kernels/internal/reference/cumsum.h" - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/types.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { -namespace { - -constexpr int kInputTensor = 0; -constexpr int kAxisTensor = 1; -constexpr int kOutputTensor = 0; - -constexpr int kCumSumIntegerShift = 20; - -// only used with INT8 tensors -struct OpData { - int32_t output_activation_min; - int32_t output_activation_max; - int32_t input_offset; - int32_t output_offset; - int32_t input_multiplier; - int32_t output_multiplier; - int input_shift; - int output_shift; - int left_shift; -}; - -TfLiteStatus CalculateOpData(TfLiteContext* context, TfLiteNode* node) { - TF_LITE_ENSURE_EQ(context, NumInputs(node), 2); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - - MicroContext* micro_context = GetMicroContext(context); - - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kInputTensor); - TfLiteTensor* axis = - micro_context->AllocateTempInputTensor(node, kAxisTensor); - - TF_LITE_ENSURE(context, - input->type == kTfLiteFloat32 || input->type == kTfLiteInt8); - TF_LITE_ENSURE_EQ(context, axis->type, kTfLiteInt32); - - TF_LITE_ENSURE_EQ(context, NumElements(axis), 1); - - TF_LITE_ENSURE(context, NumDimensions(input) >= 1); - - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - - TF_LITE_ENSURE_EQ(context, input->type, output->type); - TF_LITE_ENSURE(context, HaveSameShapes(input, output)); - - if (output->type == kTfLiteInt8) { - node->user_data = - context->AllocatePersistentBuffer(context, sizeof(OpData)); - OpData* data = static_cast(node->user_data); - - // 8bit -> 8bit general quantized path, with general rescalings - data->input_offset = -input->params.zero_point; - data->output_offset = output->params.zero_point; - data->left_shift = kCumSumIntegerShift; - const double twice_max_input_scale = - 2 * static_cast(input->params.scale); - const double real_input_multiplier = - static_cast(input->params.scale) / twice_max_input_scale; - const double real_output_multiplier = - twice_max_input_scale / - ((1 << data->left_shift) * static_cast(output->params.scale)); - - QuantizeMultiplierSmallerThanOneExp( - real_input_multiplier, &data->input_multiplier, &data->input_shift); - - QuantizeMultiplierSmallerThanOneExp( - real_output_multiplier, &data->output_multiplier, &data->output_shift); - - TF_LITE_ENSURE_STATUS(CalculateActivationRangeQuantized( - context, kTfLiteActNone, output, &data->output_activation_min, - &data->output_activation_max)); - } - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(axis); - micro_context->DeallocateTempTfLiteTensor(output); - - return kTfLiteOk; -} - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - return CalculateOpData(context, node); -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kInputTensor); - const TfLiteEvalTensor* axis_tensor = - tflite::micro::GetEvalInput(context, node, kAxisTensor); - - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - - auto* cs_params = static_cast(node->builtin_data); - auto input_shape = tflite::micro::GetTensorShape(input); - - int32_t axis = *tflite::micro::GetTensorData(axis_tensor); - if (axis < 0) axis += input_shape.DimensionsCount(); - - if (axis < 0 || axis >= input_shape.DimensionsCount()) { - TF_LITE_KERNEL_LOG(context, "CUMSUM Invalid axis: %d", axis); - return kTfLiteError; - } - - switch (input->type) { - case kTfLiteFloat32: { - reference_ops::CumSum(tflite::micro::GetTensorData(input), - input_shape, axis, cs_params->exclusive, - cs_params->reverse, - tflite::micro::GetTensorData(output)); - return kTfLiteOk; - } break; - - case kTfLiteInt8: { - auto* data = static_cast(node->user_data); - ArithmeticParams params; - params.left_shift = data->left_shift; - params.input1_offset = data->input_offset; - params.input1_multiplier = data->input_multiplier; - params.input1_shift = data->input_shift; - params.output_offset = data->output_offset; - params.output_multiplier = data->output_multiplier; - params.output_shift = data->output_shift; - SetActivationParams(data->output_activation_min, - data->output_activation_max, ¶ms); - reference_ops::CumSum(params, tflite::micro::GetTensorData(input), - input_shape, axis, cs_params->exclusive, - cs_params->reverse, - tflite::micro::GetTensorData(output)); - return kTfLiteOk; - } break; - - default: { - TF_LITE_KERNEL_LOG(context, - "CUMSUM only supports FLOAT32 and INT8, got %s.", - TfLiteTypeGetName(output->type)); - return kTfLiteError; - } - } - - return kTfLiteError; -} - -} // namespace - -TfLiteRegistration Register_CUMSUM() { - return tflite::micro::RegisterOp(nullptr, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/depth_to_space.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/depth_to_space.cc deleted file mode 100644 index ec000540..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/depth_to_space.cc +++ /dev/null @@ -1,142 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/kernels/internal/reference/depth_to_space.h" - -#include - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/types.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { -namespace { - -constexpr int kInputTensor = 0; -constexpr int kOutputTensor = 0; - -// input/output tensor shape rank associations -constexpr int kBatchRank = 0; -constexpr int kHeightRank = 1; -constexpr int kWidthRank = 2; -constexpr int kDepthRank = 3; - -TfLiteStatus CalculateOpData(TfLiteContext* context, TfLiteNode* node) { - auto* params = - reinterpret_cast(node->builtin_data); - - TF_LITE_ENSURE_EQ(context, NumInputs(node), 1); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - - MicroContext* micro_context = GetMicroContext(context); - - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - - TF_LITE_ENSURE_EQ(context, NumDimensions(input), 4); - - auto data_type = output->type; - TF_LITE_ENSURE(context, - data_type == kTfLiteFloat32 || data_type == kTfLiteInt8); - TF_LITE_ENSURE_TYPES_EQ(context, input->type, output->type); - - const int block_size = params->block_size; - TF_LITE_ENSURE(context, block_size > 0); - const int input_height = input->dims->data[kHeightRank]; - const int input_width = input->dims->data[kWidthRank]; - const int input_channels = input->dims->data[kDepthRank]; - int output_height = input_height * block_size; - int output_width = input_width * block_size; - int output_channels = input_channels / block_size / block_size; - - TF_LITE_ENSURE_EQ(context, input_height, output_height / block_size); - TF_LITE_ENSURE_EQ(context, input_width, output_width / block_size); - TF_LITE_ENSURE_EQ(context, input_channels, - output_channels * block_size * block_size); - - // We must update the output tensor dimensions. - // The dims storage is expected to be the same area in memory - // for both TfLiteTensor and TfLiteEvalTensor. This is important - // because TfLiteTensor in the MicroInterpreter is a temporary - // allocation. For the KernelRunner interpreter, TfLiteEvalTensor - // is a temporary allocation. We must therefore relocate the dims - // from the FlatBuffer to the persistant storage arena. - TfLiteEvalTensor* output_eval = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - TF_LITE_ENSURE_OK(context, tflite::micro::CreateWritableTensorDimsWithCopy( - context, output, output_eval)); - output->dims->data[kBatchRank] = input->dims->data[kBatchRank]; - output->dims->data[kHeightRank] = output_height; - output->dims->data[kWidthRank] = output_width; - output->dims->data[kDepthRank] = output_channels; - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(output); - - return kTfLiteOk; -} - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - return CalculateOpData(context, node); -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - auto* params = - reinterpret_cast(node->builtin_data); - - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kInputTensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - - tflite::DepthToSpaceParams op_params; - op_params.block_size = static_cast(params->block_size); - - switch (input->type) { // Already know in/out types are same. - case kTfLiteFloat32: - reference_ops::DepthToSpace(op_params, - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - case kTfLiteInt8: - reference_ops::DepthToSpace(op_params, - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - default: - TF_LITE_KERNEL_LOG( - context, "DEPTH_TO_SPACE only supports FLOAT32 and INT8, got %s.", - TfLiteTypeGetName(output->type)); - return kTfLiteError; - } - - return kTfLiteOk; -} - -} // namespace - -TfLiteRegistration Register_DEPTH_TO_SPACE() { - return tflite::micro::RegisterOp(nullptr, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/depthwise_conv.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/depthwise_conv.cc deleted file mode 100644 index d2468ff9..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/depthwise_conv.cc +++ /dev/null @@ -1,98 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/kernels/depthwise_conv.h" - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/depthwiseconv_float.h" -#include "tensorflow/lite/kernels/internal/reference/integer_ops/depthwise_conv.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/padding.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { -namespace { - -void* Init(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(OpDataConv)); -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - TFLITE_DCHECK(node->builtin_data != nullptr); - - auto& params = - *(reinterpret_cast(node->builtin_data)); - const OpDataConv& data = *(static_cast(node->user_data)); - - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kDepthwiseConvOutputTensor); - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kDepthwiseConvInputTensor); - const TfLiteEvalTensor* filter = - tflite::micro::GetEvalInput(context, node, kDepthwiseConvWeightsTensor); - const TfLiteEvalTensor* bias = - (NumInputs(node) == 3) - ? tflite::micro::GetEvalInput(context, node, kDepthwiseConvBiasTensor) - : nullptr; - - switch (input->type) { // Already know in/out types are same. - case kTfLiteFloat32: { - tflite::reference_ops::DepthwiseConv( - DepthwiseConvParamsFloat(params, data), - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(filter), - tflite::micro::GetTensorData(filter), - tflite::micro::GetTensorShape(bias), - tflite::micro::GetOptionalTensorData(bias), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - } - case kTfLiteInt8: { - reference_integer_ops::DepthwiseConvPerChannel( - DepthwiseConvParamsQuantized(params, data), - data.per_channel_output_multiplier, data.per_channel_output_shift, - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(filter), - tflite::micro::GetTensorData(filter), - tflite::micro::GetTensorShape(bias), - tflite::micro::GetOptionalTensorData(bias), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - } - default: - TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.", - TfLiteTypeGetName(input->type), input->type); - return kTfLiteError; - } - return kTfLiteOk; -} - -} // namespace - -TfLiteRegistration Register_DEPTHWISE_CONV_2D() { - return tflite::micro::RegisterOp(Init, DepthwiseConvPrepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/depthwise_conv.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/depthwise_conv.h deleted file mode 100644 index 562438d7..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/depthwise_conv.h +++ /dev/null @@ -1,80 +0,0 @@ -/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_DEPTHWISE_CONV_H_ -#define TENSORFLOW_LITE_MICRO_KERNELS_DEPTHWISE_CONV_H_ - -#include - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/types.h" -#include "tensorflow/lite/micro/kernels/conv.h" - -namespace tflite { - -extern const int kDepthwiseConvInputTensor; -extern const int kDepthwiseConvWeightsTensor; -extern const int kDepthwiseConvBiasTensor; -extern const int kDepthwiseConvOutputTensor; -extern const int kDepthwiseConvQuantizedDimension; - -// Returns a DepthwiseParams struct with all the parameters needed for a -// float computation. -DepthwiseParams DepthwiseConvParamsFloat( - const TfLiteDepthwiseConvParams& params, const OpDataConv& data); - -// Returns a DepthwiseParams struct with all the parameters needed for a -// quantized computation. -DepthwiseParams DepthwiseConvParamsQuantized( - const TfLiteDepthwiseConvParams& params, const OpDataConv& data); - -TfLiteStatus CalculateOpDataDepthwiseConv( - TfLiteContext* context, TfLiteNode* node, - const TfLiteDepthwiseConvParams& params, int width, int height, - int filter_width, int filter_height, int out_width, int out_height, - const TfLiteType data_type, OpDataConv* data); - -TfLiteStatus DepthwiseConvPrepare(TfLiteContext* context, TfLiteNode* node); - -// This is the most generic TfLiteRegistration. The actual supported types may -// still be target dependent. The only requirement is that every implementation -// (reference or optimized) must define this function. -TfLiteRegistration Register_DEPTHWISE_CONV_2D(); - -#if defined(CMSIS_NN) -// Returns a TfLiteRegistration struct for kernel variant that only supports -// int8 activations and int8 weights and uses the latency optimized -// implementations. -TfLiteRegistration Register_DEPTHWISE_CONV_2D_INT8(); - -// Returns a TfLiteRegistration struct for kernel variant that only supports -// int16 activations and int8 weights and uses the latency optimized -// implementations. -TfLiteRegistration Register_DEPTHWISE_CONV_2D_INT16(); - -#else -inline TfLiteRegistration Register_DEPTHWISE_CONV_2D_INT8() { - return Register_DEPTHWISE_CONV_2D(); -} - -inline TfLiteRegistration Register_DEPTHWISE_CONV_2D_INT16() { - return Register_DEPTHWISE_CONV_2D(); -} -#endif - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_KERNELS_DEPTHWISE_CONV_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/depthwise_conv_common.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/depthwise_conv_common.cc deleted file mode 100644 index 3bf07274..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/depthwise_conv_common.cc +++ /dev/null @@ -1,202 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/depthwiseconv_float.h" -#include "tensorflow/lite/kernels/internal/reference/integer_ops/depthwise_conv.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/padding.h" -#include "tensorflow/lite/micro/kernels/depthwise_conv.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { - -const int kDepthwiseConvInputTensor = 0; -const int kDepthwiseConvWeightsTensor = 1; -const int kDepthwiseConvBiasTensor = 2; -const int kDepthwiseConvOutputTensor = 0; - -// DepthwiseConv is quantized along dimension 3: -// https://www.tensorflow.org/lite/performance/quantization_spec -const int kDepthwiseConvQuantizedDimension = 3; - -// Returns a DepthwiseParams struct with all the parameters needed for a -// float computation. -DepthwiseParams DepthwiseConvParamsFloat( - const TfLiteDepthwiseConvParams& params, const OpDataConv& data) { - DepthwiseParams op_params; - CalculateActivationRange(params.activation, &op_params.float_activation_min, - &op_params.float_activation_max); - op_params.padding_type = tflite::micro::RuntimePaddingType(params.padding); - op_params.padding_values.width = data.padding.width; - op_params.padding_values.height = data.padding.height; - op_params.stride_width = params.stride_width; - op_params.stride_height = params.stride_height; - op_params.dilation_width_factor = params.dilation_width_factor; - op_params.dilation_height_factor = params.dilation_height_factor; - op_params.depth_multiplier = params.depth_multiplier; - return op_params; -} - -// Returns a DepthwiseParams struct with all the parameters needed for a -// quantized computation. -DepthwiseParams DepthwiseConvParamsQuantized( - const TfLiteDepthwiseConvParams& params, const OpDataConv& data) { - DepthwiseParams op_params; - op_params.input_offset = -data.input_zero_point; - op_params.weights_offset = -data.filter_zero_point; - op_params.output_offset = data.output_zero_point; - op_params.output_multiplier = data.output_multiplier; - op_params.output_shift = -data.output_shift; - op_params.padding_type = tflite::micro::RuntimePaddingType(params.padding); - op_params.padding_values.height = data.padding.height; - op_params.padding_values.width = data.padding.width; - op_params.stride_height = params.stride_height; - op_params.stride_width = params.stride_width; - op_params.dilation_height_factor = params.dilation_height_factor; - op_params.dilation_width_factor = params.dilation_width_factor; - op_params.depth_multiplier = params.depth_multiplier; - op_params.quantized_activation_min = data.output_activation_min; - op_params.quantized_activation_max = data.output_activation_max; - return op_params; -} - -TfLiteStatus CalculateOpDataDepthwiseConv( - TfLiteContext* context, TfLiteNode* node, - const TfLiteDepthwiseConvParams& params, int width, int height, - int filter_width, int filter_height, int out_width, int out_height, - const TfLiteType data_type, OpDataConv* data) { - bool has_bias = node->inputs->size == 3; - // Check number of inputs/outputs - TF_LITE_ENSURE(context, has_bias || node->inputs->size == 2); - TF_LITE_ENSURE_EQ(context, node->outputs->size, 1); - - // Matching GetWindowedOutputSize in TensorFlow. - auto padding = params.padding; - data->padding = ComputePaddingHeightWidth( - params.stride_height, params.stride_width, params.dilation_height_factor, - params.dilation_width_factor, height, width, filter_height, filter_width, - padding, &out_height, &out_width); - - MicroContext* micro_context = GetMicroContext(context); - - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kConvInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* filter = - micro_context->AllocateTempInputTensor(node, kConvWeightsTensor); - TF_LITE_ENSURE(context, filter != nullptr); - TfLiteTensor* bias = - micro_context->AllocateTempInputTensor(node, kConvBiasTensor); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kConvOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - - // Note that quantized inference requires that all tensors have their - // parameters set. This is usually done during quantized training. - if (data_type != kTfLiteFloat32) { - int output_channels = filter->dims->data[kDepthwiseConvQuantizedDimension]; - - TF_LITE_ENSURE_STATUS(tflite::PopulateConvolutionQuantizationParams( - context, input, filter, bias, output, params.activation, - &data->output_multiplier, &data->output_shift, - &data->output_activation_min, &data->output_activation_max, - data->per_channel_output_multiplier, data->per_channel_output_shift, - output_channels)); - } - - data->input_zero_point = input->params.zero_point; - data->filter_zero_point = filter->params.zero_point; - data->output_zero_point = output->params.zero_point; - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(filter); - micro_context->DeallocateTempTfLiteTensor(bias); - micro_context->DeallocateTempTfLiteTensor(output); - - return kTfLiteOk; -} - -TfLiteStatus DepthwiseConvPrepare(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - TFLITE_DCHECK(node->builtin_data != nullptr); - - OpDataConv* data = static_cast(node->user_data); - const auto& params = - *(static_cast(node->builtin_data)); - MicroContext* micro_context = GetMicroContext(context); - - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kDepthwiseConvOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kDepthwiseConvInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* filter = - micro_context->AllocateTempInputTensor(node, kDepthwiseConvWeightsTensor); - TF_LITE_ENSURE(context, filter != nullptr); - - const int input_width = input->dims->data[2]; - const int input_height = input->dims->data[1]; - const int filter_width = filter->dims->data[2]; - const int filter_height = filter->dims->data[1]; - const int output_width = output->dims->data[2]; - const int output_height = output->dims->data[1]; - - // Dynamically allocate per-channel quantization parameters. - const int num_channels = filter->dims->data[kDepthwiseConvQuantizedDimension]; - data->per_channel_output_multiplier = - static_cast(context->AllocatePersistentBuffer( - context, num_channels * sizeof(int32_t))); - data->per_channel_output_shift = - static_cast(context->AllocatePersistentBuffer( - context, num_channels * sizeof(int32_t))); - - // All per-channel quantized tensors need valid zero point and scale arrays. - if (input->type == kTfLiteInt8) { - TF_LITE_ENSURE_EQ(context, filter->quantization.type, - kTfLiteAffineQuantization); - - const auto* affine_quantization = - static_cast(filter->quantization.params); - TFLITE_DCHECK(affine_quantization != nullptr); - TFLITE_DCHECK(affine_quantization->scale != nullptr); - TFLITE_DCHECK(affine_quantization->zero_point != nullptr); - - TF_LITE_ENSURE( - context, affine_quantization->scale->size == 1 || - affine_quantization->scale->size == - filter->dims->data[kDepthwiseConvQuantizedDimension]); - - TF_LITE_ENSURE_EQ(context, affine_quantization->scale->size, - affine_quantization->zero_point->size); - } - - TF_LITE_ENSURE_STATUS(CalculateOpDataDepthwiseConv( - context, node, params, input_width, input_height, filter_width, - filter_height, output_width, output_height, input->type, data)); - - micro_context->DeallocateTempTfLiteTensor(output); - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(filter); - - return kTfLiteOk; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/dequantize.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/dequantize.cc deleted file mode 100644 index 1cf7f133..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/dequantize.cc +++ /dev/null @@ -1,88 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/kernels/internal/reference/dequantize.h" - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/quantize.h" -#include "tensorflow/lite/kernels/internal/reference/requantize.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/dequantize.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" - -namespace tflite { - -void* DequantizeInit(TfLiteContext* context, const char* buffer, - size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(DequantizeOpData)); -} - -TfLiteStatus DequantizeEval(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - DequantizeOpData* data = static_cast(node->user_data); - - const TfLiteEvalTensor* input = tflite::micro::GetEvalInput(context, node, 0); - TfLiteEvalTensor* output = tflite::micro::GetEvalOutput(context, node, 0); - - if (output->type == kTfLiteFloat32) { - switch (input->type) { - case kTfLiteInt8: - reference_ops::Dequantize(data->quantization_params, - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - case kTfLiteInt16: - reference_ops::Dequantize(data->quantization_params, - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - case kTfLiteUInt8: - reference_ops::Dequantize(data->quantization_params, - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - default: - MicroPrintf("Input %s, output %s not supported.", - TfLiteTypeGetName(input->type), - TfLiteTypeGetName(output->type)); - return kTfLiteError; - } - } else { - MicroPrintf("Input %s, output %s not supported.", - TfLiteTypeGetName(input->type), - TfLiteTypeGetName(output->type)); - return kTfLiteError; - } - - return kTfLiteOk; -} - -TfLiteRegistration Register_DEQUANTIZE() { - return tflite::micro::RegisterOp(DequantizeInit, DequantizePrepare, - DequantizeEval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/dequantize.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/dequantize.h deleted file mode 100644 index fe6ec169..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/dequantize.h +++ /dev/null @@ -1,38 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_DEQUANTIZE_H_ -#define TENSORFLOW_LITE_MICRO_KERNELS_DEQUANTIZE_H_ - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -struct DequantizeOpData { - tflite::DequantizationParams quantization_params; - // The scaling factor from input to output (aka the 'real multiplier') can - // be represented as a fixed point multiplier plus a left shift. - int32_t output_multiplier; - int output_shift; - int32_t output_zero_point; -}; - -TfLiteStatus DequantizePrepare(TfLiteContext* context, TfLiteNode* node); - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_KERNELS_DEQUANTIZE_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/dequantize_common.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/dequantize_common.cc deleted file mode 100644 index 438f9cda..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/dequantize_common.cc +++ /dev/null @@ -1,67 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/dequantize.h" -#include "tensorflow/lite/kernels/internal/reference/quantize.h" -#include "tensorflow/lite/kernels/internal/reference/requantize.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/dequantize.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { - -TfLiteStatus DequantizePrepare(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - DequantizeOpData* data = static_cast(node->user_data); - - TF_LITE_ENSURE_EQ(context, NumInputs(node), 1); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - - MicroContext* micro_context = GetMicroContext(context); - - // TODO(b/140515557): Add cached dequant to improve hybrid model performance. - TfLiteTensor* input = micro_context->AllocateTempInputTensor(node, 0); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* output = micro_context->AllocateTempOutputTensor(node, 0); - TF_LITE_ENSURE(context, output != nullptr); - - TF_LITE_ENSURE(context, input->type == kTfLiteInt8 || - input->type == kTfLiteInt16 || - input->type == kTfLiteUInt8); - TF_LITE_ENSURE(context, output->type == kTfLiteFloat32); - - if (output->type == kTfLiteInt32) { - const double effective_output_scale = - static_cast(input->params.scale) / - static_cast(output->params.scale); - QuantizeMultiplier(effective_output_scale, &data->output_multiplier, - &data->output_shift); - } - - data->quantization_params.zero_point = input->params.zero_point; - data->quantization_params.scale = static_cast(input->params.scale); - data->output_zero_point = output->params.zero_point; - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(output); - - return kTfLiteOk; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/detection_postprocess.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/detection_postprocess.cc deleted file mode 100644 index 326d87b5..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/detection_postprocess.cc +++ /dev/null @@ -1,807 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include -#include -#include - -#include "flatbuffers/flexbuffers.h" -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/micro_utils.h" - -namespace tflite { -namespace { - -/** - * This version of detection_postprocess is specific to TFLite Micro. It - * contains the following differences between the TFLite version: - * - * 1.) Temporaries (temporary tensors) - Micro use instead scratch buffer API. - * 2.) Output dimensions - the TFLite version does not support undefined out - * dimensions. So model must have static out dimensions. - */ - -// Input tensors -constexpr int kInputTensorBoxEncodings = 0; -constexpr int kInputTensorClassPredictions = 1; -constexpr int kInputTensorAnchors = 2; - -// Output tensors -constexpr int kOutputTensorDetectionBoxes = 0; -constexpr int kOutputTensorDetectionClasses = 1; -constexpr int kOutputTensorDetectionScores = 2; -constexpr int kOutputTensorNumDetections = 3; - -constexpr int kNumCoordBox = 4; -constexpr int kBatchSize = 1; - -constexpr int kNumDetectionsPerClass = 100; - -// Object Detection model produces axis-aligned boxes in two formats: -// BoxCorner represents the lower left corner (xmin, ymin) and -// the upper right corner (xmax, ymax). -// CenterSize represents the center (xcenter, ycenter), height and width. -// BoxCornerEncoding and CenterSizeEncoding are related as follows: -// ycenter = y / y_scale * anchor.h + anchor.y; -// xcenter = x / x_scale * anchor.w + anchor.x; -// half_h = 0.5*exp(h/ h_scale)) * anchor.h; -// half_w = 0.5*exp(w / w_scale)) * anchor.w; -// ymin = ycenter - half_h -// ymax = ycenter + half_h -// xmin = xcenter - half_w -// xmax = xcenter + half_w -struct BoxCornerEncoding { - float ymin; - float xmin; - float ymax; - float xmax; -}; - -struct CenterSizeEncoding { - float y; - float x; - float h; - float w; -}; -// We make sure that the memory allocations are contiguous with static_assert. -static_assert(sizeof(BoxCornerEncoding) == sizeof(float) * kNumCoordBox, - "Size of BoxCornerEncoding is 4 float values"); -static_assert(sizeof(CenterSizeEncoding) == sizeof(float) * kNumCoordBox, - "Size of CenterSizeEncoding is 4 float values"); - -struct OpData { - int max_detections; - int max_classes_per_detection; // Fast Non-Max-Suppression - int detections_per_class; // Regular Non-Max-Suppression - float non_max_suppression_score_threshold; - float intersection_over_union_threshold; - int num_classes; - bool use_regular_non_max_suppression; - CenterSizeEncoding scale_values; - - // Scratch buffers indexes - int active_candidate_idx; - int decoded_boxes_idx; - int scores_idx; - int score_buffer_idx; - int keep_scores_idx; - int scores_after_regular_non_max_suppression_idx; - int sorted_values_idx; - int keep_indices_idx; - int sorted_indices_idx; - int buffer_idx; - int selected_idx; - - // Cached tensor scale and zero point values for quantized operations - TfLiteQuantizationParams input_box_encodings; - TfLiteQuantizationParams input_class_predictions; - TfLiteQuantizationParams input_anchors; -}; - -void* Init(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - OpData* op_data = nullptr; - - const uint8_t* buffer_t = reinterpret_cast(buffer); - const flexbuffers::Map& m = flexbuffers::GetRoot(buffer_t, length).AsMap(); - op_data = reinterpret_cast( - context->AllocatePersistentBuffer(context, sizeof(OpData))); - - op_data->max_detections = m["max_detections"].AsInt32(); - op_data->max_classes_per_detection = m["max_classes_per_detection"].AsInt32(); - if (m["detections_per_class"].IsNull()) - op_data->detections_per_class = kNumDetectionsPerClass; - else - op_data->detections_per_class = m["detections_per_class"].AsInt32(); - if (m["use_regular_nms"].IsNull()) - op_data->use_regular_non_max_suppression = false; - else - op_data->use_regular_non_max_suppression = m["use_regular_nms"].AsBool(); - - op_data->non_max_suppression_score_threshold = - m["nms_score_threshold"].AsFloat(); - op_data->intersection_over_union_threshold = m["nms_iou_threshold"].AsFloat(); - op_data->num_classes = m["num_classes"].AsInt32(); - op_data->scale_values.y = m["y_scale"].AsFloat(); - op_data->scale_values.x = m["x_scale"].AsFloat(); - op_data->scale_values.h = m["h_scale"].AsFloat(); - op_data->scale_values.w = m["w_scale"].AsFloat(); - - return op_data; -} - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - auto* op_data = static_cast(node->user_data); - - MicroContext* micro_context = GetMicroContext(context); - - // Inputs: box_encodings, scores, anchors - TF_LITE_ENSURE_EQ(context, NumInputs(node), 3); - TfLiteTensor* input_box_encodings = - micro_context->AllocateTempInputTensor(node, kInputTensorBoxEncodings); - TfLiteTensor* input_class_predictions = - micro_context->AllocateTempInputTensor(node, - kInputTensorClassPredictions); - TfLiteTensor* input_anchors = - micro_context->AllocateTempInputTensor(node, kInputTensorAnchors); - TF_LITE_ENSURE_EQ(context, NumDimensions(input_box_encodings), 3); - TF_LITE_ENSURE_EQ(context, NumDimensions(input_class_predictions), 3); - TF_LITE_ENSURE_EQ(context, NumDimensions(input_anchors), 2); - - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 4); - const int num_boxes = input_box_encodings->dims->data[1]; - const int num_classes = op_data->num_classes; - - op_data->input_box_encodings.scale = input_box_encodings->params.scale; - op_data->input_box_encodings.zero_point = - input_box_encodings->params.zero_point; - op_data->input_class_predictions.scale = - input_class_predictions->params.scale; - op_data->input_class_predictions.zero_point = - input_class_predictions->params.zero_point; - op_data->input_anchors.scale = input_anchors->params.scale; - op_data->input_anchors.zero_point = input_anchors->params.zero_point; - - // Scratch tensors - context->RequestScratchBufferInArena(context, num_boxes, - &op_data->active_candidate_idx); - context->RequestScratchBufferInArena(context, - num_boxes * kNumCoordBox * sizeof(float), - &op_data->decoded_boxes_idx); - context->RequestScratchBufferInArena( - context, - input_class_predictions->dims->data[1] * - input_class_predictions->dims->data[2] * sizeof(float), - &op_data->scores_idx); - - // Additional buffers - context->RequestScratchBufferInArena(context, num_boxes * sizeof(float), - &op_data->score_buffer_idx); - context->RequestScratchBufferInArena(context, num_boxes * sizeof(float), - &op_data->keep_scores_idx); - context->RequestScratchBufferInArena( - context, op_data->max_detections * num_boxes * sizeof(float), - &op_data->scores_after_regular_non_max_suppression_idx); - context->RequestScratchBufferInArena( - context, op_data->max_detections * num_boxes * sizeof(float), - &op_data->sorted_values_idx); - context->RequestScratchBufferInArena(context, num_boxes * sizeof(int), - &op_data->keep_indices_idx); - context->RequestScratchBufferInArena( - context, op_data->max_detections * num_boxes * sizeof(int), - &op_data->sorted_indices_idx); - int buffer_size = std::max(num_classes, op_data->max_detections); - context->RequestScratchBufferInArena( - context, buffer_size * num_boxes * sizeof(int), &op_data->buffer_idx); - buffer_size = std::min(num_boxes, op_data->max_detections); - context->RequestScratchBufferInArena( - context, buffer_size * num_boxes * sizeof(int), &op_data->selected_idx); - - // Outputs: detection_boxes, detection_scores, detection_classes, - // num_detections - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 4); - - micro_context->DeallocateTempTfLiteTensor(input_box_encodings); - micro_context->DeallocateTempTfLiteTensor(input_class_predictions); - micro_context->DeallocateTempTfLiteTensor(input_anchors); - - return kTfLiteOk; -} - -class Dequantizer { - public: - Dequantizer(int zero_point, float scale) - : zero_point_(zero_point), scale_(scale) {} - float operator()(uint8_t x) { - return (static_cast(x) - zero_point_) * scale_; - } - - private: - int zero_point_; - float scale_; -}; - -template -T ReInterpretTensor(const TfLiteEvalTensor* tensor) { - const float* tensor_base = tflite::micro::GetTensorData(tensor); - return reinterpret_cast(tensor_base); -} - -template -T ReInterpretTensor(TfLiteEvalTensor* tensor) { - float* tensor_base = tflite::micro::GetTensorData(tensor); - return reinterpret_cast(tensor_base); -} - -TfLiteStatus DecodeCenterSizeBoxes(TfLiteContext* context, TfLiteNode* node, - OpData* op_data) { - // Parse input tensor boxencodings - const TfLiteEvalTensor* input_box_encodings = - tflite::micro::GetEvalInput(context, node, kInputTensorBoxEncodings); - TF_LITE_ENSURE_EQ(context, input_box_encodings->dims->data[0], kBatchSize); - const int num_boxes = input_box_encodings->dims->data[1]; - TF_LITE_ENSURE(context, input_box_encodings->dims->data[2] >= kNumCoordBox); - const TfLiteEvalTensor* input_anchors = - tflite::micro::GetEvalInput(context, node, kInputTensorAnchors); - - // Decode the boxes to get (ymin, xmin, ymax, xmax) based on the anchors - CenterSizeEncoding box_centersize; - CenterSizeEncoding scale_values = op_data->scale_values; - CenterSizeEncoding anchor; - for (int idx = 0; idx < num_boxes; ++idx) { - switch (input_box_encodings->type) { - // Float - case kTfLiteFloat32: { - // Please see DequantizeBoxEncodings function for the support detail. - const int box_encoding_idx = idx * input_box_encodings->dims->data[2]; - const float* boxes = &(tflite::micro::GetTensorData( - input_box_encodings)[box_encoding_idx]); - box_centersize = *reinterpret_cast(boxes); - anchor = - ReInterpretTensor(input_anchors)[idx]; - break; - } - default: - // Unsupported type. - return kTfLiteError; - } - - float ycenter = static_cast(static_cast(box_centersize.y) / - static_cast(scale_values.y) * - static_cast(anchor.h) + - static_cast(anchor.y)); - - float xcenter = static_cast(static_cast(box_centersize.x) / - static_cast(scale_values.x) * - static_cast(anchor.w) + - static_cast(anchor.x)); - - float half_h = - static_cast(0.5 * - (std::exp(static_cast(box_centersize.h) / - static_cast(scale_values.h))) * - static_cast(anchor.h)); - float half_w = - static_cast(0.5 * - (std::exp(static_cast(box_centersize.w) / - static_cast(scale_values.w))) * - static_cast(anchor.w)); - - float* decoded_boxes = reinterpret_cast( - context->GetScratchBuffer(context, op_data->decoded_boxes_idx)); - auto& box = reinterpret_cast(decoded_boxes)[idx]; - box.ymin = ycenter - half_h; - box.xmin = xcenter - half_w; - box.ymax = ycenter + half_h; - box.xmax = xcenter + half_w; - } - return kTfLiteOk; -} - -void DecreasingPartialArgSort(const float* values, int num_values, - int num_to_sort, int* indices) { - std::iota(indices, indices + num_values, 0); - std::partial_sort(indices, indices + num_to_sort, indices + num_values, - [&values](const int i, const int j) { - return std::tie(values[i], j) > std::tie(values[j], i); - }); -} - -template -void InsertionSort(int* start, int* end, Compare compare) { - for (int* i = start; i != end; ++i) { - std::rotate(std::upper_bound(start, i, *i, compare), i, i + 1); - } -} - -template -void TopDownMerge(int* values, int* scratch, const int half_num_values, - int num_values, Compare compare) { - int left = 0; - int right = half_num_values; - - for (int i = 0; i < num_values; i++) { - if (left >= half_num_values || - (right < num_values && compare(values[right], values[left]))) { - scratch[i] = values[right++]; - } else { - scratch[i] = values[left++]; - } - } - memcpy(values, scratch, num_values * sizeof(int)); -} - -template -void MergeSort(int* values, int* scratch, const int num_values, - Compare compare) { - constexpr int threshold = 20; - - if (num_values < threshold) { - InsertionSort(values, values + num_values, compare); - return; - } - - const int half_num_values = num_values / 2; - - MergeSort(values, scratch, half_num_values, compare); - MergeSort(values + half_num_values, scratch, num_values - half_num_values, - compare); - TopDownMerge(values, scratch, half_num_values, num_values, compare); -} - -void DecreasingArgSort(const float* values, int num_values, int* indices, - int* scratch) { - std::iota(indices, indices + num_values, 0); - - MergeSort(indices, scratch, num_values, [&values](const int i, const int j) { - return values[i] > values[j]; - }); -} - -int SelectDetectionsAboveScoreThreshold(const float* values, int size, - const float threshold, - float* keep_values, int* keep_indices) { - int counter = 0; - for (int i = 0; i < size; i++) { - if (values[i] >= threshold) { - keep_values[counter] = values[i]; - keep_indices[counter] = i; - counter++; - } - } - return counter; -} - -bool ValidateBoxes(const float* decoded_boxes, const int num_boxes) { - for (int i = 0; i < num_boxes; ++i) { - // ymax>=ymin, xmax>=xmin - auto& box = reinterpret_cast(decoded_boxes)[i]; - if (box.ymin >= box.ymax || box.xmin >= box.xmax) { - return false; - } - } - return true; -} - -float ComputeIntersectionOverUnion(const float* decoded_boxes, const int i, - const int j) { - auto& box_i = reinterpret_cast(decoded_boxes)[i]; - auto& box_j = reinterpret_cast(decoded_boxes)[j]; - const float area_i = (box_i.ymax - box_i.ymin) * (box_i.xmax - box_i.xmin); - const float area_j = (box_j.ymax - box_j.ymin) * (box_j.xmax - box_j.xmin); - if (area_i <= 0 || area_j <= 0) return 0.0; - const float intersection_ymin = std::max(box_i.ymin, box_j.ymin); - const float intersection_xmin = std::max(box_i.xmin, box_j.xmin); - const float intersection_ymax = std::min(box_i.ymax, box_j.ymax); - const float intersection_xmax = std::min(box_i.xmax, box_j.xmax); - const float intersection_area = - std::max(intersection_ymax - intersection_ymin, 0.0) * - std::max(intersection_xmax - intersection_xmin, 0.0); - return intersection_area / (area_i + area_j - intersection_area); -} - -// NonMaxSuppressionSingleClass() prunes out the box locations with high overlap -// before selecting the highest scoring boxes (max_detections in number) -// It assumes all boxes are good in beginning and sorts based on the scores. -// If lower-scoring box has too much overlap with a higher-scoring box, -// we get rid of the lower-scoring box. -// Complexity is O(N^2) pairwise comparison between boxes -TfLiteStatus NonMaxSuppressionSingleClassHelper( - TfLiteContext* context, TfLiteNode* node, OpData* op_data, - const float* scores, int* selected, int* selected_size, - int max_detections) { - const TfLiteEvalTensor* input_box_encodings = - tflite::micro::GetEvalInput(context, node, kInputTensorBoxEncodings); - const int num_boxes = input_box_encodings->dims->data[1]; - const float non_max_suppression_score_threshold = - op_data->non_max_suppression_score_threshold; - const float intersection_over_union_threshold = - op_data->intersection_over_union_threshold; - // Maximum detections should be positive. - TF_LITE_ENSURE(context, (max_detections >= 0)); - // intersection_over_union_threshold should be positive - // and should be less than 1. - TF_LITE_ENSURE(context, (intersection_over_union_threshold > 0.0f) && - (intersection_over_union_threshold <= 1.0f)); - // Validate boxes - float* decoded_boxes = reinterpret_cast( - context->GetScratchBuffer(context, op_data->decoded_boxes_idx)); - - TF_LITE_ENSURE(context, ValidateBoxes(decoded_boxes, num_boxes)); - - // threshold scores - int* keep_indices = reinterpret_cast( - context->GetScratchBuffer(context, op_data->keep_indices_idx)); - float* keep_scores = reinterpret_cast( - context->GetScratchBuffer(context, op_data->keep_scores_idx)); - int num_scores_kept = SelectDetectionsAboveScoreThreshold( - scores, num_boxes, non_max_suppression_score_threshold, keep_scores, - keep_indices); - int* sorted_indices = reinterpret_cast( - context->GetScratchBuffer(context, op_data->sorted_indices_idx)); - - // Reusing keep_indices for scratch buffer and write back its values - // after the sorting is done. - DecreasingArgSort(keep_scores, num_scores_kept, sorted_indices, keep_indices); - int counter = 0; - for (int i = 0; i < num_boxes; i++) { - if (scores[i] >= non_max_suppression_score_threshold) { - keep_indices[counter] = i; - counter++; - } - } - - const int num_boxes_kept = num_scores_kept; - const int output_size = std::min(num_boxes_kept, max_detections); - *selected_size = 0; - - int num_active_candidate = num_boxes_kept; - uint8_t* active_box_candidate = reinterpret_cast( - context->GetScratchBuffer(context, op_data->active_candidate_idx)); - - for (int row = 0; row < num_boxes_kept; row++) { - active_box_candidate[row] = 1; - } - for (int i = 0; i < num_boxes_kept; ++i) { - if (num_active_candidate == 0 || *selected_size >= output_size) break; - if (active_box_candidate[i] == 1) { - selected[(*selected_size)++] = keep_indices[sorted_indices[i]]; - active_box_candidate[i] = 0; - num_active_candidate--; - } else { - continue; - } - for (int j = i + 1; j < num_boxes_kept; ++j) { - if (active_box_candidate[j] == 1) { - float intersection_over_union = ComputeIntersectionOverUnion( - decoded_boxes, keep_indices[sorted_indices[i]], - keep_indices[sorted_indices[j]]); - - if (intersection_over_union > intersection_over_union_threshold) { - active_box_candidate[j] = 0; - num_active_candidate--; - } - } - } - } - - return kTfLiteOk; -} - -// This function implements a regular version of Non Maximal Suppression (NMS) -// for multiple classes where -// 1) we do NMS separately for each class across all anchors and -// 2) keep only the highest anchor scores across all classes -// 3) The worst runtime of the regular NMS is O(K*N^2) -// where N is the number of anchors and K the number of -// classes. -TfLiteStatus NonMaxSuppressionMultiClassRegularHelper(TfLiteContext* context, - TfLiteNode* node, - OpData* op_data, - const float* scores) { - const TfLiteEvalTensor* input_box_encodings = - tflite::micro::GetEvalInput(context, node, kInputTensorBoxEncodings); - const TfLiteEvalTensor* input_class_predictions = - tflite::micro::GetEvalInput(context, node, kInputTensorClassPredictions); - TfLiteEvalTensor* detection_boxes = - tflite::micro::GetEvalOutput(context, node, kOutputTensorDetectionBoxes); - TfLiteEvalTensor* detection_classes = tflite::micro::GetEvalOutput( - context, node, kOutputTensorDetectionClasses); - TfLiteEvalTensor* detection_scores = - tflite::micro::GetEvalOutput(context, node, kOutputTensorDetectionScores); - TfLiteEvalTensor* num_detections = - tflite::micro::GetEvalOutput(context, node, kOutputTensorNumDetections); - - const int num_boxes = input_box_encodings->dims->data[1]; - const int num_classes = op_data->num_classes; - const int num_detections_per_class = op_data->detections_per_class; - const int max_detections = op_data->max_detections; - const int num_classes_with_background = - input_class_predictions->dims->data[2]; - // The row index offset is 1 if background class is included and 0 otherwise. - int label_offset = num_classes_with_background - num_classes; - TF_LITE_ENSURE(context, num_detections_per_class > 0); - - // For each class, perform non-max suppression. - float* class_scores = reinterpret_cast( - context->GetScratchBuffer(context, op_data->score_buffer_idx)); - int* box_indices_after_regular_non_max_suppression = reinterpret_cast( - context->GetScratchBuffer(context, op_data->buffer_idx)); - float* scores_after_regular_non_max_suppression = - reinterpret_cast(context->GetScratchBuffer( - context, op_data->scores_after_regular_non_max_suppression_idx)); - - int size_of_sorted_indices = 0; - int* sorted_indices = reinterpret_cast( - context->GetScratchBuffer(context, op_data->sorted_indices_idx)); - float* sorted_values = reinterpret_cast( - context->GetScratchBuffer(context, op_data->sorted_values_idx)); - - for (int col = 0; col < num_classes; col++) { - for (int row = 0; row < num_boxes; row++) { - // Get scores of boxes corresponding to all anchors for single class - class_scores[row] = - *(scores + row * num_classes_with_background + col + label_offset); - } - // Perform non-maximal suppression on single class - int selected_size = 0; - int* selected = reinterpret_cast( - context->GetScratchBuffer(context, op_data->selected_idx)); - TF_LITE_ENSURE_STATUS(NonMaxSuppressionSingleClassHelper( - context, node, op_data, class_scores, selected, &selected_size, - num_detections_per_class)); - // Add selected indices from non-max suppression of boxes in this class - int output_index = size_of_sorted_indices; - for (int i = 0; i < selected_size; i++) { - int selected_index = selected[i]; - - box_indices_after_regular_non_max_suppression[output_index] = - (selected_index * num_classes_with_background + col + label_offset); - scores_after_regular_non_max_suppression[output_index] = - class_scores[selected_index]; - output_index++; - } - // Sort the max scores among the selected indices - // Get the indices for top scores - int num_indices_to_sort = std::min(output_index, max_detections); - DecreasingPartialArgSort(scores_after_regular_non_max_suppression, - output_index, num_indices_to_sort, sorted_indices); - - // Copy values to temporary vectors - for (int row = 0; row < num_indices_to_sort; row++) { - int temp = sorted_indices[row]; - sorted_indices[row] = box_indices_after_regular_non_max_suppression[temp]; - sorted_values[row] = scores_after_regular_non_max_suppression[temp]; - } - // Copy scores and indices from temporary vectors - for (int row = 0; row < num_indices_to_sort; row++) { - box_indices_after_regular_non_max_suppression[row] = sorted_indices[row]; - scores_after_regular_non_max_suppression[row] = sorted_values[row]; - } - size_of_sorted_indices = num_indices_to_sort; - } - - // Allocate output tensors - for (int output_box_index = 0; output_box_index < max_detections; - output_box_index++) { - if (output_box_index < size_of_sorted_indices) { - const int anchor_index = floor( - box_indices_after_regular_non_max_suppression[output_box_index] / - num_classes_with_background); - const int class_index = - box_indices_after_regular_non_max_suppression[output_box_index] - - anchor_index * num_classes_with_background - label_offset; - const float selected_score = - scores_after_regular_non_max_suppression[output_box_index]; - // detection_boxes - float* decoded_boxes = reinterpret_cast( - context->GetScratchBuffer(context, op_data->decoded_boxes_idx)); - ReInterpretTensor(detection_boxes)[output_box_index] = - reinterpret_cast(decoded_boxes)[anchor_index]; - // detection_classes - tflite::micro::GetTensorData(detection_classes)[output_box_index] = - class_index; - // detection_scores - tflite::micro::GetTensorData(detection_scores)[output_box_index] = - selected_score; - } else { - ReInterpretTensor( - detection_boxes)[output_box_index] = {0.0f, 0.0f, 0.0f, 0.0f}; - // detection_classes - tflite::micro::GetTensorData(detection_classes)[output_box_index] = - 0.0f; - // detection_scores - tflite::micro::GetTensorData(detection_scores)[output_box_index] = - 0.0f; - } - } - tflite::micro::GetTensorData(num_detections)[0] = - size_of_sorted_indices; - - return kTfLiteOk; -} - -// This function implements a fast version of Non Maximal Suppression for -// multiple classes where -// 1) we keep the top-k scores for each anchor and -// 2) during NMS, each anchor only uses the highest class score for sorting. -// 3) Compared to standard NMS, the worst runtime of this version is O(N^2) -// instead of O(KN^2) where N is the number of anchors and K the number of -// classes. -TfLiteStatus NonMaxSuppressionMultiClassFastHelper(TfLiteContext* context, - TfLiteNode* node, - OpData* op_data, - const float* scores) { - const TfLiteEvalTensor* input_box_encodings = - tflite::micro::GetEvalInput(context, node, kInputTensorBoxEncodings); - const TfLiteEvalTensor* input_class_predictions = - tflite::micro::GetEvalInput(context, node, kInputTensorClassPredictions); - TfLiteEvalTensor* detection_boxes = - tflite::micro::GetEvalOutput(context, node, kOutputTensorDetectionBoxes); - - TfLiteEvalTensor* detection_classes = tflite::micro::GetEvalOutput( - context, node, kOutputTensorDetectionClasses); - TfLiteEvalTensor* detection_scores = - tflite::micro::GetEvalOutput(context, node, kOutputTensorDetectionScores); - TfLiteEvalTensor* num_detections = - tflite::micro::GetEvalOutput(context, node, kOutputTensorNumDetections); - - const int num_boxes = input_box_encodings->dims->data[1]; - const int num_classes = op_data->num_classes; - const int max_categories_per_anchor = op_data->max_classes_per_detection; - const int num_classes_with_background = - input_class_predictions->dims->data[2]; - - // The row index offset is 1 if background class is included and 0 otherwise. - int label_offset = num_classes_with_background - num_classes; - TF_LITE_ENSURE(context, (max_categories_per_anchor > 0)); - const int num_categories_per_anchor = - std::min(max_categories_per_anchor, num_classes); - float* max_scores = reinterpret_cast( - context->GetScratchBuffer(context, op_data->score_buffer_idx)); - int* sorted_class_indices = reinterpret_cast( - context->GetScratchBuffer(context, op_data->buffer_idx)); - - for (int row = 0; row < num_boxes; row++) { - const float* box_scores = - scores + row * num_classes_with_background + label_offset; - int* class_indices = sorted_class_indices + row * num_classes; - DecreasingPartialArgSort(box_scores, num_classes, num_categories_per_anchor, - class_indices); - max_scores[row] = box_scores[class_indices[0]]; - } - - // Perform non-maximal suppression on max scores - int selected_size = 0; - int* selected = reinterpret_cast( - context->GetScratchBuffer(context, op_data->selected_idx)); - TF_LITE_ENSURE_STATUS(NonMaxSuppressionSingleClassHelper( - context, node, op_data, max_scores, selected, &selected_size, - op_data->max_detections)); - - // Allocate output tensors - int output_box_index = 0; - - for (int i = 0; i < selected_size; i++) { - int selected_index = selected[i]; - - const float* box_scores = - scores + selected_index * num_classes_with_background + label_offset; - const int* class_indices = - sorted_class_indices + selected_index * num_classes; - - for (int col = 0; col < num_categories_per_anchor; ++col) { - int box_offset = num_categories_per_anchor * output_box_index + col; - - // detection_boxes - float* decoded_boxes = reinterpret_cast( - context->GetScratchBuffer(context, op_data->decoded_boxes_idx)); - ReInterpretTensor(detection_boxes)[box_offset] = - reinterpret_cast(decoded_boxes)[selected_index]; - - // detection_classes - tflite::micro::GetTensorData(detection_classes)[box_offset] = - class_indices[col]; - - // detection_scores - tflite::micro::GetTensorData(detection_scores)[box_offset] = - box_scores[class_indices[col]]; - - output_box_index++; - } - } - - tflite::micro::GetTensorData(num_detections)[0] = output_box_index; - return kTfLiteOk; -} - -TfLiteStatus NonMaxSuppressionMultiClass(TfLiteContext* context, - TfLiteNode* node, OpData* op_data) { - // Get the input tensors - const TfLiteEvalTensor* input_box_encodings = - tflite::micro::GetEvalInput(context, node, kInputTensorBoxEncodings); - const TfLiteEvalTensor* input_class_predictions = - tflite::micro::GetEvalInput(context, node, kInputTensorClassPredictions); - const int num_boxes = input_box_encodings->dims->data[1]; - const int num_classes = op_data->num_classes; - - TF_LITE_ENSURE_EQ(context, input_class_predictions->dims->data[0], - kBatchSize); - TF_LITE_ENSURE_EQ(context, input_class_predictions->dims->data[1], num_boxes); - const int num_classes_with_background = - input_class_predictions->dims->data[2]; - - TF_LITE_ENSURE(context, (num_classes_with_background - num_classes <= 1)); - TF_LITE_ENSURE(context, (num_classes_with_background >= num_classes)); - - const float* scores; - switch (input_class_predictions->type) { - case kTfLiteFloat32: - scores = tflite::micro::GetTensorData(input_class_predictions); - break; - default: - // Unsupported type. - return kTfLiteError; - } - - if (op_data->use_regular_non_max_suppression) { - TF_LITE_ENSURE_STATUS(NonMaxSuppressionMultiClassRegularHelper( - context, node, op_data, scores)); - } else { - TF_LITE_ENSURE_STATUS( - NonMaxSuppressionMultiClassFastHelper(context, node, op_data, scores)); - } - - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - TF_LITE_ENSURE(context, (kBatchSize == 1)); - auto* op_data = static_cast(node->user_data); - - // These two functions correspond to two blocks in the Object Detection model. - // In future, we would like to break the custom op in two blocks, which is - // currently not feasible because we would like to input quantized inputs - // and do all calculations in float. Mixed quantized/float calculations are - // currently not supported in TFLite. - - // This fills in temporary decoded_boxes - // by transforming input_box_encodings and input_anchors from - // CenterSizeEncodings to BoxCornerEncoding - TF_LITE_ENSURE_STATUS(DecodeCenterSizeBoxes(context, node, op_data)); - - // This fills in the output tensors - // by choosing effective set of decoded boxes - // based on Non Maximal Suppression, i.e. selecting - // highest scoring non-overlapping boxes. - TF_LITE_ENSURE_STATUS(NonMaxSuppressionMultiClass(context, node, op_data)); - - return kTfLiteOk; -} -} // namespace - -TfLiteRegistration* Register_DETECTION_POSTPROCESS() { - static TfLiteRegistration r = tflite::micro::RegisterOp(Init, Prepare, Eval); - return &r; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/detection_postprocess_flexbuffers_generated_data.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/detection_postprocess_flexbuffers_generated_data.h deleted file mode 100644 index f5b9eae0..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/detection_postprocess_flexbuffers_generated_data.h +++ /dev/null @@ -1,25 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_FLEXBUFFERS_GENERATED_DATA_H -#define TENSORFLOW_LITE_MICRO_KERNELS_FLEXBUFFERS_GENERATED_DATA_H - -extern const int g_gen_data_size_none_regular_nms; -extern const unsigned char g_gen_data_none_regular_nms[]; - -extern const int g_gen_data_size_regular_nms; -extern const unsigned char g_gen_data_regular_nms[]; - -#endif diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/div.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/div.cc deleted file mode 100644 index 099c0225..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/div.cc +++ /dev/null @@ -1,208 +0,0 @@ -/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/kernels/internal/reference/div.h" - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/process_broadcast_shapes.h" -#include "tensorflow/lite/kernels/internal/types.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { -namespace { - -constexpr int kInputTensor1 = 0; -constexpr int kInputTensor2 = 1; -constexpr int kOutputTensor = 0; - -struct OpDataDiv { - // Parameters used in the quantized paths where the output is 8bit - int32_t input1_zero_point; - int32_t input2_zero_point; - int32_t output_zero_point; - int32_t output_activation_min; - int32_t output_activation_max; - - // Parameters used in all quantized paths - int32_t output_multiplier; - int output_shift; -}; - -TfLiteStatus CalculateOpDataDiv(TfLiteContext* context, TfLiteTensor* input1, - TfLiteTensor* input2, TfLiteTensor* output, - TfLiteDivParams* params, OpDataDiv* data) { - TF_LITE_ENSURE_TYPES_EQ(context, input1->type, input2->type); - TF_LITE_ENSURE_TYPES_EQ(context, input1->type, output->type); - - if (output->type == kTfLiteInt8) { - TF_LITE_ENSURE_STATUS(CalculateActivationRangeQuantized( - context, params->activation, output, &data->output_activation_min, - &data->output_activation_max)); - const double real_multiplier = static_cast( - input1->params.scale / (input2->params.scale * output->params.scale)); - QuantizeMultiplier(real_multiplier, &data->output_multiplier, - &data->output_shift); - data->input1_zero_point = input1->params.zero_point; - data->input2_zero_point = input2->params.zero_point; - data->output_zero_point = output->params.zero_point; - } - - return kTfLiteOk; -} - -void* Init(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(OpDataDiv)); -} - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - TFLITE_DCHECK(node->builtin_data != nullptr); - - MicroContext* micro_context = GetMicroContext(context); - TfLiteTensor* input1 = - micro_context->AllocateTempInputTensor(node, kInputTensor1); - TF_LITE_ENSURE(context, input1 != nullptr); - TfLiteTensor* input2 = - micro_context->AllocateTempInputTensor(node, kInputTensor2); - TF_LITE_ENSURE(context, input2 != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - - OpDataDiv* data = static_cast(node->user_data); - auto* params = reinterpret_cast(node->builtin_data); - - TF_LITE_ENSURE_STATUS( - CalculateOpDataDiv(context, input1, input2, output, params, data)); - - micro_context->DeallocateTempTfLiteTensor(input1); - micro_context->DeallocateTempTfLiteTensor(input2); - micro_context->DeallocateTempTfLiteTensor(output); - return kTfLiteOk; -} - -void EvalDiv(TfLiteContext* context, TfLiteNode* node, TfLiteDivParams* params, - const OpDataDiv* data, const TfLiteEvalTensor* input1, - const TfLiteEvalTensor* input2, TfLiteEvalTensor* output) { - tflite::ArithmeticParams op_params = {}; - -#define TF_LITE_DIV(type, opname, data_type) \ - data_type output_activation_min, output_activation_max; \ - CalculateActivationRange(params->activation, &output_activation_min, \ - &output_activation_max); \ - SetActivationParams(output_activation_min, output_activation_max, \ - &op_params); \ - type::opname(op_params, tflite::micro::GetTensorShape(input1), \ - tflite::micro::GetTensorData(input1), \ - tflite::micro::GetTensorShape(input2), \ - tflite::micro::GetTensorData(input2), \ - tflite::micro::GetTensorShape(output), \ - tflite::micro::GetTensorData(output)) - - bool requires_broadcast = reference_ops::ProcessBroadcastShapes( - tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorShape(input2), &op_params); - - if (requires_broadcast) { - TF_LITE_DIV(reference_ops, BroadcastDivSlow, float); - } else { - TF_LITE_DIV(reference_ops, Div, float); - } -#undef TF_LITE_DIV -} - -TfLiteStatus EvalQuantized(TfLiteContext* context, TfLiteNode* node, - TfLiteDivParams* params, const OpDataDiv* data, - const TfLiteEvalTensor* input1, - const TfLiteEvalTensor* input2, - TfLiteEvalTensor* output) { - tflite::ArithmeticParams op_params = {}; - -#define TF_LITE_DIV(type, opname, dtype) \ - type::opname(op_params, tflite::micro::GetTensorShape(input1), \ - tflite::micro::GetTensorData(input1), \ - tflite::micro::GetTensorShape(input2), \ - tflite::micro::GetTensorData(input2), \ - tflite::micro::GetTensorShape(output), \ - tflite::micro::GetTensorData(output)) - - if (input1->type == kTfLiteInt8 && input2->type == kTfLiteInt8 && - output->type == kTfLiteInt8) { - SetActivationParams(data->output_activation_min, - data->output_activation_max, &op_params); - op_params.input1_offset = -data->input1_zero_point; - op_params.input2_offset = -data->input2_zero_point; - op_params.output_offset = data->output_zero_point; - op_params.output_multiplier = data->output_multiplier; - op_params.output_shift = data->output_shift; - - bool requires_broadcast = reference_ops::ProcessBroadcastShapes( - tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorShape(input2), &op_params); - - if (requires_broadcast) { - TF_LITE_DIV(reference_ops, BroadcastDivSlow, int8_t); - } else { - TF_LITE_DIV(reference_ops, Div, int8_t); - } -#undef TF_LITE_DIV - } else { - TF_LITE_KERNEL_LOG( - context, "Unsupported combination of input and output types in DIV."); - return kTfLiteError; - } - - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->builtin_data != nullptr); - auto* params = static_cast(node->builtin_data); - TFLITE_DCHECK(node->user_data != nullptr); - auto* data = static_cast(node->user_data); - - const TfLiteEvalTensor* input1 = - tflite::micro::GetEvalInput(context, node, kInputTensor1); - const TfLiteEvalTensor* input2 = - tflite::micro::GetEvalInput(context, node, kInputTensor2); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - - if (output->type == kTfLiteFloat32) { - EvalDiv(context, node, params, data, input1, input2, output); - } else if (output->type == kTfLiteInt8) { - TF_LITE_ENSURE_OK(context, EvalQuantized(context, node, params, data, - input1, input2, output)); - } else { - TF_LITE_KERNEL_LOG(context, - "DIV only supports FLOAT32, quantized INT8 " - "now, got type %s (%d).", - TfLiteTypeGetName(output->type), output->type); - return kTfLiteError; - } - - return kTfLiteOk; -} - -} // namespace - -TfLiteRegistration Register_DIV() { - return tflite::micro::RegisterOp(Init, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/elementwise.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/elementwise.cc deleted file mode 100644 index b1cb1dcb..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/elementwise.cc +++ /dev/null @@ -1,429 +0,0 @@ -/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/micro_utils.h" - -namespace tflite { -namespace ops { -namespace micro { -namespace elementwise { -namespace { - -constexpr int kAbsNameId = 0; -constexpr int kRsrqtNameId = 1; - -const int kElementwiseInputTensor = 0; -const int kElementwiseOutputTensor = 0; - -struct OpDataAbsRsqrt { - int32_t multiplier; - int shift; - int input_offset; - int output_offset; - bool needs_rescale; - TfLiteQuantizationType input_quantization_type; - TfLiteType input_type; -}; - -bool IsNumericSupportedType(const TfLiteType type) { - return type == kTfLiteFloat32; -} - -bool IsLogicalSupportedType(const TfLiteType type) { - return type == kTfLiteBool; -} - -bool IsAbsSupportedType(const TfLiteType type) { - return type == kTfLiteFloat32 || type == kTfLiteInt8 || type == kTfLiteInt16; -} - -bool IsRsqrtSupportedType(const TfLiteType type) { - return type == kTfLiteFloat32 || type == kTfLiteInt8; -} - -inline void SetAbsOutputMultiplier(const float input_scale, - const float output_scale, - int32_t* multiplier, int* shift) { - QuantizeMultiplier(static_cast(input_scale / output_scale), - multiplier, shift); -} - -inline void SetRsqrtOutputMultiplier(const float input_scale, - const float output_scale, - int32_t* multiplier, int* shift) { - const double scale = - 1. / static_cast((std::sqrt(input_scale) * output_scale)); - QuantizeMultiplier(scale, multiplier, shift); -} - -typedef bool (*IsSupportedType)(TfLiteType); -template -TfLiteStatus GenericPrepare(TfLiteContext* context, TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - TF_LITE_ENSURE_EQ(context, NumInputs(node), 1); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kElementwiseInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kElementwiseOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - TF_LITE_ENSURE_TYPES_EQ(context, input->type, output->type); - if (!IsSupportedType(input->type)) { - TF_LITE_KERNEL_LOG(context, "Input data type %s (%d) is not supported.", - TfLiteTypeGetName(input->type), input->type); - return kTfLiteError; - } - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(output); - return kTfLiteOk; -} - -typedef bool (*IsSupportedType)(TfLiteType); -template -TfLiteStatus PrepareAbsRsqrt(TfLiteContext* context, TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - TF_LITE_ENSURE_EQ(context, NumInputs(node), 1); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - TfLiteTensor* input = micro_context->AllocateTempInputTensor(node, 0); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* output = micro_context->AllocateTempOutputTensor(node, 0); - TF_LITE_ENSURE(context, output != nullptr); - TF_LITE_ENSURE_TYPES_EQ(context, input->type, output->type); - if (!IsSupportedType(input->type)) { - TF_LITE_KERNEL_LOG(context, "Input data type %s (%d) is not supported.", - TfLiteTypeGetName(input->type), input->type); - return kTfLiteError; - } - - auto* op_data = static_cast(node->user_data); - op_data->input_type = input->type; - - // For int16 type input, we support both quantized and non-quantized - // evaluation. - if (op_nameid == kAbsNameId) { - op_data->input_quantization_type = input->quantization.type; - } - - if (input->type == kTfLiteInt8 || - (input->type == kTfLiteInt16 && - input->quantization.type != kTfLiteNoQuantization)) { - TF_LITE_ENSURE_EQ(context, input->quantization.type, - kTfLiteAffineQuantization); - TF_LITE_ENSURE_EQ(context, output->quantization.type, - kTfLiteAffineQuantization); - const auto* input_params = - reinterpret_cast(input->quantization.params); - const auto* output_params = reinterpret_cast( - output->quantization.params); - TF_LITE_ENSURE(context, input_params != nullptr); - TF_LITE_ENSURE(context, input_params->scale != nullptr); - TF_LITE_ENSURE(context, input_params->scale->size > 0); - TF_LITE_ENSURE(context, input_params->zero_point->size > 0); - TF_LITE_ENSURE(context, output_params != nullptr); - TF_LITE_ENSURE(context, output_params->scale != nullptr); - TF_LITE_ENSURE(context, output_params->scale->size > 0); - TF_LITE_ENSURE(context, output_params->zero_point->size > 0); - op_data->input_offset = input_params->zero_point->data[0]; - op_data->output_offset = output_params->zero_point->data[0]; - if (input->type == kTfLiteInt16) { - TF_LITE_ENSURE_EQ(context, op_data->input_offset, 0); - TF_LITE_ENSURE_EQ(context, op_data->output_offset, 0); - } - const float input_scale = input_params->scale->data[0]; - const float output_scale = output_params->scale->data[0]; - op_data->needs_rescale = input_scale != output_scale; - if (op_nameid == kAbsNameId && op_data->needs_rescale) { - SetAbsOutputMultiplier(input_scale, output_scale, &op_data->multiplier, - &op_data->shift); - } else if (op_nameid == kRsrqtNameId) { - SetRsqrtOutputMultiplier(input_scale, output_scale, &op_data->multiplier, - &op_data->shift); - } - } - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(output); - return kTfLiteOk; -} - -template -inline TfLiteStatus EvalImplQuantized( - TfLiteContext* context, TfLiteNode* node, - T func(TfLiteContext*, TfLiteNode*, T), - TfLiteStatus validate_input_func(TfLiteContext*, TfLiteNode*, T), - TfLiteType expected_type) { - const TfLiteEvalTensor* input = tflite::micro::GetEvalInput(context, node, 0); - TfLiteEvalTensor* output = tflite::micro::GetEvalOutput(context, node, 0); - TF_LITE_ENSURE_TYPES_EQ(context, input->type, expected_type); - const size_t num_elements = ElementCount(*input->dims); - const T* in_data = tflite::micro::GetTensorData(input); - T* out_data = tflite::micro::GetTensorData(output); - for (size_t i = 0; i < num_elements; ++i) { - if (validate_input_func) { - TF_LITE_ENSURE_OK(context, - validate_input_func(context, node, in_data[i])); - } - out_data[i] = func(context, node, in_data[i]); - } - return kTfLiteOk; -} - -template -inline T AbsHelper(T i) { - return std::abs(i); -} - -template -inline TfLiteStatus EvalImpl(TfLiteContext* context, TfLiteNode* node, - T func(T), TfLiteStatus validate_input_func(T), - TfLiteType expected_type) { - const TfLiteEvalTensor* input = tflite::micro::GetEvalInput(context, node, 0); - TfLiteEvalTensor* output = tflite::micro::GetEvalOutput(context, node, 0); - TF_LITE_ENSURE_TYPES_EQ(context, input->type, expected_type); - const size_t num_elements = ElementCount(*input->dims); - const T* in_data = tflite::micro::GetTensorData(input); - T* out_data = tflite::micro::GetTensorData(output); - for (size_t i = 0; i < num_elements; ++i) { - if (validate_input_func) { - TF_LITE_ENSURE_OK(context, validate_input_func(in_data[i])); - } - out_data[i] = func(in_data[i]); - } - return kTfLiteOk; -} - -inline TfLiteStatus EvalNumeric(TfLiteContext* context, TfLiteNode* node, - float float_func(float)) { - return EvalImpl(context, node, float_func, - /*validate_input_func=*/nullptr, kTfLiteFloat32); -} - -inline TfLiteStatus EvalLogical(TfLiteContext* context, TfLiteNode* node, - - bool bool_func(bool)) { - return EvalImpl(context, node, bool_func, - /*validate_input_func=*/nullptr, kTfLiteBool); -} - -void* ElementWiseAbsRsqrtInit(TfLiteContext* context, const char* buffer, - size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(OpDataAbsRsqrt)); -} - -template -inline T AbsEvalQuantized(TfLiteContext* context, TfLiteNode* node, T i) { - const auto* op_data = static_cast(node->user_data); - const int kMin = std::numeric_limits::min(); - const int kMax = std::numeric_limits::max(); - - const int32_t value = std::abs(i - op_data->input_offset); - if (!op_data->needs_rescale) { - return static_cast( - std::min(std::max(static_cast(value + op_data->output_offset), - static_cast(kMin)), - static_cast(kMax))); - } - - const int32_t output = tflite::MultiplyByQuantizedMultiplier( - value, op_data->multiplier, op_data->shift) + - op_data->output_offset; - return static_cast(std::min( - std::max(static_cast(output), static_cast(kMin)), - static_cast(kMax))); -} - -template -inline T RsqrtEvalQuantized(TfLiteContext* context, TfLiteNode* node, T i) { - const auto* op_data = static_cast(node->user_data); - const int kMin = std::numeric_limits::min(); - const int kMax = std::numeric_limits::max(); - - const int32_t value = (i - op_data->input_offset); - const int32_t kShift = 20; // Shift to keep value integer. - if (value == 0) { - // Assume that any value close to 0 represents the max output value. - return static_cast(kMax); - } - int32_t inv_sqrt_multiplier; - int inv_sqrt_shift; - GetInvSqrtQuantizedMultiplierExp(value, kReverseShift, &inv_sqrt_multiplier, - &inv_sqrt_shift); - const int32_t data = tflite::MultiplyByQuantizedMultiplier( - static_cast(1), inv_sqrt_multiplier, inv_sqrt_shift + kShift); - const int32_t output = - tflite::MultiplyByQuantizedMultiplier(data, op_data->multiplier, - op_data->shift - kShift) + - op_data->output_offset; - return static_cast(std::min( - std::max(static_cast(output), static_cast(kMin)), - static_cast(kMax))); -} - -template -TfLiteStatus validate_input_func(TfLiteContext* context, TfLiteNode* node, - T i) { - const auto* op_data = static_cast(node->user_data); - - TF_LITE_ENSURE_MSG(context, i >= op_data->input_offset, - "Rsqrt is only defined for positive values"); - return static_cast(kTfLiteOk); -} - -TfLiteStatus AbsEval(TfLiteContext* context, TfLiteNode* node) { - OpDataAbsRsqrt* op_data = reinterpret_cast(node->user_data); - TfLiteType type = op_data->input_type; - TfLiteQuantizationType input_quantization_type = - op_data->input_quantization_type; - TfLiteStatus eval_result; - - switch (type) { - case kTfLiteFloat32: - eval_result = EvalNumeric(context, node, std::abs); - break; - case kTfLiteInt8: - eval_result = - EvalImplQuantized(context, node, AbsEvalQuantized, - /*validate_input_func=*/nullptr, type); - break; - case kTfLiteInt16: - eval_result = - input_quantization_type == kTfLiteNoQuantization - ? EvalImpl(context, node, AbsHelper, - /*validate_input_func=*/nullptr, type) - : EvalImplQuantized(context, node, AbsEvalQuantized, - /*validate_input_func=*/nullptr, - type); - break; - default: - TF_LITE_KERNEL_LOG(context, "Current data type %s is not supported.", - TfLiteTypeGetName(type)); - return kTfLiteError; - break; - } - return eval_result; -} - -TfLiteStatus SinEval(TfLiteContext* context, TfLiteNode* node) { - return EvalNumeric(context, node, std::sin); -} - -TfLiteStatus CosEval(TfLiteContext* context, TfLiteNode* node) { - return EvalNumeric(context, node, std::cos); -} - -TfLiteStatus LogEval(TfLiteContext* context, TfLiteNode* node) { - return EvalNumeric(context, node, std::log); -} - -TfLiteStatus SqrtEval(TfLiteContext* context, TfLiteNode* node) { - return EvalNumeric(context, node, std::sqrt); -} - -TfLiteStatus RsqrtEval(TfLiteContext* context, TfLiteNode* node) { - const auto* op_data = static_cast(node->user_data); - TfLiteType type = op_data->input_type; - switch (type) { - case kTfLiteFloat32: - return EvalImpl( - context, node, [](float f) { return 1.f / std::sqrt(f); }, - /*validate_input_func=*/nullptr, type); - case kTfLiteInt8: - return EvalImplQuantized(context, node, - elementwise::RsqrtEvalQuantized, - elementwise::validate_input_func, type); - - default: - TF_LITE_KERNEL_LOG(context, "Current data type %s is not supported.", - TfLiteTypeGetName(type)); - return kTfLiteError; - } -} - -TfLiteStatus SquareEval(TfLiteContext* context, TfLiteNode* node) { - return EvalNumeric(context, node, [](float f) { return f * f; }); -} - -TfLiteStatus LogicalNotEval(TfLiteContext* context, TfLiteNode* node) { - return EvalLogical(context, node, [](bool v) { return !v; }); -} - -} // namespace -} // namespace elementwise - -TfLiteRegistration Register_ABS() { - return tflite::micro::RegisterOp( - elementwise::ElementWiseAbsRsqrtInit, - elementwise::PrepareAbsRsqrt, - elementwise::AbsEval); -} - -TfLiteRegistration Register_SIN() { - return tflite::micro::RegisterOp( - nullptr, elementwise::GenericPrepare, - elementwise::SinEval); -} - -TfLiteRegistration Register_COS() { - return tflite::micro::RegisterOp( - nullptr, elementwise::GenericPrepare, - elementwise::CosEval); -} - -TfLiteRegistration Register_LOG() { - return tflite::micro::RegisterOp( - nullptr, elementwise::GenericPrepare, - elementwise::LogEval); -} - -TfLiteRegistration Register_SQRT() { - return tflite::micro::RegisterOp( - nullptr, elementwise::GenericPrepare, - elementwise::SqrtEval); -} - -TfLiteRegistration Register_RSQRT() { - return tflite::micro::RegisterOp( - elementwise::ElementWiseAbsRsqrtInit, - elementwise::PrepareAbsRsqrt, - elementwise::RsqrtEval); -} - -TfLiteRegistration Register_SQUARE() { - return tflite::micro::RegisterOp( - nullptr, elementwise::GenericPrepare, - elementwise::SquareEval); -} - -TfLiteRegistration Register_LOGICAL_NOT() { - return tflite::micro::RegisterOp( - nullptr, elementwise::GenericPrepare, - elementwise::LogicalNotEval); -} - -} // namespace micro -} // namespace ops -} // namespace tflite \ No newline at end of file diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/elu.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/elu.cc deleted file mode 100644 index 0b64e89d..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/elu.cc +++ /dev/null @@ -1,152 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/kernels/internal/reference/elu.h" - -#include -#include - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/cppmath.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/process_broadcast_shapes.h" -#include "tensorflow/lite/kernels/internal/types.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" - -namespace tflite { -namespace { - -// Input/output tensor index. -constexpr int kInputTensor = 0; -constexpr int kOutputTensor = 0; - -// OLD-TODO(b/142762739): We should figure out a multi-threading plan for most -// of the activation ops below. - -struct OpData { - int8_t table[256]; -}; - -using TransformFunc = float (*)(float); - -template -void PopulateLookupTable(const TfLiteTensor* input, const TfLiteTensor* output, - const TransformFunc transform, OpData* data) { - if (sizeof(T) != 1) { - MicroPrintf("Lookup table valid only for 8bit"); - TFLITE_ABORT; - } - - const float inverse_scale = 1 / output->params.scale; - int32_t maxval = std::numeric_limits::max(); - int32_t minval = std::numeric_limits::min(); - for (int32_t val = minval; val <= maxval; ++val) { - const float dequantized = - input->params.scale * (val - input->params.zero_point); - const float transformed = transform(dequantized); - const float rescaled = TfLiteRound(transformed * inverse_scale); - const int32_t quantized = - static_cast(rescaled + output->params.zero_point); - data->table[static_cast(static_cast(val))] = - static_cast(std::max(std::min(maxval, quantized), minval)); - } -} - -// OLD-TODO(b/143696793): move this to optimized_ops. -void EvalUsingLookupTable(const OpData* data, const TfLiteEvalTensor* input, - TfLiteEvalTensor* output) { - const int size = MatchingFlatSize(tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorShape(output)); - int8_t* output_data = tflite::micro::GetTensorData(output); - const int8_t* input_data = tflite::micro::GetTensorData(input); - - for (int i = 0; i < size; ++i) { - output_data[i] = data->table[static_cast(input_data[i])]; - } -} - -TfLiteStatus CalculateOpData(TfLiteContext* context, TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - - TF_LITE_ENSURE_EQ(context, NumInputs(node), 1); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - TF_LITE_ENSURE_TYPES_EQ(context, input->type, output->type); - - // Use LUT to handle quantized elu path. - if (input->type == kTfLiteInt8) { - OpData* data = static_cast(node->user_data); - TransformFunc transform = [](float value) { - return value < 0.0f ? std::exp(value) - 1.0f : value; - }; - PopulateLookupTable(input, output, transform, data); - } - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(output); - return kTfLiteOk; -} - -void* EluInit(TfLiteContext* context, const char* buffer, size_t length) { - // This is a builtin op, so we don't use the contents in 'buffer', if any. - // Instead, we allocate a new object to carry information from Prepare() to - // Eval(). - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(OpData)); -} - -TfLiteStatus EluPrepare(TfLiteContext* context, TfLiteNode* node) { - return CalculateOpData(context, node); -} - -TfLiteStatus EluEval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kInputTensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - switch (input->type) { - case kTfLiteFloat32: { - reference_ops::Elu(tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - return kTfLiteOk; - } - case kTfLiteInt8: { - const OpData* data = static_cast(node->user_data); - EvalUsingLookupTable(data, input, output); - return kTfLiteOk; - } - default: - TF_LITE_KERNEL_LOG( - context, "ELU only supports float32 and int8 currently, got %s.", - TfLiteTypeGetName(input->type)); - return kTfLiteError; - } -} - -} // namespace - -TfLiteRegistration Register_ELU() { - return tflite::micro::RegisterOp(EluInit, EluPrepare, EluEval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/esp_nn/README.md b/code/components/tflite-lib/tensorflow/lite/micro/kernels/esp_nn/README.md deleted file mode 100644 index b0c215fb..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/esp_nn/README.md +++ /dev/null @@ -1,11 +0,0 @@ -# Info - -These are the Espressif chipset specific replacement kernels. -The kernels call optimized routines or reference routines depending upon optimization option selected. - -By default optimizations are selected if available. -To change this behaviour, please make the appropriate `ESP-NN` menu selection after running: - -``` -idf.py menuconfig -``` diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/esp_nn/add.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/esp_nn/add.cc deleted file mode 100644 index 2f1ac58d..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/esp_nn/add.cc +++ /dev/null @@ -1,202 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/kernels/internal/reference/add.h" - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/integer_ops/add.h" -#include "tensorflow/lite/kernels/internal/reference/process_broadcast_shapes.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/kernels/add.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/memory_helpers.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" - -#include - -#if ESP_NN -#include -#endif - -long long add_total_time = 0; - -namespace tflite { - -void EvalAdd(TfLiteContext* context, TfLiteNode* node, TfLiteAddParams* params, - const OpDataAdd* data, const TfLiteEvalTensor* input1, - const TfLiteEvalTensor* input2, TfLiteEvalTensor* output) { - tflite::ArithmeticParams op_params; - SetActivationParams(data->output_activation_min_f32, - data->output_activation_max_f32, &op_params); - if (data->requires_broadcast) { - reference_ops::BroadcastAdd4DSlow( - op_params, tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } else { - reference_ops::Add(op_params, tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } -} - -TfLiteStatus EvalAddQuantized(TfLiteContext* context, TfLiteNode* node, - TfLiteAddParams* params, const OpDataAdd* data, - const TfLiteEvalTensor* input1, - const TfLiteEvalTensor* input2, - TfLiteEvalTensor* output) { - tflite::ArithmeticParams op_params; - op_params.left_shift = data->left_shift; - op_params.input1_offset = data->input1_offset; - op_params.input1_multiplier = data->input1_multiplier; - op_params.input1_shift = data->input1_shift; - op_params.input2_offset = data->input2_offset; - op_params.input2_multiplier = data->input2_multiplier; - op_params.input2_shift = data->input2_shift; - op_params.output_offset = data->output_offset; - op_params.output_multiplier = data->output_multiplier; - op_params.output_shift = data->output_shift; - SetActivationParams(data->output_activation_min, data->output_activation_max, - &op_params); - bool need_broadcast = reference_ops::ProcessBroadcastShapes( - tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorShape(input2), &op_params); - - switch (output->type) { - case kTfLiteInt8: { - if (need_broadcast) { - reference_integer_ops::BroadcastAdd4DSlow( - op_params, tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } else { -#if ESP_NN - const int8_t *input1_data = tflite::micro::GetTensorData(input1); - const int8_t *input2_data = tflite::micro::GetTensorData(input2); - int8_t *out_data = tflite::micro::GetTensorData(output); - - esp_nn_add_elementwise_s8(input1_data, - input2_data, - data->input1_offset, - data->input2_offset, - data->input1_multiplier, - data->input2_multiplier, - data->input1_shift, - data->input2_shift, - data->left_shift, - out_data, - data->output_offset, - data->output_multiplier, - data->output_shift, - data->output_activation_min, - data->output_activation_max, - MatchingElementsSize(tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorShape(output)) - ); -#else - reference_integer_ops::Add( - op_params, tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); -#endif - } - break; - } - case kTfLiteInt16: { - if (need_broadcast) { - reference_ops::BroadcastAdd4DSlow( - op_params, tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } else { - reference_ops::Add(op_params, tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output), - false); - } - break; - } - default: - MicroPrintf("Type %s (%d) not supported.", - TfLiteTypeGetName(output->type), output->type); - return kTfLiteError; - } - - return kTfLiteOk; -} - -void* AddInit(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(OpDataAdd)); -} - -TfLiteStatus AddEval(TfLiteContext* context, TfLiteNode* node) { - auto* params = reinterpret_cast(node->builtin_data); - - TFLITE_DCHECK(node->user_data != nullptr); - const OpDataAdd* data = static_cast(node->user_data); - - const TfLiteEvalTensor* input1 = - tflite::micro::GetEvalInput(context, node, kAddInputTensor1); - const TfLiteEvalTensor* input2 = - tflite::micro::GetEvalInput(context, node, kAddInputTensor2); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kAddOutputTensor); - - long long start_time = esp_timer_get_time(); - - if (output->type == kTfLiteFloat32) { - EvalAdd(context, node, params, data, input1, input2, output); - } else if (output->type == kTfLiteInt8 || output->type == kTfLiteInt16) { - TF_LITE_ENSURE_OK(context, EvalAddQuantized(context, node, params, data, - input1, input2, output)); - } else { - MicroPrintf("Type %s (%d) not supported.", TfLiteTypeGetName(output->type), - output->type); - return kTfLiteError; - } - add_total_time += esp_timer_get_time() - start_time; - - return kTfLiteOk; -} - -TfLiteRegistration Register_ADD() { - return tflite::micro::RegisterOp(AddInit, AddPrepare, AddEval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/esp_nn/conv.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/esp_nn/conv.cc deleted file mode 100644 index 919dd006..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/esp_nn/conv.cc +++ /dev/null @@ -1,344 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/kernels/conv.h" - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/conv.h" -#include "tensorflow/lite/kernels/internal/reference/integer_ops/conv.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/padding.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -#include "freertos/FreeRTOS.h" -#include - -#if ESP_NN -#include -#endif - - -long long conv_total_time = 0; - -namespace tflite { -namespace { - -struct NodeData { - OpDataConv op_data; -#if ESP_NN - int buffer_idx; -#endif -}; - -void* Init(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(NodeData)); -} - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - TFLITE_DCHECK(node->builtin_data != nullptr); - - NodeData* data = static_cast(node->user_data); - const auto& params = - *(static_cast(node->builtin_data)); - - MicroContext* micro_context = GetMicroContext(context); - - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kConvInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* filter = - micro_context->AllocateTempInputTensor(node, kConvWeightsTensor); - TF_LITE_ENSURE(context, filter != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kConvOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - - const int input_width = input->dims->data[2]; - const int input_height = input->dims->data[1]; - const int filter_width = filter->dims->data[2]; - const int filter_height = filter->dims->data[1]; - const int output_width = output->dims->data[2]; - const int output_height = output->dims->data[1]; - - // Dynamically allocate per-channel quantization parameters. - const int num_channels = filter->dims->data[kConvQuantizedDimension]; - data->op_data.per_channel_output_multiplier = - static_cast(context->AllocatePersistentBuffer( - context, num_channels * sizeof(int32_t))); - data->op_data.per_channel_output_shift = - static_cast(context->AllocatePersistentBuffer( - context, num_channels * sizeof(int32_t))); - - // All per-channel quantized tensors need valid zero point and scale arrays. - if (input->type == kTfLiteInt8) { - TF_LITE_ENSURE_EQ(context, filter->quantization.type, - kTfLiteAffineQuantization); - - const auto* affine_quantization = - static_cast(filter->quantization.params); - TFLITE_DCHECK(affine_quantization != nullptr); - TFLITE_DCHECK(affine_quantization->scale != nullptr); - TFLITE_DCHECK(affine_quantization->zero_point != nullptr); - - TF_LITE_ENSURE(context, - affine_quantization->scale->size == 1 || - affine_quantization->scale->size == - filter->dims->data[kConvQuantizedDimension]); - TF_LITE_ENSURE_EQ(context, affine_quantization->scale->size, - affine_quantization->zero_point->size); - } - - TF_LITE_ENSURE_STATUS(CalculateOpDataConv( - context, node, params, input_width, input_height, filter_width, - filter_height, output_width, output_height, input->type, &data->op_data)); - -#if ESP_NN - if (input->type == kTfLiteInt8) { - data_dims_t input_dims = { - .width = input_width, .height = input_height, - .channels = input->dims->data[3], 1 - }; - data_dims_t output_dims = { - .width = output_width, .height = output_height, - .channels = output->dims->data[3], 1 - }; - data_dims_t filter_dims = {.width = filter_width, .height = filter_height, 0, 0}; - conv_params_t conv_params = { - .in_offset = 0, .out_offset = 0, - .stride = {params.stride_width, params.stride_height}, - .padding = {data->op_data.padding.width, data->op_data.padding.height}, - .dilation = {0, 0}, .activation = {-128, 127} - }; - - int scratch_buf_size = esp_nn_get_conv_scratch_size( - &input_dims, &filter_dims, &output_dims, &conv_params); - if (scratch_buf_size > 0) { - TF_LITE_ENSURE_STATUS(context->RequestScratchBufferInArena( - context, scratch_buf_size, &data->buffer_idx)); - } else { - data->buffer_idx = -1; - } - } -#endif - - micro_context->DeallocateTempTfLiteTensor(output); - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(filter); - - return kTfLiteOk; -} - -#if ESP_NN -// Fixed-point per-channel-quantization convolution Int8 function wrapper. -inline void EvalQuantizedPerChannel( - TfLiteContext* context, TfLiteNode* node, const TfLiteConvParams& params, - const NodeData& data, const TfLiteEvalTensor* input, - const TfLiteEvalTensor* filter, const TfLiteEvalTensor* bias, - TfLiteEvalTensor* output) { - const int dilation_width_factor = params.dilation_width_factor; - const int dilation_height_factor = params.dilation_height_factor; - - if (dilation_width_factor == 1 && dilation_height_factor == 1) { - // Get parameters. - RuntimeShape filter_shape = tflite::micro::GetTensorShape(filter); - RuntimeShape input_shape = tflite::micro::GetTensorShape(input); - RuntimeShape output_shape = tflite::micro::GetTensorShape(output); - RuntimeShape bias_shape = tflite::micro::GetTensorShape(bias); - - const int8_t *input_data = tflite::micro::GetTensorData(input); - int8_t *output_data = tflite::micro::GetTensorData(output); - - const int32_t input_offset = -data.op_data.input_zero_point; - const int32_t output_offset = data.op_data.output_zero_point; - const int stride_width = params.stride_width; - const int stride_height = params.stride_height; - const int pad_width = data.op_data.padding.width; - const int pad_height = data.op_data.padding.height; - - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - const int filter_height = filter_shape.Dims(1); - const int filter_width = filter_shape.Dims(2); - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - - // Set min and max value of the output. - const int32_t activation_min = data.op_data.output_activation_min; - const int32_t activation_max = data.op_data.output_activation_max; - - // Consistency check. - TFLITE_DCHECK_LE(activation_min, activation_max); - TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(filter_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4); - const int batch_size = MatchingDim(input_shape, 0, output_shape, 0); - const int input_depth = MatchingDim(input_shape, 3, filter_shape, 3); - const int output_depth = MatchingDim(filter_shape, 0, output_shape, 3); - - if (tflite::micro::GetTensorData(bias)) { - TFLITE_DCHECK_EQ(bias_shape.FlatSize(), output_depth); - } - - void *scratch_buf = NULL; - if (data.buffer_idx > -1) { - scratch_buf = context->GetScratchBuffer(context, data.buffer_idx); - } - esp_nn_set_conv_scratch_buf(scratch_buf); - - const int input_size = input_width * input_height * input_depth; - const int output_size = output_width * output_height * output_depth; - - data_dims_t input_dims = { - .width = input_width, .height = input_height, - .channels = input_depth, 1 - }; - data_dims_t output_dims = { - .width = output_width, .height = output_height, - .channels = output_depth, 1 - }; - data_dims_t filter_dims = {.width = filter_width, .height = filter_height, 0, 0}; - conv_params_t conv_params = { - .in_offset = input_offset, .out_offset = output_offset, - .stride = {stride_width, stride_height}, - .padding = {pad_width, pad_height}, - .dilation = {0, 0}, - .activation = {activation_min, activation_max} - }; - quant_data_t quant_data = { - .shift = data.op_data.per_channel_output_shift, - .mult = data.op_data.per_channel_output_multiplier - }; - - for (int i_batch = 0; i_batch < batch_size; i_batch++) { - esp_nn_conv_s8(&input_dims, input_data + i_batch * input_size, - &filter_dims, tflite::micro::GetTensorData(filter), - tflite::micro::GetTensorData(bias), - &output_dims, output_data + i_batch * output_size, - &conv_params, &quant_data); - } - } else { - reference_integer_ops::ConvPerChannel( - ConvParamsQuantized(params, data.op_data), - data.op_data.per_channel_output_multiplier, - data.op_data.per_channel_output_shift, - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(filter), - tflite::micro::GetTensorData(filter), - tflite::micro::GetTensorShape(bias), - tflite::micro::GetTensorData(bias), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } -} -#endif - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kConvInputTensor); - const TfLiteEvalTensor* filter = - tflite::micro::GetEvalInput(context, node, kConvWeightsTensor); - const TfLiteEvalTensor* bias = - (NumInputs(node) == 3) - ? tflite::micro::GetEvalInput(context, node, kConvBiasTensor) - : nullptr; - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kConvOutputTensor); - - TFLITE_DCHECK(node->builtin_data != nullptr); - const auto& params = - *(reinterpret_cast(node->builtin_data)); - TFLITE_DCHECK(node->user_data != nullptr); - const auto& data = *(static_cast(node->user_data)); - - TF_LITE_ENSURE_EQ(context, input->type, output->type); - TF_LITE_ENSURE_MSG(context, input->type == filter->type, - "Hybrid models are not supported on TFLite Micro."); - - long long start_time = esp_timer_get_time(); - switch (input->type) { // Already know in/out types are same. - case kTfLiteFloat32: { - tflite::reference_ops::Conv( - ConvParamsFloat(params, data.op_data), - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(filter), - tflite::micro::GetTensorData(filter), - tflite::micro::GetTensorShape(bias), - tflite::micro::GetTensorData(bias), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output), - tflite::micro::GetTensorShape(nullptr), nullptr); - break; - } - case kTfLiteInt8: { -#if ESP_NN - EvalQuantizedPerChannel(context, node, params, data, input, filter, - bias, output); -#else - reference_integer_ops::ConvPerChannel( - ConvParamsQuantized(params, data.op_data), - data.op_data.per_channel_output_multiplier, - data.op_data.per_channel_output_shift, - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(filter), - tflite::micro::GetTensorData(filter), - tflite::micro::GetTensorShape(bias), - tflite::micro::GetTensorData(bias), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); -#endif - break; - } - case kTfLiteUInt8: { - //EvalQuantized - reference_ops::Conv(ConvParamsQuantized(params, data.op_data), - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(filter), - tflite::micro::GetTensorData(filter), - tflite::micro::GetTensorShape(bias), - tflite::micro::GetTensorData(bias), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output), - tflite::micro::GetTensorShape(nullptr), nullptr, - nullptr); - break; - } - default: - TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.", - TfLiteTypeGetName(input->type), input->type); - return kTfLiteError; - } - long long time_this_instance = esp_timer_get_time() - start_time; - conv_total_time += time_this_instance; - //printf("time this instance: %llu\n", time_this_instance / 1000); - return kTfLiteOk; -} - -} // namespace - -TfLiteRegistration Register_CONV_2D() { - return tflite::micro::RegisterOp(Init, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/esp_nn/depthwise_conv.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/esp_nn/depthwise_conv.cc deleted file mode 100644 index a2460248..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/esp_nn/depthwise_conv.cc +++ /dev/null @@ -1,346 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/kernels/depthwise_conv.h" - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/depthwiseconv_float.h" -#include "tensorflow/lite/kernels/internal/reference/depthwiseconv_uint8.h" -#include "tensorflow/lite/kernels/internal/reference/integer_ops/depthwise_conv.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/padding.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -#include "freertos/FreeRTOS.h" -#include - -#if ESP_NN -#include -#endif - -long long dc_total_time = 0; - -namespace tflite { -namespace { - -struct NodeData { - OpDataConv op_data; -#if ESP_NN - int buffer_idx; -#endif -}; - -void* Init(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(NodeData)); -} - -#if ESP_NN -inline void EvalQuantizedPerChannel(TfLiteContext* context, TfLiteNode* node, - const TfLiteDepthwiseConvParams& params, - const NodeData& data, - const TfLiteEvalTensor* input, - const TfLiteEvalTensor* filter, - const TfLiteEvalTensor* bias, - TfLiteEvalTensor* output) { - const int dilation_width_factor = params.dilation_width_factor; - const int dilation_height_factor = params.dilation_height_factor; - - if (dilation_width_factor == 1 && dilation_height_factor == 1) { - // Get parameters. - RuntimeShape input_shape = tflite::micro::GetTensorShape(input); - RuntimeShape filter_shape = tflite::micro::GetTensorShape(filter); - RuntimeShape output_shape = tflite::micro::GetTensorShape(output); - RuntimeShape bias_shape = tflite::micro::GetTensorShape(bias); - - TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(filter_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4); - - const int8_t *input_data = tflite::micro::GetTensorData(input); - int8_t *output_data = tflite::micro::GetTensorData(output); - - const int depth_multiplier = params.depth_multiplier; - const int32_t input_offset = -data.op_data.input_zero_point; - const int32_t output_offset = data.op_data.output_zero_point; - const int stride_width = params.stride_width; - const int stride_height = params.stride_height; - const int pad_width = data.op_data.padding.width; - const int pad_height = data.op_data.padding.height; - - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - const int input_depth = input_shape.Dims(3); - const int filter_height = filter_shape.Dims(1); - const int filter_width = filter_shape.Dims(2); - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - - // Set min and max value of the output. - const int32_t activation_min = data.op_data.output_activation_min; - const int32_t activation_max = data.op_data.output_activation_max; - - // Consistency check. - TFLITE_DCHECK_LE(activation_min, activation_max); - const int batch_size = MatchingDim(input_shape, 0, output_shape, 0); - const int output_depth = MatchingDim(filter_shape, 3, output_shape, 3); - - TFLITE_DCHECK_EQ(output_depth, input_depth * depth_multiplier); - if (tflite::micro::GetTensorData(bias)) { - TFLITE_DCHECK_EQ(bias_shape.FlatSize(), output_depth); - } - - const int input_size = input_width * input_height * input_depth; - const int output_size = output_width * output_height * output_depth; - void *scratch_buf = NULL; - if (data.buffer_idx > -1) { - scratch_buf = context->GetScratchBuffer(context, data.buffer_idx); - } - - esp_nn_set_depthwise_conv_scratch_buf(scratch_buf); - - data_dims_t input_dims = { - .width = input_width, .height = input_height, - .channels = input_depth, 1 - }; - data_dims_t output_dims = { - .width = output_width, .height = output_height, - .channels = output_depth, 1 - }; - data_dims_t filter_dims = {.width = filter_width, .height = filter_height, 0, 0}; - dw_conv_params_t conv_params = { - .in_offset = input_offset, .out_offset = output_offset, - .ch_mult = depth_multiplier, - .stride = {stride_width, stride_height}, - .padding = {pad_width, pad_height}, .dilation = {0, 0}, - .activation = {activation_min, activation_max} - }; - quant_data_t quant_data = { - .shift = data.op_data.per_channel_output_shift, - .mult = data.op_data.per_channel_output_multiplier - }; - - for (int i_batch = 0; i_batch < batch_size; i_batch++) { - esp_nn_depthwise_conv_s8(&input_dims, input_data + i_batch * input_size, - &filter_dims, tflite::micro::GetTensorData(filter), - tflite::micro::GetTensorData(bias), - &output_dims, output_data + i_batch * output_size, - &conv_params, &quant_data); - } - } else { - reference_integer_ops::DepthwiseConvPerChannel( - DepthwiseConvParamsQuantized(params, data.op_data), - data.op_data.per_channel_output_multiplier, - data.op_data.per_channel_output_shift, - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(filter), - tflite::micro::GetTensorData(filter), - tflite::micro::GetTensorShape(bias), - tflite::micro::GetTensorData(bias), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } -} -#endif - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - TFLITE_DCHECK(node->builtin_data != nullptr); - - NodeData* data = static_cast(node->user_data); - const TfLiteDepthwiseConvParams& params = - *(static_cast(node->builtin_data)); - - MicroContext* micro_context = GetMicroContext(context); - - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kConvInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* filter = - micro_context->AllocateTempInputTensor(node, kConvWeightsTensor); - TF_LITE_ENSURE(context, filter != nullptr); - TfLiteTensor* bias = - micro_context->AllocateTempInputTensor(node, kConvBiasTensor); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kConvOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - - const int input_width = input->dims->data[2]; - const int input_height = input->dims->data[1]; - const int filter_width = filter->dims->data[2]; - const int filter_height = filter->dims->data[1]; - const int output_width = output->dims->data[2]; - const int output_height = output->dims->data[1]; - - // Dynamically allocate per-channel quantization parameters. - const int num_channels = filter->dims->data[kDepthwiseConvQuantizedDimension]; - data->op_data.per_channel_output_multiplier = - static_cast(context->AllocatePersistentBuffer( - context, num_channels * sizeof(int32_t))); - data->op_data.per_channel_output_shift = - static_cast(context->AllocatePersistentBuffer( - context, num_channels * sizeof(int32_t))); - - // All per-channel quantized tensors need valid zero point and scale arrays. - if (input->type == kTfLiteInt8) { - TF_LITE_ENSURE_EQ(context, filter->quantization.type, - kTfLiteAffineQuantization); - - const auto* affine_quantization = - static_cast(filter->quantization.params); - TFLITE_DCHECK(affine_quantization != nullptr); - TFLITE_DCHECK(affine_quantization->scale != nullptr); - TFLITE_DCHECK(affine_quantization->zero_point != nullptr); - - TF_LITE_ENSURE( - context, affine_quantization->scale->size == 1 || - affine_quantization->scale->size == - filter->dims->data[kDepthwiseConvQuantizedDimension]); - - TF_LITE_ENSURE_EQ(context, affine_quantization->scale->size, - affine_quantization->zero_point->size); - } - - TF_LITE_ENSURE_STATUS(CalculateOpDataDepthwiseConv( - context, node, params, input_width, input_height, filter_width, - filter_height, output_width, output_height, input->type, &data->op_data)); - -#if ESP_NN - if (input->type == kTfLiteInt8) { - data_dims_t input_dims = { - .width = input_width, .height = input_height, - .channels = input->dims->data[3], 1 - }; - data_dims_t output_dims = { - .width = output_width, .height = output_height, - .channels = output->dims->data[3], 1 - }; - data_dims_t filter_dims = {.width = filter_width, .height = filter_height, 0, 0}; - dw_conv_params_t conv_params = { - .in_offset = 0, .out_offset = 0, - .ch_mult = params.depth_multiplier, - .stride = {params.stride_width, params.stride_height}, - .padding = {data->op_data.padding.width, data->op_data.padding.height}, - .dilation = {0, 0}, .activation = {-128, 127} - }; - - int scratch_buf_size = esp_nn_get_depthwise_conv_scratch_size( - &input_dims, &filter_dims, &output_dims, &conv_params); - if (scratch_buf_size > 0) { - TF_LITE_ENSURE_STATUS(context->RequestScratchBufferInArena( - context, scratch_buf_size, &data->buffer_idx)); - } else { - data->buffer_idx = -1; - } - } -#endif - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(filter); - micro_context->DeallocateTempTfLiteTensor(bias); - micro_context->DeallocateTempTfLiteTensor(output); - - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - TFLITE_DCHECK(node->builtin_data != nullptr); - - auto& params = - *(reinterpret_cast(node->builtin_data)); - const NodeData& data = *(static_cast(node->user_data)); - - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kDepthwiseConvOutputTensor); - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kDepthwiseConvInputTensor); - const TfLiteEvalTensor* filter = - tflite::micro::GetEvalInput(context, node, kDepthwiseConvWeightsTensor); - const TfLiteEvalTensor* bias = - (NumInputs(node) == 3) - ? tflite::micro::GetEvalInput(context, node, kDepthwiseConvBiasTensor) - : nullptr; - - long long start_time = esp_timer_get_time(); - switch (input->type) { // Already know in/out types are same. - case kTfLiteFloat32: - tflite::reference_ops::DepthwiseConv( - DepthwiseConvParamsFloat(params, data.op_data), - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(filter), - tflite::micro::GetTensorData(filter), - tflite::micro::GetTensorShape(bias), - tflite::micro::GetTensorData(bias), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - case kTfLiteInt8: -#if ESP_NN - EvalQuantizedPerChannel(context, node, params, data, input, filter, bias, - output); -#else - reference_integer_ops::DepthwiseConvPerChannel( - DepthwiseConvParamsQuantized(params, data.op_data), - data.op_data.per_channel_output_multiplier, - data.op_data.per_channel_output_shift, - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(filter), - tflite::micro::GetTensorData(filter), - tflite::micro::GetTensorShape(bias), - tflite::micro::GetTensorData(bias), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); -#endif - break; - case kTfLiteUInt8: - //EvalQuantized(context, node, params, &data, input, filter, bias, output); - reference_ops::DepthwiseConv( - DepthwiseConvParamsQuantized(params, data.op_data), - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(filter), - tflite::micro::GetTensorData(filter), - tflite::micro::GetTensorShape(bias), - tflite::micro::GetTensorData(bias), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - default: - TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.", - TfLiteTypeGetName(input->type), input->type); - return kTfLiteError; - } - long long time_this_instance = esp_timer_get_time() - start_time; - dc_total_time += time_this_instance; - // printf("time this instance: %llu\n", time_this_instance / 1000); - - return kTfLiteOk; -} - -} // namespace - -TfLiteRegistration Register_DEPTHWISE_CONV_2D() { - return tflite::micro::RegisterOp(Init, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/esp_nn/fully_connected.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/esp_nn/fully_connected.cc deleted file mode 100644 index 484cffb6..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/esp_nn/fully_connected.cc +++ /dev/null @@ -1,191 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/kernels/fully_connected.h" - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/fully_connected.h" -#include "tensorflow/lite/kernels/internal/reference/integer_ops/fully_connected.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -#if ESP_NN -#include -#endif - -#include - -long long fc_total_time = 0; - -namespace tflite { -namespace { - -void* Init(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, - sizeof(OpDataFullyConnected)); -} - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - - TFLITE_DCHECK(node->user_data != nullptr); - TFLITE_DCHECK(node->builtin_data != nullptr); - - auto* data = static_cast(node->user_data); - const auto params = - static_cast(node->builtin_data); - - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kFullyConnectedInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* filter = micro_context->AllocateTempInputTensor( - node, kFullyConnectedWeightsTensor); - TF_LITE_ENSURE(context, filter != nullptr); - TfLiteTensor* bias = - micro_context->AllocateTempInputTensor(node, kFullyConnectedBiasTensor); - TfLiteTensor* output = micro_context->AllocateTempOutputTensor( - node, kFullyConnectedOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - - TF_LITE_ENSURE_TYPES_EQ(context, input->type, output->type); - TF_LITE_ENSURE_MSG(context, input->type == filter->type, - "Hybrid models are not supported on TFLite Micro."); - - TF_LITE_ENSURE_OK(context, CalculateOpDataFullyConnected( - context, params->activation, input->type, - input, filter, bias, output, data)); - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(filter); - if (bias != nullptr) { - micro_context->DeallocateTempTfLiteTensor(bias); - } - micro_context->DeallocateTempTfLiteTensor(output); - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->builtin_data != nullptr); - const auto* params = - static_cast(node->builtin_data); - - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kFullyConnectedInputTensor); - const TfLiteEvalTensor* filter = - tflite::micro::GetEvalInput(context, node, kFullyConnectedWeightsTensor); - const TfLiteEvalTensor* bias = - tflite::micro::GetEvalInput(context, node, kFullyConnectedBiasTensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kFullyConnectedOutputTensor); - - TFLITE_DCHECK(node->user_data != nullptr); - const auto& data = - *(static_cast(node->user_data)); - - long long start_time = esp_timer_get_time(); - // Checks in Prepare ensure input, output and filter types are all the same. - switch (input->type) { - case kTfLiteFloat32: { - tflite::reference_ops::FullyConnected( - FullyConnectedParamsFloat(params->activation), - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(filter), - tflite::micro::GetTensorData(filter), - tflite::micro::GetTensorShape(bias), - tflite::micro::GetTensorData(bias), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - } - - case kTfLiteInt8: { - const int32_t* bias_data = - nullptr != bias ? tflite::micro::GetTensorData(bias) - : nullptr; -#if ESP_NN - const RuntimeShape& filter_shape = tflite::micro::GetTensorShape(filter); - const RuntimeShape& output_shape = tflite::micro::GetTensorShape(output); - const int filter_dim_count = filter_shape.DimensionsCount(); - const int batches = output_shape.Dims(0); - const int output_depth = output_shape.Dims(1); - TFLITE_DCHECK_LE(output_depth, filter_shape.Dims(filter_dim_count - 2)); - const int accum_depth = filter_shape.Dims(filter_dim_count - 1); - - const int8_t *input_data = tflite::micro::GetTensorData(input); - int8_t *output_data = tflite::micro::GetTensorData(output); - const int8_t *filter_data = tflite::micro::GetTensorData(filter); - - for (int b = 0; b < batches; ++b) { - esp_nn_fully_connected_s8(input_data, -data.input_zero_point, - accum_depth, - filter_data, -data.filter_zero_point, - bias_data, output_data, output_depth, - data.output_zero_point, - data.output_shift, data.output_multiplier, - data.output_activation_min, - data.output_activation_max); - input_data += accum_depth; - output_data += output_depth; - } -#else - tflite::reference_integer_ops::FullyConnected( - FullyConnectedParamsQuantized(data), - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(filter), - tflite::micro::GetTensorData(filter), - tflite::micro::GetTensorShape(bias), bias_data, - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); -#endif - break; - } - - case kTfLiteUInt8: { - tflite::reference_ops::FullyConnected( - FullyConnectedParamsQuantized(data), - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(filter), - tflite::micro::GetTensorData(filter), - tflite::micro::GetTensorShape(bias), - tflite::micro::GetTensorData(bias), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - } - default: { - TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.", - TfLiteTypeGetName(input->type), input->type); - return kTfLiteError; - } - } - fc_total_time += esp_timer_get_time() - start_time; - return kTfLiteOk; -} - -} // namespace - -TfLiteRegistration Register_FULLY_CONNECTED() { - return tflite::micro::RegisterOp(Init, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/esp_nn/mul.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/esp_nn/mul.cc deleted file mode 100644 index 02413f5c..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/esp_nn/mul.cc +++ /dev/null @@ -1,124 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/kernels/mul.h" - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/integer_ops/mul.h" -#include "tensorflow/lite/kernels/internal/reference/mul.h" -#include "tensorflow/lite/kernels/internal/reference/process_broadcast_shapes.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/memory_helpers.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" - -#if ESP_NN -#include -#endif - -#include - -long long mul_total_time = 0; - -namespace tflite { -#if ESP_NN -void MulEvalQuantized(TfLiteContext* context, TfLiteNode* node, - const OpDataMul* data, const TfLiteEvalTensor* input1, - const TfLiteEvalTensor* input2, - TfLiteEvalTensor* output) { - tflite::ArithmeticParams op_params = {}; - op_params.quantized_activation_min = data->output_activation_min; - op_params.quantized_activation_max = data->output_activation_max; - op_params.float_activation_max = data->output_activation_max_f32; - op_params.input1_offset = -data->input1_zero_point; - op_params.input2_offset = -data->input2_zero_point; - op_params.output_offset = data->output_zero_point; - op_params.output_multiplier = data->output_multiplier; - op_params.output_shift = data->output_shift; - - bool need_broadcast = reference_ops::ProcessBroadcastShapes( - tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorShape(input2), &op_params); - - if (need_broadcast) { - reference_integer_ops::BroadcastMul4DSlow( - op_params, tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } else { - const int8_t *input1_data = tflite::micro::GetTensorData(input1); - const int8_t *input2_data = tflite::micro::GetTensorData(input2); - int8_t *out_data = tflite::micro::GetTensorData(output); - - esp_nn_mul_elementwise_s8(input1_data, input2_data, op_params.input1_offset, - op_params.input2_offset, out_data, op_params.output_offset, - op_params.output_multiplier, op_params.output_shift, - op_params.quantized_activation_min, op_params.quantized_activation_max, - MatchingElementsSize(tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorShape(output))); - } -} -#endif - -TfLiteStatus MulEval(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->builtin_data != nullptr); - auto* params = reinterpret_cast(node->builtin_data); - - TFLITE_DCHECK(node->user_data != nullptr); - const OpDataMul* data = static_cast(node->user_data); - - const TfLiteEvalTensor* input1 = - tflite::micro::GetEvalInput(context, node, kMulInput1Tensor); - const TfLiteEvalTensor* input2 = - tflite::micro::GetEvalInput(context, node, kMulInput2Tensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kMulOutputTensor); - - long long start_time = esp_timer_get_time(); - switch (input1->type) { - case kTfLiteInt8: -#if ESP_NN - MulEvalQuantized(context, node, data, input1, input2, output); -#else - EvalMulQuantizedReference(context, node, data, input1, input2, output); -#endif - break; - case kTfLiteInt32: - EvalMulQuantizedReference(context, node, data, input1, input2, output); - break; - case kTfLiteFloat32: - EvalMulFloatReference(context, node, params, data, input1, input2, - output); - break; - default: - MicroPrintf("Type %s (%d) not supported.", - TfLiteTypeGetName(input1->type), input1->type); - return kTfLiteError; - } - mul_total_time += esp_timer_get_time() - start_time; - return kTfLiteOk; -} - -TfLiteRegistration Register_MUL() { - return tflite::micro::RegisterOp(MulInit, MulPrepare, MulEval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/esp_nn/pooling.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/esp_nn/pooling.cc deleted file mode 100644 index b450929e..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/esp_nn/pooling.cc +++ /dev/null @@ -1,231 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/kernels/internal/reference/pooling.h" - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/pooling.h" - -#if ESP_NN -#include -#endif - -#include - -long long pooling_total_time = 0; - -namespace tflite { - -namespace { -#if ESP_NN -void AverageEvalQuantized(TfLiteContext* context, const TfLiteNode* node, - const TfLitePoolParams* params, const OpDataPooling* data, - const TfLiteEvalTensor* input, - TfLiteEvalTensor* output) { - - const int stride_height = params->stride_height; - const int stride_width = params->stride_width; - const int filter_height = params->filter_height; - const int filter_width = params->filter_width; - const int activation_min = data->activation_min; - const int activation_max = data->activation_max; - const int pad_height = data->padding.height; - const int pad_width = data->padding.width; - - const RuntimeShape& input_shape = tflite::micro::GetTensorShape(input); - const RuntimeShape& output_shape = tflite::micro::GetTensorShape(output); - TFLITE_DCHECK_LE(activation_min, activation_max); - TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4); - const int batches = MatchingDim(input_shape, 0, output_shape, 0); - const int depth = MatchingDim(input_shape, 3, output_shape, 3); - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - - const int8_t *input_data = tflite::micro::GetTensorData(input); - int8_t *output_data = tflite::micro::GetTensorData(output); - - const int input_size = input_width * input_height * depth; - const int output_size = output_width * output_height * depth; - - if (depth % 4 == 0) { // S3 version only supports channels multiple of 4 - for (int batch = 0; batch < batches; ++batch) { - esp_nn_avg_pool_s8(input_data, input_width, input_height, - output_data, output_width, output_height, - stride_width, stride_height, - filter_width, filter_height, - pad_width, pad_height, - activation_min, activation_max, depth); - input_data += input_size; - output_data += output_size; - } - } else { - for (int batch = 0; batch < batches; ++batch) { - esp_nn_avg_pool_s8_ansi(input_data, input_width, input_height, - output_data, output_width, output_height, - stride_width, stride_height, - filter_width, filter_height, - pad_width, pad_height, - activation_min, activation_max, depth); - input_data += input_size; - output_data += output_size; - } - } -} - -void MaxEvalQuantized(TfLiteContext* context, TfLiteNode* node, - TfLitePoolParams* params, const OpDataPooling* data, - const TfLiteEvalTensor* input, TfLiteEvalTensor* output) { - - const int stride_height = params->stride_height; - const int stride_width = params->stride_width; - const int filter_height = params->filter_height; - const int filter_width = params->filter_width; - const int activation_min = data->activation_min; - const int activation_max = data->activation_max; - const int pad_height = data->padding.height; - const int pad_width = data->padding.width; - - const RuntimeShape& input_shape = tflite::micro::GetTensorShape(input); - const RuntimeShape& output_shape = tflite::micro::GetTensorShape(output); - TFLITE_DCHECK_LE(activation_min, activation_max); - TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4); - TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4); - const int batches = MatchingDim(input_shape, 0, output_shape, 0); - const int depth = MatchingDim(input_shape, 3, output_shape, 3); - const int input_height = input_shape.Dims(1); - const int input_width = input_shape.Dims(2); - const int output_height = output_shape.Dims(1); - const int output_width = output_shape.Dims(2); - - const int8_t *input_data = tflite::micro::GetTensorData(input); - int8_t *output_data = tflite::micro::GetTensorData(output); - - const int input_size = input_width * input_height * depth; - const int output_size = output_width * output_height * depth; - if (depth % 4 == 0) { // S3 version only supports channels multiple of 4 - for (int batch = 0; batch < batches; ++batch) { - esp_nn_max_pool_s8(input_data, input_width, input_height, - output_data, output_width, output_height, - stride_width, stride_height, - filter_width, filter_height, - pad_width, pad_height, - activation_min, activation_max, depth); - input_data += input_size; - output_data += output_size; - } - } else { - for (int batch = 0; batch < batches; ++batch) { - esp_nn_max_pool_s8_ansi(input_data, input_width, input_height, - output_data, output_width, output_height, - stride_width, stride_height, - filter_width, filter_height, - pad_width, pad_height, - activation_min, activation_max, depth); - input_data += input_size; - output_data += output_size; - } - } -} -#endif - -TfLiteStatus AverageEval(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->builtin_data != nullptr); - auto* params = reinterpret_cast(node->builtin_data); - - TFLITE_DCHECK(node->user_data != nullptr); - const OpDataPooling* data = - static_cast(node->user_data); - - const TfLiteEvalTensor* input = - micro::GetEvalInput(context, node, kPoolingInputTensor); - TfLiteEvalTensor* output = - micro::GetEvalOutput(context, node, kPoolingOutputTensor); - - long long start_time = esp_timer_get_time(); - // Inputs and outputs share the same type, guaranteed by the converter. - switch (input->type) { - case kTfLiteFloat32: - AveragePoolingEvalFloat(context, node, params, data, input, output); - break; - case kTfLiteInt8: -#if ESP_NN - AverageEvalQuantized(context, node, params, data, input, output); -#else - AveragePoolingEvalQuantized(context, node, params, data, input, output); -#endif - break; - default: - TF_LITE_KERNEL_LOG(context, "Input type %s is not currently supported", - TfLiteTypeGetName(input->type)); - return kTfLiteError; - } - pooling_total_time += esp_timer_get_time() - start_time; - return kTfLiteOk; -} - -TfLiteStatus MaxEval(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->builtin_data != nullptr); - auto* params = reinterpret_cast(node->builtin_data); - - TFLITE_DCHECK(node->user_data != nullptr); - const OpDataPooling* data = - static_cast(node->user_data); - - const TfLiteEvalTensor* input = - micro::GetEvalInput(context, node, kPoolingInputTensor); - TfLiteEvalTensor* output = - micro::GetEvalOutput(context, node, kPoolingOutputTensor); - - long long start_time = esp_timer_get_time(); - switch (input->type) { - case kTfLiteFloat32: - MaxPoolingEvalFloat(context, node, params, data, input, output); - break; - case kTfLiteInt8: -#if ESP_NN - MaxEvalQuantized(context, node, params, data, input, output); -#else - MaxPoolingEvalQuantized(context, node, params, data, input, output); -#endif - break; - default: - TF_LITE_KERNEL_LOG(context, "Type %s not currently supported.", - TfLiteTypeGetName(input->type)); - return kTfLiteError; - } - pooling_total_time += esp_timer_get_time() - start_time; - return kTfLiteOk; -} - -void* Init(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(OpDataPooling)); -} - -} // namespace - -TfLiteRegistration Register_AVERAGE_POOL_2D() { - return tflite::micro::RegisterOp(Init, PoolingPrepare, AverageEval); -} - -TfLiteRegistration Register_MAX_POOL_2D() { - return tflite::micro::RegisterOp(Init, PoolingPrepare, MaxEval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/esp_nn/softmax.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/esp_nn/softmax.cc deleted file mode 100644 index 9a967839..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/esp_nn/softmax.cc +++ /dev/null @@ -1,208 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/kernels/softmax.h" - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/softmax.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -#include "freertos/FreeRTOS.h" -#include - -#if ESP_NN -#include -#endif - -long long softmax_total_time = 0; - -namespace tflite { -namespace { -// Softmax parameter data that persists in user_data -const int kInt16LUTArraySize = 513; - -struct NodeData { - SoftmaxParams op_data; -#if ESP_NN - int buffer_idx; -#endif -}; - -static void* Init(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(NodeData)); -} - -void SoftmaxQuantized(TfLiteContext* context, const TfLiteEvalTensor* input, - TfLiteEvalTensor* output, const NodeData* data) { - if (input->type == kTfLiteInt8) { - if (output->type == kTfLiteInt16) { - tflite::reference_ops::Softmax( - data->op_data, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } else { -#if ESP_NN - const int32_t input_beta_multiplier = data->op_data.input_multiplier; - const int32_t input_beta_left_shift = data->op_data.input_left_shift; - const int diff_min = data->op_data.diff_min; - const RuntimeShape input_shape = tflite::micro::GetTensorShape(input); - const RuntimeShape output_shape = tflite::micro::GetTensorShape(output); - const int trailing_dim = input_shape.DimensionsCount() - 1; - const int outer_size = - MatchingFlatSizeSkipDim(input_shape, trailing_dim, output_shape); - const int depth = - MatchingDim(input_shape, trailing_dim, output_shape, trailing_dim); - const int8_t *in_ptr = tflite::micro::GetTensorData(input); - int8_t *out_ptr = tflite::micro::GetTensorData(output); - void *scratch_buf = NULL; - if (data->buffer_idx > -1) { - scratch_buf = context->GetScratchBuffer(context, data->buffer_idx); - } - esp_nn_set_softmax_scratch_buf(scratch_buf); - esp_nn_softmax_s8(in_ptr, outer_size, depth, input_beta_multiplier, - input_beta_left_shift, diff_min, out_ptr); -#else - tflite::reference_ops::Softmax( - data->op_data, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); -#endif - } - } else { - tflite::reference_ops::SoftmaxInt16( - data->op_data, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } -} - -static TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input = tflite::micro::GetEvalInput(context, node, 0); - TfLiteEvalTensor* output = tflite::micro::GetEvalOutput(context, node, 0); - - TFLITE_DCHECK(node->user_data != nullptr); - NodeData data = *static_cast(node->user_data); - - long long start_time = esp_timer_get_time(); - switch (input->type) { - case kTfLiteFloat32: { - tflite::reference_ops::Softmax( - data.op_data, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } - break; - case kTfLiteInt8: - case kTfLiteInt16: { - SoftmaxQuantized(context, input, output, &data); - } - break; - default: - TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.", - TfLiteTypeGetName(input->type), input->type); - return kTfLiteError; - } - softmax_total_time += esp_timer_get_time() - start_time; - return kTfLiteOk; -} - -static TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - - TF_LITE_ENSURE_EQ(context, NumInputs(node), 1); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - TfLiteTensor* input = micro_context->AllocateTempInputTensor(node, 0); - TF_LITE_ENSURE(context, input != nullptr); - TF_LITE_ENSURE(context, NumDimensions(input) >= 1); - TfLiteTensor* output = micro_context->AllocateTempOutputTensor(node, 0); - TF_LITE_ENSURE(context, output != nullptr); - - TF_LITE_ENSURE(context, node->user_data != nullptr); - NodeData* data = static_cast(node->user_data); - // Only allocate LUTs for KTfLiteInt16 data type - if (input->type == kTfLiteInt16) { - void* raw_exp_lut = context->AllocatePersistentBuffer( - context, sizeof(int16_t) * kInt16LUTArraySize); - TF_LITE_ENSURE(context, raw_exp_lut != nullptr); - data->op_data.exp_lut = reinterpret_cast(raw_exp_lut); - void* one_over_one_plus_x_lut = context->AllocatePersistentBuffer( - context, sizeof(int16_t) * kInt16LUTArraySize); - TF_LITE_ENSURE(context, one_over_one_plus_x_lut != nullptr); - data->op_data.one_over_one_plus_x_lut = - reinterpret_cast(one_over_one_plus_x_lut); - } - - if (output->type == kTfLiteInt16) { - TF_LITE_ENSURE(context, - input->type == kTfLiteInt8 || input->type == kTfLiteInt16); - } else { - TF_LITE_ENSURE_EQ(context, input->type, output->type); - } - - // Populate LUT if required - if (input->type == kTfLiteInt16) { - TF_LITE_ENSURE_EQ(context, output->params.zero_point, 0); - // exp LUT only used on negative values - // we consider exp(-10.0) is insignificant to accumulation - gen_lut( - [](float value) { return std::exp(value); }, -10.0f, 0.0f, -1.0f, 1.0f, - data->op_data.exp_lut); - gen_lut( - [](float value) { return 1.0f / (1.0f + value); }, 0.0f, 1.0f, -1.0f, - 1.0f, data->op_data.one_over_one_plus_x_lut); - data->op_data.zero_point = output->params.zero_point; - data->op_data.scale = output->params.scale; - } - - auto* params = static_cast(node->builtin_data); - auto ret_val = - CalculateSoftmaxParams(context, input, output, params, &data->op_data); - -#if ESP_NN - if (output->type == kTfLiteInt8 && input->type == kTfLiteInt8) { - const int32_t input_width = input->dims->data[1]; - const int32_t input_height = input->dims->data[2]; - int scratch_buf_size = esp_nn_get_softmax_scratch_size(input_width, - input_height); - if (scratch_buf_size > 0) { - TF_LITE_ENSURE_STATUS(context->RequestScratchBufferInArena( - context, scratch_buf_size, &data->buffer_idx)); - } - } -#endif - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(output); - return ret_val; -} - -} // namespace - -TfLiteRegistration Register_SOFTMAX() { - return tflite::micro::RegisterOp(Init, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/ethosu.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/ethosu.cc deleted file mode 100644 index c305121e..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/ethosu.cc +++ /dev/null @@ -1,27 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -// -// This is a stub file for non-Ethos platforms -// -#include "tensorflow/lite/c/common.h" - -namespace tflite { - -TfLiteRegistration* Register_ETHOSU() { return nullptr; } - -const char* GetString_ETHOSU() { return ""; } - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/ethosu.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/ethosu.h deleted file mode 100644 index cfbb0d3f..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/ethosu.h +++ /dev/null @@ -1,28 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_ETHOSU_H_ -#define TENSORFLOW_LITE_MICRO_KERNELS_ETHOSU_H_ - -#include "tensorflow/lite/c/common.h" - -namespace tflite { - -TfLiteRegistration* Register_ETHOSU(); - -const char* GetString_ETHOSU(); - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_KERNELS_ETHOSU_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/exp.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/exp.cc deleted file mode 100644 index ae26f636..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/exp.cc +++ /dev/null @@ -1,78 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/kernels/internal/reference/exp.h" - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { -namespace { - -constexpr int kInputTensor = 0; -constexpr int kOutputTensor = 0; - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - - TF_LITE_ENSURE_EQ(context, NumInputs(node), 1); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - TF_LITE_ENSURE_TYPES_EQ(context, input->type, kTfLiteFloat32); - TF_LITE_ENSURE_TYPES_EQ(context, output->type, input->type); - TF_LITE_ENSURE_EQ(context, output->bytes, input->bytes); - TF_LITE_ENSURE_EQ(context, output->dims->size, input->dims->size); - for (int i = 0; i < output->dims->size; ++i) { - TF_LITE_ENSURE_EQ(context, output->dims->data[i], input->dims->data[i]); - } - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(output); - - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kInputTensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - int flat_size = MatchingFlatSize(tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorShape(output)); - - if (input->type == kTfLiteFloat32) { - reference_ops::Exp(tflite::micro::GetTensorData(input), - static_cast(flat_size), - tflite::micro::GetTensorData(output)); - } else { - TF_LITE_KERNEL_LOG(context, "Type %s (%d) currently not supported by Exp.", - TfLiteTypeGetName(input->type), input->type); - return kTfLiteError; - } - return kTfLiteOk; -} -} // namespace - -TfLiteRegistration Register_EXP() { - return tflite::micro::RegisterOp(nullptr, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/expand_dims.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/expand_dims.cc deleted file mode 100644 index 4b105bf6..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/expand_dims.cc +++ /dev/null @@ -1,152 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/micro_utils.h" - -namespace tflite { -namespace { - -constexpr int kInputTensor = 0; -constexpr int kAxisTensor = 1; -constexpr int kOutputTensor = 0; - -TfLiteStatus GetAxisValueFromTensor(TfLiteContext* context, - const TfLiteTensor* axis, - int32_t* axis_value) { - const int axis_dims = (tflite::GetTensorShape(axis)).DimensionsCount(); - if (axis_dims > 1) { - TF_LITE_KERNEL_LOG(context, "Axis has only one element for Expand_Dims.", - axis_dims); - return kTfLiteError; - } - - if (kTfLiteInt32 == (axis->type)) { - const int32_t* axis_ptr = tflite::GetTensorData(axis); - *axis_value = axis_ptr[0]; - return kTfLiteOk; - } else { - TF_LITE_KERNEL_LOG(context, - "Axis type %s (%d) not supported by Expand_Dims.", - TfLiteTypeGetName(axis->type), axis->type); - return kTfLiteError; - } -} - -// Verifies that the output tensor's dimension shape is equivalent to inserting -// a dimension of length 1 at the dimension index axis of input's shape as -// defined in https://www.tensorflow.org/api_docs/python/tf/expand_dims. -TfLiteStatus VerifyTensorDim(TfLiteContext* context, const TfLiteTensor* input, - const TfLiteTensor* axis_tensor, - const TfLiteTensor* output) { - int32_t axis_value = 0; - TF_LITE_ENSURE_OK(context, - GetAxisValueFromTensor(context, axis_tensor, &axis_value)); - - tflite::RuntimeShape input_shape = tflite::GetTensorShape(input); - if (axis_value < 0) { - axis_value = input_shape.DimensionsCount() + 1 + axis_value; - } - TF_LITE_ENSURE(context, axis_value <= input_shape.DimensionsCount()); - - // TFLM only supports fixed dimension tensor and assumes that the output shape - // is fully specified in the model. As such, TFLM directly use the pointer to - // the dimension array in the model buffer. - tflite::RuntimeShape output_shape = tflite::GetTensorShape(output); - - TF_LITE_ENSURE(context, output_shape.DimensionsCount() == - input_shape.DimensionsCount() + 1); - for (int i = 0; i < output_shape.DimensionsCount(); ++i) { - if (i < axis_value) { - TF_LITE_ENSURE(context, output_shape.Dims(i) == input_shape.Dims(i)); - } else if (i == axis_value) { - TF_LITE_ENSURE(context, output_shape.Dims(i) == 1); - } else { - TF_LITE_ENSURE(context, output_shape.Dims(i) == input_shape.Dims(i - 1)); - } - } - return kTfLiteOk; -} - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - - TF_LITE_ENSURE_EQ(context, NumInputs(node), 2); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* axis = - micro_context->AllocateTempInputTensor(node, kAxisTensor); - TF_LITE_ENSURE(context, axis != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - output->type = input->type; - if (IsDynamicTensor(axis)) { - TF_LITE_KERNEL_LOG(context, - "DynamicTensor is not yet supported by Expand_Dims."); - return kTfLiteError; - } - TF_LITE_ENSURE_OK(context, VerifyTensorDim(context, input, axis, output)); - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(axis); - micro_context->DeallocateTempTfLiteTensor(output); - return kTfLiteOk; -} - -template -void memCopyN(T* out, const T* in, const int num_elements) { - for (int i = 0; i < num_elements; ++i) { - out[i] = in[i]; - } -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kInputTensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - const int flat_size = ElementCount(*input->dims); - - switch (input->type) { - case kTfLiteFloat32: { - memCopyN(tflite::micro::GetTensorData(output), - tflite::micro::GetTensorData(input), flat_size); - } break; - case kTfLiteInt8: { - memCopyN(tflite::micro::GetTensorData(output), - tflite::micro::GetTensorData(input), flat_size); - } break; - default: - TF_LITE_KERNEL_LOG( - context, - "Expand_Dims only currently supports int8 and float32, got %d.", - input->type); - return kTfLiteError; - } - return kTfLiteOk; -} -} // namespace - -TfLiteRegistration Register_EXPAND_DIMS() { - return tflite::micro::RegisterOp(nullptr, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/fill.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/fill.cc deleted file mode 100644 index 9f438b89..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/fill.cc +++ /dev/null @@ -1,141 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/kernels/internal/reference/fill.h" - -#include - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { - -namespace { - -template -TfLiteStatus EnsureEqImpl(TfLiteContext* context, const TfLiteIntArray* array, - const TfLiteTensor* tensor) { - for (int i = 0; i < array->size; ++i) { - TF_LITE_ENSURE_EQ(context, array->data[i], GetTensorData(tensor)[i]); - } - return kTfLiteOk; -} - -// Ensure the equality of an int array and a tensor, which must be -// one-dimensional and of an integer type. -TfLiteStatus EnsureEq(TfLiteContext* context, const TfLiteIntArray* array, - const TfLiteTensor* tensor) { - TF_LITE_ENSURE_EQ(context, NumDimensions(tensor), 1); - const auto tensor_len = tensor->dims->data[0]; - TF_LITE_ENSURE_EQ(context, array->size, tensor_len); - - switch (tensor->type) { - case kTfLiteInt8: - return EnsureEqImpl(context, array, tensor); - case kTfLiteInt16: - return EnsureEqImpl(context, array, tensor); - case kTfLiteInt32: - return EnsureEqImpl(context, array, tensor); - case kTfLiteInt64: - return EnsureEqImpl(context, array, tensor); - default: - TF_LITE_KERNEL_LOG(context, - "cannot compare int array to tensor of type %d.", - tensor->type); - return kTfLiteError; - } -} - -constexpr int kDimsTensor = 0; -constexpr int kValueTensor = 1; -constexpr int kOutputTensor = 0; - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - - // Ensure inputs and outputs exist. - TfLiteTensor* dims = - micro_context->AllocateTempInputTensor(node, kDimsTensor); - TF_LITE_ENSURE(context, dims != nullptr); - TfLiteTensor* value = - micro_context->AllocateTempInputTensor(node, kValueTensor); - TF_LITE_ENSURE(context, value != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - - // The value tensor must be a scalar. - TF_LITE_ENSURE_EQ(context, NumDimensions(value), 0); - - // The value type and output type must match. - TF_LITE_ENSURE_EQ(context, value->type, output->type); - - // The dimension of the output tensor is known in model already. - TFLITE_DCHECK(output->dims != nullptr); - - if (dims->data.data != nullptr) { - // When the dims tensor is specified in model already (i.e. is not an - // activation tensor), the dims tensor must match the output tensor shape. - // As a byproduct, ensures the dims tensor is of an integer type. - TF_LITE_ENSURE_OK(context, EnsureEq(context, output->dims, dims)); - } - - micro_context->DeallocateTempTfLiteTensor(dims); - micro_context->DeallocateTempTfLiteTensor(value); - micro_context->DeallocateTempTfLiteTensor(output); - return kTfLiteOk; -} - -template -void FillImpl(const TfLiteEvalTensor* value, TfLiteEvalTensor* output) { - reference_ops::Fill( - micro::GetTensorShape(value), micro::GetTensorData(value), - micro::GetTensorShape(output), micro::GetTensorData(output)); -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* value = - micro::GetEvalInput(context, node, kValueTensor); - TfLiteEvalTensor* output = micro::GetEvalOutput(context, node, kOutputTensor); - - switch (value->type) { - case kTfLiteFloat32: - FillImpl(value, output); - break; - case kTfLiteInt32: - FillImpl(value, output); - break; - case kTfLiteInt8: - FillImpl(value, output); - break; - default: - TF_LITE_KERNEL_LOG( - context, "Fill only currently supports float32 for input 1, got %d.", - TfLiteTypeGetName(value->type)); - return kTfLiteError; - } - - return kTfLiteOk; -} - -} // namespace - -TfLiteRegistration Register_FILL() { - return tflite::micro::RegisterOp(nullptr, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/floor.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/floor.cc deleted file mode 100644 index 6b2a4cc2..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/floor.cc +++ /dev/null @@ -1,50 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/kernels/internal/reference/floor.h" - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { -namespace ops { -namespace micro { -namespace floor { - -constexpr int kInputTensor = 0; -constexpr int kOutputTensor = 0; - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kInputTensor); - TF_LITE_ENSURE_TYPES_EQ(context, input->type, kTfLiteFloat32); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - reference_ops::Floor(tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - return kTfLiteOk; -} -} // namespace floor - -TfLiteRegistration Register_FLOOR() { - return tflite::micro::RegisterOp(nullptr, nullptr, floor::Eval); -} - -} // namespace micro -} // namespace ops -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/floor_div.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/floor_div.cc deleted file mode 100644 index 333a1eba..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/floor_div.cc +++ /dev/null @@ -1,129 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/kernels/internal/reference/floor_div.h" - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/reference/binary_function.h" -#include "tensorflow/lite/kernels/internal/types.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/micro_utils.h" - -namespace tflite { -namespace { - -// Input/output tensor index. -constexpr int kInputTensor1 = 0; -constexpr int kInputTensor2 = 1; -constexpr int kOutputTensor = 0; - -TfLiteStatus CalculateOpData(TfLiteContext* context, TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - - TF_LITE_ENSURE_EQ(context, NumInputs(node), 2); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - - TfLiteTensor* input1 = - micro_context->AllocateTempInputTensor(node, kInputTensor1); - TF_LITE_ENSURE(context, input1 != nullptr); - TfLiteTensor* input2 = - micro_context->AllocateTempInputTensor(node, kInputTensor2); - TF_LITE_ENSURE(context, input2 != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - - TF_LITE_ENSURE_TYPES_EQ(context, input1->type, input2->type); - TF_LITE_ENSURE_TYPES_EQ(context, input1->type, output->type); - - micro_context->DeallocateTempTfLiteTensor(input1); - micro_context->DeallocateTempTfLiteTensor(input2); - micro_context->DeallocateTempTfLiteTensor(output); - - return kTfLiteOk; -} - -void* Init(TfLiteContext* context, const char* buffer, size_t length) { - return nullptr; -} - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - return CalculateOpData(context, node); -} - -template -TfLiteStatus EvalFloorDiv(TfLiteContext* context, - const TfLiteEvalTensor* input1, - const TfLiteEvalTensor* input2, - TfLiteEvalTensor* output) { - const T* denominator_data = tflite::micro::GetTensorData(input2); - - // Validate the denominator. - for (int i = 0; i < tflite::ElementCount(*input2->dims); ++i) { - if (std::equal_to()(denominator_data[i], 0)) { - TF_LITE_KERNEL_LOG(context, "Division by 0"); - return kTfLiteError; - } - } - - bool requires_broadcast = !tflite::micro::HaveSameShapes(input1, input2); - - if (requires_broadcast) { - reference_ops::BroadcastBinaryFunction4DSlow( - tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), denominator_data, - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output), reference_ops::FloorDiv); - } else { - reference_ops::BinaryFunction( - tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), denominator_data, - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output), reference_ops::FloorDiv); - } - - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input1 = - tflite::micro::GetEvalInput(context, node, kInputTensor1); - const TfLiteEvalTensor* input2 = - tflite::micro::GetEvalInput(context, node, kInputTensor2); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - - switch (input1->type) { - case kTfLiteFloat32: { - return EvalFloorDiv(context, input1, input2, output); - } - default: { - TF_LITE_KERNEL_LOG(context, "Type '%s' is not supported by FLOOR_DIV.", - TfLiteTypeGetName(input1->type)); - return kTfLiteError; - } - } -} - -} // namespace - -TfLiteRegistration Register_FLOOR_DIV() { - return tflite::micro::RegisterOp(Init, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/floor_mod.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/floor_mod.cc deleted file mode 100644 index 9bb49497..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/floor_mod.cc +++ /dev/null @@ -1,127 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/kernels/internal/reference/floor_mod.h" - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/reference/binary_function.h" -#include "tensorflow/lite/kernels/internal/reference/process_broadcast_shapes.h" -#include "tensorflow/lite/kernels/internal/types.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/micro_utils.h" - -// OLD-TODO(b/117523611): We should factor out a binary_op and put binary ops -// there. -namespace tflite { -namespace { - -// Input/output tensor index. -constexpr int kInputTensor1 = 0; -constexpr int kInputTensor2 = 1; -constexpr int kOutputTensor = 0; - -// OLD-TODO(b/117912880): Support quantization. - -TfLiteStatus CalculateOpData(TfLiteContext* context, TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - - TF_LITE_ENSURE_EQ(context, NumInputs(node), 2); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - - TfLiteTensor* input1 = - micro_context->AllocateTempInputTensor(node, kInputTensor1); - TF_LITE_ENSURE(context, input1 != nullptr); - TfLiteTensor* input2 = - micro_context->AllocateTempInputTensor(node, kInputTensor2); - TF_LITE_ENSURE(context, input2 != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - - TF_LITE_ENSURE_TYPES_EQ(context, input1->type, input2->type); - TF_LITE_ENSURE_TYPES_EQ(context, input1->type, output->type); - - micro_context->DeallocateTempTfLiteTensor(input1); - micro_context->DeallocateTempTfLiteTensor(input2); - micro_context->DeallocateTempTfLiteTensor(output); - - return kTfLiteOk; -} - -void* Init(TfLiteContext* context, const char* buffer, size_t length) { - return nullptr; -} - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - return CalculateOpData(context, node); -} - -template -TfLiteStatus EvalFloorMod(TfLiteContext* context, bool requires_broadcast, - const TfLiteEvalTensor* input1, - const TfLiteEvalTensor* input2, - TfLiteEvalTensor* output) { - const T* denominator_data = tflite::micro::GetTensorData(input2); - - if (requires_broadcast) { - reference_ops::BroadcastBinaryFunction4DSlow( - tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), denominator_data, - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output), reference_ops::FloorMod); - } else { - reference_ops::BinaryFunction( - tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), denominator_data, - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output), reference_ops::FloorMod); - } - - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input1 = - tflite::micro::GetEvalInput(context, node, kInputTensor1); - const TfLiteEvalTensor* input2 = - tflite::micro::GetEvalInput(context, node, kInputTensor2); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - - bool requires_broadcast = !tflite::micro::HaveSameShapes(input1, input2); - - switch (input1->type) { - case kTfLiteFloat32: { - return EvalFloorMod(context, requires_broadcast, input1, input2, - output); - } - default: { - TF_LITE_KERNEL_LOG(context, "Type '%s' is not supported by FLOOR_MOD.", - TfLiteTypeGetName(input1->type)); - return kTfLiteError; - } - } -} - -} // namespace - -TfLiteRegistration Register_FLOOR_MOD() { - return tflite::micro::RegisterOp(Init, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/fully_connected.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/fully_connected.cc deleted file mode 100644 index a083edd7..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/fully_connected.cc +++ /dev/null @@ -1,158 +0,0 @@ -/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/kernels/fully_connected.h" - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/fully_connected.h" -#include "tensorflow/lite/kernels/internal/reference/integer_ops/fully_connected.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { -namespace { - -void* Init(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, - sizeof(OpDataFullyConnected)); -} - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - - TFLITE_DCHECK(node->user_data != nullptr); - TFLITE_DCHECK(node->builtin_data != nullptr); - - auto* data = static_cast(node->user_data); - const auto params = - static_cast(node->builtin_data); - - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kFullyConnectedInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* filter = micro_context->AllocateTempInputTensor( - node, kFullyConnectedWeightsTensor); - TF_LITE_ENSURE(context, filter != nullptr); - TfLiteTensor* bias = - micro_context->AllocateTempInputTensor(node, kFullyConnectedBiasTensor); - TfLiteTensor* output = micro_context->AllocateTempOutputTensor( - node, kFullyConnectedOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - TF_LITE_ENSURE_TYPES_EQ(context, input->type, output->type); - - TF_LITE_ENSURE_OK(context, CalculateOpDataFullyConnected( - context, params->activation, input->type, - input, filter, bias, output, data)); - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(filter); - if (bias != nullptr) { - micro_context->DeallocateTempTfLiteTensor(bias); - } - micro_context->DeallocateTempTfLiteTensor(output); - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->builtin_data != nullptr); - const auto* params = - static_cast(node->builtin_data); - - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kFullyConnectedInputTensor); - const TfLiteEvalTensor* filter = - tflite::micro::GetEvalInput(context, node, kFullyConnectedWeightsTensor); - const TfLiteEvalTensor* bias = - tflite::micro::GetEvalInput(context, node, kFullyConnectedBiasTensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kFullyConnectedOutputTensor); - - TFLITE_DCHECK(node->user_data != nullptr); - const auto& data = - *(static_cast(node->user_data)); - - // Checks in Prepare ensure input, output and filter types are all the same. - switch (input->type) { - case kTfLiteFloat32: { - const float* bias_data = - nullptr != bias ? tflite::micro::GetTensorData(bias) : nullptr; - - tflite::reference_ops::FullyConnected( - FullyConnectedParamsFloat(params->activation), - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(filter), - tflite::micro::GetTensorData(filter), - tflite::micro::GetTensorShape(bias), bias_data, - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - } - - case kTfLiteInt8: { - const int32_t* bias_data = - nullptr != bias ? tflite::micro::GetTensorData(bias) - : nullptr; - - tflite::reference_integer_ops::FullyConnected( - FullyConnectedParamsQuantized(data), - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(filter), - tflite::micro::GetTensorData(filter), - tflite::micro::GetTensorShape(bias), bias_data, - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - } - - case kTfLiteInt16: { - const int64_t* bias_data = - nullptr != bias ? tflite::micro::GetTensorData(bias) - : nullptr; - - tflite::reference_integer_ops::FullyConnected( - FullyConnectedParamsQuantized(data), - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(filter), - tflite::micro::GetTensorData(filter), - tflite::micro::GetTensorShape(bias), bias_data, - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - } - - default: { - TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.", - TfLiteTypeGetName(input->type), input->type); - return kTfLiteError; - } - } - return kTfLiteOk; -} - -} // namespace - -TfLiteRegistration Register_FULLY_CONNECTED() { - return tflite::micro::RegisterOp(Init, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/fully_connected.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/fully_connected.h deleted file mode 100644 index 93026cd5..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/fully_connected.h +++ /dev/null @@ -1,104 +0,0 @@ -/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_FULLY_CONNECTED_H_ -#define TENSORFLOW_LITE_MICRO_KERNELS_FULLY_CONNECTED_H_ - -#include - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -struct OpDataFullyConnected { - // The scaling factor from input to output (aka the 'real multiplier') can - // be represented as a fixed point multiplier plus a left shift. - int32_t output_multiplier; - int output_shift; - // The range of the fused activation layer. For example for kNone and - // uint8_t these would be 0 and 255. - int32_t output_activation_min; - int32_t output_activation_max; - // The index of the temporary tensor where the quantized inputs are cached. - int input_quantized_index; - // Cached zero point values of tensors. - int32_t input_zero_point; - int32_t filter_zero_point; - int32_t output_zero_point; -}; - -extern const int kFullyConnectedInputTensor; -extern const int kFullyConnectedWeightsTensor; -extern const int kFullyConnectedBiasTensor; -extern const int kFullyConnectedOutputTensor; - -// Returns a FullyConnectedParams struct with all the parameters needed for a -// float computation. -FullyConnectedParams FullyConnectedParamsFloat( - TfLiteFusedActivation activation); - -// Returns a FullyConnectedParams struct with all the parameters needed for a -// quantized computation. -FullyConnectedParams FullyConnectedParamsQuantized( - const OpDataFullyConnected& op_data); - -TfLiteStatus CalculateOpDataFullyConnected( - TfLiteContext* context, TfLiteFusedActivation activation, - TfLiteType data_type, const TfLiteTensor* input, const TfLiteTensor* filter, - const TfLiteTensor* bias, TfLiteTensor* output, OpDataFullyConnected* data); - -// This is the most generic TfLiteRegistration. The actual supported types may -// still be target dependent. The only requirement is that every implementation -// (reference or optimized) must define this function. -TfLiteRegistration Register_FULLY_CONNECTED(); - -#if defined(CMSIS_NN) || defined(HEXAGON) -// Returns a TfLiteRegistration struct for kernel variant that only supports -// int8. -TfLiteRegistration Register_FULLY_CONNECTED_INT8(); - -#else -// Note that while this block gets used for both reference and optimized kernels -// that do not have any specialized implementations, the only goal here is to -// define fallback implementation that allow reference kernels to still be used -// from applications that call a more specific kernel variant. - -inline TfLiteRegistration Register_FULLY_CONNECTED_INT8() { - return Register_FULLY_CONNECTED(); -} - -#endif - -#if defined(CMSIS_NN) -// Returns a TfLiteRegistration struct for kernel variant that only supports -// int16. -TfLiteRegistration Register_FULLY_CONNECTED_INT16(); - -#else -// Note that while this block gets used for both reference and optimized kernels -// that do not have any specialized implementations, the only goal here is to -// define fallback implementation that allow reference kernels to still be used -// from applications that call a more specific kernel variant. - -inline TfLiteRegistration Register_FULLY_CONNECTED_INT16() { - return Register_FULLY_CONNECTED(); -} - -#endif - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_KERNELS_FULLY_CONNECTED_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/fully_connected_common.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/fully_connected_common.cc deleted file mode 100644 index e7d0056c..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/fully_connected_common.cc +++ /dev/null @@ -1,83 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/fully_connected.h" -#include "tensorflow/lite/kernels/internal/reference/integer_ops/fully_connected.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/fully_connected.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { - -const int kFullyConnectedInputTensor = 0; -const int kFullyConnectedWeightsTensor = 1; -const int kFullyConnectedBiasTensor = 2; -const int kFullyConnectedOutputTensor = 0; - -FullyConnectedParams FullyConnectedParamsQuantized( - const OpDataFullyConnected& op_data) { - FullyConnectedParams op_params; - op_params.input_offset = -op_data.input_zero_point; - op_params.weights_offset = -op_data.filter_zero_point; - op_params.output_offset = op_data.output_zero_point; - op_params.output_multiplier = op_data.output_multiplier; - op_params.output_shift = op_data.output_shift; - op_params.quantized_activation_min = op_data.output_activation_min; - op_params.quantized_activation_max = op_data.output_activation_max; - return op_params; -} - -FullyConnectedParams FullyConnectedParamsFloat( - TfLiteFusedActivation activation) { - FullyConnectedParams op_params; - CalculateActivationRange(activation, &op_params.float_activation_min, - &op_params.float_activation_max); - return op_params; -} - -TfLiteStatus CalculateOpDataFullyConnected( - TfLiteContext* context, TfLiteFusedActivation activation, - TfLiteType data_type, const TfLiteTensor* input, const TfLiteTensor* filter, - const TfLiteTensor* bias, TfLiteTensor* output, - OpDataFullyConnected* data) { - if (data_type != kTfLiteFloat32) { - double real_multiplier = 0.0; - TF_LITE_ENSURE_STATUS(GetQuantizedConvolutionMultipler( - context, input, filter, bias, output, &real_multiplier)); - QuantizeMultiplier(real_multiplier, &data->output_multiplier, - &data->output_shift); - - data->input_zero_point = input->params.zero_point; - // Filter weights will always be symmetric quantized since we only support - // int8 quantization. See - // https://github.com/tensorflow/tensorflow/issues/44912 for additional - // context. - TFLITE_DCHECK(filter->params.zero_point == 0); - data->filter_zero_point = filter->params.zero_point; - data->output_zero_point = output->params.zero_point; - - return CalculateActivationRangeQuantized(context, activation, output, - &data->output_activation_min, - &data->output_activation_max); - } - return kTfLiteOk; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/gather.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/gather.cc deleted file mode 100644 index 6035efa7..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/gather.cc +++ /dev/null @@ -1,224 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/micro_utils.h" - -namespace tflite { -namespace { - -constexpr int kInputTensor = 0; -constexpr int kInputPositions = 1; -constexpr int kOutputTensor = 0; - -template -TfLiteStatus Gather(const TfLiteGatherParams* params, - const TfLiteEvalTensor* input, - const TfLiteEvalTensor* coords, TfLiteEvalTensor* output) { - const InputT* input_data = tflite::micro::GetTensorData(input); - const CoordsT* coords_data = tflite::micro::GetTensorData(coords); - InputT* output_data = tflite::micro::GetTensorData(output); - const TfLiteIntArray* input_dims = input->dims; - const int input_dims_size = input_dims->size; - int axis = params->axis; - if (axis < 0) { - axis += input_dims_size; - } - TFLITE_DCHECK_GE(axis, 0); - TFLITE_DCHECK_LT(axis, input_dims_size); - - int batch_dims = params->batch_dims; - // batch_dims should be in range: [-rank(coords), rank(coords)]. - // Negative batch_dims is added with rank of coords. - const TfLiteIntArray* coords_dims = coords->dims; - const int coords_dims_size = coords_dims->size; - if (batch_dims < 0) { - batch_dims += coords_dims_size; - } - TFLITE_DCHECK_GE(batch_dims, 0); - TFLITE_DCHECK_LT(batch_dims, input_dims_size); - TFLITE_DCHECK_LE(batch_dims, coords_dims_size); - TFLITE_DCHECK_GE(axis, batch_dims); - for (int i = 0; i < batch_dims; ++i) { - TFLITE_DCHECK_EQ(input_dims->data[i], coords_dims->data[i]); - } - - const int axis_size = input_dims->data[axis]; - - int batch_size = 1; - for (int i = 0; i < batch_dims; ++i) { - batch_size *= input_dims->data[i]; - } - int outer_size = 1; - for (int i = batch_dims; i < axis; ++i) { - outer_size *= input_dims->data[i]; - } - int inner_size = 1; - for (int i = axis + 1; i < input_dims_size; ++i) { - inner_size *= input_dims->data[i]; - } - int coord_size = 1; - for (int i = batch_dims; i < coords_dims_size; ++i) { - coord_size *= coords_dims->data[i]; - } - - for (int batch = 0; batch < batch_size; ++batch) { - for (int outer = 0; outer < outer_size; ++outer) { - for (int coord = 0; coord < coord_size; ++coord) { - TFLITE_DCHECK_GE(coords_data[coord], 0); - TFLITE_DCHECK_LT(coords_data[coord], axis_size); - std::memcpy(output_data + - (((batch * outer_size) + outer) * coord_size + coord) * - inner_size, - input_data + (((batch * outer_size) + outer) * axis_size + - coords_data[batch * coord_size + coord]) * - inner_size, - sizeof(InputT) * inner_size); - } - } - } - return kTfLiteOk; -} - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - - TF_LITE_ENSURE_EQ(context, NumInputs(node), 2); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - - const auto* params = - reinterpret_cast(node->builtin_data); - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* coords = - micro_context->AllocateTempInputTensor(node, kInputPositions); - TF_LITE_ENSURE(context, coords != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - - switch (coords->type) { - case kTfLiteInt32: - break; - default: - TF_LITE_KERNEL_LOG(context, - "Positions of type '%s' are not supported by gather.", - TfLiteTypeGetName(coords->type)); - return kTfLiteError; - break; - } - - // Assign to output the input type. - output->type = input->type; - - // Check conditions for different types. - switch (input->type) { - case kTfLiteFloat32: - case kTfLiteInt8: - break; - default: - TF_LITE_KERNEL_LOG(context, "Type '%s' is not supported by gather.", - TfLiteTypeGetName(input->type)); - return kTfLiteError; - break; - } - - int axis = params->axis; - if (axis < 0) { - axis += NumDimensions(input); - } - TF_LITE_ENSURE(context, 0 <= axis && axis < NumDimensions(input)); - - int batch_dims = params->batch_dims; - // batch_dims should be in range: [-rank(coords), rank(coords)]. - // Negative batch_dims is added with rank of coords. - if (batch_dims < 0) { - batch_dims += NumDimensions(coords); - } - TF_LITE_ENSURE(context, batch_dims <= axis); - TF_LITE_ENSURE(context, 0 <= batch_dims && batch_dims < NumDimensions(input)); - TF_LITE_ENSURE(context, batch_dims <= NumDimensions(coords)); - for (int i = 0; i < batch_dims; ++i) { - TF_LITE_ENSURE_EQ(context, input->dims->data[i], coords->dims->data[i]); - } - - // GATHER updates the output tensor dimensions, but TfLiteTensor in the - // MicroInterpreter is a temporary allocation. We must therefore relocate the - // dims from the FlatBuffer to the persistant storage arena. - TfLiteEvalTensor* output_eval = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - TF_LITE_ENSURE_OK(context, tflite::micro::CreateWritableTensorDimsWithCopy( - context, output, output_eval)); - - TfLiteIntArray* output_shape = output->dims; - output_shape->size = - NumDimensions(input) + NumDimensions(coords) - 1 - batch_dims; - int output_index = 0; - for (int i = 0; i < axis; ++i) { - output_shape->data[output_index++] = input->dims->data[i]; - } - for (int i = batch_dims; i < coords->dims->size; ++i) { - output_shape->data[output_index++] = coords->dims->data[i]; - } - for (int i = axis + 1; i < input->dims->size; ++i) { - output_shape->data[output_index++] = input->dims->data[i]; - } - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(coords); - micro_context->DeallocateTempTfLiteTensor(output); - - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const auto* params = - reinterpret_cast(node->builtin_data); - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kInputTensor); - const TfLiteEvalTensor* coords = - tflite::micro::GetEvalInput(context, node, kInputPositions); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - - if (coords->type == kTfLiteInt32) { - switch (input->type) { - case kTfLiteFloat32: - return Gather(params, input, coords, output); - break; - case kTfLiteInt8: - return Gather(params, input, coords, output); - break; - default: - TF_LITE_KERNEL_LOG(context, "Type '%s' is not supported by gather.", - TfLiteTypeGetName(input->type)); - return kTfLiteError; - break; - } - } - return kTfLiteOk; -} -} // namespace - -TfLiteRegistration Register_GATHER() { - return tflite::micro::RegisterOp(nullptr, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/gather_nd.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/gather_nd.cc deleted file mode 100644 index 4327177d..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/gather_nd.cc +++ /dev/null @@ -1,210 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/micro_utils.h" - -namespace tflite { -namespace { - -constexpr int kParams = 0; -constexpr int kIndices = 1; -constexpr int kOutputTensor = 0; -constexpr int MAX_INDICES_ND = 5; - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - - TF_LITE_ENSURE_EQ(context, NumInputs(node), 2); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - - TfLiteTensor* params = micro_context->AllocateTempInputTensor(node, kParams); - TF_LITE_ENSURE(context, params != nullptr); - TfLiteTensor* indices = - micro_context->AllocateTempInputTensor(node, kIndices); - TF_LITE_ENSURE(context, indices != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - - switch (params->type) { - case kTfLiteFloat32: - case kTfLiteInt8: - break; - default: - TF_LITE_KERNEL_LOG(context, - "Params of type '%s' are not supported by gather_nd.", - TfLiteTypeGetName(params->type)); - return kTfLiteError; - break; - } - switch (indices->type) { - case kTfLiteInt32: - break; - default: - TF_LITE_KERNEL_LOG(context, - "Indices of type '%s' are not supported by gather_nd.", - TfLiteTypeGetName(indices->type)); - return kTfLiteError; - } - - const int params_rank = NumDimensions(params); - const int indices_rank = NumDimensions(indices); - const int indices_nd = SizeOfDimension(indices, indices_rank - 1); - if (params_rank < 1) { - TF_LITE_KERNEL_LOG(context, "Params must be at least a vector."); - return kTfLiteError; - } - if (indices_rank < 1) { - TF_LITE_KERNEL_LOG(context, "Indices must be at least a vector."); - return kTfLiteError; - } - if (indices_nd > params_rank) { - TF_LITE_KERNEL_LOG( - context, "Index innermost dimension length must be <= params rank."); - return kTfLiteError; - } - if (indices_nd > MAX_INDICES_ND) { - TF_LITE_KERNEL_LOG(context, - "Index innermost dimension length must not exceed %d.", - MAX_INDICES_ND); - return kTfLiteError; - } - - // Assign to output the input type. - output->type = params->type; - - // TFLM gather_nd does not create the output tensor, but it needs to ensure - // that the output shape is correct. The result shape is - // indices.shape[:-1] + params.shape[indices.shape[-1]:] - TfLiteIntArray* output_shape = output->dims; - int output_index = 0; - for (int i = 0; i < indices_rank - 1; ++i) { - output_shape->data[output_index++] = indices->dims->data[i]; - } - for (int i = indices_nd; i < params_rank; ++i) { - output_shape->data[output_index++] = params->dims->data[i]; - } - output_shape->size = output_index; - - micro_context->DeallocateTempTfLiteTensor(params); - micro_context->DeallocateTempTfLiteTensor(indices); - micro_context->DeallocateTempTfLiteTensor(output); - return kTfLiteOk; -} - -template -TfLiteStatus GatherNd(const TfLiteEvalTensor* params, - const TfLiteEvalTensor* indices, - TfLiteEvalTensor* output) { - const int indices_dims = indices->dims->size; - const int indices_nd = indices->dims->data[indices_dims - 1]; - const int params_dims = params->dims->size; - const IndicesT* index_data = tflite::micro::GetTensorData(indices); - const ParamsT* param_data = tflite::micro::GetTensorData(params); - ParamsT* output_data = tflite::micro::GetTensorData(output); - - int n_slices = 1; - for (int i = 0; i < indices_dims - 1; ++i) { - n_slices *= indices->dims->data[i]; - } - - // If indices[-1] == params.rank, fetch single elements. - // If indices[-1] < params.rank, fetch slices. - int slice_size = 1; - for (int i = indices_nd; i < params_dims; ++i) { - slice_size *= params->dims->data[i]; - } - - int params_flat_size = ElementCount(*params->dims); - int remain_flat_size = params_flat_size; - - // Number of elements per dimension - int dims_to_count[MAX_INDICES_ND]; - for (int i = 0; i < indices_nd; ++i) { - dims_to_count[i] = remain_flat_size / params->dims->data[i]; - remain_flat_size = dims_to_count[i]; - } - - for (int i = 0; i < n_slices; ++i) { - int from_pos = 0; - for (int j = 0; j < indices_nd; ++j) { - int offset = i * indices_nd + j; - IndicesT index = index_data[offset]; - from_pos += index * dims_to_count[j]; - } - if (from_pos < 0 || from_pos + slice_size > params_flat_size) { - return kTfLiteError; - } - std::memcpy(output_data + i * slice_size, param_data + from_pos, - sizeof(ParamsT) * slice_size); - } - return kTfLiteOk; -} - -template -TfLiteStatus EvalGatherNd(TfLiteContext* context, - const TfLiteEvalTensor* params, - const TfLiteEvalTensor* indices, - TfLiteEvalTensor* output) { - TfLiteStatus status = kTfLiteError; - switch (params->type) { - case kTfLiteFloat32: - status = GatherNd(params, indices, output); - break; - case kTfLiteInt8: - status = GatherNd(params, indices, output); - break; - default: - TF_LITE_KERNEL_LOG(context, - "Params type '%s' are not supported by gather_nd.", - TfLiteTypeGetName(params->type)); - return kTfLiteError; - } - if (status != kTfLiteOk) { - TF_LITE_KERNEL_LOG(context, "gather_nd index out of bounds"); - } - return status; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* params = - tflite::micro::GetEvalInput(context, node, kParams); - const TfLiteEvalTensor* indices = - tflite::micro::GetEvalInput(context, node, kIndices); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - - switch (indices->type) { - case kTfLiteInt32: - return EvalGatherNd(context, params, indices, output); - break; - default: - TF_LITE_KERNEL_LOG(context, - "Indices of type '%s' are not supported by gather_nd.", - TfLiteTypeGetName(indices->type)); - return kTfLiteError; - } -} -} // namespace - -TfLiteRegistration Register_GATHER_ND() { - return tflite::micro::RegisterOp(nullptr, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/hard_swish.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/hard_swish.cc deleted file mode 100644 index 055e12e6..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/hard_swish.cc +++ /dev/null @@ -1,75 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/kernels/internal/reference/hard_swish.h" - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/internal/types.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/kernels/hard_swish.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" -#include "tensorflow/lite/micro/micro_utils.h" - -namespace tflite { -namespace { -void* HardSwishInit(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(HardSwishParams)); -} - -TfLiteStatus HardSwishEval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kHardSwishInputTensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kHardSwishOutputTensor); - HardSwishParams* params = static_cast(node->user_data); - - switch (input->type) { - case kTfLiteFloat32: { - tflite::reference_ops::HardSwish( - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } break; - case kTfLiteInt8: { - tflite::reference_ops::HardSwish( - *params, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } break; - default: { - MicroPrintf("Unsupported type %s", TfLiteTypeGetName(input->type)); - return kTfLiteError; - } - } - return kTfLiteOk; -} - -} // namespace - -TfLiteRegistration Register_HARD_SWISH() { - return tflite::micro::RegisterOp(HardSwishInit, tflite::HardSwishPrepare, - HardSwishEval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/hard_swish.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/hard_swish.h deleted file mode 100644 index 3ffe60dc..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/hard_swish.h +++ /dev/null @@ -1,30 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_HARD_SWISH_H_ -#define TENSORFLOW_LITE_MICRO_KERNELS_HARD_SWISH_H_ - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" - -namespace tflite { - -extern const int kHardSwishInputTensor; -extern const int kHardSwishOutputTensor; - -TfLiteStatus HardSwishPrepare(TfLiteContext* context, TfLiteNode* node); -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_KERNELS_HARD_SWISH_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/hard_swish_common.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/hard_swish_common.cc deleted file mode 100644 index 8f846522..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/hard_swish_common.cc +++ /dev/null @@ -1,86 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/hard_swish.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/internal/types.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/kernels/hard_swish.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/micro_utils.h" - -namespace tflite { - -const int kHardSwishInputTensor = 0; -const int kHardSwishOutputTensor = 0; - -TfLiteStatus HardSwishPrepare(TfLiteContext* context, TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - - TFLITE_DCHECK(node->user_data != nullptr); - TF_LITE_ENSURE_EQ(context, NumInputs(node), 1); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kHardSwishInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kHardSwishOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - - if (input->type == kTfLiteInt8) { - HardSwishParams* params = static_cast(node->user_data); - - params->input_zero_point = input->params.zero_point; - params->output_zero_point = output->params.zero_point; - - const float input_scale = input->params.scale; - const float hires_input_scale = (1.0f / 128.0f) * input_scale; - const float reluish_scale = 3.0f / 32768.0f; - const float output_scale = output->params.scale; - - const double output_multiplier = - static_cast(hires_input_scale / output_scale); - int32_t output_multiplier_fixedpoint_int32; - QuantizeMultiplier(output_multiplier, &output_multiplier_fixedpoint_int32, - ¶ms->output_multiplier_exponent); - DownScaleInt32ToInt16Multiplier( - output_multiplier_fixedpoint_int32, - ¶ms->output_multiplier_fixedpoint_int16); - - TF_LITE_ENSURE(context, params->output_multiplier_exponent <= 0); - - const double reluish_multiplier = - static_cast(hires_input_scale / reluish_scale); - int32_t reluish_multiplier_fixedpoint_int32; - QuantizeMultiplier(reluish_multiplier, &reluish_multiplier_fixedpoint_int32, - ¶ms->reluish_multiplier_exponent); - DownScaleInt32ToInt16Multiplier( - reluish_multiplier_fixedpoint_int32, - ¶ms->reluish_multiplier_fixedpoint_int16); - } - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(output); - - return kTfLiteOk; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/if.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/if.cc deleted file mode 100644 index 39eca8b4..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/if.cc +++ /dev/null @@ -1,121 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include - -#include - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/memory_helpers.h" -#include "tensorflow/lite/micro/micro_context.h" -#include "tensorflow/lite/micro/micro_graph.h" -#include "tensorflow/lite/schema/schema_generated.h" - -namespace tflite { - -namespace { - -struct OpData { - int then_subgraph_index; - int else_subgraph_index; -}; - -void* Init(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(OpData)); -} - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - OpData* op_data = reinterpret_cast(node->user_data); - const auto* params = - reinterpret_cast(node->builtin_data); - op_data->then_subgraph_index = params->then_subgraph_index; - op_data->else_subgraph_index = params->else_subgraph_index; - - TF_LITE_ENSURE(context, node->inputs->size > 0); - - // The first input is the condition. - tflite::MicroContext* micro_context = tflite::GetMicroContext(context); - TfLiteTensor* cond = micro_context->AllocateTempInputTensor(node, 0); - - TF_LITE_ENSURE(context, cond != nullptr); - TF_LITE_ENSURE_EQ(context, cond->type, kTfLiteBool); - TF_LITE_ENSURE_EQ(context, NumElements(cond), 1); - - micro_context->DeallocateTempTfLiteTensor(cond); - - // The first input of the node is the condition. The rest of inputs are - // passed to the branch subgraphs. Therefore, the number of subgraph inputs - // will be the number of node inputs - 1. - size_t num_inputs = node->inputs->size - 1; - size_t num_outputs = node->outputs->size; - - MicroGraph& graph_info = micro_context->graph(); - - TF_LITE_ENSURE(context, - op_data->then_subgraph_index < graph_info.NumSubgraphs()); - TF_LITE_ENSURE(context, - op_data->else_subgraph_index < graph_info.NumSubgraphs()); - - TF_LITE_ENSURE_EQ(context, num_inputs, - graph_info.NumSubgraphInputs(op_data->then_subgraph_index)); - TF_LITE_ENSURE_EQ( - context, num_outputs, - graph_info.NumSubgraphOutputs(op_data->then_subgraph_index)); - - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const OpData* op_data = reinterpret_cast(node->user_data); - - tflite::MicroContext* micro_context = tflite::GetMicroContext(context); - TfLiteTensor* cond = micro_context->AllocateTempInputTensor(node, 0); - - TF_LITE_ENSURE(context, cond != nullptr); - bool cond_value = cond->data.b[0]; - micro_context->DeallocateTempTfLiteTensor(cond); - - MicroGraph* graph_info = µ_context->graph(); - // Currently we copy the input / output between the subgraphs. - int active_branch_subgraph_index = - cond_value ? op_data->then_subgraph_index : op_data->else_subgraph_index; - - TF_LITE_ENSURE_OK(context, - tflite::micro::CopyOpInputsToSubgraphInputs( - context, node, graph_info, active_branch_subgraph_index, - /*first_tensor_idx=*/1)); - - TF_LITE_ENSURE_OK(context, - graph_info->InvokeSubgraph(active_branch_subgraph_index)); - - TF_LITE_ENSURE_OK( - context, tflite::micro::CopySubgraphOutputsToOpOutputs( - context, node, graph_info, active_branch_subgraph_index)); - - return kTfLiteOk; -} - -} // namespace. - -TfLiteRegistration Register_IF() { - return tflite::micro::RegisterOp(Init, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/kernel_runner.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/kernel_runner.cc deleted file mode 100644 index dd4c14f4..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/kernel_runner.cc +++ /dev/null @@ -1,110 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/kernels/kernel_runner.h" - -#include "tensorflow/lite/micro/arena_allocator/single_arena_buffer_allocator.h" -#include "tensorflow/lite/micro/micro_arena_constants.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" -#include "tensorflow/lite/micro/test_helpers.h" - -namespace tflite { -namespace micro { - -// TODO(b/161841696): Consider moving away from global arena buffers: -constexpr int KernelRunner::kKernelRunnerBufferSize_; -uint8_t KernelRunner::kKernelRunnerBuffer_[]; - -void ClearBufferApi(TfLiteContext* context_) { - context_->GetScratchBuffer = nullptr; - context_->GetExternalContext = nullptr; - context_->AllocatePersistentBuffer = nullptr; - context_->RequestScratchBufferInArena = nullptr; -} - -KernelRunner::KernelRunner(const TfLiteRegistration& registration, - TfLiteTensor* tensors, int tensors_size, - TfLiteIntArray* inputs, TfLiteIntArray* outputs, - void* builtin_data, TfLiteIntArray* intermediates) - : registration_(registration), - allocator_(SingleArenaBufferAllocator::Create(GetMicroErrorReporter(), - kKernelRunnerBuffer_, - kKernelRunnerBufferSize_)), - mock_micro_graph_(allocator_), - fake_micro_context_(tensors, allocator_, &mock_micro_graph_) { - // Prepare TfLiteContext: - context_.impl_ = static_cast(&fake_micro_context_); - context_.ReportError = MicroContextReportOpError; - context_.recommended_num_threads = 1; - context_.GetTensor = MicroContextGetTensor; - context_.GetEvalTensor = MicroContextGetEvalTensor; - tflite::micro::ClearBufferApi(&context_); - context_.AllocatePersistentBuffer = MicroContextAllocatePersistentBuffer; - - context_.recommended_num_threads = 0; - - // Prepare TfLiteNode: - node_.inputs = inputs; - node_.outputs = outputs; - node_.builtin_data = builtin_data; - node_.intermediates = intermediates; -} - -bool KernelRunner::ValidateTempBufferDeallocated() { - return fake_micro_context_.IsAllTempTfLiteTensorDeallocated(); -} - -TfLiteStatus KernelRunner::InitAndPrepare(const char* init_data, - size_t length) { - if (registration_.init) { - tflite::micro::ClearBufferApi(&context_); - context_.AllocatePersistentBuffer = MicroContextAllocatePersistentBuffer; - node_.user_data = registration_.init(&context_, init_data, length); - } - - TF_LITE_ENSURE(&context_, ValidateTempBufferDeallocated()); - - if (registration_.prepare) { - tflite ::micro::ClearBufferApi(&context_); - context_.AllocatePersistentBuffer = MicroContextAllocatePersistentBuffer; - context_.RequestScratchBufferInArena = - MicroContextRequestScratchBufferInArena; - context_.GetExternalContext = MicroContextGetExternalContext; - TF_LITE_ENSURE_STATUS(registration_.prepare(&context_, &node_)); - } - - TF_LITE_ENSURE(&context_, ValidateTempBufferDeallocated()); - - return kTfLiteOk; -} - -TfLiteStatus KernelRunner::Invoke() { - tflite::micro::ClearBufferApi(&context_); - context_.GetScratchBuffer = MicroContextGetScratchBuffer; - - if (registration_.invoke == nullptr) { - MicroPrintf("TfLiteRegistration missing invoke function pointer!"); - return kTfLiteError; - } - - TF_LITE_ENSURE_STATUS(registration_.invoke(&context_, &node_)); - - TF_LITE_ENSURE(&context_, ValidateTempBufferDeallocated()); - - return kTfLiteOk; -} - -} // namespace micro -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/kernel_runner.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/kernel_runner.h deleted file mode 100644 index 4482b70e..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/kernel_runner.h +++ /dev/null @@ -1,76 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_KERNEL_RUNNER_H_ -#define TENSORFLOW_LITE_MICRO_KERNELS_KERNEL_RUNNER_H_ - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/micro/arena_allocator/single_arena_buffer_allocator.h" -#include "tensorflow/lite/micro/fake_micro_context.h" -#include "tensorflow/lite/micro/mock_micro_graph.h" - -namespace tflite { -namespace micro { - -// Helper class to perform a simulated kernel (i.e. TfLiteRegistration) -// lifecycle (init, prepare, invoke). All internal allocations are handled by -// this class. Simply pass in the registration, list of required tensors, inputs -// array, outputs array, and any pre-builtin data. Calling Invoke() will -// automatically walk the kernel and outputs will be ready on the TfLiteTensor -// output provided during construction. -class KernelRunner { - public: - KernelRunner(const TfLiteRegistration& registration, TfLiteTensor* tensors, - int tensors_size, TfLiteIntArray* inputs, - TfLiteIntArray* outputs, void* builtin_data, - TfLiteIntArray* intermediates = nullptr); - - // Calls init and prepare on the kernel (i.e. TfLiteRegistration) struct. Any - // exceptions will be DebugLog'd and returned as a status code. - TfLiteStatus InitAndPrepare(const char* init_data = nullptr, - size_t length = 0); - - // Calls init, prepare, and invoke on a given TfLiteRegistration pointer. - // After successful invoke, results will be available in the output tensor as - // passed into the constructor of this class. - TfLiteStatus Invoke(); - - // Returns a pointer to the internal MockMicroGraph which KernelRunner uses - // to stub out MicroGraph methods and track invocations on each subgraph. - MockMicroGraph* GetMockGraph() { return &mock_micro_graph_; } - - // Returns true if all temp buffer in tests are deallocated. - // TODO(b/209453859): move this function to private after deallocation checks - // are enabled for all kernel tests. - bool ValidateTempBufferDeallocated(); - - private: - static constexpr int kKernelRunnerBufferSize_ = 10000; - static uint8_t kKernelRunnerBuffer_[kKernelRunnerBufferSize_]; - - TfLiteContext context_ = {}; - TfLiteNode node_ = {}; - const TfLiteRegistration& registration_; - - SingleArenaBufferAllocator* allocator_; - MockMicroGraph mock_micro_graph_; - FakeMicroContext fake_micro_context_; -}; - -} // namespace micro -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_KERNELS_KERNEL_RUNNER_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/kernel_util.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/kernel_util.cc deleted file mode 100644 index 91c0bc91..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/kernel_util.cc +++ /dev/null @@ -1,218 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/micro/memory_helpers.h" - -namespace tflite { -namespace micro { - -namespace { - -int ValidateTensorIndexing(const TfLiteContext* context, int index, - int max_size, const int* tensor_indices) { - if (index >= 0 && index < max_size) { - const int tensor_index = tensor_indices[index]; - if (tensor_index != kTfLiteOptionalTensor) { - return tensor_index; - } - } - return -1; -} - -} // namespace - -TfLiteRegistration RegisterOp( - void* (*init)(TfLiteContext* context, const char* buffer, size_t length), - TfLiteStatus (*prepare)(TfLiteContext* context, TfLiteNode* node), - TfLiteStatus (*invoke)(TfLiteContext* context, TfLiteNode* node)) { - return {/*init=*/init, - /*free=*/nullptr, - /*prepare=*/prepare, - /*invoke=*/invoke, - /*profiling_string=*/nullptr, - /*builtin_code=*/0, - /*custom_name=*/nullptr, - /*version=*/0, - /*registration_external=*/nullptr}; -} - -// Returns a mutable tensor for a given input index. is_variable must be checked -// during prepare when the full TfLiteTensor is available. -TfLiteEvalTensor* GetMutableEvalInput(const TfLiteContext* context, - const TfLiteNode* node, int index) { - TFLITE_DCHECK(context != nullptr); - TFLITE_DCHECK(node != nullptr); - const int tensor_index = ValidateTensorIndexing( - context, index, node->inputs->size, node->inputs->data); - - if (tensor_index < 0) { - return nullptr; - } - - return context->GetEvalTensor(context, node->inputs->data[index]); -} - -// Returns the TfLiteEvalTensor struct for a given input index in a node. -const TfLiteEvalTensor* GetEvalInput(const TfLiteContext* context, - const TfLiteNode* node, int index) { - return GetMutableEvalInput(context, node, index); -} - -// Returns the TfLiteEvalTensor struct for a given output index in a node. -TfLiteEvalTensor* GetEvalOutput(const TfLiteContext* context, - const TfLiteNode* node, int index) { - TFLITE_DCHECK(context != nullptr); - TFLITE_DCHECK(node != nullptr); - return context->GetEvalTensor(context, node->outputs->data[index]); -} - -bool HaveSameShapes(const TfLiteEvalTensor* input1, - const TfLiteEvalTensor* input2) { - TFLITE_DCHECK(input1 != nullptr); - TFLITE_DCHECK(input2 != nullptr); - return TfLiteIntArrayEqual(input1->dims, input2->dims); -} - -const RuntimeShape GetTensorShape(const TfLiteEvalTensor* tensor) { - if (tensor == nullptr || tensor->dims == nullptr) { - return RuntimeShape(); - } - TfLiteIntArray* dims = tensor->dims; - const int dims_size = dims->size; - const int32_t* dims_data = reinterpret_cast(dims->data); - return RuntimeShape(dims_size, dims_data); -} - -PaddingType RuntimePaddingType(TfLitePadding padding) { - switch (padding) { - case TfLitePadding::kTfLitePaddingSame: - return PaddingType::kSame; - case TfLitePadding::kTfLitePaddingValid: - return PaddingType::kValid; - case TfLitePadding::kTfLitePaddingUnknown: - default: - return PaddingType::kNone; - } -} - -// Relocate tensor dims from FlatBuffer to the persistent storage arena. -// The old dims data is copied to the new storage area. -// The tensor and eval_tensor must be the same tensor. -// Only use during Prepare phase. -TfLiteStatus CreateWritableTensorDimsWithCopy(TfLiteContext* context, - TfLiteTensor* tensor, - TfLiteEvalTensor* eval_tensor) { - TF_LITE_ENSURE(context, tensor != nullptr); - TF_LITE_ENSURE(context, eval_tensor != nullptr); - TF_LITE_ENSURE(context, context->AllocatePersistentBuffer != nullptr); - int ranks = tensor->dims->size; - size_t alloc_size = TfLiteIntArrayGetSizeInBytes(ranks); - TfLiteIntArray* new_dims = static_cast( - context->AllocatePersistentBuffer(context, alloc_size)); - TfLiteIntArray* old_dims = tensor->dims; - new_dims->size = ranks; - tensor->dims = new_dims; - eval_tensor->dims = new_dims; - for (int i = 0; i < ranks; i++) { - new_dims->data[i] = old_dims->data[i]; - } - - return kTfLiteOk; -} - -// Verify that both tensors have the same type and size, then return the size -// of both tensors in bytes if they are the same, or -1 if they are different. -size_t ValidateAndGetTensorSizes(const TfLiteEvalTensor* tensor1, - const TfLiteEvalTensor* tensor2) { - TFLITE_DCHECK(tensor1->type == tensor2->type); - size_t tensor1_size = 0; - size_t tensor2_size = 0; - TfLiteEvalTensorByteLength(tensor1, &tensor1_size); - TfLiteEvalTensorByteLength(tensor2, &tensor2_size); - return (tensor1_size == tensor2_size) ? tensor1_size : -1; -} - -TfLiteStatus CopyOpInputsToOpOutputs(TfLiteContext* context, TfLiteNode* node) { - TF_LITE_ENSURE(context, node->inputs->size == node->outputs->size); - for (int i = 0; i < node->inputs->size; i++) { - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, i); - TfLiteEvalTensor* output = tflite::micro::GetEvalOutput(context, node, i); - int bytes = ValidateAndGetTensorSizes(input, output); - TF_LITE_ENSURE(context, bytes >= 0); - memcpy(output->data.raw, input->data.raw, bytes); - } - return kTfLiteOk; -} - -TfLiteStatus CopyOpInputsToSubgraphInputs(TfLiteContext* context, - TfLiteNode* node, - MicroGraph* graph_info, - int subgraph_idx, - int first_tensor_idx) { - TF_LITE_ENSURE(context, - static_cast(node->inputs->size - first_tensor_idx) == - graph_info->NumSubgraphInputs(subgraph_idx)); - for (int i = 0; i < node->inputs->size - first_tensor_idx; i++) { - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, i + first_tensor_idx); - TfLiteEvalTensor* subgraph_input = - graph_info->GetSubgraphInput(subgraph_idx, i); - int bytes = ValidateAndGetTensorSizes(input, subgraph_input); - TF_LITE_ENSURE(context, bytes >= 0); - memcpy(subgraph_input->data.raw, input->data.raw, bytes); - } - return kTfLiteOk; -} - -TfLiteStatus CopyOpOutputsToSubgraphInputs(TfLiteContext* context, - TfLiteNode* node, - MicroGraph* graph_info, - int subgraph_idx) { - TF_LITE_ENSURE(context, static_cast(node->outputs->size) == - graph_info->NumSubgraphInputs(subgraph_idx)); - for (int i = 0; i < node->outputs->size; i++) { - TfLiteEvalTensor* output = tflite::micro::GetEvalOutput(context, node, i); - TfLiteEvalTensor* subgraph_input = - graph_info->GetSubgraphInput(subgraph_idx, i); - int bytes = ValidateAndGetTensorSizes(output, subgraph_input); - TF_LITE_ENSURE(context, bytes >= 0); - memcpy(subgraph_input->data.raw, output->data.raw, bytes); - } - return kTfLiteOk; -} - -TfLiteStatus CopySubgraphOutputsToOpOutputs(TfLiteContext* context, - TfLiteNode* node, - MicroGraph* graph_info, - int subgraph_idx) { - TF_LITE_ENSURE(context, static_cast(node->outputs->size) == - graph_info->NumSubgraphOutputs(subgraph_idx)); - for (int i = 0; i < node->outputs->size; i++) { - TfLiteEvalTensor* output = tflite::micro::GetEvalOutput(context, node, i); - TfLiteEvalTensor* subgraph_output = - graph_info->GetSubgraphOutput(subgraph_idx, i); - int bytes = ValidateAndGetTensorSizes(output, subgraph_output); - TF_LITE_ENSURE(context, bytes >= 0); - memcpy(output->data.raw, subgraph_output->data.raw, bytes); - } - return kTfLiteOk; -} - -} // namespace micro -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/kernel_util.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/kernel_util.h deleted file mode 100644 index d6f20c72..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/kernel_util.h +++ /dev/null @@ -1,123 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_KERNEL_UTIL_H_ -#define TENSORFLOW_LITE_MICRO_KERNELS_KERNEL_UTIL_H_ - -#include - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/internal/types.h" -#include "tensorflow/lite/micro/micro_context.h" - -namespace tflite { -namespace micro { - -TfLiteRegistration RegisterOp( - void* (*init)(TfLiteContext* context, const char* buffer, size_t length), - TfLiteStatus (*prepare)(TfLiteContext* context, TfLiteNode* node), - TfLiteStatus (*invoke)(TfLiteContext* context, TfLiteNode* node)); - -// Returns a mutable tensor for a given input index. is_variable must be checked -// during prepare when the full TfLiteTensor is available. -TfLiteEvalTensor* GetMutableEvalInput(const TfLiteContext* context, - const TfLiteNode* node, int index); - -// Returns the TfLiteEvalTensor struct for a given input index in a node. -const TfLiteEvalTensor* GetEvalInput(const TfLiteContext* context, - const TfLiteNode* node, int index); - -// Returns the TfLiteEvalTensor struct for a given output index in a node. -TfLiteEvalTensor* GetEvalOutput(const TfLiteContext* context, - const TfLiteNode* node, int index); - -// Returns data for a TfLiteEvalTensor struct that are expected to exist. -template -T* GetTensorData(TfLiteEvalTensor* tensor) { - TFLITE_DCHECK(tensor != nullptr); - return reinterpret_cast(tensor->data.raw); -} - -// Returns const data for a TfLiteEvalTensor struct that are expected to exist. -template -const T* GetTensorData(const TfLiteEvalTensor* tensor) { - TFLITE_DCHECK(tensor != nullptr); - return reinterpret_cast(tensor->data.raw); -} - -// Returns data for a TfLiteEvalTensor struct that could be null. -template -T* GetOptionalTensorData(TfLiteEvalTensor* tensor) { - return tensor == nullptr ? nullptr : reinterpret_cast(tensor->data.raw); -} - -// Returns const data for a TfLiteEvalTensor struct that could be null. -template -const T* GetOptionalTensorData(const TfLiteEvalTensor* tensor) { - return tensor == nullptr ? nullptr - : reinterpret_cast(tensor->data.raw); -} - -// Returns the shape of a TfLiteEvalTensor struct. -const RuntimeShape GetTensorShape(const TfLiteEvalTensor* tensor); - -// Return true if the given tensors have the same shape. -bool HaveSameShapes(const TfLiteEvalTensor* input1, - const TfLiteEvalTensor* input2); - -PaddingType RuntimePaddingType(TfLitePadding padding); - -// Relocate tensor dims from FlatBuffer to the persistent storage arena. -// The old dims data is copied to the new storage area. -// The tensor and eval_tensor must be the same tensor. -// Only use during Prepare phase. -TfLiteStatus CreateWritableTensorDimsWithCopy(TfLiteContext* context, - TfLiteTensor* tensor, - TfLiteEvalTensor* eval_tensor); - -// Copy all op input tensors to op output tensors. Requires all op input tensor -// shapes and types to be identical to op output tensor shapes and types. -TfLiteStatus CopyOpInputsToOpOutputs(TfLiteContext* context, TfLiteNode* node); - -// Copy all op input tensors to subgraph input tensors. Requires all op input -// tensor shapes and types to be identical to subgraph input tensor shapes and -// types. -TfLiteStatus CopyOpInputsToSubgraphInputs(TfLiteContext* context, - TfLiteNode* node, - MicroGraph* graph_info, - int subgraph_idx, - int first_tensor_idx); - -// Copy all op output tensors to subgraph input tensors. Requires all op output -// tensor shapes and types to be identical to subgraph input tensor shapes and -// types. -TfLiteStatus CopyOpOutputsToSubgraphInputs(TfLiteContext* context, - TfLiteNode* node, - MicroGraph* graph_info, - int subgraph_idx); - -// Copy all subgraph output tensors to op outputs. Requires all subgraph output -// tensor shapes and types to be identical to op output tensor shapes and types. -TfLiteStatus CopySubgraphOutputsToOpOutputs(TfLiteContext* context, - TfLiteNode* node, - MicroGraph* graph_info, - int subgraph_idx); - -} // namespace micro -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_KERNELS_KERNEL_UTIL_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/l2_pool_2d.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/l2_pool_2d.cc deleted file mode 100644 index 2b2a27bf..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/l2_pool_2d.cc +++ /dev/null @@ -1,142 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include -#include - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/reference/pooling.h" -#include "tensorflow/lite/kernels/internal/types.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/padding.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { -namespace { - -// Input/output tensor index. -constexpr int kInputTensor = 0; -constexpr int kOutputTensor = 0; - -// required rank for input/output tensor shape -constexpr int kTensorShapeRank = 4; - -// input/output tensor shape rank associations -enum { kBatchRank = 0, kHeightRank, kWidthRank, kChannelRank }; - -TfLiteStatus L2Prepare(TfLiteContext* context, TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - - auto* params = static_cast(node->builtin_data); - - TF_LITE_ENSURE_EQ(context, NumInputs(node), 1); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TF_LITE_ENSURE_EQ(context, NumDimensions(input), kTensorShapeRank); - TF_LITE_ENSURE_EQ(context, NumDimensions(output), kTensorShapeRank); - TF_LITE_ENSURE_TYPES_EQ(context, input->type, output->type); - - int batches = SizeOfDimension(input, kBatchRank); - int height = SizeOfDimension(input, kHeightRank); - int width = SizeOfDimension(input, kWidthRank); - int channels_out = SizeOfDimension(input, kChannelRank); - - // Matching GetWindowedOutputSize in TensorFlow. - auto padding = params->padding; - int out_width, out_height; - - params->computed.padding = ComputePaddingHeightWidth( - params->stride_height, params->stride_width, 1, 1, height, width, - params->filter_height, params->filter_width, padding, &out_height, - &out_width); - - // We currently don't have a quantized implementation of L2Pool - TF_LITE_ENSURE_TYPES_EQ(context, input->type, kTfLiteFloat32); - - // We must update the output tensor dimensions. - // The dims storage is expected to be the same area in memory - // for both TfLiteTensor and TfLiteEvalTensor. This is important - // because TfLiteTensor in the MicroInterpreter is a temporary - // allocation. For the KernelRunner interpreter, TfLiteEvalTensor - // is a temporary allocation. We must therefore relocate the dims - // from the FlatBuffer to the persistant storage arena. - TfLiteEvalTensor* output_eval = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - TF_LITE_ENSURE_OK(context, tflite::micro::CreateWritableTensorDimsWithCopy( - context, output, output_eval)); - output->dims->data[kBatchRank] = batches; - output->dims->data[kHeightRank] = out_height; - output->dims->data[kWidthRank] = out_width; - output->dims->data[kChannelRank] = channels_out; - - micro_context->DeallocateTempTfLiteTensor(output); - micro_context->DeallocateTempTfLiteTensor(input); - - return kTfLiteOk; -} - -void L2EvalFloat(const TfLitePoolParams& params, const TfLiteEvalTensor& input, - tflite::PoolParams* op_params, TfLiteEvalTensor* output) { - float activation_min, activation_max; - CalculateActivationRange(params.activation, &activation_min, &activation_max); - - op_params->float_activation_min = activation_min; - op_params->float_activation_max = activation_max; - reference_ops::L2Pool(*op_params, tflite::micro::GetTensorShape(&input), - tflite::micro::GetTensorData(&input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); -} - -TfLiteStatus L2Eval(TfLiteContext* context, TfLiteNode* node) { - auto* params = static_cast(node->builtin_data); - - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kInputTensor); - - tflite::PoolParams op_params; - op_params.stride_height = params->stride_height; - op_params.stride_width = params->stride_width; - op_params.filter_height = params->filter_height; - op_params.filter_width = params->filter_width; - op_params.padding_values.height = params->computed.padding.height; - op_params.padding_values.width = params->computed.padding.width; - - switch (input->type) { // Already know in/out types are same. - case kTfLiteFloat32: - L2EvalFloat(*params, *input, &op_params, output); - break; - default: - TF_LITE_KERNEL_LOG(context, - "L2_POOL_2D only supports float32 currently, got %s.", - TfLiteTypeGetName(input->type)); - return kTfLiteError; - } - return kTfLiteOk; -} - -} // namespace - -TfLiteRegistration Register_L2_POOL_2D() { - return tflite::micro::RegisterOp(nullptr, L2Prepare, L2Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/l2norm.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/l2norm.cc deleted file mode 100644 index 45858e78..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/l2norm.cc +++ /dev/null @@ -1,147 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/portable_tensor.h" -#include "tensorflow/lite/kernels/internal/reference/integer_ops/l2normalization.h" -#include "tensorflow/lite/kernels/internal/reference/l2normalization.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { -namespace ops { -namespace micro { -namespace l2norm { - -namespace { - -// This file has two implementation of L2Norm. -enum KernelType { - kReference, - kGenericOptimized, -}; - -constexpr int kInputTensor = 0; -constexpr int kOutputTensor = 0; - -} // namespace - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - TFLITE_DCHECK(node->builtin_data != nullptr); - - auto* params = reinterpret_cast(node->builtin_data); - L2NormalizationParams* data = - static_cast(node->user_data); - - TF_LITE_ENSURE_EQ(context, NumInputs(node), 1); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - - MicroContext* micro_context = GetMicroContext(context); - - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - TF_LITE_ENSURE(context, NumDimensions(input) <= 4); - - TF_LITE_ENSURE(context, - output->type == kTfLiteFloat32 || output->type == kTfLiteInt8); - TF_LITE_ENSURE_TYPES_EQ(context, input->type, output->type); - - if (output->type == kTfLiteInt8) { - data->input_zero_point = input->params.zero_point; - } else if (output->type == kTfLiteFloat32) { - data->input_zero_point = 0; - } - - // Our implementations don't currently support activations. - TF_LITE_ENSURE_EQ(context, params->activation, kTfLiteActNone); - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(output); - return kTfLiteOk; -} - -void* Init(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, - sizeof(L2NormalizationParams)); -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - const L2NormalizationParams& data = - *(static_cast(node->user_data)); - - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kInputTensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - - // TODO(b/143912164): instead of hardcode the epsilon here, we should read it - // from tensorflow, i.e., adding a params. - // We don't compute epsilon for quantized kernel: - // - // epsilon_float = (epsilon_quant - zp) * scale - // so - // espsilon_quant = epsilon_float / scale + zp - // We know epsilon_float is just a very small number to avoid division by - // zero error, and scale is > 1, so the integer value of epsilon for quant - // is just dominated by the zero point. - // Also, GetInvSqrtQuantizedMultiplierExp handles the scenario where the sum - // of input value squared is zero case well. - // So we don't even need to do handle the epsilon for quantized kernel case. - const float epsilon = 1e-6f; - if (output->type == kTfLiteFloat32) { - reference_ops::L2Normalization(data, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output), - epsilon); - } else if (output->type == kTfLiteInt8) { - const auto input_shape = tflite::micro::GetTensorShape(input); - const auto output_shape = tflite::micro::GetTensorShape(output); - const int trailing_dim = input_shape.DimensionsCount() - 1; - const int depth = - MatchingDim(input_shape, trailing_dim, output_shape, trailing_dim); - const int outer_size = - MatchingFlatSizeSkipDim(input_shape, trailing_dim, output_shape); - reference_integer_ops::L2Normalization( - data.input_zero_point, outer_size, depth, - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorData(output)); - } else { - TF_LITE_KERNEL_LOG(context, "Output type is %s, requires float.", - TfLiteTypeGetName(output->type)); - return kTfLiteError; - } - - return kTfLiteOk; -} - -} // namespace l2norm - -TfLiteRegistration Register_L2NORM_REF() { - return tflite::micro::RegisterOp(l2norm::Init, l2norm::Prepare, l2norm::Eval); -} - -TfLiteRegistration Register_L2_NORMALIZATION() { return Register_L2NORM_REF(); } - -} // namespace micro -} // namespace ops -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/leaky_relu.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/leaky_relu.cc deleted file mode 100644 index 96c1b1b1..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/leaky_relu.cc +++ /dev/null @@ -1,95 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/kernels/internal/reference/leaky_relu.h" - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/process_broadcast_shapes.h" -#include "tensorflow/lite/kernels/internal/types.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/leaky_relu.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" - -namespace tflite { - -template -void QuantizeLeakyRelu(const LeakyReluOpData& data, - const TfLiteEvalTensor* input, - TfLiteEvalTensor* output) { - LeakyReluParams op_params = {}; - - op_params.input_offset = data.input_zero_point; - op_params.output_offset = data.output_zero_point; - op_params.output_multiplier_alpha = data.output_multiplier_alpha; - op_params.output_shift_alpha = data.output_shift_alpha; - op_params.output_multiplier_identity = data.output_multiplier_identity; - op_params.output_shift_identity = data.output_shift_identity; - reference_ops::QuantizeLeakyRelu(op_params, - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); -} - -void* LeakyReluInit(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(LeakyReluOpData)); -} - -TfLiteStatus LeakyReluEval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kInputTensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - const LeakyReluOpData& data = *static_cast(node->user_data); - - switch (input->type) { - case kTfLiteFloat32: { - LeakyReluParams op_params = {}; - const auto* params = - static_cast(node->builtin_data); - - op_params.alpha = params->alpha; - reference_ops::LeakyRelu(op_params, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - return kTfLiteOk; - } break; - case kTfLiteInt8: { - QuantizeLeakyRelu(data, input, output); - return kTfLiteOk; - } break; - case kTfLiteInt16: { - QuantizeLeakyRelu(data, input, output); - return kTfLiteOk; - } break; - default: - MicroPrintf("Only float32, int8 are supported by LEAKY_RELU, got %s.", - TfLiteTypeGetName(input->type)); - return kTfLiteError; - } - - return kTfLiteError; -} - -TfLiteRegistration Register_LEAKY_RELU() { - return tflite::micro::RegisterOp(LeakyReluInit, LeakyReluPrepare, - LeakyReluEval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/leaky_relu.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/leaky_relu.h deleted file mode 100644 index dfcd6e93..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/leaky_relu.h +++ /dev/null @@ -1,43 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_LEAKY_RELU_H_ -#define TENSORFLOW_LITE_MICRO_KERNELS_LEAKY_RELU_H_ - -#include "tensorflow/lite/c/common.h" - -namespace tflite { - -// Input/output tensor index. -extern const int kInputTensor; -extern const int kOutputTensor; - -struct LeakyReluOpData { - // quantization parameters - int32_t output_multiplier_alpha; - int32_t output_shift_alpha; - int32_t output_multiplier_identity; - int32_t output_shift_identity; - int32_t input_zero_point; - int32_t output_zero_point; -}; - -TfLiteStatus CalculateOpDataLeakyRelu(TfLiteContext* context, TfLiteNode* node); - -TfLiteStatus LeakyReluPrepare(TfLiteContext* context, TfLiteNode* node); - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_KERNELS_LEAKY_RELU_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/leaky_relu_common.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/leaky_relu_common.cc deleted file mode 100644 index 3d1ffebb..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/leaky_relu_common.cc +++ /dev/null @@ -1,78 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/leaky_relu.h" -#include "tensorflow/lite/kernels/internal/reference/process_broadcast_shapes.h" -#include "tensorflow/lite/kernels/internal/types.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/leaky_relu.h" - -namespace tflite { - -// Input/output tensor index. -const int kInputTensor = 0; -const int kOutputTensor = 0; - -TfLiteStatus CalculateOpDataLeakyRelu(TfLiteContext* context, - TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - - TF_LITE_ENSURE_EQ(context, NumInputs(node), 1); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - TF_LITE_ENSURE_TYPES_EQ(context, input->type, output->type); - - if (output->type == kTfLiteInt8 || output->type == kTfLiteInt16) { - LeakyReluOpData* data = static_cast(node->user_data); - const auto* params = - static_cast(node->builtin_data); - - data->input_zero_point = input->params.zero_point; - data->output_zero_point = output->params.zero_point; - - int output_shift_alpha; - double alpha_multiplier = static_cast( - input->params.scale * params->alpha / output->params.scale); - QuantizeMultiplier(alpha_multiplier, &data->output_multiplier_alpha, - &output_shift_alpha); - data->output_shift_alpha = static_cast(output_shift_alpha); - - int output_shift_identity; - double identity_multiplier = - static_cast(input->params.scale / output->params.scale); - QuantizeMultiplier(identity_multiplier, &data->output_multiplier_identity, - &output_shift_identity); - data->output_shift_identity = static_cast(output_shift_identity); - } - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(output); - - return kTfLiteOk; -} - -TfLiteStatus LeakyReluPrepare(TfLiteContext* context, TfLiteNode* node) { - return CalculateOpDataLeakyRelu(context, node); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/log_softmax.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/log_softmax.cc deleted file mode 100644 index 5fd87612..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/log_softmax.cc +++ /dev/null @@ -1,148 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/kernels/internal/reference/log_softmax.h" - -#include -#include - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/internal/types.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { -namespace { - -// used only with quantized data -struct LogSoftmaxOpData { - int32_t input_multiplier; - int32_t input_left_shift; - int32_t reverse_scaling_divisor; - int32_t reverse_scaling_right_shift; - int diff_min; - size_t outer_size; // number of tensor elements skipping computation axis - size_t depth; // number of tensor elements on computation axis -}; - -// input/output tensor index -constexpr int kInputTensor = 0; -constexpr int kOutputTensor = 0; - -TfLiteStatus CalculateOpData(TfLiteContext* context, TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - - TF_LITE_ENSURE_EQ(context, NumInputs(node), 1); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - TF_LITE_ENSURE_TYPES_EQ(context, input->type, output->type); - - TF_LITE_ENSURE(context, HaveSameShapes(input, output)); - - if (input->type == kTfLiteInt8) { - node->user_data = - context->AllocatePersistentBuffer(context, sizeof(LogSoftmaxOpData)); - auto data = static_cast(node->user_data); - - // quantization datum - constexpr int32_t kOutputZeroPoint = 127; - constexpr float kOutputScale = 16.0 / 256; - constexpr double kBeta = 1.0; - constexpr int kScaledDiffIntegerBits = 5; - - TF_LITE_ENSURE(context, output->params.scale == kOutputScale); - TF_LITE_ENSURE(context, output->params.zero_point == kOutputZeroPoint); - - int input_left_shift; - int reverse_scaling_right_shift; - tflite::PreprocessLogSoftmaxScalingExp( - kBeta, static_cast(input->params.scale), kScaledDiffIntegerBits, - &data->input_multiplier, &input_left_shift, - &data->reverse_scaling_divisor, &reverse_scaling_right_shift); - data->input_left_shift = static_cast(input_left_shift); - data->reverse_scaling_right_shift = - static_cast(-reverse_scaling_right_shift); - // diff_min has a negative value, and is used to limit the maximum magnitude - // of the diffs, which are <= 0. - data->diff_min = - -tflite::CalculateInputRadius(kScaledDiffIntegerBits, input_left_shift); - - RuntimeShape input_shape = GetTensorShape(input); - const int trailing_dim = input_shape.DimensionsCount() - 1; - data->outer_size = - static_cast(FlatSizeSkipDim(input_shape, trailing_dim)); - data->depth = static_cast(input_shape.Dims(trailing_dim)); - } - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(output); - return kTfLiteOk; -} - -TfLiteStatus LogSoftmaxPrepare(TfLiteContext* context, TfLiteNode* node) { - return CalculateOpData(context, node); -} - -TfLiteStatus LogSoftmaxEval(TfLiteContext* context, TfLiteNode* node) { - const LogSoftmaxOpData* data = - static_cast(node->user_data); - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kInputTensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - switch (input->type) { - case kTfLiteFloat32: { - SoftmaxParams op_params = {}; - reference_ops::LogSoftmax(op_params, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - return kTfLiteOk; - } - case kTfLiteInt8: { - SoftmaxParams op_params = {}; - op_params.input_multiplier = data->input_multiplier; - op_params.input_left_shift = data->input_left_shift; - op_params.reverse_scaling_divisor = data->reverse_scaling_divisor; - op_params.reverse_scaling_right_shift = data->reverse_scaling_right_shift; - op_params.diff_min = data->diff_min; - reference_ops::LogSoftmax(op_params, data->outer_size, data->depth, - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - return kTfLiteOk; - } - default: - TF_LITE_KERNEL_LOG(context, - "LOG_SOFTMAX only supports float32, int8, got %s.", - TfLiteTypeGetName(input->type)); - return kTfLiteError; - } -} - -} // namespace - -TfLiteRegistration Register_LOG_SOFTMAX() { - return tflite::micro::RegisterOp(nullptr, LogSoftmaxPrepare, LogSoftmaxEval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/logical.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/logical.cc deleted file mode 100644 index c85e0c5b..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/logical.cc +++ /dev/null @@ -1,44 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/micro/kernels/logical.h" - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/reference/binary_function.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { -namespace { - -TfLiteStatus LogicalOrEval(TfLiteContext* context, TfLiteNode* node) { - return LogicalImpl(context, node, LogicalOr); -} - -TfLiteStatus LogicalAndEval(TfLiteContext* context, TfLiteNode* node) { - return LogicalImpl(context, node, LogicalAnd); -} - -} // namespace - -TfLiteRegistration Register_LOGICAL_OR() { - return tflite::micro::RegisterOp(nullptr, nullptr, LogicalOrEval); -} - -TfLiteRegistration Register_LOGICAL_AND() { - return tflite::micro::RegisterOp(nullptr, nullptr, LogicalAndEval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/logical.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/logical.h deleted file mode 100644 index e70e4576..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/logical.h +++ /dev/null @@ -1,35 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_LOGICAL_H_ -#define TENSORFLOW_LITE_MICRO_KERNELS_LOGICAL_H_ - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" - -namespace tflite { -// Input/output tensor index. -extern const int kLogicalInputTensor1; -extern const int kLogicalInputTensor2; -extern const int kLogicalOutputTensor; - -TfLiteStatus LogicalImpl(TfLiteContext* context, TfLiteNode* node, - bool (*func)(bool, bool)); - -bool LogicalOr(bool x, bool y); -bool LogicalAnd(bool x, bool y); - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_KERNELS_LOGICAL_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/logical_common.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/logical_common.cc deleted file mode 100644 index 2612d3a4..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/logical_common.cc +++ /dev/null @@ -1,63 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/reference/binary_function.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/logical.h" - -namespace tflite { - -// Input/output tensor index. -const int kLogicalInputTensor1 = 0; -const int kLogicalInputTensor2 = 1; -const int kLogicalOutputTensor = 0; - -TfLiteStatus LogicalImpl(TfLiteContext* context, TfLiteNode* node, - bool (*func)(bool, bool)) { - const TfLiteEvalTensor* input1 = - tflite::micro::GetEvalInput(context, node, kLogicalInputTensor1); - const TfLiteEvalTensor* input2 = - tflite::micro::GetEvalInput(context, node, kLogicalInputTensor2); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kLogicalOutputTensor); - - if (tflite::micro::HaveSameShapes(input1, input2)) { - reference_ops::BinaryFunction( - tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output), func); - } else { - reference_ops::BroadcastBinaryFunction4DSlow( - tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output), func); - } - - return kTfLiteOk; -} - -bool LogicalOr(bool x, bool y) { return x || y; } - -bool LogicalAnd(bool x, bool y) { return x && y; } - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/logistic.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/logistic.cc deleted file mode 100644 index f8ac1c23..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/logistic.cc +++ /dev/null @@ -1,111 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/kernels/internal/reference/integer_ops/logistic.h" - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/logistic.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/logistic.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" - -namespace tflite { -namespace { - -void* LogisticInit(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(OpDataLogistic)); -} - -TfLiteStatus LogisticEval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kLogisticInputTensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kLogisticOutputTensor); - - TFLITE_DCHECK(node->user_data != nullptr); - OpDataLogistic* data = static_cast(node->user_data); - - if (input->type == kTfLiteFloat32) { - switch (output->type) { - case kTfLiteFloat32: { - reference_ops::Logistic(tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - return kTfLiteOk; - } - default: - MicroPrintf("Input %s, output %s not supported.", - TfLiteTypeGetName(input->type), - TfLiteTypeGetName(output->type)); - return kTfLiteError; - } - } else if (input->type == kTfLiteInt16) { - switch (output->type) { - case kTfLiteInt16: { - reference_integer_ops::Logistic( - data->input_multiplier, data->input_left_shift, - NumElements(input->dims), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorData(output)); - return kTfLiteOk; - } - default: - MicroPrintf("Input %s, output %s not supported.", - TfLiteTypeGetName(input->type), - TfLiteTypeGetName(output->type)); - return kTfLiteError; - } - } else if (input->type == kTfLiteInt8) { - switch (output->type) { - case kTfLiteInt8: { - reference_integer_ops::Logistic( - data->input_zero_point, data->input_range_radius, - data->input_multiplier, data->input_left_shift, - NumElements(input->dims), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorData(output)); - return kTfLiteOk; - } - default: - MicroPrintf("Input %s, output %s not supported.", - TfLiteTypeGetName(input->type), - TfLiteTypeGetName(output->type)); - return kTfLiteError; - } - } else { - // TODO(b/141211002): Also support other data types once we have supported - // temporary tensors in TFLM. - MicroPrintf("Input %s, output %s not supported.", - TfLiteTypeGetName(input->type), - TfLiteTypeGetName(output->type)); - return kTfLiteError; - } - return kTfLiteOk; -} - -} // namespace - -TfLiteRegistration Register_LOGISTIC() { - return tflite::micro::RegisterOp(LogisticInit, LogisticPrepare, LogisticEval); -} -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/logistic.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/logistic.h deleted file mode 100644 index 1de0cdab..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/logistic.h +++ /dev/null @@ -1,42 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_LOGISTIC_H_ -#define TENSORFLOW_LITE_MICRO_KERNELS_LOGISTIC_H_ - -#include - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" - -namespace tflite { -extern const int kLogisticInputTensor; -extern const int kLogisticOutputTensor; - -struct OpDataLogistic { - int32_t input_zero_point; - int32_t input_range_radius; - int32_t input_multiplier; - int input_left_shift; -}; - -TfLiteStatus CalculateArithmeticOpDataLogistic(TfLiteContext* context, - TfLiteNode* node, - OpDataLogistic* data); - -TfLiteStatus LogisticPrepare(TfLiteContext* context, TfLiteNode* node); - -} // namespace tflite -#endif // TENSORFLOW_LITE_MICRO_KERNELS_LOGISTIC_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/logistic_common.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/logistic_common.cc deleted file mode 100644 index a79fd6bb..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/logistic_common.cc +++ /dev/null @@ -1,119 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/integer_ops/logistic.h" -#include "tensorflow/lite/kernels/internal/reference/logistic.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/logistic.h" - -namespace tflite { -const int kLogisticInputTensor = 0; -const int kLogisticOutputTensor = 0; - -TfLiteStatus CalculateArithmeticOpDataLogistic(TfLiteContext* context, - TfLiteNode* node, - OpDataLogistic* data) { - MicroContext* micro_context = GetMicroContext(context); - - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kLogisticInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kLogisticOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - - TF_LITE_ENSURE_TYPES_EQ(context, input->type, output->type); - if (input->type == kTfLiteInt8) { - TF_LITE_ENSURE_EQ(context, output->params.zero_point, - std::numeric_limits::min()); - - static constexpr int kInputIntegerBits = 4; - const double input_real_multiplier = - static_cast(input->params.scale) * - static_cast(1 << (31 - kInputIntegerBits)); - - data->input_zero_point = input->params.zero_point; - - const double q = std::frexp(input_real_multiplier, &data->input_left_shift); - data->input_multiplier = static_cast(TfLiteRound(q * (1ll << 31))); - - data->input_range_radius = - CalculateInputRadius(kInputIntegerBits, data->input_left_shift, 31); - } - - if (input->type == kTfLiteInt16) { - static constexpr int kInputIntegerBits = 3; - static constexpr int kOutputFractionalBits = 15; - - // See comments in TanhPrepare about requiring zero_point==0 - // and a power-of-two ("POT") scale. - - TF_LITE_ENSURE_EQ(context, input->params.zero_point, 0); - TF_LITE_ENSURE_EQ(context, output->params.zero_point, 0); - - int input_scale_log2_rounded; - bool param_scale_pot = - CheckedLog2(input->params.scale, &input_scale_log2_rounded); - - data->input_left_shift = - (15 - kInputIntegerBits) + input_scale_log2_rounded; - param_scale_pot &= (data->input_left_shift == 0); - - if (param_scale_pot) { - data->input_multiplier = 0; - } else { - // Calculate multiplier to change input scale to 1/(3*4096) - // as required by the table lookup. - // In this scaling +/-2^17 represents +/-10.7 - double multiplier = - static_cast(input->params.scale) * 4096.0 * 3.0; - - data->input_left_shift = 0; - - while (multiplier <= 32767.0 / 2.0 && data->input_left_shift <= 30) { - data->input_left_shift++; - multiplier = multiplier * 2.0; - } - - data->input_multiplier = static_cast(multiplier); - } - - int output_scale_log2_rounded; - TF_LITE_ENSURE( - context, CheckedLog2(output->params.scale, &output_scale_log2_rounded)); - TF_LITE_ENSURE_EQ(context, output_scale_log2_rounded, - -kOutputFractionalBits); - } - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(output); - return kTfLiteOk; -} - -TfLiteStatus LogisticPrepare(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - OpDataLogistic* data = static_cast(node->user_data); - - return CalculateArithmeticOpDataLogistic(context, node, data); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/lstm_eval.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/lstm_eval.cc deleted file mode 100644 index f157a8d0..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/lstm_eval.cc +++ /dev/null @@ -1,2955 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/micro/kernels/lstm_eval.h" - -#include -#include -#include -#include - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/micro_tensor_utils.h" -namespace tflite { -namespace { - -void ComputeRowSums( - int32_t* input_to_input_row_sums, int32_t* input_to_forget_row_sums, - int32_t* input_to_cell_row_sums, int32_t* input_to_output_row_sums, - int32_t* aux_input_to_input_row_sums, int32_t* aux_input_to_forget_row_sums, - int32_t* aux_input_to_cell_row_sums, int32_t* aux_input_to_output_row_sums, - int32_t* recurrent_to_input_row_sums, int32_t* recurrent_to_forget_row_sums, - int32_t* recurrent_to_cell_row_sums, int32_t* recurrent_to_output_row_sums, - int32_t* projection_weights_row_sums, int32_t* row_sums, int n_cell, - int n_input, int n_aux_input, int n_output, - const int8_t* input_to_input_weights_ptr, - const int8_t* input_to_forget_weights_ptr, - const int8_t* input_to_cell_weights_ptr, - const int8_t* input_to_output_weights_ptr, - const int8_t* aux_input_to_input_weights_ptr, - const int8_t* aux_input_to_forget_weights_ptr, - const int8_t* aux_input_to_cell_weights_ptr, - const int8_t* aux_input_to_output_weights_ptr, - const int8_t* recurrent_to_input_weights_ptr, - const int8_t* recurrent_to_forget_weights_ptr, - const int8_t* recurrent_to_cell_weights_ptr, - const int8_t* recurrent_to_output_weights_ptr, - const int8_t* projection_weights_ptr, bool use_cifg, - const float* aux_input_ptr) { - // Compute the row sums for dequantization - if (!use_cifg) { - micro_tensor_utils::ReductionSumVector( - input_to_input_weights_ptr, input_to_input_row_sums, n_cell, n_input); - } - micro_tensor_utils::ReductionSumVector( - input_to_forget_weights_ptr, input_to_forget_row_sums, n_cell, n_input); - micro_tensor_utils::ReductionSumVector( - input_to_cell_weights_ptr, input_to_cell_row_sums, n_cell, n_input); - micro_tensor_utils::ReductionSumVector( - input_to_output_weights_ptr, input_to_output_row_sums, n_cell, n_input); - - if (aux_input_ptr) { - if (!use_cifg) { - micro_tensor_utils::ReductionSumVector(aux_input_to_input_weights_ptr, - aux_input_to_input_row_sums, - n_cell, n_aux_input); - } - micro_tensor_utils::ReductionSumVector(aux_input_to_forget_weights_ptr, - aux_input_to_forget_row_sums, n_cell, - n_aux_input); - micro_tensor_utils::ReductionSumVector(aux_input_to_cell_weights_ptr, - aux_input_to_cell_row_sums, n_cell, - n_aux_input); - micro_tensor_utils::ReductionSumVector(aux_input_to_output_weights_ptr, - aux_input_to_output_row_sums, n_cell, - n_aux_input); - } - if (!use_cifg) { - micro_tensor_utils::ReductionSumVector(recurrent_to_input_weights_ptr, - recurrent_to_input_row_sums, n_cell, - n_output); - } - micro_tensor_utils::ReductionSumVector(recurrent_to_forget_weights_ptr, - recurrent_to_forget_row_sums, n_cell, - n_output); - micro_tensor_utils::ReductionSumVector(recurrent_to_cell_weights_ptr, - recurrent_to_cell_row_sums, n_cell, - n_output); - micro_tensor_utils::ReductionSumVector(recurrent_to_output_weights_ptr, - recurrent_to_output_row_sums, n_cell, - n_output); - - if (projection_weights_ptr != nullptr) { - micro_tensor_utils::ReductionSumVector( - projection_weights_ptr, projection_weights_row_sums, n_output, n_cell); - } -} - -// Calculates a single LSTM gate. -// -// Implements the following formula: (* is matrix multiply) -// gate = activate(W_input * input + W_aux * aux_input + -// W_peephole * cell + W_recurrent * prev_output + bias) -// with layer norm: -// gate = activate(W_norm * normalize(...) + bias) // not adding bias inside -// -// Activation is sigmoid except for the "cell" gate (configurable, usually tanh) -// -// Parameters: -// Input vectors (to LSTM): | Size: | Optional? -// input | n_input | -// aux_input | n_aux_input | y (bidir LSTM) -// Input vectors (persistent states): -// output_state | n_output | -// cell_state | n_cell | -// 'Constant' inputs: -// input_to_gate_weights | n_cell * n_input | -// aux_input_to_gate_weights | n_cell * n_aux_input | y (bidir LSTM) -// recurrent_to_gate_weights | n_cell * n_output | -// cell_to_gate_weights | n_cell | y (peephole) -// gate_bias | n_cell | -// layer_norm_coefficients | n_cell | y (layer norm) -// Output vector: -// gate | n_cell | -// Scalar parameters: -// n_batch - batch size / number of vectors -// n_input, n_aux_input, n_output, n_cell - size of vectors. -// activation - activation to use. -// is_input_all_zeros, is_aux_input_all_zeros - if input vectors are all zero. -// use_layer_norm - if doing layer norm LSTM. -inline void CalculateLstmGateFloat( - const float* input, const float* input_to_gate_weights, - const float* aux_input, const float* aux_input_to_gate_weights, - const float* output_state, const float* recurrent_to_gate_weights, - const float* cell_state, const float* cell_to_gate_weights, - const float* layer_norm_coefficients, const float* gate_bias, - const int n_batch, const int n_input, const int n_aux_input, - const int n_output, const int n_cell, - const TfLiteFusedActivation activation, float* gate, - const bool is_input_all_zeros, const bool is_aux_input_all_zeros) { - const bool use_peephole = (cell_to_gate_weights != nullptr); - const bool use_layer_norm = (layer_norm_coefficients != nullptr); - - // Initialize scratch buffers with bias for regular lstm or initialize with - // zero for layer norm lstm. - if (use_layer_norm) { - memset(gate, 0, n_cell * n_batch * sizeof(float)); - } else { - micro_tensor_utils::VectorBatchVectorAssign(gate_bias, n_cell, n_batch, - gate); - } - // For each batch and cell: compute input_weight * input. - // Skip if input is all zeros. - if (!is_input_all_zeros) { - micro_tensor_utils::MatrixBatchVectorMultiplyAccumulate( - input_to_gate_weights, n_cell, n_input, input, n_batch, gate); - } - // For each batch and cell: compute aux_input_weight * aux_input. - // Skip if auxiliary input is not available or all zeros. - if (!is_aux_input_all_zeros) { - micro_tensor_utils::MatrixBatchVectorMultiplyAccumulate( - aux_input_to_gate_weights, n_cell, n_aux_input, aux_input, n_batch, - gate); - } - // For each batch and cell: compute recurrent_weight * output_state. - micro_tensor_utils::MatrixBatchVectorMultiplyAccumulate( - recurrent_to_gate_weights, n_cell, n_output, output_state, n_batch, gate); - // For each batch and cell: compute cell_weight .* cell_state (peephole LSTM) - if (use_peephole) { - micro_tensor_utils::VectorBatchVectorCwiseProductAccumulate( - cell_to_gate_weights, n_cell, cell_state, n_batch, gate); - } - // Do layer normalization (if layer norm LSTM) - if (use_layer_norm) { - micro_tensor_utils::MeanStddevNormalization(gate, gate, n_cell, n_batch); - micro_tensor_utils::VectorBatchVectorCwiseProduct( - layer_norm_coefficients, n_cell, gate, n_batch, gate); - micro_tensor_utils::VectorBatchVectorAdd(gate_bias, n_cell, n_batch, gate); - } - // Apply activation - micro_tensor_utils::ApplyActivationToVector(gate, n_batch * n_cell, - activation, gate); -} - -// Updates the LSTM cell state, used by both float and hybrid LSTM versions. -// -// Implements the following formula: -// cell_state_new = clip(forget_gate * cell_state + input_gate * cell_gate) -// -// With CIFG LSTM, input gate is replaced by (1-forget_gate). -// -// Parameters: -// - n_batch, n_cell: sizes of vectors -// - cell_state: input/output vector, size n_batch*n_cell -// - input_gate: input vector, size n_batch*n_cell. -// - forget_gate: input/scratch vector, size n_batch*n_cell, modified with CIFG -// - cell_gate: input vector, size n_batch*n_cell. -// - use_cifg: use 1-forget_gate instead of input_gate. -// - clip: if > 0, clip the resulting cell state to [-clip, +clip]. -void UpdateLstmCellFloat(int n_batch, int n_cell, float* cell_state, - const float* input_gate, float* forget_gate, - const float* cell_gate, bool use_cifg, float clip) { - micro_tensor_utils::VectorVectorCwiseProduct(forget_gate, cell_state, - n_batch * n_cell, cell_state); - - if (use_cifg) { - // With CIFG, input_gate = 1-forget_gate. Use the forget_gate array as - // scratch, as input_gate array is not allocated in this case. (Be careful - // not to write to the scratch before reading the forget gate data.) - float* scratch = forget_gate; - micro_tensor_utils::Sub1Vector(forget_gate, n_batch * n_cell, scratch); - micro_tensor_utils::VectorVectorCwiseProductAccumulate( - cell_gate, scratch, n_batch * n_cell, cell_state); - } else { - micro_tensor_utils::VectorVectorCwiseProductAccumulate( - cell_gate, input_gate, n_batch * n_cell, cell_state); - } - if (clip > 0.0f) { - micro_tensor_utils::CwiseClipping(cell_state, n_batch * n_cell, clip); - } -} - -// Calculates the output state tensor of an LSTM step. -// -// Implements the following formula: -// output_no_projection = output_gate .* activate(cell_state) -// (elementwise vector product) -// If no projection is used: -// output = output_state = output_no_projection -// With projection: -// output = output_state = clip(W*output_no_projection + bias) -// -// Output might not have a different 'stride' than n_batch, so we need to copy. -// -// Parameters: -// - n_batch: batches: the number of distinct vectors in each array. -// - n_cell, n_output: sizes of vectors. -// - cell_state, output_gate: input vectors, size n_batch*n_cell. -// - projection_weights, projection_weights_scale, projection_bias: -// constant inputs, describing projection matrix and bias. -// - proj_clip: if > 0, clip the output of the projection. -// - output_state: output vector, size n_batch*n_output. Must be contigous. -// - scratch: scratch area, size n_batch*n_cell. -void CalculateLstmOutputFloat(int n_batch, int n_cell, int n_output, - const float* cell_state, const float* output_gate, - TfLiteFusedActivation activation, - const float* projection_weights, - const float* projection_bias, - const float proj_clip, float* output_state, - float* scratch) { - micro_tensor_utils::ApplyActivationToVector(cell_state, n_batch * n_cell, - activation, scratch); - micro_tensor_utils::VectorVectorCwiseProduct(output_gate, scratch, - n_batch * n_cell, scratch); - - const bool use_projection = (projection_weights != nullptr); - const bool use_projection_bias = (projection_bias != nullptr); - - if (use_projection) { - if (use_projection_bias) { - micro_tensor_utils::VectorBatchVectorAssign(projection_bias, n_output, - n_batch, output_state); - } else { - memset(output_state, 0, n_batch * n_output * sizeof(float)); - } - micro_tensor_utils::MatrixBatchVectorMultiplyAccumulate( - projection_weights, n_output, n_cell, scratch, n_batch, output_state); - if (proj_clip > 0.0f) { - micro_tensor_utils::CwiseClipping(output_state, n_batch * n_output, - proj_clip); - } - } else { - std::memcpy(output_state, scratch, n_batch * n_output * sizeof(float)); - } -} - -// Calculates a single LSTM gate, hybrid version. -// Implements the same functionality as CalculateLstmGateFloat. -void CalculateLstmGateHybrid( - // Input and weights - const int8_t* input, const float* input_sf, const int32_t* input_zp, - const int8_t* input_to_gate_weights, - const uint8_t* input_to_gate_weights_ledger, - const float input_to_gate_weights_scale, int32_t* input_to_gate_row_sums, - // Aux input and weights - const int8_t* aux_input, const float* aux_input_sf, - const int32_t* aux_input_zp, const int8_t* aux_input_to_gate_weights, - const float aux_input_to_gate_weights_scale, - int32_t* aux_input_to_gate_row_sums, - // Output state and weights - const int8_t* output_state, const float* output_state_sf, - const int32_t* output_state_zp, const int8_t* recurrent_to_gate_weights, - const uint8_t* recurrent_to_gate_weights_ledger, - const float recurrent_to_gate_weights_scale, - int32_t* recurrent_to_gate_row_sums, - // Cell state and weights (peephole LSTM) - const float* cell_state, const int8_t* cell_to_gate_weights, - const float cell_to_gate_weights_scale, - // Layer normalization coefficients (layer norm LSTM) + gate bias - const float* layer_norm_coefficients, const float* gate_bias, - // Array sizes - const int n_batch, const int n_input, const int n_aux_input, - const int n_output, const int n_cell, - const TfLiteFusedActivation activation, - // Output - float* gate, - // Parameters for performance optimizations - const bool is_input_all_zeros, const bool is_aux_input_all_zeros, - const bool is_output_state_all_zeros, bool* compute_row_sums, - // Scratch arrays - float* scratch0, // size: n_batch - float* scratch1, // size: n_cell, only used if peephole LSTM - float* scales, // size: n_batch - int32_t* accum_scratch // For MatrixBatchVectorMultiplyAccumulate -) { - const bool use_peephole = (cell_to_gate_weights != nullptr); - const bool use_layer_norm = (layer_norm_coefficients != nullptr); - - // Initialize scratch buffers with bias for regular lstm or initialize with - // zero for layer norm lstm. - if (use_layer_norm) { - memset(gate, 0, n_cell * n_batch * sizeof(float)); - } else { - micro_tensor_utils::VectorBatchVectorAssign(gate_bias, n_cell, n_batch, - gate); - } - // For each batch and cell: compute input_weight * input. - // Skip if input is all zeros. - if (!is_input_all_zeros) { - if (input_to_gate_weights_ledger != nullptr) { - for (int i = 0; i < n_batch; i++) { - scales[i] = input_to_gate_weights_scale * input_sf[i]; - } - micro_tensor_utils::SparseMatrixBatchVectorMultiplyAccumulate( - input_to_gate_weights, input_to_gate_weights_ledger, n_cell, n_input, - input, scales, n_batch, gate); - - } else { - micro_tensor_utils::MatrixBatchVectorMultiplyAccumulate( - input_to_gate_weights, n_cell, n_input, input, - input_to_gate_weights_scale, input_sf, n_batch, gate, - /*per_channel_scale=*/nullptr, input_zp, accum_scratch, - input_to_gate_row_sums, compute_row_sums, scratch0, nullptr); - } - } - // For each batch and cell: compute aux_input_weight * aux_input. - // Skip if auxiliary input is not available or all zeros. - if (!is_aux_input_all_zeros) { - micro_tensor_utils::MatrixBatchVectorMultiplyAccumulate( - aux_input_to_gate_weights, n_cell, n_aux_input, aux_input, - aux_input_to_gate_weights_scale, aux_input_sf, n_batch, gate, - /*per_channel_scale=*/nullptr, aux_input_zp, accum_scratch, - aux_input_to_gate_row_sums, compute_row_sums, scratch0, nullptr); - } - // For each batch and cell: compute recurrent_weight * output_state. - // Skip if output state is all zeros. - if (!is_output_state_all_zeros) { - if (recurrent_to_gate_weights_ledger != nullptr) { - for (int i = 0; i < n_batch; i++) { - scales[i] = recurrent_to_gate_weights_scale * input_sf[i]; - } - micro_tensor_utils::SparseMatrixBatchVectorMultiplyAccumulate( - recurrent_to_gate_weights, recurrent_to_gate_weights_ledger, n_cell, - n_output, output_state, scales, n_batch, gate); - } else { - micro_tensor_utils::MatrixBatchVectorMultiplyAccumulate( - recurrent_to_gate_weights, n_cell, n_output, output_state, - recurrent_to_gate_weights_scale, output_state_sf, n_batch, gate, - /*per_channel_scale=*/nullptr, output_state_zp, accum_scratch, - recurrent_to_gate_row_sums, compute_row_sums, scratch0, nullptr); - } - } - // For each batch and cell: compute cell_weight .* cell_state (peephole LSTM) - if (use_peephole) { - float* recovered_cell_weights = scratch1; - micro_tensor_utils::VectorScalarMultiply(cell_to_gate_weights, n_cell, - cell_to_gate_weights_scale, - recovered_cell_weights); - micro_tensor_utils::VectorBatchVectorCwiseProductAccumulate( - recovered_cell_weights, n_cell, cell_state, n_batch, gate); - } - // Do layer normalization (if layer norm LSTM) - if (use_layer_norm) { - micro_tensor_utils::MeanStddevNormalization(gate, gate, n_cell, n_batch); - micro_tensor_utils::VectorBatchVectorCwiseProduct( - layer_norm_coefficients, n_cell, gate, n_batch, gate); - micro_tensor_utils::VectorBatchVectorAdd(gate_bias, n_cell, n_batch, gate); - } - // Apply activation - micro_tensor_utils::ApplyActivationToVector(gate, n_cell * n_batch, - activation, gate); -} - -// Calculates the output state tensor of an LSTM step. See Float version too. -// -// Parameters: -// - n_batch: batches: the number of distinct vectors in each array. -// - n_cell, n_output: sizes of vectors. -// - cell_state, output_gate: input vectors, size n_batch*n_cell. -// - projection_weights, projection_weights_scale, projection_bias: -// constant inputs, describing projection matrix and bias. -// - proj_clip: if > 0, clip the output of the projection. -// - output_state: output vector, size n_batch*n_output. Must be contigous. -// - asymmetric_quantize_inputs: parameter to control quantization. -// - projection_weights_row_sums, compute_row_sums: Data for optimized -// MatrixBatchVectorMultiplyAccumulate. -// - scratch0: scratch area of size n_batch*n_cell -// - scratch1: scratch area of size n_batch*n_cell -// - scratch2: scratch area of size n_batch -// - scratch3: scratch area of size n_batch -// - scratch4: scratch area used by MatrixBatchVectorMultiplyAccumulate -// - scales: scratch area of size n_batch -void CalculateLstmOutputHybrid( - int n_batch, int n_cell, int n_output, const float* cell_state, - const float* output_gate, TfLiteFusedActivation activation, - const int8_t* projection_weights, const uint8_t* projection_weights_ledger, - float projection_weights_scale, const float* projection_bias, - const float proj_clip, float* output_state, bool asymmetric_quantize_inputs, - int32_t* projection_weights_row_sums, bool* compute_row_sums, - float* scratch0, int8_t* scratch1, float* scratch2, int32_t* scratch3, - int32_t* scratch4, float* scales) { - micro_tensor_utils::ApplyActivationToVector(cell_state, n_batch * n_cell, - activation, scratch0); - micro_tensor_utils::VectorVectorCwiseProduct(output_gate, scratch0, - n_batch * n_cell, scratch0); - - const bool use_projection = (projection_weights != nullptr); - const bool use_projection_bias = (projection_bias != nullptr); - - if (use_projection) { - if (use_projection_bias) { - micro_tensor_utils::VectorBatchVectorAssign(projection_bias, n_output, - n_batch, output_state); - } else { - memset(output_state, 0, n_batch * n_output * sizeof(float)); - } - if (!micro_tensor_utils::IsZeroVector(scratch0, n_batch * n_cell)) { - // Save quantization and matmul computation for all zero output. - micro_tensor_utils::BatchQuantizeFloats(scratch0, n_batch, n_cell, - scratch1, scratch2, scratch3, - asymmetric_quantize_inputs); - if (projection_weights_ledger != nullptr) { - for (int i = 0; i < n_batch; i++) { - scales[i] = projection_weights_scale * scratch2[i]; - } - micro_tensor_utils::SparseMatrixBatchVectorMultiplyAccumulate( - projection_weights, projection_weights_ledger, n_output, n_cell, - scratch1, scales, n_batch, output_state); - } else { - micro_tensor_utils::MatrixBatchVectorMultiplyAccumulate( - projection_weights, n_output, n_cell, scratch1, - projection_weights_scale, scratch2, n_batch, output_state, - /*per_channel_scale=*/nullptr, scratch3, scratch4, - projection_weights_row_sums, compute_row_sums, scratch2, nullptr); - } - } - if (proj_clip > 0.0f) { - micro_tensor_utils::CwiseClipping(output_state, n_batch * n_output, - proj_clip); - } - } else { - std::memcpy(output_state, scratch0, n_batch * n_output * sizeof(float)); - } -} - -// Calculates a single LSTM gate, int8x8_16 version. -// Implements the same functionality as CalculateLstmGateFloat. -void CalculateLstmGateInteger8x8_16( - // Input and weights - const int8_t* input, const int8_t* input_to_gate_weights, - const int32_t* input_to_gate_bias, const int32_t input_to_gate_scale_a, - const int32_t input_to_gate_scale_b, - // Output state and weights - const int8_t* output_state, const int8_t* recurrent_to_gate_weights, - const int32_t* recurrent_to_gate_bias, - const int32_t recurrent_to_gate_scale_a, - const int32_t recurrent_to_gate_scale_b, - // Cell state and weights - const int16_t* cell_state, const int16_t* cell_to_gate_weights, - const int32_t cell_to_gate_scale_a, const int32_t cell_to_gate_scale_b, - // Layer normalization parameters (layer norm LSTM) - const int16_t* layer_norm_coefficients, const int32_t* layer_norm_bias, - const int32_t layer_norm_input_scale_a, - const int32_t layer_norm_input_scale_b, - const int32_t layer_norm_variance_guard, - // Array sizes - const int n_batch, const int n_input, const int n_output, const int n_cell, - const TfLiteFusedActivation activation, - // Output - int16_t* gate, - // Parameters for performance optimizations - // Scratch arrays - int32_t* scratch5) { - const bool use_peephole = (cell_to_gate_weights != nullptr); - const bool use_layer_norm = (layer_norm_coefficients != nullptr); - - // Initialize scratch buffers with zeros. Note that unlike float and hybrid - // versions, bias is only used in layer normalization. - memset(gate, 0, n_batch * n_cell * sizeof(int16_t)); - // For each batch and cell: compute input_weight * input. - micro_tensor_utils::MatrixBatchVectorMultiplyAccumulate( - input, input_to_gate_bias, input_to_gate_weights, input_to_gate_scale_a, - input_to_gate_scale_b, n_batch, n_input, n_cell, 0, scratch5, gate, - nullptr); - // Note: no aux_input. - - // For each batch and cell: compute recurrent_weight * output_state. - micro_tensor_utils::MatrixBatchVectorMultiplyAccumulate( - output_state, recurrent_to_gate_bias, recurrent_to_gate_weights, - recurrent_to_gate_scale_a, recurrent_to_gate_scale_b, n_batch, n_output, - n_cell, 0, scratch5, gate, nullptr); - // For each batch and cell: compute cell_weight * cell_state (peephole LSTM) - if (use_peephole) { - micro_tensor_utils::VectorBatchVectorCwiseProductAccumulate( - cell_to_gate_weights, n_output, cell_state, n_batch, - cell_to_gate_scale_a, cell_to_gate_scale_b, gate); - } - // Do layer normalization (if layer norm LSTM) - if (use_layer_norm) { - micro_tensor_utils::ApplyLayerNorm( - gate, layer_norm_coefficients, layer_norm_bias, - layer_norm_input_scale_a, layer_norm_input_scale_b, - layer_norm_variance_guard, n_batch, n_cell, gate); - } - // Apply activation - switch (activation) { - case kTfLiteActSigmoid: - micro_tensor_utils::ApplySigmoid(gate, n_batch, n_cell, gate); - break; - case kTfLiteActTanh: - micro_tensor_utils::ApplyTanh(3, gate, n_batch, n_cell, gate); - break; - default: - // Only Sigmoid or Tanh is used. - TFLITE_ASSERT_FALSE; - } -} - -// Updates the LSTM cell state, used by both integer LSTM versions. -// Also see UpdateLstmCellFloat. -// -// Parameters: -// - n_batch, n_cell: sizes of vectors -// - cell_state: input/output vector, size n_batch*n_cell -// - cell_state_scale: scaling factor of cell state. -// - input_gate: input vector, size n_batch*n_cell. -// - forget_gate: input/scratch vector, size n_batch*n_cell, always modified. -// - cell_gate: input vector, size n_batch*n_cell. -// - use_cifg: use 1-forget_gate instead of input_gate. -// - clip: if > 0, clip the resulting cell state to [-clip, +clip]. -void UpdateLstmCellInteger(int n_batch, int n_cell, int16_t* cell_state, - int32_t cell_state_scale, const int16_t* input_gate, - int16_t* forget_gate, const int16_t* cell_gate, - bool use_cifg, int16_t clip) { - // Use the forget_gate array as scratch, as input_gate array is not allocated - // in CIFG case. (Be careful not to write to the scratch before reading the - // forget gate data.) - int16_t* scratch = forget_gate; - - micro_tensor_utils::CwiseMul(forget_gate, cell_state, n_batch, n_cell, 15, - cell_state); - if (use_cifg) { - micro_tensor_utils::Sub1Vector(forget_gate, n_batch * n_cell, scratch); - micro_tensor_utils::CwiseMul(scratch, cell_gate, n_batch, n_cell, - 30 + cell_state_scale, scratch); - } else { - micro_tensor_utils::CwiseMul(input_gate, cell_gate, n_batch, n_cell, - 30 + cell_state_scale, scratch); - } - micro_tensor_utils::CwiseAdd(cell_state, scratch, n_batch, n_cell, - cell_state); - - if (clip > 0) { - micro_tensor_utils::CwiseClipping(cell_state, n_batch * n_cell, clip); - } -} - -// Calculates the output state tensor of an LSTM step. See Float and hybrid -// versions as well. -// -// Parameters: -// - n_batch: batches: the number of distinct vectors in each array. -// - n_cell, n_output: sizes of vectors. -// - cell_state, output_gate: input vectors, size n_batch*n_cell. -// - cell_state_scale: scaling of cell_state. -// - hidden_scale_[a|b]: effective scale of cell_state.*output_gate -// - hidden_zp: zero_point for cell_state.*output_gate -// - projection_weights, proj_scale_[a|b], projection_bias: -// constant inputs, describing projection matrix and bias. -// - output_state_zp: zero point of output_state. (Input, calibrated value.) -// - quantized_proj_clip: if > 0, clip the output of the projection. -// - output_state: output vector, size n_batch*n_output. Must be contigous. -// - scratch0: scratch area of size n_batch*n_cell -// - scratch1: scratch area of size n_batch*n_cell -// - scratch2: scratch area used by MatrixBatchVectorMultiplyAccumulate -void CalculateLstmOutputInteger8x8_16( - int n_batch, int n_cell, int n_output, const int16_t* cell_state, - int32_t cell_state_scale, const int16_t* output_gate, - int32_t hidden_scale_a, int32_t hidden_scale_b, int32_t hidden_zp, - const int8_t* projection_weights, int32_t proj_scale_a, - int32_t proj_scale_b, const int32_t* projection_bias, - int32_t output_state_zp, int8_t quantized_proj_clip, int8_t* output_state, - int16_t* scratch0, int8_t* scratch1, int32_t* scratch2) { - // Note: unlike float/hybrid, the activation is always Tanh. - micro_tensor_utils::ApplyTanh(15 + cell_state_scale, cell_state, n_batch, - n_cell, scratch0); - micro_tensor_utils::CwiseMul(output_gate, scratch0, hidden_scale_a, - hidden_scale_b, n_batch, n_cell, hidden_zp, - scratch1); - - const bool use_projection = (projection_weights != nullptr); - - if (use_projection) { - // Note: no bias like in float/hybrid - memset(output_state, 0, n_batch * n_output * sizeof(int8_t)); - micro_tensor_utils::MatrixBatchVectorMultiplyAccumulate( - scratch1, projection_bias, projection_weights, proj_scale_a, - proj_scale_b, n_batch, n_cell, n_output, output_state_zp, scratch2, - output_state, nullptr); - if (quantized_proj_clip > 0) { - micro_tensor_utils::CwiseClipping(output_state, n_batch * n_output, - quantized_proj_clip); - } - } else { - std::memcpy(output_state, scratch1, n_batch * n_output * sizeof(int8_t)); - } -} - -// Calculates a single LSTM gate, int8x8_8 version. -// Implements the same functionality as CalculateLstmGateFloat. -void CalculateLstmGateInteger8x8_8( - // Inputs and weights - const int8_t* input, int32_t input_zp, const int8_t* input_to_gate_weight, - const int32_t input_to_gate_scale_a, const int32_t input_to_gate_scale_b, - const int32_t input_times_weights_scale_a, - const int32_t input_times_weights_scale_b, - const int32_t input_times_weights_zp, - // Output state and weights - const int8_t* output_state, const int32_t output_state_zp, - const int8_t* recurrent_to_gate_weight, - const int32_t recurrent_to_gate_scale_a, - const int32_t recurrent_to_gate_scale_b, - const int32_t output_state_times_weights_scale_a, - const int32_t output_state_times_weights_scale_b, - const int32_t output_state_times_weights_zp, - // Layer normalization parameters (layer norm LSTM) - const int16_t* layer_norm_gate_weight, - const int32_t layer_norm_gate_scale_a, - const int32_t layer_norm_gate_scale_b, const int32_t* gate_bias, - // Array sizes - const int n_batch, const int n_input, const int n_output, const int n_cell, - const TfLiteFusedActivation activation, - // Output - int16_t* gate, - // Scratch arrays, both sized n_batch*n_cell - int8_t* scratch0, int8_t* scratch1) { - // Multiply input * input_weights => scratch0 - micro_tensor_utils::MatrixBatchVectorMultiply( - input, input_zp, input_to_gate_weight, input_to_gate_scale_a, - input_to_gate_scale_b, n_batch, n_input, n_cell, scratch0, - input_times_weights_zp); - // Multiply output_state * recurrent_weights => scratch1 - micro_tensor_utils::MatrixBatchVectorMultiply( - output_state, output_state_zp, recurrent_to_gate_weight, - recurrent_to_gate_scale_a, recurrent_to_gate_scale_b, n_batch, n_output, - n_cell, scratch1, output_state_times_weights_zp); - // Add scratch0 + scratch1 => gate - micro_tensor_utils::TwoGateSaturatingAdd( - scratch0, input_times_weights_zp, scratch1, output_state_times_weights_zp, - input_times_weights_scale_a, input_times_weights_scale_b, - output_state_times_weights_scale_a, output_state_times_weights_scale_b, - n_batch, n_cell, gate); - // Apply layer normalization. - micro_tensor_utils::ApplyLayerNormFloat( - gate, layer_norm_gate_weight, layer_norm_gate_scale_a, - layer_norm_gate_scale_b, gate_bias, n_batch, n_cell, gate); - // Apply activation. - switch (activation) { - case kTfLiteActSigmoid: - micro_tensor_utils::ApplySigmoidFloat(gate, n_batch, n_cell, gate); - break; - case kTfLiteActTanh: - micro_tensor_utils::ApplyTanhFloat(gate, n_batch, n_cell, -12, gate); - break; - default: - // Only Sigmoid or Tanh is used. - TFLITE_ASSERT_FALSE; - } -} - -// Calculates the output state tensor of an LSTM step. See Float and hybrid -// versions as well. -// -// Parameters: -// - n_batch: batches: the number of distinct vectors in each array. -// - n_cell, n_output: sizes of vectors. -// - cell_state, output_gate: input vectors, size n_batch*n_cell. -// - projection_weights, proj_scale_[a|b], projection_bias: -// constant inputs, describing projection matrix and bias. -// - output_state_zp: zero point of the output state. -// - quantized_proj_clip: if > 0, clip the output of the projection. -// - output_state: output vector, size n_batch*n_output. Must be contigous. -// - scratch: scratch area of size n_batch*n_cell -void CalculateLstmOutputInteger8x8_8( - int n_batch, int n_cell, int n_output, const int16_t* cell_state, - const int16_t* output_gate, const int8_t* projection_weights, - int32_t proj_scale_a, int32_t proj_scale_b, const int32_t* projection_bias, - int32_t output_state_zp, int32_t quantized_proj_clip, int8_t* output_state, - int16_t* scratch) { - // Note: unlike float/hybrid, the activation is always Tanh. - micro_tensor_utils::ApplyTanhFloat(cell_state, n_batch, n_cell, -15, scratch); - micro_tensor_utils::CwiseMul(output_gate, scratch, n_batch, n_cell, - 15 + 15 - 15, scratch); - // Note: no bias like in float/hybrid - micro_tensor_utils::MatrixBatchVectorMultiply( - scratch, projection_weights, proj_scale_a, proj_scale_b, projection_bias, - n_batch, n_cell, n_output, output_state_zp, output_state); - if (quantized_proj_clip > 0) { - micro_tensor_utils::CwiseClipping(output_state, n_batch * n_output, - quantized_proj_clip); - } -} - -// Performs an LSTM batch inference step for input specified by input_ptr. -// The LSTM cell is specified by the pointers to its weights (*_weights_ptr) and -// biases (*_bias_ptr), and buffers (*_scratch), along with additional -// parameters: -// - params: various LSTM params including activation, clipping, etc., -// - n_batch: size of batch, -// - n_cell: number of cells (or units), -// - n_input: the input size, -// - n_aux_input: the auxiliary input size. -// - n_output: the output size. -// - output_batch_leading_dim: the leading dimension of the output buffer. -// -// Input of size 'n_batch * n_input': -// input_ptr -// Input of size 'n_batch * n_aux_input': -// aux_input_ptr - optional (can be nullptr) -// -// LSTM weights: -// Input weights of size 'n_cell * n_input': -// input_to_input_weights - optional -// input_to_forget_weights -// input_to_cell_weights -// input_to_output_weights -// Auxiliary input weights of size 'n_cell * n_aux_input': -// aux_input_to_input_weights - optional -// aux_input_to_forget_weights - optional -// aux_input_to_cell_weights - optional -// aux_input_to_output_weights - optional -// Recurrent weights of size 'n_cell * n_output': -// recurrent_to_input_weights - optional -// recurrent_to_forget_weights -// recurrent_to_cell_weights -// recurrent_to_input_weights -// Peephole weights of size 'n_cell', representing diagonal matrices. -// cell_to_input_weights - optional -// cell_to_cell_weights - optional -// cell_to_output_weights - optional -// Projection weights of size 'n_output * n_cell' -// projection_weights_ptr - optional -// Gate biases of size 'n_cell': -// input_gate_bias_ptr - optional -// forget_gate_bias_ptr -// cell_gate_bias_ptr -// output_gate_bias_ptr -// -// Layer norm coefficients of size 'n_cell', representing diagonal matrices. -// input_layer_norm_coefficients_ptr - optional -// forget_layer_norm_coefficients_ptr - optional -// cell_layer_norm_coefficients_ptr - optional -// output_layer_norm_coefficients_ptr - optional -// -// The pointers to the cell and output state and the output are updated. -// -// The pointers input_ptr, aux_input_ptr, and output_ptr point to data aligned -// in batch_major order, and each step processes batch_size many inputs from -// input_ptr, and updates batch_size many cell and output states. -// -// The output_batch_dim is output.shape[-1], i.e. the outermost dimension of the -// output tensor, and in most cases will be equal to n_output. It is usually not -// when we want to store the LSTM output into a slice of the output tensor, e.g. -// for bidirectional LSTMs with merge_outputs. In this case, the batched -// operations cannot be used since they assume that the batched outputs are -// contiguous, and we manually loop over the batched outputs. -inline void LstmStepFloat( - const float* input_ptr, const float* input_to_input_weights_ptr, - const float* input_to_forget_weights_ptr, - const float* input_to_cell_weights_ptr, - const float* input_to_output_weights_ptr, const float* aux_input_ptr, - const float* aux_input_to_input_weights_ptr, - const float* aux_input_to_forget_weights_ptr, - const float* aux_input_to_cell_weights_ptr, - const float* aux_input_to_output_weights_ptr, - const float* recurrent_to_input_weights_ptr, - const float* recurrent_to_forget_weights_ptr, - const float* recurrent_to_cell_weights_ptr, - const float* recurrent_to_output_weights_ptr, - const float* cell_to_input_weights_ptr, - const float* cell_to_forget_weights_ptr, - const float* cell_to_output_weights_ptr, - const float* input_layer_norm_coefficients_ptr, - const float* forget_layer_norm_coefficients_ptr, - const float* cell_layer_norm_coefficients_ptr, - const float* output_layer_norm_coefficients_ptr, - const float* input_gate_bias_ptr, const float* forget_gate_bias_ptr, - const float* cell_gate_bias_ptr, const float* output_gate_bias_ptr, - const float* projection_weights_ptr, const float* projection_bias_ptr, - const TfLiteLSTMParams* params, int n_batch, int n_cell, int n_input, - int n_aux_input, int n_output, int output_batch_leading_dim, - float* output_state_ptr, float* cell_state_ptr, float* scratch0, - float* scratch1, float* scratch2, float* scratch3, float* output_ptr) { - // Since we have already checked that weights are all there or none, we can - // check the existence of only one to the get the condition. - const bool use_cifg = (input_to_input_weights_ptr == nullptr); - - // Make named scratch buffers. - float* input_gate_scratch = scratch0; - float* forget_gate_scratch = scratch1; - float* cell_gate_scratch = scratch2; - float* output_gate_scratch = scratch3; - - // Check if inputs are all zeros so we can skip some computations. - const bool is_input_all_zeros = - micro_tensor_utils::IsZeroVector(input_ptr, n_batch * n_input); - const bool is_aux_input_all_zeros = - (aux_input_ptr == nullptr || - micro_tensor_utils::IsZeroVector(aux_input_ptr, n_batch * n_aux_input)); - if (!use_cifg) { - // Calculate the input gate. (If not CIFG.) - CalculateLstmGateFloat( - input_ptr, input_to_input_weights_ptr, aux_input_ptr, - aux_input_to_input_weights_ptr, output_state_ptr, - recurrent_to_input_weights_ptr, cell_state_ptr, - cell_to_input_weights_ptr, input_layer_norm_coefficients_ptr, - input_gate_bias_ptr, n_batch, n_input, n_aux_input, n_output, n_cell, - /*activation=*/kTfLiteActSigmoid, input_gate_scratch, - is_input_all_zeros, is_aux_input_all_zeros); - } - // Calculate the forget gate. - CalculateLstmGateFloat( - input_ptr, input_to_forget_weights_ptr, aux_input_ptr, - aux_input_to_forget_weights_ptr, output_state_ptr, - recurrent_to_forget_weights_ptr, cell_state_ptr, - cell_to_forget_weights_ptr, forget_layer_norm_coefficients_ptr, - forget_gate_bias_ptr, n_batch, n_input, n_aux_input, n_output, n_cell, - /*activation=*/kTfLiteActSigmoid, forget_gate_scratch, is_input_all_zeros, - is_aux_input_all_zeros); - // Calculate the cell update gate. - CalculateLstmGateFloat(input_ptr, input_to_cell_weights_ptr, aux_input_ptr, - aux_input_to_cell_weights_ptr, output_state_ptr, - recurrent_to_cell_weights_ptr, /*cell_state=*/nullptr, - /*cell_to_gate_weights=*/nullptr, - cell_layer_norm_coefficients_ptr, cell_gate_bias_ptr, - n_batch, n_input, n_aux_input, n_output, n_cell, - params->activation, cell_gate_scratch, - is_input_all_zeros, is_aux_input_all_zeros); - // Update the cell state. - UpdateLstmCellFloat(n_batch, n_cell, cell_state_ptr, input_gate_scratch, - forget_gate_scratch, cell_gate_scratch, use_cifg, - params->cell_clip); - // Calculate output gate. - CalculateLstmGateFloat( - input_ptr, input_to_output_weights_ptr, aux_input_ptr, - aux_input_to_output_weights_ptr, output_state_ptr, - recurrent_to_output_weights_ptr, cell_state_ptr, - cell_to_output_weights_ptr, output_layer_norm_coefficients_ptr, - output_gate_bias_ptr, n_batch, n_input, n_aux_input, n_output, n_cell, - /*activation=*/kTfLiteActSigmoid, output_gate_scratch, is_input_all_zeros, - is_aux_input_all_zeros); - // Update the output state. - CalculateLstmOutputFloat(n_batch, n_cell, n_output, cell_state_ptr, - output_gate_scratch, params->activation, - projection_weights_ptr, projection_bias_ptr, - params->proj_clip, output_state_ptr, scratch2); - // Copy output state to the output. Note that the output's rows may not be - // contiguous (output_batch_leading_dim != n_output). - for (int b = 0; b < n_batch; b++) { - std::memcpy(output_ptr + b * output_batch_leading_dim, - output_state_ptr + b * n_output, n_output * sizeof(float)); - } -} - -// Same as above but with quantized weight matrices. In detail: -// Input of size 'n_batch * n_input': -// input_ptr -// Input of size 'n_batch * n_aux_input': -// aux_input_ptr - optional (can be nullptr) -// -// LSTM weights: -// Quantized input weights of size 'n_cell * n_input': -// input_to_input_weights - optional -// input_to_forget_weights -// input_to_cell_weights -// input_to_input_weights -// Quantized auxiliary input weights of size 'n_cell * n_aux_input': -// aux_input_to_input_weights - optional -// aux_input_to_forget_weights - optional -// aux_input_to_cell_weights - optional -// aux_input_to_output_weights - optional -// Quantized recurrent weights of size 'n_cell * n_output': -// recurrent_to_input_weights - optional -// recurrent_to_forget_weights -// recurrent_to_cell_weights -// recurrent_to_input_weights -// Quantized peephole weights of size 'n_cell', representing diagonal matrices. -// cell_to_input_weights - optional -// cell_to_cell_weights - optional -// cell_to_output_weights - optional -// Quantized projection weights of size 'n_output * n_cell' -// projection_weights_ptr - optional -// Weight scales (scalars) for each of the weights above. -// input_to_input_weights_scale - optional -// input_to_forget_weights_scale -// input_to_cell_weights_scale -// input_to_output_weights_scale -// aux_input_to_input_weights_scale - optional -// aux_input_to_forget_weights_scale - optional -// aux_input_to_cell_weights_scale - optional -// aux_input_to_output_weights_scale - optional -// recurrent_to_input_weights_scale - optional -// recurrent_to_forget_weights_scale -// recurrent_to_cell_weights_scale -// recurrent_to_output_weights_scale -// cell_to_input_weights_scale, -// cell_to_forget_weights_scale, -// cell_to_output_weights_scale, -// projection_weights_scale - optional -// Gate biases of size 'n_cell': -// input_gate_bias_ptr - optional -// forget_gate_bias_ptr -// cell_gate_bias_ptr -// output_gate_bias_ptr -// -// Layer norm coefficients of size 'n_cell', representing diagonal matrices. -// input_layer_norm_coefficients_ptr - optional -// forget_layer_norm_coefficients_ptr - optional -// cell_layer_norm_coefficients_ptr - optional -// output_layer_norm_coefficients_ptr - optional -// -// Temporary pre-allocated storage for quantized values: -// quantized_input_ptr (same size as input_ptr) -// quantized_output_state_ptr (same size as output_state_ptr) -// quantized_output_scratch (same size as cell_state_ptr) -// Temporary pre-allocated storage for recovered values: -// recovered_cell_weights (same size as cell_to_*_weights) -// -// Outputs: -// output_state_ptr - size 'n_batch * n_output' -// cell_state_ptr - size 'n_batch * n_cell' -// output_ptr - size 'n_batch * output_batch_leading_dim' -inline void LstmStepHybrid( - const float* input_ptr, const int8_t* input_to_input_weights_ptr, - const uint8_t* input_to_input_weights_ledger_ptr, - float input_to_input_weights_scale, - const int8_t* input_to_forget_weights_ptr, - const uint8_t* input_to_forget_weights_ledger_ptr, - float input_to_forget_weights_scale, - const int8_t* input_to_cell_weights_ptr, - const uint8_t* input_to_cell_weights_ledger_ptr, - float input_to_cell_weights_scale, - const int8_t* input_to_output_weights_ptr, - const uint8_t* input_to_output_weights_ledger_ptr, - float input_to_output_weights_scale, const float* aux_input_ptr, - const int8_t* aux_input_to_input_weights_ptr, - float aux_input_to_input_weights_scale, - const int8_t* aux_input_to_forget_weights_ptr, - float aux_input_to_forget_weights_scale, - const int8_t* aux_input_to_cell_weights_ptr, - float aux_input_to_cell_weights_scale, - const int8_t* aux_input_to_output_weights_ptr, - float aux_input_to_output_weights_scale, - const int8_t* recurrent_to_input_weights_ptr, - const uint8_t* recurrent_to_input_weights_ledger_ptr, - float recurrent_to_input_weights_scale, - const int8_t* recurrent_to_forget_weights_ptr, - const uint8_t* recurrent_to_forget_weights_ledger_ptr, - float recurrent_to_forget_weights_scale, - const int8_t* recurrent_to_cell_weights_ptr, - const uint8_t* recurrent_to_cell_weights_ledger_ptr, - float recurrent_to_cell_weights_scale, - const int8_t* recurrent_to_output_weights_ptr, - const uint8_t* recurrent_to_output_weights_ledger_ptr, - float recurrent_to_output_weights_scale, - const int8_t* cell_to_input_weights_ptr, float cell_to_input_weights_scale, - const int8_t* cell_to_forget_weights_ptr, - float cell_to_forget_weights_scale, - const int8_t* cell_to_output_weights_ptr, - float cell_to_output_weights_scale, - const float* input_layer_norm_coefficients_ptr, - const float* forget_layer_norm_coefficients_ptr, - const float* cell_layer_norm_coefficients_ptr, - const float* output_layer_norm_coefficients_ptr, - const float* input_gate_bias_ptr, const float* forget_gate_bias_ptr, - const float* cell_gate_bias_ptr, const float* output_gate_bias_ptr, - const int8_t* projection_weights_ptr, - const uint8_t* projection_weights_ledger_ptr, - float projection_weights_scale, const float* projection_bias_ptr, - const TfLiteLSTMParams* params, int n_batch, int n_cell, int n_input, - int n_aux_input, int n_output, int output_batch_leading_dim, - float* scratch0, float* scratch1, float* scratch2, float* scratch3, - float* scales, float* input_sf, float* aux_input_sf, float* output_state_sf, - float* scaling_factors_scratch, float* recovered_cell_weights, - int8_t* quantized_input_ptr, int8_t* quantized_aux_input_ptr, - int8_t* quantized_output_state_ptr, int8_t* quantized_output_scratch, - float* output_state_ptr, float* cell_state_ptr, int32_t* accum_scratch_ptr, - float* output_ptr, int32_t* input_zp, int32_t* aux_input_zp, - int32_t* output_state_zp, int32_t* row_sums, int row_sums_size, - bool* compute_row_sums, bool asymmetric_quantize_inputs) { - // Since we have already checked that weights are all there or none, we - // can check the existence of only one to the get the condition. - const bool use_cifg = (input_to_input_weights_ptr == nullptr); - // Make named scratch buffers for the different gates. - float* input_gate_scratch = scratch0; - float* forget_gate_scratch = scratch1; - float* cell_gate_scratch = scratch2; - float* output_gate_scratch = scratch3; - - int32_t* input_to_input_row_sums = nullptr; - int32_t* input_to_forget_row_sums = nullptr; - int32_t* input_to_cell_row_sums = nullptr; - int32_t* input_to_output_row_sums = nullptr; - int32_t* aux_input_to_input_row_sums = nullptr; - int32_t* aux_input_to_forget_row_sums = nullptr; - int32_t* aux_input_to_cell_row_sums = nullptr; - int32_t* aux_input_to_output_row_sums = nullptr; - int32_t* recurrent_to_input_row_sums = nullptr; - int32_t* recurrent_to_forget_row_sums = nullptr; - int32_t* recurrent_to_cell_row_sums = nullptr; - int32_t* recurrent_to_output_row_sums = nullptr; - int32_t* projection_weights_row_sums = nullptr; - - if (asymmetric_quantize_inputs) { - int num_row_sums = use_cifg ? 6 : 8; - if (aux_input_ptr != nullptr) { - num_row_sums += use_cifg ? 3 : 4; - } - if (projection_weights_ptr != nullptr) { - num_row_sums += ceil(static_cast(n_output) / n_cell); - } - TFLITE_DCHECK(row_sums_size == num_row_sums); - input_to_input_row_sums = row_sums; - input_to_forget_row_sums = - use_cifg ? input_to_input_row_sums : input_to_input_row_sums + n_cell; - input_to_cell_row_sums = input_to_forget_row_sums + n_cell; - input_to_output_row_sums = input_to_cell_row_sums + n_cell; - if (aux_input_ptr != nullptr) { - aux_input_to_input_row_sums = input_to_output_row_sums + n_cell; - aux_input_to_forget_row_sums = use_cifg - ? aux_input_to_input_row_sums - : aux_input_to_input_row_sums + n_cell; - aux_input_to_cell_row_sums = aux_input_to_forget_row_sums + n_cell; - aux_input_to_output_row_sums = aux_input_to_cell_row_sums + n_cell; - } - recurrent_to_input_row_sums = aux_input_ptr - ? aux_input_to_output_row_sums + n_cell - : input_to_output_row_sums + n_cell; - recurrent_to_forget_row_sums = use_cifg - ? recurrent_to_input_row_sums - : recurrent_to_input_row_sums + n_cell; - recurrent_to_cell_row_sums = recurrent_to_forget_row_sums + n_cell; - recurrent_to_output_row_sums = recurrent_to_cell_row_sums + n_cell; - if (projection_weights_ptr != nullptr) { - projection_weights_row_sums = recurrent_to_output_row_sums + n_cell; - } - if (*compute_row_sums) { - ComputeRowSums( - input_to_input_row_sums, input_to_forget_row_sums, - input_to_cell_row_sums, input_to_output_row_sums, - aux_input_to_input_row_sums, aux_input_to_forget_row_sums, - aux_input_to_cell_row_sums, aux_input_to_output_row_sums, - recurrent_to_input_row_sums, recurrent_to_forget_row_sums, - recurrent_to_cell_row_sums, recurrent_to_output_row_sums, - projection_weights_row_sums, row_sums, n_cell, n_input, n_aux_input, - n_output, input_to_input_weights_ptr, input_to_forget_weights_ptr, - input_to_cell_weights_ptr, input_to_output_weights_ptr, - aux_input_to_input_weights_ptr, aux_input_to_forget_weights_ptr, - aux_input_to_cell_weights_ptr, aux_input_to_output_weights_ptr, - recurrent_to_input_weights_ptr, recurrent_to_forget_weights_ptr, - recurrent_to_cell_weights_ptr, recurrent_to_output_weights_ptr, - projection_weights_ptr, use_cifg, aux_input_ptr); - *compute_row_sums = false; - } - } - - // Check if inputs are all zeros so we can skip some computations. - const bool is_input_all_zeros = - micro_tensor_utils::IsZeroVector(input_ptr, n_batch * n_input); - const bool is_aux_input_all_zeros = - (aux_input_ptr == nullptr || - micro_tensor_utils::IsZeroVector(aux_input_ptr, n_batch * n_aux_input)); - const bool is_output_state_all_zeros = - micro_tensor_utils::IsZeroVector(output_state_ptr, n_batch * n_output); - // Quantize inputs. - if (!is_input_all_zeros) { - micro_tensor_utils::BatchQuantizeFloats( - input_ptr, n_batch, n_input, quantized_input_ptr, input_sf, input_zp, - asymmetric_quantize_inputs); - } - if (!is_aux_input_all_zeros) { - micro_tensor_utils::BatchQuantizeFloats( - aux_input_ptr, n_batch, n_aux_input, quantized_aux_input_ptr, - aux_input_sf, aux_input_zp, asymmetric_quantize_inputs); - } - if (!is_output_state_all_zeros) { - micro_tensor_utils::BatchQuantizeFloats( - output_state_ptr, n_batch, n_output, quantized_output_state_ptr, - output_state_sf, output_state_zp, asymmetric_quantize_inputs); - } - if (!use_cifg) { - // Calculate the input gate. (If not CIFG.) - CalculateLstmGateHybrid( - quantized_input_ptr, input_sf, input_zp, input_to_input_weights_ptr, - input_to_input_weights_ledger_ptr, input_to_input_weights_scale, - input_to_input_row_sums, quantized_aux_input_ptr, aux_input_sf, - aux_input_zp, aux_input_to_input_weights_ptr, - aux_input_to_input_weights_scale, aux_input_to_input_row_sums, - quantized_output_state_ptr, output_state_sf, output_state_zp, - recurrent_to_input_weights_ptr, recurrent_to_input_weights_ledger_ptr, - recurrent_to_input_weights_scale, recurrent_to_input_row_sums, - cell_state_ptr, cell_to_input_weights_ptr, cell_to_input_weights_scale, - input_layer_norm_coefficients_ptr, input_gate_bias_ptr, n_batch, - n_input, n_aux_input, n_output, n_cell, kTfLiteActSigmoid, - input_gate_scratch, is_input_all_zeros, is_aux_input_all_zeros, - is_output_state_all_zeros, compute_row_sums, scaling_factors_scratch, - recovered_cell_weights, scales, accum_scratch_ptr); - } - // Calculate the forget gate. - CalculateLstmGateHybrid( - quantized_input_ptr, input_sf, input_zp, input_to_forget_weights_ptr, - input_to_forget_weights_ledger_ptr, input_to_forget_weights_scale, - input_to_forget_row_sums, quantized_aux_input_ptr, aux_input_sf, - aux_input_zp, aux_input_to_forget_weights_ptr, - aux_input_to_forget_weights_scale, aux_input_to_forget_row_sums, - quantized_output_state_ptr, output_state_sf, output_state_zp, - recurrent_to_forget_weights_ptr, recurrent_to_forget_weights_ledger_ptr, - recurrent_to_forget_weights_scale, recurrent_to_forget_row_sums, - cell_state_ptr, cell_to_forget_weights_ptr, cell_to_forget_weights_scale, - forget_layer_norm_coefficients_ptr, forget_gate_bias_ptr, n_batch, - n_input, n_aux_input, n_output, n_cell, kTfLiteActSigmoid, - forget_gate_scratch, is_input_all_zeros, is_aux_input_all_zeros, - is_output_state_all_zeros, compute_row_sums, scaling_factors_scratch, - recovered_cell_weights, scales, accum_scratch_ptr); - // Calculate the cell update gate. - CalculateLstmGateHybrid( - quantized_input_ptr, input_sf, input_zp, input_to_cell_weights_ptr, - input_to_cell_weights_ledger_ptr, input_to_cell_weights_scale, - input_to_cell_row_sums, quantized_aux_input_ptr, aux_input_sf, - aux_input_zp, aux_input_to_cell_weights_ptr, - aux_input_to_cell_weights_scale, aux_input_to_cell_row_sums, - quantized_output_state_ptr, output_state_sf, output_state_zp, - recurrent_to_cell_weights_ptr, recurrent_to_cell_weights_ledger_ptr, - recurrent_to_cell_weights_scale, recurrent_to_cell_row_sums, - /*cell_state=*/nullptr, /*cell_to_gate_weights=*/nullptr, - /*cell_to_gate_weights_scale=*/0.0f, cell_layer_norm_coefficients_ptr, - cell_gate_bias_ptr, n_batch, n_input, n_aux_input, n_output, n_cell, - params->activation, cell_gate_scratch, is_input_all_zeros, - is_aux_input_all_zeros, is_output_state_all_zeros, compute_row_sums, - scaling_factors_scratch, recovered_cell_weights, scales, - accum_scratch_ptr); - // Update the cell state. - UpdateLstmCellFloat(n_batch, n_cell, cell_state_ptr, input_gate_scratch, - forget_gate_scratch, cell_gate_scratch, use_cifg, - params->cell_clip); - // Calculate the output gate. - CalculateLstmGateHybrid( - quantized_input_ptr, input_sf, input_zp, input_to_output_weights_ptr, - input_to_output_weights_ledger_ptr, input_to_output_weights_scale, - input_to_output_row_sums, quantized_aux_input_ptr, aux_input_sf, - aux_input_zp, aux_input_to_output_weights_ptr, - aux_input_to_output_weights_scale, aux_input_to_output_row_sums, - quantized_output_state_ptr, output_state_sf, output_state_zp, - recurrent_to_output_weights_ptr, recurrent_to_output_weights_ledger_ptr, - recurrent_to_output_weights_scale, recurrent_to_output_row_sums, - cell_state_ptr, cell_to_output_weights_ptr, cell_to_output_weights_scale, - output_layer_norm_coefficients_ptr, output_gate_bias_ptr, n_batch, - n_input, n_aux_input, n_output, n_cell, kTfLiteActSigmoid, - output_gate_scratch, is_input_all_zeros, is_aux_input_all_zeros, - is_output_state_all_zeros, compute_row_sums, scaling_factors_scratch, - recovered_cell_weights, scales, accum_scratch_ptr); - // Update the output state. - CalculateLstmOutputHybrid( - n_batch, n_cell, n_output, cell_state_ptr, output_gate_scratch, - params->activation, projection_weights_ptr, projection_weights_ledger_ptr, - projection_weights_scale, projection_bias_ptr, params->proj_clip, - output_state_ptr, asymmetric_quantize_inputs, projection_weights_row_sums, - compute_row_sums, scratch2, quantized_output_scratch, input_sf, input_zp, - accum_scratch_ptr, scales); - // Copy output state to the output. Note that the output's rows may not be - // contiguous (output_batch_leading_dim != n_output). - for (int b = 0; b < n_batch; b++) { - std::memcpy(output_ptr + b * output_batch_leading_dim, - output_state_ptr + b * n_output, n_output * sizeof(float)); - } -} - -// Fully quantized lstm kernel for 16 bit gate matmul output. -// -// Input tensor of size n_batch * n_input: -// input_ptr -// -// LSTM weights: -// Quantized input weights of size 'n_cell * n_input': -// input_to_input_weight_ptr - optional -// input_to_forget_weight_ptr - optional -// input_to_cell_weight_ptr - optional -// input_to_output_weight_ptr - optional -// -// Quantized recurrent weights of size 'n_cell * n_output': -// recurrent_to_input_weight_ptr - optional -// recurrent_to_forget_weights_ptr -// recurrent_to_cell_weights_ptr -// recurrent_to_input_weights_ptr -// -// Quantized peephole weights of size 'n_cell', representing diagonal matrices. -// cell_to_input_weights - optional -// cell_to_cell_weights - optional -// cell_to_output_weights - optional -// -// Quantized projection weights of size 'n_output * n_cell' -// projection_weight_ptr - optional -// -// Weight scales (scalars) for each of the weights above. -// effective_input_to_input_scale_a - optional -// effective_input_to_input_scale_b - optional -// effective_input_to_forget_scale_a -// effective_input_to_forget_scale_b -// effective_input_to_cell_scale_a -// effective_input_to_cell_scale_b -// effective_input_to_output_scale_a -// effective_input_to_output_scale_b -// effective_recurrent_to_input_scale_a - optional -// effective_recurrent_to_input_scale_b - optional -// effective_recurrent_to_forget_scale_a -// effective_recurrent_to_forget_scale_b -// effective_recurrent_to_cell_scale_a -// effective_recurrent_to_cell_scale_b -// effective_recurrent_to_output_scale_a -// effective_recurrent_to_output_scale_b -// effective_proj_scale_a - optional -// effective_proj_scale_b - optional -// -// Gate biases of size 'n_cell': -// input_gate_bias_ptr - optional -// forget_gate_bias_ptr -// cell_gate_bias_ptr -// output_gate_bias_ptr -// -// Layer norm coefficients of size 'n_cell', representing diagonal matrices. -// layer_norm_input_weight_ptr - optional -// layer_norm_forget_weight_ptr - optional -// layer_norm_cell_weight_ptr - optional -// layer_norm_output_weight_ptr - optional -// -// Layer norm scales of size 'n_cell'. -// layer_norm_input_scale_a - optional -// layer_norm_input_scale_b - optional -// layer_norm_forget_scale_a - optional -// layer_norm_forget_scale_b - optional -// layer_norm_cell_scale_a - optional -// layer_norm_cell_scale_b - optional -// layer_norm_output_scale_a - optional -// layer_norm_output_scale_b - optional -// -// Scalar values: -// quantized_cell_clip: quantized clip value for cell. -// quantized_proj_clip: quantized clip value for projection. -// cell_state_scale: the power of two scale for cell state. -// -// Zero points: -// output_state_zp: zero point of output state -// hidden_zp: zero point for hidden state. -// -// Temporary pre-allocated storage for the calculation. Each is of size n_cell * -// n_batch. -// scratch0 -// scratch1 -// scratch2 -// scratch3 -// scratch4 -// scratch5: this scratch buffer is created purely for optimizing the -// MatrixBatchVectorMultiplyAccumulate. -// -// Outputs: -// output_state_ptr - size 'n_batch * n_output' -// cell_state_ptr - size 'n_batch * n_cell' -// output_ptr - size 'n_batch * n_output' -// TODO(b/159947023): scratch0 is not used if (!cifg). Don't allocate then. -inline void LstmStepInteger8x8_16( - const int8_t* input_ptr, const int8_t* input_to_input_weight_ptr, - int32_t effective_input_to_input_scale_a, - int32_t effective_input_to_input_scale_b, - const int8_t* input_to_forget_weight_ptr, - int32_t effective_input_to_forget_scale_a, - int32_t effective_input_to_forget_scale_b, - const int8_t* input_to_cell_weight_ptr, - int32_t effective_input_to_cell_scale_a, - int32_t effective_input_to_cell_scale_b, - const int8_t* input_to_output_weight_ptr, - int32_t effective_input_to_output_scale_a, - int32_t effective_input_to_output_scale_b, - const int8_t* recurrent_to_input_weight_ptr, - int32_t effective_recurrent_to_input_scale_a, - int32_t effective_recurrent_to_input_scale_b, - const int8_t* recurrent_to_forget_weight_ptr, - int32_t effective_recurrent_to_forget_scale_a, - int32_t effective_recurrent_to_forget_scale_b, - const int8_t* recurrent_to_cell_weight_ptr, - int32_t effective_recurrent_to_cell_scale_a, - int32_t effective_recurrent_to_cell_scale_b, - const int8_t* recurrent_to_output_weight_ptr, - int32_t effective_recurrent_to_output_scale_a, - int32_t effective_recurrent_to_output_scale_b, - const int16_t* cell_to_input_weight_ptr, - int32_t effective_cell_to_input_scale_a, - int32_t effective_cell_to_input_scale_b, - const int16_t* cell_to_forget_weight_ptr, - int32_t effective_cell_to_forget_scale_a, - int32_t effective_cell_to_forget_scale_b, - const int16_t* cell_to_output_weight_ptr, - int32_t effective_cell_to_output_scale_a, - int32_t effective_cell_to_output_scale_b, - const int8_t* projection_weight_ptr, int32_t effective_proj_scale_a, - int32_t effective_proj_scale_b, int32_t hidden_zp, - int32_t effective_hidden_scale_a, int32_t effective_hidden_scale_b, - const int16_t* layer_norm_input_weight_ptr, - int32_t layer_norm_input_scale_a, int32_t layer_norm_input_scale_b, - const int16_t* layer_norm_forget_weight_ptr, - int32_t layer_norm_forget_scale_a, int32_t layer_norm_forget_scale_b, - const int16_t* layer_norm_cell_weight_ptr, int32_t layer_norm_cell_scale_a, - int32_t layer_norm_cell_scale_b, - const int16_t* layer_norm_output_weight_ptr, - int32_t layer_norm_output_scale_a, int32_t layer_norm_output_scale_b, - const int32_t* input_gate_bias_ptr, const int32_t* forget_gate_bias_ptr, - const int32_t* cell_gate_bias_ptr, const int32_t* output_gate_bias_ptr, - int16_t quantized_cell_clip, int8_t quantized_proj_clip, - int32_t cell_state_scale, int32_t input_variance_guard, - int32_t forget_variance_guard, int32_t cell_variance_guard, - int32_t output_variance_guard, - const int32_t* input_to_forget_effective_bias, - const int32_t* recurrent_to_forget_effective_bias, - const int32_t* input_to_cell_effective_bias, - const int32_t* recurrent_to_cell_effective_bias, - const int32_t* input_to_output_effective_bias, - const int32_t* recurrent_to_output_effective_bias, - const int32_t* input_to_input_effective_bias, - const int32_t* recurrent_to_input_effective_bias, - const int32_t* projection_effective_bias, int n_batch, int n_cell, - int n_input, int n_output, int8_t* output_state_ptr, - int32_t output_state_zp, int16_t* cell_state_ptr, int8_t* output_ptr, - int16_t* scratch0, int16_t* scratch1, int16_t* scratch2, int16_t* scratch3, - int8_t* scratch4, int32_t* scratch5) { - // Make named scratch buffers for the different gates. - int16_t* input_gate_scratch = scratch0; - int16_t* forget_gate_scratch = scratch1; - int16_t* cell_gate_scratch = scratch2; - int16_t* output_gate_scratch = scratch3; - - // Since we have already checked that weights are all there or none, we - // can check the existence of only one to the get the condition. - const bool use_cifg = (input_to_input_weight_ptr == nullptr); - - // Check for nullptrs. - TFLITE_DCHECK(input_to_forget_effective_bias); - TFLITE_DCHECK(recurrent_to_forget_effective_bias); - TFLITE_DCHECK(input_to_cell_effective_bias); - TFLITE_DCHECK(recurrent_to_cell_effective_bias); - TFLITE_DCHECK(input_to_output_effective_bias); - TFLITE_DCHECK(recurrent_to_output_effective_bias); - if (!use_cifg) { - TFLITE_DCHECK(input_to_input_effective_bias); - TFLITE_DCHECK(recurrent_to_input_effective_bias); - } - const bool use_projection = (projection_weight_ptr != nullptr); - if (use_projection) { - TFLITE_DCHECK(projection_effective_bias); - } - if (!use_cifg) { - // Calculate the input gate. (If not CIFG.) - CalculateLstmGateInteger8x8_16( - input_ptr, input_to_input_weight_ptr, input_to_input_effective_bias, - effective_input_to_input_scale_a, effective_input_to_input_scale_b, - output_state_ptr, recurrent_to_input_weight_ptr, - recurrent_to_input_effective_bias, effective_recurrent_to_input_scale_a, - effective_recurrent_to_input_scale_b, cell_state_ptr, - cell_to_input_weight_ptr, effective_cell_to_input_scale_a, - effective_cell_to_input_scale_b, layer_norm_input_weight_ptr, - input_gate_bias_ptr, layer_norm_input_scale_a, layer_norm_input_scale_b, - input_variance_guard, n_batch, n_input, n_output, n_cell, - kTfLiteActSigmoid, input_gate_scratch, scratch5); - } - // Calculate the forget gate. - CalculateLstmGateInteger8x8_16( - input_ptr, input_to_forget_weight_ptr, input_to_forget_effective_bias, - effective_input_to_forget_scale_a, effective_input_to_forget_scale_b, - output_state_ptr, recurrent_to_forget_weight_ptr, - recurrent_to_forget_effective_bias, effective_recurrent_to_forget_scale_a, - effective_recurrent_to_forget_scale_b, cell_state_ptr, - cell_to_forget_weight_ptr, effective_cell_to_forget_scale_a, - effective_cell_to_forget_scale_b, layer_norm_forget_weight_ptr, - forget_gate_bias_ptr, layer_norm_forget_scale_a, - layer_norm_forget_scale_b, forget_variance_guard, n_batch, n_input, - n_output, n_cell, kTfLiteActSigmoid, forget_gate_scratch, scratch5); - // Calculate the cell update gate. - CalculateLstmGateInteger8x8_16( - input_ptr, input_to_cell_weight_ptr, input_to_cell_effective_bias, - effective_input_to_cell_scale_a, effective_input_to_cell_scale_b, - output_state_ptr, recurrent_to_cell_weight_ptr, - recurrent_to_cell_effective_bias, effective_recurrent_to_cell_scale_a, - effective_recurrent_to_cell_scale_b, cell_state_ptr, - /*cell_to_gate_weights=*/nullptr, /*cell_to_gate_scale_a=*/0, - /*cell_to_gate_scale_b=*/0, layer_norm_cell_weight_ptr, - cell_gate_bias_ptr, layer_norm_cell_scale_a, layer_norm_cell_scale_b, - cell_variance_guard, n_batch, n_input, n_output, n_cell, kTfLiteActTanh, - cell_gate_scratch, scratch5); - // Update the cell state. - UpdateLstmCellInteger(n_batch, n_cell, cell_state_ptr, cell_state_scale, - input_gate_scratch, forget_gate_scratch, - cell_gate_scratch, use_cifg, quantized_cell_clip); - // Calculate the output gate. - CalculateLstmGateInteger8x8_16( - input_ptr, input_to_output_weight_ptr, input_to_output_effective_bias, - effective_input_to_output_scale_a, effective_input_to_output_scale_b, - output_state_ptr, recurrent_to_output_weight_ptr, - recurrent_to_output_effective_bias, effective_recurrent_to_output_scale_a, - effective_recurrent_to_output_scale_b, cell_state_ptr, - cell_to_output_weight_ptr, effective_cell_to_output_scale_a, - effective_cell_to_output_scale_b, layer_norm_output_weight_ptr, - output_gate_bias_ptr, layer_norm_output_scale_a, - layer_norm_output_scale_b, output_variance_guard, n_batch, n_input, - n_output, n_cell, kTfLiteActSigmoid, output_gate_scratch, scratch5); - // Update the output state. - CalculateLstmOutputInteger8x8_16( - n_batch, n_cell, n_output, cell_state_ptr, cell_state_scale, - output_gate_scratch, effective_hidden_scale_a, effective_hidden_scale_b, - hidden_zp, projection_weight_ptr, effective_proj_scale_a, - effective_proj_scale_b, projection_effective_bias, output_state_zp, - quantized_proj_clip, output_state_ptr, scratch0, scratch4, scratch5); - // Copy output state to the output. Note that unlike float or hybrid, output - // is always contiguous. - std::memcpy(output_ptr, output_state_ptr, - n_batch * n_output * sizeof(int8_t)); -} - -// Fully quantized lstm kernel for 8 bit gate matmul output. -// -// Input tensor of size n_batch * n_input: -// input_ptr -// -// LSTM weights: -// Quantized input weights of size 'n_cell * n_input': -// input_to_input_weight_ptr - optional -// input_to_forget_weight_ptr - optional -// input_to_cell_weight_ptr - optional -// input_to_output_weight_ptr - optional -// -// Quantized recurrent weights of size 'n_cell * n_output': -// recurrent_to_input_weight_ptr - optional -// recurrent_to_forget_weights_ptr -// recurrent_to_cell_weights_ptr -// recurrent_to_input_weights_ptr -// -// Quantized peephole weights of size 'n_cell', representing diagonal matrices. -// cell_to_input_weights - optional -// cell_to_cell_weights - optional -// cell_to_output_weights - optional -// -// Quantized projection weights of size 'n_output * n_cell' -// projection_weight_ptr - optional -// -// Weight scales (scalars) for each of the weights above. -// effective_input_to_input_scale_a - optional -// effective_input_to_input_scale_b - optional -// effective_input_to_forget_scale_a -// effective_input_to_forget_scale_b -// effective_input_to_cell_scale_a -// effective_input_to_cell_scale_b -// effective_input_to_output_scale_a -// effective_input_to_output_scale_b -// effective_recurrent_to_input_scale_a - optional -// effective_recurrent_to_input_scale_b - optional -// effective_recurrent_to_forget_scale_a -// effective_recurrent_to_forget_scale_b -// effective_recurrent_to_cell_scale_a -// effective_recurrent_to_cell_scale_b -// effective_recurrent_to_output_scale_a -// effective_recurrent_to_output_scale_b -// effective_proj_scale_a - optional -// effective_proj_scale_b - optional -// -// Gate biases of size 'n_cell': -// input_gate_bias_ptr - optional -// forget_gate_bias_ptr -// cell_gate_bias_ptr -// output_gate_bias_ptr -// -// Layer norm coefficients of size 'n_cell', representing diagonal matrices. -// layer_norm_input_weight_ptr - optional -// layer_norm_forget_weight_ptr - optional -// layer_norm_cell_weight_ptr - optional -// layer_norm_output_weight_ptr - optional -// -// Layer norm scales of size 'n_cell'. -// layer_norm_input_scale_a - optional -// layer_norm_input_scale_b - optional -// layer_norm_forget_scale_a - optional -// layer_norm_forget_scale_b - optional -// layer_norm_cell_scale_a - optional -// layer_norm_cell_scale_b - optional -// layer_norm_output_scale_a - optional -// layer_norm_output_scale_b - optional -// -// Scalar values: -// quantized_cell_clip: quantized clip value for cell. -// quantized_proj_clip: quantized clip value for projection. -// cell_state_scale: the power of two scale for cell state. -// -// Zero points: -// input_zp: zero point for input tensor. -// output_state_zp: zero point of output state. -// hidden_zp: zero point for hidden state. -// -// Temporary pre-allocated storage for the calculation. Each is of size n_cell * -// n_batch. -// scratch0 -// scratch1 -// scratch2 -// scratch3 -// scratch4 -// scratch5 -// scratch6 -// scratch7 -// -// Outputs: -// output_state_ptr - size 'n_batch * n_output' -// cell_state_ptr - size 'n_batch * n_cell' -// output_ptr - size 'n_batch * n_output' -// -// Can move zero point calculation into Prepare() for better perfomance. -// TODO(b/159947023): scratch5 is unused, remove. -inline void LstmStepInteger8x8_8( - const int8_t* input_ptr, int32_t input_zp, - const int8_t* input_to_input_weight_ptr, - int32_t effective_input_to_input_scale_a, - int32_t effective_input_to_input_scale_b, - const int8_t* input_to_forget_weight_ptr, - int32_t effective_input_to_forget_scale_a, - int32_t effective_input_to_forget_scale_b, - const int8_t* input_to_cell_weight_ptr, - int32_t effective_input_to_cell_scale_a, - int32_t effective_input_to_cell_scale_b, - const int8_t* input_to_output_weight_ptr, - int32_t effective_input_to_output_scale_a, - int32_t effective_input_to_output_scale_b, - const int8_t* recurrent_to_input_weight_ptr, - int32_t effective_recurrent_to_input_scale_a, - int32_t effective_recurrent_to_input_scale_b, - const int8_t* recurrent_to_forget_weight_ptr, - int32_t effective_recurrent_to_forget_scale_a, - int32_t effective_recurrent_to_forget_scale_b, - const int8_t* recurrent_to_cell_weight_ptr, - int32_t effective_recurrent_to_cell_scale_a, - int32_t effective_recurrent_to_cell_scale_b, - const int8_t* recurrent_to_output_weight_ptr, - int32_t effective_recurrent_to_output_scale_a, - int32_t effective_recurrent_to_output_scale_b, - const int8_t* cell_to_input_weight_ptr, - int32_t effective_cell_to_input_scale_a, - int32_t effective_cell_to_input_scale_b, - const int8_t* cell_to_forget_weight_ptr, - int32_t effective_cell_to_forget_scale_a, - int32_t effective_cell_to_forget_scale_b, - const int8_t* cell_to_output_weight_ptr, - int32_t effective_cell_to_output_scale_a, - int32_t effective_cell_to_output_scale_b, - const int8_t* projection_weight_ptr, int32_t effective_proj_scale_a, - int32_t effective_proj_scale_b, const int16_t* layer_norm_input_weight_ptr, - int32_t layer_norm_input_scale_a, int32_t layer_norm_input_scale_b, - const int16_t* layer_norm_forget_weight_ptr, - int32_t layer_norm_forget_scale_a, int32_t layer_norm_forget_scale_b, - const int16_t* layer_norm_cell_weight_ptr, int32_t layer_norm_cell_scale_a, - int32_t layer_norm_cell_scale_b, - const int16_t* layer_norm_output_weight_ptr, - int32_t layer_norm_output_scale_a, int32_t layer_norm_output_scale_b, - const int32_t* input_gate_bias_ptr, const int32_t* forget_gate_bias_ptr, - const int32_t* cell_gate_bias_ptr, const int32_t* output_gate_bias_ptr, - const int32_t* projection_bias_ptr, const TfLiteLSTMParams* params, - const int32_t* intermediate_scale_a, const int32_t* intermediate_scale_b, - const int32_t* intermediate_zp, int16_t quantized_cell_clip, - int8_t quantized_proj_clip, int n_batch, int n_cell, int n_input, - int n_output, int output_batch_leading_dim, int8_t* output_state_ptr, - int32_t output_state_zp, int16_t* cell_state_ptr, int8_t* output_ptr, - int8_t* scratch0, int8_t* scratch1, int16_t* scratch2, int16_t* scratch3, - int16_t* scratch4, int16_t* scratch5, int16_t* scratch6, - int16_t* scratch7) { - // TODO(b/159066113): scratch5 is unused, remove. - - // Make named scratch buffers for the different gates. - int16_t* forget_gate_scratch = scratch2; - int16_t* cell_gate_scratch = scratch3; - int16_t* output_gate_scratch = scratch4; - // no-CIFG is not supported here - - // Calculate the forget gate. - CalculateLstmGateInteger8x8_8( - input_ptr, input_zp, input_to_forget_weight_ptr, - effective_input_to_forget_scale_a, effective_input_to_forget_scale_b, - intermediate_scale_a[2], intermediate_scale_b[2], intermediate_zp[4], - output_state_ptr, output_state_zp, recurrent_to_forget_weight_ptr, - effective_recurrent_to_forget_scale_a, - effective_recurrent_to_forget_scale_b, intermediate_scale_a[3], - intermediate_scale_b[3], intermediate_zp[5], layer_norm_forget_weight_ptr, - layer_norm_forget_scale_a, layer_norm_forget_scale_b, - forget_gate_bias_ptr, n_batch, n_input, n_output, n_cell, - kTfLiteActSigmoid, forget_gate_scratch, scratch0, scratch1); - // Calculate the cell update gate. - CalculateLstmGateInteger8x8_8( - input_ptr, input_zp, input_to_cell_weight_ptr, - effective_input_to_cell_scale_a, effective_input_to_cell_scale_b, - intermediate_scale_a[4], intermediate_scale_b[4], intermediate_zp[7], - output_state_ptr, output_state_zp, recurrent_to_cell_weight_ptr, - effective_recurrent_to_cell_scale_a, effective_recurrent_to_cell_scale_b, - intermediate_scale_a[5], intermediate_scale_b[5], intermediate_zp[8], - layer_norm_cell_weight_ptr, layer_norm_cell_scale_a, - layer_norm_cell_scale_b, cell_gate_bias_ptr, n_batch, n_input, n_output, - n_cell, kTfLiteActTanh, cell_gate_scratch, scratch0, scratch1); - // Update the cell state. - UpdateLstmCellInteger(n_batch, n_cell, cell_state_ptr, - /*cell_state_scale=*/-15, /*input_gate=*/nullptr, - forget_gate_scratch, cell_gate_scratch, - /*use_cifg=*/true, quantized_cell_clip); - // Calculate the output gate. - CalculateLstmGateInteger8x8_8( - input_ptr, input_zp, input_to_output_weight_ptr, - effective_input_to_output_scale_a, effective_input_to_output_scale_b, - intermediate_scale_a[6], intermediate_scale_b[6], intermediate_zp[10], - output_state_ptr, output_state_zp, recurrent_to_output_weight_ptr, - effective_recurrent_to_output_scale_a, - effective_recurrent_to_output_scale_b, intermediate_scale_a[11], - intermediate_scale_b[7], intermediate_zp[7], layer_norm_output_weight_ptr, - layer_norm_output_scale_a, layer_norm_output_scale_b, - output_gate_bias_ptr, n_batch, n_input, n_output, n_cell, - kTfLiteActSigmoid, output_gate_scratch, scratch0, scratch1); - // Update the output state. - CalculateLstmOutputInteger8x8_8( - n_batch, n_cell, n_output, cell_state_ptr, output_gate_scratch, - projection_weight_ptr, effective_proj_scale_a, effective_proj_scale_b, - projection_bias_ptr, output_state_zp, quantized_proj_clip, - output_state_ptr, scratch2); - // Copy output state to the output. Note that unlike float or hybrid, output - // is always contigous. - std::memcpy(output_ptr, output_state_ptr, - n_batch * n_output * sizeof(int8_t)); -} - -} // namespace - -TfLiteStatus EvalFloatLstm( - const TfLiteEvalTensor* input, - const TfLiteEvalTensor* input_to_input_weights, - const TfLiteEvalTensor* input_to_forget_weights, - const TfLiteEvalTensor* input_to_cell_weights, - const TfLiteEvalTensor* input_to_output_weights, - const TfLiteEvalTensor* recurrent_to_input_weights, - const TfLiteEvalTensor* recurrent_to_forget_weights, - const TfLiteEvalTensor* recurrent_to_cell_weights, - const TfLiteEvalTensor* recurrent_to_output_weights, - const TfLiteEvalTensor* cell_to_input_weights, - const TfLiteEvalTensor* cell_to_forget_weights, - const TfLiteEvalTensor* cell_to_output_weights, - const TfLiteEvalTensor* input_layer_norm_coefficients, - const TfLiteEvalTensor* forget_layer_norm_coefficients, - const TfLiteEvalTensor* cell_layer_norm_coefficients, - const TfLiteEvalTensor* output_layer_norm_coefficients, - const TfLiteEvalTensor* aux_input, - const TfLiteEvalTensor* aux_input_to_input_weights, - const TfLiteEvalTensor* aux_input_to_forget_weights, - const TfLiteEvalTensor* aux_input_to_cell_weights, - const TfLiteEvalTensor* aux_input_to_output_weights, - const TfLiteEvalTensor* input_gate_bias, - const TfLiteEvalTensor* forget_gate_bias, - const TfLiteEvalTensor* cell_gate_bias, - const TfLiteEvalTensor* output_gate_bias, - const TfLiteEvalTensor* projection_weights, - const TfLiteEvalTensor* projection_bias, const TfLiteLSTMParams* params, - bool forward_sequence, bool time_major, int output_offset, - float* scratch_buffer, TfLiteEvalTensor* output_state, - TfLiteEvalTensor* cell_state, TfLiteEvalTensor* output) { - TFLITE_DCHECK(input->dims->size >= 2 && input->dims->size <= 3); - int max_time, n_batch; - if (input->dims->size == 3) { - max_time = (time_major) ? input->dims->data[0] : input->dims->data[1]; - n_batch = (time_major) ? input->dims->data[1] : input->dims->data[0]; - } else { - max_time = 1; - n_batch = input->dims->data[0]; - } - const int n_input = input->dims->data[input->dims->size - 1]; - const int aux_input_size = - (aux_input) ? aux_input->dims->data[aux_input->dims->size - 1] : 0; - - // n_cell and n_output will be the same size when there is no projection. - const int n_cell = input_to_output_weights->dims->data[0]; - const int n_output = recurrent_to_output_weights->dims->data[1]; - - // Since we have already checked that weights are all there or none, we can - // check the existence of only one to the get the condition. - const bool use_cifg = (input_to_input_weights == nullptr); - - // Index the scratch buffers pointers to the global scratch buffer. - float* input_gate_scratch = nullptr; - float* cell_gate_scratch = nullptr; - float* forget_gate_scratch = nullptr; - float* output_gate_scratch = nullptr; - if (use_cifg) { - cell_gate_scratch = scratch_buffer; - forget_gate_scratch = scratch_buffer + n_cell * n_batch; - output_gate_scratch = scratch_buffer + 2 * n_cell * n_batch; - } else { - input_gate_scratch = scratch_buffer; - cell_gate_scratch = scratch_buffer + n_cell * n_batch; - forget_gate_scratch = scratch_buffer + 2 * n_cell * n_batch; - output_gate_scratch = scratch_buffer + 3 * n_cell * n_batch; - } - - const int output_batch_leading_dim = - output->dims->data[output->dims->size - 1]; - if (time_major) { - // Loop through the sequence. - const int input_step = n_batch * n_input; - const int output_step = n_batch * output_batch_leading_dim; - for (int t = 0; t < max_time; t++) { - // If this is the forward_sequence, step forward, otherwise step - // backwards. - const int t_rel = forward_sequence ? t : max_time - t - 1; - const float* input_ptr = - tflite::micro::GetTensorData(input) + t_rel * input_step; - const float* aux_input_ptr = nullptr; - if (aux_input) { - aux_input_ptr = - tflite::micro::GetTensorData(aux_input) + t_rel * input_step; - } - float* output_ptr = tflite::micro::GetTensorData(output) + - t_rel * output_step + output_offset; - - LstmStepFloat( - input_ptr, - input_to_input_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_to_input_weights), - input_to_forget_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_to_forget_weights), - input_to_cell_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_to_cell_weights), - input_to_output_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_to_output_weights), - aux_input_ptr, - aux_input_to_input_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(aux_input_to_input_weights), - aux_input_to_forget_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - aux_input_to_forget_weights), - aux_input_to_cell_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(aux_input_to_cell_weights), - aux_input_to_output_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - aux_input_to_output_weights), - recurrent_to_input_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(recurrent_to_input_weights), - recurrent_to_forget_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - recurrent_to_forget_weights), - recurrent_to_cell_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(recurrent_to_cell_weights), - recurrent_to_output_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - recurrent_to_output_weights), - cell_to_input_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(cell_to_input_weights), - cell_to_forget_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(cell_to_forget_weights), - cell_to_output_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(cell_to_output_weights), - input_layer_norm_coefficients == nullptr - ? nullptr - : tflite::micro::GetTensorData( - input_layer_norm_coefficients), - forget_layer_norm_coefficients == nullptr - ? nullptr - : tflite::micro::GetTensorData( - forget_layer_norm_coefficients), - cell_layer_norm_coefficients == nullptr - ? nullptr - : tflite::micro::GetTensorData( - cell_layer_norm_coefficients), - output_layer_norm_coefficients == nullptr - ? nullptr - : tflite::micro::GetTensorData( - output_layer_norm_coefficients), - input_gate_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_gate_bias), - forget_gate_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(forget_gate_bias), - cell_gate_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(cell_gate_bias), - output_gate_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(output_gate_bias), - projection_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(projection_weights), - projection_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(projection_bias), - params, n_batch, n_cell, n_input, aux_input_size, n_output, - output_batch_leading_dim, - tflite::micro::GetTensorData(output_state), - tflite::micro::GetTensorData(cell_state), input_gate_scratch, - forget_gate_scratch, cell_gate_scratch, output_gate_scratch, - output_ptr); - } - } else { - for (int b = 0; b < n_batch; b++) { - const int input_step = n_input; - const int output_step = output_batch_leading_dim; - for (int t = 0; t < max_time; t++) { - // If this is the forward_sequence, step forward, otherwise step - // backwards. - const int t_rel = forward_sequence ? t : max_time - t - 1; - const int time_offset = b * max_time + t_rel; - const float* input_ptr = tflite::micro::GetTensorData(input) + - time_offset * input_step; - const float* aux_input_ptr = nullptr; - if (aux_input) { - aux_input_ptr = tflite::micro::GetTensorData(aux_input) + - time_offset * input_step; - } - float* output_ptr = tflite::micro::GetTensorData(output) + - time_offset * output_step + output_offset; - - // Offset the {output,cell}_state pointers to the right batch. - float* output_state_ptr = - tflite::micro::GetTensorData(output_state) + - b * output_batch_leading_dim; - float* cell_state_ptr = - tflite::micro::GetTensorData(cell_state) + b * n_cell; - // Offset the scratch pointers to the right batch. - float* input_gate_scratch_ptr = - input_gate_scratch ? input_gate_scratch + b * n_cell : nullptr; - float* forget_gate_scratch_ptr = forget_gate_scratch + b * n_cell; - float* cell_gate_scratch_ptr = cell_gate_scratch + b * n_cell; - float* output_gate_scratch_ptr = output_gate_scratch + b * n_cell; - - LstmStepFloat( - input_ptr, - input_to_input_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_to_input_weights), - input_to_forget_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_to_forget_weights), - input_to_cell_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_to_cell_weights), - input_to_output_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_to_output_weights), - aux_input_ptr, - aux_input_to_input_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - aux_input_to_input_weights), - aux_input_to_forget_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - aux_input_to_forget_weights), - aux_input_to_cell_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - aux_input_to_cell_weights), - aux_input_to_output_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - aux_input_to_output_weights), - recurrent_to_input_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - recurrent_to_input_weights), - recurrent_to_forget_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - recurrent_to_forget_weights), - recurrent_to_cell_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - recurrent_to_cell_weights), - recurrent_to_output_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - recurrent_to_output_weights), - cell_to_input_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(cell_to_input_weights), - cell_to_forget_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(cell_to_forget_weights), - cell_to_output_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(cell_to_output_weights), - input_layer_norm_coefficients == nullptr - ? nullptr - : tflite::micro::GetTensorData( - input_layer_norm_coefficients), - forget_layer_norm_coefficients == nullptr - ? nullptr - : tflite::micro::GetTensorData( - forget_layer_norm_coefficients), - cell_layer_norm_coefficients == nullptr - ? nullptr - : tflite::micro::GetTensorData( - cell_layer_norm_coefficients), - output_layer_norm_coefficients == nullptr - ? nullptr - : tflite::micro::GetTensorData( - output_layer_norm_coefficients), - input_gate_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_gate_bias), - forget_gate_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(forget_gate_bias), - cell_gate_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(cell_gate_bias), - output_gate_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(output_gate_bias), - projection_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(projection_weights), - projection_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(projection_bias), - params, - /*n_batch=*/1, n_cell, n_input, aux_input_size, n_output, - output_batch_leading_dim, output_state_ptr, cell_state_ptr, - input_gate_scratch_ptr, forget_gate_scratch_ptr, - cell_gate_scratch_ptr, output_gate_scratch_ptr, output_ptr); - } - } - } - return kTfLiteOk; -} - -TfLiteStatus EvalHybridLstm( - const HybridLstmScales* hybrid_lstm_scales, const TfLiteEvalTensor* input, - const TfLiteEvalTensor* input_to_input_weights, - const TfLiteEvalTensor* input_to_input_weights_ledger, - const TfLiteEvalTensor* input_to_forget_weights, - const TfLiteEvalTensor* input_to_forget_weights_ledger, - const TfLiteEvalTensor* input_to_cell_weights, - const TfLiteEvalTensor* input_to_cell_weights_ledger, - const TfLiteEvalTensor* input_to_output_weights, - const TfLiteEvalTensor* input_to_output_weights_ledger, - const TfLiteEvalTensor* recurrent_to_input_weights, - const TfLiteEvalTensor* recurrent_to_input_weights_ledger, - const TfLiteEvalTensor* recurrent_to_forget_weights, - const TfLiteEvalTensor* recurrent_to_forget_weights_ledger, - const TfLiteEvalTensor* recurrent_to_cell_weights, - const TfLiteEvalTensor* recurrent_to_cell_weights_ledger, - const TfLiteEvalTensor* recurrent_to_output_weights, - const TfLiteEvalTensor* recurrent_to_output_weights_ledger, - const TfLiteEvalTensor* cell_to_input_weights, - const TfLiteEvalTensor* cell_to_forget_weights, - const TfLiteEvalTensor* cell_to_output_weights, - const TfLiteEvalTensor* input_layer_norm_coefficients, - const TfLiteEvalTensor* forget_layer_norm_coefficients, - const TfLiteEvalTensor* cell_layer_norm_coefficients, - const TfLiteEvalTensor* output_layer_norm_coefficients, - const TfLiteEvalTensor* aux_input, - const TfLiteEvalTensor* aux_input_to_input_weights, - const TfLiteEvalTensor* aux_input_to_forget_weights, - const TfLiteEvalTensor* aux_input_to_cell_weights, - const TfLiteEvalTensor* aux_input_to_output_weights, - const TfLiteEvalTensor* input_gate_bias, - const TfLiteEvalTensor* forget_gate_bias, - const TfLiteEvalTensor* cell_gate_bias, - const TfLiteEvalTensor* output_gate_bias, - const TfLiteEvalTensor* projection_weights, - const TfLiteEvalTensor* projection_weights_ledger, - const TfLiteEvalTensor* projection_bias, const TfLiteLSTMParams* params, - bool forward_sequence, bool time_major, int output_offset, - float* scratch_buffer, float* input_sf, float* aux_input_sf, - float* output_state_sf, float* prod_scaling_factors, - float* recovered_cell_weights, int8_t* input_quantized, - int8_t* aux_input_quantized, int8_t* output_state_quantized, - int8_t* cell_state_quantized, float* scales, TfLiteEvalTensor* output_state, - TfLiteEvalTensor* cell_state, int32_t* output_scratch_buffer, - TfLiteEvalTensor* output, int32_t* input_zp, int32_t* aux_input_zp, - int32_t* output_state_zp, int32_t* row_sums, int row_sums_size, - bool* compute_row_sums) { - TFLITE_DCHECK(input->dims->size >= 2 && input->dims->size <= 3); - const int n_input = input->dims->data[input->dims->size - 1]; - int max_time, n_batch; - if (input->dims->size == 2) { - max_time = 1; - n_batch = input->dims->data[0]; - } else { - max_time = (time_major) ? input->dims->data[0] : input->dims->data[1]; - n_batch = (time_major) ? input->dims->data[1] : input->dims->data[0]; - } - const int aux_input_size = - (aux_input) ? aux_input->dims->data[aux_input->dims->size - 1] : 0; - // n_cell and n_output will be the same size when there is no projection. - const int n_cell = input_to_output_weights->dims->data[0]; - const int n_output = recurrent_to_output_weights->dims->data[1]; - - // Since we have already checked that weights are all there or none, we can - // check the existence of only one to get the condition. - const bool use_cifg = (input_to_input_weights == nullptr); - - float* input_gate_scratch = nullptr; - float* cell_gate_scratch = nullptr; - float* forget_gate_scratch = nullptr; - float* output_gate_scratch = nullptr; - if (use_cifg) { - cell_gate_scratch = scratch_buffer; - forget_gate_scratch = scratch_buffer + n_cell * n_batch; - output_gate_scratch = scratch_buffer + 2 * n_cell * n_batch; - } else { - input_gate_scratch = scratch_buffer; - cell_gate_scratch = scratch_buffer + n_cell * n_batch; - forget_gate_scratch = scratch_buffer + 2 * n_cell * n_batch; - output_gate_scratch = scratch_buffer + 3 * n_cell * n_batch; - } - - const int output_batch_leading_dim = - output->dims->data[output->dims->size - 1]; - - int32_t* input_zp_ptr = nullptr; - int32_t* aux_input_zp_ptr = nullptr; - int32_t* output_state_zp_ptr = nullptr; - int32_t* row_sums_ptr = nullptr; - if (params->asymmetric_quantize_inputs) { - input_zp_ptr = input_zp; - aux_input_zp_ptr = aux_input_zp; - output_state_zp_ptr = output_state_zp; - row_sums_ptr = row_sums; - } - - if (time_major) { - // Feed the sequence into the LSTM step-by-step. - const int input_step = n_batch * n_input; - const int output_step = n_batch * output_batch_leading_dim; - for (int t = 0; t < max_time; t++) { - // If this is the forward_sequence, step forward, otherwise step - // backwards. - const int t_rel = forward_sequence ? t : max_time - t - 1; - const float* input_ptr = - tflite::micro::GetTensorData(input) + t_rel * input_step; - const float* aux_input_ptr = nullptr; - if (aux_input) { - aux_input_ptr = - tflite::micro::GetTensorData(aux_input) + t_rel * input_step; - } - float* output_ptr = tflite::micro::GetTensorData(output) + - t_rel * output_step + output_offset; - LstmStepHybrid( - input_ptr, - input_to_input_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_to_input_weights), - input_to_input_weights_ledger == nullptr - ? nullptr - : tflite::micro::GetTensorData( - input_to_input_weights_ledger), - hybrid_lstm_scales->input_to_input_weights_scale, - input_to_forget_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_to_forget_weights), - input_to_forget_weights_ledger == nullptr - ? nullptr - : tflite::micro::GetTensorData( - input_to_forget_weights_ledger), - hybrid_lstm_scales->input_to_forget_weights_scale, - input_to_cell_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_to_cell_weights), - input_to_cell_weights_ledger == nullptr - ? nullptr - : tflite::micro::GetTensorData( - input_to_cell_weights_ledger), - hybrid_lstm_scales->input_to_cell_weights_scale, - input_to_output_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_to_output_weights), - input_to_output_weights_ledger == nullptr - ? nullptr - : tflite::micro::GetTensorData( - input_to_output_weights_ledger), - hybrid_lstm_scales->input_to_output_weights_scale, aux_input_ptr, - aux_input_to_input_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - aux_input_to_input_weights), - hybrid_lstm_scales->aux_input_to_input_weights_scale, - aux_input_to_forget_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - aux_input_to_forget_weights), - hybrid_lstm_scales->aux_input_to_forget_weights_scale, - aux_input_to_cell_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(aux_input_to_cell_weights), - hybrid_lstm_scales->aux_input_to_cell_weights_scale, - aux_input_to_output_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - aux_input_to_output_weights), - hybrid_lstm_scales->aux_input_to_output_weights_scale, - recurrent_to_input_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - recurrent_to_input_weights), - recurrent_to_input_weights_ledger == nullptr - ? nullptr - : tflite::micro::GetTensorData( - recurrent_to_input_weights_ledger), - hybrid_lstm_scales->recurrent_to_input_weights_scale, - recurrent_to_forget_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - recurrent_to_forget_weights), - recurrent_to_forget_weights_ledger == nullptr - ? nullptr - : tflite::micro::GetTensorData( - recurrent_to_forget_weights_ledger), - hybrid_lstm_scales->recurrent_to_forget_weights_scale, - recurrent_to_cell_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(recurrent_to_cell_weights), - recurrent_to_cell_weights_ledger == nullptr - ? nullptr - : tflite::micro::GetTensorData( - recurrent_to_cell_weights_ledger), - hybrid_lstm_scales->recurrent_to_cell_weights_scale, - recurrent_to_output_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - recurrent_to_output_weights), - recurrent_to_output_weights_ledger == nullptr - ? nullptr - : tflite::micro::GetTensorData( - recurrent_to_output_weights_ledger), - hybrid_lstm_scales->recurrent_to_output_weights_scale, - cell_to_input_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(cell_to_input_weights), - hybrid_lstm_scales->cell_to_input_weights_scale, - cell_to_forget_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(cell_to_forget_weights), - hybrid_lstm_scales->cell_to_forget_weights_scale, - cell_to_output_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(cell_to_output_weights), - hybrid_lstm_scales->cell_to_output_weights_scale, - input_layer_norm_coefficients == nullptr - ? nullptr - : tflite::micro::GetTensorData( - input_layer_norm_coefficients), - forget_layer_norm_coefficients == nullptr - ? nullptr - : tflite::micro::GetTensorData( - forget_layer_norm_coefficients), - cell_layer_norm_coefficients == nullptr - ? nullptr - : tflite::micro::GetTensorData( - cell_layer_norm_coefficients), - output_layer_norm_coefficients == nullptr - ? nullptr - : tflite::micro::GetTensorData( - output_layer_norm_coefficients), - input_gate_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_gate_bias), - forget_gate_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(forget_gate_bias), - cell_gate_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(cell_gate_bias), - output_gate_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(output_gate_bias), - projection_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(projection_weights), - projection_weights_ledger == nullptr - ? nullptr - : tflite::micro::GetTensorData( - projection_weights_ledger), - hybrid_lstm_scales->projection_weights_scale, - projection_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(projection_bias), - params, n_batch, n_cell, n_input, aux_input_size, n_output, - output_batch_leading_dim, input_gate_scratch, forget_gate_scratch, - cell_gate_scratch, output_gate_scratch, scales, input_sf, - aux_input_sf, output_state_sf, prod_scaling_factors, - recovered_cell_weights, input_quantized, aux_input_quantized, - output_state_quantized, cell_state_quantized, - tflite::micro::GetTensorData(output_state), - tflite::micro::GetTensorData(cell_state), - output_scratch_buffer, output_ptr, input_zp_ptr, aux_input_zp_ptr, - output_state_zp_ptr, row_sums_ptr, row_sums_size, compute_row_sums, - params->asymmetric_quantize_inputs); - } - } else { - for (int b = 0; b < n_batch; b++) { - const int input_step = n_input; - const int output_step = output_batch_leading_dim; - for (int t = 0; t < max_time; t++) { - // If this is the forward_sequence, step forward, otherwise step - // backwards. - const int t_rel = forward_sequence ? t : max_time - t - 1; - const int time_offset = b * max_time + t_rel; - const float* input_ptr = tflite::micro::GetTensorData(input) + - time_offset * input_step; - const float* aux_input_ptr = nullptr; - if (aux_input) { - aux_input_ptr = tflite::micro::GetTensorData(aux_input) + - time_offset * input_step; - } - float* output_ptr = tflite::micro::GetTensorData(output) + - time_offset * output_step + output_offset; - - // Offset the {output,cell}_state pointers to the right batch. - float* output_state_ptr = - tflite::micro::GetTensorData(output_state) + - b * output_batch_leading_dim; - float* cell_state_ptr = - tflite::micro::GetTensorData(cell_state) + b * n_cell; - // Offset the scratch pointers to the right batch. - float* input_gate_scratch_ptr = - input_gate_scratch ? input_gate_scratch + b * n_cell : nullptr; - float* forget_gate_scratch_ptr = forget_gate_scratch + b * n_cell; - float* cell_gate_scratch_ptr = cell_gate_scratch + b * n_cell; - float* output_gate_scratch_ptr = output_gate_scratch + b * n_cell; - - LstmStepHybrid( - input_ptr, - input_to_input_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_to_input_weights), - input_to_input_weights_ledger == nullptr - ? nullptr - : tflite::micro::GetTensorData( - input_to_input_weights_ledger), - hybrid_lstm_scales->input_to_input_weights_scale, - input_to_forget_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_to_forget_weights), - input_to_forget_weights_ledger == nullptr - ? nullptr - : tflite::micro::GetTensorData( - input_to_forget_weights_ledger), - hybrid_lstm_scales->input_to_forget_weights_scale, - input_to_cell_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_to_cell_weights), - input_to_cell_weights_ledger == nullptr - ? nullptr - : tflite::micro::GetTensorData( - input_to_cell_weights_ledger), - hybrid_lstm_scales->input_to_cell_weights_scale, - input_to_output_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_to_output_weights), - input_to_output_weights_ledger == nullptr - ? nullptr - : tflite::micro::GetTensorData( - input_to_output_weights_ledger), - hybrid_lstm_scales->input_to_output_weights_scale, aux_input_ptr, - aux_input_to_input_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - aux_input_to_input_weights), - hybrid_lstm_scales->aux_input_to_input_weights_scale, - aux_input_to_forget_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - aux_input_to_forget_weights), - hybrid_lstm_scales->aux_input_to_forget_weights_scale, - aux_input_to_cell_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - aux_input_to_cell_weights), - hybrid_lstm_scales->aux_input_to_cell_weights_scale, - aux_input_to_output_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - aux_input_to_output_weights), - hybrid_lstm_scales->aux_input_to_output_weights_scale, - recurrent_to_input_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - recurrent_to_input_weights), - recurrent_to_input_weights_ledger == nullptr - ? nullptr - : tflite::micro::GetTensorData( - recurrent_to_input_weights_ledger), - hybrid_lstm_scales->recurrent_to_input_weights_scale, - recurrent_to_forget_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - recurrent_to_forget_weights), - recurrent_to_forget_weights_ledger == nullptr - ? nullptr - : tflite::micro::GetTensorData( - recurrent_to_forget_weights_ledger), - hybrid_lstm_scales->recurrent_to_forget_weights_scale, - recurrent_to_cell_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - recurrent_to_cell_weights), - recurrent_to_cell_weights_ledger == nullptr - ? nullptr - : tflite::micro::GetTensorData( - recurrent_to_cell_weights_ledger), - hybrid_lstm_scales->recurrent_to_cell_weights_scale, - recurrent_to_output_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - recurrent_to_output_weights), - recurrent_to_output_weights_ledger == nullptr - ? nullptr - : tflite::micro::GetTensorData( - recurrent_to_output_weights_ledger), - hybrid_lstm_scales->recurrent_to_output_weights_scale, - cell_to_input_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(cell_to_input_weights), - hybrid_lstm_scales->cell_to_input_weights_scale, - cell_to_forget_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(cell_to_forget_weights), - hybrid_lstm_scales->cell_to_forget_weights_scale, - cell_to_output_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(cell_to_output_weights), - hybrid_lstm_scales->cell_to_output_weights_scale, - input_layer_norm_coefficients == nullptr - ? nullptr - : tflite::micro::GetTensorData( - input_layer_norm_coefficients), - forget_layer_norm_coefficients == nullptr - ? nullptr - : tflite::micro::GetTensorData( - forget_layer_norm_coefficients), - cell_layer_norm_coefficients == nullptr - ? nullptr - : tflite::micro::GetTensorData( - cell_layer_norm_coefficients), - output_layer_norm_coefficients == nullptr - ? nullptr - : tflite::micro::GetTensorData( - output_layer_norm_coefficients), - input_gate_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_gate_bias), - forget_gate_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(forget_gate_bias), - cell_gate_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(cell_gate_bias), - output_gate_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(output_gate_bias), - projection_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(projection_weights), - projection_weights_ledger == nullptr - ? nullptr - : tflite::micro::GetTensorData( - projection_weights_ledger), - hybrid_lstm_scales->projection_weights_scale, - projection_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(projection_bias), - params, - /*n_batch=*/1, n_cell, n_input, aux_input_size, n_output, - output_batch_leading_dim, input_gate_scratch_ptr, - forget_gate_scratch_ptr, cell_gate_scratch_ptr, - output_gate_scratch_ptr, scales, input_sf, aux_input_sf, - output_state_sf, prod_scaling_factors, recovered_cell_weights, - input_quantized, aux_input_quantized, output_state_quantized, - cell_state_quantized, output_state_ptr, cell_state_ptr, - output_scratch_buffer, output_ptr, input_zp_ptr, aux_input_zp_ptr, - output_state_zp_ptr, row_sums_ptr, row_sums_size, compute_row_sums, - params->asymmetric_quantize_inputs); - } - } - } - - return kTfLiteOk; -} - -TfLiteStatus EvalInteger8x8_16Lstm( - const TfLiteEvalTensor* input, - const TfLiteEvalTensor* input_to_input_weights, - const TfLiteEvalTensor* input_to_forget_weights, - const TfLiteEvalTensor* input_to_cell_weights, - const TfLiteEvalTensor* input_to_output_weights, - const TfLiteEvalTensor* recurrent_to_input_weights, - const TfLiteEvalTensor* recurrent_to_forget_weights, - const TfLiteEvalTensor* recurrent_to_cell_weights, - const TfLiteEvalTensor* recurrent_to_output_weights, - const TfLiteEvalTensor* cell_to_input_weights, - const TfLiteEvalTensor* cell_to_forget_weights, - const TfLiteEvalTensor* cell_to_output_weights, - const TfLiteEvalTensor* input_layer_norm_coefficients, - const TfLiteEvalTensor* forget_layer_norm_coefficients, - const TfLiteEvalTensor* cell_layer_norm_coefficients, - const TfLiteEvalTensor* output_layer_norm_coefficients, - const TfLiteEvalTensor* input_gate_bias, - const TfLiteEvalTensor* forget_gate_bias, - const TfLiteEvalTensor* cell_gate_bias, - const TfLiteEvalTensor* output_gate_bias, - const TfLiteEvalTensor* projection_weights, - const TfLiteEvalTensor* projection_bias, const TfLiteLSTMParams* params, - bool forward_sequence, bool time_major, - const IntegerLstmParameter* integer_lstm_param, int32_t output_state_zp, - TfLiteEvalTensor* output_state, TfLiteEvalTensor* cell_state, - TfLiteEvalTensor* output, int16_t* scratch0, int16_t* scratch1, - int16_t* scratch2, int16_t* scratch3, int8_t* scratch4, int32_t* scratch5) { - TFLITE_DCHECK(input->dims->size >= 2 && input->dims->size <= 3); - const int n_input = input->dims->data[input->dims->size - 1]; - int max_time, n_batch; - if (input->dims->size == 2) { - max_time = 1; - n_batch = input->dims->data[0]; - } else { - max_time = (time_major) ? input->dims->data[0] : input->dims->data[1]; - n_batch = (time_major) ? input->dims->data[1] : input->dims->data[0]; - } - - // n_cell and n_output will be the same size when there is no projection. - const int n_cell = input_to_output_weights->dims->data[0]; - const int n_output = recurrent_to_output_weights->dims->data[1]; - - // Get params for time/batch/sequence. - const int output_batch_leading_dim = - output->dims->data[output->dims->size - 1]; - - if (time_major) { - const int input_step = n_batch * n_input; - const int output_step = n_batch * output_batch_leading_dim; - for (int t = 0; t < max_time; t++) { - const int t_rel = t; - int8_t* output_ptr = - tflite::micro::GetTensorData(output) + t_rel * output_step; - const int8_t* input_ptr = - tflite::micro::GetTensorData(input) + t_rel * input_step; - LstmStepInteger8x8_16( - input_ptr, - input_to_input_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_to_input_weights), - integer_lstm_param->effective_input_to_input_scale_a, - integer_lstm_param->effective_input_to_input_scale_b, - input_to_forget_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_to_forget_weights), - integer_lstm_param->effective_input_to_forget_scale_a, - integer_lstm_param->effective_input_to_forget_scale_b, - input_to_cell_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_to_cell_weights), - integer_lstm_param->effective_input_to_cell_scale_a, - integer_lstm_param->effective_input_to_cell_scale_b, - input_to_output_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_to_output_weights), - integer_lstm_param->effective_input_to_output_scale_a, - integer_lstm_param->effective_input_to_output_scale_b, - recurrent_to_input_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - recurrent_to_input_weights), - integer_lstm_param->effective_recurrent_to_input_scale_a, - integer_lstm_param->effective_recurrent_to_input_scale_b, - recurrent_to_forget_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - recurrent_to_forget_weights), - integer_lstm_param->effective_recurrent_to_forget_scale_a, - integer_lstm_param->effective_recurrent_to_forget_scale_b, - recurrent_to_cell_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(recurrent_to_cell_weights), - integer_lstm_param->effective_recurrent_to_cell_scale_a, - integer_lstm_param->effective_recurrent_to_cell_scale_b, - recurrent_to_output_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - recurrent_to_output_weights), - integer_lstm_param->effective_recurrent_to_output_scale_a, - integer_lstm_param->effective_recurrent_to_output_scale_b, - cell_to_input_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(cell_to_input_weights), - integer_lstm_param->effective_cell_to_input_scale_a, - integer_lstm_param->effective_cell_to_input_scale_b, - cell_to_forget_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(cell_to_forget_weights), - integer_lstm_param->effective_cell_to_forget_scale_a, - integer_lstm_param->effective_cell_to_forget_scale_b, - cell_to_output_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(cell_to_output_weights), - integer_lstm_param->effective_cell_to_output_scale_a, - integer_lstm_param->effective_cell_to_output_scale_b, - projection_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(projection_weights), - integer_lstm_param->effective_proj_scale_a, - integer_lstm_param->effective_proj_scale_b, - integer_lstm_param->hidden_zp, - integer_lstm_param->effective_hidden_scale_a, - integer_lstm_param->effective_hidden_scale_b, - input_layer_norm_coefficients == nullptr - ? nullptr - : tflite::micro::GetTensorData( - input_layer_norm_coefficients), - integer_lstm_param->layer_norm_input_scale_a, - integer_lstm_param->layer_norm_input_scale_b, - forget_layer_norm_coefficients == nullptr - ? nullptr - : tflite::micro::GetTensorData( - forget_layer_norm_coefficients), - integer_lstm_param->layer_norm_forget_scale_a, - integer_lstm_param->layer_norm_forget_scale_b, - cell_layer_norm_coefficients == nullptr - ? nullptr - : tflite::micro::GetTensorData( - cell_layer_norm_coefficients), - integer_lstm_param->layer_norm_cell_scale_a, - integer_lstm_param->layer_norm_cell_scale_b, - output_layer_norm_coefficients == nullptr - ? nullptr - : tflite::micro::GetTensorData( - output_layer_norm_coefficients), - integer_lstm_param->layer_norm_output_scale_a, - integer_lstm_param->layer_norm_output_scale_b, - input_gate_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_gate_bias), - forget_gate_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(forget_gate_bias), - cell_gate_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(cell_gate_bias), - output_gate_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(output_gate_bias), - integer_lstm_param->quantized_cell_clip, - integer_lstm_param->quantized_proj_clip, - integer_lstm_param->cell_scale, - integer_lstm_param->input_variance_guard, - integer_lstm_param->forget_variance_guard, - integer_lstm_param->cell_variance_guard, - integer_lstm_param->output_variance_guard, - integer_lstm_param->input_to_forget_effective_bias, - integer_lstm_param->recurrent_to_forget_effective_bias, - integer_lstm_param->input_to_cell_effective_bias, - integer_lstm_param->recurrent_to_cell_effective_bias, - integer_lstm_param->input_to_output_effective_bias, - integer_lstm_param->recurrent_to_output_effective_bias, - integer_lstm_param->input_to_input_effective_bias, - integer_lstm_param->recurrent_to_input_effective_bias, - integer_lstm_param->projection_effective_bias, n_batch, n_cell, - n_input, n_output, tflite::micro::GetTensorData(output_state), - output_state_zp, tflite::micro::GetTensorData(cell_state), - output_ptr, scratch0, scratch1, scratch2, scratch3, scratch4, - scratch5); - } - } else { - for (int b = 0; b < n_batch; b++) { - const int input_step = n_input; - const int output_step = output_batch_leading_dim; - for (int t = 0; t < max_time; t++) { - // If this is the forward_sequence, step forward, otherwise step - // backwards. - const int t_rel = forward_sequence ? t : max_time - t - 1; - const int time_offset = b * max_time + t_rel; - const int8_t* input_ptr = tflite::micro::GetTensorData(input) + - time_offset * input_step; - int8_t* output_ptr = tflite::micro::GetTensorData(output) + - time_offset * output_step; - - // Offset the {output,cell}_state pointers to the right batch. - int8_t* output_state_ptr = - tflite::micro::GetTensorData(output_state) + - b * output_batch_leading_dim; - int16_t* cell_state_ptr = - tflite::micro::GetTensorData(cell_state) + b * n_cell; - - LstmStepInteger8x8_16( - input_ptr, - input_to_input_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_to_input_weights), - integer_lstm_param->effective_input_to_input_scale_a, - integer_lstm_param->effective_input_to_input_scale_b, - input_to_forget_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_to_forget_weights), - integer_lstm_param->effective_input_to_forget_scale_a, - integer_lstm_param->effective_input_to_forget_scale_b, - input_to_cell_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_to_cell_weights), - integer_lstm_param->effective_input_to_cell_scale_a, - integer_lstm_param->effective_input_to_cell_scale_b, - input_to_output_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_to_output_weights), - integer_lstm_param->effective_input_to_output_scale_a, - integer_lstm_param->effective_input_to_output_scale_b, - recurrent_to_input_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - recurrent_to_input_weights), - integer_lstm_param->effective_recurrent_to_input_scale_a, - integer_lstm_param->effective_recurrent_to_input_scale_b, - recurrent_to_forget_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - recurrent_to_forget_weights), - integer_lstm_param->effective_recurrent_to_forget_scale_a, - integer_lstm_param->effective_recurrent_to_forget_scale_b, - recurrent_to_cell_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - recurrent_to_cell_weights), - integer_lstm_param->effective_recurrent_to_cell_scale_a, - integer_lstm_param->effective_recurrent_to_cell_scale_b, - recurrent_to_output_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData( - recurrent_to_output_weights), - integer_lstm_param->effective_recurrent_to_output_scale_a, - integer_lstm_param->effective_recurrent_to_output_scale_b, - cell_to_input_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(cell_to_input_weights), - integer_lstm_param->effective_cell_to_input_scale_a, - integer_lstm_param->effective_cell_to_input_scale_b, - cell_to_forget_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(cell_to_forget_weights), - integer_lstm_param->effective_cell_to_forget_scale_a, - integer_lstm_param->effective_cell_to_forget_scale_b, - cell_to_output_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(cell_to_output_weights), - integer_lstm_param->effective_cell_to_output_scale_a, - integer_lstm_param->effective_cell_to_output_scale_b, - projection_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(projection_weights), - integer_lstm_param->effective_proj_scale_a, - integer_lstm_param->effective_proj_scale_b, - integer_lstm_param->hidden_zp, - integer_lstm_param->effective_hidden_scale_a, - integer_lstm_param->effective_hidden_scale_b, - input_layer_norm_coefficients == nullptr - ? nullptr - : tflite::micro::GetTensorData( - input_layer_norm_coefficients), - integer_lstm_param->layer_norm_input_scale_a, - integer_lstm_param->layer_norm_input_scale_b, - forget_layer_norm_coefficients == nullptr - ? nullptr - : tflite::micro::GetTensorData( - forget_layer_norm_coefficients), - integer_lstm_param->layer_norm_forget_scale_a, - integer_lstm_param->layer_norm_forget_scale_b, - cell_layer_norm_coefficients == nullptr - ? nullptr - : tflite::micro::GetTensorData( - cell_layer_norm_coefficients), - integer_lstm_param->layer_norm_cell_scale_a, - integer_lstm_param->layer_norm_cell_scale_b, - output_layer_norm_coefficients == nullptr - ? nullptr - : tflite::micro::GetTensorData( - output_layer_norm_coefficients), - integer_lstm_param->layer_norm_output_scale_a, - integer_lstm_param->layer_norm_output_scale_b, - input_gate_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_gate_bias), - forget_gate_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(forget_gate_bias), - cell_gate_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(cell_gate_bias), - output_gate_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(output_gate_bias), - integer_lstm_param->quantized_cell_clip, - integer_lstm_param->quantized_proj_clip, - integer_lstm_param->cell_scale, - integer_lstm_param->input_variance_guard, - integer_lstm_param->forget_variance_guard, - integer_lstm_param->cell_variance_guard, - integer_lstm_param->output_variance_guard, - integer_lstm_param->input_to_forget_effective_bias, - integer_lstm_param->recurrent_to_forget_effective_bias, - integer_lstm_param->input_to_cell_effective_bias, - integer_lstm_param->recurrent_to_cell_effective_bias, - integer_lstm_param->input_to_output_effective_bias, - integer_lstm_param->recurrent_to_output_effective_bias, - integer_lstm_param->input_to_input_effective_bias, - integer_lstm_param->recurrent_to_input_effective_bias, - integer_lstm_param->projection_effective_bias, /*n_batch=*/1, - n_cell, n_input, n_output, output_state_ptr, output_state_zp, - cell_state_ptr, output_ptr, scratch0, scratch1, scratch2, scratch3, - scratch4, scratch5); - } - } - } - - return kTfLiteOk; -} - -TfLiteStatus EvalInteger8x8_8Lstm( - const TfLiteEvalTensor* input, - const TfLiteEvalTensor* input_to_input_weights, - const TfLiteEvalTensor* input_to_forget_weights, - const TfLiteEvalTensor* input_to_cell_weights, - const TfLiteEvalTensor* input_to_output_weights, - const TfLiteEvalTensor* recurrent_to_input_weights, - const TfLiteEvalTensor* recurrent_to_forget_weights, - const TfLiteEvalTensor* recurrent_to_cell_weights, - const TfLiteEvalTensor* recurrent_to_output_weights, - const TfLiteEvalTensor* cell_to_input_weights, - const TfLiteEvalTensor* cell_to_forget_weights, - const TfLiteEvalTensor* cell_to_output_weights, - const TfLiteEvalTensor* input_layer_norm_coefficients, - const TfLiteEvalTensor* forget_layer_norm_coefficients, - const TfLiteEvalTensor* cell_layer_norm_coefficients, - const TfLiteEvalTensor* output_layer_norm_coefficients, - const TfLiteEvalTensor* input_gate_bias, - const TfLiteEvalTensor* forget_gate_bias, - const TfLiteEvalTensor* cell_gate_bias, - const TfLiteEvalTensor* output_gate_bias, - const TfLiteEvalTensor* projection_weights, - const TfLiteEvalTensor* projection_bias, const TfLiteLSTMParams* params, - TfLiteEvalTensor* output_state, TfLiteEvalTensor* cell_state, - TfLiteEvalTensor* output, const IntegerLstmParameter* integer_lstm_param, - int32_t input_zp, int32_t output_state_zp, int8_t* scratch0, - int8_t* scratch1, int16_t* scratch2, int16_t* scratch3, int16_t* scratch4, - int16_t* scratch5, int16_t* scratch6, int16_t* scratch7) { - TFLITE_DCHECK(input->dims->size >= 2 && input->dims->size <= 3); - const int n_input = input->dims->data[input->dims->size - 1]; - int max_time, n_batch; - if (input->dims->size == 2) { - max_time = 1; - n_batch = input->dims->data[0]; - } else { - max_time = input->dims->data[0]; - n_batch = input->dims->data[1]; - } - - // n_cell and n_output will be the same size when there is no projection. - const int n_cell = input_to_output_weights->dims->data[0]; - const int n_output = recurrent_to_output_weights->dims->data[1]; - - // Get params for time/batch/sequence. - const int output_batch_leading_dim = - output->dims->data[output->dims->size - 1]; - const int input_step = n_batch * n_input; - const int output_step = n_batch * output_batch_leading_dim; - - for (int t = 0; t < max_time; t++) { - const int t_rel = t; - int8_t* output_ptr = - tflite::micro::GetTensorData(output) + t_rel * output_step; - // Input can be int8 asymmetric or int16 symmetric. - const int8_t* input_ptr = - tflite::micro::GetTensorData(input) + t_rel * input_step; - LstmStepInteger8x8_8( - input_ptr, input_zp, - - input_to_input_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_to_input_weights), - integer_lstm_param->effective_input_to_input_scale_a, - integer_lstm_param->effective_input_to_input_scale_b, - - input_to_forget_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_to_forget_weights), - integer_lstm_param->effective_input_to_forget_scale_a, - integer_lstm_param->effective_input_to_forget_scale_b, - - input_to_cell_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_to_cell_weights), - integer_lstm_param->effective_input_to_cell_scale_a, - integer_lstm_param->effective_input_to_cell_scale_b, - - input_to_output_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_to_output_weights), - integer_lstm_param->effective_input_to_output_scale_a, - integer_lstm_param->effective_input_to_output_scale_b, - - recurrent_to_input_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(recurrent_to_input_weights), - integer_lstm_param->effective_recurrent_to_input_scale_a, - integer_lstm_param->effective_recurrent_to_input_scale_b, - - recurrent_to_forget_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(recurrent_to_forget_weights), - integer_lstm_param->effective_recurrent_to_forget_scale_a, - integer_lstm_param->effective_recurrent_to_forget_scale_b, - - recurrent_to_cell_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(recurrent_to_cell_weights), - integer_lstm_param->effective_recurrent_to_cell_scale_a, - integer_lstm_param->effective_recurrent_to_cell_scale_b, - - recurrent_to_output_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(recurrent_to_output_weights), - integer_lstm_param->effective_recurrent_to_output_scale_a, - integer_lstm_param->effective_recurrent_to_output_scale_b, - - cell_to_input_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(cell_to_input_weights), - integer_lstm_param->effective_cell_to_input_scale_a, - integer_lstm_param->effective_cell_to_input_scale_b, - - cell_to_forget_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(cell_to_forget_weights), - integer_lstm_param->effective_cell_to_forget_scale_a, - integer_lstm_param->effective_cell_to_forget_scale_b, - - cell_to_output_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(cell_to_output_weights), - integer_lstm_param->effective_cell_to_output_scale_a, - integer_lstm_param->effective_cell_to_output_scale_b, - - projection_weights == nullptr - ? nullptr - : tflite::micro::GetTensorData(projection_weights), - integer_lstm_param->effective_proj_scale_a, - integer_lstm_param->effective_proj_scale_b, - - input_layer_norm_coefficients == nullptr - ? nullptr - : tflite::micro::GetTensorData( - input_layer_norm_coefficients), - integer_lstm_param->layer_norm_input_scale_a, - integer_lstm_param->layer_norm_input_scale_b, - - forget_layer_norm_coefficients == nullptr - ? nullptr - : tflite::micro::GetTensorData( - forget_layer_norm_coefficients), - integer_lstm_param->layer_norm_forget_scale_a, - integer_lstm_param->layer_norm_forget_scale_b, - - cell_layer_norm_coefficients == nullptr - ? nullptr - : tflite::micro::GetTensorData( - cell_layer_norm_coefficients), - integer_lstm_param->layer_norm_cell_scale_a, - integer_lstm_param->layer_norm_cell_scale_b, - - output_layer_norm_coefficients == nullptr - ? nullptr - : tflite::micro::GetTensorData( - output_layer_norm_coefficients), - integer_lstm_param->layer_norm_output_scale_a, - integer_lstm_param->layer_norm_output_scale_b, - - input_gate_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(input_gate_bias), - forget_gate_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(forget_gate_bias), - cell_gate_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(cell_gate_bias), - output_gate_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(output_gate_bias), - projection_bias == nullptr - ? nullptr - : tflite::micro::GetTensorData(projection_bias), - - params, integer_lstm_param->intermediate_scale_a, - integer_lstm_param->intermediate_scale_b, - integer_lstm_param->intermediate_zp, - integer_lstm_param->quantized_cell_clip, - integer_lstm_param->quantized_proj_clip, n_batch, n_cell, n_input, - n_output, output_batch_leading_dim, - tflite::micro::GetTensorData(output_state), output_state_zp, - tflite::micro::GetTensorData(cell_state), output_ptr, scratch0, - scratch1, scratch2, scratch3, scratch4, scratch5, scratch6, scratch7); - } - - return kTfLiteOk; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/lstm_eval.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/lstm_eval.h deleted file mode 100644 index 218b4938..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/lstm_eval.h +++ /dev/null @@ -1,250 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_LSTM_EVAL_H_ -#define TENSORFLOW_LITE_MICRO_KERNELS_LSTM_EVAL_H_ - -#include -#include - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" - -namespace tflite { - -// Pamameters for integer LSTM. -// Consider split this into two Integer Parameters if more fields are added. -struct IntegerLstmParameter { - int32_t effective_input_to_input_scale_a; - int32_t effective_input_to_input_scale_b; - int32_t effective_recurrent_to_input_scale_a; - int32_t effective_recurrent_to_input_scale_b; - int32_t effective_cell_to_input_scale_a; - int32_t effective_cell_to_input_scale_b; - int32_t effective_input_to_forget_scale_a; - int32_t effective_input_to_forget_scale_b; - int32_t effective_recurrent_to_forget_scale_a; - int32_t effective_recurrent_to_forget_scale_b; - int32_t effective_cell_to_forget_scale_a; - int32_t effective_cell_to_forget_scale_b; - int32_t effective_input_to_cell_scale_a; - int32_t effective_input_to_cell_scale_b; - int32_t effective_recurrent_to_cell_scale_a; - int32_t effective_recurrent_to_cell_scale_b; - int32_t effective_input_to_output_scale_a; - int32_t effective_input_to_output_scale_b; - int32_t effective_recurrent_to_output_scale_a; - int32_t effective_recurrent_to_output_scale_b; - int32_t effective_cell_to_output_scale_a; - int32_t effective_cell_to_output_scale_b; - int32_t effective_proj_scale_a; - int32_t effective_proj_scale_b; - int32_t effective_hidden_scale_a; - int32_t effective_hidden_scale_b; - int32_t layer_norm_input_scale_a; - int32_t layer_norm_input_scale_b; - int32_t layer_norm_forget_scale_a; - int32_t layer_norm_forget_scale_b; - int32_t layer_norm_cell_scale_a; - int32_t layer_norm_cell_scale_b; - int32_t layer_norm_output_scale_a; - int32_t layer_norm_output_scale_b; - // Quantized clip value for cell and projection. Zero value means no clipping. - int16_t quantized_cell_clip; - int8_t quantized_proj_clip; - int32_t hidden_zp; - int32_t cell_scale; - - int32_t input_variance_guard; - int32_t forget_variance_guard; - int32_t cell_variance_guard; - int32_t output_variance_guard; - - // Pre-calculate bias + zero_point * weight. - int32_t* input_to_forget_effective_bias; - int32_t* recurrent_to_forget_effective_bias; - int32_t* input_to_cell_effective_bias; - int32_t* recurrent_to_cell_effective_bias; - int32_t* input_to_output_effective_bias; - int32_t* recurrent_to_output_effective_bias; - int32_t* input_to_input_effective_bias; - int32_t* recurrent_to_input_effective_bias; - int32_t* projection_effective_bias; - - // Scale and zero point for intermediate tensors. - // Used only in the 8x8_8 case. - int32_t intermediate_scale_a[8]; - int32_t intermediate_scale_b[8]; - int32_t intermediate_zp[12]; -}; - -// Scales for hybrid op with integer inputs and float weights -struct HybridLstmScales { - float input_to_input_weights_scale; - float input_to_forget_weights_scale; - float input_to_cell_weights_scale; - float input_to_output_weights_scale; - float aux_input_to_input_weights_scale; - float aux_input_to_forget_weights_scale; - float aux_input_to_cell_weights_scale; - float aux_input_to_output_weights_scale; - float recurrent_to_input_weights_scale; - float recurrent_to_forget_weights_scale; - float recurrent_to_cell_weights_scale; - float recurrent_to_output_weights_scale; - float cell_to_input_weights_scale; - float cell_to_forget_weights_scale; - float cell_to_output_weights_scale; - float projection_weights_scale; -}; - -TfLiteStatus EvalFloatLstm( - const TfLiteEvalTensor* input, - const TfLiteEvalTensor* input_to_input_weights, - const TfLiteEvalTensor* input_to_forget_weights, - const TfLiteEvalTensor* input_to_cell_weights, - const TfLiteEvalTensor* input_to_output_weights, - const TfLiteEvalTensor* recurrent_to_input_weights, - const TfLiteEvalTensor* recurrent_to_forget_weights, - const TfLiteEvalTensor* recurrent_to_cell_weights, - const TfLiteEvalTensor* recurrent_to_output_weights, - const TfLiteEvalTensor* cell_to_input_weights, - const TfLiteEvalTensor* cell_to_forget_weights, - const TfLiteEvalTensor* cell_to_output_weights, - const TfLiteEvalTensor* input_layer_norm_coefficients, - const TfLiteEvalTensor* forget_layer_norm_coefficients, - const TfLiteEvalTensor* cell_layer_norm_coefficients, - const TfLiteEvalTensor* output_layer_norm_coefficients, - const TfLiteEvalTensor* aux_input, - const TfLiteEvalTensor* aux_input_to_input_weights, - const TfLiteEvalTensor* aux_input_to_forget_weights, - const TfLiteEvalTensor* aux_input_to_cell_weights, - const TfLiteEvalTensor* aux_input_to_output_weights, - const TfLiteEvalTensor* input_gate_bias, - const TfLiteEvalTensor* forget_gate_bias, - const TfLiteEvalTensor* cell_gate_bias, - const TfLiteEvalTensor* output_gate_bias, - const TfLiteEvalTensor* projection_weights, - const TfLiteEvalTensor* projection_bias, const TfLiteLSTMParams* params, - bool forward_sequence, bool time_major, int output_offset, - float* scratch_buffer, TfLiteEvalTensor* output_state, - TfLiteEvalTensor* cell_state, TfLiteEvalTensor* output); - -TfLiteStatus EvalHybridLstm( - const HybridLstmScales* hybrid_lstm_scales, const TfLiteEvalTensor* input, - const TfLiteEvalTensor* input_to_input_weights, - const TfLiteEvalTensor* input_to_input_weights_ledger, - const TfLiteEvalTensor* input_to_forget_weights, - const TfLiteEvalTensor* input_to_forget_weights_ledger, - const TfLiteEvalTensor* input_to_cell_weights, - const TfLiteEvalTensor* input_to_cell_weights_ledger, - const TfLiteEvalTensor* input_to_output_weights, - const TfLiteEvalTensor* input_to_output_weights_ledger, - const TfLiteEvalTensor* recurrent_to_input_weights, - const TfLiteEvalTensor* recurrent_to_input_weights_ledger, - const TfLiteEvalTensor* recurrent_to_forget_weights, - const TfLiteEvalTensor* recurrent_to_forget_weights_ledger, - const TfLiteEvalTensor* recurrent_to_cell_weights, - const TfLiteEvalTensor* recurrent_to_cell_weights_ledger, - const TfLiteEvalTensor* recurrent_to_output_weights, - const TfLiteEvalTensor* recurrent_to_output_weights_ledger, - const TfLiteEvalTensor* cell_to_input_weights, - const TfLiteEvalTensor* cell_to_forget_weights, - const TfLiteEvalTensor* cell_to_output_weights, - const TfLiteEvalTensor* input_layer_norm_coefficients, - const TfLiteEvalTensor* forget_layer_norm_coefficients, - const TfLiteEvalTensor* cell_layer_norm_coefficients, - const TfLiteEvalTensor* output_layer_norm_coefficients, - const TfLiteEvalTensor* aux_input, - const TfLiteEvalTensor* aux_input_to_input_weights, - const TfLiteEvalTensor* aux_input_to_forget_weights, - const TfLiteEvalTensor* aux_input_to_cell_weights, - const TfLiteEvalTensor* aux_input_to_output_weights, - const TfLiteEvalTensor* input_gate_bias, - const TfLiteEvalTensor* forget_gate_bias, - const TfLiteEvalTensor* cell_gate_bias, - const TfLiteEvalTensor* output_gate_bias, - const TfLiteEvalTensor* projection_weights, - const TfLiteEvalTensor* projection_weights_ledger, - const TfLiteEvalTensor* projection_bias, const TfLiteLSTMParams* params, - bool forward_sequence, bool time_major, int output_offset, - float* scratch_buffer, float* input_sf, float* aux_input_sf, - float* output_state_sf, float* prod_scaling_factors, - float* recovered_cell_weights, int8_t* input_quantized, - int8_t* aux_input_quantized, int8_t* output_state_quantized, - int8_t* cell_state_quantized, float* scales, TfLiteEvalTensor* output_state, - TfLiteEvalTensor* cell_state, int32_t* output_scratch_buffer, - TfLiteEvalTensor* output, int32_t* input_zp, int32_t* aux_input_zp, - int32_t* output_state_zp, int32_t* row_sums, int row_sums_size, - bool* compute_row_sums); - -TfLiteStatus EvalInteger8x8_16Lstm( - const TfLiteEvalTensor* input, - const TfLiteEvalTensor* input_to_input_weights, - const TfLiteEvalTensor* input_to_forget_weights, - const TfLiteEvalTensor* input_to_cell_weights, - const TfLiteEvalTensor* input_to_output_weights, - const TfLiteEvalTensor* recurrent_to_input_weights, - const TfLiteEvalTensor* recurrent_to_forget_weights, - const TfLiteEvalTensor* recurrent_to_cell_weights, - const TfLiteEvalTensor* recurrent_to_output_weights, - const TfLiteEvalTensor* cell_to_input_weights, - const TfLiteEvalTensor* cell_to_forget_weights, - const TfLiteEvalTensor* cell_to_output_weights, - const TfLiteEvalTensor* input_layer_norm_coefficients, - const TfLiteEvalTensor* forget_layer_norm_coefficients, - const TfLiteEvalTensor* cell_layer_norm_coefficients, - const TfLiteEvalTensor* output_layer_norm_coefficients, - const TfLiteEvalTensor* input_gate_bias, - const TfLiteEvalTensor* forget_gate_bias, - const TfLiteEvalTensor* cell_gate_bias, - const TfLiteEvalTensor* output_gate_bias, - const TfLiteEvalTensor* projection_weights, - const TfLiteEvalTensor* projection_bias, const TfLiteLSTMParams* params, - bool forward_sequence, bool time_major, - const IntegerLstmParameter* integer_lstm_param, int32_t output_state_zp, - TfLiteEvalTensor* output_state, TfLiteEvalTensor* cell_state, - TfLiteEvalTensor* output, int16_t* scratch0, int16_t* scratch1, - int16_t* scratch2, int16_t* scratch3, int8_t* scratch4, int32_t* scratch5); - -TfLiteStatus EvalInteger8x8_8Lstm( - const TfLiteEvalTensor* input, - const TfLiteEvalTensor* input_to_input_weights, - const TfLiteEvalTensor* input_to_forget_weights, - const TfLiteEvalTensor* input_to_cell_weights, - const TfLiteEvalTensor* input_to_output_weights, - const TfLiteEvalTensor* recurrent_to_input_weights, - const TfLiteEvalTensor* recurrent_to_forget_weights, - const TfLiteEvalTensor* recurrent_to_cell_weights, - const TfLiteEvalTensor* recurrent_to_output_weights, - const TfLiteEvalTensor* cell_to_input_weights, - const TfLiteEvalTensor* cell_to_forget_weights, - const TfLiteEvalTensor* cell_to_output_weights, - const TfLiteEvalTensor* input_layer_norm_coefficients, - const TfLiteEvalTensor* forget_layer_norm_coefficients, - const TfLiteEvalTensor* cell_layer_norm_coefficients, - const TfLiteEvalTensor* output_layer_norm_coefficients, - const TfLiteEvalTensor* input_gate_bias, - const TfLiteEvalTensor* forget_gate_bias, - const TfLiteEvalTensor* cell_gate_bias, - const TfLiteEvalTensor* output_gate_bias, - const TfLiteEvalTensor* projection_weights, - const TfLiteEvalTensor* projection_bias, const TfLiteLSTMParams* params, - TfLiteEvalTensor* output_state, TfLiteEvalTensor* cell_state, - TfLiteEvalTensor* output, const IntegerLstmParameter* integer_lstm_param, - int8_t* scratch0, int8_t* scratch1, int16_t* scratch2, int16_t* scratch3, - int16_t* scratch4, int16_t* scratch5, int16_t* scratch6, int16_t* scratch7); - -} // namespace tflite -#endif // TENSORFLOW_LITE_MICRO_KERNELS_LSTM_EVAL_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/lstm_shared.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/lstm_shared.h deleted file mode 100644 index ee34b848..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/lstm_shared.h +++ /dev/null @@ -1,67 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_LSTM_SHARED_H_ -#define TENSORFLOW_LITE_MICRO_KERNELS_LSTM_SHARED_H_ - -namespace tflite { - -// Input Tensors of size {n_batch, n_input} -constexpr int kLstmInputTensor = 0; - -// Input weight tensors of size: {n_cell, n_input} -constexpr int kLstmInputToInputWeightsTensor = 1; // Optional -constexpr int kLstmInputToForgetWeightsTensor = 2; -constexpr int kLstmInputToCellWeightsTensor = 3; -constexpr int kLstmInputToOutputWeightsTensor = 4; - -// Recurrent weight tensors of size {n_cell, n_output} -constexpr int kLstmRecurrentToInputWeightsTensor = 5; // Optional -constexpr int kLstmRecurrentToForgetWeightsTensor = 6; -constexpr int kLstmRecurrentToCellWeightsTensor = 7; -constexpr int kLstmRecurrentToOutputWeightsTensor = 8; - -// Peephole weights tensors of size {n_cell}, representing a diagonal matrix. -constexpr int kLstmCellToInputWeightsTensor = 9; // Optional -constexpr int kLstmCellToForgetWeightsTensor = 10; // Optional -constexpr int kLstmCellToOutputWeightsTensor = 11; // Optional - -// Gates bias tensors of size {n_cell} -constexpr int kLstmInputGateBiasTensor = 12; // Optional -constexpr int kLstmForgetGateBiasTensor = 13; -constexpr int kLstmCellGateBiasTensor = 14; -constexpr int kLstmOutputGateBiasTensor = 15; - -// Projection weight tensor of size {n_output, n_cell} -constexpr int kLstmProjectionWeightsTensor = 16; // Optional -// Projection bias tensor of size {n_output} -constexpr int kLstmProjectionBiasTensor = 17; // Optional - -// These state tensors are defined as variable tensors, and will be modified by -// this op. -constexpr int kLstmOutputStateTensor = 18; -constexpr int kLstmCellStateTensor = 19; - -// Layer norm coefficient tensors of size {n_cell}, representing a diagonal -// matrix. -constexpr int kLstmInputLayerNormCoefficientsTensor = 20; // Optional -constexpr int kLstmForgetLayerNormCoefficientsTensor = 21; // Optional -constexpr int kLstmCellLayerNormCoefficientsTensor = 22; // Optional -constexpr int kLstmOutputLayerNormCoefficientsTensor = 23; // Optional - -// Output tensors. -constexpr int kLstmOutputTensor = 0; - -} // namespace tflite -#endif // TENSORFLOW_LITE_MICRO_KERNELS_LSTM_SHARED_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/maximum_minimum.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/maximum_minimum.cc deleted file mode 100644 index 7964f1e6..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/maximum_minimum.cc +++ /dev/null @@ -1,133 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/kernels/internal/reference/maximum_minimum.h" - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { -namespace ops { -namespace micro { -namespace maximum_minimum { -namespace { - -// This file has a reference implementation of TFMaximum/TFMinimum. -enum KernelType { - kReference, -}; - -constexpr int kInputTensor1 = 0; -constexpr int kInputTensor2 = 1; -constexpr int kOutputTensor = 0; - -struct OpContext { - OpContext(TfLiteContext* context, TfLiteNode* node) { - input1 = tflite::micro::GetEvalInput(context, node, kInputTensor1); - input2 = tflite::micro::GetEvalInput(context, node, kInputTensor2); - output = tflite::micro::GetEvalOutput(context, node, kOutputTensor); - } - const TfLiteEvalTensor* input1; - const TfLiteEvalTensor* input2; - TfLiteEvalTensor* output; -}; - -struct MaximumOp { - template - static data_type op(data_type el1, data_type el2) { - return el1 > el2 ? el1 : el2; - } -}; - -struct MinimumOp { - template - static data_type op(data_type el1, data_type el2) { - return el1 < el2 ? el1 : el2; - } -}; - -} // namespace - -template -void TFLiteOperation(TfLiteContext* context, TfLiteNode* node, - const OpContext& op_context) { - reference_ops::MaximumMinimumBroadcastSlow( - tflite::micro::GetTensorShape(op_context.input1), - tflite::micro::GetTensorData(op_context.input1), - tflite::micro::GetTensorShape(op_context.input2), - tflite::micro::GetTensorData(op_context.input2), - tflite::micro::GetTensorShape(op_context.output), - tflite::micro::GetTensorData(op_context.output), - op_type::template op); -} - -template -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - OpContext op_context(context, node); - - if (kernel_type == kReference) { - switch (op_context.output->type) { - case kTfLiteFloat32: - TFLiteOperation(context, node, op_context); - break; - case kTfLiteInt8: - TFLiteOperation(context, node, op_context); - break; - case kTfLiteInt32: - TFLiteOperation(context, node, op_context); - break; - case kTfLiteInt64: - TFLiteOperation(context, node, op_context); - break; - default: - TF_LITE_KERNEL_LOG(context, - "Type %s (%d) is not supported by Maximum/Minimum.", - TfLiteTypeGetName(op_context.output->type), - op_context.output->type); - return kTfLiteError; - } - } else { - TF_LITE_KERNEL_LOG(context, - "Kernel type not supported by Maximum/Minimum."); - return kTfLiteError; - } - return kTfLiteOk; -} - -} // namespace maximum_minimum - -TfLiteRegistration Register_MAXIMUM() { - return tflite::micro::RegisterOp( - nullptr, nullptr, - maximum_minimum::Eval); -} - -TfLiteRegistration Register_MINIMUM() { - return tflite::micro::RegisterOp( - nullptr, nullptr, - maximum_minimum::Eval); -} - -} // namespace micro -} // namespace ops -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/micro_ops.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/micro_ops.h deleted file mode 100644 index c4dec92d..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/micro_ops.h +++ /dev/null @@ -1,133 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_MICRO_OPS_H_ -#define TENSORFLOW_LITE_MICRO_KERNELS_MICRO_OPS_H_ - -#include "tensorflow/lite/c/common.h" - -// Forward declaration of all micro op kernel registration methods. These -// registrations are included with the standard `BuiltinOpResolver`. -// -// This header is particularly useful in cases where only a subset of ops are -// needed. In such cases, the client can selectively add only the registrations -// their model requires, using a custom `(Micro)MutableOpResolver`. Selective -// registration in turn allows the linker to strip unused kernels. - -namespace tflite { - -// TFLM is incrementally moving towards a flat tflite namespace -// (https://abseil.io/tips/130). Any new ops (or cleanup of existing ops should -// have their Register function declarations in the tflite namespace. - -TfLiteRegistration Register_ADD(); -TfLiteRegistration Register_ADD_N(); -TfLiteRegistration Register_ASSIGN_VARIABLE(); -TfLiteRegistration Register_AVERAGE_POOL_2D(); -TfLiteRegistration Register_BATCH_TO_SPACE_ND(); -TfLiteRegistration Register_BROADCAST_ARGS(); -TfLiteRegistration Register_BROADCAST_TO(); -TfLiteRegistration Register_CALL_ONCE(); -TfLiteRegistration Register_CAST(); -// TODO(b/160234179): Change custom OPs to also return by value. -TfLiteRegistration* Register_CIRCULAR_BUFFER(); -TfLiteRegistration Register_CUMSUM(); -TfLiteRegistration Register_DEPTH_TO_SPACE(); -TfLiteRegistration Register_DEPTHWISE_CONV_2D(); -TfLiteRegistration Register_DEQUANTIZE(); -TfLiteRegistration Register_DIV(); -TfLiteRegistration Register_ELU(); -TfLiteRegistration Register_EXP(); -TfLiteRegistration Register_EXPAND_DIMS(); -TfLiteRegistration Register_FILL(); -TfLiteRegistration Register_FLOOR_DIV(); -TfLiteRegistration Register_FLOOR_MOD(); -TfLiteRegistration Register_GATHER(); -TfLiteRegistration Register_GATHER_ND(); -TfLiteRegistration Register_HARD_SWISH(); -TfLiteRegistration Register_IF(); -TfLiteRegistration Register_L2_POOL_2D(); -TfLiteRegistration Register_LEAKY_RELU(); -TfLiteRegistration Register_LOG_SOFTMAX(); -TfLiteRegistration Register_LOGICAL_AND(); -TfLiteRegistration Register_LOGICAL_OR(); -TfLiteRegistration Register_LOGISTIC(); -TfLiteRegistration Register_MAX_POOL_2D(); -TfLiteRegistration Register_MIRROR_PAD(); -TfLiteRegistration Register_PRELU(); -TfLiteRegistration Register_MUL(); -TfLiteRegistration Register_QUANTIZE(); -TfLiteRegistration Register_READ_VARIABLE(); -TfLiteRegistration Register_RELU(); -TfLiteRegistration Register_RELU6(); -TfLiteRegistration Register_RESIZE_BILINEAR(); -TfLiteRegistration Register_SHAPE(); -TfLiteRegistration Register_SLICE(); -TfLiteRegistration Register_SPACE_TO_BATCH_ND(); -TfLiteRegistration Register_SPACE_TO_DEPTH(); -TfLiteRegistration Register_SQUARED_DIFFERENCE(); -TfLiteRegistration Register_SQUEEZE(); -TfLiteRegistration Register_SUB(); -TfLiteRegistration Register_SVDF(); -TfLiteRegistration Register_TRANSPOSE(); -TfLiteRegistration Register_TRANSPOSE_CONV(); -// TODO(b/230666079): resolve conflict with xtensa implementation -TfLiteRegistration Register_UNIDIRECTIONAL_SEQUENCE_LSTM(); -TfLiteRegistration Register_VAR_HANDLE(); -TfLiteRegistration Register_WHILE(); -TfLiteRegistration Register_ZEROS_LIKE(); - -namespace ops { -namespace micro { - -TfLiteRegistration Register_ABS(); -TfLiteRegistration Register_ARG_MAX(); -TfLiteRegistration Register_ARG_MIN(); -TfLiteRegistration Register_CEIL(); -TfLiteRegistration Register_CONCATENATION(); -TfLiteRegistration Register_COS(); -TfLiteRegistration Register_EQUAL(); -TfLiteRegistration Register_FLOOR(); -TfLiteRegistration Register_GREATER(); -TfLiteRegistration Register_GREATER_EQUAL(); -TfLiteRegistration Register_LESS(); -TfLiteRegistration Register_LESS_EQUAL(); -TfLiteRegistration Register_LOG(); -TfLiteRegistration Register_LOGICAL_NOT(); -TfLiteRegistration Register_MAXIMUM(); -TfLiteRegistration Register_MINIMUM(); -TfLiteRegistration Register_NEG(); -TfLiteRegistration Register_NOT_EQUAL(); -TfLiteRegistration Register_PACK(); -TfLiteRegistration Register_PAD(); -TfLiteRegistration Register_PADV2(); -TfLiteRegistration Register_RESHAPE(); -TfLiteRegistration Register_RESIZE_NEAREST_NEIGHBOR(); -TfLiteRegistration Register_ROUND(); -TfLiteRegistration Register_RSQRT(); -TfLiteRegistration Register_SIN(); -TfLiteRegistration Register_SPLIT(); -TfLiteRegistration Register_SPLIT_V(); -TfLiteRegistration Register_SQRT(); -TfLiteRegistration Register_SQUARE(); -TfLiteRegistration Register_STRIDED_SLICE(); -TfLiteRegistration Register_UNPACK(); -TfLiteRegistration Register_L2_NORMALIZATION(); -TfLiteRegistration Register_TANH(); - -} // namespace micro -} // namespace ops -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_KERNELS_MICRO_OPS_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/micro_tensor_utils.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/micro_tensor_utils.cc deleted file mode 100644 index 88b097c7..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/micro_tensor_utils.cc +++ /dev/null @@ -1,809 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/micro/kernels/micro_tensor_utils.h" - -#include -#include -#include -#include -#include -#include - -#include "fixedpoint/fixedpoint.h" // from @gemmlowp -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/internal/cppmath.h" -#include "tensorflow/lite/kernels/op_macros.h" - -namespace tflite { -namespace micro_tensor_utils { - -namespace { -const int32_t kInt16Max = std::numeric_limits::max(); -const int32_t kInt16Min = std::numeric_limits::min(); -} // namespace - -void PortableSymmetricQuantizeFloats(const float* values, const int size, - int8_t* quantized_values, float* min_value, - float* max_value, float* scaling_factor) { - auto minmax = std::minmax_element(values, values + size); - *min_value = *minmax.first; - *max_value = *minmax.second; - - PortableSymmetricQuantizeFloats(values, size, quantized_values, *min_value, - *max_value, scaling_factor); -} - -void PortableSymmetricQuantizeFloats(const float* values, const int size, - int8_t* quantized_values, float min_value, - float max_value, float* scaling_factor) { - const int32_t kScale = 127; - const float range = std::max(std::abs(min_value), std::abs(max_value)); - if (range == 0) { - memset(quantized_values, 0, size * sizeof(int8_t)); - *scaling_factor = 1; - return; - } - *scaling_factor = range / kScale; - const float scaling_factor_inv = kScale / range; - for (int i = 0; i < size; ++i) { - const int32_t quantized_value = - static_cast(TfLiteRound(values[i] * scaling_factor_inv)); - // Clamp: just in case some odd numeric offset. - quantized_values[i] = static_cast( - std::min(kScale, std::max(-kScale, quantized_value))); - } -} - -void PortableAsymmetricQuantizeFloats(const float* values, const int size, - int8_t* quantized_values, - float* scaling_factor, int32_t* offset) { - const int32_t kMinScale = -128; - const int32_t kMaxScale = 127; - const double qmin_double = kMinScale; - const double qmax_double = kMaxScale; - const auto minmax = std::minmax_element(values, values + size); - const double rmin = static_cast(std::min(0.0f, *minmax.first)); - const double rmax = static_cast(std::max(0.0f, *minmax.second)); - if (rmin == rmax) { - memset(quantized_values, 0, size * sizeof(int8_t)); - *scaling_factor = 1; - *offset = 0; - return; - } else { - double scale = (rmax - rmin) / (qmax_double - qmin_double); - const double zero_point_from_min = qmin_double - rmin / scale; - const double zero_point_from_max = qmax_double - rmax / scale; - const double zero_point_from_min_error = - std::abs(qmin_double) + std::abs(rmin / scale); - const double zero_point_from_max_error = - std::abs(qmax_double) + std::abs(rmax / scale); - const double zero_point_double = - zero_point_from_min_error < zero_point_from_max_error - ? zero_point_from_min - : zero_point_from_max; - int8_t nudged_zero_point = 0; - if (zero_point_double <= qmin_double) { - nudged_zero_point = kMinScale; - } else if (zero_point_double >= qmax_double) { - nudged_zero_point = kMaxScale; - } else { - nudged_zero_point = static_cast(round(zero_point_double)); - } - *scaling_factor = scale; - *offset = nudged_zero_point; - } - const float scaling_factor_inv = 1.0f / *scaling_factor; - for (int i = 0; i < size; ++i) { - const int32_t quantized_value = static_cast( - TfLiteRound(*offset + values[i] * scaling_factor_inv)); - quantized_values[i] = - std::min(kMaxScale, std::max(kMinScale, quantized_value)); - } -} - -void PortableMatrixBatchVectorMultiplyAccumulate(const float* matrix, - int m_rows, int m_cols, - const float* vector, - int n_batch, float* result) { - float* result_in_batch = result; - for (int b = 0; b < n_batch; b++) { - const float* matrix_ptr = matrix; - for (int r = 0; r < m_rows; r++) { - float dot_prod = 0.0f; - const float* vector_in_batch = vector + b * m_cols; - for (int c = 0; c < m_cols; c++) { - dot_prod += *matrix_ptr++ * *vector_in_batch++; - } - *result_in_batch += dot_prod; - ++result_in_batch; - } - } -} - -void PortableMatrixBatchVectorMultiplyAccumulate( - const int8_t* __restrict__ matrix, const int m_rows, const int m_cols, - const int8_t* __restrict__ vectors, const float* scaling_factors, - int n_batch, float* __restrict__ result) { - for (int batch = 0; batch < n_batch; ++batch, vectors += m_cols) { - const float batch_scaling_factor = scaling_factors[batch]; - // Get the address of the first row. - const int8_t* row_ptr = matrix; - for (int row = 0; row < m_rows; ++row) { - // Initialize the dot product sum for the row to 0. - int32_t dotprod = 0; - // TODO(b/230666277): remove this -#if defined(__GNUC__) - // Prefetch the row to cache. - __builtin_prefetch(row_ptr, 0 /* prefetch for read */, - 3 /* temporal locality */); -#endif - for (int col = 0; col < m_cols; ++col, ++row_ptr) { - dotprod += (*row_ptr) * (vectors[col]); - } // for col - *result += dotprod * batch_scaling_factor; - ++result; - } // for row - } // for batch -} - -void PortableMatrixBatchVectorMultiplyAccumulate( - const int8_t* __restrict__ matrix, const int m_rows, const int m_cols, - const int8_t* __restrict__ vectors, const float* scaling_factors, - int n_batch, float* __restrict__ result, const float* per_channel_scale, - const int32_t* input_offset, int32_t* scratch, int32_t* row_sums, - bool* compute_row_sums, CpuBackendContext* context) { - if (input_offset == nullptr) { - PortableMatrixBatchVectorMultiplyAccumulate( - matrix, m_rows, m_cols, vectors, scaling_factors, n_batch, result); - return; - } - if (!compute_row_sums || *compute_row_sums) { - PortableReductionSumVector(matrix, row_sums, m_rows, m_cols); - if (compute_row_sums) { - *compute_row_sums = false; - } - } - - for (int batch = 0; batch < n_batch; ++batch, vectors += m_cols) { - const float batch_scaling_factor = scaling_factors[batch]; - const int32_t batch_offset = input_offset[batch]; - const int8_t* row_ptr = matrix; - for (int row = 0; row < m_rows; ++row) { - int32_t dotprod = 0; - float scale = batch_scaling_factor; - if (per_channel_scale) { - scale *= per_channel_scale[row]; - } -#if defined(__GNUC__) - // Prefetch the row to cache. - __builtin_prefetch(row_ptr, 0 /* prefetch for read */, - 3 /* temporal locality */); -#endif - for (int col = 0; col < m_cols; ++col, ++row_ptr) { - dotprod += (*row_ptr) * vectors[col]; - } // for col - dotprod -= row_sums[row] * batch_offset; - *result += dotprod * scale; - ++result; - } // for row - } // for batch -} - -void PortableSparseMatrixBatchVectorMultiplyAccumulate1x4( - const float* __restrict__ matrix, const int32_t* __restrict__ segments, - const int32_t* __restrict__ indices, int m_rows, int m_cols, - const float* __restrict__ vector, int n_batch, float* __restrict__ result) { - const int kBlockSize = 4; - TFLITE_DCHECK_EQ(m_cols % kBlockSize, 0); - for (int batch = 0; batch < n_batch; batch++) { - const float* matrix_ptr = matrix; - for (int row = 0; row < m_rows; row++) { - float dot_prod = 0.0f; - const float* vector_in_batch = vector + batch * m_cols; - for (int i = segments[row]; i < segments[row + 1]; i++) { - const int block_start_index = indices[i] * kBlockSize; - const float* vector_block_in_batch_ptr = - vector_in_batch + block_start_index; - for (int c = 0; c < kBlockSize; c++) { - dot_prod += *matrix_ptr++ * *vector_block_in_batch_ptr++; - } - } - result[batch * m_rows + row] += dot_prod; - } - } -} - -void PortableSparseMatrixBatchVectorMultiplyAccumulate1x16( - const int8_t* __restrict__ matrix, const int32_t* __restrict__ segments, - const int32_t* __restrict__ indices, int m_rows, int m_cols, - const int8_t* __restrict__ vector, const int32_t* __restrict__ bias_vector, - int n_batch, const int32_t input_offset, const int32_t output_multiplier, - const int32_t output_shift, const int32_t output_offset, - const int32_t output_activation_min, const int32_t output_activation_max, - int8_t* __restrict__ result) { - const int kBlockSize = 16; - TFLITE_DCHECK_EQ(m_cols % kBlockSize, 0); - for (int batch = 0; batch < n_batch; ++batch) { - const int8_t* matrix_ptr = matrix; - for (int row = 0; row < m_rows; ++row) { - int32_t dot_prod = 0; - const int8_t* vector_in_batch = vector + batch * m_cols; - for (int i = segments[row]; i < segments[row + 1]; ++i) { - const int block_start_index = indices[i] * kBlockSize; - const int8_t* vector_block_in_batch_ptr = - vector_in_batch + block_start_index; - for (int c = 0; c < kBlockSize; c++) { - dot_prod += *matrix_ptr * *vector_block_in_batch_ptr++; - dot_prod += *matrix_ptr++ * input_offset; - } - } - const int32_t bias_value = bias_vector != nullptr ? bias_vector[row] : 0; - dot_prod = MultiplyByQuantizedMultiplier(dot_prod + bias_value, - output_multiplier, output_shift); - dot_prod += output_offset; - result[batch * m_rows + row] = - static_cast(ActivationFunctionWithMinMax( - dot_prod, output_activation_min, output_activation_max)); - } - } -} - -void PortableSparseMatrixBatchVectorMultiplyAccumulate( - const float* __restrict__ matrix, const uint8_t* __restrict__ ledger, - int m_rows, int m_cols, const float* __restrict__ vector, int n_batch, - float* __restrict__ result) { - const int kBlockSize = 16; - TFLITE_DCHECK_EQ( // NOLINT - m_cols % kBlockSize, 0); - for (int batch = 0; batch < n_batch; batch++) { - const float* matrix_ptr = matrix; - const uint8_t* ledger_ptr = ledger; - for (int row = 0; row < m_rows; row++) { - float dot_prod = 0.0f; - int num_nonzero_blocks = *ledger_ptr++; - if (num_nonzero_blocks > 0) { - const float* vector_in_batch = vector + batch * m_cols; - for (int i = 0; i < num_nonzero_blocks; i++) { - const int block_start_index = *ledger_ptr++ * kBlockSize; - const float* vector_block_in_batch_ptr = - vector_in_batch + block_start_index; - for (int c = 0; c < kBlockSize; c++) { - dot_prod += *matrix_ptr++ * *vector_block_in_batch_ptr++; - } - } - } - result[batch * m_rows + row] += dot_prod; - } - } -} - -void PortableSparseMatrixBatchVectorMultiplyAccumulate( - const int8_t* __restrict__ matrix, const uint8_t* ledger, const int m_rows, - const int m_cols, const int8_t* __restrict__ vectors, - const float* scaling_factors, int n_batch, float* __restrict__ result) { - static const int kBlockSize = 16; - TFLITE_DCHECK_EQ( // NOLINT - m_cols % kBlockSize, 0); - for (int batch = 0; batch < n_batch; ++batch, vectors += m_cols) { - const float batch_scaling_factor = scaling_factors[batch]; - const uint8_t* ledger_ptr = ledger; - // Get the address of the first row. - const int8_t* row_ptr = matrix; - for (int row = 0; row < m_rows; ++row) { - // Initialize the dot product sum for the row to 0. - int32_t dotprod = 0; -#if defined(__GNUC__) - // Prefetch the row to cache. - __builtin_prefetch(row_ptr, 0 /* prefetch for read */, - 3 /* temporal locality */); -#endif - int num_nonzero_blocks = *ledger_ptr++; - for (int i = 0; i < num_nonzero_blocks; i++) { - const int block_start_index = *ledger_ptr++ * kBlockSize; - const int8_t* vector_block_ptr = vectors + block_start_index; - for (int c = 0; c < kBlockSize; c++) { - dotprod += (*row_ptr++) * (*vector_block_ptr++); - } // for block - } // for num_nonzero_blocks - result[batch * m_rows + row] += dotprod * batch_scaling_factor; - } // for row - } // for batch -} - -template -void PortableMatrixBatchVectorMultiplyAccumulateImpl( - const int8_t* input, const int32_t* bias, - const int8_t* input_to_gate_weights, int32_t multiplier, int32_t shift, - int32_t n_batch, int32_t n_input, int32_t n_output, int32_t output_zp, - T* output) { - const int16_t output_max = std::numeric_limits::max(); - const int16_t output_min = std::numeric_limits::min(); - for (int batch = 0; batch < n_batch; ++batch) { - for (int row = 0; row < n_output; ++row) { - int32_t acc = bias[row]; - for (int col = 0; col < n_input; ++col) { - int8_t input_val = input[batch * n_input + col]; - int8_t weights_val = input_to_gate_weights[row * n_input + col]; - acc += input_val * weights_val; - } - acc = MultiplyByQuantizedMultiplier(acc, multiplier, shift); - acc += output_zp; - acc += output[batch * n_output + row]; - if (acc > output_max) { - acc = output_max; - } - if (acc < output_min) { - acc = output_min; - } - output[batch * n_output + row] = static_cast(acc); - } - } -} - -void PortableMatrixBatchVectorMultiplyAccumulate( - const int8_t* input, const int32_t* bias, - const int8_t* input_to_gate_weights, int32_t multiplier, int32_t shift, - int32_t n_batch, int32_t n_input, int32_t n_output, int32_t output_zp, - int32_t* scratch, int16_t* output, CpuBackendContext* context) { - PortableMatrixBatchVectorMultiplyAccumulateImpl( - input, bias, input_to_gate_weights, multiplier, shift, n_batch, n_input, - n_output, output_zp, output); -} - -void PortableMatrixBatchVectorMultiplyAccumulate( - const int8_t* input, const int32_t* bias, - const int8_t* input_to_gate_weights, int32_t multiplier, int32_t shift, - int32_t n_batch, int32_t n_input, int32_t n_output, int32_t output_zp, - int32_t* scratch, int8_t* output, CpuBackendContext* context) { - PortableMatrixBatchVectorMultiplyAccumulateImpl( - input, bias, input_to_gate_weights, multiplier, shift, n_batch, n_input, - n_output, output_zp, output); -} - -void PortableMatrixBatchVectorMultiply(const int8_t* input, - int32_t input_zeropoint, - const int8_t* input_to_gate_weights, - int32_t input_to_gate_effective_scale_a, - int32_t input_to_gate_effective_scale_b, - int32_t n_batch, int32_t n_input, - int32_t n_cell, int8_t* gate_output, - int8_t gate_output_zp) { - const int32_t int8_max = std::numeric_limits::max(); - const int32_t int8_min = std::numeric_limits::min(); - for (int batch = 0; batch < n_batch; ++batch) { - for (int row = 0; row < n_cell; ++row) { - int32_t acc = 0; - for (int col = 0; col < n_input; ++col) { - int32_t input_val = input[batch * n_input + col]; - int8_t weights_val = input_to_gate_weights[row * n_input + col]; - acc += (input_val - input_zeropoint) * weights_val; - } - acc = MultiplyByQuantizedMultiplier(acc, input_to_gate_effective_scale_a, - input_to_gate_effective_scale_b); - acc += gate_output_zp; - if (acc > int8_max) { - acc = int8_max; - } - if (acc < int8_min) { - acc = int8_min; - } - gate_output[batch * n_cell + row] = static_cast(acc); - } - } -} - -void PortableMatrixBatchVectorMultiply( - const int16_t* hidden, const int8_t* hidden_to_output_weights, - int32_t proj_effective_scale_a, int32_t proj_effective_scale_b, - const int32_t* gate_bias, int32_t n_batch, int32_t n_hidden, - int32_t n_output, int32_t output_zp, int8_t* proj_output) { - const int16_t int8_max = std::numeric_limits::max(); - const int16_t int8_min = std::numeric_limits::min(); - for (int batch = 0; batch < n_batch; ++batch) { - for (int row = 0; row < n_output; ++row) { - int64_t acc = gate_bias[row]; - for (int col = 0; col < n_hidden; ++col) { - int16_t input_val = hidden[batch * n_hidden + col]; - int8_t weights_val = hidden_to_output_weights[row * n_hidden + col]; - int64_t curr = acc; - acc += input_val * weights_val; - if (input_val * weights_val > 0 && acc < curr) { - acc = std::numeric_limits::max(); - } - if (input_val * weights_val < 0 && acc > curr) { - acc = std::numeric_limits::min(); - } - } - acc = MultiplyByQuantizedMultiplier(acc, proj_effective_scale_a, - proj_effective_scale_b); - acc += output_zp; - if (acc > int8_max) { - acc = int8_max; - } - if (acc < int8_min) { - acc = int8_min; - } - proj_output[batch * n_output + row] = acc; - } - } -} - -void PortableApplyLayerNorm(const int16_t* input, - const int16_t* layer_norm_weights, - const int32_t* bias, int32_t layer_norm_scale_a, - int32_t layer_norm_scale_b, int32_t variance_limit, - int n_batch, int n_input, int16_t* output) { - // The square of std::pow(2, 10), which is the extra factor that makes sure - // normalized values has enough resolution. - static const int kTwoToPower20 = 1 << 20; - for (int i = 0; i < n_batch; ++i) { - int64_t sum = 0; - int64_t sum_sq = 0; - for (int j = 0; j < n_input; ++j) { - const int32_t index = i * n_input + j; - int32_t val = static_cast(input[index]); - sum += val; - sum_sq += val * val; - } - int32_t mean = - static_cast(static_cast(sum) * 1024 / n_input); - // TODO(b/173994730): Avoids overflow but only works for POT n_input. - int32_t temp = kTwoToPower20 / n_input; - int64_t variance = - sum_sq * temp - static_cast(mean) * static_cast(mean); - int32_t variance2 = static_cast(variance / kTwoToPower20); - if (variance2 < 1) { - variance2 = variance_limit; - } - int32_t stddev_inverse_a; - int stddev_inverse_b; - GetInvSqrtQuantizedMultiplierExp(variance2, /*reverse_shift*/ -1, - &stddev_inverse_a, &stddev_inverse_b); - - for (int j = 0; j < n_input; ++j) { - const int32_t index = i * n_input + j; - int32_t val = static_cast(input[index]); - int32_t shifted = 1024 * val - mean; - int32_t rescaled = MultiplyByQuantizedMultiplier( - shifted, stddev_inverse_a, stddev_inverse_b); - int64_t val3 = rescaled * layer_norm_weights[j] + bias[j]; - int32_t val4 = - static_cast((val3 > 0 ? val3 + 512 : val3 - 512) / 1024); - int32_t val5 = MultiplyByQuantizedMultiplier(val4, layer_norm_scale_a, - layer_norm_scale_b + 12); - val5 = std::min(std::max(kInt16Min, val5), kInt16Max); - output[index] = static_cast(val5); - } - } -} - -void PortableApplyLayerNormFloat(const int16_t* input, - const int16_t* layer_norm_weights, - int32_t layer_norm_scale_a, - int32_t layer_norm_scale_b, - const int32_t* bias, int n_batch, int n_input, - int16_t* output) { - const int32_t int16_max = std::numeric_limits::max(); - const int32_t int16_min = std::numeric_limits::min(); - const float layer_norm_scale = - layer_norm_scale_a * - std::pow(2.0, static_cast(layer_norm_scale_b - 31)); - const float bias_scale = - static_cast(std::pow(2.0, -10)) * layer_norm_scale; - - for (int batch = 0; batch < n_batch; ++batch) { - float sum = 0.0f; - float sum_sq = 0.0f; - for (int i = 0; i < n_input; ++i) { - const int index = batch * n_input + i; - const float value = static_cast(input[index]); - sum += value; - sum_sq += value * value; - } - const float mean = sum / n_input; - float stddev_inv = 0.0f; - const float variance = sum_sq / n_input - mean * mean; - if (variance == 0) { - stddev_inv = 1.0f / std::sqrt(1e-8f); - } else { - stddev_inv = 1.0f / std::sqrt(variance); - } - for (int i = 0; i < n_input; ++i) { - const int index = batch * n_input + i; - const float normalized_value = - (static_cast(input[index]) - mean) * stddev_inv; - const float weighted_normalized_value = - normalized_value * layer_norm_weights[i] * layer_norm_scale + - bias[i] * bias_scale; - const int32_t quant_output = static_cast(round( - weighted_normalized_value * static_cast(std::pow(2, 12)))); - output[index] = std::min(int16_max, std::max(int16_min, quant_output)); - } - } -} - -void PortableMatrixScalarMultiplyAccumulate(const int8_t* matrix, - int32_t scalar, int32_t n_row, - int32_t n_col, int32_t* output) { - for (int i = 0; i < n_row; ++i) { - int32_t row_sum = 0; - for (int j = 0; j < n_col; ++j) { - row_sum += *matrix++; - } - output[i] += row_sum * scalar; - } -} - -void PortableApplySigmoid(const int16_t* input, int32_t n_batch, - int32_t n_input, int16_t* output) { - for (int batch = 0; batch < n_batch; ++batch) { - for (int c = 0; c < n_input; c++) { - using F3 = gemmlowp::FixedPoint; - using F0 = gemmlowp::FixedPoint; - const int index = batch * n_input + c; - F3 sigmoid_input = F3::FromRaw(input[index]); - F0 sigmoid_output = gemmlowp::logistic(sigmoid_input); - output[index] = sigmoid_output.raw(); - } - } -} - -void PortableApplySigmoidFloat(const int16_t* input, int32_t n_batch, - int32_t n_input, int16_t* output) { - const int32_t int16_max = std::numeric_limits::max(); - const int32_t int16_min = std::numeric_limits::min(); - for (int batch = 0; batch < n_batch; ++batch) { - for (int i = 0; i < n_input; ++i) { - const int index = batch * n_input + i; - const float float_input = - input[index] * static_cast(std::pow(2, -12)); - const float float_output = 1.0f / (1.0f + std::exp(-float_input)); - const int32_t quant_output = static_cast( - float_output * static_cast(std::pow(2, 15))); - const int32_t quant_output_clamped = - std::min(int16_max, std::max(int16_min, quant_output)); - output[index] = static_cast(quant_output_clamped); - } - } -} - -template -void PortableApplyTanhImpl(const int16_t* input, int32_t n_batch, - int32_t n_input, int16_t* output) { - using FX = gemmlowp::FixedPoint; - using F0 = gemmlowp::FixedPoint; - for (int batch = 0; batch < n_batch; ++batch) { - for (int i = 0; i < n_input; ++i) { - const int index = batch * n_input + i; - FX tanh_input = FX::FromRaw(input[index]); - F0 tanh_output = gemmlowp::tanh(tanh_input); - output[index] = tanh_output.raw(); - } - } -} - -void PortableApplyTanh(int32_t integer_bits, const int16_t* input, - int32_t n_batch, int32_t n_input, int16_t* output) { - if (integer_bits > 6) { - TFLITE_ASSERT_FALSE; - } -#define DISPATCH_TANH(i) \ - case i: \ - PortableApplyTanhImpl(input, n_batch, n_input, output); \ - break; - switch (integer_bits) { - DISPATCH_TANH(0); - DISPATCH_TANH(1); - DISPATCH_TANH(2); - DISPATCH_TANH(3); - DISPATCH_TANH(4); - DISPATCH_TANH(5); - DISPATCH_TANH(6); - default: - return; - } -#undef DISPATCH_TANH -} - -void PortableApplyTanhFloat(const int16_t* input, int32_t n_batch, - int32_t n_input, int32_t integer_bits, - int16_t* output) { - const int32_t int16_max = std::numeric_limits::max(); - const int32_t int16_min = std::numeric_limits::min(); - const double two = 2.0; - for (int batch = 0; batch < n_batch; ++batch) { - for (int i = 0; i < n_input; ++i) { - const int index = batch * n_input + i; - const float float_input = - input[index] * std::pow(two, static_cast(integer_bits)); - const float float_output = std::tanh(float_input); - const int32_t quant_output = static_cast( - float_output * static_cast(std::pow(2, 15))); - const int32_t quant_output_clamped = - std::min(int16_max, std::max(int16_min, quant_output)); - output[index] = static_cast(quant_output_clamped); - } - } -} - -void PortableCwiseMul(const int16_t* input_1, const int16_t* input_2, - int n_batch, int n_input, int shift, int16_t* output) { - for (int batch = 0; batch < n_batch; ++batch) { - for (int i = 0; i < n_input; ++i) { - const int index = batch * n_input + i; - const int16_t a = input_1[index]; - const int16_t b = input_2[index]; - const int32_t value = static_cast(a) * static_cast(b); - output[index] = - static_cast(gemmlowp::RoundingDivideByPOT(value, shift)); - } - } -} - -void PortableCwiseMul(const int16_t* input_1, const int16_t* input_2, - int32_t multiplier, int32_t shift, int32_t n_batch, - int32_t n_input, int32_t output_zp, int8_t* output) { - for (int batch = 0; batch < n_batch; ++batch) { - for (int i = 0; i < n_input; ++i) { - const int index = batch * n_input + i; - const int16_t a = input_1[index]; - const int16_t b = input_2[index]; - int32_t value = static_cast(a) * static_cast(b); - value = MultiplyByQuantizedMultiplier(value, multiplier, shift); - value -= output_zp; - value = std::min(std::max(static_cast(-128), value), - static_cast(127)); - - output[index] = static_cast(value); - } - } -} - -void PortableCwiseAdd(const int16_t* input_1, const int16_t* input_2, - int n_batch, int n_input, int16_t* output) { - for (int batch = 0; batch < n_batch; ++batch) { - for (int i = 0; i < n_input; ++i) { - const int index = batch * n_input + i; - int32_t sum = input_1[index] + input_2[index]; - const int32_t sum_clamped = std::min(kInt16Max, std::max(kInt16Min, sum)); - output[index] = static_cast(sum_clamped); - } - } -} - -float PortableVectorVectorDotProduct(const float* vector1, const float* vector2, - int v_size) { - float result = 0.0; - for (int v = 0; v < v_size; v++) { - result += *vector1++ * *vector2++; - } - return result; -} - -namespace { -inline int32_t VectorVectorDotProduct(const int16_t* vector1, - const int16_t* vector2, int v_size) { - int32_t result = 0; - for (int v = 0; v < v_size; v++) { - result += *vector1++ * *vector2++; - } - return result; -} -} // namespace - -void PortableBatchVectorBatchVectorDotProduct(const int16_t* vector1, - const int16_t* vector2, - int v_size, int n_batch, - int32_t* result) { - for (int b = 0; b < n_batch; b++) { - result[b] = VectorVectorDotProduct(vector1, vector2, v_size); - vector1 += v_size; - vector2 += v_size; - } -} - -void PortableVectorBatchVectorCwiseProductAccumulate( - const int16_t* vector, int v_size, const int16_t* batch_vector, int n_batch, - int32_t multiplier, int shift, int16_t* result) { - for (int b = 0; b < n_batch; b++) { - for (int v = 0; v < v_size; v++) { - int32_t prod = vector[v] * *batch_vector++; - prod = MultiplyByQuantizedMultiplier(prod, multiplier, shift); - int32_t output = prod + *result; - output = std::max(std::min(static_cast(32767), output), - static_cast(-32768)); - *result++ = output; - } - } -} - -void PortableSub1Vector(const float* vector, int v_size, float* result) { - for (int v = 0; v < v_size; v++) { - *result++ = 1.0f - *vector++; - } -} - -void PortableSub1Vector(const int16_t* vector, int v_size, int16_t* result) { - static const int16_t kOne = 32767; - for (int v = 0; v < v_size; v++) { - *result++ = kOne - *vector++; - } -} - -void PortableVectorScalarMultiply(const int8_t* vector, const int v_size, - const float scale, float* result) { - for (int v = 0; v < v_size; ++v) { - *result++ = scale * *vector++; - } -} - -void PortableMeanStddevNormalization(const float* __restrict__ input_vector, - float* __restrict__ output_vector, - int v_size, int n_batch) { - for (int batch = 0; batch < n_batch; ++batch) { - float sum = 0.0f; - for (int i = 0; i < v_size; ++i) { - sum += input_vector[i]; - } - const float mean = sum / v_size; - float sum_diff_sq = 0.0f; - for (int i = 0; i < v_size; ++i) { - const float diff = input_vector[i] - mean; - sum_diff_sq += diff * diff; - } - const float variance = sum_diff_sq / v_size; - constexpr float kNormalizationConstant = 1e-8f; - const float stddev_inv = - 1.0f / std::sqrt(variance + kNormalizationConstant); - for (int i = 0; i < v_size; ++i) { - output_vector[i] = (input_vector[i] - mean) * stddev_inv; - } - input_vector += v_size; - output_vector += v_size; - } -} - -void PortableTwoGateSaturatingAdd(const int8_t* input, int8_t input_zp, - const int8_t* recurrent, int8_t recurrent_zp, - int32_t input_effective_scale_a, - int32_t input_effective_scale_b, - int32_t recurrent_effective_scale_a, - int32_t recurrent_effective_scale_b, - int32_t n_batch, int32_t n_cell, - int16_t* output) { - const int32_t int16_max = std::numeric_limits::max(); - const int32_t int16_min = std::numeric_limits::min(); - for (int i = 0; i < n_batch * n_cell; ++i) { - int32_t x = static_cast(input[i]) - static_cast(input_zp); - int32_t h = - static_cast(recurrent[i]) - static_cast(recurrent_zp); - int32_t x_scaled = MultiplyByQuantizedMultiplier(x, input_effective_scale_a, - input_effective_scale_b); - int32_t h_scaled = MultiplyByQuantizedMultiplier( - h, recurrent_effective_scale_a, recurrent_effective_scale_b); - int32_t y = h_scaled + x_scaled; - if (y > int16_max) { - y = int16_max; - } - if (y < int16_min) { - y = int16_min; - } - output[i] = static_cast(y); - } -} - -} // namespace micro_tensor_utils -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/micro_tensor_utils.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/micro_tensor_utils.h deleted file mode 100644 index 673ba6a3..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/micro_tensor_utils.h +++ /dev/null @@ -1,874 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -// This file and the associated .cc file is branched from -// tensorflow/lite/kernels/internal/reference/portable_tensor_utils* -// TFLM needs to create its own because the original files are coupled with -// the tensor_utils module, which we cannot reuse due to its use of the -// Eigen library. - -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_MICRO_TENSOR_UTILS_H_ -#define TENSORFLOW_LITE_MICRO_KERNELS_MICRO_TENSOR_UTILS_H_ - -#include -#include -#include - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" - -#if defined(_MSC_VER) -#define __restrict__ __restrict -#endif - -namespace tflite { - -// Not all backends support CpuBackendContext usage, so forward declare to avoid -// pulling in its implementation. -// TODO(b/230666277): consider removing this since micro does not utilize it -class CpuBackendContext; - -namespace micro_tensor_utils { - -template -inline bool PortableIsZeroVector(const T* vector, int v_size) { - for (int i = 0; i < v_size; ++i) { - if (vector[i] != 0) { - return false; - } - } - return true; -} - -void PortableSymmetricQuantizeFloats(const float* values, const int size, - int8_t* quantized_values, float* min_value, - float* max_value, float* scaling_factor); - -void PortableSymmetricQuantizeFloats(const float* values, const int size, - int8_t* quantized_values, float min_value, - float max_value, float* scaling_factor); - -void PortableAsymmetricQuantizeFloats(const float* values, const int size, - int8_t* quantized_values, - float* scaling_factor, int32_t* offset); - -// Multiply a matrix by a batch vector, and store results in a batch-size -// vector. -void PortableMatrixBatchVectorMultiplyAccumulate(const float* matrix, - int m_rows, int m_cols, - const float* vector, - int n_batch, float* result); - -void PortableMatrixBatchVectorMultiplyAccumulate( - const int8_t* __restrict__ matrix, const int m_rows, const int m_cols, - const int8_t* __restrict__ vectors, const float* scaling_factors, - int n_batch, float* __restrict__ result); - -void PortableMatrixBatchVectorMultiplyAccumulate( - const int8_t* __restrict__ matrix, const int m_rows, const int m_cols, - const int8_t* __restrict__ vectors, const float* scaling_factors, - int n_batch, float* __restrict__ result, const float* per_channel_scale, - const int32_t* input_offset, int32_t* scratch, int32_t* row_sums, - bool* compute_row_sums, CpuBackendContext* context); - -void PortableMatrixBatchVectorMultiplyAccumulate( - const int8_t* __restrict__ matrix, const int m_rows, const int m_cols, - const int8_t* __restrict__ vector, const float* scaling_factors, - int n_batch, int32_t* scratch, float* __restrict__ result, - CpuBackendContext* context); - -void PortableSparseMatrixBatchVectorMultiplyAccumulate1x4( - const float* __restrict__ matrix, const int32_t* __restrict__ segments, - const int32_t* __restrict__ indices, int m_rows, int m_cols, - const float* __restrict__ vector, int n_batch, float* __restrict__ result); - -void PortableSparseMatrixBatchVectorMultiplyAccumulate( - const float* __restrict__ matrix, const uint8_t* __restrict__ ledger, - int m_rows, int m_cols, const float* __restrict__ vector, int n_batch, - float* __restrict__ result); - -void PortableSparseMatrixBatchVectorMultiplyAccumulate1x16( - const int8_t* __restrict__ matrix, const int32_t* __restrict__ segments, - const int32_t* __restrict__ indices, int m_rows, int m_cols, - const int8_t* __restrict__ vector, const int32_t* __restrict__ bias_vector, - int n_batch, const int32_t input_offset, const int32_t output_multiplier, - const int32_t output_shift, const int32_t output_offset, - const int32_t output_activation_min, const int32_t output_activation_max, - int8_t* __restrict__ result); - -void PortableSparseMatrixBatchVectorMultiplyAccumulate( - const int8_t* __restrict__ matrix, const uint8_t* ledger, const int m_rows, - const int m_cols, const int8_t* __restrict__ vectors, - const float* scaling_factors, int n_batch, float* __restrict__ result); - -// Dot product of two vectors. -float PortableVectorVectorDotProduct(const float* vector1, const float* vector2, - int v_size); - -void PortableBatchVectorBatchVectorDotProduct(const int16_t* vector1, - const int16_t* vector2, - int v_size, int n_batch, - int32_t* result); - -void PortableVectorBatchVectorCwiseProductAccumulate( - const int16_t* vector, int v_size, const int16_t* batch_vector, int n_batch, - int32_t multiplier, int shift, int16_t* result); - -void PortableMatrixBatchVectorMultiplyAccumulate( - const int8_t* input, const int32_t* bias, - const int8_t* input_to_gate_weights, int32_t multiplier, int32_t shift, - int32_t n_batch, int32_t n_input, int32_t n_output, int32_t output_zp, - int32_t* scratch, int16_t* output, CpuBackendContext* context); - -void PortableMatrixBatchVectorMultiplyAccumulate( - const int8_t* input, const int32_t* bias, - const int8_t* input_to_gate_weights, int32_t multiplier, int32_t shift, - int32_t n_batch, int32_t n_input, int32_t n_output, int32_t output_zp, - int32_t* scratch, int8_t* output, CpuBackendContext* context); - -void PortableMatrixBatchVectorMultiply(const int8_t* input, - int32_t input_zeropoint, - const int8_t* input_to_gate_weights, - int32_t input_to_gate_effective_scale_a, - int32_t input_to_gate_effective_scale_b, - int32_t n_batch, int32_t n_input, - int32_t n_cell, int8_t* gate_output, - int8_t gate_output_zp); - -void PortableMatrixBatchVectorMultiply( - const int16_t* hidden, const int8_t* hidden_to_output_weights, - int32_t proj_effective_scale_a, int32_t proj_effective_scale_b, - const int32_t* gate_bias, int32_t n_batch, int32_t n_hidden, - int32_t n_output, int32_t output_zp, int8_t* proj_output); - -void PortableMatrixScalarMultiplyAccumulate(const int8_t* matrix, - int32_t scalar, int32_t n_row, - int32_t n_col, int32_t* output); - -void PortableApplyLayerNorm(const int16_t* input, - const int16_t* layer_norm_weights, - const int32_t* bias, int32_t layer_norm_scale_a, - int32_t layer_norm_scale_b, int32_t variance_limit, - int n_batch, int n_input, int16_t* output); - -void PortableApplyLayerNormFloat(const int16_t* input, - const int16_t* layer_norm_weights, - int32_t layer_norm_scale_a, - int32_t layer_norm_scale_b, - const int32_t* bias, int n_batch, int n_input, - int16_t* output); - -void PortableApplySigmoid(const int16_t* input, int32_t n_batch, - int32_t n_input, int16_t* output); - -void PortableApplySigmoidFloat(const int16_t* input, int32_t n_batch, - int32_t n_input, int16_t* output); - -void PortableApplyTanh(int32_t integer_bits, const int16_t* input, - int32_t n_batch, int32_t n_input, int16_t* output); - -void PortableApplyTanhFloat(const int16_t* input, int32_t n_batch, - int32_t n_input, int32_t integer_bits, - int16_t* output); - -void PortableCwiseMul(const int16_t* input_1, const int16_t* input_2, - int n_batch, int n_input, int shift, int16_t* output); - -void PortableCwiseMul(const int16_t* input_1, const int16_t* input_2, - int32_t multiplier, int32_t shift, int32_t n_batch, - int32_t n_input, int32_t output_zp, int8_t* output); - -void PortableCwiseAdd(const int16_t* input_1, const int16_t* input_2, - int n_batch, int n_input, int16_t* output); - -template -inline void PortableCwiseClipping(T* vector, const int v_size, - const T& clipping_value) { - for (int i = 0; i < v_size; i++) { - vector[i] = std::max(std::min(clipping_value, vector[i]), - static_cast(-clipping_value)); - } -} - -// Batch vector initialization with another vector. -void PortableVectorBatchVectorAssign(const float* vector, int v_size, - int n_batch, float* batch_vector); - -// Compute "1.0f - elements of vector" (used in CIFG). -void PortableSub1Vector(const float* vector, int v_size, float* result); - -void PortableSub1Vector(const int16_t* vector, int v_size, int16_t* result); - -// Multiply all elements of vector with a scalar. -void PortableVectorScalarMultiply(const int8_t* vector, int v_size, float scale, - float* result); - -// Reduce-sum on a vector: -// input_vector: pointer to input vector. -// output_vector: pointer to vector. -// output_size: output vector size. -// reduction_size: number of consecutive elements from input vector which are -// added to get one element of output. -template -inline void PortableReductionSumVector(const INPUT* input_vector, - OUTPUT* output_vector, int output_size, - int reduction_size) { - for (int o = 0; o < output_size; o++) { - OUTPUT result = 0; - for (int r = 0; r < reduction_size; r++) { - result += input_vector[r]; - } - output_vector[o] = result; - input_vector += reduction_size; - } -} - -// Layer norm for each batch. -void PortableMeanStddevNormalization(const float* __restrict__ input_vector, - float* __restrict__ output_vector, - int v_size, int n_batch); - -// Saturate Add. -void PortableTwoGateSaturatingAdd(const int8_t* input, int8_t input_zp, - const int8_t* recurrent, int8_t recurrent_zp, - int32_t input_effective_scale_a, - int32_t input_effective_scale_b, - int32_t recurrent_effective_scale_a, - int32_t recurrent_effective_scale_b, - int32_t n_batch, int32_t n_cell, - int16_t* output); - -// Add another vector for each batch in the batch vector. -template -inline void VectorBatchVectorAdd(const T* vector, int v_size, int n_batch, - T* batch_vector) { - for (int b = 0; b < n_batch; b++) { - for (int i = 0; i < v_size; ++i) { - batch_vector[i] += vector[i]; - } - batch_vector += v_size; - } -} - -// Cwise product of two vectors. -template -inline void VectorVectorCwiseProduct(const T* vector1, const T* vector2, - int v_size, T* result) { - for (int v = 0; v < v_size; v++) { - *result++ = *vector1++ * *vector2++; - } -} - -// Cwise product of a vector and a batch-vector. -template -inline void VectorBatchVectorCwiseProduct(const T* vector, int v_size, - const T* batch_vector, int n_batch, - T* result) { - for (int b = 0; b < n_batch; b++) { - VectorVectorCwiseProduct(vector, batch_vector, v_size, result); - // Update the pointers. - result += v_size; - batch_vector += v_size; - } -} - -// Reduce-sum on a float input vector: -// input_vector: float pointer to input vector. -// output_vector: float pointer to vector. -// output_size: output vector size. -// reduction_size: number of consecutive elements from input vector which are -// added to get one element of output. -inline void ReductionSumVector(const float* input_vector, float* output_vector, - int output_size, int reduction_size) { - PortableReductionSumVector(input_vector, output_vector, output_size, - reduction_size); -} - -// Same as above but input/output is 32 bit integer. -inline void ReductionSumVector(const int32_t* input_vector, - int32_t* output_vector, int output_size, - int reduction_size) { - PortableReductionSumVector(input_vector, output_vector, output_size, - reduction_size); -} - -// Same as above but input is 8 bit integer. -inline void ReductionSumVector(const int8_t* input_vector, - int32_t* output_vector, int output_size, - int reduction_size) { - PortableReductionSumVector(input_vector, output_vector, output_size, - reduction_size); -} - -// Cwise product and accumulate of two vectors. Since it's a MAC operation, the -// assumption here is that result array is initialized to valid values. -template -inline void VectorVectorCwiseProductAccumulate(const T* __restrict__ vector1, - const T* __restrict__ vector2, - int v_size, - T* __restrict__ result) { - for (int v = 0; v < v_size; v++) { - *result++ += *vector1++ * *vector2++; - } -} - -// Batch vector initialization with another vector. -template -inline void VectorBatchVectorAssign(const T* vector, int v_size, int n_batch, - T* batch_vector) { - for (int b = 0; b < n_batch; b++) { - std::copy_n(vector, v_size, batch_vector + b * v_size); - } -} - -inline void SymmetricQuantizeFloats(const float* values, const int size, - int8_t* quantized_values, float* min, - float* max, float* scaling_factor) { - PortableSymmetricQuantizeFloats(values, size, quantized_values, min, max, - scaling_factor); -} - -inline void SymmetricQuantizeFloats(const float* values, const int size, - int8_t* quantized_values, float min_value, - float max_value, float* scaling_factor) { - PortableSymmetricQuantizeFloats(values, size, quantized_values, min_value, - max_value, scaling_factor); -} - -inline void AsymmetricQuantizeFloats(const float* values, const int size, - int8_t* quantized_values, - float* scaling_factor, int32_t* offset) { - PortableAsymmetricQuantizeFloats(values, size, quantized_values, - scaling_factor, offset); -} - -// Helper function to quantize floats. -// float_data_ptr input float vectors -// n_batch number of input vectors -// n_data size of a single input vector -// quantized_data_ptr (out) vector with quantized data -// scaling_factors (out) scaling factors (one per vector) -// zero_points (out) zero points (one per vector) -// do_asymmetric controls if the quantization should be asymmetric. -inline void BatchQuantizeFloats(const float* float_data_ptr, int n_batch, - int n_data, int8_t* quantized_data_ptr, - float* scaling_factors, int32_t* zero_points, - bool do_asymmetric) { - for (int b = 0; b < n_batch; ++b) { - const int offset = b * n_data; - if (do_asymmetric) { - AsymmetricQuantizeFloats(float_data_ptr + offset, n_data, - quantized_data_ptr + offset, &scaling_factors[b], - &zero_points[b]); - } else { - float unused_min, unused_max; - SymmetricQuantizeFloats(float_data_ptr + offset, n_data, - quantized_data_ptr + offset, &unused_min, - &unused_max, &scaling_factors[b]); - } - } -} - -// Check if all entries of a vector are zero for float. -inline bool IsZeroVector(const float* vector, int v_size) { - return PortableIsZeroVector(vector, v_size); -} - -// Check if all entries of a vector are zero for int8_t. -inline bool IsZeroVector(const int8_t* vector, int v_size) { - return PortableIsZeroVector(vector, v_size); -} - -// Apply Layer Normalization (https://arxiv.org/abs/1607.06450) to a Quantized -// vector. -// Parameters: -// - input: batch vector of size n_batch * n_input; 16 bit. -// - layer_norm_weights: the quantized layer normalization weights. -// - bias: the bias for the layer normalization. -// - layer_norm_scale_a: multiplier for scale factor. -// - layer_norm_scale_b: shift for scale factor. -// - variance_limit: the guard to make sure the inverse does not overflow. -// - n_batch: the number of batches. -// - n_input: the size for input and output. -// - output: the 16 bit output -inline void ApplyLayerNorm(const int16_t* input, - const int16_t* layer_norm_weights, - const int32_t* bias, int32_t layer_norm_scale_a, - int32_t layer_norm_scale_b, int32_t variance_limit, - int n_batch, int n_input, int16_t* output) { - PortableApplyLayerNorm(input, layer_norm_weights, bias, layer_norm_scale_a, - layer_norm_scale_b, variance_limit, n_batch, n_input, - output); -} - -// Same as above but the internal calculation is done in float. -inline void ApplyLayerNormFloat(const int16_t* input, - const int16_t* layer_norm_weights, - int32_t layer_norm_scale_a, - int32_t layer_norm_scale_b, const int32_t* bias, - int n_batch, int n_input, int16_t* output) { - PortableApplyLayerNormFloat(input, layer_norm_weights, layer_norm_scale_a, - layer_norm_scale_b, bias, n_batch, n_input, - output); -} - -// Apply Sigmoid to a quantized vector. -// Parameters: -// - input: batch vector of size n_batch * n_input; 16 bit. -// - n_batch: the number of batches. -// - n_input: the size for input and output. -// - output: the 16 bit output -// The input is in Q3.12 format and the output is in Q0.15 format. -inline void ApplySigmoid(const int16_t* input, int32_t n_batch, int32_t n_input, - int16_t* output) { - PortableApplySigmoid(input, n_batch, n_input, output); -} - -// Same as above but the internal calcualtion is float. -inline void ApplySigmoidFloat(const int16_t* input, int32_t n_batch, - int32_t n_input, int16_t* output) { - PortableApplySigmoidFloat(input, n_batch, n_input, output); -} - -// Apply Tanh to a quantized vector. -// Parameters: -// - integer_bits: the integer bits of the input. -// Currently supports 0, 1, 2, 3, 4, 5, 6. -// - input: batch vector of size n_batch * n_input; 16 bit. -// - n_batch: the number of batches. -// - n_input: the size for input and output. -// - output: the 16 bit output -// The input is in Qm.15-m format and the output is in Q0.15 format. -inline void ApplyTanh(int32_t integer_bits, const int16_t* input, - int32_t n_batch, int32_t n_input, int16_t* output) { - PortableApplyTanh(integer_bits, input, n_batch, n_input, output); -} - -// Apply Tanh to a quantized vector. Tbe internal calculation is in float. -// - Input has 2^(integer_bits) as scale. -// - Output has Q0.15 as scale. -inline void ApplyTanhFloat(const int16_t* input, int32_t n_batch, - int32_t n_input, int32_t integer_bits, - int16_t* output) { - PortableApplyTanhFloat(input, n_batch, n_input, integer_bits, output); -} - -// Element-wise multiplication of two quantized vectors. -// Parameters: -// - input_1: batch vector of size n_batch * n_input; 16 bit. -// - input_2: batch vector of size n_batch * n_input; 16 bit. -// - n_batch: the number of batches. -// - n_input: the size for input and output. -// - shift: the shift needed to produce the output. -// - output: the 16 bit output of size n_batch * n_input. -// Output does not need to be initialized. -inline void CwiseMul(const int16_t* input_1, const int16_t* input_2, - int n_batch, int n_input, int shift, int16_t* output) { - PortableCwiseMul(input_1, input_2, n_batch, n_input, shift, output); -} - -// Element-wise multiplication of two quantized vectors with rescaling. -// Parameters: -// - input_1: batch vector of size n_batch * n_input; 16 bit. -// - input_2: batch vector of size n_batch * n_input; 16 bit. -// - multiplier: the multiplier part of scale. -// - shift: the shift part of scale. -// - n_batch: the number of batches. -// - n_input: the size for input and output. -// - output: the 8 bit output of size n_batch * n_input. -// - output_zp: the zero point of output. -// Output does not need to be initialized. -// Multiplier ("m") and shift ("s") are connected to scale ("s") with s = m * -// 2^(s - 31). -inline void CwiseMul(const int16_t* input_1, const int16_t* input_2, - int32_t multiplier, int32_t shift, int32_t n_batch, - int32_t n_input, int32_t output_zp, int8_t* output) { - PortableCwiseMul(input_1, input_2, multiplier, shift, n_batch, n_input, - output_zp, output); -} - -// Element-wise in-place clipping of a vector. Overloaded for float, int16_t, -// int8_t. Parameters: -// - vector: vector of size v_size. -// - v_size: the size of the vector. -// - clipping_value: the value used for clipping. -inline void CwiseClipping(float* vector, const int v_size, - const float clipping_value) { - PortableCwiseClipping(vector, v_size, clipping_value); -} - -inline void CwiseClipping(int16_t* vector, const int v_size, - const int16_t clipping_value) { - PortableCwiseClipping(vector, v_size, clipping_value); -} - -inline void CwiseClipping(int8_t* vector, const int v_size, - const int8_t clipping_value) { - PortableCwiseClipping(vector, v_size, clipping_value); -} - -// Element-wise saturating addition of two quantized vectors without rescaling. -// Parameters: -// - input_1: batch vector of size n_batch * n_input; 16 bit. -// - input_2: batch vector of size n_batch * n_input; 16 bit. -// - n_batch: the number of batches. -// - n_input: the size for input and output. -// - output: the 8 bit output of size n_batch * n_input. -// Output does not need to be initialized. -inline void CwiseAdd(const int16_t* input_1, const int16_t* input_2, - int n_batch, int n_input, int16_t* output) { - PortableCwiseAdd(input_1, input_2, n_batch, n_input, output); -} - -inline void MeanStddevNormalization(const float* input_vector, - float* output_vector, int v_size, - int n_batch) { - PortableMeanStddevNormalization(input_vector, output_vector, v_size, n_batch); -} - -inline void Sub1Vector(const float* vector, int v_size, float* result) { - PortableSub1Vector(vector, v_size, result); -} - -inline void Sub1Vector(const int16_t* vector, int v_size, int16_t* result) { - PortableSub1Vector(vector, v_size, result); -} - -// Multiply all elements of vector with a scalar. -inline void VectorScalarMultiply(const int8_t* vector, int v_size, float scale, - float* result) { - PortableVectorScalarMultiply(vector, v_size, scale, result); -} - -// Saturate Add with rescale on both inputs. -inline void TwoGateSaturatingAdd(const int8_t* input, int8_t input_zp, - const int8_t* recurrent, int8_t recurrent_zp, - int32_t input_effective_scale_a, - int32_t input_effective_scale_b, - int32_t recurrent_effective_scale_a, - int32_t recurrent_effective_scale_b, - int32_t n_batch, int32_t n_cell, - int16_t* output) { - PortableTwoGateSaturatingAdd( - input, input_zp, recurrent, recurrent_zp, input_effective_scale_a, - input_effective_scale_b, recurrent_effective_scale_a, - recurrent_effective_scale_b, n_batch, n_cell, output); -} - -// Multiplies a matrix by a "batched" vector (i.e. a matrix with a batch -// dimension composed by input vectors independent from each other). The result -// of the multiplication is accumulated to the passed result buffer. -// More specifically, for a matrix M of shape [n, i] and a batched-vector -// of shape [i, batch] it will first compute the product of shape [n, batch]. -// This product will be accumulated to the result buffer. -inline void MatrixBatchVectorMultiplyAccumulate(const float* matrix, int m_rows, - int m_cols, const float* vector, - int n_batch, float* result) { - PortableMatrixBatchVectorMultiplyAccumulate(matrix, m_rows, m_cols, vector, - n_batch, result); -} - -// Same as the function above, but the matrix is a sparse tensor with block -// pattern 1x4. -// This function assumes that m_cols is a multiple of the block size (4 in this -// case) so that there's no incomplete block. -inline void MatrixBatchVectorMultiplyAccumulate( - const int8_t* __restrict__ matrix, const int m_rows, const int m_cols, - const int8_t* __restrict__ vector, const float* scaling_factors, - int n_batch, float* __restrict__ result) { - PortableMatrixBatchVectorMultiplyAccumulate(matrix, m_rows, m_cols, vector, - scaling_factors, n_batch, result); -} - -inline void MatrixBatchVectorMultiplyAccumulate( - const int8_t* __restrict__ matrix, const int m_rows, const int m_cols, - const int8_t* __restrict__ vectors, const float* scaling_factors, - int n_batch, float* __restrict__ result, const float* per_channel_scale, - const int32_t* input_offset, int32_t* scratch, int32_t* row_sums, - bool* compute_row_sums, CpuBackendContext* context) { - PortableMatrixBatchVectorMultiplyAccumulate( - matrix, m_rows, m_cols, vectors, scaling_factors, n_batch, result, - per_channel_scale, input_offset, scratch, row_sums, compute_row_sums, - context); -} - -inline void MatrixBatchVectorMultiplyAccumulate( - const int8_t* __restrict__ matrix, const int m_rows, const int m_cols, - const int8_t* __restrict__ vector, const float* scaling_factors, - int n_batch, int32_t* scratch, float* __restrict__ result, - CpuBackendContext* context) { - PortableMatrixBatchVectorMultiplyAccumulate(matrix, m_rows, m_cols, vector, - scaling_factors, n_batch, result); -} - -// Same as the function above, but the matrix is a sparse tensor with block -// pattern 1x4. -// This function assumes that m_cols is a multiple of the block size (4 in this -// case) so that there's no incomplete block. -inline void SparseMatrixBatchVectorMultiplyAccumulate1x4( - const float* __restrict__ matrix, const int32_t* __restrict__ segments, - const int32_t* __restrict__ indices, int m_rows, int m_cols, - const float* __restrict__ vector, int n_batch, float* __restrict__ result) { - PortableSparseMatrixBatchVectorMultiplyAccumulate1x4( - matrix, segments, indices, m_rows, m_cols, vector, n_batch, result); -} - -// Same as the function above, but the matrix is stored in block compressed -// sparse row format with block pattern 1x16 which consists of two arrays: -// 1. A matrix array stores non-zero blocks of the matrix in row major. -// 2. A ledger array stores nrows groups, one group per row. Each group starts -// with an integer representing the number of non-zero blocks for the -// corresponding row and follows with column indexes of the first element -// of each non-zero block. -// This function assumes that -// 1. m_cols is a multiple of 16 so that all blocks are full blocks. -// 2. m_cols < 254 * 16 so that block index can be represented by uint8. -inline void SparseMatrixBatchVectorMultiplyAccumulate( - const float* __restrict__ matrix, const uint8_t* __restrict__ ledger, - int m_rows, int m_cols, const float* __restrict__ vector, int n_batch, - float* __restrict__ result) { - PortableSparseMatrixBatchVectorMultiplyAccumulate( - matrix, ledger, m_rows, m_cols, vector, n_batch, result); -} - -// Same as the function above, but the matrix is a sparse tensor with block -// pattern 1x16. -// This function assumes that m_cols is a multiple of the block size (16 in this -// case) so that there's no incomplete block. Also, it assumes all offsets of -// input, output and filter are zero. -inline void SparseMatrixBatchVectorMultiplyAccumulate1x16( - const int8_t* __restrict__ matrix, const int32_t* __restrict__ segments, - const int32_t* __restrict__ indices, int m_rows, int m_cols, - const int8_t* __restrict__ vector, const int32_t* __restrict__ bias_vector, - int n_batch, const int32_t input_offset, const int32_t output_multiplier, - const int32_t output_shift, const int32_t output_offset, - const int32_t output_activation_min, const int32_t output_activation_max, - int8_t* __restrict__ result) { - PortableSparseMatrixBatchVectorMultiplyAccumulate1x16( - matrix, segments, indices, m_rows, m_cols, vector, bias_vector, n_batch, - input_offset, output_multiplier, output_shift, output_offset, - output_activation_min, output_activation_max, result); -} - -// Same as the function above, but the matrix is stored in block compressed -// sparse row format with block pattern 1x16 which consists of two arrays: -// 1. A matrix array stores non-zero blocks of the matrix in row major. -// 2. A ledger array stores nrows groups, one group per row. Each group starts -// with an integer representing the number of non-zero blocks for the -// corresponding row followed by column index of the first element of -// each non-zero block. -// This function assumes that -// 1. m_cols is a multiple of 16 so that all blocks are full blocks. -// 2. m_cols < 254 * 16 so that block index can be represented by uint8. -inline void SparseMatrixBatchVectorMultiplyAccumulate( - const int8_t* __restrict__ matrix, const uint8_t* ledger, const int m_rows, - const int m_cols, const int8_t* __restrict__ vectors, - const float* scaling_factors, int n_batch, float* __restrict__ result) { - PortableSparseMatrixBatchVectorMultiplyAccumulate( - matrix, ledger, m_rows, m_cols, vectors, scaling_factors, n_batch, - result); -} - -// Same as the above 8, 8, 8 integer matmul except for the presence of zero -// point and non-accumulative. -// TODO(b/148688698): remove this function by folding zero point calculation in -// prepare() function. -inline void MatrixBatchVectorMultiplyAccumulate( - const int8_t* input, const int32_t* bias, - const int8_t* input_to_gate_weights, int32_t multiplier, int32_t shift, - int32_t n_batch, int32_t n_input, int32_t n_output, int32_t output_zp, - int32_t* scratch, int16_t* output, CpuBackendContext* context) { - PortableMatrixBatchVectorMultiplyAccumulate( - input, bias, input_to_gate_weights, multiplier, shift, n_batch, n_input, - n_output, output_zp, scratch, output, context); -} - -// Same as above but has 16 bit and 8 bit input and 8 bit output. -// Used in projection when hidden is 16bit. -inline void MatrixBatchVectorMultiplyAccumulate( - const int8_t* input, const int32_t* bias, - const int8_t* input_to_gate_weights, int32_t multiplier, int32_t shift, - int32_t n_batch, int32_t n_input, int32_t n_output, int32_t output_zp, - int32_t* scratch, int8_t* output, CpuBackendContext* context) { - PortableMatrixBatchVectorMultiplyAccumulate( - input, bias, input_to_gate_weights, multiplier, shift, n_batch, n_input, - n_output, output_zp, scratch, output, context); -} - -// Same as the function above, but provides separate scaling factor for the -// matrix and the vectors. The scaling factors are multiplied in the -// scaling_factor_scratch buffer. -inline void MatrixBatchVectorMultiplyAccumulate( - const int8_t* __restrict__ matrix, const int m_rows, const int m_cols, - const int8_t* __restrict__ vectors, const float matrix_scaling_factor, - const float* vector_scaling_factors, int n_batch, - float* __restrict__ result, const float* per_channel_scale, - const int32_t* input_offset, int32_t* scratch, int32_t* row_sums, - bool* compute_row_sums, float* scaling_factor_scratch, - CpuBackendContext* context) { - for (int b = 0; b < n_batch; ++b) { - scaling_factor_scratch[b] = - vector_scaling_factors[b] * matrix_scaling_factor; - } - MatrixBatchVectorMultiplyAccumulate(matrix, m_rows, m_cols, vectors, - scaling_factor_scratch, n_batch, result, - per_channel_scale, input_offset, scratch, - row_sums, compute_row_sums, context); -} - -// Multiplies a matrix with a scalar and reduce the result on each row to a -// scalar. -// Parameters: -// - matrix: matrix of size n_row * n_col -// - scalar: the scalar that is multiplied to each element in the matrix -// - n_row: the row count of the matrix -// - n_col: the column count of the matrix -// - output: the 32bit output -// Note: We do not need saturation because the int8 * int8 is safe from overflow -// in (2^31-1) / (2^14) = 131072, which is bigger than the n_row. Non-zero -// initial output value is not exceptionally large. -inline void MatrixScalarMultiplyAccumulate(const int8_t* matrix, int32_t scalar, - int32_t n_row, int32_t n_col, - int32_t* output) { - PortableMatrixScalarMultiplyAccumulate(matrix, scalar, n_row, n_col, output); -} - -// Same as the above 8, 8, 8 integer matmul except for the presence of zero -// point and non-accumulative. -// TODO(b/148688698): remove this function by folding zero point calculation in -// prepare() function. -inline void MatrixBatchVectorMultiply(const int8_t* input, - int32_t input_zeropoint, - const int8_t* input_to_gate_weights, - int32_t input_to_gate_effective_scale_a, - int32_t input_to_gate_effective_scale_b, - int32_t n_batch, int32_t n_input, - int32_t n_cell, int8_t* gate_output, - int8_t gate_output_zp) { - PortableMatrixBatchVectorMultiply( - input, input_zeropoint, input_to_gate_weights, - input_to_gate_effective_scale_a, input_to_gate_effective_scale_b, n_batch, - n_input, n_cell, gate_output, gate_output_zp); -} - -// Same as above but has 16 bit and 8 bit input and 8 bit output. -// Used in projection when hidden is 16bit. -inline void MatrixBatchVectorMultiply(const int16_t* hidden, - const int8_t* hidden_to_output_weights, - int32_t proj_effective_scale_a, - int32_t proj_effective_scale_b, - const int32_t* gate_bias, int32_t n_batch, - int32_t n_hidden, int32_t n_output, - int32_t output_zp, int8_t* proj_output) { - PortableMatrixBatchVectorMultiply(hidden, hidden_to_output_weights, - proj_effective_scale_a, - proj_effective_scale_b, gate_bias, n_batch, - n_hidden, n_output, output_zp, proj_output); -} - -// Cwise product and accumulate of a vector and a batch-vector. Since it's a MAC -// operation, the assumption here is that result array is initialized to valid -// values. -template -inline void VectorBatchVectorCwiseProductAccumulate(const T* vector, int v_size, - const T* batch_vector, - int n_batch, T* result) { - for (int b = 0; b < n_batch; b++) { - VectorVectorCwiseProductAccumulate(vector, batch_vector, v_size, result); - // Update the pointers. - result += v_size; - batch_vector += v_size; - } -} - -// Same as above, but inputs are 16bit integer and output is 16bit integer. -inline void VectorBatchVectorCwiseProductAccumulate( - const int16_t* vector, int v_size, const int16_t* batch_vector, int n_batch, - int32_t multiplier, int shift, int16_t* result) { - PortableVectorBatchVectorCwiseProductAccumulate( - vector, v_size, batch_vector, n_batch, multiplier, shift, result); -} - -// Apply Rectified Linear to elements of a vector. -inline void ApplyReluToVector(const float* vector, int v_size, float* result) { - for (int v = 0; v < v_size; v++) { - result[v] = std::max(0.0f, vector[v]); - } -} - -// Apply Rectified Linear 1 (cap to [-1;1]) to elements of a vector -inline void ApplyRelu1ToVector(const float* vector, int v_size, float* result) { - for (int v = 0; v < v_size; v++) { - result[v] = std::max(-1.0f, std::min(vector[v], 1.0f)); - } -} - -// Apply Rectified Linear 6 (cap to [0;6]) to elements of a vector -inline void ApplyRelu6ToVector(const float* vector, int v_size, float* result) { - for (int v = 0; v < v_size; v++) { - result[v] = std::max(0.0f, std::min(vector[v], 6.0f)); - } -} - -// Apply tanh to elements of a vector -inline void ApplyTanhToVector(const float* vector, int v_size, float* result) { - for (int v = 0; v < v_size; v++) { - result[v] = std::tanh(vector[v]); - } -} - -// Apply signbit to elements of a vector -inline void ApplySignbitToVector(const float* vector, int v_size, - float* result) { - for (int v = 0; v < v_size; v++) { - result[v] = std::signbit(vector[v]); - } -} - -// Apply sigmoid to elements of a vector. -inline void ApplySigmoidToVector(const float* vector, int v_size, - float* result) { - for (int v = 0; v < v_size; v++) { - result[v] = 1.0f / (1.0f + std::exp(-vector[v])); - } -} - -// Apply appropriate activation function to elements of a vector. -inline void ApplyActivationToVector(const float* vector, int v_size, - TfLiteFusedActivation activation, - float* result) { - switch (activation) { - case kTfLiteActNone: - return; - case kTfLiteActRelu: - return ApplyReluToVector(vector, v_size, result); - case kTfLiteActReluN1To1: - return ApplyRelu1ToVector(vector, v_size, result); - case kTfLiteActRelu6: - return ApplyRelu6ToVector(vector, v_size, result); - case kTfLiteActTanh: - return ApplyTanhToVector(vector, v_size, result); - case kTfLiteActSignBit: - return ApplySignbitToVector(vector, v_size, result); - case kTfLiteActSigmoid: - return ApplySigmoidToVector(vector, v_size, result); - } -} - -} // namespace micro_tensor_utils - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_KERNELS_MICRO_TENSOR_UTILS_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/micro_utils.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/micro_utils.h deleted file mode 100644 index e406ac12..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/micro_utils.h +++ /dev/null @@ -1,40 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_MICRO_UTILS_H_ -#define TENSORFLOW_LITE_MICRO_KERNELS_MICRO_UTILS_H_ -namespace tflite { -namespace ops { -namespace micro { - -// Same as gtl::Greater but defined here to reduce dependencies and -// binary size for micro environment. -struct Greater { - template - bool operator()(const T& x, const T& y) const { - return x > y; - } -}; - -struct Less { - template - bool operator()(const T& x, const T& y) const { - return x < y; - } -}; - -} // namespace micro -} // namespace ops -} // namespace tflite -#endif // TENSORFLOW_LITE_MICRO_KERNELS_MICRO_UTILS_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/mirror_pad.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/mirror_pad.cc deleted file mode 100644 index 90d3bd9e..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/mirror_pad.cc +++ /dev/null @@ -1,215 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { -namespace { - -struct OpDataMirrorPad { - int input_dims; - int output_size; - int offset; - int output_dims_num_elements_buffer_index; - int input_dims_num_elements_buffer_index; -}; - -// Helper method that fills the left and right pads. -template -inline void GetPadding(const T* data, int offset, int64_t* left_pad, - int64_t* right_pad) { - *left_pad = static_cast(*(data + offset * 2)); - *right_pad = static_cast(*(data + offset * 2 + 1)); -} - -// Given dimension index and the left/right padding. -// Returns the corresponding dimension in the input array. -inline int GetInputDimension(int padded_dimension, int left_pad, int right_pad, - int input_dim_size, int offset) { - if (padded_dimension < left_pad) { - const int original_ind = left_pad + offset - 1; - return original_ind - (std::min(padded_dimension, original_ind - offset)); - } - padded_dimension -= left_pad; - if (padded_dimension >= input_dim_size) { - padded_dimension -= input_dim_size; - const int original_ind = input_dim_size - (1 + offset); - return original_ind - std::min(padded_dimension, original_ind); - } - return padded_dimension; -} - -// Given and index in output array, returns the index of the value -// in input array. -int GetFlatIndex(int index, int num_dims, - const TfLiteEvalTensor* padding_matrix, - const TfLiteIntArray* input_dims, - int* output_dims_num_elements, int* input_dims_num_elements, - const int offset) { - int flat_index = 0; - int64_t left_pad = 0, right_pad = 0, dimension_index, index_in_input; - - for (int i = 0; i < num_dims; ++i) { - switch (padding_matrix->type) { - case kTfLiteInt32: - GetPadding(padding_matrix->data.i32, i, &left_pad, &right_pad); - break; - case kTfLiteInt64: - GetPadding(padding_matrix->data.i64, i, &left_pad, &right_pad); - break; - default: - break; - } - dimension_index = index / output_dims_num_elements[i]; - - index_in_input = GetInputDimension(dimension_index, left_pad, right_pad, - input_dims->data[i], offset); - - flat_index += index_in_input * (input_dims_num_elements)[i]; - index %= output_dims_num_elements[i]; - } - - return flat_index; -} - -template -void MirrorPad(const TfLiteEvalTensor* padding_matrix, - const TfLiteIntArray* input_dims, int* output_dims_num_elements, - int* input_dims_num_elements, const T* input_data, - T* output_data, const int offset, const int num_dims, - const int output_size) { - for (int i = 0; i < output_size; ++i) { - output_data[i] = input_data[GetFlatIndex( - i, num_dims, padding_matrix, input_dims, output_dims_num_elements, - input_dims_num_elements, offset)]; - } -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - TfLiteStatus status = kTfLiteOk; - const OpDataMirrorPad* data = - static_cast(node->user_data); - - const TfLiteEvalTensor* input_tensor = - tflite::micro::GetEvalInput(context, node, 0); - const TfLiteEvalTensor* padding_matrix = - tflite::micro::GetEvalInput(context, node, 1); - - TfLiteEvalTensor* output_tensor = - tflite::micro::GetEvalOutput(context, node, 0); - const int input_dims = data->input_dims; - const int output_size = data->output_size; - - int* input_dims_num_elements = (int*)context->GetScratchBuffer( - context, data->input_dims_num_elements_buffer_index); - int* output_dims_num_elements = (int*)context->GetScratchBuffer( - context, data->output_dims_num_elements_buffer_index); - - for (int i = 0; i < input_dims; i++) { - output_dims_num_elements[i] = 1; - input_dims_num_elements[i] = 1; - } - - for (int i = input_dims - 2; i >= 0; i--) { - output_dims_num_elements[i] = - output_dims_num_elements[i + 1] * output_tensor->dims->data[i + 1]; - - input_dims_num_elements[i] = - input_dims_num_elements[i + 1] * input_tensor->dims->data[i + 1]; - } - - switch (output_tensor->type) { - case kTfLiteFloat32: { - MirrorPad(padding_matrix, input_tensor->dims, output_dims_num_elements, - input_dims_num_elements, - tflite::micro::GetTensorData(input_tensor), - tflite::micro::GetTensorData(output_tensor), - data->offset, input_dims, output_size); - break; - } - case kTfLiteInt8: { - MirrorPad(padding_matrix, input_tensor->dims, output_dims_num_elements, - input_dims_num_elements, - tflite::micro::GetTensorData(input_tensor), - tflite::micro::GetTensorData(output_tensor), - data->offset, input_dims, output_size); - break; - } - default: - status = kTfLiteError; - break; - } - -#undef TF_LITE_MIRROR_PAD - - return status; -} - -void* Init(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(OpDataMirrorPad)); -} - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - - TFLITE_DCHECK(node->user_data != nullptr); - OpDataMirrorPad* data = static_cast(node->user_data); - - TfLiteTensor* input_tensor = micro_context->AllocateTempInputTensor(node, 0); - TfLiteTensor* padding_matrix = - micro_context->AllocateTempInputTensor(node, 1); - TfLiteTensor* output_tensor = - micro_context->AllocateTempOutputTensor(node, 0); - - TF_LITE_ENSURE_EQ(context, NumDimensions(padding_matrix), 2); - TF_LITE_ENSURE_EQ(context, SizeOfDimension(padding_matrix, 0), - NumDimensions(input_tensor)); - auto* params = - reinterpret_cast(node->builtin_data); - if (params == nullptr) { - return kTfLiteError; - } - - data->offset = - params->mode != TfLiteMirrorPaddingMode::kTfLiteMirrorPaddingReflect ? 0 - : 1; - data->input_dims = NumDimensions(input_tensor); - data->output_size = NumElements(output_tensor); - - TF_LITE_ENSURE_STATUS(context->RequestScratchBufferInArena( - context, data->input_dims * sizeof(int), - &data->output_dims_num_elements_buffer_index)); - TF_LITE_ENSURE_STATUS(context->RequestScratchBufferInArena( - context, data->input_dims * sizeof(int), - &data->input_dims_num_elements_buffer_index)); - - micro_context->DeallocateTempTfLiteTensor(input_tensor); - micro_context->DeallocateTempTfLiteTensor(padding_matrix); - micro_context->DeallocateTempTfLiteTensor(output_tensor); - return kTfLiteOk; -} - -} // namespace - -TfLiteRegistration Register_MIRROR_PAD() { - return tflite::micro::RegisterOp(Init, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/mul.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/mul.cc deleted file mode 100644 index 59f006b0..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/mul.cc +++ /dev/null @@ -1,67 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/kernels/mul.h" - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/integer_ops/mul.h" -#include "tensorflow/lite/kernels/internal/reference/mul.h" -#include "tensorflow/lite/kernels/internal/reference/process_broadcast_shapes.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/memory_helpers.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" - -namespace tflite { - -TfLiteStatus MulEval(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->builtin_data != nullptr); - auto* params = reinterpret_cast(node->builtin_data); - - TFLITE_DCHECK(node->user_data != nullptr); - const OpDataMul* data = static_cast(node->user_data); - - const TfLiteEvalTensor* input1 = - tflite::micro::GetEvalInput(context, node, kMulInput1Tensor); - const TfLiteEvalTensor* input2 = - tflite::micro::GetEvalInput(context, node, kMulInput2Tensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kMulOutputTensor); - - switch (input1->type) { - case kTfLiteInt8: - case kTfLiteInt32: - EvalMulQuantizedReference(context, node, data, input1, input2, output); - break; - case kTfLiteFloat32: - EvalMulFloatReference(context, node, params, data, input1, input2, - output); - break; - default: - MicroPrintf("Type %s (%d) not supported.", - TfLiteTypeGetName(input1->type), input1->type); - return kTfLiteError; - } - - return kTfLiteOk; -} - -TfLiteRegistration Register_MUL() { - return tflite::micro::RegisterOp(MulInit, MulPrepare, MulEval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/mul.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/mul.h deleted file mode 100644 index e6d1a9b1..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/mul.h +++ /dev/null @@ -1,65 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_MUL_H_ -#define TENSORFLOW_LITE_MICRO_KERNELS_MUL_H_ - -#include - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" - -namespace tflite { - -extern const int kMulInput1Tensor; -extern const int kMulInput2Tensor; -extern const int kMulOutputTensor; - -struct OpDataMul { - int32_t input1_zero_point; - int32_t input2_zero_point; - - int32_t output_activation_min; - int32_t output_activation_max; - int32_t output_zero_point; - int32_t output_multiplier; - int output_shift; - - float output_activation_min_f32; - float output_activation_max_f32; -}; - -void* MulInit(TfLiteContext* context, const char* buffer, size_t length); - -TfLiteStatus CalculateOpDataMul(TfLiteContext* context, TfLiteNode* node, - TfLiteMulParams* params, OpDataMul* data); - -TfLiteStatus MulPrepare(TfLiteContext* context, TfLiteNode* node); - -void EvalMulQuantizedReference(TfLiteContext* context, TfLiteNode* node, - const OpDataMul* data, - const TfLiteEvalTensor* input1, - const TfLiteEvalTensor* input2, - TfLiteEvalTensor* output); - -void EvalMulFloatReference(TfLiteContext* context, TfLiteNode* node, - TfLiteMulParams* params, const OpDataMul* data, - const TfLiteEvalTensor* input1, - const TfLiteEvalTensor* input2, - TfLiteEvalTensor* output); - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_KERNELS_MUL_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/mul_common.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/mul_common.cc deleted file mode 100644 index 6d19ac7a..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/mul_common.cc +++ /dev/null @@ -1,184 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/integer_ops/mul.h" -#include "tensorflow/lite/kernels/internal/reference/mul.h" -#include "tensorflow/lite/kernels/internal/reference/process_broadcast_shapes.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/mul.h" -#include "tensorflow/lite/micro/memory_helpers.h" - -namespace tflite { - -const int kMulInput1Tensor = 0; -const int kMulInput2Tensor = 1; -const int kMulOutputTensor = 0; - -void* MulInit(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(OpDataMul)); -} - -TfLiteStatus CalculateOpDataMul(TfLiteContext* context, TfLiteNode* node, - TfLiteMulParams* params, OpDataMul* data) { - MicroContext* micro_context = GetMicroContext(context); - - TfLiteTensor* input1 = - micro_context->AllocateTempInputTensor(node, kMulInput1Tensor); - TF_LITE_ENSURE(context, input1 != nullptr); - TfLiteTensor* input2 = - micro_context->AllocateTempInputTensor(node, kMulInput2Tensor); - TF_LITE_ENSURE(context, input2 != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kMulOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - - TF_LITE_ENSURE_EQ(context, NumInputs(node), 2); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - - TF_LITE_ENSURE_TYPES_EQ(context, input1->type, input2->type); - - if (output->type == kTfLiteInt8) { - TF_LITE_ENSURE_STATUS(CalculateActivationRangeQuantized( - context, params->activation, output, &data->output_activation_min, - &data->output_activation_max)); - - double real_multiplier = static_cast(input1->params.scale) * - static_cast(input2->params.scale) / - static_cast(output->params.scale); - QuantizeMultiplier(real_multiplier, &data->output_multiplier, - &data->output_shift); - - data->input1_zero_point = input1->params.zero_point; - data->input2_zero_point = input2->params.zero_point; - data->output_zero_point = output->params.zero_point; - } else if (output->type == kTfLiteInt32) { - CalculateActivationRange(params->activation, &data->output_activation_min, - &data->output_activation_max); - } else { - CalculateActivationRange(params->activation, - &data->output_activation_min_f32, - &data->output_activation_max_f32); - } - - micro_context->DeallocateTempTfLiteTensor(input1); - micro_context->DeallocateTempTfLiteTensor(input2); - micro_context->DeallocateTempTfLiteTensor(output); - return kTfLiteOk; -} - -TfLiteStatus MulPrepare(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->builtin_data != nullptr); - auto* params = reinterpret_cast(node->builtin_data); - - TFLITE_DCHECK(node->user_data != nullptr); - OpDataMul* data = static_cast(node->user_data); - - return CalculateOpDataMul(context, node, params, data); -} - -void EvalMulQuantizedReference(TfLiteContext* context, TfLiteNode* node, - const OpDataMul* data, - const TfLiteEvalTensor* input1, - const TfLiteEvalTensor* input2, - TfLiteEvalTensor* output) { - tflite::ArithmeticParams op_params = {}; - op_params.quantized_activation_min = data->output_activation_min; - op_params.quantized_activation_max = data->output_activation_max; - op_params.float_activation_max = data->output_activation_max_f32; - op_params.input1_offset = -data->input1_zero_point; - op_params.input2_offset = -data->input2_zero_point; - op_params.output_offset = data->output_zero_point; - op_params.output_multiplier = data->output_multiplier; - op_params.output_shift = data->output_shift; - - bool need_broadcast = reference_ops::ProcessBroadcastShapes( - tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorShape(input2), &op_params); - - if (input1->type == kTfLiteInt8) { - if (need_broadcast) { - reference_integer_ops::BroadcastMul4DSlow( - op_params, tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } else { - reference_integer_ops::Mul(op_params, - tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } - } else if (input1->type == kTfLiteInt32) { - if (need_broadcast) { - reference_ops::BroadcastMul4DSlow( - op_params, tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } else { - reference_ops::Mul(op_params, tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } - } -} - -void EvalMulFloatReference(TfLiteContext* context, TfLiteNode* node, - TfLiteMulParams* params, const OpDataMul* data, - const TfLiteEvalTensor* input1, - const TfLiteEvalTensor* input2, - TfLiteEvalTensor* output) { - tflite::ArithmeticParams op_params = {}; - op_params.float_activation_min = data->output_activation_min_f32; - op_params.float_activation_max = data->output_activation_max_f32; - - bool need_broadcast = reference_ops::ProcessBroadcastShapes( - tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorShape(input2), &op_params); - - if (need_broadcast) { - reference_ops::BroadcastMul4DSlow( - op_params, tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } else { - reference_ops::Mul(op_params, tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/neg.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/neg.cc deleted file mode 100644 index 59dd8cb8..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/neg.cc +++ /dev/null @@ -1,59 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/kernels/internal/reference/neg.h" - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { -namespace ops { -namespace micro { -namespace neg { - -constexpr int kInputTensor = 0; -constexpr int kOutputTensor = 0; - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kInputTensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - switch (input->type) { - // TODO(wangtz): handle for kTfLiteInt8 - case kTfLiteFloat32: - reference_ops::Negate(tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - default: - TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.", - TfLiteTypeGetName(input->type), input->type); - return kTfLiteError; - } - return kTfLiteOk; -} - -} // namespace neg - -TfLiteRegistration Register_NEG() { - return tflite::micro::RegisterOp(nullptr, nullptr, neg::Eval); -} - -} // namespace micro -} // namespace ops -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/pack.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/pack.cc deleted file mode 100644 index 56f3b96e..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/pack.cc +++ /dev/null @@ -1,116 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { -namespace ops { -namespace micro { -namespace pack { -namespace { - -constexpr int kOutputTensor = 0; - -template -TfLiteStatus PackImpl(TfLiteContext* context, TfLiteNode* node, - TfLiteEvalTensor* output, int values_count, int axis) { - const TfLiteEvalTensor* input0 = - tflite::micro::GetEvalInput(context, node, 0); - - const int dimensions = output->dims->size; - const TfLiteIntArray* input_dims = input0->dims; - const TfLiteIntArray* output_dims = output->dims; - - if (axis < 0) { - axis += dimensions; - } - - int outer_size = 1; - for (int i = 0; i < axis; ++i) { - outer_size *= output_dims->data[i]; - } - int copy_size = 1; - for (int i = axis + 1; i < dimensions; ++i) { - copy_size *= output_dims->data[i]; - } - int input_size = 1; - for (int i = 0; i < input_dims->size; ++i) { - input_size *= input_dims->data[i]; - } - TFLITE_DCHECK_EQ(input_size, copy_size * outer_size); - - T* output_data = tflite::micro::GetTensorData(output); - - for (int i = 0; i < values_count; ++i) { - const TfLiteEvalTensor* t = tflite::micro::GetEvalInput(context, node, i); - const T* input_data = tflite::micro::GetTensorData(t); - for (int k = 0; k < outer_size; ++k) { - const T* input_ptr = input_data + copy_size * k; - int loc = k * values_count * copy_size + i * copy_size; - T* output_ptr = output_data + loc; - for (int j = 0; j < copy_size; ++j) output_ptr[j] = input_ptr[j]; - } - } - - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const TfLitePackParams* data = - reinterpret_cast(node->builtin_data); - - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - - switch (output->type) { - case kTfLiteFloat32: { - return PackImpl(context, node, output, data->values_count, - data->axis); - } - case kTfLiteInt8: { - return PackImpl(context, node, output, data->values_count, - data->axis); - } - case kTfLiteInt32: { - return PackImpl(context, node, output, data->values_count, - data->axis); - } - case kTfLiteInt64: { - return PackImpl(context, node, output, data->values_count, - data->axis); - } - default: { - TF_LITE_KERNEL_LOG(context, "Type '%s' is not supported by pack.", - TfLiteTypeGetName(output->type)); - return kTfLiteError; - } - } - - return kTfLiteOk; -} - -} // namespace -} // namespace pack - -TfLiteRegistration Register_PACK() { - return tflite::micro::RegisterOp(nullptr, nullptr, pack::Eval); -} - -} // namespace micro -} // namespace ops -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/pad.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/pad.cc deleted file mode 100644 index b645f983..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/pad.cc +++ /dev/null @@ -1,236 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/kernels/internal/reference/pad.h" - -#include - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/portable_tensor.h" -#include "tensorflow/lite/kernels/internal/types.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { -namespace ops { -namespace micro { -namespace pad { -namespace { - -struct OpData { - PadParams params; - int32_t output_zero_point; -}; - -} // namespace - -void* Init(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(OpData)); -} - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - - TFLITE_DCHECK(node->user_data != nullptr); - OpData* data = static_cast(node->user_data); - - TF_LITE_ENSURE(context, NumInputs(node) == 2 || NumInputs(node) == 3); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, /*index=*/0); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* paddings = - micro_context->AllocateTempInputTensor(node, /*index=*/1); - TF_LITE_ENSURE(context, paddings != nullptr); - TfLiteTensor* constant_values = - NumInputs(node) == 3 - ? micro_context->AllocateTempInputTensor(node, /*index=*/2) - : nullptr; - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, /*index=*/0); - TF_LITE_ENSURE(context, output != nullptr); - - TF_LITE_ENSURE_EQ(context, input->type, output->type); - - // Current implementations rely on the inputs being <= 4D. - TF_LITE_ENSURE(context, NumDimensions(input) <= - reference_ops::PadKernelMaxDimensionCount()); - - if (constant_values != nullptr) { - TF_LITE_ENSURE_EQ(context, input->type, constant_values->type); - // Ensure that constant_values is a scalar. - TF_LITE_ENSURE_EQ(context, NumElements(constant_values), 1); - } - - // There must be a pair of paddings for each output dimension. - TF_LITE_ENSURE_EQ(context, GetTensorShape(paddings).FlatSize(), - output->dims->size * 2); - - // On Micro, outputs must be properly sized by the converter. - // NOTE: This data is only available because the paddings buffer is stored in - // the flatbuffer: - TF_LITE_ENSURE(context, IsConstantTensor(paddings)); - const int32_t* paddings_data = GetTensorData(paddings); - for (int i = 0; i < output->dims->size; i++) { - int output_dim = output->dims->data[i]; - int expected_dim = - input->dims->data[i] + paddings_data[i * 2] + paddings_data[i * 2 + 1]; - TF_LITE_ENSURE_EQ(context, output_dim, expected_dim); - } - - // Calculate OpData: - data->params.resizing_category = ResizingCategory::kGenericResize; - const int paddings_total = GetTensorShape(paddings).FlatSize(); - if (paddings_total == 8 && (paddings_data[0] == 0 && paddings_data[1] == 0) && - (paddings_data[6] == 0 && paddings_data[7] == 0)) { - data->params.resizing_category = ResizingCategory::kImageStyle; - } - - const int num_input_dimensions = NumDimensions(input); - data->params.left_padding_count = num_input_dimensions; - data->params.right_padding_count = num_input_dimensions; - - for (int idx = num_input_dimensions - 1; idx >= 0; --idx) { - data->params.left_padding[idx] = paddings_data[idx * 2]; - data->params.right_padding[idx] = paddings_data[idx * 2 + 1]; - } - - if (input->type == kTfLiteInt8) { - if (constant_values == nullptr) { - // Quantized Pad requires that 0 is represented in the quantized - // range. - TF_LITE_ENSURE(context, output->params.zero_point >= - std::numeric_limits::min()); - TF_LITE_ENSURE(context, output->params.zero_point <= - std::numeric_limits::max()); - } else { - // Quantized Pad requires that 'constant_values' is represented in the - // same quantized range as the input and output tensors. - TF_LITE_ENSURE_EQ(context, output->params.zero_point, - constant_values->params.zero_point); - TF_LITE_ENSURE_EQ(context, static_cast(output->params.scale), - static_cast(constant_values->params.scale)); - } - data->output_zero_point = output->params.zero_point; - } - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(paddings); - if (constant_values != nullptr) { - micro_context->DeallocateTempTfLiteTensor(constant_values); - } - micro_context->DeallocateTempTfLiteTensor(output); - - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - const OpData* data = static_cast(node->user_data); - - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, /*index=*/0); - const TfLiteEvalTensor* constant_values = - NumInputs(node) == 3 - ? tflite::micro::GetEvalInput(context, node, /*index=*/2) - : nullptr; - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, /*index=*/0); - - switch (input->type) { - case kTfLiteFloat32: { - float pad_value = - constant_values == nullptr - ? 0.f - : *tflite::micro::GetTensorData(constant_values); - if (data->params.resizing_category == ResizingCategory::kImageStyle) { - reference_ops::PadImageStyle( - data->params, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), &pad_value, - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } else { - reference_ops::Pad(data->params, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - &pad_value, tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } - } break; - case kTfLiteInt8: { - int8_t pad_value; - if (constant_values == nullptr) { - pad_value = static_cast(data->output_zero_point); - } else { - pad_value = *tflite::micro::GetTensorData(constant_values); - } - if (data->params.resizing_category == ResizingCategory::kImageStyle) { - reference_ops::PadImageStyle( - data->params, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), &pad_value, - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } else { - reference_ops::Pad(data->params, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - &pad_value, tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } - } break; - case kTfLiteInt16: { - int16_t pad_value = - constant_values == nullptr - ? 0 - : *tflite::micro::GetTensorData(constant_values); - reference_ops::Pad(data->params, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - &pad_value, tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } break; - case kTfLiteInt32: { - int32_t pad_value = - constant_values == nullptr - ? 0 - : *tflite::micro::GetTensorData(constant_values); - reference_ops::Pad(data->params, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - &pad_value, tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } break; - default: - - TF_LITE_KERNEL_LOG(context, "Type %s not currently supported by Pad.", - TfLiteTypeGetName(input->type)); - return kTfLiteError; - } - return kTfLiteOk; -} - -} // namespace pad - -TfLiteRegistration Register_PAD() { - return tflite::micro::RegisterOp(pad::Init, pad::Prepare, pad::Eval); -} - -// Also register Pad as PadV2. -TfLiteRegistration Register_PADV2() { - return tflite::micro::RegisterOp(pad::Init, pad::Prepare, pad::Eval); -} - -} // namespace micro -} // namespace ops -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/pooling.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/pooling.cc deleted file mode 100644 index a2ef8b62..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/pooling.cc +++ /dev/null @@ -1,98 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/kernels/internal/reference/pooling.h" - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/pooling.h" - -namespace tflite { - -namespace { - -TfLiteStatus AverageEval(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->builtin_data != nullptr); - auto* params = reinterpret_cast(node->builtin_data); - - TFLITE_DCHECK(node->user_data != nullptr); - const OpDataPooling* data = - static_cast(node->user_data); - - const TfLiteEvalTensor* input = - micro::GetEvalInput(context, node, kPoolingInputTensor); - TfLiteEvalTensor* output = - micro::GetEvalOutput(context, node, kPoolingOutputTensor); - - // Inputs and outputs share the same type, guaranteed by the converter. - switch (input->type) { - case kTfLiteFloat32: - AveragePoolingEvalFloat(context, node, params, data, input, output); - break; - case kTfLiteInt8: - AveragePoolingEvalQuantized(context, node, params, data, input, output); - break; - default: - TF_LITE_KERNEL_LOG(context, "Input type %s is not currently supported", - TfLiteTypeGetName(input->type)); - return kTfLiteError; - } - return kTfLiteOk; -} - -TfLiteStatus MaxEval(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->builtin_data != nullptr); - auto* params = reinterpret_cast(node->builtin_data); - - TFLITE_DCHECK(node->user_data != nullptr); - const OpDataPooling* data = - static_cast(node->user_data); - - const TfLiteEvalTensor* input = - micro::GetEvalInput(context, node, kPoolingInputTensor); - TfLiteEvalTensor* output = - micro::GetEvalOutput(context, node, kPoolingOutputTensor); - - switch (input->type) { - case kTfLiteFloat32: - MaxPoolingEvalFloat(context, node, params, data, input, output); - break; - case kTfLiteInt8: - MaxPoolingEvalQuantized(context, node, params, data, input, output); - break; - default: - TF_LITE_KERNEL_LOG(context, "Type %s not currently supported.", - TfLiteTypeGetName(input->type)); - return kTfLiteError; - } - return kTfLiteOk; -} - -void* Init(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(OpDataPooling)); -} - -} // namespace - -TfLiteRegistration Register_AVERAGE_POOL_2D() { - return tflite::micro::RegisterOp(Init, PoolingPrepare, AverageEval); -} - -TfLiteRegistration Register_MAX_POOL_2D() { - return tflite::micro::RegisterOp(Init, PoolingPrepare, MaxEval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/pooling.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/pooling.h deleted file mode 100644 index a80f1f59..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/pooling.h +++ /dev/null @@ -1,71 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_POOLING_H_ -#define TENSORFLOW_LITE_MICRO_KERNELS_POOLING_H_ - -#include - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" - -namespace tflite { - -extern const int kPoolingInputTensor; -extern const int kPoolingOutputTensor; - -struct OpDataPooling { - TfLitePaddingValues padding; - int32_t activation_min; - int32_t activation_max; - float activation_min_f32; - float activation_max_f32; -}; - -TfLiteStatus CalculateOpDataPooling(const TfLiteContext* context, - const TfLitePoolParams* params, - const TfLiteTensor* input, - const TfLiteTensor* output, - OpDataPooling* data); - -TfLiteStatus PoolingPrepare(TfLiteContext* context, TfLiteNode* node); - -void AveragePoolingEvalFloat(const TfLiteContext* context, - const TfLiteNode* node, - const TfLitePoolParams* params, - const OpDataPooling* data, - const TfLiteEvalTensor* input, - TfLiteEvalTensor* output); - -void AveragePoolingEvalQuantized(TfLiteContext* context, const TfLiteNode* node, - const TfLitePoolParams* params, - const OpDataPooling* data, - const TfLiteEvalTensor* input, - TfLiteEvalTensor* output); - -void MaxPoolingEvalFloat(TfLiteContext* context, TfLiteNode* node, - TfLitePoolParams* params, const OpDataPooling* data, - const TfLiteEvalTensor* input, - TfLiteEvalTensor* output); - -void MaxPoolingEvalQuantized(TfLiteContext* context, TfLiteNode* node, - TfLitePoolParams* params, - const OpDataPooling* data, - const TfLiteEvalTensor* input, - TfLiteEvalTensor* output); - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_KERNELS_POOLING_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/pooling_common.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/pooling_common.cc deleted file mode 100644 index ddc18f0b..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/pooling_common.cc +++ /dev/null @@ -1,170 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/kernels/internal/reference/integer_ops/pooling.h" -#include "tensorflow/lite/kernels/internal/reference/pooling.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/padding.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/pooling.h" - -namespace tflite { - -const int kPoolingInputTensor = 0; -const int kPoolingOutputTensor = 0; - -TfLiteStatus CalculateOpDataPooling(const TfLiteContext* context, - const TfLitePoolParams* params, - const TfLiteTensor* input, - const TfLiteTensor* output, - OpDataPooling* data) { - // input: batch, height, width, channel - int height = SizeOfDimension(input, 1); - int width = SizeOfDimension(input, 2); - - int out_height, out_width; - - data->padding = ComputePaddingHeightWidth( - params->stride_height, params->stride_width, - /*dilation_rate_height=*/1, - /*dilation_rate_width=*/1, height, width, params->filter_height, - params->filter_width, params->padding, &out_height, &out_width); - - return kTfLiteOk; -} - -TfLiteStatus PoolingPrepare(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->builtin_data != nullptr); - auto* params = reinterpret_cast(node->builtin_data); - - TFLITE_DCHECK(node->user_data != nullptr); - OpDataPooling* data = static_cast(node->user_data); - - MicroContext* micro_context = GetMicroContext(context); - - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kPoolingInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kPoolingOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - - TF_LITE_ENSURE_STATUS( - CalculateOpDataPooling(context, params, input, output, data)); - - if (input->type == kTfLiteFloat32) { - CalculateActivationRange(params->activation, &data->activation_min_f32, - &data->activation_max_f32); - } else if (input->type == kTfLiteInt8) { - CalculateActivationRangeQuantized(context, params->activation, output, - &data->activation_min, - &data->activation_max); - } - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(output); - - return kTfLiteOk; -} - -void AveragePoolingEvalFloat(const TfLiteContext* context, - const TfLiteNode* node, - const TfLitePoolParams* params, - const OpDataPooling* data, - const TfLiteEvalTensor* input, - TfLiteEvalTensor* output) { - PoolParams op_params; - op_params.stride_height = params->stride_height; - op_params.stride_width = params->stride_width; - op_params.filter_height = params->filter_height; - op_params.filter_width = params->filter_width; - op_params.padding_values.height = data->padding.height; - op_params.padding_values.width = data->padding.width; - op_params.float_activation_min = data->activation_min_f32; - op_params.float_activation_max = data->activation_max_f32; - reference_ops::AveragePool(op_params, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); -} - -void AveragePoolingEvalQuantized(TfLiteContext* context, const TfLiteNode* node, - const TfLitePoolParams* params, - const OpDataPooling* data, - const TfLiteEvalTensor* input, - TfLiteEvalTensor* output) { - TFLITE_DCHECK(input->type == kTfLiteInt8); - - PoolParams op_params; - op_params.stride_height = params->stride_height; - op_params.stride_width = params->stride_width; - op_params.filter_height = params->filter_height; - op_params.filter_width = params->filter_width; - op_params.padding_values.height = data->padding.height; - op_params.padding_values.width = data->padding.width; - op_params.quantized_activation_min = data->activation_min; - op_params.quantized_activation_max = data->activation_max; - - reference_integer_ops::AveragePool( - op_params, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); -} - -void MaxPoolingEvalFloat(TfLiteContext* context, TfLiteNode* node, - TfLitePoolParams* params, const OpDataPooling* data, - const TfLiteEvalTensor* input, - TfLiteEvalTensor* output) { - tflite::PoolParams op_params; - op_params.stride_height = params->stride_height; - op_params.stride_width = params->stride_width; - op_params.filter_height = params->filter_height; - op_params.filter_width = params->filter_width; - op_params.padding_values.height = data->padding.height; - op_params.padding_values.width = data->padding.width; - op_params.float_activation_min = data->activation_min_f32; - op_params.float_activation_max = data->activation_max_f32; - reference_ops::MaxPool(op_params, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); -} - -void MaxPoolingEvalQuantized(TfLiteContext* context, TfLiteNode* node, - TfLitePoolParams* params, - const OpDataPooling* data, - const TfLiteEvalTensor* input, - TfLiteEvalTensor* output) { - tflite::PoolParams op_params; - op_params.stride_height = params->stride_height; - op_params.stride_width = params->stride_width; - op_params.filter_height = params->filter_height; - op_params.filter_width = params->filter_width; - op_params.padding_values.height = data->padding.height; - op_params.padding_values.width = data->padding.width; - op_params.quantized_activation_min = data->activation_min; - op_params.quantized_activation_max = data->activation_max; - - reference_integer_ops::MaxPool(op_params, - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/prelu.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/prelu.cc deleted file mode 100644 index 54cc0e02..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/prelu.cc +++ /dev/null @@ -1,75 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/kernels/internal/reference/prelu.h" - -#include - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/prelu.h" - -namespace tflite { - -void* PreluInit(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(PreluParams)); -} - -TfLiteStatus PreluEval(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - const PreluParams& params = - *(static_cast(node->user_data)); - - const TfLiteEvalTensor* input = tflite::micro::GetEvalInput(context, node, 0); - const TfLiteEvalTensor* alpha = tflite::micro::GetEvalInput(context, node, 1); - TfLiteEvalTensor* output = tflite::micro::GetEvalOutput(context, node, 0); - - switch (input->type) { - case kTfLiteFloat32: { - BroadcastPrelu4DSlowFloat(tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(alpha), - tflite::micro::GetTensorData(alpha), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - return kTfLiteOk; - } break; - case kTfLiteInt8: { - reference_ops::BroadcastPrelu4DSlow( - params, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(alpha), - tflite::micro::GetTensorData(alpha), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - return kTfLiteOk; - } break; - default: - TF_LITE_KERNEL_LOG( - context, "Only float32 and uint8_t are supported currently, got %d.", - TfLiteTypeGetName(input->type)); - return kTfLiteError; - } -} - -TfLiteRegistration Register_PRELU() { - return tflite::micro::RegisterOp(PreluInit, PreluPrepare, PreluEval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/prelu.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/prelu.h deleted file mode 100644 index 571d1e88..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/prelu.h +++ /dev/null @@ -1,39 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_PRELU_H_ -#define TENSORFLOW_LITE_MICRO_KERNELS_PRELU_H_ - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -TfLiteStatus CalculatePreluParams(const TfLiteTensor* input, - const TfLiteTensor* alpha, - TfLiteTensor* output, PreluParams* params); - -void BroadcastPrelu4DSlowFloat(const RuntimeShape& unextended_input1_shape, - const float* input1_data, - const RuntimeShape& unextended_input2_shape, - const float* input2_data, - const RuntimeShape& unextended_output_shape, - float* output_data); - -TfLiteStatus PreluPrepare(TfLiteContext* context, TfLiteNode* node); - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_KERNELS_PRELU_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/prelu_common.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/prelu_common.cc deleted file mode 100644 index 1a89cadf..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/prelu_common.cc +++ /dev/null @@ -1,105 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/prelu.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/prelu.h" - -namespace tflite { - -TfLiteStatus CalculatePreluParams(const TfLiteTensor* input, - const TfLiteTensor* alpha, - TfLiteTensor* output, PreluParams* params) { - if (output->type == kTfLiteInt8 || output->type == kTfLiteInt16) { - double real_multiplier_1 = static_cast(input->params.scale) / - static_cast(output->params.scale); - double real_multiplier_2 = static_cast(input->params.scale) * - static_cast(alpha->params.scale) / - static_cast(output->params.scale); - QuantizeMultiplier(real_multiplier_1, ¶ms->output_multiplier_1, - ¶ms->output_shift_1); - QuantizeMultiplier(real_multiplier_2, ¶ms->output_multiplier_2, - ¶ms->output_shift_2); - - params->input_offset = -input->params.zero_point; - params->alpha_offset = -alpha->params.zero_point; - params->output_offset = output->params.zero_point; - } - - return kTfLiteOk; -} - -void BroadcastPrelu4DSlowFloat(const RuntimeShape& unextended_input1_shape, - const float* input1_data, - const RuntimeShape& unextended_input2_shape, - const float* input2_data, - const RuntimeShape& unextended_output_shape, - float* output_data) { - TFLITE_DCHECK_LE(unextended_input1_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(unextended_input2_shape.DimensionsCount(), 4); - TFLITE_DCHECK_LE(unextended_output_shape.DimensionsCount(), 4); - const RuntimeShape output_shape = - RuntimeShape::ExtendedShape(4, unextended_output_shape); - - NdArrayDesc<4> desc1; - NdArrayDesc<4> desc2; - NdArrayDescsForElementwiseBroadcast(unextended_input1_shape, - unextended_input2_shape, &desc1, &desc2); - - for (int b = 0; b < output_shape.Dims(0); ++b) { - for (int y = 0; y < output_shape.Dims(1); ++y) { - for (int x = 0; x < output_shape.Dims(2); ++x) { - for (int c = 0; c < output_shape.Dims(3); ++c) { - auto out_idx = Offset(output_shape, b, y, x, c); - auto in1_idx = SubscriptToIndex(desc1, b, y, x, c); - auto in2_idx = SubscriptToIndex(desc2, b, y, x, c); - auto in1_val = input1_data[in1_idx]; - auto in2_val = input2_data[in2_idx]; - output_data[out_idx] = in1_val >= 0.0f ? in1_val : in1_val * in2_val; - } - } - } - } -} - -TfLiteStatus PreluPrepare(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - PreluParams* params = static_cast(node->user_data); - - MicroContext* micro_context = GetMicroContext(context); - - TfLiteTensor* input = micro_context->AllocateTempInputTensor(node, 0); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* alpha = micro_context->AllocateTempInputTensor(node, 1); - TF_LITE_ENSURE(context, alpha != nullptr); - TfLiteTensor* output = micro_context->AllocateTempOutputTensor(node, 0); - TF_LITE_ENSURE(context, output != nullptr); - - TF_LITE_ENSURE_OK(context, - CalculatePreluParams(input, alpha, output, params)); - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(alpha); - micro_context->DeallocateTempTfLiteTensor(output); - return kTfLiteOk; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/quantize.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/quantize.cc deleted file mode 100644 index b5eb9c3c..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/quantize.cc +++ /dev/null @@ -1,41 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/kernels/quantize.h" - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/micro_utils.h" - -namespace tflite { -namespace { - -void* Init(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, - sizeof(OpDataQuantizeReference)); -} - -} // namespace - -TfLiteRegistration Register_QUANTIZE() { - return tflite::micro::RegisterOp(Init, PrepareQuantizeReference, - EvalQuantizeReference); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/quantize.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/quantize.h deleted file mode 100644 index ba93809a..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/quantize.h +++ /dev/null @@ -1,37 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_QUANTIZE_H_ -#define TENSORFLOW_LITE_MICRO_KERNELS_QUANTIZE_H_ - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -struct OpDataQuantizeReference { - tflite::QuantizationParams quantization_params; - // The scaling factor from input to output (aka the 'real multiplier') can - // be represented as a fixed point multiplier plus a left shift. - int32_t requantize_output_multiplier; - int requantize_output_shift; - - int32_t input_zero_point; -}; - -TfLiteStatus EvalQuantizeReference(TfLiteContext* context, TfLiteNode* node); -TfLiteStatus PrepareQuantizeReference(TfLiteContext* context, TfLiteNode* node); -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_KERNELS_QUANTIZE_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/quantize_common.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/quantize_common.cc deleted file mode 100644 index 94220529..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/quantize_common.cc +++ /dev/null @@ -1,239 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/quantize.h" -#include "tensorflow/lite/kernels/internal/reference/requantize.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/quantize.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" -#include "tensorflow/lite/micro/micro_utils.h" - -namespace tflite { - -TfLiteStatus PrepareQuantizeReference(TfLiteContext* context, - TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - auto* data = static_cast(node->user_data); - - TF_LITE_ENSURE_EQ(context, NumInputs(node), 1); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - - MicroContext* micro_context = GetMicroContext(context); - - TfLiteTensor* input = micro_context->AllocateTempInputTensor(node, 0); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* output = micro_context->AllocateTempOutputTensor(node, 0); - TF_LITE_ENSURE(context, output != nullptr); - - // TODO(b/128934713): Add support for fixed-point per-channel quantization. - // Currently this only support affine per-layer quantization. - TF_LITE_ENSURE_EQ(context, output->quantization.type, - kTfLiteAffineQuantization); - const auto* affine_quantization = - reinterpret_cast(output->quantization.params); - TF_LITE_ENSURE(context, affine_quantization); - TF_LITE_ENSURE(context, affine_quantization->scale); - TF_LITE_ENSURE(context, affine_quantization->scale->size == 1); - - TF_LITE_ENSURE( - context, input->type == kTfLiteFloat32 || input->type == kTfLiteInt32 || - input->type == kTfLiteInt16 || input->type == kTfLiteInt8 || - input->type == kTfLiteUInt8); - TF_LITE_ENSURE(context, output->type == kTfLiteInt8 || - output->type == kTfLiteInt16 || - output->type == kTfLiteInt32 || - output->type == kTfLiteUInt8); - - if ((input->type == kTfLiteInt16 && output->type == kTfLiteInt8) || - (input->type == kTfLiteInt8 && output->type == kTfLiteInt8) || - (input->type == kTfLiteInt8 && output->type == kTfLiteUInt8) || - (input->type == kTfLiteUInt8 && output->type == kTfLiteInt8) || - (input->type == kTfLiteInt8 && output->type == kTfLiteInt16) || - (input->type == kTfLiteInt8 && output->type == kTfLiteInt32) || - (input->type == kTfLiteInt16 && output->type == kTfLiteInt16) || - (input->type == kTfLiteInt16 && output->type == kTfLiteInt32) || - (input->type == kTfLiteInt32 && output->type == kTfLiteInt8) || - (input->type == kTfLiteInt32 && output->type == kTfLiteInt16)) { - double effective_scale = static_cast(input->params.scale) / - static_cast(output->params.scale); - - QuantizeMultiplier(effective_scale, &data->requantize_output_multiplier, - &data->requantize_output_shift); - } - - data->quantization_params.zero_point = output->params.zero_point; - data->quantization_params.scale = static_cast(output->params.scale); - - data->input_zero_point = input->params.zero_point; - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(output); - return kTfLiteOk; -} - -TfLiteStatus EvalQuantizeReference(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - auto* data = static_cast(node->user_data); - - const TfLiteEvalTensor* input = tflite::micro::GetEvalInput(context, node, 0); - TfLiteEvalTensor* output = tflite::micro::GetEvalOutput(context, node, 0); - - if (input->type == kTfLiteFloat32) { - switch (output->type) { - case kTfLiteInt8: - reference_ops::AffineQuantize( - data->quantization_params, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - case kTfLiteInt16: - reference_ops::AffineQuantize( - data->quantization_params, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - return kTfLiteOk; - default: - MicroPrintf("Input %s, output %s not supported.", - TfLiteTypeGetName(input->type), - TfLiteTypeGetName(output->type)); - return kTfLiteError; - } - } else if (input->type == kTfLiteInt32) { - size_t size = ElementCount(*input->dims); - switch (output->type) { - case kTfLiteInt8: - reference_ops::Requantize( - tflite::micro::GetTensorData(input), size, - data->requantize_output_multiplier, data->requantize_output_shift, - data->input_zero_point, data->quantization_params.zero_point, - tflite::micro::GetTensorData(output)); - break; - case kTfLiteInt16: - reference_ops::Requantize( - tflite::micro::GetTensorData(input), size, - data->requantize_output_multiplier, data->requantize_output_shift, - data->input_zero_point, data->quantization_params.zero_point, - tflite::micro::GetTensorData(output)); - break; - default: - MicroPrintf("Input %s, output %s not supported.", - TfLiteTypeGetName(input->type), - TfLiteTypeGetName(output->type)); - return kTfLiteError; - } - } else if (input->type == kTfLiteInt16) { - size_t size = ElementCount(*input->dims); - switch (output->type) { - case kTfLiteInt8: - reference_ops::Requantize( - tflite::micro::GetTensorData(input), size, - data->requantize_output_multiplier, data->requantize_output_shift, - data->input_zero_point, data->quantization_params.zero_point, - tflite::micro::GetTensorData(output)); - break; - case kTfLiteInt16: - reference_ops::Requantize( - tflite::micro::GetTensorData(input), size, - data->requantize_output_multiplier, data->requantize_output_shift, - data->input_zero_point, data->quantization_params.zero_point, - tflite::micro::GetTensorData(output)); - return kTfLiteOk; - case kTfLiteInt32: - reference_ops::Requantize( - tflite::micro::GetTensorData(input), size, - data->requantize_output_multiplier, data->requantize_output_shift, - data->input_zero_point, data->quantization_params.zero_point, - tflite::micro::GetTensorData(output)); - return kTfLiteOk; - default: - MicroPrintf("Input %s, output %s not supported.", - TfLiteTypeGetName(input->type), - TfLiteTypeGetName(output->type)); - return kTfLiteError; - } - } else if (input->type == kTfLiteInt8) { - // Int8 to Int8 requantization, required if the input and output tensors - // have different scales and/or zero points. - size_t size = ElementCount(*input->dims); - switch (output->type) { - case kTfLiteInt8: - reference_ops::Requantize( - tflite::micro::GetTensorData(input), size, - data->requantize_output_multiplier, data->requantize_output_shift, - data->input_zero_point, data->quantization_params.zero_point, - tflite::micro::GetTensorData(output)); - break; - case kTfLiteUInt8: - reference_ops::Requantize( - tflite::micro::GetTensorData(input), size, - data->requantize_output_multiplier, data->requantize_output_shift, - data->input_zero_point, data->quantization_params.zero_point, - tflite::micro::GetTensorData(output)); - break; - case kTfLiteInt16: - reference_ops::Requantize( - tflite::micro::GetTensorData(input), size, - data->requantize_output_multiplier, data->requantize_output_shift, - data->input_zero_point, data->quantization_params.zero_point, - tflite::micro::GetTensorData(output)); - break; - case kTfLiteInt32: - reference_ops::Requantize( - tflite::micro::GetTensorData(input), size, - data->requantize_output_multiplier, data->requantize_output_shift, - data->input_zero_point, data->quantization_params.zero_point, - tflite::micro::GetTensorData(output)); - break; - default: - MicroPrintf("Input %s, output %s not supported.", - TfLiteTypeGetName(input->type), - TfLiteTypeGetName(output->type)); - return kTfLiteError; - } - } else if (input->type == kTfLiteUInt8) { - size_t size = ElementCount(*input->dims); - switch (output->type) { - case kTfLiteInt8: - reference_ops::Requantize( - tflite::micro::GetTensorData(input), size, - data->requantize_output_multiplier, data->requantize_output_shift, - data->input_zero_point, data->quantization_params.zero_point, - tflite::micro::GetTensorData(output)); - break; - default: - MicroPrintf("Input %s, output %s not supported.", - TfLiteTypeGetName(input->type), - TfLiteTypeGetName(output->type)); - return kTfLiteError; - } - } else { - MicroPrintf("Input %s, output %s not supported.", - TfLiteTypeGetName(input->type), - TfLiteTypeGetName(output->type)); - return kTfLiteError; - } - - return kTfLiteOk; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/read_variable.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/read_variable.cc deleted file mode 100644 index 422c0384..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/read_variable.cc +++ /dev/null @@ -1,87 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include - -#include - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/memory_helpers.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" -#include "tensorflow/lite/micro/micro_graph.h" -#include "tensorflow/lite/micro/micro_resource_variable.h" -#include "tensorflow/lite/schema/schema_generated.h" - -namespace tflite { - -namespace { - -constexpr int kInputVariableId = 0; -constexpr int kOutputValue = 0; - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(NumInputs(node) == 1); - TFLITE_DCHECK(NumOutputs(node) == 1); - - MicroContext* micro_context = GetMicroContext(context); - - TfLiteTensor* input_resource_id_tensor = - micro_context->AllocateTempInputTensor(node, kInputVariableId); - - TFLITE_DCHECK(input_resource_id_tensor != nullptr); - TFLITE_DCHECK(input_resource_id_tensor->type == kTfLiteResource); - TFLITE_DCHECK(NumElements(input_resource_id_tensor) == 1); - - micro_context->DeallocateTempTfLiteTensor(input_resource_id_tensor); - - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input_resource_id_tensor = - tflite::micro::GetEvalInput(context, node, kInputVariableId); - TFLITE_DCHECK(input_resource_id_tensor != nullptr); - - TfLiteEvalTensor* output_value = - tflite::micro::GetEvalOutput(context, node, kOutputValue); - TFLITE_DCHECK(output_value != nullptr); - - tflite::MicroContext* micro_context = tflite::GetMicroContext(context); - MicroGraph& graph_info = micro_context->graph(); - - MicroResourceVariables* resources = graph_info.GetResourceVariables(); - if (resources == nullptr) { - MicroPrintf( - "READ_VARIABLE requires resource variables. Please create " - "ResourceVariables and pass it to the interpreter."); - return kTfLiteError; - } - TF_LITE_ENSURE_OK( - context, - resources->Read(input_resource_id_tensor->data.i32[0], output_value)); - return kTfLiteOk; -} - -} // namespace. - -TfLiteRegistration Register_READ_VARIABLE() { - return tflite::micro::RegisterOp(nullptr, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/reduce.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/reduce.cc deleted file mode 100644 index b4734f93..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/reduce.cc +++ /dev/null @@ -1,72 +0,0 @@ -/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/kernels/internal/reference/reduce.h" - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/integer_ops/mean.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/internal/types.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/reduce.h" -#include "tensorflow/lite/micro/micro_utils.h" - -namespace tflite { - -void* InitReduce(TfLiteContext* context, const char* buffer, size_t length) { - return context->AllocatePersistentBuffer(context, sizeof(OpDataReduce)); -} - -TfLiteStatus PrepareMax(TfLiteContext* context, TfLiteNode* node) { - return PrepareMaxHelper(context, node, - static_cast(node->user_data)); -} - -TfLiteStatus PrepareMeanOrSum(TfLiteContext* context, TfLiteNode* node) { - return PrepareMeanOrSumHelper(context, node, - static_cast(node->user_data)); -} - -TfLiteStatus EvalMean(TfLiteContext* context, TfLiteNode* node) { - return EvalMeanHelper(context, node, - static_cast(node->user_data)); -} - -TfLiteStatus EvalMax(TfLiteContext* context, TfLiteNode* node) { - OpDataReduce* op_data = static_cast(node->user_data); - return EvalMaxHelper(context, node, op_data); -} - -TfLiteStatus EvalSum(TfLiteContext* context, TfLiteNode* node) { - return EvalSumHelper(context, node, - static_cast(node->user_data)); -} - -TfLiteRegistration Register_MEAN() { - return tflite::micro::RegisterOp(InitReduce, PrepareMeanOrSum, EvalMean); -} - -TfLiteRegistration Register_REDUCE_MAX() { - return tflite::micro::RegisterOp(InitReduce, PrepareMax, EvalMax); -} - -TfLiteRegistration Register_SUM() { - return tflite::micro::RegisterOp(InitReduce, PrepareMeanOrSum, EvalSum); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/reduce.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/reduce.h deleted file mode 100644 index 8d524069..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/reduce.h +++ /dev/null @@ -1,64 +0,0 @@ -/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_REDUCE_H_ -#define TENSORFLOW_LITE_MICRO_KERNELS_REDUCE_H_ - -#include - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -extern const int kMaxNumberOfAxis; -extern const int kMaxNumberOfReducedAxis; - -struct OpDataReduce { - int32_t multiplier; - int shift; - int temp_buffer_idx; - int resolved_axis_idx; - int input_zp; - float input_scale; - int output_zp; - float output_scale; - int num_output_elements; -}; - -TfLiteStatus PrepareMaxHelper(TfLiteContext* context, TfLiteNode* node, - OpDataReduce* op_data); - -TfLiteStatus PrepareMeanOrSumHelper(TfLiteContext* context, TfLiteNode* node, - OpDataReduce* op_data); - -TfLiteStatus EvalMaxHelper(TfLiteContext* context, TfLiteNode* node, - OpDataReduce* op_data); -TfLiteStatus EvalMeanHelper(TfLiteContext* context, TfLiteNode* node, - OpDataReduce* op_data); -TfLiteStatus EvalSumHelper(TfLiteContext* context, TfLiteNode* node, - OpDataReduce* op_data); - -void ReduceResolveAxis(const int* axis_data, int axis_count, - MeanParams* op_params); - -TfLiteRegistration Register_MEAN(); -TfLiteRegistration Register_REDUCE_MAX(); -TfLiteRegistration Register_SUM(); - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_KERNELS_REDUCE_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/reduce_common.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/reduce_common.cc deleted file mode 100644 index a6f940c6..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/reduce_common.cc +++ /dev/null @@ -1,374 +0,0 @@ -/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/integer_ops/mean.h" -#include "tensorflow/lite/kernels/internal/reference/reduce.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/internal/types.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/reduce.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" -#include "tensorflow/lite/micro/micro_utils.h" - -namespace tflite { - -const int kMaxNumberOfAxis = 4; -const int kMaxNumberOfReducedAxis = 2; - -TfLiteStatus PrepareSimple(TfLiteContext* context, TfLiteNode* node, - int32_t* multiplier, int* shift) { - MicroContext* micro_context = GetMicroContext(context); - - // Inputs Tensor (dtype depends on quantization): - // [0] = Input - // [1] = Axis - TfLiteTensor* input = micro_context->AllocateTempInputTensor(node, 0); - - // Outputs Tensor (dtype depends on quantization): - // [0] = Output - - // Validate number of inputs and outputs - TF_LITE_ENSURE_EQ(context, node->inputs->size, 2); - TF_LITE_ENSURE_EQ(context, node->outputs->size, 1); - - // Validate axis type - TfLiteTensor* axis = micro_context->AllocateTempInputTensor(node, 1); - TF_LITE_ENSURE(context, axis != nullptr); - TF_LITE_ENSURE_TYPES_EQ(context, axis->type, kTfLiteInt32); - - if (input->type == kTfLiteInt8) { - TfLiteTensor* output = micro_context->AllocateTempOutputTensor(node, 0); - const double real_multiplier = static_cast(input->params.scale) / - static_cast(output->params.scale); - QuantizeMultiplier(real_multiplier, multiplier, shift); - micro_context->DeallocateTempTfLiteTensor(output); - } - micro_context->DeallocateTempTfLiteTensor(axis); - micro_context->DeallocateTempTfLiteTensor(input); - return kTfLiteOk; -} - -TfLiteStatus PrepareMaxHelper(TfLiteContext* context, TfLiteNode* node, - OpDataReduce* op_data) { - TF_LITE_ENSURE_OK(context, PrepareSimple(context, node, &op_data->multiplier, - &op_data->shift)); - - MicroContext* micro_context = GetMicroContext(context); - TfLiteTensor* input = micro_context->AllocateTempInputTensor(node, 0); - TfLiteTensor* output = micro_context->AllocateTempOutputTensor(node, 0); - TfLiteTensor* axis = micro_context->AllocateTempInputTensor(node, 1); - - op_data->input_scale = input->params.scale; - op_data->output_scale = output->params.scale; - op_data->num_output_elements = NumElements(output); - - context->RequestScratchBufferInArena(context, sizeof(int) * input->dims->size, - &op_data->temp_buffer_idx); - context->RequestScratchBufferInArena( - context, sizeof(int) * static_cast(ElementCount(*axis->dims)), - &op_data->resolved_axis_idx); - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(output); - micro_context->DeallocateTempTfLiteTensor(axis); - return kTfLiteOk; -} - -TfLiteStatus PrepareMeanOrSumHelper(TfLiteContext* context, TfLiteNode* node, - OpDataReduce* op_data) { - MicroContext* micro_context = GetMicroContext(context); - TfLiteTensor* input = micro_context->AllocateTempInputTensor(node, 0); - TfLiteTensor* output = micro_context->AllocateTempOutputTensor(node, 0); - if (input->type == kTfLiteInt8 || input->type == kTfLiteInt16) { - const double real_multiplier = static_cast(input->params.scale) / - static_cast(output->params.scale); - QuantizeMultiplier(real_multiplier, &op_data->multiplier, &op_data->shift); - } - - int output_size = NumElements(output); - if (input->type == kTfLiteInt8 || input->type == kTfLiteInt16) { - context->RequestScratchBufferInArena(context, output_size * sizeof(int32_t), - &op_data->temp_buffer_idx); - op_data->input_zp = input->params.zero_point; - op_data->input_scale = input->params.scale; - op_data->output_zp = output->params.zero_point; - op_data->output_scale = output->params.scale; - } - - TF_LITE_ENSURE_OK( - context, - PrepareSimple(context, node, &(op_data->multiplier), &(op_data->shift))); - // TODO(b/144955155): Support uint8_t(b/144955155) and int8_t(b/144955018) - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(output); - return kTfLiteOk; -} - -void ResolveAxis(const int* axis_data, int axis_count, - tflite::MeanParams* op_params) { - int i = 0; - for (; i < axis_count; ++i) { - op_params->axis[i] = static_cast(axis_data[i]); - } - for (; i < 4; ++i) { - op_params->axis[i] = 1; - } - op_params->axis_count = axis_count; -} - -TfLiteStatus EvalMeanHelper(TfLiteContext* context, TfLiteNode* node, - OpDataReduce* op_data) { - const TfLiteEvalTensor* input = tflite::micro::GetEvalInput(context, node, 0); - const TfLiteEvalTensor* axis = tflite::micro::GetEvalInput(context, node, 1); - TfLiteEvalTensor* output = tflite::micro::GetEvalOutput(context, node, 0); - TfLiteReducerParams* params = - reinterpret_cast(node->builtin_data); - - int num_axis = static_cast(ElementCount(*axis->dims)); - int temp_index[kMaxNumberOfAxis]; - int resolved_axis[kMaxNumberOfReducedAxis]; - - tflite::MeanParams op_params; - ResolveAxis(tflite::micro::GetTensorData(axis), num_axis, &op_params); - - // Special case mean implementation exists for 4D mean across axes 1 and 2. - bool special_case_4d_axes_1_and_2 = - input->dims->size == 4 && op_params.axis_count == 2 && - ((op_params.axis[0] == 1 && op_params.axis[1] == 2) || - (op_params.axis[0] == 2 && op_params.axis[1] == 1)); - - switch (input->type) { - case kTfLiteFloat32: { - // Defer to specialized implementation for 4D Mean across axes 1 & 2. - if (params->keep_dims && special_case_4d_axes_1_and_2) { - reference_ops::Mean(op_params, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } else { - TF_LITE_ENSURE( - context, - reference_ops::Mean( - tflite::micro::GetTensorData(input), input->dims->data, - input->dims->size, tflite::micro::GetTensorData(output), - output->dims->data, output->dims->size, - tflite::micro::GetTensorData(axis), num_axis, - params->keep_dims, temp_index, resolved_axis, - tflite::micro::GetTensorData(output))); - } - } break; - case kTfLiteInt8: { - // Defer to specialized implementation for 4D Mean across axes 1 & 2. - if (params->keep_dims && special_case_4d_axes_1_and_2) { - reference_integer_ops::Mean( - op_params, op_data->multiplier, op_data->shift, - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), op_data->input_zp, - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output), op_data->output_zp); - } else if (op_data->input_zp == op_data->output_zp && - op_data->input_scale == op_data->output_scale) { - int32_t* temp_buffer = static_cast( - context->GetScratchBuffer(context, op_data->temp_buffer_idx)); - TF_LITE_ENSURE( - context, - reference_ops::Mean( - tflite::micro::GetTensorData(input), input->dims->data, - input->dims->size, tflite::micro::GetTensorData(output), - output->dims->data, output->dims->size, - tflite::micro::GetTensorData(axis), num_axis, - params->keep_dims, temp_index, resolved_axis, temp_buffer)); - } else { - int32_t* temp_buffer = static_cast( - context->GetScratchBuffer(context, op_data->temp_buffer_idx)); - TF_LITE_ENSURE( - context, - reference_ops::QuantizedMeanOrSum( - tflite::micro::GetTensorData(input), op_data->input_zp, - op_data->input_scale, input->dims->data, input->dims->size, - tflite::micro::GetTensorData(output), - op_data->output_zp, op_data->output_scale, output->dims->data, - output->dims->size, tflite::micro::GetTensorData(axis), - num_axis, params->keep_dims, temp_index, resolved_axis, - temp_buffer, false)); - } - } break; - case kTfLiteInt16: { - // Defer to specialized implementation for 4D Mean across axes 1 & 2. - if (params->keep_dims && special_case_4d_axes_1_and_2) { - reference_integer_ops::Mean( - op_params, op_data->multiplier, op_data->shift, - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), op_data->input_zp, - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output), op_data->output_zp); - } else if (op_data->input_zp == op_data->output_zp && - op_data->input_scale == op_data->output_scale) { - int32_t* temp_buffer = static_cast( - context->GetScratchBuffer(context, op_data->temp_buffer_idx)); - TF_LITE_ENSURE( - context, - reference_ops::Mean(tflite::micro::GetTensorData(input), - input->dims->data, input->dims->size, - tflite::micro::GetTensorData(output), - output->dims->data, output->dims->size, - tflite::micro::GetTensorData(axis), - num_axis, params->keep_dims, temp_index, - resolved_axis, temp_buffer)); - } else { - int32_t* temp_buffer = static_cast( - context->GetScratchBuffer(context, op_data->temp_buffer_idx)); - TF_LITE_ENSURE( - context, - reference_ops::QuantizedMeanOrSum( - tflite::micro::GetTensorData(input), op_data->input_zp, - op_data->input_scale, input->dims->data, input->dims->size, - tflite::micro::GetTensorData(output), - op_data->output_zp, op_data->output_scale, output->dims->data, - output->dims->size, tflite::micro::GetTensorData(axis), - num_axis, params->keep_dims, temp_index, resolved_axis, - temp_buffer, false)); - } - } break; - default: - TF_LITE_ENSURE_MSG(context, false, - "Currently, only float32, int8 or int16 input type " - "is supported."); - } - return kTfLiteOk; -} - -TfLiteStatus EvalMaxHelper(TfLiteContext* context, TfLiteNode* node, - OpDataReduce* op_data) { - const TfLiteEvalTensor* input = tflite::micro::GetEvalInput(context, node, 0); - const TfLiteEvalTensor* axis = tflite::micro::GetEvalInput(context, node, 1); - TfLiteEvalTensor* output = tflite::micro::GetEvalOutput(context, node, 0); - TF_LITE_ENSURE_TYPES_EQ(context, input->type, output->type); - TfLiteReducerParams* params = - static_cast(node->builtin_data); - - // Interpret an axis tensor with null dimensions as a scalar - int num_axis = static_cast(ElementCount(*axis->dims)); - int* temp_buffer = static_cast( - context->GetScratchBuffer(context, op_data->temp_buffer_idx)); - int* resolved_axis = static_cast( - context->GetScratchBuffer(context, op_data->resolved_axis_idx)); - switch (input->type) { - case kTfLiteFloat32: - TF_LITE_ENSURE( - context, - reference_ops::ReduceGeneric( - tflite::micro::GetTensorData(input), input->dims->data, - input->dims->size, tflite::micro::GetTensorData(output), - output->dims->data, output->dims->size, - tflite::micro::GetTensorData(axis), num_axis, - params->keep_dims, temp_buffer, resolved_axis, - std::numeric_limits::lowest(), - [](const float current, const float in) -> float { - return (in > current) ? in : current; - })); - break; - case kTfLiteInt8: - TF_LITE_ENSURE_EQ(context, static_cast(op_data->input_scale), - static_cast(op_data->output_scale)); - TF_LITE_ENSURE_EQ(context, op_data->input_zp, op_data->output_zp); - TF_LITE_ENSURE( - context, - reference_ops::ReduceGeneric( - tflite::micro::GetTensorData(input), input->dims->data, - input->dims->size, tflite::micro::GetTensorData(output), - output->dims->data, output->dims->size, - tflite::micro::GetTensorData(axis), num_axis, - params->keep_dims, temp_buffer, resolved_axis, - std::numeric_limits::lowest(), - [](const int8_t current, const int8_t in) -> int8_t { - return (in > current) ? in : current; - })); - break; - default: - MicroPrintf("Only float32 and int8 types are supported."); - return kTfLiteError; - } - return kTfLiteOk; -} - -TfLiteStatus EvalSumHelper(TfLiteContext* context, TfLiteNode* node, - OpDataReduce* op_data) { - const TfLiteEvalTensor* input = tflite::micro::GetEvalInput(context, node, 0); - const TfLiteEvalTensor* axis = tflite::micro::GetEvalInput(context, node, 1); - TfLiteEvalTensor* output = tflite::micro::GetEvalOutput(context, node, 0); - TF_LITE_ENSURE_TYPES_EQ(context, input->type, output->type); - TfLiteReducerParams* params = - static_cast(node->builtin_data); - - // Interpret an axis tensor with null dimensions as a scalar. - int num_axis = static_cast(ElementCount(*axis->dims)); - int temp_index[kMaxNumberOfAxis]; - int resolved_axis[kMaxNumberOfReducedAxis]; - - switch (input->type) { - case kTfLiteFloat32: { - TF_LITE_ENSURE( - context, - reference_ops::ReduceGeneric( - tflite::micro::GetTensorData(input), input->dims->data, - input->dims->size, tflite::micro::GetTensorData(output), - output->dims->data, output->dims->size, - tflite::micro::GetTensorData(axis), num_axis, - params->keep_dims, temp_index, resolved_axis, /*init_value=*/0.f, - [](const float current, const float in) -> float { - return in + current; - })); - } break; - case kTfLiteInt8: { - int32_t* temp_buffer = static_cast( - context->GetScratchBuffer(context, op_data->temp_buffer_idx)); - TF_LITE_ENSURE( - context, - reference_ops::QuantizedMeanOrSum( - tflite::micro::GetTensorData(input), op_data->input_zp, - op_data->input_scale, input->dims->data, input->dims->size, - tflite::micro::GetTensorData(output), op_data->output_zp, - op_data->output_scale, output->dims->data, output->dims->size, - tflite::micro::GetTensorData(axis), num_axis, - params->keep_dims, temp_index, resolved_axis, temp_buffer, - /*compute_sum=*/true)); - } break; - case kTfLiteInt16: { - int32_t* temp_buffer = static_cast( - context->GetScratchBuffer(context, op_data->temp_buffer_idx)); - TF_LITE_ENSURE( - context, - reference_ops::QuantizedMeanOrSum( - tflite::micro::GetTensorData(input), op_data->input_zp, - op_data->input_scale, input->dims->data, input->dims->size, - tflite::micro::GetTensorData(output), op_data->output_zp, - op_data->output_scale, output->dims->data, output->dims->size, - tflite::micro::GetTensorData(axis), num_axis, - params->keep_dims, temp_index, resolved_axis, temp_buffer, - /*compute_sum=*/true)); - } break; - default: - MicroPrintf("Only float32, int8, and int16 types are supported."); - return kTfLiteError; - } - return kTfLiteOk; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/reshape.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/reshape.cc deleted file mode 100644 index 832ba261..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/reshape.cc +++ /dev/null @@ -1,118 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/memory_helpers.h" -#include "tensorflow/lite/micro/micro_utils.h" - -namespace tflite { -namespace ops { -namespace micro { -namespace reshape { - -constexpr int kInputTensor = 0; -constexpr int kOutputTensor = 0; - -TfLiteStatus ReshapeOutput(TfLiteContext* context, TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - // Tensorflow's Reshape allows one of the shape components to have the - // special -1 value, meaning it will be calculated automatically based on the - // input. Here we calculate what that dimension should be so that the number - // of output elements in the same as the number of input elements. - int num_input_elements = NumElements(input); - TfLiteIntArray* output_shape = output->dims; - - if (NumInputs(node) == 1 && // Legacy scalar supported with params. - output_shape->size == 1 && output_shape->data[0] == 0) { - // Legacy tflite models use a shape parameter of [0] to indicate scalars, - // so adjust accordingly. TODO(b/111614235): Allow zero-sized buffers during - // toco conversion. - output_shape->size = 0; - } - - int num_output_elements = 1; - int stretch_dim = -1; - for (int i = 0; i < output_shape->size; ++i) { - int value = output_shape->data[i]; - if (value == -1) { - TF_LITE_ENSURE_EQ(context, stretch_dim, -1); - stretch_dim = i; - } else { - num_output_elements *= value; - } - } - if (stretch_dim != -1) { - output_shape->data[stretch_dim] = num_input_elements / num_output_elements; - num_output_elements *= output_shape->data[stretch_dim]; - } - - TF_LITE_ENSURE_TYPES_EQ(context, input->type, output->type); - TF_LITE_ENSURE_EQ(context, num_input_elements, num_output_elements); - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(output); - return kTfLiteOk; -} - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - TF_LITE_ENSURE(context, NumInputs(node) == 1 || NumInputs(node) == 2); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - TF_LITE_ENSURE_EQ(context, ReshapeOutput(context, node), kTfLiteOk); - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kInputTensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - - // TODO(b/162522304): storing input bytes in OpData increases some models - // significantly, possibly due to alignment issues. - size_t input_bytes; - TF_LITE_ENSURE_STATUS(TfLiteTypeSizeOf(input->type, &input_bytes)); - input_bytes *= ElementCount(*input->dims); - - // Do nothing for in-place reshape. - if (input->data.raw != output->data.raw) { - // Otherwise perform reshape with copy. - memcpy(output->data.raw, input->data.raw, input_bytes); - } - return kTfLiteOk; -} - -} // namespace reshape - -TfLiteRegistration Register_RESHAPE() { - return tflite::micro::RegisterOp(nullptr, reshape::Prepare, reshape::Eval); -} - -} // namespace micro -} // namespace ops -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/resize_bilinear.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/resize_bilinear.cc deleted file mode 100644 index a90057b9..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/resize_bilinear.cc +++ /dev/null @@ -1,117 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/kernels/internal/reference/resize_bilinear.h" - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/micro_utils.h" - -namespace tflite { -namespace { - -constexpr int kInputTensor = 0; -constexpr int kSizeTensor = 1; -constexpr int kOutputTensor = 0; - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - - TF_LITE_ENSURE_EQ(context, NumInputs(node), 2); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kInputTensor); - TfLiteTensor* size = - micro_context->AllocateTempInputTensor(node, kSizeTensor); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - - TF_LITE_ENSURE_EQ(context, NumDimensions(input), 4); - TF_LITE_ENSURE_EQ(context, NumDimensions(size), 1); - - TF_LITE_ENSURE_EQ(context, size->type, kTfLiteInt32); - output->type = input->type; - - TF_LITE_ENSURE_MSG(context, IsConstantTensor(size), - "Non constant size tensor not supported"); - - // Ensure params are valid. - auto* params = - reinterpret_cast(node->builtin_data); - if (params->half_pixel_centers && params->align_corners) { - TF_LITE_KERNEL_LOG( - context, "If half_pixel_centers is True, align_corners must be False."); - return kTfLiteError; - } - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(size); - micro_context->DeallocateTempTfLiteTensor(output); - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - auto* params = - reinterpret_cast(node->builtin_data); - - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kInputTensor); - const TfLiteEvalTensor* size = - tflite::micro::GetEvalInput(context, node, kSizeTensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - - if (output->type == kTfLiteFloat32) { - tflite::ResizeBilinearParams op_params; - op_params.align_corners = params->align_corners; - op_params.half_pixel_centers = params->half_pixel_centers; - reference_ops::ResizeBilinear(op_params, - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(size), - tflite::micro::GetTensorData(size), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } else if (output->type == kTfLiteInt8) { - tflite::ResizeBilinearParams op_params; - op_params.align_corners = params->align_corners; - op_params.half_pixel_centers = params->half_pixel_centers; - reference_ops::ResizeBilinearInteger( - op_params, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(size), - tflite::micro::GetTensorData(size), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } else { - TF_LITE_KERNEL_LOG(context, "Output type is %d, requires float or int8.", - output->type); - return kTfLiteError; - } - - return kTfLiteOk; -} - -} // namespace - -TfLiteRegistration Register_RESIZE_BILINEAR() { - return tflite::micro::RegisterOp(nullptr, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/resize_nearest_neighbor.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/resize_nearest_neighbor.cc deleted file mode 100644 index ce507445..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/resize_nearest_neighbor.cc +++ /dev/null @@ -1,126 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/kernels/internal/reference/resize_nearest_neighbor.h" - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" - -namespace tflite { -namespace ops { -namespace micro { -namespace resize_nearest_neighbor { - -constexpr int kInputTensor = 0; -constexpr int kSizeTensor = 1; -constexpr int kOutputTensor = 0; - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - - TF_LITE_ENSURE_EQ(context, NumInputs(node), 2); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kInputTensor); - TfLiteTensor* size = - micro_context->AllocateTempInputTensor(node, kSizeTensor); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - - // Our current implementations rely on the input being 4D, - // and the size being 1D tensor with exactly 2 elements. - TF_LITE_ENSURE_EQ(context, NumDimensions(input), 4); - TF_LITE_ENSURE_EQ(context, NumDimensions(size), 1); - TF_LITE_ENSURE_EQ(context, size->type, kTfLiteInt32); - TF_LITE_ENSURE_EQ(context, size->dims->data[0], 2); - - output->type = input->type; - - if (!IsConstantTensor(size)) { - TF_LITE_KERNEL_LOG(context, "Dynamic tensors are unsupported in tfmicro."); - return kTfLiteError; - } - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(size); - micro_context->DeallocateTempTfLiteTensor(output); - - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - auto* params = - reinterpret_cast(node->builtin_data); - - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kInputTensor); - const TfLiteEvalTensor* size = - tflite::micro::GetEvalInput(context, node, kSizeTensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - - tflite::ResizeNearestNeighborParams op_params; - op_params.align_corners = params->align_corners; - op_params.half_pixel_centers = false; - - if (output->type == kTfLiteFloat32) { - reference_ops::ResizeNearestNeighbor( - op_params, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(size), - tflite::micro::GetTensorData(size), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } else if (output->type == kTfLiteInt8) { - reference_ops::ResizeNearestNeighbor( - op_params, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(size), - tflite::micro::GetTensorData(size), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } else if (output->type == kTfLiteInt16) { - reference_ops::ResizeNearestNeighbor( - op_params, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(size), - tflite::micro::GetTensorData(size), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } else { - MicroPrintf("Output tensor type %s (%d) not supported.", - TfLiteTypeGetName(output->type), output->type); - - return kTfLiteError; - } - - return kTfLiteOk; -} -} // namespace resize_nearest_neighbor - -TfLiteRegistration Register_RESIZE_NEAREST_NEIGHBOR() { - return tflite::micro::RegisterOp(nullptr, resize_nearest_neighbor::Prepare, - resize_nearest_neighbor::Eval); -} - -} // namespace micro -} // namespace ops -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/round.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/round.cc deleted file mode 100644 index 0bda8783..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/round.cc +++ /dev/null @@ -1,76 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/kernels/internal/reference/round.h" - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { -namespace ops { -namespace micro { -namespace round { - -constexpr int kInputTensor = 0; -constexpr int kOutputTensor = 0; - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - TF_LITE_ENSURE_EQ(context, NumInputs(node), 1); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - TF_LITE_ENSURE_TYPES_EQ(context, input->type, kTfLiteFloat32); - TF_LITE_ENSURE_TYPES_EQ(context, output->type, input->type); - TF_LITE_ENSURE_EQ(context, output->bytes, input->bytes); - TF_LITE_ENSURE_EQ(context, output->dims->size, input->dims->size); - for (int i = 0; i < output->dims->size; ++i) { - TF_LITE_ENSURE_EQ(context, output->dims->data[i], input->dims->data[i]); - } - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(output); - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kInputTensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - - reference_ops::Round(tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - - return kTfLiteOk; -} -} // namespace round - -TfLiteRegistration Register_ROUND() { - return tflite::micro::RegisterOp(nullptr, round::Prepare, round::Eval); -} - -} // namespace micro -} // namespace ops -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/shape.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/shape.cc deleted file mode 100644 index 02f663a8..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/shape.cc +++ /dev/null @@ -1,66 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/memory_helpers.h" -#include "tensorflow/lite/micro/micro_utils.h" - -namespace tflite { - -namespace { -constexpr int kInputTensor = 0; -constexpr int kOutputTensor = 0; - -void ExtractShape(const TfLiteEvalTensor* input, int32_t* output_data) { - for (int i = 0; i < input->dims->size; ++i) { - output_data[i] = input->dims->data[i]; - } -} - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - TF_LITE_ENSURE_EQ(context, NumInputs(node), 1); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kInputTensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - if (output->type != kTfLiteInt32) { - TF_LITE_KERNEL_LOG(context, "Output type %s (%d) not supported.", - TfLiteTypeGetName(output->type), output->type); - return kTfLiteError; - } else { - ExtractShape(input, tflite::micro::GetTensorData(output)); - } - - return kTfLiteOk; -} - -} // namespace - -TfLiteRegistration Register_SHAPE() { - return tflite::micro::RegisterOp(nullptr, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/slice.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/slice.cc deleted file mode 100644 index 212cf47f..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/slice.cc +++ /dev/null @@ -1,157 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/kernels/internal/reference/slice.h" - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" - -namespace tflite { - -namespace { - -constexpr int kInputTensor = 0; -constexpr int kBeginTensor = 1; -constexpr int kSizeTensor = 2; -constexpr int kOutputTensor = 0; - -const int kMaxDim = 5; - -template -void GetBeginAndSizeVectors(int dimensions, const TfLiteEvalTensor* begin, - const TfLiteEvalTensor* size, int32_t* begins, - int32_t* sizes) { - int offset = kMaxDim - dimensions; - for (int idx = 0; idx < dimensions; ++idx) { - begins[offset + idx] = tflite::micro::GetTensorData(begin)[idx]; - sizes[offset + idx] = tflite::micro::GetTensorData(size)[idx]; - } -} - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - - TF_LITE_ENSURE_EQ(context, NumInputs(node), 3); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kInputTensor); - TFLITE_DCHECK(input != nullptr); - TfLiteTensor* begin = - micro_context->AllocateTempInputTensor(node, kBeginTensor); - TFLITE_DCHECK(begin != nullptr); - TfLiteTensor* size = - micro_context->AllocateTempInputTensor(node, kSizeTensor); - TFLITE_DCHECK(size != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - TFLITE_DCHECK(output != nullptr); - - // Ensure validity of input tensor and its dimension. - TFLITE_DCHECK(input->type == output->type); - TFLITE_DCHECK(begin->type == size->type); - TFLITE_DCHECK(begin->type == kTfLiteInt32 || begin->type == kTfLiteInt64); - TFLITE_DCHECK(size->type == kTfLiteInt32 || size->type == kTfLiteInt64); - TFLITE_DCHECK(NumDimensions(begin) == 1); - TFLITE_DCHECK(NumDimensions(size) == 1); - TFLITE_DCHECK(NumElements(begin) == NumElements(size)); - TFLITE_DCHECK(NumDimensions(input) <= kMaxDim); - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(begin); - micro_context->DeallocateTempTfLiteTensor(size); - micro_context->DeallocateTempTfLiteTensor(output); - - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kInputTensor); - const TfLiteEvalTensor* begin = - tflite::micro::GetEvalInput(context, node, kBeginTensor); - const TfLiteEvalTensor* size = - tflite::micro::GetEvalInput(context, node, kSizeTensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - - tflite::SliceParams op_params; - op_params.begin_count = kMaxDim; - op_params.size_count = kMaxDim; - for (int i = 0; i < kMaxDim; ++i) { - op_params.begin[i] = 0; - op_params.size[i] = 1; - } - - if (begin->type == kTfLiteInt32) { - GetBeginAndSizeVectors(input->dims->size, begin, size, - op_params.begin, op_params.size); - } else if (begin->type == kTfLiteInt64) { - GetBeginAndSizeVectors(input->dims->size, begin, size, - op_params.begin, op_params.size); - } else { - TF_LITE_KERNEL_LOG(context, "Begin tensor type %s (%d) not supported.", - TfLiteTypeGetName(input->type), input->type); - return kTfLiteError; - } - - switch (input->type) { - case kTfLiteFloat32: - reference_ops::Slice(op_params, - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - case kTfLiteInt32: - reference_ops::Slice( - op_params, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - case kTfLiteInt8: - reference_ops::Slice( - op_params, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - case kTfLiteInt16: - reference_ops::Slice( - op_params, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - default: - MicroPrintf("Input tensor type %s (%d) not supported.", - TfLiteTypeGetName(input->type), input->type); - return kTfLiteError; - } - return kTfLiteOk; -} - -} // namespace - -TfLiteRegistration Register_SLICE() { - return tflite::micro::RegisterOp(nullptr, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/softmax.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/softmax.cc deleted file mode 100644 index c2cee3c5..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/softmax.cc +++ /dev/null @@ -1,89 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/kernels/softmax.h" - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/softmax.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { -namespace { - -void SoftmaxQuantized(const TfLiteEvalTensor* input, TfLiteEvalTensor* output, - const SoftmaxParams& op_data) { - if (input->type == kTfLiteInt8) { - if (output->type == kTfLiteInt16) { - tflite::reference_ops::Softmax( - op_data, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } else { - tflite::reference_ops::Softmax( - op_data, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } - } else { - tflite::reference_ops::SoftmaxInt16( - op_data, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } -} - -TfLiteStatus SoftmaxEval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input = tflite::micro::GetEvalInput(context, node, 0); - TfLiteEvalTensor* output = tflite::micro::GetEvalOutput(context, node, 0); - - TFLITE_DCHECK(node->user_data != nullptr); - SoftmaxParams op_data = *static_cast(node->user_data); - - switch (input->type) { - case kTfLiteFloat32: { - tflite::reference_ops::Softmax( - op_data, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - return kTfLiteOk; - } - case kTfLiteInt8: - case kTfLiteInt16: { - SoftmaxQuantized(input, output, op_data); - return kTfLiteOk; - } - default: - TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.", - TfLiteTypeGetName(input->type), input->type); - return kTfLiteError; - } -} -} // namespace - -TfLiteRegistration Register_SOFTMAX() { - return tflite::micro::RegisterOp(SoftmaxInit, SoftmaxPrepare, SoftmaxEval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/softmax.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/softmax.h deleted file mode 100644 index 7096d202..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/softmax.h +++ /dev/null @@ -1,69 +0,0 @@ -/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_SOFTMAX_H_ -#define TENSORFLOW_LITE_MICRO_KERNELS_SOFTMAX_H_ - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/types.h" - -namespace tflite { - -void* SoftmaxInit(TfLiteContext* context, const char* buffer, size_t length); - -// Common helper function to SoftmaxPrepare. -TfLiteStatus CalculateSoftmaxParams(TfLiteContext* context, - const TfLiteTensor* input, - TfLiteTensor* output, - const TfLiteSoftmaxParams* params, - SoftmaxParams* op_data); - -TfLiteStatus SoftmaxPrepare(TfLiteContext* context, TfLiteNode* node); - -// This is the most generic TfLiteRegistration. The actual supported types may -// still be target dependent. The only requirement is that every implementation -// (reference or optimized) must define this function. -TfLiteRegistration Register_SOFTMAX(); - -#if defined(XTENSA) || defined(CMSIS_NN) -// Returns a TfLiteRegistration struct for kernel variant that only supports -// int8 input and int16 output. -TfLiteRegistration Register_SOFTMAX_INT8_INT16(); -#else -inline TfLiteRegistration Register_SOFTMAX_INT8_INT16() { - return Register_SOFTMAX(); -} -#endif - -#if defined(CMSIS_NN) -// Returns a TfLiteRegistration struct for kernel variant that only supports -// int8 input/output and uses the latency optimized implementations. -TfLiteRegistration Register_SOFTMAX_INT8(); - -// Returns a TfLiteRegistration struct for kernel variant that only supports -// int16 input/output and uses the latency optimized implementations. -TfLiteRegistration Register_SOFTMAX_INT16(); - -#else -inline TfLiteRegistration Register_SOFTMAX_INT8() { return Register_SOFTMAX(); } - -inline TfLiteRegistration Register_SOFTMAX_INT16() { - return Register_SOFTMAX(); -} -#endif - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_KERNELS_SOFTMAX_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/softmax_common.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/softmax_common.cc deleted file mode 100644 index b5378dae..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/softmax_common.cc +++ /dev/null @@ -1,162 +0,0 @@ -/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/kernels/softmax.h" -#include "tensorflow/lite/micro/micro_context.h" - -namespace tflite { - -namespace { -// Softmax parameter data that persists in user_data -const int kInt16LUTArraySize = 513; - -TfLiteStatus InitializeLutForInt16(TfLiteContext* context, - const TfLiteTensor* input, - TfLiteTensor* output, - SoftmaxParams* op_data) { - // Only allocate LUTs for KTfLiteInt16 data type - if (input->type == kTfLiteInt16) { - void* raw_exp_lut = context->AllocatePersistentBuffer( - context, sizeof(int16_t) * kInt16LUTArraySize); - TF_LITE_ENSURE(context, raw_exp_lut != nullptr); - op_data->exp_lut = reinterpret_cast(raw_exp_lut); - void* one_over_one_plus_x_lut = context->AllocatePersistentBuffer( - context, sizeof(int16_t) * kInt16LUTArraySize); - TF_LITE_ENSURE(context, one_over_one_plus_x_lut != nullptr); - op_data->one_over_one_plus_x_lut = - reinterpret_cast(one_over_one_plus_x_lut); - } - - if (output->type == kTfLiteInt16) { - TF_LITE_ENSURE(context, - input->type == kTfLiteInt8 || input->type == kTfLiteInt16); - } else { - TF_LITE_ENSURE_EQ(context, input->type, output->type); - } - - // Populate LUT if required - if (input->type == kTfLiteInt16) { - TF_LITE_ENSURE_EQ(context, output->params.zero_point, 0); - // exp LUT only used on negative values - // we consider exp(-10.0) is insignificant to accumulation - gen_lut( - [](float value) { return std::exp(value); }, -10.0f, 0.0f, -1.0f, 1.0f, - op_data->exp_lut); - gen_lut( - [](float value) { return 1.0f / (1.0f + value); }, 0.0f, 1.0f, -1.0f, - 1.0f, op_data->one_over_one_plus_x_lut); - op_data->zero_point = output->params.zero_point; - op_data->scale = output->params.scale; - } - - return kTfLiteOk; -} - -} // namespace - -TfLiteStatus CalculateSoftmaxParams(TfLiteContext* context, - const TfLiteTensor* input, - TfLiteTensor* output, - const TfLiteSoftmaxParams* params, - SoftmaxParams* op_data) { - if (InitializeLutForInt16(context, input, output, op_data) != kTfLiteOk) { - return kTfLiteError; - } - - if (input->type == kTfLiteInt8 || input->type == kTfLiteInt16) { - if (input->type == kTfLiteInt16) { - TF_LITE_ENSURE_EQ(context, output->params.zero_point, 0); - TF_LITE_ENSURE_NEAR(context, output->params.scale, 1.f / 32768, - (0.001f * 1.f / 32768)); - } else { // input->type == kTfLiteInt8 - TF_LITE_ENSURE_TYPES_EQ(context, input->type, kTfLiteInt8); - if (output->type == kTfLiteInt16) { - TF_LITE_ENSURE_EQ(context, output->params.zero_point, -32768); - TF_LITE_ENSURE_NEAR(context, output->params.scale, 1.f / 65536, - (0.001f * 1.f / 65536)); - } else { // output->type == kTfLiteint8 - TF_LITE_ENSURE_TYPES_EQ(context, output->type, kTfLiteInt8); - TF_LITE_ENSURE_EQ(context, output->params.zero_point, -128); - TF_LITE_ENSURE(context, output->params.scale == 1.f / 256); - } - } - - static const int kScaledDiffIntegerBits = 5; - - // Calculate input_multiplier and input_left_shift - if (input->type == kTfLiteInt16) { - int input_left_shift; - double input_scale_beta_rescale = - static_cast(input->params.scale) * - static_cast(params->beta) / - (10.0 / 65535.0); // scale the input_diff such that [-65535, 0] - // correspond to [-10.0, 0.0] - QuantizeMultiplier(input_scale_beta_rescale, &op_data->input_multiplier, - &input_left_shift); - op_data->input_left_shift = input_left_shift; - } else { - int input_left_shift; - tflite::PreprocessSoftmaxScaling( - static_cast(params->beta), - static_cast(input->params.scale), kScaledDiffIntegerBits, - &op_data->input_multiplier, &input_left_shift); - op_data->input_left_shift = input_left_shift; - op_data->diff_min = - -1.0 * tflite::CalculateInputRadius(kScaledDiffIntegerBits, - op_data->input_left_shift); - } - } else { - TF_LITE_ENSURE_TYPES_EQ(context, input->type, kTfLiteFloat32); - TF_LITE_ENSURE_TYPES_EQ(context, output->type, kTfLiteFloat32); - op_data->beta = static_cast(params->beta); - } - return kTfLiteOk; -} - -void* SoftmaxInit(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(SoftmaxParams)); -} - -TfLiteStatus SoftmaxPrepare(TfLiteContext* context, TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - - TF_LITE_ENSURE_EQ(context, NumInputs(node), 1); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - TfLiteTensor* input = micro_context->AllocateTempInputTensor(node, 0); - TF_LITE_ENSURE(context, input != nullptr); - TF_LITE_ENSURE(context, NumDimensions(input) >= 1); - TfLiteTensor* output = micro_context->AllocateTempOutputTensor(node, 0); - TF_LITE_ENSURE(context, output != nullptr); - - TF_LITE_ENSURE(context, node->user_data != nullptr); - SoftmaxParams* op_data = static_cast(node->user_data); - - auto* params = static_cast(node->builtin_data); - auto ret_val = - CalculateSoftmaxParams(context, input, output, params, op_data); - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(output); - return ret_val; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/space_to_batch_nd.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/space_to_batch_nd.cc deleted file mode 100644 index 21f81312..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/space_to_batch_nd.cc +++ /dev/null @@ -1,120 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/kernels/internal/reference/space_to_batch_nd.h" - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/internal/types.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/micro_utils.h" - -namespace tflite { - -namespace { - -constexpr int kInputTensor = 0; -constexpr int kBlockShapeTensor = 1; -constexpr int kCropsTensor = 2; -constexpr int kOutputTensor = 0; - -// Currently, only 3D NHC and 4D NHWC input/output op_context are supported. -// In case of 3D input, it will be extended to 3D NHWC by adding W=1. -// The 4D array need to have exactly 2 spatial dimensions. -// TODO(b/149952582): Support arbitrary dimension in SpaceToBatchND. -const int kInputOutputMinDimensionNum = 3; -const int kInputOutputMaxDimensionNum = 4; - -void* Init(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(SpaceToBatchParams)); -} - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - - TF_LITE_ENSURE_EQ(context, NumInputs(node), 3); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kInputTensor); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - TF_LITE_ENSURE(context, input != nullptr && output != nullptr); - - TF_LITE_ENSURE(context, NumDimensions(input) >= kInputOutputMinDimensionNum); - TF_LITE_ENSURE(context, NumDimensions(output) >= kInputOutputMinDimensionNum); - TF_LITE_ENSURE(context, NumDimensions(input) <= kInputOutputMaxDimensionNum); - TF_LITE_ENSURE(context, NumDimensions(output) <= kInputOutputMaxDimensionNum); - TF_LITE_ENSURE_TYPES_EQ(context, input->type, output->type); - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(output); - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - const SpaceToBatchParams& params = - *(static_cast(node->user_data)); - - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kInputTensor); - const TfLiteEvalTensor* block_shape = - tflite::micro::GetEvalInput(context, node, kBlockShapeTensor); - const TfLiteEvalTensor* crops = - tflite::micro::GetEvalInput(context, node, kCropsTensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - - switch (input->type) { // Already know in/out types are same. - case kTfLiteFloat32: - reference_ops::SpaceToBatchND( - params, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(block_shape), - tflite::micro::GetTensorData(block_shape), - tflite::micro::GetTensorShape(crops), - tflite::micro::GetTensorData(crops), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - case kTfLiteInt8: - reference_ops::SpaceToBatchND( - params, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(block_shape), - tflite::micro::GetTensorData(block_shape), - tflite::micro::GetTensorShape(crops), - tflite::micro::GetTensorData(crops), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - default: - TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.", - TfLiteTypeGetName(input->type), input->type); - return kTfLiteError; - } - return kTfLiteOk; -} - -} // namespace. - -TfLiteRegistration Register_SPACE_TO_BATCH_ND() { - return tflite::micro::RegisterOp(Init, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/space_to_depth.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/space_to_depth.cc deleted file mode 100644 index 30519b27..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/space_to_depth.cc +++ /dev/null @@ -1,127 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/kernels/internal/reference/space_to_depth.h" - -#include - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/types.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { - -namespace { - -constexpr int kInputTensor = 0; -constexpr int kOutputTensor = 0; -constexpr int kBatchRank = 0; -constexpr int kHeightRank = 1; -constexpr int kWidthRank = 2; -constexpr int kDepthRank = 3; - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - auto* params = - reinterpret_cast(node->builtin_data); - - TF_LITE_ENSURE_EQ(context, NumInputs(node), 1); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - - MicroContext* micro_context = GetMicroContext(context); - - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - - TF_LITE_ENSURE_EQ(context, NumDimensions(input), 4); - - auto data_type = output->type; - TF_LITE_ENSURE(context, - data_type == kTfLiteFloat32 || data_type == kTfLiteInt8); - TF_LITE_ENSURE_TYPES_EQ(context, input->type, output->type); - - const int block_size = params->block_size; - const int input_height = input->dims->data[kHeightRank]; - const int input_width = input->dims->data[kWidthRank]; - int output_height = input_height / block_size; - int output_width = input_width / block_size; - - TF_LITE_ENSURE_EQ(context, input_height, output_height * block_size); - TF_LITE_ENSURE_EQ(context, input_width, output_width * block_size); - - // Relocate dims to the persistent storage arena before changing them, - // otherwise we'd be modifying temporary copies made by the interpreters each - // time they process the layer. - TfLiteEvalTensor* output_eval = - micro::GetEvalOutput(context, node, kOutputTensor); - TF_LITE_ENSURE_OK(context, micro::CreateWritableTensorDimsWithCopy( - context, output, output_eval)); - - output->dims->data[kBatchRank] = input->dims->data[kBatchRank]; - output->dims->data[kHeightRank] = output_height; - output->dims->data[kWidthRank] = output_width; - output->dims->data[kDepthRank] = - input->dims->data[kDepthRank] * block_size * block_size; - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(output); - - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - auto* params = - reinterpret_cast(node->builtin_data); - - const TfLiteEvalTensor* input = - micro::GetEvalInput(context, node, kInputTensor); - TfLiteEvalTensor* output = micro::GetEvalOutput(context, node, kOutputTensor); - - SpaceToDepthParams op_params; - op_params.block_size = params->block_size; - - switch (input->type) { // Already know in/out types are same. - case kTfLiteFloat32: - reference_ops::SpaceToDepth(op_params, micro::GetTensorShape(input), - micro::GetTensorData(input), - micro::GetTensorShape(output), - micro::GetTensorData(output)); - break; - case kTfLiteInt8: - reference_ops::SpaceToDepth(op_params, micro::GetTensorShape(input), - micro::GetTensorData(input), - micro::GetTensorShape(output), - micro::GetTensorData(output)); - break; - default: - TF_LITE_KERNEL_LOG( - context, "SPACE_TO_DEPTH only supports FLOAT32 and INT8, got %s.", - TfLiteTypeGetName(input->type)); - return kTfLiteError; - } - - return kTfLiteOk; -} - -} // namespace - -TfLiteRegistration Register_SPACE_TO_DEPTH() { - return tflite::micro::RegisterOp(nullptr, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/split.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/split.cc deleted file mode 100644 index 06584d45..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/split.cc +++ /dev/null @@ -1,128 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" - -namespace tflite { -namespace ops { -namespace micro { -namespace split { - -template -TfLiteStatus SplitImpl(TfLiteContext* context, TfLiteNode* node, - const TfLiteEvalTensor* input, int axis_value) { - const int output_count = NumOutputs(node); - const TfLiteIntArray* input_dims = input->dims; - const TfLiteEvalTensor* output0 = - tflite::micro::GetEvalOutput(context, node, 0); - const TfLiteIntArray* output_dims = output0->dims; - - const int split_dimensions = input_dims->size; - int axis = axis_value < 0 ? axis_value + split_dimensions : axis_value; - - TFLITE_DCHECK_LT(axis, split_dimensions); - TFLITE_DCHECK_EQ(output_dims->size, split_dimensions); - - int64_t split_size = output_dims->data[axis] * output_count; - - TFLITE_DCHECK_EQ(split_size, input_dims->data[axis]); - int64_t outer_size = 1; - for (int i = 0; i < axis; ++i) { - outer_size *= input_dims->data[i]; - } - - int64_t base_inner_size = 1; - for (int i = axis + 1; i < split_dimensions; ++i) { - base_inner_size *= input_dims->data[i]; - } - - const T* input_ptr = tflite::micro::GetTensorData(input); - for (int k = 0; k < outer_size; ++k) { - for (int i = 0; i < output_count; ++i) { - TfLiteEvalTensor* t = tflite::micro::GetEvalOutput(context, node, i); - T* output_data = tflite::micro::GetTensorData(t); - const int copy_size = output_dims->data[axis] * base_inner_size; - T* output_ptr = output_data + k * copy_size; - for (int j = 0; j < copy_size; ++j) output_ptr[j] = input_ptr[j]; - input_ptr += copy_size; - } - } - - return kTfLiteOk; -} - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - TfLiteTensor* axis = micro_context->AllocateTempInputTensor(node, 0); - TF_LITE_ENSURE(context, axis != nullptr); - - // Dynamic output tensors are needed if axis tensor is not constant. - // But Micro doesn't support dynamic memory allocation, so we only support - // constant axis tensor for now. - TF_LITE_ENSURE_MSG(context, IsConstantTensor(axis), - "Non constant axis tensor not supported"); - - micro_context->DeallocateTempTfLiteTensor(axis); - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* axis = tflite::micro::GetEvalInput(context, node, 0); - const TfLiteEvalTensor* input = tflite::micro::GetEvalInput(context, node, 1); - - int axis_value = tflite::micro::GetTensorData(axis)[0]; - if (axis_value < 0) { - axis_value += input->dims->size; - } - - TF_LITE_ENSURE(context, axis_value >= 0); - TF_LITE_ENSURE(context, axis_value < input->dims->size); - - switch (input->type) { - case kTfLiteFloat32: { - return SplitImpl(context, node, input, axis_value); - } - case kTfLiteInt8: { - return SplitImpl(context, node, input, axis_value); - } - case kTfLiteInt16: { - return SplitImpl(context, node, input, axis_value); - } - case kTfLiteInt32: { - return SplitImpl(context, node, input, axis_value); - } - default: - MicroPrintf("Type %s currently not supported.", - TfLiteTypeGetName(input->type)); - return kTfLiteError; - } - - return kTfLiteOk; -} - -} // namespace split - -TfLiteRegistration Register_SPLIT() { - return tflite::micro::RegisterOp(nullptr, split::Prepare, split::Eval); -} - -} // namespace micro -} // namespace ops -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/split_v.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/split_v.cc deleted file mode 100644 index 3ea35130..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/split_v.cc +++ /dev/null @@ -1,129 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { -namespace ops { -namespace micro { -namespace split_v { - -template -TfLiteStatus SplitImpl(TfLiteContext* context, TfLiteNode* node, - const TfLiteEvalTensor* input, int axis_value) { - const TfLiteIntArray* input_dims = input->dims; - const TfLiteEvalTensor* output0 = - tflite::micro::GetEvalOutput(context, node, 0); - - const int split_dimensions = input_dims->size; - - TFLITE_DCHECK_LT(axis_value, split_dimensions); - TFLITE_DCHECK_EQ(output0->dims->size, split_dimensions); - - int64_t split_size = 0; - const int output_count = NumOutputs(node); - for (int i = 0; i < output_count; i++) { - split_size += - tflite::micro::GetEvalOutput(context, node, i)->dims->data[axis_value]; - } - TFLITE_DCHECK_EQ(split_size, input_dims->data[axis_value]); - int64_t outer_size = 1; - for (int i = 0; i < axis_value; ++i) { - outer_size *= input_dims->data[i]; - } - - int64_t base_inner_size = 1; - for (int i = axis_value + 1; i < split_dimensions; ++i) { - base_inner_size *= input_dims->data[i]; - } - - const T* input_ptr = tflite::micro::GetTensorData(input); - for (int k = 0; k < outer_size; ++k) { - for (int i = 0; i < output_count; ++i) { - TfLiteEvalTensor* output_tensor = - tflite::micro::GetEvalOutput(context, node, i); - T* output_data = tflite::micro::GetTensorData(output_tensor); - const int copy_size = - output_tensor->dims->data[axis_value] * base_inner_size; - T* output_ptr = output_data + k * copy_size; - for (int j = 0; j < copy_size; ++j) output_ptr[j] = input_ptr[j]; - input_ptr += copy_size; - } - } - - return kTfLiteOk; -} - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - TF_LITE_ENSURE_EQ(context, NumInputs(node), 3); - - MicroContext* micro_context = GetMicroContext(context); - // Dynamic output tensors are needed if axis tensor is not constant. - // But Micro doesn't support dynamic memory allocation, so we only support - // constant axis tensor for now. - TfLiteTensor* axis = micro_context->AllocateTempInputTensor(node, 2); - TF_LITE_ENSURE_MSG(context, IsConstantTensor(axis), - "Non constant axis tensor not supported"); - micro_context->DeallocateTempTfLiteTensor(axis); - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input = tflite::micro::GetEvalInput(context, node, 0); - const TfLiteEvalTensor* axis = tflite::micro::GetEvalInput(context, node, 2); - - int axis_value = tflite::micro::GetTensorData(axis)[0]; - if (axis_value < 0) { - axis_value += input->dims->size; - } - - TF_LITE_ENSURE(context, axis_value >= 0); - TF_LITE_ENSURE(context, axis_value < input->dims->size); - - switch (input->type) { - case kTfLiteFloat32: { - return SplitImpl(context, node, input, axis_value); - } - case kTfLiteInt8: { - return SplitImpl(context, node, input, axis_value); - } - case kTfLiteInt16: { - return SplitImpl(context, node, input, axis_value); - } - case kTfLiteInt32: { - return SplitImpl(context, node, input, axis_value); - } - default: - TF_LITE_KERNEL_LOG(context, "Type %s currently not supported.", - TfLiteTypeGetName(input->type)); - return kTfLiteError; - } - return kTfLiteOk; -} - -} // namespace split_v - -TfLiteRegistration Register_SPLIT_V() { - return tflite::micro::RegisterOp(nullptr, split_v::Prepare, split_v::Eval); -} - -} // namespace micro -} // namespace ops -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/squared_difference.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/squared_difference.cc deleted file mode 100644 index ca924e26..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/squared_difference.cc +++ /dev/null @@ -1,247 +0,0 @@ -/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/binary_function.h" -#include "tensorflow/lite/kernels/internal/reference/integer_ops/add.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/micro_context.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" - -namespace tflite { -namespace { -constexpr int kInputTensor1 = 0; -constexpr int kInputTensor2 = 1; -constexpr int kOutputTensor = 0; - -struct OpData { - bool requires_broadcast; - ArithmeticParams arithmetic_params; -}; - -template -T SquaredDifference(T input1, T input2) { - const T difference = input1 - input2; - return difference * difference; -} - -void* SquaredDifferenceInit(TfLiteContext* context, const char* buffer, - size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(OpData)); -} - -TfLiteStatus SquaredDifferencePrepare(TfLiteContext* context, - TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - OpData* data = reinterpret_cast(node->user_data); - data->requires_broadcast = false; - - TF_LITE_ENSURE_EQ(context, NumInputs(node), 2); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - - MicroContext* micro_context = GetMicroContext(context); - - TfLiteTensor* input1 = - micro_context->AllocateTempInputTensor(node, kInputTensor1); - TF_LITE_ENSURE(context, input1 != nullptr); - TfLiteTensor* input2 = - micro_context->AllocateTempInputTensor(node, kInputTensor2); - TF_LITE_ENSURE(context, input2 != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - - TF_LITE_ENSURE_TYPES_EQ(context, input1->type, input2->type); - output->type = input2->type; - - // Ensure the quantization parameters are equivalent. - if (input1->type == kTfLiteInt8) { - const auto& input1_quantization_params = input1->params; - const auto& input2_quantization_params = input2->params; - const auto& output_quantization_params = output->params; - const int32_t integer_type_min = std::numeric_limits::min(); - const int32_t integer_type_max = std::numeric_limits::max(); - TF_LITE_ENSURE(context, - input1_quantization_params.zero_point >= integer_type_min); - TF_LITE_ENSURE(context, - input1_quantization_params.zero_point <= integer_type_max); - TF_LITE_ENSURE(context, - input2_quantization_params.zero_point >= integer_type_min); - TF_LITE_ENSURE(context, - input2_quantization_params.zero_point <= integer_type_max); - TF_LITE_ENSURE(context, - output_quantization_params.zero_point >= integer_type_min); - TF_LITE_ENSURE(context, - output_quantization_params.zero_point <= integer_type_max); - data->arithmetic_params.input1_offset = - -input1_quantization_params.zero_point; - data->arithmetic_params.input2_offset = - -input2_quantization_params.zero_point; - data->arithmetic_params.output_offset = - output_quantization_params.zero_point; - - // shift to make integer for scales. - // 7 is selected so that maximum shifted result 255^2 * (1 << (7 * 2 )) - // does not overflow signed 32-bit integer - data->arithmetic_params.left_shift = 7; - const double twice_max_input_scale = - 2.0 * static_cast(std::max(input1_quantization_params.scale, - input2_quantization_params.scale)); - const double real_input1_multiplier = - static_cast(input1_quantization_params.scale) / - twice_max_input_scale; - double real_input2_multiplier = - static_cast(input2_quantization_params.scale) / - twice_max_input_scale; - const double real_output_multiplier = - (twice_max_input_scale * twice_max_input_scale) / - static_cast((1 << data->arithmetic_params.left_shift * 2) * - output_quantization_params.scale); - QuantizeMultiplierSmallerThanOneExp( - real_input1_multiplier, &data->arithmetic_params.input1_multiplier, - &data->arithmetic_params.input1_shift); - QuantizeMultiplierSmallerThanOneExp( - real_input2_multiplier, &data->arithmetic_params.input2_multiplier, - &data->arithmetic_params.input2_shift); - QuantizeMultiplierSmallerThanOneExp( - real_output_multiplier, &data->arithmetic_params.output_multiplier, - &data->arithmetic_params.output_shift); - data->arithmetic_params.quantized_activation_min = - std::numeric_limits::min(); - data->arithmetic_params.quantized_activation_max = - std::numeric_limits::max(); - } - - data->requires_broadcast = !HaveSameShapes(input1, input2); - - micro_context->DeallocateTempTfLiteTensor(input1); - micro_context->DeallocateTempTfLiteTensor(input2); - micro_context->DeallocateTempTfLiteTensor(output); - return kTfLiteOk; -} - -inline int8_t SquaredDifference(int8_t x, int8_t y, - const ArithmeticParams& params) { - const int32_t input1_val = params.input1_offset + x; - const int32_t input2_val = params.input2_offset + y; - const int32_t shifted_input1_val = input1_val * (1 << params.left_shift); - const int32_t shifted_input2_val = input2_val * (1 << params.left_shift); - const int32_t scaled_input1_val = - MultiplyByQuantizedMultiplierSmallerThanOneExp( - shifted_input1_val, params.input1_multiplier, params.input1_shift); - const int32_t scaled_input2_val = - MultiplyByQuantizedMultiplierSmallerThanOneExp( - shifted_input2_val, params.input2_multiplier, params.input2_shift); - const int32_t raw_diff = scaled_input1_val - scaled_input2_val; - - // Max of this is 255^2 * (1 << 14), so won't overflow 32 bits. - const int32_t squared_raw_diff = raw_diff * raw_diff; - const int32_t raw_output = - MultiplyByQuantizedMultiplierSmallerThanOneExp( - squared_raw_diff, params.output_multiplier, params.output_shift) + - params.output_offset; - const int32_t clamped_output = - std::min(params.quantized_activation_max, - std::max(params.quantized_activation_min, raw_output)); - return static_cast(clamped_output); -} - -template -void EvalQuantizedSquaredDifference(TfLiteContext* context, TfLiteNode* node, - const OpData* data, - const TfLiteEvalTensor* input1, - const TfLiteEvalTensor* input2, - TfLiteEvalTensor* output) { - const auto* op_data = static_cast(node->user_data); - if (data->requires_broadcast) { - reference_integer_ops::BroadcastBinaryFunction4DSlow( - op_data->arithmetic_params, tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output), - reference_integer_ops::CheckArithmeticParams, SquaredDifference); - } else { - const int flat_size = tflite::micro::GetTensorShape(input1).FlatSize(); - reference_integer_ops::ElementWise( - flat_size, op_data->arithmetic_params, - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorData(output), - reference_integer_ops::CheckArithmeticParams, SquaredDifference); - } -} - -template -void EvalSquaredDifference(TfLiteContext* context, TfLiteNode* node, - const OpData* data, const TfLiteEvalTensor* input1, - const TfLiteEvalTensor* input2, - TfLiteEvalTensor* output) { - if (data->requires_broadcast) { - reference_ops::BroadcastBinaryFunction4DSlow( - tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output), SquaredDifference); - } else { - reference_ops::BinaryFunction( - tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output), SquaredDifference); - } -} - -TfLiteStatus SquaredDifferenceEval(TfLiteContext* context, TfLiteNode* node) { - OpData* data = reinterpret_cast(node->user_data); - - const TfLiteEvalTensor* input1 = - tflite::micro::GetEvalInput(context, node, kInputTensor1); - const TfLiteEvalTensor* input2 = - tflite::micro::GetEvalInput(context, node, kInputTensor2); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - - if (output->type == kTfLiteFloat32) { - EvalSquaredDifference(context, node, data, input1, input2, output); - } else if (output->type == kTfLiteInt32) { - EvalSquaredDifference(context, node, data, input1, input2, output); - } else if (output->type == kTfLiteInt8) { - EvalQuantizedSquaredDifference(context, node, data, input1, input2, - output); - } else { - MicroPrintf( - "SquaredDifference only supports FLOAT32, INT32 and INT8 now, got %d.", - output->type); - return kTfLiteError; - } - - return kTfLiteOk; -} -} // namespace - -TfLiteRegistration Register_SQUARED_DIFFERENCE() { - return tflite::micro::RegisterOp( - SquaredDifferenceInit, SquaredDifferencePrepare, SquaredDifferenceEval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/squeeze.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/squeeze.cc deleted file mode 100644 index e81b5b56..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/squeeze.cc +++ /dev/null @@ -1,117 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/process_broadcast_shapes.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/memory_helpers.h" - -namespace tflite { -namespace { - -struct SqueezeContext { - SqueezeContext(TfLiteContext* context, TfLiteNode* node) { - params = reinterpret_cast(node->builtin_data); - micro_context = GetMicroContext(context); - input = micro_context->AllocateTempInputTensor(node, 0); - output = micro_context->AllocateTempOutputTensor(node, 0); - } - ~SqueezeContext() { - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(output); - } - MicroContext* micro_context; - TfLiteSqueezeParams* params; - TfLiteTensor* input; - TfLiteTensor* output; -}; - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - TF_LITE_ENSURE_EQ(context, NumInputs(node), 1); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - - SqueezeContext op_context(context, node); - const int input_num_dims = NumDimensions(op_context.input); - const int num_squeeze_dims = op_context.params->num_squeeze_dims; - - // Determines number of dimensions of output tensor after squeeze. - const TfLiteIntArray* input_dims = op_context.input->dims; - const TfLiteIntArray* output_dims = op_context.output->dims; - const int* squeeze_dims = op_context.params->squeeze_dims; - - constexpr int max_squeeze_dims = 8; - TF_LITE_ENSURE(context, input_num_dims <= max_squeeze_dims); - bool should_squeeze[max_squeeze_dims] = {}; - - if (num_squeeze_dims == 0) { - for (int idx = 0; idx < input_num_dims; ++idx) { - if (input_dims->data[idx] == 1) { - should_squeeze[idx] = true; - } - } - } else { - for (int idx = 0; idx < num_squeeze_dims; ++idx) { - int current = squeeze_dims[idx] < 0 ? squeeze_dims[idx] + input_num_dims - : squeeze_dims[idx]; - TF_LITE_ENSURE(context, current >= 0 && current < input_num_dims && - input_dims->data[current] == 1); - should_squeeze[current] = true; - } - } - - // Ensure output dimensions are big enough. - for (int in_idx = 0, out_idx = 0; in_idx < input_num_dims; ++in_idx) { - if (!should_squeeze[in_idx]) { - TFLITE_CHECK_GE(output_dims->data[out_idx++], input_dims->data[in_idx]); - } - } - - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input = tflite::micro::GetEvalInput(context, node, 0); - - if (input->type == kTfLiteString) { - TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.", - TfLiteTypeGetName(input->type), input->type); - return kTfLiteError; - } - - TfLiteEvalTensor* output = tflite::micro::GetEvalOutput(context, node, 0); - size_t input_byte_size; - size_t output_byte_size; - TF_LITE_ENSURE_OK(context, - TfLiteEvalTensorByteLength(input, &input_byte_size)); - TF_LITE_ENSURE_OK(context, - TfLiteEvalTensorByteLength(output, &output_byte_size)); - - TF_LITE_ENSURE_EQ(context, input_byte_size, output_byte_size); - memcpy(output->data.raw, input->data.raw, input_byte_size); - return kTfLiteOk; -} - -} // namespace - -TfLiteRegistration Register_SQUEEZE() { - return tflite::micro::RegisterOp(nullptr, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/strided_slice.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/strided_slice.cc deleted file mode 100644 index 832e2ccd..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/strided_slice.cc +++ /dev/null @@ -1,202 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/kernels/internal/reference/strided_slice.h" - -#include -#include - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { -namespace ops { -namespace micro { -namespace strided_slice { - -constexpr int kInputTensor = 0; -constexpr int kBeginTensor = 1; -constexpr int kEndTensor = 2; -constexpr int kStridesTensor = 3; -constexpr int kOutputTensor = 0; - -struct StridedSliceContext { - StridedSliceContext(TfLiteContext* context, TfLiteNode* node) { - params = reinterpret_cast(node->builtin_data); - micro_context = GetMicroContext(context); - input = micro_context->AllocateTempInputTensor(node, kInputTensor); - begin = micro_context->AllocateTempInputTensor(node, kBeginTensor); - end = micro_context->AllocateTempInputTensor(node, kEndTensor); - strides = micro_context->AllocateTempInputTensor(node, kStridesTensor); - output = micro_context->AllocateTempOutputTensor(node, kOutputTensor); - dims = NumDimensions(input); - } - ~StridedSliceContext() { - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(begin); - micro_context->DeallocateTempTfLiteTensor(end); - micro_context->DeallocateTempTfLiteTensor(strides); - micro_context->DeallocateTempTfLiteTensor(output); - } - const TfLiteStridedSliceParams* params; - MicroContext* micro_context; - TfLiteTensor* input; - TfLiteTensor* begin; - TfLiteTensor* end; - TfLiteTensor* strides; - TfLiteTensor* output; - int dims; -}; - -// This Op only supports 1-4D cases and since we use the reference 4D -// implementation, the 1-3D tensors are mapped to 4D. -const int kMaxDim = 4; - -tflite::StridedSliceParams BuildStridedSliceParams( - StridedSliceContext* op_context) { - tflite::StridedSliceParams op_params; - op_params.start_indices_count = op_context->dims; - op_params.stop_indices_count = op_context->dims; - op_params.strides_count = op_context->dims; - - for (int i = 0; i < op_context->dims; ++i) { - op_params.start_indices[i] = GetTensorData(op_context->begin)[i]; - op_params.stop_indices[i] = GetTensorData(op_context->end)[i]; - op_params.strides[i] = GetTensorData(op_context->strides)[i]; - } - - op_params.begin_mask = op_context->params->begin_mask; - op_params.ellipsis_mask = 0; - op_params.end_mask = op_context->params->end_mask; - op_params.new_axis_mask = 0; - op_params.shrink_axis_mask = op_context->params->shrink_axis_mask; - return op_params; -} - -// Processes the indexing tensors (begin, end and strides) to resize the -// output tensor. This function is callable from both Prepare() and Eval() as -// long as the caller ensures the indexing tensors are present. -TfLiteStatus CheckOutputSize(TfLiteContext* context, - StridedSliceContext* op_context) { - using ::tflite::strided_slice::StartForAxis; - using ::tflite::strided_slice::StopForAxis; - TfLiteIntArray* output_shape = op_context->output->dims; - int shape_size = 0; - auto op_params = BuildStridedSliceParams(op_context); - auto input_shape = GetTensorShape(op_context->input); - for (int idx = 0; idx < op_context->dims; ++idx) { - int32_t stride = GetTensorData(op_context->strides)[idx]; - TF_LITE_ENSURE_MSG(context, stride != 0, "stride value has to be non-zero"); - int32_t begin = StartForAxis(op_params, input_shape, idx); - int32_t end = StopForAxis(op_params, input_shape, idx, begin); - - // When shrinking an axis, the end position does not matter (and can be - // incorrect when negative indexing is used, see Issue #19260). Always use - // begin + 1 to generate a length 1 slice, since begin has - // already been adjusted for negative indices by StartForAxis. - const bool shrink_axis = op_context->params->shrink_axis_mask & (1 << idx); - if (shrink_axis) { - end = begin + 1; - } - - // This is valid for both positive and negative strides - int32_t dim_shape = std::ceil((end - begin) / static_cast(stride)); - dim_shape = dim_shape < 0 ? 0 : dim_shape; - if (!shrink_axis) { - TF_LITE_ENSURE_EQ(context, output_shape->data[shape_size], dim_shape); - shape_size++; - } - } - TF_LITE_ENSURE_EQ(context, output_shape->size, shape_size); - return kTfLiteOk; -} - -void* Init(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(StridedSliceParams)); -} - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - StridedSliceParams* op_params = - static_cast(node->user_data); - TF_LITE_ENSURE_EQ(context, NumInputs(node), 4); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - StridedSliceContext op_context(context, node); - TF_LITE_ENSURE_MSG(context, op_context.dims <= kMaxDim, - "input dim should not exceed 4"); - auto params = BuildStridedSliceParams(&op_context); - memcpy(op_params, ¶ms, sizeof(StridedSliceParams)); - return CheckOutputSize(context, &op_context); -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - const StridedSliceParams& op_params = - *(static_cast(node->user_data)); - - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kInputTensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - switch (output->type) { - case kTfLiteFloat32: - reference_ops::StridedSlice(op_params, - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - case kTfLiteInt8: - reference_ops::StridedSlice(op_params, - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - case kTfLiteInt16: - reference_ops::StridedSlice( - op_params, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - case kTfLiteInt32: - reference_ops::StridedSlice( - op_params, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - default: - TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.", - TfLiteTypeGetName(input->type), input->type); - return kTfLiteError; - } - return kTfLiteOk; -} -} // namespace strided_slice - -TfLiteRegistration Register_STRIDED_SLICE() { - return tflite::micro::RegisterOp(strided_slice::Init, strided_slice::Prepare, - strided_slice::Eval); -} - -} // namespace micro -} // namespace ops -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/sub.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/sub.cc deleted file mode 100644 index 40bddbad..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/sub.cc +++ /dev/null @@ -1,168 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/kernels/sub.h" - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/add.h" -#include "tensorflow/lite/kernels/internal/reference/process_broadcast_shapes.h" -#include "tensorflow/lite/kernels/internal/reference/sub.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/internal/types.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" - -namespace tflite { - -void* SubInit(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(OpDataSub)); -} - -void EvalSub(TfLiteContext* context, TfLiteNode* node, TfLiteSubParams* params, - const OpDataSub* data, const TfLiteEvalTensor* input1, - const TfLiteEvalTensor* input2, TfLiteEvalTensor* output) { - float output_activation_min, output_activation_max; - CalculateActivationRange(params->activation, &output_activation_min, - &output_activation_max); - tflite::ArithmeticParams op_params; - SetActivationParams(output_activation_min, output_activation_max, &op_params); - if (data->requires_broadcast) { - tflite::reference_ops::BroadcastSubSlow( - op_params, tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } else { - tflite::reference_ops::SubWithActivation( - op_params, tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } -} - -TfLiteStatus EvalSubQuantized(TfLiteContext* context, TfLiteNode* node, - TfLiteSubParams* params, const OpDataSub* data, - const TfLiteEvalTensor* input1, - const TfLiteEvalTensor* input2, - TfLiteEvalTensor* output) { - tflite::ArithmeticParams op_params; - op_params.left_shift = data->left_shift; - op_params.input1_offset = data->input1_offset; - op_params.input1_multiplier = data->input1_multiplier; - op_params.input1_shift = data->input1_shift; - op_params.input2_offset = data->input2_offset; - op_params.input2_multiplier = data->input2_multiplier; - op_params.input2_shift = data->input2_shift; - op_params.output_offset = data->output_offset; - op_params.output_multiplier = data->output_multiplier; - op_params.output_shift = data->output_shift; - SetActivationParams(data->output_activation_min, data->output_activation_max, - &op_params); - bool need_broadcast = reference_ops::ProcessBroadcastShapes( - tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorShape(input2), &op_params); - - switch (output->type) { - case kTfLiteInt8: { - if (need_broadcast) { - tflite::reference_ops::BroadcastQuantSubSlow( - op_params, tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } else { - tflite::reference_ops::Sub( - op_params, tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } - break; - } - case kTfLiteInt16: { - if (need_broadcast) { - tflite::reference_ops::BroadcastQuantSubSlow( - op_params, tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } else { - tflite::reference_ops::Sub( - op_params, tflite::micro::GetTensorShape(input1), - tflite::micro::GetTensorData(input1), - tflite::micro::GetTensorShape(input2), - tflite::micro::GetTensorData(input2), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - } - break; - } - default: - MicroPrintf("Quantized type %s not currently supported.", - TfLiteTypeGetName(output->type)); - return kTfLiteError; - } - return kTfLiteOk; -} - -TfLiteStatus SubEval(TfLiteContext* context, TfLiteNode* node) { - auto* params = reinterpret_cast(node->builtin_data); - - const TfLiteEvalTensor* input1 = - tflite::micro::GetEvalInput(context, node, kSubInputTensor1); - const TfLiteEvalTensor* input2 = - tflite::micro::GetEvalInput(context, node, kSubInputTensor2); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kSubOutputTensor); - - TFLITE_DCHECK(node->user_data != nullptr); - const OpDataSub& data = *(static_cast(node->user_data)); - - if (output->type == kTfLiteFloat32) { - EvalSub(context, node, params, &data, input1, input2, output); - } else if (output->type == kTfLiteInt8 || output->type == kTfLiteInt16) { - TF_LITE_ENSURE_OK(context, EvalSubQuantized(context, node, params, &data, - input1, input2, output)); - } else { - MicroPrintf("Type %s (%d) not supported.", TfLiteTypeGetName(output->type), - output->type); - return kTfLiteError; - } - - return kTfLiteOk; -} - -TfLiteRegistration Register_SUB() { - return tflite::micro::RegisterOp(SubInit, SubPrepare, SubEval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/sub.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/sub.h deleted file mode 100644 index 29900221..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/sub.h +++ /dev/null @@ -1,60 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_SUB_H_ -#define TENSORFLOW_LITE_MICRO_KERNELS_SUB_H_ - -#include - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" - -namespace tflite { - -extern const int kSubInputTensor1; -extern const int kSubInputTensor2; -extern const int kSubOutputTensor; - -struct OpDataSub { - bool requires_broadcast; - - // These fields are used in both the general 8-bit -> 8bit quantized path, - // and the special 16-bit -> 16bit quantized path - int input1_shift; - int input2_shift; - int32_t output_activation_min; - int32_t output_activation_max; - - // These fields are used only in the general 8-bit -> 8bit quantized path - int32_t input1_multiplier; - int32_t input2_multiplier; - int32_t output_multiplier; - int output_shift; - int left_shift; - int32_t input1_offset; - int32_t input2_offset; - int32_t output_offset; -}; - -TfLiteStatus CalculateOpDataSub(TfLiteContext* context, TfLiteSubParams* params, - const TfLiteTensor* input1, - const TfLiteTensor* input2, - TfLiteTensor* output, OpDataSub* data); - -TfLiteStatus SubPrepare(TfLiteContext* context, TfLiteNode* node); - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_KERNELS_SUB_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/sub_common.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/sub_common.cc deleted file mode 100644 index d6647462..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/sub_common.cc +++ /dev/null @@ -1,107 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/add.h" -#include "tensorflow/lite/kernels/internal/reference/process_broadcast_shapes.h" -#include "tensorflow/lite/kernels/internal/reference/sub.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/internal/types.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/sub.h" - -namespace tflite { - -const int kSubInputTensor1 = 0; -const int kSubInputTensor2 = 1; -const int kSubOutputTensor = 0; - -TfLiteStatus CalculateOpDataSub(TfLiteContext* context, TfLiteSubParams* params, - const TfLiteTensor* input1, - const TfLiteTensor* input2, - TfLiteTensor* output, OpDataSub* data) { - data->requires_broadcast = !HaveSameShapes(input1, input2); - - if (output->type == kTfLiteInt8 || output->type == kTfLiteInt16) { - // 8bit -> 8bit general quantized path, with general rescalings - data->input1_offset = -input1->params.zero_point; - data->input2_offset = -input2->params.zero_point; - data->output_offset = output->params.zero_point; - - // The shift is set to 15 in case of 16-bit and 20 in case of 8-bit, - // accordingly. In case of 16-bit we have 65535 << 15 which is less than 1 - // << 31, therefore the addition will still fit in a 32 bit accumulator. - data->left_shift = output->type == kTfLiteInt16 ? 15 : 20; - const float twice_max_input_scale = - 2 * std::max(input1->params.scale, input2->params.scale); - const double real_input1_multiplier = - static_cast(input1->params.scale / twice_max_input_scale); - const double real_input2_multiplier = - static_cast(input2->params.scale / twice_max_input_scale); - const double real_output_multiplier = - static_cast(twice_max_input_scale / - ((1 << data->left_shift) * output->params.scale)); - - QuantizeMultiplierSmallerThanOneExp( - real_input1_multiplier, &data->input1_multiplier, &data->input1_shift); - - QuantizeMultiplierSmallerThanOneExp( - real_input2_multiplier, &data->input2_multiplier, &data->input2_shift); - - QuantizeMultiplierSmallerThanOneExp( - real_output_multiplier, &data->output_multiplier, &data->output_shift); - - TF_LITE_ENSURE_STATUS(CalculateActivationRangeQuantized( - context, params->activation, output, &data->output_activation_min, - &data->output_activation_max)); - } - - return kTfLiteOk; -} - -TfLiteStatus SubPrepare(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - TFLITE_DCHECK(node->builtin_data != nullptr); - - OpDataSub* data = static_cast(node->user_data); - auto* params = reinterpret_cast(node->builtin_data); - - MicroContext* micro_context = GetMicroContext(context); - - TfLiteTensor* input1 = - micro_context->AllocateTempInputTensor(node, kSubInputTensor1); - TF_LITE_ENSURE(context, input1 != nullptr); - TfLiteTensor* input2 = - micro_context->AllocateTempInputTensor(node, kSubInputTensor2); - TF_LITE_ENSURE(context, input2 != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kSubOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - - TF_LITE_ENSURE_STATUS( - CalculateOpDataSub(context, params, input1, input2, output, data)); - - micro_context->DeallocateTempTfLiteTensor(input1); - micro_context->DeallocateTempTfLiteTensor(input2); - micro_context->DeallocateTempTfLiteTensor(output); - return kTfLiteOk; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/svdf.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/svdf.cc deleted file mode 100644 index 5994db94..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/svdf.cc +++ /dev/null @@ -1,106 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/kernels/svdf.h" - -#include - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/kernels/activation_utils.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" -#include "tensorflow/lite/micro/micro_utils.h" - -namespace tflite { -namespace { - -void* Init(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(OpDataSvdf)); -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - auto* params = reinterpret_cast(node->builtin_data); - TFLITE_DCHECK(node->user_data != nullptr); - const OpDataSvdf& data = *(static_cast(node->user_data)); - - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kSvdfInputTensor); - const TfLiteEvalTensor* weights_feature = - tflite::micro::GetEvalInput(context, node, kSvdfWeightsFeatureTensor); - const TfLiteEvalTensor* weights_time = - tflite::micro::GetEvalInput(context, node, kSvdfWeightsTimeTensor); - const TfLiteEvalTensor* bias = - (NumInputs(node) == 5) - ? tflite::micro::GetEvalInput(context, node, kSvdfBiasTensor) - : nullptr; - TfLiteEvalTensor* activation_state = tflite::micro::GetMutableEvalInput( - context, node, kSvdfInputActivationStateTensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kSvdfOutputTensor); - - switch (weights_feature->type) { - case kTfLiteFloat32: { - EvalFloatSvdfReference( - context, node, input, weights_feature, weights_time, bias, params, - data.scratch_tensor_index, activation_state, output); - return kTfLiteOk; - break; - } - - case kTfLiteInt8: { - switch (weights_time->type) { - case kTfLiteInt16: { - EvalInt16SvdfReference(context, node, input, weights_feature, - weights_time, bias, params, activation_state, - output, data); - return kTfLiteOk; - break; - } - case kTfLiteInt8: { - EvalInt8SvdfReference(context, node, input, weights_feature, - weights_time, bias, params, activation_state, - output, data); - return kTfLiteOk; - break; - } - default: - MicroPrintf("Type %s not currently supported.", - TfLiteTypeGetName(weights_time->type)); - return kTfLiteError; - } - } - - default: - MicroPrintf("Type %s not currently supported.", - TfLiteTypeGetName(weights_feature->type)); - return kTfLiteError; - } - return kTfLiteOk; -} - -} // namespace - -TfLiteRegistration Register_SVDF() { - return tflite::micro::RegisterOp(Init, PrepareSvdf, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/svdf.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/svdf.h deleted file mode 100644 index 7a04a5c6..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/svdf.h +++ /dev/null @@ -1,99 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_SVDF_H_ -#define TENSORFLOW_LITE_MICRO_KERNELS_SVDF_H_ - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" - -namespace tflite { - -struct OpDataSvdf { - int32_t effective_scale_1_a; - int32_t effective_scale_2_a; - // b versions of each scale are kept at int since the numbers are just the - // shift value - typically between [-32, 32]. - int effective_scale_1_b; - int effective_scale_2_b; - int scratch_tensor_index; - int scratch_output_tensor_index; - - // Cached tensor zero point values for quantized operations. - int input_zero_point; - int output_zero_point; - int activation_state_zero_point; -}; - -// Input tensors. -extern const int kSvdfInputTensor; -extern const int kSvdfWeightsFeatureTensor; -extern const int kSvdfWeightsTimeTensor; -extern const int kSvdfBiasTensor; -// This is a variable tensor, and will be modified by this op. -extern const int kSvdfInputActivationStateTensor; - -// Output tensor. -extern const int kSvdfOutputTensor; - -void EvalInt8SvdfReference(TfLiteContext* context, TfLiteNode* node, - const TfLiteEvalTensor* input_tensor, - const TfLiteEvalTensor* weights_feature_tensor, - const TfLiteEvalTensor* weights_time_tensor, - const TfLiteEvalTensor* bias_tensor, - const TfLiteSVDFParams* params, - TfLiteEvalTensor* activation_state_tensor, - TfLiteEvalTensor* output_tensor, - const OpDataSvdf& data); - -// TODO(#523): remove 16-bit code when no longer needed. -void EvalInt16SvdfReference(TfLiteContext* context, TfLiteNode* node, - const TfLiteEvalTensor* input_tensor, - const TfLiteEvalTensor* weights_feature_tensor, - const TfLiteEvalTensor* weights_time_tensor, - const TfLiteEvalTensor* bias_tensor, - const TfLiteSVDFParams* params, - TfLiteEvalTensor* activation_state_tensor, - TfLiteEvalTensor* output_tensor, - const OpDataSvdf& data); - -void EvalFloatSvdfReference( - TfLiteContext* context, TfLiteNode* node, const TfLiteEvalTensor* input, - const TfLiteEvalTensor* weights_feature, - const TfLiteEvalTensor* weights_time, const TfLiteEvalTensor* bias, - const TfLiteSVDFParams* params, int scratch_tensor_index, - TfLiteEvalTensor* activation_state, TfLiteEvalTensor* output); - -TfLiteStatus PrepareSvdf(TfLiteContext* context, TfLiteNode* node); - -// This is the most generic TfLiteRegistration. The actual supported types may -// still be target dependent. The only requirement is that every implementation -// (reference or optimized) must define this function. -TfLiteRegistration Register_SVDF(); - -#if defined(HEXAGON) -TfLiteRegistration Register_SVDF_INT8(); - -#else -// Note that while this block gets used for both reference and optimized kernels -// that do not have any specialized implementations, the only goal here is to -// define fallback implementation that allow reference kernels to still be used -// from applications that call a more specific kernel variant. - -inline TfLiteRegistration Register_SVDF_INT8() { return Register_SVDF(); } - -#endif -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_KERNELS_SVDF_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/svdf_common.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/svdf_common.cc deleted file mode 100644 index fb92b4fd..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/svdf_common.cc +++ /dev/null @@ -1,514 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/kernels/activation_utils.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/svdf.h" -#include "tensorflow/lite/micro/micro_utils.h" - -namespace tflite { - -/** - * This version of SVDF is specific to TFLite Micro. It contains the following - * differences between the TFLite version: - * - * 1.) Scratch tensor allocation - scratch tensors must be known ahead of time - * for the Micro interpreter. - * 2.) Output dimensions - the TFLite version determines output size and runtime - * and resizes the output tensor. Micro runtime does not support tensor - * resizing. - */ - -const int kSvdfInputTensor = 0; -const int kSvdfWeightsFeatureTensor = 1; -const int kSvdfWeightsTimeTensor = 2; -const int kSvdfBiasTensor = 3; -const int kSvdfInputActivationStateTensor = - 4; // This is a variable tensor, and will be modified by this op. -const int kSvdfOutputTensor = 0; - -template -void EvalIntegerSvdfReference(TfLiteContext* context, TfLiteNode* node, - const TfLiteEvalTensor* input_tensor, - const TfLiteEvalTensor* weights_feature_tensor, - const TfLiteEvalTensor* weights_time_tensor, - const TfLiteEvalTensor* bias_tensor, - const TfLiteSVDFParams* params, - TfLiteEvalTensor* activation_state_tensor, - TfLiteEvalTensor* output_tensor, - const OpDataSvdf& data) { - const int n_rank = params->rank; - const int n_batch = input_tensor->dims->data[0]; - const int n_input = input_tensor->dims->data[1]; - const int n_filter = weights_feature_tensor->dims->data[0]; - const int n_unit = n_filter / n_rank; - const int n_memory = weights_time_tensor->dims->data[1]; - - TFLITE_DCHECK(context != nullptr); - TFLITE_DCHECK(context->GetScratchBuffer != nullptr); - - int32_t* scratch_tensor = static_cast( - context->GetScratchBuffer(context, data.scratch_tensor_index)); - int32_t* scratch_output_tensor = static_cast( - context->GetScratchBuffer(context, data.scratch_output_tensor_index)); - - // Shift states. - T* const state_ptr = tflite::micro::GetTensorData(activation_state_tensor); - - // Left shift the activation_state. - { - T* new_state_start = state_ptr; - const T* old_state_start = state_ptr + 1; - const T* old_state_end = state_ptr + n_batch * n_filter * n_memory; - while (old_state_start != old_state_end) { - *new_state_start++ = *old_state_start++; - } - } - - // Note: no need to clear the latest activation, matmul is not accumulative. - - // Feature matmul. - { - T* state = tflite::micro::GetTensorData(activation_state_tensor); - const int8_t* input = tflite::micro::GetTensorData(input_tensor); - const int8_t* weight_feature = - tflite::micro::GetTensorData(weights_feature_tensor); - const int32_t output_max = std::numeric_limits::max(); - const int32_t output_min = std::numeric_limits::min(); - T* result_in_batch = state + (n_memory - 1); - for (int b = 0; b < n_batch; b++) { - const int8_t* matrix_ptr = weight_feature; - for (int r = 0; r < n_filter; r++) { - int32_t dot_prod = 0; - const int8_t* vector_in_batch = input + b * n_input; - for (int c = 0; c < n_input; c++) { - dot_prod += - *matrix_ptr++ * (*vector_in_batch++ - data.input_zero_point); - } - dot_prod = MultiplyByQuantizedMultiplier( - dot_prod, data.effective_scale_1_a, data.effective_scale_1_b); - dot_prod = std::min(std::max(output_min, dot_prod), output_max); - // The int16 version of the op assumes a zero_point of 0. This - // code accounts for the potentially non-zero zero_point for the int8 - // version of the op. - *result_in_batch = data.activation_state_zero_point + dot_prod; - result_in_batch += n_memory; - } - } - } - - // Time. - { - for (int b = 0; b < n_batch; ++b) { - int32_t* scratch_ptr_batch = scratch_tensor + b * n_filter; - - // Perform batched vector dot product: - const T* vector1_ptr = - tflite::micro::GetTensorData(weights_time_tensor); - const T* vector2_ptr = - tflite::micro::GetTensorData(activation_state_tensor) + - b * n_memory * n_filter; - - for (int i = 0; i < n_filter; i++) { - *scratch_ptr_batch = 0; - for (int j = 0; j < n_memory; j++) { - *scratch_ptr_batch += - *vector1_ptr++ * - (*vector2_ptr++ - data.activation_state_zero_point); - } - scratch_ptr_batch++; - } - } - } - - // Reduce, add bias, rescale, activation. - { - // Add bias. - if (bias_tensor) { - // Vector batch assign: - const int32_t* bias_data = - tflite::micro::GetTensorData(bias_tensor); - for (int i = 0; i < n_batch; ++i) { - int32_t* output_ptr = scratch_output_tensor + i * n_unit; - const int32_t* bias_ptr = bias_data; - for (int j = 0; j < n_unit; ++j) { - *output_ptr++ = *bias_ptr++; - } - } - } else { - int32_t* output_ptr = scratch_output_tensor; - for (int i = 0; i < n_batch * n_unit; ++i) { - *output_ptr++ = 0; - } - } - - // Reduce. - for (int b = 0; b < n_batch; ++b) { - int32_t* output_temp_ptr = scratch_output_tensor + b * n_unit; - int32_t* scratch_ptr_batch = scratch_tensor + b * n_filter; - - // Reduction sum vector - for (int i = 0; i < n_unit; ++i) { - for (int j = 0; j < n_rank; ++j) { - output_temp_ptr[i] += *scratch_ptr_batch++; - } - } - } - - // Rescale. - const int32_t output_max = std::numeric_limits::max(); - const int32_t output_min = std::numeric_limits::min(); - for (int i = 0; i < n_batch * n_unit; ++i) { - int32_t x1 = scratch_output_tensor[i]; - int32_t x2 = MultiplyByQuantizedMultiplier(x1, data.effective_scale_2_a, - data.effective_scale_2_b); - int32_t x3 = x2 + data.output_zero_point; - int32_t x4 = std::min(std::max(output_min, x3), output_max); - tflite::micro::GetTensorData(output_tensor)[i] = - static_cast(x4); - } - } -} - -/** - * Generate two versions of the integer code. One with int16_t type for the - * time weights and the activation state, and another one with int8_t for the - * same. - */ - -void EvalInt16SvdfReference(TfLiteContext* context, TfLiteNode* node, - const TfLiteEvalTensor* input_tensor, - const TfLiteEvalTensor* weights_feature_tensor, - const TfLiteEvalTensor* weights_time_tensor, - const TfLiteEvalTensor* bias_tensor, - const TfLiteSVDFParams* params, - TfLiteEvalTensor* activation_state_tensor, - TfLiteEvalTensor* output_tensor, - const OpDataSvdf& data) { - EvalIntegerSvdfReference( - context, node, input_tensor, weights_feature_tensor, weights_time_tensor, - bias_tensor, params, activation_state_tensor, output_tensor, data); -} - -void EvalInt8SvdfReference(TfLiteContext* context, TfLiteNode* node, - const TfLiteEvalTensor* input_tensor, - const TfLiteEvalTensor* weights_feature_tensor, - const TfLiteEvalTensor* weights_time_tensor, - const TfLiteEvalTensor* bias_tensor, - const TfLiteSVDFParams* params, - TfLiteEvalTensor* activation_state_tensor, - TfLiteEvalTensor* output_tensor, - const OpDataSvdf& data) { - EvalIntegerSvdfReference( - context, node, input_tensor, weights_feature_tensor, weights_time_tensor, - bias_tensor, params, activation_state_tensor, output_tensor, data); -} - -static inline void ApplyTimeWeightsBiasAndActivation( - int batch_size, int memory_size, int num_filters, int num_units, int rank, - const float* const weights_time_ptr, const float* const bias_ptr, - TfLiteFusedActivation activation, float* const state_ptr, - float* const scratch_ptr, float* const output_ptr) { - // Compute matmul(activation_state, weights_time). - for (int b = 0; b < batch_size; ++b) { - // Perform batched vector dot product: - float* scratch_ptr_batch = scratch_ptr + b * num_filters; - const float* vector1_ptr = weights_time_ptr; - const float* vector2_ptr = state_ptr + b * memory_size * num_filters; - for (int i = 0; i < num_filters; ++i) { - *scratch_ptr_batch = 0.f; - for (int j = 0; j < memory_size; ++j) { - *scratch_ptr_batch += *vector1_ptr++ * *vector2_ptr++; - } - scratch_ptr_batch++; - } - } - - // Initialize output with bias if provided. - if (bias_ptr) { - // VectorBatchVectorAssign - for (int i = 0; i < batch_size; ++i) { - float* output_data = output_ptr + i * num_units; - const float* bias_data = bias_ptr; - for (int j = 0; j < num_units; ++j) { - *output_data++ = *bias_data++; - } - } - } else { - float* output_data = output_ptr; - for (int i = 0; i < batch_size * num_units; ++i) { - *output_data++ = 0.0f; - } - } - - // Reduction sum. - for (int b = 0; b < batch_size; ++b) { - float* output_ptr_batch = output_ptr + b * num_units; - float* scratch_ptr_batch = scratch_ptr + b * num_filters; - - // Reduction sum vector - for (int i = 0; i < num_units; ++i) { - for (int j = 0; j < rank; j++) { - output_ptr_batch[i] += *scratch_ptr_batch++; - } - } - } - - // Apply activation. - for (int b = 0; b < batch_size; ++b) { - float* output_ptr_batch = output_ptr + b * num_units; - for (int i = 0; i < num_units; ++i) { - *output_ptr_batch = - tflite::ops::micro::ActivationValFloat(activation, *output_ptr_batch); - ++output_ptr_batch; - } - } -} - -void EvalFloatSvdfReference( - TfLiteContext* context, TfLiteNode* node, const TfLiteEvalTensor* input, - const TfLiteEvalTensor* weights_feature, - const TfLiteEvalTensor* weights_time, const TfLiteEvalTensor* bias, - const TfLiteSVDFParams* params, int scratch_tensor_index, - TfLiteEvalTensor* activation_state, TfLiteEvalTensor* output) { - const int rank = params->rank; - const int batch_size = input->dims->data[0]; - const int input_size = input->dims->data[1]; - const int num_filters = weights_feature->dims->data[0]; - const int num_units = num_filters / rank; - const int memory_size = weights_time->dims->data[1]; - - const float* weights_feature_ptr = - tflite::micro::GetTensorData(weights_feature); - const float* weights_time_ptr = - tflite::micro::GetTensorData(weights_time); - const float* bias_ptr = tflite::micro::GetTensorData(bias); - const float* input_ptr = tflite::micro::GetTensorData(input); - - float* state_ptr = tflite::micro::GetTensorData(activation_state); - - TFLITE_DCHECK(context != nullptr); - TFLITE_DCHECK(context->GetScratchBuffer != nullptr); - - float* scratch_ptr = static_cast( - context->GetScratchBuffer(context, scratch_tensor_index)); - - float* output_ptr = tflite::micro::GetTensorData(output); - - // Left shift the activation_state. - { - float* new_state_start = state_ptr; - const float* old_state_start = state_ptr + 1; - const float* old_state_end = - state_ptr + batch_size * num_filters * memory_size; - while (old_state_start != old_state_end) { - *new_state_start++ = *old_state_start++; - } - } - - // Note: no need to clear the latest activation, matmul is not accumulative. - - // Compute conv1d(inputs, weights_feature). - // The activation_state's rightmost column is used to save current cycle - // activation. This is achieved by starting at state_ptr[memory_size - 1] and - // having the stride equal to memory_size. - - // Perform batched matrix vector multiply operation: - { - const float* matrix = weights_feature_ptr; - const float* vector = input_ptr; - float* result = &state_ptr[memory_size - 1]; - float* result_in_batch = result; - for (int i = 0; i < batch_size; ++i) { - const float* matrix_ptr = matrix; - for (int j = 0; j < num_filters; ++j) { - float dot_prod = 0.0f; - const float* vector_in_batch = vector + i * input_size; - for (int k = 0; k < input_size; ++k) { - dot_prod += *matrix_ptr++ * *vector_in_batch++; - } - *result_in_batch = dot_prod; - result_in_batch += memory_size; - } - } - } - - ApplyTimeWeightsBiasAndActivation( - batch_size, memory_size, num_filters, num_units, rank, weights_time_ptr, - bias_ptr, params->activation, state_ptr, scratch_ptr, output_ptr); -} - -TfLiteStatus PrepareSvdf(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->builtin_data != nullptr); - - const auto* params = static_cast(node->builtin_data); - - MicroContext* micro_context = GetMicroContext(context); - - // Validate Tensor Inputs (dtype depends on quantization): - // [0] = Input, {2, batch_size, input_size} - // [1] = Weights Feature, {2, num_filters, input_size} - // [2] = Weights Time, {2, num_filters, memory_size} - // [3] = Bias (optional), {1, num_units} - // [4] = Activation State (variable), - // {2, batch_size, memory_size * num_filters} - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kSvdfInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* weights_feature = - micro_context->AllocateTempInputTensor(node, kSvdfWeightsFeatureTensor); - TF_LITE_ENSURE(context, weights_feature != nullptr); - TfLiteTensor* weights_time = - micro_context->AllocateTempInputTensor(node, kSvdfWeightsTimeTensor); - TF_LITE_ENSURE(context, weights_time != nullptr); - TfLiteTensor* bias = - micro_context->AllocateTempInputTensor(node, kSvdfBiasTensor); - TfLiteTensor* activation_state = micro_context->AllocateTempInputTensor( - node, kSvdfInputActivationStateTensor); - TF_LITE_ENSURE(context, activation_state != nullptr); - - // Define input constants based on input tensor definition above: - const int rank = params->rank; - const int input_size = input->dims->data[1]; - const int batch_size = input->dims->data[0]; - const int num_filters = weights_feature->dims->data[0]; - TF_LITE_ENSURE_EQ(context, num_filters % rank, 0); - const int num_units = num_filters / rank; - const int memory_size = weights_time->dims->data[1]; - - // Validate Input Tensor: - TF_LITE_ENSURE(context, - input->type == kTfLiteFloat32 || input->type == kTfLiteInt8); - TF_LITE_ENSURE_EQ(context, NumDimensions(input), 2); - - // Validate Tensor Output: - // [0] = float/int8_t, {2, batch_size, num_units} - TF_LITE_ENSURE_EQ(context, node->outputs->size, 1); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kSvdfOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - TF_LITE_ENSURE_EQ(context, NumDimensions(output), 2); - TF_LITE_ENSURE_EQ(context, output->dims->data[0], batch_size); - TF_LITE_ENSURE_EQ(context, output->dims->data[1], num_units); - - // Validate Weights Feature Input Tensor: - TF_LITE_ENSURE_EQ(context, NumDimensions(weights_feature), 2); - TF_LITE_ENSURE_EQ(context, weights_feature->dims->data[1], input_size); - - // Validate Weights Time Input Tensor: - TF_LITE_ENSURE_EQ(context, NumDimensions(weights_time), 2); - TF_LITE_ENSURE_EQ(context, weights_time->dims->data[0], num_filters); - TF_LITE_ENSURE_EQ(context, weights_time->dims->data[1], memory_size); - - // Validate Optional Bias Input Tensor: - if (bias != nullptr) { - TF_LITE_ENSURE_EQ(context, bias->dims->data[0], num_units); - } - - // Validate Activation State Input Tensor: - TF_LITE_ENSURE_EQ(context, NumDimensions(activation_state), 2); - TF_LITE_ENSURE_EQ(context, activation_state->dims->data[0], batch_size); - TF_LITE_ENSURE_EQ(context, activation_state->dims->data[1], - memory_size * num_filters); - // Since is_variable is not part of TFLiteEvalTensor, check is_variable here. - TF_LITE_ENSURE_EQ(context, activation_state->is_variable, true); - - TF_LITE_ENSURE_EQ(context, node->inputs->size, 5); - - TFLITE_DCHECK(node->user_data != nullptr); - OpDataSvdf* data = static_cast(node->user_data); - - if (input->type == kTfLiteInt8) { - TF_LITE_ENSURE_EQ(context, weights_feature->type, kTfLiteInt8); - TF_LITE_ENSURE(context, (weights_time->type == kTfLiteInt16) || - (weights_time->type == kTfLiteInt8)); - TF_LITE_ENSURE(context, (activation_state->type == kTfLiteInt16) || - (activation_state->type == kTfLiteInt8)); - if (bias != nullptr) { - TF_LITE_ENSURE_EQ(context, bias->type, kTfLiteInt32); - } - - TF_LITE_ENSURE_TYPES_EQ(context, output->type, kTfLiteInt8); - - const double effective_scale_1 = static_cast( - input->params.scale * weights_feature->params.scale / - activation_state->params.scale); - const double effective_scale_2 = - static_cast(activation_state->params.scale * - weights_time->params.scale / output->params.scale); - - // TODO(b/162018098): Use TF_LITE_ENSURE_NEAR when it is ready. - TF_LITE_ENSURE( - context, - std::abs(static_cast(bias->params.scale) - - static_cast(activation_state->params.scale * - weights_time->params.scale)) < 1e-5); - - QuantizeMultiplier(effective_scale_1, &(data->effective_scale_1_a), - &(data->effective_scale_1_b)); - QuantizeMultiplier(effective_scale_2, &(data->effective_scale_2_a), - &(data->effective_scale_2_b)); - - data->input_zero_point = input->params.zero_point; - data->output_zero_point = output->params.zero_point; - data->activation_state_zero_point = activation_state->params.zero_point; - - TFLITE_DCHECK(context->RequestScratchBufferInArena != nullptr); - - const TfLiteStatus scratch_status = context->RequestScratchBufferInArena( - context, batch_size * num_filters * sizeof(int32_t), - &(data->scratch_tensor_index)); - TF_LITE_ENSURE_OK(context, scratch_status); - - const TfLiteStatus scratch_output_status = - context->RequestScratchBufferInArena( - context, batch_size * num_units * sizeof(int32_t), - &(data->scratch_output_tensor_index)); - TF_LITE_ENSURE_OK(context, scratch_output_status); - } else { - TF_LITE_ENSURE_EQ(context, weights_feature->type, kTfLiteFloat32); - TF_LITE_ENSURE_EQ(context, weights_time->type, kTfLiteFloat32); - TF_LITE_ENSURE_EQ(context, activation_state->type, kTfLiteFloat32); - if (bias != nullptr) { - TF_LITE_ENSURE_EQ(context, bias->type, kTfLiteFloat32); - } - TF_LITE_ENSURE_TYPES_EQ(context, output->type, kTfLiteFloat32); - - TFLITE_DCHECK(context->RequestScratchBufferInArena != nullptr); - const TfLiteStatus scratch_status = context->RequestScratchBufferInArena( - context, batch_size * num_filters * sizeof(float), - &(data->scratch_tensor_index)); - TF_LITE_ENSURE_OK(context, scratch_status); - } - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(weights_feature); - micro_context->DeallocateTempTfLiteTensor(weights_time); - micro_context->DeallocateTempTfLiteTensor(activation_state); - micro_context->DeallocateTempTfLiteTensor(output); - micro_context->DeallocateTempTfLiteTensor(bias); - return kTfLiteOk; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/tanh.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/tanh.cc deleted file mode 100644 index e97a9035..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/tanh.cc +++ /dev/null @@ -1,203 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/kernels/internal/reference/integer_ops/tanh.h" - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/tanh.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/micro_utils.h" - -namespace tflite { -namespace ops { -namespace micro { -namespace activations { -namespace { -constexpr int kInputTensor = 0; -constexpr int kOutputTensor = 0; - -struct OpData { - int32_t input_zero_point; - int32_t input_range_radius; - int32_t input_multiplier; - int input_left_shift; -}; - -void* TanhInit(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(OpData)); -} - -TfLiteStatus CalculateArithmeticOpData(TfLiteContext* context, TfLiteNode* node, - OpData* data) { - MicroContext* micro_context = GetMicroContext(context); - TF_LITE_ENSURE_EQ(context, NumInputs(node), 1); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - - TF_LITE_ENSURE_TYPES_EQ(context, input->type, output->type); - - if (input->type == kTfLiteInt8) { - static constexpr int kInputIntegerBits = 4; - const double input_real_multiplier = - static_cast(input->params.scale) * - static_cast(1 << (31 - kInputIntegerBits)); - - const double q = std::frexp(input_real_multiplier, &data->input_left_shift); - data->input_multiplier = static_cast(TfLiteRound(q * (1ll << 31))); - - data->input_range_radius = - CalculateInputRadius(kInputIntegerBits, data->input_left_shift, 31); - } - - if (input->type == kTfLiteInt16) { - static constexpr int kInputIntegerBits = 3; - static constexpr int kOutputFractionalBits = 15; - - // These operators are implemented in fixed-point arithmetic, - // which intrinsically wants symmetric ranges (zero_point==0) - // and power-of-two scales (power-of-two is abbreviated below as POT). - // While more general support would be possible by means of rescaling, - // that would add some overhead and some loss of accuracy and wouldn't - // be used at the moment as current quantized LSTM applications are - // happy with symmetric, power-of-two-scales quantization. So we just - // implement that narrow case only for now. - - TF_LITE_ENSURE_EQ(context, input->params.zero_point, 0); - TF_LITE_ENSURE_EQ(context, output->params.zero_point, 0); - - int input_scale_log2_rounded; - bool param_scale_pot = - CheckedLog2(input->params.scale, &input_scale_log2_rounded); - - data->input_left_shift = - (15 - kInputIntegerBits) + input_scale_log2_rounded; - param_scale_pot &= - (data->input_left_shift == 0 || data->input_left_shift == 1); - - if (param_scale_pot) { - data->input_multiplier = 0; - } else { - // Calculate multiplier to change input scale to 1/(3*4096) - // as required by the table lookup. - // The number 3.0 in the multiplier comes from here, - // because the interval is [-10.7, 10.7] instead of [-8, 8]. - // So, in this scaling +/-2^17 represents +/-10.7. - - double multiplier = - static_cast(input->params.scale) * 4096.0 * 3.0; - data->input_left_shift = 0; - - while (multiplier <= 32767.0 / 2.0 && data->input_left_shift <= 30) { - data->input_left_shift++; - multiplier = multiplier * 2.0; - } - - data->input_multiplier = static_cast(multiplier); - } - - int output_scale_log2_rounded; - TF_LITE_ENSURE( - context, CheckedLog2(output->params.scale, &output_scale_log2_rounded)); - TF_LITE_ENSURE_EQ(context, output_scale_log2_rounded, - -kOutputFractionalBits); - } - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(output); - return kTfLiteOk; -} - -TfLiteStatus TanhPrepare(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - - OpData* data = static_cast(node->user_data); - - MicroContext* micro_context = GetMicroContext(context); - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - data->input_zero_point = input->params.zero_point; - TF_LITE_ENSURE_OK(context, CalculateArithmeticOpData(context, node, data)); - - micro_context->DeallocateTempTfLiteTensor(input); - return kTfLiteOk; -} - -} // namespace - -TfLiteStatus TanhEval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kInputTensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - - TFLITE_DCHECK(node->user_data != nullptr); - const OpData& data = *(static_cast(node->user_data)); - - switch (input->type) { - case kTfLiteFloat32: { - reference_ops::Tanh(tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - return kTfLiteOk; - } break; - case kTfLiteInt16: { - reference_integer_ops::Tanh( - data.input_multiplier, data.input_left_shift, - tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - return kTfLiteOk; - } break; - case kTfLiteInt8: { - reference_integer_ops::Tanh( - data.input_zero_point, data.input_range_radius, data.input_multiplier, - data.input_left_shift, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - return kTfLiteOk; - } break; - default: - TF_LITE_KERNEL_LOG(context, "Input %s, output %s not supported.", - TfLiteTypeGetName(input->type), - TfLiteTypeGetName(output->type)); - return kTfLiteError; - } -} - -} // namespace activations - -TfLiteRegistration Register_TANH() { - return tflite::micro::RegisterOp( - activations::TanhInit, activations::TanhPrepare, activations::TanhEval); -} -} // namespace micro -} // namespace ops -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/transpose.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/transpose.cc deleted file mode 100644 index 9f77e04d..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/transpose.cc +++ /dev/null @@ -1,121 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/kernels/internal/reference/transpose.h" - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/internal/types.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { -namespace { - -constexpr int kInputTensor = 0; -constexpr int kPermTensor = 1; -constexpr int kOutputTensor = 0; - -struct TransposeContext { - TransposeContext(TfLiteContext* context, TfLiteNode* node) { - micro_context = GetMicroContext(context); - input = micro_context->AllocateTempInputTensor(node, kInputTensor); - perm = micro_context->AllocateTempInputTensor(node, kPermTensor); - output = micro_context->AllocateTempOutputTensor(node, kOutputTensor); - } - ~TransposeContext() { - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(perm); - micro_context->DeallocateTempTfLiteTensor(output); - } - MicroContext* micro_context; - TfLiteTensor* input; - TfLiteTensor* perm; - TfLiteTensor* output; -}; - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - TF_LITE_ENSURE_EQ(context, NumInputs(node), 2); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - - TransposeContext op_context(context, node); - - // Ensure validity of input tensor. - TF_LITE_ENSURE_MSG(context, NumDimensions(op_context.input) <= 5, - "Transpose op only supports 1D-5D input arrays."); - TF_LITE_ENSURE_TYPES_EQ(context, op_context.input->type, - op_context.output->type); - - int dims = NumDimensions(op_context.input); - const int32_t* perm_data = GetTensorData(op_context.perm); - - // Ensure validity of the permutations tensor as a 1D tensor. - TF_LITE_ENSURE_EQ(context, NumDimensions(op_context.perm), 1); - TF_LITE_ENSURE_EQ(context, op_context.perm->dims->data[0], dims); - for (int idx = 0; idx < dims; ++idx) { - TF_LITE_ENSURE_MSG(context, (perm_data[idx] >= 0 && perm_data[idx] < dims), - "Transpose op permutations array is out of bounds."); - } - - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* perm_tensor = - tflite::micro::GetEvalInput(context, node, kPermTensor); - const int32_t* perm_data = perm_tensor->data.i32; - const int size = perm_tensor->dims->data[0]; - TransposeParams params; - params.perm_count = size; - for (int i = 0; i < size; ++i) { - params.perm[i] = perm_data[i]; - } - - // Transpose kernel only does rearranging values not numeric evaluations - // on each cell. It's safe to implement per size of scalar type and this - // trick keeps the total code size in a reasonable range. - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kInputTensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - switch (input->type) { - case kTfLiteFloat32: - reference_ops::Transpose(params, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - case kTfLiteInt8: - reference_ops::Transpose(params, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output)); - break; - default: - TF_LITE_KERNEL_LOG(context, - "Type %s is currently not supported by Transpose. " - "Only float32 and int8 is supported", - TfLiteTypeGetName(input->type)); - return kTfLiteError; - } - - return kTfLiteOk; -} - -} // namespace - -TfLiteRegistration Register_TRANSPOSE() { - return tflite::micro::RegisterOp(nullptr, Prepare, Eval); -} -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/transpose_conv.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/transpose_conv.cc deleted file mode 100644 index 0b2afd5b..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/transpose_conv.cc +++ /dev/null @@ -1,343 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/kernels/internal/reference/transpose_conv.h" - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/common.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/reference/integer_ops/transpose_conv.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/kernels/padding.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { -namespace { - -// For the TfLite transpose_conv implementation, input tensor 0 corresponds to -// the OutputShapeTensor. However, since TFLM does not support dynamic tensors, -// the TFLM implementation ignores input tensor 0 and the only inputs we care -// about are kFilterTensor, kInputTensor and kBiasTensor. -constexpr int kFilterTensor = 1; -constexpr int kInputTensor = 2; -constexpr int kBiasTensor = 3; -constexpr int kOutputTensor = 0; - -// Conv is quantized along dimension 0: -// https://www.tensorflow.org/lite/performance/quantization_spec -constexpr int kConvQuantizedDimension = 0; - -struct OpData { - ConvParams params; - - // A scratch buffer is required for quantized implementations. - int scratch_buffer_index; - - // TODO(b/192090531): Remove this once all 8x16 transpose conv models use - // 64-bit biases. - int bias_converted_buffer_index; - - // Multiplier and shift arrays are required for the int8 implementation. - int32_t* per_channel_output_multiplier; - int32_t* per_channel_output_shift; -}; - -inline PaddingType RuntimePaddingType(TfLitePadding padding) { - switch (padding) { - case TfLitePadding::kTfLitePaddingSame: - return PaddingType::kSame; - case TfLitePadding::kTfLitePaddingValid: - return PaddingType::kValid; - case TfLitePadding::kTfLitePaddingUnknown: - default: - return PaddingType::kNone; - } -} - -TfLiteStatus CalculateOpData(TfLiteContext* context, TfLiteNode* node, - const TfLiteTransposeConvParams* params, int width, - int height, int filter_width, int filter_height, - const TfLiteType data_type, OpData* data) { - bool has_bias = node->inputs->size == 4; - // Check number of inputs/outputs - TF_LITE_ENSURE(context, has_bias || node->inputs->size == 3); - TF_LITE_ENSURE_EQ(context, node->outputs->size, 1); - - // Matching GetWindowedOutputSize in TensorFlow. - auto padding = params->padding; - int unused_output_width; - int unused_output_height; - TfLitePaddingValues padding_values = ComputePaddingHeightWidth( - params->stride_height, params->stride_width, 1, - 1, // Dilation height and width are always 1 for transpose_conv. - height, width, filter_height, filter_width, padding, - &unused_output_height, &unused_output_width); - - data->params.padding_type = RuntimePaddingType(padding); - data->params.padding_values.width = padding_values.width; - data->params.padding_values.height = padding_values.height; - - // Note that quantized inference requires that all tensors have their - // parameters set. This is usually done during quantized training. - if (data_type != kTfLiteFloat32) { - MicroContext* micro_context = GetMicroContext(context); - - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* filter = - micro_context->AllocateTempInputTensor(node, kFilterTensor); - TF_LITE_ENSURE(context, filter != nullptr); - TfLiteTensor* bias = - micro_context->AllocateTempInputTensor(node, kBiasTensor); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - int output_channels = filter->dims->data[kConvQuantizedDimension]; - - TF_LITE_ENSURE_STATUS(tflite::PopulateConvolutionQuantizationParams( - context, input, filter, bias, output, kTfLiteActNone, - &data->params.output_multiplier, &data->params.output_shift, - &data->params.quantized_activation_min, - &data->params.quantized_activation_max, - data->per_channel_output_multiplier, data->per_channel_output_shift, - output_channels)); - - // TODO(b/192090531): Remove this once all 8x16 transpose conv models use - // 64-bit biases. - if (input->type == kTfLiteInt16) { - TFLITE_DCHECK(filter->type == kTfLiteInt8); - TFLITE_DCHECK(output->type == kTfLiteInt16); - if (bias->type == kTfLiteInt16) { - TFLITE_DCHECK( - context->RequestScratchBufferInArena( - context, GetTensorShape(bias).FlatSize() * sizeof(std::int64_t), - &(data->bias_converted_buffer_index)) == kTfLiteOk); - } - } - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(filter); - micro_context->DeallocateTempTfLiteTensor(output); - if (bias != nullptr) { - micro_context->DeallocateTempTfLiteTensor(bias); - } - } - return kTfLiteOk; -} - -void* Init(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(OpData)); -} - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - TFLITE_DCHECK(node->user_data != nullptr); - TFLITE_DCHECK(node->builtin_data != nullptr); - - OpData* data = static_cast(node->user_data); - const auto params = - static_cast(node->builtin_data); - - MicroContext* micro_context = GetMicroContext(context); - - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* filter = - micro_context->AllocateTempInputTensor(node, kFilterTensor); - TF_LITE_ENSURE(context, filter != nullptr); - - // Get height and width of the output. - const int width = SizeOfDimension(output, 2); - const int height = SizeOfDimension(output, 1); - const int filter_width = SizeOfDimension(filter, 2); - const int filter_height = SizeOfDimension(filter, 1); - - // Dynamically allocate per-channel quantization parameters. - const int num_channels = filter->dims->data[kConvQuantizedDimension]; - data->per_channel_output_multiplier = - static_cast(context->AllocatePersistentBuffer( - context, num_channels * sizeof(int32_t))); - data->per_channel_output_shift = - static_cast(context->AllocatePersistentBuffer( - context, num_channels * sizeof(int32_t))); - - // Quantized kernels use an int32 scratch buffer. - if (input->type == kTfLiteInt8) { - TFLITE_DCHECK(context->RequestScratchBufferInArena != nullptr); - TFLITE_DCHECK(context->RequestScratchBufferInArena( - context, - GetTensorShape(output).FlatSize() * sizeof(int32_t), - &(data->scratch_buffer_index)) == kTfLiteOk); - } - - // Quantized 16x8 kernels use an int64 scratch buffer. - if (input->type == kTfLiteInt16) { - TFLITE_DCHECK(context->RequestScratchBufferInArena != nullptr); - TFLITE_DCHECK(context->RequestScratchBufferInArena( - context, - GetTensorShape(output).FlatSize() * sizeof(std::int64_t), - &(data->scratch_buffer_index)) == kTfLiteOk); - } - - // All per-channel quantized tensors need valid zero point and scale arrays. - if (input->type == kTfLiteInt8 || input->type == kTfLiteInt16) { - TF_LITE_ENSURE_EQ(context, filter->quantization.type, - kTfLiteAffineQuantization); - - const auto* affine_quantization = - static_cast(filter->quantization.params); - TF_LITE_ENSURE(context, affine_quantization); - TF_LITE_ENSURE(context, affine_quantization->scale); - TF_LITE_ENSURE(context, affine_quantization->zero_point); - - TF_LITE_ENSURE(context, - affine_quantization->scale->size == 1 || - affine_quantization->scale->size == - filter->dims->data[kConvQuantizedDimension]); - TF_LITE_ENSURE_EQ(context, affine_quantization->scale->size, - affine_quantization->zero_point->size); - } - - TF_LITE_ENSURE_STATUS(CalculateOpData(context, node, params, width, height, - filter_width, filter_height, - input->type, data)); - - // Offsets (zero points) - data->params.input_offset = -input->params.zero_point; - data->params.weights_offset = -filter->params.zero_point; - data->params.output_offset = output->params.zero_point; - - // Stride - data->params.stride_width = params->stride_width; - data->params.stride_height = params->stride_height; - - micro_context->DeallocateTempTfLiteTensor(output); - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(filter); - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kInputTensor); - const TfLiteEvalTensor* filter = - tflite::micro::GetEvalInput(context, node, kFilterTensor); - const TfLiteEvalTensor* bias = - (NumInputs(node) == 4) - ? tflite::micro::GetEvalInput(context, node, kBiasTensor) - : nullptr; - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - - TFLITE_DCHECK(node->user_data != nullptr); - const OpData& data = *(static_cast(node->user_data)); - - TF_LITE_ENSURE_EQ(context, input->type, output->type); - TF_LITE_ENSURE_MSG( - context, - input->type == filter->type || - (input->type == kTfLiteInt16 && filter->type == kTfLiteInt8), - "Hybrid models are not supported on TFLite Micro."); - - switch (input->type) { // Already know in/out types are same. - case kTfLiteFloat32: { - reference_ops::TransposeConv( - data.params, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(filter), - tflite::micro::GetTensorData(filter), - tflite::micro::GetTensorShape(bias), - tflite::micro::GetOptionalTensorData(bias), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output), - tflite::micro::GetTensorShape(nullptr), nullptr); - break; - } - case kTfLiteInt8: { - int32_t* scratch_buffer = static_cast( - context->GetScratchBuffer(context, data.scratch_buffer_index)); - reference_integer_ops::TransposeConv( - data.params, data.per_channel_output_multiplier, - data.per_channel_output_shift, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(filter), - tflite::micro::GetTensorData(filter), - tflite::micro::GetTensorShape(bias), - tflite::micro::GetOptionalTensorData(bias), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output), - tflite::micro::GetTensorShape(nullptr), nullptr, scratch_buffer); - break; - } - case kTfLiteInt16: { - std::int64_t* scratch_buffer = static_cast( - context->GetScratchBuffer(context, data.scratch_buffer_index)); - // TODO(b/192090531): Remove this once all 8x16 transpose conv models use - // 64-bit biases. - if (bias != nullptr && bias->type == kTfLiteInt16) { - std::int64_t* bias_converted_buffer = - static_cast(context->GetScratchBuffer( - context, data.bias_converted_buffer_index)); - for (int i = 0; i < tflite::micro::GetTensorShape(bias).FlatSize(); - i++) { - bias_converted_buffer[i] = bias->data.i16[i]; - } - reference_integer_ops::TransposeConv( - data.params, data.per_channel_output_multiplier, - data.per_channel_output_shift, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(filter), - tflite::micro::GetTensorData(filter), - tflite::micro::GetTensorShape(bias), bias_converted_buffer, - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output), - tflite::micro::GetTensorShape(nullptr), nullptr, scratch_buffer); - } else { - reference_integer_ops::TransposeConv( - data.params, data.per_channel_output_multiplier, - data.per_channel_output_shift, tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorData(input), - tflite::micro::GetTensorShape(filter), - tflite::micro::GetTensorData(filter), - tflite::micro::GetTensorShape(bias), - tflite::micro::GetOptionalTensorData(bias), - tflite::micro::GetTensorShape(output), - tflite::micro::GetTensorData(output), - tflite::micro::GetTensorShape(nullptr), nullptr, scratch_buffer); - } - break; - } - default: - TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.", - TfLiteTypeGetName(input->type), input->type); - return kTfLiteError; - } - return kTfLiteOk; -} - -} // namespace - -TfLiteRegistration Register_TRANSPOSE_CONV() { - return tflite::micro::RegisterOp(Init, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/unidirectional_sequence_lstm.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/unidirectional_sequence_lstm.cc deleted file mode 100644 index 7f3c50e4..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/unidirectional_sequence_lstm.cc +++ /dev/null @@ -1,1696 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include -#include - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/internal/quantization_util.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/lstm_eval.h" -#include "tensorflow/lite/micro/kernels/lstm_shared.h" -#include "tensorflow/lite/micro/kernels/micro_tensor_utils.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" - -namespace tflite { - -namespace { - -constexpr int scratch_index_size = 12; - -struct UnidirectionalSequenceLstmOpData { - // If the lstm is layer norm. - bool use_layer_norm; - // The scratch index. - int scratch_index[scratch_index_size]; - - int32_t row_sums_size; - int32_t* row_sums; - bool compute_row_sums = false; - - int32_t input_zero_point; - int32_t output_state_zero_point; - - IntegerLstmParameter integer_lstm_param; - HybridLstmScales hybrid_lstm_scales; -}; - -TfLiteStatus PopulateQuantizedLstmParams8x8_16( - TfLiteContext* context, TfLiteNode* node, - IntegerLstmParameter* integer_lstm_param) { - MicroContext* micro_context = GetMicroContext(context); - - // Calculate quantized clip for projection and cell. - const auto* params = - static_cast(node->builtin_data); - const float cell_clip = params->cell_clip; - const float proj_clip = params->proj_clip; - - TfLiteTensor* cell_state = - micro_context->AllocateTempInputTensor(node, kLstmCellStateTensor); - TF_LITE_ENSURE(context, cell_state != nullptr); - TF_LITE_ENSURE(context, cell_state->is_variable); - TfLiteTensor* output_tensor = - micro_context->AllocateTempOutputTensor(node, kLstmOutputTensor); - - TF_LITE_ENSURE(context, - cell_state->quantization.type != kTfLiteNoQuantization); - auto* cell_state_params = - static_cast(cell_state->quantization.params); - TF_LITE_ENSURE(context, - output_tensor->quantization.type != kTfLiteNoQuantization); - auto* proj_params = static_cast( - output_tensor->quantization.params); - if (cell_clip > 0.0f) { - integer_lstm_param->quantized_cell_clip = static_cast(std::min( - std::max(cell_clip / cell_state_params->scale->data[0], -32768.0f), - 32767.0f)); - } else { - integer_lstm_param->quantized_cell_clip = 0; - } - if (proj_clip > 0.0f) { - integer_lstm_param->quantized_proj_clip = static_cast(std::min( - std::max(proj_clip / proj_params->scale->data[0], -128.0f), 127.0f)); - } else { - integer_lstm_param->quantized_proj_clip = 0; - } - - // Calculate effective scales. - UnidirectionalSequenceLstmOpData* op_data = - static_cast(node->user_data); - const bool use_layer_norm = op_data->use_layer_norm; - - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kLstmInputTensor); - - TfLiteTensor* input_to_input_weights = micro_context->AllocateTempInputTensor( - node, kLstmInputToInputWeightsTensor); - TfLiteTensor* input_to_forget_weights = - micro_context->AllocateTempInputTensor(node, - kLstmInputToForgetWeightsTensor); - TfLiteTensor* input_to_cell_weights = micro_context->AllocateTempInputTensor( - node, kLstmInputToCellWeightsTensor); - TfLiteTensor* input_to_output_weights = - micro_context->AllocateTempInputTensor(node, - kLstmInputToOutputWeightsTensor); - - TfLiteTensor* recurrent_to_input_weights = - micro_context->AllocateTempInputTensor( - node, kLstmRecurrentToInputWeightsTensor); - TfLiteTensor* recurrent_to_forget_weights = - micro_context->AllocateTempInputTensor( - node, kLstmRecurrentToForgetWeightsTensor); - TfLiteTensor* recurrent_to_cell_weights = - micro_context->AllocateTempInputTensor(node, - kLstmRecurrentToCellWeightsTensor); - TfLiteTensor* recurrent_to_output_weights = - micro_context->AllocateTempInputTensor( - node, kLstmRecurrentToOutputWeightsTensor); - - TfLiteTensor* cell_to_input_weights = micro_context->AllocateTempInputTensor( - node, kLstmCellToInputWeightsTensor); - TfLiteTensor* cell_to_forget_weights = micro_context->AllocateTempInputTensor( - node, kLstmCellToForgetWeightsTensor); - TfLiteTensor* cell_to_output_weights = micro_context->AllocateTempInputTensor( - node, kLstmCellToOutputWeightsTensor); - - TfLiteTensor* input_layer_norm_coefficients = - micro_context->AllocateTempInputTensor( - node, kLstmInputLayerNormCoefficientsTensor); - TfLiteTensor* forget_layer_norm_coefficients = - micro_context->AllocateTempInputTensor( - node, kLstmForgetLayerNormCoefficientsTensor); - TfLiteTensor* cell_layer_norm_coefficients = - micro_context->AllocateTempInputTensor( - node, kLstmCellLayerNormCoefficientsTensor); - TfLiteTensor* output_layer_norm_coefficients = - micro_context->AllocateTempInputTensor( - node, kLstmOutputLayerNormCoefficientsTensor); - - TfLiteTensor* projection_weights = micro_context->AllocateTempInputTensor( - node, kLstmProjectionWeightsTensor); - - TfLiteTensor* output_state = - micro_context->AllocateTempInputTensor(node, kLstmOutputStateTensor); - TF_LITE_ENSURE(context, output_state != nullptr); - TF_LITE_ENSURE(context, output_state->is_variable); - - // Since we have already checked that weights are all there or none, we can - // check the existence of only one to get the condition. - const bool use_cifg = (input_to_input_weights == nullptr); - const bool use_peephole = (cell_to_output_weights != nullptr); - const bool use_projection = (projection_weights != nullptr); - - // Get intermediate scales and zero points. - float intermediate_scale[5]; - int32_t intermediate_zp[5]; - for (int i = 0; i < 4; ++i) { - if (use_layer_norm) { - TfLiteTensor* intermediate = - micro_context->AllocateTempIntermediateTensor(node, i); - TF_LITE_ENSURE(context, - intermediate->quantization.type != kTfLiteNoQuantization); - auto* params_intermediate = static_cast( - intermediate->quantization.params); - intermediate_scale[i] = params_intermediate->scale->data[0]; - intermediate_zp[i] = params_intermediate->zero_point->data[0]; - if (intermediate != nullptr) { - micro_context->DeallocateTempTfLiteTensor(intermediate); - } - } else { - // Q3.12 for activation functions. - intermediate_scale[i] = std::pow(2.0f, -12.0f); - intermediate_zp[i] = 0; - } - } - // In the absence of projection, hidden becomes otuput and this intermediate - // is ignored. - TfLiteTensor* hidden = micro_context->AllocateTempIntermediateTensor(node, 4); - TF_LITE_ENSURE(context, hidden->quantization.type != kTfLiteNoQuantization); - auto* hidden_params = - static_cast(hidden->quantization.params); - intermediate_scale[4] = hidden_params->scale->data[0]; - intermediate_zp[4] = hidden_params->zero_point->data[0]; - if (hidden != nullptr) { - micro_context->DeallocateTempTfLiteTensor(hidden); - } - - // Scales. - const float default_scale = 1.0; - float input_scale = default_scale; - float input_to_input_weight_scale = default_scale; - float recurrent_to_input_weight_scale = default_scale; - float cell_to_input_weight_scale = default_scale; - float input_to_forget_weight_scale = default_scale; - float recurrent_to_forget_weight_scale = default_scale; - float cell_to_forget_weight_scale = default_scale; - float input_to_cell_weight_scale = default_scale; - float recurrent_to_cell_weight_scale = default_scale; - float input_to_output_weight_scale = default_scale; - float recurrent_to_output_weight_scale = default_scale; - float cell_to_output_weight_scale = default_scale; - float projection_weight_scale = default_scale; - float layer_norm_input_scale = default_scale; - float layer_norm_forget_scale = default_scale; - float layer_norm_cell_scale = default_scale; - float layer_norm_output_scale = default_scale; - float output_state_scale = default_scale; - int cell_scale = 1; - - // Effective scales. - float effective_input_to_input_scale = default_scale; - float effective_recurrent_to_input_scale = default_scale; - float effective_cell_to_input_scale = default_scale; - float effective_input_to_forget_scale = default_scale; - float effective_recurrent_to_forget_scale = default_scale; - float effective_cell_to_forget_scale = default_scale; - float effective_input_to_cell_scale = default_scale; - float effective_recurrent_to_cell_scale = default_scale; - float effective_input_to_output_scale = default_scale; - float effective_recurrent_to_output_scale = default_scale; - float effective_cell_to_output_scale = default_scale; - float effective_proj_scale = default_scale; - float effective_hidden_scale = default_scale; - - // Populate scales. - if (!use_cifg) { - input_to_input_weight_scale = input_to_input_weights->params.scale; - recurrent_to_input_weight_scale = recurrent_to_input_weights->params.scale; - } - - if (use_peephole) { - if (!use_cifg) { - cell_to_input_weight_scale = cell_to_input_weights->params.scale; - } - cell_to_forget_weight_scale = cell_to_forget_weights->params.scale; - cell_to_output_weight_scale = cell_to_output_weights->params.scale; - } - - if (use_layer_norm) { - if (!use_cifg) { - layer_norm_input_scale = input_layer_norm_coefficients->params.scale; - } - layer_norm_forget_scale = forget_layer_norm_coefficients->params.scale; - layer_norm_cell_scale = cell_layer_norm_coefficients->params.scale; - layer_norm_output_scale = output_layer_norm_coefficients->params.scale; - } - - if (use_projection) { - projection_weight_scale = projection_weights->params.scale; - } - output_state_scale = output_state->params.scale; - - input_to_forget_weight_scale = input_to_forget_weights->params.scale; - input_to_cell_weight_scale = input_to_cell_weights->params.scale; - input_to_output_weight_scale = input_to_output_weights->params.scale; - recurrent_to_forget_weight_scale = recurrent_to_forget_weights->params.scale; - recurrent_to_cell_weight_scale = recurrent_to_cell_weights->params.scale; - recurrent_to_output_weight_scale = recurrent_to_output_weights->params.scale; - - // Check cell state (already used above) - TF_LITE_ENSURE(context, CheckedLog2(cell_state->params.scale, &cell_scale)); - // TF_LITE_ENSURE(context, cell_scale <= -9); - integer_lstm_param->cell_scale = cell_scale; - input_scale = input->params.scale; - - // Calculate effective scales. - if (!use_cifg) { - effective_input_to_input_scale = - input_to_input_weight_scale * input_scale / intermediate_scale[0]; - effective_recurrent_to_input_scale = recurrent_to_input_weight_scale * - output_state_scale / - intermediate_scale[0]; - } - effective_input_to_forget_scale = - input_to_forget_weight_scale * input_scale / intermediate_scale[1]; - effective_recurrent_to_forget_scale = recurrent_to_forget_weight_scale * - output_state_scale / - intermediate_scale[1]; - - effective_input_to_cell_scale = - input_to_cell_weight_scale * input_scale / intermediate_scale[2]; - effective_recurrent_to_cell_scale = recurrent_to_cell_weight_scale * - output_state_scale / - intermediate_scale[2]; - - effective_input_to_output_scale = - input_to_output_weight_scale * input_scale / intermediate_scale[3]; - effective_recurrent_to_output_scale = recurrent_to_output_weight_scale * - output_state_scale / - intermediate_scale[3]; - - effective_hidden_scale = - std::pow(2.0f, -15.0f) / intermediate_scale[4] * std::pow(2.0f, -15.0f); - - effective_proj_scale = - projection_weight_scale * intermediate_scale[4] / output_state_scale; - - if (use_peephole) { - if (!use_cifg) { - effective_cell_to_input_scale = - std::pow(2.0f, static_cast(cell_scale)) * - cell_to_input_weight_scale / intermediate_scale[0]; - } - effective_cell_to_forget_scale = - std::pow(2.0f, static_cast(cell_scale)) * - cell_to_forget_weight_scale / intermediate_scale[1]; - effective_cell_to_output_scale = - std::pow(2.0f, static_cast(cell_scale)) * - cell_to_output_weight_scale / intermediate_scale[3]; - } - - // Decompose scales. - int shift_output; - QuantizeMultiplier(static_cast(effective_input_to_input_scale), - &integer_lstm_param->effective_input_to_input_scale_a, - &shift_output); - integer_lstm_param->effective_input_to_input_scale_b = - static_cast(shift_output); - QuantizeMultiplier(static_cast(effective_recurrent_to_input_scale), - &integer_lstm_param->effective_recurrent_to_input_scale_a, - &shift_output); - integer_lstm_param->effective_recurrent_to_input_scale_b = - static_cast(shift_output); - QuantizeMultiplier(static_cast(effective_cell_to_input_scale), - &integer_lstm_param->effective_cell_to_input_scale_a, - &shift_output); - integer_lstm_param->effective_cell_to_input_scale_b = - static_cast(shift_output); - QuantizeMultiplier(static_cast(effective_input_to_forget_scale), - &integer_lstm_param->effective_input_to_forget_scale_a, - &shift_output); - integer_lstm_param->effective_input_to_forget_scale_b = - static_cast(shift_output); - QuantizeMultiplier(static_cast(effective_recurrent_to_forget_scale), - &integer_lstm_param->effective_recurrent_to_forget_scale_a, - &shift_output); - integer_lstm_param->effective_recurrent_to_forget_scale_b = - static_cast(shift_output); - QuantizeMultiplier(static_cast(effective_cell_to_forget_scale), - &integer_lstm_param->effective_cell_to_forget_scale_a, - &shift_output); - integer_lstm_param->effective_cell_to_forget_scale_b = - static_cast(shift_output); - QuantizeMultiplier(static_cast(effective_input_to_cell_scale), - &integer_lstm_param->effective_input_to_cell_scale_a, - &shift_output); - integer_lstm_param->effective_input_to_cell_scale_b = - static_cast(shift_output); - QuantizeMultiplier(static_cast(effective_recurrent_to_cell_scale), - &integer_lstm_param->effective_recurrent_to_cell_scale_a, - &shift_output); - integer_lstm_param->effective_recurrent_to_cell_scale_b = - static_cast(shift_output); - QuantizeMultiplier(static_cast(effective_input_to_output_scale), - &integer_lstm_param->effective_input_to_output_scale_a, - &shift_output); - integer_lstm_param->effective_input_to_output_scale_b = - static_cast(shift_output); - QuantizeMultiplier(static_cast(effective_recurrent_to_output_scale), - &integer_lstm_param->effective_recurrent_to_output_scale_a, - &shift_output); - integer_lstm_param->effective_recurrent_to_output_scale_b = - static_cast(shift_output); - QuantizeMultiplier(static_cast(effective_cell_to_output_scale), - &integer_lstm_param->effective_cell_to_output_scale_a, - &shift_output); - integer_lstm_param->effective_cell_to_output_scale_b = - static_cast(shift_output); - QuantizeMultiplier(static_cast(effective_proj_scale), - &integer_lstm_param->effective_proj_scale_a, - &shift_output); - integer_lstm_param->effective_proj_scale_b = - static_cast(shift_output); - QuantizeMultiplier(static_cast(effective_hidden_scale), - &integer_lstm_param->effective_hidden_scale_a, - &shift_output); - integer_lstm_param->effective_hidden_scale_b = - static_cast(shift_output); - QuantizeMultiplier(static_cast(layer_norm_input_scale), - &integer_lstm_param->layer_norm_input_scale_a, - &shift_output); - integer_lstm_param->layer_norm_input_scale_b = - static_cast(shift_output); - QuantizeMultiplier(static_cast(layer_norm_forget_scale), - &integer_lstm_param->layer_norm_forget_scale_a, - &shift_output); - integer_lstm_param->layer_norm_forget_scale_b = - static_cast(shift_output); - QuantizeMultiplier(static_cast(layer_norm_cell_scale), - &integer_lstm_param->layer_norm_cell_scale_a, - &shift_output); - integer_lstm_param->layer_norm_cell_scale_b = - static_cast(shift_output); - QuantizeMultiplier(static_cast(layer_norm_output_scale), - &integer_lstm_param->layer_norm_output_scale_a, - &shift_output); - integer_lstm_param->layer_norm_output_scale_b = - static_cast(shift_output); - - integer_lstm_param->hidden_zp = intermediate_zp[4]; - - // 10000 is used to make sure the kernel logic does not overflow. - if (!use_cifg) { - integer_lstm_param->input_variance_guard = - std::max(1, static_cast(10000 * layer_norm_input_scale)); - } - integer_lstm_param->forget_variance_guard = - std::max(1, static_cast(10000 * layer_norm_forget_scale)); - integer_lstm_param->cell_variance_guard = - std::max(1, static_cast(10000 * layer_norm_cell_scale)); - integer_lstm_param->output_variance_guard = - std::max(1, static_cast(10000 * layer_norm_output_scale)); - - if (cell_state != nullptr) { - micro_context->DeallocateTempTfLiteTensor(cell_state); - } - if (output_tensor != nullptr) { - micro_context->DeallocateTempTfLiteTensor(output_tensor); - } - if (input != nullptr) { - micro_context->DeallocateTempTfLiteTensor(input); - } - if (input_to_input_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(input_to_input_weights); - } - if (input_to_forget_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(input_to_forget_weights); - } - if (input_to_cell_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(input_to_cell_weights); - } - if (input_to_output_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(input_to_output_weights); - } - if (recurrent_to_input_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(recurrent_to_input_weights); - } - if (recurrent_to_forget_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(recurrent_to_forget_weights); - } - if (recurrent_to_cell_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(recurrent_to_cell_weights); - } - if (recurrent_to_output_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(recurrent_to_output_weights); - } - if (cell_to_input_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(cell_to_input_weights); - } - if (cell_to_forget_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(cell_to_forget_weights); - } - if (cell_to_output_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(cell_to_output_weights); - } - if (input_layer_norm_coefficients != nullptr) { - micro_context->DeallocateTempTfLiteTensor(input_layer_norm_coefficients); - } - if (forget_layer_norm_coefficients != nullptr) { - micro_context->DeallocateTempTfLiteTensor(forget_layer_norm_coefficients); - } - if (cell_layer_norm_coefficients != nullptr) { - micro_context->DeallocateTempTfLiteTensor(cell_layer_norm_coefficients); - } - if (output_layer_norm_coefficients != nullptr) { - micro_context->DeallocateTempTfLiteTensor(output_layer_norm_coefficients); - } - if (projection_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(projection_weights); - } - if (output_state != nullptr) { - micro_context->DeallocateTempTfLiteTensor(output_state); - } - - return kTfLiteOk; -} - -// Temporary buffers used for hybrid mode -enum HybridTempBuffer { - kPrimaryScratchBuffer = 0, - kInputQuantized = 1, - kOutputStateQuantized = 2, - kCellStateQuantized = 3, - kInputScalingFactors = 4, - kOutputStateScalingFactors = 5, - kProductScalingFactors = 6, - kRecoveredCellWeights = 7, - kAccumScratch = 8, - kInputZeroPoints = 9, - kOutputStateZeroPoints = 10, - kScales = 11, - kNumHybridTempBuffers = 12, -}; - -void* UnidirectionalSequenceLstmInit(TfLiteContext* context, const char* buffer, - size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer( - context, sizeof(UnidirectionalSequenceLstmOpData)); -} - -// Check that input tensor dimensions matches with each other. -TfLiteStatus SetHybridScales(TfLiteContext* context, TfLiteNode* node) { - UnidirectionalSequenceLstmOpData* op_data = - reinterpret_cast(node->user_data); - MicroContext* micro_context = GetMicroContext(context); - - TfLiteTensor* input_to_input_weights = micro_context->AllocateTempInputTensor( - node, kLstmInputToInputWeightsTensor); - op_data->hybrid_lstm_scales.input_to_input_weights_scale = - (input_to_input_weights != nullptr) ? input_to_input_weights->params.scale - : 1.0f; - - TfLiteTensor* input_to_forget_weights = - micro_context->AllocateTempInputTensor(node, - kLstmInputToForgetWeightsTensor); - op_data->hybrid_lstm_scales.input_to_forget_weights_scale = - (input_to_forget_weights != nullptr) - ? input_to_forget_weights->params.scale - : 1.0f; - - TfLiteTensor* input_to_cell_weights = micro_context->AllocateTempInputTensor( - node, kLstmInputToCellWeightsTensor); - op_data->hybrid_lstm_scales.input_to_cell_weights_scale = - (input_to_cell_weights != nullptr) ? input_to_cell_weights->params.scale - : 1.0f; - - TfLiteTensor* input_to_output_weights = - micro_context->AllocateTempInputTensor(node, - kLstmInputToOutputWeightsTensor); - op_data->hybrid_lstm_scales.input_to_output_weights_scale = - (input_to_output_weights != nullptr) - ? input_to_output_weights->params.scale - : 1.0f; - - op_data->hybrid_lstm_scales.aux_input_to_input_weights_scale = 1.0f; - op_data->hybrid_lstm_scales.aux_input_to_forget_weights_scale = 1.0f; - op_data->hybrid_lstm_scales.aux_input_to_cell_weights_scale = 1.0f; - op_data->hybrid_lstm_scales.aux_input_to_output_weights_scale = 1.0f; - - TfLiteTensor* recurrent_to_input_weights = - micro_context->AllocateTempInputTensor( - node, kLstmRecurrentToInputWeightsTensor); - op_data->hybrid_lstm_scales.recurrent_to_input_weights_scale = - (recurrent_to_input_weights != nullptr) - ? recurrent_to_input_weights->params.scale - : 1.0f; - - TfLiteTensor* recurrent_to_forget_weights = - micro_context->AllocateTempInputTensor( - node, kLstmRecurrentToForgetWeightsTensor); - op_data->hybrid_lstm_scales.recurrent_to_forget_weights_scale = - (recurrent_to_forget_weights != nullptr) - ? recurrent_to_forget_weights->params.scale - : 1.0f; - - TfLiteTensor* recurrent_to_cell_weights = - micro_context->AllocateTempInputTensor(node, - kLstmRecurrentToCellWeightsTensor); - op_data->hybrid_lstm_scales.recurrent_to_cell_weights_scale = - (recurrent_to_cell_weights != nullptr) - ? recurrent_to_cell_weights->params.scale - : 1.0f; - - TfLiteTensor* recurrent_to_output_weights = - micro_context->AllocateTempInputTensor( - node, kLstmRecurrentToOutputWeightsTensor); - op_data->hybrid_lstm_scales.recurrent_to_output_weights_scale = - (recurrent_to_output_weights != nullptr) - ? recurrent_to_output_weights->params.scale - : 1.0f; - - TfLiteTensor* cell_to_input_weights = micro_context->AllocateTempInputTensor( - node, kLstmCellToInputWeightsTensor); - op_data->hybrid_lstm_scales.cell_to_input_weights_scale = - (cell_to_input_weights != nullptr) ? cell_to_input_weights->params.scale - : 1.0f; - - TfLiteTensor* cell_to_forget_weights = micro_context->AllocateTempInputTensor( - node, kLstmCellToForgetWeightsTensor); - op_data->hybrid_lstm_scales.cell_to_forget_weights_scale = - (cell_to_forget_weights != nullptr) ? cell_to_forget_weights->params.scale - : 1.0f; - - TfLiteTensor* cell_to_output_weights = micro_context->AllocateTempInputTensor( - node, kLstmCellToOutputWeightsTensor); - op_data->hybrid_lstm_scales.cell_to_output_weights_scale = - (cell_to_output_weights != nullptr) ? cell_to_output_weights->params.scale - : 1.0f; - - TfLiteTensor* projection_weights = micro_context->AllocateTempInputTensor( - node, kLstmProjectionWeightsTensor); - op_data->hybrid_lstm_scales.projection_weights_scale = - (projection_weights != nullptr) ? projection_weights->params.scale : 1.0f; - - if (input_to_input_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(input_to_input_weights); - } - - if (input_to_forget_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(input_to_forget_weights); - } - - if (input_to_cell_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(input_to_cell_weights); - } - - if (input_to_output_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(input_to_output_weights); - } - - if (recurrent_to_input_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(recurrent_to_input_weights); - } - - if (recurrent_to_forget_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(recurrent_to_forget_weights); - } - - if (recurrent_to_cell_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(recurrent_to_cell_weights); - } - - if (recurrent_to_output_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(recurrent_to_output_weights); - } - - if (cell_to_input_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(cell_to_input_weights); - } - - if (cell_to_forget_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(cell_to_forget_weights); - } - - if (cell_to_output_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(cell_to_output_weights); - } - - if (projection_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(projection_weights); - } - - return kTfLiteOk; -} - -// Check that input tensor dimensions matches with each other. -TfLiteStatus CheckInputTensorDimensions(TfLiteContext* context, - TfLiteNode* node, int n_input, - int n_output, int n_cell, - bool use_layer_norm, bool is_integer) { - MicroContext* micro_context = GetMicroContext(context); - - const auto* params = reinterpret_cast(node->builtin_data); - - // Making sure clipping parameters have valid values. - // == 0 means no clipping - // > 0 means clipping - TF_LITE_ENSURE(context, params->cell_clip >= 0); - TF_LITE_ENSURE(context, params->proj_clip >= 0); - - TfLiteTensor* input_to_input_weights = micro_context->AllocateTempInputTensor( - node, kLstmInputToInputWeightsTensor); - if (input_to_input_weights != nullptr) { - TF_LITE_ENSURE_EQ(context, input_to_input_weights->dims->size, 2); - TF_LITE_ENSURE_EQ(context, input_to_input_weights->dims->data[0], n_cell); - TF_LITE_ENSURE_EQ(context, input_to_input_weights->dims->data[1], n_input); - } - - TfLiteTensor* input_to_forget_weights = - micro_context->AllocateTempInputTensor(node, - kLstmInputToForgetWeightsTensor); - TF_LITE_ENSURE_EQ(context, input_to_forget_weights->dims->size, 2); - TF_LITE_ENSURE_EQ(context, input_to_forget_weights->dims->data[0], n_cell); - TF_LITE_ENSURE_EQ(context, input_to_forget_weights->dims->data[1], n_input); - - TfLiteTensor* input_to_cell_weights = micro_context->AllocateTempInputTensor( - node, kLstmInputToCellWeightsTensor); - TF_LITE_ENSURE_EQ(context, input_to_cell_weights->dims->size, 2); - TF_LITE_ENSURE_EQ(context, input_to_cell_weights->dims->data[0], n_cell); - TF_LITE_ENSURE_EQ(context, input_to_cell_weights->dims->data[1], n_input); - - TfLiteTensor* recurrent_to_input_weights = - micro_context->AllocateTempInputTensor( - node, kLstmRecurrentToInputWeightsTensor); - if (recurrent_to_input_weights != nullptr) { - TF_LITE_ENSURE_EQ(context, recurrent_to_input_weights->dims->size, 2); - TF_LITE_ENSURE_EQ(context, recurrent_to_input_weights->dims->data[0], - n_cell); - TF_LITE_ENSURE_EQ(context, recurrent_to_input_weights->dims->data[1], - n_output); - } - - TfLiteTensor* recurrent_to_forget_weights = - micro_context->AllocateTempInputTensor( - node, kLstmRecurrentToForgetWeightsTensor); - TF_LITE_ENSURE_EQ(context, recurrent_to_forget_weights->dims->size, 2); - TF_LITE_ENSURE_EQ(context, recurrent_to_forget_weights->dims->data[0], - n_cell); - TF_LITE_ENSURE_EQ(context, recurrent_to_forget_weights->dims->data[1], - n_output); - - TfLiteTensor* recurrent_to_cell_weights = - micro_context->AllocateTempInputTensor(node, - kLstmRecurrentToCellWeightsTensor); - TF_LITE_ENSURE_EQ(context, recurrent_to_cell_weights->dims->size, 2); - TF_LITE_ENSURE_EQ(context, recurrent_to_cell_weights->dims->data[0], n_cell); - TF_LITE_ENSURE_EQ(context, recurrent_to_cell_weights->dims->data[1], - n_output); - - // We make sure the input-gate's parameters are either both present (regular - // LSTM) or not at all (CIFG-LSTM). - const bool cifg_weights_all_or_none = - ((input_to_input_weights != nullptr) && - (recurrent_to_input_weights != nullptr)) || - ((input_to_input_weights == nullptr) && - (recurrent_to_input_weights == nullptr)); - TF_LITE_ENSURE(context, cifg_weights_all_or_none == true); - - TfLiteTensor* cell_to_input_weights = micro_context->AllocateTempInputTensor( - node, kLstmCellToInputWeightsTensor); - if (cell_to_input_weights != nullptr) { - TF_LITE_ENSURE_EQ(context, cell_to_input_weights->dims->size, 1); - TF_LITE_ENSURE_EQ(context, cell_to_input_weights->dims->data[0], n_cell); - TF_LITE_ENSURE_TYPES_EQ( - context, cell_to_input_weights->type, - is_integer ? kTfLiteInt16 : input_to_forget_weights->type); - } - - TfLiteTensor* cell_to_forget_weights = micro_context->AllocateTempInputTensor( - node, kLstmCellToForgetWeightsTensor); - if (cell_to_forget_weights != nullptr) { - TF_LITE_ENSURE_EQ(context, cell_to_forget_weights->dims->size, 1); - TF_LITE_ENSURE_EQ(context, cell_to_forget_weights->dims->data[0], n_cell); - TF_LITE_ENSURE_TYPES_EQ( - context, cell_to_forget_weights->type, - is_integer ? kTfLiteInt16 : input_to_forget_weights->type); - } - - TfLiteTensor* cell_to_output_weights = micro_context->AllocateTempInputTensor( - node, kLstmCellToOutputWeightsTensor); - if (cell_to_output_weights != nullptr) { - TF_LITE_ENSURE_EQ(context, cell_to_output_weights->dims->size, 1); - TF_LITE_ENSURE_EQ(context, cell_to_output_weights->dims->data[0], n_cell); - TF_LITE_ENSURE_TYPES_EQ( - context, cell_to_output_weights->type, - is_integer ? kTfLiteInt16 : input_to_forget_weights->type); - } - - // Making sure the peephole weights are there all or none. - const bool use_cifg = (input_to_input_weights == nullptr); - const bool peephole_weights_all_or_none = - ((cell_to_input_weights != nullptr || use_cifg) && - (cell_to_forget_weights != nullptr) && - (cell_to_output_weights != nullptr)) || - ((cell_to_input_weights == nullptr) && - (cell_to_forget_weights == nullptr) && - (cell_to_output_weights == nullptr)); - TF_LITE_ENSURE(context, peephole_weights_all_or_none == true); - - // Make sure the input gate bias is present only when not a CIFG-LSTM. - TfLiteTensor* input_gate_bias = - micro_context->AllocateTempInputTensor(node, kLstmInputGateBiasTensor); - if (use_cifg) { - TF_LITE_ENSURE_EQ(context, input_gate_bias, nullptr); - } else { - TF_LITE_ENSURE_EQ(context, input_gate_bias->dims->size, 1); - TF_LITE_ENSURE_EQ(context, input_gate_bias->dims->data[0], n_cell); - if (is_integer) { - TF_LITE_ENSURE_TYPES_EQ(context, input_gate_bias->type, kTfLiteInt32); - } else { - TF_LITE_ENSURE_TYPES_EQ(context, input_gate_bias->type, kTfLiteFloat32); - } - } - - TfLiteTensor* forget_gate_bias = - micro_context->AllocateTempInputTensor(node, kLstmForgetGateBiasTensor); - TF_LITE_ENSURE_EQ(context, forget_gate_bias->dims->size, 1); - TF_LITE_ENSURE_EQ(context, forget_gate_bias->dims->data[0], n_cell); - if (is_integer) { - TF_LITE_ENSURE_TYPES_EQ(context, forget_gate_bias->type, kTfLiteInt32); - } else { - TF_LITE_ENSURE_TYPES_EQ(context, forget_gate_bias->type, kTfLiteFloat32); - } - - TfLiteTensor* cell_gate_bias = - micro_context->AllocateTempInputTensor(node, kLstmCellGateBiasTensor); - TF_LITE_ENSURE_EQ(context, cell_gate_bias->dims->size, 1); - TF_LITE_ENSURE_EQ(context, cell_gate_bias->dims->data[0], n_cell); - if (is_integer) { - TF_LITE_ENSURE_TYPES_EQ(context, cell_gate_bias->type, kTfLiteInt32); - } else { - TF_LITE_ENSURE_TYPES_EQ(context, cell_gate_bias->type, kTfLiteFloat32); - } - - TfLiteTensor* output_gate_bias = - micro_context->AllocateTempInputTensor(node, kLstmOutputGateBiasTensor); - TF_LITE_ENSURE_EQ(context, output_gate_bias->dims->size, 1); - TF_LITE_ENSURE_EQ(context, output_gate_bias->dims->data[0], n_cell); - if (is_integer) { - TF_LITE_ENSURE_TYPES_EQ(context, output_gate_bias->type, kTfLiteInt32); - } else { - TF_LITE_ENSURE_TYPES_EQ(context, output_gate_bias->type, kTfLiteFloat32); - } - - TfLiteTensor* projection_weights = micro_context->AllocateTempInputTensor( - node, kLstmProjectionWeightsTensor); - if (projection_weights != nullptr) { - TF_LITE_ENSURE_EQ(context, projection_weights->dims->size, 2); - TF_LITE_ENSURE_EQ(context, projection_weights->dims->data[0], n_output); - TF_LITE_ENSURE_EQ(context, projection_weights->dims->data[1], n_cell); - } - - TfLiteTensor* projection_bias = - micro_context->AllocateTempInputTensor(node, kLstmProjectionBiasTensor); - if (projection_bias != nullptr) { - TF_LITE_ENSURE_EQ(context, projection_bias->dims->size, 1); - TF_LITE_ENSURE_EQ(context, projection_bias->dims->data[0], n_output); - if (is_integer) { - TF_LITE_ENSURE_TYPES_EQ(context, projection_bias->type, kTfLiteInt32); - } else { - TF_LITE_ENSURE_TYPES_EQ(context, projection_bias->type, kTfLiteFloat32); - } - } - - // Making sure the projection tensors are consistent: - // 1) If projection weight is not present, then projection bias should not be - // present. - // 2) If projection weight is present, then projection bias is optional. - const bool projecton_tensors_consistent = - ((projection_weights != nullptr) || (projection_bias == nullptr)); - TF_LITE_ENSURE(context, projecton_tensors_consistent == true); - - if (use_layer_norm) { - TfLiteTensor* input_layer_norm_coefficients = - micro_context->AllocateTempInputTensor( - node, kLstmInputLayerNormCoefficientsTensor); - if (use_cifg) { - TF_LITE_ENSURE_EQ(context, input_layer_norm_coefficients, nullptr); - } else { - TF_LITE_ENSURE(context, input_layer_norm_coefficients != nullptr); - TF_LITE_ENSURE_EQ(context, input_layer_norm_coefficients->dims->size, 1); - TF_LITE_ENSURE_EQ(context, input_layer_norm_coefficients->dims->data[0], - n_cell); - if (is_integer) { - TF_LITE_ENSURE_TYPES_EQ(context, input_layer_norm_coefficients->type, - kTfLiteInt16); - } else { - TF_LITE_ENSURE_TYPES_EQ(context, input_layer_norm_coefficients->type, - kTfLiteFloat32); - } - } - - TfLiteTensor* forget_layer_norm_coefficients = - micro_context->AllocateTempInputTensor( - node, kLstmForgetLayerNormCoefficientsTensor); - TF_LITE_ENSURE_EQ(context, forget_layer_norm_coefficients->dims->size, 1); - TF_LITE_ENSURE_EQ(context, forget_layer_norm_coefficients->dims->data[0], - n_cell); - if (is_integer) { - TF_LITE_ENSURE_TYPES_EQ(context, forget_layer_norm_coefficients->type, - kTfLiteInt16); - } else { - TF_LITE_ENSURE_TYPES_EQ(context, forget_layer_norm_coefficients->type, - kTfLiteFloat32); - } - - TfLiteTensor* cell_layer_norm_coefficients = - micro_context->AllocateTempInputTensor( - node, kLstmCellLayerNormCoefficientsTensor); - TF_LITE_ENSURE_EQ(context, cell_layer_norm_coefficients->dims->size, 1); - TF_LITE_ENSURE_EQ(context, cell_layer_norm_coefficients->dims->data[0], - n_cell); - if (is_integer) { - TF_LITE_ENSURE_TYPES_EQ(context, cell_layer_norm_coefficients->type, - kTfLiteInt16); - } else { - TF_LITE_ENSURE_TYPES_EQ(context, cell_layer_norm_coefficients->type, - kTfLiteFloat32); - } - - TfLiteTensor* output_layer_norm_coefficients = - micro_context->AllocateTempInputTensor( - node, kLstmOutputLayerNormCoefficientsTensor); - TF_LITE_ENSURE_EQ(context, output_layer_norm_coefficients->dims->size, 1); - TF_LITE_ENSURE_EQ(context, output_layer_norm_coefficients->dims->data[0], - n_cell); - if (is_integer) { - TF_LITE_ENSURE_TYPES_EQ(context, output_layer_norm_coefficients->type, - kTfLiteInt16); - } else { - TF_LITE_ENSURE_TYPES_EQ(context, output_layer_norm_coefficients->type, - kTfLiteFloat32); - } - if (input_layer_norm_coefficients != nullptr) { - micro_context->DeallocateTempTfLiteTensor(input_layer_norm_coefficients); - } - if (forget_layer_norm_coefficients != nullptr) { - micro_context->DeallocateTempTfLiteTensor(forget_layer_norm_coefficients); - } - if (cell_layer_norm_coefficients != nullptr) { - micro_context->DeallocateTempTfLiteTensor(cell_layer_norm_coefficients); - } - if (output_layer_norm_coefficients != nullptr) { - micro_context->DeallocateTempTfLiteTensor(output_layer_norm_coefficients); - } - } - - if (input_to_input_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(input_to_input_weights); - } - if (input_to_forget_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(input_to_forget_weights); - } - if (input_to_cell_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(input_to_cell_weights); - } - if (recurrent_to_input_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(recurrent_to_input_weights); - } - if (recurrent_to_forget_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(recurrent_to_forget_weights); - } - micro_context->DeallocateTempTfLiteTensor(recurrent_to_cell_weights); - if (cell_to_input_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(cell_to_input_weights); - } - if (cell_to_forget_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(cell_to_forget_weights); - } - if (cell_to_output_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(cell_to_output_weights); - } - if (input_gate_bias != nullptr) { - micro_context->DeallocateTempTfLiteTensor(input_gate_bias); - } - if (forget_gate_bias != nullptr) { - micro_context->DeallocateTempTfLiteTensor(forget_gate_bias); - } - if (cell_gate_bias != nullptr) { - micro_context->DeallocateTempTfLiteTensor(cell_gate_bias); - } - if (output_gate_bias != nullptr) { - micro_context->DeallocateTempTfLiteTensor(output_gate_bias); - } - if (projection_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(projection_weights); - } - if (projection_bias != nullptr) { - micro_context->DeallocateTempTfLiteTensor(projection_bias); - } - - return kTfLiteOk; -} - -TfLiteStatus PrecomputeZeroPointTimesWeightWithBias( - TfLiteContext* context, int32_t zero_point, - const TfLiteTensor* weight_tensor, const TfLiteTensor* bias_tensor, - int32_t** output) { - if (weight_tensor == nullptr) { - return kTfLiteOk; - } - - const RuntimeShape& weight_shape = GetTensorShape(weight_tensor); - TF_LITE_ENSURE_EQ(context, weight_shape.DimensionsCount(), 2); - const int row = weight_shape.Dims(0); - const int col = weight_shape.Dims(1); - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - *output = static_cast( - context->AllocatePersistentBuffer(context, row * sizeof(int32_t))); - - if (bias_tensor == nullptr) { - memset(*output, 0, row * sizeof(int32_t)); - } else { - const int32_t* bias = GetTensorData(bias_tensor); - memcpy(*output, bias, row * sizeof(int32_t)); - } - if (zero_point != 0) { - const int8_t* weight = GetTensorData(weight_tensor); - micro_tensor_utils::MatrixScalarMultiplyAccumulate(weight, zero_point, row, - col, *output); - } - return kTfLiteOk; -} - -TfLiteStatus PopulatePrecomputedZPTimesWeightsWithBias( - TfLiteContext* context, UnidirectionalSequenceLstmOpData* op_data, - TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kLstmInputTensor); - TfLiteTensor* output_state = - micro_context->AllocateTempInputTensor(node, kLstmOutputStateTensor); - TF_LITE_ENSURE(context, output_state != nullptr); - TF_LITE_ENSURE(context, output_state->is_variable); - - const int32_t input_zero_point = -input->params.zero_point; - const int32_t output_state_zero_point = -output_state->params.zero_point; - - TfLiteTensor* input_to_input_weights = micro_context->AllocateTempInputTensor( - node, kLstmInputToInputWeightsTensor); - TfLiteTensor* input_to_forget_weights = - micro_context->AllocateTempInputTensor(node, - kLstmInputToForgetWeightsTensor); - TfLiteTensor* input_to_cell_weights = micro_context->AllocateTempInputTensor( - node, kLstmInputToCellWeightsTensor); - TfLiteTensor* input_to_output_weights = - micro_context->AllocateTempInputTensor(node, - kLstmInputToOutputWeightsTensor); - - TfLiteTensor* recurrent_to_input_weights = - micro_context->AllocateTempInputTensor( - node, kLstmRecurrentToInputWeightsTensor); - TfLiteTensor* recurrent_to_forget_weights = - micro_context->AllocateTempInputTensor( - node, kLstmRecurrentToForgetWeightsTensor); - TfLiteTensor* recurrent_to_cell_weights = - micro_context->AllocateTempInputTensor(node, - kLstmRecurrentToCellWeightsTensor); - TfLiteTensor* recurrent_to_output_weights = - micro_context->AllocateTempInputTensor( - node, kLstmRecurrentToOutputWeightsTensor); - - TfLiteTensor* projection_weights = micro_context->AllocateTempInputTensor( - node, kLstmProjectionWeightsTensor); - TfLiteTensor* projection_bias = - micro_context->AllocateTempInputTensor(node, kLstmProjectionBiasTensor); - - IntegerLstmParameter* integer_lstm_params = &op_data->integer_lstm_param; - - TfLiteTensor* intermediate = - micro_context->AllocateTempIntermediateTensor(node, 4); - TF_LITE_ENSURE(context, - intermediate->quantization.type != kTfLiteNoQuantization); - const auto* params = - static_cast(intermediate->quantization.params); - const int32_t hidden_zp = params->zero_point->data[0]; - - // Get bias and perform zero point calculation. - // When there is layer normalization, the gate bias does not apply to matmul - // directly: - // y = ln(w * x + w * r + w * c) + b. - const bool is_layer_norm = op_data->use_layer_norm; - - // Forget gate. - TfLiteTensor* forget_gate_bias = is_layer_norm - ? nullptr - : micro_context->AllocateTempInputTensor( - node, kLstmForgetGateBiasTensor); - TF_LITE_ENSURE_OK( - context, - PrecomputeZeroPointTimesWeightWithBias( - context, input_zero_point, input_to_forget_weights, forget_gate_bias, - &(integer_lstm_params->input_to_forget_effective_bias))); - - TF_LITE_ENSURE_OK( - context, - PrecomputeZeroPointTimesWeightWithBias( - context, output_state_zero_point, recurrent_to_forget_weights, - nullptr, &(integer_lstm_params->recurrent_to_forget_effective_bias))); - - // Modulation gate. - TfLiteTensor* cell_gate_bias = is_layer_norm - ? nullptr - : micro_context->AllocateTempInputTensor( - node, kLstmCellGateBiasTensor); - TF_LITE_ENSURE_OK( - context, - PrecomputeZeroPointTimesWeightWithBias( - context, input_zero_point, input_to_cell_weights, cell_gate_bias, - &(integer_lstm_params->input_to_cell_effective_bias))); - TF_LITE_ENSURE_OK( - context, - PrecomputeZeroPointTimesWeightWithBias( - context, output_state_zero_point, recurrent_to_cell_weights, nullptr, - &(integer_lstm_params->recurrent_to_cell_effective_bias))); - - // Output gate. - TfLiteTensor* output_gate_bias = is_layer_norm - ? nullptr - : micro_context->AllocateTempInputTensor( - node, kLstmOutputGateBiasTensor); - TF_LITE_ENSURE_OK( - context, - PrecomputeZeroPointTimesWeightWithBias( - context, input_zero_point, input_to_output_weights, output_gate_bias, - &(integer_lstm_params->input_to_output_effective_bias))); - - TF_LITE_ENSURE_OK( - context, - PrecomputeZeroPointTimesWeightWithBias( - context, output_state_zero_point, recurrent_to_output_weights, - nullptr, &(integer_lstm_params->recurrent_to_output_effective_bias))); - - // Input gate. The calculation is only meaningful for non-cifg case. - TfLiteTensor* input_gate_bias = is_layer_norm - ? nullptr - : micro_context->AllocateTempInputTensor( - node, kLstmInputGateBiasTensor); - TF_LITE_ENSURE_OK( - context, - PrecomputeZeroPointTimesWeightWithBias( - context, input_zero_point, input_to_input_weights, input_gate_bias, - &(integer_lstm_params->input_to_input_effective_bias))); - TF_LITE_ENSURE_OK( - context, - PrecomputeZeroPointTimesWeightWithBias( - context, output_state_zero_point, recurrent_to_input_weights, nullptr, - &(integer_lstm_params->recurrent_to_input_effective_bias))); - - // Projection bias. The calculation is only meaningful for with projection. - TF_LITE_ENSURE_OK(context, - PrecomputeZeroPointTimesWeightWithBias( - context, hidden_zp, projection_weights, projection_bias, - &(integer_lstm_params->projection_effective_bias))); - - if (input != nullptr) { - micro_context->DeallocateTempTfLiteTensor(input); - } - if (output_state != nullptr) { - micro_context->DeallocateTempTfLiteTensor(output_state); - } - if (input_to_input_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(input_to_input_weights); - } - if (input_to_forget_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(input_to_forget_weights); - } - if (input_to_cell_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(input_to_cell_weights); - } - if (input_to_output_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(input_to_output_weights); - } - if (recurrent_to_input_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(recurrent_to_input_weights); - } - if (recurrent_to_forget_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(recurrent_to_forget_weights); - } - if (recurrent_to_cell_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(recurrent_to_cell_weights); - } - if (recurrent_to_output_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(recurrent_to_output_weights); - } - if (projection_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(projection_weights); - } - if (projection_bias != nullptr) { - micro_context->DeallocateTempTfLiteTensor(projection_bias); - } - if (forget_gate_bias != nullptr) { - micro_context->DeallocateTempTfLiteTensor(forget_gate_bias); - } - if (cell_gate_bias != nullptr) { - micro_context->DeallocateTempTfLiteTensor(cell_gate_bias); - } - if (output_gate_bias != nullptr) { - micro_context->DeallocateTempTfLiteTensor(output_gate_bias); - } - if (input_gate_bias != nullptr) { - micro_context->DeallocateTempTfLiteTensor(input_gate_bias); - } - - if (intermediate != nullptr) { - micro_context->DeallocateTempTfLiteTensor(intermediate); - } - - return kTfLiteOk; -} - -// Resize the output and state tensors based on the sizes of the input tensors. -// Allocate a temporary scratch tensor. Also check that the sizes of the input -// tensors match each other. -TfLiteStatus UnidirectionalSequenceLstmPrepare(TfLiteContext* context, - TfLiteNode* node) { - UnidirectionalSequenceLstmOpData* op_data = - reinterpret_cast(node->user_data); - - MicroContext* micro_context = GetMicroContext(context); - - // Check we have all the inputs and outputs we need. - bool use_layer_norm = false; - if (node->inputs->size == 24) { - TfLiteTensor* forget_layer_norm_coefficients = - micro_context->AllocateTempInputTensor( - node, kLstmForgetLayerNormCoefficientsTensor); - if (forget_layer_norm_coefficients == nullptr) { - use_layer_norm = false; - } else { - use_layer_norm = true; - } - if (forget_layer_norm_coefficients != nullptr) { - micro_context->DeallocateTempTfLiteTensor(forget_layer_norm_coefficients); - } - } else if (node->inputs->size == 20) { - // This is deprecated and is only kept here for backward compatibility. - use_layer_norm = false; - } else { - MicroPrintf("The LSTM Full kernel expects 20 or 24 inputs. Got %d inputs", - node->inputs->size); - return kTfLiteError; - } - TF_LITE_ENSURE_EQ(context, node->outputs->size, 1); - op_data->use_layer_norm = use_layer_norm; - - // Inferring batch size, number of outputs and sequence length and - // number of cells from the input tensors. - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kLstmInputTensor); - op_data->input_zero_point = input->params.zero_point; - const bool is_integer = input->type == kTfLiteInt8; - TF_LITE_ENSURE(context, input->dims->size > 1); - const auto* params = - reinterpret_cast( - node->builtin_data); - const bool time_major = params->time_major; - const int n_batch = time_major ? input->dims->data[1] : input->dims->data[0]; - const int n_input = input->dims->data[2]; - - TfLiteTensor* input_to_output_weights = - micro_context->AllocateTempInputTensor(node, - kLstmInputToOutputWeightsTensor); - const int n_cell = input_to_output_weights->dims->data[0]; - TF_LITE_ENSURE_EQ(context, input_to_output_weights->dims->size, 2); - TF_LITE_ENSURE_EQ(context, input_to_output_weights->dims->data[1], n_input); - - TfLiteTensor* recurrent_to_output_weights = - micro_context->AllocateTempInputTensor( - node, kLstmRecurrentToOutputWeightsTensor); - TF_LITE_ENSURE_EQ(context, recurrent_to_output_weights->dims->size, 2); - TF_LITE_ENSURE_EQ(context, recurrent_to_output_weights->dims->data[0], - n_cell); - const int n_output = recurrent_to_output_weights->dims->data[1]; - - // Check that input tensor dimensions matches with each other. - TF_LITE_ENSURE_OK( - context, CheckInputTensorDimensions(context, node, n_input, n_output, - n_cell, use_layer_norm, is_integer)); - - // Get the pointer to output, output_state and cell_state buffer tensors. - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kLstmOutputTensor); - - TfLiteTensor* output_state = - micro_context->AllocateTempInputTensor(node, kLstmOutputStateTensor); - TF_LITE_ENSURE(context, output_state != nullptr); - TF_LITE_ENSURE(context, output_state->is_variable); - op_data->output_state_zero_point = output_state->params.zero_point; - TfLiteTensor* cell_state = - micro_context->AllocateTempInputTensor(node, kLstmCellStateTensor); - TF_LITE_ENSURE(context, cell_state != nullptr); - TF_LITE_ENSURE(context, cell_state->is_variable); - - // Check the shape of input state tensors. - // These tensor may be 1D or 2D. It's fine as long as the total size is - // correct. - TF_LITE_ENSURE_EQ(context, NumElements(output_state), n_batch * n_output); - TF_LITE_ENSURE_EQ(context, NumElements(cell_state), n_batch * n_cell); - - // Check the shape of output tensor against that of input tensor - TF_LITE_ENSURE_EQ(context, output->dims->size, 3); - TF_LITE_ENSURE_EQ(context, input->dims->data[0], output->dims->data[0]); - TF_LITE_ENSURE_EQ(context, input->dims->data[1], output->dims->data[1]); - TF_LITE_ENSURE_EQ(context, output->dims->data[2], n_output); - - if (is_integer) { - const int num_intermediate_tensors = node->intermediates->size; - TF_LITE_ENSURE(context, num_intermediate_tensors == 5); - } - - TfLiteTensor* input_to_input_weights = micro_context->AllocateTempInputTensor( - node, kLstmInputToInputWeightsTensor); - - const bool use_cifg = (input_to_input_weights == nullptr); - - // Create a primary scratch buffer for hybrid and float - // If is_integer, primary scratch buffer has a different size - if (!is_integer) { - int scratch_buffer_size[2]; - scratch_buffer_size[0] = n_batch; - - if (use_cifg) { - // Reserving space for Cell, Forget, Output gates - scratch_buffer_size[1] = n_cell * 3; - } else { - // Reserving space for Input, Cell, Forget, Output gates - scratch_buffer_size[1] = n_cell * 4; - } - - TF_LITE_ENSURE_OK(context, - context->RequestScratchBufferInArena( - context, - scratch_buffer_size[0] * scratch_buffer_size[1] * - TfLiteTypeGetSize(input->type), - &(op_data->scratch_index[kPrimaryScratchBuffer]))); - } - - if (IsHybridOp(input, input_to_output_weights)) { - TF_LITE_ENSURE(context, kNumHybridTempBuffers <= scratch_index_size); - - TF_LITE_ENSURE_OK(context, SetHybridScales(context, node)); - - op_data->compute_row_sums = true; - - // Allocate temporary tensors to store quantized values of input, - // output_state and cell_state tensors. - - TF_LITE_ENSURE_OK(context, - context->RequestScratchBufferInArena( - context, - GetTensorShape(input).FlatSize() * - TfLiteTypeGetSize(input_to_output_weights->type), - &(op_data->scratch_index[kInputQuantized]))); - - TF_LITE_ENSURE_OK(context, - context->RequestScratchBufferInArena( - context, - GetTensorShape(output_state).FlatSize() * - TfLiteTypeGetSize(input_to_output_weights->type), - &(op_data->scratch_index[kOutputStateQuantized]))); - - TF_LITE_ENSURE_OK(context, - context->RequestScratchBufferInArena( - context, - GetTensorShape(cell_state).FlatSize() * - TfLiteTypeGetSize(input_to_output_weights->type), - &(op_data->scratch_index[kCellStateQuantized]))); - - TF_LITE_ENSURE_OK(context, - context->RequestScratchBufferInArena( - context, n_batch * TfLiteTypeGetSize(kTfLiteFloat32), - &(op_data->scratch_index[kScales]))); - - // Allocate temporary buffers to store scaling factors and product scaling - // factors. The latter is a convenience storage which allows to quantize - // a vector once (which produces the scaling factors) and multiply it with - // different matrices (which requires multiplying the scaling factors with - // the scaling factor of the matrix). - - TF_LITE_ENSURE_OK(context, - context->RequestScratchBufferInArena( - context, n_batch * TfLiteTypeGetSize(kTfLiteFloat32), - &(op_data->scratch_index[kInputScalingFactors]))); - - TF_LITE_ENSURE_OK( - context, context->RequestScratchBufferInArena( - context, n_batch * TfLiteTypeGetSize(kTfLiteFloat32), - &(op_data->scratch_index[kOutputStateScalingFactors]))); - - TF_LITE_ENSURE_OK(context, - context->RequestScratchBufferInArena( - context, n_batch * TfLiteTypeGetSize(kTfLiteFloat32), - &(op_data->scratch_index[kProductScalingFactors]))); - - // Allocate a temporary buffer to store the recovered cell weights. Since - // this is used for diagonal matrices, only need to store n_cell values. - TF_LITE_ENSURE_OK(context, - context->RequestScratchBufferInArena( - context, n_cell * TfLiteTypeGetSize(kTfLiteFloat32), - &(op_data->scratch_index[kRecoveredCellWeights]))); - - // Allocate a temporary buffer to store the accumulated int32 values. - TF_LITE_ENSURE_OK( - context, - context->RequestScratchBufferInArena( - context, n_cell * n_batch * TfLiteTypeGetSize(kTfLiteInt32), - &(op_data->scratch_index[kAccumScratch]))); - - TF_LITE_ENSURE_OK(context, - context->RequestScratchBufferInArena( - context, n_batch * TfLiteTypeGetSize(kTfLiteFloat32), - &(op_data->scratch_index[kInputZeroPoints]))); - - TF_LITE_ENSURE_OK(context, - context->RequestScratchBufferInArena( - context, n_batch * TfLiteTypeGetSize(kTfLiteFloat32), - &(op_data->scratch_index[kOutputStateZeroPoints]))); - - int row_sums_rows = use_cifg ? 6 : 8; - TfLiteTensor* projection_weights = micro_context->AllocateTempInputTensor( - node, kLstmProjectionWeightsTensor); - if (projection_weights != nullptr) { - row_sums_rows += ceil(static_cast(n_output) / n_cell); - } - op_data->row_sums_size = row_sums_rows; - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - op_data->row_sums = static_cast(context->AllocatePersistentBuffer( - context, row_sums_rows * n_cell * sizeof(int32_t))); - if (projection_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(projection_weights); - } - } - - if (is_integer) { - // Integer UnidirectionalSequenceLSTM prepare function for 8x8->16. - // This code path needs 5 intermediate tensors per Op. - // Populate quantization parameters. - PopulateQuantizedLstmParams8x8_16(context, node, - &op_data->integer_lstm_param); - // Allocate scratch buffer. Need 4 16-bit buffer with size n_batch * n_cell - // and 1 8-bit buffer with size n_batch * n_cell. For integer - // UnidirectionalSequenceLSTM, we do not need the extra 32-bit buffer. - for (int i = 0; i < 5; ++i) { - TfLiteType buffer_type = kTfLiteInt16; - - if (i == 4) { - buffer_type = kTfLiteInt8; - } - - TF_LITE_ENSURE_OK( - context, - context->RequestScratchBufferInArena( - context, n_batch * n_cell * TfLiteTypeGetSize(buffer_type), - &(op_data->scratch_index[i]))); - } - - // Populate precomputed zp * weight. - TF_LITE_ENSURE_OK(context, PopulatePrecomputedZPTimesWeightsWithBias( - context, op_data, node)); - } - - if (input != nullptr) { - micro_context->DeallocateTempTfLiteTensor(input); - } - if (input_to_output_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(input_to_output_weights); - } - if (recurrent_to_output_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(recurrent_to_output_weights); - } - if (output != nullptr) { - micro_context->DeallocateTempTfLiteTensor(output); - } - if (output_state != nullptr) { - micro_context->DeallocateTempTfLiteTensor(output_state); - } - if (cell_state != nullptr) { - micro_context->DeallocateTempTfLiteTensor(cell_state); - } - - if (input_to_input_weights != nullptr) { - micro_context->DeallocateTempTfLiteTensor(input_to_input_weights); - } - return kTfLiteOk; -} - -TfLiteStatus UnidirectionalSequenceLstmEval(TfLiteContext* context, - TfLiteNode* node) { - TFLITE_DCHECK(context->GetScratchBuffer != nullptr); - - const auto* params = - reinterpret_cast( - node->builtin_data); - const UnidirectionalSequenceLstmOpData* op_data = - reinterpret_cast(node->user_data); - const bool use_layer_norm = op_data->use_layer_norm; - const bool time_major = params->time_major; - - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kLstmInputTensor); - - const TfLiteEvalTensor* input_to_input_weights = tflite::micro::GetEvalInput( - context, node, kLstmInputToInputWeightsTensor); - - const TfLiteEvalTensor* input_to_forget_weights = tflite::micro::GetEvalInput( - context, node, kLstmInputToForgetWeightsTensor); - - const TfLiteEvalTensor* input_to_cell_weights = - tflite::micro::GetEvalInput(context, node, kLstmInputToCellWeightsTensor); - - const TfLiteEvalTensor* input_to_output_weights = tflite::micro::GetEvalInput( - context, node, kLstmInputToOutputWeightsTensor); - - const TfLiteEvalTensor* recurrent_to_input_weights = - tflite::micro::GetEvalInput(context, node, - kLstmRecurrentToInputWeightsTensor); - - const TfLiteEvalTensor* recurrent_to_forget_weights = - tflite::micro::GetEvalInput(context, node, - kLstmRecurrentToForgetWeightsTensor); - - const TfLiteEvalTensor* recurrent_to_cell_weights = - tflite::micro::GetEvalInput(context, node, - kLstmRecurrentToCellWeightsTensor); - - const TfLiteEvalTensor* recurrent_to_output_weights = - tflite::micro::GetEvalInput(context, node, - kLstmRecurrentToOutputWeightsTensor); - - const TfLiteEvalTensor* cell_to_input_weights = - tflite::micro::GetEvalInput(context, node, kLstmCellToInputWeightsTensor); - - const TfLiteEvalTensor* cell_to_forget_weights = tflite::micro::GetEvalInput( - context, node, kLstmCellToForgetWeightsTensor); - - const TfLiteEvalTensor* cell_to_output_weights = tflite::micro::GetEvalInput( - context, node, kLstmCellToOutputWeightsTensor); - - const TfLiteEvalTensor* input_gate_bias = - tflite::micro::GetEvalInput(context, node, kLstmInputGateBiasTensor); - - const TfLiteEvalTensor* forget_gate_bias = - tflite::micro::GetEvalInput(context, node, kLstmForgetGateBiasTensor); - - const TfLiteEvalTensor* cell_gate_bias = - tflite::micro::GetEvalInput(context, node, kLstmCellGateBiasTensor); - - const TfLiteEvalTensor* output_gate_bias = - tflite::micro::GetEvalInput(context, node, kLstmOutputGateBiasTensor); - - const TfLiteEvalTensor* projection_weights = - tflite::micro::GetEvalInput(context, node, kLstmProjectionWeightsTensor); - - const TfLiteEvalTensor* projection_bias = - tflite::micro::GetEvalInput(context, node, kLstmProjectionBiasTensor); - - TfLiteEvalTensor* output_state = - tflite::micro::GetMutableEvalInput(context, node, kLstmOutputStateTensor); - - TfLiteEvalTensor* cell_state = - tflite::micro::GetMutableEvalInput(context, node, kLstmCellStateTensor); - - TFLITE_DCHECK(cell_state != nullptr); - - const TfLiteEvalTensor* input_layer_norm_coefficients = - use_layer_norm ? tflite::micro::GetEvalInput( - context, node, kLstmInputLayerNormCoefficientsTensor) - : nullptr; - const TfLiteEvalTensor* forget_layer_norm_coefficients = - use_layer_norm - ? tflite::micro::GetEvalInput(context, node, - kLstmForgetLayerNormCoefficientsTensor) - : nullptr; - const TfLiteEvalTensor* cell_layer_norm_coefficients = - use_layer_norm ? tflite::micro::GetEvalInput( - context, node, kLstmCellLayerNormCoefficientsTensor) - : nullptr; - const TfLiteEvalTensor* output_layer_norm_coefficients = - use_layer_norm - ? tflite::micro::GetEvalInput(context, node, - kLstmOutputLayerNormCoefficientsTensor) - : nullptr; - - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kLstmOutputTensor); - - // Copy out the LSTM specific params so they can be passed in the function. - TfLiteLSTMParams lstm_params; - lstm_params.activation = params->activation; - lstm_params.cell_clip = params->cell_clip; - lstm_params.proj_clip = params->proj_clip; - lstm_params.asymmetric_quantize_inputs = params->asymmetric_quantize_inputs; - - switch (input_to_output_weights->type) { - case kTfLiteFloat32: { - // Index the scratch buffers pointers to the global scratch buffer. - return EvalFloatLstm( - input, input_to_input_weights, input_to_forget_weights, - input_to_cell_weights, input_to_output_weights, - recurrent_to_input_weights, recurrent_to_forget_weights, - recurrent_to_cell_weights, recurrent_to_output_weights, - cell_to_input_weights, cell_to_forget_weights, cell_to_output_weights, - input_layer_norm_coefficients, forget_layer_norm_coefficients, - cell_layer_norm_coefficients, output_layer_norm_coefficients, - /*aux_input=*/nullptr, - /*aux_input_to_input_weights=*/nullptr, - /*aux_input_to_forget_weights=*/nullptr, - /*aux_input_to_cell_weights=*/nullptr, - /*aux_input_to_output_weights=*/nullptr, input_gate_bias, - forget_gate_bias, cell_gate_bias, output_gate_bias, - projection_weights, projection_bias, &lstm_params, - /*forward_sequence=*/true, time_major, - /*output_offset=*/0, - reinterpret_cast(context->GetScratchBuffer( - context, op_data->scratch_index[kPrimaryScratchBuffer])), - output_state, cell_state, output); - } break; - case kTfLiteUInt8: - case kTfLiteInt8: { - const bool is_hybrid = input->type == kTfLiteFloat32; - if (is_hybrid) { - // Index the scratch buffers pointers to the global scratch buffer. - UnidirectionalSequenceLstmOpData* op_data_rw = - reinterpret_cast( - node->user_data); - return EvalHybridLstm( - &(op_data->hybrid_lstm_scales), input, input_to_input_weights, - /*input_to_input_weights_ledger*/ nullptr, input_to_forget_weights, - /*input_to_forget_weights_ledger*/ nullptr, input_to_cell_weights, - /*input_to_cell_weights_ledger*/ nullptr, input_to_output_weights, - /*input_to_output_weights_ledger*/ nullptr, - recurrent_to_input_weights, - /*recurrent_to_input_weights_ledger*/ nullptr, - recurrent_to_forget_weights, - /*recurrent_to_forget_weights_ledger*/ nullptr, - recurrent_to_cell_weights, - /*recurrent_to_cell_weights_ledger*/ nullptr, - recurrent_to_output_weights, - /*recurrent_to_output_weights_ledger*/ nullptr, - cell_to_input_weights, cell_to_forget_weights, - cell_to_output_weights, input_layer_norm_coefficients, - forget_layer_norm_coefficients, cell_layer_norm_coefficients, - output_layer_norm_coefficients, - /*aux_input=*/nullptr, - /*aux_input_to_input_weights=*/nullptr, - /*aux_input_to_forget_weights=*/nullptr, - /*aux_input_to_cell_weights=*/nullptr, - /*aux_input_to_output_weights=*/nullptr, input_gate_bias, - forget_gate_bias, cell_gate_bias, output_gate_bias, - projection_weights, /*projection_weights_ledger*/ nullptr, - projection_bias, &lstm_params, - /*forward_sequence=*/true, time_major, - /*output_offset=*/0, - reinterpret_cast(context->GetScratchBuffer( - context, op_data->scratch_index[kPrimaryScratchBuffer])), - reinterpret_cast(context->GetScratchBuffer( - context, op_data->scratch_index[kInputScalingFactors])), - /*aux_input_sf=*/nullptr, - reinterpret_cast(context->GetScratchBuffer( - context, op_data->scratch_index[kOutputStateScalingFactors])), - reinterpret_cast(context->GetScratchBuffer( - context, op_data->scratch_index[kProductScalingFactors])), - reinterpret_cast(context->GetScratchBuffer( - context, op_data->scratch_index[kRecoveredCellWeights])), - reinterpret_cast(context->GetScratchBuffer( - context, op_data->scratch_index[kInputQuantized])), - /*aux_input_quantized=*/nullptr, - reinterpret_cast(context->GetScratchBuffer( - context, op_data->scratch_index[kOutputStateQuantized])), - reinterpret_cast(context->GetScratchBuffer( - context, op_data->scratch_index[kCellStateQuantized])), - reinterpret_cast(context->GetScratchBuffer( - context, op_data->scratch_index[kScales])), - output_state, cell_state, - reinterpret_cast(context->GetScratchBuffer( - context, op_data->scratch_index[kAccumScratch])), - output, - reinterpret_cast(context->GetScratchBuffer( - context, op_data->scratch_index[kInputZeroPoints])), - /*aux_input_zp=*/nullptr, - reinterpret_cast(context->GetScratchBuffer( - context, op_data->scratch_index[kOutputStateZeroPoints])), - op_data_rw->row_sums, op_data_rw->row_sums_size, - &op_data_rw->compute_row_sums); - } else { - return EvalInteger8x8_16Lstm( - input, input_to_input_weights, input_to_forget_weights, - input_to_cell_weights, input_to_output_weights, - recurrent_to_input_weights, recurrent_to_forget_weights, - recurrent_to_cell_weights, recurrent_to_output_weights, - cell_to_input_weights, cell_to_forget_weights, - cell_to_output_weights, input_layer_norm_coefficients, - forget_layer_norm_coefficients, cell_layer_norm_coefficients, - output_layer_norm_coefficients, input_gate_bias, forget_gate_bias, - cell_gate_bias, output_gate_bias, projection_weights, - projection_bias, &lstm_params, /*forward_sequence=*/true, - time_major, &op_data->integer_lstm_param, - op_data->output_state_zero_point, output_state, cell_state, output, - reinterpret_cast( - context->GetScratchBuffer(context, op_data->scratch_index[0])), - reinterpret_cast( - context->GetScratchBuffer(context, op_data->scratch_index[1])), - reinterpret_cast( - context->GetScratchBuffer(context, op_data->scratch_index[2])), - reinterpret_cast( - context->GetScratchBuffer(context, op_data->scratch_index[3])), - reinterpret_cast( - context->GetScratchBuffer(context, op_data->scratch_index[4])), - nullptr); - } - } break; - default: - MicroPrintf("Type %s is not currently supported.", - TfLiteTypeGetName(input_to_output_weights->type)); - return kTfLiteError; - } -} - -} // namespace - -TfLiteRegistration Register_UNIDIRECTIONAL_SEQUENCE_LSTM() { - return tflite::micro::RegisterOp(UnidirectionalSequenceLstmInit, - UnidirectionalSequenceLstmPrepare, - UnidirectionalSequenceLstmEval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/unidirectional_sequence_lstm_test_config.h b/code/components/tflite-lib/tensorflow/lite/micro/kernels/unidirectional_sequence_lstm_test_config.h deleted file mode 100644 index e37c0efd..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/unidirectional_sequence_lstm_test_config.h +++ /dev/null @@ -1,244 +0,0 @@ -/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_MICRO_KERNELS_UNIDIRECTIONAL_SEQUENCE_LSTM_TEST_CONFIG_H_ -#define TENSORFLOW_LITE_MICRO_KERNELS_UNIDIRECTIONAL_SEQUENCE_LSTM_TEST_CONFIG_H_ - -#include "tensorflow/lite/c/common.h" - -namespace tflite { -namespace testing { - -// TODO(b/230666079) enable below tests for xtensa when the xtensa -// kernel is reconciled with reference kernel -#if !defined(XTENSA) - -typedef struct LstmIntegerTestConfig { - const int n_batch; - const int n_input; - const int n_cell; - const int n_output; - const int sequence_length; - const bool time_major; - const bool use_cifg; - const bool use_peephole; - const bool use_projection_weights; - const bool use_projection_bias; - const bool use_layer_norm; - const bool use_8x8_8_implementation; - float intermediate_scale[5][2]; - int intermediate_zp[5][2]; - TfLiteAffineQuantization* intermediate_qparam; - - const float* input; - int8_t* input_quant; - - const float* input_to_input_weights; - int8_t* lstm_i2i_quant; - const float* input_to_forget_weights; - int8_t* lstm_i2f_quant; - const float* input_to_cell_weights; - int8_t* lstm_i2c_quant; - const float* input_to_output_weights; - int8_t* lstm_i2o_quant; - - const float* recurrent_to_input_weights; - int8_t* lstm_r2i_quant; - const float* recurrent_to_forget_weights; - int8_t* lstm_r2f_quant; - const float* recurrent_to_cell_weights; - int8_t* lstm_r2c_quant; - const float* recurrent_to_output_weights; - int8_t* lstm_r2o_quant; - - const float* cell_to_input_weights; - int16_t* lstm_c2i_quant; - const float* cell_to_forget_weights; - int16_t* lstm_c2f_quant; - const float* cell_to_output_weights; - int16_t* lstm_c2o_quant; - - const float* input_gate_bias; - int32_t* lstm_igate_bias_quant; - const float* forget_gate_bias; - int32_t* lstm_fgate_bias_quant; - const float* cell_gate_bias; - int32_t* lstm_cgate_bias_quant; - const float* output_gate_bias; - int32_t* lstm_ogate_bias_quant; - - const float* projection_weights; - int8_t* lstm_proj_w_quant; - const float* projection_bias; - int32_t* projection_bias_quant; - - int16_t* output_state; - int16_t* cell_state; - - const float* input_layer_norm_coefficients; - int16_t* lstm_input_layer_norm_coeff_quant; - const float* forget_layer_norm_coefficients; - int16_t* lstm_forget_layer_norm_coeff_quant; - const float* cell_layer_norm_coefficients; - int16_t* lstm_cell_layer_norm_coeff_quant; - const float* output_layer_norm_coefficients; - int16_t* lstm_output_layer_norm_coeff_quant; - - int8_t* output; - const int8_t* expected_output; - - bool asymmetric_quantize_inputs; - const float ranges[25][2]; -} LstmIntegerTestConfig; - -typedef struct LstmFloatTestConfig { - const int n_batch; - const int n_input; - const int n_cell; - const int n_output; - const int sequence_length; - const bool time_major; - const bool use_cifg; - const bool use_peephole; - const bool use_projection_weights; - const bool use_projection_bias; - const bool use_layer_norm; - const float cell_clip; - const float proj_clip; - - const float* input_original; - float* input; - - const float* input_to_input_weights; - const float* input_to_forget_weights; - const float* input_to_cell_weights; - const float* input_to_output_weights; - - const float* recurrent_to_input_weights; - const float* recurrent_to_forget_weights; - const float* recurrent_to_cell_weights; - const float* recurrent_to_output_weights; - - const float* cell_to_input_weights; - const float* cell_to_forget_weights; - const float* cell_to_output_weights; - - const float* input_gate_bias; - const float* forget_gate_bias; - const float* cell_gate_bias; - const float* output_gate_bias; - - const float* projection_weights; - const float* projection_bias; - - float* output_state; - float* cell_state; - - const float* input_layer_norm_coefficients; - const float* forget_layer_norm_coefficients; - const float* cell_layer_norm_coefficients; - const float* output_layer_norm_coefficients; - - float* output; - const float* expected_output_original; - float* expected_output; -} LstmFloatTestConfig; - -typedef struct LstmWeightQuantizationBuffers { - int8_t* lstm_i2i_quant; - float* lstm_i2i_scale; - int* lstm_i2i_zp; - TfLiteAffineQuantization* lstm_i2i_qparam; - - int8_t* lstm_i2f_quant; - float* lstm_i2f_scale; - int* lstm_i2f_zp; - TfLiteAffineQuantization* lstm_i2f_qparam; - - int8_t* lstm_i2c_quant; - float* lstm_i2c_scale; - int* lstm_i2c_zp; - TfLiteAffineQuantization* lstm_i2c_qparam; - - int8_t* lstm_i2o_quant; - float* lstm_i2o_scale; - int* lstm_i2o_zp; - TfLiteAffineQuantization* lstm_i2o_qparam; - - int8_t* lstm_r2i_quant; - float* lstm_r2i_scale; - int* lstm_r2i_zp; - TfLiteAffineQuantization* lstm_r2i_qparam; - - int8_t* lstm_r2f_quant; - float* lstm_r2f_scale; - int* lstm_r2f_zp; - TfLiteAffineQuantization* lstm_r2f_qparam; - - int8_t* lstm_r2c_quant; - float* lstm_r2c_scale; - int* lstm_r2c_zp; - TfLiteAffineQuantization* lstm_r2c_qparam; - - int8_t* lstm_r2o_quant; - float* lstm_r2o_scale; - int* lstm_r2o_zp; - TfLiteAffineQuantization* lstm_r2o_qparam; - - int8_t* lstm_c2i_quant; - float* lstm_c2i_scale; - int* lstm_c2i_zp; - TfLiteAffineQuantization* lstm_c2i_qparam; - - int8_t* lstm_c2f_quant; - float* lstm_c2f_scale; - int* lstm_c2f_zp; - TfLiteAffineQuantization* lstm_c2f_qparam; - - int8_t* lstm_c2o_quant; - float* lstm_c2o_scale; - int* lstm_c2o_zp; - TfLiteAffineQuantization* lstm_c2o_qparam; - - int8_t* lstm_proj_w_quant; - float* lstm_proj_w_scale; - int* lstm_proj_w_zp; - TfLiteAffineQuantization* lstm_proj_w_qparam; -} LstmWeightQuantizationBuffers; - -extern LstmIntegerTestConfig lstm_integer_no_peephole_config; - -extern LstmIntegerTestConfig lstm_integer_peephole_config; - -extern LstmFloatTestConfig lstm_no_cifg_no_peephole_no_proj_config; - -extern LstmFloatTestConfig lstm_cifg_peephole_no_proj_config; - -extern LstmFloatTestConfig lstm_no_cifg_peephole_proj_config; - -extern LstmFloatTestConfig lstm_no_cifg_peephole_proj_bias_config; - -extern LstmWeightQuantizationBuffers lstm_no_cifg_no_peephole_no_proj_buffers; - -extern LstmWeightQuantizationBuffers lstm_cifg_peephole_no_proj_buffers; - -extern LstmWeightQuantizationBuffers lstm_no_cifg_peephole_proj_buffers; - -extern LstmFloatTestConfig cifg_peephole_no_proj_config_layer_norm; - -#endif // !defined(XTENSA) -} // namespace testing -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_KERNELS_UNIDIRECTIONAL_SEQUENCE_LSTM_TEST_CONFIG_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/unpack.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/unpack.cc deleted file mode 100644 index d199add0..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/unpack.cc +++ /dev/null @@ -1,111 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { -namespace ops { -namespace micro { -namespace unpack { -namespace { - -constexpr int kInputTensor = 0; - -template -TfLiteStatus UnpackImpl(TfLiteContext* context, TfLiteNode* node, - const TfLiteEvalTensor* input, int output_count, - int axis) { - const TfLiteEvalTensor* output0 = - tflite::micro::GetEvalOutput(context, node, 0); - const TfLiteIntArray* input_dims = input->dims; - const TfLiteIntArray* output_dims = output0->dims; - const int dimensions = input_dims->size; - - if (axis < 0) { - axis += input->dims->size; - } - - TFLITE_DCHECK_LT(axis, dimensions); - - int outer_size = 1; - for (int i = 0; i < axis; ++i) { - outer_size *= input_dims->data[i]; - } - int copy_size = 1; - for (int i = axis + 1; i < dimensions; ++i) { - copy_size *= input_dims->data[i]; - } - int output_size = 1; - for (int i = 0; i < output_dims->size; ++i) { - output_size *= output_dims->data[i]; - } - TFLITE_DCHECK_EQ(output_size, copy_size * outer_size); - - const T* input_data = tflite::micro::GetTensorData(input); - - for (int i = 0; i < output_count; ++i) { - TfLiteEvalTensor* t = tflite::micro::GetEvalOutput(context, node, i); - T* output_data = tflite::micro::GetTensorData(t); - for (int k = 0; k < outer_size; ++k) { - T* output_ptr = output_data + copy_size * k; - int loc = k * output_count * copy_size + i * copy_size; - const T* input_ptr = input_data + loc; - for (int j = 0; j < copy_size; ++j) output_ptr[j] = input_ptr[j]; - } - } - - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - TfLiteUnpackParams* data = - reinterpret_cast(node->builtin_data); - - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kInputTensor); - - switch (input->type) { - case kTfLiteFloat32: { - return UnpackImpl(context, node, input, data->num, data->axis); - } - case kTfLiteInt32: { - return UnpackImpl(context, node, input, data->num, data->axis); - } - case kTfLiteInt8: { - return UnpackImpl(context, node, input, data->num, data->axis); - } - default: { - TF_LITE_KERNEL_LOG(context, "Type '%s' is not supported by unpack.", - TfLiteTypeGetName(input->type)); - return kTfLiteError; - } - } - - return kTfLiteOk; -} -} // namespace -} // namespace unpack - -TfLiteRegistration Register_UNPACK() { - return tflite::micro::RegisterOp(nullptr, nullptr, unpack::Eval); -} - -} // namespace micro -} // namespace ops -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/var_handle.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/var_handle.cc deleted file mode 100644 index db044f3f..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/var_handle.cc +++ /dev/null @@ -1,93 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include - -#include - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/memory_helpers.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" -#include "tensorflow/lite/micro/micro_graph.h" -#include "tensorflow/lite/micro/micro_resource_variable.h" -#include "tensorflow/lite/schema/schema_generated.h" - -namespace tflite { - -namespace { - -struct OpData { - int32_t resource_id; -}; - -void* Init(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(OpData)); -} - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - OpData* op_data = reinterpret_cast(node->user_data); - const auto* params = - reinterpret_cast(node->builtin_data); - - tflite::MicroContext* micro_context = tflite::GetMicroContext(context); - MicroGraph& graph_info = micro_context->graph(); - - MicroResourceVariables* resources = graph_info.GetResourceVariables(); - if (resources == nullptr) { - MicroPrintf( - "VAR_HANDLE requires resource variables. Please create " - "ResourceVariables and pass it to the interpreter."); - return kTfLiteError; - } - op_data->resource_id = - resources->CreateIdIfNoneFound(params->container, params->shared_name); - if (op_data->resource_id < 0) { - return kTfLiteError; - } - - TfLiteEvalTensor* output = tflite::micro::GetEvalOutput(context, node, 0); - TFLITE_DCHECK(output != nullptr); - - // Assign saved resource_id so this output tensor will always return the - // correct resource id. - output->data.i32 = &op_data->resource_id; - - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - OpData* op_data = reinterpret_cast(node->user_data); - - TfLiteEvalTensor* output = tflite::micro::GetEvalOutput(context, node, 0); - TFLITE_DCHECK(output != nullptr); - - // Assign saved resource_id so this output tensor will always return the - // correct resource id. - output->data.i32 = &op_data->resource_id; - return kTfLiteOk; -} - -} // namespace. - -TfLiteRegistration Register_VAR_HANDLE() { - return tflite::micro::RegisterOp(Init, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/while.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/while.cc deleted file mode 100644 index 811c9eae..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/while.cc +++ /dev/null @@ -1,133 +0,0 @@ -/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include - -#include - -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/memory_helpers.h" -#include "tensorflow/lite/micro/micro_context.h" -#include "tensorflow/lite/micro/micro_graph.h" -#include "tensorflow/lite/micro/micro_utils.h" - -namespace tflite { - -namespace { - -struct OpData { - int cond_subgraph_index; - int body_subgraph_index; -}; - -void* Init(TfLiteContext* context, const char* buffer, size_t length) { - TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); - return context->AllocatePersistentBuffer(context, sizeof(OpData)); -} - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - OpData* op_data = reinterpret_cast(node->user_data); - const auto* params = - reinterpret_cast(node->builtin_data); - - op_data->cond_subgraph_index = params->cond_subgraph_index; - op_data->body_subgraph_index = params->body_subgraph_index; - - // The first input is the condition. - tflite::MicroContext* micro_context = tflite::GetMicroContext(context); - - size_t num_inputs = node->inputs->size; - size_t num_outputs = node->outputs->size; - - MicroGraph& graph_info = micro_context->graph(); - - TF_LITE_ENSURE(context, - op_data->cond_subgraph_index < graph_info.NumSubgraphs()); - TF_LITE_ENSURE(context, - op_data->body_subgraph_index < graph_info.NumSubgraphs()); - - TF_LITE_ENSURE_EQ(context, num_inputs, - graph_info.NumSubgraphInputs(op_data->cond_subgraph_index)); - TF_LITE_ENSURE_EQ(context, num_inputs, - graph_info.NumSubgraphInputs(op_data->body_subgraph_index)); - TF_LITE_ENSURE_EQ(context, num_inputs, num_outputs); - TF_LITE_ENSURE_EQ( - context, num_outputs, - graph_info.NumSubgraphOutputs(op_data->body_subgraph_index)); - - return kTfLiteOk; -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const OpData* op_data = reinterpret_cast(node->user_data); - - tflite::MicroContext* micro_context = tflite::GetMicroContext(context); - MicroGraph* graph_info = µ_context->graph(); - - TF_LITE_ENSURE_OK(context, - tflite::micro::CopyOpInputsToSubgraphInputs( - context, node, graph_info, op_data->cond_subgraph_index, - /*first_tensor_idx=*/0)); - - TF_LITE_ENSURE_OK(context, - graph_info->InvokeSubgraph(op_data->cond_subgraph_index)); - - TfLiteEvalTensor* cond_subgraph_output = graph_info->GetSubgraphOutput( - op_data->cond_subgraph_index, /*tensor_idx=*/0); - bool cond_value = cond_subgraph_output->data.b[0]; - - TF_LITE_ENSURE_OK(context, - tflite::micro::CopyOpInputsToSubgraphInputs( - context, node, graph_info, op_data->body_subgraph_index, - /*first_tensor_idx=*/0)); - TF_LITE_ENSURE_OK(context, - tflite::micro::CopyOpInputsToOpOutputs(context, node)); - - while (cond_value == true) { - // Copy output of this iteration back to the body input. - TF_LITE_ENSURE_OK( - context, tflite::micro::CopyOpOutputsToSubgraphInputs( - context, node, graph_info, op_data->body_subgraph_index)); - TF_LITE_ENSURE_OK(context, - graph_info->InvokeSubgraph(op_data->body_subgraph_index)); - - TF_LITE_ENSURE_OK( - context, tflite::micro::CopySubgraphOutputsToOpOutputs( - context, node, graph_info, op_data->body_subgraph_index)); - TF_LITE_ENSURE_OK( - context, tflite::micro::CopyOpOutputsToSubgraphInputs( - context, node, graph_info, op_data->cond_subgraph_index)); - TF_LITE_ENSURE_OK(context, - graph_info->InvokeSubgraph(op_data->cond_subgraph_index)); - - cond_subgraph_output = graph_info->GetSubgraphOutput( - op_data->cond_subgraph_index, /*tensor_idx=*/0); - cond_value = cond_subgraph_output->data.b[0]; - } - - return kTfLiteOk; -} - -} // namespace. - -TfLiteRegistration Register_WHILE() { - return tflite::micro::RegisterOp(Init, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/kernels/zeros_like.cc b/code/components/tflite-lib/tensorflow/lite/micro/kernels/zeros_like.cc deleted file mode 100644 index fd6e6612..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/kernels/zeros_like.cc +++ /dev/null @@ -1,87 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" - -namespace tflite { -namespace { - -constexpr int kInputTensor = 0; -constexpr int kOutputTensor = 0; - -TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { - MicroContext* micro_context = GetMicroContext(context); - - TF_LITE_ENSURE_EQ(context, NumInputs(node), 1); - TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - TfLiteTensor* output = - micro_context->AllocateTempOutputTensor(node, kOutputTensor); - TF_LITE_ENSURE(context, output != nullptr); - output->type = input->type; - - micro_context->DeallocateTempTfLiteTensor(input); - micro_context->DeallocateTempTfLiteTensor(output); - return kTfLiteOk; -} - -template -void resetZeros(T* out, const int num_elements) { - for (int i = 0; i < num_elements; ++i) { - out[i] = static_cast(0); - } -} - -TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kInputTensor); - TfLiteEvalTensor* output = - tflite::micro::GetEvalOutput(context, node, kOutputTensor); - int flat_size = MatchingFlatSize(tflite::micro::GetTensorShape(input), - tflite::micro::GetTensorShape(output)); - switch (input->type) { - case kTfLiteInt64: - resetZeros(tflite::micro::GetTensorData(output), flat_size); - break; - case kTfLiteInt32: - resetZeros(tflite::micro::GetTensorData(output), flat_size); - break; - case kTfLiteInt8: - resetZeros(tflite::micro::GetTensorData(output), flat_size); - break; - case kTfLiteFloat32: - resetZeros(tflite::micro::GetTensorData(output), flat_size); - break; - default: - TF_LITE_KERNEL_LOG(context, - "ZerosLike only currently supports int64, int32, " - "and float32, got %d.", - input->type); - return kTfLiteError; - } - return kTfLiteOk; -} -} // namespace - -TfLiteRegistration Register_ZEROS_LIKE() { - return tflite::micro::RegisterOp(nullptr, Prepare, Eval); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/memory_helpers.cc b/code/components/tflite-lib/tensorflow/lite/micro/memory_helpers.cc deleted file mode 100644 index 930202de..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/memory_helpers.cc +++ /dev/null @@ -1,170 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/memory_helpers.h" - -#include -#include - -#include "flatbuffers/flatbuffers.h" // from @flatbuffers -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/core/api/error_reporter.h" -#include "tensorflow/lite/core/api/flatbuffer_conversions.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/schema/schema_generated.h" - -namespace tflite { - -uint8_t* AlignPointerUp(uint8_t* data, size_t alignment) { - std::uintptr_t data_as_uintptr_t = reinterpret_cast(data); - uint8_t* aligned_result = reinterpret_cast( - ((data_as_uintptr_t + (alignment - 1)) / alignment) * alignment); - return aligned_result; -} - -uint8_t* AlignPointerDown(uint8_t* data, size_t alignment) { - std::uintptr_t data_as_uintptr_t = reinterpret_cast(data); - uint8_t* aligned_result = - reinterpret_cast((data_as_uintptr_t / alignment) * alignment); - return aligned_result; -} - -size_t AlignSizeUp(size_t size, size_t alignment) { - size_t aligned_size = (((size + (alignment - 1)) / alignment) * alignment); - return aligned_size; -} - -TfLiteStatus TfLiteTypeSizeOf(TfLiteType type, size_t* size) { - switch (type) { - case kTfLiteFloat16: - *size = sizeof(int16_t); - break; - case kTfLiteFloat32: - *size = sizeof(float); - break; - case kTfLiteFloat64: - *size = sizeof(double); - break; - case kTfLiteInt16: - *size = sizeof(int16_t); - break; - case kTfLiteInt32: - *size = sizeof(int32_t); - break; - case kTfLiteUInt32: - *size = sizeof(uint32_t); - break; - case kTfLiteUInt8: - *size = sizeof(uint8_t); - break; - case kTfLiteInt8: - *size = sizeof(int8_t); - break; - case kTfLiteInt64: - *size = sizeof(int64_t); - break; - case kTfLiteUInt64: - *size = sizeof(uint64_t); - break; - case kTfLiteBool: - *size = sizeof(bool); - break; - case kTfLiteResource: - *size = sizeof(int32_t); - break; - case kTfLiteComplex64: - *size = sizeof(float) * 2; - break; - case kTfLiteComplex128: - *size = sizeof(double) * 2; - break; - default: - return kTfLiteError; - } - return kTfLiteOk; -} - -TfLiteStatus BytesRequiredForTensor(const tflite::Tensor& flatbuffer_tensor, - size_t* bytes, size_t* type_size, - ErrorReporter* error_reporter) { - int element_count = 1; - // If flatbuffer_tensor.shape == nullptr, then flatbuffer_tensor is a scalar - // so has 1 element. - if (flatbuffer_tensor.shape() != nullptr) { - for (size_t n = 0; n < flatbuffer_tensor.shape()->Length(); ++n) { - element_count *= flatbuffer_tensor.shape()->Get(n); - } - } - - TfLiteType tf_lite_type; - TF_LITE_ENSURE_STATUS(ConvertTensorType(flatbuffer_tensor.type(), - &tf_lite_type, error_reporter)); - TF_LITE_ENSURE_STATUS(TfLiteTypeSizeOf(tf_lite_type, type_size)); - *bytes = element_count * (*type_size); - return kTfLiteOk; -} - -TfLiteStatus TfLiteEvalTensorByteLength(const TfLiteEvalTensor* eval_tensor, - size_t* out_bytes) { - TFLITE_DCHECK(out_bytes != nullptr); - - int element_count = 1; - // If eval_tensor->dims == nullptr, then tensor is a scalar so has 1 element. - if (eval_tensor->dims != nullptr) { - for (int n = 0; n < eval_tensor->dims->size; ++n) { - element_count *= eval_tensor->dims->data[n]; - } - } - size_t type_size; - TF_LITE_ENSURE_STATUS(TfLiteTypeSizeOf(eval_tensor->type, &type_size)); - *out_bytes = element_count * type_size; - return kTfLiteOk; -} - -TfLiteStatus AllocateOutputDimensionsFromInput(TfLiteContext* context, - const TfLiteTensor* input1, - const TfLiteTensor* input2, - TfLiteTensor* output) { - const TfLiteTensor* input = nullptr; - - TF_LITE_ENSURE(context, input1->dims != nullptr); - TF_LITE_ENSURE(context, input2->dims != nullptr); - TF_LITE_ENSURE(context, output->dims->size == 0); - - input = input1->dims->size > input2->dims->size ? input1 : input2; - TF_LITE_ENSURE(context, output->type == input->type); - - size_t size = 0; - TfLiteTypeSizeOf(input->type, &size); - const int dimensions_count = tflite::GetTensorShape(input).DimensionsCount(); - for (int i = 0; i < dimensions_count; i++) { - size *= input->dims->data[i]; - } - - output->bytes = size; - - output->dims = - reinterpret_cast(context->AllocatePersistentBuffer( - context, TfLiteIntArrayGetSizeInBytes(size))); - - output->dims->size = input->dims->size; - for (int i = 0; i < dimensions_count; i++) { - output->dims->data[i] = input->dims->data[i]; - } - - return kTfLiteOk; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/memory_helpers.h b/code/components/tflite-lib/tensorflow/lite/micro/memory_helpers.h deleted file mode 100644 index 8f5526ce..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/memory_helpers.h +++ /dev/null @@ -1,59 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_MICRO_MEMORY_HELPERS_H_ -#define TENSORFLOW_LITE_MICRO_MEMORY_HELPERS_H_ - -#include -#include - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/core/api/error_reporter.h" -#include "tensorflow/lite/schema/schema_generated.h" - -namespace tflite { - -// Returns the next pointer address aligned to the given alignment. -uint8_t* AlignPointerUp(uint8_t* data, size_t alignment); - -// Returns the previous pointer address aligned to the given alignment. -uint8_t* AlignPointerDown(uint8_t* data, size_t alignment); - -// Returns an increased size that's a multiple of alignment. -size_t AlignSizeUp(size_t size, size_t alignment); - -// Returns size in bytes for a given TfLiteType. -TfLiteStatus TfLiteTypeSizeOf(TfLiteType type, size_t* size); - -// How many bytes are needed to hold a tensor's contents. -TfLiteStatus BytesRequiredForTensor(const tflite::Tensor& flatbuffer_tensor, - size_t* bytes, size_t* type_size, - ErrorReporter* error_reporter); - -// How many bytes are used in a TfLiteEvalTensor instance. The byte length is -// returned in out_bytes. -TfLiteStatus TfLiteEvalTensorByteLength(const TfLiteEvalTensor* eval_tensor, - size_t* out_bytes); - -// Deduce output dimensions from input and allocate given size. -// Useful for operators with two inputs where the largest input should equal the -// output dimension. -TfLiteStatus AllocateOutputDimensionsFromInput(TfLiteContext* context, - const TfLiteTensor* input1, - const TfLiteTensor* input2, - TfLiteTensor* output); - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_MEMORY_HELPERS_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/memory_planner/greedy_memory_planner.cc b/code/components/tflite-lib/tensorflow/lite/micro/memory_planner/greedy_memory_planner.cc deleted file mode 100644 index bdfc8304..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/memory_planner/greedy_memory_planner.cc +++ /dev/null @@ -1,452 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/memory_planner/greedy_memory_planner.h" - -#include "tensorflow/lite/micro/micro_error_reporter.h" -#include "tensorflow/lite/micro/micro_string.h" - -namespace tflite { - -namespace { - -// Returns a character representing a numbered buffer -// for GreedyMemoryPlanner::PrintMemoryPlan() -char GetOrdinalCharacter(int i) { - if (i < 10) { - return '0' + i; - } else if (i < 36) { - return 'a' + (i - 10); - } else if (i < 62) { - return 'A' + (i - 36); - } - return '*'; -} - -} // namespace - -// Simple stable in-place sort function. Not time-efficient for large arrays. -// Would normally be in an anonymous namespace to keep it private, but we want -// to be able to test it externally. -void ReverseSortInPlace(int* values, int* ids, int size) { - bool any_swapped; - do { - any_swapped = false; - for (int i = 1; i < size; ++i) { - if (values[i - 1] < values[i]) { - const int value_temp = values[i - 1]; - values[i - 1] = values[i]; - values[i] = value_temp; - const int id_temp = ids[i - 1]; - ids[i - 1] = ids[i]; - ids[i] = id_temp; - any_swapped = true; - } - } - } while (any_swapped); -} - -GreedyMemoryPlanner::GreedyMemoryPlanner() {} - -TfLiteStatus GreedyMemoryPlanner::Init(unsigned char* scratch_buffer, - int scratch_buffer_size) { - // Reset internal states - buffer_count_ = 0; - need_to_calculate_offsets_ = true; - - // Allocate the arrays we need within the scratch buffer arena. - max_buffer_count_ = scratch_buffer_size / per_buffer_size(); - - unsigned char* next_free = scratch_buffer; - requirements_ = reinterpret_cast(next_free); - next_free += sizeof(BufferRequirements) * max_buffer_count_; - - buffer_sizes_sorted_ = reinterpret_cast(next_free); - next_free += sizeof(int) * max_buffer_count_; - - buffer_ids_sorted_ = reinterpret_cast(next_free); - next_free += sizeof(int) * max_buffer_count_; - - buffers_sorted_by_offset_ = reinterpret_cast(next_free); - next_free += sizeof(ListEntry) * max_buffer_count_; - - buffer_offsets_ = reinterpret_cast(next_free); - return kTfLiteOk; -} - -GreedyMemoryPlanner::~GreedyMemoryPlanner() { - // We don't own the scratch buffer, so don't deallocate anything. -} - -TfLiteStatus GreedyMemoryPlanner::AddBuffer( - tflite::ErrorReporter* error_reporter, int size, int first_time_used, - int last_time_used) { - if (buffer_count_ >= max_buffer_count_) { - TF_LITE_REPORT_ERROR(error_reporter, "Too many buffers (max is %d)", - max_buffer_count_); - return kTfLiteError; - } - BufferRequirements* current = &requirements_[buffer_count_]; - current->size = size; - current->first_time_used = first_time_used; - current->last_time_used = last_time_used; - current->offline_offset = kOnlinePlannedBuffer; - ++buffer_count_; - need_to_calculate_offsets_ = true; - return kTfLiteOk; -} - -TfLiteStatus GreedyMemoryPlanner::AddBuffer( - tflite::ErrorReporter* error_reporter, int size, int first_time_used, - int last_time_used, int offline_offset) { - BufferRequirements* current = &requirements_[buffer_count_]; - if (AddBuffer(error_reporter, size, first_time_used, last_time_used) != - kTfLiteOk) { - return kTfLiteError; - } - current->offline_offset = offline_offset; - return kTfLiteOk; -} - -bool GreedyMemoryPlanner::DoesEntryOverlapInTime( - const GreedyMemoryPlanner::ListEntry* entry, const int first_time_used, - const int last_time_used) const { - const BufferRequirements* entry_requirements = - &requirements_[entry->requirements_index]; - if (entry_requirements->first_time_used > last_time_used) { - return false; - } - if (first_time_used > entry_requirements->last_time_used) { - return false; - } - return true; -} - -GreedyMemoryPlanner::ListEntry* -GreedyMemoryPlanner::NextSimultaneouslyActiveBuffer( - const GreedyMemoryPlanner::ListEntry* start, const int first_time_used, - const int last_time_used) { - ListEntry* result = nullptr; - ListEntry* candidate_next_entry; - if (start == nullptr) { - candidate_next_entry = &buffers_sorted_by_offset_[first_entry_index_]; - } else { - if (start->next_entry_index == -1) { - return nullptr; - } - candidate_next_entry = &buffers_sorted_by_offset_[start->next_entry_index]; - } - do { - if (DoesEntryOverlapInTime(candidate_next_entry, first_time_used, - last_time_used)) { - result = candidate_next_entry; - break; - } - if (candidate_next_entry->next_entry_index == -1) { - break; - } - candidate_next_entry = - &buffers_sorted_by_offset_[candidate_next_entry->next_entry_index]; - } while (true); - return result; -} - -void GreedyMemoryPlanner::CalculateOffsetsIfNeeded() { - if (!need_to_calculate_offsets_ || (buffer_count_ == 0)) { - return; - } - need_to_calculate_offsets_ = false; - - // Start off by ordering the buffers in descending order of size. - // This helps find a more compact layout. Intuitively, you can think - // about putting the large buffers in place first, and then the - // smaller buffers can fit in the gaps, rather than fragmenting the - // gaps with small buffers at the beginning. Add offline planned offsets - // first in the list, since they have a predetermined offset. - int idx_from_tail = buffer_count_; - int idx_from_head = 0; - for (int i = 0; i < buffer_count_; ++i) { - if (requirements_[i].offline_offset == kOnlinePlannedBuffer) { - idx_from_tail--; - buffer_sizes_sorted_[idx_from_tail] = requirements_[i].size; - buffer_ids_sorted_[idx_from_tail] = i; - buffer_offsets_[i] = -1; - } else { - buffer_sizes_sorted_[idx_from_head] = requirements_[i].size; - buffer_ids_sorted_[idx_from_head] = i; - buffer_offsets_[i] = requirements_[i].offline_offset; - idx_from_head++; - } - } - - // This sorting algorithm is naive, and may end up taking a very long time - // with hundreds of buffers. Do not sort the offline planned offsets. - ReverseSortInPlace(&buffer_sizes_sorted_[idx_from_head], - &buffer_ids_sorted_[idx_from_head], - buffer_count_ - idx_from_head); - - // Initialize the first entry to the first buffer in - // buffer_ids_sorted_. - // - If there are no offline planned offsets, the largest buffer will be - // first, and the buffers will be handled in size order. - // - If offline offsets are present, these will be handled first in order - // for the greedy algorithm to utilized gaps in the offline plan. - first_entry_index_ = 0; - next_free_entry_ = 1; - ListEntry* first_entry = &buffers_sorted_by_offset_[first_entry_index_]; - first_entry->next_entry_index = -1; // to mark the entry as end of list - int buffer_id = buffer_ids_sorted_[0]; - first_entry->requirements_index = buffer_id; - if (requirements_[buffer_id].offline_offset == kOnlinePlannedBuffer) { - buffer_offsets_[buffer_id] = 0; - } - first_entry->offset = buffer_offsets_[buffer_id]; - - // Work through the rest of the buffers to find a good gap to place each one. - for (int i = 1; i < buffer_count_; ++i) { - // The id is the order the buffer was originally added by the client. - buffer_id = buffer_ids_sorted_[i]; - // Look at what size and time range the buffer needs to be active. - BufferRequirements* wanted_requirements = &requirements_[buffer_id]; - const int wanted_size = wanted_requirements->size; - const int wanted_first_time_used = wanted_requirements->first_time_used; - const int wanted_last_time_used = wanted_requirements->last_time_used; - - // Find the first buffer that's active in our time range. All placed - // buffers are stored in the order of their starting position in the arena - // so that it's easy to find the next buffer in memory, and so the gap. - // The candidate_entry variable holds the buffer that we're considering - // placing the current buffer after. - - int candidate_offset = 0; - // Loop through the offset-ordered list of buffers, looking for gaps. - if (wanted_requirements->offline_offset == kOnlinePlannedBuffer) { - ListEntry* prior_entry = nullptr; - while (true) { - // Find out what the next active buffer is. - ListEntry* next_entry = NextSimultaneouslyActiveBuffer( - prior_entry, wanted_first_time_used, wanted_last_time_used); - - if (prior_entry) { - BufferRequirements* candidate_requirements = - &requirements_[prior_entry->requirements_index]; - const int prior_entry_offset = - prior_entry->offset + candidate_requirements->size; - if (prior_entry_offset > candidate_offset) { - candidate_offset = prior_entry_offset; - } - } - if (next_entry == nullptr) { - // We're at the end of the list, so we can always append the buffer - // here. - break; - } - // Find out how much space there is between us and the next buffer. - const int gap = next_entry->offset - candidate_offset; - if (gap >= wanted_size) { - // This entry has a big enough gap between it and the next, so - // use it! - break; - } - // The gap wasn't big enough, so move on to another candidate. - prior_entry = next_entry; - } - } else { - // Offline planned offset are to be considered constant - candidate_offset = wanted_requirements->offline_offset; - } - // At this point, we've either found a gap (possibly at the end of the - // list) and want to place the buffer there, or there are no other active - // buffers in this time range and so we can put it at offset zero. - // Record the buffer's offset in our plan. - buffer_offsets_[buffer_id] = candidate_offset; - // Add the newly-placed buffer to our offset-ordered list, so that - // subsequent passes can fit in their buffers around it. - ListEntry* new_entry = &buffers_sorted_by_offset_[next_free_entry_]; - new_entry->offset = candidate_offset; - new_entry->requirements_index = buffer_id; - const int new_entry_index = next_free_entry_; - ++next_free_entry_; - - if (first_entry->offset > candidate_offset) { - // The new entry offset is smaller than the first entry offset => - // replace the first entry - first_entry = new_entry; - first_entry->next_entry_index = first_entry_index_; - first_entry_index_ = new_entry_index; - } else { - ListEntry* current_entry = first_entry; - // Make sure that we insert the buffer at the correct place in the - // buffer-offset-ordered list - while (true) { - const int next_entry_index = current_entry->next_entry_index; - if (next_entry_index == -1) { - // We're at the end of the list, so just add the new entry here. - current_entry->next_entry_index = new_entry_index; - new_entry->next_entry_index = -1; - break; - } - // not at the end of the list -> take a look at next entry - ListEntry* next_entry = &buffers_sorted_by_offset_[next_entry_index]; - if (next_entry->offset > candidate_offset) { - // We're at the right spot to do an insertion and retain the sorting - // order, so place the new entry here. - new_entry->next_entry_index = current_entry->next_entry_index; - current_entry->next_entry_index = new_entry_index; - break; - } - current_entry = next_entry; - } - } - } -} - -size_t GreedyMemoryPlanner::GetMaximumMemorySize() { - CalculateOffsetsIfNeeded(); - if (buffer_count_ == 0) { - return 0; - } - ListEntry* entry = &buffers_sorted_by_offset_[first_entry_index_]; - size_t max_size = 0; - while (entry) { - BufferRequirements* requirements = - &requirements_[entry->requirements_index]; - const size_t current_size = entry->offset + requirements->size; - if (current_size > max_size) { - max_size = current_size; - } - if (entry->next_entry_index == -1) { - break; - } - entry = &buffers_sorted_by_offset_[entry->next_entry_index]; - } - return max_size; -} - -void GreedyMemoryPlanner::PrintMemoryPlan() { - CalculateOffsetsIfNeeded(); - - for (int i = 0; i < buffer_count_; ++i) { - MicroPrintf("%c (id=%d): size=%d, offset=%d, first_used=%d last_used=%d", - GetOrdinalCharacter(i), i, requirements_[i].size, - buffer_offsets_[i], requirements_[i].first_time_used, - requirements_[i].last_time_used); - } - - constexpr int kLineWidth = 80; - int max_size = kLineWidth; - int max_time = 0; - for (int i = 0; i < buffer_count_; ++i) { - BufferRequirements* requirements = &requirements_[i]; - const int offset = buffer_offsets_[i]; - const int last_time_used = requirements->last_time_used; - const int size = offset + requirements->size; - if (size > max_size) { - max_size = size; - } - if (last_time_used > max_time) { - max_time = last_time_used; - } - } - - char line[kLineWidth + 1]; - for (int t = 0; t <= max_time; ++t) { - for (int c = 0; c < kLineWidth; ++c) { - line[c] = '.'; - } - int memory_use = 0; - for (int i = 0; i < buffer_count_; ++i) { - BufferRequirements* requirements = &requirements_[i]; - if ((t < requirements->first_time_used) || - (t > requirements->last_time_used)) { - continue; - } - const int offset = buffer_offsets_[i]; - if (offset == -1) { - continue; - } - const int size = requirements->size; - memory_use += size; - const int line_start = (offset * kLineWidth) / max_size; - const int line_end = ((offset + size) * kLineWidth) / max_size; - for (int n = line_start; n < line_end; ++n) { - if (line[n] == '.') { - line[n] = GetOrdinalCharacter(i); - } else { - line[n] = '!'; - } - } - } - line[kLineWidth] = 0; - - MicroPrintf("%s%d: %s (%dk)", t < 10 ? " " : "", t, (const char*)line, - (memory_use + 1023) / 1024); - } -} - -int GreedyMemoryPlanner::GetBufferCount() { return buffer_count_; } - -TfLiteStatus GreedyMemoryPlanner::GetOffsetForBuffer( - tflite::ErrorReporter* error_reporter, int buffer_index, int* offset) { - CalculateOffsetsIfNeeded(); - if ((buffer_index < 0) || (buffer_index >= buffer_count_)) { - TF_LITE_REPORT_ERROR(error_reporter, - "buffer index %d is outside range 0 to %d", - buffer_index, buffer_count_); - return kTfLiteError; - } - *offset = buffer_offsets_[buffer_index]; - return kTfLiteOk; -} - -bool GreedyMemoryPlanner::DoAnyBuffersOverlap(ErrorReporter* error_reporter) { - CalculateOffsetsIfNeeded(); - bool were_overlaps_found = false; - for (int i = 0; i < buffer_count_; ++i) { - BufferRequirements* a_requirements = &requirements_[i]; - const int a_start_offset = buffer_offsets_[i]; - const int a_first_time_used = a_requirements->first_time_used; - const int a_last_time_used = a_requirements->last_time_used; - const int a_end_offset = a_start_offset + a_requirements->size; - for (int j = 0; j < buffer_count_; ++j) { - if (i == j) { - continue; - } - BufferRequirements* b_requirements = &requirements_[j]; - const int b_start_offset = buffer_offsets_[j]; - const int b_first_time_used = b_requirements->first_time_used; - const int b_last_time_used = b_requirements->last_time_used; - const int b_end_offset = b_start_offset + b_requirements->size; - if ((a_first_time_used > b_last_time_used) || - (b_first_time_used > a_last_time_used)) { - // Buffers don't overlap in time. - continue; - } - if ((a_start_offset >= b_end_offset) || - (b_start_offset >= a_end_offset)) { - // No overlap in memory. - continue; - } - were_overlaps_found = true; - TF_LITE_REPORT_ERROR( - error_reporter, "Overlap: %d (%d=>%d, %d->%d) vs %d (%d=>%d, %d->%d)", - i, a_first_time_used, a_last_time_used, a_start_offset, a_end_offset, - j, b_first_time_used, b_last_time_used, b_start_offset, b_end_offset); - } - } - return were_overlaps_found; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/memory_planner/greedy_memory_planner.h b/code/components/tflite-lib/tensorflow/lite/micro/memory_planner/greedy_memory_planner.h deleted file mode 100644 index a34f3c59..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/memory_planner/greedy_memory_planner.h +++ /dev/null @@ -1,167 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_MEMORY_PLANNER_GREEDY_MEMORY_PLANNER_H_ -#define TENSORFLOW_LITE_MICRO_MEMORY_PLANNER_GREEDY_MEMORY_PLANNER_H_ - -#include "tensorflow/lite/micro/compatibility.h" -#include "tensorflow/lite/micro/memory_planner/micro_memory_planner.h" - -namespace tflite { - -constexpr int kOnlinePlannedBuffer = -1; - -// A memory planner that uses a greedy algorithm to arrange buffers in memory -// to minimize the overall arena size needed. -// -// The algorithm works like this: -// - The client enters the buffer information through AddBuffer(). -// - When a function like GetOffsetForBuffer() is called, the -// CalculateOffsetsIfNeeded() method is invoked. -// - If an up to date plan is not already present, one will be calculated. -// - The buffers are sorted in descending order of size. -// - The largest buffer is placed at offset zero. -// - The rest of the buffers are looped through in descending size order. -// - The other buffers that need to be in memory at the same time are found. -// - The first gap between simultaneously active buffers that the current -// buffer fits into will be used. -// - If no large-enough gap is found, the current buffer is placed after the -// last buffer that's simultaneously active. -// - This continues until all buffers are placed, and the offsets stored. -// -// This is not guaranteed to produce the best placement, since that's an -// NP-Complete problem, but in practice it should produce one that's decent. -class GreedyMemoryPlanner : public MicroMemoryPlanner { - public: - GreedyMemoryPlanner(); - ~GreedyMemoryPlanner() override; - - // You need to pass in an area of memory to be used for planning. The client - // should ensure the validity of the memory when it needs to use this object. - // This memory isn't owned by this object, so management should be handled by - // the client. This is so it can be stack or globally allocated if necessary - // on devices without dynamic memory allocation. How many buffers can be - // planned for will depend on the size of this scratch memory, so you should - // enlarge it if you see an error when calling AddBuffer(). The memory can be - // reused once you're done with the planner, as long as you copy the - // calculated offsets to another location. Each buffer requires about 36 bytes - // of scratch. - TfLiteStatus Init(unsigned char* scratch_buffer, - int scratch_buffer_size) override; - - // Record details of a buffer we want to place. - TfLiteStatus AddBuffer(ErrorReporter* error_reporter, int size, - int first_time_used, int last_time_used) override; - - // Record details of an offline planned buffer offset we want to place. - // offline_offset is the buffer offset from the start of the arena. - TfLiteStatus AddBuffer(ErrorReporter* error_reporter, int size, - int first_time_used, int last_time_used, - int offline_offset) override; - - // Returns the high-water mark of used memory. This is the minimum size of a - // memory arena you'd need to allocate to hold these buffers. - size_t GetMaximumMemorySize() override; - - // How many buffers have been recorded. - int GetBufferCount() override; - - // Where a given buffer should be placed in the memory arena. - // This information is stored in the memory arena itself, so once the arena - // is used for inference, it will be overwritten. - TfLiteStatus GetOffsetForBuffer(ErrorReporter* error_reporter, - int buffer_index, int* offset) override; - - // Prints an ascii-art diagram of the buffer layout plan. - void PrintMemoryPlan() override; - - // Debug method to check whether any buffer allocations are overlapping. This - // is an O(N^2) complexity operation, so only use for testing. - bool DoAnyBuffersOverlap(ErrorReporter* error_reporter); - - // Used to store a list of buffers ordered by their offset. - struct ListEntry { - int offset; - int requirements_index; - int next_entry_index; - }; - - // Number of bytes required in order to plan a buffer. - static size_t per_buffer_size() { - const int per_buffer_size = - sizeof(BufferRequirements) + // requirements_ - sizeof(int) + // buffer_sizes_sorted_ - sizeof(int) + // buffer_ids_sorted_ - sizeof(ListEntry) + // buffers_sorted_by_offset_ - sizeof(int); // buffer_offsets_; - return per_buffer_size; - } - - private: - // Whether a buffer is active in a given time range. - bool DoesEntryOverlapInTime(const ListEntry* entry, const int first_time_used, - const int last_time_used) const; - - // Walks the list to return the next buffer that is active in a given time - // range, or a null pointer if there are none. - ListEntry* NextSimultaneouslyActiveBuffer(const ListEntry* start, - const int first_time_used, - const int last_time_used); - - // If there isn't an up to date plan, calculate a new one. - void CalculateOffsetsIfNeeded(); - - // How many buffers we can plan for, based on the arena size we're given in - // the constructor. - int max_buffer_count_; - - // The number of buffers added so far. - int buffer_count_; - - // Records the client-provided information about each buffer. - struct BufferRequirements { - int size; - int offline_offset; - int first_time_used; - int last_time_used; - }; - - // Working arrays used during the layout algorithm. - BufferRequirements* requirements_; - // buffer_sizes_sorted_ and buffer_ids_sorted_ are sorted according to: - // { - // offline planned buffers, - // online planned buffers sorted by size - // } - int* buffer_sizes_sorted_; - int* buffer_ids_sorted_; - ListEntry* buffers_sorted_by_offset_; - int next_free_entry_; // Index of the next free entry of - // buffers_sorted_by_offset_ - int first_entry_index_; // Index of the first entry (smallest offset) of - // buffers_sorted_by_offset_ - - // Stores the outcome of the plan, the location of each buffer in the arena. - int* buffer_offsets_; - - // Whether buffers have been added since the last plan was calculated. - bool need_to_calculate_offsets_; - - TF_LITE_REMOVE_VIRTUAL_DELETE -}; - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_MEMORY_PLANNER_GREEDY_MEMORY_PLANNER_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/memory_planner/linear_memory_planner.cc b/code/components/tflite-lib/tensorflow/lite/micro/memory_planner/linear_memory_planner.cc deleted file mode 100644 index d25a4f22..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/memory_planner/linear_memory_planner.cc +++ /dev/null @@ -1,54 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/memory_planner/linear_memory_planner.h" - -namespace tflite { - -LinearMemoryPlanner::LinearMemoryPlanner() - : current_buffer_count_(0), next_free_offset_(0) {} -LinearMemoryPlanner::~LinearMemoryPlanner() {} - -TfLiteStatus LinearMemoryPlanner::AddBuffer( - tflite::ErrorReporter* error_reporter, int size, int first_time_used, - int last_time_used) { - if (current_buffer_count_ >= kMaxBufferCount) { - TF_LITE_REPORT_ERROR(error_reporter, "Too many buffers (max is %d)", - kMaxBufferCount); - return kTfLiteError; - } - buffer_offsets_[current_buffer_count_] = next_free_offset_; - next_free_offset_ += size; - ++current_buffer_count_; - return kTfLiteOk; -} - -size_t LinearMemoryPlanner::GetMaximumMemorySize() { return next_free_offset_; } - -int LinearMemoryPlanner::GetBufferCount() { return current_buffer_count_; } - -TfLiteStatus LinearMemoryPlanner::GetOffsetForBuffer( - tflite::ErrorReporter* error_reporter, int buffer_index, int* offset) { - if ((buffer_index < 0) || (buffer_index >= current_buffer_count_)) { - TF_LITE_REPORT_ERROR(error_reporter, - "buffer index %d is outside range 0 to %d", - buffer_index, current_buffer_count_); - return kTfLiteError; - } - *offset = buffer_offsets_[buffer_index]; - return kTfLiteOk; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/memory_planner/linear_memory_planner.h b/code/components/tflite-lib/tensorflow/lite/micro/memory_planner/linear_memory_planner.h deleted file mode 100644 index 128ef808..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/memory_planner/linear_memory_planner.h +++ /dev/null @@ -1,50 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_MEMORY_PLANNER_LINEAR_MEMORY_PLANNER_H_ -#define TENSORFLOW_LITE_MICRO_MEMORY_PLANNER_LINEAR_MEMORY_PLANNER_H_ - -#include "tensorflow/lite/micro/compatibility.h" -#include "tensorflow/lite/micro/memory_planner/micro_memory_planner.h" - -namespace tflite { - -// The simplest possible memory planner that just lays out all buffers at -// increasing offsets without trying to reuse memory. -class LinearMemoryPlanner : public MicroMemoryPlanner { - public: - LinearMemoryPlanner(); - ~LinearMemoryPlanner() override; - - TfLiteStatus AddBuffer(tflite::ErrorReporter* error_reporter, int size, - int first_time_used, int last_time_used) override; - - size_t GetMaximumMemorySize() override; - int GetBufferCount() override; - TfLiteStatus GetOffsetForBuffer(tflite::ErrorReporter* error_reporter, - int buffer_index, int* offset) override; - - private: - static constexpr int kMaxBufferCount = 1024; - size_t buffer_offsets_[kMaxBufferCount]; - int current_buffer_count_; - size_t next_free_offset_; - - TF_LITE_REMOVE_VIRTUAL_DELETE -}; - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_MEMORY_PLANNER_LINEAR_MEMORY_PLANNER_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/memory_planner/memory_plan_struct.h b/code/components/tflite-lib/tensorflow/lite/micro/memory_planner/memory_plan_struct.h deleted file mode 100644 index c8c431cc..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/memory_planner/memory_plan_struct.h +++ /dev/null @@ -1,73 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_MEMORY_PLANNER_MEMORY_PLAN_STRUCT_H_ -#define TENSORFLOW_LITE_MICRO_MEMORY_PLANNER_MEMORY_PLAN_STRUCT_H_ - -#include -#include - -#include "tensorflow/lite/micro/micro_utils.h" - -namespace tflite { - -// This is an experimental feature and subjected to change. -// More description is available at -// tensorflow/lite/micro/docs/offline_memory_plan.md. - -// Describes a buffer's layout inside an arena. This struct should be kept as -// small as possible for memory footprint sensitive applications and should use -// only primitive fields, making it easy to adjust offline. -struct BufferDescriptor { - // Starting offset inside an arena for this buffer. - // Offset is the minimum information needed for the buffer. The user knows - // the model and the size of each buffer in order to lay out a valid buffer - // plan. - int32_t offset; -}; - -// A structure describing the lay out of buffers inside an arena. -struct BufferPlan { - // Number of buffers described in this plan. - int32_t buffer_count; - - // Each element describes one buffer. - // Buffer index is implicit by the order of AddBuffer() call. - // Specifically, indices of activation tensors are 0 … N-1 where N is the - // number of activation tensors. - // The rest are based on the order of OP requests. - // - // This is a flexible array member and should ideally be - // arena_entries[]; However, in order to support a variety - // of compilers (and without needing to add ifdef's), we - // are implementing the flexible array member with an array of - // length 1 as the last member of the struct. When the size of a BufferPlan - // is needed, use the provided SizeOfBufferPlan(buffer_count) that - // accounts for this implemenatation caveat. - BufferDescriptor buffer_plan_entries[1]; -}; - -// Returns size of a BufferPlan given a buffer count. This size is compile time -// known if buffer_count is a compile time constant. -constexpr size_t SizeOfBufferPlan(int32_t buffer_count) { - // Minus 1 because a BufferPlan struct have a BufferDescriptor already. - // Max to provide a lower bound for the corner case of buffer_count = 0. - return sizeof(BufferPlan) + - sizeof(BufferDescriptor) * Max(buffer_count - 1, 0); -} - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_MEMORY_PLANNER_MEMORY_PLAN_STRUCT_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/memory_planner/micro_memory_planner.h b/code/components/tflite-lib/tensorflow/lite/micro/memory_planner/micro_memory_planner.h deleted file mode 100644 index cec56335..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/memory_planner/micro_memory_planner.h +++ /dev/null @@ -1,95 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_MICRO_MEMORY_PLANNER_MEMORY_PLANNER_H_ -#define TENSORFLOW_LITE_MICRO_MICRO_MEMORY_PLANNER_MEMORY_PLANNER_H_ - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/core/api/error_reporter.h" - -namespace tflite { - -// Interface class for planning the layout of memory buffers during the -// execution of a graph. -// It's designed to be used by a client that iterates in any order through the -// buffers it wants to lay out, and then calls the getter functions for -// information about the calculated layout. For example: -// -// SomeMemoryPlanner planner; -// planner.AddBuffer(reporter, 100, 0, 1); // Buffer 0 -// planner.AddBuffer(reporter, 50, 2, 3); // Buffer 1 -// planner.AddBuffer(reporter, 50, 2, 3); // Buffer 2 -// -// int offset0; -// TF_EXPECT_OK(planner.GetOffsetForBuffer(reporter, 0, &offset0)); -// int offset1; -// TF_EXPECT_OK(planner.GetOffsetForBuffer(reporter, 1, &offset1)); -// int offset2; -// TF_EXPECT_OK(planner.GetOffsetForBuffer(reporter, 2, &offset2)); -// const int arena_size_needed = planner.GetMaximumMemorySize(); -// -// The goal is for applications to be able to experiment with different layout -// strategies without changing their client code, by swapping out classes that -// implement this interface.= -class MicroMemoryPlanner { - public: - MicroMemoryPlanner() {} - virtual ~MicroMemoryPlanner() {} - - // Pass information about a buffer's size and lifetime to the layout - // algorithm. The order this is called implicitly assigns an index to the - // result, so the buffer information that's passed into the N-th call of - // this method will be used as the buffer_index argument to - // GetOffsetForBuffer(). - virtual TfLiteStatus AddBuffer(tflite::ErrorReporter* error_reporter, - int size, int first_time_used, - int last_time_used) = 0; - - // Record details of an offline planned buffer offset we want to place. - // offline_offset is the buffer offset from the start of the arena. - // This is to support offline memory planning from the flatbuffer metadata. - // By default, it returns an error. - virtual TfLiteStatus AddBuffer(ErrorReporter* error_reporter, int size, - int first_time_used, int last_time_used, - int offline_offset) { - return kTfLiteError; - } - - // The largest contiguous block of memory that's needed to hold the layout. - virtual size_t GetMaximumMemorySize() = 0; - // How many buffers have been added to the planner. - virtual int GetBufferCount() = 0; - // Calculated layout offset for the N-th buffer added to the planner. - virtual TfLiteStatus GetOffsetForBuffer(tflite::ErrorReporter* error_reporter, - int buffer_index, int* offset) = 0; - - // Provides the scratch buffer in case that the memory planner needs it. - // The lifetime of scratch buffers lifetime lasts until the static memory plan - // is committed. - // The default implementation is for the memory planner that does not need - // scratch buffer and simply returns ok. - virtual TfLiteStatus Init(unsigned char* scratch_buffer, - int scratch_buffer_size) { - return kTfLiteOk; - } - - virtual void PrintMemoryPlan() { - // Default does nothing. - } -}; - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_MICRO_MEMORY_PLANNER_MEMORY_PLANNER_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/memory_planner/non_persistent_buffer_planner_shim.cc b/code/components/tflite-lib/tensorflow/lite/micro/memory_planner/non_persistent_buffer_planner_shim.cc deleted file mode 100644 index 700627a5..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/memory_planner/non_persistent_buffer_planner_shim.cc +++ /dev/null @@ -1,66 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/memory_planner/non_persistent_buffer_planner_shim.h" - -#include "tensorflow/lite/micro/micro_error_reporter.h" - -namespace tflite { - -NonPersistentMemoryPlannerShim::NonPersistentMemoryPlannerShim( - const BufferPlan* buffer_plan) - : buffer_plan_(buffer_plan), buffer_request_count_(0) {} - -NonPersistentMemoryPlannerShim::~NonPersistentMemoryPlannerShim() {} - -TfLiteStatus NonPersistentMemoryPlannerShim::AddBuffer( - tflite::ErrorReporter* error_reporter, int size, int first_time_used, - int last_time_used) { - buffer_request_count_++; - if (buffer_request_count_ > buffer_plan_->buffer_count) { - MicroPrintf( - "Attempting to add buffer %d, but only %d buffers in given buffer " - "plan.", - buffer_request_count_, buffer_plan_->buffer_count); - return kTfLiteError; - } - return kTfLiteOk; -} - -size_t NonPersistentMemoryPlannerShim::GetMaximumMemorySize() { - // Simply return 0 to let the framework accept this memory plan - // because the client ensure validity of the memory plan. - return 0; -} - -// How many buffers are in the given memory plan. -int NonPersistentMemoryPlannerShim::GetBufferCount() { - return buffer_plan_->buffer_count; -} - -TfLiteStatus NonPersistentMemoryPlannerShim::GetOffsetForBuffer( - ErrorReporter* error_reporter, int buffer_request_index, int* offset) { - if (buffer_request_index >= buffer_plan_->buffer_count) { - MicroPrintf( - "Attempting to get offset for buffer %d, but only %d buffers in given " - "buffer plan.", - buffer_request_index, buffer_plan_->buffer_count); - return kTfLiteError; - } - *offset = buffer_plan_->buffer_plan_entries[buffer_request_index].offset; - return kTfLiteOk; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/memory_planner/non_persistent_buffer_planner_shim.h b/code/components/tflite-lib/tensorflow/lite/micro/memory_planner/non_persistent_buffer_planner_shim.h deleted file mode 100644 index 945ac123..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/memory_planner/non_persistent_buffer_planner_shim.h +++ /dev/null @@ -1,130 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_MEMORY_PLANNER_NON_PERSISTENT_MEMORY_PLANNER_SHIM_H__ -#define TENSORFLOW_LITE_MICRO_MEMORY_PLANNER_NON_PERSISTENT_MEMORY_PLANNER_SHIM_H__ - -#include "tensorflow/lite/micro/compatibility.h" -#include "tensorflow/lite/micro/memory_planner/memory_plan_struct.h" -#include "tensorflow/lite/micro/memory_planner/micro_memory_planner.h" - -namespace tflite { - -/* This is an experimental feature and subjected to change. - * -The NonPersistentMemoryPlannerShim enables TFLM to work with an external tooling -that can plan the offset of each non persistent buffer for the Model within the -TFLM arena. - -If the NonPersistentMemoryPlannerShim is used, then the final binary does not -have any of the symbols associated with the GreedyMemoryPlanner which results in -a reduced memory footprint. - -Additionally, the offline planning of the non-persistent buffers can be used to -have a more efficient utilization compared to the GreedyMemoryPlanner. - -For example, consider the following hypothetical model: - -A1(400) A2(401) -──┬─────────┐ ┌─────────── - │ │ │ - │ │ │ - │ ▼ ▼ - │ ┌────────┐ - │ │ OP1 │ - │ └───┬────┘ A4(201) - │ A3(10) │ │ - │ │ │ - │ │ │ - │ ┌───┴────┐ │ - │ │ OP2 │◄────────┤ - │ └───┬────┘ │ - │ A5(11) │ A6(202) │ - │ │ │ │ - │ ▼ │ │ - │ ┌────────┐ │ │ - │ │ OP3 │◄─┘ │ - │ └───┬────┘ │ - │ │ A8(200) │ - │ A7(12) │ │ │ - │ │ │ │ - │ ┌───┴────┐◄──┘ │ - └──────►│ OP4 │ │ - └───┬────┘◄────────┘ - │ - A9(13) │ - ▼ - -The GreedyMemoryPlanner will give the following memory layout that requires 1012 -bytes of scratch arena size: - -┌─────────────────────────────────────────┬──────────────────────────┬────────┬───────┐ -│ A2(401) │ A1(400) │ A4(201)│ -A3(10)│ -└─────────────────────────────────────────┴──────────────────────────┴────────┴───────┘ - -┌───────────┬──────┬──────┐ -│ A6(202) │A5(11)│A7(12)│ -└───────────┴──────┴──────┘ - -┌──────────┬───────┐ -│ A8(200) │A9(13) │ -└──────────┴───────┘ - -But a more efficient offline memory plan that requires only 826 bytes of scratch -arena size can be - -┌──────────────────────────────────────┬─────────────────────────────┬───────┬──────┐ -│ A1(400) │ A2(401) │ -A3(10)│A5(11)│ -└──────────────────────────────────────┴─────────────────────────────┴───────┴──────┘ - - ┌────────────────┬────────────┬────────┬───────┐ - │A4(201) │ A8(200) │A9(13) -│A7(12) │ └────────────────┴────────────┴────────┴───────┘ - - ┌─────────────┐ - │ A6(202) │ - └─────────────┘ - -*/ -class NonPersistentMemoryPlannerShim : public MicroMemoryPlanner { - public: - // Does not take ownership of buffer_plan, which must refer to a valid - // BufferPlan that outlives this object. - explicit NonPersistentMemoryPlannerShim(const BufferPlan* buffer_plan); - ~NonPersistentMemoryPlannerShim() override; - - TfLiteStatus GetOffsetForBuffer(ErrorReporter* error_reporter, - int buffer_request_index, - int* offset) override; - - TfLiteStatus AddBuffer(tflite::ErrorReporter* error_reporter, int size, - int first_time_used, int last_time_used) override; - size_t GetMaximumMemorySize() override; - int GetBufferCount() override; - - private: - const BufferPlan* buffer_plan_; // not owned, can't be null - - // The number of buffers requested so far. Used for error checking. - int buffer_request_count_; - - TF_LITE_REMOVE_VIRTUAL_DELETE -}; - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_MEMORY_PLANNER_NON_PERSISTENT_MEMORY_PLANNER_SHIM_H__ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/micro_allocation_info.cc b/code/components/tflite-lib/tensorflow/lite/micro/micro_allocation_info.cc deleted file mode 100644 index edab2b83..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/micro_allocation_info.cc +++ /dev/null @@ -1,351 +0,0 @@ -/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/micro/micro_allocation_info.h" - -#include "tensorflow/lite/c/c_api_types.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/memory_helpers.h" -#include "tensorflow/lite/micro/memory_planner/greedy_memory_planner.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" - -namespace tflite { - -namespace { -constexpr char kOfflineMemAllocMetadata[] = "OfflineMemoryAllocation"; -constexpr int kUninitializedLifetime = -1; -} // namespace - -// Mark the given Allocation info as first created at the specified allocation -// scope count. Only the first creation must be recorded since the allocation -// scope count monotonically increases throughout the lifetime marking process. -void AllocationInfoBuilder::UpdateFirstCreated(AllocationInfo* current, - int allocation_scope_count) { - TFLITE_DCHECK(current->first_created <= allocation_scope_count); - if (current->first_created == kUninitializedLifetime) { - current->first_created = allocation_scope_count; - } -} - -// Mark the given AllocationInfo as last used at the specified allocation scope -// count. Update the last used marker every time, since the allocation scope -// count monotonically increases through the lifetime marking process. -void AllocationInfoBuilder::UpdateLastUsed(AllocationInfo* current, - int allocation_scope_count) { - TFLITE_DCHECK(current->last_used <= allocation_scope_count); - current->last_used = allocation_scope_count; -} - -TfLiteStatus AllocationInfoBuilder::MarkSubgraphLifetimesIfNecessary( - const Operator* op, internal::ScratchBufferRequest* scratch_buffer_requests, - ScratchBufferHandle* scratch_buffer_handles, - SubgraphAllocations* allocations) { - int first_subgraph_index = -1; - int second_subgraph_index = -1; - const OperatorCode* opcode = - model_->operator_codes()->Get(op->opcode_index()); - switch (opcode->builtin_code()) { - case BuiltinOperator_IF: { - first_subgraph_index = - op->builtin_options_as_IfOptions()->then_subgraph_index(); - second_subgraph_index = - op->builtin_options_as_IfOptions()->else_subgraph_index(); - break; - } - case BuiltinOperator_CALL_ONCE: { - first_subgraph_index = - op->builtin_options_as_CallOnceOptions()->init_subgraph_index(); - break; - } - case BuiltinOperator_WHILE: { - first_subgraph_index = - op->builtin_options_as_WhileOptions()->cond_subgraph_index(); - second_subgraph_index = - op->builtin_options_as_WhileOptions()->body_subgraph_index(); - break; - } - default: { - break; - } - } - if (first_subgraph_index != -1) { - // Enter a new allocation scope for each subgraph. - allocation_scope_count_++; - TF_LITE_ENSURE_STATUS( - MarkAllocationLifetimes(first_subgraph_index, scratch_buffer_requests, - scratch_buffer_handles, allocations)); - } - if (second_subgraph_index != -1) { - // Enter a new allocation scope for each subgraph. - allocation_scope_count_++; - TF_LITE_ENSURE_STATUS( - MarkAllocationLifetimes(second_subgraph_index, scratch_buffer_requests, - scratch_buffer_handles, allocations)); - } - return kTfLiteOk; -} - -TfLiteStatus AllocationInfoBuilder::CreateAllocationInfo( - int scratch_buffer_request_count) { - size_t subgraph_offsets_length = model_->subgraphs()->size() * sizeof(size_t); - info_.subgraph_offsets = - reinterpret_cast(non_persistent_allocator_->AllocateTemp( - subgraph_offsets_length, alignof(size_t))); - if (info_.subgraph_offsets == nullptr) { - TF_LITE_REPORT_ERROR( - reporter_, - "Failed to allocate memory for memory planning, %d bytes required", - subgraph_offsets_length); - return kTfLiteError; - } - size_t tensor_count = 0; - for (size_t subgraph_idx = 0; subgraph_idx < model_->subgraphs()->size(); - subgraph_idx++) { - // Add all tensors in each subgraph to the AllocationInfo array. Even weight - // tensors are added but marked with needs_allocating = false. Including all - // tensors in the graph here simplifies logic. - info_.subgraph_offsets[subgraph_idx] = tensor_count; - tensor_count += model_->subgraphs()->Get(subgraph_idx)->tensors()->size(); - } - info_.tensor_count = tensor_count; - - // Scratch buffer allocations follow tensor allocations, so the scratch offset - // is equal to the number of tensor allocations. - info_.scratch_offset = tensor_count; - info_.allocation_info_count = tensor_count + scratch_buffer_request_count; - info_.scratch_buffer_count = scratch_buffer_request_count; - size_t bytes = sizeof(AllocationInfo) * info_.allocation_info_count; - - // Allocate an array of AllocationInfo structs from the temp section. This - // struct will be used by AllocationInfoBuilder to find buffer usage. - info_.allocation_info = reinterpret_cast( - non_persistent_allocator_->AllocateTemp(bytes, alignof(AllocationInfo))); - if (info_.allocation_info == nullptr) { - TF_LITE_REPORT_ERROR( - reporter_, - "Failed to allocate memory for memory planning, %d bytes required", - bytes); - return kTfLiteError; - } - return kTfLiteOk; -} - -TfLiteStatus AllocationInfoBuilder::FreeAllocationInfo() { - non_persistent_allocator_->DeallocateTemp( - reinterpret_cast(info_.allocation_info)); - non_persistent_allocator_->DeallocateTemp( - reinterpret_cast(info_.subgraph_offsets)); - return kTfLiteOk; -} - -TfLiteStatus AllocationInfoBuilder::ValidateSubgraph( - const SubGraph* subgraph, TfLiteEvalTensor* eval_tensors) { - uint32_t operators_size = NumSubgraphOperators(subgraph); - - for (uint32_t i = 0; i < operators_size; i++) { - const auto op = subgraph->operators()->Get(i); - for (size_t n = 0; - op->intermediates() != nullptr && n < op->intermediates()->size(); - n++) { - const int tensor_index = op->intermediates()->Get(n); - size_t tensor_size = -1; - TF_LITE_ENSURE_STATUS(TfLiteEvalTensorByteLength( - &eval_tensors[tensor_index], &tensor_size)); - if (tensor_size != 0) { - MicroPrintf( - "Does not support intermediate tensor with non-zero size: %d", - tensor_size); - return kTfLiteError; - } - } - } - return kTfLiteOk; -} - -TfLiteStatus AllocationInfoBuilder::InitializeAllocationInfo( - const int32_t* offline_offsets, SubgraphAllocations* allocations) { - AllocationInfo* allocation_info = info_.allocation_info; - // Initialize allocation info for every tensor in every subgraph. - for (size_t subgraph_idx = 0; subgraph_idx < model_->subgraphs()->size(); - subgraph_idx++) { - const SubGraph* subgraph = model_->subgraphs()->Get(subgraph_idx); - TfLiteEvalTensor* eval_tensors = allocations[subgraph_idx].tensors; - AllocationInfo* subgraph_allocation_info = - &allocation_info[info_.subgraph_offsets[subgraph_idx]]; - - // Ensure constraints are met. - TF_LITE_ENSURE_STATUS(ValidateSubgraph(subgraph, eval_tensors)); - - for (size_t i = 0; i < subgraph->tensors()->size(); ++i) { - AllocationInfo* current = &subgraph_allocation_info[i]; - current->output_ptr = &(eval_tensors[i].data.data); - - TF_LITE_ENSURE_STATUS( - TfLiteEvalTensorByteLength(&eval_tensors[i], ¤t->bytes)); - - current->first_created = kUninitializedLifetime; - current->last_used = kUninitializedLifetime; - current->needs_allocating = - (eval_tensors[i].data.data == nullptr) && - (!subgraph->tensors()->Get(i)->is_variable()) && - (current->bytes != 0); - if (offline_offsets) { - current->offline_offset = offline_offsets[i]; - } else { - current->offline_offset = kOnlinePlannedBuffer; - } - } - } - // Initialize allocation info for every scratch buffer. - AllocationInfo* scratch_allocation_info = - &allocation_info[info_.scratch_offset]; - for (size_t i = 0; i < info_.scratch_buffer_count; i++) { - AllocationInfo* current = &scratch_allocation_info[i]; - current->first_created = kUninitializedLifetime; - current->last_used = kUninitializedLifetime; - current->needs_allocating = true; - current->offline_offset = kOnlinePlannedBuffer; - } - return kTfLiteOk; -} - -TfLiteStatus AllocationInfoBuilder::MarkAllocationLifetimes( - int subgraph_idx, internal::ScratchBufferRequest* scratch_buffer_requests, - ScratchBufferHandle* scratch_buffer_handles, - SubgraphAllocations* allocations) { - const SubGraph* subgraph = model_->subgraphs()->Get(subgraph_idx); - - AllocationInfo* allocation_info = info_.allocation_info; - // Each subgraph's tensor allocations are in a contiguous block starting at - // subgraph_offsets_[subgraph index] with one entry per tensor. - AllocationInfo* subgraph_allocation_info = - &allocation_info[info_.subgraph_offsets[subgraph_idx]]; - - uint32_t operators_size = NumSubgraphOperators(subgraph); - // Mark all inputs as created at the start of the subgraph invocation. - for (size_t i = 0; - subgraph->inputs() != nullptr && i < subgraph->inputs()->size(); ++i) { - const int tensor_index = subgraph->inputs()->Get(i); - AllocationInfo* current = &subgraph_allocation_info[tensor_index]; - UpdateFirstCreated(current, allocation_scope_count_); - } - - for (uint32_t i = 0; i < operators_size; i++) { - // Each operator has a new allocation scope. - allocation_scope_count_++; - const auto* op = subgraph->operators()->Get(i); - // Figure out when the first creation and use of each tensor is. - for (size_t n = 0; op->outputs() != nullptr && n < op->outputs()->size(); - ++n) { - const int tensor_index = op->outputs()->Get(n); - AllocationInfo* current = &subgraph_allocation_info[tensor_index]; - UpdateFirstCreated(current, allocation_scope_count_); - } - - // Keep track of scope count before any subgraphs, so that scratch buffers' - // lifetime within a control flow op properly overlaps with all subgraphs. - int start_allocation_scope_count = allocation_scope_count_; - - // Control flow operators can invoke subgraphs. Plan these subgraphs - // before continuing on to the rest of the graph. - MarkSubgraphLifetimesIfNecessary(op, scratch_buffer_requests, - scratch_buffer_handles, allocations); - - // Figure out when the last use of each tensor is. - for (size_t n = 0; op->inputs() != nullptr && n < op->inputs()->size(); - ++n) { - const int tensor_index = op->inputs()->Get(n); - // Optional bias tensors can have an index of -1 when they are omitted. - if (tensor_index >= 0) { - AllocationInfo* current = &subgraph_allocation_info[tensor_index]; - // No need to update creation since it is either marked by the subgraph - // or producer op, or it is not part of the memory plan (weight, bias - // tensor). - UpdateLastUsed(current, allocation_scope_count_); - } - } - for (size_t n = 0; op->outputs() != nullptr && n < op->outputs()->size(); - ++n) { - const int tensor_index = op->outputs()->Get(n); - AllocationInfo* current = &subgraph_allocation_info[tensor_index]; - UpdateLastUsed(current, allocation_scope_count_); - } - - // Mark thse lifetime of scratch buffers belonging to the current node. This - // operation is O(N * M) where N is the total number of visited nodes and M - // is the total number of scratch buffers. - // TODO(b/217794030): Optimize this memory planning code. - AllocationInfo* scratch_allocation_info = - &allocation_info[info_.scratch_offset]; - for (size_t scratch_idx = 0; scratch_idx < info_.scratch_buffer_count; - scratch_idx++) { - internal::ScratchBufferRequest request = - scratch_buffer_requests[scratch_idx]; - AllocationInfo* current = &scratch_allocation_info[scratch_idx]; - if (request.node_idx == static_cast(i) && - request.subgraph_idx == static_cast(subgraph_idx)) { - ScratchBufferHandle* current_handle = - &(scratch_buffer_handles[scratch_idx]); - current->output_ptr = reinterpret_cast(¤t_handle->data); - current->bytes = request.bytes; - UpdateFirstCreated(current, start_allocation_scope_count); - UpdateLastUsed(current, allocation_scope_count_); - } - } - } - - // Mark all outputs as persistent to the end of the subgraph invocation. - for (size_t i = 0; - subgraph->outputs() != nullptr && i < subgraph->outputs()->size(); ++i) { - const int tensor_index = subgraph->outputs()->Get(i); - AllocationInfo* current = &subgraph_allocation_info[tensor_index]; - UpdateLastUsed(current, allocation_scope_count_); - } - return kTfLiteOk; -} - -// Get offline tensors allocation plan. See -// micro/docs/memory_management.md for more info. -TfLiteStatus AllocationInfoBuilder::GetOfflinePlannedOffsets( - const int32_t** offline_planner_offsets) { - if (model_->metadata()) { - for (size_t i = 0; i < model_->metadata()->size(); ++i) { - auto metadata = model_->metadata()->Get(i); - if (strncmp(metadata->name()->c_str(), kOfflineMemAllocMetadata, - strlen(kOfflineMemAllocMetadata)) == 0) { - const flatbuffers::Vector>* buffers = - model_->buffers(); - auto* buffer = (*buffers)[metadata->buffer()]; - auto* array = buffer->data(); - const uint32_t* metadata_buffer = - reinterpret_cast(array->data()); - const size_t nbr_tensors = static_cast(metadata_buffer[2]); - *offline_planner_offsets = - reinterpret_cast(&metadata_buffer[3]); - - if (info_.tensor_count != nbr_tensors) { - TF_LITE_REPORT_ERROR(reporter_, - "Nbr of offline buffer offsets (%d) in metadata " - "not equal nbr tensors (%d)\n", - nbr_tensors, info_.tensor_count); - return kTfLiteError; - } - } - } - } - return kTfLiteOk; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/micro_allocation_info.h b/code/components/tflite-lib/tensorflow/lite/micro/micro_allocation_info.h deleted file mode 100644 index 4ea435b3..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/micro_allocation_info.h +++ /dev/null @@ -1,152 +0,0 @@ -/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_MICRO_MICRO_ALLOCATION_INFO_H_ -#define TENSORFLOW_LITE_MICRO_MICRO_ALLOCATION_INFO_H_ - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/core/api/error_reporter.h" -#include "tensorflow/lite/core/api/flatbuffer_conversions.h" -#include "tensorflow/lite/micro/compatibility.h" -#include "tensorflow/lite/micro/flatbuffer_utils.h" -#include "tensorflow/lite/micro/micro_allocator.h" -#include "tensorflow/lite/schema/schema_generated.h" - -namespace tflite { - -// Used to hold information used during allocation calculations. -struct AllocationInfo { - size_t bytes; - void** output_ptr; - int first_created; - int last_used; - int32_t offline_offset; - bool needs_allocating; -}; - -// Used to hold the allocation info list and related metadata for the entire -// graph (including subgraphs). Since all subgraphs are planned together, the -// allocation info list contains allocations for all subgraphs. Track the offset -// into this list for each subgraph then reserve space to track all allocations. -// -// The AllocationInfo list is a contiguous list of allocations across all -// subgraphs and scratch buffers. Each element here is marked as -// st. The following is a possible -// AllocationInfo list: -// [s0t0, s0t1, s1t0, s2t1, s1t2, s3t0, s3t1, scratch0, scratch1, scratch2] -// -// For this example, the subgraph offsets would be [0, 2, 5] and the scratch -// offset would be 7. -struct GraphAllocationInfo { - AllocationInfo* allocation_info; - size_t allocation_info_count; - size_t* subgraph_offsets; - size_t scratch_offset; - size_t tensor_count; - size_t scratch_buffer_count; -}; - -// A helper class to construct AllocationInfo array. This array contains the -// lifetime of tensors / scratch_buffer and will be used to calculate the memory -// plan. Methods need to be called in order from `Create`, Init`, `Add*`, to -// `Finish`. -class AllocationInfoBuilder { - public: - AllocationInfoBuilder(const Model* model, - INonPersistentBufferAllocator* non_persistent_allocator, - ErrorReporter* reporter) - : model_(model), - non_persistent_allocator_(non_persistent_allocator) -#if !defined(TF_LITE_STRIP_ERROR_STRINGS) - , - reporter_(reporter) -#endif - { - } - - // Check if model contains offline planned buffer offsets. - // - If there's no metadata available, offline_planner_offsets is not set - // - If there's metadata available, offline_planner_offsets will point to the - // first offset in the metadata buffer list. - TfLiteStatus GetOfflinePlannedOffsets( - const int32_t** offline_planner_offsets); - - // Allocate memory for the allocation info array as well as offsets into that - // array for each subgraph. - TfLiteStatus CreateAllocationInfo(int scratch_buffer_request_count); - - // Release memory used for the allocation info array. - TfLiteStatus FreeAllocationInfo(); - - // Initialize AllocationInfo for all tensors and scratch buffers in the graph. - TfLiteStatus InitializeAllocationInfo(const int32_t* offline_offsets, - SubgraphAllocations* allocations); - - // Mark the scope of each tensor and scratch buffer across the graph. Enter - // all possible subgraphs invoked by each control flow operator. This method - // marks the maximum lifetime of each buffer so that tensors are correctly - // planned for all valid invocation flows. - TfLiteStatus MarkAllocationLifetimes( - int subgraph_idx, internal::ScratchBufferRequest* scratch_buffer_request, - ScratchBufferHandle* scratch_buffer_handles, - SubgraphAllocations* allocations); - - // Identify control flow operators and recursively mark all subgraphs which - // that operator can invoke. The lifetime of all tensors within a subgraph - // can only be extended. The order of subgraph invocation does not matter - // since subgraphs within the same control flow operator are executed - // within their own allocation scope (planned buffers in a subgraph cannot - // persist beyond the end of that subgraph's invocation). - TfLiteStatus MarkSubgraphLifetimesIfNecessary( - const Operator* op, - internal::ScratchBufferRequest* scratch_buffer_requests, - ScratchBufferHandle* scratch_buffer_handles, - SubgraphAllocations* allocations); - - // Returns the number of allocations. - int AllocationCount() const { return info_.allocation_info_count; } - - // Returns a pointer to the built AllocationInfo array. - AllocationInfo* Finish() const { return info_.allocation_info; } - - private: - // Mark the given Allocation info as first created at the specified allocation - // scope count. Only the first creation must be recorded since the allocation - // scope count monotonically increases throughout the lifetime marking - // process. - void UpdateFirstCreated(AllocationInfo* current, int allocation_scope_count); - - // Mark the given AllocationInfo as last used at the specified allocation - // scope - // count. Update the last used marker every time, since the allocation scope - // count monotonically increases through the lifetime marking process. - void UpdateLastUsed(AllocationInfo* current, int allocation_scope_count); - - // Validate if a subgraph satisfies assumptions. - TfLiteStatus ValidateSubgraph(const SubGraph* subgraph, - TfLiteEvalTensor* eval_tensors); - - const tflite::Model* model_ = nullptr; - INonPersistentBufferAllocator* non_persistent_allocator_ = nullptr; -#if !defined(TF_LITE_STRIP_ERROR_STRINGS) - ErrorReporter* reporter_ = nullptr; -#endif - - GraphAllocationInfo info_; - int allocation_scope_count_ = 0; -}; - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_MICRO_ALLOCATION_INFO_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/micro_allocator.cc b/code/components/tflite-lib/tensorflow/lite/micro/micro_allocator.cc deleted file mode 100644 index 6b78fdc0..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/micro_allocator.cc +++ /dev/null @@ -1,964 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/micro_allocator.h" - -#include -#include - -#include "flatbuffers/flatbuffers.h" // from @flatbuffers -#include "tensorflow/lite/c/c_api_types.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/core/api/error_reporter.h" -#include "tensorflow/lite/core/api/flatbuffer_conversions.h" -#include "tensorflow/lite/core/api/op_resolver.h" -#include "tensorflow/lite/core/api/tensor_utils.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/micro/arena_allocator/non_persistent_arena_buffer_allocator.h" -#include "tensorflow/lite/micro/arena_allocator/persistent_arena_buffer_allocator.h" -#include "tensorflow/lite/micro/arena_allocator/single_arena_buffer_allocator.h" -#include "tensorflow/lite/micro/compatibility.h" -#include "tensorflow/lite/micro/flatbuffer_utils.h" -#include "tensorflow/lite/micro/memory_helpers.h" -#include "tensorflow/lite/micro/memory_planner/greedy_memory_planner.h" -#include "tensorflow/lite/micro/memory_planner/micro_memory_planner.h" -#include "tensorflow/lite/micro/micro_allocation_info.h" -#include "tensorflow/lite/micro/micro_arena_constants.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" -#include "tensorflow/lite/schema/schema_generated.h" -#include "tensorflow/lite/schema/schema_utils.h" - -namespace tflite { - -namespace { - -// Maximum number of scratch buffer requests per operator. Operator kernels that -// request more than this value will receive an exception. -constexpr size_t kMaxScratchBuffersPerOp = 12; - -// Sentinel value used as a placeholder to mark a ScratchBufferRequest request -// needs a node id assignment. -constexpr int kUnassignedScratchBufferRequestIndex = -1; - -const TfLiteIntArray kZeroLengthIntArray = {}; - -class MicroBuiltinDataAllocator : public BuiltinDataAllocator { - public: - explicit MicroBuiltinDataAllocator( - IPersistentBufferAllocator* persistent_allocator) - : persistent_allocator_(persistent_allocator) {} - - void* Allocate(size_t size, size_t alignment_hint) override { - return persistent_allocator_->AllocatePersistentBuffer(size, - alignment_hint); - } - void Deallocate(void* data) override { - // Do not deallocate, builtin data needs to be available for the life time - // of the model. - } - - TF_LITE_REMOVE_VIRTUAL_DELETE - - private: - IPersistentBufferAllocator* persistent_allocator_; -}; - -TfLiteStatus CreatePlan(ErrorReporter* error_reporter, - MicroMemoryPlanner* planner, - const AllocationInfo* allocation_info, - size_t allocation_info_size) { - // Add the tensors to our allocation plan. - for (size_t i = 0; i < allocation_info_size; ++i) { - const AllocationInfo* current = &allocation_info[i]; - if (current->needs_allocating) { - size_t aligned_bytes_required = - AlignSizeUp(current->bytes, MicroArenaBufferAlignment()); - if (current->offline_offset == kOnlinePlannedBuffer) { - TF_LITE_ENSURE_STATUS( - planner->AddBuffer(error_reporter, aligned_bytes_required, - current->first_created, current->last_used)); - } else { - TF_LITE_ENSURE_STATUS(planner->AddBuffer( - error_reporter, aligned_bytes_required, current->first_created, - current->last_used, current->offline_offset)); - } - } - } - return kTfLiteOk; -} - -TfLiteStatus CommitPlan(ErrorReporter* error_reporter, - MicroMemoryPlanner* planner, uint8_t* starting_point, - const AllocationInfo* allocation_info, - size_t allocation_info_size) { - // Figure out the actual memory addresses for each buffer, based on the plan. - int planner_index = 0; - for (size_t i = 0; i < allocation_info_size; ++i) { - const AllocationInfo* current = &allocation_info[i]; - if (current->needs_allocating) { - int offset = -1; - TF_LITE_ENSURE_STATUS( - planner->GetOffsetForBuffer(error_reporter, planner_index, &offset)); - *current->output_ptr = reinterpret_cast(starting_point + offset); - ++planner_index; - } - } - return kTfLiteOk; -} - -IPersistentBufferAllocator* CreatePersistentArenaAllocator(uint8_t* buffer_head, - size_t buffer_size) { - // Align the actually used area by the tail because persistent buffer grows - // from the bottom to top. - uint8_t* aligned_buffer_tail = - AlignPointerDown(buffer_head + buffer_size, MicroArenaBufferAlignment()); - size_t aligned_buffer_size = aligned_buffer_tail - buffer_head; - PersistentArenaBufferAllocator tmp = - PersistentArenaBufferAllocator(buffer_head, aligned_buffer_size); - - // Allocate enough bytes from the buffer to create a - // SingleArenaBufferAllocator. The new instance will use the current adjusted - // tail buffer from the tmp allocator instance. - uint8_t* allocator_buffer = - tmp.AllocatePersistentBuffer(sizeof(PersistentArenaBufferAllocator), - alignof(PersistentArenaBufferAllocator)); - // Use the default copy constructor to populate internal states. - return new (allocator_buffer) PersistentArenaBufferAllocator(tmp); -} - -// NonPersistentBufferAllocator instance is created in the persistent buffer -// because it has to be persistent to keep track of the non-persistent buffer -// information. -INonPersistentBufferAllocator* CreateNonPersistentArenaAllocator( - uint8_t* buffer_head, size_t buffer_size, - IPersistentBufferAllocator* persistent_buffer_allocator) { - uint8_t* allocator_buffer = - persistent_buffer_allocator->AllocatePersistentBuffer( - sizeof(NonPersistentArenaBufferAllocator), - alignof(NonPersistentArenaBufferAllocator)); - // Align the actually used area by the head because persistent buffer grows - // from the head to bottom. - uint8_t* aligned_buffer_head = - AlignPointerUp(buffer_head, MicroArenaBufferAlignment()); - size_t aligned_buffer_size = buffer_head + buffer_size - aligned_buffer_head; - - INonPersistentBufferAllocator* non_persistent_buffer_allocator = - new (allocator_buffer) NonPersistentArenaBufferAllocator( - aligned_buffer_head, aligned_buffer_size); - return non_persistent_buffer_allocator; -} - -} // namespace - -namespace internal { - -// Returns a pointer to any buffer associated with the flatbuffer tensor. Can -// return nullptr if no buffer is found. -void* GetFlatbufferTensorBuffer( - const tflite::Tensor& flatbuffer_tensor, - const flatbuffers::Vector>* buffers) { - // We need to figure out where the actual contents of this tensor are stored - // in memory. We'll check to see if there's a serialized buffer (pretty much - // the same as a constant op in TensorFlow) associated with this tensor first, - // and if there is update the runtime structure to point to its location in - // memory. - // First see if there's any buffer information in the serialized tensor. - // TODO(b/170379532): Add better unit tests to validate flatbuffer values. - void* out_buffer = nullptr; - if (auto* buffer = (*buffers)[flatbuffer_tensor.buffer()]) { - // If we've found a buffer, does it have any data? - if (auto* array = buffer->data()) { - // If it has any data, is the data size larger than zero? - if (array->size()) { - // We've found a buffer with valid data, so update the runtime tensor - // data structure to point to it. - out_buffer = const_cast(static_cast(array->data())); - } - } - // TODO(petewarden): It's not clear in what circumstances we could have a - // buffer in the serialized tensor, but it doesn't have any data in it. Is - // that a validly-generated file, and if so what does it mean, or is it an - // error condition? It would be good to tighten up the specification to make - // it less ambiguous. - } - return out_buffer; -} - -TfLiteStatus InitializeTfLiteTensorFromFlatbuffer( - IPersistentBufferAllocator* persistent_buffer_allocator, - INonPersistentBufferAllocator* non_persistent_buffer_allocator, - bool allocate_temp, const tflite::Tensor& flatbuffer_tensor, - const flatbuffers::Vector>* buffers, - ErrorReporter* error_reporter, TfLiteTensor* result) { - TFLITE_DCHECK(result != nullptr); - - *result = {}; - // Make sure the serialized type is one we know how to deal with, and convert - // it from a flatbuffer enum into a constant used by the kernel C API. - TF_LITE_ENSURE_STATUS(ConvertTensorType(flatbuffer_tensor.type(), - &result->type, error_reporter)); - // Make sure we remember if the serialized tensor is designated as a variable. - result->is_variable = flatbuffer_tensor.is_variable(); - - result->data.data = GetFlatbufferTensorBuffer(flatbuffer_tensor, buffers); - - // TODO(petewarden): Some of these paths aren't getting enough testing - // coverage, so we should figure out some tests that exercise them. - if (result->data.data == nullptr) { - // The tensor contents haven't been set from a serialized buffer, so - // make a note that they will be allocated from memory. The actual - // allocation won't happen until later. - result->allocation_type = kTfLiteArenaRw; - } else { - // We set the data from a serialized buffer, so record tha. - result->allocation_type = kTfLiteMmapRo; - } - - // Figure out what the size in bytes of the buffer is and store it. - size_t type_size; - TF_LITE_ENSURE_STATUS(BytesRequiredForTensor( - flatbuffer_tensor, &result->bytes, &type_size, error_reporter)); - - if (flatbuffer_tensor.shape() == nullptr) { - // flatbuffer_tensor.shape() can return a nullptr in the case of a scalar - // tensor. - // TODO(b/188459715): figure out why const_cast is required here. - result->dims = const_cast(&kZeroLengthIntArray); - } else { - // TFLM doesn't allow reshaping the tensor which requires dynamic memory - // allocation so it is safe to drop the const qualifier. In the future, if - // we really want to update the tensor shape, we can always pass in a new - // TfLiteIntArray - especially we have to do so if the dimension is - result->dims = FlatBufferVectorToTfLiteTypeArray(flatbuffer_tensor.shape()); - } - - // Copy the quantization information from the serialized data. - const auto* src_quantization = flatbuffer_tensor.quantization(); - if (src_quantization && src_quantization->scale() && - (src_quantization->scale()->size() > 0) && - src_quantization->zero_point() && - (src_quantization->zero_point()->size() > 0)) { - // Always populate the TfLiteTensor.params field, even if there are - // per-channel quantization parameters. - result->params.scale = src_quantization->scale()->Get(0); - // Note that the zero_point field in the FlatBuffers schema is a 64-bit - // integer, but the zero_point field in the TfLiteQuantizationParams struct - // is a 32-bit integer. - result->params.zero_point = - static_cast(src_quantization->zero_point()->Get(0)); - - // Populate per-channel quantization params. - int channels = src_quantization->scale()->size(); - TfLiteAffineQuantization* quantization = - allocate_temp - ? reinterpret_cast( - non_persistent_buffer_allocator->AllocateTemp( - sizeof(TfLiteAffineQuantization), - alignof(TfLiteAffineQuantization))) - : reinterpret_cast( - persistent_buffer_allocator->AllocatePersistentBuffer( - sizeof(TfLiteAffineQuantization), - alignof(TfLiteAffineQuantization))); - if (quantization == nullptr) { - TF_LITE_REPORT_ERROR(error_reporter, - "Unable to allocate TfLiteAffineQuantization.\n"); - return kTfLiteError; - } - - // TODO(b/153688719): Reduce tail allocation by using a global zero-point - // buffer. This value can not be reused from the flatbuffer since the - // zero_point is stored as a int64_t. - quantization->zero_point = - allocate_temp - ? reinterpret_cast( - non_persistent_buffer_allocator->AllocateTemp( - TfLiteIntArrayGetSizeInBytes(channels), - alignof(TfLiteIntArray))) - : reinterpret_cast( - persistent_buffer_allocator->AllocatePersistentBuffer( - TfLiteIntArrayGetSizeInBytes(channels), - alignof(TfLiteIntArray))); - if (quantization->zero_point == nullptr) { - TF_LITE_REPORT_ERROR(error_reporter, - "Unable to allocate quantization->zero_point.\n"); - return kTfLiteError; - } - - quantization->scale = - FlatBufferVectorToTfLiteTypeArray(src_quantization->scale()); - - quantization->zero_point->size = channels; - int* zero_point_data = quantization->zero_point->data; - for (int i = 0; i < channels; i++) { - // As a space-saving optimization, zero point arrays for weights can be - // reduced to a single value, since all zero points for weights are 0. - zero_point_data[i] = src_quantization->zero_point()->size() == - src_quantization->scale()->size() - ? src_quantization->zero_point()->Get(i) - : src_quantization->zero_point()->Get(0); - } - // TODO(rocky): Need to add a micro_allocator test case that fails when - // this is not copied: - quantization->quantized_dimension = src_quantization->quantized_dimension(); - - result->quantization = {kTfLiteAffineQuantization, quantization}; - } - return kTfLiteOk; -} - -TfLiteStatus InitializeTfLiteEvalTensorFromFlatbuffer( - const tflite::Tensor& flatbuffer_tensor, - const flatbuffers::Vector>* buffers, - ErrorReporter* error_reporter, TfLiteEvalTensor* result) { - *result = {}; - // Make sure the serialized type is one we know how to deal with, and convert - // it from a flatbuffer enum into a constant used by the kernel C API. - TF_LITE_ENSURE_STATUS(ConvertTensorType(flatbuffer_tensor.type(), - &result->type, error_reporter)); - - result->data.data = GetFlatbufferTensorBuffer(flatbuffer_tensor, buffers); - - if (flatbuffer_tensor.shape() == nullptr) { - // flatbuffer_tensor.shape() can return a nullptr in the case of a scalar - // tensor. - result->dims = const_cast(&kZeroLengthIntArray); - } else { - result->dims = FlatBufferVectorToTfLiteTypeArray(flatbuffer_tensor.shape()); - } - return kTfLiteOk; -} - -} // namespace internal - -size_t MicroAllocator::GetDefaultTailUsage(bool is_memory_planner_given) { - // TODO(b/208703041): a template version of AlignSizeUp to make expression - // shorter. - size_t total_size = - AlignSizeUp(sizeof(SingleArenaBufferAllocator), - alignof(SingleArenaBufferAllocator)) + - AlignSizeUp(sizeof(MicroAllocator), alignof(MicroAllocator)) + - AlignSizeUp(sizeof(MicroBuiltinDataAllocator), - alignof(MicroBuiltinDataAllocator)) + - AlignSizeUp(sizeof(SubgraphAllocations), alignof(SubgraphAllocations)); - if (!is_memory_planner_given) { - total_size += - AlignSizeUp(sizeof(GreedyMemoryPlanner), alignof(GreedyMemoryPlanner)); - } - return total_size; -} - -MicroAllocator::MicroAllocator(SingleArenaBufferAllocator* memory_allocator, - MicroMemoryPlanner* memory_planner, - ErrorReporter* error_reporter) - : non_persistent_buffer_allocator_(memory_allocator), - persistent_buffer_allocator_(memory_allocator), - memory_planner_(memory_planner), - error_reporter_(error_reporter), - model_is_allocating_(false) {} - -MicroAllocator::MicroAllocator( - IPersistentBufferAllocator* persistent_buffer_allocator, - INonPersistentBufferAllocator* non_persistent_buffer_allocator, - MicroMemoryPlanner* memory_planner, ErrorReporter* error_reporter) - : non_persistent_buffer_allocator_(non_persistent_buffer_allocator), - persistent_buffer_allocator_(persistent_buffer_allocator), - memory_planner_(memory_planner), - error_reporter_(error_reporter), - model_is_allocating_(false) {} - -MicroAllocator::~MicroAllocator() {} - -MicroAllocator* MicroAllocator::Create(uint8_t* tensor_arena, size_t arena_size, - MicroMemoryPlanner* memory_planner, - ErrorReporter* error_reporter) { - uint8_t* aligned_arena = - AlignPointerUp(tensor_arena, MicroArenaBufferAlignment()); - size_t aligned_arena_size = tensor_arena + arena_size - aligned_arena; - SingleArenaBufferAllocator* memory_allocator = - SingleArenaBufferAllocator::Create(error_reporter, aligned_arena, - aligned_arena_size); - - return Create(memory_allocator, memory_planner, error_reporter); -} - -MicroAllocator* MicroAllocator::Create(uint8_t* tensor_arena, size_t arena_size, - ErrorReporter* error_reporter) { - uint8_t* aligned_arena = - AlignPointerUp(tensor_arena, MicroArenaBufferAlignment()); - size_t aligned_arena_size = tensor_arena + arena_size - aligned_arena; - SingleArenaBufferAllocator* memory_allocator = - SingleArenaBufferAllocator::Create(error_reporter, aligned_arena, - aligned_arena_size); - - // By default create GreedyMemoryPlanner. - // If a different MemoryPlanner is needed, use the other api. - uint8_t* memory_planner_buffer = memory_allocator->AllocatePersistentBuffer( - sizeof(GreedyMemoryPlanner), alignof(GreedyMemoryPlanner)); - GreedyMemoryPlanner* memory_planner = - new (memory_planner_buffer) GreedyMemoryPlanner(); - - return Create(memory_allocator, memory_planner, error_reporter); -} - -MicroAllocator* MicroAllocator::Create( - SingleArenaBufferAllocator* memory_allocator, - MicroMemoryPlanner* memory_planner, ErrorReporter* error_reporter) { - TFLITE_DCHECK(memory_allocator != nullptr); - TFLITE_DCHECK(error_reporter != nullptr); - TFLITE_DCHECK(memory_planner != nullptr); - - uint8_t* allocator_buffer = memory_allocator->AllocatePersistentBuffer( - sizeof(MicroAllocator), alignof(MicroAllocator)); - MicroAllocator* allocator = new (allocator_buffer) MicroAllocator( - memory_allocator, memory_allocator, memory_planner, error_reporter); - return allocator; -} - -MicroAllocator* MicroAllocator::Create(uint8_t* persistent_tensor_arena, - size_t persistent_arena_size, - uint8_t* non_persistent_tensor_arena, - size_t non_persistent_arena_size, - ErrorReporter* error_reporter) { - TFLITE_DCHECK(persistent_tensor_arena != nullptr); - TFLITE_DCHECK(non_persistent_tensor_arena != nullptr); - TFLITE_DCHECK(persistent_tensor_arena != non_persistent_tensor_arena); - TFLITE_DCHECK(error_reporter != nullptr); - - IPersistentBufferAllocator* persistent_buffer_allocator = - CreatePersistentArenaAllocator(persistent_tensor_arena, - persistent_arena_size); - INonPersistentBufferAllocator* non_persistent_buffer_allocator = - CreateNonPersistentArenaAllocator(non_persistent_tensor_arena, - non_persistent_arena_size, - persistent_buffer_allocator); - - uint8_t* memory_planner_buffer = - persistent_buffer_allocator->AllocatePersistentBuffer( - sizeof(GreedyMemoryPlanner), alignof(GreedyMemoryPlanner)); - GreedyMemoryPlanner* memory_planner = - new (memory_planner_buffer) GreedyMemoryPlanner(); - - uint8_t* micro_allocator_buffer = - persistent_buffer_allocator->AllocatePersistentBuffer( - sizeof(MicroAllocator), alignof(MicroAllocator)); - MicroAllocator* allocator = new (micro_allocator_buffer) MicroAllocator( - persistent_buffer_allocator, non_persistent_buffer_allocator, - memory_planner, error_reporter); - return allocator; -} - -SubgraphAllocations* MicroAllocator::StartModelAllocation(const Model* model) { - TFLITE_DCHECK(model != nullptr); - - if (model_is_allocating_) { - TF_LITE_REPORT_ERROR(error_reporter_, - "MicroAllocator: Model allocation started before " - "finishing previously allocated model"); - return nullptr; - } - - model_is_allocating_ = true; - - uint8_t* data_allocator_buffer = - persistent_buffer_allocator_->AllocatePersistentBuffer( - sizeof(MicroBuiltinDataAllocator), - alignof(MicroBuiltinDataAllocator)); - builtin_data_allocator_ = new (data_allocator_buffer) - MicroBuiltinDataAllocator(persistent_buffer_allocator_); - - if (InitScratchBufferData() != kTfLiteOk) { - return nullptr; - } - - // Allocate struct to store eval tensors, nodes and registrations. - SubgraphAllocations* output = reinterpret_cast( - persistent_buffer_allocator_->AllocatePersistentBuffer( - sizeof(SubgraphAllocations) * model->subgraphs()->size(), - alignof(SubgraphAllocations))); - if (output == nullptr) { - MicroPrintf("Failed to allocate memory for model metadata."); - return nullptr; - } - - if (AllocateTfLiteEvalTensors(model, output) != kTfLiteOk || - AllocateNodeAndRegistrations(model, output) != kTfLiteOk) { - return nullptr; - } - return output; -} - -TfLiteStatus MicroAllocator::FinishModelAllocation( - const Model* model, SubgraphAllocations* subgraph_allocations, - ScratchBufferHandle** scratch_buffer_handles) { - if (!model_is_allocating_) { - TF_LITE_REPORT_ERROR(error_reporter_, - "MicroAllocator: Model allocation finished before " - "starting allocating model"); - return kTfLiteError; - } - - // Allocate scratch buffer metadata and buffers for variable tensors. - for (size_t subgraph_idx = 0; subgraph_idx < model->subgraphs()->size(); - subgraph_idx++) { - const SubGraph* subgraph = model->subgraphs()->Get(subgraph_idx); - TFLITE_DCHECK(subgraph != nullptr); - - TF_LITE_ENSURE_STATUS(AllocateScratchBufferHandles( - scratch_buffer_handles, scratch_buffer_request_count_)); - TF_LITE_ENSURE_STATUS(AllocateVariables( - subgraph, subgraph_allocations[subgraph_idx].tensors)); - } - - // Plan all subgraphs and scratch buffers together. - TF_LITE_ENSURE_STATUS(CommitStaticMemoryPlan(model, subgraph_allocations, - *scratch_buffer_handles)); - model_is_allocating_ = false; - return kTfLiteOk; -} - -void* MicroAllocator::AllocatePersistentBuffer(size_t bytes) { - return persistent_buffer_allocator_->AllocatePersistentBuffer( - bytes, MicroArenaBufferAlignment()); -} - -TfLiteStatus MicroAllocator::RequestScratchBufferInArena(size_t bytes, - int subgraph_idx, - int* buffer_idx) { - // All scratch buffer requests are stored in the head section of the arena - // when a model is in the prepare phase. First align a scratch buffer request - // pointer to the start of the head: - internal::ScratchBufferRequest* requests = GetScratchBufferRequests(); - - // Count the number of requested scratch buffers for the current node: - size_t current_node_request_count = 0; - for (size_t i = 0; i < scratch_buffer_request_count_; ++i) { - if (requests[i].node_idx == kUnassignedScratchBufferRequestIndex) { - ++current_node_request_count; - } - } - - // First, ensure that the per-kernel request has not exceeded the limit: - if (current_node_request_count >= kMaxScratchBuffersPerOp) { - TF_LITE_REPORT_ERROR( - error_reporter_, - "Scratch buffer request exeeds limit per operator (%d)", - kMaxScratchBuffersPerOp); - return kTfLiteError; - } - - // Initialize and assign values for the request at the current index: - internal::ScratchBufferRequest* current_request = - &requests[scratch_buffer_request_count_]; - *current_request = {}; - // Assign -1 as a sentinel value that will be updated when the node finishes - // allocating: - current_request->bytes = bytes; - current_request->node_idx = kUnassignedScratchBufferRequestIndex; - current_request->subgraph_idx = subgraph_idx; - - // Assign the current request index to the out-param: - *buffer_idx = scratch_buffer_request_count_; - - // Bump the request count to prepare for the next request: - ++scratch_buffer_request_count_; - return kTfLiteOk; -} - -TfLiteStatus MicroAllocator::FinishPrepareNodeAllocations(int node_id) { - // When a node has finished preparing, all temp allocations performed by the - // kernel should be cleaned up: - TF_LITE_ENSURE_STATUS(ResetTempAllocations()); - - // Find and update any new scratch buffer requests for the current node: - internal::ScratchBufferRequest* requests = GetScratchBufferRequests(); - - for (size_t i = 0; i < scratch_buffer_request_count_; ++i) { - // A request with a node_idx of -1 is a sentinel value used to indicate this - // was a new request for the current node. The allocator finally knows the - // node index at this point. Assign the value and update the list of new - // requests so the head section can be adjusted to allow for the next kernel - // to allocate at most kMaxScratchBuffersPerOp requests: - if (requests[i].node_idx == kUnassignedScratchBufferRequestIndex) { - requests[i].node_idx = node_id; - } - } - - // Ensure that the head is re-adjusted to allow for another at-most - // kMaxScratchBuffersPerOp scratch buffer requests in the next operator: - TF_LITE_ENSURE_STATUS(non_persistent_buffer_allocator_->ResizeBuffer( - scratch_buffer_head_, - sizeof(internal::ScratchBufferRequest) * - (scratch_buffer_request_count_ + kMaxScratchBuffersPerOp), - alignof(internal::ScratchBufferRequest))); - - return kTfLiteOk; -} - -size_t MicroAllocator::used_bytes() const { - return non_persistent_buffer_allocator_->GetNonPersistentUsedBytes() + - persistent_buffer_allocator_->GetPersistentUsedBytes(); -} - -TfLiteStatus MicroAllocator::AllocateNodeAndRegistrations( - const Model* model, SubgraphAllocations* subgraph_allocations) { - TFLITE_DCHECK(subgraph_allocations != nullptr); - - for (size_t subgraph_idx = 0; subgraph_idx < model->subgraphs()->size(); - subgraph_idx++) { - const SubGraph* subgraph = model->subgraphs()->Get(subgraph_idx); - TFLITE_DCHECK(subgraph != nullptr); - - uint32_t operators_size = NumSubgraphOperators(subgraph); - - // Initialize NodeAndRegistrations for the subgraph. - NodeAndRegistration* output = reinterpret_cast( - persistent_buffer_allocator_->AllocatePersistentBuffer( - sizeof(NodeAndRegistration) * operators_size, - alignof(NodeAndRegistration))); - if (output == nullptr) { - TF_LITE_REPORT_ERROR( - error_reporter_, - "Failed to allocate memory for node_and_registrations."); - return kTfLiteError; - } - subgraph_allocations[subgraph_idx].node_and_registrations = output; - } - return kTfLiteOk; -} - -TfLiteTensor* MicroAllocator::AllocatePersistentTfLiteTensor( - const Model* model, const SubgraphAllocations* subgraph_allocations, - int tensor_index, int subgraph_index) { - const SubGraph* subgraph = model->subgraphs()->Get(subgraph_index); - TFLITE_DCHECK(subgraph != nullptr); - - // This value is allocated from persistent arena space. It is guaranteed to be - // around for the lifetime of the application. - TfLiteTensor* tensor = AllocatePersistentTfLiteTensorInternal(); - - // Populate any fields from the flatbuffer, since this TfLiteTensor struct is - // allocated in the persistent section of the arena, ensure that additional - // allocations also take place in that section of the arena. - if (PopulateTfLiteTensorFromFlatbuffer( - model, tensor, tensor_index, subgraph_index, - /*allocate_temp=*/false) != kTfLiteOk) { - TF_LITE_REPORT_ERROR(error_reporter_, - "Failed to populate a persistent TfLiteTensor struct " - "from flatbuffer data!"); - return nullptr; - } - - if (subgraph_allocations != nullptr) { - // Tensor buffers that are allocated at runtime (e.g. non-weight buffers) - // and not located in the flatbuffer are stored on the pre-allocated list of - // TfLiteEvalTensors structs. These structs are the source of truth, simply - // point the corresponding buffer to the new TfLiteTensor data value. - tensor->data.data = - subgraph_allocations[subgraph_index].tensors[tensor_index].data.data; - // TfLiteEvalTensor structs must also be the source of truth for the - // TfLiteTensor dims. - tensor->dims = - subgraph_allocations[subgraph_index].tensors[tensor_index].dims; - } - return tensor; -} - -void MicroAllocator::DeallocateTempTfLiteTensor(TfLiteTensor* tensor) { - TFLITE_DCHECK(tensor != nullptr); - - if (tensor->quantization.type == kTfLiteAffineQuantization) { - TFLITE_DCHECK(tensor->quantization.params != nullptr); - TfLiteAffineQuantization* quantization = - reinterpret_cast( - tensor->quantization.params); - - non_persistent_buffer_allocator_->DeallocateTemp( - reinterpret_cast(quantization->zero_point)); - non_persistent_buffer_allocator_->DeallocateTemp( - reinterpret_cast(quantization)); - } - - // Clear the data in case someone still access tensor arena by mistake - tensor->quantization.type = kTfLiteNoQuantization; - tensor->quantization.params = nullptr; - tensor->data.data = nullptr; - tensor->dims = nullptr; - non_persistent_buffer_allocator_->DeallocateTemp( - reinterpret_cast(tensor)); -} - -TfLiteTensor* MicroAllocator::AllocateTempTfLiteTensor( - const Model* model, const SubgraphAllocations* subgraph_allocations, - int tensor_index, int subgraph_index) { - const SubGraph* subgraph = model->subgraphs()->Get(subgraph_index); - TFLITE_DCHECK(subgraph != nullptr); - - // This value is allocated from temporary arena space. It is guaranteed to be - // around for at least the scope of the calling function. Since this struct - // allocation takes place in temp space, no need to own or cleanup. - TfLiteTensor* tensor = reinterpret_cast( - non_persistent_buffer_allocator_->AllocateTemp(sizeof(TfLiteTensor), - alignof(TfLiteTensor))); - - // Populate any fields from the flatbuffer, since this TfLiteTensor struct is - // allocated in the temp section of the arena, ensure that additional - // allocations also take place in that section of the arena. - if (PopulateTfLiteTensorFromFlatbuffer(model, tensor, tensor_index, - subgraph_index, - /*allocate_temp=*/true) != kTfLiteOk) { - TF_LITE_REPORT_ERROR( - error_reporter_, - "Failed to populate a temp TfLiteTensor struct from flatbuffer data!"); - return nullptr; - } - - if (subgraph_allocations != nullptr) { - // Tensor buffers that are allocated at runtime (e.g. non-weight buffers) - // and not located in the flatbuffer are stored on the pre-allocated list of - // TfLiteEvalTensors structs. These structs are the source of truth, simply - // point the corresponding buffer to the new TfLiteTensor data value. - tensor->data.data = - subgraph_allocations[subgraph_index].tensors[tensor_index].data.data; - // TfLiteEvalTensor structs must also be the source of truth for the - // TfLiteTensor dims. - tensor->dims = - subgraph_allocations[subgraph_index].tensors[tensor_index].dims; - } - return tensor; -} - -TfLiteStatus MicroAllocator::ResetTempAllocations() { - return non_persistent_buffer_allocator_->ResetTempAllocations(); -} - -bool MicroAllocator::IsAllTempDeallocated() { - return non_persistent_buffer_allocator_->IsAllTempDeallocated(); -} - -TfLiteStatus MicroAllocator::AllocateTfLiteEvalTensors( - const Model* model, SubgraphAllocations* subgraph_allocations) { - TFLITE_DCHECK(subgraph_allocations != nullptr); - - for (size_t subgraph_idx = 0; subgraph_idx < model->subgraphs()->size(); - subgraph_idx++) { - const SubGraph* subgraph = model->subgraphs()->Get(subgraph_idx); - TFLITE_DCHECK(subgraph != nullptr); - - size_t alloc_count = subgraph->tensors()->size(); - TfLiteEvalTensor* tensors = reinterpret_cast( - persistent_buffer_allocator_->AllocatePersistentBuffer( - sizeof(TfLiteEvalTensor) * alloc_count, alignof(TfLiteEvalTensor))); - if (tensors == nullptr) { - TF_LITE_REPORT_ERROR( - error_reporter_, - "Failed to allocate memory for context->eval_tensors, " - "%d bytes required", - sizeof(TfLiteEvalTensor) * alloc_count); - return kTfLiteError; - } - - for (size_t i = 0; i < alloc_count; ++i) { - TfLiteStatus status = internal::InitializeTfLiteEvalTensorFromFlatbuffer( - *subgraph->tensors()->Get(i), model->buffers(), error_reporter_, - &tensors[i]); - if (status != kTfLiteOk) { - TF_LITE_REPORT_ERROR(error_reporter_, "Failed to initialize tensor %d", - i); - return kTfLiteError; - } - } - subgraph_allocations[subgraph_idx].tensors = tensors; - } - return kTfLiteOk; -} - -TfLiteStatus MicroAllocator::AllocateVariables(const SubGraph* subgraph, - TfLiteEvalTensor* eval_tensors) { - for (size_t i = 0; i < subgraph->tensors()->size(); ++i) { - auto* tensor = subgraph->tensors()->Get(i); - if (tensor->is_variable()) { - size_t buffer_size; - TF_LITE_ENSURE_STATUS( - TfLiteEvalTensorByteLength(&eval_tensors[i], &buffer_size)); - - eval_tensors[i].data.data = - persistent_buffer_allocator_->AllocatePersistentBuffer( - buffer_size, MicroArenaBufferAlignment()); - - if (eval_tensors[i].data.data == nullptr) { - TF_LITE_REPORT_ERROR(error_reporter_, - "Failed to allocate variable tensor of size %d", - buffer_size); - return kTfLiteError; - } - } - } - return kTfLiteOk; -} - -TfLiteTensor* MicroAllocator::AllocatePersistentTfLiteTensorInternal() { - return reinterpret_cast( - persistent_buffer_allocator_->AllocatePersistentBuffer( - sizeof(TfLiteTensor), alignof(TfLiteTensor))); -} - -TfLiteStatus MicroAllocator::PopulateTfLiteTensorFromFlatbuffer( - const Model* model, TfLiteTensor* tensor, int tensor_index, - int subgraph_idx, bool allocate_temp) { - // TODO(b/162311891): This method serves as a stub to ensure quantized - // allocations in the tail can be recorded. Once the interpreter has APIs for - // accessing buffers on TfLiteEvalTensor this method can be dropped. - return internal::InitializeTfLiteTensorFromFlatbuffer( - persistent_buffer_allocator_, non_persistent_buffer_allocator_, - allocate_temp, - *model->subgraphs()->Get(subgraph_idx)->tensors()->Get(tensor_index), - model->buffers(), error_reporter_, tensor); -} - -ErrorReporter* MicroAllocator::error_reporter() const { - return error_reporter_; -} - -TfLiteStatus MicroAllocator::CommitStaticMemoryPlan( - const Model* model, SubgraphAllocations* allocations, - ScratchBufferHandle* scratch_buffer_handles) { - size_t head_usage = 0; - // Create static memory plan - // 1. Calculate AllocationInfo to know the lifetime of each tensor/buffer. - // 2. Add them into the planner (such as the GreedyMemoryPlanner). - // 3. Static memory planning using the planner. - // 4. Set tensor/buffer pointers based on the offsets from the previous step. - // - // Note that AllocationInfo is only needed for creating the plan. It will be - // allocated from the temp section and cleaned up at the bottom of this - // function. - - // Use the AllocationInfoBuilder class to help determine where buffers are - // used in the subgraph. - AllocationInfoBuilder builder(model, non_persistent_buffer_allocator_, - error_reporter_); - TF_LITE_ENSURE_STATUS( - builder.CreateAllocationInfo(scratch_buffer_request_count_)); - - const int32_t* offline_planner_offsets = nullptr; - TF_LITE_ENSURE_STATUS( - builder.GetOfflinePlannedOffsets(&offline_planner_offsets)); - TF_LITE_ENSURE_STATUS( - builder.InitializeAllocationInfo(offline_planner_offsets, allocations)); - - internal::ScratchBufferRequest* scratch_buffer_requests = - GetScratchBufferRequests(); - TF_LITE_ENSURE_STATUS(builder.MarkAllocationLifetimes( - 0, scratch_buffer_requests, scratch_buffer_handles, allocations)); - int allocation_info_count = builder.AllocationCount(); - AllocationInfo* allocation_info = builder.Finish(); - - // Remaining arena size that memory planner can use for calculating offsets. - size_t remaining_arena_size = - non_persistent_buffer_allocator_->GetAvailableMemory( - MicroArenaBufferAlignment()); - uint8_t* planner_arena = non_persistent_buffer_allocator_->AllocateTemp( - remaining_arena_size, MicroArenaBufferAlignment()); - TF_LITE_ENSURE(error_reporter_, planner_arena != nullptr); - memory_planner_->Init(planner_arena, remaining_arena_size); - TF_LITE_ENSURE_STATUS(CreatePlan(error_reporter_, memory_planner_, - allocation_info, allocation_info_count)); - - // Commit the plan. - TF_LITE_ENSURE_STATUS( - CommitPlan(error_reporter_, memory_planner_, - non_persistent_buffer_allocator_->GetOverlayMemoryAddress(), - allocation_info, allocation_info_count)); - - // Reset all temp allocations used above: - builder.FreeAllocationInfo(); - non_persistent_buffer_allocator_->DeallocateTemp(planner_arena); - TF_LITE_ENSURE_STATUS( - non_persistent_buffer_allocator_->ResetTempAllocations()); - TF_LITE_ENSURE_STATUS( - non_persistent_buffer_allocator_->DeallocateResizableBuffer( - scratch_buffer_head_)); - -#ifdef TF_LITE_SHOW_MEMORY_USE - memory_planner_->PrintMemoryPlan(); -#endif - head_usage = memory_planner_->GetMaximumMemorySize(); - - // The head is used to store memory plans for one model at a time during the - // model preparation stage, and is re-purposed to store scratch buffer handles - // during model invocation. The head must be as large as the greater of the - // largest model memory plan's size and the total space required for all - // scratch buffer handles. - if (max_head_buffer_usage_ < head_usage) { - max_head_buffer_usage_ = head_usage; - } - - // The head is used for storing scratch buffer allocations before finalizing a - // memory plan in this function. Ensure that the head is set to the largest - // memory plan sent through the allocator: - TF_LITE_ENSURE_STATUS( - non_persistent_buffer_allocator_->ReserveNonPersistentOverlayMemory( - max_head_buffer_usage_, MicroArenaBufferAlignment())); - return kTfLiteOk; -} - -TfLiteStatus MicroAllocator::AllocateScratchBufferHandles( - ScratchBufferHandle** scratch_buffer_handles, size_t handle_count) { - TFLITE_DCHECK(scratch_buffer_handles != nullptr); - - if (scratch_buffer_request_count_ == 0) { - // No scratch buffer requests were requested during model allocation. - return kTfLiteOk; - } - - // Allocate a consecutive block of memory store the scratch buffer handles. - // This alignment ensures quick lookup during inference time for the model: - *scratch_buffer_handles = reinterpret_cast( - persistent_buffer_allocator_->AllocatePersistentBuffer( - sizeof(ScratchBufferHandle) * handle_count, - alignof(ScratchBufferHandle))); - - return kTfLiteOk; -} - -TfLiteStatus MicroAllocator::InitScratchBufferData() { - // A model is preparing to allocate resources, ensure that scratch buffer - // request counter is cleared: - scratch_buffer_request_count_ = 0; - - // All requests will be stored in the head section. Each kernel is allowed at - // most kMaxScratchBuffersPerOp requests. Adjust the head to reserve at most - // that many requests to begin: - scratch_buffer_head_ = - non_persistent_buffer_allocator_->AllocateResizableBuffer( - sizeof(internal::ScratchBufferRequest) * kMaxScratchBuffersPerOp, - alignof(internal::ScratchBufferRequest)); - if (scratch_buffer_head_ == nullptr) { - return kTfLiteError; - } - - return kTfLiteOk; -} - -internal::ScratchBufferRequest* MicroAllocator::GetScratchBufferRequests() { - return reinterpret_cast(AlignPointerUp( - scratch_buffer_head_, alignof(internal::ScratchBufferRequest))); -} - -BuiltinDataAllocator* MicroAllocator::GetBuiltinDataAllocator() { - return builtin_data_allocator_; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/micro_allocator.h b/code/components/tflite-lib/tensorflow/lite/micro/micro_allocator.h deleted file mode 100644 index 7381489d..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/micro_allocator.h +++ /dev/null @@ -1,331 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_MICRO_MICRO_ALLOCATOR_H_ -#define TENSORFLOW_LITE_MICRO_MICRO_ALLOCATOR_H_ - -#include -#include - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/core/api/error_reporter.h" -#include "tensorflow/lite/core/api/flatbuffer_conversions.h" -#include "tensorflow/lite/micro/arena_allocator/single_arena_buffer_allocator.h" -#include "tensorflow/lite/micro/compatibility.h" -#include "tensorflow/lite/micro/flatbuffer_utils.h" -#include "tensorflow/lite/micro/memory_planner/micro_memory_planner.h" -#include "tensorflow/lite/schema/schema_generated.h" - -namespace tflite { - -// TODO(b/199402574): rename to tflite_internal or just remove internal -// namespace. -namespace internal { - -// Sets up all of the data structure members for a TfLiteTensor based on the -// contents of a serialized tensor in the flatbuffer. -// TODO(b/162311891): Drop this method when the interpreter has an API for -// returning buffers on TfLiteEvalTensor. -TfLiteStatus InitializeTfLiteTensorFromFlatbuffer( - IPersistentBufferAllocator* persistent_buffer_allocator, - INonPersistentBufferAllocator* non_persistent_buffer_allocator, - bool allocate_temp, const tflite::Tensor& flatbuffer_tensor, - const flatbuffers::Vector>* buffers, - ErrorReporter* error_reporter, TfLiteTensor* result); - -// Holds placeholder information for a scratch buffer request from a kernel. -// This struct is only used during the model prepare stage. Each request from a -// kernel is stored in the head section. During the prepare stage, the head -// section will at least hold kMaxScratchBuffersPerOp number of requests plus -// any requests from previous kernel requests. -// -// When the memory plan is finalized, these structs are no longer used in favor -// of a sequential, array of ScratchBufferHandle allocations in the tail -// section. These allocations are indexed by the request API defined in the -// TfLiteContext struct. -typedef struct { - // Number of bytes required by the buffer. The actual allocated size might be - // greater than `bytes` due to buffer alignment. - size_t bytes; - // Node where the buffer is allocated for. This provides useful information to - // determine the lifetime of the buffer. In AllocationInfo, this buffer will - // have `before` = node_idx and `after` = node_idx. - int node_idx; - int subgraph_idx; -} ScratchBufferRequest; - -} // namespace internal - -typedef struct { - TfLiteNode node; - const TfLiteRegistration* registration; -} NodeAndRegistration; - -// Holds a pointer to a buffer for a scratch buffer requested by a kernel during -// the model prepare stage. This struct is allocated in-place and allows for -// quick pointer-indexed lookup for speed during model inference. -typedef struct { - // Pointer to location of the scratch buffer: - uint8_t* data; -} ScratchBufferHandle; - -// Stores all per-subgraph allocations. This includes the node and registration -// array, tensor list and scratch buffer handles for each subgraph. -typedef struct { - NodeAndRegistration* node_and_registrations; - TfLiteEvalTensor* tensors; -} SubgraphAllocations; - -// Allocator responsible for allocating memory for all intermediate tensors -// necessary to invoke a model. -// -// The lifetime of the model, tensor arena and error reporter must be at -// least as long as that of the allocator object, since the allocator needs -// them to be accessible during its entire lifetime. -// -// The MicroAllocator simply plans out additional allocations that are required -// to standup a model for inference in TF Micro. This class currently relies on -// an additional allocator - SingleArenaBufferAllocator - for all allocations -// from an arena. These allocations are divided into head (non-persistent) and -// tail (persistent) regions: -// -// Memory layout to help understand how it works -// This information could change in the future version. -// ************** .memory_allocator->GetBuffer() -// Tensors/Scratch buffers (head) -// ************** .head_watermark -// unused memory -// ************** .memory_allocator->GetBuffer() + ->GetMaxBufferSize() -// - ->GetDataSize() -// persistent area (tail) -// ************** .memory_allocator->GetBuffer() + ->GetMaxBufferSize() -class MicroAllocator { - public: - // Creates a MicroAllocator instance from a given tensor arena. This arena - // will be managed by the created instance. The GreedyMemoryPlanner will - // by default be used and created on the arena. - // Note: Please use alignas(16) to make sure tensor_arena is 16 - // bytes aligned, otherwise some head room will be wasted. - // TODO(b/157615197): Cleanup constructor + factory usage. - static MicroAllocator* Create(uint8_t* tensor_arena, size_t arena_size, - ErrorReporter* error_reporter); - - // Creates a MicroAllocator instance from a given tensor arena and a given - // MemoryPlanner. This arena will be managed by the created instance. Note: - // Please use alignas(16) to make sure tensor_arena is 16 bytes - // aligned, otherwise some head room will be wasted. - static MicroAllocator* Create(uint8_t* tensor_arena, size_t arena_size, - MicroMemoryPlanner* memory_planner, - ErrorReporter* error_reporter); - - // Creates a MicroAllocator instance using the provided - // SingleArenaBufferAllocator instance and the MemoryPlanner. This allocator - // instance will use the SingleArenaBufferAllocator instance to manage - // allocations internally. - static MicroAllocator* Create(SingleArenaBufferAllocator* memory_allocator, - MicroMemoryPlanner* memory_planner, - ErrorReporter* error_reporter); - - // Creates a MicroAllocator instance using the provided - // SingleArenaBufferAllocator instance and the MemoryPlanner. This allocator - // instance will use the SingleArenaBufferAllocator instance to manage - // allocations internally. - static MicroAllocator* Create(uint8_t* persistent_tensor_arena, - size_t persistent_arena_size, - uint8_t* non_persistent_tensor_arena, - size_t non_persistent_arena_size, - ErrorReporter* error_reporter); - - // Returns the fixed amount of memory overhead of MicroAllocator. - static size_t GetDefaultTailUsage(bool is_memory_planner_given); - - // Allocates internal resources required for model inference for each subgraph - // from the arena. - // - // This method will run through the flatbuffer data supplied in the model to - // properly allocate tensor, node, and op registration data. This method is - // expected to be followed with a call to FinishModelAllocation() Returns a - // pointer to an array of SubgraphAllocations (also stored in the tail of the - // arena) where each index corresponds to a different subgraph in the model. - // Return value is nullptr if the allocations failed. - SubgraphAllocations* StartModelAllocation(const Model* model); - - // Finish allocating internal resources required for model inference. - // - // -Plan the memory for activation tensors and scratch buffers. - // -Update eval tensors for each subgraph based on planned offsets. - // -Allocate scratch buffer handles array and update based on planned offsets. - // - // This method should be called after assigning model resources - // in StartModelAllocation(). The subgraph_allocations pointer should be the - // value passed into this class during StartModelAllocation(). Scratch buffer - // handles are stored in the out-param `scratch_buffer_handles` array which is - // allocated in this method. This value will be used in `GetScratchBuffer` - // call to retrieve scratch buffers. - TfLiteStatus FinishModelAllocation( - const Model* model, SubgraphAllocations* subgraph_allocations, - ScratchBufferHandle** scratch_buffer_handles); - - // Allocates a TfLiteTensor struct and populates the returned value with - // properties from the model flatbuffer. This struct is allocated from - // persistent arena memory is only guaranteed for the lifetime of the - // application. The eval_tensors pointer should be the value passed into this - // class during StartModelAllocation() and contains the source-of-truth for - // buffers. - virtual TfLiteTensor* AllocatePersistentTfLiteTensor( - const Model* model, const SubgraphAllocations* subgraph_allocations, - int tensor_index, int subgraph_index); - - // Allocates a TfLiteTensor struct and populates the returned value with - // properties from the model flatbuffer. This struct is allocated from - // temporary arena memory is only guaranteed until a call is made to - // ResetTempAllocations(). Subgraph_allocaitons contains the array of - // TfLiteEvalTensors. If the newly allocated temp at the specified subgraph - // and tensor index is already present int the TfLiteEvalTensor array, its - // data buffer will be re-used. - virtual TfLiteTensor* AllocateTempTfLiteTensor( - const Model* model, const SubgraphAllocations* subgraph_allocations, - int tensor_index, int subgraph_index); - - virtual void DeallocateTempTfLiteTensor(TfLiteTensor*); - - // Resets all temporary allocations. This method should be called after a - // chain of temp allocations (e.g. chain of TfLiteTensor objects via - // AllocateTfLiteTensor()). - virtual TfLiteStatus ResetTempAllocations(); - - // Returns true if all temporary buffers including temp TfLiteTensor are - // already deallocated. - virtual bool IsAllTempDeallocated(); - - // Allocates persistent buffer which has the same life time as the allocator. - // The memory is immediately available and is allocated from the tail of the - // arena. - virtual void* AllocatePersistentBuffer(size_t bytes); - - // Register a scratch buffer of size `bytes` for Node with `node_id`. - // This method only requests a buffer with a given size to be used after a - // model has finished allocation via FinishModelAllocation(). All requested - // buffers will be accessible by the out-param in that method. - TfLiteStatus RequestScratchBufferInArena(size_t bytes, int subgraph_idx, - int* buffer_idx); - - // Finish allocating a specific NodeAndRegistration prepare block (kernel - // entry for a model) with a given node ID. This call ensures that any scratch - // buffer requests and temporary allocations are handled and ready for the - // next node prepare block. - TfLiteStatus FinishPrepareNodeAllocations(int node_id); - - // Returns the arena usage in bytes, only available after - // `FinishModelAllocation`. Otherwise, it will return 0. - size_t used_bytes() const; - - BuiltinDataAllocator* GetBuiltinDataAllocator(); - - protected: - MicroAllocator(SingleArenaBufferAllocator* memory_allocator, - MicroMemoryPlanner* memory_planner, - ErrorReporter* error_reporter); - MicroAllocator(IPersistentBufferAllocator* persistent_buffer_allocator, - INonPersistentBufferAllocator* non_persistent_buffer_allocator, - MicroMemoryPlanner* memory_planner, - ErrorReporter* error_reporter); - virtual ~MicroAllocator(); - - // Allocates an array in the arena to hold pointers to the node and - // registration pointers required to represent the inference graph of the - // model. - virtual TfLiteStatus AllocateNodeAndRegistrations( - const Model* model, SubgraphAllocations* subgraph_allocations); - - // Allocates the list of persistent TfLiteEvalTensors that are used for the - // "eval" phase of model inference. These structs will be the source of truth - // for all tensor buffers. - virtual TfLiteStatus AllocateTfLiteEvalTensors( - const Model* model, SubgraphAllocations* subgraph_allocations); - // Allocates persistent tensor buffers for variable tensors in the subgraph. - virtual TfLiteStatus AllocateVariables(const SubGraph* subgraph, - TfLiteEvalTensor* eval_tensors); - - // Allocate and return a persistent TfLiteTensor. - // TODO(b/162311891): Drop this method when the interpreter has an API for - // accessing TfLiteEvalTensor structs. - virtual TfLiteTensor* AllocatePersistentTfLiteTensorInternal(); - - // Populates a TfLiteTensor struct with data from the model flatbuffer. Any - // quantization data is allocated from either the tail (persistent) or temp - // sections of the arena based on the allocation flag. - virtual TfLiteStatus PopulateTfLiteTensorFromFlatbuffer(const Model* model, - TfLiteTensor* tensor, - int tensor_index, - int subgraph_idx, - bool allocate_temp); - - ErrorReporter* error_reporter() const; - - private: - // Commits a memory plan for all non-persistent buffer allocations in the - // 'head' section of the memory arena. The eval_tensors pointer is the list of - // pre-allocated TfLiteEvalTensor structs that will point to the buffers that - // will be allocated into the head section in this function call. The - // scratch_buffer_handles pointer is the array of pre-allocated - // ScratchBufferHandle structs that will point to allocated buffers also in - // the head section. - virtual TfLiteStatus CommitStaticMemoryPlan( - const Model* model, SubgraphAllocations* allocations, - ScratchBufferHandle* scratch_buffer_handles); - - // Allocates an array of ScratchBufferHandle structs in the tail section for a - // given number of handles. - virtual TfLiteStatus AllocateScratchBufferHandles( - ScratchBufferHandle** scratch_buffer_handles, size_t handle_count); - - // Clears all internal scratch buffer request counts and resets the head to - // prepare for kernels to request scratch buffer data when a model is - // preparing. - TfLiteStatus InitScratchBufferData(); - - // Returns the pointer for the array of ScratchBufferRequest allocations in - // the head section. - internal::ScratchBufferRequest* GetScratchBufferRequests(); - - // A simple memory allocator that always allocate from the arena tail or head. - INonPersistentBufferAllocator* non_persistent_buffer_allocator_; - IPersistentBufferAllocator* persistent_buffer_allocator_; - - // Allocator used to allocate persistent builtin data. - BuiltinDataAllocator* builtin_data_allocator_; - - // Activation buffer memory planner. - MicroMemoryPlanner* memory_planner_; - - ErrorReporter* error_reporter_; - bool model_is_allocating_; - - // Holds the number of ScratchBufferRequest instances stored in the head - // section when a model is allocating. - size_t scratch_buffer_request_count_ = 0; - - // Holds ScratchBufferRequest when a model is allocating - uint8_t* scratch_buffer_head_ = nullptr; - - // Holds the byte length of the memory plan with the largest head usage. Used - // to ensure that multi-tenant allocations can share the head for buffers. - size_t max_head_buffer_usage_ = 0; - - TF_LITE_REMOVE_VIRTUAL_DELETE -}; - -} // namespace tflite -#endif // TENSORFLOW_LITE_MICRO_MICRO_ALLOCATOR_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/micro_arena_constants.h b/code/components/tflite-lib/tensorflow/lite/micro/micro_arena_constants.h deleted file mode 100644 index 82828176..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/micro_arena_constants.h +++ /dev/null @@ -1,28 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_MICRO_ARENA_CONSTANTS_H_ -#define TENSORFLOW_LITE_MICRO_MICRO_ARENA_CONSTANTS_H_ - -namespace tflite { - -// The default buffer alignment requirement. -// We align tensor buffers to 16-byte boundaries, since this is a common -// requirement for SIMD extensions. -constexpr int MicroArenaBufferAlignment() { return 16; } - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_MICRO_ARENA_CONSTANTS_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/micro_context.cc b/code/components/tflite-lib/tensorflow/lite/micro/micro_context.cc deleted file mode 100644 index 9ec694b8..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/micro_context.cc +++ /dev/null @@ -1,129 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/micro_context.h" - -#include -#include -#include - -#include "tensorflow/lite/micro/micro_error_reporter.h" - -namespace tflite { -MicroContext::MicroContext(MicroAllocator* allocator, const Model* model, - MicroGraph* graph) - : allocator_(*allocator), graph_(*graph), model_(model) {} - -MicroContext::~MicroContext() {} - -void* MicroContext::AllocatePersistentBuffer(size_t bytes) { - return allocator_.AllocatePersistentBuffer(bytes); -} - -TfLiteStatus MicroContext::RequestScratchBufferInArena(size_t bytes, - int* buffer_idx) { - return allocator_.RequestScratchBufferInArena( - bytes, graph_.GetCurrentSubgraphIndex(), buffer_idx); -} - -void* MicroContext::GetScratchBuffer(int buffer_idx) { - ScratchBufferHandle* handle = scratch_buffer_handles_ + buffer_idx; - return handle->data; -} - -TfLiteTensor* MicroContext::AllocateTempTfLiteTensor(int tensor_idx) { - return allocator_.AllocateTempTfLiteTensor(model_, graph_.GetAllocations(), - tensor_idx, - graph_.GetCurrentSubgraphIndex()); -} - -int MicroContext::GetTensorIndex(int index, int max_size, - const int* tensor_indices) { - if (index >= 0 && index < max_size) { - const int tensor_index = tensor_indices[index]; - if (tensor_index != kTfLiteOptionalTensor) { - return tensor_index; - } - } - return -1; -} - -TfLiteTensor* MicroContext::AllocateTempInputTensor(const TfLiteNode* node, - int index) { - const int tensor_index = - GetTensorIndex(index, node->inputs->size, node->inputs->data); - if (tensor_index < 0) { - return nullptr; - } - return AllocateTempTfLiteTensor(tensor_index); -} - -TfLiteTensor* MicroContext::AllocateTempOutputTensor(const TfLiteNode* node, - int index) { - const int tensor_index = - GetTensorIndex(index, node->outputs->size, node->outputs->data); - if (tensor_index < 0) { - return nullptr; - } - return AllocateTempTfLiteTensor(tensor_index); -} - -TfLiteTensor* MicroContext::AllocateTempIntermediateTensor( - const TfLiteNode* node, int index) { - const int tensor_index = GetTensorIndex(index, node->intermediates->size, - node->intermediates->data); - if (tensor_index < 0) { - return nullptr; - } - return AllocateTempTfLiteTensor(tensor_index); -} - -void MicroContext::DeallocateTempTfLiteTensor(TfLiteTensor* tensor) { - return allocator_.DeallocateTempTfLiteTensor(tensor); -} - -TfLiteEvalTensor* MicroContext::GetEvalTensor(int tensor_idx) { - return &graph_.GetAllocations()[graph_.GetCurrentSubgraphIndex()] - .tensors[tensor_idx]; -} - -void MicroContext::SetScratchBufferHandles( - ScratchBufferHandle* scratch_buffer_handles) { - scratch_buffer_handles_ = scratch_buffer_handles; -} - -TfLiteStatus MicroContext::set_external_context( - void* external_context_payload) { - if (external_context_payload == nullptr || - external_context_payload_ != nullptr) { - MicroPrintf( - "Attempting to set external context to %x but it was %x already", - external_context_payload, external_context_payload_); - return kTfLiteError; - } - - external_context_payload_ = external_context_payload; - return kTfLiteOk; -} - -void MicroContextReportOpError(struct TfLiteContext* context, - const char* format, ...) { - va_list args; - va_start(args, format); - GetMicroErrorReporter()->Report(format, args); - va_end(args); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/micro_context.h b/code/components/tflite-lib/tensorflow/lite/micro/micro_context.h deleted file mode 100644 index e7be6544..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/micro_context.h +++ /dev/null @@ -1,161 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_MICRO_CONTEXT_H_ -#define TENSORFLOW_LITE_MICRO_MICRO_CONTEXT_H_ - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/micro/micro_allocator.h" -#include "tensorflow/lite/micro/micro_graph.h" - -namespace tflite { -// MicroContext is eventually going to become the API between TFLM and the -// kernels, replacing all the functions in TfLiteContext. The end state is code -// kernels to have code like: -// -// MicroContext* micro_context = GetMicroContext(context); -// micro_context-> -class MicroContext { - public: - // Does not take any ownership, and all pointers must refer to valid objects - // that outlive the one constructed. - explicit MicroContext(MicroAllocator* allocator, const Model* model, - MicroGraph* graph); - virtual ~MicroContext(); - - // Allocate persistent buffer which has the same life time as the interpreter. - // Returns nullptr on failure. - // The memory is allocated from the tail. - // This method is only available in Init or Prepare stage. - // Virtual so that it can be faked for kernel tests. - virtual void* AllocatePersistentBuffer(size_t bytes); - - // Request a scratch buffer in the arena through static memory planning. - // This method is only available in Prepare stage and the buffer is allocated - // by the interpreter between Prepare and Eval stage. In Eval stage, - // GetScratchBuffer API can be used to fetch the address. - // Virtual so that it can be faked for kernel tests. - virtual TfLiteStatus RequestScratchBufferInArena(size_t bytes, - int* buffer_idx); - - // Get the scratch buffer pointer. - // This method is only available in Eval stage. - // Virtual so that it can be faked for kernel tests. - virtual void* GetScratchBuffer(int buffer_idx); - - // Returns a temporary TfLiteTensor struct for a given index. - // Virtual so that it can be faked for kernel tests. - virtual TfLiteTensor* AllocateTempTfLiteTensor(int tensor_idx); - - // Returns a temporary TfLiteTensor struct for the specified input tensor of a - // given mode. This is the recommended API over the deprecated - // GetInput/GetInputSafe to get a temp input tensor. The returned tensor shall - // be freed via calling DeallocateTempTfLiteTensor. - virtual TfLiteTensor* AllocateTempInputTensor(const TfLiteNode* node, - int index); - - // Returns a temporary TfLiteTensor struct for the specified output tensor of - // a given mode. This is the recommended API over the deprecated - // GetOutput/GetOutputSafe to get a temp output tensor. The returned tensor - // shall be freed via calling DeallocateTempTfLiteTensor. - virtual TfLiteTensor* AllocateTempOutputTensor(const TfLiteNode* node, - int index); - - // Returns a temporary TfLiteTensor struct for the specified intermediate - // tensor of a given mode. This is the recommended API over the deprecated - // GetIntermediates/GetIntermediatesSafe to get a temp intermediate tensor. - // The returned tensor shall be freed via calling DeallocateTempTfLiteTensor. - virtual TfLiteTensor* AllocateTempIntermediateTensor(const TfLiteNode* node, - int index); - - // Deallocates a temp TfLiteTensor. - // Virtual so that it can be faked for kernel tests. - virtual void DeallocateTempTfLiteTensor(TfLiteTensor* tensor); - - // Returns a TfLiteEvalTensor struct for a given index. - // Virtual so that it can be faked for kernel tests. - virtual TfLiteEvalTensor* GetEvalTensor(int tensor_idx); - - // Does not take ownership of the pointer and the pointer must refer to valid - // an object that outlive this class instance. - // This can only be called once to set one external context. - TfLiteStatus set_external_context(void* external_context_payload); - - void* external_context() { return external_context_payload_; } - - MicroGraph& graph() { return graph_; } - - // Sets the pointer to a list of ScratchBufferHandle instances. - // Not API between TFLM and kernels. Primarily used by the framework for - // housekeeping in MicroContext. - void SetScratchBufferHandles(ScratchBufferHandle* scratch_buffer_handles); - - private: - // Return the tensor index as tensor_indices[index]. tensor_indices is of - // max_size. Return -1 if index is not in the valid range of tensor_indices. - int GetTensorIndex(int index, int max_size, const int* tensor_indices); - - MicroAllocator& allocator_; - MicroGraph& graph_; - const Model* model_; - - ScratchBufferHandle* scratch_buffer_handles_ = nullptr; - void* external_context_payload_ = nullptr; - - TF_LITE_REMOVE_VIRTUAL_DELETE -}; - -inline MicroContext* GetMicroContext(const struct TfLiteContext* context) { - return reinterpret_cast(context->impl_); -} - -// Deprecated API. Prefer to using the MicroContext API directly from the -// kernels. -// TODO(b/213010668): migrate all existing kernels to use MicroContext, delete -// these functions, and remove corresponding members from the TfLiteContext -// struct for TFLM. -inline void* MicroContextAllocatePersistentBuffer(TfLiteContext* ctx, - size_t bytes) { - return GetMicroContext(ctx)->AllocatePersistentBuffer(bytes); -} -inline TfLiteStatus MicroContextRequestScratchBufferInArena(TfLiteContext* ctx, - size_t bytes, - int* buffer_idx) { - return GetMicroContext(ctx)->RequestScratchBufferInArena(bytes, buffer_idx); -} -inline void* MicroContextGetScratchBuffer(TfLiteContext* ctx, int buffer_idx) { - return GetMicroContext(ctx)->GetScratchBuffer(buffer_idx); -} -inline TfLiteTensor* MicroContextGetTensor(const struct TfLiteContext* context, - int tensor_idx) { - return GetMicroContext(context)->AllocateTempTfLiteTensor(tensor_idx); -} -inline TfLiteEvalTensor* MicroContextGetEvalTensor( - const struct TfLiteContext* context, int tensor_idx) { - return GetMicroContext(context)->GetEvalTensor(tensor_idx); -} -inline TfLiteExternalContext* MicroContextGetExternalContext( - TfLiteContext* context, TfLiteExternalContextType unused) { - return reinterpret_cast( - GetMicroContext(context)->external_context()); -} - -// Requests that an error be reported with format string msg. -void MicroContextReportOpError(struct TfLiteContext* context, - const char* format, ...); - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_MICRO_CONTEXT_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/micro_error_reporter.cc b/code/components/tflite-lib/tensorflow/lite/micro/micro_error_reporter.cc deleted file mode 100644 index 5aba058d..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/micro_error_reporter.cc +++ /dev/null @@ -1,68 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/micro_error_reporter.h" - -#include -#include -#include - -#if !defined(TF_LITE_STRIP_ERROR_STRINGS) -#include "tensorflow/lite/micro/debug_log.h" -#include "tensorflow/lite/micro/micro_string.h" -#endif - -namespace { -uint8_t micro_error_reporter_buffer[sizeof(tflite::MicroErrorReporter)]; -tflite::MicroErrorReporter* error_reporter_ = nullptr; - -void Log(const char* format, va_list args) { -#if !defined(TF_LITE_STRIP_ERROR_STRINGS) - // Only pulling in the implementation of this function for builds where we - // expect to make use of it to be extra cautious about not increasing the code - // size. - static constexpr int kMaxLogLen = 256; - char log_buffer[kMaxLogLen]; - MicroVsnprintf(log_buffer, kMaxLogLen, format, args); - DebugLog(log_buffer); - DebugLog("\r\n"); -#endif -} - -} // namespace - -#if !defined(TF_LITE_STRIP_ERROR_STRINGS) -void MicroPrintf(const char* format, ...) { - va_list args; - va_start(args, format); - Log(format, args); - va_end(args); -} -#endif - -namespace tflite { -ErrorReporter* GetMicroErrorReporter() { - if (error_reporter_ == nullptr) { - error_reporter_ = new (micro_error_reporter_buffer) MicroErrorReporter(); - } - return error_reporter_; -} - -int MicroErrorReporter::Report(const char* format, va_list args) { - Log(format, args); - return 0; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/micro_error_reporter.h b/code/components/tflite-lib/tensorflow/lite/micro/micro_error_reporter.h deleted file mode 100644 index 0e3b0c38..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/micro_error_reporter.h +++ /dev/null @@ -1,56 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_MICRO_MICRO_ERROR_REPORTER_H_ -#define TENSORFLOW_LITE_MICRO_MICRO_ERROR_REPORTER_H_ - -#include - -#include "tensorflow/lite/core/api/error_reporter.h" -#include "tensorflow/lite/micro/compatibility.h" - -#if !defined(TF_LITE_STRIP_ERROR_STRINGS) -// This function can be used independent of the MicroErrorReporter to get -// printf-like functionalitys and are common to all target platforms. -void MicroPrintf(const char* format, ...); -#else -// We use a #define to ensure that the strings are completely stripped, to -// prevent an unnecessary increase in the binary size. -#define MicroPrintf(...) tflite::Unused(__VA_ARGS__) -#endif - -namespace tflite { - -// From -// https://stackoverflow.com/questions/23235910/variadic-unused-function-macro -template -void Unused(Args&&... args) { - (void)(sizeof...(args)); -} - -// Get a pointer to a singleton global error reporter. -ErrorReporter* GetMicroErrorReporter(); - -class MicroErrorReporter : public ErrorReporter { - public: - ~MicroErrorReporter() override {} - int Report(const char* format, va_list args) override; - - private: - TF_LITE_REMOVE_VIRTUAL_DELETE -}; - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_MICRO_ERROR_REPORTER_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/micro_graph.cc b/code/components/tflite-lib/tensorflow/lite/micro/micro_graph.cc deleted file mode 100644 index d9b2176e..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/micro_graph.cc +++ /dev/null @@ -1,248 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/micro_graph.h" - -#include "flatbuffers/flatbuffers.h" // from @flatbuffers -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/micro/flatbuffer_utils.h" -#include "tensorflow/lite/micro/memory_helpers.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" -#include "tensorflow/lite/micro/micro_profiler.h" -#include "tensorflow/lite/schema/schema_generated.h" - -namespace tflite { -namespace { - -const char* OpNameFromRegistration(const TfLiteRegistration* registration) { - if (registration->builtin_code == BuiltinOperator_CUSTOM) { - return registration->custom_name; - } else { - return EnumNameBuiltinOperator(BuiltinOperator(registration->builtin_code)); - } -} - -} // namespace - -MicroGraph::MicroGraph(TfLiteContext* context, const Model* model, - MicroAllocator* allocator, - MicroResourceVariables* resource_variables) - : context_(context), - model_(model), - allocator_(allocator), - current_subgraph_index_(0), - resource_variables_(resource_variables) { - if (model != nullptr) { - subgraphs_ = model->subgraphs(); - } -} - -MicroGraph::~MicroGraph() {} - -TfLiteStatus MicroGraph::InitSubgraphs() { - int previous_subgraph_idx = current_subgraph_index_; - - for (size_t subgraph_idx = 0; subgraph_idx < subgraphs_->size(); - subgraph_idx++) { - current_subgraph_index_ = subgraph_idx; - uint32_t operators_size = NumSubgraphOperators(model_, subgraph_idx); - for (size_t i = 0; i < operators_size; ++i) { - TfLiteNode* node = - &(subgraph_allocations_[subgraph_idx].node_and_registrations[i].node); - const TfLiteRegistration* registration = - subgraph_allocations_[subgraph_idx] - .node_and_registrations[i] - .registration; - size_t init_data_size; - const char* init_data; - if (registration->builtin_code == BuiltinOperator_CUSTOM) { - init_data = reinterpret_cast(node->custom_initial_data); - init_data_size = node->custom_initial_data_size; - } else { - init_data = reinterpret_cast(node->builtin_data); - init_data_size = 0; - } - if (registration->init) { - node->user_data = - registration->init(context_, init_data, init_data_size); - } - } - } - current_subgraph_index_ = previous_subgraph_idx; - - return kTfLiteOk; -} - -TfLiteStatus MicroGraph::PrepareSubgraphs() { - int previous_subgraph_idx = current_subgraph_index_; - - for (size_t subgraph_idx = 0; subgraph_idx < subgraphs_->size(); - subgraph_idx++) { - current_subgraph_index_ = subgraph_idx; - uint32_t operators_size = NumSubgraphOperators(model_, subgraph_idx); - for (size_t i = 0; i < operators_size; ++i) { - TfLiteNode* node = - &(subgraph_allocations_[subgraph_idx].node_and_registrations[i].node); - const TfLiteRegistration* registration = - subgraph_allocations_[subgraph_idx] - .node_and_registrations[i] - .registration; - if (registration->prepare != nullptr) { - TfLiteStatus prepare_status = registration->prepare(context_, node); - if (prepare_status != kTfLiteOk) { - MicroPrintf("Node %s (number %df) failed to prepare with status %d", - OpNameFromRegistration(registration), i, prepare_status); - return kTfLiteError; - } - } - allocator_->FinishPrepareNodeAllocations(/*node_id=*/i); - } - } - current_subgraph_index_ = previous_subgraph_idx; - - return kTfLiteOk; -} - -TfLiteStatus MicroGraph::FreeSubgraphs() { - int previous_subgraph_idx = current_subgraph_index_; - - for (size_t subgraph_idx = 0; subgraph_idx < subgraphs_->size(); - subgraph_idx++) { - current_subgraph_index_ = subgraph_idx; - uint32_t operators_size = NumSubgraphOperators(model_, subgraph_idx); - for (size_t i = 0; i < operators_size; ++i) { - TfLiteNode* node = - &(subgraph_allocations_[subgraph_idx].node_and_registrations[i].node); - const TfLiteRegistration* registration = - subgraph_allocations_[subgraph_idx] - .node_and_registrations[i] - .registration; - // registration is allocated outside the interpreter, so double check to - // make sure it's not nullptr; - if (registration != nullptr && registration->free != nullptr) { - registration->free(context_, node->user_data); - } - } - } - current_subgraph_index_ = previous_subgraph_idx; - - return kTfLiteOk; -} - -TfLiteStatus MicroGraph::InvokeSubgraph(int subgraph_idx) { - int previous_subgraph_idx = current_subgraph_index_; - current_subgraph_index_ = subgraph_idx; - - if (static_cast(subgraph_idx) >= subgraphs_->size()) { - MicroPrintf("Accessing subgraph %d but only %d subgraphs found", - subgraph_idx, subgraphs_->size()); - return kTfLiteError; - } - uint32_t operators_size = NumSubgraphOperators(model_, subgraph_idx); - for (size_t i = 0; i < operators_size; ++i) { - TfLiteNode* node = - &(subgraph_allocations_[subgraph_idx].node_and_registrations[i].node); - const TfLiteRegistration* registration = subgraph_allocations_[subgraph_idx] - .node_and_registrations[i] - .registration; - -// This ifdef is needed (even though ScopedMicroProfiler itself is a no-op with -// -DTF_LITE_STRIP_ERROR_STRINGS) because the function OpNameFromRegistration is -// only defined for builds with the error strings. -#if !defined(TF_LITE_STRIP_ERROR_STRINGS) - ScopedMicroProfiler scoped_profiler( - OpNameFromRegistration(registration), - reinterpret_cast(context_->profiler)); -#endif - - TFLITE_DCHECK(registration->invoke); - TfLiteStatus invoke_status = registration->invoke(context_, node); - - // All TfLiteTensor structs used in the kernel are allocated from temp - // memory in the allocator. This creates a chain of allocations in the - // temp section. The call below resets the chain of allocations to - // prepare for the next call. - allocator_->ResetTempAllocations(); - - if (invoke_status == kTfLiteError) { - MicroPrintf("Node %s (number %d) failed to invoke with status %d", - OpNameFromRegistration(registration), i, invoke_status); - return kTfLiteError; - } else if (invoke_status != kTfLiteOk) { - return invoke_status; - } - } - current_subgraph_index_ = previous_subgraph_idx; - return kTfLiteOk; -} - -TfLiteStatus MicroGraph::ResetVariableTensors() { - for (size_t subgraph_idx = 0; subgraph_idx < subgraphs_->size(); - subgraph_idx++) { - const SubGraph* subgraph = (*subgraphs_)[subgraph_idx]; - for (size_t i = 0; i < subgraph->tensors()->size(); ++i) { - auto* tensor = subgraph->tensors()->Get(i); - if (tensor->is_variable()) { - size_t buffer_size; - TF_LITE_ENSURE_STATUS(TfLiteEvalTensorByteLength( - &subgraph_allocations_[subgraph_idx].tensors[i], &buffer_size)); - - int value = 0; - if (tensor->type() == tflite::TensorType_INT8) { - value = tensor->quantization()->zero_point()->Get(0); - } - memset(subgraph_allocations_[subgraph_idx].tensors[i].data.raw, value, - buffer_size); - } - } - } - if (resource_variables_ != nullptr) { - resource_variables_->ResetAll(); - } - - return kTfLiteOk; -} - -int MicroGraph::NumSubgraphs() { return model_->subgraphs()->size(); } - -void MicroGraph::SetSubgraphAllocations( - SubgraphAllocations* subgraph_allocations) { - subgraph_allocations_ = subgraph_allocations; -} - -size_t MicroGraph::NumSubgraphInputs(int subgraph_idx) { - return model_->subgraphs()->Get(subgraph_idx)->inputs()->size(); -} - -TfLiteEvalTensor* MicroGraph::GetSubgraphInput(int subgraph_idx, - int input_idx) { - int tensor_idx = - model_->subgraphs()->Get(subgraph_idx)->inputs()->Get(input_idx); - return &subgraph_allocations_[subgraph_idx].tensors[tensor_idx]; -} - -size_t MicroGraph::NumSubgraphOutputs(int subgraph_idx) { - return model_->subgraphs()->Get(subgraph_idx)->outputs()->size(); -} - -TfLiteEvalTensor* MicroGraph::GetSubgraphOutput(int subgraph_idx, - int output_idx) { - int tensor_idx = - model_->subgraphs()->Get(subgraph_idx)->outputs()->Get(output_idx); - return &subgraph_allocations_[subgraph_idx].tensors[tensor_idx]; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/micro_graph.h b/code/components/tflite-lib/tensorflow/lite/micro/micro_graph.h deleted file mode 100644 index 942082ac..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/micro_graph.h +++ /dev/null @@ -1,104 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_MICRO_GRAPH_H_ -#define TENSORFLOW_LITE_MICRO_MICRO_GRAPH_H_ - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/micro/micro_allocator.h" -#include "tensorflow/lite/micro/micro_resource_variable.h" -#include "tensorflow/lite/schema/schema_generated.h" - -namespace tflite { - -// Abstracts the details of interacting with the tflite::Model. -// -// Provides methods to access, initialize, prepare, invoke and free any -// subgraph in the tflite::Graph. -class MicroGraph { - public: - // The lifetime of the context, model, allocator and resource_variables must - // be at least as long as that of the graph object, since the this class may - // need to access them at any time. If resource_variables is a nullptr, - // GetResourceVariables will return a nullptr. - MicroGraph(TfLiteContext* context, const Model* model, - MicroAllocator* allocator, - MicroResourceVariables* resource_variables); - virtual ~MicroGraph(); - - // Sets up builtin data and calls TfLiteRegistration->Init for every operator - // in every subgraph in the model. - virtual TfLiteStatus InitSubgraphs(); - - // Calls TfLiteRegistration->Prepare for every operator in every subgraph in - // the model. - virtual TfLiteStatus PrepareSubgraphs(); - - // Calls TfLiteRegistration->Free for every operator in every subgraph in the - // model. - virtual TfLiteStatus FreeSubgraphs(); - - // Calls TfLiteRegistration->Invoke for every operator in a single subgraph in - // the model. - virtual TfLiteStatus InvokeSubgraph(int subgraph_idx); - - // Zeros out all variable tensors in all subgraphs in the model. - virtual TfLiteStatus ResetVariableTensors(); - - // Number of tensor inputs to a specified subgraph in the model. - virtual size_t NumSubgraphInputs(int subgraph_idx); - - // Get the specified input tensor of a specified subgraph in the model. - virtual TfLiteEvalTensor* GetSubgraphInput(int subgraph_idx, int input_idx); - - // Number of tensor outputs from a specified subgraph in the model. - virtual size_t NumSubgraphOutputs(int subgraph_idx); - - // Get the specified output tensor of a specified subgraph in the model. - virtual TfLiteEvalTensor* GetSubgraphOutput(int subgraph_idx, int output_idx); - - // Number of subgraphs in the model. - virtual int NumSubgraphs(); - - // Hook to pass in subgraph allocations tracked within the interpreter, - // allowing MicroGraph to init / prepare / invoke subgraphs in the model. - void SetSubgraphAllocations(SubgraphAllocations* subgraph_allocations); - - // Get the current subgraph index. Within an on operator, this is guaranteed - // to be the subgraph of that operator. - int GetCurrentSubgraphIndex() { return current_subgraph_index_; } - - // Gets the list of alloctions for each subgraph. This is the source of truth - // for all per-subgraph allocation data. - SubgraphAllocations* GetAllocations() { return subgraph_allocations_; } - - // Get the resource variables for this TFLM graph. - MicroResourceVariables* GetResourceVariables() { return resource_variables_; } - - private: - TfLiteContext* context_; - const Model* model_; - MicroAllocator* allocator_; - SubgraphAllocations* subgraph_allocations_ = nullptr; - int current_subgraph_index_; - MicroResourceVariables* resource_variables_; - const flatbuffers::Vector>* subgraphs_; - - TF_LITE_REMOVE_VIRTUAL_DELETE -}; - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_MICRO_GRAPH_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/micro_interpreter.cc b/code/components/tflite-lib/tensorflow/lite/micro/micro_interpreter.cc deleted file mode 100644 index f726a5f3..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/micro_interpreter.cc +++ /dev/null @@ -1,330 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/micro/micro_interpreter.h" - -#include -#include -#include - -#include "flatbuffers/flatbuffers.h" // from @flatbuffers -#include "tensorflow/lite/c/c_api_types.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/core/api/error_reporter.h" -#include "tensorflow/lite/core/api/tensor_utils.h" -#include "tensorflow/lite/micro/flatbuffer_utils.h" -#include "tensorflow/lite/micro/memory_helpers.h" -#include "tensorflow/lite/micro/micro_allocator.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" -#include "tensorflow/lite/micro/micro_op_resolver.h" -#include "tensorflow/lite/micro/micro_profiler.h" -#include "tensorflow/lite/schema/schema_generated.h" -#include "tensorflow/lite/schema/schema_utils.h" - -namespace tflite { - -MicroInterpreter::MicroInterpreter(const Model* model, - const MicroOpResolver& op_resolver, - uint8_t* tensor_arena, - size_t tensor_arena_size, - ErrorReporter* error_reporter, - MicroResourceVariables* resource_variables, - MicroProfiler* profiler) - : model_(model), - op_resolver_(op_resolver), - error_reporter_(error_reporter), - allocator_(*MicroAllocator::Create(tensor_arena, tensor_arena_size, - error_reporter)), - - graph_(&context_, model, &allocator_, resource_variables), - tensors_allocated_(false), - initialization_status_(kTfLiteError), - input_tensors_(nullptr), - output_tensors_(nullptr), - micro_context_(&allocator_, model_, &graph_) { - Init(profiler); -} - -MicroInterpreter::MicroInterpreter(const Model* model, - const MicroOpResolver& op_resolver, - MicroAllocator* allocator, - ErrorReporter* error_reporter, - MicroResourceVariables* resource_variables, - MicroProfiler* profiler) - : model_(model), - op_resolver_(op_resolver), - error_reporter_(error_reporter), - allocator_(*allocator), - graph_(&context_, model, allocator, resource_variables), - tensors_allocated_(false), - initialization_status_(kTfLiteError), - input_tensors_(nullptr), - output_tensors_(nullptr), - micro_context_(&allocator_, model_, &graph_) { - Init(profiler); -} - -MicroInterpreter::~MicroInterpreter() { - if (graph_.GetAllocations() != nullptr) { - graph_.FreeSubgraphs(); - } -} - -void MicroInterpreter::Init(MicroProfiler* profiler) { - context_.impl_ = static_cast(µ_context_); - context_.ReportError = MicroContextReportOpError; - context_.GetTensor = MicroContextGetTensor; - context_.GetEvalTensor = MicroContextGetEvalTensor; - context_.profiler = profiler; - - initialization_status_ = kTfLiteOk; -} - -TfLiteStatus MicroInterpreter::PrepareNodeAndRegistrationDataFromFlatbuffer() { - for (int subgraph_idx = 0; subgraph_idx < graph_.NumSubgraphs(); - subgraph_idx++) { - const SubGraph* subgraph = model_->subgraphs()->Get(subgraph_idx); - TFLITE_DCHECK(subgraph != nullptr); - - auto* opcodes = model_->operator_codes(); - BuiltinDataAllocator* builtin_data_allocator = - allocator_.GetBuiltinDataAllocator(); - uint32_t operators_size = NumSubgraphOperators(subgraph); - for (size_t i = 0; i < operators_size; ++i) { - const auto* op = subgraph->operators()->Get(i); - const size_t index = op->opcode_index(); - if (index >= opcodes->size()) { - MicroPrintf("Missing registration for opcode_index %d\n", index); - return kTfLiteError; - } - const auto* opcode = opcodes->Get(index); - TfLiteStatus status = - GetRegistrationFromOpCode(opcode, op_resolver_, error_reporter_, - &(graph_.GetAllocations()[subgraph_idx] - .node_and_registrations[i] - .registration)); - if (status != kTfLiteOk) { - MicroPrintf("Failed to get registration from op code %s\n ", - EnumNameBuiltinOperator(GetBuiltinCode(opcode))); - return status; - } - const auto* registration = graph_.GetAllocations()[subgraph_idx] - .node_and_registrations[i] - .registration; - if (registration == nullptr) { - MicroPrintf("Skipping op for opcode_index %d\n", index); - return kTfLiteError; - } - BuiltinOperator op_type = - static_cast(registration->builtin_code); - - const char* custom_data = nullptr; - size_t custom_data_size = 0; - unsigned char* builtin_data = nullptr; - - if (op_type == BuiltinOperator_CUSTOM) { - // Custom Ops may or may not have a non-null custom_options field. - if (op->custom_options() != nullptr) { - custom_data = - reinterpret_cast(op->custom_options()->data()); - custom_data_size = op->custom_options()->size(); - } - } else { - if (op->custom_options() != nullptr) { - MicroPrintf( - "Unsupported behavior: found builtin operator %s with custom " - "options.\n", - EnumNameBuiltinOperator(op_type)); - return kTfLiteError; - } - - MicroOpResolver::BuiltinParseFunction parser = - op_resolver_.GetOpDataParser(op_type); - if (parser == nullptr) { - MicroPrintf("Did not find a parser for %s", - EnumNameBuiltinOperator(op_type)); - - return kTfLiteError; - } - TF_LITE_ENSURE_STATUS(parser(op, error_reporter_, - builtin_data_allocator, - (void**)(&builtin_data))); - } - - TfLiteIntArray* inputs_array = - FlatBufferVectorToTfLiteTypeArray(op->inputs()); - TfLiteIntArray* outputs_array = - FlatBufferVectorToTfLiteTypeArray(op->outputs()); - - TfLiteNode* node = &( - graph_.GetAllocations()[subgraph_idx].node_and_registrations[i].node); - *node = {}; - node->inputs = inputs_array; - node->outputs = outputs_array; - node->builtin_data = reinterpret_cast(builtin_data); - node->custom_initial_data = custom_data; - node->custom_initial_data_size = custom_data_size; - - if (op->intermediates() && (op->intermediates()->size() > 0)) { - node->intermediates = - FlatBufferVectorToTfLiteTypeArray(op->intermediates()); - } - } - } - return kTfLiteOk; -} - -TfLiteStatus MicroInterpreter::AllocateTensors() { - SubgraphAllocations* allocations = allocator_.StartModelAllocation(model_); - - if (allocations == nullptr) { - TF_LITE_REPORT_ERROR(error_reporter_, - "Failed starting model allocation.\n"); - initialization_status_ = kTfLiteError; - return kTfLiteError; - } - - graph_.SetSubgraphAllocations(allocations); - - TF_LITE_ENSURE_STATUS(PrepareNodeAndRegistrationDataFromFlatbuffer()); - - // Only allow AllocatePersistentBuffer in Init stage. - context_.AllocatePersistentBuffer = MicroContextAllocatePersistentBuffer; - context_.RequestScratchBufferInArena = nullptr; - context_.GetScratchBuffer = nullptr; - context_.GetExternalContext = nullptr; - TF_LITE_ENSURE_STATUS(graph_.InitSubgraphs()); - - // Both AllocatePersistentBuffer and RequestScratchBufferInArena is - // available in Prepare stage. - context_.RequestScratchBufferInArena = - MicroContextRequestScratchBufferInArena; - // external_context become available in Prepare stage. - context_.GetExternalContext = MicroContextGetExternalContext; - - TF_LITE_ENSURE_STATUS(graph_.PrepareSubgraphs()); - - // Prepare is done, we're ready for Invoke. Memory allocation is no longer - // allowed. Kernels can only fetch scratch buffers via GetScratchBuffer. - context_.AllocatePersistentBuffer = nullptr; - context_.RequestScratchBufferInArena = nullptr; - context_.GetScratchBuffer = MicroContextGetScratchBuffer; - - TF_LITE_ENSURE_OK(&context_, allocator_.FinishModelAllocation( - model_, graph_.GetAllocations(), - &scratch_buffer_handles_)); - - micro_context_.SetScratchBufferHandles(scratch_buffer_handles_); - - // TODO(b/162311891): Drop these allocations when the interpreter supports - // handling buffers from TfLiteEvalTensor. - input_tensors_ = - reinterpret_cast(allocator_.AllocatePersistentBuffer( - sizeof(TfLiteTensor*) * inputs_size())); - if (input_tensors_ == nullptr) { - TF_LITE_REPORT_ERROR( - error_reporter_, - "Failed to allocate memory for context->input_tensors_, " - "%d bytes required", - sizeof(TfLiteTensor*) * inputs_size()); - return kTfLiteError; - } - - for (size_t i = 0; i < inputs_size(); ++i) { - input_tensors_[i] = allocator_.AllocatePersistentTfLiteTensor( - model_, graph_.GetAllocations(), inputs().Get(i), 0); - if (input_tensors_[i] == nullptr) { - TF_LITE_REPORT_ERROR(error_reporter_, - "Failed to initialize input tensor %d", i); - return kTfLiteError; - } - } - - // TODO(b/162311891): Drop these allocations when the interpreter supports - // handling buffers from TfLiteEvalTensor. - output_tensors_ = - reinterpret_cast(allocator_.AllocatePersistentBuffer( - sizeof(TfLiteTensor*) * outputs_size())); - if (output_tensors_ == nullptr) { - TF_LITE_REPORT_ERROR( - error_reporter_, - "Failed to allocate memory for context->output_tensors_, " - "%d bytes required", - sizeof(TfLiteTensor*) * outputs_size()); - return kTfLiteError; - } - - for (size_t i = 0; i < outputs_size(); ++i) { - output_tensors_[i] = allocator_.AllocatePersistentTfLiteTensor( - model_, graph_.GetAllocations(), outputs().Get(i), 0); - if (output_tensors_[i] == nullptr) { - TF_LITE_REPORT_ERROR(error_reporter_, - "Failed to initialize output tensor %d", i); - return kTfLiteError; - } - } - - TF_LITE_ENSURE_STATUS(ResetVariableTensors()); - - tensors_allocated_ = true; - return kTfLiteOk; -} - -TfLiteStatus MicroInterpreter::Invoke() { - if (initialization_status_ != kTfLiteOk) { - TF_LITE_REPORT_ERROR(error_reporter_, - "Invoke() called after initialization failed\n"); - return kTfLiteError; - } - - // Ensure tensors are allocated before the interpreter is invoked to avoid - // difficult to debug segfaults. - if (!tensors_allocated_) { - TF_LITE_ENSURE_OK(&context_, AllocateTensors()); - } - return graph_.InvokeSubgraph(0); -} - -TfLiteTensor* MicroInterpreter::input(size_t index) { - const size_t length = inputs_size(); - if (index >= length) { - TF_LITE_REPORT_ERROR(error_reporter_, - "Input index %d out of range (length is %d)", index, - length); - return nullptr; - } - return input_tensors_[index]; -} - -TfLiteTensor* MicroInterpreter::output(size_t index) { - const size_t length = outputs_size(); - if (index >= length) { - TF_LITE_REPORT_ERROR(error_reporter_, - "Output index %d out of range (length is %d)", index, - length); - return nullptr; - } - return output_tensors_[index]; -} - -TfLiteStatus MicroInterpreter::ResetVariableTensors() { - return graph_.ResetVariableTensors(); -} - -TfLiteStatus MicroInterpreter::SetMicroExternalContext( - void* external_context_payload) { - return micro_context_.set_external_context(external_context_payload); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/micro_interpreter.h b/code/components/tflite-lib/tensorflow/lite/micro/micro_interpreter.h deleted file mode 100644 index aa409386..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/micro_interpreter.h +++ /dev/null @@ -1,172 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_MICRO_MICRO_INTERPRETER_H_ -#define TENSORFLOW_LITE_MICRO_MICRO_INTERPRETER_H_ - -#include -#include - -#include "flatbuffers/flatbuffers.h" // from @flatbuffers -#include "tensorflow/lite/c/c_api_types.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/core/api/error_reporter.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/micro/micro_allocator.h" -#include "tensorflow/lite/micro/micro_context.h" -#include "tensorflow/lite/micro/micro_graph.h" -#include "tensorflow/lite/micro/micro_op_resolver.h" -#include "tensorflow/lite/micro/micro_profiler.h" -#include "tensorflow/lite/portable_type_to_tflitetype.h" -#include "tensorflow/lite/schema/schema_generated.h" - -// Copied from tensorflow/lite/version.h to avoid a dependency chain into -// tensorflow/core. -#define TFLITE_SCHEMA_VERSION (3) - -namespace tflite { - -class MicroInterpreter { - public: - // The lifetime of the model, op resolver, tensor arena, error reporter, - // resource variables, and profiler must be at least as long as that of the - // interpreter object, since the interpreter may need to access them at any - // time. This means that you should usually create them with the same scope as - // each other, for example having them all allocated on the stack as local - // variables through a top-level function. The interpreter doesn't do any - // deallocation of any of the pointed-to objects, ownership remains with the - // caller. - MicroInterpreter(const Model* model, const MicroOpResolver& op_resolver, - uint8_t* tensor_arena, size_t tensor_arena_size, - ErrorReporter* error_reporter, - MicroResourceVariables* resource_variables = nullptr, - MicroProfiler* profiler = nullptr); - - // Create an interpreter instance using an existing MicroAllocator instance. - // This constructor should be used when creating an allocator that needs to - // have allocation handled in more than one interpreter or for recording - // allocations inside the interpreter. The lifetime of the allocator must be - // as long as that of the interpreter object. - MicroInterpreter(const Model* model, const MicroOpResolver& op_resolver, - MicroAllocator* allocator, ErrorReporter* error_reporter, - MicroResourceVariables* resource_variables = nullptr, - MicroProfiler* profiler = nullptr); - - ~MicroInterpreter(); - - // Runs through the model and allocates all necessary input, output and - // intermediate tensors. - TfLiteStatus AllocateTensors(); - - // In order to support partial graph runs for strided models, this can return - // values other than kTfLiteOk and kTfLiteError. - // TODO(b/149795762): Add this to the TfLiteStatus enum. - TfLiteStatus Invoke(); - - // This is the recommended API for an application to pass an external payload - // pointer as an external context to kernels. The life time of the payload - // pointer should be at least as long as this interpreter. TFLM supports only - // one external context. - TfLiteStatus SetMicroExternalContext(void* external_context_payload); - - TfLiteTensor* input(size_t index); - size_t inputs_size() const { - return model_->subgraphs()->Get(0)->inputs()->size(); - } - const flatbuffers::Vector& inputs() const { - return *model_->subgraphs()->Get(0)->inputs(); - } - TfLiteTensor* input_tensor(size_t index) { return input(index); } - template - T* typed_input_tensor(int tensor_index) { - if (TfLiteTensor* tensor_ptr = input_tensor(tensor_index)) { - if (tensor_ptr->type == typeToTfLiteType()) { - return GetTensorData(tensor_ptr); - } - } - return nullptr; - } - - TfLiteTensor* output(size_t index); - size_t outputs_size() const { - return model_->subgraphs()->Get(0)->outputs()->size(); - } - const flatbuffers::Vector& outputs() const { - return *model_->subgraphs()->Get(0)->outputs(); - } - TfLiteTensor* output_tensor(size_t index) { return output(index); } - template - T* typed_output_tensor(int tensor_index) { - if (TfLiteTensor* tensor_ptr = output_tensor(tensor_index)) { - if (tensor_ptr->type == typeToTfLiteType()) { - return GetTensorData(tensor_ptr); - } - } - return nullptr; - } - - // Reset all variable tensors to the default value. - TfLiteStatus ResetVariableTensors(); - - TfLiteStatus initialization_status() const { return initialization_status_; } - - // Populates node and registration pointers representing the inference graph - // of the model from values inside the flatbuffer (loaded from the TfLiteModel - // instance). Persistent data (e.g. operator data) is allocated from the - // arena. - TfLiteStatus PrepareNodeAndRegistrationDataFromFlatbuffer(); - - // For debugging only. - // Returns the actual used arena in bytes. This method gives the optimal arena - // size. It's only available after `AllocateTensors` has been called. - // Note that normally `tensor_arena` requires 16 bytes alignment to fully - // utilize the space. If it's not the case, the optimial arena size would be - // arena_used_bytes() + 16. - size_t arena_used_bytes() const { return allocator_.used_bytes(); } - - protected: - const MicroAllocator& allocator() const { return allocator_; } - const TfLiteContext& context() const { return context_; } - - private: - // TODO(b/158263161): Consider switching to Create() function to enable better - // error reporting during initialization. - void Init(MicroProfiler* profiler); - - // Gets the current subgraph index used from within context methods. - int get_subgraph_index() { return graph_.GetCurrentSubgraphIndex(); } - - const Model* model_; - const MicroOpResolver& op_resolver_; - ErrorReporter* error_reporter_; - TfLiteContext context_ = {}; - MicroAllocator& allocator_; - MicroGraph graph_; - bool tensors_allocated_; - - TfLiteStatus initialization_status_; - - ScratchBufferHandle* scratch_buffer_handles_ = nullptr; - - // TODO(b/162311891): Clean these pointers up when this class supports buffers - // from TfLiteEvalTensor. - TfLiteTensor** input_tensors_; - TfLiteTensor** output_tensors_; - - MicroContext micro_context_; -}; - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_MICRO_INTERPRETER_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/micro_mutable_op_resolver.h b/code/components/tflite-lib/tensorflow/lite/micro/micro_mutable_op_resolver.h deleted file mode 100644 index 4b273417..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/micro_mutable_op_resolver.h +++ /dev/null @@ -1,632 +0,0 @@ -/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_MICRO_MICRO_MUTABLE_OP_RESOLVER_H_ -#define TENSORFLOW_LITE_MICRO_MICRO_MUTABLE_OP_RESOLVER_H_ - -#include -#include - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/core/api/error_reporter.h" -#include "tensorflow/lite/core/api/flatbuffer_conversions.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/compatibility.h" -#include "tensorflow/lite/micro/kernels/conv.h" -#include "tensorflow/lite/micro/kernels/depthwise_conv.h" -#include "tensorflow/lite/micro/kernels/ethosu.h" -#include "tensorflow/lite/micro/kernels/fully_connected.h" -#include "tensorflow/lite/micro/kernels/micro_ops.h" -#include "tensorflow/lite/micro/kernels/reduce.h" -#include "tensorflow/lite/micro/kernels/softmax.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" -#include "tensorflow/lite/micro/micro_op_resolver.h" -#include "tensorflow/lite/schema/schema_generated.h" - -namespace tflite { -TfLiteRegistration* Register_DETECTION_POSTPROCESS(); - -template -class MicroMutableOpResolver : public MicroOpResolver { - public: - TF_LITE_REMOVE_VIRTUAL_DELETE - - explicit MicroMutableOpResolver(ErrorReporter* error_reporter = nullptr) - : error_reporter_(error_reporter) {} - - const TfLiteRegistration* FindOp(tflite::BuiltinOperator op) const override { - if (op == BuiltinOperator_CUSTOM) return nullptr; - - for (unsigned int i = 0; i < registrations_len_; ++i) { - const TfLiteRegistration& registration = registrations_[i]; - if (registration.builtin_code == op) { - return ®istration; - } - } - return nullptr; - } - - const TfLiteRegistration* FindOp(const char* op) const override { - for (unsigned int i = 0; i < registrations_len_; ++i) { - const TfLiteRegistration& registration = registrations_[i]; - if ((registration.builtin_code == BuiltinOperator_CUSTOM) && - (strcmp(registration.custom_name, op) == 0)) { - return ®istration; - } - } - return nullptr; - } - - MicroOpResolver::BuiltinParseFunction GetOpDataParser( - BuiltinOperator op) const override { - TFLITE_DCHECK(num_buitin_ops_ <= tOpCount); - for (unsigned int i = 0; i < num_buitin_ops_; ++i) { - if (builtin_codes_[i] == op) return builtin_parsers_[i]; - } - return nullptr; - } - - // Registers a Custom Operator with the MicroOpResolver. - // - // Only the first call for a given name will be successful. i.e. if this - // function is called again for a previously added Custom Operator, the - // MicroOpResolver will be unchanged and this function will return - // kTfLiteError. - TfLiteStatus AddCustom(const char* name, TfLiteRegistration* registration) { - if (registrations_len_ >= tOpCount) { - MicroPrintf( - "Couldn't register custom op '%s', resolver size is too" - "small (%d)", - name, tOpCount); - return kTfLiteError; - } - - if (FindOp(name) != nullptr) { - MicroPrintf("Calling AddCustom for the same op more than once "); - MicroPrintf("is not supported (Op: %s).", name); - return kTfLiteError; - } - - TfLiteRegistration* new_registration = ®istrations_[registrations_len_]; - registrations_len_ += 1; - - *new_registration = *registration; - new_registration->builtin_code = BuiltinOperator_CUSTOM; - new_registration->custom_name = name; - return kTfLiteOk; - } - - // The Add* functions below add the various Builtin operators to the - // MicroMutableOpResolver object. - - TfLiteStatus AddAbs() { - return AddBuiltin(BuiltinOperator_ABS, tflite::ops::micro::Register_ABS(), - ParseAbs); - } - - TfLiteStatus AddAdd(const TfLiteRegistration& registration = Register_ADD()) { - return AddBuiltin(BuiltinOperator_ADD, registration, ParseAdd); - } - - TfLiteStatus AddAddN() { - return AddBuiltin(BuiltinOperator_ADD_N, tflite::Register_ADD_N(), - ParseAddN); - } - - TfLiteStatus AddArgMax() { - return AddBuiltin(BuiltinOperator_ARG_MAX, - tflite::ops::micro::Register_ARG_MAX(), ParseArgMax); - } - - TfLiteStatus AddArgMin() { - return AddBuiltin(BuiltinOperator_ARG_MIN, - tflite::ops::micro::Register_ARG_MIN(), ParseArgMin); - } - - TfLiteStatus AddAssignVariable() { - return AddBuiltin(BuiltinOperator_ASSIGN_VARIABLE, - tflite::Register_ASSIGN_VARIABLE(), ParseAssignVariable); - } - - TfLiteStatus AddAveragePool2D() { - return AddBuiltin(BuiltinOperator_AVERAGE_POOL_2D, - tflite::Register_AVERAGE_POOL_2D(), ParsePool); - } - - TfLiteStatus AddBatchToSpaceNd() { - return AddBuiltin(BuiltinOperator_BATCH_TO_SPACE_ND, - Register_BATCH_TO_SPACE_ND(), ParseBatchToSpaceNd); - } - - TfLiteStatus AddBroadcastArgs() { - return AddBuiltin(BuiltinOperator_BROADCAST_ARGS, Register_BROADCAST_ARGS(), - ParseBroadcastArgs); - } - - TfLiteStatus AddBroadcastTo() { - return AddBuiltin(BuiltinOperator_BROADCAST_TO, Register_BROADCAST_TO(), - ParseBroadcastTo); - } - - TfLiteStatus AddCallOnce() { - return AddBuiltin(BuiltinOperator_CALL_ONCE, Register_CALL_ONCE(), - ParseCallOnce); - } - - TfLiteStatus AddCast() { - return AddBuiltin(BuiltinOperator_CAST, Register_CAST(), ParseCast); - } - - TfLiteStatus AddCeil() { - return AddBuiltin(BuiltinOperator_CEIL, tflite::ops::micro::Register_CEIL(), - ParseCeil); - } - - TfLiteStatus AddCircularBuffer() { - return AddCustom("CIRCULAR_BUFFER", tflite::Register_CIRCULAR_BUFFER()); - } - - TfLiteStatus AddConcatenation() { - return AddBuiltin(BuiltinOperator_CONCATENATION, - tflite::ops::micro::Register_CONCATENATION(), - ParseConcatenation); - } - - TfLiteStatus AddConv2D( - const TfLiteRegistration& registration = Register_CONV_2D()) { - return AddBuiltin(BuiltinOperator_CONV_2D, registration, ParseConv2D); - } - - TfLiteStatus AddCos() { - return AddBuiltin(BuiltinOperator_COS, tflite::ops::micro::Register_COS(), - ParseCos); - } - - TfLiteStatus AddCumSum() { - return AddBuiltin(BuiltinOperator_CUMSUM, tflite::Register_CUMSUM(), - ParseCumsum); - } - - TfLiteStatus AddDepthToSpace() { - return AddBuiltin(BuiltinOperator_DEPTH_TO_SPACE, - tflite::Register_DEPTH_TO_SPACE(), ParseDepthToSpace); - } - - TfLiteStatus AddDepthwiseConv2D( - const TfLiteRegistration& registration = Register_DEPTHWISE_CONV_2D()) { - return AddBuiltin(BuiltinOperator_DEPTHWISE_CONV_2D, registration, - ParseDepthwiseConv2D); - } - - TfLiteStatus AddDequantize() { - return AddBuiltin(BuiltinOperator_DEQUANTIZE, tflite::Register_DEQUANTIZE(), - ParseDequantize); - } - - TfLiteStatus AddDetectionPostprocess() { - return AddCustom("TFLite_Detection_PostProcess", - tflite::Register_DETECTION_POSTPROCESS()); - } - - TfLiteStatus AddDiv() { - return AddBuiltin(BuiltinOperator_DIV, tflite::Register_DIV(), ParseDiv); - } - - TfLiteStatus AddElu() { - return AddBuiltin(BuiltinOperator_ELU, tflite::Register_ELU(), ParseElu); - } - - TfLiteStatus AddEqual() { - return AddBuiltin(BuiltinOperator_EQUAL, - tflite::ops::micro::Register_EQUAL(), ParseEqual); - } - - TfLiteStatus AddEthosU() { - TfLiteRegistration* registration = tflite::Register_ETHOSU(); - if (registration) { - return AddCustom(tflite::GetString_ETHOSU(), registration); - } - return kTfLiteOk; - } - - TfLiteStatus AddExp() { - return AddBuiltin(BuiltinOperator_EXP, Register_EXP(), ParseExp); - } - - TfLiteStatus AddExpandDims() { - return AddBuiltin(BuiltinOperator_EXPAND_DIMS, Register_EXPAND_DIMS(), - ParseExpandDims); - } - - TfLiteStatus AddFill() { - return AddBuiltin(BuiltinOperator_FILL, tflite::Register_FILL(), ParseFill); - } - - TfLiteStatus AddFloor() { - return AddBuiltin(BuiltinOperator_FLOOR, - tflite::ops::micro::Register_FLOOR(), ParseFloor); - } - - TfLiteStatus AddFloorDiv() { - return AddBuiltin(BuiltinOperator_FLOOR_DIV, tflite::Register_FLOOR_DIV(), - ParseFloorDiv); - } - - TfLiteStatus AddFloorMod() { - return AddBuiltin(BuiltinOperator_FLOOR_MOD, tflite::Register_FLOOR_MOD(), - ParseFloorMod); - } - - TfLiteStatus AddFullyConnected( - const TfLiteRegistration& registration = Register_FULLY_CONNECTED()) { - return AddBuiltin(BuiltinOperator_FULLY_CONNECTED, registration, - ParseFullyConnected); - } - - TfLiteStatus AddGather() { - return AddBuiltin(BuiltinOperator_GATHER, tflite::Register_GATHER(), - ParseGather); - } - - TfLiteStatus AddGatherNd() { - return AddBuiltin(BuiltinOperator_GATHER_ND, tflite::Register_GATHER_ND(), - ParseGatherNd); - } - - TfLiteStatus AddGreater() { - return AddBuiltin(BuiltinOperator_GREATER, - tflite::ops::micro::Register_GREATER(), ParseGreater); - } - - TfLiteStatus AddGreaterEqual() { - return AddBuiltin(BuiltinOperator_GREATER_EQUAL, - tflite::ops::micro::Register_GREATER_EQUAL(), - ParseGreaterEqual); - } - - TfLiteStatus AddHardSwish() { - return AddBuiltin(BuiltinOperator_HARD_SWISH, tflite::Register_HARD_SWISH(), - ParseHardSwish); - } - - TfLiteStatus AddIf() { - return AddBuiltin(BuiltinOperator_IF, tflite::Register_IF(), ParseIf); - } - - TfLiteStatus AddL2Normalization() { - return AddBuiltin(BuiltinOperator_L2_NORMALIZATION, - tflite::ops::micro::Register_L2_NORMALIZATION(), - ParseL2Normalization); - } - - TfLiteStatus AddL2Pool2D() { - return AddBuiltin(BuiltinOperator_L2_POOL_2D, tflite::Register_L2_POOL_2D(), - ParsePool); - } - - TfLiteStatus AddLeakyRelu() { - return AddBuiltin(BuiltinOperator_LEAKY_RELU, tflite::Register_LEAKY_RELU(), - ParseLeakyRelu); - } - - TfLiteStatus AddLess() { - return AddBuiltin(BuiltinOperator_LESS, tflite::ops::micro::Register_LESS(), - ParseLess); - } - - TfLiteStatus AddLessEqual() { - return AddBuiltin(BuiltinOperator_LESS_EQUAL, - tflite::ops::micro::Register_LESS_EQUAL(), - ParseLessEqual); - } - - TfLiteStatus AddLog() { - return AddBuiltin(BuiltinOperator_LOG, tflite::ops::micro::Register_LOG(), - ParseLog); - } - - TfLiteStatus AddLogicalAnd() { - return AddBuiltin(BuiltinOperator_LOGICAL_AND, - tflite::Register_LOGICAL_AND(), ParseLogicalAnd); - } - - TfLiteStatus AddLogicalNot() { - return AddBuiltin(BuiltinOperator_LOGICAL_NOT, - tflite::ops::micro::Register_LOGICAL_NOT(), - ParseLogicalNot); - } - - TfLiteStatus AddLogicalOr() { - return AddBuiltin(BuiltinOperator_LOGICAL_OR, tflite::Register_LOGICAL_OR(), - ParseLogicalOr); - } - - TfLiteStatus AddLogistic() { - return AddBuiltin(BuiltinOperator_LOGISTIC, tflite::Register_LOGISTIC(), - ParseLogistic); - } - - TfLiteStatus AddMaximum() { - return AddBuiltin(BuiltinOperator_MAXIMUM, - tflite::ops::micro::Register_MAXIMUM(), ParseMaximum); - } - - TfLiteStatus AddMaxPool2D() { - return AddBuiltin(BuiltinOperator_MAX_POOL_2D, - tflite::Register_MAX_POOL_2D(), ParsePool); - } - - TfLiteStatus AddMirrorPad() { - return AddBuiltin(BuiltinOperator_MIRROR_PAD, tflite::Register_MIRROR_PAD(), - ParseMirrorPad); - } - - TfLiteStatus AddMean() { - return AddBuiltin(BuiltinOperator_MEAN, Register_MEAN(), ParseReducer); - } - - TfLiteStatus AddMinimum() { - return AddBuiltin(BuiltinOperator_MINIMUM, - tflite::ops::micro::Register_MINIMUM(), ParseMinimum); - } - - TfLiteStatus AddMul() { - return AddBuiltin(BuiltinOperator_MUL, tflite::Register_MUL(), ParseMul); - } - - TfLiteStatus AddNeg() { - return AddBuiltin(BuiltinOperator_NEG, tflite::ops::micro::Register_NEG(), - ParseNeg); - } - - TfLiteStatus AddNotEqual() { - return AddBuiltin(BuiltinOperator_NOT_EQUAL, - tflite::ops::micro::Register_NOT_EQUAL(), ParseNotEqual); - } - - TfLiteStatus AddPack() { - return AddBuiltin(BuiltinOperator_PACK, tflite::ops::micro::Register_PACK(), - ParsePack); - } - - TfLiteStatus AddPad() { - return AddBuiltin(BuiltinOperator_PAD, tflite::ops::micro::Register_PAD(), - ParsePad); - } - - TfLiteStatus AddPadV2() { - return AddBuiltin(BuiltinOperator_PADV2, - tflite::ops::micro::Register_PADV2(), ParsePadV2); - } - - TfLiteStatus AddPrelu() { - return AddBuiltin(BuiltinOperator_PRELU, tflite::Register_PRELU(), - ParsePrelu); - } - - TfLiteStatus AddQuantize() { - return AddBuiltin(BuiltinOperator_QUANTIZE, Register_QUANTIZE(), - ParseQuantize); - } - - TfLiteStatus AddReadVariable() { - return AddBuiltin(BuiltinOperator_READ_VARIABLE, - tflite::Register_READ_VARIABLE(), ParseReadVariable); - } - - TfLiteStatus AddReduceMax() { - return AddBuiltin(BuiltinOperator_REDUCE_MAX, Register_REDUCE_MAX(), - ParseReducer); - } - - TfLiteStatus AddRelu() { - return AddBuiltin(BuiltinOperator_RELU, tflite::Register_RELU(), ParseRelu); - } - - TfLiteStatus AddRelu6() { - return AddBuiltin(BuiltinOperator_RELU6, tflite::Register_RELU6(), - ParseRelu6); - } - - TfLiteStatus AddReshape() { - return AddBuiltin(BuiltinOperator_RESHAPE, - tflite::ops::micro::Register_RESHAPE(), ParseReshape); - } - - TfLiteStatus AddResizeBilinear() { - return AddBuiltin(BuiltinOperator_RESIZE_BILINEAR, - Register_RESIZE_BILINEAR(), ParseResizeBilinear); - } - - TfLiteStatus AddResizeNearestNeighbor() { - return AddBuiltin(BuiltinOperator_RESIZE_NEAREST_NEIGHBOR, - tflite::ops::micro::Register_RESIZE_NEAREST_NEIGHBOR(), - ParseResizeNearestNeighbor); - } - - TfLiteStatus AddRound() { - return AddBuiltin(BuiltinOperator_ROUND, - tflite::ops::micro::Register_ROUND(), ParseRound); - } - - TfLiteStatus AddRsqrt() { - return AddBuiltin(BuiltinOperator_RSQRT, - tflite::ops::micro::Register_RSQRT(), ParseRsqrt); - } - - TfLiteStatus AddShape() { - return AddBuiltin(BuiltinOperator_SHAPE, Register_SHAPE(), ParseShape); - } - - TfLiteStatus AddSin() { - return AddBuiltin(BuiltinOperator_SIN, tflite::ops::micro::Register_SIN(), - ParseSin); - } - - TfLiteStatus AddSlice() { - return AddBuiltin(BuiltinOperator_SLICE, Register_SLICE(), ParseSlice); - } - - TfLiteStatus AddSoftmax( - const TfLiteRegistration& registration = Register_SOFTMAX()) { - return AddBuiltin(BuiltinOperator_SOFTMAX, registration, ParseSoftmax); - } - - TfLiteStatus AddSpaceToBatchNd() { - return AddBuiltin(BuiltinOperator_SPACE_TO_BATCH_ND, - Register_SPACE_TO_BATCH_ND(), ParseSpaceToBatchNd); - } - - TfLiteStatus AddSpaceToDepth() { - return AddBuiltin(BuiltinOperator_SPACE_TO_DEPTH, Register_SPACE_TO_DEPTH(), - ParseSpaceToDepth); - } - - TfLiteStatus AddSplit() { - return AddBuiltin(BuiltinOperator_SPLIT, - tflite::ops::micro::Register_SPLIT(), ParseSplit); - } - - TfLiteStatus AddSplitV() { - return AddBuiltin(BuiltinOperator_SPLIT_V, - tflite::ops::micro::Register_SPLIT_V(), ParseSplitV); - } - - TfLiteStatus AddSqueeze() { - return AddBuiltin(BuiltinOperator_SQUEEZE, Register_SQUEEZE(), - ParseSqueeze); - } - - TfLiteStatus AddSqrt() { - return AddBuiltin(BuiltinOperator_SQRT, tflite::ops::micro::Register_SQRT(), - ParseSqrt); - } - - TfLiteStatus AddSquare() { - return AddBuiltin(BuiltinOperator_SQUARE, - tflite::ops::micro::Register_SQUARE(), ParseSquare); - } - - TfLiteStatus AddStridedSlice() { - return AddBuiltin(BuiltinOperator_STRIDED_SLICE, - tflite::ops::micro::Register_STRIDED_SLICE(), - ParseStridedSlice); - } - - TfLiteStatus AddSub() { - return AddBuiltin(BuiltinOperator_SUB, tflite::Register_SUB(), ParseSub); - } - - TfLiteStatus AddSum() { - return AddBuiltin(BuiltinOperator_SUM, Register_SUM(), ParseReducer); - } - - TfLiteStatus AddSvdf( - const TfLiteRegistration& registration = Register_SVDF()) { - return AddBuiltin(BuiltinOperator_SVDF, registration, ParseSvdf); - } - - TfLiteStatus AddTanh() { - return AddBuiltin(BuiltinOperator_TANH, tflite::ops::micro::Register_TANH(), - ParseTanh); - } - - TfLiteStatus AddTransposeConv() { - return AddBuiltin(BuiltinOperator_TRANSPOSE_CONV, - tflite::Register_TRANSPOSE_CONV(), ParseTransposeConv); - } - - TfLiteStatus AddTranspose() { - return AddBuiltin(BuiltinOperator_TRANSPOSE, Register_TRANSPOSE(), - ParseTranspose); - } - - TfLiteStatus AddUnpack() { - return AddBuiltin(BuiltinOperator_UNPACK, - tflite::ops::micro::Register_UNPACK(), ParseUnpack); - } - - TfLiteStatus AddUnidirectionalSequenceLSTM() { - return AddBuiltin(BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM, - Register_UNIDIRECTIONAL_SEQUENCE_LSTM(), - ParseUnidirectionalSequenceLSTM); - } - - TfLiteStatus AddVarHandle() { - return AddBuiltin(BuiltinOperator_VAR_HANDLE, Register_VAR_HANDLE(), - ParseVarHandle); - } - - TfLiteStatus AddWhile() { - return AddBuiltin(BuiltinOperator_WHILE, Register_WHILE(), ParseWhile); - } - - TfLiteStatus AddZerosLike() { - return AddBuiltin(BuiltinOperator_ZEROS_LIKE, Register_ZEROS_LIKE(), - ParseZerosLike); - } - - unsigned int GetRegistrationLength() { return registrations_len_; } - - private: - TfLiteStatus AddBuiltin(tflite::BuiltinOperator op, - const TfLiteRegistration& registration, - MicroOpResolver::BuiltinParseFunction parser) { - if (op == BuiltinOperator_CUSTOM) { - MicroPrintf("Invalid parameter BuiltinOperator_CUSTOM to the "); - MicroPrintf("AddBuiltin function."); - return kTfLiteError; - } - - if (FindOp(op) != nullptr) { - MicroPrintf("Calling AddBuiltin with the same op more than "); - MicroPrintf("once is not supported (Op: #%d).", op); - return kTfLiteError; - } - - if (registrations_len_ >= tOpCount) { - MicroPrintf("Couldn't register builtin op #%d, resolver size ", op); - MicroPrintf("is too small (%d).", tOpCount); - return kTfLiteError; - } - - registrations_[registrations_len_] = registration; - // Strictly speaking, the builtin_code is not necessary for TFLM but filling - // it in regardless. - registrations_[registrations_len_].builtin_code = op; - registrations_len_++; - - builtin_codes_[num_buitin_ops_] = op; - builtin_parsers_[num_buitin_ops_] = parser; - num_buitin_ops_++; - - return kTfLiteOk; - } - - TfLiteRegistration registrations_[tOpCount]; - unsigned int registrations_len_ = 0; - - // Arrays (and counter) to store the builtin codes and their corresponding - // parse functions as these are registered with the Op Resolver. - BuiltinOperator builtin_codes_[tOpCount]; - MicroOpResolver::BuiltinParseFunction builtin_parsers_[tOpCount]; - unsigned int num_buitin_ops_ = 0; - - ErrorReporter* error_reporter_; -}; - -}; // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_MICRO_MUTABLE_OP_RESOLVER_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/micro_op_resolver.h b/code/components/tflite-lib/tensorflow/lite/micro/micro_op_resolver.h deleted file mode 100644 index 757b6b89..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/micro_op_resolver.h +++ /dev/null @@ -1,73 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_MICRO_MICRO_OP_RESOLVER_H_ -#define TENSORFLOW_LITE_MICRO_MICRO_OP_RESOLVER_H_ - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/core/api/error_reporter.h" -#include "tensorflow/lite/core/api/flatbuffer_conversions.h" -#include "tensorflow/lite/core/api/op_resolver.h" -#include "tensorflow/lite/schema/schema_generated.h" - -namespace tflite { - -// This is an interface for the OpResolver for TFLiteMicro. The differences from -// the TFLite OpResolver base class are to: -// * explicitly remove support for Op versions -// * allow for finer grained registration of the Builtin Ops to reduce code -// size for TFLiteMicro. -// -// We need an interface class instead of directly using MicroMutableOpResolver -// because MicroMutableOpResolver is a class template with the number of -// registered Ops as the template parameter. -class MicroOpResolver : public OpResolver { - public: - typedef TfLiteStatus (*BuiltinParseFunction)(const Operator* op, - ErrorReporter* error_reporter, - BuiltinDataAllocator* allocator, - void** builtin_data); - - // Returns the Op registration struct corresponding to the enum code from the - // flatbuffer schema. Returns nullptr if the op is not found or if op == - // BuiltinOperator_CUSTOM. - virtual const TfLiteRegistration* FindOp(BuiltinOperator op) const = 0; - - // Returns the Op registration struct corresponding to the custom operator by - // name. - virtual const TfLiteRegistration* FindOp(const char* op) const = 0; - - // This implementation exists for compatibility with the OpResolver base class - // and disregards the version parameter. - const TfLiteRegistration* FindOp(BuiltinOperator op, - int version) const final { - return FindOp(op); - } - - // This implementation exists for compatibility with the OpResolver base class - // and disregards the version parameter. - const TfLiteRegistration* FindOp(const char* op, int version) const final { - return FindOp(op); - } - - // Returns the operator specific parsing function for the OpData for a - // BuiltinOperator (if registered), else nullptr. - virtual BuiltinParseFunction GetOpDataParser(BuiltinOperator op) const = 0; - - ~MicroOpResolver() override {} -}; - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_MICRO_OP_RESOLVER_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/micro_profiler.cc b/code/components/tflite-lib/tensorflow/lite/micro/micro_profiler.cc deleted file mode 100644 index 72f3d37f..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/micro_profiler.cc +++ /dev/null @@ -1,70 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/micro/micro_profiler.h" - -#include -#include - -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" -#include "tensorflow/lite/micro/micro_time.h" - -namespace tflite { - -uint32_t MicroProfiler::BeginEvent(const char* tag) { - if (num_events_ == kMaxEvents) { - num_events_ = 0; - } - - tags_[num_events_] = tag; - start_ticks_[num_events_] = GetCurrentTimeTicks(); - end_ticks_[num_events_] = start_ticks_[num_events_] - 1; - return num_events_++; -} - -void MicroProfiler::EndEvent(uint32_t event_handle) { - TFLITE_DCHECK(event_handle < kMaxEvents); - end_ticks_[event_handle] = GetCurrentTimeTicks(); -} - -uint32_t MicroProfiler::GetTotalTicks() const { - int32_t ticks = 0; - for (int i = 0; i < num_events_; ++i) { - ticks += end_ticks_[i] - start_ticks_[i]; - } - return ticks; -} - -void MicroProfiler::Log() const { -#if !defined(TF_LITE_STRIP_ERROR_STRINGS) - for (int i = 0; i < num_events_; ++i) { - uint32_t ticks = end_ticks_[i] - start_ticks_[i]; - MicroPrintf("%s took %" PRIu32 " ticks (%d ms).", tags_[i], ticks, - TicksToMs(ticks)); - } -#endif -} - -void MicroProfiler::LogCsv() const { -#if !defined(TF_LITE_STRIP_ERROR_STRINGS) - MicroPrintf("\"Event\",\"Tag\",\"Ticks\""); - for (int i = 0; i < num_events_; ++i) { - uint32_t ticks = end_ticks_[i] - start_ticks_[i]; - MicroPrintf("%d,%s,%" PRIu32, i, tags_[i], ticks); - } -#endif -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/micro_profiler.h b/code/components/tflite-lib/tensorflow/lite/micro/micro_profiler.h deleted file mode 100644 index 41f41a35..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/micro_profiler.h +++ /dev/null @@ -1,123 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_MICRO_PROFILER_H_ -#define TENSORFLOW_LITE_MICRO_MICRO_PROFILER_H_ - -#include - -#include "tensorflow/lite/micro/compatibility.h" - -namespace tflite { - -// MicroProfiler creates a common way to gain fine-grained insight into runtime -// performance. Bottleck operators can be identified along with slow code -// sections. This can be used in conjunction with running the relevant micro -// benchmark to evaluate end-to-end performance. -class MicroProfiler { - public: - MicroProfiler() = default; - virtual ~MicroProfiler() = default; - - // Marks the start of a new event and returns an event handle that can be used - // to mark the end of the event via EndEvent. The lifetime of the tag - // parameter must exceed that of the MicroProfiler. - virtual uint32_t BeginEvent(const char* tag); - - // Marks the end of an event associated with event_handle. It is the - // responsibility of the caller to ensure than EndEvent is called once and - // only once per event_handle. - // - // If EndEvent is called more than once for the same event_handle, the last - // call will be used as the end of event marker.If EndEvent is called 0 times - // for a particular event_handle, the duration of that event will be 0 ticks. - virtual void EndEvent(uint32_t event_handle); - - // Clears all the events that have been currently profiled. - void ClearEvents() { num_events_ = 0; } - - // Returns the sum of the ticks taken across all the events. This number - // is only meaningful if all of the events are disjoint (the end time of - // event[i] <= start time of event[i+1]). - uint32_t GetTotalTicks() const; - - // Prints the profiling information of each of the events in human readable - // form. - void Log() const; - - // Prints the profiling information of each of the events in CSV (Comma - // Separated Value) form. - void LogCsv() const; - - private: - // Maximum number of events that this class can keep track of. If we call - // AddEvent more than kMaxEvents number of times, then the oldest event's - // profiling information will be overwritten. - static constexpr int kMaxEvents = 1024; - - const char* tags_[kMaxEvents]; - uint32_t start_ticks_[kMaxEvents]; - uint32_t end_ticks_[kMaxEvents]; - int num_events_ = 0; - - TF_LITE_REMOVE_VIRTUAL_DELETE; -}; - -#if defined(TF_LITE_STRIP_ERROR_STRINGS) -// For release builds, the ScopedMicroProfiler is a noop. -// -// This is done because the ScipedProfiler is used as part of the -// MicroInterpreter and we want to ensure zero overhead for the release builds. -class ScopedMicroProfiler { - public: - explicit ScopedMicroProfiler(const char* tag, MicroProfiler* profiler) {} -}; - -#else - -// This class can be used to add events to a MicroProfiler object that span the -// lifetime of the ScopedMicroProfiler object. -// Usage example: -// -// MicroProfiler profiler(); -// ... -// { -// ScopedMicroProfiler scoped_profiler("custom_tag", profiler); -// work_to_profile(); -// } -class ScopedMicroProfiler { - public: - explicit ScopedMicroProfiler(const char* tag, MicroProfiler* profiler) - : profiler_(profiler) { - if (profiler_ != nullptr) { - event_handle_ = profiler_->BeginEvent(tag); - } - } - - ~ScopedMicroProfiler() { - if (profiler_ != nullptr) { - profiler_->EndEvent(event_handle_); - } - } - - private: - uint32_t event_handle_ = 0; - MicroProfiler* profiler_ = nullptr; -}; -#endif // !defined(TF_LITE_STRIP_ERROR_STRINGS) - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_MICRO_PROFILER_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/micro_resource_variable.cc b/code/components/tflite-lib/tensorflow/lite/micro/micro_resource_variable.cc deleted file mode 100644 index c4577773..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/micro_resource_variable.cc +++ /dev/null @@ -1,148 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/micro_resource_variable.h" - -#include - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/micro/memory_helpers.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" -#include "tensorflow/lite/micro/micro_utils.h" - -namespace tflite { - -namespace {} // namespace - -MicroResourceVariables* MicroResourceVariables::Create( - MicroAllocator* allocator, int max_num_variables) { - TFLITE_DCHECK(allocator != nullptr); - - uint8_t* allocator_buffer = static_cast( - allocator->AllocatePersistentBuffer(sizeof(MicroResourceVariables))); - MicroResourceVariable* variable_array = - static_cast(allocator->AllocatePersistentBuffer( - sizeof(MicroResourceVariable) * max_num_variables)); - MicroResourceVariables* variables = new (allocator_buffer) - MicroResourceVariables(variable_array, max_num_variables); - return variables; -} - -int MicroResourceVariables::CreateIdIfNoneFound(const char* container, - const char* shared_name) { - int resource_id = FindId(container, shared_name); - if (resource_id >= 0) { - return resource_id; - } - - // no existing variable found for the given container and shared name pair. - if (num_resource_variables_ >= max_variable_count_) { - MicroPrintf( - "Failed to allocate resource variable. Maximum resource variable count " - "(%d) " - "reached.", - max_variable_count_); - return -1; - } - - resource_id = num_resource_variables_++; - resource_variables_[resource_id].container = container; - resource_variables_[resource_id].shared_name = shared_name; - resource_variables_[resource_id].resource_buffer = nullptr; - resource_variables_[resource_id].bytes = 0; - return resource_id; -} - -TfLiteStatus MicroResourceVariables::Read(int id, - const TfLiteEvalTensor* tensor) { - if (id < 0 || id >= num_resource_variables_) { - MicroPrintf("Attempting to read non-existent resource variable %d", id); - return kTfLiteError; - } - MicroResourceVariable variable = resource_variables_[id]; - TFLITE_DCHECK(EvalTensorBytes(tensor) == variable.bytes); - TFLITE_DCHECK(variable.resource_buffer != nullptr); - memcpy(tensor->data.raw, variable.resource_buffer, variable.bytes); - return kTfLiteOk; -} - -TfLiteStatus MicroResourceVariables::Allocate(int id, TfLiteContext* context, - const TfLiteTensor* tensor) { - if (id < 0 || id >= num_resource_variables_) { - MicroPrintf("Attempting to read non-existent resource variable %d", id); - return kTfLiteError; - } - - MicroResourceVariable& variable = resource_variables_[id]; - - if (variable.resource_buffer == nullptr) { - variable.bytes = tensor->bytes; - variable.resource_buffer = - context->AllocatePersistentBuffer(context, tensor->bytes); - if (variable.resource_buffer == nullptr) { - MicroPrintf("Failed to allocate resource buffer."); - return kTfLiteError; - } - // Zero out resource buffers by deafult. Buffers can be initialized to - // nonzero values using ASSIGN_VARIABLE. - memset(variable.resource_buffer, 0, variable.bytes); - } - - return kTfLiteOk; -} - -TfLiteStatus MicroResourceVariables::Assign(int id, - const TfLiteEvalTensor* tensor) { - if (id < 0 || id >= num_resource_variables_) { - MicroPrintf("Attempting to read non-existent resource variable %d", id); - return kTfLiteError; - } - MicroResourceVariable variable = resource_variables_[id]; - - if (variable.resource_buffer == nullptr) { - MicroPrintf( - "Attempting to assign from a TfLiteEvalTensor before the resource " - "buffer has been allocated. Make sure to call AssignResourceVariable " - "with a TfLiteTensor first."); - return kTfLiteError; - } - TFLITE_DCHECK(EvalTensorBytes(tensor) == variable.bytes); - memcpy(variable.resource_buffer, tensor->data.raw, variable.bytes); - return kTfLiteOk; -} - -TfLiteStatus MicroResourceVariables::ResetAll() { - for (int i = 0; i < num_resource_variables_; i++) { - MicroResourceVariable variable = resource_variables_[i]; - memset(variable.resource_buffer, 0, variable.bytes); - } - return kTfLiteOk; -} - -int MicroResourceVariables::FindId(const char* container, - const char* shared_name) { - for (int i = 0; i < num_resource_variables_; i++) { - // Some TFLite flatbuffers contain null container names to save space. - if ((container == nullptr || - !strcmp(container, resource_variables_[i].container)) && - !strcmp(shared_name, resource_variables_[i].shared_name)) { - return i; - } - } - return -1; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/micro_resource_variable.h b/code/components/tflite-lib/tensorflow/lite/micro/micro_resource_variable.h deleted file mode 100644 index e8df991c..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/micro_resource_variable.h +++ /dev/null @@ -1,87 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TFLITE_MICRO_TENSORFLOW_LITE_MICRO_MICRO_RESOURCE_H_ -#define TFLITE_MICRO_TENSORFLOW_LITE_MICRO_MICRO_RESOURCE_H_ - -#include - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/micro/micro_allocator.h" - -namespace tflite { - -class MicroResourceVariables { - public: - // Create - static MicroResourceVariables* Create(MicroAllocator* allocator, - int num_variables); - - // Creates a resource variable if none is available for the given container - // and shared name pair. Returns the resource ID corresponding to the - // container and shared name pair. If allocation fails, the returned resource - // ID will be negative. The the container and shared_name must outlive this - // class. - int CreateIdIfNoneFound(const char* container, const char* shared_name); - - // Read the resource buffer associated with the given ID into the given - // tensor. - TfLiteStatus Read(int id, const TfLiteEvalTensor* tensor); - - // Allocates the resource buffer if none has been allocated, based on the - // length of the input tensor. Copies input tensor contents to the resource - // buffer. - TfLiteStatus Allocate(int id, TfLiteContext* context, - const TfLiteTensor* tensor); - - // Copies input tensor contents to the resource buffer. - // AllocateResourceVariable with a TFLite tensor must have been called first - // in order to allocate the resource buffer. - TfLiteStatus Assign(int id, const TfLiteEvalTensor* tensor); - - // Zeros out all resource buffers. - TfLiteStatus ResetAll(); - - private: - int FindId(const char* container, const char* shared_name); - - // Micro resource contains the mapping between resource container/name strings - // and resouce IDs. Each resource ID corresponds to a resource buffer pointer. - // The resouce ID is created during the VAR_HANDLE operator preparation stage. - // The resource buffer pointer is created during ASSIGN_VARIABLE preparation - // stage based on the size of the TFLiteTensor being assigned. - struct MicroResourceVariable { - const char* container; - const char* shared_name; - void* resource_buffer; - - // This is only for verifying read size. - size_t bytes; - }; - - MicroResourceVariables(MicroResourceVariable* variables, - int max_variable_count) - : resource_variables_(variables), - max_variable_count_(max_variable_count), - num_resource_variables_(0) {} - - MicroResourceVariable* resource_variables_; - int max_variable_count_; - int num_resource_variables_; -}; - -} // namespace tflite - -#endif // TFLITE_MICRO_TENSORFLOW_LITE_MICRO_MICRO_RESOURCE_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/micro_string.cc b/code/components/tflite-lib/tensorflow/lite/micro/micro_string.cc deleted file mode 100644 index bb41a9e3..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/micro_string.cc +++ /dev/null @@ -1,317 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -// Implements debug logging for numbers by converting them into strings and then -// calling the main DebugLog(char*) function. These are separated into a -// different file so that platforms can just implement the string output version -// of DebugLog() and then get the numerical variations without requiring any -// more code. - -#include "tensorflow/lite/micro/micro_string.h" - -#include -#include -#include - -namespace { - -// Int formats can need up to 10 bytes for the value plus a single byte for the -// sign. -constexpr int kMaxIntCharsNeeded = 10 + 1; -// Hex formats can need up to 8 bytes for the value plus two bytes for the "0x". -constexpr int kMaxHexCharsNeeded = 8 + 2; - -// Float formats can need up to 7 bytes for the fraction plus 3 bytes for "x2^" -// plus 3 bytes for the exponent and a single sign bit. -constexpr float kMaxFloatCharsNeeded = 7 + 3 + 3 + 1; - -// All input buffers to the number conversion functions must be this long. -const int kFastToBufferSize = 48; - -// Reverses a zero-terminated string in-place. -char* ReverseStringInPlace(char* start, char* end) { - char* p1 = start; - char* p2 = end - 1; - while (p1 < p2) { - char tmp = *p1; - *p1++ = *p2; - *p2-- = tmp; - } - return start; -} - -// Appends a string to a string, in-place. You need to pass in the maximum -// string length as the second argument. -char* StrCatStr(char* main, int main_max_length, const char* to_append) { - char* current = main; - while (*current != 0) { - ++current; - } - char* current_end = main + (main_max_length - 1); - while ((*to_append != 0) && (current < current_end)) { - *current = *to_append; - ++current; - ++to_append; - } - *current = 0; - return current; -} - -// Populates the provided buffer with an ASCII representation of the number. -char* FastUInt32ToBufferLeft(uint32_t i, char* buffer, int base) { - char* start = buffer; - do { - int32_t digit = i % base; - char character; - if (digit < 10) { - character = '0' + digit; - } else { - character = 'a' + (digit - 10); - } - *buffer++ = character; - i /= base; - } while (i > 0); - *buffer = 0; - ReverseStringInPlace(start, buffer); - return buffer; -} - -// Populates the provided buffer with an ASCII representation of the number. -char* FastInt32ToBufferLeft(int32_t i, char* buffer) { - uint32_t u = i; - if (i < 0) { - *buffer++ = '-'; - u = -u; - } - return FastUInt32ToBufferLeft(u, buffer, 10); -} - -// Converts a number to a string and appends it to another. -char* StrCatInt32(char* main, int main_max_length, int32_t number) { - char number_string[kFastToBufferSize]; - FastInt32ToBufferLeft(number, number_string); - return StrCatStr(main, main_max_length, number_string); -} - -// Converts a number to a string and appends it to another. -char* StrCatUInt32(char* main, int main_max_length, uint32_t number, int base) { - char number_string[kFastToBufferSize]; - FastUInt32ToBufferLeft(number, number_string, base); - return StrCatStr(main, main_max_length, number_string); -} - -// Populates the provided buffer with ASCII representation of the float number. -// Avoids the use of any floating point instructions (since these aren't -// supported on many microcontrollers) and as a consequence prints values with -// power-of-two exponents. -char* FastFloatToBufferLeft(float f, char* buffer) { - char* current = buffer; - char* current_end = buffer + (kFastToBufferSize - 1); - // Access the bit fields of the floating point value to avoid requiring any - // float instructions. These constants are derived from IEEE 754. - const uint32_t sign_mask = 0x80000000; - const uint32_t exponent_mask = 0x7f800000; - const int32_t exponent_shift = 23; - const int32_t exponent_bias = 127; - const uint32_t fraction_mask = 0x007fffff; - uint32_t u; - memcpy(&u, &f, sizeof(int32_t)); - const int32_t exponent = - ((u & exponent_mask) >> exponent_shift) - exponent_bias; - const uint32_t fraction = (u & fraction_mask); - // Expect ~0x2B1B9D3 for fraction. - if (u & sign_mask) { - *current = '-'; - current += 1; - } - *current = 0; - // These are special cases for infinities and not-a-numbers. - if (exponent == 128) { - if (fraction == 0) { - current = StrCatStr(current, (current_end - current), "Inf"); - return current; - } else { - current = StrCatStr(current, (current_end - current), "NaN"); - return current; - } - } - // 0x007fffff (8388607) represents 0.99... for the fraction, so to print the - // correct decimal digits we need to scale our value before passing it to the - // conversion function. This scale should be 10000000/8388608 = 1.1920928955. - // We can approximate this using multiply-adds and right-shifts using the - // values in this array. The 1. portion of the number string is printed out - // in a fixed way before the fraction, below. - const int32_t scale_shifts_size = 13; - const int8_t scale_shifts[13] = {3, 4, 8, 11, 13, 14, 17, - 18, 19, 20, 21, 22, 23}; - uint32_t scaled_fraction = fraction; - for (int i = 0; i < scale_shifts_size; ++i) { - scaled_fraction += (fraction >> scale_shifts[i]); - } - *current = '1'; - current += 1; - *current = '.'; - current += 1; - *current = 0; - - // Prepend leading zeros to fill in all 7 bytes of the fraction. Truncate - // zeros off the end of the fraction. Every fractional value takes 7 bytes. - // For example, 2500 would be written into the buffer as 0002500 since it - // represents .00025. - constexpr int kMaxFractionalDigits = 7; - - // Abort early if there is not enough space in the buffer. - if (current_end - current <= kMaxFractionalDigits) { - return current; - } - - // Pre-fill buffer with zeros to ensure zero-truncation works properly. - for (int i = 1; i < kMaxFractionalDigits; i++) { - *(current + i) = '0'; - } - - // Track how large the fraction is to add leading zeros. - char* previous = current; - current = StrCatUInt32(current, (current_end - current), scaled_fraction, 10); - int fraction_digits = current - previous; - int leading_zeros = kMaxFractionalDigits - fraction_digits; - - // Overwrite the null terminator from StrCatUInt32 to ensure zero-trunctaion - // works properly. - *current = '0'; - - // Shift fraction values and prepend zeros if necessary. - if (leading_zeros != 0) { - for (int i = 0; i < fraction_digits; i++) { - current--; - *(current + leading_zeros) = *current; - *current = '0'; - } - current += kMaxFractionalDigits; - } - - // Truncate trailing zeros for cleaner logs. Ensure we leave at least one - // fractional character for the case when scaled_fraction is 0. - while (*(current - 1) == '0' && (current - 1) > previous) { - current--; - } - *current = 0; - current = StrCatStr(current, (current_end - current), "*2^"); - current = StrCatInt32(current, (current_end - current), exponent); - return current; -} - -int FormatInt32(char* output, int32_t i) { - return static_cast(FastInt32ToBufferLeft(i, output) - output); -} - -int FormatUInt32(char* output, uint32_t i) { - return static_cast(FastUInt32ToBufferLeft(i, output, 10) - output); -} - -int FormatHex(char* output, uint32_t i) { - return static_cast(FastUInt32ToBufferLeft(i, output, 16) - output); -} - -int FormatFloat(char* output, float i) { - return static_cast(FastFloatToBufferLeft(i, output) - output); -} - -} // namespace - -extern "C" int MicroVsnprintf(char* output, int len, const char* format, - va_list args) { - int output_index = 0; - const char* current = format; - // One extra character must be left for the null terminator. - const int usable_length = len - 1; - while (*current != '\0' && output_index < usable_length) { - if (*current == '%') { - current++; - switch (*current) { - case 'd': - // Cut off log message if format could exceed log buffer length. - if (usable_length - output_index < kMaxIntCharsNeeded) { - output[output_index++] = '\0'; - return output_index; - } - output_index += - FormatInt32(&output[output_index], va_arg(args, int32_t)); - current++; - break; - case 'u': - if (usable_length - output_index < kMaxIntCharsNeeded) { - output[output_index++] = '\0'; - return output_index; - } - output_index += - FormatUInt32(&output[output_index], va_arg(args, uint32_t)); - current++; - break; - case 'x': - if (usable_length - output_index < kMaxHexCharsNeeded) { - output[output_index++] = '\0'; - return output_index; - } - output[output_index++] = '0'; - output[output_index++] = 'x'; - output_index += - FormatHex(&output[output_index], va_arg(args, uint32_t)); - current++; - break; - case 'f': - if (usable_length - output_index < kMaxFloatCharsNeeded) { - output[output_index++] = '\0'; - return output_index; - } - output_index += - FormatFloat(&output[output_index], va_arg(args, double)); - current++; - break; - case '%': - output[output_index++] = *current++; - break; - case 'c': - if (usable_length - output_index < 1) { - output[output_index++] = '\0'; - return output_index; - } - output[output_index++] = va_arg(args, int32_t); - current++; - break; - case 's': - char* string = va_arg(args, char*); - int string_idx = 0; - while (string_idx + output_index < usable_length && - string[string_idx] != '\0') { - output[output_index++] = string[string_idx++]; - } - current++; - } - } else { - output[output_index++] = *current++; - } - } - output[output_index++] = '\0'; - return output_index; -} - -extern "C" int MicroSnprintf(char* output, int len, const char* format, ...) { - va_list args; - va_start(args, format); - int bytes_written = MicroVsnprintf(output, len, format, args); - va_end(args); - return bytes_written; -} diff --git a/code/components/tflite-lib/tensorflow/lite/micro/micro_string.h b/code/components/tflite-lib/tensorflow/lite/micro/micro_string.h deleted file mode 100644 index 59303e82..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/micro_string.h +++ /dev/null @@ -1,33 +0,0 @@ -/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_MICRO_MICRO_STRING_H_ -#define TENSORFLOW_LITE_MICRO_MICRO_STRING_H_ - -#include - -// Implements simple string formatting for numeric types. Returns the number of -// bytes written to output. -extern "C" { -// Functionally equivalent to vsnprintf, trimmed down for TFLite Micro. -// MicroSnprintf() is implemented using MicroVsnprintf(). -int MicroVsnprintf(char* output, int len, const char* format, va_list args); -// Functionally equavalent to snprintf, trimmed down for TFLite Micro. -// For example, MicroSnprintf(buffer, 10, "int %d", 10) will put the string -// "int 10" in the buffer. -// Floating point values are logged in exponent notation (1.XXX*2^N). -int MicroSnprintf(char* output, int len, const char* format, ...); -} - -#endif // TENSORFLOW_LITE_MICRO_MICRO_STRING_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/micro_time.cc b/code/components/tflite-lib/tensorflow/lite/micro/micro_time.cc deleted file mode 100644 index 2d74fdba..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/micro_time.cc +++ /dev/null @@ -1,58 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -// Reference implementation of timer functions. Platforms are not required to -// implement these timer methods, but they are required to enable profiling. - -// On platforms that have a POSIX stack or C library, it can be written using -// methods from or clock() from . - -// To add an equivalent function for your own platform, create your own -// implementation file, and place it in a subfolder with named after the OS -// you're targeting. For example, see the Cortex M bare metal version in -// tensorflow/lite/micro/bluepill/micro_time.cc - -#include "tensorflow/lite/micro/micro_time.h" - -#if defined(TF_LITE_USE_CTIME) -#include -#endif - -namespace tflite { - -#if !defined(TF_LITE_USE_CTIME) - -// Reference implementation of the ticks_per_second() function that's required -// for a platform to support Tensorflow Lite for Microcontrollers profiling. -// This returns 0 by default because timing is an optional feature that builds -// without errors on platforms that do not need it. -uint32_t ticks_per_second() { return 0; } - -// Reference implementation of the GetCurrentTimeTicks() function that's -// required for a platform to support Tensorflow Lite for Microcontrollers -// profiling. This returns 0 by default because timing is an optional feature -// that builds without errors on platforms that do not need it. -uint32_t GetCurrentTimeTicks() { return 0; } - -#else // defined(TF_LITE_USE_CTIME) - -// For platforms that support ctime, we implment the micro_time interface in -// this central location. -uint32_t ticks_per_second() { return CLOCKS_PER_SEC; } - -uint32_t GetCurrentTimeTicks() { return clock(); } -#endif - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/micro_time.h b/code/components/tflite-lib/tensorflow/lite/micro/micro_time.h deleted file mode 100644 index 7a8ab455..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/micro_time.h +++ /dev/null @@ -1,36 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_MICRO_MICRO_TIME_H_ -#define TENSORFLOW_LITE_MICRO_MICRO_TIME_H_ - -#include - -namespace tflite { - -// These functions should be implemented by each target platform, and provide an -// accurate tick count along with how many ticks there are per second. -uint32_t ticks_per_second(); - -// Return time in ticks. The meaning of a tick varies per platform. -uint32_t GetCurrentTimeTicks(); - -inline uint32_t TicksToMs(int32_t ticks) { - return static_cast(1000.0f * static_cast(ticks) / - static_cast(ticks_per_second())); -} - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_MICRO_TIME_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/micro_utils.cc b/code/components/tflite-lib/tensorflow/lite/micro/micro_utils.cc deleted file mode 100644 index 97b83695..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/micro_utils.cc +++ /dev/null @@ -1,91 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/micro_utils.h" - -#include -#include -#include - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/op_macros.h" -#include "tensorflow/lite/micro/memory_helpers.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" - -namespace tflite { - -int ElementCount(const TfLiteIntArray& dims) { - int result = 1; - for (int i = 0; i < dims.size; ++i) { - result *= dims.data[i]; - } - return result; -} - -size_t EvalTensorBytes(const TfLiteEvalTensor* tensor) { - size_t bytes_per_element; - TFLITE_DCHECK(kTfLiteOk == - TfLiteTypeSizeOf(tensor->type, &bytes_per_element)); - return ElementCount(*tensor->dims) * bytes_per_element; -} - -void SignedSymmetricPerChannelQuantize(const float* values, - TfLiteIntArray* dims, - int quantized_dimension, - int8_t* quantized_values, - float* scaling_factors) { - int input_size = ElementCount(*dims); - int channel_count = dims->data[quantized_dimension]; - int per_channel_size = input_size / channel_count; - - int stride; - int channel_stride; - if (quantized_dimension == 0) { - stride = 1; - channel_stride = per_channel_size; - } else if (quantized_dimension == 3) { - stride = channel_count; - channel_stride = 1; - } else { - MicroPrintf("quantized dimension must be 0 or 3"); - TFLITE_ABORT; - } - - // Calculate scales for each channel. - for (int channel = 0; channel < channel_count; channel++) { - float min = 0; - float max = 0; - - for (int i = 0; i < per_channel_size; i++) { - int idx = channel * channel_stride + i * stride; - min = fminf(min, values[idx]); - max = fmaxf(max, values[idx]); - } - scaling_factors[channel] = - fmaxf(fabs(min), fabs(max)) / std::numeric_limits::max(); - for (int i = 0; i < per_channel_size; i++) { - int idx = channel * channel_stride + i * stride; - const int32_t quantized_value = - static_cast(roundf(values[idx] / scaling_factors[channel])); - // Clamp: just in case some odd numeric offset. - quantized_values[idx] = - fminf(std::numeric_limits::max(), - fmaxf(std::numeric_limits::min() + 1, quantized_value)); - } - } -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/micro_utils.h b/code/components/tflite-lib/tensorflow/lite/micro/micro_utils.h deleted file mode 100644 index 84d5c437..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/micro_utils.h +++ /dev/null @@ -1,143 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_MICRO_UTILS_H_ -#define TENSORFLOW_LITE_MICRO_MICRO_UTILS_H_ - -#include -#include -#include -#include - -#include "tensorflow/lite/c/common.h" - -namespace tflite { - -// Returns number of elements in the shape array. - -int ElementCount(const TfLiteIntArray& dims); - -size_t EvalTensorBytes(const TfLiteEvalTensor* tensor); - -// C++11 does not support constexpr max; hence, use ternary conditional to -// create our own constexpr Max function. -constexpr int Max(int a, int b) { return a >= b ? a : b; } - -// Converts a float value into a quantized value. Note that large values (close -// to max int and min int) may see significant error due to a lack of floating -// point granularity for large values. -template -T FloatToQuantizedType(const float value, const float scale, int zero_point) { - int32_t result = round(value / scale) + zero_point; - result = - std::max(static_cast(std::numeric_limits::min()), result); - result = - std::min(static_cast(std::numeric_limits::max()), result); - return result; -} - -template -T FloatToSymmetricQuantizedType(const float value, const float scale) { - // 64-bit values are required since 8x16 conv accumulates to int64, meaning - // an int64 bias is required. - std::int64_t result = round(value / scale); - result = std::max( - static_cast(std::numeric_limits::min() + 1), result); - result = std::min(static_cast(std::numeric_limits::max()), - result); - return result; -} - -// Helper methods to quantize arrays of floats to the desired format. -// -// There are several key flavors of quantization in TfLite: -// asymmetric symmetric per channel -// int8_t | X | X | X | -// uint8_t | X | X | | -// int16_t | X | | | -// int32_t | | X | X | -// -// The per-op quantization spec can be found here: -// https://www.tensorflow.org/lite/performance/quantization_spec -template -void Quantize(const float* input, T* output, int num_elements, float scale, - int zero_point) { - for (int i = 0; i < num_elements; i++) { - output[i] = FloatToQuantizedType(input[i], scale, zero_point); - } -} - -template -void SymmetricQuantize(const float* input, T* output, int num_elements, - float scale) { - for (int i = 0; i < num_elements; i++) { - output[i] = FloatToSymmetricQuantizedType(input[i], scale); - } -} - -template -void SymmetricPerChannelQuantize(const float* input, T* output, - int num_elements, int num_channels, - float* scales) { - int elements_per_channel = num_elements / num_channels; - for (int i = 0; i < num_channels; i++) { - for (int j = 0; j < elements_per_channel; j++) { - output[i * elements_per_channel + j] = FloatToSymmetricQuantizedType( - input[i * elements_per_channel + j], scales[i]); - } - } -} - -void SignedSymmetricPerChannelQuantize(const float* values, - TfLiteIntArray* dims, - int quantized_dimension, - int8_t* quantized_values, - float* scaling_factor); - -// Quantizes inputs based on the values provided, choosing the smallest range -// which includes all input values. -template -void SymmetricQuantizeCalculateScales(const float* values, TfLiteIntArray* dims, - T* output, float* scale) { - int input_size = ElementCount(*dims); - - float min = 0; - float max = 0; - for (int i = 0; i < input_size; i++) { - min = fminf(min, values[i]); - max = fmaxf(max, values[i]); - } - *scale = fmaxf(std::abs(min), std::abs(max)) / std::numeric_limits::max(); - for (int i = 0; i < input_size; i++) { - const int32_t quantized_value = - static_cast(roundf(values[i] / *scale)); - // Clamp: just in case some odd numeric offset. - quantized_value = fminf(std::numeric_limits::max(), quantized_value); - quantized_value = fmaxf(std::numeric_limits::min() + 1, quantized_value); - output[i] = quantized_value; - } -} - -template -void Dequantize(const T* values, const int size, const float scale, - int zero_point, float* dequantized_values) { - for (int i = 0; i < size; ++i) { - dequantized_values[i] = (values[i] - zero_point) * scale; - } -} - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_MICRO_UTILS_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/mock_micro_graph.cc b/code/components/tflite-lib/tensorflow/lite/micro/mock_micro_graph.cc deleted file mode 100644 index 438a4065..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/mock_micro_graph.cc +++ /dev/null @@ -1,66 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/mock_micro_graph.h" - -#include "tensorflow/lite/micro/test_helpers.h" - -namespace tflite { - -MockMicroGraph::MockMicroGraph(SingleArenaBufferAllocator* allocator) - : MicroGraph(nullptr, nullptr, nullptr, nullptr), - allocator_(allocator), - init_count_(0), - prepare_count_(0), - free_count_(0) { - memset(invoke_counts_, 0, sizeof(invoke_counts_)); - mock_tensor_ = - reinterpret_cast(allocator_->AllocatePersistentBuffer( - sizeof(TfLiteEvalTensor), alignof(TfLiteEvalTensor))); - int* dims_array = reinterpret_cast( - allocator_->AllocatePersistentBuffer(3 * sizeof(int), alignof(int))); - float* data_array = reinterpret_cast( - allocator_->AllocatePersistentBuffer(2 * sizeof(float), alignof(float))); - int dims[] = {2, 1, 2}; - memcpy(dims_array, dims, 3 * sizeof(int)); - mock_tensor_->dims = testing::IntArrayFromInts(dims_array); - mock_tensor_->data.f = data_array; - mock_tensor_->type = kTfLiteFloat32; -} - -TfLiteStatus MockMicroGraph::InvokeSubgraph(int subgraph_idx) { - invoke_counts_[subgraph_idx]++; - return kTfLiteOk; -} - -TfLiteStatus MockMicroGraph::ResetVariableTensors() { return kTfLiteOk; } - -size_t MockMicroGraph::NumSubgraphInputs(int subgraph_idx) { return 1; } - -TfLiteEvalTensor* MockMicroGraph::GetSubgraphInput(int subgraph_idx, - int tensor_idx) { - return mock_tensor_; -} - -size_t MockMicroGraph::NumSubgraphOutputs(int subgraph_idx) { return 1; } - -TfLiteEvalTensor* MockMicroGraph::GetSubgraphOutput(int subgraph_idx, - int tensor_idx) { - return mock_tensor_; -} - -int MockMicroGraph::NumSubgraphs() { return kMaxSubgraphs; } - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/mock_micro_graph.h b/code/components/tflite-lib/tensorflow/lite/micro/mock_micro_graph.h deleted file mode 100644 index 3ae7d7cf..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/mock_micro_graph.h +++ /dev/null @@ -1,60 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_MOCK_MICRO_GRAPH_H_ -#define TENSORFLOW_LITE_MICRO_MOCK_MICRO_GRAPH_H_ - -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/micro/micro_allocator.h" -#include "tensorflow/lite/micro/micro_graph.h" -#include "tensorflow/lite/schema/schema_generated.h" - -namespace tflite { - -// MockMicroGraph stubs out all MicroGraph methods used during invoke. A count -// of the number of calls to invoke for each subgraph is maintained for -// validation of control flow operators. -class MockMicroGraph : public MicroGraph { - public: - explicit MockMicroGraph(SingleArenaBufferAllocator* allocator); - TfLiteStatus InvokeSubgraph(int subgraph_idx) override; - TfLiteStatus ResetVariableTensors() override; - size_t NumSubgraphInputs(int subgraph_idx) override; - TfLiteEvalTensor* GetSubgraphInput(int subgraph_idx, int tensor_idx) override; - size_t NumSubgraphOutputs(int subgraph_idx) override; - TfLiteEvalTensor* GetSubgraphOutput(int subgraph_idx, - int tensor_idx) override; - int NumSubgraphs() override; - int get_init_count() const { return init_count_; } - int get_prepare_count() const { return prepare_count_; } - int get_free_count() const { return free_count_; } - int get_invoke_count(int subgraph_idx) const { - return invoke_counts_[subgraph_idx]; - } - - private: - static constexpr int kMaxSubgraphs = 10; - SingleArenaBufferAllocator* allocator_; - TfLiteEvalTensor* mock_tensor_; - int init_count_; - int prepare_count_; - int free_count_; - int invoke_counts_[kMaxSubgraphs]; - TF_LITE_REMOVE_VIRTUAL_DELETE -}; - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_MOCK_MICRO_GRAPH_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/recording_micro_allocator.cc b/code/components/tflite-lib/tensorflow/lite/micro/recording_micro_allocator.cc deleted file mode 100644 index 61f7816a..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/recording_micro_allocator.cc +++ /dev/null @@ -1,262 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/recording_micro_allocator.h" - -#include "tensorflow/lite/core/api/error_reporter.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/micro/arena_allocator/recording_single_arena_buffer_allocator.h" -#include "tensorflow/lite/micro/compatibility.h" -#include "tensorflow/lite/micro/memory_helpers.h" -#include "tensorflow/lite/micro/memory_planner/greedy_memory_planner.h" -#include "tensorflow/lite/micro/micro_allocator.h" -#include "tensorflow/lite/micro/micro_error_reporter.h" - -namespace tflite { - -size_t RecordingMicroAllocator::GetDefaultTailUsage() { - // RecordingMicroAllocator inherits from MicroAllocator and its tail usage is - // similar with MicroAllocator with SingleArenaBufferAllocator and - // MicroAllocator being replaced. - // TODO(b/208703041): a template version of AlignSizeUp to make expression - // shorter. - return MicroAllocator::GetDefaultTailUsage( - /*is_memory_planner_given=*/false) + - AlignSizeUp(sizeof(RecordingSingleArenaBufferAllocator), - alignof(RecordingSingleArenaBufferAllocator)) - - AlignSizeUp(sizeof(SingleArenaBufferAllocator), - alignof(SingleArenaBufferAllocator)) + - AlignSizeUp(sizeof(RecordingMicroAllocator), - alignof(RecordingMicroAllocator)) - - AlignSizeUp(sizeof(MicroAllocator), alignof(MicroAllocator)); -} - -RecordingMicroAllocator::RecordingMicroAllocator( - RecordingSingleArenaBufferAllocator* recording_memory_allocator, - MicroMemoryPlanner* memory_planner, ErrorReporter* error_reporter) - : MicroAllocator(recording_memory_allocator, memory_planner, - error_reporter), - recording_memory_allocator_(recording_memory_allocator) {} - -RecordingMicroAllocator* RecordingMicroAllocator::Create( - uint8_t* tensor_arena, size_t arena_size, ErrorReporter* error_reporter) { - TFLITE_DCHECK(error_reporter != nullptr); - - RecordingSingleArenaBufferAllocator* simple_memory_allocator = - RecordingSingleArenaBufferAllocator::Create(error_reporter, tensor_arena, - arena_size); - TFLITE_DCHECK(simple_memory_allocator != nullptr); - - uint8_t* memory_planner_buffer = - simple_memory_allocator->AllocatePersistentBuffer( - sizeof(GreedyMemoryPlanner), alignof(GreedyMemoryPlanner)); - GreedyMemoryPlanner* memory_planner = - new (memory_planner_buffer) GreedyMemoryPlanner(); - - uint8_t* allocator_buffer = simple_memory_allocator->AllocatePersistentBuffer( - sizeof(RecordingMicroAllocator), alignof(RecordingMicroAllocator)); - RecordingMicroAllocator* allocator = - new (allocator_buffer) RecordingMicroAllocator( - simple_memory_allocator, memory_planner, error_reporter); - return allocator; -} - -RecordedAllocation RecordingMicroAllocator::GetRecordedAllocation( - RecordedAllocationType allocation_type) const { - switch (allocation_type) { - case RecordedAllocationType::kTfLiteEvalTensorData: - return recorded_tflite_eval_tensor_data_; - case RecordedAllocationType::kPersistentTfLiteTensorData: - return recorded_persistent_tflite_tensor_data_; - case RecordedAllocationType::kPersistentTfLiteTensorQuantizationData: - return recorded_persistent_tflite_tensor_quantization_data_; - case RecordedAllocationType::kPersistentBufferData: - return recorded_persistent_buffer_data_; - case RecordedAllocationType::kTfLiteTensorVariableBufferData: - return recorded_tflite_tensor_variable_buffer_data_; - case RecordedAllocationType::kNodeAndRegistrationArray: - return recorded_node_and_registration_array_data_; - case RecordedAllocationType::kOpData: - return recorded_op_data_; - } - TF_LITE_REPORT_ERROR(error_reporter(), "Invalid allocation type supplied: %d", - allocation_type); - return RecordedAllocation(); -} - -const RecordingSingleArenaBufferAllocator* -RecordingMicroAllocator::GetSimpleMemoryAllocator() const { - return recording_memory_allocator_; -} - -void RecordingMicroAllocator::PrintAllocations() const { - TF_LITE_REPORT_ERROR( - error_reporter(), - "[RecordingMicroAllocator] Arena allocation total %d bytes", - recording_memory_allocator_->GetUsedBytes()); - TF_LITE_REPORT_ERROR( - error_reporter(), - "[RecordingMicroAllocator] Arena allocation head %d bytes", - recording_memory_allocator_->GetNonPersistentUsedBytes()); - TF_LITE_REPORT_ERROR( - error_reporter(), - "[RecordingMicroAllocator] Arena allocation tail %d bytes", - recording_memory_allocator_->GetPersistentUsedBytes()); - PrintRecordedAllocation(RecordedAllocationType::kTfLiteEvalTensorData, - "TfLiteEvalTensor data", "allocations"); - PrintRecordedAllocation(RecordedAllocationType::kPersistentTfLiteTensorData, - "Persistent TfLiteTensor data", "tensors"); - PrintRecordedAllocation( - RecordedAllocationType::kPersistentTfLiteTensorQuantizationData, - "Persistent TfLiteTensor quantization data", "allocations"); - PrintRecordedAllocation(RecordedAllocationType::kPersistentBufferData, - "Persistent buffer data", "allocations"); - PrintRecordedAllocation( - RecordedAllocationType::kTfLiteTensorVariableBufferData, - "TfLiteTensor variable buffer data", "allocations"); - PrintRecordedAllocation(RecordedAllocationType::kNodeAndRegistrationArray, - "NodeAndRegistration struct", - "NodeAndRegistration structs"); - PrintRecordedAllocation(RecordedAllocationType::kOpData, - "Operator runtime data", "OpData structs"); -} - -void* RecordingMicroAllocator::AllocatePersistentBuffer(size_t bytes) { - RecordedAllocation allocations = SnapshotAllocationUsage(); - void* buffer = MicroAllocator::AllocatePersistentBuffer(bytes); - RecordAllocationUsage(allocations, recorded_persistent_buffer_data_); - - return buffer; -} - -void RecordingMicroAllocator::PrintRecordedAllocation( - RecordedAllocationType allocation_type, const char* allocation_name, - const char* allocation_description) const { -#ifndef TF_LITE_STRIP_ERROR_STRINGS - RecordedAllocation allocation = GetRecordedAllocation(allocation_type); - if (allocation.used_bytes > 0 || allocation.requested_bytes > 0) { - TF_LITE_REPORT_ERROR( - error_reporter(), - "[RecordingMicroAllocator] '%s' used %d bytes with alignment overhead " - "(requested %d bytes for %d %s)", - allocation_name, allocation.used_bytes, allocation.requested_bytes, - allocation.count, allocation_description); - } -#endif -} - -TfLiteStatus RecordingMicroAllocator::AllocateNodeAndRegistrations( - const Model* model, SubgraphAllocations* subgraph_allocations) { - RecordedAllocation allocations = SnapshotAllocationUsage(); - - TfLiteStatus status = - MicroAllocator::AllocateNodeAndRegistrations(model, subgraph_allocations); - for (size_t subgraph_idx = 0; subgraph_idx < model->subgraphs()->size(); - subgraph_idx++) { - RecordAllocationUsage(allocations, - recorded_node_and_registration_array_data_); - // The allocation count in SingleArenaBufferAllocator will only be 1. To - // provide better logging, decrement by 1 and add in the actual number of - // operators used in the graph: The allocation for this recording will - // always be 1. This is because the parent class mallocs one large - // allocation for the number of nodes in the graph (e.g. - // sizeof(NodeAndRegistration) * num_nodes). To prevent extra overhead and - // potential for fragmentation, manually adjust the accounting by - // decrementing by 1 and adding the actual number of nodes used in the - // graph: - recorded_node_and_registration_array_data_.count += - model->subgraphs()->Get(subgraph_idx)->operators()->size() - 1; - } - return status; -} - -TfLiteStatus RecordingMicroAllocator::AllocateTfLiteEvalTensors( - const Model* model, SubgraphAllocations* subgraph_allocations) { - RecordedAllocation allocations = SnapshotAllocationUsage(); - - TfLiteStatus status = - MicroAllocator::AllocateTfLiteEvalTensors(model, subgraph_allocations); - for (size_t subgraph_idx = 0; subgraph_idx < model->subgraphs()->size(); - subgraph_idx++) { - RecordAllocationUsage(allocations, recorded_tflite_eval_tensor_data_); - // The allocation for this recording will always be 1. This is because the - // parent class mallocs one large allocation for the number of tensors in - // the graph (e.g. sizeof(TfLiteEvalTensor) * num_tensors). To prevent extra - // overhead and potential for fragmentation, manually adjust the accounting - // by decrementing by 1 and adding the actual number of tensors used in the - // graph: - recorded_tflite_eval_tensor_data_.count += - model->subgraphs()->Get(subgraph_idx)->tensors()->size() - 1; - } - return status; -} - -TfLiteStatus RecordingMicroAllocator::AllocateVariables( - const SubGraph* subgraph, TfLiteEvalTensor* eval_tensors) { - RecordedAllocation allocations = SnapshotAllocationUsage(); - - TfLiteStatus status = - MicroAllocator::AllocateVariables(subgraph, eval_tensors); - - RecordAllocationUsage(allocations, - recorded_tflite_tensor_variable_buffer_data_); - return status; -} - -TfLiteTensor* -RecordingMicroAllocator::AllocatePersistentTfLiteTensorInternal() { - RecordedAllocation allocations = SnapshotAllocationUsage(); - - TfLiteTensor* result = - MicroAllocator::AllocatePersistentTfLiteTensorInternal(); - - RecordAllocationUsage(allocations, recorded_persistent_tflite_tensor_data_); - return result; -} - -TfLiteStatus RecordingMicroAllocator::PopulateTfLiteTensorFromFlatbuffer( - const Model* model, TfLiteTensor* tensor, int tensor_index, - int subgraph_index, bool allocate_temp) { - RecordedAllocation allocations = SnapshotAllocationUsage(); - - TfLiteStatus status = MicroAllocator::PopulateTfLiteTensorFromFlatbuffer( - model, tensor, tensor_index, subgraph_index, allocate_temp); - - RecordAllocationUsage(allocations, - recorded_persistent_tflite_tensor_quantization_data_); - return status; -} - -RecordedAllocation RecordingMicroAllocator::SnapshotAllocationUsage() const { - return {/*requested_bytes=*/recording_memory_allocator_->GetRequestedBytes(), - /*used_bytes=*/recording_memory_allocator_->GetUsedBytes(), - /*count=*/recording_memory_allocator_->GetAllocatedCount()}; -} - -void RecordingMicroAllocator::RecordAllocationUsage( - const RecordedAllocation& snapshotted_allocation, - RecordedAllocation& recorded_allocation) { - recorded_allocation.requested_bytes += - recording_memory_allocator_->GetRequestedBytes() - - snapshotted_allocation.requested_bytes; - recorded_allocation.used_bytes += - recording_memory_allocator_->GetUsedBytes() - - snapshotted_allocation.used_bytes; - recorded_allocation.count += - recording_memory_allocator_->GetAllocatedCount() - - snapshotted_allocation.count; -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/recording_micro_allocator.h b/code/components/tflite-lib/tensorflow/lite/micro/recording_micro_allocator.h deleted file mode 100644 index 699b1a22..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/recording_micro_allocator.h +++ /dev/null @@ -1,126 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_RECORDING_MICRO_ALLOCATOR_H_ -#define TENSORFLOW_LITE_MICRO_RECORDING_MICRO_ALLOCATOR_H_ - -#include "tensorflow/lite/micro/arena_allocator/recording_single_arena_buffer_allocator.h" -#include "tensorflow/lite/micro/compatibility.h" -#include "tensorflow/lite/micro/micro_allocator.h" - -namespace tflite { - -// List of buckets currently recorded by this class. Each type keeps a list of -// allocated information during model initialization. -// TODO(b/169834511): Add tracking for scratch buffer allocations. -enum class RecordedAllocationType { - kTfLiteEvalTensorData, - kPersistentTfLiteTensorData, - kPersistentTfLiteTensorQuantizationData, - kPersistentBufferData, - kTfLiteTensorVariableBufferData, - kNodeAndRegistrationArray, - kOpData, -}; - -// Container for holding information about allocation recordings by a given -// type. Each recording contains the number of bytes requested, the actual bytes -// allocated (can defer from requested by alignment), and the number of items -// allocated. -struct RecordedAllocation { - size_t requested_bytes; - size_t used_bytes; - size_t count; -}; - -// Utility subclass of MicroAllocator that records all allocations -// inside the arena. A summary of allocations can be logged through the -// ErrorReporter by invoking LogAllocations(). This special allocator requires -// an instance of RecordingSingleArenaBufferAllocator to capture allocations in -// the head and tail. Arena allocation recording can be retrieved by type -// through the GetRecordedAllocation() function. This class should only be used -// for auditing memory usage or integration testing. -class RecordingMicroAllocator : public MicroAllocator { - public: - static RecordingMicroAllocator* Create(uint8_t* tensor_arena, - size_t arena_size, - ErrorReporter* error_reporter); - - // Returns the fixed amount of memory overhead of RecordingMicroAllocator. - static size_t GetDefaultTailUsage(); - - // Returns the recorded allocations information for a given allocation type. - RecordedAllocation GetRecordedAllocation( - RecordedAllocationType allocation_type) const; - - const RecordingSingleArenaBufferAllocator* GetSimpleMemoryAllocator() const; - - // Logs out through the ErrorReporter all allocation recordings by type - // defined in RecordedAllocationType. - void PrintAllocations() const; - - void* AllocatePersistentBuffer(size_t bytes) override; - - protected: - TfLiteStatus AllocateNodeAndRegistrations( - const Model* model, SubgraphAllocations* subgraph_allocations) override; - TfLiteStatus AllocateTfLiteEvalTensors( - const Model* model, SubgraphAllocations* subgraph_allocations) override; - TfLiteStatus AllocateVariables(const SubGraph* subgraph, - TfLiteEvalTensor* eval_tensors) override; - // TODO(b/162311891): Once all kernels have been updated to the new API drop - // this method. It is only used to record TfLiteTensor persistent allocations. - TfLiteTensor* AllocatePersistentTfLiteTensorInternal() override; - - // TODO(b/162311891): Once all kernels have been updated to the new API drop - // this function since all allocations for quantized data will take place in - // the temp section. - TfLiteStatus PopulateTfLiteTensorFromFlatbuffer(const Model* model, - TfLiteTensor* tensor, - int tensor_index, - int subgraph_index, - bool allocate_temp) override; - - private: - RecordingMicroAllocator(RecordingSingleArenaBufferAllocator* memory_allocator, - MicroMemoryPlanner* memory_planner, - ErrorReporter* error_reporter); - - void PrintRecordedAllocation(RecordedAllocationType allocation_type, - const char* allocation_name, - const char* allocation_description) const; - - RecordedAllocation SnapshotAllocationUsage() const; - void RecordAllocationUsage(const RecordedAllocation& snapshotted_allocation, - RecordedAllocation& recorded_allocation); - - const RecordingSingleArenaBufferAllocator* recording_memory_allocator_; - - RecordedAllocation recorded_tflite_eval_tensor_data_ = {}; - RecordedAllocation recorded_persistent_tflite_tensor_data_ = {}; - RecordedAllocation recorded_persistent_tflite_tensor_quantization_data_ = {}; - RecordedAllocation recorded_persistent_buffer_data_ = {}; - RecordedAllocation recorded_tflite_tensor_variable_buffer_data_ = {}; - RecordedAllocation recorded_node_and_registration_array_data_ = {}; - - // TODO(b/187993291): Re-enable OpData allocating tracking. - RecordedAllocation recorded_op_data_ = {}; - - TF_LITE_REMOVE_VIRTUAL_DELETE -}; - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_RECORDING_MICRO_ALLOCATOR_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/recording_micro_interpreter.h b/code/components/tflite-lib/tensorflow/lite/micro/recording_micro_interpreter.h deleted file mode 100644 index 6d7602be..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/recording_micro_interpreter.h +++ /dev/null @@ -1,70 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_RECORDING_MICRO_INTERPRETER_H_ -#define TENSORFLOW_LITE_MICRO_RECORDING_MICRO_INTERPRETER_H_ - -#include "tensorflow/lite/micro/micro_interpreter.h" -#include "tensorflow/lite/micro/recording_micro_allocator.h" - -namespace tflite { - -// Utility subclass that enables internal recordings of the MicroInterpreter. -// This class should be used to audit and analyze memory arena usage for a given -// model and interpreter. -// -// After construction and the first Invoke() or AllocateTensors() call - the -// memory usage is recorded and available through the GetMicroAllocator() -// function. See RecordingMicroAlloctor for more details on what is currently -// recorded from arena allocations. -// -// It is recommended for users to increase the tensor arena size by at least 1kb -// to ensure enough additional memory is available for internal recordings. -class RecordingMicroInterpreter : public MicroInterpreter { - public: - RecordingMicroInterpreter(const Model* model, - const MicroOpResolver& op_resolver, - uint8_t* tensor_arena, size_t tensor_arena_size, - ErrorReporter* error_reporter, - MicroResourceVariables* resource_variable = nullptr, - MicroProfiler* profiler = nullptr) - : MicroInterpreter(model, op_resolver, - RecordingMicroAllocator::Create( - tensor_arena, tensor_arena_size, error_reporter), - error_reporter, resource_variable, profiler), - recording_micro_allocator_( - static_cast(allocator())) {} - - RecordingMicroInterpreter(const Model* model, - const MicroOpResolver& op_resolver, - RecordingMicroAllocator* allocator, - ErrorReporter* error_reporter, - MicroResourceVariables* resource_variable = nullptr, - MicroProfiler* profiler = nullptr) - : MicroInterpreter(model, op_resolver, allocator, error_reporter, - resource_variable, profiler), - recording_micro_allocator_(*allocator) {} - - const RecordingMicroAllocator& GetMicroAllocator() const { - return recording_micro_allocator_; - } - - private: - const RecordingMicroAllocator& recording_micro_allocator_; -}; - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_RECORDING_MICRO_INTERPRETER_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/system_setup.cc b/code/components/tflite-lib/tensorflow/lite/micro/system_setup.cc deleted file mode 100644 index db4a1007..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/system_setup.cc +++ /dev/null @@ -1,25 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/system_setup.h" - -namespace tflite { - -// To add an equivalent function for your own platform, create your own -// implementation file, and place it in a subfolder named after the target. See -// tensorflow/lite/micro/debug_log.cc for a similar example. -void InitializeTarget() {} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/system_setup.h b/code/components/tflite-lib/tensorflow/lite/micro/system_setup.h deleted file mode 100644 index 71ab13a8..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/system_setup.h +++ /dev/null @@ -1,27 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_MICRO_SYSTEM_SETUP_H_ -#define TENSORFLOW_LITE_MICRO_SYSTEM_SETUP_H_ - -namespace tflite { - -// This should called during initialization of TFLM binaries and tests. It can -// be specialized if there is a need for custom target-specific intialization. -// For more information, see tensorflow/lite/micro/system_setup.cc. -void InitializeTarget(); - -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_SYSTEM_SETUP_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/test_helper_custom_ops.cc b/code/components/tflite-lib/tensorflow/lite/micro/test_helper_custom_ops.cc deleted file mode 100644 index c89483e1..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/test_helper_custom_ops.cc +++ /dev/null @@ -1,113 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/test_helper_custom_ops.h" - -#include -#include -#include -#include -#include - -#include "flatbuffers/flatbuffers.h" // from @flatbuffers -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/core/api/error_reporter.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/all_ops_resolver.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/micro_utils.h" -#include "tensorflow/lite/schema/schema_generated.h" - -// TODO(b/170464050): Use TFLM test only version of schema_utils. - -namespace tflite { -namespace testing { - -const TfLiteRegistration* PackerOp::getRegistration() { - return GetMutableRegistration(); -} - -TfLiteRegistration* PackerOp::GetMutableRegistration() { - static TfLiteRegistration r; - r.init = Init; - r.prepare = Prepare; - r.invoke = Invoke; - r.free = Free; - return &r; -} - -void* PackerOp::Init(TfLiteContext* context, const char* buffer, - size_t length) { - freed_ = false; - // Do nothing. - return nullptr; -} - -void PackerOp::Free(TfLiteContext* context, void* buffer) { freed_ = true; } - -TfLiteStatus PackerOp::Prepare(TfLiteContext* context, TfLiteNode* node) { - return kTfLiteOk; -} - -TfLiteStatus PackerOp::Invoke(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input1 = - tflite::micro::GetEvalInput(context, node, 0); - TF_LITE_ENSURE(context, input1 != nullptr); - const int32_t* input1_data = input1->data.i32; - TF_LITE_ENSURE_EQ(context, input1->dims->size, 1); - const int32_t input1_len = input1->dims->data[0]; - - const TfLiteEvalTensor* input2 = - tflite::micro::GetEvalInput(context, node, 1); - TF_LITE_ENSURE(context, input2 != nullptr); - const int32_t* input2_data = input2->data.i32; - TF_LITE_ENSURE_EQ(context, input2->dims->size, 1); - const int32_t input2_len = input2->dims->data[0]; - - TfLiteEvalTensor* output = tflite::micro::GetEvalOutput(context, node, 0); - TF_LITE_ENSURE(context, output != nullptr); - int32_t* output_data = output->data.i32; - int32_t output_len = output->dims->data[0]; - - // Fill output with input: first with the first tensor, then with the second - // tensor up to the size of the output tensor. - int cnt = 0; - int i; - for (i = 0; i < input1_len && cnt < output_len; i++, cnt++) { - output_data[cnt] = input1_data[i]; - } - if (cnt >= output_len) { - return kTfLiteOk; - } - - for (i = 0; i < input2_len && cnt < output_len; i++, cnt++) { - output_data[cnt] = input2_data[i]; - } - if (cnt >= output_len) { - return kTfLiteOk; - } - - for (; cnt < output_len; cnt++) { - output_data[cnt] = 0; - } - return kTfLiteOk; -} - -bool PackerOp::freed_ = false; - -} // namespace testing -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/test_helper_custom_ops.h b/code/components/tflite-lib/tensorflow/lite/micro/test_helper_custom_ops.h deleted file mode 100644 index b8c025a7..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/test_helper_custom_ops.h +++ /dev/null @@ -1,50 +0,0 @@ -/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_TEST_HELPER_CUSTOM_OPS_H_ -#define TENSORFLOW_LITE_MICRO_TEST_HELPER_CUSTOM_OPS_H_ - -#include -#include - -#include "flatbuffers/flatbuffers.h" // from @flatbuffers -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/micro/all_ops_resolver.h" -#include "tensorflow/lite/micro/micro_utils.h" -#include "tensorflow/lite/portable_type_to_tflitetype.h" -#include "tensorflow/lite/schema/schema_generated.h" - -namespace tflite { -namespace testing { - -class PackerOp { - public: - static const TfLiteRegistration* getRegistration(); - static TfLiteRegistration* GetMutableRegistration(); - static void* Init(TfLiteContext* context, const char* buffer, size_t length); - static void Free(TfLiteContext* context, void* buffer); - static TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node); - static TfLiteStatus Invoke(TfLiteContext* context, TfLiteNode* node); - - private: - static bool freed_; -}; - -} // namespace testing -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_TEST_HELPER_CUSTOM_OPS_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/micro/test_helpers.cc b/code/components/tflite-lib/tensorflow/lite/micro/test_helpers.cc deleted file mode 100644 index 2411bbf8..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/test_helpers.cc +++ /dev/null @@ -1,1914 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/lite/micro/test_helpers.h" - -#include -#include -#include -#include -#include - -#include "flatbuffers/flatbuffers.h" // from @flatbuffers -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/core/api/error_reporter.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/kernels/kernel_util.h" -#include "tensorflow/lite/micro/all_ops_resolver.h" -#include "tensorflow/lite/micro/kernels/kernel_util.h" -#include "tensorflow/lite/micro/memory_helpers.h" -#include "tensorflow/lite/micro/micro_arena_constants.h" -#include "tensorflow/lite/micro/micro_utils.h" -#include "tensorflow/lite/micro/test_helper_custom_ops.h" -#include "tensorflow/lite/schema/schema_generated.h" - -// TODO(b/170464050): Use TFLM test only version of schema_utils. - -namespace tflite { -namespace testing { -namespace { - -class StackAllocator : public flatbuffers::Allocator { - public: - StackAllocator(size_t alignment) : data_size_(0) { - data_ = AlignPointerUp(data_backing_, alignment); - } - - uint8_t* allocate(size_t size) override { - TFLITE_DCHECK((data_size_ + size) <= kStackAllocatorSize); - uint8_t* result = data_; - data_ += size; - data_size_ += size; - return result; - } - - void deallocate(uint8_t* p, size_t) override {} - - static StackAllocator& instance(size_t alignment = 1) { - // Avoid using true dynamic memory allocation to be portable to bare metal. - static char inst_memory[sizeof(StackAllocator)]; - static StackAllocator* inst = new (inst_memory) StackAllocator(alignment); - return *inst; - } - - static constexpr size_t kStackAllocatorSize = 8192; - - private: - uint8_t data_backing_[kStackAllocatorSize]; - uint8_t* data_; - int data_size_; - - TF_LITE_REMOVE_VIRTUAL_DELETE -}; - -flatbuffers::FlatBufferBuilder* BuilderInstance() { - static char inst_memory[sizeof(flatbuffers::FlatBufferBuilder)]; - static flatbuffers::FlatBufferBuilder* inst = - new (inst_memory) flatbuffers::FlatBufferBuilder( - StackAllocator::kStackAllocatorSize, - &StackAllocator::instance(MicroArenaBufferAlignment())); - return inst; -} - -// A wrapper around FlatBuffer API to help build model easily. -class ModelBuilder { - public: - typedef int32_t Tensor; - typedef int Operator; - typedef int Node; - - // `builder` needs to be available until BuildModel is called. - explicit ModelBuilder(flatbuffers::FlatBufferBuilder* builder) - : builder_(builder) {} - - // Registers an operator that will be used in the model. - Operator RegisterOp(BuiltinOperator op, const char* custom_code); - - // Adds a tensor to the model. - Tensor AddTensor(TensorType type, std::initializer_list shape) { - return AddTensorImpl(type, /* is_variable */ false, shape); - } - - // Adds a variable tensor to the model. - Tensor AddVariableTensor(TensorType type, - std::initializer_list shape) { - return AddTensorImpl(type, /* is_variable */ true, shape); - } - - // Adds a node to the model with given input and output Tensors. - Node AddNode(Operator op, std::initializer_list inputs, - std::initializer_list outputs, - std::initializer_list intermediates = - std::initializer_list{}); - - void AddMetadata(const char* description_string, - const int32_t* metadata_buffer_data, size_t num_elements); - - // Constructs the flatbuffer model using `builder_` and return a pointer to - // it. The returned model has the same lifetime as `builder_`. - // Note the default value of 0 for num_subgraph_inputs means all tensor inputs - // are in subgraph input list. - const Model* BuildModel(std::initializer_list inputs, - std::initializer_list outputs, - size_t num_subgraph_inputs = 0); - - private: - // Adds a tensor to the model. - Tensor AddTensorImpl(TensorType type, bool is_variable, - std::initializer_list shape); - - flatbuffers::FlatBufferBuilder* builder_; - - static constexpr int kMaxOperatorCodes = 10; - flatbuffers::Offset operator_codes_[kMaxOperatorCodes]; - int next_operator_code_id_ = 0; - - static constexpr int kMaxOperators = 50; - flatbuffers::Offset operators_[kMaxOperators]; - int next_operator_id_ = 0; - - static constexpr int kMaxTensors = 50; - flatbuffers::Offset tensors_[kMaxTensors]; - - static constexpr int kMaxMetadataBuffers = 10; - - static constexpr int kMaxMetadatas = 10; - flatbuffers::Offset metadata_[kMaxMetadatas]; - - flatbuffers::Offset metadata_buffers_[kMaxMetadataBuffers]; - - int nbr_of_metadata_buffers_ = 0; - - int next_tensor_id_ = 0; -}; - -ModelBuilder::Operator ModelBuilder::RegisterOp(BuiltinOperator op, - const char* custom_code) { - TFLITE_DCHECK(next_operator_code_id_ <= kMaxOperatorCodes); - operator_codes_[next_operator_code_id_] = tflite::CreateOperatorCodeDirect( - *builder_, /*deprecated_builtin_code=*/0, custom_code, /*version=*/0, op); - next_operator_code_id_++; - return next_operator_code_id_ - 1; -} - -ModelBuilder::Node ModelBuilder::AddNode( - ModelBuilder::Operator op, - std::initializer_list inputs, - std::initializer_list outputs, - std::initializer_list intermediates) { - TFLITE_DCHECK(next_operator_id_ <= kMaxOperators); - operators_[next_operator_id_] = tflite::CreateOperator( - *builder_, op, builder_->CreateVector(inputs.begin(), inputs.size()), - builder_->CreateVector(outputs.begin(), outputs.size()), - BuiltinOptions_NONE, - /*builtin_options=*/0, - /*custom_options=*/0, tflite::CustomOptionsFormat_FLEXBUFFERS, - /*mutating_variable_inputs =*/0, - builder_->CreateVector(intermediates.begin(), intermediates.size())); - next_operator_id_++; - return next_operator_id_ - 1; -} - -void ModelBuilder::AddMetadata(const char* description_string, - const int32_t* metadata_buffer_data, - size_t num_elements) { - metadata_[ModelBuilder::nbr_of_metadata_buffers_] = - CreateMetadata(*builder_, builder_->CreateString(description_string), - 1 + ModelBuilder::nbr_of_metadata_buffers_); - - metadata_buffers_[nbr_of_metadata_buffers_] = tflite::CreateBuffer( - *builder_, builder_->CreateVector((uint8_t*)metadata_buffer_data, - sizeof(uint32_t) * num_elements)); - - ModelBuilder::nbr_of_metadata_buffers_++; -} - -const Model* ModelBuilder::BuildModel( - std::initializer_list inputs, - std::initializer_list outputs, - size_t num_subgraph_inputs) { - // Model schema requires an empty buffer at idx 0. - size_t buffer_size = 1 + ModelBuilder::nbr_of_metadata_buffers_; - flatbuffers::Offset buffers[kMaxMetadataBuffers]; - buffers[0] = tflite::CreateBuffer(*builder_); - - // Place the metadata buffers first in the buffer since the indices for them - // have already been set in AddMetadata() - for (int i = 1; i < ModelBuilder::nbr_of_metadata_buffers_ + 1; ++i) { - buffers[i] = metadata_buffers_[i - 1]; - } - - // Default to single subgraph model. - constexpr size_t subgraphs_size = 1; - - // Find out number of subgraph inputs. - if (num_subgraph_inputs == 0) { - // This is the default case. - num_subgraph_inputs = inputs.size(); - } else { - // A non-zero value of num_subgraph_inputs means that some of - // the operator input tensors are not subgraph inputs. - TFLITE_DCHECK(num_subgraph_inputs <= inputs.size()); - } - - const flatbuffers::Offset subgraphs[subgraphs_size] = { - tflite::CreateSubGraph( - *builder_, builder_->CreateVector(tensors_, next_tensor_id_), - builder_->CreateVector(inputs.begin(), num_subgraph_inputs), - builder_->CreateVector(outputs.begin(), outputs.size()), - builder_->CreateVector(operators_, next_operator_id_), - builder_->CreateString("test_subgraph"))}; - - flatbuffers::Offset model_offset; - if (ModelBuilder::nbr_of_metadata_buffers_ > 0) { - model_offset = tflite::CreateModel( - *builder_, 0, - builder_->CreateVector(operator_codes_, next_operator_code_id_), - builder_->CreateVector(subgraphs, subgraphs_size), - builder_->CreateString("teset_model"), - builder_->CreateVector(buffers, buffer_size), 0, - builder_->CreateVector(metadata_, - ModelBuilder::nbr_of_metadata_buffers_)); - } else { - model_offset = tflite::CreateModel( - *builder_, 0, - builder_->CreateVector(operator_codes_, next_operator_code_id_), - builder_->CreateVector(subgraphs, subgraphs_size), - builder_->CreateString("teset_model"), - builder_->CreateVector(buffers, buffer_size)); - } - - tflite::FinishModelBuffer(*builder_, model_offset); - void* model_pointer = builder_->GetBufferPointer(); - const Model* model = flatbuffers::GetRoot(model_pointer); - return model; -} - -ModelBuilder::Tensor ModelBuilder::AddTensorImpl( - TensorType type, bool is_variable, std::initializer_list shape) { - TFLITE_DCHECK(next_tensor_id_ <= kMaxTensors); - tensors_[next_tensor_id_] = tflite::CreateTensor( - *builder_, builder_->CreateVector(shape.begin(), shape.size()), type, - /* buffer */ 0, /* name */ 0, /* quantization */ 0, - /* is_variable */ is_variable, - /* sparsity */ 0); - next_tensor_id_++; - return next_tensor_id_ - 1; -} - -const Model* BuildSimpleStatefulModel() { - using flatbuffers::Offset; - flatbuffers::FlatBufferBuilder* fb_builder = BuilderInstance(); - - ModelBuilder model_builder(fb_builder); - - const int op_id = - model_builder.RegisterOp(BuiltinOperator_CUSTOM, "simple_stateful_op"); - const int input_tensor = model_builder.AddTensor(TensorType_INT8, {3}); - const int median_tensor = model_builder.AddTensor(TensorType_INT8, {3}); - const int invoke_count_tensor = - model_builder.AddTensor(TensorType_INT32, {1}); - const int intermediate_tensor = - model_builder.AddTensor(TensorType_FLOAT32, {0}); - - model_builder.AddNode(op_id, {input_tensor}, - {median_tensor, invoke_count_tensor}, - {intermediate_tensor}); - return model_builder.BuildModel({input_tensor}, - {median_tensor, invoke_count_tensor}); -} - -const Model* BuildSimpleModelWithBranch() { - using flatbuffers::Offset; - flatbuffers::FlatBufferBuilder* fb_builder = BuilderInstance(); - - ModelBuilder model_builder(fb_builder); - /* Model structure - | t0 - +------| - | v - | +---------+ - | | n0 | - | | | - | +---------+ - v + - | - +---------+ | t1 - | n1 | | - | | | - +---------+ | - | | - t2 | v - | +---------+ - +-->| n2 | - | | - +-------|-+ - |t3 - v - */ - const int op_id = - model_builder.RegisterOp(BuiltinOperator_CUSTOM, "mock_custom"); - const int t0 = model_builder.AddTensor(TensorType_FLOAT32, {2, 2, 3}); - const int t1 = model_builder.AddTensor(TensorType_FLOAT32, {2, 2, 3}); - const int t2 = model_builder.AddTensor(TensorType_FLOAT32, {2, 2, 3}); - const int t3 = model_builder.AddTensor(TensorType_FLOAT32, {2, 2, 3}); - model_builder.AddNode(op_id, {t0}, {t1}); // n0 - model_builder.AddNode(op_id, {t0}, {t2}); // n1 - model_builder.AddNode(op_id, {t1, t2}, {t3}); // n2 - return model_builder.BuildModel({t0}, {t3}); -} - -const Model* BuildModelWithOfflinePlanning(int number_of_tensors, - const int32_t* metadata_buffer, - NodeConnection* node_conn, - int num_conns, - int num_subgraph_inputs) { - using flatbuffers::Offset; - flatbuffers::FlatBufferBuilder* fb_builder = BuilderInstance(); - - ModelBuilder model_builder(fb_builder); - - const int op_id = - model_builder.RegisterOp(BuiltinOperator_CUSTOM, "mock_custom"); - - for (int i = 0; i < number_of_tensors; ++i) { - model_builder.AddTensor(TensorType_FLOAT32, {2, 2, 3}); - } - - for (int i = 0; i < num_conns; ++i) { - model_builder.AddNode(op_id, node_conn[i].input, node_conn[i].output); - } - - model_builder.AddMetadata( - "OfflineMemoryAllocation", metadata_buffer, - number_of_tensors + tflite::testing::kOfflinePlannerHeaderSize); - - return model_builder.BuildModel( - node_conn[0].input, node_conn[num_conns - 1].output, num_subgraph_inputs); -} - -const Model* BuildModelWithUnusedInputs() { - using flatbuffers::Offset; - flatbuffers::FlatBufferBuilder* builder = BuilderInstance(); - - constexpr size_t buffers_size = 1; - const Offset buffers[buffers_size] = {CreateBuffer(*builder)}; - constexpr size_t tensor_shape_size = 2; - const int32_t tensor_shape[tensor_shape_size] = {1, 64}; - constexpr size_t tensors_size = 4; - const Offset tensors[tensors_size] = { - CreateTensor(*builder, - builder->CreateVector(tensor_shape, tensor_shape_size), - TensorType_INT8, 0, - builder->CreateString("test_input_tensor"), 0, false), - CreateTensor(*builder, - builder->CreateVector(tensor_shape, tensor_shape_size), - TensorType_INT8, 0, - builder->CreateString("test_unused_input_tensor"), 0, false), - CreateTensor(*builder, - builder->CreateVector(tensor_shape, tensor_shape_size), - TensorType_INT8, 0, - builder->CreateString("test_output_tensor"), 0, false), - CreateTensor(*builder, - builder->CreateVector(tensor_shape, tensor_shape_size), - TensorType_INT8, 0, - builder->CreateString("test_unused_tensor"), 0, false), - }; - constexpr size_t inputs_size = 2; - const int32_t inputs[inputs_size] = {0, 1}; - constexpr size_t outputs_size = 1; - const int32_t outputs[outputs_size] = {2}; - constexpr size_t operator_inputs_size = 1; - const int32_t operator_inputs[operator_inputs_size] = {0}; - constexpr size_t operator_outputs_size = 1; - const int32_t operator_outputs[operator_outputs_size] = {2}; - constexpr size_t operators_size = 1; - const Offset operators[operators_size] = { - CreateOperator( - *builder, 0, - builder->CreateVector(operator_inputs, operator_inputs_size), - builder->CreateVector(operator_outputs, operator_outputs_size), - BuiltinOptions_NONE), - }; - constexpr size_t subgraphs_size = 1; - const Offset subgraphs[subgraphs_size] = { - CreateSubGraph(*builder, builder->CreateVector(tensors, tensors_size), - builder->CreateVector(inputs, inputs_size), - builder->CreateVector(outputs, outputs_size), - builder->CreateVector(operators, operators_size), - builder->CreateString("test_subgraph"))}; - constexpr size_t operator_codes_size = 1; - const Offset operator_codes[operator_codes_size] = { - CreateOperatorCodeDirect(*builder, /*deprecated_builtin_code=*/0, - "mock_custom", - /*version=*/0, BuiltinOperator_CUSTOM)}; - const Offset model_offset = CreateModel( - *builder, 0, builder->CreateVector(operator_codes, operator_codes_size), - builder->CreateVector(subgraphs, subgraphs_size), - builder->CreateString("test_model"), - builder->CreateVector(buffers, buffers_size)); - FinishModelBuffer(*builder, model_offset); - void* model_pointer = builder->GetBufferPointer(); - const Model* model = flatbuffers::GetRoot(model_pointer); - return model; -} - -const Model* BuildModelWithUnusedOperatorOutputs() { - using flatbuffers::Offset; - flatbuffers::FlatBufferBuilder* builder = BuilderInstance(); - - constexpr size_t buffers_size = 1; - const Offset buffers[buffers_size] = {CreateBuffer(*builder)}; - constexpr size_t tensor_shape_size = 2; - const int32_t tensor_shape[tensor_shape_size] = {1, 64}; - constexpr size_t tensors_size = 2; - const Offset tensors[tensors_size] = { - CreateTensor(*builder, - builder->CreateVector(tensor_shape, tensor_shape_size), - TensorType_INT8, 0, - builder->CreateString("test_input_tensor"), 0, false), - CreateTensor( - *builder, builder->CreateVector(tensor_shape, tensor_shape_size), - TensorType_INT8, 0, - builder->CreateString("test_unused_output_tensor"), 0, false)}; - constexpr size_t inputs_size = 0; - const int32_t inputs[inputs_size] = {}; - constexpr size_t outputs_size = 1; - const int32_t outputs[outputs_size] = {0}; - constexpr size_t operator_inputs_size = 0; - const int32_t operator_inputs[operator_inputs_size] = {}; - constexpr size_t operator_outputs_size = 2; - const int32_t operator_outputs[operator_outputs_size] = {0, 1}; - constexpr size_t operators_size = 1; - const Offset operators[operators_size] = { - CreateOperator( - *builder, 0, - builder->CreateVector(operator_inputs, operator_inputs_size), - builder->CreateVector(operator_outputs, operator_outputs_size), - BuiltinOptions_NONE), - }; - constexpr size_t subgraphs_size = 1; - const Offset subgraphs[subgraphs_size] = { - CreateSubGraph(*builder, builder->CreateVector(tensors, tensors_size), - builder->CreateVector(inputs, inputs_size), - builder->CreateVector(outputs, outputs_size), - builder->CreateVector(operators, operators_size), - builder->CreateString("test_subgraph"))}; - constexpr size_t operator_codes_size = 1; - const Offset operator_codes[operator_codes_size] = { - CreateOperatorCodeDirect(*builder, /*deprecated_builtin_code=*/0, - "mock_custom", - /*version=*/0, BuiltinOperator_CUSTOM)}; - const Offset model_offset = CreateModel( - *builder, 0, builder->CreateVector(operator_codes, operator_codes_size), - builder->CreateVector(subgraphs, subgraphs_size), - builder->CreateString("test_model"), - builder->CreateVector(buffers, buffers_size)); - FinishModelBuffer(*builder, model_offset); - void* model_pointer = builder->GetBufferPointer(); - const Model* model = flatbuffers::GetRoot(model_pointer); - return model; -} - -const Model* BuildModelWith256x256Tensor() { - using flatbuffers::Offset; - flatbuffers::FlatBufferBuilder* fb_builder = BuilderInstance(); - - ModelBuilder model_builder(fb_builder); - - const int op_id = - model_builder.RegisterOp(BuiltinOperator_CUSTOM, "mock_custom"); - const int input1_tensor = - model_builder.AddTensor(TensorType_INT8, {256, 256}); - const int input2_tensor = - model_builder.AddTensor(TensorType_INT8, {256, 256}); - const int output_tensor = - model_builder.AddTensor(TensorType_INT8, {256, 256}); - - model_builder.AddNode(op_id, {input1_tensor, input2_tensor}, {output_tensor}); - return model_builder.BuildModel({input1_tensor, input2_tensor}, - {output_tensor}); -} - -const Model* BuildSimpleMockModel() { - using flatbuffers::Offset; - flatbuffers::FlatBufferBuilder* builder = BuilderInstance(); - - constexpr size_t buffer_data_size = 1; - const uint8_t buffer_data[buffer_data_size] = {21}; - constexpr size_t buffers_size = 2; - const Offset buffers[buffers_size] = { - CreateBuffer(*builder), - CreateBuffer(*builder, - builder->CreateVector(buffer_data, buffer_data_size))}; - constexpr size_t tensor_shape_size = 1; - const int32_t tensor_shape[tensor_shape_size] = {1}; - constexpr size_t tensors_size = 4; - const Offset tensors[tensors_size] = { - CreateTensor(*builder, - builder->CreateVector(tensor_shape, tensor_shape_size), - TensorType_INT32, 0, - builder->CreateString("test_input_tensor"), 0, false), - CreateTensor(*builder, - builder->CreateVector(tensor_shape, tensor_shape_size), - TensorType_INT8, 1, - builder->CreateString("test_weight_tensor"), 0, false), - CreateTensor(*builder, - builder->CreateVector(tensor_shape, tensor_shape_size), - TensorType_INT32, 0, - builder->CreateString("test_output_tensor"), 0, false), - CreateTensor(*builder, - builder->CreateVector(tensor_shape, tensor_shape_size), - TensorType_INT32, 0, - builder->CreateString("test_output2_tensor"), 0, false), - }; - constexpr size_t inputs_size = 1; - const int32_t inputs[inputs_size] = {0}; - constexpr size_t outputs_size = 2; - const int32_t outputs[outputs_size] = {2, 3}; - constexpr size_t operator_inputs_size = 2; - const int32_t operator_inputs[operator_inputs_size] = {0, 1}; - constexpr size_t operator_outputs_size = 1; - const int32_t operator_outputs[operator_outputs_size] = {2}; - const int32_t operator2_outputs[operator_outputs_size] = {3}; - constexpr size_t operators_size = 2; - const Offset operators[operators_size] = { - CreateOperator( - *builder, 0, - builder->CreateVector(operator_inputs, operator_inputs_size), - builder->CreateVector(operator_outputs, operator_outputs_size), - BuiltinOptions_NONE), - CreateOperator( - *builder, 0, - builder->CreateVector(operator_inputs, operator_inputs_size), - builder->CreateVector(operator2_outputs, operator_outputs_size), - BuiltinOptions_NONE), - }; - constexpr size_t subgraphs_size = 1; - const Offset subgraphs[subgraphs_size] = { - CreateSubGraph(*builder, builder->CreateVector(tensors, tensors_size), - builder->CreateVector(inputs, inputs_size), - builder->CreateVector(outputs, outputs_size), - builder->CreateVector(operators, operators_size), - builder->CreateString("test_subgraph"))}; - constexpr size_t operator_codes_size = 1; - const Offset operator_codes[operator_codes_size] = { - CreateOperatorCodeDirect(*builder, /*deprecated_builtin_code=*/0, - "mock_custom", - /*version=*/0, BuiltinOperator_CUSTOM)}; - const Offset model_offset = CreateModel( - *builder, 0, builder->CreateVector(operator_codes, operator_codes_size), - builder->CreateVector(subgraphs, subgraphs_size), - builder->CreateString("test_model"), - builder->CreateVector(buffers, buffers_size)); - FinishModelBuffer(*builder, model_offset); - void* model_pointer = builder->GetBufferPointer(); - const Model* model = flatbuffers::GetRoot(model_pointer); - return model; -} - -const Model* BuildComplexMockModel() { - using flatbuffers::Offset; - flatbuffers::FlatBufferBuilder* builder = BuilderInstance(); - - constexpr size_t buffer_data_size = 1; - const uint8_t buffer_data_1[buffer_data_size] = {21}; - const uint8_t buffer_data_2[buffer_data_size] = {21}; - const uint8_t buffer_data_3[buffer_data_size] = {21}; - constexpr size_t buffers_size = 7; - const Offset buffers[buffers_size] = { - // Op 1 buffers: - CreateBuffer(*builder), - CreateBuffer(*builder), - CreateBuffer(*builder, - builder->CreateVector(buffer_data_1, buffer_data_size)), - // Op 2 buffers: - CreateBuffer(*builder), - CreateBuffer(*builder, - builder->CreateVector(buffer_data_2, buffer_data_size)), - // Op 3 buffers: - CreateBuffer(*builder), - CreateBuffer(*builder, - builder->CreateVector(buffer_data_3, buffer_data_size)), - }; - constexpr size_t tensor_shape_size = 1; - const int32_t tensor_shape[tensor_shape_size] = {1}; - - constexpr size_t tensors_size = 10; - const Offset tensors[tensors_size] = { - // Op 1 inputs: - CreateTensor( - *builder, builder->CreateVector(tensor_shape, tensor_shape_size), - TensorType_INT32, 0, builder->CreateString("test_input_tensor_1"), 0, - false /* is_variable */), - CreateTensor( - *builder, builder->CreateVector(tensor_shape, tensor_shape_size), - TensorType_INT32, 1, builder->CreateString("test_variable_tensor_1"), - 0, true /* is_variable */), - CreateTensor( - *builder, builder->CreateVector(tensor_shape, tensor_shape_size), - TensorType_INT8, 2, builder->CreateString("test_weight_tensor_1"), 0, - false /* is_variable */), - // Op 1 output / Op 2 input: - CreateTensor( - *builder, builder->CreateVector(tensor_shape, tensor_shape_size), - TensorType_INT32, 0, builder->CreateString("test_output_tensor_1"), 0, - false /* is_variable */), - // Op 2 inputs: - CreateTensor( - *builder, builder->CreateVector(tensor_shape, tensor_shape_size), - TensorType_INT32, 1, builder->CreateString("test_variable_tensor_2"), - 0, true /* is_variable */), - CreateTensor( - *builder, builder->CreateVector(tensor_shape, tensor_shape_size), - TensorType_INT8, 2, builder->CreateString("test_weight_tensor_2"), 0, - false /* is_variable */), - // Op 2 output / Op 3 input: - CreateTensor( - *builder, builder->CreateVector(tensor_shape, tensor_shape_size), - TensorType_INT32, 0, builder->CreateString("test_output_tensor_2"), 0, - false /* is_variable */), - // Op 3 inputs: - CreateTensor( - *builder, builder->CreateVector(tensor_shape, tensor_shape_size), - TensorType_INT32, 1, builder->CreateString("test_variable_tensor_3"), - 0, true /* is_variable */), - CreateTensor( - *builder, builder->CreateVector(tensor_shape, tensor_shape_size), - TensorType_INT8, 2, builder->CreateString("test_weight_tensor_3"), 0, - false /* is_variable */), - // Op 3 output: - CreateTensor( - *builder, builder->CreateVector(tensor_shape, tensor_shape_size), - TensorType_INT32, 0, builder->CreateString("test_output_tensor_3"), 0, - false /* is_variable */), - }; - - constexpr size_t operators_size = 3; - Offset operators[operators_size]; - { - // Set Op 1 attributes: - constexpr size_t operator_inputs_size = 3; - const int32_t operator_inputs[operator_inputs_size] = {0, 1, 2}; - constexpr size_t operator_outputs_size = 1; - const int32_t operator_outputs[operator_outputs_size] = {3}; - - operators[0] = {CreateOperator( - *builder, 0, - builder->CreateVector(operator_inputs, operator_inputs_size), - builder->CreateVector(operator_outputs, operator_outputs_size), - BuiltinOptions_NONE)}; - } - - { - // Set Op 2 attributes - constexpr size_t operator_inputs_size = 3; - const int32_t operator_inputs[operator_inputs_size] = {3, 4, 5}; - constexpr size_t operator_outputs_size = 1; - const int32_t operator_outputs[operator_outputs_size] = {6}; - - operators[1] = {CreateOperator( - *builder, 0, - builder->CreateVector(operator_inputs, operator_inputs_size), - builder->CreateVector(operator_outputs, operator_outputs_size), - BuiltinOptions_NONE)}; - } - - { - // Set Op 3 attributes - constexpr size_t operator_inputs_size = 3; - const int32_t operator_inputs[operator_inputs_size] = {6, 7, 8}; - constexpr size_t operator_outputs_size = 1; - const int32_t operator_outputs[operator_outputs_size] = {9}; - - operators[2] = {CreateOperator( - *builder, 0, - builder->CreateVector(operator_inputs, operator_inputs_size), - builder->CreateVector(operator_outputs, operator_outputs_size), - BuiltinOptions_NONE)}; - } - - constexpr size_t inputs_size = 1; - const int32_t inputs[inputs_size] = {0}; - constexpr size_t outputs_size = 1; - const int32_t outputs[outputs_size] = {9}; - - constexpr size_t subgraphs_size = 1; - const Offset subgraphs[subgraphs_size] = { - CreateSubGraph(*builder, builder->CreateVector(tensors, tensors_size), - builder->CreateVector(inputs, inputs_size), - builder->CreateVector(outputs, outputs_size), - builder->CreateVector(operators, operators_size), - builder->CreateString("test_subgraph"))}; - - constexpr size_t operator_codes_size = 1; - const Offset operator_codes[operator_codes_size] = { - CreateOperatorCodeDirect(*builder, /*deprecated_builtin_code=*/0, - "mock_custom", - /*version=*/0, BuiltinOperator_CUSTOM)}; - - const Offset model_offset = CreateModel( - *builder, 0, builder->CreateVector(operator_codes, operator_codes_size), - builder->CreateVector(subgraphs, subgraphs_size), - builder->CreateString("test_model"), - builder->CreateVector(buffers, buffers_size)); - - FinishModelBuffer(*builder, model_offset); - void* model_pointer = builder->GetBufferPointer(); - const Model* model = flatbuffers::GetRoot(model_pointer); - return model; -} - -const Model* BuildSimpleMultipleInputsModel() { - using flatbuffers::Offset; - flatbuffers::FlatBufferBuilder* builder = BuilderInstance(); - - constexpr size_t buffers_size = 1; - const Offset buffers[buffers_size] = { - CreateBuffer(*builder), - }; - constexpr size_t tensor_shape_size = 1; - const int32_t tensor_shape[tensor_shape_size] = {1}; - constexpr size_t tensors_size = 4; - const Offset tensors[tensors_size] = { - CreateTensor(*builder, - builder->CreateVector(tensor_shape, tensor_shape_size), - TensorType_INT32, 0, - builder->CreateString("test_input_tensor1"), 0, false), - CreateTensor(*builder, - builder->CreateVector(tensor_shape, tensor_shape_size), - TensorType_INT8, 0, - builder->CreateString("test_input_tensor2"), 0, false), - CreateTensor(*builder, - builder->CreateVector(tensor_shape, tensor_shape_size), - TensorType_INT32, 0, - builder->CreateString("test_input_tensor3"), 0, false), - CreateTensor(*builder, - builder->CreateVector(tensor_shape, tensor_shape_size), - TensorType_INT32, 0, - builder->CreateString("test_output_tensor"), 0, false), - }; - constexpr size_t inputs_size = 3; - const int32_t inputs[inputs_size] = {0, 1, 2}; - constexpr size_t outputs_size = 1; - const int32_t outputs[outputs_size] = {3}; - constexpr size_t operator_inputs_size = 3; - const int32_t operator_inputs[operator_inputs_size] = {0, 1, 2}; - constexpr size_t operator_outputs_size = 1; - const int32_t operator_outputs[operator_outputs_size] = {3}; - constexpr size_t operators_size = 1; - const Offset operators[operators_size] = { - CreateOperator( - *builder, 0, - builder->CreateVector(operator_inputs, operator_inputs_size), - builder->CreateVector(operator_outputs, operator_outputs_size), - BuiltinOptions_NONE), - }; - constexpr size_t subgraphs_size = 1; - const Offset subgraphs[subgraphs_size] = { - CreateSubGraph(*builder, builder->CreateVector(tensors, tensors_size), - builder->CreateVector(inputs, inputs_size), - builder->CreateVector(outputs, outputs_size), - builder->CreateVector(operators, operators_size), - builder->CreateString("test_subgraph"))}; - constexpr size_t operator_codes_size = 1; - const Offset operator_codes[operator_codes_size] = { - CreateOperatorCodeDirect(*builder, /*deprecated_builtin_code=*/0, - "multiple_inputs_op", - /*version=*/0, BuiltinOperator_CUSTOM)}; - const Offset model_offset = CreateModel( - *builder, 0, builder->CreateVector(operator_codes, operator_codes_size), - builder->CreateVector(subgraphs, subgraphs_size), - builder->CreateString("test_model"), - builder->CreateVector(buffers, buffers_size)); - FinishModelBuffer(*builder, model_offset); - void* model_pointer = builder->GetBufferPointer(); - const Model* model = flatbuffers::GetRoot(model_pointer); - return model; -} - -const Model* BuildSimpleModelWithSubgraphsAndIf() { - using flatbuffers::Offset; - flatbuffers::FlatBufferBuilder* builder = BuilderInstance(); - - constexpr size_t buffers_size = 1; - const Offset buffers[buffers_size] = { - CreateBuffer(*builder), - }; - const int32_t condition_tensor_shape[] = {1}; - const int32_t data_tensor_shape[] = {1, 2}; - constexpr size_t tensors_size = 4; - const Offset subgraph1_tensors[tensors_size] = { - CreateTensor(*builder, builder->CreateVector(condition_tensor_shape, 1), - TensorType_BOOL, 0, - builder->CreateString("condition tensor"), 0, false), - CreateTensor(*builder, builder->CreateVector(data_tensor_shape, 2), - TensorType_FLOAT32, 0, - builder->CreateString("input_tensor1"), 0, false), - CreateTensor(*builder, builder->CreateVector(data_tensor_shape, 2), - TensorType_FLOAT32, 0, - builder->CreateString("input_tensor2"), 0, false), - CreateTensor(*builder, builder->CreateVector(data_tensor_shape, 2), - TensorType_FLOAT32, 0, - builder->CreateString("output_tensor"), 0, false), - }; - const Offset subgraph2_tensors[tensors_size] = { - CreateTensor(*builder, builder->CreateVector(data_tensor_shape, 2), - TensorType_FLOAT32, 0, - builder->CreateString("input_tensor1"), 0, false), - CreateTensor(*builder, builder->CreateVector(data_tensor_shape, 2), - TensorType_FLOAT32, 0, - builder->CreateString("input_tensor2"), 0, false), - CreateTensor(*builder, builder->CreateVector(data_tensor_shape, 2), - TensorType_FLOAT32, 0, - builder->CreateString("output_tensor"), 0, false), - }; - const Offset subgraph3_tensors[tensors_size] = { - CreateTensor(*builder, builder->CreateVector(data_tensor_shape, 2), - TensorType_FLOAT32, 0, - builder->CreateString("input_tensor1"), 0, false), - CreateTensor(*builder, builder->CreateVector(data_tensor_shape, 2), - TensorType_FLOAT32, 0, - builder->CreateString("input_tensor2"), 0, false), - CreateTensor(*builder, builder->CreateVector(data_tensor_shape, 2), - TensorType_FLOAT32, 0, - builder->CreateString("output_tensor"), 0, false), - }; - - constexpr size_t if_inputs_size = 3; - const int32_t if_inputs[if_inputs_size] = {0, 1, 2}; - constexpr size_t outputs_size = 1; - const int32_t if_outputs[outputs_size] = {3}; - constexpr size_t operator_inputs_size = 2; - const int32_t operator_inputs[operator_inputs_size] = {0, 1}; - const int32_t operator_outputs[outputs_size] = {2}; - constexpr size_t operators_size = 1; - const Offset subgraph1_operators[operators_size] = { - CreateOperator( - *builder, 0, builder->CreateVector(if_inputs, if_inputs_size), - builder->CreateVector(if_outputs, outputs_size), - BuiltinOptions_IfOptions, CreateIfOptions(*builder, 1, 2).Union()), - }; - const Offset subgraph2_operators[operators_size] = { - CreateOperator( - *builder, 1, - builder->CreateVector(operator_inputs, operator_inputs_size), - builder->CreateVector(operator_outputs, outputs_size), - BuiltinOptions_NONE), - }; - const Offset subgraph3_operators[operators_size] = { - CreateOperator( - *builder, 2, - builder->CreateVector(operator_inputs, operator_inputs_size), - builder->CreateVector(operator_outputs, outputs_size), - BuiltinOptions_NONE), - }; - constexpr size_t subgraphs_size = 3; - const Offset subgraphs[subgraphs_size] = { - CreateSubGraph(*builder, builder->CreateVector(subgraph1_tensors, 4), - builder->CreateVector(if_inputs, if_inputs_size), - builder->CreateVector(if_outputs, outputs_size), - builder->CreateVector(subgraph1_operators, operators_size), - builder->CreateString("if_subgraph")), - CreateSubGraph( - *builder, builder->CreateVector(subgraph2_tensors, 3), - builder->CreateVector(operator_inputs, operator_inputs_size), - builder->CreateVector(operator_outputs, outputs_size), - builder->CreateVector(subgraph2_operators, operators_size), - builder->CreateString("then_subgraph")), - CreateSubGraph( - *builder, builder->CreateVector(subgraph3_tensors, 3), - builder->CreateVector(operator_inputs, operator_inputs_size), - builder->CreateVector(operator_outputs, outputs_size), - builder->CreateVector(subgraph3_operators, operators_size), - builder->CreateString("else_subgraph")), - }; - constexpr size_t operator_codes_size = 3; - const Offset operator_codes[operator_codes_size] = { - CreateOperatorCodeDirect(*builder, /*deprecated_builtin_code=*/0, - "multiple_inputs_op", - /*version=*/0, BuiltinOperator_IF), - CreateOperatorCodeDirect(*builder, /*deprecated_builtin_code=*/0, - "multiple_inputs_op", - /*version=*/0, BuiltinOperator_ADD), - CreateOperatorCodeDirect(*builder, /*deprecated_builtin_code=*/0, - "multiple_inputs_op", - /*version=*/0, BuiltinOperator_MUL), - }; - const Offset model_offset = CreateModel( - *builder, 0, builder->CreateVector(operator_codes, operator_codes_size), - builder->CreateVector(subgraphs, subgraphs_size), - builder->CreateString("test_model"), - builder->CreateVector(buffers, buffers_size)); - FinishModelBuffer(*builder, model_offset); - void* model_pointer = builder->GetBufferPointer(); - const Model* model = flatbuffers::GetRoot(model_pointer); - return model; -} - -const Model* BuildSimpleModelWithSubgraphsAndWhile() { - using flatbuffers::Offset; - flatbuffers::FlatBufferBuilder* builder = BuilderInstance(); - - constexpr size_t buffers_size = 1; - const Offset buffers[buffers_size] = { - CreateBuffer(*builder), - }; - const int32_t data_tensor_shape[] = {1, 1}; - constexpr size_t while_tensors_size = 4; - constexpr size_t op_tensors_size = 3; - const Offset subgraph0_tensors[while_tensors_size] = { - CreateTensor(*builder, builder->CreateVector(data_tensor_shape, 1), - TensorType_FLOAT32, 0, - builder->CreateString("input_tensor0"), 0, false), - CreateTensor(*builder, builder->CreateVector(data_tensor_shape, 1), - TensorType_FLOAT32, 0, - builder->CreateString("input_tensor1"), 0, false), - CreateTensor(*builder, builder->CreateVector(data_tensor_shape, 1), - TensorType_FLOAT32, 0, - builder->CreateString("output_tensor0"), 0, false), - CreateTensor(*builder, builder->CreateVector(data_tensor_shape, 1), - TensorType_FLOAT32, 0, - builder->CreateString("output_tensor1"), 0, false), - }; - const Offset subgraph1_tensors[op_tensors_size] = { - CreateTensor(*builder, builder->CreateVector(data_tensor_shape, 1), - TensorType_FLOAT32, 0, - builder->CreateString("input_tensor1"), 0, false), - CreateTensor(*builder, builder->CreateVector(data_tensor_shape, 1), - TensorType_FLOAT32, 0, - builder->CreateString("input_tensor2"), 0, false), - CreateTensor(*builder, builder->CreateVector(data_tensor_shape, 1), - TensorType_BOOL, 0, - builder->CreateString("condition_tensor"), 0, false), - }; - const Offset subgraph2_tensors[op_tensors_size] = { - CreateTensor(*builder, builder->CreateVector(data_tensor_shape, 1), - TensorType_FLOAT32, 0, - builder->CreateString("input_tensor0"), 0, false), - CreateTensor(*builder, builder->CreateVector(data_tensor_shape, 1), - TensorType_FLOAT32, 0, - builder->CreateString("input_tensor1"), 0, false), - CreateTensor(*builder, builder->CreateVector(data_tensor_shape, 1), - TensorType_FLOAT32, 0, - builder->CreateString("output_tensor0"), 0, false), - }; - - constexpr size_t inputs_size = 2; - const int32_t inputs[inputs_size] = {0, 1}; - constexpr size_t while_outputs_size = 2; - const int32_t while_outputs[while_outputs_size] = {2, 3}; - constexpr size_t cond_outputs_size = 1; - const int32_t cond_outputs[cond_outputs_size] = {2}; - constexpr size_t add_outputs_size = 1; - const int32_t add_outputs[add_outputs_size] = {2}; - constexpr size_t add_subgraph_outputs_size = 2; - const int32_t add_subgraph_outputs[add_subgraph_outputs_size] = {2, 1}; - constexpr size_t operators_size = 1; - const Offset subgraph0_operators[operators_size] = { - CreateOperator(*builder, 0, builder->CreateVector(inputs, inputs_size), - builder->CreateVector(while_outputs, while_outputs_size), - BuiltinOptions_WhileOptions, - CreateWhileOptions(*builder, 1, 2).Union()), - }; - const Offset subgraph1_operators[operators_size] = { - CreateOperator(*builder, 1, builder->CreateVector(inputs, inputs_size), - builder->CreateVector(cond_outputs, cond_outputs_size), - BuiltinOptions_NONE), - }; - const Offset subgraph2_operators[operators_size] = { - CreateOperator(*builder, 2, builder->CreateVector(inputs, inputs_size), - builder->CreateVector(add_outputs, add_outputs_size), - BuiltinOptions_NONE), - }; - constexpr size_t subgraphs_size = 3; - const Offset subgraphs[subgraphs_size] = { - CreateSubGraph(*builder, builder->CreateVector(subgraph0_tensors, 4), - builder->CreateVector(inputs, inputs_size), - builder->CreateVector(while_outputs, while_outputs_size), - builder->CreateVector(subgraph0_operators, operators_size), - builder->CreateString("while_subgraph")), - CreateSubGraph(*builder, builder->CreateVector(subgraph1_tensors, 3), - builder->CreateVector(inputs, inputs_size), - builder->CreateVector(cond_outputs, cond_outputs_size), - builder->CreateVector(subgraph1_operators, operators_size), - builder->CreateString("cond_subgraph")), - CreateSubGraph(*builder, builder->CreateVector(subgraph2_tensors, 3), - builder->CreateVector(inputs, inputs_size), - builder->CreateVector(add_subgraph_outputs, - add_subgraph_outputs_size), - builder->CreateVector(subgraph2_operators, operators_size), - builder->CreateString("body_subgraph")), - }; - constexpr size_t operator_codes_size = 3; - const Offset operator_codes[operator_codes_size] = { - CreateOperatorCodeDirect(*builder, /*deprecated_builtin_code=*/0, - "multiple_inputs_op", - /*version=*/0, BuiltinOperator_WHILE), - CreateOperatorCodeDirect(*builder, /*deprecated_builtin_code=*/0, - "multiple_inputs_op", - /*version=*/0, BuiltinOperator_LESS), - CreateOperatorCodeDirect(*builder, /*deprecated_builtin_code=*/0, - "multiple_inputs_op", - /*version=*/0, BuiltinOperator_ADD), - }; - const Offset model_offset = CreateModel( - *builder, 0, builder->CreateVector(operator_codes, operator_codes_size), - builder->CreateVector(subgraphs, subgraphs_size), - builder->CreateString("test_model"), - builder->CreateVector(buffers, buffers_size)); - FinishModelBuffer(*builder, model_offset); - void* model_pointer = builder->GetBufferPointer(); - const Model* model = flatbuffers::GetRoot(model_pointer); - return model; -} - -// Build a model with If and two subgraphs: two data tensors A1 of size 2, A2 of -// size 4 are first concatenated, then cut to a new tensor A3 of size 3; the new -// tensor A3 of size 3 is then concatenated with A2 tensor of size 4 to produce -// a final output tensor A4. This model is specially crafted to capture the -// corner case outlined in go/avoid-memory-corruption-in-if-operator. -// -// Subgraph0 -// A0(1) A2_0(4) A1_0(2) -// | | | ---+ -// v v v | -// +--------------+ | -// | IF | | -// +------+-------+ | -// | A3_0(3) | -// v | -// +--------------+ | -// | CUSTOM |<---+ -// +------+-------+ -// | -// v -// A4_0(8) -// -// Subgraph1/2 -// A1_1(2) A2_1(4) -// | | -// v v -// +---------------+ -// | CUSTOM | -// +-------+-------+ -// | -// v A3_1(3) -// -// And it leads to memory plan as below -// -// Subgraph0 Layout -// -// -// <------------A4_0 -------------> <----- A2_0-------> <----A3_0 ---> -// +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+ -// | | | | | | | | | 3 | 4 | 5 | 6 | | | | -// +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+ -// -// +----+----+----+ -// | 1 | 2 | A0 | -// +----+----+----+ -// <---A1_0--> -// -// Subgraph 1 Layout -// -// +----+----+----+----+----+----+----+----+----+ -// | | | | | | | | | | -// +----+----+----+----+----+----+----+----+----+ -// -// -// <------A2_1 -------><----A3_1 ---><--A1_1---> -// -// -// A1_1 of subgraph 1 will overlap with A2_0 of subgraph 0. -// In a buggy implementation of IF, two overwrite may happen: -// 1. copying input from A1_0 to A1_1 overwrites A2_0 before A2_0 is copied to -// A2_1; thus subgraph 1 produce incorrect output. -// 2. copying output from A3_1 to A4_0 overwrites A1_0, which should remain -// intact so that it can be used by the OP after the IF operator in subgraph 0 -// - -const Model* BuildModelWithIfAndSubgraphInputTensorOverlap() { - using flatbuffers::Offset; - flatbuffers::FlatBufferBuilder* builder = BuilderInstance(); - - constexpr TensorType kTensorType = TensorType_INT32; - constexpr int kBlockSize = - tflite::MicroArenaBufferAlignment() / sizeof(int32_t); - constexpr size_t kBuffersCount = 1; - const Offset buffers[kBuffersCount] = { - CreateBuffer(*builder), - }; - const int32_t kConditionTensorShape[] = {1}; - const int32_t kIfInput1TensorShape[] = {2 * kBlockSize}; - const int32_t kIfInput2TensorShape[] = {4 * kBlockSize}; - const int32_t kIfOutputTensorShape[] = {3 * kBlockSize}; - const int32_t kFinalOutputTensorShape[] = {8 * kBlockSize}; - constexpr size_t kSubgraph0TensorsCount = 5; - const Offset kSubgraph0Tensors[kSubgraph0TensorsCount] = { - CreateTensor(*builder, builder->CreateVector(kConditionTensorShape, 1), - TensorType_BOOL, 0, - builder->CreateString("condition tensor"), 0, false), - CreateTensor(*builder, builder->CreateVector(kIfInput1TensorShape, 1), - kTensorType, 0, builder->CreateString("if_input_tensor1"), 0, - false), - CreateTensor(*builder, builder->CreateVector(kIfInput2TensorShape, 1), - kTensorType, 0, builder->CreateString("if_input_tensor2"), 0, - false), - CreateTensor(*builder, builder->CreateVector(kIfOutputTensorShape, 1), - kTensorType, 0, builder->CreateString("if_output_tensor"), 0, - false), - CreateTensor(*builder, builder->CreateVector(kFinalOutputTensorShape, 1), - kTensorType, 0, builder->CreateString("final_output_tensor"), - 0, false), - }; - - // Subgraph 1 is the chosen path if condition tensor in IF is true. - constexpr size_t kSubgraph1TensorsCount = 3; - const Offset kSubgraph1Tensors[kSubgraph1TensorsCount] = { - CreateTensor(*builder, builder->CreateVector(kIfInput1TensorShape, 1), - kTensorType, 0, - builder->CreateString("subgraph1_input_tensor1"), 0, false), - CreateTensor(*builder, builder->CreateVector(kIfInput2TensorShape, 1), - kTensorType, 0, - builder->CreateString("subgraph1_input_tensor2"), 0, false), - CreateTensor(*builder, builder->CreateVector(kIfOutputTensorShape, 1), - kTensorType, 0, - builder->CreateString("subgraph1_output_tensor"), 0, false), - }; - - // Subgraph 2 is the chosen path if condition tensor in IF is false - constexpr size_t kSubgraph2TensorsCount = 3; - const Offset kSubgraph2Tensors[kSubgraph2TensorsCount] = { - CreateTensor(*builder, builder->CreateVector(kIfInput1TensorShape, 1), - kTensorType, 0, builder->CreateString("if_input_tensor1"), 0, - false), - CreateTensor(*builder, builder->CreateVector(kIfInput2TensorShape, 1), - kTensorType, 0, builder->CreateString("if_input_tensor2"), 0, - false), - CreateTensor(*builder, builder->CreateVector(kIfOutputTensorShape, 1), - kTensorType, 0, builder->CreateString("if_output_tensor"), 0, - false), - }; - - constexpr int kIfOpCodeIndex = 0; - constexpr int kCustomOpCodeIndex = 1; - - constexpr size_t kIfInputsCount = 3; - const int32_t kIfInputs[kIfInputsCount] = {0, 1, 2}; - constexpr size_t kOutputsCount = 1; - const int32_t kIfOutputs[kOutputsCount] = {3}; - constexpr size_t kOpAfterIfInputsCount = 2; - const int32_t kOpAfterIfInputs[kOpAfterIfInputsCount] = {3, 2}; - const int32_t kOpAfterIfOutputs[kOutputsCount] = {4}; - constexpr size_t kOperatorsCount = 2; - const Offset kSubgraph0Operators[kOperatorsCount] = { - CreateOperator(*builder, kIfOpCodeIndex, - builder->CreateVector(kIfInputs, kIfInputsCount), - builder->CreateVector(kIfOutputs, kOutputsCount), - BuiltinOptions_IfOptions, - CreateIfOptions(*builder, 1, 2).Union()), - CreateOperator( - *builder, kCustomOpCodeIndex, - builder->CreateVector(kOpAfterIfInputs, kOpAfterIfInputsCount), - builder->CreateVector(kOpAfterIfOutputs, kOutputsCount)), - }; - - constexpr size_t kSubgraph1InputsCount = 2; - const int32_t kSubgraph1Inputs[kSubgraph1InputsCount] = {0, 1}; - constexpr size_t kSubgraph1OutputsCount = 1; - const int32_t kSubgraph1Outputs[kSubgraph1OutputsCount] = {2}; - constexpr size_t kSubgraph1OperatorsCount = 1; - const Offset kSubgraph1Operators[kSubgraph1OperatorsCount] = { - CreateOperator( - *builder, kCustomOpCodeIndex, - builder->CreateVector(kSubgraph1Inputs, kSubgraph1InputsCount), - builder->CreateVector(kSubgraph1Outputs, kSubgraph1OutputsCount), - BuiltinOptions_NONE), - }; - - constexpr size_t kSubgraph2InputsCount = 2; - const int32_t kSubgraph2Inputs[kSubgraph2InputsCount] = {0, 1}; - constexpr size_t kSubgraph2OutputsCount = 1; - const int32_t kSubgraph2Outputs[kSubgraph2OutputsCount] = {2}; - constexpr size_t kSubgraph2OperatorsCount = 1; - const Offset kSubgraph2Operators[kSubgraph2OperatorsCount] = { - CreateOperator( - *builder, kCustomOpCodeIndex, - builder->CreateVector(kSubgraph2Inputs, kSubgraph2InputsCount), - builder->CreateVector(kSubgraph2Outputs, kSubgraph2OutputsCount), - BuiltinOptions_NONE), - }; - - constexpr size_t kSubgraphsCount = 3; - const Offset kSubgraphs[kSubgraphsCount] = { - CreateSubGraph( - *builder, - builder->CreateVector(kSubgraph0Tensors, kSubgraph0TensorsCount), - builder->CreateVector(kIfInputs, kIfInputsCount), - builder->CreateVector(kOpAfterIfOutputs, kOutputsCount), - builder->CreateVector(kSubgraph0Operators, kOperatorsCount), - builder->CreateString("if_subgraph")), - CreateSubGraph( - *builder, - builder->CreateVector(kSubgraph1Tensors, kSubgraph1TensorsCount), - builder->CreateVector(kSubgraph1Inputs, kSubgraph1InputsCount), - builder->CreateVector(kSubgraph1Outputs, kSubgraph1OutputsCount), - builder->CreateVector(kSubgraph1Operators, kSubgraph1OperatorsCount), - builder->CreateString("then_subgraph")), - CreateSubGraph( - *builder, - builder->CreateVector(kSubgraph2Tensors, kSubgraph2TensorsCount), - builder->CreateVector(kSubgraph2Inputs, kSubgraph2InputsCount), - builder->CreateVector(kSubgraph2Outputs, kSubgraph2OutputsCount), - builder->CreateVector(kSubgraph2Operators, kSubgraph2OperatorsCount), - builder->CreateString("else_subgraph")), - }; - - constexpr size_t kOperatorCodesCount = 2; - const Offset kOperatorCodes[kOperatorCodesCount] = { - CreateOperatorCodeDirect(*builder, /*deprecated_builtin_code=*/0, "if", - /*version=*/0, BuiltinOperator_IF), - CreateOperatorCodeDirect(*builder, /*deprecated_builtin_code=*/0, - "custom_packer_op", - /*version=*/0, BuiltinOperator_CUSTOM), - }; - const Offset kModelOffset = CreateModel( - *builder, 0, builder->CreateVector(kOperatorCodes, kOperatorCodesCount), - builder->CreateVector(kSubgraphs, kSubgraphsCount), - builder->CreateString("test_model"), - builder->CreateVector(buffers, kBuffersCount)); - FinishModelBuffer(*builder, kModelOffset); - void* model_pointer = builder->GetBufferPointer(); - const Model* model = flatbuffers::GetRoot(model_pointer); - return model; -} - -// Mock model with one main subgraph containing a single CALL_ONCE op (with null -// inputs and outputs) which invokes a second subgraph which has null inputs and -// outputs. -const Model* BuildSimpleMockModelWithNullInputsOutputs() { - using flatbuffers::Offset; - flatbuffers::FlatBufferBuilder* builder = BuilderInstance(); - - constexpr size_t buffers_size = 1; - const Offset buffers[buffers_size] = { - CreateBuffer(*builder), - }; - constexpr size_t tensor_shape_size = 1; - const int32_t tensor_shape[tensor_shape_size] = {0}; - constexpr size_t tensors_size = 1; - const Offset tensors[tensors_size] = { - CreateTensor(*builder, - builder->CreateVector(tensor_shape, tensor_shape_size), - TensorType_INT32, 0, - builder->CreateString("test_input_tensor1"), 0, false), - }; - constexpr size_t subgraph0_inputs_size = 1; - const int32_t subgraph0_inputs[subgraph0_inputs_size] = {0}; - constexpr size_t subgraph0_outputs_size = 1; - const int32_t subgraph0_outputs[subgraph0_outputs_size] = {0}; - constexpr size_t operators_size = 1; - const Offset subgraph0_operators[operators_size] = { - CreateOperator(*builder, 0, {}, {}, BuiltinOptions_CallOnceOptions, - CreateCallOnceOptions(*builder, 1).Union()), - }; - const Offset subgraph1_operators[operators_size] = { - CreateOperator(*builder, 1, {}, {}, BuiltinOptions_NONE)}; - constexpr size_t subgraphs_size = 2; - const Offset subgraphs[subgraphs_size] = { - CreateSubGraph( - *builder, builder->CreateVector(tensors, tensors_size), - builder->CreateVector(subgraph0_inputs, subgraph0_inputs_size), - builder->CreateVector(subgraph0_outputs, subgraph0_outputs_size), - builder->CreateVector(subgraph0_operators, operators_size), - builder->CreateString("main_subgraph")), - CreateSubGraph(*builder, builder->CreateVector(tensors, tensors_size), {}, - {}, - builder->CreateVector(subgraph1_operators, operators_size), - builder->CreateString("secondary subgraph")), - }; - constexpr size_t operator_codes_size = 2; - const Offset operator_codes[operator_codes_size] = { - CreateOperatorCodeDirect(*builder, /*deprecated_builtin_code=*/0, - "call_once_op", - /*version=*/0, BuiltinOperator_CALL_ONCE), - CreateOperatorCodeDirect(*builder, /*deprecated_builtin_code=*/0, "no_op", - /*version=*/0, BuiltinOperator_CUSTOM)}; - const Offset model_offset = CreateModel( - *builder, 0, builder->CreateVector(operator_codes, operator_codes_size), - builder->CreateVector(subgraphs, subgraphs_size), - builder->CreateString("test_model"), - builder->CreateVector(buffers, buffers_size)); - FinishModelBuffer(*builder, model_offset); - void* model_pointer = builder->GetBufferPointer(); - const Model* model = flatbuffers::GetRoot(model_pointer); - return model; -} - -} // namespace - -const TfLiteRegistration* SimpleStatefulOp::getRegistration() { - return GetMutableRegistration(); -} - -TfLiteRegistration* SimpleStatefulOp::GetMutableRegistration() { - static TfLiteRegistration r; - r.init = Init; - r.prepare = Prepare; - r.invoke = Invoke; - return &r; -} - -void* SimpleStatefulOp::Init(TfLiteContext* context, const char* buffer, - size_t length) { - TFLITE_DCHECK(context->AllocateBufferForEval == nullptr); - TFLITE_DCHECK(context->GetScratchBuffer == nullptr); - TFLITE_DCHECK(context->RequestScratchBufferInArena == nullptr); - - void* raw = context->AllocatePersistentBuffer(context, sizeof(OpData)); - OpData* data = reinterpret_cast(raw); - *data = {}; - return raw; -} - -TfLiteStatus SimpleStatefulOp::Prepare(TfLiteContext* context, - TfLiteNode* node) { - OpData* data = reinterpret_cast(node->user_data); - - // Make sure that the input is in uint8_t with at least 1 data entry. - MicroContext* micro_context = GetMicroContext(context); - TfLiteTensor* input = - micro_context->AllocateTempInputTensor(node, kInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - - if (input->type != kTfLiteInt8) return kTfLiteError; - if (NumElements(input->dims) == 0) return kTfLiteError; - - // Allocate a temporary buffer with the same size of input for sorting. - TF_LITE_ENSURE_STATUS(context->RequestScratchBufferInArena( - context, sizeof(uint8_t) * NumElements(input->dims), - &data->sorting_buffer)); - // We can interleave scratch / persistent buffer allocation. - data->invoke_count = reinterpret_cast( - context->AllocatePersistentBuffer(context, sizeof(int))); - *data->invoke_count = 0; - - micro_context->DeallocateTempTfLiteTensor(input); - return kTfLiteOk; -} - -TfLiteStatus SimpleStatefulOp::Invoke(TfLiteContext* context, - TfLiteNode* node) { - OpData* data = reinterpret_cast(node->user_data); - *data->invoke_count += 1; - - const TfLiteEvalTensor* input = - tflite::micro::GetEvalInput(context, node, kInputTensor); - TF_LITE_ENSURE(context, input != nullptr); - const uint8_t* input_data = input->data.uint8; - int size = NumElements(input->dims); - - uint8_t* sorting_buffer = reinterpret_cast( - context->GetScratchBuffer(context, data->sorting_buffer)); - // Copy inputs data to the sorting buffer. We don't want to mutate the input - // tensor as it might be used by a another node. - for (int i = 0; i < size; i++) { - sorting_buffer[i] = input_data[i]; - } - - // In place insertion sort on `sorting_buffer`. - for (int i = 1; i < size; i++) { - for (int j = i; j > 0 && sorting_buffer[j] < sorting_buffer[j - 1]; j--) { - std::swap(sorting_buffer[j], sorting_buffer[j - 1]); - } - } - - TfLiteEvalTensor* median = - tflite::micro::GetEvalOutput(context, node, kMedianTensor); - TF_LITE_ENSURE(context, median != nullptr); - uint8_t* median_data = median->data.uint8; - TfLiteEvalTensor* invoke_count = - tflite::micro::GetEvalOutput(context, node, kInvokeCount); - TF_LITE_ENSURE(context, invoke_count != nullptr); - int32_t* invoke_count_data = invoke_count->data.i32; - - median_data[0] = sorting_buffer[size / 2]; - invoke_count_data[0] = *data->invoke_count; - return kTfLiteOk; -} - -const TfLiteRegistration* MockCustom::getRegistration() { - return GetMutableRegistration(); -} - -TfLiteRegistration* MockCustom::GetMutableRegistration() { - static TfLiteRegistration r; - r.init = Init; - r.prepare = Prepare; - r.invoke = Invoke; - r.free = Free; - return &r; -} - -void* MockCustom::Init(TfLiteContext* context, const char* buffer, - size_t length) { - // We don't support delegate in TFL micro. This is a weak check to test if - // context struct being zero-initialized. - TFLITE_DCHECK(context->ReplaceNodeSubsetsWithDelegateKernels == nullptr); - freed_ = false; - // Do nothing. - return nullptr; -} - -void MockCustom::Free(TfLiteContext* context, void* buffer) { freed_ = true; } - -TfLiteStatus MockCustom::Prepare(TfLiteContext* context, TfLiteNode* node) { - return kTfLiteOk; -} - -TfLiteStatus MockCustom::Invoke(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input = tflite::micro::GetEvalInput(context, node, 0); - TF_LITE_ENSURE(context, input != nullptr); - const int32_t* input_data = input->data.i32; - const TfLiteEvalTensor* weight = - tflite::micro::GetEvalInput(context, node, 1); - TF_LITE_ENSURE(context, weight != nullptr); - const uint8_t* weight_data = weight->data.uint8; - TfLiteEvalTensor* output = tflite::micro::GetEvalOutput(context, node, 0); - TF_LITE_ENSURE(context, output != nullptr); - int32_t* output_data = output->data.i32; - output_data[0] = - 0; // Catch output tensor sharing memory with an input tensor - output_data[0] = input_data[0] + weight_data[0]; - return kTfLiteOk; -} - -bool MockCustom::freed_ = false; - -const TfLiteRegistration* MultipleInputs::getRegistration() { - return GetMutableRegistration(); -} - -TfLiteRegistration* MultipleInputs::GetMutableRegistration() { - static TfLiteRegistration r; - r.init = Init; - r.prepare = Prepare; - r.invoke = Invoke; - r.free = Free; - return &r; -} - -void* MultipleInputs::Init(TfLiteContext* context, const char* buffer, - size_t length) { - // We don't support delegate in TFL micro. This is a weak check to test if - // context struct being zero-initialized. - TFLITE_DCHECK(context->ReplaceNodeSubsetsWithDelegateKernels == nullptr); - freed_ = false; - // Do nothing. - return nullptr; -} - -void MultipleInputs::Free(TfLiteContext* context, void* buffer) { - freed_ = true; -} - -TfLiteStatus MultipleInputs::Prepare(TfLiteContext* context, TfLiteNode* node) { - return kTfLiteOk; -} - -TfLiteStatus MultipleInputs::Invoke(TfLiteContext* context, TfLiteNode* node) { - const TfLiteEvalTensor* input = tflite::micro::GetEvalInput(context, node, 0); - TF_LITE_ENSURE(context, input != nullptr); - const int32_t* input_data = input->data.i32; - const TfLiteEvalTensor* input1 = - tflite::micro::GetEvalInput(context, node, 1); - TF_LITE_ENSURE(context, input1 != nullptr); - const int32_t* input_data1 = input1->data.i32; - const TfLiteEvalTensor* input2 = - tflite::micro::GetEvalInput(context, node, 2); - TF_LITE_ENSURE(context, input2 != nullptr); - const int32_t* input_data2 = input2->data.i32; - - TfLiteEvalTensor* output = tflite::micro::GetEvalOutput(context, node, 0); - TF_LITE_ENSURE(context, output != nullptr); - int32_t* output_data = output->data.i32; - output_data[0] = - 0; // Catch output tensor sharing memory with an input tensor - output_data[0] = input_data[0] + input_data1[0] + input_data2[0]; - return kTfLiteOk; -} - -bool MultipleInputs::freed_ = false; - -const TfLiteRegistration* NoOp::getRegistration() { - return GetMutableRegistration(); -} - -TfLiteRegistration* NoOp::GetMutableRegistration() { - static TfLiteRegistration r; - r.init = Init; - r.prepare = Prepare; - r.invoke = Invoke; - r.free = Free; - return &r; -} - -void* NoOp::Init(TfLiteContext* context, const char* buffer, size_t length) { - // We don't support delegate in TFL micro. This is a weak check to test if - // context struct being zero-initialized. - TFLITE_DCHECK(context->ReplaceNodeSubsetsWithDelegateKernels == nullptr); - freed_ = false; - // Do nothing. - return nullptr; -} - -void NoOp::Free(TfLiteContext* context, void* buffer) { freed_ = true; } - -TfLiteStatus NoOp::Prepare(TfLiteContext* context, TfLiteNode* node) { - return kTfLiteOk; -} - -TfLiteStatus NoOp::Invoke(TfLiteContext* context, TfLiteNode* node) { - return kTfLiteOk; -} - -bool NoOp::freed_ = false; - -AllOpsResolver GetOpResolver() { - AllOpsResolver op_resolver; - op_resolver.AddCustom("mock_custom", MockCustom::GetMutableRegistration()); - op_resolver.AddCustom("simple_stateful_op", - SimpleStatefulOp::GetMutableRegistration()); - op_resolver.AddCustom("multiple_inputs_op", - MultipleInputs::GetMutableRegistration()); - op_resolver.AddCustom("no_op", NoOp::GetMutableRegistration()); - op_resolver.AddCustom("custom_packer_op", PackerOp::GetMutableRegistration()); - return op_resolver; -} - -const Model* GetModelWithUnusedInputs() { - static Model* model = nullptr; - if (!model) { - model = const_cast(BuildModelWithUnusedInputs()); - } - return model; -} - -const Model* GetModelWithUnusedOperatorOutputs() { - static Model* model = nullptr; - if (!model) { - model = const_cast(BuildModelWithUnusedOperatorOutputs()); - } - return model; -} - -const Model* GetModelWith256x256Tensor() { - static const Model* model = BuildModelWith256x256Tensor(); - return model; -} - -const Model* GetSimpleMockModel() { - static Model* model = nullptr; - if (!model) { - model = const_cast(BuildSimpleMockModel()); - } - return model; -} - -const Model* GetSimpleMultipleInputsModel() { - static Model* model = nullptr; - if (!model) { - model = const_cast(BuildSimpleMultipleInputsModel()); - } - return model; -} - -const Model* GetSimpleModelWithSubgraphsAndIf() { - static Model* model = nullptr; - if (!model) { - model = const_cast(BuildSimpleModelWithSubgraphsAndIf()); - } - return model; -} - -const Model* GetSimpleModelWithSubgraphsAndWhile() { - static Model* model = nullptr; - if (!model) { - model = const_cast(BuildSimpleModelWithSubgraphsAndWhile()); - } - return model; -} - -const Model* GetModelWithIfAndSubgraphInputTensorOverlap() { - static Model* model = nullptr; - if (!model) { - model = const_cast(BuildModelWithIfAndSubgraphInputTensorOverlap()); - } - return model; -} - -const Model* GetSimpleModelWithNullInputsAndOutputs() { - static Model* model = nullptr; - if (!model) { - model = const_cast(BuildSimpleMockModelWithNullInputsOutputs()); - } - return model; -} - -const Model* GetComplexMockModel() { - static Model* model = nullptr; - if (!model) { - model = const_cast(BuildComplexMockModel()); - } - return model; -} - -const Model* GetSimpleModelWithBranch() { - static Model* model = nullptr; - if (!model) { - model = const_cast(BuildSimpleModelWithBranch()); - } - return model; -} - -const Model* GetModelWithOfflinePlanning(int num_tensors, - const int32_t* metadata_buffer, - NodeConnection* node_conn, - int num_conns, - int num_subgraph_inputs) { - const Model* model = BuildModelWithOfflinePlanning( - num_tensors, metadata_buffer, node_conn, num_conns, num_subgraph_inputs); - return model; -} - -const Model* GetSimpleStatefulModel() { - static Model* model = nullptr; - if (!model) { - model = const_cast(BuildSimpleStatefulModel()); - } - return model; -} - -const Tensor* Create1dFlatbufferTensor(int size, bool is_variable) { - using flatbuffers::Offset; - flatbuffers::FlatBufferBuilder* builder = BuilderInstance(); - constexpr size_t tensor_shape_size = 1; - const int32_t tensor_shape[tensor_shape_size] = {size}; - const Offset tensor_offset = CreateTensor( - *builder, builder->CreateVector(tensor_shape, tensor_shape_size), - TensorType_INT32, 0, builder->CreateString("test_tensor"), 0, - is_variable); - builder->Finish(tensor_offset); - void* tensor_pointer = builder->GetBufferPointer(); - const Tensor* tensor = flatbuffers::GetRoot(tensor_pointer); - return tensor; -} - -const Tensor* CreateQuantizedFlatbufferTensor(int size) { - using flatbuffers::Offset; - flatbuffers::FlatBufferBuilder* builder = BuilderInstance(); - constexpr size_t quant_params_size = 1; - const float min_array[quant_params_size] = {0.1f}; - const float max_array[quant_params_size] = {0.2f}; - const float scale_array[quant_params_size] = {0.3f}; - const int64_t zero_point_array[quant_params_size] = {100ll}; - - const Offset quant_params = - CreateQuantizationParameters( - *builder, - /*min=*/builder->CreateVector(min_array, quant_params_size), - /*max=*/builder->CreateVector(max_array, quant_params_size), - /*scale=*/ - builder->CreateVector(scale_array, quant_params_size), - /*zero_point=*/ - builder->CreateVector(zero_point_array, quant_params_size)); - - constexpr size_t tensor_shape_size = 1; - const int32_t tensor_shape[tensor_shape_size] = {size}; - const Offset tensor_offset = CreateTensor( - *builder, builder->CreateVector(tensor_shape, tensor_shape_size), - TensorType_INT32, 0, builder->CreateString("test_tensor"), quant_params, - false); - builder->Finish(tensor_offset); - void* tensor_pointer = builder->GetBufferPointer(); - const Tensor* tensor = flatbuffers::GetRoot(tensor_pointer); - return tensor; -} - -const Tensor* CreateMissingQuantizationFlatbufferTensor(int size) { - using flatbuffers::Offset; - flatbuffers::FlatBufferBuilder* builder = BuilderInstance(); - const Offset quant_params = - CreateQuantizationParameters(*builder, 0, 0, 0, 0, - QuantizationDetails_NONE, 0, 0); - constexpr size_t tensor_shape_size = 1; - const int32_t tensor_shape[tensor_shape_size] = {size}; - const Offset tensor_offset = CreateTensor( - *builder, builder->CreateVector(tensor_shape, tensor_shape_size), - TensorType_INT32, 0, builder->CreateString("test_tensor"), quant_params, - false); - builder->Finish(tensor_offset); - void* tensor_pointer = builder->GetBufferPointer(); - const Tensor* tensor = flatbuffers::GetRoot(tensor_pointer); - return tensor; -} - -const flatbuffers::Vector>* -CreateFlatbufferBuffers() { - using flatbuffers::Offset; - flatbuffers::FlatBufferBuilder* builder = BuilderInstance(); - constexpr size_t buffers_size = 1; - const Offset buffers[buffers_size] = { - CreateBuffer(*builder), - }; - const flatbuffers::Offset>> - buffers_offset = builder->CreateVector(buffers, buffers_size); - builder->Finish(buffers_offset); - void* buffers_pointer = builder->GetBufferPointer(); - const flatbuffers::Vector>* result = - flatbuffers::GetRoot>>( - buffers_pointer); - return result; -} - -int TestStrcmp(const char* a, const char* b) { - if ((a == nullptr) || (b == nullptr)) { - return -1; - } - while ((*a != 0) && (*a == *b)) { - a++; - b++; - } - return *reinterpret_cast(a) - - *reinterpret_cast(b); -} - -// Wrapper to forward kernel errors to the interpreter's error reporter. -void ReportOpError(struct TfLiteContext* context, const char* format, ...) { -#ifndef TF_LITE_STRIP_ERROR_STRINGS - ErrorReporter* error_reporter = static_cast(context->impl_); - va_list args; - va_start(args, format); - TF_LITE_REPORT_ERROR(error_reporter, format, args); - va_end(args); -#endif -} - -// Create a TfLiteIntArray from an array of ints. The first element in the -// supplied array must be the size of the array expressed as an int. -TfLiteIntArray* IntArrayFromInts(int* int_array) { - return reinterpret_cast(int_array); -} - -// Create a TfLiteFloatArray from an array of floats. The first element in the -// supplied array must be the size of the array expressed as a float. -TfLiteFloatArray* FloatArrayFromFloats(const float* floats) { - static_assert(sizeof(float) == sizeof(int), - "assumes sizeof(float) == sizeof(int) to perform casting"); - int size = static_cast(floats[0]); - *reinterpret_cast(const_cast(floats)) = size; - return reinterpret_cast(const_cast(floats)); -} - -TfLiteTensor CreateQuantizedBiasTensor(const float* data, int16_t* quantized, - TfLiteIntArray* dims, float input_scale, - float weights_scale, bool is_variable) { - float bias_scale = input_scale * weights_scale; - tflite::SymmetricQuantize(data, quantized, ElementCount(*dims), bias_scale); - - // Quantized int16_t tensors always have a zero point of 0, since the range of - // int16_t values is large, and because zero point costs extra cycles during - // processing. - TfLiteTensor result = - CreateQuantizedTensor(quantized, dims, bias_scale, 0, is_variable); - return result; -} - -TfLiteTensor CreateQuantizedBiasTensor(const float* data, int32_t* quantized, - TfLiteIntArray* dims, float input_scale, - float weights_scale, bool is_variable) { - float bias_scale = input_scale * weights_scale; - tflite::SymmetricQuantize(data, quantized, ElementCount(*dims), bias_scale); - - // Quantized int32_t tensors always have a zero point of 0, since the range of - // int32_t values is large, and because zero point costs extra cycles during - // processing. - TfLiteTensor result = - CreateQuantizedTensor(quantized, dims, bias_scale, 0, is_variable); - return result; -} - -TfLiteTensor CreateQuantizedBiasTensor(const float* data, - std::int64_t* quantized, - TfLiteIntArray* dims, float input_scale, - float weights_scale, bool is_variable) { - float bias_scale = input_scale * weights_scale; - tflite::SymmetricQuantize(data, quantized, ElementCount(*dims), bias_scale); - - // Quantized int32_t tensors always have a zero point of 0, since the range of - // int32_t values is large, and because zero point costs extra cycles during - // processing. - TfLiteTensor result = - CreateQuantizedTensor(quantized, dims, bias_scale, 0, is_variable); - return result; -} - -// Quantizes int32_t bias tensor with per-channel weights determined by input -// scale multiplied by weight scale for each channel. -template -TfLiteTensor CreatePerChannelQuantizedBiasTensor( - const float* input, T* quantized, TfLiteIntArray* dims, float input_scale, - float* weight_scales, float* scales, int* zero_points, - TfLiteAffineQuantization* affine_quant, int quantized_dimension, - bool is_variable) { - int input_size = ElementCount(*dims); - int num_channels = dims->data[quantized_dimension]; - // First element is reserved for array length - zero_points[0] = num_channels; - scales[0] = static_cast(num_channels); - float* scales_array = &scales[1]; - for (int i = 0; i < num_channels; i++) { - scales_array[i] = input_scale * weight_scales[i]; - zero_points[i + 1] = 0; - } - - SymmetricPerChannelQuantize(input, quantized, input_size, num_channels, - scales_array); - - affine_quant->scale = FloatArrayFromFloats(scales); - affine_quant->zero_point = IntArrayFromInts(zero_points); - affine_quant->quantized_dimension = quantized_dimension; - - TfLiteTensor result = CreateTensor(quantized, dims, is_variable); - result.quantization = {kTfLiteAffineQuantization, affine_quant}; - return result; -} - -TfLiteTensor CreatePerChannelQuantizedBiasTensor( - const float* input, int32_t* quantized, TfLiteIntArray* dims, - float input_scale, float* weight_scales, float* scales, int* zero_points, - TfLiteAffineQuantization* affine_quant, int quantized_dimension, - bool is_variable) { - return CreatePerChannelQuantizedBiasTensor( - input, quantized, dims, input_scale, weight_scales, scales, zero_points, - affine_quant, quantized_dimension, is_variable); -} - -TfLiteTensor CreatePerChannelQuantizedBiasTensor( - const float* input, std::int64_t* quantized, TfLiteIntArray* dims, - float input_scale, float* weight_scales, float* scales, int* zero_points, - TfLiteAffineQuantization* affine_quant, int quantized_dimension, - bool is_variable) { - return CreatePerChannelQuantizedBiasTensor( - input, quantized, dims, input_scale, weight_scales, scales, zero_points, - affine_quant, quantized_dimension, is_variable); -} - -TfLiteTensor CreateSymmetricPerChannelQuantizedTensor( - const float* input, int8_t* quantized, TfLiteIntArray* dims, float* scales, - int* zero_points, TfLiteAffineQuantization* affine_quant, - int quantized_dimension, bool is_variable) { - int channel_count = dims->data[quantized_dimension]; - scales[0] = static_cast(channel_count); - zero_points[0] = channel_count; - - SignedSymmetricPerChannelQuantize(input, dims, quantized_dimension, quantized, - &scales[1]); - - for (int i = 0; i < channel_count; i++) { - zero_points[i + 1] = 0; - } - - affine_quant->scale = FloatArrayFromFloats(scales); - affine_quant->zero_point = IntArrayFromInts(zero_points); - affine_quant->quantized_dimension = quantized_dimension; - - TfLiteTensor result = CreateTensor(quantized, dims, is_variable); - result.quantization = {kTfLiteAffineQuantization, affine_quant}; - return result; -} - -size_t GetModelTensorCount(const Model* model) { - auto* subgraphs = model->subgraphs(); - if (subgraphs) { - return (*subgraphs)[0]->tensors()->size(); - } - return 0; -} - -} // namespace testing -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/micro/test_helpers.h b/code/components/tflite-lib/tensorflow/lite/micro/test_helpers.h deleted file mode 100644 index 5441ce3e..00000000 --- a/code/components/tflite-lib/tensorflow/lite/micro/test_helpers.h +++ /dev/null @@ -1,298 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_LITE_MICRO_TEST_HELPERS_H_ -#define TENSORFLOW_LITE_MICRO_TEST_HELPERS_H_ - -#include -#include - -#include "flatbuffers/flatbuffers.h" // from @flatbuffers -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/kernels/internal/compatibility.h" -#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" -#include "tensorflow/lite/micro/all_ops_resolver.h" -#include "tensorflow/lite/micro/micro_utils.h" -#include "tensorflow/lite/portable_type_to_tflitetype.h" -#include "tensorflow/lite/schema/schema_generated.h" - -namespace tflite { -namespace testing { - -constexpr int kOfflinePlannerHeaderSize = 3; - -struct NodeConnection_ { - std::initializer_list input; - std::initializer_list output; -}; -typedef struct NodeConnection_ NodeConnection; - -// A simple operator that returns the median of the input with the number of -// times the kernel was invoked. The implementation below is deliberately -// complicated, just to demonstrate how kernel memory planning works. -class SimpleStatefulOp { - static constexpr int kBufferNotAllocated = 0; - // Inputs: - static constexpr int kInputTensor = 0; - // Outputs: - static constexpr int kMedianTensor = 0; - static constexpr int kInvokeCount = 1; - struct OpData { - int* invoke_count = nullptr; - int sorting_buffer = kBufferNotAllocated; - }; - - public: - static const TfLiteRegistration* getRegistration(); - static TfLiteRegistration* GetMutableRegistration(); - static void* Init(TfLiteContext* context, const char* buffer, size_t length); - static TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node); - static TfLiteStatus Invoke(TfLiteContext* context, TfLiteNode* node); -}; - -class MockCustom { - public: - static const TfLiteRegistration* getRegistration(); - static TfLiteRegistration* GetMutableRegistration(); - static void* Init(TfLiteContext* context, const char* buffer, size_t length); - static void Free(TfLiteContext* context, void* buffer); - static TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node); - static TfLiteStatus Invoke(TfLiteContext* context, TfLiteNode* node); - - static bool freed_; -}; - -// A simple operator with the purpose of testing multiple inputs. It returns -// the sum of the inputs. -class MultipleInputs { - public: - static const TfLiteRegistration* getRegistration(); - static TfLiteRegistration* GetMutableRegistration(); - static void* Init(TfLiteContext* context, const char* buffer, size_t length); - static void Free(TfLiteContext* context, void* buffer); - static TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node); - static TfLiteStatus Invoke(TfLiteContext* context, TfLiteNode* node); - - static bool freed_; -}; - -// A simple no-op operator. -class NoOp { - public: - static const TfLiteRegistration* getRegistration(); - static TfLiteRegistration* GetMutableRegistration(); - static void* Init(TfLiteContext* context, const char* buffer, size_t length); - static void Free(TfLiteContext* context, void* buffer); - static TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node); - static TfLiteStatus Invoke(TfLiteContext* context, TfLiteNode* node); - - static bool freed_; -}; - -// Returns an Op Resolver that can be used in the testing code. -AllOpsResolver GetOpResolver(); - -// Returns a simple example flatbuffer TensorFlow Lite model. Contains 1 input, -// 1 layer of weights, 1 output Tensor, and 1 operator. -const Model* GetSimpleMockModel(); - -// Returns a flatbuffer TensorFlow Lite model with more inputs, variable -// tensors, and operators. -const Model* GetComplexMockModel(); - -// Returns a simple example flatbuffer TensorFlow Lite model. Contains 1 input, -// 1 layer of weights, 1 output Tensor, and 1 operator. -// The size of all three tensors is 256 x 256, which is larger than what other -// models provide from this test helper. -const Model* GetModelWith256x256Tensor(); - -// Returns a simple flatbuffer model with two branches. -const Model* GetSimpleModelWithBranch(); - -// Returns a simple example flatbuffer TensorFlow Lite model. Contains 3 inputs, -// 1 output Tensor, and 1 operator. -const Model* GetSimpleMultipleInputsModel(); - -// Returns a simple flatbuffer model with offline planned tensors -// @param[in] num_tensors Number of tensors in the model. -// @param[in] metadata_buffer Metadata for offline planner. -// @param[in] node_con List of connections, i.e. operators -// in the model. -// @param[in] num_conns Number of connections. -// @param[in] num_subgraph_inputs How many of the input tensors are in -// the subgraph inputs. The default value -// of 0 means all of the input tensors -// are in the subgraph input list. There -// must be at least 1 input tensor in the -// subgraph input list. -const Model* GetModelWithOfflinePlanning(int num_tensors, - const int32_t* metadata_buffer, - NodeConnection* node_conn, - int num_conns, - int num_subgraph_inputs = 0); - -// Returns a flatbuffer with a single operator, two inputs (one unused) and one -// output. -const Model* GetModelWithUnusedInputs(); - -// Returns a flatbuffer with a single operator, zero inputs and two outputs -// (one unused). -const Model* GetModelWithUnusedOperatorOutputs(); - -// Returns a flatbuffer model with `simple_stateful_op` -const Model* GetSimpleStatefulModel(); - -// Returns a flatbuffer model with "if" and two subgraphs. -const Model* GetSimpleModelWithSubgraphsAndIf(); - -// Returns a flatbuffer model with "while" and three subgraphs. -const Model* GetSimpleModelWithSubgraphsAndWhile(); - -// Returns a flatbuffer model with "if" and two subgraphs and the input tensor 1 -// of "if" subgraph overlaps with the input tensor 2 of subgraph 1. -const Model* GetModelWithIfAndSubgraphInputTensorOverlap(); - -// Returns a flatbuffer model with null subgraph/operator inputs and outputs. -const Model* GetSimpleModelWithNullInputsAndOutputs(); - -// Builds a one-dimensional flatbuffer tensor of the given size. -const Tensor* Create1dFlatbufferTensor(int size, bool is_variable = false); - -// Builds a one-dimensional flatbuffer tensor of the given size with -// quantization metadata. -const Tensor* CreateQuantizedFlatbufferTensor(int size); - -// Creates a one-dimensional tensor with no quantization metadata. -const Tensor* CreateMissingQuantizationFlatbufferTensor(int size); - -// Creates a vector of flatbuffer buffers. -const flatbuffers::Vector>* -CreateFlatbufferBuffers(); - -// Performs a simple string comparison without requiring standard C library. -int TestStrcmp(const char* a, const char* b); - -// Wrapper to forward kernel errors to the interpreter's error reporter. -void ReportOpError(struct TfLiteContext* context, const char* format, ...); - -void PopulateContext(TfLiteTensor* tensors, int tensors_size, - TfLiteContext* context); - -// Create a TfLiteIntArray from an array of ints. The first element in the -// supplied array must be the size of the array expressed as an int. -TfLiteIntArray* IntArrayFromInts(int* int_array); - -// Create a TfLiteFloatArray from an array of floats. The first element in the -// supplied array must be the size of the array expressed as a float. -TfLiteFloatArray* FloatArrayFromFloats(const float* floats); - -template -TfLiteTensor CreateTensor(const T* data, TfLiteIntArray* dims, - const bool is_variable = false) { - TfLiteTensor result; - result.dims = dims; - result.params = {}; - result.quantization = {kTfLiteNoQuantization, nullptr}; - result.is_variable = is_variable; - result.allocation_type = kTfLiteMemNone; - result.type = typeToTfLiteType(); - // Const cast is used to allow passing in const and non-const arrays within a - // single CreateTensor method. A Const array should be used for immutable - // input tensors and non-const array should be used for mutable and output - // tensors. - result.data.data = const_cast(data); - result.quantization = {kTfLiteAffineQuantization, nullptr}; - result.bytes = ElementCount(*dims) * sizeof(T); - return result; -} - -template -TfLiteTensor CreateQuantizedTensor(const T* data, TfLiteIntArray* dims, - const float scale, const int zero_point = 0, - const bool is_variable = false) { - TfLiteTensor result = CreateTensor(data, dims, is_variable); - result.params = {scale, zero_point}; - result.quantization = {kTfLiteAffineQuantization, nullptr}; - return result; -} - -template -TfLiteTensor CreateQuantizedTensor(const float* input, T* quantized, - TfLiteIntArray* dims, float scale, - int zero_point, bool is_variable = false) { - int input_size = ElementCount(*dims); - tflite::Quantize(input, quantized, input_size, scale, zero_point); - return CreateQuantizedTensor(quantized, dims, scale, zero_point, is_variable); -} - -TfLiteTensor CreateQuantizedBiasTensor(const float* data, int16_t* quantized, - TfLiteIntArray* dims, float input_scale, - float weights_scale, - bool is_variable = false); - -TfLiteTensor CreateQuantizedBiasTensor(const float* data, int32_t* quantized, - TfLiteIntArray* dims, float input_scale, - float weights_scale, - bool is_variable = false); - -TfLiteTensor CreateQuantizedBiasTensor(const float* data, - std::int64_t* quantized, - TfLiteIntArray* dims, float input_scale, - float weights_scale, - bool is_variable = false); - -// Quantizes int32_t bias tensor with per-channel weights determined by input -// scale multiplied by weight scale for each channel. -TfLiteTensor CreatePerChannelQuantizedBiasTensor( - const float* input, int32_t* quantized, TfLiteIntArray* dims, - float input_scale, float* weight_scales, float* scales, int* zero_points, - TfLiteAffineQuantization* affine_quant, int quantized_dimension, - bool is_variable = false); - -// Quantizes int64_t bias tensor with per-channel weights determined by input -// scale multiplied by weight scale for each channel. -TfLiteTensor CreatePerChannelQuantizedBiasTensor( - const float* input, std::int64_t* quantized, TfLiteIntArray* dims, - float input_scale, float* weight_scales, float* scales, int* zero_points, - TfLiteAffineQuantization* affine_quant, int quantized_dimension, - bool is_variable = false); - -TfLiteTensor CreateSymmetricPerChannelQuantizedTensor( - const float* input, int8_t* quantized, TfLiteIntArray* dims, float* scales, - int* zero_points, TfLiteAffineQuantization* affine_quant, - int quantized_dimension, bool is_variable = false); - -// Returns the number of tensors in the default subgraph for a tflite::Model. -size_t GetModelTensorCount(const Model* model); - -// Derives the quantization scaling factor from a min and max range. -template -inline float ScaleFromMinMax(const float min, const float max) { - return (max - min) / - static_cast((std::numeric_limits::max() * 1.0) - - std::numeric_limits::min()); -} - -// Derives the quantization zero point from a min and max range. -template -inline int ZeroPointFromMinMax(const float min, const float max) { - return static_cast(std::numeric_limits::min()) + - static_cast(-min / ScaleFromMinMax(min, max) + 0.5f); -} - -} // namespace testing -} // namespace tflite - -#endif // TENSORFLOW_LITE_MICRO_TEST_HELPERS_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/portable_type_to_tflitetype.h b/code/components/tflite-lib/tensorflow/lite/portable_type_to_tflitetype.h deleted file mode 100644 index 52d7fdef..00000000 --- a/code/components/tflite-lib/tensorflow/lite/portable_type_to_tflitetype.h +++ /dev/null @@ -1,75 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_PORTABLE_TYPE_TO_TFLITETYPE_H_ -#define TENSORFLOW_LITE_PORTABLE_TYPE_TO_TFLITETYPE_H_ - -// Most of the definitions have been moved to this subheader so that Micro -// can include it without relying on and , which isn't -// available on all platforms. - -// Arduino build defines abs as a macro here. That is invalid C++, and breaks -// libc++'s header, undefine it. -#ifdef abs -#undef abs -#endif - -#include - -#include "tensorflow/lite/c/common.h" - -namespace tflite { - -// Map statically from a C++ type to a TfLiteType. Used in interpreter for -// safe casts. -// Example: -// typeToTfLiteType() -> kTfLiteBool -template -constexpr TfLiteType typeToTfLiteType() { - return kTfLiteNoType; -} -// Map from TfLiteType to the corresponding C++ type. -// Example: -// TfLiteTypeToType::Type -> bool -template -struct TfLiteTypeToType {}; // Specializations below - -// Template specialization for both typeToTfLiteType and TfLiteTypeToType. -#define MATCH_TYPE_AND_TFLITE_TYPE(CPP_TYPE, TFLITE_TYPE_ENUM) \ - template <> \ - constexpr TfLiteType typeToTfLiteType() { \ - return TFLITE_TYPE_ENUM; \ - } \ - template <> \ - struct TfLiteTypeToType { \ - using Type = CPP_TYPE; \ - } - -// No string mapping is included here, since the TF Lite packed representation -// doesn't correspond to a C++ type well. -MATCH_TYPE_AND_TFLITE_TYPE(int32_t, kTfLiteInt32); -MATCH_TYPE_AND_TFLITE_TYPE(uint32_t, kTfLiteUInt32); -MATCH_TYPE_AND_TFLITE_TYPE(int16_t, kTfLiteInt16); -MATCH_TYPE_AND_TFLITE_TYPE(uint16_t, kTfLiteUInt16); -MATCH_TYPE_AND_TFLITE_TYPE(int64_t, kTfLiteInt64); -MATCH_TYPE_AND_TFLITE_TYPE(float, kTfLiteFloat32); -MATCH_TYPE_AND_TFLITE_TYPE(unsigned char, kTfLiteUInt8); -MATCH_TYPE_AND_TFLITE_TYPE(int8_t, kTfLiteInt8); -MATCH_TYPE_AND_TFLITE_TYPE(bool, kTfLiteBool); -MATCH_TYPE_AND_TFLITE_TYPE(TfLiteFloat16, kTfLiteFloat16); -MATCH_TYPE_AND_TFLITE_TYPE(double, kTfLiteFloat64); -MATCH_TYPE_AND_TFLITE_TYPE(uint64_t, kTfLiteUInt64); - -} // namespace tflite -#endif // TENSORFLOW_LITE_PORTABLE_TYPE_TO_TFLITETYPE_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/schema/schema_generated.h b/code/components/tflite-lib/tensorflow/lite/schema/schema_generated.h deleted file mode 100644 index 5d1cee85..00000000 --- a/code/components/tflite-lib/tensorflow/lite/schema/schema_generated.h +++ /dev/null @@ -1,19788 +0,0 @@ -// automatically generated by the FlatBuffers compiler, do not modify - - -#ifndef FLATBUFFERS_GENERATED_SCHEMA_TFLITE_H_ -#define FLATBUFFERS_GENERATED_SCHEMA_TFLITE_H_ - -#include "flatbuffers/flatbuffers.h" - -namespace tflite { - -struct CustomQuantization; -struct CustomQuantizationBuilder; -struct CustomQuantizationT; - -struct QuantizationParameters; -struct QuantizationParametersBuilder; -struct QuantizationParametersT; - -struct Int32Vector; -struct Int32VectorBuilder; -struct Int32VectorT; - -struct Uint16Vector; -struct Uint16VectorBuilder; -struct Uint16VectorT; - -struct Uint8Vector; -struct Uint8VectorBuilder; -struct Uint8VectorT; - -struct DimensionMetadata; -struct DimensionMetadataBuilder; -struct DimensionMetadataT; - -struct SparsityParameters; -struct SparsityParametersBuilder; -struct SparsityParametersT; - -struct Tensor; -struct TensorBuilder; -struct TensorT; - -struct Conv2DOptions; -struct Conv2DOptionsBuilder; -struct Conv2DOptionsT; - -struct Conv3DOptions; -struct Conv3DOptionsBuilder; -struct Conv3DOptionsT; - -struct Pool2DOptions; -struct Pool2DOptionsBuilder; -struct Pool2DOptionsT; - -struct DepthwiseConv2DOptions; -struct DepthwiseConv2DOptionsBuilder; -struct DepthwiseConv2DOptionsT; - -struct ConcatEmbeddingsOptions; -struct ConcatEmbeddingsOptionsBuilder; -struct ConcatEmbeddingsOptionsT; - -struct LSHProjectionOptions; -struct LSHProjectionOptionsBuilder; -struct LSHProjectionOptionsT; - -struct SVDFOptions; -struct SVDFOptionsBuilder; -struct SVDFOptionsT; - -struct RNNOptions; -struct RNNOptionsBuilder; -struct RNNOptionsT; - -struct SequenceRNNOptions; -struct SequenceRNNOptionsBuilder; -struct SequenceRNNOptionsT; - -struct BidirectionalSequenceRNNOptions; -struct BidirectionalSequenceRNNOptionsBuilder; -struct BidirectionalSequenceRNNOptionsT; - -struct FullyConnectedOptions; -struct FullyConnectedOptionsBuilder; -struct FullyConnectedOptionsT; - -struct SoftmaxOptions; -struct SoftmaxOptionsBuilder; -struct SoftmaxOptionsT; - -struct ConcatenationOptions; -struct ConcatenationOptionsBuilder; -struct ConcatenationOptionsT; - -struct AddOptions; -struct AddOptionsBuilder; -struct AddOptionsT; - -struct MulOptions; -struct MulOptionsBuilder; -struct MulOptionsT; - -struct L2NormOptions; -struct L2NormOptionsBuilder; -struct L2NormOptionsT; - -struct LocalResponseNormalizationOptions; -struct LocalResponseNormalizationOptionsBuilder; -struct LocalResponseNormalizationOptionsT; - -struct LSTMOptions; -struct LSTMOptionsBuilder; -struct LSTMOptionsT; - -struct UnidirectionalSequenceLSTMOptions; -struct UnidirectionalSequenceLSTMOptionsBuilder; -struct UnidirectionalSequenceLSTMOptionsT; - -struct BidirectionalSequenceLSTMOptions; -struct BidirectionalSequenceLSTMOptionsBuilder; -struct BidirectionalSequenceLSTMOptionsT; - -struct ResizeBilinearOptions; -struct ResizeBilinearOptionsBuilder; -struct ResizeBilinearOptionsT; - -struct ResizeNearestNeighborOptions; -struct ResizeNearestNeighborOptionsBuilder; -struct ResizeNearestNeighborOptionsT; - -struct CallOptions; -struct CallOptionsBuilder; -struct CallOptionsT; - -struct PadOptions; -struct PadOptionsBuilder; -struct PadOptionsT; - -struct PadV2Options; -struct PadV2OptionsBuilder; -struct PadV2OptionsT; - -struct ReshapeOptions; -struct ReshapeOptionsBuilder; -struct ReshapeOptionsT; - -struct SpaceToBatchNDOptions; -struct SpaceToBatchNDOptionsBuilder; -struct SpaceToBatchNDOptionsT; - -struct BatchToSpaceNDOptions; -struct BatchToSpaceNDOptionsBuilder; -struct BatchToSpaceNDOptionsT; - -struct SkipGramOptions; -struct SkipGramOptionsBuilder; -struct SkipGramOptionsT; - -struct SpaceToDepthOptions; -struct SpaceToDepthOptionsBuilder; -struct SpaceToDepthOptionsT; - -struct DepthToSpaceOptions; -struct DepthToSpaceOptionsBuilder; -struct DepthToSpaceOptionsT; - -struct SubOptions; -struct SubOptionsBuilder; -struct SubOptionsT; - -struct DivOptions; -struct DivOptionsBuilder; -struct DivOptionsT; - -struct TopKV2Options; -struct TopKV2OptionsBuilder; -struct TopKV2OptionsT; - -struct EmbeddingLookupSparseOptions; -struct EmbeddingLookupSparseOptionsBuilder; -struct EmbeddingLookupSparseOptionsT; - -struct GatherOptions; -struct GatherOptionsBuilder; -struct GatherOptionsT; - -struct TransposeOptions; -struct TransposeOptionsBuilder; -struct TransposeOptionsT; - -struct ExpOptions; -struct ExpOptionsBuilder; -struct ExpOptionsT; - -struct CosOptions; -struct CosOptionsBuilder; -struct CosOptionsT; - -struct ReducerOptions; -struct ReducerOptionsBuilder; -struct ReducerOptionsT; - -struct SqueezeOptions; -struct SqueezeOptionsBuilder; -struct SqueezeOptionsT; - -struct SplitOptions; -struct SplitOptionsBuilder; -struct SplitOptionsT; - -struct SplitVOptions; -struct SplitVOptionsBuilder; -struct SplitVOptionsT; - -struct StridedSliceOptions; -struct StridedSliceOptionsBuilder; -struct StridedSliceOptionsT; - -struct LogSoftmaxOptions; -struct LogSoftmaxOptionsBuilder; -struct LogSoftmaxOptionsT; - -struct CastOptions; -struct CastOptionsBuilder; -struct CastOptionsT; - -struct DequantizeOptions; -struct DequantizeOptionsBuilder; -struct DequantizeOptionsT; - -struct MaximumMinimumOptions; -struct MaximumMinimumOptionsBuilder; -struct MaximumMinimumOptionsT; - -struct TileOptions; -struct TileOptionsBuilder; -struct TileOptionsT; - -struct ArgMaxOptions; -struct ArgMaxOptionsBuilder; -struct ArgMaxOptionsT; - -struct ArgMinOptions; -struct ArgMinOptionsBuilder; -struct ArgMinOptionsT; - -struct GreaterOptions; -struct GreaterOptionsBuilder; -struct GreaterOptionsT; - -struct GreaterEqualOptions; -struct GreaterEqualOptionsBuilder; -struct GreaterEqualOptionsT; - -struct LessOptions; -struct LessOptionsBuilder; -struct LessOptionsT; - -struct LessEqualOptions; -struct LessEqualOptionsBuilder; -struct LessEqualOptionsT; - -struct NegOptions; -struct NegOptionsBuilder; -struct NegOptionsT; - -struct SelectOptions; -struct SelectOptionsBuilder; -struct SelectOptionsT; - -struct SliceOptions; -struct SliceOptionsBuilder; -struct SliceOptionsT; - -struct TransposeConvOptions; -struct TransposeConvOptionsBuilder; -struct TransposeConvOptionsT; - -struct ExpandDimsOptions; -struct ExpandDimsOptionsBuilder; -struct ExpandDimsOptionsT; - -struct SparseToDenseOptions; -struct SparseToDenseOptionsBuilder; -struct SparseToDenseOptionsT; - -struct EqualOptions; -struct EqualOptionsBuilder; -struct EqualOptionsT; - -struct NotEqualOptions; -struct NotEqualOptionsBuilder; -struct NotEqualOptionsT; - -struct ShapeOptions; -struct ShapeOptionsBuilder; -struct ShapeOptionsT; - -struct RankOptions; -struct RankOptionsBuilder; -struct RankOptionsT; - -struct PowOptions; -struct PowOptionsBuilder; -struct PowOptionsT; - -struct FakeQuantOptions; -struct FakeQuantOptionsBuilder; -struct FakeQuantOptionsT; - -struct PackOptions; -struct PackOptionsBuilder; -struct PackOptionsT; - -struct LogicalOrOptions; -struct LogicalOrOptionsBuilder; -struct LogicalOrOptionsT; - -struct OneHotOptions; -struct OneHotOptionsBuilder; -struct OneHotOptionsT; - -struct AbsOptions; -struct AbsOptionsBuilder; -struct AbsOptionsT; - -struct HardSwishOptions; -struct HardSwishOptionsBuilder; -struct HardSwishOptionsT; - -struct LogicalAndOptions; -struct LogicalAndOptionsBuilder; -struct LogicalAndOptionsT; - -struct LogicalNotOptions; -struct LogicalNotOptionsBuilder; -struct LogicalNotOptionsT; - -struct UnpackOptions; -struct UnpackOptionsBuilder; -struct UnpackOptionsT; - -struct FloorDivOptions; -struct FloorDivOptionsBuilder; -struct FloorDivOptionsT; - -struct SquareOptions; -struct SquareOptionsBuilder; -struct SquareOptionsT; - -struct ZerosLikeOptions; -struct ZerosLikeOptionsBuilder; -struct ZerosLikeOptionsT; - -struct FillOptions; -struct FillOptionsBuilder; -struct FillOptionsT; - -struct FloorModOptions; -struct FloorModOptionsBuilder; -struct FloorModOptionsT; - -struct RangeOptions; -struct RangeOptionsBuilder; -struct RangeOptionsT; - -struct LeakyReluOptions; -struct LeakyReluOptionsBuilder; -struct LeakyReluOptionsT; - -struct SquaredDifferenceOptions; -struct SquaredDifferenceOptionsBuilder; -struct SquaredDifferenceOptionsT; - -struct MirrorPadOptions; -struct MirrorPadOptionsBuilder; -struct MirrorPadOptionsT; - -struct UniqueOptions; -struct UniqueOptionsBuilder; -struct UniqueOptionsT; - -struct ReverseV2Options; -struct ReverseV2OptionsBuilder; -struct ReverseV2OptionsT; - -struct AddNOptions; -struct AddNOptionsBuilder; -struct AddNOptionsT; - -struct GatherNdOptions; -struct GatherNdOptionsBuilder; -struct GatherNdOptionsT; - -struct WhereOptions; -struct WhereOptionsBuilder; -struct WhereOptionsT; - -struct ReverseSequenceOptions; -struct ReverseSequenceOptionsBuilder; -struct ReverseSequenceOptionsT; - -struct MatrixDiagOptions; -struct MatrixDiagOptionsBuilder; -struct MatrixDiagOptionsT; - -struct QuantizeOptions; -struct QuantizeOptionsBuilder; -struct QuantizeOptionsT; - -struct MatrixSetDiagOptions; -struct MatrixSetDiagOptionsBuilder; -struct MatrixSetDiagOptionsT; - -struct IfOptions; -struct IfOptionsBuilder; -struct IfOptionsT; - -struct CallOnceOptions; -struct CallOnceOptionsBuilder; -struct CallOnceOptionsT; - -struct WhileOptions; -struct WhileOptionsBuilder; -struct WhileOptionsT; - -struct NonMaxSuppressionV4Options; -struct NonMaxSuppressionV4OptionsBuilder; -struct NonMaxSuppressionV4OptionsT; - -struct NonMaxSuppressionV5Options; -struct NonMaxSuppressionV5OptionsBuilder; -struct NonMaxSuppressionV5OptionsT; - -struct ScatterNdOptions; -struct ScatterNdOptionsBuilder; -struct ScatterNdOptionsT; - -struct SelectV2Options; -struct SelectV2OptionsBuilder; -struct SelectV2OptionsT; - -struct DensifyOptions; -struct DensifyOptionsBuilder; -struct DensifyOptionsT; - -struct SegmentSumOptions; -struct SegmentSumOptionsBuilder; -struct SegmentSumOptionsT; - -struct BatchMatMulOptions; -struct BatchMatMulOptionsBuilder; -struct BatchMatMulOptionsT; - -struct CumsumOptions; -struct CumsumOptionsBuilder; -struct CumsumOptionsT; - -struct BroadcastToOptions; -struct BroadcastToOptionsBuilder; -struct BroadcastToOptionsT; - -struct Rfft2dOptions; -struct Rfft2dOptionsBuilder; -struct Rfft2dOptionsT; - -struct HashtableOptions; -struct HashtableOptionsBuilder; -struct HashtableOptionsT; - -struct HashtableFindOptions; -struct HashtableFindOptionsBuilder; -struct HashtableFindOptionsT; - -struct HashtableImportOptions; -struct HashtableImportOptionsBuilder; -struct HashtableImportOptionsT; - -struct HashtableSizeOptions; -struct HashtableSizeOptionsBuilder; -struct HashtableSizeOptionsT; - -struct VarHandleOptions; -struct VarHandleOptionsBuilder; -struct VarHandleOptionsT; - -struct ReadVariableOptions; -struct ReadVariableOptionsBuilder; -struct ReadVariableOptionsT; - -struct AssignVariableOptions; -struct AssignVariableOptionsBuilder; -struct AssignVariableOptionsT; - -struct RandomOptions; -struct RandomOptionsBuilder; -struct RandomOptionsT; - -struct BucketizeOptions; -struct BucketizeOptionsBuilder; -struct BucketizeOptionsT; - -struct GeluOptions; -struct GeluOptionsBuilder; -struct GeluOptionsT; - -struct DynamicUpdateSliceOptions; -struct DynamicUpdateSliceOptionsBuilder; -struct DynamicUpdateSliceOptionsT; - -struct UnsortedSegmentProdOptions; -struct UnsortedSegmentProdOptionsBuilder; -struct UnsortedSegmentProdOptionsT; - -struct UnsortedSegmentMaxOptions; -struct UnsortedSegmentMaxOptionsBuilder; -struct UnsortedSegmentMaxOptionsT; - -struct UnsortedSegmentSumOptions; -struct UnsortedSegmentSumOptionsBuilder; -struct UnsortedSegmentSumOptionsT; - -struct ATan2Options; -struct ATan2OptionsBuilder; -struct ATan2OptionsT; - -struct UnsortedSegmentMinOptions; -struct UnsortedSegmentMinOptionsBuilder; -struct UnsortedSegmentMinOptionsT; - -struct OperatorCode; -struct OperatorCodeBuilder; -struct OperatorCodeT; - -struct Operator; -struct OperatorBuilder; -struct OperatorT; - -struct SubGraph; -struct SubGraphBuilder; -struct SubGraphT; - -struct Buffer; -struct BufferBuilder; -struct BufferT; - -struct Metadata; -struct MetadataBuilder; -struct MetadataT; - -struct TensorMap; -struct TensorMapBuilder; -struct TensorMapT; - -struct SignatureDef; -struct SignatureDefBuilder; -struct SignatureDefT; - -struct Model; -struct ModelBuilder; -struct ModelT; - -enum TensorType { - TensorType_FLOAT32 = 0, - TensorType_FLOAT16 = 1, - TensorType_INT32 = 2, - TensorType_UINT8 = 3, - TensorType_INT64 = 4, - TensorType_STRING = 5, - TensorType_BOOL = 6, - TensorType_INT16 = 7, - TensorType_COMPLEX64 = 8, - TensorType_INT8 = 9, - TensorType_FLOAT64 = 10, - TensorType_COMPLEX128 = 11, - TensorType_UINT64 = 12, - TensorType_RESOURCE = 13, - TensorType_VARIANT = 14, - TensorType_UINT32 = 15, - TensorType_UINT16 = 16, - TensorType_MIN = TensorType_FLOAT32, - TensorType_MAX = TensorType_UINT16 -}; - -inline const TensorType (&EnumValuesTensorType())[17] { - static const TensorType values[] = { - TensorType_FLOAT32, - TensorType_FLOAT16, - TensorType_INT32, - TensorType_UINT8, - TensorType_INT64, - TensorType_STRING, - TensorType_BOOL, - TensorType_INT16, - TensorType_COMPLEX64, - TensorType_INT8, - TensorType_FLOAT64, - TensorType_COMPLEX128, - TensorType_UINT64, - TensorType_RESOURCE, - TensorType_VARIANT, - TensorType_UINT32, - TensorType_UINT16 - }; - return values; -} - -inline const char * const *EnumNamesTensorType() { - static const char * const names[18] = { - "FLOAT32", - "FLOAT16", - "INT32", - "UINT8", - "INT64", - "STRING", - "BOOL", - "INT16", - "COMPLEX64", - "INT8", - "FLOAT64", - "COMPLEX128", - "UINT64", - "RESOURCE", - "VARIANT", - "UINT32", - "UINT16", - nullptr - }; - return names; -} - -inline const char *EnumNameTensorType(TensorType e) { - if (flatbuffers::IsOutRange(e, TensorType_FLOAT32, TensorType_UINT16)) return ""; - const size_t index = static_cast(e); - return EnumNamesTensorType()[index]; -} - -enum QuantizationDetails { - QuantizationDetails_NONE = 0, - QuantizationDetails_CustomQuantization = 1, - QuantizationDetails_MIN = QuantizationDetails_NONE, - QuantizationDetails_MAX = QuantizationDetails_CustomQuantization -}; - -inline const QuantizationDetails (&EnumValuesQuantizationDetails())[2] { - static const QuantizationDetails values[] = { - QuantizationDetails_NONE, - QuantizationDetails_CustomQuantization - }; - return values; -} - -inline const char * const *EnumNamesQuantizationDetails() { - static const char * const names[3] = { - "NONE", - "CustomQuantization", - nullptr - }; - return names; -} - -inline const char *EnumNameQuantizationDetails(QuantizationDetails e) { - if (flatbuffers::IsOutRange(e, QuantizationDetails_NONE, QuantizationDetails_CustomQuantization)) return ""; - const size_t index = static_cast(e); - return EnumNamesQuantizationDetails()[index]; -} - -template struct QuantizationDetailsTraits { - static const QuantizationDetails enum_value = QuantizationDetails_NONE; -}; - -template<> struct QuantizationDetailsTraits { - static const QuantizationDetails enum_value = QuantizationDetails_CustomQuantization; -}; - -struct QuantizationDetailsUnion { - QuantizationDetails type; - void *value; - - QuantizationDetailsUnion() : type(QuantizationDetails_NONE), value(nullptr) {} - QuantizationDetailsUnion(QuantizationDetailsUnion&& u) FLATBUFFERS_NOEXCEPT : - type(QuantizationDetails_NONE), value(nullptr) - { std::swap(type, u.type); std::swap(value, u.value); } - QuantizationDetailsUnion(const QuantizationDetailsUnion &); - QuantizationDetailsUnion &operator=(const QuantizationDetailsUnion &u) - { QuantizationDetailsUnion t(u); std::swap(type, t.type); std::swap(value, t.value); return *this; } - QuantizationDetailsUnion &operator=(QuantizationDetailsUnion &&u) FLATBUFFERS_NOEXCEPT - { std::swap(type, u.type); std::swap(value, u.value); return *this; } - ~QuantizationDetailsUnion() { Reset(); } - - void Reset(); - -#ifndef FLATBUFFERS_CPP98_STL - template - void Set(T&& val) { - using RT = typename std::remove_reference::type; - Reset(); - type = QuantizationDetailsTraits::enum_value; - if (type != QuantizationDetails_NONE) { - value = new RT(std::forward(val)); - } - } -#endif // FLATBUFFERS_CPP98_STL - - static void *UnPack(const void *obj, QuantizationDetails type, const flatbuffers::resolver_function_t *resolver); - flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const flatbuffers::rehasher_function_t *_rehasher = nullptr) const; - - tflite::CustomQuantizationT *AsCustomQuantization() { - return type == QuantizationDetails_CustomQuantization ? - reinterpret_cast(value) : nullptr; - } - const tflite::CustomQuantizationT *AsCustomQuantization() const { - return type == QuantizationDetails_CustomQuantization ? - reinterpret_cast(value) : nullptr; - } -}; - -bool VerifyQuantizationDetails(flatbuffers::Verifier &verifier, const void *obj, QuantizationDetails type); -bool VerifyQuantizationDetailsVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector> *values, const flatbuffers::Vector *types); - -enum DimensionType { - DimensionType_DENSE = 0, - DimensionType_SPARSE_CSR = 1, - DimensionType_MIN = DimensionType_DENSE, - DimensionType_MAX = DimensionType_SPARSE_CSR -}; - -inline const DimensionType (&EnumValuesDimensionType())[2] { - static const DimensionType values[] = { - DimensionType_DENSE, - DimensionType_SPARSE_CSR - }; - return values; -} - -inline const char * const *EnumNamesDimensionType() { - static const char * const names[3] = { - "DENSE", - "SPARSE_CSR", - nullptr - }; - return names; -} - -inline const char *EnumNameDimensionType(DimensionType e) { - if (flatbuffers::IsOutRange(e, DimensionType_DENSE, DimensionType_SPARSE_CSR)) return ""; - const size_t index = static_cast(e); - return EnumNamesDimensionType()[index]; -} - -enum SparseIndexVector { - SparseIndexVector_NONE = 0, - SparseIndexVector_Int32Vector = 1, - SparseIndexVector_Uint16Vector = 2, - SparseIndexVector_Uint8Vector = 3, - SparseIndexVector_MIN = SparseIndexVector_NONE, - SparseIndexVector_MAX = SparseIndexVector_Uint8Vector -}; - -inline const SparseIndexVector (&EnumValuesSparseIndexVector())[4] { - static const SparseIndexVector values[] = { - SparseIndexVector_NONE, - SparseIndexVector_Int32Vector, - SparseIndexVector_Uint16Vector, - SparseIndexVector_Uint8Vector - }; - return values; -} - -inline const char * const *EnumNamesSparseIndexVector() { - static const char * const names[5] = { - "NONE", - "Int32Vector", - "Uint16Vector", - "Uint8Vector", - nullptr - }; - return names; -} - -inline const char *EnumNameSparseIndexVector(SparseIndexVector e) { - if (flatbuffers::IsOutRange(e, SparseIndexVector_NONE, SparseIndexVector_Uint8Vector)) return ""; - const size_t index = static_cast(e); - return EnumNamesSparseIndexVector()[index]; -} - -template struct SparseIndexVectorTraits { - static const SparseIndexVector enum_value = SparseIndexVector_NONE; -}; - -template<> struct SparseIndexVectorTraits { - static const SparseIndexVector enum_value = SparseIndexVector_Int32Vector; -}; - -template<> struct SparseIndexVectorTraits { - static const SparseIndexVector enum_value = SparseIndexVector_Uint16Vector; -}; - -template<> struct SparseIndexVectorTraits { - static const SparseIndexVector enum_value = SparseIndexVector_Uint8Vector; -}; - -struct SparseIndexVectorUnion { - SparseIndexVector type; - void *value; - - SparseIndexVectorUnion() : type(SparseIndexVector_NONE), value(nullptr) {} - SparseIndexVectorUnion(SparseIndexVectorUnion&& u) FLATBUFFERS_NOEXCEPT : - type(SparseIndexVector_NONE), value(nullptr) - { std::swap(type, u.type); std::swap(value, u.value); } - SparseIndexVectorUnion(const SparseIndexVectorUnion &); - SparseIndexVectorUnion &operator=(const SparseIndexVectorUnion &u) - { SparseIndexVectorUnion t(u); std::swap(type, t.type); std::swap(value, t.value); return *this; } - SparseIndexVectorUnion &operator=(SparseIndexVectorUnion &&u) FLATBUFFERS_NOEXCEPT - { std::swap(type, u.type); std::swap(value, u.value); return *this; } - ~SparseIndexVectorUnion() { Reset(); } - - void Reset(); - -#ifndef FLATBUFFERS_CPP98_STL - template - void Set(T&& val) { - using RT = typename std::remove_reference::type; - Reset(); - type = SparseIndexVectorTraits::enum_value; - if (type != SparseIndexVector_NONE) { - value = new RT(std::forward(val)); - } - } -#endif // FLATBUFFERS_CPP98_STL - - static void *UnPack(const void *obj, SparseIndexVector type, const flatbuffers::resolver_function_t *resolver); - flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const flatbuffers::rehasher_function_t *_rehasher = nullptr) const; - - tflite::Int32VectorT *AsInt32Vector() { - return type == SparseIndexVector_Int32Vector ? - reinterpret_cast(value) : nullptr; - } - const tflite::Int32VectorT *AsInt32Vector() const { - return type == SparseIndexVector_Int32Vector ? - reinterpret_cast(value) : nullptr; - } - tflite::Uint16VectorT *AsUint16Vector() { - return type == SparseIndexVector_Uint16Vector ? - reinterpret_cast(value) : nullptr; - } - const tflite::Uint16VectorT *AsUint16Vector() const { - return type == SparseIndexVector_Uint16Vector ? - reinterpret_cast(value) : nullptr; - } - tflite::Uint8VectorT *AsUint8Vector() { - return type == SparseIndexVector_Uint8Vector ? - reinterpret_cast(value) : nullptr; - } - const tflite::Uint8VectorT *AsUint8Vector() const { - return type == SparseIndexVector_Uint8Vector ? - reinterpret_cast(value) : nullptr; - } -}; - -bool VerifySparseIndexVector(flatbuffers::Verifier &verifier, const void *obj, SparseIndexVector type); -bool VerifySparseIndexVectorVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector> *values, const flatbuffers::Vector *types); - -enum BuiltinOperator { - BuiltinOperator_ADD = 0, - BuiltinOperator_AVERAGE_POOL_2D = 1, - BuiltinOperator_CONCATENATION = 2, - BuiltinOperator_CONV_2D = 3, - BuiltinOperator_DEPTHWISE_CONV_2D = 4, - BuiltinOperator_DEPTH_TO_SPACE = 5, - BuiltinOperator_DEQUANTIZE = 6, - BuiltinOperator_EMBEDDING_LOOKUP = 7, - BuiltinOperator_FLOOR = 8, - BuiltinOperator_FULLY_CONNECTED = 9, - BuiltinOperator_HASHTABLE_LOOKUP = 10, - BuiltinOperator_L2_NORMALIZATION = 11, - BuiltinOperator_L2_POOL_2D = 12, - BuiltinOperator_LOCAL_RESPONSE_NORMALIZATION = 13, - BuiltinOperator_LOGISTIC = 14, - BuiltinOperator_LSH_PROJECTION = 15, - BuiltinOperator_LSTM = 16, - BuiltinOperator_MAX_POOL_2D = 17, - BuiltinOperator_MUL = 18, - BuiltinOperator_RELU = 19, - BuiltinOperator_RELU_N1_TO_1 = 20, - BuiltinOperator_RELU6 = 21, - BuiltinOperator_RESHAPE = 22, - BuiltinOperator_RESIZE_BILINEAR = 23, - BuiltinOperator_RNN = 24, - BuiltinOperator_SOFTMAX = 25, - BuiltinOperator_SPACE_TO_DEPTH = 26, - BuiltinOperator_SVDF = 27, - BuiltinOperator_TANH = 28, - BuiltinOperator_CONCAT_EMBEDDINGS = 29, - BuiltinOperator_SKIP_GRAM = 30, - BuiltinOperator_CALL = 31, - BuiltinOperator_CUSTOM = 32, - BuiltinOperator_EMBEDDING_LOOKUP_SPARSE = 33, - BuiltinOperator_PAD = 34, - BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_RNN = 35, - BuiltinOperator_GATHER = 36, - BuiltinOperator_BATCH_TO_SPACE_ND = 37, - BuiltinOperator_SPACE_TO_BATCH_ND = 38, - BuiltinOperator_TRANSPOSE = 39, - BuiltinOperator_MEAN = 40, - BuiltinOperator_SUB = 41, - BuiltinOperator_DIV = 42, - BuiltinOperator_SQUEEZE = 43, - BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM = 44, - BuiltinOperator_STRIDED_SLICE = 45, - BuiltinOperator_BIDIRECTIONAL_SEQUENCE_RNN = 46, - BuiltinOperator_EXP = 47, - BuiltinOperator_TOPK_V2 = 48, - BuiltinOperator_SPLIT = 49, - BuiltinOperator_LOG_SOFTMAX = 50, - BuiltinOperator_DELEGATE = 51, - BuiltinOperator_BIDIRECTIONAL_SEQUENCE_LSTM = 52, - BuiltinOperator_CAST = 53, - BuiltinOperator_PRELU = 54, - BuiltinOperator_MAXIMUM = 55, - BuiltinOperator_ARG_MAX = 56, - BuiltinOperator_MINIMUM = 57, - BuiltinOperator_LESS = 58, - BuiltinOperator_NEG = 59, - BuiltinOperator_PADV2 = 60, - BuiltinOperator_GREATER = 61, - BuiltinOperator_GREATER_EQUAL = 62, - BuiltinOperator_LESS_EQUAL = 63, - BuiltinOperator_SELECT = 64, - BuiltinOperator_SLICE = 65, - BuiltinOperator_SIN = 66, - BuiltinOperator_TRANSPOSE_CONV = 67, - BuiltinOperator_SPARSE_TO_DENSE = 68, - BuiltinOperator_TILE = 69, - BuiltinOperator_EXPAND_DIMS = 70, - BuiltinOperator_EQUAL = 71, - BuiltinOperator_NOT_EQUAL = 72, - BuiltinOperator_LOG = 73, - BuiltinOperator_SUM = 74, - BuiltinOperator_SQRT = 75, - BuiltinOperator_RSQRT = 76, - BuiltinOperator_SHAPE = 77, - BuiltinOperator_POW = 78, - BuiltinOperator_ARG_MIN = 79, - BuiltinOperator_FAKE_QUANT = 80, - BuiltinOperator_REDUCE_PROD = 81, - BuiltinOperator_REDUCE_MAX = 82, - BuiltinOperator_PACK = 83, - BuiltinOperator_LOGICAL_OR = 84, - BuiltinOperator_ONE_HOT = 85, - BuiltinOperator_LOGICAL_AND = 86, - BuiltinOperator_LOGICAL_NOT = 87, - BuiltinOperator_UNPACK = 88, - BuiltinOperator_REDUCE_MIN = 89, - BuiltinOperator_FLOOR_DIV = 90, - BuiltinOperator_REDUCE_ANY = 91, - BuiltinOperator_SQUARE = 92, - BuiltinOperator_ZEROS_LIKE = 93, - BuiltinOperator_FILL = 94, - BuiltinOperator_FLOOR_MOD = 95, - BuiltinOperator_RANGE = 96, - BuiltinOperator_RESIZE_NEAREST_NEIGHBOR = 97, - BuiltinOperator_LEAKY_RELU = 98, - BuiltinOperator_SQUARED_DIFFERENCE = 99, - BuiltinOperator_MIRROR_PAD = 100, - BuiltinOperator_ABS = 101, - BuiltinOperator_SPLIT_V = 102, - BuiltinOperator_UNIQUE = 103, - BuiltinOperator_CEIL = 104, - BuiltinOperator_REVERSE_V2 = 105, - BuiltinOperator_ADD_N = 106, - BuiltinOperator_GATHER_ND = 107, - BuiltinOperator_COS = 108, - BuiltinOperator_WHERE = 109, - BuiltinOperator_RANK = 110, - BuiltinOperator_ELU = 111, - BuiltinOperator_REVERSE_SEQUENCE = 112, - BuiltinOperator_MATRIX_DIAG = 113, - BuiltinOperator_QUANTIZE = 114, - BuiltinOperator_MATRIX_SET_DIAG = 115, - BuiltinOperator_ROUND = 116, - BuiltinOperator_HARD_SWISH = 117, - BuiltinOperator_IF = 118, - BuiltinOperator_WHILE = 119, - BuiltinOperator_NON_MAX_SUPPRESSION_V4 = 120, - BuiltinOperator_NON_MAX_SUPPRESSION_V5 = 121, - BuiltinOperator_SCATTER_ND = 122, - BuiltinOperator_SELECT_V2 = 123, - BuiltinOperator_DENSIFY = 124, - BuiltinOperator_SEGMENT_SUM = 125, - BuiltinOperator_BATCH_MATMUL = 126, - BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES = 127, - BuiltinOperator_CUMSUM = 128, - BuiltinOperator_CALL_ONCE = 129, - BuiltinOperator_BROADCAST_TO = 130, - BuiltinOperator_RFFT2D = 131, - BuiltinOperator_CONV_3D = 132, - BuiltinOperator_IMAG = 133, - BuiltinOperator_REAL = 134, - BuiltinOperator_COMPLEX_ABS = 135, - BuiltinOperator_HASHTABLE = 136, - BuiltinOperator_HASHTABLE_FIND = 137, - BuiltinOperator_HASHTABLE_IMPORT = 138, - BuiltinOperator_HASHTABLE_SIZE = 139, - BuiltinOperator_REDUCE_ALL = 140, - BuiltinOperator_CONV_3D_TRANSPOSE = 141, - BuiltinOperator_VAR_HANDLE = 142, - BuiltinOperator_READ_VARIABLE = 143, - BuiltinOperator_ASSIGN_VARIABLE = 144, - BuiltinOperator_BROADCAST_ARGS = 145, - BuiltinOperator_RANDOM_STANDARD_NORMAL = 146, - BuiltinOperator_BUCKETIZE = 147, - BuiltinOperator_RANDOM_UNIFORM = 148, - BuiltinOperator_MULTINOMIAL = 149, - BuiltinOperator_GELU = 150, - BuiltinOperator_DYNAMIC_UPDATE_SLICE = 151, - BuiltinOperator_RELU_0_TO_1 = 152, - BuiltinOperator_UNSORTED_SEGMENT_PROD = 153, - BuiltinOperator_UNSORTED_SEGMENT_MAX = 154, - BuiltinOperator_UNSORTED_SEGMENT_SUM = 155, - BuiltinOperator_ATAN2 = 156, - BuiltinOperator_UNSORTED_SEGMENT_MIN = 157, - BuiltinOperator_MIN = BuiltinOperator_ADD, - BuiltinOperator_MAX = BuiltinOperator_UNSORTED_SEGMENT_MIN -}; - -inline const BuiltinOperator (&EnumValuesBuiltinOperator())[158] { - static const BuiltinOperator values[] = { - BuiltinOperator_ADD, - BuiltinOperator_AVERAGE_POOL_2D, - BuiltinOperator_CONCATENATION, - BuiltinOperator_CONV_2D, - BuiltinOperator_DEPTHWISE_CONV_2D, - BuiltinOperator_DEPTH_TO_SPACE, - BuiltinOperator_DEQUANTIZE, - BuiltinOperator_EMBEDDING_LOOKUP, - BuiltinOperator_FLOOR, - BuiltinOperator_FULLY_CONNECTED, - BuiltinOperator_HASHTABLE_LOOKUP, - BuiltinOperator_L2_NORMALIZATION, - BuiltinOperator_L2_POOL_2D, - BuiltinOperator_LOCAL_RESPONSE_NORMALIZATION, - BuiltinOperator_LOGISTIC, - BuiltinOperator_LSH_PROJECTION, - BuiltinOperator_LSTM, - BuiltinOperator_MAX_POOL_2D, - BuiltinOperator_MUL, - BuiltinOperator_RELU, - BuiltinOperator_RELU_N1_TO_1, - BuiltinOperator_RELU6, - BuiltinOperator_RESHAPE, - BuiltinOperator_RESIZE_BILINEAR, - BuiltinOperator_RNN, - BuiltinOperator_SOFTMAX, - BuiltinOperator_SPACE_TO_DEPTH, - BuiltinOperator_SVDF, - BuiltinOperator_TANH, - BuiltinOperator_CONCAT_EMBEDDINGS, - BuiltinOperator_SKIP_GRAM, - BuiltinOperator_CALL, - BuiltinOperator_CUSTOM, - BuiltinOperator_EMBEDDING_LOOKUP_SPARSE, - BuiltinOperator_PAD, - BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_RNN, - BuiltinOperator_GATHER, - BuiltinOperator_BATCH_TO_SPACE_ND, - BuiltinOperator_SPACE_TO_BATCH_ND, - BuiltinOperator_TRANSPOSE, - BuiltinOperator_MEAN, - BuiltinOperator_SUB, - BuiltinOperator_DIV, - BuiltinOperator_SQUEEZE, - BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM, - BuiltinOperator_STRIDED_SLICE, - BuiltinOperator_BIDIRECTIONAL_SEQUENCE_RNN, - BuiltinOperator_EXP, - BuiltinOperator_TOPK_V2, - BuiltinOperator_SPLIT, - BuiltinOperator_LOG_SOFTMAX, - BuiltinOperator_DELEGATE, - BuiltinOperator_BIDIRECTIONAL_SEQUENCE_LSTM, - BuiltinOperator_CAST, - BuiltinOperator_PRELU, - BuiltinOperator_MAXIMUM, - BuiltinOperator_ARG_MAX, - BuiltinOperator_MINIMUM, - BuiltinOperator_LESS, - BuiltinOperator_NEG, - BuiltinOperator_PADV2, - BuiltinOperator_GREATER, - BuiltinOperator_GREATER_EQUAL, - BuiltinOperator_LESS_EQUAL, - BuiltinOperator_SELECT, - BuiltinOperator_SLICE, - BuiltinOperator_SIN, - BuiltinOperator_TRANSPOSE_CONV, - BuiltinOperator_SPARSE_TO_DENSE, - BuiltinOperator_TILE, - BuiltinOperator_EXPAND_DIMS, - BuiltinOperator_EQUAL, - BuiltinOperator_NOT_EQUAL, - BuiltinOperator_LOG, - BuiltinOperator_SUM, - BuiltinOperator_SQRT, - BuiltinOperator_RSQRT, - BuiltinOperator_SHAPE, - BuiltinOperator_POW, - BuiltinOperator_ARG_MIN, - BuiltinOperator_FAKE_QUANT, - BuiltinOperator_REDUCE_PROD, - BuiltinOperator_REDUCE_MAX, - BuiltinOperator_PACK, - BuiltinOperator_LOGICAL_OR, - BuiltinOperator_ONE_HOT, - BuiltinOperator_LOGICAL_AND, - BuiltinOperator_LOGICAL_NOT, - BuiltinOperator_UNPACK, - BuiltinOperator_REDUCE_MIN, - BuiltinOperator_FLOOR_DIV, - BuiltinOperator_REDUCE_ANY, - BuiltinOperator_SQUARE, - BuiltinOperator_ZEROS_LIKE, - BuiltinOperator_FILL, - BuiltinOperator_FLOOR_MOD, - BuiltinOperator_RANGE, - BuiltinOperator_RESIZE_NEAREST_NEIGHBOR, - BuiltinOperator_LEAKY_RELU, - BuiltinOperator_SQUARED_DIFFERENCE, - BuiltinOperator_MIRROR_PAD, - BuiltinOperator_ABS, - BuiltinOperator_SPLIT_V, - BuiltinOperator_UNIQUE, - BuiltinOperator_CEIL, - BuiltinOperator_REVERSE_V2, - BuiltinOperator_ADD_N, - BuiltinOperator_GATHER_ND, - BuiltinOperator_COS, - BuiltinOperator_WHERE, - BuiltinOperator_RANK, - BuiltinOperator_ELU, - BuiltinOperator_REVERSE_SEQUENCE, - BuiltinOperator_MATRIX_DIAG, - BuiltinOperator_QUANTIZE, - BuiltinOperator_MATRIX_SET_DIAG, - BuiltinOperator_ROUND, - BuiltinOperator_HARD_SWISH, - BuiltinOperator_IF, - BuiltinOperator_WHILE, - BuiltinOperator_NON_MAX_SUPPRESSION_V4, - BuiltinOperator_NON_MAX_SUPPRESSION_V5, - BuiltinOperator_SCATTER_ND, - BuiltinOperator_SELECT_V2, - BuiltinOperator_DENSIFY, - BuiltinOperator_SEGMENT_SUM, - BuiltinOperator_BATCH_MATMUL, - BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES, - BuiltinOperator_CUMSUM, - BuiltinOperator_CALL_ONCE, - BuiltinOperator_BROADCAST_TO, - BuiltinOperator_RFFT2D, - BuiltinOperator_CONV_3D, - BuiltinOperator_IMAG, - BuiltinOperator_REAL, - BuiltinOperator_COMPLEX_ABS, - BuiltinOperator_HASHTABLE, - BuiltinOperator_HASHTABLE_FIND, - BuiltinOperator_HASHTABLE_IMPORT, - BuiltinOperator_HASHTABLE_SIZE, - BuiltinOperator_REDUCE_ALL, - BuiltinOperator_CONV_3D_TRANSPOSE, - BuiltinOperator_VAR_HANDLE, - BuiltinOperator_READ_VARIABLE, - BuiltinOperator_ASSIGN_VARIABLE, - BuiltinOperator_BROADCAST_ARGS, - BuiltinOperator_RANDOM_STANDARD_NORMAL, - BuiltinOperator_BUCKETIZE, - BuiltinOperator_RANDOM_UNIFORM, - BuiltinOperator_MULTINOMIAL, - BuiltinOperator_GELU, - BuiltinOperator_DYNAMIC_UPDATE_SLICE, - BuiltinOperator_RELU_0_TO_1, - BuiltinOperator_UNSORTED_SEGMENT_PROD, - BuiltinOperator_UNSORTED_SEGMENT_MAX, - BuiltinOperator_UNSORTED_SEGMENT_SUM, - BuiltinOperator_ATAN2, - BuiltinOperator_UNSORTED_SEGMENT_MIN - }; - return values; -} - -inline const char * const *EnumNamesBuiltinOperator() { - static const char * const names[159] = { - "ADD", - "AVERAGE_POOL_2D", - "CONCATENATION", - "CONV_2D", - "DEPTHWISE_CONV_2D", - "DEPTH_TO_SPACE", - "DEQUANTIZE", - "EMBEDDING_LOOKUP", - "FLOOR", - "FULLY_CONNECTED", - "HASHTABLE_LOOKUP", - "L2_NORMALIZATION", - "L2_POOL_2D", - "LOCAL_RESPONSE_NORMALIZATION", - "LOGISTIC", - "LSH_PROJECTION", - "LSTM", - "MAX_POOL_2D", - "MUL", - "RELU", - "RELU_N1_TO_1", - "RELU6", - "RESHAPE", - "RESIZE_BILINEAR", - "RNN", - "SOFTMAX", - "SPACE_TO_DEPTH", - "SVDF", - "TANH", - "CONCAT_EMBEDDINGS", - "SKIP_GRAM", - "CALL", - "CUSTOM", - "EMBEDDING_LOOKUP_SPARSE", - "PAD", - "UNIDIRECTIONAL_SEQUENCE_RNN", - "GATHER", - "BATCH_TO_SPACE_ND", - "SPACE_TO_BATCH_ND", - "TRANSPOSE", - "MEAN", - "SUB", - "DIV", - "SQUEEZE", - "UNIDIRECTIONAL_SEQUENCE_LSTM", - "STRIDED_SLICE", - "BIDIRECTIONAL_SEQUENCE_RNN", - "EXP", - "TOPK_V2", - "SPLIT", - "LOG_SOFTMAX", - "DELEGATE", - "BIDIRECTIONAL_SEQUENCE_LSTM", - "CAST", - "PRELU", - "MAXIMUM", - "ARG_MAX", - "MINIMUM", - "LESS", - "NEG", - "PADV2", - "GREATER", - "GREATER_EQUAL", - "LESS_EQUAL", - "SELECT", - "SLICE", - "SIN", - "TRANSPOSE_CONV", - "SPARSE_TO_DENSE", - "TILE", - "EXPAND_DIMS", - "EQUAL", - "NOT_EQUAL", - "LOG", - "SUM", - "SQRT", - "RSQRT", - "SHAPE", - "POW", - "ARG_MIN", - "FAKE_QUANT", - "REDUCE_PROD", - "REDUCE_MAX", - "PACK", - "LOGICAL_OR", - "ONE_HOT", - "LOGICAL_AND", - "LOGICAL_NOT", - "UNPACK", - "REDUCE_MIN", - "FLOOR_DIV", - "REDUCE_ANY", - "SQUARE", - "ZEROS_LIKE", - "FILL", - "FLOOR_MOD", - "RANGE", - "RESIZE_NEAREST_NEIGHBOR", - "LEAKY_RELU", - "SQUARED_DIFFERENCE", - "MIRROR_PAD", - "ABS", - "SPLIT_V", - "UNIQUE", - "CEIL", - "REVERSE_V2", - "ADD_N", - "GATHER_ND", - "COS", - "WHERE", - "RANK", - "ELU", - "REVERSE_SEQUENCE", - "MATRIX_DIAG", - "QUANTIZE", - "MATRIX_SET_DIAG", - "ROUND", - "HARD_SWISH", - "IF", - "WHILE", - "NON_MAX_SUPPRESSION_V4", - "NON_MAX_SUPPRESSION_V5", - "SCATTER_ND", - "SELECT_V2", - "DENSIFY", - "SEGMENT_SUM", - "BATCH_MATMUL", - "PLACEHOLDER_FOR_GREATER_OP_CODES", - "CUMSUM", - "CALL_ONCE", - "BROADCAST_TO", - "RFFT2D", - "CONV_3D", - "IMAG", - "REAL", - "COMPLEX_ABS", - "HASHTABLE", - "HASHTABLE_FIND", - "HASHTABLE_IMPORT", - "HASHTABLE_SIZE", - "REDUCE_ALL", - "CONV_3D_TRANSPOSE", - "VAR_HANDLE", - "READ_VARIABLE", - "ASSIGN_VARIABLE", - "BROADCAST_ARGS", - "RANDOM_STANDARD_NORMAL", - "BUCKETIZE", - "RANDOM_UNIFORM", - "MULTINOMIAL", - "GELU", - "DYNAMIC_UPDATE_SLICE", - "RELU_0_TO_1", - "UNSORTED_SEGMENT_PROD", - "UNSORTED_SEGMENT_MAX", - "UNSORTED_SEGMENT_SUM", - "ATAN2", - "UNSORTED_SEGMENT_MIN", - nullptr - }; - return names; -} - -inline const char *EnumNameBuiltinOperator(BuiltinOperator e) { - if (flatbuffers::IsOutRange(e, BuiltinOperator_ADD, BuiltinOperator_UNSORTED_SEGMENT_MIN)) return ""; - const size_t index = static_cast(e); - return EnumNamesBuiltinOperator()[index]; -} - -enum BuiltinOptions { - BuiltinOptions_NONE = 0, - BuiltinOptions_Conv2DOptions = 1, - BuiltinOptions_DepthwiseConv2DOptions = 2, - BuiltinOptions_ConcatEmbeddingsOptions = 3, - BuiltinOptions_LSHProjectionOptions = 4, - BuiltinOptions_Pool2DOptions = 5, - BuiltinOptions_SVDFOptions = 6, - BuiltinOptions_RNNOptions = 7, - BuiltinOptions_FullyConnectedOptions = 8, - BuiltinOptions_SoftmaxOptions = 9, - BuiltinOptions_ConcatenationOptions = 10, - BuiltinOptions_AddOptions = 11, - BuiltinOptions_L2NormOptions = 12, - BuiltinOptions_LocalResponseNormalizationOptions = 13, - BuiltinOptions_LSTMOptions = 14, - BuiltinOptions_ResizeBilinearOptions = 15, - BuiltinOptions_CallOptions = 16, - BuiltinOptions_ReshapeOptions = 17, - BuiltinOptions_SkipGramOptions = 18, - BuiltinOptions_SpaceToDepthOptions = 19, - BuiltinOptions_EmbeddingLookupSparseOptions = 20, - BuiltinOptions_MulOptions = 21, - BuiltinOptions_PadOptions = 22, - BuiltinOptions_GatherOptions = 23, - BuiltinOptions_BatchToSpaceNDOptions = 24, - BuiltinOptions_SpaceToBatchNDOptions = 25, - BuiltinOptions_TransposeOptions = 26, - BuiltinOptions_ReducerOptions = 27, - BuiltinOptions_SubOptions = 28, - BuiltinOptions_DivOptions = 29, - BuiltinOptions_SqueezeOptions = 30, - BuiltinOptions_SequenceRNNOptions = 31, - BuiltinOptions_StridedSliceOptions = 32, - BuiltinOptions_ExpOptions = 33, - BuiltinOptions_TopKV2Options = 34, - BuiltinOptions_SplitOptions = 35, - BuiltinOptions_LogSoftmaxOptions = 36, - BuiltinOptions_CastOptions = 37, - BuiltinOptions_DequantizeOptions = 38, - BuiltinOptions_MaximumMinimumOptions = 39, - BuiltinOptions_ArgMaxOptions = 40, - BuiltinOptions_LessOptions = 41, - BuiltinOptions_NegOptions = 42, - BuiltinOptions_PadV2Options = 43, - BuiltinOptions_GreaterOptions = 44, - BuiltinOptions_GreaterEqualOptions = 45, - BuiltinOptions_LessEqualOptions = 46, - BuiltinOptions_SelectOptions = 47, - BuiltinOptions_SliceOptions = 48, - BuiltinOptions_TransposeConvOptions = 49, - BuiltinOptions_SparseToDenseOptions = 50, - BuiltinOptions_TileOptions = 51, - BuiltinOptions_ExpandDimsOptions = 52, - BuiltinOptions_EqualOptions = 53, - BuiltinOptions_NotEqualOptions = 54, - BuiltinOptions_ShapeOptions = 55, - BuiltinOptions_PowOptions = 56, - BuiltinOptions_ArgMinOptions = 57, - BuiltinOptions_FakeQuantOptions = 58, - BuiltinOptions_PackOptions = 59, - BuiltinOptions_LogicalOrOptions = 60, - BuiltinOptions_OneHotOptions = 61, - BuiltinOptions_LogicalAndOptions = 62, - BuiltinOptions_LogicalNotOptions = 63, - BuiltinOptions_UnpackOptions = 64, - BuiltinOptions_FloorDivOptions = 65, - BuiltinOptions_SquareOptions = 66, - BuiltinOptions_ZerosLikeOptions = 67, - BuiltinOptions_FillOptions = 68, - BuiltinOptions_BidirectionalSequenceLSTMOptions = 69, - BuiltinOptions_BidirectionalSequenceRNNOptions = 70, - BuiltinOptions_UnidirectionalSequenceLSTMOptions = 71, - BuiltinOptions_FloorModOptions = 72, - BuiltinOptions_RangeOptions = 73, - BuiltinOptions_ResizeNearestNeighborOptions = 74, - BuiltinOptions_LeakyReluOptions = 75, - BuiltinOptions_SquaredDifferenceOptions = 76, - BuiltinOptions_MirrorPadOptions = 77, - BuiltinOptions_AbsOptions = 78, - BuiltinOptions_SplitVOptions = 79, - BuiltinOptions_UniqueOptions = 80, - BuiltinOptions_ReverseV2Options = 81, - BuiltinOptions_AddNOptions = 82, - BuiltinOptions_GatherNdOptions = 83, - BuiltinOptions_CosOptions = 84, - BuiltinOptions_WhereOptions = 85, - BuiltinOptions_RankOptions = 86, - BuiltinOptions_ReverseSequenceOptions = 87, - BuiltinOptions_MatrixDiagOptions = 88, - BuiltinOptions_QuantizeOptions = 89, - BuiltinOptions_MatrixSetDiagOptions = 90, - BuiltinOptions_HardSwishOptions = 91, - BuiltinOptions_IfOptions = 92, - BuiltinOptions_WhileOptions = 93, - BuiltinOptions_DepthToSpaceOptions = 94, - BuiltinOptions_NonMaxSuppressionV4Options = 95, - BuiltinOptions_NonMaxSuppressionV5Options = 96, - BuiltinOptions_ScatterNdOptions = 97, - BuiltinOptions_SelectV2Options = 98, - BuiltinOptions_DensifyOptions = 99, - BuiltinOptions_SegmentSumOptions = 100, - BuiltinOptions_BatchMatMulOptions = 101, - BuiltinOptions_CumsumOptions = 102, - BuiltinOptions_CallOnceOptions = 103, - BuiltinOptions_BroadcastToOptions = 104, - BuiltinOptions_Rfft2dOptions = 105, - BuiltinOptions_Conv3DOptions = 106, - BuiltinOptions_HashtableOptions = 107, - BuiltinOptions_HashtableFindOptions = 108, - BuiltinOptions_HashtableImportOptions = 109, - BuiltinOptions_HashtableSizeOptions = 110, - BuiltinOptions_VarHandleOptions = 111, - BuiltinOptions_ReadVariableOptions = 112, - BuiltinOptions_AssignVariableOptions = 113, - BuiltinOptions_RandomOptions = 114, - BuiltinOptions_BucketizeOptions = 115, - BuiltinOptions_GeluOptions = 116, - BuiltinOptions_DynamicUpdateSliceOptions = 117, - BuiltinOptions_UnsortedSegmentProdOptions = 118, - BuiltinOptions_UnsortedSegmentMaxOptions = 119, - BuiltinOptions_UnsortedSegmentMinOptions = 120, - BuiltinOptions_UnsortedSegmentSumOptions = 121, - BuiltinOptions_ATan2Options = 122, - BuiltinOptions_MIN = BuiltinOptions_NONE, - BuiltinOptions_MAX = BuiltinOptions_ATan2Options -}; - -inline const BuiltinOptions (&EnumValuesBuiltinOptions())[123] { - static const BuiltinOptions values[] = { - BuiltinOptions_NONE, - BuiltinOptions_Conv2DOptions, - BuiltinOptions_DepthwiseConv2DOptions, - BuiltinOptions_ConcatEmbeddingsOptions, - BuiltinOptions_LSHProjectionOptions, - BuiltinOptions_Pool2DOptions, - BuiltinOptions_SVDFOptions, - BuiltinOptions_RNNOptions, - BuiltinOptions_FullyConnectedOptions, - BuiltinOptions_SoftmaxOptions, - BuiltinOptions_ConcatenationOptions, - BuiltinOptions_AddOptions, - BuiltinOptions_L2NormOptions, - BuiltinOptions_LocalResponseNormalizationOptions, - BuiltinOptions_LSTMOptions, - BuiltinOptions_ResizeBilinearOptions, - BuiltinOptions_CallOptions, - BuiltinOptions_ReshapeOptions, - BuiltinOptions_SkipGramOptions, - BuiltinOptions_SpaceToDepthOptions, - BuiltinOptions_EmbeddingLookupSparseOptions, - BuiltinOptions_MulOptions, - BuiltinOptions_PadOptions, - BuiltinOptions_GatherOptions, - BuiltinOptions_BatchToSpaceNDOptions, - BuiltinOptions_SpaceToBatchNDOptions, - BuiltinOptions_TransposeOptions, - BuiltinOptions_ReducerOptions, - BuiltinOptions_SubOptions, - BuiltinOptions_DivOptions, - BuiltinOptions_SqueezeOptions, - BuiltinOptions_SequenceRNNOptions, - BuiltinOptions_StridedSliceOptions, - BuiltinOptions_ExpOptions, - BuiltinOptions_TopKV2Options, - BuiltinOptions_SplitOptions, - BuiltinOptions_LogSoftmaxOptions, - BuiltinOptions_CastOptions, - BuiltinOptions_DequantizeOptions, - BuiltinOptions_MaximumMinimumOptions, - BuiltinOptions_ArgMaxOptions, - BuiltinOptions_LessOptions, - BuiltinOptions_NegOptions, - BuiltinOptions_PadV2Options, - BuiltinOptions_GreaterOptions, - BuiltinOptions_GreaterEqualOptions, - BuiltinOptions_LessEqualOptions, - BuiltinOptions_SelectOptions, - BuiltinOptions_SliceOptions, - BuiltinOptions_TransposeConvOptions, - BuiltinOptions_SparseToDenseOptions, - BuiltinOptions_TileOptions, - BuiltinOptions_ExpandDimsOptions, - BuiltinOptions_EqualOptions, - BuiltinOptions_NotEqualOptions, - BuiltinOptions_ShapeOptions, - BuiltinOptions_PowOptions, - BuiltinOptions_ArgMinOptions, - BuiltinOptions_FakeQuantOptions, - BuiltinOptions_PackOptions, - BuiltinOptions_LogicalOrOptions, - BuiltinOptions_OneHotOptions, - BuiltinOptions_LogicalAndOptions, - BuiltinOptions_LogicalNotOptions, - BuiltinOptions_UnpackOptions, - BuiltinOptions_FloorDivOptions, - BuiltinOptions_SquareOptions, - BuiltinOptions_ZerosLikeOptions, - BuiltinOptions_FillOptions, - BuiltinOptions_BidirectionalSequenceLSTMOptions, - BuiltinOptions_BidirectionalSequenceRNNOptions, - BuiltinOptions_UnidirectionalSequenceLSTMOptions, - BuiltinOptions_FloorModOptions, - BuiltinOptions_RangeOptions, - BuiltinOptions_ResizeNearestNeighborOptions, - BuiltinOptions_LeakyReluOptions, - BuiltinOptions_SquaredDifferenceOptions, - BuiltinOptions_MirrorPadOptions, - BuiltinOptions_AbsOptions, - BuiltinOptions_SplitVOptions, - BuiltinOptions_UniqueOptions, - BuiltinOptions_ReverseV2Options, - BuiltinOptions_AddNOptions, - BuiltinOptions_GatherNdOptions, - BuiltinOptions_CosOptions, - BuiltinOptions_WhereOptions, - BuiltinOptions_RankOptions, - BuiltinOptions_ReverseSequenceOptions, - BuiltinOptions_MatrixDiagOptions, - BuiltinOptions_QuantizeOptions, - BuiltinOptions_MatrixSetDiagOptions, - BuiltinOptions_HardSwishOptions, - BuiltinOptions_IfOptions, - BuiltinOptions_WhileOptions, - BuiltinOptions_DepthToSpaceOptions, - BuiltinOptions_NonMaxSuppressionV4Options, - BuiltinOptions_NonMaxSuppressionV5Options, - BuiltinOptions_ScatterNdOptions, - BuiltinOptions_SelectV2Options, - BuiltinOptions_DensifyOptions, - BuiltinOptions_SegmentSumOptions, - BuiltinOptions_BatchMatMulOptions, - BuiltinOptions_CumsumOptions, - BuiltinOptions_CallOnceOptions, - BuiltinOptions_BroadcastToOptions, - BuiltinOptions_Rfft2dOptions, - BuiltinOptions_Conv3DOptions, - BuiltinOptions_HashtableOptions, - BuiltinOptions_HashtableFindOptions, - BuiltinOptions_HashtableImportOptions, - BuiltinOptions_HashtableSizeOptions, - BuiltinOptions_VarHandleOptions, - BuiltinOptions_ReadVariableOptions, - BuiltinOptions_AssignVariableOptions, - BuiltinOptions_RandomOptions, - BuiltinOptions_BucketizeOptions, - BuiltinOptions_GeluOptions, - BuiltinOptions_DynamicUpdateSliceOptions, - BuiltinOptions_UnsortedSegmentProdOptions, - BuiltinOptions_UnsortedSegmentMaxOptions, - BuiltinOptions_UnsortedSegmentMinOptions, - BuiltinOptions_UnsortedSegmentSumOptions, - BuiltinOptions_ATan2Options - }; - return values; -} - -inline const char * const *EnumNamesBuiltinOptions() { - static const char * const names[124] = { - "NONE", - "Conv2DOptions", - "DepthwiseConv2DOptions", - "ConcatEmbeddingsOptions", - "LSHProjectionOptions", - "Pool2DOptions", - "SVDFOptions", - "RNNOptions", - "FullyConnectedOptions", - "SoftmaxOptions", - "ConcatenationOptions", - "AddOptions", - "L2NormOptions", - "LocalResponseNormalizationOptions", - "LSTMOptions", - "ResizeBilinearOptions", - "CallOptions", - "ReshapeOptions", - "SkipGramOptions", - "SpaceToDepthOptions", - "EmbeddingLookupSparseOptions", - "MulOptions", - "PadOptions", - "GatherOptions", - "BatchToSpaceNDOptions", - "SpaceToBatchNDOptions", - "TransposeOptions", - "ReducerOptions", - "SubOptions", - "DivOptions", - "SqueezeOptions", - "SequenceRNNOptions", - "StridedSliceOptions", - "ExpOptions", - "TopKV2Options", - "SplitOptions", - "LogSoftmaxOptions", - "CastOptions", - "DequantizeOptions", - "MaximumMinimumOptions", - "ArgMaxOptions", - "LessOptions", - "NegOptions", - "PadV2Options", - "GreaterOptions", - "GreaterEqualOptions", - "LessEqualOptions", - "SelectOptions", - "SliceOptions", - "TransposeConvOptions", - "SparseToDenseOptions", - "TileOptions", - "ExpandDimsOptions", - "EqualOptions", - "NotEqualOptions", - "ShapeOptions", - "PowOptions", - "ArgMinOptions", - "FakeQuantOptions", - "PackOptions", - "LogicalOrOptions", - "OneHotOptions", - "LogicalAndOptions", - "LogicalNotOptions", - "UnpackOptions", - "FloorDivOptions", - "SquareOptions", - "ZerosLikeOptions", - "FillOptions", - "BidirectionalSequenceLSTMOptions", - "BidirectionalSequenceRNNOptions", - "UnidirectionalSequenceLSTMOptions", - "FloorModOptions", - "RangeOptions", - "ResizeNearestNeighborOptions", - "LeakyReluOptions", - "SquaredDifferenceOptions", - "MirrorPadOptions", - "AbsOptions", - "SplitVOptions", - "UniqueOptions", - "ReverseV2Options", - "AddNOptions", - "GatherNdOptions", - "CosOptions", - "WhereOptions", - "RankOptions", - "ReverseSequenceOptions", - "MatrixDiagOptions", - "QuantizeOptions", - "MatrixSetDiagOptions", - "HardSwishOptions", - "IfOptions", - "WhileOptions", - "DepthToSpaceOptions", - "NonMaxSuppressionV4Options", - "NonMaxSuppressionV5Options", - "ScatterNdOptions", - "SelectV2Options", - "DensifyOptions", - "SegmentSumOptions", - "BatchMatMulOptions", - "CumsumOptions", - "CallOnceOptions", - "BroadcastToOptions", - "Rfft2dOptions", - "Conv3DOptions", - "HashtableOptions", - "HashtableFindOptions", - "HashtableImportOptions", - "HashtableSizeOptions", - "VarHandleOptions", - "ReadVariableOptions", - "AssignVariableOptions", - "RandomOptions", - "BucketizeOptions", - "GeluOptions", - "DynamicUpdateSliceOptions", - "UnsortedSegmentProdOptions", - "UnsortedSegmentMaxOptions", - "UnsortedSegmentMinOptions", - "UnsortedSegmentSumOptions", - "ATan2Options", - nullptr - }; - return names; -} - -inline const char *EnumNameBuiltinOptions(BuiltinOptions e) { - if (flatbuffers::IsOutRange(e, BuiltinOptions_NONE, BuiltinOptions_ATan2Options)) return ""; - const size_t index = static_cast(e); - return EnumNamesBuiltinOptions()[index]; -} - -template struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_NONE; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_Conv2DOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_DepthwiseConv2DOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_ConcatEmbeddingsOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_LSHProjectionOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_Pool2DOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_SVDFOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_RNNOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_FullyConnectedOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_SoftmaxOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_ConcatenationOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_AddOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_L2NormOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_LocalResponseNormalizationOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_LSTMOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_ResizeBilinearOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_CallOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_ReshapeOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_SkipGramOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_SpaceToDepthOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_EmbeddingLookupSparseOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_MulOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_PadOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_GatherOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_BatchToSpaceNDOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_SpaceToBatchNDOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_TransposeOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_ReducerOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_SubOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_DivOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_SqueezeOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_SequenceRNNOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_StridedSliceOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_ExpOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_TopKV2Options; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_SplitOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_LogSoftmaxOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_CastOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_DequantizeOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_MaximumMinimumOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_ArgMaxOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_LessOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_NegOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_PadV2Options; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_GreaterOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_GreaterEqualOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_LessEqualOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_SelectOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_SliceOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_TransposeConvOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_SparseToDenseOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_TileOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_ExpandDimsOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_EqualOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_NotEqualOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_ShapeOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_PowOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_ArgMinOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_FakeQuantOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_PackOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_LogicalOrOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_OneHotOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_LogicalAndOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_LogicalNotOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_UnpackOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_FloorDivOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_SquareOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_ZerosLikeOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_FillOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_BidirectionalSequenceLSTMOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_BidirectionalSequenceRNNOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_UnidirectionalSequenceLSTMOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_FloorModOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_RangeOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_ResizeNearestNeighborOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_LeakyReluOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_SquaredDifferenceOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_MirrorPadOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_AbsOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_SplitVOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_UniqueOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_ReverseV2Options; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_AddNOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_GatherNdOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_CosOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_WhereOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_RankOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_ReverseSequenceOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_MatrixDiagOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_QuantizeOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_MatrixSetDiagOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_HardSwishOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_IfOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_WhileOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_DepthToSpaceOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_NonMaxSuppressionV4Options; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_NonMaxSuppressionV5Options; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_ScatterNdOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_SelectV2Options; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_DensifyOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_SegmentSumOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_BatchMatMulOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_CumsumOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_CallOnceOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_BroadcastToOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_Rfft2dOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_Conv3DOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_HashtableOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_HashtableFindOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_HashtableImportOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_HashtableSizeOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_VarHandleOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_ReadVariableOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_AssignVariableOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_RandomOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_BucketizeOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_GeluOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_DynamicUpdateSliceOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_UnsortedSegmentProdOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_UnsortedSegmentMaxOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_UnsortedSegmentMinOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_UnsortedSegmentSumOptions; -}; - -template<> struct BuiltinOptionsTraits { - static const BuiltinOptions enum_value = BuiltinOptions_ATan2Options; -}; - -struct BuiltinOptionsUnion { - BuiltinOptions type; - void *value; - - BuiltinOptionsUnion() : type(BuiltinOptions_NONE), value(nullptr) {} - BuiltinOptionsUnion(BuiltinOptionsUnion&& u) FLATBUFFERS_NOEXCEPT : - type(BuiltinOptions_NONE), value(nullptr) - { std::swap(type, u.type); std::swap(value, u.value); } - BuiltinOptionsUnion(const BuiltinOptionsUnion &); - BuiltinOptionsUnion &operator=(const BuiltinOptionsUnion &u) - { BuiltinOptionsUnion t(u); std::swap(type, t.type); std::swap(value, t.value); return *this; } - BuiltinOptionsUnion &operator=(BuiltinOptionsUnion &&u) FLATBUFFERS_NOEXCEPT - { std::swap(type, u.type); std::swap(value, u.value); return *this; } - ~BuiltinOptionsUnion() { Reset(); } - - void Reset(); - -#ifndef FLATBUFFERS_CPP98_STL - template - void Set(T&& val) { - using RT = typename std::remove_reference::type; - Reset(); - type = BuiltinOptionsTraits::enum_value; - if (type != BuiltinOptions_NONE) { - value = new RT(std::forward(val)); - } - } -#endif // FLATBUFFERS_CPP98_STL - - static void *UnPack(const void *obj, BuiltinOptions type, const flatbuffers::resolver_function_t *resolver); - flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const flatbuffers::rehasher_function_t *_rehasher = nullptr) const; - - tflite::Conv2DOptionsT *AsConv2DOptions() { - return type == BuiltinOptions_Conv2DOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::Conv2DOptionsT *AsConv2DOptions() const { - return type == BuiltinOptions_Conv2DOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::DepthwiseConv2DOptionsT *AsDepthwiseConv2DOptions() { - return type == BuiltinOptions_DepthwiseConv2DOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::DepthwiseConv2DOptionsT *AsDepthwiseConv2DOptions() const { - return type == BuiltinOptions_DepthwiseConv2DOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::ConcatEmbeddingsOptionsT *AsConcatEmbeddingsOptions() { - return type == BuiltinOptions_ConcatEmbeddingsOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::ConcatEmbeddingsOptionsT *AsConcatEmbeddingsOptions() const { - return type == BuiltinOptions_ConcatEmbeddingsOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::LSHProjectionOptionsT *AsLSHProjectionOptions() { - return type == BuiltinOptions_LSHProjectionOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::LSHProjectionOptionsT *AsLSHProjectionOptions() const { - return type == BuiltinOptions_LSHProjectionOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::Pool2DOptionsT *AsPool2DOptions() { - return type == BuiltinOptions_Pool2DOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::Pool2DOptionsT *AsPool2DOptions() const { - return type == BuiltinOptions_Pool2DOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::SVDFOptionsT *AsSVDFOptions() { - return type == BuiltinOptions_SVDFOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::SVDFOptionsT *AsSVDFOptions() const { - return type == BuiltinOptions_SVDFOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::RNNOptionsT *AsRNNOptions() { - return type == BuiltinOptions_RNNOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::RNNOptionsT *AsRNNOptions() const { - return type == BuiltinOptions_RNNOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::FullyConnectedOptionsT *AsFullyConnectedOptions() { - return type == BuiltinOptions_FullyConnectedOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::FullyConnectedOptionsT *AsFullyConnectedOptions() const { - return type == BuiltinOptions_FullyConnectedOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::SoftmaxOptionsT *AsSoftmaxOptions() { - return type == BuiltinOptions_SoftmaxOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::SoftmaxOptionsT *AsSoftmaxOptions() const { - return type == BuiltinOptions_SoftmaxOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::ConcatenationOptionsT *AsConcatenationOptions() { - return type == BuiltinOptions_ConcatenationOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::ConcatenationOptionsT *AsConcatenationOptions() const { - return type == BuiltinOptions_ConcatenationOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::AddOptionsT *AsAddOptions() { - return type == BuiltinOptions_AddOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::AddOptionsT *AsAddOptions() const { - return type == BuiltinOptions_AddOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::L2NormOptionsT *AsL2NormOptions() { - return type == BuiltinOptions_L2NormOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::L2NormOptionsT *AsL2NormOptions() const { - return type == BuiltinOptions_L2NormOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::LocalResponseNormalizationOptionsT *AsLocalResponseNormalizationOptions() { - return type == BuiltinOptions_LocalResponseNormalizationOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::LocalResponseNormalizationOptionsT *AsLocalResponseNormalizationOptions() const { - return type == BuiltinOptions_LocalResponseNormalizationOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::LSTMOptionsT *AsLSTMOptions() { - return type == BuiltinOptions_LSTMOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::LSTMOptionsT *AsLSTMOptions() const { - return type == BuiltinOptions_LSTMOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::ResizeBilinearOptionsT *AsResizeBilinearOptions() { - return type == BuiltinOptions_ResizeBilinearOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::ResizeBilinearOptionsT *AsResizeBilinearOptions() const { - return type == BuiltinOptions_ResizeBilinearOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::CallOptionsT *AsCallOptions() { - return type == BuiltinOptions_CallOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::CallOptionsT *AsCallOptions() const { - return type == BuiltinOptions_CallOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::ReshapeOptionsT *AsReshapeOptions() { - return type == BuiltinOptions_ReshapeOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::ReshapeOptionsT *AsReshapeOptions() const { - return type == BuiltinOptions_ReshapeOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::SkipGramOptionsT *AsSkipGramOptions() { - return type == BuiltinOptions_SkipGramOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::SkipGramOptionsT *AsSkipGramOptions() const { - return type == BuiltinOptions_SkipGramOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::SpaceToDepthOptionsT *AsSpaceToDepthOptions() { - return type == BuiltinOptions_SpaceToDepthOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::SpaceToDepthOptionsT *AsSpaceToDepthOptions() const { - return type == BuiltinOptions_SpaceToDepthOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::EmbeddingLookupSparseOptionsT *AsEmbeddingLookupSparseOptions() { - return type == BuiltinOptions_EmbeddingLookupSparseOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::EmbeddingLookupSparseOptionsT *AsEmbeddingLookupSparseOptions() const { - return type == BuiltinOptions_EmbeddingLookupSparseOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::MulOptionsT *AsMulOptions() { - return type == BuiltinOptions_MulOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::MulOptionsT *AsMulOptions() const { - return type == BuiltinOptions_MulOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::PadOptionsT *AsPadOptions() { - return type == BuiltinOptions_PadOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::PadOptionsT *AsPadOptions() const { - return type == BuiltinOptions_PadOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::GatherOptionsT *AsGatherOptions() { - return type == BuiltinOptions_GatherOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::GatherOptionsT *AsGatherOptions() const { - return type == BuiltinOptions_GatherOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::BatchToSpaceNDOptionsT *AsBatchToSpaceNDOptions() { - return type == BuiltinOptions_BatchToSpaceNDOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::BatchToSpaceNDOptionsT *AsBatchToSpaceNDOptions() const { - return type == BuiltinOptions_BatchToSpaceNDOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::SpaceToBatchNDOptionsT *AsSpaceToBatchNDOptions() { - return type == BuiltinOptions_SpaceToBatchNDOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::SpaceToBatchNDOptionsT *AsSpaceToBatchNDOptions() const { - return type == BuiltinOptions_SpaceToBatchNDOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::TransposeOptionsT *AsTransposeOptions() { - return type == BuiltinOptions_TransposeOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::TransposeOptionsT *AsTransposeOptions() const { - return type == BuiltinOptions_TransposeOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::ReducerOptionsT *AsReducerOptions() { - return type == BuiltinOptions_ReducerOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::ReducerOptionsT *AsReducerOptions() const { - return type == BuiltinOptions_ReducerOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::SubOptionsT *AsSubOptions() { - return type == BuiltinOptions_SubOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::SubOptionsT *AsSubOptions() const { - return type == BuiltinOptions_SubOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::DivOptionsT *AsDivOptions() { - return type == BuiltinOptions_DivOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::DivOptionsT *AsDivOptions() const { - return type == BuiltinOptions_DivOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::SqueezeOptionsT *AsSqueezeOptions() { - return type == BuiltinOptions_SqueezeOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::SqueezeOptionsT *AsSqueezeOptions() const { - return type == BuiltinOptions_SqueezeOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::SequenceRNNOptionsT *AsSequenceRNNOptions() { - return type == BuiltinOptions_SequenceRNNOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::SequenceRNNOptionsT *AsSequenceRNNOptions() const { - return type == BuiltinOptions_SequenceRNNOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::StridedSliceOptionsT *AsStridedSliceOptions() { - return type == BuiltinOptions_StridedSliceOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::StridedSliceOptionsT *AsStridedSliceOptions() const { - return type == BuiltinOptions_StridedSliceOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::ExpOptionsT *AsExpOptions() { - return type == BuiltinOptions_ExpOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::ExpOptionsT *AsExpOptions() const { - return type == BuiltinOptions_ExpOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::TopKV2OptionsT *AsTopKV2Options() { - return type == BuiltinOptions_TopKV2Options ? - reinterpret_cast(value) : nullptr; - } - const tflite::TopKV2OptionsT *AsTopKV2Options() const { - return type == BuiltinOptions_TopKV2Options ? - reinterpret_cast(value) : nullptr; - } - tflite::SplitOptionsT *AsSplitOptions() { - return type == BuiltinOptions_SplitOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::SplitOptionsT *AsSplitOptions() const { - return type == BuiltinOptions_SplitOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::LogSoftmaxOptionsT *AsLogSoftmaxOptions() { - return type == BuiltinOptions_LogSoftmaxOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::LogSoftmaxOptionsT *AsLogSoftmaxOptions() const { - return type == BuiltinOptions_LogSoftmaxOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::CastOptionsT *AsCastOptions() { - return type == BuiltinOptions_CastOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::CastOptionsT *AsCastOptions() const { - return type == BuiltinOptions_CastOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::DequantizeOptionsT *AsDequantizeOptions() { - return type == BuiltinOptions_DequantizeOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::DequantizeOptionsT *AsDequantizeOptions() const { - return type == BuiltinOptions_DequantizeOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::MaximumMinimumOptionsT *AsMaximumMinimumOptions() { - return type == BuiltinOptions_MaximumMinimumOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::MaximumMinimumOptionsT *AsMaximumMinimumOptions() const { - return type == BuiltinOptions_MaximumMinimumOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::ArgMaxOptionsT *AsArgMaxOptions() { - return type == BuiltinOptions_ArgMaxOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::ArgMaxOptionsT *AsArgMaxOptions() const { - return type == BuiltinOptions_ArgMaxOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::LessOptionsT *AsLessOptions() { - return type == BuiltinOptions_LessOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::LessOptionsT *AsLessOptions() const { - return type == BuiltinOptions_LessOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::NegOptionsT *AsNegOptions() { - return type == BuiltinOptions_NegOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::NegOptionsT *AsNegOptions() const { - return type == BuiltinOptions_NegOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::PadV2OptionsT *AsPadV2Options() { - return type == BuiltinOptions_PadV2Options ? - reinterpret_cast(value) : nullptr; - } - const tflite::PadV2OptionsT *AsPadV2Options() const { - return type == BuiltinOptions_PadV2Options ? - reinterpret_cast(value) : nullptr; - } - tflite::GreaterOptionsT *AsGreaterOptions() { - return type == BuiltinOptions_GreaterOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::GreaterOptionsT *AsGreaterOptions() const { - return type == BuiltinOptions_GreaterOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::GreaterEqualOptionsT *AsGreaterEqualOptions() { - return type == BuiltinOptions_GreaterEqualOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::GreaterEqualOptionsT *AsGreaterEqualOptions() const { - return type == BuiltinOptions_GreaterEqualOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::LessEqualOptionsT *AsLessEqualOptions() { - return type == BuiltinOptions_LessEqualOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::LessEqualOptionsT *AsLessEqualOptions() const { - return type == BuiltinOptions_LessEqualOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::SelectOptionsT *AsSelectOptions() { - return type == BuiltinOptions_SelectOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::SelectOptionsT *AsSelectOptions() const { - return type == BuiltinOptions_SelectOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::SliceOptionsT *AsSliceOptions() { - return type == BuiltinOptions_SliceOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::SliceOptionsT *AsSliceOptions() const { - return type == BuiltinOptions_SliceOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::TransposeConvOptionsT *AsTransposeConvOptions() { - return type == BuiltinOptions_TransposeConvOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::TransposeConvOptionsT *AsTransposeConvOptions() const { - return type == BuiltinOptions_TransposeConvOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::SparseToDenseOptionsT *AsSparseToDenseOptions() { - return type == BuiltinOptions_SparseToDenseOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::SparseToDenseOptionsT *AsSparseToDenseOptions() const { - return type == BuiltinOptions_SparseToDenseOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::TileOptionsT *AsTileOptions() { - return type == BuiltinOptions_TileOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::TileOptionsT *AsTileOptions() const { - return type == BuiltinOptions_TileOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::ExpandDimsOptionsT *AsExpandDimsOptions() { - return type == BuiltinOptions_ExpandDimsOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::ExpandDimsOptionsT *AsExpandDimsOptions() const { - return type == BuiltinOptions_ExpandDimsOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::EqualOptionsT *AsEqualOptions() { - return type == BuiltinOptions_EqualOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::EqualOptionsT *AsEqualOptions() const { - return type == BuiltinOptions_EqualOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::NotEqualOptionsT *AsNotEqualOptions() { - return type == BuiltinOptions_NotEqualOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::NotEqualOptionsT *AsNotEqualOptions() const { - return type == BuiltinOptions_NotEqualOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::ShapeOptionsT *AsShapeOptions() { - return type == BuiltinOptions_ShapeOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::ShapeOptionsT *AsShapeOptions() const { - return type == BuiltinOptions_ShapeOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::PowOptionsT *AsPowOptions() { - return type == BuiltinOptions_PowOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::PowOptionsT *AsPowOptions() const { - return type == BuiltinOptions_PowOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::ArgMinOptionsT *AsArgMinOptions() { - return type == BuiltinOptions_ArgMinOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::ArgMinOptionsT *AsArgMinOptions() const { - return type == BuiltinOptions_ArgMinOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::FakeQuantOptionsT *AsFakeQuantOptions() { - return type == BuiltinOptions_FakeQuantOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::FakeQuantOptionsT *AsFakeQuantOptions() const { - return type == BuiltinOptions_FakeQuantOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::PackOptionsT *AsPackOptions() { - return type == BuiltinOptions_PackOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::PackOptionsT *AsPackOptions() const { - return type == BuiltinOptions_PackOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::LogicalOrOptionsT *AsLogicalOrOptions() { - return type == BuiltinOptions_LogicalOrOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::LogicalOrOptionsT *AsLogicalOrOptions() const { - return type == BuiltinOptions_LogicalOrOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::OneHotOptionsT *AsOneHotOptions() { - return type == BuiltinOptions_OneHotOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::OneHotOptionsT *AsOneHotOptions() const { - return type == BuiltinOptions_OneHotOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::LogicalAndOptionsT *AsLogicalAndOptions() { - return type == BuiltinOptions_LogicalAndOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::LogicalAndOptionsT *AsLogicalAndOptions() const { - return type == BuiltinOptions_LogicalAndOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::LogicalNotOptionsT *AsLogicalNotOptions() { - return type == BuiltinOptions_LogicalNotOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::LogicalNotOptionsT *AsLogicalNotOptions() const { - return type == BuiltinOptions_LogicalNotOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::UnpackOptionsT *AsUnpackOptions() { - return type == BuiltinOptions_UnpackOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::UnpackOptionsT *AsUnpackOptions() const { - return type == BuiltinOptions_UnpackOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::FloorDivOptionsT *AsFloorDivOptions() { - return type == BuiltinOptions_FloorDivOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::FloorDivOptionsT *AsFloorDivOptions() const { - return type == BuiltinOptions_FloorDivOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::SquareOptionsT *AsSquareOptions() { - return type == BuiltinOptions_SquareOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::SquareOptionsT *AsSquareOptions() const { - return type == BuiltinOptions_SquareOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::ZerosLikeOptionsT *AsZerosLikeOptions() { - return type == BuiltinOptions_ZerosLikeOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::ZerosLikeOptionsT *AsZerosLikeOptions() const { - return type == BuiltinOptions_ZerosLikeOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::FillOptionsT *AsFillOptions() { - return type == BuiltinOptions_FillOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::FillOptionsT *AsFillOptions() const { - return type == BuiltinOptions_FillOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::BidirectionalSequenceLSTMOptionsT *AsBidirectionalSequenceLSTMOptions() { - return type == BuiltinOptions_BidirectionalSequenceLSTMOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::BidirectionalSequenceLSTMOptionsT *AsBidirectionalSequenceLSTMOptions() const { - return type == BuiltinOptions_BidirectionalSequenceLSTMOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::BidirectionalSequenceRNNOptionsT *AsBidirectionalSequenceRNNOptions() { - return type == BuiltinOptions_BidirectionalSequenceRNNOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::BidirectionalSequenceRNNOptionsT *AsBidirectionalSequenceRNNOptions() const { - return type == BuiltinOptions_BidirectionalSequenceRNNOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::UnidirectionalSequenceLSTMOptionsT *AsUnidirectionalSequenceLSTMOptions() { - return type == BuiltinOptions_UnidirectionalSequenceLSTMOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::UnidirectionalSequenceLSTMOptionsT *AsUnidirectionalSequenceLSTMOptions() const { - return type == BuiltinOptions_UnidirectionalSequenceLSTMOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::FloorModOptionsT *AsFloorModOptions() { - return type == BuiltinOptions_FloorModOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::FloorModOptionsT *AsFloorModOptions() const { - return type == BuiltinOptions_FloorModOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::RangeOptionsT *AsRangeOptions() { - return type == BuiltinOptions_RangeOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::RangeOptionsT *AsRangeOptions() const { - return type == BuiltinOptions_RangeOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::ResizeNearestNeighborOptionsT *AsResizeNearestNeighborOptions() { - return type == BuiltinOptions_ResizeNearestNeighborOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::ResizeNearestNeighborOptionsT *AsResizeNearestNeighborOptions() const { - return type == BuiltinOptions_ResizeNearestNeighborOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::LeakyReluOptionsT *AsLeakyReluOptions() { - return type == BuiltinOptions_LeakyReluOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::LeakyReluOptionsT *AsLeakyReluOptions() const { - return type == BuiltinOptions_LeakyReluOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::SquaredDifferenceOptionsT *AsSquaredDifferenceOptions() { - return type == BuiltinOptions_SquaredDifferenceOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::SquaredDifferenceOptionsT *AsSquaredDifferenceOptions() const { - return type == BuiltinOptions_SquaredDifferenceOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::MirrorPadOptionsT *AsMirrorPadOptions() { - return type == BuiltinOptions_MirrorPadOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::MirrorPadOptionsT *AsMirrorPadOptions() const { - return type == BuiltinOptions_MirrorPadOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::AbsOptionsT *AsAbsOptions() { - return type == BuiltinOptions_AbsOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::AbsOptionsT *AsAbsOptions() const { - return type == BuiltinOptions_AbsOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::SplitVOptionsT *AsSplitVOptions() { - return type == BuiltinOptions_SplitVOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::SplitVOptionsT *AsSplitVOptions() const { - return type == BuiltinOptions_SplitVOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::UniqueOptionsT *AsUniqueOptions() { - return type == BuiltinOptions_UniqueOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::UniqueOptionsT *AsUniqueOptions() const { - return type == BuiltinOptions_UniqueOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::ReverseV2OptionsT *AsReverseV2Options() { - return type == BuiltinOptions_ReverseV2Options ? - reinterpret_cast(value) : nullptr; - } - const tflite::ReverseV2OptionsT *AsReverseV2Options() const { - return type == BuiltinOptions_ReverseV2Options ? - reinterpret_cast(value) : nullptr; - } - tflite::AddNOptionsT *AsAddNOptions() { - return type == BuiltinOptions_AddNOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::AddNOptionsT *AsAddNOptions() const { - return type == BuiltinOptions_AddNOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::GatherNdOptionsT *AsGatherNdOptions() { - return type == BuiltinOptions_GatherNdOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::GatherNdOptionsT *AsGatherNdOptions() const { - return type == BuiltinOptions_GatherNdOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::CosOptionsT *AsCosOptions() { - return type == BuiltinOptions_CosOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::CosOptionsT *AsCosOptions() const { - return type == BuiltinOptions_CosOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::WhereOptionsT *AsWhereOptions() { - return type == BuiltinOptions_WhereOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::WhereOptionsT *AsWhereOptions() const { - return type == BuiltinOptions_WhereOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::RankOptionsT *AsRankOptions() { - return type == BuiltinOptions_RankOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::RankOptionsT *AsRankOptions() const { - return type == BuiltinOptions_RankOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::ReverseSequenceOptionsT *AsReverseSequenceOptions() { - return type == BuiltinOptions_ReverseSequenceOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::ReverseSequenceOptionsT *AsReverseSequenceOptions() const { - return type == BuiltinOptions_ReverseSequenceOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::MatrixDiagOptionsT *AsMatrixDiagOptions() { - return type == BuiltinOptions_MatrixDiagOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::MatrixDiagOptionsT *AsMatrixDiagOptions() const { - return type == BuiltinOptions_MatrixDiagOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::QuantizeOptionsT *AsQuantizeOptions() { - return type == BuiltinOptions_QuantizeOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::QuantizeOptionsT *AsQuantizeOptions() const { - return type == BuiltinOptions_QuantizeOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::MatrixSetDiagOptionsT *AsMatrixSetDiagOptions() { - return type == BuiltinOptions_MatrixSetDiagOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::MatrixSetDiagOptionsT *AsMatrixSetDiagOptions() const { - return type == BuiltinOptions_MatrixSetDiagOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::HardSwishOptionsT *AsHardSwishOptions() { - return type == BuiltinOptions_HardSwishOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::HardSwishOptionsT *AsHardSwishOptions() const { - return type == BuiltinOptions_HardSwishOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::IfOptionsT *AsIfOptions() { - return type == BuiltinOptions_IfOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::IfOptionsT *AsIfOptions() const { - return type == BuiltinOptions_IfOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::WhileOptionsT *AsWhileOptions() { - return type == BuiltinOptions_WhileOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::WhileOptionsT *AsWhileOptions() const { - return type == BuiltinOptions_WhileOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::DepthToSpaceOptionsT *AsDepthToSpaceOptions() { - return type == BuiltinOptions_DepthToSpaceOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::DepthToSpaceOptionsT *AsDepthToSpaceOptions() const { - return type == BuiltinOptions_DepthToSpaceOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::NonMaxSuppressionV4OptionsT *AsNonMaxSuppressionV4Options() { - return type == BuiltinOptions_NonMaxSuppressionV4Options ? - reinterpret_cast(value) : nullptr; - } - const tflite::NonMaxSuppressionV4OptionsT *AsNonMaxSuppressionV4Options() const { - return type == BuiltinOptions_NonMaxSuppressionV4Options ? - reinterpret_cast(value) : nullptr; - } - tflite::NonMaxSuppressionV5OptionsT *AsNonMaxSuppressionV5Options() { - return type == BuiltinOptions_NonMaxSuppressionV5Options ? - reinterpret_cast(value) : nullptr; - } - const tflite::NonMaxSuppressionV5OptionsT *AsNonMaxSuppressionV5Options() const { - return type == BuiltinOptions_NonMaxSuppressionV5Options ? - reinterpret_cast(value) : nullptr; - } - tflite::ScatterNdOptionsT *AsScatterNdOptions() { - return type == BuiltinOptions_ScatterNdOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::ScatterNdOptionsT *AsScatterNdOptions() const { - return type == BuiltinOptions_ScatterNdOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::SelectV2OptionsT *AsSelectV2Options() { - return type == BuiltinOptions_SelectV2Options ? - reinterpret_cast(value) : nullptr; - } - const tflite::SelectV2OptionsT *AsSelectV2Options() const { - return type == BuiltinOptions_SelectV2Options ? - reinterpret_cast(value) : nullptr; - } - tflite::DensifyOptionsT *AsDensifyOptions() { - return type == BuiltinOptions_DensifyOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::DensifyOptionsT *AsDensifyOptions() const { - return type == BuiltinOptions_DensifyOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::SegmentSumOptionsT *AsSegmentSumOptions() { - return type == BuiltinOptions_SegmentSumOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::SegmentSumOptionsT *AsSegmentSumOptions() const { - return type == BuiltinOptions_SegmentSumOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::BatchMatMulOptionsT *AsBatchMatMulOptions() { - return type == BuiltinOptions_BatchMatMulOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::BatchMatMulOptionsT *AsBatchMatMulOptions() const { - return type == BuiltinOptions_BatchMatMulOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::CumsumOptionsT *AsCumsumOptions() { - return type == BuiltinOptions_CumsumOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::CumsumOptionsT *AsCumsumOptions() const { - return type == BuiltinOptions_CumsumOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::CallOnceOptionsT *AsCallOnceOptions() { - return type == BuiltinOptions_CallOnceOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::CallOnceOptionsT *AsCallOnceOptions() const { - return type == BuiltinOptions_CallOnceOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::BroadcastToOptionsT *AsBroadcastToOptions() { - return type == BuiltinOptions_BroadcastToOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::BroadcastToOptionsT *AsBroadcastToOptions() const { - return type == BuiltinOptions_BroadcastToOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::Rfft2dOptionsT *AsRfft2dOptions() { - return type == BuiltinOptions_Rfft2dOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::Rfft2dOptionsT *AsRfft2dOptions() const { - return type == BuiltinOptions_Rfft2dOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::Conv3DOptionsT *AsConv3DOptions() { - return type == BuiltinOptions_Conv3DOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::Conv3DOptionsT *AsConv3DOptions() const { - return type == BuiltinOptions_Conv3DOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::HashtableOptionsT *AsHashtableOptions() { - return type == BuiltinOptions_HashtableOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::HashtableOptionsT *AsHashtableOptions() const { - return type == BuiltinOptions_HashtableOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::HashtableFindOptionsT *AsHashtableFindOptions() { - return type == BuiltinOptions_HashtableFindOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::HashtableFindOptionsT *AsHashtableFindOptions() const { - return type == BuiltinOptions_HashtableFindOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::HashtableImportOptionsT *AsHashtableImportOptions() { - return type == BuiltinOptions_HashtableImportOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::HashtableImportOptionsT *AsHashtableImportOptions() const { - return type == BuiltinOptions_HashtableImportOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::HashtableSizeOptionsT *AsHashtableSizeOptions() { - return type == BuiltinOptions_HashtableSizeOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::HashtableSizeOptionsT *AsHashtableSizeOptions() const { - return type == BuiltinOptions_HashtableSizeOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::VarHandleOptionsT *AsVarHandleOptions() { - return type == BuiltinOptions_VarHandleOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::VarHandleOptionsT *AsVarHandleOptions() const { - return type == BuiltinOptions_VarHandleOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::ReadVariableOptionsT *AsReadVariableOptions() { - return type == BuiltinOptions_ReadVariableOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::ReadVariableOptionsT *AsReadVariableOptions() const { - return type == BuiltinOptions_ReadVariableOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::AssignVariableOptionsT *AsAssignVariableOptions() { - return type == BuiltinOptions_AssignVariableOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::AssignVariableOptionsT *AsAssignVariableOptions() const { - return type == BuiltinOptions_AssignVariableOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::RandomOptionsT *AsRandomOptions() { - return type == BuiltinOptions_RandomOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::RandomOptionsT *AsRandomOptions() const { - return type == BuiltinOptions_RandomOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::BucketizeOptionsT *AsBucketizeOptions() { - return type == BuiltinOptions_BucketizeOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::BucketizeOptionsT *AsBucketizeOptions() const { - return type == BuiltinOptions_BucketizeOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::GeluOptionsT *AsGeluOptions() { - return type == BuiltinOptions_GeluOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::GeluOptionsT *AsGeluOptions() const { - return type == BuiltinOptions_GeluOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::DynamicUpdateSliceOptionsT *AsDynamicUpdateSliceOptions() { - return type == BuiltinOptions_DynamicUpdateSliceOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::DynamicUpdateSliceOptionsT *AsDynamicUpdateSliceOptions() const { - return type == BuiltinOptions_DynamicUpdateSliceOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::UnsortedSegmentProdOptionsT *AsUnsortedSegmentProdOptions() { - return type == BuiltinOptions_UnsortedSegmentProdOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::UnsortedSegmentProdOptionsT *AsUnsortedSegmentProdOptions() const { - return type == BuiltinOptions_UnsortedSegmentProdOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::UnsortedSegmentMaxOptionsT *AsUnsortedSegmentMaxOptions() { - return type == BuiltinOptions_UnsortedSegmentMaxOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::UnsortedSegmentMaxOptionsT *AsUnsortedSegmentMaxOptions() const { - return type == BuiltinOptions_UnsortedSegmentMaxOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::UnsortedSegmentMinOptionsT *AsUnsortedSegmentMinOptions() { - return type == BuiltinOptions_UnsortedSegmentMinOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::UnsortedSegmentMinOptionsT *AsUnsortedSegmentMinOptions() const { - return type == BuiltinOptions_UnsortedSegmentMinOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::UnsortedSegmentSumOptionsT *AsUnsortedSegmentSumOptions() { - return type == BuiltinOptions_UnsortedSegmentSumOptions ? - reinterpret_cast(value) : nullptr; - } - const tflite::UnsortedSegmentSumOptionsT *AsUnsortedSegmentSumOptions() const { - return type == BuiltinOptions_UnsortedSegmentSumOptions ? - reinterpret_cast(value) : nullptr; - } - tflite::ATan2OptionsT *AsATan2Options() { - return type == BuiltinOptions_ATan2Options ? - reinterpret_cast(value) : nullptr; - } - const tflite::ATan2OptionsT *AsATan2Options() const { - return type == BuiltinOptions_ATan2Options ? - reinterpret_cast(value) : nullptr; - } -}; - -bool VerifyBuiltinOptions(flatbuffers::Verifier &verifier, const void *obj, BuiltinOptions type); -bool VerifyBuiltinOptionsVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector> *values, const flatbuffers::Vector *types); - -enum Padding { - Padding_SAME = 0, - Padding_VALID = 1, - Padding_MIN = Padding_SAME, - Padding_MAX = Padding_VALID -}; - -inline const Padding (&EnumValuesPadding())[2] { - static const Padding values[] = { - Padding_SAME, - Padding_VALID - }; - return values; -} - -inline const char * const *EnumNamesPadding() { - static const char * const names[3] = { - "SAME", - "VALID", - nullptr - }; - return names; -} - -inline const char *EnumNamePadding(Padding e) { - if (flatbuffers::IsOutRange(e, Padding_SAME, Padding_VALID)) return ""; - const size_t index = static_cast(e); - return EnumNamesPadding()[index]; -} - -enum ActivationFunctionType { - ActivationFunctionType_NONE = 0, - ActivationFunctionType_RELU = 1, - ActivationFunctionType_RELU_N1_TO_1 = 2, - ActivationFunctionType_RELU6 = 3, - ActivationFunctionType_TANH = 4, - ActivationFunctionType_SIGN_BIT = 5, - ActivationFunctionType_MIN = ActivationFunctionType_NONE, - ActivationFunctionType_MAX = ActivationFunctionType_SIGN_BIT -}; - -inline const ActivationFunctionType (&EnumValuesActivationFunctionType())[6] { - static const ActivationFunctionType values[] = { - ActivationFunctionType_NONE, - ActivationFunctionType_RELU, - ActivationFunctionType_RELU_N1_TO_1, - ActivationFunctionType_RELU6, - ActivationFunctionType_TANH, - ActivationFunctionType_SIGN_BIT - }; - return values; -} - -inline const char * const *EnumNamesActivationFunctionType() { - static const char * const names[7] = { - "NONE", - "RELU", - "RELU_N1_TO_1", - "RELU6", - "TANH", - "SIGN_BIT", - nullptr - }; - return names; -} - -inline const char *EnumNameActivationFunctionType(ActivationFunctionType e) { - if (flatbuffers::IsOutRange(e, ActivationFunctionType_NONE, ActivationFunctionType_SIGN_BIT)) return ""; - const size_t index = static_cast(e); - return EnumNamesActivationFunctionType()[index]; -} - -enum LSHProjectionType { - LSHProjectionType_UNKNOWN = 0, - LSHProjectionType_SPARSE = 1, - LSHProjectionType_DENSE = 2, - LSHProjectionType_MIN = LSHProjectionType_UNKNOWN, - LSHProjectionType_MAX = LSHProjectionType_DENSE -}; - -inline const LSHProjectionType (&EnumValuesLSHProjectionType())[3] { - static const LSHProjectionType values[] = { - LSHProjectionType_UNKNOWN, - LSHProjectionType_SPARSE, - LSHProjectionType_DENSE - }; - return values; -} - -inline const char * const *EnumNamesLSHProjectionType() { - static const char * const names[4] = { - "UNKNOWN", - "SPARSE", - "DENSE", - nullptr - }; - return names; -} - -inline const char *EnumNameLSHProjectionType(LSHProjectionType e) { - if (flatbuffers::IsOutRange(e, LSHProjectionType_UNKNOWN, LSHProjectionType_DENSE)) return ""; - const size_t index = static_cast(e); - return EnumNamesLSHProjectionType()[index]; -} - -enum FullyConnectedOptionsWeightsFormat { - FullyConnectedOptionsWeightsFormat_DEFAULT = 0, - FullyConnectedOptionsWeightsFormat_SHUFFLED4x16INT8 = 1, - FullyConnectedOptionsWeightsFormat_MIN = FullyConnectedOptionsWeightsFormat_DEFAULT, - FullyConnectedOptionsWeightsFormat_MAX = FullyConnectedOptionsWeightsFormat_SHUFFLED4x16INT8 -}; - -inline const FullyConnectedOptionsWeightsFormat (&EnumValuesFullyConnectedOptionsWeightsFormat())[2] { - static const FullyConnectedOptionsWeightsFormat values[] = { - FullyConnectedOptionsWeightsFormat_DEFAULT, - FullyConnectedOptionsWeightsFormat_SHUFFLED4x16INT8 - }; - return values; -} - -inline const char * const *EnumNamesFullyConnectedOptionsWeightsFormat() { - static const char * const names[3] = { - "DEFAULT", - "SHUFFLED4x16INT8", - nullptr - }; - return names; -} - -inline const char *EnumNameFullyConnectedOptionsWeightsFormat(FullyConnectedOptionsWeightsFormat e) { - if (flatbuffers::IsOutRange(e, FullyConnectedOptionsWeightsFormat_DEFAULT, FullyConnectedOptionsWeightsFormat_SHUFFLED4x16INT8)) return ""; - const size_t index = static_cast(e); - return EnumNamesFullyConnectedOptionsWeightsFormat()[index]; -} - -enum LSTMKernelType { - LSTMKernelType_FULL = 0, - LSTMKernelType_BASIC = 1, - LSTMKernelType_MIN = LSTMKernelType_FULL, - LSTMKernelType_MAX = LSTMKernelType_BASIC -}; - -inline const LSTMKernelType (&EnumValuesLSTMKernelType())[2] { - static const LSTMKernelType values[] = { - LSTMKernelType_FULL, - LSTMKernelType_BASIC - }; - return values; -} - -inline const char * const *EnumNamesLSTMKernelType() { - static const char * const names[3] = { - "FULL", - "BASIC", - nullptr - }; - return names; -} - -inline const char *EnumNameLSTMKernelType(LSTMKernelType e) { - if (flatbuffers::IsOutRange(e, LSTMKernelType_FULL, LSTMKernelType_BASIC)) return ""; - const size_t index = static_cast(e); - return EnumNamesLSTMKernelType()[index]; -} - -enum CombinerType { - CombinerType_SUM = 0, - CombinerType_MEAN = 1, - CombinerType_SQRTN = 2, - CombinerType_MIN = CombinerType_SUM, - CombinerType_MAX = CombinerType_SQRTN -}; - -inline const CombinerType (&EnumValuesCombinerType())[3] { - static const CombinerType values[] = { - CombinerType_SUM, - CombinerType_MEAN, - CombinerType_SQRTN - }; - return values; -} - -inline const char * const *EnumNamesCombinerType() { - static const char * const names[4] = { - "SUM", - "MEAN", - "SQRTN", - nullptr - }; - return names; -} - -inline const char *EnumNameCombinerType(CombinerType e) { - if (flatbuffers::IsOutRange(e, CombinerType_SUM, CombinerType_SQRTN)) return ""; - const size_t index = static_cast(e); - return EnumNamesCombinerType()[index]; -} - -enum MirrorPadMode { - MirrorPadMode_REFLECT = 0, - MirrorPadMode_SYMMETRIC = 1, - MirrorPadMode_MIN = MirrorPadMode_REFLECT, - MirrorPadMode_MAX = MirrorPadMode_SYMMETRIC -}; - -inline const MirrorPadMode (&EnumValuesMirrorPadMode())[2] { - static const MirrorPadMode values[] = { - MirrorPadMode_REFLECT, - MirrorPadMode_SYMMETRIC - }; - return values; -} - -inline const char * const *EnumNamesMirrorPadMode() { - static const char * const names[3] = { - "REFLECT", - "SYMMETRIC", - nullptr - }; - return names; -} - -inline const char *EnumNameMirrorPadMode(MirrorPadMode e) { - if (flatbuffers::IsOutRange(e, MirrorPadMode_REFLECT, MirrorPadMode_SYMMETRIC)) return ""; - const size_t index = static_cast(e); - return EnumNamesMirrorPadMode()[index]; -} - -enum CustomOptionsFormat { - CustomOptionsFormat_FLEXBUFFERS = 0, - CustomOptionsFormat_MIN = CustomOptionsFormat_FLEXBUFFERS, - CustomOptionsFormat_MAX = CustomOptionsFormat_FLEXBUFFERS -}; - -inline const CustomOptionsFormat (&EnumValuesCustomOptionsFormat())[1] { - static const CustomOptionsFormat values[] = { - CustomOptionsFormat_FLEXBUFFERS - }; - return values; -} - -inline const char * const *EnumNamesCustomOptionsFormat() { - static const char * const names[2] = { - "FLEXBUFFERS", - nullptr - }; - return names; -} - -inline const char *EnumNameCustomOptionsFormat(CustomOptionsFormat e) { - if (flatbuffers::IsOutRange(e, CustomOptionsFormat_FLEXBUFFERS, CustomOptionsFormat_FLEXBUFFERS)) return ""; - const size_t index = static_cast(e); - return EnumNamesCustomOptionsFormat()[index]; -} - -struct CustomQuantizationT : public flatbuffers::NativeTable { - typedef CustomQuantization TableType; - std::vector custom; - CustomQuantizationT() { - } -}; - -struct CustomQuantization FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef CustomQuantizationT NativeTableType; - typedef CustomQuantizationBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_CUSTOM = 4 - }; - const flatbuffers::Vector *custom() const { - return GetPointer *>(VT_CUSTOM); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyOffset(verifier, VT_CUSTOM) && - verifier.VerifyVector(custom()) && - verifier.EndTable(); - } - CustomQuantizationT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(CustomQuantizationT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const CustomQuantizationT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct CustomQuantizationBuilder { - typedef CustomQuantization Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_custom(flatbuffers::Offset> custom) { - fbb_.AddOffset(CustomQuantization::VT_CUSTOM, custom); - } - explicit CustomQuantizationBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - CustomQuantizationBuilder &operator=(const CustomQuantizationBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateCustomQuantization( - flatbuffers::FlatBufferBuilder &_fbb, - flatbuffers::Offset> custom = 0) { - CustomQuantizationBuilder builder_(_fbb); - builder_.add_custom(custom); - return builder_.Finish(); -} - -inline flatbuffers::Offset CreateCustomQuantizationDirect( - flatbuffers::FlatBufferBuilder &_fbb, - const std::vector *custom = nullptr) { - if (custom) { _fbb.ForceVectorAlignment(custom->size(), sizeof(uint8_t), 16); } - auto custom__ = custom ? _fbb.CreateVector(*custom) : 0; - return tflite::CreateCustomQuantization( - _fbb, - custom__); -} - -flatbuffers::Offset CreateCustomQuantization(flatbuffers::FlatBufferBuilder &_fbb, const CustomQuantizationT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct QuantizationParametersT : public flatbuffers::NativeTable { - typedef QuantizationParameters TableType; - std::vector min; - std::vector max; - std::vector scale; - std::vector zero_point; - tflite::QuantizationDetailsUnion details; - int32_t quantized_dimension; - QuantizationParametersT() - : quantized_dimension(0) { - } -}; - -struct QuantizationParameters FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef QuantizationParametersT NativeTableType; - typedef QuantizationParametersBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_MIN = 4, - VT_MAX = 6, - VT_SCALE = 8, - VT_ZERO_POINT = 10, - VT_DETAILS_TYPE = 12, - VT_DETAILS = 14, - VT_QUANTIZED_DIMENSION = 16 - }; - const flatbuffers::Vector *min() const { - return GetPointer *>(VT_MIN); - } - const flatbuffers::Vector *max() const { - return GetPointer *>(VT_MAX); - } - const flatbuffers::Vector *scale() const { - return GetPointer *>(VT_SCALE); - } - const flatbuffers::Vector *zero_point() const { - return GetPointer *>(VT_ZERO_POINT); - } - tflite::QuantizationDetails details_type() const { - return static_cast(GetField(VT_DETAILS_TYPE, 0)); - } - const void *details() const { - return GetPointer(VT_DETAILS); - } - template const T *details_as() const; - const tflite::CustomQuantization *details_as_CustomQuantization() const { - return details_type() == tflite::QuantizationDetails_CustomQuantization ? static_cast(details()) : nullptr; - } - int32_t quantized_dimension() const { - return GetField(VT_QUANTIZED_DIMENSION, 0); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyOffset(verifier, VT_MIN) && - verifier.VerifyVector(min()) && - VerifyOffset(verifier, VT_MAX) && - verifier.VerifyVector(max()) && - VerifyOffset(verifier, VT_SCALE) && - verifier.VerifyVector(scale()) && - VerifyOffset(verifier, VT_ZERO_POINT) && - verifier.VerifyVector(zero_point()) && - VerifyField(verifier, VT_DETAILS_TYPE) && - VerifyOffset(verifier, VT_DETAILS) && - VerifyQuantizationDetails(verifier, details(), details_type()) && - VerifyField(verifier, VT_QUANTIZED_DIMENSION) && - verifier.EndTable(); - } - QuantizationParametersT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(QuantizationParametersT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const QuantizationParametersT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -template<> inline const tflite::CustomQuantization *QuantizationParameters::details_as() const { - return details_as_CustomQuantization(); -} - -struct QuantizationParametersBuilder { - typedef QuantizationParameters Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_min(flatbuffers::Offset> min) { - fbb_.AddOffset(QuantizationParameters::VT_MIN, min); - } - void add_max(flatbuffers::Offset> max) { - fbb_.AddOffset(QuantizationParameters::VT_MAX, max); - } - void add_scale(flatbuffers::Offset> scale) { - fbb_.AddOffset(QuantizationParameters::VT_SCALE, scale); - } - void add_zero_point(flatbuffers::Offset> zero_point) { - fbb_.AddOffset(QuantizationParameters::VT_ZERO_POINT, zero_point); - } - void add_details_type(tflite::QuantizationDetails details_type) { - fbb_.AddElement(QuantizationParameters::VT_DETAILS_TYPE, static_cast(details_type), 0); - } - void add_details(flatbuffers::Offset details) { - fbb_.AddOffset(QuantizationParameters::VT_DETAILS, details); - } - void add_quantized_dimension(int32_t quantized_dimension) { - fbb_.AddElement(QuantizationParameters::VT_QUANTIZED_DIMENSION, quantized_dimension, 0); - } - explicit QuantizationParametersBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - QuantizationParametersBuilder &operator=(const QuantizationParametersBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateQuantizationParameters( - flatbuffers::FlatBufferBuilder &_fbb, - flatbuffers::Offset> min = 0, - flatbuffers::Offset> max = 0, - flatbuffers::Offset> scale = 0, - flatbuffers::Offset> zero_point = 0, - tflite::QuantizationDetails details_type = tflite::QuantizationDetails_NONE, - flatbuffers::Offset details = 0, - int32_t quantized_dimension = 0) { - QuantizationParametersBuilder builder_(_fbb); - builder_.add_quantized_dimension(quantized_dimension); - builder_.add_details(details); - builder_.add_zero_point(zero_point); - builder_.add_scale(scale); - builder_.add_max(max); - builder_.add_min(min); - builder_.add_details_type(details_type); - return builder_.Finish(); -} - -inline flatbuffers::Offset CreateQuantizationParametersDirect( - flatbuffers::FlatBufferBuilder &_fbb, - const std::vector *min = nullptr, - const std::vector *max = nullptr, - const std::vector *scale = nullptr, - const std::vector *zero_point = nullptr, - tflite::QuantizationDetails details_type = tflite::QuantizationDetails_NONE, - flatbuffers::Offset details = 0, - int32_t quantized_dimension = 0) { - auto min__ = min ? _fbb.CreateVector(*min) : 0; - auto max__ = max ? _fbb.CreateVector(*max) : 0; - auto scale__ = scale ? _fbb.CreateVector(*scale) : 0; - auto zero_point__ = zero_point ? _fbb.CreateVector(*zero_point) : 0; - return tflite::CreateQuantizationParameters( - _fbb, - min__, - max__, - scale__, - zero_point__, - details_type, - details, - quantized_dimension); -} - -flatbuffers::Offset CreateQuantizationParameters(flatbuffers::FlatBufferBuilder &_fbb, const QuantizationParametersT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct Int32VectorT : public flatbuffers::NativeTable { - typedef Int32Vector TableType; - std::vector values; - Int32VectorT() { - } -}; - -struct Int32Vector FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef Int32VectorT NativeTableType; - typedef Int32VectorBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_VALUES = 4 - }; - const flatbuffers::Vector *values() const { - return GetPointer *>(VT_VALUES); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyOffset(verifier, VT_VALUES) && - verifier.VerifyVector(values()) && - verifier.EndTable(); - } - Int32VectorT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(Int32VectorT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const Int32VectorT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct Int32VectorBuilder { - typedef Int32Vector Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_values(flatbuffers::Offset> values) { - fbb_.AddOffset(Int32Vector::VT_VALUES, values); - } - explicit Int32VectorBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - Int32VectorBuilder &operator=(const Int32VectorBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateInt32Vector( - flatbuffers::FlatBufferBuilder &_fbb, - flatbuffers::Offset> values = 0) { - Int32VectorBuilder builder_(_fbb); - builder_.add_values(values); - return builder_.Finish(); -} - -inline flatbuffers::Offset CreateInt32VectorDirect( - flatbuffers::FlatBufferBuilder &_fbb, - const std::vector *values = nullptr) { - auto values__ = values ? _fbb.CreateVector(*values) : 0; - return tflite::CreateInt32Vector( - _fbb, - values__); -} - -flatbuffers::Offset CreateInt32Vector(flatbuffers::FlatBufferBuilder &_fbb, const Int32VectorT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct Uint16VectorT : public flatbuffers::NativeTable { - typedef Uint16Vector TableType; - std::vector values; - Uint16VectorT() { - } -}; - -struct Uint16Vector FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef Uint16VectorT NativeTableType; - typedef Uint16VectorBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_VALUES = 4 - }; - const flatbuffers::Vector *values() const { - return GetPointer *>(VT_VALUES); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyOffset(verifier, VT_VALUES) && - verifier.VerifyVector(values()) && - verifier.EndTable(); - } - Uint16VectorT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(Uint16VectorT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const Uint16VectorT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct Uint16VectorBuilder { - typedef Uint16Vector Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_values(flatbuffers::Offset> values) { - fbb_.AddOffset(Uint16Vector::VT_VALUES, values); - } - explicit Uint16VectorBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - Uint16VectorBuilder &operator=(const Uint16VectorBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateUint16Vector( - flatbuffers::FlatBufferBuilder &_fbb, - flatbuffers::Offset> values = 0) { - Uint16VectorBuilder builder_(_fbb); - builder_.add_values(values); - return builder_.Finish(); -} - -inline flatbuffers::Offset CreateUint16VectorDirect( - flatbuffers::FlatBufferBuilder &_fbb, - const std::vector *values = nullptr) { - if (values) { _fbb.ForceVectorAlignment(values->size(), sizeof(uint16_t), 4); } - auto values__ = values ? _fbb.CreateVector(*values) : 0; - return tflite::CreateUint16Vector( - _fbb, - values__); -} - -flatbuffers::Offset CreateUint16Vector(flatbuffers::FlatBufferBuilder &_fbb, const Uint16VectorT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct Uint8VectorT : public flatbuffers::NativeTable { - typedef Uint8Vector TableType; - std::vector values; - Uint8VectorT() { - } -}; - -struct Uint8Vector FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef Uint8VectorT NativeTableType; - typedef Uint8VectorBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_VALUES = 4 - }; - const flatbuffers::Vector *values() const { - return GetPointer *>(VT_VALUES); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyOffset(verifier, VT_VALUES) && - verifier.VerifyVector(values()) && - verifier.EndTable(); - } - Uint8VectorT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(Uint8VectorT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const Uint8VectorT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct Uint8VectorBuilder { - typedef Uint8Vector Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_values(flatbuffers::Offset> values) { - fbb_.AddOffset(Uint8Vector::VT_VALUES, values); - } - explicit Uint8VectorBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - Uint8VectorBuilder &operator=(const Uint8VectorBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateUint8Vector( - flatbuffers::FlatBufferBuilder &_fbb, - flatbuffers::Offset> values = 0) { - Uint8VectorBuilder builder_(_fbb); - builder_.add_values(values); - return builder_.Finish(); -} - -inline flatbuffers::Offset CreateUint8VectorDirect( - flatbuffers::FlatBufferBuilder &_fbb, - const std::vector *values = nullptr) { - if (values) { _fbb.ForceVectorAlignment(values->size(), sizeof(uint8_t), 4); } - auto values__ = values ? _fbb.CreateVector(*values) : 0; - return tflite::CreateUint8Vector( - _fbb, - values__); -} - -flatbuffers::Offset CreateUint8Vector(flatbuffers::FlatBufferBuilder &_fbb, const Uint8VectorT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct DimensionMetadataT : public flatbuffers::NativeTable { - typedef DimensionMetadata TableType; - tflite::DimensionType format; - int32_t dense_size; - tflite::SparseIndexVectorUnion array_segments; - tflite::SparseIndexVectorUnion array_indices; - DimensionMetadataT() - : format(tflite::DimensionType_DENSE), - dense_size(0) { - } -}; - -struct DimensionMetadata FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef DimensionMetadataT NativeTableType; - typedef DimensionMetadataBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_FORMAT = 4, - VT_DENSE_SIZE = 6, - VT_ARRAY_SEGMENTS_TYPE = 8, - VT_ARRAY_SEGMENTS = 10, - VT_ARRAY_INDICES_TYPE = 12, - VT_ARRAY_INDICES = 14 - }; - tflite::DimensionType format() const { - return static_cast(GetField(VT_FORMAT, 0)); - } - int32_t dense_size() const { - return GetField(VT_DENSE_SIZE, 0); - } - tflite::SparseIndexVector array_segments_type() const { - return static_cast(GetField(VT_ARRAY_SEGMENTS_TYPE, 0)); - } - const void *array_segments() const { - return GetPointer(VT_ARRAY_SEGMENTS); - } - template const T *array_segments_as() const; - const tflite::Int32Vector *array_segments_as_Int32Vector() const { - return array_segments_type() == tflite::SparseIndexVector_Int32Vector ? static_cast(array_segments()) : nullptr; - } - const tflite::Uint16Vector *array_segments_as_Uint16Vector() const { - return array_segments_type() == tflite::SparseIndexVector_Uint16Vector ? static_cast(array_segments()) : nullptr; - } - const tflite::Uint8Vector *array_segments_as_Uint8Vector() const { - return array_segments_type() == tflite::SparseIndexVector_Uint8Vector ? static_cast(array_segments()) : nullptr; - } - tflite::SparseIndexVector array_indices_type() const { - return static_cast(GetField(VT_ARRAY_INDICES_TYPE, 0)); - } - const void *array_indices() const { - return GetPointer(VT_ARRAY_INDICES); - } - template const T *array_indices_as() const; - const tflite::Int32Vector *array_indices_as_Int32Vector() const { - return array_indices_type() == tflite::SparseIndexVector_Int32Vector ? static_cast(array_indices()) : nullptr; - } - const tflite::Uint16Vector *array_indices_as_Uint16Vector() const { - return array_indices_type() == tflite::SparseIndexVector_Uint16Vector ? static_cast(array_indices()) : nullptr; - } - const tflite::Uint8Vector *array_indices_as_Uint8Vector() const { - return array_indices_type() == tflite::SparseIndexVector_Uint8Vector ? static_cast(array_indices()) : nullptr; - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_FORMAT) && - VerifyField(verifier, VT_DENSE_SIZE) && - VerifyField(verifier, VT_ARRAY_SEGMENTS_TYPE) && - VerifyOffset(verifier, VT_ARRAY_SEGMENTS) && - VerifySparseIndexVector(verifier, array_segments(), array_segments_type()) && - VerifyField(verifier, VT_ARRAY_INDICES_TYPE) && - VerifyOffset(verifier, VT_ARRAY_INDICES) && - VerifySparseIndexVector(verifier, array_indices(), array_indices_type()) && - verifier.EndTable(); - } - DimensionMetadataT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(DimensionMetadataT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const DimensionMetadataT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -template<> inline const tflite::Int32Vector *DimensionMetadata::array_segments_as() const { - return array_segments_as_Int32Vector(); -} - -template<> inline const tflite::Uint16Vector *DimensionMetadata::array_segments_as() const { - return array_segments_as_Uint16Vector(); -} - -template<> inline const tflite::Uint8Vector *DimensionMetadata::array_segments_as() const { - return array_segments_as_Uint8Vector(); -} - -template<> inline const tflite::Int32Vector *DimensionMetadata::array_indices_as() const { - return array_indices_as_Int32Vector(); -} - -template<> inline const tflite::Uint16Vector *DimensionMetadata::array_indices_as() const { - return array_indices_as_Uint16Vector(); -} - -template<> inline const tflite::Uint8Vector *DimensionMetadata::array_indices_as() const { - return array_indices_as_Uint8Vector(); -} - -struct DimensionMetadataBuilder { - typedef DimensionMetadata Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_format(tflite::DimensionType format) { - fbb_.AddElement(DimensionMetadata::VT_FORMAT, static_cast(format), 0); - } - void add_dense_size(int32_t dense_size) { - fbb_.AddElement(DimensionMetadata::VT_DENSE_SIZE, dense_size, 0); - } - void add_array_segments_type(tflite::SparseIndexVector array_segments_type) { - fbb_.AddElement(DimensionMetadata::VT_ARRAY_SEGMENTS_TYPE, static_cast(array_segments_type), 0); - } - void add_array_segments(flatbuffers::Offset array_segments) { - fbb_.AddOffset(DimensionMetadata::VT_ARRAY_SEGMENTS, array_segments); - } - void add_array_indices_type(tflite::SparseIndexVector array_indices_type) { - fbb_.AddElement(DimensionMetadata::VT_ARRAY_INDICES_TYPE, static_cast(array_indices_type), 0); - } - void add_array_indices(flatbuffers::Offset array_indices) { - fbb_.AddOffset(DimensionMetadata::VT_ARRAY_INDICES, array_indices); - } - explicit DimensionMetadataBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - DimensionMetadataBuilder &operator=(const DimensionMetadataBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateDimensionMetadata( - flatbuffers::FlatBufferBuilder &_fbb, - tflite::DimensionType format = tflite::DimensionType_DENSE, - int32_t dense_size = 0, - tflite::SparseIndexVector array_segments_type = tflite::SparseIndexVector_NONE, - flatbuffers::Offset array_segments = 0, - tflite::SparseIndexVector array_indices_type = tflite::SparseIndexVector_NONE, - flatbuffers::Offset array_indices = 0) { - DimensionMetadataBuilder builder_(_fbb); - builder_.add_array_indices(array_indices); - builder_.add_array_segments(array_segments); - builder_.add_dense_size(dense_size); - builder_.add_array_indices_type(array_indices_type); - builder_.add_array_segments_type(array_segments_type); - builder_.add_format(format); - return builder_.Finish(); -} - -flatbuffers::Offset CreateDimensionMetadata(flatbuffers::FlatBufferBuilder &_fbb, const DimensionMetadataT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct SparsityParametersT : public flatbuffers::NativeTable { - typedef SparsityParameters TableType; - std::vector traversal_order; - std::vector block_map; - std::vector> dim_metadata; - SparsityParametersT() { - } -}; - -struct SparsityParameters FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef SparsityParametersT NativeTableType; - typedef SparsityParametersBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_TRAVERSAL_ORDER = 4, - VT_BLOCK_MAP = 6, - VT_DIM_METADATA = 8 - }; - const flatbuffers::Vector *traversal_order() const { - return GetPointer *>(VT_TRAVERSAL_ORDER); - } - const flatbuffers::Vector *block_map() const { - return GetPointer *>(VT_BLOCK_MAP); - } - const flatbuffers::Vector> *dim_metadata() const { - return GetPointer> *>(VT_DIM_METADATA); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyOffset(verifier, VT_TRAVERSAL_ORDER) && - verifier.VerifyVector(traversal_order()) && - VerifyOffset(verifier, VT_BLOCK_MAP) && - verifier.VerifyVector(block_map()) && - VerifyOffset(verifier, VT_DIM_METADATA) && - verifier.VerifyVector(dim_metadata()) && - verifier.VerifyVectorOfTables(dim_metadata()) && - verifier.EndTable(); - } - SparsityParametersT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(SparsityParametersT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const SparsityParametersT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct SparsityParametersBuilder { - typedef SparsityParameters Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_traversal_order(flatbuffers::Offset> traversal_order) { - fbb_.AddOffset(SparsityParameters::VT_TRAVERSAL_ORDER, traversal_order); - } - void add_block_map(flatbuffers::Offset> block_map) { - fbb_.AddOffset(SparsityParameters::VT_BLOCK_MAP, block_map); - } - void add_dim_metadata(flatbuffers::Offset>> dim_metadata) { - fbb_.AddOffset(SparsityParameters::VT_DIM_METADATA, dim_metadata); - } - explicit SparsityParametersBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - SparsityParametersBuilder &operator=(const SparsityParametersBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateSparsityParameters( - flatbuffers::FlatBufferBuilder &_fbb, - flatbuffers::Offset> traversal_order = 0, - flatbuffers::Offset> block_map = 0, - flatbuffers::Offset>> dim_metadata = 0) { - SparsityParametersBuilder builder_(_fbb); - builder_.add_dim_metadata(dim_metadata); - builder_.add_block_map(block_map); - builder_.add_traversal_order(traversal_order); - return builder_.Finish(); -} - -inline flatbuffers::Offset CreateSparsityParametersDirect( - flatbuffers::FlatBufferBuilder &_fbb, - const std::vector *traversal_order = nullptr, - const std::vector *block_map = nullptr, - const std::vector> *dim_metadata = nullptr) { - auto traversal_order__ = traversal_order ? _fbb.CreateVector(*traversal_order) : 0; - auto block_map__ = block_map ? _fbb.CreateVector(*block_map) : 0; - auto dim_metadata__ = dim_metadata ? _fbb.CreateVector>(*dim_metadata) : 0; - return tflite::CreateSparsityParameters( - _fbb, - traversal_order__, - block_map__, - dim_metadata__); -} - -flatbuffers::Offset CreateSparsityParameters(flatbuffers::FlatBufferBuilder &_fbb, const SparsityParametersT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct TensorT : public flatbuffers::NativeTable { - typedef Tensor TableType; - std::vector shape; - tflite::TensorType type; - uint32_t buffer; - std::string name; - std::unique_ptr quantization; - bool is_variable; - std::unique_ptr sparsity; - std::vector shape_signature; - bool has_rank; - TensorT() - : type(tflite::TensorType_FLOAT32), - buffer(0), - is_variable(false), - has_rank(false) { - } -}; - -struct Tensor FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef TensorT NativeTableType; - typedef TensorBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_SHAPE = 4, - VT_TYPE = 6, - VT_BUFFER = 8, - VT_NAME = 10, - VT_QUANTIZATION = 12, - VT_IS_VARIABLE = 14, - VT_SPARSITY = 16, - VT_SHAPE_SIGNATURE = 18, - VT_HAS_RANK = 20 - }; - const flatbuffers::Vector *shape() const { - return GetPointer *>(VT_SHAPE); - } - tflite::TensorType type() const { - return static_cast(GetField(VT_TYPE, 0)); - } - uint32_t buffer() const { - return GetField(VT_BUFFER, 0); - } - const flatbuffers::String *name() const { - return GetPointer(VT_NAME); - } - const tflite::QuantizationParameters *quantization() const { - return GetPointer(VT_QUANTIZATION); - } - bool is_variable() const { - return GetField(VT_IS_VARIABLE, 0) != 0; - } - const tflite::SparsityParameters *sparsity() const { - return GetPointer(VT_SPARSITY); - } - const flatbuffers::Vector *shape_signature() const { - return GetPointer *>(VT_SHAPE_SIGNATURE); - } - bool has_rank() const { - return GetField(VT_HAS_RANK, 0) != 0; - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyOffset(verifier, VT_SHAPE) && - verifier.VerifyVector(shape()) && - VerifyField(verifier, VT_TYPE) && - VerifyField(verifier, VT_BUFFER) && - VerifyOffset(verifier, VT_NAME) && - verifier.VerifyString(name()) && - VerifyOffset(verifier, VT_QUANTIZATION) && - verifier.VerifyTable(quantization()) && - VerifyField(verifier, VT_IS_VARIABLE) && - VerifyOffset(verifier, VT_SPARSITY) && - verifier.VerifyTable(sparsity()) && - VerifyOffset(verifier, VT_SHAPE_SIGNATURE) && - verifier.VerifyVector(shape_signature()) && - VerifyField(verifier, VT_HAS_RANK) && - verifier.EndTable(); - } - TensorT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(TensorT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const TensorT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct TensorBuilder { - typedef Tensor Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_shape(flatbuffers::Offset> shape) { - fbb_.AddOffset(Tensor::VT_SHAPE, shape); - } - void add_type(tflite::TensorType type) { - fbb_.AddElement(Tensor::VT_TYPE, static_cast(type), 0); - } - void add_buffer(uint32_t buffer) { - fbb_.AddElement(Tensor::VT_BUFFER, buffer, 0); - } - void add_name(flatbuffers::Offset name) { - fbb_.AddOffset(Tensor::VT_NAME, name); - } - void add_quantization(flatbuffers::Offset quantization) { - fbb_.AddOffset(Tensor::VT_QUANTIZATION, quantization); - } - void add_is_variable(bool is_variable) { - fbb_.AddElement(Tensor::VT_IS_VARIABLE, static_cast(is_variable), 0); - } - void add_sparsity(flatbuffers::Offset sparsity) { - fbb_.AddOffset(Tensor::VT_SPARSITY, sparsity); - } - void add_shape_signature(flatbuffers::Offset> shape_signature) { - fbb_.AddOffset(Tensor::VT_SHAPE_SIGNATURE, shape_signature); - } - void add_has_rank(bool has_rank) { - fbb_.AddElement(Tensor::VT_HAS_RANK, static_cast(has_rank), 0); - } - explicit TensorBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - TensorBuilder &operator=(const TensorBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateTensor( - flatbuffers::FlatBufferBuilder &_fbb, - flatbuffers::Offset> shape = 0, - tflite::TensorType type = tflite::TensorType_FLOAT32, - uint32_t buffer = 0, - flatbuffers::Offset name = 0, - flatbuffers::Offset quantization = 0, - bool is_variable = false, - flatbuffers::Offset sparsity = 0, - flatbuffers::Offset> shape_signature = 0, - bool has_rank = false) { - TensorBuilder builder_(_fbb); - builder_.add_shape_signature(shape_signature); - builder_.add_sparsity(sparsity); - builder_.add_quantization(quantization); - builder_.add_name(name); - builder_.add_buffer(buffer); - builder_.add_shape(shape); - builder_.add_has_rank(has_rank); - builder_.add_is_variable(is_variable); - builder_.add_type(type); - return builder_.Finish(); -} - -inline flatbuffers::Offset CreateTensorDirect( - flatbuffers::FlatBufferBuilder &_fbb, - const std::vector *shape = nullptr, - tflite::TensorType type = tflite::TensorType_FLOAT32, - uint32_t buffer = 0, - const char *name = nullptr, - flatbuffers::Offset quantization = 0, - bool is_variable = false, - flatbuffers::Offset sparsity = 0, - const std::vector *shape_signature = nullptr, - bool has_rank = false) { - auto shape__ = shape ? _fbb.CreateVector(*shape) : 0; - auto name__ = name ? _fbb.CreateString(name) : 0; - auto shape_signature__ = shape_signature ? _fbb.CreateVector(*shape_signature) : 0; - return tflite::CreateTensor( - _fbb, - shape__, - type, - buffer, - name__, - quantization, - is_variable, - sparsity, - shape_signature__, - has_rank); -} - -flatbuffers::Offset CreateTensor(flatbuffers::FlatBufferBuilder &_fbb, const TensorT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct Conv2DOptionsT : public flatbuffers::NativeTable { - typedef Conv2DOptions TableType; - tflite::Padding padding; - int32_t stride_w; - int32_t stride_h; - tflite::ActivationFunctionType fused_activation_function; - int32_t dilation_w_factor; - int32_t dilation_h_factor; - Conv2DOptionsT() - : padding(tflite::Padding_SAME), - stride_w(0), - stride_h(0), - fused_activation_function(tflite::ActivationFunctionType_NONE), - dilation_w_factor(1), - dilation_h_factor(1) { - } -}; - -struct Conv2DOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef Conv2DOptionsT NativeTableType; - typedef Conv2DOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_PADDING = 4, - VT_STRIDE_W = 6, - VT_STRIDE_H = 8, - VT_FUSED_ACTIVATION_FUNCTION = 10, - VT_DILATION_W_FACTOR = 12, - VT_DILATION_H_FACTOR = 14 - }; - tflite::Padding padding() const { - return static_cast(GetField(VT_PADDING, 0)); - } - int32_t stride_w() const { - return GetField(VT_STRIDE_W, 0); - } - int32_t stride_h() const { - return GetField(VT_STRIDE_H, 0); - } - tflite::ActivationFunctionType fused_activation_function() const { - return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); - } - int32_t dilation_w_factor() const { - return GetField(VT_DILATION_W_FACTOR, 1); - } - int32_t dilation_h_factor() const { - return GetField(VT_DILATION_H_FACTOR, 1); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_PADDING) && - VerifyField(verifier, VT_STRIDE_W) && - VerifyField(verifier, VT_STRIDE_H) && - VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION) && - VerifyField(verifier, VT_DILATION_W_FACTOR) && - VerifyField(verifier, VT_DILATION_H_FACTOR) && - verifier.EndTable(); - } - Conv2DOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(Conv2DOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const Conv2DOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct Conv2DOptionsBuilder { - typedef Conv2DOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_padding(tflite::Padding padding) { - fbb_.AddElement(Conv2DOptions::VT_PADDING, static_cast(padding), 0); - } - void add_stride_w(int32_t stride_w) { - fbb_.AddElement(Conv2DOptions::VT_STRIDE_W, stride_w, 0); - } - void add_stride_h(int32_t stride_h) { - fbb_.AddElement(Conv2DOptions::VT_STRIDE_H, stride_h, 0); - } - void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { - fbb_.AddElement(Conv2DOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); - } - void add_dilation_w_factor(int32_t dilation_w_factor) { - fbb_.AddElement(Conv2DOptions::VT_DILATION_W_FACTOR, dilation_w_factor, 1); - } - void add_dilation_h_factor(int32_t dilation_h_factor) { - fbb_.AddElement(Conv2DOptions::VT_DILATION_H_FACTOR, dilation_h_factor, 1); - } - explicit Conv2DOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - Conv2DOptionsBuilder &operator=(const Conv2DOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateConv2DOptions( - flatbuffers::FlatBufferBuilder &_fbb, - tflite::Padding padding = tflite::Padding_SAME, - int32_t stride_w = 0, - int32_t stride_h = 0, - tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE, - int32_t dilation_w_factor = 1, - int32_t dilation_h_factor = 1) { - Conv2DOptionsBuilder builder_(_fbb); - builder_.add_dilation_h_factor(dilation_h_factor); - builder_.add_dilation_w_factor(dilation_w_factor); - builder_.add_stride_h(stride_h); - builder_.add_stride_w(stride_w); - builder_.add_fused_activation_function(fused_activation_function); - builder_.add_padding(padding); - return builder_.Finish(); -} - -flatbuffers::Offset CreateConv2DOptions(flatbuffers::FlatBufferBuilder &_fbb, const Conv2DOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct Conv3DOptionsT : public flatbuffers::NativeTable { - typedef Conv3DOptions TableType; - tflite::Padding padding; - int32_t stride_d; - int32_t stride_w; - int32_t stride_h; - tflite::ActivationFunctionType fused_activation_function; - int32_t dilation_d_factor; - int32_t dilation_w_factor; - int32_t dilation_h_factor; - Conv3DOptionsT() - : padding(tflite::Padding_SAME), - stride_d(0), - stride_w(0), - stride_h(0), - fused_activation_function(tflite::ActivationFunctionType_NONE), - dilation_d_factor(1), - dilation_w_factor(1), - dilation_h_factor(1) { - } -}; - -struct Conv3DOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef Conv3DOptionsT NativeTableType; - typedef Conv3DOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_PADDING = 4, - VT_STRIDE_D = 6, - VT_STRIDE_W = 8, - VT_STRIDE_H = 10, - VT_FUSED_ACTIVATION_FUNCTION = 12, - VT_DILATION_D_FACTOR = 14, - VT_DILATION_W_FACTOR = 16, - VT_DILATION_H_FACTOR = 18 - }; - tflite::Padding padding() const { - return static_cast(GetField(VT_PADDING, 0)); - } - int32_t stride_d() const { - return GetField(VT_STRIDE_D, 0); - } - int32_t stride_w() const { - return GetField(VT_STRIDE_W, 0); - } - int32_t stride_h() const { - return GetField(VT_STRIDE_H, 0); - } - tflite::ActivationFunctionType fused_activation_function() const { - return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); - } - int32_t dilation_d_factor() const { - return GetField(VT_DILATION_D_FACTOR, 1); - } - int32_t dilation_w_factor() const { - return GetField(VT_DILATION_W_FACTOR, 1); - } - int32_t dilation_h_factor() const { - return GetField(VT_DILATION_H_FACTOR, 1); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_PADDING) && - VerifyField(verifier, VT_STRIDE_D) && - VerifyField(verifier, VT_STRIDE_W) && - VerifyField(verifier, VT_STRIDE_H) && - VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION) && - VerifyField(verifier, VT_DILATION_D_FACTOR) && - VerifyField(verifier, VT_DILATION_W_FACTOR) && - VerifyField(verifier, VT_DILATION_H_FACTOR) && - verifier.EndTable(); - } - Conv3DOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(Conv3DOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const Conv3DOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct Conv3DOptionsBuilder { - typedef Conv3DOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_padding(tflite::Padding padding) { - fbb_.AddElement(Conv3DOptions::VT_PADDING, static_cast(padding), 0); - } - void add_stride_d(int32_t stride_d) { - fbb_.AddElement(Conv3DOptions::VT_STRIDE_D, stride_d, 0); - } - void add_stride_w(int32_t stride_w) { - fbb_.AddElement(Conv3DOptions::VT_STRIDE_W, stride_w, 0); - } - void add_stride_h(int32_t stride_h) { - fbb_.AddElement(Conv3DOptions::VT_STRIDE_H, stride_h, 0); - } - void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { - fbb_.AddElement(Conv3DOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); - } - void add_dilation_d_factor(int32_t dilation_d_factor) { - fbb_.AddElement(Conv3DOptions::VT_DILATION_D_FACTOR, dilation_d_factor, 1); - } - void add_dilation_w_factor(int32_t dilation_w_factor) { - fbb_.AddElement(Conv3DOptions::VT_DILATION_W_FACTOR, dilation_w_factor, 1); - } - void add_dilation_h_factor(int32_t dilation_h_factor) { - fbb_.AddElement(Conv3DOptions::VT_DILATION_H_FACTOR, dilation_h_factor, 1); - } - explicit Conv3DOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - Conv3DOptionsBuilder &operator=(const Conv3DOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateConv3DOptions( - flatbuffers::FlatBufferBuilder &_fbb, - tflite::Padding padding = tflite::Padding_SAME, - int32_t stride_d = 0, - int32_t stride_w = 0, - int32_t stride_h = 0, - tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE, - int32_t dilation_d_factor = 1, - int32_t dilation_w_factor = 1, - int32_t dilation_h_factor = 1) { - Conv3DOptionsBuilder builder_(_fbb); - builder_.add_dilation_h_factor(dilation_h_factor); - builder_.add_dilation_w_factor(dilation_w_factor); - builder_.add_dilation_d_factor(dilation_d_factor); - builder_.add_stride_h(stride_h); - builder_.add_stride_w(stride_w); - builder_.add_stride_d(stride_d); - builder_.add_fused_activation_function(fused_activation_function); - builder_.add_padding(padding); - return builder_.Finish(); -} - -flatbuffers::Offset CreateConv3DOptions(flatbuffers::FlatBufferBuilder &_fbb, const Conv3DOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct Pool2DOptionsT : public flatbuffers::NativeTable { - typedef Pool2DOptions TableType; - tflite::Padding padding; - int32_t stride_w; - int32_t stride_h; - int32_t filter_width; - int32_t filter_height; - tflite::ActivationFunctionType fused_activation_function; - Pool2DOptionsT() - : padding(tflite::Padding_SAME), - stride_w(0), - stride_h(0), - filter_width(0), - filter_height(0), - fused_activation_function(tflite::ActivationFunctionType_NONE) { - } -}; - -struct Pool2DOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef Pool2DOptionsT NativeTableType; - typedef Pool2DOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_PADDING = 4, - VT_STRIDE_W = 6, - VT_STRIDE_H = 8, - VT_FILTER_WIDTH = 10, - VT_FILTER_HEIGHT = 12, - VT_FUSED_ACTIVATION_FUNCTION = 14 - }; - tflite::Padding padding() const { - return static_cast(GetField(VT_PADDING, 0)); - } - int32_t stride_w() const { - return GetField(VT_STRIDE_W, 0); - } - int32_t stride_h() const { - return GetField(VT_STRIDE_H, 0); - } - int32_t filter_width() const { - return GetField(VT_FILTER_WIDTH, 0); - } - int32_t filter_height() const { - return GetField(VT_FILTER_HEIGHT, 0); - } - tflite::ActivationFunctionType fused_activation_function() const { - return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_PADDING) && - VerifyField(verifier, VT_STRIDE_W) && - VerifyField(verifier, VT_STRIDE_H) && - VerifyField(verifier, VT_FILTER_WIDTH) && - VerifyField(verifier, VT_FILTER_HEIGHT) && - VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION) && - verifier.EndTable(); - } - Pool2DOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(Pool2DOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const Pool2DOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct Pool2DOptionsBuilder { - typedef Pool2DOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_padding(tflite::Padding padding) { - fbb_.AddElement(Pool2DOptions::VT_PADDING, static_cast(padding), 0); - } - void add_stride_w(int32_t stride_w) { - fbb_.AddElement(Pool2DOptions::VT_STRIDE_W, stride_w, 0); - } - void add_stride_h(int32_t stride_h) { - fbb_.AddElement(Pool2DOptions::VT_STRIDE_H, stride_h, 0); - } - void add_filter_width(int32_t filter_width) { - fbb_.AddElement(Pool2DOptions::VT_FILTER_WIDTH, filter_width, 0); - } - void add_filter_height(int32_t filter_height) { - fbb_.AddElement(Pool2DOptions::VT_FILTER_HEIGHT, filter_height, 0); - } - void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { - fbb_.AddElement(Pool2DOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); - } - explicit Pool2DOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - Pool2DOptionsBuilder &operator=(const Pool2DOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreatePool2DOptions( - flatbuffers::FlatBufferBuilder &_fbb, - tflite::Padding padding = tflite::Padding_SAME, - int32_t stride_w = 0, - int32_t stride_h = 0, - int32_t filter_width = 0, - int32_t filter_height = 0, - tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE) { - Pool2DOptionsBuilder builder_(_fbb); - builder_.add_filter_height(filter_height); - builder_.add_filter_width(filter_width); - builder_.add_stride_h(stride_h); - builder_.add_stride_w(stride_w); - builder_.add_fused_activation_function(fused_activation_function); - builder_.add_padding(padding); - return builder_.Finish(); -} - -flatbuffers::Offset CreatePool2DOptions(flatbuffers::FlatBufferBuilder &_fbb, const Pool2DOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct DepthwiseConv2DOptionsT : public flatbuffers::NativeTable { - typedef DepthwiseConv2DOptions TableType; - tflite::Padding padding; - int32_t stride_w; - int32_t stride_h; - int32_t depth_multiplier; - tflite::ActivationFunctionType fused_activation_function; - int32_t dilation_w_factor; - int32_t dilation_h_factor; - DepthwiseConv2DOptionsT() - : padding(tflite::Padding_SAME), - stride_w(0), - stride_h(0), - depth_multiplier(0), - fused_activation_function(tflite::ActivationFunctionType_NONE), - dilation_w_factor(1), - dilation_h_factor(1) { - } -}; - -struct DepthwiseConv2DOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef DepthwiseConv2DOptionsT NativeTableType; - typedef DepthwiseConv2DOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_PADDING = 4, - VT_STRIDE_W = 6, - VT_STRIDE_H = 8, - VT_DEPTH_MULTIPLIER = 10, - VT_FUSED_ACTIVATION_FUNCTION = 12, - VT_DILATION_W_FACTOR = 14, - VT_DILATION_H_FACTOR = 16 - }; - tflite::Padding padding() const { - return static_cast(GetField(VT_PADDING, 0)); - } - int32_t stride_w() const { - return GetField(VT_STRIDE_W, 0); - } - int32_t stride_h() const { - return GetField(VT_STRIDE_H, 0); - } - int32_t depth_multiplier() const { - return GetField(VT_DEPTH_MULTIPLIER, 0); - } - tflite::ActivationFunctionType fused_activation_function() const { - return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); - } - int32_t dilation_w_factor() const { - return GetField(VT_DILATION_W_FACTOR, 1); - } - int32_t dilation_h_factor() const { - return GetField(VT_DILATION_H_FACTOR, 1); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_PADDING) && - VerifyField(verifier, VT_STRIDE_W) && - VerifyField(verifier, VT_STRIDE_H) && - VerifyField(verifier, VT_DEPTH_MULTIPLIER) && - VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION) && - VerifyField(verifier, VT_DILATION_W_FACTOR) && - VerifyField(verifier, VT_DILATION_H_FACTOR) && - verifier.EndTable(); - } - DepthwiseConv2DOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(DepthwiseConv2DOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const DepthwiseConv2DOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct DepthwiseConv2DOptionsBuilder { - typedef DepthwiseConv2DOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_padding(tflite::Padding padding) { - fbb_.AddElement(DepthwiseConv2DOptions::VT_PADDING, static_cast(padding), 0); - } - void add_stride_w(int32_t stride_w) { - fbb_.AddElement(DepthwiseConv2DOptions::VT_STRIDE_W, stride_w, 0); - } - void add_stride_h(int32_t stride_h) { - fbb_.AddElement(DepthwiseConv2DOptions::VT_STRIDE_H, stride_h, 0); - } - void add_depth_multiplier(int32_t depth_multiplier) { - fbb_.AddElement(DepthwiseConv2DOptions::VT_DEPTH_MULTIPLIER, depth_multiplier, 0); - } - void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { - fbb_.AddElement(DepthwiseConv2DOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); - } - void add_dilation_w_factor(int32_t dilation_w_factor) { - fbb_.AddElement(DepthwiseConv2DOptions::VT_DILATION_W_FACTOR, dilation_w_factor, 1); - } - void add_dilation_h_factor(int32_t dilation_h_factor) { - fbb_.AddElement(DepthwiseConv2DOptions::VT_DILATION_H_FACTOR, dilation_h_factor, 1); - } - explicit DepthwiseConv2DOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - DepthwiseConv2DOptionsBuilder &operator=(const DepthwiseConv2DOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateDepthwiseConv2DOptions( - flatbuffers::FlatBufferBuilder &_fbb, - tflite::Padding padding = tflite::Padding_SAME, - int32_t stride_w = 0, - int32_t stride_h = 0, - int32_t depth_multiplier = 0, - tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE, - int32_t dilation_w_factor = 1, - int32_t dilation_h_factor = 1) { - DepthwiseConv2DOptionsBuilder builder_(_fbb); - builder_.add_dilation_h_factor(dilation_h_factor); - builder_.add_dilation_w_factor(dilation_w_factor); - builder_.add_depth_multiplier(depth_multiplier); - builder_.add_stride_h(stride_h); - builder_.add_stride_w(stride_w); - builder_.add_fused_activation_function(fused_activation_function); - builder_.add_padding(padding); - return builder_.Finish(); -} - -flatbuffers::Offset CreateDepthwiseConv2DOptions(flatbuffers::FlatBufferBuilder &_fbb, const DepthwiseConv2DOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct ConcatEmbeddingsOptionsT : public flatbuffers::NativeTable { - typedef ConcatEmbeddingsOptions TableType; - int32_t num_channels; - std::vector num_columns_per_channel; - std::vector embedding_dim_per_channel; - ConcatEmbeddingsOptionsT() - : num_channels(0) { - } -}; - -struct ConcatEmbeddingsOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef ConcatEmbeddingsOptionsT NativeTableType; - typedef ConcatEmbeddingsOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_NUM_CHANNELS = 4, - VT_NUM_COLUMNS_PER_CHANNEL = 6, - VT_EMBEDDING_DIM_PER_CHANNEL = 8 - }; - int32_t num_channels() const { - return GetField(VT_NUM_CHANNELS, 0); - } - const flatbuffers::Vector *num_columns_per_channel() const { - return GetPointer *>(VT_NUM_COLUMNS_PER_CHANNEL); - } - const flatbuffers::Vector *embedding_dim_per_channel() const { - return GetPointer *>(VT_EMBEDDING_DIM_PER_CHANNEL); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_NUM_CHANNELS) && - VerifyOffset(verifier, VT_NUM_COLUMNS_PER_CHANNEL) && - verifier.VerifyVector(num_columns_per_channel()) && - VerifyOffset(verifier, VT_EMBEDDING_DIM_PER_CHANNEL) && - verifier.VerifyVector(embedding_dim_per_channel()) && - verifier.EndTable(); - } - ConcatEmbeddingsOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(ConcatEmbeddingsOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const ConcatEmbeddingsOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct ConcatEmbeddingsOptionsBuilder { - typedef ConcatEmbeddingsOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_num_channels(int32_t num_channels) { - fbb_.AddElement(ConcatEmbeddingsOptions::VT_NUM_CHANNELS, num_channels, 0); - } - void add_num_columns_per_channel(flatbuffers::Offset> num_columns_per_channel) { - fbb_.AddOffset(ConcatEmbeddingsOptions::VT_NUM_COLUMNS_PER_CHANNEL, num_columns_per_channel); - } - void add_embedding_dim_per_channel(flatbuffers::Offset> embedding_dim_per_channel) { - fbb_.AddOffset(ConcatEmbeddingsOptions::VT_EMBEDDING_DIM_PER_CHANNEL, embedding_dim_per_channel); - } - explicit ConcatEmbeddingsOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - ConcatEmbeddingsOptionsBuilder &operator=(const ConcatEmbeddingsOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateConcatEmbeddingsOptions( - flatbuffers::FlatBufferBuilder &_fbb, - int32_t num_channels = 0, - flatbuffers::Offset> num_columns_per_channel = 0, - flatbuffers::Offset> embedding_dim_per_channel = 0) { - ConcatEmbeddingsOptionsBuilder builder_(_fbb); - builder_.add_embedding_dim_per_channel(embedding_dim_per_channel); - builder_.add_num_columns_per_channel(num_columns_per_channel); - builder_.add_num_channels(num_channels); - return builder_.Finish(); -} - -inline flatbuffers::Offset CreateConcatEmbeddingsOptionsDirect( - flatbuffers::FlatBufferBuilder &_fbb, - int32_t num_channels = 0, - const std::vector *num_columns_per_channel = nullptr, - const std::vector *embedding_dim_per_channel = nullptr) { - auto num_columns_per_channel__ = num_columns_per_channel ? _fbb.CreateVector(*num_columns_per_channel) : 0; - auto embedding_dim_per_channel__ = embedding_dim_per_channel ? _fbb.CreateVector(*embedding_dim_per_channel) : 0; - return tflite::CreateConcatEmbeddingsOptions( - _fbb, - num_channels, - num_columns_per_channel__, - embedding_dim_per_channel__); -} - -flatbuffers::Offset CreateConcatEmbeddingsOptions(flatbuffers::FlatBufferBuilder &_fbb, const ConcatEmbeddingsOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct LSHProjectionOptionsT : public flatbuffers::NativeTable { - typedef LSHProjectionOptions TableType; - tflite::LSHProjectionType type; - LSHProjectionOptionsT() - : type(tflite::LSHProjectionType_UNKNOWN) { - } -}; - -struct LSHProjectionOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef LSHProjectionOptionsT NativeTableType; - typedef LSHProjectionOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_TYPE = 4 - }; - tflite::LSHProjectionType type() const { - return static_cast(GetField(VT_TYPE, 0)); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_TYPE) && - verifier.EndTable(); - } - LSHProjectionOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(LSHProjectionOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const LSHProjectionOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct LSHProjectionOptionsBuilder { - typedef LSHProjectionOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_type(tflite::LSHProjectionType type) { - fbb_.AddElement(LSHProjectionOptions::VT_TYPE, static_cast(type), 0); - } - explicit LSHProjectionOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - LSHProjectionOptionsBuilder &operator=(const LSHProjectionOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateLSHProjectionOptions( - flatbuffers::FlatBufferBuilder &_fbb, - tflite::LSHProjectionType type = tflite::LSHProjectionType_UNKNOWN) { - LSHProjectionOptionsBuilder builder_(_fbb); - builder_.add_type(type); - return builder_.Finish(); -} - -flatbuffers::Offset CreateLSHProjectionOptions(flatbuffers::FlatBufferBuilder &_fbb, const LSHProjectionOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct SVDFOptionsT : public flatbuffers::NativeTable { - typedef SVDFOptions TableType; - int32_t rank; - tflite::ActivationFunctionType fused_activation_function; - bool asymmetric_quantize_inputs; - SVDFOptionsT() - : rank(0), - fused_activation_function(tflite::ActivationFunctionType_NONE), - asymmetric_quantize_inputs(false) { - } -}; - -struct SVDFOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef SVDFOptionsT NativeTableType; - typedef SVDFOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_RANK = 4, - VT_FUSED_ACTIVATION_FUNCTION = 6, - VT_ASYMMETRIC_QUANTIZE_INPUTS = 8 - }; - int32_t rank() const { - return GetField(VT_RANK, 0); - } - tflite::ActivationFunctionType fused_activation_function() const { - return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); - } - bool asymmetric_quantize_inputs() const { - return GetField(VT_ASYMMETRIC_QUANTIZE_INPUTS, 0) != 0; - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_RANK) && - VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION) && - VerifyField(verifier, VT_ASYMMETRIC_QUANTIZE_INPUTS) && - verifier.EndTable(); - } - SVDFOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(SVDFOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const SVDFOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct SVDFOptionsBuilder { - typedef SVDFOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_rank(int32_t rank) { - fbb_.AddElement(SVDFOptions::VT_RANK, rank, 0); - } - void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { - fbb_.AddElement(SVDFOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); - } - void add_asymmetric_quantize_inputs(bool asymmetric_quantize_inputs) { - fbb_.AddElement(SVDFOptions::VT_ASYMMETRIC_QUANTIZE_INPUTS, static_cast(asymmetric_quantize_inputs), 0); - } - explicit SVDFOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - SVDFOptionsBuilder &operator=(const SVDFOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateSVDFOptions( - flatbuffers::FlatBufferBuilder &_fbb, - int32_t rank = 0, - tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE, - bool asymmetric_quantize_inputs = false) { - SVDFOptionsBuilder builder_(_fbb); - builder_.add_rank(rank); - builder_.add_asymmetric_quantize_inputs(asymmetric_quantize_inputs); - builder_.add_fused_activation_function(fused_activation_function); - return builder_.Finish(); -} - -flatbuffers::Offset CreateSVDFOptions(flatbuffers::FlatBufferBuilder &_fbb, const SVDFOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct RNNOptionsT : public flatbuffers::NativeTable { - typedef RNNOptions TableType; - tflite::ActivationFunctionType fused_activation_function; - bool asymmetric_quantize_inputs; - RNNOptionsT() - : fused_activation_function(tflite::ActivationFunctionType_NONE), - asymmetric_quantize_inputs(false) { - } -}; - -struct RNNOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef RNNOptionsT NativeTableType; - typedef RNNOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_FUSED_ACTIVATION_FUNCTION = 4, - VT_ASYMMETRIC_QUANTIZE_INPUTS = 6 - }; - tflite::ActivationFunctionType fused_activation_function() const { - return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); - } - bool asymmetric_quantize_inputs() const { - return GetField(VT_ASYMMETRIC_QUANTIZE_INPUTS, 0) != 0; - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION) && - VerifyField(verifier, VT_ASYMMETRIC_QUANTIZE_INPUTS) && - verifier.EndTable(); - } - RNNOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(RNNOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const RNNOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct RNNOptionsBuilder { - typedef RNNOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { - fbb_.AddElement(RNNOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); - } - void add_asymmetric_quantize_inputs(bool asymmetric_quantize_inputs) { - fbb_.AddElement(RNNOptions::VT_ASYMMETRIC_QUANTIZE_INPUTS, static_cast(asymmetric_quantize_inputs), 0); - } - explicit RNNOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - RNNOptionsBuilder &operator=(const RNNOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateRNNOptions( - flatbuffers::FlatBufferBuilder &_fbb, - tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE, - bool asymmetric_quantize_inputs = false) { - RNNOptionsBuilder builder_(_fbb); - builder_.add_asymmetric_quantize_inputs(asymmetric_quantize_inputs); - builder_.add_fused_activation_function(fused_activation_function); - return builder_.Finish(); -} - -flatbuffers::Offset CreateRNNOptions(flatbuffers::FlatBufferBuilder &_fbb, const RNNOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct SequenceRNNOptionsT : public flatbuffers::NativeTable { - typedef SequenceRNNOptions TableType; - bool time_major; - tflite::ActivationFunctionType fused_activation_function; - bool asymmetric_quantize_inputs; - SequenceRNNOptionsT() - : time_major(false), - fused_activation_function(tflite::ActivationFunctionType_NONE), - asymmetric_quantize_inputs(false) { - } -}; - -struct SequenceRNNOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef SequenceRNNOptionsT NativeTableType; - typedef SequenceRNNOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_TIME_MAJOR = 4, - VT_FUSED_ACTIVATION_FUNCTION = 6, - VT_ASYMMETRIC_QUANTIZE_INPUTS = 8 - }; - bool time_major() const { - return GetField(VT_TIME_MAJOR, 0) != 0; - } - tflite::ActivationFunctionType fused_activation_function() const { - return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); - } - bool asymmetric_quantize_inputs() const { - return GetField(VT_ASYMMETRIC_QUANTIZE_INPUTS, 0) != 0; - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_TIME_MAJOR) && - VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION) && - VerifyField(verifier, VT_ASYMMETRIC_QUANTIZE_INPUTS) && - verifier.EndTable(); - } - SequenceRNNOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(SequenceRNNOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const SequenceRNNOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct SequenceRNNOptionsBuilder { - typedef SequenceRNNOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_time_major(bool time_major) { - fbb_.AddElement(SequenceRNNOptions::VT_TIME_MAJOR, static_cast(time_major), 0); - } - void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { - fbb_.AddElement(SequenceRNNOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); - } - void add_asymmetric_quantize_inputs(bool asymmetric_quantize_inputs) { - fbb_.AddElement(SequenceRNNOptions::VT_ASYMMETRIC_QUANTIZE_INPUTS, static_cast(asymmetric_quantize_inputs), 0); - } - explicit SequenceRNNOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - SequenceRNNOptionsBuilder &operator=(const SequenceRNNOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateSequenceRNNOptions( - flatbuffers::FlatBufferBuilder &_fbb, - bool time_major = false, - tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE, - bool asymmetric_quantize_inputs = false) { - SequenceRNNOptionsBuilder builder_(_fbb); - builder_.add_asymmetric_quantize_inputs(asymmetric_quantize_inputs); - builder_.add_fused_activation_function(fused_activation_function); - builder_.add_time_major(time_major); - return builder_.Finish(); -} - -flatbuffers::Offset CreateSequenceRNNOptions(flatbuffers::FlatBufferBuilder &_fbb, const SequenceRNNOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct BidirectionalSequenceRNNOptionsT : public flatbuffers::NativeTable { - typedef BidirectionalSequenceRNNOptions TableType; - bool time_major; - tflite::ActivationFunctionType fused_activation_function; - bool merge_outputs; - bool asymmetric_quantize_inputs; - BidirectionalSequenceRNNOptionsT() - : time_major(false), - fused_activation_function(tflite::ActivationFunctionType_NONE), - merge_outputs(false), - asymmetric_quantize_inputs(false) { - } -}; - -struct BidirectionalSequenceRNNOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef BidirectionalSequenceRNNOptionsT NativeTableType; - typedef BidirectionalSequenceRNNOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_TIME_MAJOR = 4, - VT_FUSED_ACTIVATION_FUNCTION = 6, - VT_MERGE_OUTPUTS = 8, - VT_ASYMMETRIC_QUANTIZE_INPUTS = 10 - }; - bool time_major() const { - return GetField(VT_TIME_MAJOR, 0) != 0; - } - tflite::ActivationFunctionType fused_activation_function() const { - return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); - } - bool merge_outputs() const { - return GetField(VT_MERGE_OUTPUTS, 0) != 0; - } - bool asymmetric_quantize_inputs() const { - return GetField(VT_ASYMMETRIC_QUANTIZE_INPUTS, 0) != 0; - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_TIME_MAJOR) && - VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION) && - VerifyField(verifier, VT_MERGE_OUTPUTS) && - VerifyField(verifier, VT_ASYMMETRIC_QUANTIZE_INPUTS) && - verifier.EndTable(); - } - BidirectionalSequenceRNNOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(BidirectionalSequenceRNNOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const BidirectionalSequenceRNNOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct BidirectionalSequenceRNNOptionsBuilder { - typedef BidirectionalSequenceRNNOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_time_major(bool time_major) { - fbb_.AddElement(BidirectionalSequenceRNNOptions::VT_TIME_MAJOR, static_cast(time_major), 0); - } - void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { - fbb_.AddElement(BidirectionalSequenceRNNOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); - } - void add_merge_outputs(bool merge_outputs) { - fbb_.AddElement(BidirectionalSequenceRNNOptions::VT_MERGE_OUTPUTS, static_cast(merge_outputs), 0); - } - void add_asymmetric_quantize_inputs(bool asymmetric_quantize_inputs) { - fbb_.AddElement(BidirectionalSequenceRNNOptions::VT_ASYMMETRIC_QUANTIZE_INPUTS, static_cast(asymmetric_quantize_inputs), 0); - } - explicit BidirectionalSequenceRNNOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - BidirectionalSequenceRNNOptionsBuilder &operator=(const BidirectionalSequenceRNNOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateBidirectionalSequenceRNNOptions( - flatbuffers::FlatBufferBuilder &_fbb, - bool time_major = false, - tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE, - bool merge_outputs = false, - bool asymmetric_quantize_inputs = false) { - BidirectionalSequenceRNNOptionsBuilder builder_(_fbb); - builder_.add_asymmetric_quantize_inputs(asymmetric_quantize_inputs); - builder_.add_merge_outputs(merge_outputs); - builder_.add_fused_activation_function(fused_activation_function); - builder_.add_time_major(time_major); - return builder_.Finish(); -} - -flatbuffers::Offset CreateBidirectionalSequenceRNNOptions(flatbuffers::FlatBufferBuilder &_fbb, const BidirectionalSequenceRNNOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct FullyConnectedOptionsT : public flatbuffers::NativeTable { - typedef FullyConnectedOptions TableType; - tflite::ActivationFunctionType fused_activation_function; - tflite::FullyConnectedOptionsWeightsFormat weights_format; - bool keep_num_dims; - bool asymmetric_quantize_inputs; - FullyConnectedOptionsT() - : fused_activation_function(tflite::ActivationFunctionType_NONE), - weights_format(tflite::FullyConnectedOptionsWeightsFormat_DEFAULT), - keep_num_dims(false), - asymmetric_quantize_inputs(false) { - } -}; - -struct FullyConnectedOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef FullyConnectedOptionsT NativeTableType; - typedef FullyConnectedOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_FUSED_ACTIVATION_FUNCTION = 4, - VT_WEIGHTS_FORMAT = 6, - VT_KEEP_NUM_DIMS = 8, - VT_ASYMMETRIC_QUANTIZE_INPUTS = 10 - }; - tflite::ActivationFunctionType fused_activation_function() const { - return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); - } - tflite::FullyConnectedOptionsWeightsFormat weights_format() const { - return static_cast(GetField(VT_WEIGHTS_FORMAT, 0)); - } - bool keep_num_dims() const { - return GetField(VT_KEEP_NUM_DIMS, 0) != 0; - } - bool asymmetric_quantize_inputs() const { - return GetField(VT_ASYMMETRIC_QUANTIZE_INPUTS, 0) != 0; - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION) && - VerifyField(verifier, VT_WEIGHTS_FORMAT) && - VerifyField(verifier, VT_KEEP_NUM_DIMS) && - VerifyField(verifier, VT_ASYMMETRIC_QUANTIZE_INPUTS) && - verifier.EndTable(); - } - FullyConnectedOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(FullyConnectedOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const FullyConnectedOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct FullyConnectedOptionsBuilder { - typedef FullyConnectedOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { - fbb_.AddElement(FullyConnectedOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); - } - void add_weights_format(tflite::FullyConnectedOptionsWeightsFormat weights_format) { - fbb_.AddElement(FullyConnectedOptions::VT_WEIGHTS_FORMAT, static_cast(weights_format), 0); - } - void add_keep_num_dims(bool keep_num_dims) { - fbb_.AddElement(FullyConnectedOptions::VT_KEEP_NUM_DIMS, static_cast(keep_num_dims), 0); - } - void add_asymmetric_quantize_inputs(bool asymmetric_quantize_inputs) { - fbb_.AddElement(FullyConnectedOptions::VT_ASYMMETRIC_QUANTIZE_INPUTS, static_cast(asymmetric_quantize_inputs), 0); - } - explicit FullyConnectedOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - FullyConnectedOptionsBuilder &operator=(const FullyConnectedOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateFullyConnectedOptions( - flatbuffers::FlatBufferBuilder &_fbb, - tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE, - tflite::FullyConnectedOptionsWeightsFormat weights_format = tflite::FullyConnectedOptionsWeightsFormat_DEFAULT, - bool keep_num_dims = false, - bool asymmetric_quantize_inputs = false) { - FullyConnectedOptionsBuilder builder_(_fbb); - builder_.add_asymmetric_quantize_inputs(asymmetric_quantize_inputs); - builder_.add_keep_num_dims(keep_num_dims); - builder_.add_weights_format(weights_format); - builder_.add_fused_activation_function(fused_activation_function); - return builder_.Finish(); -} - -flatbuffers::Offset CreateFullyConnectedOptions(flatbuffers::FlatBufferBuilder &_fbb, const FullyConnectedOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct SoftmaxOptionsT : public flatbuffers::NativeTable { - typedef SoftmaxOptions TableType; - float beta; - SoftmaxOptionsT() - : beta(0.0f) { - } -}; - -struct SoftmaxOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef SoftmaxOptionsT NativeTableType; - typedef SoftmaxOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_BETA = 4 - }; - float beta() const { - return GetField(VT_BETA, 0.0f); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_BETA) && - verifier.EndTable(); - } - SoftmaxOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(SoftmaxOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const SoftmaxOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct SoftmaxOptionsBuilder { - typedef SoftmaxOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_beta(float beta) { - fbb_.AddElement(SoftmaxOptions::VT_BETA, beta, 0.0f); - } - explicit SoftmaxOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - SoftmaxOptionsBuilder &operator=(const SoftmaxOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateSoftmaxOptions( - flatbuffers::FlatBufferBuilder &_fbb, - float beta = 0.0f) { - SoftmaxOptionsBuilder builder_(_fbb); - builder_.add_beta(beta); - return builder_.Finish(); -} - -flatbuffers::Offset CreateSoftmaxOptions(flatbuffers::FlatBufferBuilder &_fbb, const SoftmaxOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct ConcatenationOptionsT : public flatbuffers::NativeTable { - typedef ConcatenationOptions TableType; - int32_t axis; - tflite::ActivationFunctionType fused_activation_function; - ConcatenationOptionsT() - : axis(0), - fused_activation_function(tflite::ActivationFunctionType_NONE) { - } -}; - -struct ConcatenationOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef ConcatenationOptionsT NativeTableType; - typedef ConcatenationOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_AXIS = 4, - VT_FUSED_ACTIVATION_FUNCTION = 6 - }; - int32_t axis() const { - return GetField(VT_AXIS, 0); - } - tflite::ActivationFunctionType fused_activation_function() const { - return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_AXIS) && - VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION) && - verifier.EndTable(); - } - ConcatenationOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(ConcatenationOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const ConcatenationOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct ConcatenationOptionsBuilder { - typedef ConcatenationOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_axis(int32_t axis) { - fbb_.AddElement(ConcatenationOptions::VT_AXIS, axis, 0); - } - void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { - fbb_.AddElement(ConcatenationOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); - } - explicit ConcatenationOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - ConcatenationOptionsBuilder &operator=(const ConcatenationOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateConcatenationOptions( - flatbuffers::FlatBufferBuilder &_fbb, - int32_t axis = 0, - tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE) { - ConcatenationOptionsBuilder builder_(_fbb); - builder_.add_axis(axis); - builder_.add_fused_activation_function(fused_activation_function); - return builder_.Finish(); -} - -flatbuffers::Offset CreateConcatenationOptions(flatbuffers::FlatBufferBuilder &_fbb, const ConcatenationOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct AddOptionsT : public flatbuffers::NativeTable { - typedef AddOptions TableType; - tflite::ActivationFunctionType fused_activation_function; - bool pot_scale_int16; - AddOptionsT() - : fused_activation_function(tflite::ActivationFunctionType_NONE), - pot_scale_int16(true) { - } -}; - -struct AddOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef AddOptionsT NativeTableType; - typedef AddOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_FUSED_ACTIVATION_FUNCTION = 4, - VT_POT_SCALE_INT16 = 6 - }; - tflite::ActivationFunctionType fused_activation_function() const { - return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); - } - bool pot_scale_int16() const { - return GetField(VT_POT_SCALE_INT16, 1) != 0; - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION) && - VerifyField(verifier, VT_POT_SCALE_INT16) && - verifier.EndTable(); - } - AddOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(AddOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const AddOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct AddOptionsBuilder { - typedef AddOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { - fbb_.AddElement(AddOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); - } - void add_pot_scale_int16(bool pot_scale_int16) { - fbb_.AddElement(AddOptions::VT_POT_SCALE_INT16, static_cast(pot_scale_int16), 1); - } - explicit AddOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - AddOptionsBuilder &operator=(const AddOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateAddOptions( - flatbuffers::FlatBufferBuilder &_fbb, - tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE, - bool pot_scale_int16 = true) { - AddOptionsBuilder builder_(_fbb); - builder_.add_pot_scale_int16(pot_scale_int16); - builder_.add_fused_activation_function(fused_activation_function); - return builder_.Finish(); -} - -flatbuffers::Offset CreateAddOptions(flatbuffers::FlatBufferBuilder &_fbb, const AddOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct MulOptionsT : public flatbuffers::NativeTable { - typedef MulOptions TableType; - tflite::ActivationFunctionType fused_activation_function; - MulOptionsT() - : fused_activation_function(tflite::ActivationFunctionType_NONE) { - } -}; - -struct MulOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef MulOptionsT NativeTableType; - typedef MulOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_FUSED_ACTIVATION_FUNCTION = 4 - }; - tflite::ActivationFunctionType fused_activation_function() const { - return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION) && - verifier.EndTable(); - } - MulOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(MulOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const MulOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct MulOptionsBuilder { - typedef MulOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { - fbb_.AddElement(MulOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); - } - explicit MulOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - MulOptionsBuilder &operator=(const MulOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateMulOptions( - flatbuffers::FlatBufferBuilder &_fbb, - tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE) { - MulOptionsBuilder builder_(_fbb); - builder_.add_fused_activation_function(fused_activation_function); - return builder_.Finish(); -} - -flatbuffers::Offset CreateMulOptions(flatbuffers::FlatBufferBuilder &_fbb, const MulOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct L2NormOptionsT : public flatbuffers::NativeTable { - typedef L2NormOptions TableType; - tflite::ActivationFunctionType fused_activation_function; - L2NormOptionsT() - : fused_activation_function(tflite::ActivationFunctionType_NONE) { - } -}; - -struct L2NormOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef L2NormOptionsT NativeTableType; - typedef L2NormOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_FUSED_ACTIVATION_FUNCTION = 4 - }; - tflite::ActivationFunctionType fused_activation_function() const { - return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION) && - verifier.EndTable(); - } - L2NormOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(L2NormOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const L2NormOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct L2NormOptionsBuilder { - typedef L2NormOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { - fbb_.AddElement(L2NormOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); - } - explicit L2NormOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - L2NormOptionsBuilder &operator=(const L2NormOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateL2NormOptions( - flatbuffers::FlatBufferBuilder &_fbb, - tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE) { - L2NormOptionsBuilder builder_(_fbb); - builder_.add_fused_activation_function(fused_activation_function); - return builder_.Finish(); -} - -flatbuffers::Offset CreateL2NormOptions(flatbuffers::FlatBufferBuilder &_fbb, const L2NormOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct LocalResponseNormalizationOptionsT : public flatbuffers::NativeTable { - typedef LocalResponseNormalizationOptions TableType; - int32_t radius; - float bias; - float alpha; - float beta; - LocalResponseNormalizationOptionsT() - : radius(0), - bias(0.0f), - alpha(0.0f), - beta(0.0f) { - } -}; - -struct LocalResponseNormalizationOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef LocalResponseNormalizationOptionsT NativeTableType; - typedef LocalResponseNormalizationOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_RADIUS = 4, - VT_BIAS = 6, - VT_ALPHA = 8, - VT_BETA = 10 - }; - int32_t radius() const { - return GetField(VT_RADIUS, 0); - } - float bias() const { - return GetField(VT_BIAS, 0.0f); - } - float alpha() const { - return GetField(VT_ALPHA, 0.0f); - } - float beta() const { - return GetField(VT_BETA, 0.0f); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_RADIUS) && - VerifyField(verifier, VT_BIAS) && - VerifyField(verifier, VT_ALPHA) && - VerifyField(verifier, VT_BETA) && - verifier.EndTable(); - } - LocalResponseNormalizationOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(LocalResponseNormalizationOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const LocalResponseNormalizationOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct LocalResponseNormalizationOptionsBuilder { - typedef LocalResponseNormalizationOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_radius(int32_t radius) { - fbb_.AddElement(LocalResponseNormalizationOptions::VT_RADIUS, radius, 0); - } - void add_bias(float bias) { - fbb_.AddElement(LocalResponseNormalizationOptions::VT_BIAS, bias, 0.0f); - } - void add_alpha(float alpha) { - fbb_.AddElement(LocalResponseNormalizationOptions::VT_ALPHA, alpha, 0.0f); - } - void add_beta(float beta) { - fbb_.AddElement(LocalResponseNormalizationOptions::VT_BETA, beta, 0.0f); - } - explicit LocalResponseNormalizationOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - LocalResponseNormalizationOptionsBuilder &operator=(const LocalResponseNormalizationOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateLocalResponseNormalizationOptions( - flatbuffers::FlatBufferBuilder &_fbb, - int32_t radius = 0, - float bias = 0.0f, - float alpha = 0.0f, - float beta = 0.0f) { - LocalResponseNormalizationOptionsBuilder builder_(_fbb); - builder_.add_beta(beta); - builder_.add_alpha(alpha); - builder_.add_bias(bias); - builder_.add_radius(radius); - return builder_.Finish(); -} - -flatbuffers::Offset CreateLocalResponseNormalizationOptions(flatbuffers::FlatBufferBuilder &_fbb, const LocalResponseNormalizationOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct LSTMOptionsT : public flatbuffers::NativeTable { - typedef LSTMOptions TableType; - tflite::ActivationFunctionType fused_activation_function; - float cell_clip; - float proj_clip; - tflite::LSTMKernelType kernel_type; - bool asymmetric_quantize_inputs; - LSTMOptionsT() - : fused_activation_function(tflite::ActivationFunctionType_NONE), - cell_clip(0.0f), - proj_clip(0.0f), - kernel_type(tflite::LSTMKernelType_FULL), - asymmetric_quantize_inputs(false) { - } -}; - -struct LSTMOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef LSTMOptionsT NativeTableType; - typedef LSTMOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_FUSED_ACTIVATION_FUNCTION = 4, - VT_CELL_CLIP = 6, - VT_PROJ_CLIP = 8, - VT_KERNEL_TYPE = 10, - VT_ASYMMETRIC_QUANTIZE_INPUTS = 12 - }; - tflite::ActivationFunctionType fused_activation_function() const { - return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); - } - float cell_clip() const { - return GetField(VT_CELL_CLIP, 0.0f); - } - float proj_clip() const { - return GetField(VT_PROJ_CLIP, 0.0f); - } - tflite::LSTMKernelType kernel_type() const { - return static_cast(GetField(VT_KERNEL_TYPE, 0)); - } - bool asymmetric_quantize_inputs() const { - return GetField(VT_ASYMMETRIC_QUANTIZE_INPUTS, 0) != 0; - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION) && - VerifyField(verifier, VT_CELL_CLIP) && - VerifyField(verifier, VT_PROJ_CLIP) && - VerifyField(verifier, VT_KERNEL_TYPE) && - VerifyField(verifier, VT_ASYMMETRIC_QUANTIZE_INPUTS) && - verifier.EndTable(); - } - LSTMOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(LSTMOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const LSTMOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct LSTMOptionsBuilder { - typedef LSTMOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { - fbb_.AddElement(LSTMOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); - } - void add_cell_clip(float cell_clip) { - fbb_.AddElement(LSTMOptions::VT_CELL_CLIP, cell_clip, 0.0f); - } - void add_proj_clip(float proj_clip) { - fbb_.AddElement(LSTMOptions::VT_PROJ_CLIP, proj_clip, 0.0f); - } - void add_kernel_type(tflite::LSTMKernelType kernel_type) { - fbb_.AddElement(LSTMOptions::VT_KERNEL_TYPE, static_cast(kernel_type), 0); - } - void add_asymmetric_quantize_inputs(bool asymmetric_quantize_inputs) { - fbb_.AddElement(LSTMOptions::VT_ASYMMETRIC_QUANTIZE_INPUTS, static_cast(asymmetric_quantize_inputs), 0); - } - explicit LSTMOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - LSTMOptionsBuilder &operator=(const LSTMOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateLSTMOptions( - flatbuffers::FlatBufferBuilder &_fbb, - tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE, - float cell_clip = 0.0f, - float proj_clip = 0.0f, - tflite::LSTMKernelType kernel_type = tflite::LSTMKernelType_FULL, - bool asymmetric_quantize_inputs = false) { - LSTMOptionsBuilder builder_(_fbb); - builder_.add_proj_clip(proj_clip); - builder_.add_cell_clip(cell_clip); - builder_.add_asymmetric_quantize_inputs(asymmetric_quantize_inputs); - builder_.add_kernel_type(kernel_type); - builder_.add_fused_activation_function(fused_activation_function); - return builder_.Finish(); -} - -flatbuffers::Offset CreateLSTMOptions(flatbuffers::FlatBufferBuilder &_fbb, const LSTMOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct UnidirectionalSequenceLSTMOptionsT : public flatbuffers::NativeTable { - typedef UnidirectionalSequenceLSTMOptions TableType; - tflite::ActivationFunctionType fused_activation_function; - float cell_clip; - float proj_clip; - bool time_major; - bool asymmetric_quantize_inputs; - UnidirectionalSequenceLSTMOptionsT() - : fused_activation_function(tflite::ActivationFunctionType_NONE), - cell_clip(0.0f), - proj_clip(0.0f), - time_major(false), - asymmetric_quantize_inputs(false) { - } -}; - -struct UnidirectionalSequenceLSTMOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef UnidirectionalSequenceLSTMOptionsT NativeTableType; - typedef UnidirectionalSequenceLSTMOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_FUSED_ACTIVATION_FUNCTION = 4, - VT_CELL_CLIP = 6, - VT_PROJ_CLIP = 8, - VT_TIME_MAJOR = 10, - VT_ASYMMETRIC_QUANTIZE_INPUTS = 12 - }; - tflite::ActivationFunctionType fused_activation_function() const { - return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); - } - float cell_clip() const { - return GetField(VT_CELL_CLIP, 0.0f); - } - float proj_clip() const { - return GetField(VT_PROJ_CLIP, 0.0f); - } - bool time_major() const { - return GetField(VT_TIME_MAJOR, 0) != 0; - } - bool asymmetric_quantize_inputs() const { - return GetField(VT_ASYMMETRIC_QUANTIZE_INPUTS, 0) != 0; - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION) && - VerifyField(verifier, VT_CELL_CLIP) && - VerifyField(verifier, VT_PROJ_CLIP) && - VerifyField(verifier, VT_TIME_MAJOR) && - VerifyField(verifier, VT_ASYMMETRIC_QUANTIZE_INPUTS) && - verifier.EndTable(); - } - UnidirectionalSequenceLSTMOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(UnidirectionalSequenceLSTMOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const UnidirectionalSequenceLSTMOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct UnidirectionalSequenceLSTMOptionsBuilder { - typedef UnidirectionalSequenceLSTMOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { - fbb_.AddElement(UnidirectionalSequenceLSTMOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); - } - void add_cell_clip(float cell_clip) { - fbb_.AddElement(UnidirectionalSequenceLSTMOptions::VT_CELL_CLIP, cell_clip, 0.0f); - } - void add_proj_clip(float proj_clip) { - fbb_.AddElement(UnidirectionalSequenceLSTMOptions::VT_PROJ_CLIP, proj_clip, 0.0f); - } - void add_time_major(bool time_major) { - fbb_.AddElement(UnidirectionalSequenceLSTMOptions::VT_TIME_MAJOR, static_cast(time_major), 0); - } - void add_asymmetric_quantize_inputs(bool asymmetric_quantize_inputs) { - fbb_.AddElement(UnidirectionalSequenceLSTMOptions::VT_ASYMMETRIC_QUANTIZE_INPUTS, static_cast(asymmetric_quantize_inputs), 0); - } - explicit UnidirectionalSequenceLSTMOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - UnidirectionalSequenceLSTMOptionsBuilder &operator=(const UnidirectionalSequenceLSTMOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateUnidirectionalSequenceLSTMOptions( - flatbuffers::FlatBufferBuilder &_fbb, - tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE, - float cell_clip = 0.0f, - float proj_clip = 0.0f, - bool time_major = false, - bool asymmetric_quantize_inputs = false) { - UnidirectionalSequenceLSTMOptionsBuilder builder_(_fbb); - builder_.add_proj_clip(proj_clip); - builder_.add_cell_clip(cell_clip); - builder_.add_asymmetric_quantize_inputs(asymmetric_quantize_inputs); - builder_.add_time_major(time_major); - builder_.add_fused_activation_function(fused_activation_function); - return builder_.Finish(); -} - -flatbuffers::Offset CreateUnidirectionalSequenceLSTMOptions(flatbuffers::FlatBufferBuilder &_fbb, const UnidirectionalSequenceLSTMOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct BidirectionalSequenceLSTMOptionsT : public flatbuffers::NativeTable { - typedef BidirectionalSequenceLSTMOptions TableType; - tflite::ActivationFunctionType fused_activation_function; - float cell_clip; - float proj_clip; - bool merge_outputs; - bool time_major; - bool asymmetric_quantize_inputs; - BidirectionalSequenceLSTMOptionsT() - : fused_activation_function(tflite::ActivationFunctionType_NONE), - cell_clip(0.0f), - proj_clip(0.0f), - merge_outputs(false), - time_major(true), - asymmetric_quantize_inputs(false) { - } -}; - -struct BidirectionalSequenceLSTMOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef BidirectionalSequenceLSTMOptionsT NativeTableType; - typedef BidirectionalSequenceLSTMOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_FUSED_ACTIVATION_FUNCTION = 4, - VT_CELL_CLIP = 6, - VT_PROJ_CLIP = 8, - VT_MERGE_OUTPUTS = 10, - VT_TIME_MAJOR = 12, - VT_ASYMMETRIC_QUANTIZE_INPUTS = 14 - }; - tflite::ActivationFunctionType fused_activation_function() const { - return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); - } - float cell_clip() const { - return GetField(VT_CELL_CLIP, 0.0f); - } - float proj_clip() const { - return GetField(VT_PROJ_CLIP, 0.0f); - } - bool merge_outputs() const { - return GetField(VT_MERGE_OUTPUTS, 0) != 0; - } - bool time_major() const { - return GetField(VT_TIME_MAJOR, 1) != 0; - } - bool asymmetric_quantize_inputs() const { - return GetField(VT_ASYMMETRIC_QUANTIZE_INPUTS, 0) != 0; - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION) && - VerifyField(verifier, VT_CELL_CLIP) && - VerifyField(verifier, VT_PROJ_CLIP) && - VerifyField(verifier, VT_MERGE_OUTPUTS) && - VerifyField(verifier, VT_TIME_MAJOR) && - VerifyField(verifier, VT_ASYMMETRIC_QUANTIZE_INPUTS) && - verifier.EndTable(); - } - BidirectionalSequenceLSTMOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(BidirectionalSequenceLSTMOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const BidirectionalSequenceLSTMOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct BidirectionalSequenceLSTMOptionsBuilder { - typedef BidirectionalSequenceLSTMOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { - fbb_.AddElement(BidirectionalSequenceLSTMOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); - } - void add_cell_clip(float cell_clip) { - fbb_.AddElement(BidirectionalSequenceLSTMOptions::VT_CELL_CLIP, cell_clip, 0.0f); - } - void add_proj_clip(float proj_clip) { - fbb_.AddElement(BidirectionalSequenceLSTMOptions::VT_PROJ_CLIP, proj_clip, 0.0f); - } - void add_merge_outputs(bool merge_outputs) { - fbb_.AddElement(BidirectionalSequenceLSTMOptions::VT_MERGE_OUTPUTS, static_cast(merge_outputs), 0); - } - void add_time_major(bool time_major) { - fbb_.AddElement(BidirectionalSequenceLSTMOptions::VT_TIME_MAJOR, static_cast(time_major), 1); - } - void add_asymmetric_quantize_inputs(bool asymmetric_quantize_inputs) { - fbb_.AddElement(BidirectionalSequenceLSTMOptions::VT_ASYMMETRIC_QUANTIZE_INPUTS, static_cast(asymmetric_quantize_inputs), 0); - } - explicit BidirectionalSequenceLSTMOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - BidirectionalSequenceLSTMOptionsBuilder &operator=(const BidirectionalSequenceLSTMOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateBidirectionalSequenceLSTMOptions( - flatbuffers::FlatBufferBuilder &_fbb, - tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE, - float cell_clip = 0.0f, - float proj_clip = 0.0f, - bool merge_outputs = false, - bool time_major = true, - bool asymmetric_quantize_inputs = false) { - BidirectionalSequenceLSTMOptionsBuilder builder_(_fbb); - builder_.add_proj_clip(proj_clip); - builder_.add_cell_clip(cell_clip); - builder_.add_asymmetric_quantize_inputs(asymmetric_quantize_inputs); - builder_.add_time_major(time_major); - builder_.add_merge_outputs(merge_outputs); - builder_.add_fused_activation_function(fused_activation_function); - return builder_.Finish(); -} - -flatbuffers::Offset CreateBidirectionalSequenceLSTMOptions(flatbuffers::FlatBufferBuilder &_fbb, const BidirectionalSequenceLSTMOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct ResizeBilinearOptionsT : public flatbuffers::NativeTable { - typedef ResizeBilinearOptions TableType; - bool align_corners; - bool half_pixel_centers; - ResizeBilinearOptionsT() - : align_corners(false), - half_pixel_centers(false) { - } -}; - -struct ResizeBilinearOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef ResizeBilinearOptionsT NativeTableType; - typedef ResizeBilinearOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_ALIGN_CORNERS = 8, - VT_HALF_PIXEL_CENTERS = 10 - }; - bool align_corners() const { - return GetField(VT_ALIGN_CORNERS, 0) != 0; - } - bool half_pixel_centers() const { - return GetField(VT_HALF_PIXEL_CENTERS, 0) != 0; - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_ALIGN_CORNERS) && - VerifyField(verifier, VT_HALF_PIXEL_CENTERS) && - verifier.EndTable(); - } - ResizeBilinearOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(ResizeBilinearOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const ResizeBilinearOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct ResizeBilinearOptionsBuilder { - typedef ResizeBilinearOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_align_corners(bool align_corners) { - fbb_.AddElement(ResizeBilinearOptions::VT_ALIGN_CORNERS, static_cast(align_corners), 0); - } - void add_half_pixel_centers(bool half_pixel_centers) { - fbb_.AddElement(ResizeBilinearOptions::VT_HALF_PIXEL_CENTERS, static_cast(half_pixel_centers), 0); - } - explicit ResizeBilinearOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - ResizeBilinearOptionsBuilder &operator=(const ResizeBilinearOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateResizeBilinearOptions( - flatbuffers::FlatBufferBuilder &_fbb, - bool align_corners = false, - bool half_pixel_centers = false) { - ResizeBilinearOptionsBuilder builder_(_fbb); - builder_.add_half_pixel_centers(half_pixel_centers); - builder_.add_align_corners(align_corners); - return builder_.Finish(); -} - -flatbuffers::Offset CreateResizeBilinearOptions(flatbuffers::FlatBufferBuilder &_fbb, const ResizeBilinearOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct ResizeNearestNeighborOptionsT : public flatbuffers::NativeTable { - typedef ResizeNearestNeighborOptions TableType; - bool align_corners; - bool half_pixel_centers; - ResizeNearestNeighborOptionsT() - : align_corners(false), - half_pixel_centers(false) { - } -}; - -struct ResizeNearestNeighborOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef ResizeNearestNeighborOptionsT NativeTableType; - typedef ResizeNearestNeighborOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_ALIGN_CORNERS = 4, - VT_HALF_PIXEL_CENTERS = 6 - }; - bool align_corners() const { - return GetField(VT_ALIGN_CORNERS, 0) != 0; - } - bool half_pixel_centers() const { - return GetField(VT_HALF_PIXEL_CENTERS, 0) != 0; - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_ALIGN_CORNERS) && - VerifyField(verifier, VT_HALF_PIXEL_CENTERS) && - verifier.EndTable(); - } - ResizeNearestNeighborOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(ResizeNearestNeighborOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const ResizeNearestNeighborOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct ResizeNearestNeighborOptionsBuilder { - typedef ResizeNearestNeighborOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_align_corners(bool align_corners) { - fbb_.AddElement(ResizeNearestNeighborOptions::VT_ALIGN_CORNERS, static_cast(align_corners), 0); - } - void add_half_pixel_centers(bool half_pixel_centers) { - fbb_.AddElement(ResizeNearestNeighborOptions::VT_HALF_PIXEL_CENTERS, static_cast(half_pixel_centers), 0); - } - explicit ResizeNearestNeighborOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - ResizeNearestNeighborOptionsBuilder &operator=(const ResizeNearestNeighborOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateResizeNearestNeighborOptions( - flatbuffers::FlatBufferBuilder &_fbb, - bool align_corners = false, - bool half_pixel_centers = false) { - ResizeNearestNeighborOptionsBuilder builder_(_fbb); - builder_.add_half_pixel_centers(half_pixel_centers); - builder_.add_align_corners(align_corners); - return builder_.Finish(); -} - -flatbuffers::Offset CreateResizeNearestNeighborOptions(flatbuffers::FlatBufferBuilder &_fbb, const ResizeNearestNeighborOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct CallOptionsT : public flatbuffers::NativeTable { - typedef CallOptions TableType; - uint32_t subgraph; - CallOptionsT() - : subgraph(0) { - } -}; - -struct CallOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef CallOptionsT NativeTableType; - typedef CallOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_SUBGRAPH = 4 - }; - uint32_t subgraph() const { - return GetField(VT_SUBGRAPH, 0); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_SUBGRAPH) && - verifier.EndTable(); - } - CallOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(CallOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const CallOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct CallOptionsBuilder { - typedef CallOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_subgraph(uint32_t subgraph) { - fbb_.AddElement(CallOptions::VT_SUBGRAPH, subgraph, 0); - } - explicit CallOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - CallOptionsBuilder &operator=(const CallOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateCallOptions( - flatbuffers::FlatBufferBuilder &_fbb, - uint32_t subgraph = 0) { - CallOptionsBuilder builder_(_fbb); - builder_.add_subgraph(subgraph); - return builder_.Finish(); -} - -flatbuffers::Offset CreateCallOptions(flatbuffers::FlatBufferBuilder &_fbb, const CallOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct PadOptionsT : public flatbuffers::NativeTable { - typedef PadOptions TableType; - PadOptionsT() { - } -}; - -struct PadOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef PadOptionsT NativeTableType; - typedef PadOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - PadOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(PadOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const PadOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct PadOptionsBuilder { - typedef PadOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit PadOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - PadOptionsBuilder &operator=(const PadOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreatePadOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - PadOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreatePadOptions(flatbuffers::FlatBufferBuilder &_fbb, const PadOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct PadV2OptionsT : public flatbuffers::NativeTable { - typedef PadV2Options TableType; - PadV2OptionsT() { - } -}; - -struct PadV2Options FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef PadV2OptionsT NativeTableType; - typedef PadV2OptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - PadV2OptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(PadV2OptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const PadV2OptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct PadV2OptionsBuilder { - typedef PadV2Options Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit PadV2OptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - PadV2OptionsBuilder &operator=(const PadV2OptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreatePadV2Options( - flatbuffers::FlatBufferBuilder &_fbb) { - PadV2OptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreatePadV2Options(flatbuffers::FlatBufferBuilder &_fbb, const PadV2OptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct ReshapeOptionsT : public flatbuffers::NativeTable { - typedef ReshapeOptions TableType; - std::vector new_shape; - ReshapeOptionsT() { - } -}; - -struct ReshapeOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef ReshapeOptionsT NativeTableType; - typedef ReshapeOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_NEW_SHAPE = 4 - }; - const flatbuffers::Vector *new_shape() const { - return GetPointer *>(VT_NEW_SHAPE); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyOffset(verifier, VT_NEW_SHAPE) && - verifier.VerifyVector(new_shape()) && - verifier.EndTable(); - } - ReshapeOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(ReshapeOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const ReshapeOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct ReshapeOptionsBuilder { - typedef ReshapeOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_new_shape(flatbuffers::Offset> new_shape) { - fbb_.AddOffset(ReshapeOptions::VT_NEW_SHAPE, new_shape); - } - explicit ReshapeOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - ReshapeOptionsBuilder &operator=(const ReshapeOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateReshapeOptions( - flatbuffers::FlatBufferBuilder &_fbb, - flatbuffers::Offset> new_shape = 0) { - ReshapeOptionsBuilder builder_(_fbb); - builder_.add_new_shape(new_shape); - return builder_.Finish(); -} - -inline flatbuffers::Offset CreateReshapeOptionsDirect( - flatbuffers::FlatBufferBuilder &_fbb, - const std::vector *new_shape = nullptr) { - auto new_shape__ = new_shape ? _fbb.CreateVector(*new_shape) : 0; - return tflite::CreateReshapeOptions( - _fbb, - new_shape__); -} - -flatbuffers::Offset CreateReshapeOptions(flatbuffers::FlatBufferBuilder &_fbb, const ReshapeOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct SpaceToBatchNDOptionsT : public flatbuffers::NativeTable { - typedef SpaceToBatchNDOptions TableType; - SpaceToBatchNDOptionsT() { - } -}; - -struct SpaceToBatchNDOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef SpaceToBatchNDOptionsT NativeTableType; - typedef SpaceToBatchNDOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - SpaceToBatchNDOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(SpaceToBatchNDOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const SpaceToBatchNDOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct SpaceToBatchNDOptionsBuilder { - typedef SpaceToBatchNDOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit SpaceToBatchNDOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - SpaceToBatchNDOptionsBuilder &operator=(const SpaceToBatchNDOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateSpaceToBatchNDOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - SpaceToBatchNDOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateSpaceToBatchNDOptions(flatbuffers::FlatBufferBuilder &_fbb, const SpaceToBatchNDOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct BatchToSpaceNDOptionsT : public flatbuffers::NativeTable { - typedef BatchToSpaceNDOptions TableType; - BatchToSpaceNDOptionsT() { - } -}; - -struct BatchToSpaceNDOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef BatchToSpaceNDOptionsT NativeTableType; - typedef BatchToSpaceNDOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - BatchToSpaceNDOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(BatchToSpaceNDOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const BatchToSpaceNDOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct BatchToSpaceNDOptionsBuilder { - typedef BatchToSpaceNDOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit BatchToSpaceNDOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - BatchToSpaceNDOptionsBuilder &operator=(const BatchToSpaceNDOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateBatchToSpaceNDOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - BatchToSpaceNDOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateBatchToSpaceNDOptions(flatbuffers::FlatBufferBuilder &_fbb, const BatchToSpaceNDOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct SkipGramOptionsT : public flatbuffers::NativeTable { - typedef SkipGramOptions TableType; - int32_t ngram_size; - int32_t max_skip_size; - bool include_all_ngrams; - SkipGramOptionsT() - : ngram_size(0), - max_skip_size(0), - include_all_ngrams(false) { - } -}; - -struct SkipGramOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef SkipGramOptionsT NativeTableType; - typedef SkipGramOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_NGRAM_SIZE = 4, - VT_MAX_SKIP_SIZE = 6, - VT_INCLUDE_ALL_NGRAMS = 8 - }; - int32_t ngram_size() const { - return GetField(VT_NGRAM_SIZE, 0); - } - int32_t max_skip_size() const { - return GetField(VT_MAX_SKIP_SIZE, 0); - } - bool include_all_ngrams() const { - return GetField(VT_INCLUDE_ALL_NGRAMS, 0) != 0; - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_NGRAM_SIZE) && - VerifyField(verifier, VT_MAX_SKIP_SIZE) && - VerifyField(verifier, VT_INCLUDE_ALL_NGRAMS) && - verifier.EndTable(); - } - SkipGramOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(SkipGramOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const SkipGramOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct SkipGramOptionsBuilder { - typedef SkipGramOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_ngram_size(int32_t ngram_size) { - fbb_.AddElement(SkipGramOptions::VT_NGRAM_SIZE, ngram_size, 0); - } - void add_max_skip_size(int32_t max_skip_size) { - fbb_.AddElement(SkipGramOptions::VT_MAX_SKIP_SIZE, max_skip_size, 0); - } - void add_include_all_ngrams(bool include_all_ngrams) { - fbb_.AddElement(SkipGramOptions::VT_INCLUDE_ALL_NGRAMS, static_cast(include_all_ngrams), 0); - } - explicit SkipGramOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - SkipGramOptionsBuilder &operator=(const SkipGramOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateSkipGramOptions( - flatbuffers::FlatBufferBuilder &_fbb, - int32_t ngram_size = 0, - int32_t max_skip_size = 0, - bool include_all_ngrams = false) { - SkipGramOptionsBuilder builder_(_fbb); - builder_.add_max_skip_size(max_skip_size); - builder_.add_ngram_size(ngram_size); - builder_.add_include_all_ngrams(include_all_ngrams); - return builder_.Finish(); -} - -flatbuffers::Offset CreateSkipGramOptions(flatbuffers::FlatBufferBuilder &_fbb, const SkipGramOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct SpaceToDepthOptionsT : public flatbuffers::NativeTable { - typedef SpaceToDepthOptions TableType; - int32_t block_size; - SpaceToDepthOptionsT() - : block_size(0) { - } -}; - -struct SpaceToDepthOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef SpaceToDepthOptionsT NativeTableType; - typedef SpaceToDepthOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_BLOCK_SIZE = 4 - }; - int32_t block_size() const { - return GetField(VT_BLOCK_SIZE, 0); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_BLOCK_SIZE) && - verifier.EndTable(); - } - SpaceToDepthOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(SpaceToDepthOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const SpaceToDepthOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct SpaceToDepthOptionsBuilder { - typedef SpaceToDepthOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_block_size(int32_t block_size) { - fbb_.AddElement(SpaceToDepthOptions::VT_BLOCK_SIZE, block_size, 0); - } - explicit SpaceToDepthOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - SpaceToDepthOptionsBuilder &operator=(const SpaceToDepthOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateSpaceToDepthOptions( - flatbuffers::FlatBufferBuilder &_fbb, - int32_t block_size = 0) { - SpaceToDepthOptionsBuilder builder_(_fbb); - builder_.add_block_size(block_size); - return builder_.Finish(); -} - -flatbuffers::Offset CreateSpaceToDepthOptions(flatbuffers::FlatBufferBuilder &_fbb, const SpaceToDepthOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct DepthToSpaceOptionsT : public flatbuffers::NativeTable { - typedef DepthToSpaceOptions TableType; - int32_t block_size; - DepthToSpaceOptionsT() - : block_size(0) { - } -}; - -struct DepthToSpaceOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef DepthToSpaceOptionsT NativeTableType; - typedef DepthToSpaceOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_BLOCK_SIZE = 4 - }; - int32_t block_size() const { - return GetField(VT_BLOCK_SIZE, 0); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_BLOCK_SIZE) && - verifier.EndTable(); - } - DepthToSpaceOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(DepthToSpaceOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const DepthToSpaceOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct DepthToSpaceOptionsBuilder { - typedef DepthToSpaceOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_block_size(int32_t block_size) { - fbb_.AddElement(DepthToSpaceOptions::VT_BLOCK_SIZE, block_size, 0); - } - explicit DepthToSpaceOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - DepthToSpaceOptionsBuilder &operator=(const DepthToSpaceOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateDepthToSpaceOptions( - flatbuffers::FlatBufferBuilder &_fbb, - int32_t block_size = 0) { - DepthToSpaceOptionsBuilder builder_(_fbb); - builder_.add_block_size(block_size); - return builder_.Finish(); -} - -flatbuffers::Offset CreateDepthToSpaceOptions(flatbuffers::FlatBufferBuilder &_fbb, const DepthToSpaceOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct SubOptionsT : public flatbuffers::NativeTable { - typedef SubOptions TableType; - tflite::ActivationFunctionType fused_activation_function; - bool pot_scale_int16; - SubOptionsT() - : fused_activation_function(tflite::ActivationFunctionType_NONE), - pot_scale_int16(true) { - } -}; - -struct SubOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef SubOptionsT NativeTableType; - typedef SubOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_FUSED_ACTIVATION_FUNCTION = 4, - VT_POT_SCALE_INT16 = 6 - }; - tflite::ActivationFunctionType fused_activation_function() const { - return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); - } - bool pot_scale_int16() const { - return GetField(VT_POT_SCALE_INT16, 1) != 0; - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION) && - VerifyField(verifier, VT_POT_SCALE_INT16) && - verifier.EndTable(); - } - SubOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(SubOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const SubOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct SubOptionsBuilder { - typedef SubOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { - fbb_.AddElement(SubOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); - } - void add_pot_scale_int16(bool pot_scale_int16) { - fbb_.AddElement(SubOptions::VT_POT_SCALE_INT16, static_cast(pot_scale_int16), 1); - } - explicit SubOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - SubOptionsBuilder &operator=(const SubOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateSubOptions( - flatbuffers::FlatBufferBuilder &_fbb, - tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE, - bool pot_scale_int16 = true) { - SubOptionsBuilder builder_(_fbb); - builder_.add_pot_scale_int16(pot_scale_int16); - builder_.add_fused_activation_function(fused_activation_function); - return builder_.Finish(); -} - -flatbuffers::Offset CreateSubOptions(flatbuffers::FlatBufferBuilder &_fbb, const SubOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct DivOptionsT : public flatbuffers::NativeTable { - typedef DivOptions TableType; - tflite::ActivationFunctionType fused_activation_function; - DivOptionsT() - : fused_activation_function(tflite::ActivationFunctionType_NONE) { - } -}; - -struct DivOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef DivOptionsT NativeTableType; - typedef DivOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_FUSED_ACTIVATION_FUNCTION = 4 - }; - tflite::ActivationFunctionType fused_activation_function() const { - return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION) && - verifier.EndTable(); - } - DivOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(DivOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const DivOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct DivOptionsBuilder { - typedef DivOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { - fbb_.AddElement(DivOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); - } - explicit DivOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - DivOptionsBuilder &operator=(const DivOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateDivOptions( - flatbuffers::FlatBufferBuilder &_fbb, - tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE) { - DivOptionsBuilder builder_(_fbb); - builder_.add_fused_activation_function(fused_activation_function); - return builder_.Finish(); -} - -flatbuffers::Offset CreateDivOptions(flatbuffers::FlatBufferBuilder &_fbb, const DivOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct TopKV2OptionsT : public flatbuffers::NativeTable { - typedef TopKV2Options TableType; - TopKV2OptionsT() { - } -}; - -struct TopKV2Options FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef TopKV2OptionsT NativeTableType; - typedef TopKV2OptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - TopKV2OptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(TopKV2OptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const TopKV2OptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct TopKV2OptionsBuilder { - typedef TopKV2Options Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit TopKV2OptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - TopKV2OptionsBuilder &operator=(const TopKV2OptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateTopKV2Options( - flatbuffers::FlatBufferBuilder &_fbb) { - TopKV2OptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateTopKV2Options(flatbuffers::FlatBufferBuilder &_fbb, const TopKV2OptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct EmbeddingLookupSparseOptionsT : public flatbuffers::NativeTable { - typedef EmbeddingLookupSparseOptions TableType; - tflite::CombinerType combiner; - EmbeddingLookupSparseOptionsT() - : combiner(tflite::CombinerType_SUM) { - } -}; - -struct EmbeddingLookupSparseOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef EmbeddingLookupSparseOptionsT NativeTableType; - typedef EmbeddingLookupSparseOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_COMBINER = 4 - }; - tflite::CombinerType combiner() const { - return static_cast(GetField(VT_COMBINER, 0)); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_COMBINER) && - verifier.EndTable(); - } - EmbeddingLookupSparseOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(EmbeddingLookupSparseOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const EmbeddingLookupSparseOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct EmbeddingLookupSparseOptionsBuilder { - typedef EmbeddingLookupSparseOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_combiner(tflite::CombinerType combiner) { - fbb_.AddElement(EmbeddingLookupSparseOptions::VT_COMBINER, static_cast(combiner), 0); - } - explicit EmbeddingLookupSparseOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - EmbeddingLookupSparseOptionsBuilder &operator=(const EmbeddingLookupSparseOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateEmbeddingLookupSparseOptions( - flatbuffers::FlatBufferBuilder &_fbb, - tflite::CombinerType combiner = tflite::CombinerType_SUM) { - EmbeddingLookupSparseOptionsBuilder builder_(_fbb); - builder_.add_combiner(combiner); - return builder_.Finish(); -} - -flatbuffers::Offset CreateEmbeddingLookupSparseOptions(flatbuffers::FlatBufferBuilder &_fbb, const EmbeddingLookupSparseOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct GatherOptionsT : public flatbuffers::NativeTable { - typedef GatherOptions TableType; - int32_t axis; - int32_t batch_dims; - GatherOptionsT() - : axis(0), - batch_dims(0) { - } -}; - -struct GatherOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef GatherOptionsT NativeTableType; - typedef GatherOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_AXIS = 4, - VT_BATCH_DIMS = 6 - }; - int32_t axis() const { - return GetField(VT_AXIS, 0); - } - int32_t batch_dims() const { - return GetField(VT_BATCH_DIMS, 0); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_AXIS) && - VerifyField(verifier, VT_BATCH_DIMS) && - verifier.EndTable(); - } - GatherOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(GatherOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const GatherOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct GatherOptionsBuilder { - typedef GatherOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_axis(int32_t axis) { - fbb_.AddElement(GatherOptions::VT_AXIS, axis, 0); - } - void add_batch_dims(int32_t batch_dims) { - fbb_.AddElement(GatherOptions::VT_BATCH_DIMS, batch_dims, 0); - } - explicit GatherOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - GatherOptionsBuilder &operator=(const GatherOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateGatherOptions( - flatbuffers::FlatBufferBuilder &_fbb, - int32_t axis = 0, - int32_t batch_dims = 0) { - GatherOptionsBuilder builder_(_fbb); - builder_.add_batch_dims(batch_dims); - builder_.add_axis(axis); - return builder_.Finish(); -} - -flatbuffers::Offset CreateGatherOptions(flatbuffers::FlatBufferBuilder &_fbb, const GatherOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct TransposeOptionsT : public flatbuffers::NativeTable { - typedef TransposeOptions TableType; - TransposeOptionsT() { - } -}; - -struct TransposeOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef TransposeOptionsT NativeTableType; - typedef TransposeOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - TransposeOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(TransposeOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const TransposeOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct TransposeOptionsBuilder { - typedef TransposeOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit TransposeOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - TransposeOptionsBuilder &operator=(const TransposeOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateTransposeOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - TransposeOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateTransposeOptions(flatbuffers::FlatBufferBuilder &_fbb, const TransposeOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct ExpOptionsT : public flatbuffers::NativeTable { - typedef ExpOptions TableType; - ExpOptionsT() { - } -}; - -struct ExpOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef ExpOptionsT NativeTableType; - typedef ExpOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - ExpOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(ExpOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const ExpOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct ExpOptionsBuilder { - typedef ExpOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit ExpOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - ExpOptionsBuilder &operator=(const ExpOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateExpOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - ExpOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateExpOptions(flatbuffers::FlatBufferBuilder &_fbb, const ExpOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct CosOptionsT : public flatbuffers::NativeTable { - typedef CosOptions TableType; - CosOptionsT() { - } -}; - -struct CosOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef CosOptionsT NativeTableType; - typedef CosOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - CosOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(CosOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const CosOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct CosOptionsBuilder { - typedef CosOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit CosOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - CosOptionsBuilder &operator=(const CosOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateCosOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - CosOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateCosOptions(flatbuffers::FlatBufferBuilder &_fbb, const CosOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct ReducerOptionsT : public flatbuffers::NativeTable { - typedef ReducerOptions TableType; - bool keep_dims; - ReducerOptionsT() - : keep_dims(false) { - } -}; - -struct ReducerOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef ReducerOptionsT NativeTableType; - typedef ReducerOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_KEEP_DIMS = 4 - }; - bool keep_dims() const { - return GetField(VT_KEEP_DIMS, 0) != 0; - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_KEEP_DIMS) && - verifier.EndTable(); - } - ReducerOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(ReducerOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const ReducerOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct ReducerOptionsBuilder { - typedef ReducerOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_keep_dims(bool keep_dims) { - fbb_.AddElement(ReducerOptions::VT_KEEP_DIMS, static_cast(keep_dims), 0); - } - explicit ReducerOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - ReducerOptionsBuilder &operator=(const ReducerOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateReducerOptions( - flatbuffers::FlatBufferBuilder &_fbb, - bool keep_dims = false) { - ReducerOptionsBuilder builder_(_fbb); - builder_.add_keep_dims(keep_dims); - return builder_.Finish(); -} - -flatbuffers::Offset CreateReducerOptions(flatbuffers::FlatBufferBuilder &_fbb, const ReducerOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct SqueezeOptionsT : public flatbuffers::NativeTable { - typedef SqueezeOptions TableType; - std::vector squeeze_dims; - SqueezeOptionsT() { - } -}; - -struct SqueezeOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef SqueezeOptionsT NativeTableType; - typedef SqueezeOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_SQUEEZE_DIMS = 4 - }; - const flatbuffers::Vector *squeeze_dims() const { - return GetPointer *>(VT_SQUEEZE_DIMS); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyOffset(verifier, VT_SQUEEZE_DIMS) && - verifier.VerifyVector(squeeze_dims()) && - verifier.EndTable(); - } - SqueezeOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(SqueezeOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const SqueezeOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct SqueezeOptionsBuilder { - typedef SqueezeOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_squeeze_dims(flatbuffers::Offset> squeeze_dims) { - fbb_.AddOffset(SqueezeOptions::VT_SQUEEZE_DIMS, squeeze_dims); - } - explicit SqueezeOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - SqueezeOptionsBuilder &operator=(const SqueezeOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateSqueezeOptions( - flatbuffers::FlatBufferBuilder &_fbb, - flatbuffers::Offset> squeeze_dims = 0) { - SqueezeOptionsBuilder builder_(_fbb); - builder_.add_squeeze_dims(squeeze_dims); - return builder_.Finish(); -} - -inline flatbuffers::Offset CreateSqueezeOptionsDirect( - flatbuffers::FlatBufferBuilder &_fbb, - const std::vector *squeeze_dims = nullptr) { - auto squeeze_dims__ = squeeze_dims ? _fbb.CreateVector(*squeeze_dims) : 0; - return tflite::CreateSqueezeOptions( - _fbb, - squeeze_dims__); -} - -flatbuffers::Offset CreateSqueezeOptions(flatbuffers::FlatBufferBuilder &_fbb, const SqueezeOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct SplitOptionsT : public flatbuffers::NativeTable { - typedef SplitOptions TableType; - int32_t num_splits; - SplitOptionsT() - : num_splits(0) { - } -}; - -struct SplitOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef SplitOptionsT NativeTableType; - typedef SplitOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_NUM_SPLITS = 4 - }; - int32_t num_splits() const { - return GetField(VT_NUM_SPLITS, 0); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_NUM_SPLITS) && - verifier.EndTable(); - } - SplitOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(SplitOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const SplitOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct SplitOptionsBuilder { - typedef SplitOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_num_splits(int32_t num_splits) { - fbb_.AddElement(SplitOptions::VT_NUM_SPLITS, num_splits, 0); - } - explicit SplitOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - SplitOptionsBuilder &operator=(const SplitOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateSplitOptions( - flatbuffers::FlatBufferBuilder &_fbb, - int32_t num_splits = 0) { - SplitOptionsBuilder builder_(_fbb); - builder_.add_num_splits(num_splits); - return builder_.Finish(); -} - -flatbuffers::Offset CreateSplitOptions(flatbuffers::FlatBufferBuilder &_fbb, const SplitOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct SplitVOptionsT : public flatbuffers::NativeTable { - typedef SplitVOptions TableType; - int32_t num_splits; - SplitVOptionsT() - : num_splits(0) { - } -}; - -struct SplitVOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef SplitVOptionsT NativeTableType; - typedef SplitVOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_NUM_SPLITS = 4 - }; - int32_t num_splits() const { - return GetField(VT_NUM_SPLITS, 0); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_NUM_SPLITS) && - verifier.EndTable(); - } - SplitVOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(SplitVOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const SplitVOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct SplitVOptionsBuilder { - typedef SplitVOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_num_splits(int32_t num_splits) { - fbb_.AddElement(SplitVOptions::VT_NUM_SPLITS, num_splits, 0); - } - explicit SplitVOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - SplitVOptionsBuilder &operator=(const SplitVOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateSplitVOptions( - flatbuffers::FlatBufferBuilder &_fbb, - int32_t num_splits = 0) { - SplitVOptionsBuilder builder_(_fbb); - builder_.add_num_splits(num_splits); - return builder_.Finish(); -} - -flatbuffers::Offset CreateSplitVOptions(flatbuffers::FlatBufferBuilder &_fbb, const SplitVOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct StridedSliceOptionsT : public flatbuffers::NativeTable { - typedef StridedSliceOptions TableType; - int32_t begin_mask; - int32_t end_mask; - int32_t ellipsis_mask; - int32_t new_axis_mask; - int32_t shrink_axis_mask; - StridedSliceOptionsT() - : begin_mask(0), - end_mask(0), - ellipsis_mask(0), - new_axis_mask(0), - shrink_axis_mask(0) { - } -}; - -struct StridedSliceOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef StridedSliceOptionsT NativeTableType; - typedef StridedSliceOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_BEGIN_MASK = 4, - VT_END_MASK = 6, - VT_ELLIPSIS_MASK = 8, - VT_NEW_AXIS_MASK = 10, - VT_SHRINK_AXIS_MASK = 12 - }; - int32_t begin_mask() const { - return GetField(VT_BEGIN_MASK, 0); - } - int32_t end_mask() const { - return GetField(VT_END_MASK, 0); - } - int32_t ellipsis_mask() const { - return GetField(VT_ELLIPSIS_MASK, 0); - } - int32_t new_axis_mask() const { - return GetField(VT_NEW_AXIS_MASK, 0); - } - int32_t shrink_axis_mask() const { - return GetField(VT_SHRINK_AXIS_MASK, 0); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_BEGIN_MASK) && - VerifyField(verifier, VT_END_MASK) && - VerifyField(verifier, VT_ELLIPSIS_MASK) && - VerifyField(verifier, VT_NEW_AXIS_MASK) && - VerifyField(verifier, VT_SHRINK_AXIS_MASK) && - verifier.EndTable(); - } - StridedSliceOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(StridedSliceOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const StridedSliceOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct StridedSliceOptionsBuilder { - typedef StridedSliceOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_begin_mask(int32_t begin_mask) { - fbb_.AddElement(StridedSliceOptions::VT_BEGIN_MASK, begin_mask, 0); - } - void add_end_mask(int32_t end_mask) { - fbb_.AddElement(StridedSliceOptions::VT_END_MASK, end_mask, 0); - } - void add_ellipsis_mask(int32_t ellipsis_mask) { - fbb_.AddElement(StridedSliceOptions::VT_ELLIPSIS_MASK, ellipsis_mask, 0); - } - void add_new_axis_mask(int32_t new_axis_mask) { - fbb_.AddElement(StridedSliceOptions::VT_NEW_AXIS_MASK, new_axis_mask, 0); - } - void add_shrink_axis_mask(int32_t shrink_axis_mask) { - fbb_.AddElement(StridedSliceOptions::VT_SHRINK_AXIS_MASK, shrink_axis_mask, 0); - } - explicit StridedSliceOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - StridedSliceOptionsBuilder &operator=(const StridedSliceOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateStridedSliceOptions( - flatbuffers::FlatBufferBuilder &_fbb, - int32_t begin_mask = 0, - int32_t end_mask = 0, - int32_t ellipsis_mask = 0, - int32_t new_axis_mask = 0, - int32_t shrink_axis_mask = 0) { - StridedSliceOptionsBuilder builder_(_fbb); - builder_.add_shrink_axis_mask(shrink_axis_mask); - builder_.add_new_axis_mask(new_axis_mask); - builder_.add_ellipsis_mask(ellipsis_mask); - builder_.add_end_mask(end_mask); - builder_.add_begin_mask(begin_mask); - return builder_.Finish(); -} - -flatbuffers::Offset CreateStridedSliceOptions(flatbuffers::FlatBufferBuilder &_fbb, const StridedSliceOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct LogSoftmaxOptionsT : public flatbuffers::NativeTable { - typedef LogSoftmaxOptions TableType; - LogSoftmaxOptionsT() { - } -}; - -struct LogSoftmaxOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef LogSoftmaxOptionsT NativeTableType; - typedef LogSoftmaxOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - LogSoftmaxOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(LogSoftmaxOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const LogSoftmaxOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct LogSoftmaxOptionsBuilder { - typedef LogSoftmaxOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit LogSoftmaxOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - LogSoftmaxOptionsBuilder &operator=(const LogSoftmaxOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateLogSoftmaxOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - LogSoftmaxOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateLogSoftmaxOptions(flatbuffers::FlatBufferBuilder &_fbb, const LogSoftmaxOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct CastOptionsT : public flatbuffers::NativeTable { - typedef CastOptions TableType; - tflite::TensorType in_data_type; - tflite::TensorType out_data_type; - CastOptionsT() - : in_data_type(tflite::TensorType_FLOAT32), - out_data_type(tflite::TensorType_FLOAT32) { - } -}; - -struct CastOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef CastOptionsT NativeTableType; - typedef CastOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_IN_DATA_TYPE = 4, - VT_OUT_DATA_TYPE = 6 - }; - tflite::TensorType in_data_type() const { - return static_cast(GetField(VT_IN_DATA_TYPE, 0)); - } - tflite::TensorType out_data_type() const { - return static_cast(GetField(VT_OUT_DATA_TYPE, 0)); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_IN_DATA_TYPE) && - VerifyField(verifier, VT_OUT_DATA_TYPE) && - verifier.EndTable(); - } - CastOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(CastOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const CastOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct CastOptionsBuilder { - typedef CastOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_in_data_type(tflite::TensorType in_data_type) { - fbb_.AddElement(CastOptions::VT_IN_DATA_TYPE, static_cast(in_data_type), 0); - } - void add_out_data_type(tflite::TensorType out_data_type) { - fbb_.AddElement(CastOptions::VT_OUT_DATA_TYPE, static_cast(out_data_type), 0); - } - explicit CastOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - CastOptionsBuilder &operator=(const CastOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateCastOptions( - flatbuffers::FlatBufferBuilder &_fbb, - tflite::TensorType in_data_type = tflite::TensorType_FLOAT32, - tflite::TensorType out_data_type = tflite::TensorType_FLOAT32) { - CastOptionsBuilder builder_(_fbb); - builder_.add_out_data_type(out_data_type); - builder_.add_in_data_type(in_data_type); - return builder_.Finish(); -} - -flatbuffers::Offset CreateCastOptions(flatbuffers::FlatBufferBuilder &_fbb, const CastOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct DequantizeOptionsT : public flatbuffers::NativeTable { - typedef DequantizeOptions TableType; - DequantizeOptionsT() { - } -}; - -struct DequantizeOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef DequantizeOptionsT NativeTableType; - typedef DequantizeOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - DequantizeOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(DequantizeOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const DequantizeOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct DequantizeOptionsBuilder { - typedef DequantizeOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit DequantizeOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - DequantizeOptionsBuilder &operator=(const DequantizeOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateDequantizeOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - DequantizeOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateDequantizeOptions(flatbuffers::FlatBufferBuilder &_fbb, const DequantizeOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct MaximumMinimumOptionsT : public flatbuffers::NativeTable { - typedef MaximumMinimumOptions TableType; - MaximumMinimumOptionsT() { - } -}; - -struct MaximumMinimumOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef MaximumMinimumOptionsT NativeTableType; - typedef MaximumMinimumOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - MaximumMinimumOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(MaximumMinimumOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const MaximumMinimumOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct MaximumMinimumOptionsBuilder { - typedef MaximumMinimumOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit MaximumMinimumOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - MaximumMinimumOptionsBuilder &operator=(const MaximumMinimumOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateMaximumMinimumOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - MaximumMinimumOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateMaximumMinimumOptions(flatbuffers::FlatBufferBuilder &_fbb, const MaximumMinimumOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct TileOptionsT : public flatbuffers::NativeTable { - typedef TileOptions TableType; - TileOptionsT() { - } -}; - -struct TileOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef TileOptionsT NativeTableType; - typedef TileOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - TileOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(TileOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const TileOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct TileOptionsBuilder { - typedef TileOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit TileOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - TileOptionsBuilder &operator=(const TileOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateTileOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - TileOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateTileOptions(flatbuffers::FlatBufferBuilder &_fbb, const TileOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct ArgMaxOptionsT : public flatbuffers::NativeTable { - typedef ArgMaxOptions TableType; - tflite::TensorType output_type; - ArgMaxOptionsT() - : output_type(tflite::TensorType_FLOAT32) { - } -}; - -struct ArgMaxOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef ArgMaxOptionsT NativeTableType; - typedef ArgMaxOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_OUTPUT_TYPE = 4 - }; - tflite::TensorType output_type() const { - return static_cast(GetField(VT_OUTPUT_TYPE, 0)); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_OUTPUT_TYPE) && - verifier.EndTable(); - } - ArgMaxOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(ArgMaxOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const ArgMaxOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct ArgMaxOptionsBuilder { - typedef ArgMaxOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_output_type(tflite::TensorType output_type) { - fbb_.AddElement(ArgMaxOptions::VT_OUTPUT_TYPE, static_cast(output_type), 0); - } - explicit ArgMaxOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - ArgMaxOptionsBuilder &operator=(const ArgMaxOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateArgMaxOptions( - flatbuffers::FlatBufferBuilder &_fbb, - tflite::TensorType output_type = tflite::TensorType_FLOAT32) { - ArgMaxOptionsBuilder builder_(_fbb); - builder_.add_output_type(output_type); - return builder_.Finish(); -} - -flatbuffers::Offset CreateArgMaxOptions(flatbuffers::FlatBufferBuilder &_fbb, const ArgMaxOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct ArgMinOptionsT : public flatbuffers::NativeTable { - typedef ArgMinOptions TableType; - tflite::TensorType output_type; - ArgMinOptionsT() - : output_type(tflite::TensorType_FLOAT32) { - } -}; - -struct ArgMinOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef ArgMinOptionsT NativeTableType; - typedef ArgMinOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_OUTPUT_TYPE = 4 - }; - tflite::TensorType output_type() const { - return static_cast(GetField(VT_OUTPUT_TYPE, 0)); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_OUTPUT_TYPE) && - verifier.EndTable(); - } - ArgMinOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(ArgMinOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const ArgMinOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct ArgMinOptionsBuilder { - typedef ArgMinOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_output_type(tflite::TensorType output_type) { - fbb_.AddElement(ArgMinOptions::VT_OUTPUT_TYPE, static_cast(output_type), 0); - } - explicit ArgMinOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - ArgMinOptionsBuilder &operator=(const ArgMinOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateArgMinOptions( - flatbuffers::FlatBufferBuilder &_fbb, - tflite::TensorType output_type = tflite::TensorType_FLOAT32) { - ArgMinOptionsBuilder builder_(_fbb); - builder_.add_output_type(output_type); - return builder_.Finish(); -} - -flatbuffers::Offset CreateArgMinOptions(flatbuffers::FlatBufferBuilder &_fbb, const ArgMinOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct GreaterOptionsT : public flatbuffers::NativeTable { - typedef GreaterOptions TableType; - GreaterOptionsT() { - } -}; - -struct GreaterOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef GreaterOptionsT NativeTableType; - typedef GreaterOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - GreaterOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(GreaterOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const GreaterOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct GreaterOptionsBuilder { - typedef GreaterOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit GreaterOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - GreaterOptionsBuilder &operator=(const GreaterOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateGreaterOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - GreaterOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateGreaterOptions(flatbuffers::FlatBufferBuilder &_fbb, const GreaterOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct GreaterEqualOptionsT : public flatbuffers::NativeTable { - typedef GreaterEqualOptions TableType; - GreaterEqualOptionsT() { - } -}; - -struct GreaterEqualOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef GreaterEqualOptionsT NativeTableType; - typedef GreaterEqualOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - GreaterEqualOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(GreaterEqualOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const GreaterEqualOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct GreaterEqualOptionsBuilder { - typedef GreaterEqualOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit GreaterEqualOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - GreaterEqualOptionsBuilder &operator=(const GreaterEqualOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateGreaterEqualOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - GreaterEqualOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateGreaterEqualOptions(flatbuffers::FlatBufferBuilder &_fbb, const GreaterEqualOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct LessOptionsT : public flatbuffers::NativeTable { - typedef LessOptions TableType; - LessOptionsT() { - } -}; - -struct LessOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef LessOptionsT NativeTableType; - typedef LessOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - LessOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(LessOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const LessOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct LessOptionsBuilder { - typedef LessOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit LessOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - LessOptionsBuilder &operator=(const LessOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateLessOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - LessOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateLessOptions(flatbuffers::FlatBufferBuilder &_fbb, const LessOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct LessEqualOptionsT : public flatbuffers::NativeTable { - typedef LessEqualOptions TableType; - LessEqualOptionsT() { - } -}; - -struct LessEqualOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef LessEqualOptionsT NativeTableType; - typedef LessEqualOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - LessEqualOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(LessEqualOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const LessEqualOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct LessEqualOptionsBuilder { - typedef LessEqualOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit LessEqualOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - LessEqualOptionsBuilder &operator=(const LessEqualOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateLessEqualOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - LessEqualOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateLessEqualOptions(flatbuffers::FlatBufferBuilder &_fbb, const LessEqualOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct NegOptionsT : public flatbuffers::NativeTable { - typedef NegOptions TableType; - NegOptionsT() { - } -}; - -struct NegOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef NegOptionsT NativeTableType; - typedef NegOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - NegOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(NegOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const NegOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct NegOptionsBuilder { - typedef NegOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit NegOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - NegOptionsBuilder &operator=(const NegOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateNegOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - NegOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateNegOptions(flatbuffers::FlatBufferBuilder &_fbb, const NegOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct SelectOptionsT : public flatbuffers::NativeTable { - typedef SelectOptions TableType; - SelectOptionsT() { - } -}; - -struct SelectOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef SelectOptionsT NativeTableType; - typedef SelectOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - SelectOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(SelectOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const SelectOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct SelectOptionsBuilder { - typedef SelectOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit SelectOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - SelectOptionsBuilder &operator=(const SelectOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateSelectOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - SelectOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateSelectOptions(flatbuffers::FlatBufferBuilder &_fbb, const SelectOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct SliceOptionsT : public flatbuffers::NativeTable { - typedef SliceOptions TableType; - SliceOptionsT() { - } -}; - -struct SliceOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef SliceOptionsT NativeTableType; - typedef SliceOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - SliceOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(SliceOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const SliceOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct SliceOptionsBuilder { - typedef SliceOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit SliceOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - SliceOptionsBuilder &operator=(const SliceOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateSliceOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - SliceOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateSliceOptions(flatbuffers::FlatBufferBuilder &_fbb, const SliceOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct TransposeConvOptionsT : public flatbuffers::NativeTable { - typedef TransposeConvOptions TableType; - tflite::Padding padding; - int32_t stride_w; - int32_t stride_h; - TransposeConvOptionsT() - : padding(tflite::Padding_SAME), - stride_w(0), - stride_h(0) { - } -}; - -struct TransposeConvOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef TransposeConvOptionsT NativeTableType; - typedef TransposeConvOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_PADDING = 4, - VT_STRIDE_W = 6, - VT_STRIDE_H = 8 - }; - tflite::Padding padding() const { - return static_cast(GetField(VT_PADDING, 0)); - } - int32_t stride_w() const { - return GetField(VT_STRIDE_W, 0); - } - int32_t stride_h() const { - return GetField(VT_STRIDE_H, 0); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_PADDING) && - VerifyField(verifier, VT_STRIDE_W) && - VerifyField(verifier, VT_STRIDE_H) && - verifier.EndTable(); - } - TransposeConvOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(TransposeConvOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const TransposeConvOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct TransposeConvOptionsBuilder { - typedef TransposeConvOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_padding(tflite::Padding padding) { - fbb_.AddElement(TransposeConvOptions::VT_PADDING, static_cast(padding), 0); - } - void add_stride_w(int32_t stride_w) { - fbb_.AddElement(TransposeConvOptions::VT_STRIDE_W, stride_w, 0); - } - void add_stride_h(int32_t stride_h) { - fbb_.AddElement(TransposeConvOptions::VT_STRIDE_H, stride_h, 0); - } - explicit TransposeConvOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - TransposeConvOptionsBuilder &operator=(const TransposeConvOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateTransposeConvOptions( - flatbuffers::FlatBufferBuilder &_fbb, - tflite::Padding padding = tflite::Padding_SAME, - int32_t stride_w = 0, - int32_t stride_h = 0) { - TransposeConvOptionsBuilder builder_(_fbb); - builder_.add_stride_h(stride_h); - builder_.add_stride_w(stride_w); - builder_.add_padding(padding); - return builder_.Finish(); -} - -flatbuffers::Offset CreateTransposeConvOptions(flatbuffers::FlatBufferBuilder &_fbb, const TransposeConvOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct ExpandDimsOptionsT : public flatbuffers::NativeTable { - typedef ExpandDimsOptions TableType; - ExpandDimsOptionsT() { - } -}; - -struct ExpandDimsOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef ExpandDimsOptionsT NativeTableType; - typedef ExpandDimsOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - ExpandDimsOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(ExpandDimsOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const ExpandDimsOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct ExpandDimsOptionsBuilder { - typedef ExpandDimsOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit ExpandDimsOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - ExpandDimsOptionsBuilder &operator=(const ExpandDimsOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateExpandDimsOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - ExpandDimsOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateExpandDimsOptions(flatbuffers::FlatBufferBuilder &_fbb, const ExpandDimsOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct SparseToDenseOptionsT : public flatbuffers::NativeTable { - typedef SparseToDenseOptions TableType; - bool validate_indices; - SparseToDenseOptionsT() - : validate_indices(false) { - } -}; - -struct SparseToDenseOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef SparseToDenseOptionsT NativeTableType; - typedef SparseToDenseOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_VALIDATE_INDICES = 4 - }; - bool validate_indices() const { - return GetField(VT_VALIDATE_INDICES, 0) != 0; - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_VALIDATE_INDICES) && - verifier.EndTable(); - } - SparseToDenseOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(SparseToDenseOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const SparseToDenseOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct SparseToDenseOptionsBuilder { - typedef SparseToDenseOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_validate_indices(bool validate_indices) { - fbb_.AddElement(SparseToDenseOptions::VT_VALIDATE_INDICES, static_cast(validate_indices), 0); - } - explicit SparseToDenseOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - SparseToDenseOptionsBuilder &operator=(const SparseToDenseOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateSparseToDenseOptions( - flatbuffers::FlatBufferBuilder &_fbb, - bool validate_indices = false) { - SparseToDenseOptionsBuilder builder_(_fbb); - builder_.add_validate_indices(validate_indices); - return builder_.Finish(); -} - -flatbuffers::Offset CreateSparseToDenseOptions(flatbuffers::FlatBufferBuilder &_fbb, const SparseToDenseOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct EqualOptionsT : public flatbuffers::NativeTable { - typedef EqualOptions TableType; - EqualOptionsT() { - } -}; - -struct EqualOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef EqualOptionsT NativeTableType; - typedef EqualOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - EqualOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(EqualOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const EqualOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct EqualOptionsBuilder { - typedef EqualOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit EqualOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - EqualOptionsBuilder &operator=(const EqualOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateEqualOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - EqualOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateEqualOptions(flatbuffers::FlatBufferBuilder &_fbb, const EqualOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct NotEqualOptionsT : public flatbuffers::NativeTable { - typedef NotEqualOptions TableType; - NotEqualOptionsT() { - } -}; - -struct NotEqualOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef NotEqualOptionsT NativeTableType; - typedef NotEqualOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - NotEqualOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(NotEqualOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const NotEqualOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct NotEqualOptionsBuilder { - typedef NotEqualOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit NotEqualOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - NotEqualOptionsBuilder &operator=(const NotEqualOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateNotEqualOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - NotEqualOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateNotEqualOptions(flatbuffers::FlatBufferBuilder &_fbb, const NotEqualOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct ShapeOptionsT : public flatbuffers::NativeTable { - typedef ShapeOptions TableType; - tflite::TensorType out_type; - ShapeOptionsT() - : out_type(tflite::TensorType_FLOAT32) { - } -}; - -struct ShapeOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef ShapeOptionsT NativeTableType; - typedef ShapeOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_OUT_TYPE = 4 - }; - tflite::TensorType out_type() const { - return static_cast(GetField(VT_OUT_TYPE, 0)); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_OUT_TYPE) && - verifier.EndTable(); - } - ShapeOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(ShapeOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const ShapeOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct ShapeOptionsBuilder { - typedef ShapeOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_out_type(tflite::TensorType out_type) { - fbb_.AddElement(ShapeOptions::VT_OUT_TYPE, static_cast(out_type), 0); - } - explicit ShapeOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - ShapeOptionsBuilder &operator=(const ShapeOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateShapeOptions( - flatbuffers::FlatBufferBuilder &_fbb, - tflite::TensorType out_type = tflite::TensorType_FLOAT32) { - ShapeOptionsBuilder builder_(_fbb); - builder_.add_out_type(out_type); - return builder_.Finish(); -} - -flatbuffers::Offset CreateShapeOptions(flatbuffers::FlatBufferBuilder &_fbb, const ShapeOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct RankOptionsT : public flatbuffers::NativeTable { - typedef RankOptions TableType; - RankOptionsT() { - } -}; - -struct RankOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef RankOptionsT NativeTableType; - typedef RankOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - RankOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(RankOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const RankOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct RankOptionsBuilder { - typedef RankOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit RankOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - RankOptionsBuilder &operator=(const RankOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateRankOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - RankOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateRankOptions(flatbuffers::FlatBufferBuilder &_fbb, const RankOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct PowOptionsT : public flatbuffers::NativeTable { - typedef PowOptions TableType; - PowOptionsT() { - } -}; - -struct PowOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef PowOptionsT NativeTableType; - typedef PowOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - PowOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(PowOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const PowOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct PowOptionsBuilder { - typedef PowOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit PowOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - PowOptionsBuilder &operator=(const PowOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreatePowOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - PowOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreatePowOptions(flatbuffers::FlatBufferBuilder &_fbb, const PowOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct FakeQuantOptionsT : public flatbuffers::NativeTable { - typedef FakeQuantOptions TableType; - float min; - float max; - int32_t num_bits; - bool narrow_range; - FakeQuantOptionsT() - : min(0.0f), - max(0.0f), - num_bits(0), - narrow_range(false) { - } -}; - -struct FakeQuantOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef FakeQuantOptionsT NativeTableType; - typedef FakeQuantOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_MIN = 4, - VT_MAX = 6, - VT_NUM_BITS = 8, - VT_NARROW_RANGE = 10 - }; - float min() const { - return GetField(VT_MIN, 0.0f); - } - float max() const { - return GetField(VT_MAX, 0.0f); - } - int32_t num_bits() const { - return GetField(VT_NUM_BITS, 0); - } - bool narrow_range() const { - return GetField(VT_NARROW_RANGE, 0) != 0; - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_MIN) && - VerifyField(verifier, VT_MAX) && - VerifyField(verifier, VT_NUM_BITS) && - VerifyField(verifier, VT_NARROW_RANGE) && - verifier.EndTable(); - } - FakeQuantOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(FakeQuantOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const FakeQuantOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct FakeQuantOptionsBuilder { - typedef FakeQuantOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_min(float min) { - fbb_.AddElement(FakeQuantOptions::VT_MIN, min, 0.0f); - } - void add_max(float max) { - fbb_.AddElement(FakeQuantOptions::VT_MAX, max, 0.0f); - } - void add_num_bits(int32_t num_bits) { - fbb_.AddElement(FakeQuantOptions::VT_NUM_BITS, num_bits, 0); - } - void add_narrow_range(bool narrow_range) { - fbb_.AddElement(FakeQuantOptions::VT_NARROW_RANGE, static_cast(narrow_range), 0); - } - explicit FakeQuantOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - FakeQuantOptionsBuilder &operator=(const FakeQuantOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateFakeQuantOptions( - flatbuffers::FlatBufferBuilder &_fbb, - float min = 0.0f, - float max = 0.0f, - int32_t num_bits = 0, - bool narrow_range = false) { - FakeQuantOptionsBuilder builder_(_fbb); - builder_.add_num_bits(num_bits); - builder_.add_max(max); - builder_.add_min(min); - builder_.add_narrow_range(narrow_range); - return builder_.Finish(); -} - -flatbuffers::Offset CreateFakeQuantOptions(flatbuffers::FlatBufferBuilder &_fbb, const FakeQuantOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct PackOptionsT : public flatbuffers::NativeTable { - typedef PackOptions TableType; - int32_t values_count; - int32_t axis; - PackOptionsT() - : values_count(0), - axis(0) { - } -}; - -struct PackOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef PackOptionsT NativeTableType; - typedef PackOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_VALUES_COUNT = 4, - VT_AXIS = 6 - }; - int32_t values_count() const { - return GetField(VT_VALUES_COUNT, 0); - } - int32_t axis() const { - return GetField(VT_AXIS, 0); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_VALUES_COUNT) && - VerifyField(verifier, VT_AXIS) && - verifier.EndTable(); - } - PackOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(PackOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const PackOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct PackOptionsBuilder { - typedef PackOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_values_count(int32_t values_count) { - fbb_.AddElement(PackOptions::VT_VALUES_COUNT, values_count, 0); - } - void add_axis(int32_t axis) { - fbb_.AddElement(PackOptions::VT_AXIS, axis, 0); - } - explicit PackOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - PackOptionsBuilder &operator=(const PackOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreatePackOptions( - flatbuffers::FlatBufferBuilder &_fbb, - int32_t values_count = 0, - int32_t axis = 0) { - PackOptionsBuilder builder_(_fbb); - builder_.add_axis(axis); - builder_.add_values_count(values_count); - return builder_.Finish(); -} - -flatbuffers::Offset CreatePackOptions(flatbuffers::FlatBufferBuilder &_fbb, const PackOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct LogicalOrOptionsT : public flatbuffers::NativeTable { - typedef LogicalOrOptions TableType; - LogicalOrOptionsT() { - } -}; - -struct LogicalOrOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef LogicalOrOptionsT NativeTableType; - typedef LogicalOrOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - LogicalOrOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(LogicalOrOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const LogicalOrOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct LogicalOrOptionsBuilder { - typedef LogicalOrOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit LogicalOrOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - LogicalOrOptionsBuilder &operator=(const LogicalOrOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateLogicalOrOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - LogicalOrOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateLogicalOrOptions(flatbuffers::FlatBufferBuilder &_fbb, const LogicalOrOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct OneHotOptionsT : public flatbuffers::NativeTable { - typedef OneHotOptions TableType; - int32_t axis; - OneHotOptionsT() - : axis(0) { - } -}; - -struct OneHotOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef OneHotOptionsT NativeTableType; - typedef OneHotOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_AXIS = 4 - }; - int32_t axis() const { - return GetField(VT_AXIS, 0); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_AXIS) && - verifier.EndTable(); - } - OneHotOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(OneHotOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const OneHotOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct OneHotOptionsBuilder { - typedef OneHotOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_axis(int32_t axis) { - fbb_.AddElement(OneHotOptions::VT_AXIS, axis, 0); - } - explicit OneHotOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - OneHotOptionsBuilder &operator=(const OneHotOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateOneHotOptions( - flatbuffers::FlatBufferBuilder &_fbb, - int32_t axis = 0) { - OneHotOptionsBuilder builder_(_fbb); - builder_.add_axis(axis); - return builder_.Finish(); -} - -flatbuffers::Offset CreateOneHotOptions(flatbuffers::FlatBufferBuilder &_fbb, const OneHotOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct AbsOptionsT : public flatbuffers::NativeTable { - typedef AbsOptions TableType; - AbsOptionsT() { - } -}; - -struct AbsOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef AbsOptionsT NativeTableType; - typedef AbsOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - AbsOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(AbsOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const AbsOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct AbsOptionsBuilder { - typedef AbsOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit AbsOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - AbsOptionsBuilder &operator=(const AbsOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateAbsOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - AbsOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateAbsOptions(flatbuffers::FlatBufferBuilder &_fbb, const AbsOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct HardSwishOptionsT : public flatbuffers::NativeTable { - typedef HardSwishOptions TableType; - HardSwishOptionsT() { - } -}; - -struct HardSwishOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef HardSwishOptionsT NativeTableType; - typedef HardSwishOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - HardSwishOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(HardSwishOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const HardSwishOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct HardSwishOptionsBuilder { - typedef HardSwishOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit HardSwishOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - HardSwishOptionsBuilder &operator=(const HardSwishOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateHardSwishOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - HardSwishOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateHardSwishOptions(flatbuffers::FlatBufferBuilder &_fbb, const HardSwishOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct LogicalAndOptionsT : public flatbuffers::NativeTable { - typedef LogicalAndOptions TableType; - LogicalAndOptionsT() { - } -}; - -struct LogicalAndOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef LogicalAndOptionsT NativeTableType; - typedef LogicalAndOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - LogicalAndOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(LogicalAndOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const LogicalAndOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct LogicalAndOptionsBuilder { - typedef LogicalAndOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit LogicalAndOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - LogicalAndOptionsBuilder &operator=(const LogicalAndOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateLogicalAndOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - LogicalAndOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateLogicalAndOptions(flatbuffers::FlatBufferBuilder &_fbb, const LogicalAndOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct LogicalNotOptionsT : public flatbuffers::NativeTable { - typedef LogicalNotOptions TableType; - LogicalNotOptionsT() { - } -}; - -struct LogicalNotOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef LogicalNotOptionsT NativeTableType; - typedef LogicalNotOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - LogicalNotOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(LogicalNotOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const LogicalNotOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct LogicalNotOptionsBuilder { - typedef LogicalNotOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit LogicalNotOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - LogicalNotOptionsBuilder &operator=(const LogicalNotOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateLogicalNotOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - LogicalNotOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateLogicalNotOptions(flatbuffers::FlatBufferBuilder &_fbb, const LogicalNotOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct UnpackOptionsT : public flatbuffers::NativeTable { - typedef UnpackOptions TableType; - int32_t num; - int32_t axis; - UnpackOptionsT() - : num(0), - axis(0) { - } -}; - -struct UnpackOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef UnpackOptionsT NativeTableType; - typedef UnpackOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_NUM = 4, - VT_AXIS = 6 - }; - int32_t num() const { - return GetField(VT_NUM, 0); - } - int32_t axis() const { - return GetField(VT_AXIS, 0); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_NUM) && - VerifyField(verifier, VT_AXIS) && - verifier.EndTable(); - } - UnpackOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(UnpackOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const UnpackOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct UnpackOptionsBuilder { - typedef UnpackOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_num(int32_t num) { - fbb_.AddElement(UnpackOptions::VT_NUM, num, 0); - } - void add_axis(int32_t axis) { - fbb_.AddElement(UnpackOptions::VT_AXIS, axis, 0); - } - explicit UnpackOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - UnpackOptionsBuilder &operator=(const UnpackOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateUnpackOptions( - flatbuffers::FlatBufferBuilder &_fbb, - int32_t num = 0, - int32_t axis = 0) { - UnpackOptionsBuilder builder_(_fbb); - builder_.add_axis(axis); - builder_.add_num(num); - return builder_.Finish(); -} - -flatbuffers::Offset CreateUnpackOptions(flatbuffers::FlatBufferBuilder &_fbb, const UnpackOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct FloorDivOptionsT : public flatbuffers::NativeTable { - typedef FloorDivOptions TableType; - FloorDivOptionsT() { - } -}; - -struct FloorDivOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef FloorDivOptionsT NativeTableType; - typedef FloorDivOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - FloorDivOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(FloorDivOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const FloorDivOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct FloorDivOptionsBuilder { - typedef FloorDivOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit FloorDivOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - FloorDivOptionsBuilder &operator=(const FloorDivOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateFloorDivOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - FloorDivOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateFloorDivOptions(flatbuffers::FlatBufferBuilder &_fbb, const FloorDivOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct SquareOptionsT : public flatbuffers::NativeTable { - typedef SquareOptions TableType; - SquareOptionsT() { - } -}; - -struct SquareOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef SquareOptionsT NativeTableType; - typedef SquareOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - SquareOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(SquareOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const SquareOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct SquareOptionsBuilder { - typedef SquareOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit SquareOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - SquareOptionsBuilder &operator=(const SquareOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateSquareOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - SquareOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateSquareOptions(flatbuffers::FlatBufferBuilder &_fbb, const SquareOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct ZerosLikeOptionsT : public flatbuffers::NativeTable { - typedef ZerosLikeOptions TableType; - ZerosLikeOptionsT() { - } -}; - -struct ZerosLikeOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef ZerosLikeOptionsT NativeTableType; - typedef ZerosLikeOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - ZerosLikeOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(ZerosLikeOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const ZerosLikeOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct ZerosLikeOptionsBuilder { - typedef ZerosLikeOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit ZerosLikeOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - ZerosLikeOptionsBuilder &operator=(const ZerosLikeOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateZerosLikeOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - ZerosLikeOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateZerosLikeOptions(flatbuffers::FlatBufferBuilder &_fbb, const ZerosLikeOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct FillOptionsT : public flatbuffers::NativeTable { - typedef FillOptions TableType; - FillOptionsT() { - } -}; - -struct FillOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef FillOptionsT NativeTableType; - typedef FillOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - FillOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(FillOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const FillOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct FillOptionsBuilder { - typedef FillOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit FillOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - FillOptionsBuilder &operator=(const FillOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateFillOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - FillOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateFillOptions(flatbuffers::FlatBufferBuilder &_fbb, const FillOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct FloorModOptionsT : public flatbuffers::NativeTable { - typedef FloorModOptions TableType; - FloorModOptionsT() { - } -}; - -struct FloorModOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef FloorModOptionsT NativeTableType; - typedef FloorModOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - FloorModOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(FloorModOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const FloorModOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct FloorModOptionsBuilder { - typedef FloorModOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit FloorModOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - FloorModOptionsBuilder &operator=(const FloorModOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateFloorModOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - FloorModOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateFloorModOptions(flatbuffers::FlatBufferBuilder &_fbb, const FloorModOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct RangeOptionsT : public flatbuffers::NativeTable { - typedef RangeOptions TableType; - RangeOptionsT() { - } -}; - -struct RangeOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef RangeOptionsT NativeTableType; - typedef RangeOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - RangeOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(RangeOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const RangeOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct RangeOptionsBuilder { - typedef RangeOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit RangeOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - RangeOptionsBuilder &operator=(const RangeOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateRangeOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - RangeOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateRangeOptions(flatbuffers::FlatBufferBuilder &_fbb, const RangeOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct LeakyReluOptionsT : public flatbuffers::NativeTable { - typedef LeakyReluOptions TableType; - float alpha; - LeakyReluOptionsT() - : alpha(0.0f) { - } -}; - -struct LeakyReluOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef LeakyReluOptionsT NativeTableType; - typedef LeakyReluOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_ALPHA = 4 - }; - float alpha() const { - return GetField(VT_ALPHA, 0.0f); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_ALPHA) && - verifier.EndTable(); - } - LeakyReluOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(LeakyReluOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const LeakyReluOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct LeakyReluOptionsBuilder { - typedef LeakyReluOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_alpha(float alpha) { - fbb_.AddElement(LeakyReluOptions::VT_ALPHA, alpha, 0.0f); - } - explicit LeakyReluOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - LeakyReluOptionsBuilder &operator=(const LeakyReluOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateLeakyReluOptions( - flatbuffers::FlatBufferBuilder &_fbb, - float alpha = 0.0f) { - LeakyReluOptionsBuilder builder_(_fbb); - builder_.add_alpha(alpha); - return builder_.Finish(); -} - -flatbuffers::Offset CreateLeakyReluOptions(flatbuffers::FlatBufferBuilder &_fbb, const LeakyReluOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct SquaredDifferenceOptionsT : public flatbuffers::NativeTable { - typedef SquaredDifferenceOptions TableType; - SquaredDifferenceOptionsT() { - } -}; - -struct SquaredDifferenceOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef SquaredDifferenceOptionsT NativeTableType; - typedef SquaredDifferenceOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - SquaredDifferenceOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(SquaredDifferenceOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const SquaredDifferenceOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct SquaredDifferenceOptionsBuilder { - typedef SquaredDifferenceOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit SquaredDifferenceOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - SquaredDifferenceOptionsBuilder &operator=(const SquaredDifferenceOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateSquaredDifferenceOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - SquaredDifferenceOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateSquaredDifferenceOptions(flatbuffers::FlatBufferBuilder &_fbb, const SquaredDifferenceOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct MirrorPadOptionsT : public flatbuffers::NativeTable { - typedef MirrorPadOptions TableType; - tflite::MirrorPadMode mode; - MirrorPadOptionsT() - : mode(tflite::MirrorPadMode_REFLECT) { - } -}; - -struct MirrorPadOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef MirrorPadOptionsT NativeTableType; - typedef MirrorPadOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_MODE = 4 - }; - tflite::MirrorPadMode mode() const { - return static_cast(GetField(VT_MODE, 0)); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_MODE) && - verifier.EndTable(); - } - MirrorPadOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(MirrorPadOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const MirrorPadOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct MirrorPadOptionsBuilder { - typedef MirrorPadOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_mode(tflite::MirrorPadMode mode) { - fbb_.AddElement(MirrorPadOptions::VT_MODE, static_cast(mode), 0); - } - explicit MirrorPadOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - MirrorPadOptionsBuilder &operator=(const MirrorPadOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateMirrorPadOptions( - flatbuffers::FlatBufferBuilder &_fbb, - tflite::MirrorPadMode mode = tflite::MirrorPadMode_REFLECT) { - MirrorPadOptionsBuilder builder_(_fbb); - builder_.add_mode(mode); - return builder_.Finish(); -} - -flatbuffers::Offset CreateMirrorPadOptions(flatbuffers::FlatBufferBuilder &_fbb, const MirrorPadOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct UniqueOptionsT : public flatbuffers::NativeTable { - typedef UniqueOptions TableType; - tflite::TensorType idx_out_type; - UniqueOptionsT() - : idx_out_type(tflite::TensorType_INT32) { - } -}; - -struct UniqueOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef UniqueOptionsT NativeTableType; - typedef UniqueOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_IDX_OUT_TYPE = 4 - }; - tflite::TensorType idx_out_type() const { - return static_cast(GetField(VT_IDX_OUT_TYPE, 2)); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_IDX_OUT_TYPE) && - verifier.EndTable(); - } - UniqueOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(UniqueOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const UniqueOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct UniqueOptionsBuilder { - typedef UniqueOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_idx_out_type(tflite::TensorType idx_out_type) { - fbb_.AddElement(UniqueOptions::VT_IDX_OUT_TYPE, static_cast(idx_out_type), 2); - } - explicit UniqueOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - UniqueOptionsBuilder &operator=(const UniqueOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateUniqueOptions( - flatbuffers::FlatBufferBuilder &_fbb, - tflite::TensorType idx_out_type = tflite::TensorType_INT32) { - UniqueOptionsBuilder builder_(_fbb); - builder_.add_idx_out_type(idx_out_type); - return builder_.Finish(); -} - -flatbuffers::Offset CreateUniqueOptions(flatbuffers::FlatBufferBuilder &_fbb, const UniqueOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct ReverseV2OptionsT : public flatbuffers::NativeTable { - typedef ReverseV2Options TableType; - ReverseV2OptionsT() { - } -}; - -struct ReverseV2Options FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef ReverseV2OptionsT NativeTableType; - typedef ReverseV2OptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - ReverseV2OptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(ReverseV2OptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const ReverseV2OptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct ReverseV2OptionsBuilder { - typedef ReverseV2Options Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit ReverseV2OptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - ReverseV2OptionsBuilder &operator=(const ReverseV2OptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateReverseV2Options( - flatbuffers::FlatBufferBuilder &_fbb) { - ReverseV2OptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateReverseV2Options(flatbuffers::FlatBufferBuilder &_fbb, const ReverseV2OptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct AddNOptionsT : public flatbuffers::NativeTable { - typedef AddNOptions TableType; - AddNOptionsT() { - } -}; - -struct AddNOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef AddNOptionsT NativeTableType; - typedef AddNOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - AddNOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(AddNOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const AddNOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct AddNOptionsBuilder { - typedef AddNOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit AddNOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - AddNOptionsBuilder &operator=(const AddNOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateAddNOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - AddNOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateAddNOptions(flatbuffers::FlatBufferBuilder &_fbb, const AddNOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct GatherNdOptionsT : public flatbuffers::NativeTable { - typedef GatherNdOptions TableType; - GatherNdOptionsT() { - } -}; - -struct GatherNdOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef GatherNdOptionsT NativeTableType; - typedef GatherNdOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - GatherNdOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(GatherNdOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const GatherNdOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct GatherNdOptionsBuilder { - typedef GatherNdOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit GatherNdOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - GatherNdOptionsBuilder &operator=(const GatherNdOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateGatherNdOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - GatherNdOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateGatherNdOptions(flatbuffers::FlatBufferBuilder &_fbb, const GatherNdOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct WhereOptionsT : public flatbuffers::NativeTable { - typedef WhereOptions TableType; - WhereOptionsT() { - } -}; - -struct WhereOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef WhereOptionsT NativeTableType; - typedef WhereOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - WhereOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(WhereOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const WhereOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct WhereOptionsBuilder { - typedef WhereOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit WhereOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - WhereOptionsBuilder &operator=(const WhereOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateWhereOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - WhereOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateWhereOptions(flatbuffers::FlatBufferBuilder &_fbb, const WhereOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct ReverseSequenceOptionsT : public flatbuffers::NativeTable { - typedef ReverseSequenceOptions TableType; - int32_t seq_dim; - int32_t batch_dim; - ReverseSequenceOptionsT() - : seq_dim(0), - batch_dim(0) { - } -}; - -struct ReverseSequenceOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef ReverseSequenceOptionsT NativeTableType; - typedef ReverseSequenceOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_SEQ_DIM = 4, - VT_BATCH_DIM = 6 - }; - int32_t seq_dim() const { - return GetField(VT_SEQ_DIM, 0); - } - int32_t batch_dim() const { - return GetField(VT_BATCH_DIM, 0); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_SEQ_DIM) && - VerifyField(verifier, VT_BATCH_DIM) && - verifier.EndTable(); - } - ReverseSequenceOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(ReverseSequenceOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const ReverseSequenceOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct ReverseSequenceOptionsBuilder { - typedef ReverseSequenceOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_seq_dim(int32_t seq_dim) { - fbb_.AddElement(ReverseSequenceOptions::VT_SEQ_DIM, seq_dim, 0); - } - void add_batch_dim(int32_t batch_dim) { - fbb_.AddElement(ReverseSequenceOptions::VT_BATCH_DIM, batch_dim, 0); - } - explicit ReverseSequenceOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - ReverseSequenceOptionsBuilder &operator=(const ReverseSequenceOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateReverseSequenceOptions( - flatbuffers::FlatBufferBuilder &_fbb, - int32_t seq_dim = 0, - int32_t batch_dim = 0) { - ReverseSequenceOptionsBuilder builder_(_fbb); - builder_.add_batch_dim(batch_dim); - builder_.add_seq_dim(seq_dim); - return builder_.Finish(); -} - -flatbuffers::Offset CreateReverseSequenceOptions(flatbuffers::FlatBufferBuilder &_fbb, const ReverseSequenceOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct MatrixDiagOptionsT : public flatbuffers::NativeTable { - typedef MatrixDiagOptions TableType; - MatrixDiagOptionsT() { - } -}; - -struct MatrixDiagOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef MatrixDiagOptionsT NativeTableType; - typedef MatrixDiagOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - MatrixDiagOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(MatrixDiagOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const MatrixDiagOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct MatrixDiagOptionsBuilder { - typedef MatrixDiagOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit MatrixDiagOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - MatrixDiagOptionsBuilder &operator=(const MatrixDiagOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateMatrixDiagOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - MatrixDiagOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateMatrixDiagOptions(flatbuffers::FlatBufferBuilder &_fbb, const MatrixDiagOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct QuantizeOptionsT : public flatbuffers::NativeTable { - typedef QuantizeOptions TableType; - QuantizeOptionsT() { - } -}; - -struct QuantizeOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef QuantizeOptionsT NativeTableType; - typedef QuantizeOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - QuantizeOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(QuantizeOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const QuantizeOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct QuantizeOptionsBuilder { - typedef QuantizeOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit QuantizeOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - QuantizeOptionsBuilder &operator=(const QuantizeOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateQuantizeOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - QuantizeOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateQuantizeOptions(flatbuffers::FlatBufferBuilder &_fbb, const QuantizeOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct MatrixSetDiagOptionsT : public flatbuffers::NativeTable { - typedef MatrixSetDiagOptions TableType; - MatrixSetDiagOptionsT() { - } -}; - -struct MatrixSetDiagOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef MatrixSetDiagOptionsT NativeTableType; - typedef MatrixSetDiagOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - MatrixSetDiagOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(MatrixSetDiagOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const MatrixSetDiagOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct MatrixSetDiagOptionsBuilder { - typedef MatrixSetDiagOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit MatrixSetDiagOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - MatrixSetDiagOptionsBuilder &operator=(const MatrixSetDiagOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateMatrixSetDiagOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - MatrixSetDiagOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateMatrixSetDiagOptions(flatbuffers::FlatBufferBuilder &_fbb, const MatrixSetDiagOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct IfOptionsT : public flatbuffers::NativeTable { - typedef IfOptions TableType; - int32_t then_subgraph_index; - int32_t else_subgraph_index; - IfOptionsT() - : then_subgraph_index(0), - else_subgraph_index(0) { - } -}; - -struct IfOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef IfOptionsT NativeTableType; - typedef IfOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_THEN_SUBGRAPH_INDEX = 4, - VT_ELSE_SUBGRAPH_INDEX = 6 - }; - int32_t then_subgraph_index() const { - return GetField(VT_THEN_SUBGRAPH_INDEX, 0); - } - int32_t else_subgraph_index() const { - return GetField(VT_ELSE_SUBGRAPH_INDEX, 0); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_THEN_SUBGRAPH_INDEX) && - VerifyField(verifier, VT_ELSE_SUBGRAPH_INDEX) && - verifier.EndTable(); - } - IfOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(IfOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const IfOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct IfOptionsBuilder { - typedef IfOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_then_subgraph_index(int32_t then_subgraph_index) { - fbb_.AddElement(IfOptions::VT_THEN_SUBGRAPH_INDEX, then_subgraph_index, 0); - } - void add_else_subgraph_index(int32_t else_subgraph_index) { - fbb_.AddElement(IfOptions::VT_ELSE_SUBGRAPH_INDEX, else_subgraph_index, 0); - } - explicit IfOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - IfOptionsBuilder &operator=(const IfOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateIfOptions( - flatbuffers::FlatBufferBuilder &_fbb, - int32_t then_subgraph_index = 0, - int32_t else_subgraph_index = 0) { - IfOptionsBuilder builder_(_fbb); - builder_.add_else_subgraph_index(else_subgraph_index); - builder_.add_then_subgraph_index(then_subgraph_index); - return builder_.Finish(); -} - -flatbuffers::Offset CreateIfOptions(flatbuffers::FlatBufferBuilder &_fbb, const IfOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct CallOnceOptionsT : public flatbuffers::NativeTable { - typedef CallOnceOptions TableType; - int32_t init_subgraph_index; - CallOnceOptionsT() - : init_subgraph_index(0) { - } -}; - -struct CallOnceOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef CallOnceOptionsT NativeTableType; - typedef CallOnceOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_INIT_SUBGRAPH_INDEX = 4 - }; - int32_t init_subgraph_index() const { - return GetField(VT_INIT_SUBGRAPH_INDEX, 0); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_INIT_SUBGRAPH_INDEX) && - verifier.EndTable(); - } - CallOnceOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(CallOnceOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const CallOnceOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct CallOnceOptionsBuilder { - typedef CallOnceOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_init_subgraph_index(int32_t init_subgraph_index) { - fbb_.AddElement(CallOnceOptions::VT_INIT_SUBGRAPH_INDEX, init_subgraph_index, 0); - } - explicit CallOnceOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - CallOnceOptionsBuilder &operator=(const CallOnceOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateCallOnceOptions( - flatbuffers::FlatBufferBuilder &_fbb, - int32_t init_subgraph_index = 0) { - CallOnceOptionsBuilder builder_(_fbb); - builder_.add_init_subgraph_index(init_subgraph_index); - return builder_.Finish(); -} - -flatbuffers::Offset CreateCallOnceOptions(flatbuffers::FlatBufferBuilder &_fbb, const CallOnceOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct WhileOptionsT : public flatbuffers::NativeTable { - typedef WhileOptions TableType; - int32_t cond_subgraph_index; - int32_t body_subgraph_index; - WhileOptionsT() - : cond_subgraph_index(0), - body_subgraph_index(0) { - } -}; - -struct WhileOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef WhileOptionsT NativeTableType; - typedef WhileOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_COND_SUBGRAPH_INDEX = 4, - VT_BODY_SUBGRAPH_INDEX = 6 - }; - int32_t cond_subgraph_index() const { - return GetField(VT_COND_SUBGRAPH_INDEX, 0); - } - int32_t body_subgraph_index() const { - return GetField(VT_BODY_SUBGRAPH_INDEX, 0); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_COND_SUBGRAPH_INDEX) && - VerifyField(verifier, VT_BODY_SUBGRAPH_INDEX) && - verifier.EndTable(); - } - WhileOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(WhileOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const WhileOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct WhileOptionsBuilder { - typedef WhileOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_cond_subgraph_index(int32_t cond_subgraph_index) { - fbb_.AddElement(WhileOptions::VT_COND_SUBGRAPH_INDEX, cond_subgraph_index, 0); - } - void add_body_subgraph_index(int32_t body_subgraph_index) { - fbb_.AddElement(WhileOptions::VT_BODY_SUBGRAPH_INDEX, body_subgraph_index, 0); - } - explicit WhileOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - WhileOptionsBuilder &operator=(const WhileOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateWhileOptions( - flatbuffers::FlatBufferBuilder &_fbb, - int32_t cond_subgraph_index = 0, - int32_t body_subgraph_index = 0) { - WhileOptionsBuilder builder_(_fbb); - builder_.add_body_subgraph_index(body_subgraph_index); - builder_.add_cond_subgraph_index(cond_subgraph_index); - return builder_.Finish(); -} - -flatbuffers::Offset CreateWhileOptions(flatbuffers::FlatBufferBuilder &_fbb, const WhileOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct NonMaxSuppressionV4OptionsT : public flatbuffers::NativeTable { - typedef NonMaxSuppressionV4Options TableType; - NonMaxSuppressionV4OptionsT() { - } -}; - -struct NonMaxSuppressionV4Options FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef NonMaxSuppressionV4OptionsT NativeTableType; - typedef NonMaxSuppressionV4OptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - NonMaxSuppressionV4OptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(NonMaxSuppressionV4OptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const NonMaxSuppressionV4OptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct NonMaxSuppressionV4OptionsBuilder { - typedef NonMaxSuppressionV4Options Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit NonMaxSuppressionV4OptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - NonMaxSuppressionV4OptionsBuilder &operator=(const NonMaxSuppressionV4OptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateNonMaxSuppressionV4Options( - flatbuffers::FlatBufferBuilder &_fbb) { - NonMaxSuppressionV4OptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateNonMaxSuppressionV4Options(flatbuffers::FlatBufferBuilder &_fbb, const NonMaxSuppressionV4OptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct NonMaxSuppressionV5OptionsT : public flatbuffers::NativeTable { - typedef NonMaxSuppressionV5Options TableType; - NonMaxSuppressionV5OptionsT() { - } -}; - -struct NonMaxSuppressionV5Options FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef NonMaxSuppressionV5OptionsT NativeTableType; - typedef NonMaxSuppressionV5OptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - NonMaxSuppressionV5OptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(NonMaxSuppressionV5OptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const NonMaxSuppressionV5OptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct NonMaxSuppressionV5OptionsBuilder { - typedef NonMaxSuppressionV5Options Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit NonMaxSuppressionV5OptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - NonMaxSuppressionV5OptionsBuilder &operator=(const NonMaxSuppressionV5OptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateNonMaxSuppressionV5Options( - flatbuffers::FlatBufferBuilder &_fbb) { - NonMaxSuppressionV5OptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateNonMaxSuppressionV5Options(flatbuffers::FlatBufferBuilder &_fbb, const NonMaxSuppressionV5OptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct ScatterNdOptionsT : public flatbuffers::NativeTable { - typedef ScatterNdOptions TableType; - ScatterNdOptionsT() { - } -}; - -struct ScatterNdOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef ScatterNdOptionsT NativeTableType; - typedef ScatterNdOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - ScatterNdOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(ScatterNdOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const ScatterNdOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct ScatterNdOptionsBuilder { - typedef ScatterNdOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit ScatterNdOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - ScatterNdOptionsBuilder &operator=(const ScatterNdOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateScatterNdOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - ScatterNdOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateScatterNdOptions(flatbuffers::FlatBufferBuilder &_fbb, const ScatterNdOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct SelectV2OptionsT : public flatbuffers::NativeTable { - typedef SelectV2Options TableType; - SelectV2OptionsT() { - } -}; - -struct SelectV2Options FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef SelectV2OptionsT NativeTableType; - typedef SelectV2OptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - SelectV2OptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(SelectV2OptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const SelectV2OptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct SelectV2OptionsBuilder { - typedef SelectV2Options Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit SelectV2OptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - SelectV2OptionsBuilder &operator=(const SelectV2OptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateSelectV2Options( - flatbuffers::FlatBufferBuilder &_fbb) { - SelectV2OptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateSelectV2Options(flatbuffers::FlatBufferBuilder &_fbb, const SelectV2OptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct DensifyOptionsT : public flatbuffers::NativeTable { - typedef DensifyOptions TableType; - DensifyOptionsT() { - } -}; - -struct DensifyOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef DensifyOptionsT NativeTableType; - typedef DensifyOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - DensifyOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(DensifyOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const DensifyOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct DensifyOptionsBuilder { - typedef DensifyOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit DensifyOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - DensifyOptionsBuilder &operator=(const DensifyOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateDensifyOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - DensifyOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateDensifyOptions(flatbuffers::FlatBufferBuilder &_fbb, const DensifyOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct SegmentSumOptionsT : public flatbuffers::NativeTable { - typedef SegmentSumOptions TableType; - SegmentSumOptionsT() { - } -}; - -struct SegmentSumOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef SegmentSumOptionsT NativeTableType; - typedef SegmentSumOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - SegmentSumOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(SegmentSumOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const SegmentSumOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct SegmentSumOptionsBuilder { - typedef SegmentSumOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit SegmentSumOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - SegmentSumOptionsBuilder &operator=(const SegmentSumOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateSegmentSumOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - SegmentSumOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateSegmentSumOptions(flatbuffers::FlatBufferBuilder &_fbb, const SegmentSumOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct BatchMatMulOptionsT : public flatbuffers::NativeTable { - typedef BatchMatMulOptions TableType; - bool adj_x; - bool adj_y; - bool asymmetric_quantize_inputs; - BatchMatMulOptionsT() - : adj_x(false), - adj_y(false), - asymmetric_quantize_inputs(false) { - } -}; - -struct BatchMatMulOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef BatchMatMulOptionsT NativeTableType; - typedef BatchMatMulOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_ADJ_X = 4, - VT_ADJ_Y = 6, - VT_ASYMMETRIC_QUANTIZE_INPUTS = 8 - }; - bool adj_x() const { - return GetField(VT_ADJ_X, 0) != 0; - } - bool adj_y() const { - return GetField(VT_ADJ_Y, 0) != 0; - } - bool asymmetric_quantize_inputs() const { - return GetField(VT_ASYMMETRIC_QUANTIZE_INPUTS, 0) != 0; - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_ADJ_X) && - VerifyField(verifier, VT_ADJ_Y) && - VerifyField(verifier, VT_ASYMMETRIC_QUANTIZE_INPUTS) && - verifier.EndTable(); - } - BatchMatMulOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(BatchMatMulOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const BatchMatMulOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct BatchMatMulOptionsBuilder { - typedef BatchMatMulOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_adj_x(bool adj_x) { - fbb_.AddElement(BatchMatMulOptions::VT_ADJ_X, static_cast(adj_x), 0); - } - void add_adj_y(bool adj_y) { - fbb_.AddElement(BatchMatMulOptions::VT_ADJ_Y, static_cast(adj_y), 0); - } - void add_asymmetric_quantize_inputs(bool asymmetric_quantize_inputs) { - fbb_.AddElement(BatchMatMulOptions::VT_ASYMMETRIC_QUANTIZE_INPUTS, static_cast(asymmetric_quantize_inputs), 0); - } - explicit BatchMatMulOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - BatchMatMulOptionsBuilder &operator=(const BatchMatMulOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateBatchMatMulOptions( - flatbuffers::FlatBufferBuilder &_fbb, - bool adj_x = false, - bool adj_y = false, - bool asymmetric_quantize_inputs = false) { - BatchMatMulOptionsBuilder builder_(_fbb); - builder_.add_asymmetric_quantize_inputs(asymmetric_quantize_inputs); - builder_.add_adj_y(adj_y); - builder_.add_adj_x(adj_x); - return builder_.Finish(); -} - -flatbuffers::Offset CreateBatchMatMulOptions(flatbuffers::FlatBufferBuilder &_fbb, const BatchMatMulOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct CumsumOptionsT : public flatbuffers::NativeTable { - typedef CumsumOptions TableType; - bool exclusive; - bool reverse; - CumsumOptionsT() - : exclusive(false), - reverse(false) { - } -}; - -struct CumsumOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef CumsumOptionsT NativeTableType; - typedef CumsumOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_EXCLUSIVE = 4, - VT_REVERSE = 6 - }; - bool exclusive() const { - return GetField(VT_EXCLUSIVE, 0) != 0; - } - bool reverse() const { - return GetField(VT_REVERSE, 0) != 0; - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_EXCLUSIVE) && - VerifyField(verifier, VT_REVERSE) && - verifier.EndTable(); - } - CumsumOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(CumsumOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const CumsumOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct CumsumOptionsBuilder { - typedef CumsumOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_exclusive(bool exclusive) { - fbb_.AddElement(CumsumOptions::VT_EXCLUSIVE, static_cast(exclusive), 0); - } - void add_reverse(bool reverse) { - fbb_.AddElement(CumsumOptions::VT_REVERSE, static_cast(reverse), 0); - } - explicit CumsumOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - CumsumOptionsBuilder &operator=(const CumsumOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateCumsumOptions( - flatbuffers::FlatBufferBuilder &_fbb, - bool exclusive = false, - bool reverse = false) { - CumsumOptionsBuilder builder_(_fbb); - builder_.add_reverse(reverse); - builder_.add_exclusive(exclusive); - return builder_.Finish(); -} - -flatbuffers::Offset CreateCumsumOptions(flatbuffers::FlatBufferBuilder &_fbb, const CumsumOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct BroadcastToOptionsT : public flatbuffers::NativeTable { - typedef BroadcastToOptions TableType; - BroadcastToOptionsT() { - } -}; - -struct BroadcastToOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef BroadcastToOptionsT NativeTableType; - typedef BroadcastToOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - BroadcastToOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(BroadcastToOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const BroadcastToOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct BroadcastToOptionsBuilder { - typedef BroadcastToOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit BroadcastToOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - BroadcastToOptionsBuilder &operator=(const BroadcastToOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateBroadcastToOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - BroadcastToOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateBroadcastToOptions(flatbuffers::FlatBufferBuilder &_fbb, const BroadcastToOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct Rfft2dOptionsT : public flatbuffers::NativeTable { - typedef Rfft2dOptions TableType; - Rfft2dOptionsT() { - } -}; - -struct Rfft2dOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef Rfft2dOptionsT NativeTableType; - typedef Rfft2dOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - Rfft2dOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(Rfft2dOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const Rfft2dOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct Rfft2dOptionsBuilder { - typedef Rfft2dOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit Rfft2dOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - Rfft2dOptionsBuilder &operator=(const Rfft2dOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateRfft2dOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - Rfft2dOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateRfft2dOptions(flatbuffers::FlatBufferBuilder &_fbb, const Rfft2dOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct HashtableOptionsT : public flatbuffers::NativeTable { - typedef HashtableOptions TableType; - int32_t table_id; - tflite::TensorType key_dtype; - tflite::TensorType value_dtype; - HashtableOptionsT() - : table_id(0), - key_dtype(tflite::TensorType_FLOAT32), - value_dtype(tflite::TensorType_FLOAT32) { - } -}; - -struct HashtableOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef HashtableOptionsT NativeTableType; - typedef HashtableOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_TABLE_ID = 4, - VT_KEY_DTYPE = 6, - VT_VALUE_DTYPE = 8 - }; - int32_t table_id() const { - return GetField(VT_TABLE_ID, 0); - } - tflite::TensorType key_dtype() const { - return static_cast(GetField(VT_KEY_DTYPE, 0)); - } - tflite::TensorType value_dtype() const { - return static_cast(GetField(VT_VALUE_DTYPE, 0)); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_TABLE_ID) && - VerifyField(verifier, VT_KEY_DTYPE) && - VerifyField(verifier, VT_VALUE_DTYPE) && - verifier.EndTable(); - } - HashtableOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(HashtableOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const HashtableOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct HashtableOptionsBuilder { - typedef HashtableOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_table_id(int32_t table_id) { - fbb_.AddElement(HashtableOptions::VT_TABLE_ID, table_id, 0); - } - void add_key_dtype(tflite::TensorType key_dtype) { - fbb_.AddElement(HashtableOptions::VT_KEY_DTYPE, static_cast(key_dtype), 0); - } - void add_value_dtype(tflite::TensorType value_dtype) { - fbb_.AddElement(HashtableOptions::VT_VALUE_DTYPE, static_cast(value_dtype), 0); - } - explicit HashtableOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - HashtableOptionsBuilder &operator=(const HashtableOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateHashtableOptions( - flatbuffers::FlatBufferBuilder &_fbb, - int32_t table_id = 0, - tflite::TensorType key_dtype = tflite::TensorType_FLOAT32, - tflite::TensorType value_dtype = tflite::TensorType_FLOAT32) { - HashtableOptionsBuilder builder_(_fbb); - builder_.add_table_id(table_id); - builder_.add_value_dtype(value_dtype); - builder_.add_key_dtype(key_dtype); - return builder_.Finish(); -} - -flatbuffers::Offset CreateHashtableOptions(flatbuffers::FlatBufferBuilder &_fbb, const HashtableOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct HashtableFindOptionsT : public flatbuffers::NativeTable { - typedef HashtableFindOptions TableType; - HashtableFindOptionsT() { - } -}; - -struct HashtableFindOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef HashtableFindOptionsT NativeTableType; - typedef HashtableFindOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - HashtableFindOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(HashtableFindOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const HashtableFindOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct HashtableFindOptionsBuilder { - typedef HashtableFindOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit HashtableFindOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - HashtableFindOptionsBuilder &operator=(const HashtableFindOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateHashtableFindOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - HashtableFindOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateHashtableFindOptions(flatbuffers::FlatBufferBuilder &_fbb, const HashtableFindOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct HashtableImportOptionsT : public flatbuffers::NativeTable { - typedef HashtableImportOptions TableType; - HashtableImportOptionsT() { - } -}; - -struct HashtableImportOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef HashtableImportOptionsT NativeTableType; - typedef HashtableImportOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - HashtableImportOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(HashtableImportOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const HashtableImportOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct HashtableImportOptionsBuilder { - typedef HashtableImportOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit HashtableImportOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - HashtableImportOptionsBuilder &operator=(const HashtableImportOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateHashtableImportOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - HashtableImportOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateHashtableImportOptions(flatbuffers::FlatBufferBuilder &_fbb, const HashtableImportOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct HashtableSizeOptionsT : public flatbuffers::NativeTable { - typedef HashtableSizeOptions TableType; - HashtableSizeOptionsT() { - } -}; - -struct HashtableSizeOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef HashtableSizeOptionsT NativeTableType; - typedef HashtableSizeOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - HashtableSizeOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(HashtableSizeOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const HashtableSizeOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct HashtableSizeOptionsBuilder { - typedef HashtableSizeOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit HashtableSizeOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - HashtableSizeOptionsBuilder &operator=(const HashtableSizeOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateHashtableSizeOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - HashtableSizeOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateHashtableSizeOptions(flatbuffers::FlatBufferBuilder &_fbb, const HashtableSizeOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct VarHandleOptionsT : public flatbuffers::NativeTable { - typedef VarHandleOptions TableType; - std::string container; - std::string shared_name; - VarHandleOptionsT() { - } -}; - -struct VarHandleOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef VarHandleOptionsT NativeTableType; - typedef VarHandleOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_CONTAINER = 4, - VT_SHARED_NAME = 6 - }; - const flatbuffers::String *container() const { - return GetPointer(VT_CONTAINER); - } - const flatbuffers::String *shared_name() const { - return GetPointer(VT_SHARED_NAME); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyOffset(verifier, VT_CONTAINER) && - verifier.VerifyString(container()) && - VerifyOffset(verifier, VT_SHARED_NAME) && - verifier.VerifyString(shared_name()) && - verifier.EndTable(); - } - VarHandleOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(VarHandleOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const VarHandleOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct VarHandleOptionsBuilder { - typedef VarHandleOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_container(flatbuffers::Offset container) { - fbb_.AddOffset(VarHandleOptions::VT_CONTAINER, container); - } - void add_shared_name(flatbuffers::Offset shared_name) { - fbb_.AddOffset(VarHandleOptions::VT_SHARED_NAME, shared_name); - } - explicit VarHandleOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - VarHandleOptionsBuilder &operator=(const VarHandleOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateVarHandleOptions( - flatbuffers::FlatBufferBuilder &_fbb, - flatbuffers::Offset container = 0, - flatbuffers::Offset shared_name = 0) { - VarHandleOptionsBuilder builder_(_fbb); - builder_.add_shared_name(shared_name); - builder_.add_container(container); - return builder_.Finish(); -} - -inline flatbuffers::Offset CreateVarHandleOptionsDirect( - flatbuffers::FlatBufferBuilder &_fbb, - const char *container = nullptr, - const char *shared_name = nullptr) { - auto container__ = container ? _fbb.CreateString(container) : 0; - auto shared_name__ = shared_name ? _fbb.CreateString(shared_name) : 0; - return tflite::CreateVarHandleOptions( - _fbb, - container__, - shared_name__); -} - -flatbuffers::Offset CreateVarHandleOptions(flatbuffers::FlatBufferBuilder &_fbb, const VarHandleOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct ReadVariableOptionsT : public flatbuffers::NativeTable { - typedef ReadVariableOptions TableType; - ReadVariableOptionsT() { - } -}; - -struct ReadVariableOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef ReadVariableOptionsT NativeTableType; - typedef ReadVariableOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - ReadVariableOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(ReadVariableOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const ReadVariableOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct ReadVariableOptionsBuilder { - typedef ReadVariableOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit ReadVariableOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - ReadVariableOptionsBuilder &operator=(const ReadVariableOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateReadVariableOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - ReadVariableOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateReadVariableOptions(flatbuffers::FlatBufferBuilder &_fbb, const ReadVariableOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct AssignVariableOptionsT : public flatbuffers::NativeTable { - typedef AssignVariableOptions TableType; - AssignVariableOptionsT() { - } -}; - -struct AssignVariableOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef AssignVariableOptionsT NativeTableType; - typedef AssignVariableOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - AssignVariableOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(AssignVariableOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const AssignVariableOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct AssignVariableOptionsBuilder { - typedef AssignVariableOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit AssignVariableOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - AssignVariableOptionsBuilder &operator=(const AssignVariableOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateAssignVariableOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - AssignVariableOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateAssignVariableOptions(flatbuffers::FlatBufferBuilder &_fbb, const AssignVariableOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct RandomOptionsT : public flatbuffers::NativeTable { - typedef RandomOptions TableType; - int64_t seed; - int64_t seed2; - RandomOptionsT() - : seed(0), - seed2(0) { - } -}; - -struct RandomOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef RandomOptionsT NativeTableType; - typedef RandomOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_SEED = 4, - VT_SEED2 = 6 - }; - int64_t seed() const { - return GetField(VT_SEED, 0); - } - int64_t seed2() const { - return GetField(VT_SEED2, 0); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_SEED) && - VerifyField(verifier, VT_SEED2) && - verifier.EndTable(); - } - RandomOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(RandomOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const RandomOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct RandomOptionsBuilder { - typedef RandomOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_seed(int64_t seed) { - fbb_.AddElement(RandomOptions::VT_SEED, seed, 0); - } - void add_seed2(int64_t seed2) { - fbb_.AddElement(RandomOptions::VT_SEED2, seed2, 0); - } - explicit RandomOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - RandomOptionsBuilder &operator=(const RandomOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateRandomOptions( - flatbuffers::FlatBufferBuilder &_fbb, - int64_t seed = 0, - int64_t seed2 = 0) { - RandomOptionsBuilder builder_(_fbb); - builder_.add_seed2(seed2); - builder_.add_seed(seed); - return builder_.Finish(); -} - -flatbuffers::Offset CreateRandomOptions(flatbuffers::FlatBufferBuilder &_fbb, const RandomOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct BucketizeOptionsT : public flatbuffers::NativeTable { - typedef BucketizeOptions TableType; - std::vector boundaries; - BucketizeOptionsT() { - } -}; - -struct BucketizeOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef BucketizeOptionsT NativeTableType; - typedef BucketizeOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_BOUNDARIES = 4 - }; - const flatbuffers::Vector *boundaries() const { - return GetPointer *>(VT_BOUNDARIES); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyOffset(verifier, VT_BOUNDARIES) && - verifier.VerifyVector(boundaries()) && - verifier.EndTable(); - } - BucketizeOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(BucketizeOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const BucketizeOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct BucketizeOptionsBuilder { - typedef BucketizeOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_boundaries(flatbuffers::Offset> boundaries) { - fbb_.AddOffset(BucketizeOptions::VT_BOUNDARIES, boundaries); - } - explicit BucketizeOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - BucketizeOptionsBuilder &operator=(const BucketizeOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateBucketizeOptions( - flatbuffers::FlatBufferBuilder &_fbb, - flatbuffers::Offset> boundaries = 0) { - BucketizeOptionsBuilder builder_(_fbb); - builder_.add_boundaries(boundaries); - return builder_.Finish(); -} - -inline flatbuffers::Offset CreateBucketizeOptionsDirect( - flatbuffers::FlatBufferBuilder &_fbb, - const std::vector *boundaries = nullptr) { - auto boundaries__ = boundaries ? _fbb.CreateVector(*boundaries) : 0; - return tflite::CreateBucketizeOptions( - _fbb, - boundaries__); -} - -flatbuffers::Offset CreateBucketizeOptions(flatbuffers::FlatBufferBuilder &_fbb, const BucketizeOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct GeluOptionsT : public flatbuffers::NativeTable { - typedef GeluOptions TableType; - bool approximate; - GeluOptionsT() - : approximate(false) { - } -}; - -struct GeluOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef GeluOptionsT NativeTableType; - typedef GeluOptionsBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_APPROXIMATE = 4 - }; - bool approximate() const { - return GetField(VT_APPROXIMATE, 0) != 0; - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_APPROXIMATE) && - verifier.EndTable(); - } - GeluOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(GeluOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const GeluOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct GeluOptionsBuilder { - typedef GeluOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_approximate(bool approximate) { - fbb_.AddElement(GeluOptions::VT_APPROXIMATE, static_cast(approximate), 0); - } - explicit GeluOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - GeluOptionsBuilder &operator=(const GeluOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateGeluOptions( - flatbuffers::FlatBufferBuilder &_fbb, - bool approximate = false) { - GeluOptionsBuilder builder_(_fbb); - builder_.add_approximate(approximate); - return builder_.Finish(); -} - -flatbuffers::Offset CreateGeluOptions(flatbuffers::FlatBufferBuilder &_fbb, const GeluOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct DynamicUpdateSliceOptionsT : public flatbuffers::NativeTable { - typedef DynamicUpdateSliceOptions TableType; - DynamicUpdateSliceOptionsT() { - } -}; - -struct DynamicUpdateSliceOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef DynamicUpdateSliceOptionsT NativeTableType; - typedef DynamicUpdateSliceOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - DynamicUpdateSliceOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(DynamicUpdateSliceOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const DynamicUpdateSliceOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct DynamicUpdateSliceOptionsBuilder { - typedef DynamicUpdateSliceOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit DynamicUpdateSliceOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - DynamicUpdateSliceOptionsBuilder &operator=(const DynamicUpdateSliceOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateDynamicUpdateSliceOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - DynamicUpdateSliceOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateDynamicUpdateSliceOptions(flatbuffers::FlatBufferBuilder &_fbb, const DynamicUpdateSliceOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct UnsortedSegmentProdOptionsT : public flatbuffers::NativeTable { - typedef UnsortedSegmentProdOptions TableType; - UnsortedSegmentProdOptionsT() { - } -}; - -struct UnsortedSegmentProdOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef UnsortedSegmentProdOptionsT NativeTableType; - typedef UnsortedSegmentProdOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - UnsortedSegmentProdOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(UnsortedSegmentProdOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentProdOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct UnsortedSegmentProdOptionsBuilder { - typedef UnsortedSegmentProdOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit UnsortedSegmentProdOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - UnsortedSegmentProdOptionsBuilder &operator=(const UnsortedSegmentProdOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateUnsortedSegmentProdOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - UnsortedSegmentProdOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateUnsortedSegmentProdOptions(flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentProdOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct UnsortedSegmentMaxOptionsT : public flatbuffers::NativeTable { - typedef UnsortedSegmentMaxOptions TableType; - UnsortedSegmentMaxOptionsT() { - } -}; - -struct UnsortedSegmentMaxOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef UnsortedSegmentMaxOptionsT NativeTableType; - typedef UnsortedSegmentMaxOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - UnsortedSegmentMaxOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(UnsortedSegmentMaxOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentMaxOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct UnsortedSegmentMaxOptionsBuilder { - typedef UnsortedSegmentMaxOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit UnsortedSegmentMaxOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - UnsortedSegmentMaxOptionsBuilder &operator=(const UnsortedSegmentMaxOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateUnsortedSegmentMaxOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - UnsortedSegmentMaxOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateUnsortedSegmentMaxOptions(flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentMaxOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct UnsortedSegmentSumOptionsT : public flatbuffers::NativeTable { - typedef UnsortedSegmentSumOptions TableType; - UnsortedSegmentSumOptionsT() { - } -}; - -struct UnsortedSegmentSumOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef UnsortedSegmentSumOptionsT NativeTableType; - typedef UnsortedSegmentSumOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - UnsortedSegmentSumOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(UnsortedSegmentSumOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentSumOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct UnsortedSegmentSumOptionsBuilder { - typedef UnsortedSegmentSumOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit UnsortedSegmentSumOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - UnsortedSegmentSumOptionsBuilder &operator=(const UnsortedSegmentSumOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateUnsortedSegmentSumOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - UnsortedSegmentSumOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateUnsortedSegmentSumOptions(flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentSumOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct ATan2OptionsT : public flatbuffers::NativeTable { - typedef ATan2Options TableType; - ATan2OptionsT() { - } -}; - -struct ATan2Options FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef ATan2OptionsT NativeTableType; - typedef ATan2OptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - ATan2OptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(ATan2OptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const ATan2OptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct ATan2OptionsBuilder { - typedef ATan2Options Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit ATan2OptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - ATan2OptionsBuilder &operator=(const ATan2OptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateATan2Options( - flatbuffers::FlatBufferBuilder &_fbb) { - ATan2OptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateATan2Options(flatbuffers::FlatBufferBuilder &_fbb, const ATan2OptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct UnsortedSegmentMinOptionsT : public flatbuffers::NativeTable { - typedef UnsortedSegmentMinOptions TableType; - UnsortedSegmentMinOptionsT() { - } -}; - -struct UnsortedSegmentMinOptions FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef UnsortedSegmentMinOptionsT NativeTableType; - typedef UnsortedSegmentMinOptionsBuilder Builder; - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - verifier.EndTable(); - } - UnsortedSegmentMinOptionsT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(UnsortedSegmentMinOptionsT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentMinOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct UnsortedSegmentMinOptionsBuilder { - typedef UnsortedSegmentMinOptions Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - explicit UnsortedSegmentMinOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - UnsortedSegmentMinOptionsBuilder &operator=(const UnsortedSegmentMinOptionsBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateUnsortedSegmentMinOptions( - flatbuffers::FlatBufferBuilder &_fbb) { - UnsortedSegmentMinOptionsBuilder builder_(_fbb); - return builder_.Finish(); -} - -flatbuffers::Offset CreateUnsortedSegmentMinOptions(flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentMinOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct OperatorCodeT : public flatbuffers::NativeTable { - typedef OperatorCode TableType; - int8_t deprecated_builtin_code; - std::string custom_code; - int32_t version; - tflite::BuiltinOperator builtin_code; - OperatorCodeT() - : deprecated_builtin_code(0), - version(1), - builtin_code(tflite::BuiltinOperator_ADD) { - } -}; - -struct OperatorCode FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef OperatorCodeT NativeTableType; - typedef OperatorCodeBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_DEPRECATED_BUILTIN_CODE = 4, - VT_CUSTOM_CODE = 6, - VT_VERSION = 8, - VT_BUILTIN_CODE = 10 - }; - int8_t deprecated_builtin_code() const { - return GetField(VT_DEPRECATED_BUILTIN_CODE, 0); - } - const flatbuffers::String *custom_code() const { - return GetPointer(VT_CUSTOM_CODE); - } - int32_t version() const { - return GetField(VT_VERSION, 1); - } - tflite::BuiltinOperator builtin_code() const { - return static_cast(GetField(VT_BUILTIN_CODE, 0)); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_DEPRECATED_BUILTIN_CODE) && - VerifyOffset(verifier, VT_CUSTOM_CODE) && - verifier.VerifyString(custom_code()) && - VerifyField(verifier, VT_VERSION) && - VerifyField(verifier, VT_BUILTIN_CODE) && - verifier.EndTable(); - } - OperatorCodeT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(OperatorCodeT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const OperatorCodeT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct OperatorCodeBuilder { - typedef OperatorCode Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_deprecated_builtin_code(int8_t deprecated_builtin_code) { - fbb_.AddElement(OperatorCode::VT_DEPRECATED_BUILTIN_CODE, deprecated_builtin_code, 0); - } - void add_custom_code(flatbuffers::Offset custom_code) { - fbb_.AddOffset(OperatorCode::VT_CUSTOM_CODE, custom_code); - } - void add_version(int32_t version) { - fbb_.AddElement(OperatorCode::VT_VERSION, version, 1); - } - void add_builtin_code(tflite::BuiltinOperator builtin_code) { - fbb_.AddElement(OperatorCode::VT_BUILTIN_CODE, static_cast(builtin_code), 0); - } - explicit OperatorCodeBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - OperatorCodeBuilder &operator=(const OperatorCodeBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateOperatorCode( - flatbuffers::FlatBufferBuilder &_fbb, - int8_t deprecated_builtin_code = 0, - flatbuffers::Offset custom_code = 0, - int32_t version = 1, - tflite::BuiltinOperator builtin_code = tflite::BuiltinOperator_ADD) { - OperatorCodeBuilder builder_(_fbb); - builder_.add_builtin_code(builtin_code); - builder_.add_version(version); - builder_.add_custom_code(custom_code); - builder_.add_deprecated_builtin_code(deprecated_builtin_code); - return builder_.Finish(); -} - -inline flatbuffers::Offset CreateOperatorCodeDirect( - flatbuffers::FlatBufferBuilder &_fbb, - int8_t deprecated_builtin_code = 0, - const char *custom_code = nullptr, - int32_t version = 1, - tflite::BuiltinOperator builtin_code = tflite::BuiltinOperator_ADD) { - auto custom_code__ = custom_code ? _fbb.CreateString(custom_code) : 0; - return tflite::CreateOperatorCode( - _fbb, - deprecated_builtin_code, - custom_code__, - version, - builtin_code); -} - -flatbuffers::Offset CreateOperatorCode(flatbuffers::FlatBufferBuilder &_fbb, const OperatorCodeT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct OperatorT : public flatbuffers::NativeTable { - typedef Operator TableType; - uint32_t opcode_index; - std::vector inputs; - std::vector outputs; - tflite::BuiltinOptionsUnion builtin_options; - std::vector custom_options; - tflite::CustomOptionsFormat custom_options_format; - std::vector mutating_variable_inputs; - std::vector intermediates; - OperatorT() - : opcode_index(0), - custom_options_format(tflite::CustomOptionsFormat_FLEXBUFFERS) { - } -}; - -struct Operator FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef OperatorT NativeTableType; - typedef OperatorBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_OPCODE_INDEX = 4, - VT_INPUTS = 6, - VT_OUTPUTS = 8, - VT_BUILTIN_OPTIONS_TYPE = 10, - VT_BUILTIN_OPTIONS = 12, - VT_CUSTOM_OPTIONS = 14, - VT_CUSTOM_OPTIONS_FORMAT = 16, - VT_MUTATING_VARIABLE_INPUTS = 18, - VT_INTERMEDIATES = 20 - }; - uint32_t opcode_index() const { - return GetField(VT_OPCODE_INDEX, 0); - } - const flatbuffers::Vector *inputs() const { - return GetPointer *>(VT_INPUTS); - } - const flatbuffers::Vector *outputs() const { - return GetPointer *>(VT_OUTPUTS); - } - tflite::BuiltinOptions builtin_options_type() const { - return static_cast(GetField(VT_BUILTIN_OPTIONS_TYPE, 0)); - } - const void *builtin_options() const { - return GetPointer(VT_BUILTIN_OPTIONS); - } - template const T *builtin_options_as() const; - const tflite::Conv2DOptions *builtin_options_as_Conv2DOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_Conv2DOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::DepthwiseConv2DOptions *builtin_options_as_DepthwiseConv2DOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_DepthwiseConv2DOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::ConcatEmbeddingsOptions *builtin_options_as_ConcatEmbeddingsOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_ConcatEmbeddingsOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::LSHProjectionOptions *builtin_options_as_LSHProjectionOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_LSHProjectionOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::Pool2DOptions *builtin_options_as_Pool2DOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_Pool2DOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::SVDFOptions *builtin_options_as_SVDFOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_SVDFOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::RNNOptions *builtin_options_as_RNNOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_RNNOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::FullyConnectedOptions *builtin_options_as_FullyConnectedOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_FullyConnectedOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::SoftmaxOptions *builtin_options_as_SoftmaxOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_SoftmaxOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::ConcatenationOptions *builtin_options_as_ConcatenationOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_ConcatenationOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::AddOptions *builtin_options_as_AddOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_AddOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::L2NormOptions *builtin_options_as_L2NormOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_L2NormOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::LocalResponseNormalizationOptions *builtin_options_as_LocalResponseNormalizationOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_LocalResponseNormalizationOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::LSTMOptions *builtin_options_as_LSTMOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_LSTMOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::ResizeBilinearOptions *builtin_options_as_ResizeBilinearOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_ResizeBilinearOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::CallOptions *builtin_options_as_CallOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_CallOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::ReshapeOptions *builtin_options_as_ReshapeOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_ReshapeOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::SkipGramOptions *builtin_options_as_SkipGramOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_SkipGramOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::SpaceToDepthOptions *builtin_options_as_SpaceToDepthOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_SpaceToDepthOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::EmbeddingLookupSparseOptions *builtin_options_as_EmbeddingLookupSparseOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_EmbeddingLookupSparseOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::MulOptions *builtin_options_as_MulOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_MulOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::PadOptions *builtin_options_as_PadOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_PadOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::GatherOptions *builtin_options_as_GatherOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_GatherOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::BatchToSpaceNDOptions *builtin_options_as_BatchToSpaceNDOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_BatchToSpaceNDOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::SpaceToBatchNDOptions *builtin_options_as_SpaceToBatchNDOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_SpaceToBatchNDOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::TransposeOptions *builtin_options_as_TransposeOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_TransposeOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::ReducerOptions *builtin_options_as_ReducerOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_ReducerOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::SubOptions *builtin_options_as_SubOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_SubOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::DivOptions *builtin_options_as_DivOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_DivOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::SqueezeOptions *builtin_options_as_SqueezeOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_SqueezeOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::SequenceRNNOptions *builtin_options_as_SequenceRNNOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_SequenceRNNOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::StridedSliceOptions *builtin_options_as_StridedSliceOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_StridedSliceOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::ExpOptions *builtin_options_as_ExpOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_ExpOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::TopKV2Options *builtin_options_as_TopKV2Options() const { - return builtin_options_type() == tflite::BuiltinOptions_TopKV2Options ? static_cast(builtin_options()) : nullptr; - } - const tflite::SplitOptions *builtin_options_as_SplitOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_SplitOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::LogSoftmaxOptions *builtin_options_as_LogSoftmaxOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_LogSoftmaxOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::CastOptions *builtin_options_as_CastOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_CastOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::DequantizeOptions *builtin_options_as_DequantizeOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_DequantizeOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::MaximumMinimumOptions *builtin_options_as_MaximumMinimumOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_MaximumMinimumOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::ArgMaxOptions *builtin_options_as_ArgMaxOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_ArgMaxOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::LessOptions *builtin_options_as_LessOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_LessOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::NegOptions *builtin_options_as_NegOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_NegOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::PadV2Options *builtin_options_as_PadV2Options() const { - return builtin_options_type() == tflite::BuiltinOptions_PadV2Options ? static_cast(builtin_options()) : nullptr; - } - const tflite::GreaterOptions *builtin_options_as_GreaterOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_GreaterOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::GreaterEqualOptions *builtin_options_as_GreaterEqualOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_GreaterEqualOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::LessEqualOptions *builtin_options_as_LessEqualOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_LessEqualOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::SelectOptions *builtin_options_as_SelectOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_SelectOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::SliceOptions *builtin_options_as_SliceOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_SliceOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::TransposeConvOptions *builtin_options_as_TransposeConvOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_TransposeConvOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::SparseToDenseOptions *builtin_options_as_SparseToDenseOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_SparseToDenseOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::TileOptions *builtin_options_as_TileOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_TileOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::ExpandDimsOptions *builtin_options_as_ExpandDimsOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_ExpandDimsOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::EqualOptions *builtin_options_as_EqualOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_EqualOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::NotEqualOptions *builtin_options_as_NotEqualOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_NotEqualOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::ShapeOptions *builtin_options_as_ShapeOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_ShapeOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::PowOptions *builtin_options_as_PowOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_PowOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::ArgMinOptions *builtin_options_as_ArgMinOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_ArgMinOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::FakeQuantOptions *builtin_options_as_FakeQuantOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_FakeQuantOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::PackOptions *builtin_options_as_PackOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_PackOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::LogicalOrOptions *builtin_options_as_LogicalOrOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_LogicalOrOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::OneHotOptions *builtin_options_as_OneHotOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_OneHotOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::LogicalAndOptions *builtin_options_as_LogicalAndOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_LogicalAndOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::LogicalNotOptions *builtin_options_as_LogicalNotOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_LogicalNotOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::UnpackOptions *builtin_options_as_UnpackOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_UnpackOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::FloorDivOptions *builtin_options_as_FloorDivOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_FloorDivOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::SquareOptions *builtin_options_as_SquareOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_SquareOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::ZerosLikeOptions *builtin_options_as_ZerosLikeOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_ZerosLikeOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::FillOptions *builtin_options_as_FillOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_FillOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::BidirectionalSequenceLSTMOptions *builtin_options_as_BidirectionalSequenceLSTMOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_BidirectionalSequenceLSTMOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::BidirectionalSequenceRNNOptions *builtin_options_as_BidirectionalSequenceRNNOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_BidirectionalSequenceRNNOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::UnidirectionalSequenceLSTMOptions *builtin_options_as_UnidirectionalSequenceLSTMOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_UnidirectionalSequenceLSTMOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::FloorModOptions *builtin_options_as_FloorModOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_FloorModOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::RangeOptions *builtin_options_as_RangeOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_RangeOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::ResizeNearestNeighborOptions *builtin_options_as_ResizeNearestNeighborOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_ResizeNearestNeighborOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::LeakyReluOptions *builtin_options_as_LeakyReluOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_LeakyReluOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::SquaredDifferenceOptions *builtin_options_as_SquaredDifferenceOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_SquaredDifferenceOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::MirrorPadOptions *builtin_options_as_MirrorPadOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_MirrorPadOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::AbsOptions *builtin_options_as_AbsOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_AbsOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::SplitVOptions *builtin_options_as_SplitVOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_SplitVOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::UniqueOptions *builtin_options_as_UniqueOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_UniqueOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::ReverseV2Options *builtin_options_as_ReverseV2Options() const { - return builtin_options_type() == tflite::BuiltinOptions_ReverseV2Options ? static_cast(builtin_options()) : nullptr; - } - const tflite::AddNOptions *builtin_options_as_AddNOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_AddNOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::GatherNdOptions *builtin_options_as_GatherNdOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_GatherNdOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::CosOptions *builtin_options_as_CosOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_CosOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::WhereOptions *builtin_options_as_WhereOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_WhereOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::RankOptions *builtin_options_as_RankOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_RankOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::ReverseSequenceOptions *builtin_options_as_ReverseSequenceOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_ReverseSequenceOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::MatrixDiagOptions *builtin_options_as_MatrixDiagOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_MatrixDiagOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::QuantizeOptions *builtin_options_as_QuantizeOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_QuantizeOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::MatrixSetDiagOptions *builtin_options_as_MatrixSetDiagOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_MatrixSetDiagOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::HardSwishOptions *builtin_options_as_HardSwishOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_HardSwishOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::IfOptions *builtin_options_as_IfOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_IfOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::WhileOptions *builtin_options_as_WhileOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_WhileOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::DepthToSpaceOptions *builtin_options_as_DepthToSpaceOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_DepthToSpaceOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::NonMaxSuppressionV4Options *builtin_options_as_NonMaxSuppressionV4Options() const { - return builtin_options_type() == tflite::BuiltinOptions_NonMaxSuppressionV4Options ? static_cast(builtin_options()) : nullptr; - } - const tflite::NonMaxSuppressionV5Options *builtin_options_as_NonMaxSuppressionV5Options() const { - return builtin_options_type() == tflite::BuiltinOptions_NonMaxSuppressionV5Options ? static_cast(builtin_options()) : nullptr; - } - const tflite::ScatterNdOptions *builtin_options_as_ScatterNdOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_ScatterNdOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::SelectV2Options *builtin_options_as_SelectV2Options() const { - return builtin_options_type() == tflite::BuiltinOptions_SelectV2Options ? static_cast(builtin_options()) : nullptr; - } - const tflite::DensifyOptions *builtin_options_as_DensifyOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_DensifyOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::SegmentSumOptions *builtin_options_as_SegmentSumOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_SegmentSumOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::BatchMatMulOptions *builtin_options_as_BatchMatMulOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_BatchMatMulOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::CumsumOptions *builtin_options_as_CumsumOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_CumsumOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::CallOnceOptions *builtin_options_as_CallOnceOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_CallOnceOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::BroadcastToOptions *builtin_options_as_BroadcastToOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_BroadcastToOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::Rfft2dOptions *builtin_options_as_Rfft2dOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_Rfft2dOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::Conv3DOptions *builtin_options_as_Conv3DOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_Conv3DOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::HashtableOptions *builtin_options_as_HashtableOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_HashtableOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::HashtableFindOptions *builtin_options_as_HashtableFindOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_HashtableFindOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::HashtableImportOptions *builtin_options_as_HashtableImportOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_HashtableImportOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::HashtableSizeOptions *builtin_options_as_HashtableSizeOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_HashtableSizeOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::VarHandleOptions *builtin_options_as_VarHandleOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_VarHandleOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::ReadVariableOptions *builtin_options_as_ReadVariableOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_ReadVariableOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::AssignVariableOptions *builtin_options_as_AssignVariableOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_AssignVariableOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::RandomOptions *builtin_options_as_RandomOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_RandomOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::BucketizeOptions *builtin_options_as_BucketizeOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_BucketizeOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::GeluOptions *builtin_options_as_GeluOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_GeluOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::DynamicUpdateSliceOptions *builtin_options_as_DynamicUpdateSliceOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_DynamicUpdateSliceOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::UnsortedSegmentProdOptions *builtin_options_as_UnsortedSegmentProdOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_UnsortedSegmentProdOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::UnsortedSegmentMaxOptions *builtin_options_as_UnsortedSegmentMaxOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_UnsortedSegmentMaxOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::UnsortedSegmentMinOptions *builtin_options_as_UnsortedSegmentMinOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_UnsortedSegmentMinOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::UnsortedSegmentSumOptions *builtin_options_as_UnsortedSegmentSumOptions() const { - return builtin_options_type() == tflite::BuiltinOptions_UnsortedSegmentSumOptions ? static_cast(builtin_options()) : nullptr; - } - const tflite::ATan2Options *builtin_options_as_ATan2Options() const { - return builtin_options_type() == tflite::BuiltinOptions_ATan2Options ? static_cast(builtin_options()) : nullptr; - } - const flatbuffers::Vector *custom_options() const { - return GetPointer *>(VT_CUSTOM_OPTIONS); - } - tflite::CustomOptionsFormat custom_options_format() const { - return static_cast(GetField(VT_CUSTOM_OPTIONS_FORMAT, 0)); - } - const flatbuffers::Vector *mutating_variable_inputs() const { - return GetPointer *>(VT_MUTATING_VARIABLE_INPUTS); - } - const flatbuffers::Vector *intermediates() const { - return GetPointer *>(VT_INTERMEDIATES); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_OPCODE_INDEX) && - VerifyOffset(verifier, VT_INPUTS) && - verifier.VerifyVector(inputs()) && - VerifyOffset(verifier, VT_OUTPUTS) && - verifier.VerifyVector(outputs()) && - VerifyField(verifier, VT_BUILTIN_OPTIONS_TYPE) && - VerifyOffset(verifier, VT_BUILTIN_OPTIONS) && - VerifyBuiltinOptions(verifier, builtin_options(), builtin_options_type()) && - VerifyOffset(verifier, VT_CUSTOM_OPTIONS) && - verifier.VerifyVector(custom_options()) && - VerifyField(verifier, VT_CUSTOM_OPTIONS_FORMAT) && - VerifyOffset(verifier, VT_MUTATING_VARIABLE_INPUTS) && - verifier.VerifyVector(mutating_variable_inputs()) && - VerifyOffset(verifier, VT_INTERMEDIATES) && - verifier.VerifyVector(intermediates()) && - verifier.EndTable(); - } - OperatorT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(OperatorT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const OperatorT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -template<> inline const tflite::Conv2DOptions *Operator::builtin_options_as() const { - return builtin_options_as_Conv2DOptions(); -} - -template<> inline const tflite::DepthwiseConv2DOptions *Operator::builtin_options_as() const { - return builtin_options_as_DepthwiseConv2DOptions(); -} - -template<> inline const tflite::ConcatEmbeddingsOptions *Operator::builtin_options_as() const { - return builtin_options_as_ConcatEmbeddingsOptions(); -} - -template<> inline const tflite::LSHProjectionOptions *Operator::builtin_options_as() const { - return builtin_options_as_LSHProjectionOptions(); -} - -template<> inline const tflite::Pool2DOptions *Operator::builtin_options_as() const { - return builtin_options_as_Pool2DOptions(); -} - -template<> inline const tflite::SVDFOptions *Operator::builtin_options_as() const { - return builtin_options_as_SVDFOptions(); -} - -template<> inline const tflite::RNNOptions *Operator::builtin_options_as() const { - return builtin_options_as_RNNOptions(); -} - -template<> inline const tflite::FullyConnectedOptions *Operator::builtin_options_as() const { - return builtin_options_as_FullyConnectedOptions(); -} - -template<> inline const tflite::SoftmaxOptions *Operator::builtin_options_as() const { - return builtin_options_as_SoftmaxOptions(); -} - -template<> inline const tflite::ConcatenationOptions *Operator::builtin_options_as() const { - return builtin_options_as_ConcatenationOptions(); -} - -template<> inline const tflite::AddOptions *Operator::builtin_options_as() const { - return builtin_options_as_AddOptions(); -} - -template<> inline const tflite::L2NormOptions *Operator::builtin_options_as() const { - return builtin_options_as_L2NormOptions(); -} - -template<> inline const tflite::LocalResponseNormalizationOptions *Operator::builtin_options_as() const { - return builtin_options_as_LocalResponseNormalizationOptions(); -} - -template<> inline const tflite::LSTMOptions *Operator::builtin_options_as() const { - return builtin_options_as_LSTMOptions(); -} - -template<> inline const tflite::ResizeBilinearOptions *Operator::builtin_options_as() const { - return builtin_options_as_ResizeBilinearOptions(); -} - -template<> inline const tflite::CallOptions *Operator::builtin_options_as() const { - return builtin_options_as_CallOptions(); -} - -template<> inline const tflite::ReshapeOptions *Operator::builtin_options_as() const { - return builtin_options_as_ReshapeOptions(); -} - -template<> inline const tflite::SkipGramOptions *Operator::builtin_options_as() const { - return builtin_options_as_SkipGramOptions(); -} - -template<> inline const tflite::SpaceToDepthOptions *Operator::builtin_options_as() const { - return builtin_options_as_SpaceToDepthOptions(); -} - -template<> inline const tflite::EmbeddingLookupSparseOptions *Operator::builtin_options_as() const { - return builtin_options_as_EmbeddingLookupSparseOptions(); -} - -template<> inline const tflite::MulOptions *Operator::builtin_options_as() const { - return builtin_options_as_MulOptions(); -} - -template<> inline const tflite::PadOptions *Operator::builtin_options_as() const { - return builtin_options_as_PadOptions(); -} - -template<> inline const tflite::GatherOptions *Operator::builtin_options_as() const { - return builtin_options_as_GatherOptions(); -} - -template<> inline const tflite::BatchToSpaceNDOptions *Operator::builtin_options_as() const { - return builtin_options_as_BatchToSpaceNDOptions(); -} - -template<> inline const tflite::SpaceToBatchNDOptions *Operator::builtin_options_as() const { - return builtin_options_as_SpaceToBatchNDOptions(); -} - -template<> inline const tflite::TransposeOptions *Operator::builtin_options_as() const { - return builtin_options_as_TransposeOptions(); -} - -template<> inline const tflite::ReducerOptions *Operator::builtin_options_as() const { - return builtin_options_as_ReducerOptions(); -} - -template<> inline const tflite::SubOptions *Operator::builtin_options_as() const { - return builtin_options_as_SubOptions(); -} - -template<> inline const tflite::DivOptions *Operator::builtin_options_as() const { - return builtin_options_as_DivOptions(); -} - -template<> inline const tflite::SqueezeOptions *Operator::builtin_options_as() const { - return builtin_options_as_SqueezeOptions(); -} - -template<> inline const tflite::SequenceRNNOptions *Operator::builtin_options_as() const { - return builtin_options_as_SequenceRNNOptions(); -} - -template<> inline const tflite::StridedSliceOptions *Operator::builtin_options_as() const { - return builtin_options_as_StridedSliceOptions(); -} - -template<> inline const tflite::ExpOptions *Operator::builtin_options_as() const { - return builtin_options_as_ExpOptions(); -} - -template<> inline const tflite::TopKV2Options *Operator::builtin_options_as() const { - return builtin_options_as_TopKV2Options(); -} - -template<> inline const tflite::SplitOptions *Operator::builtin_options_as() const { - return builtin_options_as_SplitOptions(); -} - -template<> inline const tflite::LogSoftmaxOptions *Operator::builtin_options_as() const { - return builtin_options_as_LogSoftmaxOptions(); -} - -template<> inline const tflite::CastOptions *Operator::builtin_options_as() const { - return builtin_options_as_CastOptions(); -} - -template<> inline const tflite::DequantizeOptions *Operator::builtin_options_as() const { - return builtin_options_as_DequantizeOptions(); -} - -template<> inline const tflite::MaximumMinimumOptions *Operator::builtin_options_as() const { - return builtin_options_as_MaximumMinimumOptions(); -} - -template<> inline const tflite::ArgMaxOptions *Operator::builtin_options_as() const { - return builtin_options_as_ArgMaxOptions(); -} - -template<> inline const tflite::LessOptions *Operator::builtin_options_as() const { - return builtin_options_as_LessOptions(); -} - -template<> inline const tflite::NegOptions *Operator::builtin_options_as() const { - return builtin_options_as_NegOptions(); -} - -template<> inline const tflite::PadV2Options *Operator::builtin_options_as() const { - return builtin_options_as_PadV2Options(); -} - -template<> inline const tflite::GreaterOptions *Operator::builtin_options_as() const { - return builtin_options_as_GreaterOptions(); -} - -template<> inline const tflite::GreaterEqualOptions *Operator::builtin_options_as() const { - return builtin_options_as_GreaterEqualOptions(); -} - -template<> inline const tflite::LessEqualOptions *Operator::builtin_options_as() const { - return builtin_options_as_LessEqualOptions(); -} - -template<> inline const tflite::SelectOptions *Operator::builtin_options_as() const { - return builtin_options_as_SelectOptions(); -} - -template<> inline const tflite::SliceOptions *Operator::builtin_options_as() const { - return builtin_options_as_SliceOptions(); -} - -template<> inline const tflite::TransposeConvOptions *Operator::builtin_options_as() const { - return builtin_options_as_TransposeConvOptions(); -} - -template<> inline const tflite::SparseToDenseOptions *Operator::builtin_options_as() const { - return builtin_options_as_SparseToDenseOptions(); -} - -template<> inline const tflite::TileOptions *Operator::builtin_options_as() const { - return builtin_options_as_TileOptions(); -} - -template<> inline const tflite::ExpandDimsOptions *Operator::builtin_options_as() const { - return builtin_options_as_ExpandDimsOptions(); -} - -template<> inline const tflite::EqualOptions *Operator::builtin_options_as() const { - return builtin_options_as_EqualOptions(); -} - -template<> inline const tflite::NotEqualOptions *Operator::builtin_options_as() const { - return builtin_options_as_NotEqualOptions(); -} - -template<> inline const tflite::ShapeOptions *Operator::builtin_options_as() const { - return builtin_options_as_ShapeOptions(); -} - -template<> inline const tflite::PowOptions *Operator::builtin_options_as() const { - return builtin_options_as_PowOptions(); -} - -template<> inline const tflite::ArgMinOptions *Operator::builtin_options_as() const { - return builtin_options_as_ArgMinOptions(); -} - -template<> inline const tflite::FakeQuantOptions *Operator::builtin_options_as() const { - return builtin_options_as_FakeQuantOptions(); -} - -template<> inline const tflite::PackOptions *Operator::builtin_options_as() const { - return builtin_options_as_PackOptions(); -} - -template<> inline const tflite::LogicalOrOptions *Operator::builtin_options_as() const { - return builtin_options_as_LogicalOrOptions(); -} - -template<> inline const tflite::OneHotOptions *Operator::builtin_options_as() const { - return builtin_options_as_OneHotOptions(); -} - -template<> inline const tflite::LogicalAndOptions *Operator::builtin_options_as() const { - return builtin_options_as_LogicalAndOptions(); -} - -template<> inline const tflite::LogicalNotOptions *Operator::builtin_options_as() const { - return builtin_options_as_LogicalNotOptions(); -} - -template<> inline const tflite::UnpackOptions *Operator::builtin_options_as() const { - return builtin_options_as_UnpackOptions(); -} - -template<> inline const tflite::FloorDivOptions *Operator::builtin_options_as() const { - return builtin_options_as_FloorDivOptions(); -} - -template<> inline const tflite::SquareOptions *Operator::builtin_options_as() const { - return builtin_options_as_SquareOptions(); -} - -template<> inline const tflite::ZerosLikeOptions *Operator::builtin_options_as() const { - return builtin_options_as_ZerosLikeOptions(); -} - -template<> inline const tflite::FillOptions *Operator::builtin_options_as() const { - return builtin_options_as_FillOptions(); -} - -template<> inline const tflite::BidirectionalSequenceLSTMOptions *Operator::builtin_options_as() const { - return builtin_options_as_BidirectionalSequenceLSTMOptions(); -} - -template<> inline const tflite::BidirectionalSequenceRNNOptions *Operator::builtin_options_as() const { - return builtin_options_as_BidirectionalSequenceRNNOptions(); -} - -template<> inline const tflite::UnidirectionalSequenceLSTMOptions *Operator::builtin_options_as() const { - return builtin_options_as_UnidirectionalSequenceLSTMOptions(); -} - -template<> inline const tflite::FloorModOptions *Operator::builtin_options_as() const { - return builtin_options_as_FloorModOptions(); -} - -template<> inline const tflite::RangeOptions *Operator::builtin_options_as() const { - return builtin_options_as_RangeOptions(); -} - -template<> inline const tflite::ResizeNearestNeighborOptions *Operator::builtin_options_as() const { - return builtin_options_as_ResizeNearestNeighborOptions(); -} - -template<> inline const tflite::LeakyReluOptions *Operator::builtin_options_as() const { - return builtin_options_as_LeakyReluOptions(); -} - -template<> inline const tflite::SquaredDifferenceOptions *Operator::builtin_options_as() const { - return builtin_options_as_SquaredDifferenceOptions(); -} - -template<> inline const tflite::MirrorPadOptions *Operator::builtin_options_as() const { - return builtin_options_as_MirrorPadOptions(); -} - -template<> inline const tflite::AbsOptions *Operator::builtin_options_as() const { - return builtin_options_as_AbsOptions(); -} - -template<> inline const tflite::SplitVOptions *Operator::builtin_options_as() const { - return builtin_options_as_SplitVOptions(); -} - -template<> inline const tflite::UniqueOptions *Operator::builtin_options_as() const { - return builtin_options_as_UniqueOptions(); -} - -template<> inline const tflite::ReverseV2Options *Operator::builtin_options_as() const { - return builtin_options_as_ReverseV2Options(); -} - -template<> inline const tflite::AddNOptions *Operator::builtin_options_as() const { - return builtin_options_as_AddNOptions(); -} - -template<> inline const tflite::GatherNdOptions *Operator::builtin_options_as() const { - return builtin_options_as_GatherNdOptions(); -} - -template<> inline const tflite::CosOptions *Operator::builtin_options_as() const { - return builtin_options_as_CosOptions(); -} - -template<> inline const tflite::WhereOptions *Operator::builtin_options_as() const { - return builtin_options_as_WhereOptions(); -} - -template<> inline const tflite::RankOptions *Operator::builtin_options_as() const { - return builtin_options_as_RankOptions(); -} - -template<> inline const tflite::ReverseSequenceOptions *Operator::builtin_options_as() const { - return builtin_options_as_ReverseSequenceOptions(); -} - -template<> inline const tflite::MatrixDiagOptions *Operator::builtin_options_as() const { - return builtin_options_as_MatrixDiagOptions(); -} - -template<> inline const tflite::QuantizeOptions *Operator::builtin_options_as() const { - return builtin_options_as_QuantizeOptions(); -} - -template<> inline const tflite::MatrixSetDiagOptions *Operator::builtin_options_as() const { - return builtin_options_as_MatrixSetDiagOptions(); -} - -template<> inline const tflite::HardSwishOptions *Operator::builtin_options_as() const { - return builtin_options_as_HardSwishOptions(); -} - -template<> inline const tflite::IfOptions *Operator::builtin_options_as() const { - return builtin_options_as_IfOptions(); -} - -template<> inline const tflite::WhileOptions *Operator::builtin_options_as() const { - return builtin_options_as_WhileOptions(); -} - -template<> inline const tflite::DepthToSpaceOptions *Operator::builtin_options_as() const { - return builtin_options_as_DepthToSpaceOptions(); -} - -template<> inline const tflite::NonMaxSuppressionV4Options *Operator::builtin_options_as() const { - return builtin_options_as_NonMaxSuppressionV4Options(); -} - -template<> inline const tflite::NonMaxSuppressionV5Options *Operator::builtin_options_as() const { - return builtin_options_as_NonMaxSuppressionV5Options(); -} - -template<> inline const tflite::ScatterNdOptions *Operator::builtin_options_as() const { - return builtin_options_as_ScatterNdOptions(); -} - -template<> inline const tflite::SelectV2Options *Operator::builtin_options_as() const { - return builtin_options_as_SelectV2Options(); -} - -template<> inline const tflite::DensifyOptions *Operator::builtin_options_as() const { - return builtin_options_as_DensifyOptions(); -} - -template<> inline const tflite::SegmentSumOptions *Operator::builtin_options_as() const { - return builtin_options_as_SegmentSumOptions(); -} - -template<> inline const tflite::BatchMatMulOptions *Operator::builtin_options_as() const { - return builtin_options_as_BatchMatMulOptions(); -} - -template<> inline const tflite::CumsumOptions *Operator::builtin_options_as() const { - return builtin_options_as_CumsumOptions(); -} - -template<> inline const tflite::CallOnceOptions *Operator::builtin_options_as() const { - return builtin_options_as_CallOnceOptions(); -} - -template<> inline const tflite::BroadcastToOptions *Operator::builtin_options_as() const { - return builtin_options_as_BroadcastToOptions(); -} - -template<> inline const tflite::Rfft2dOptions *Operator::builtin_options_as() const { - return builtin_options_as_Rfft2dOptions(); -} - -template<> inline const tflite::Conv3DOptions *Operator::builtin_options_as() const { - return builtin_options_as_Conv3DOptions(); -} - -template<> inline const tflite::HashtableOptions *Operator::builtin_options_as() const { - return builtin_options_as_HashtableOptions(); -} - -template<> inline const tflite::HashtableFindOptions *Operator::builtin_options_as() const { - return builtin_options_as_HashtableFindOptions(); -} - -template<> inline const tflite::HashtableImportOptions *Operator::builtin_options_as() const { - return builtin_options_as_HashtableImportOptions(); -} - -template<> inline const tflite::HashtableSizeOptions *Operator::builtin_options_as() const { - return builtin_options_as_HashtableSizeOptions(); -} - -template<> inline const tflite::VarHandleOptions *Operator::builtin_options_as() const { - return builtin_options_as_VarHandleOptions(); -} - -template<> inline const tflite::ReadVariableOptions *Operator::builtin_options_as() const { - return builtin_options_as_ReadVariableOptions(); -} - -template<> inline const tflite::AssignVariableOptions *Operator::builtin_options_as() const { - return builtin_options_as_AssignVariableOptions(); -} - -template<> inline const tflite::RandomOptions *Operator::builtin_options_as() const { - return builtin_options_as_RandomOptions(); -} - -template<> inline const tflite::BucketizeOptions *Operator::builtin_options_as() const { - return builtin_options_as_BucketizeOptions(); -} - -template<> inline const tflite::GeluOptions *Operator::builtin_options_as() const { - return builtin_options_as_GeluOptions(); -} - -template<> inline const tflite::DynamicUpdateSliceOptions *Operator::builtin_options_as() const { - return builtin_options_as_DynamicUpdateSliceOptions(); -} - -template<> inline const tflite::UnsortedSegmentProdOptions *Operator::builtin_options_as() const { - return builtin_options_as_UnsortedSegmentProdOptions(); -} - -template<> inline const tflite::UnsortedSegmentMaxOptions *Operator::builtin_options_as() const { - return builtin_options_as_UnsortedSegmentMaxOptions(); -} - -template<> inline const tflite::UnsortedSegmentMinOptions *Operator::builtin_options_as() const { - return builtin_options_as_UnsortedSegmentMinOptions(); -} - -template<> inline const tflite::UnsortedSegmentSumOptions *Operator::builtin_options_as() const { - return builtin_options_as_UnsortedSegmentSumOptions(); -} - -template<> inline const tflite::ATan2Options *Operator::builtin_options_as() const { - return builtin_options_as_ATan2Options(); -} - -struct OperatorBuilder { - typedef Operator Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_opcode_index(uint32_t opcode_index) { - fbb_.AddElement(Operator::VT_OPCODE_INDEX, opcode_index, 0); - } - void add_inputs(flatbuffers::Offset> inputs) { - fbb_.AddOffset(Operator::VT_INPUTS, inputs); - } - void add_outputs(flatbuffers::Offset> outputs) { - fbb_.AddOffset(Operator::VT_OUTPUTS, outputs); - } - void add_builtin_options_type(tflite::BuiltinOptions builtin_options_type) { - fbb_.AddElement(Operator::VT_BUILTIN_OPTIONS_TYPE, static_cast(builtin_options_type), 0); - } - void add_builtin_options(flatbuffers::Offset builtin_options) { - fbb_.AddOffset(Operator::VT_BUILTIN_OPTIONS, builtin_options); - } - void add_custom_options(flatbuffers::Offset> custom_options) { - fbb_.AddOffset(Operator::VT_CUSTOM_OPTIONS, custom_options); - } - void add_custom_options_format(tflite::CustomOptionsFormat custom_options_format) { - fbb_.AddElement(Operator::VT_CUSTOM_OPTIONS_FORMAT, static_cast(custom_options_format), 0); - } - void add_mutating_variable_inputs(flatbuffers::Offset> mutating_variable_inputs) { - fbb_.AddOffset(Operator::VT_MUTATING_VARIABLE_INPUTS, mutating_variable_inputs); - } - void add_intermediates(flatbuffers::Offset> intermediates) { - fbb_.AddOffset(Operator::VT_INTERMEDIATES, intermediates); - } - explicit OperatorBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - OperatorBuilder &operator=(const OperatorBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateOperator( - flatbuffers::FlatBufferBuilder &_fbb, - uint32_t opcode_index = 0, - flatbuffers::Offset> inputs = 0, - flatbuffers::Offset> outputs = 0, - tflite::BuiltinOptions builtin_options_type = tflite::BuiltinOptions_NONE, - flatbuffers::Offset builtin_options = 0, - flatbuffers::Offset> custom_options = 0, - tflite::CustomOptionsFormat custom_options_format = tflite::CustomOptionsFormat_FLEXBUFFERS, - flatbuffers::Offset> mutating_variable_inputs = 0, - flatbuffers::Offset> intermediates = 0) { - OperatorBuilder builder_(_fbb); - builder_.add_intermediates(intermediates); - builder_.add_mutating_variable_inputs(mutating_variable_inputs); - builder_.add_custom_options(custom_options); - builder_.add_builtin_options(builtin_options); - builder_.add_outputs(outputs); - builder_.add_inputs(inputs); - builder_.add_opcode_index(opcode_index); - builder_.add_custom_options_format(custom_options_format); - builder_.add_builtin_options_type(builtin_options_type); - return builder_.Finish(); -} - -inline flatbuffers::Offset CreateOperatorDirect( - flatbuffers::FlatBufferBuilder &_fbb, - uint32_t opcode_index = 0, - const std::vector *inputs = nullptr, - const std::vector *outputs = nullptr, - tflite::BuiltinOptions builtin_options_type = tflite::BuiltinOptions_NONE, - flatbuffers::Offset builtin_options = 0, - const std::vector *custom_options = nullptr, - tflite::CustomOptionsFormat custom_options_format = tflite::CustomOptionsFormat_FLEXBUFFERS, - const std::vector *mutating_variable_inputs = nullptr, - const std::vector *intermediates = nullptr) { - auto inputs__ = inputs ? _fbb.CreateVector(*inputs) : 0; - auto outputs__ = outputs ? _fbb.CreateVector(*outputs) : 0; - auto custom_options__ = custom_options ? _fbb.CreateVector(*custom_options) : 0; - auto mutating_variable_inputs__ = mutating_variable_inputs ? _fbb.CreateVector(*mutating_variable_inputs) : 0; - auto intermediates__ = intermediates ? _fbb.CreateVector(*intermediates) : 0; - return tflite::CreateOperator( - _fbb, - opcode_index, - inputs__, - outputs__, - builtin_options_type, - builtin_options, - custom_options__, - custom_options_format, - mutating_variable_inputs__, - intermediates__); -} - -flatbuffers::Offset CreateOperator(flatbuffers::FlatBufferBuilder &_fbb, const OperatorT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct SubGraphT : public flatbuffers::NativeTable { - typedef SubGraph TableType; - std::vector> tensors; - std::vector inputs; - std::vector outputs; - std::vector> operators; - std::string name; - SubGraphT() { - } -}; - -struct SubGraph FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef SubGraphT NativeTableType; - typedef SubGraphBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_TENSORS = 4, - VT_INPUTS = 6, - VT_OUTPUTS = 8, - VT_OPERATORS = 10, - VT_NAME = 12 - }; - const flatbuffers::Vector> *tensors() const { - return GetPointer> *>(VT_TENSORS); - } - const flatbuffers::Vector *inputs() const { - return GetPointer *>(VT_INPUTS); - } - const flatbuffers::Vector *outputs() const { - return GetPointer *>(VT_OUTPUTS); - } - const flatbuffers::Vector> *operators() const { - return GetPointer> *>(VT_OPERATORS); - } - const flatbuffers::String *name() const { - return GetPointer(VT_NAME); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyOffset(verifier, VT_TENSORS) && - verifier.VerifyVector(tensors()) && - verifier.VerifyVectorOfTables(tensors()) && - VerifyOffset(verifier, VT_INPUTS) && - verifier.VerifyVector(inputs()) && - VerifyOffset(verifier, VT_OUTPUTS) && - verifier.VerifyVector(outputs()) && - VerifyOffset(verifier, VT_OPERATORS) && - verifier.VerifyVector(operators()) && - verifier.VerifyVectorOfTables(operators()) && - VerifyOffset(verifier, VT_NAME) && - verifier.VerifyString(name()) && - verifier.EndTable(); - } - SubGraphT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(SubGraphT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const SubGraphT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct SubGraphBuilder { - typedef SubGraph Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_tensors(flatbuffers::Offset>> tensors) { - fbb_.AddOffset(SubGraph::VT_TENSORS, tensors); - } - void add_inputs(flatbuffers::Offset> inputs) { - fbb_.AddOffset(SubGraph::VT_INPUTS, inputs); - } - void add_outputs(flatbuffers::Offset> outputs) { - fbb_.AddOffset(SubGraph::VT_OUTPUTS, outputs); - } - void add_operators(flatbuffers::Offset>> operators) { - fbb_.AddOffset(SubGraph::VT_OPERATORS, operators); - } - void add_name(flatbuffers::Offset name) { - fbb_.AddOffset(SubGraph::VT_NAME, name); - } - explicit SubGraphBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - SubGraphBuilder &operator=(const SubGraphBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateSubGraph( - flatbuffers::FlatBufferBuilder &_fbb, - flatbuffers::Offset>> tensors = 0, - flatbuffers::Offset> inputs = 0, - flatbuffers::Offset> outputs = 0, - flatbuffers::Offset>> operators = 0, - flatbuffers::Offset name = 0) { - SubGraphBuilder builder_(_fbb); - builder_.add_name(name); - builder_.add_operators(operators); - builder_.add_outputs(outputs); - builder_.add_inputs(inputs); - builder_.add_tensors(tensors); - return builder_.Finish(); -} - -inline flatbuffers::Offset CreateSubGraphDirect( - flatbuffers::FlatBufferBuilder &_fbb, - const std::vector> *tensors = nullptr, - const std::vector *inputs = nullptr, - const std::vector *outputs = nullptr, - const std::vector> *operators = nullptr, - const char *name = nullptr) { - auto tensors__ = tensors ? _fbb.CreateVector>(*tensors) : 0; - auto inputs__ = inputs ? _fbb.CreateVector(*inputs) : 0; - auto outputs__ = outputs ? _fbb.CreateVector(*outputs) : 0; - auto operators__ = operators ? _fbb.CreateVector>(*operators) : 0; - auto name__ = name ? _fbb.CreateString(name) : 0; - return tflite::CreateSubGraph( - _fbb, - tensors__, - inputs__, - outputs__, - operators__, - name__); -} - -flatbuffers::Offset CreateSubGraph(flatbuffers::FlatBufferBuilder &_fbb, const SubGraphT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct BufferT : public flatbuffers::NativeTable { - typedef Buffer TableType; - std::vector data; - BufferT() { - } -}; - -struct Buffer FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef BufferT NativeTableType; - typedef BufferBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_DATA = 4 - }; - const flatbuffers::Vector *data() const { - return GetPointer *>(VT_DATA); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyOffset(verifier, VT_DATA) && - verifier.VerifyVector(data()) && - verifier.EndTable(); - } - BufferT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(BufferT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const BufferT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct BufferBuilder { - typedef Buffer Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_data(flatbuffers::Offset> data) { - fbb_.AddOffset(Buffer::VT_DATA, data); - } - explicit BufferBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - BufferBuilder &operator=(const BufferBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateBuffer( - flatbuffers::FlatBufferBuilder &_fbb, - flatbuffers::Offset> data = 0) { - BufferBuilder builder_(_fbb); - builder_.add_data(data); - return builder_.Finish(); -} - -inline flatbuffers::Offset CreateBufferDirect( - flatbuffers::FlatBufferBuilder &_fbb, - const std::vector *data = nullptr) { - if (data) { _fbb.ForceVectorAlignment(data->size(), sizeof(uint8_t), 16); } - auto data__ = data ? _fbb.CreateVector(*data) : 0; - return tflite::CreateBuffer( - _fbb, - data__); -} - -flatbuffers::Offset CreateBuffer(flatbuffers::FlatBufferBuilder &_fbb, const BufferT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct MetadataT : public flatbuffers::NativeTable { - typedef Metadata TableType; - std::string name; - uint32_t buffer; - MetadataT() - : buffer(0) { - } -}; - -struct Metadata FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef MetadataT NativeTableType; - typedef MetadataBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_NAME = 4, - VT_BUFFER = 6 - }; - const flatbuffers::String *name() const { - return GetPointer(VT_NAME); - } - uint32_t buffer() const { - return GetField(VT_BUFFER, 0); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyOffset(verifier, VT_NAME) && - verifier.VerifyString(name()) && - VerifyField(verifier, VT_BUFFER) && - verifier.EndTable(); - } - MetadataT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(MetadataT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const MetadataT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct MetadataBuilder { - typedef Metadata Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_name(flatbuffers::Offset name) { - fbb_.AddOffset(Metadata::VT_NAME, name); - } - void add_buffer(uint32_t buffer) { - fbb_.AddElement(Metadata::VT_BUFFER, buffer, 0); - } - explicit MetadataBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - MetadataBuilder &operator=(const MetadataBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateMetadata( - flatbuffers::FlatBufferBuilder &_fbb, - flatbuffers::Offset name = 0, - uint32_t buffer = 0) { - MetadataBuilder builder_(_fbb); - builder_.add_buffer(buffer); - builder_.add_name(name); - return builder_.Finish(); -} - -inline flatbuffers::Offset CreateMetadataDirect( - flatbuffers::FlatBufferBuilder &_fbb, - const char *name = nullptr, - uint32_t buffer = 0) { - auto name__ = name ? _fbb.CreateString(name) : 0; - return tflite::CreateMetadata( - _fbb, - name__, - buffer); -} - -flatbuffers::Offset CreateMetadata(flatbuffers::FlatBufferBuilder &_fbb, const MetadataT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct TensorMapT : public flatbuffers::NativeTable { - typedef TensorMap TableType; - std::string name; - uint32_t tensor_index; - TensorMapT() - : tensor_index(0) { - } -}; - -struct TensorMap FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef TensorMapT NativeTableType; - typedef TensorMapBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_NAME = 4, - VT_TENSOR_INDEX = 6 - }; - const flatbuffers::String *name() const { - return GetPointer(VT_NAME); - } - uint32_t tensor_index() const { - return GetField(VT_TENSOR_INDEX, 0); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyOffset(verifier, VT_NAME) && - verifier.VerifyString(name()) && - VerifyField(verifier, VT_TENSOR_INDEX) && - verifier.EndTable(); - } - TensorMapT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(TensorMapT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const TensorMapT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct TensorMapBuilder { - typedef TensorMap Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_name(flatbuffers::Offset name) { - fbb_.AddOffset(TensorMap::VT_NAME, name); - } - void add_tensor_index(uint32_t tensor_index) { - fbb_.AddElement(TensorMap::VT_TENSOR_INDEX, tensor_index, 0); - } - explicit TensorMapBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - TensorMapBuilder &operator=(const TensorMapBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateTensorMap( - flatbuffers::FlatBufferBuilder &_fbb, - flatbuffers::Offset name = 0, - uint32_t tensor_index = 0) { - TensorMapBuilder builder_(_fbb); - builder_.add_tensor_index(tensor_index); - builder_.add_name(name); - return builder_.Finish(); -} - -inline flatbuffers::Offset CreateTensorMapDirect( - flatbuffers::FlatBufferBuilder &_fbb, - const char *name = nullptr, - uint32_t tensor_index = 0) { - auto name__ = name ? _fbb.CreateString(name) : 0; - return tflite::CreateTensorMap( - _fbb, - name__, - tensor_index); -} - -flatbuffers::Offset CreateTensorMap(flatbuffers::FlatBufferBuilder &_fbb, const TensorMapT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct SignatureDefT : public flatbuffers::NativeTable { - typedef SignatureDef TableType; - std::vector> inputs; - std::vector> outputs; - std::string signature_key; - uint32_t subgraph_index; - SignatureDefT() - : subgraph_index(0) { - } -}; - -struct SignatureDef FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef SignatureDefT NativeTableType; - typedef SignatureDefBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_INPUTS = 4, - VT_OUTPUTS = 6, - VT_SIGNATURE_KEY = 8, - VT_SUBGRAPH_INDEX = 12 - }; - const flatbuffers::Vector> *inputs() const { - return GetPointer> *>(VT_INPUTS); - } - const flatbuffers::Vector> *outputs() const { - return GetPointer> *>(VT_OUTPUTS); - } - const flatbuffers::String *signature_key() const { - return GetPointer(VT_SIGNATURE_KEY); - } - uint32_t subgraph_index() const { - return GetField(VT_SUBGRAPH_INDEX, 0); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyOffset(verifier, VT_INPUTS) && - verifier.VerifyVector(inputs()) && - verifier.VerifyVectorOfTables(inputs()) && - VerifyOffset(verifier, VT_OUTPUTS) && - verifier.VerifyVector(outputs()) && - verifier.VerifyVectorOfTables(outputs()) && - VerifyOffset(verifier, VT_SIGNATURE_KEY) && - verifier.VerifyString(signature_key()) && - VerifyField(verifier, VT_SUBGRAPH_INDEX) && - verifier.EndTable(); - } - SignatureDefT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(SignatureDefT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const SignatureDefT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct SignatureDefBuilder { - typedef SignatureDef Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_inputs(flatbuffers::Offset>> inputs) { - fbb_.AddOffset(SignatureDef::VT_INPUTS, inputs); - } - void add_outputs(flatbuffers::Offset>> outputs) { - fbb_.AddOffset(SignatureDef::VT_OUTPUTS, outputs); - } - void add_signature_key(flatbuffers::Offset signature_key) { - fbb_.AddOffset(SignatureDef::VT_SIGNATURE_KEY, signature_key); - } - void add_subgraph_index(uint32_t subgraph_index) { - fbb_.AddElement(SignatureDef::VT_SUBGRAPH_INDEX, subgraph_index, 0); - } - explicit SignatureDefBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - SignatureDefBuilder &operator=(const SignatureDefBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateSignatureDef( - flatbuffers::FlatBufferBuilder &_fbb, - flatbuffers::Offset>> inputs = 0, - flatbuffers::Offset>> outputs = 0, - flatbuffers::Offset signature_key = 0, - uint32_t subgraph_index = 0) { - SignatureDefBuilder builder_(_fbb); - builder_.add_subgraph_index(subgraph_index); - builder_.add_signature_key(signature_key); - builder_.add_outputs(outputs); - builder_.add_inputs(inputs); - return builder_.Finish(); -} - -inline flatbuffers::Offset CreateSignatureDefDirect( - flatbuffers::FlatBufferBuilder &_fbb, - const std::vector> *inputs = nullptr, - const std::vector> *outputs = nullptr, - const char *signature_key = nullptr, - uint32_t subgraph_index = 0) { - auto inputs__ = inputs ? _fbb.CreateVector>(*inputs) : 0; - auto outputs__ = outputs ? _fbb.CreateVector>(*outputs) : 0; - auto signature_key__ = signature_key ? _fbb.CreateString(signature_key) : 0; - return tflite::CreateSignatureDef( - _fbb, - inputs__, - outputs__, - signature_key__, - subgraph_index); -} - -flatbuffers::Offset CreateSignatureDef(flatbuffers::FlatBufferBuilder &_fbb, const SignatureDefT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -struct ModelT : public flatbuffers::NativeTable { - typedef Model TableType; - uint32_t version; - std::vector> operator_codes; - std::vector> subgraphs; - std::string description; - std::vector> buffers; - std::vector metadata_buffer; - std::vector> metadata; - std::vector> signature_defs; - ModelT() - : version(0) { - } -}; - -struct Model FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - typedef ModelT NativeTableType; - typedef ModelBuilder Builder; - enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { - VT_VERSION = 4, - VT_OPERATOR_CODES = 6, - VT_SUBGRAPHS = 8, - VT_DESCRIPTION = 10, - VT_BUFFERS = 12, - VT_METADATA_BUFFER = 14, - VT_METADATA = 16, - VT_SIGNATURE_DEFS = 18 - }; - uint32_t version() const { - return GetField(VT_VERSION, 0); - } - const flatbuffers::Vector> *operator_codes() const { - return GetPointer> *>(VT_OPERATOR_CODES); - } - const flatbuffers::Vector> *subgraphs() const { - return GetPointer> *>(VT_SUBGRAPHS); - } - const flatbuffers::String *description() const { - return GetPointer(VT_DESCRIPTION); - } - const flatbuffers::Vector> *buffers() const { - return GetPointer> *>(VT_BUFFERS); - } - const flatbuffers::Vector *metadata_buffer() const { - return GetPointer *>(VT_METADATA_BUFFER); - } - const flatbuffers::Vector> *metadata() const { - return GetPointer> *>(VT_METADATA); - } - const flatbuffers::Vector> *signature_defs() const { - return GetPointer> *>(VT_SIGNATURE_DEFS); - } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, VT_VERSION) && - VerifyOffset(verifier, VT_OPERATOR_CODES) && - verifier.VerifyVector(operator_codes()) && - verifier.VerifyVectorOfTables(operator_codes()) && - VerifyOffset(verifier, VT_SUBGRAPHS) && - verifier.VerifyVector(subgraphs()) && - verifier.VerifyVectorOfTables(subgraphs()) && - VerifyOffset(verifier, VT_DESCRIPTION) && - verifier.VerifyString(description()) && - VerifyOffset(verifier, VT_BUFFERS) && - verifier.VerifyVector(buffers()) && - verifier.VerifyVectorOfTables(buffers()) && - VerifyOffset(verifier, VT_METADATA_BUFFER) && - verifier.VerifyVector(metadata_buffer()) && - VerifyOffset(verifier, VT_METADATA) && - verifier.VerifyVector(metadata()) && - verifier.VerifyVectorOfTables(metadata()) && - VerifyOffset(verifier, VT_SIGNATURE_DEFS) && - verifier.VerifyVector(signature_defs()) && - verifier.VerifyVectorOfTables(signature_defs()) && - verifier.EndTable(); - } - ModelT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; - void UnPackTo(ModelT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; - static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const ModelT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); -}; - -struct ModelBuilder { - typedef Model Table; - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_version(uint32_t version) { - fbb_.AddElement(Model::VT_VERSION, version, 0); - } - void add_operator_codes(flatbuffers::Offset>> operator_codes) { - fbb_.AddOffset(Model::VT_OPERATOR_CODES, operator_codes); - } - void add_subgraphs(flatbuffers::Offset>> subgraphs) { - fbb_.AddOffset(Model::VT_SUBGRAPHS, subgraphs); - } - void add_description(flatbuffers::Offset description) { - fbb_.AddOffset(Model::VT_DESCRIPTION, description); - } - void add_buffers(flatbuffers::Offset>> buffers) { - fbb_.AddOffset(Model::VT_BUFFERS, buffers); - } - void add_metadata_buffer(flatbuffers::Offset> metadata_buffer) { - fbb_.AddOffset(Model::VT_METADATA_BUFFER, metadata_buffer); - } - void add_metadata(flatbuffers::Offset>> metadata) { - fbb_.AddOffset(Model::VT_METADATA, metadata); - } - void add_signature_defs(flatbuffers::Offset>> signature_defs) { - fbb_.AddOffset(Model::VT_SIGNATURE_DEFS, signature_defs); - } - explicit ModelBuilder(flatbuffers::FlatBufferBuilder &_fbb) - : fbb_(_fbb) { - start_ = fbb_.StartTable(); - } - ModelBuilder &operator=(const ModelBuilder &); - flatbuffers::Offset Finish() { - const auto end = fbb_.EndTable(start_); - auto o = flatbuffers::Offset(end); - return o; - } -}; - -inline flatbuffers::Offset CreateModel( - flatbuffers::FlatBufferBuilder &_fbb, - uint32_t version = 0, - flatbuffers::Offset>> operator_codes = 0, - flatbuffers::Offset>> subgraphs = 0, - flatbuffers::Offset description = 0, - flatbuffers::Offset>> buffers = 0, - flatbuffers::Offset> metadata_buffer = 0, - flatbuffers::Offset>> metadata = 0, - flatbuffers::Offset>> signature_defs = 0) { - ModelBuilder builder_(_fbb); - builder_.add_signature_defs(signature_defs); - builder_.add_metadata(metadata); - builder_.add_metadata_buffer(metadata_buffer); - builder_.add_buffers(buffers); - builder_.add_description(description); - builder_.add_subgraphs(subgraphs); - builder_.add_operator_codes(operator_codes); - builder_.add_version(version); - return builder_.Finish(); -} - -inline flatbuffers::Offset CreateModelDirect( - flatbuffers::FlatBufferBuilder &_fbb, - uint32_t version = 0, - const std::vector> *operator_codes = nullptr, - const std::vector> *subgraphs = nullptr, - const char *description = nullptr, - const std::vector> *buffers = nullptr, - const std::vector *metadata_buffer = nullptr, - const std::vector> *metadata = nullptr, - const std::vector> *signature_defs = nullptr) { - auto operator_codes__ = operator_codes ? _fbb.CreateVector>(*operator_codes) : 0; - auto subgraphs__ = subgraphs ? _fbb.CreateVector>(*subgraphs) : 0; - auto description__ = description ? _fbb.CreateString(description) : 0; - auto buffers__ = buffers ? _fbb.CreateVector>(*buffers) : 0; - auto metadata_buffer__ = metadata_buffer ? _fbb.CreateVector(*metadata_buffer) : 0; - auto metadata__ = metadata ? _fbb.CreateVector>(*metadata) : 0; - auto signature_defs__ = signature_defs ? _fbb.CreateVector>(*signature_defs) : 0; - return tflite::CreateModel( - _fbb, - version, - operator_codes__, - subgraphs__, - description__, - buffers__, - metadata_buffer__, - metadata__, - signature_defs__); -} - -flatbuffers::Offset CreateModel(flatbuffers::FlatBufferBuilder &_fbb, const ModelT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); - -inline CustomQuantizationT *CustomQuantization::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new CustomQuantizationT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void CustomQuantization::UnPackTo(CustomQuantizationT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = custom(); if (_e) { _o->custom.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->custom[_i] = _e->Get(_i); } } } -} - -inline flatbuffers::Offset CustomQuantization::Pack(flatbuffers::FlatBufferBuilder &_fbb, const CustomQuantizationT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateCustomQuantization(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateCustomQuantization(flatbuffers::FlatBufferBuilder &_fbb, const CustomQuantizationT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const CustomQuantizationT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - _fbb.ForceVectorAlignment(_o->custom.size(), sizeof(uint8_t), 16); - auto _custom = _o->custom.size() ? _fbb.CreateVector(_o->custom) : 0; - return tflite::CreateCustomQuantization( - _fbb, - _custom); -} - -inline QuantizationParametersT *QuantizationParameters::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new QuantizationParametersT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void QuantizationParameters::UnPackTo(QuantizationParametersT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = min(); if (_e) { _o->min.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->min[_i] = _e->Get(_i); } } } - { auto _e = max(); if (_e) { _o->max.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->max[_i] = _e->Get(_i); } } } - { auto _e = scale(); if (_e) { _o->scale.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->scale[_i] = _e->Get(_i); } } } - { auto _e = zero_point(); if (_e) { _o->zero_point.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->zero_point[_i] = _e->Get(_i); } } } - { auto _e = details_type(); _o->details.type = _e; } - { auto _e = details(); if (_e) _o->details.value = tflite::QuantizationDetailsUnion::UnPack(_e, details_type(), _resolver); } - { auto _e = quantized_dimension(); _o->quantized_dimension = _e; } -} - -inline flatbuffers::Offset QuantizationParameters::Pack(flatbuffers::FlatBufferBuilder &_fbb, const QuantizationParametersT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateQuantizationParameters(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateQuantizationParameters(flatbuffers::FlatBufferBuilder &_fbb, const QuantizationParametersT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const QuantizationParametersT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _min = _o->min.size() ? _fbb.CreateVector(_o->min) : 0; - auto _max = _o->max.size() ? _fbb.CreateVector(_o->max) : 0; - auto _scale = _o->scale.size() ? _fbb.CreateVector(_o->scale) : 0; - auto _zero_point = _o->zero_point.size() ? _fbb.CreateVector(_o->zero_point) : 0; - auto _details_type = _o->details.type; - auto _details = _o->details.Pack(_fbb); - auto _quantized_dimension = _o->quantized_dimension; - return tflite::CreateQuantizationParameters( - _fbb, - _min, - _max, - _scale, - _zero_point, - _details_type, - _details, - _quantized_dimension); -} - -inline Int32VectorT *Int32Vector::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new Int32VectorT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void Int32Vector::UnPackTo(Int32VectorT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = values(); if (_e) { _o->values.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->values[_i] = _e->Get(_i); } } } -} - -inline flatbuffers::Offset Int32Vector::Pack(flatbuffers::FlatBufferBuilder &_fbb, const Int32VectorT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateInt32Vector(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateInt32Vector(flatbuffers::FlatBufferBuilder &_fbb, const Int32VectorT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const Int32VectorT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _values = _o->values.size() ? _fbb.CreateVector(_o->values) : 0; - return tflite::CreateInt32Vector( - _fbb, - _values); -} - -inline Uint16VectorT *Uint16Vector::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new Uint16VectorT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void Uint16Vector::UnPackTo(Uint16VectorT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = values(); if (_e) { _o->values.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->values[_i] = _e->Get(_i); } } } -} - -inline flatbuffers::Offset Uint16Vector::Pack(flatbuffers::FlatBufferBuilder &_fbb, const Uint16VectorT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateUint16Vector(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateUint16Vector(flatbuffers::FlatBufferBuilder &_fbb, const Uint16VectorT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const Uint16VectorT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - _fbb.ForceVectorAlignment(_o->values.size(), sizeof(uint16_t), 4); - auto _values = _o->values.size() ? _fbb.CreateVector(_o->values) : 0; - return tflite::CreateUint16Vector( - _fbb, - _values); -} - -inline Uint8VectorT *Uint8Vector::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new Uint8VectorT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void Uint8Vector::UnPackTo(Uint8VectorT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = values(); if (_e) { _o->values.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->values[_i] = _e->Get(_i); } } } -} - -inline flatbuffers::Offset Uint8Vector::Pack(flatbuffers::FlatBufferBuilder &_fbb, const Uint8VectorT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateUint8Vector(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateUint8Vector(flatbuffers::FlatBufferBuilder &_fbb, const Uint8VectorT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const Uint8VectorT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - _fbb.ForceVectorAlignment(_o->values.size(), sizeof(uint8_t), 4); - auto _values = _o->values.size() ? _fbb.CreateVector(_o->values) : 0; - return tflite::CreateUint8Vector( - _fbb, - _values); -} - -inline DimensionMetadataT *DimensionMetadata::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new DimensionMetadataT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void DimensionMetadata::UnPackTo(DimensionMetadataT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = format(); _o->format = _e; } - { auto _e = dense_size(); _o->dense_size = _e; } - { auto _e = array_segments_type(); _o->array_segments.type = _e; } - { auto _e = array_segments(); if (_e) _o->array_segments.value = tflite::SparseIndexVectorUnion::UnPack(_e, array_segments_type(), _resolver); } - { auto _e = array_indices_type(); _o->array_indices.type = _e; } - { auto _e = array_indices(); if (_e) _o->array_indices.value = tflite::SparseIndexVectorUnion::UnPack(_e, array_indices_type(), _resolver); } -} - -inline flatbuffers::Offset DimensionMetadata::Pack(flatbuffers::FlatBufferBuilder &_fbb, const DimensionMetadataT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateDimensionMetadata(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateDimensionMetadata(flatbuffers::FlatBufferBuilder &_fbb, const DimensionMetadataT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const DimensionMetadataT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _format = _o->format; - auto _dense_size = _o->dense_size; - auto _array_segments_type = _o->array_segments.type; - auto _array_segments = _o->array_segments.Pack(_fbb); - auto _array_indices_type = _o->array_indices.type; - auto _array_indices = _o->array_indices.Pack(_fbb); - return tflite::CreateDimensionMetadata( - _fbb, - _format, - _dense_size, - _array_segments_type, - _array_segments, - _array_indices_type, - _array_indices); -} - -inline SparsityParametersT *SparsityParameters::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new SparsityParametersT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void SparsityParameters::UnPackTo(SparsityParametersT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = traversal_order(); if (_e) { _o->traversal_order.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->traversal_order[_i] = _e->Get(_i); } } } - { auto _e = block_map(); if (_e) { _o->block_map.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->block_map[_i] = _e->Get(_i); } } } - { auto _e = dim_metadata(); if (_e) { _o->dim_metadata.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->dim_metadata[_i] = std::unique_ptr(_e->Get(_i)->UnPack(_resolver)); } } } -} - -inline flatbuffers::Offset SparsityParameters::Pack(flatbuffers::FlatBufferBuilder &_fbb, const SparsityParametersT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateSparsityParameters(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateSparsityParameters(flatbuffers::FlatBufferBuilder &_fbb, const SparsityParametersT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const SparsityParametersT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _traversal_order = _o->traversal_order.size() ? _fbb.CreateVector(_o->traversal_order) : 0; - auto _block_map = _o->block_map.size() ? _fbb.CreateVector(_o->block_map) : 0; - auto _dim_metadata = _o->dim_metadata.size() ? _fbb.CreateVector> (_o->dim_metadata.size(), [](size_t i, _VectorArgs *__va) { return CreateDimensionMetadata(*__va->__fbb, __va->__o->dim_metadata[i].get(), __va->__rehasher); }, &_va ) : 0; - return tflite::CreateSparsityParameters( - _fbb, - _traversal_order, - _block_map, - _dim_metadata); -} - -inline TensorT *Tensor::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new TensorT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void Tensor::UnPackTo(TensorT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = shape(); if (_e) { _o->shape.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->shape[_i] = _e->Get(_i); } } } - { auto _e = type(); _o->type = _e; } - { auto _e = buffer(); _o->buffer = _e; } - { auto _e = name(); if (_e) _o->name = _e->str(); } - { auto _e = quantization(); if (_e) _o->quantization = std::unique_ptr(_e->UnPack(_resolver)); } - { auto _e = is_variable(); _o->is_variable = _e; } - { auto _e = sparsity(); if (_e) _o->sparsity = std::unique_ptr(_e->UnPack(_resolver)); } - { auto _e = shape_signature(); if (_e) { _o->shape_signature.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->shape_signature[_i] = _e->Get(_i); } } } - { auto _e = has_rank(); _o->has_rank = _e; } -} - -inline flatbuffers::Offset Tensor::Pack(flatbuffers::FlatBufferBuilder &_fbb, const TensorT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateTensor(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateTensor(flatbuffers::FlatBufferBuilder &_fbb, const TensorT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const TensorT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _shape = _o->shape.size() ? _fbb.CreateVector(_o->shape) : 0; - auto _type = _o->type; - auto _buffer = _o->buffer; - auto _name = _o->name.empty() ? 0 : _fbb.CreateString(_o->name); - auto _quantization = _o->quantization ? CreateQuantizationParameters(_fbb, _o->quantization.get(), _rehasher) : 0; - auto _is_variable = _o->is_variable; - auto _sparsity = _o->sparsity ? CreateSparsityParameters(_fbb, _o->sparsity.get(), _rehasher) : 0; - auto _shape_signature = _o->shape_signature.size() ? _fbb.CreateVector(_o->shape_signature) : 0; - auto _has_rank = _o->has_rank; - return tflite::CreateTensor( - _fbb, - _shape, - _type, - _buffer, - _name, - _quantization, - _is_variable, - _sparsity, - _shape_signature, - _has_rank); -} - -inline Conv2DOptionsT *Conv2DOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new Conv2DOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void Conv2DOptions::UnPackTo(Conv2DOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = padding(); _o->padding = _e; } - { auto _e = stride_w(); _o->stride_w = _e; } - { auto _e = stride_h(); _o->stride_h = _e; } - { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } - { auto _e = dilation_w_factor(); _o->dilation_w_factor = _e; } - { auto _e = dilation_h_factor(); _o->dilation_h_factor = _e; } -} - -inline flatbuffers::Offset Conv2DOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const Conv2DOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateConv2DOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateConv2DOptions(flatbuffers::FlatBufferBuilder &_fbb, const Conv2DOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const Conv2DOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _padding = _o->padding; - auto _stride_w = _o->stride_w; - auto _stride_h = _o->stride_h; - auto _fused_activation_function = _o->fused_activation_function; - auto _dilation_w_factor = _o->dilation_w_factor; - auto _dilation_h_factor = _o->dilation_h_factor; - return tflite::CreateConv2DOptions( - _fbb, - _padding, - _stride_w, - _stride_h, - _fused_activation_function, - _dilation_w_factor, - _dilation_h_factor); -} - -inline Conv3DOptionsT *Conv3DOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new Conv3DOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void Conv3DOptions::UnPackTo(Conv3DOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = padding(); _o->padding = _e; } - { auto _e = stride_d(); _o->stride_d = _e; } - { auto _e = stride_w(); _o->stride_w = _e; } - { auto _e = stride_h(); _o->stride_h = _e; } - { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } - { auto _e = dilation_d_factor(); _o->dilation_d_factor = _e; } - { auto _e = dilation_w_factor(); _o->dilation_w_factor = _e; } - { auto _e = dilation_h_factor(); _o->dilation_h_factor = _e; } -} - -inline flatbuffers::Offset Conv3DOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const Conv3DOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateConv3DOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateConv3DOptions(flatbuffers::FlatBufferBuilder &_fbb, const Conv3DOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const Conv3DOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _padding = _o->padding; - auto _stride_d = _o->stride_d; - auto _stride_w = _o->stride_w; - auto _stride_h = _o->stride_h; - auto _fused_activation_function = _o->fused_activation_function; - auto _dilation_d_factor = _o->dilation_d_factor; - auto _dilation_w_factor = _o->dilation_w_factor; - auto _dilation_h_factor = _o->dilation_h_factor; - return tflite::CreateConv3DOptions( - _fbb, - _padding, - _stride_d, - _stride_w, - _stride_h, - _fused_activation_function, - _dilation_d_factor, - _dilation_w_factor, - _dilation_h_factor); -} - -inline Pool2DOptionsT *Pool2DOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new Pool2DOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void Pool2DOptions::UnPackTo(Pool2DOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = padding(); _o->padding = _e; } - { auto _e = stride_w(); _o->stride_w = _e; } - { auto _e = stride_h(); _o->stride_h = _e; } - { auto _e = filter_width(); _o->filter_width = _e; } - { auto _e = filter_height(); _o->filter_height = _e; } - { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } -} - -inline flatbuffers::Offset Pool2DOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const Pool2DOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreatePool2DOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreatePool2DOptions(flatbuffers::FlatBufferBuilder &_fbb, const Pool2DOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const Pool2DOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _padding = _o->padding; - auto _stride_w = _o->stride_w; - auto _stride_h = _o->stride_h; - auto _filter_width = _o->filter_width; - auto _filter_height = _o->filter_height; - auto _fused_activation_function = _o->fused_activation_function; - return tflite::CreatePool2DOptions( - _fbb, - _padding, - _stride_w, - _stride_h, - _filter_width, - _filter_height, - _fused_activation_function); -} - -inline DepthwiseConv2DOptionsT *DepthwiseConv2DOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new DepthwiseConv2DOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void DepthwiseConv2DOptions::UnPackTo(DepthwiseConv2DOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = padding(); _o->padding = _e; } - { auto _e = stride_w(); _o->stride_w = _e; } - { auto _e = stride_h(); _o->stride_h = _e; } - { auto _e = depth_multiplier(); _o->depth_multiplier = _e; } - { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } - { auto _e = dilation_w_factor(); _o->dilation_w_factor = _e; } - { auto _e = dilation_h_factor(); _o->dilation_h_factor = _e; } -} - -inline flatbuffers::Offset DepthwiseConv2DOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const DepthwiseConv2DOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateDepthwiseConv2DOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateDepthwiseConv2DOptions(flatbuffers::FlatBufferBuilder &_fbb, const DepthwiseConv2DOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const DepthwiseConv2DOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _padding = _o->padding; - auto _stride_w = _o->stride_w; - auto _stride_h = _o->stride_h; - auto _depth_multiplier = _o->depth_multiplier; - auto _fused_activation_function = _o->fused_activation_function; - auto _dilation_w_factor = _o->dilation_w_factor; - auto _dilation_h_factor = _o->dilation_h_factor; - return tflite::CreateDepthwiseConv2DOptions( - _fbb, - _padding, - _stride_w, - _stride_h, - _depth_multiplier, - _fused_activation_function, - _dilation_w_factor, - _dilation_h_factor); -} - -inline ConcatEmbeddingsOptionsT *ConcatEmbeddingsOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new ConcatEmbeddingsOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void ConcatEmbeddingsOptions::UnPackTo(ConcatEmbeddingsOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = num_channels(); _o->num_channels = _e; } - { auto _e = num_columns_per_channel(); if (_e) { _o->num_columns_per_channel.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->num_columns_per_channel[_i] = _e->Get(_i); } } } - { auto _e = embedding_dim_per_channel(); if (_e) { _o->embedding_dim_per_channel.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->embedding_dim_per_channel[_i] = _e->Get(_i); } } } -} - -inline flatbuffers::Offset ConcatEmbeddingsOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const ConcatEmbeddingsOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateConcatEmbeddingsOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateConcatEmbeddingsOptions(flatbuffers::FlatBufferBuilder &_fbb, const ConcatEmbeddingsOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const ConcatEmbeddingsOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _num_channels = _o->num_channels; - auto _num_columns_per_channel = _o->num_columns_per_channel.size() ? _fbb.CreateVector(_o->num_columns_per_channel) : 0; - auto _embedding_dim_per_channel = _o->embedding_dim_per_channel.size() ? _fbb.CreateVector(_o->embedding_dim_per_channel) : 0; - return tflite::CreateConcatEmbeddingsOptions( - _fbb, - _num_channels, - _num_columns_per_channel, - _embedding_dim_per_channel); -} - -inline LSHProjectionOptionsT *LSHProjectionOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new LSHProjectionOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void LSHProjectionOptions::UnPackTo(LSHProjectionOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = type(); _o->type = _e; } -} - -inline flatbuffers::Offset LSHProjectionOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const LSHProjectionOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateLSHProjectionOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateLSHProjectionOptions(flatbuffers::FlatBufferBuilder &_fbb, const LSHProjectionOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const LSHProjectionOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _type = _o->type; - return tflite::CreateLSHProjectionOptions( - _fbb, - _type); -} - -inline SVDFOptionsT *SVDFOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new SVDFOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void SVDFOptions::UnPackTo(SVDFOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = rank(); _o->rank = _e; } - { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } - { auto _e = asymmetric_quantize_inputs(); _o->asymmetric_quantize_inputs = _e; } -} - -inline flatbuffers::Offset SVDFOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const SVDFOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateSVDFOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateSVDFOptions(flatbuffers::FlatBufferBuilder &_fbb, const SVDFOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const SVDFOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _rank = _o->rank; - auto _fused_activation_function = _o->fused_activation_function; - auto _asymmetric_quantize_inputs = _o->asymmetric_quantize_inputs; - return tflite::CreateSVDFOptions( - _fbb, - _rank, - _fused_activation_function, - _asymmetric_quantize_inputs); -} - -inline RNNOptionsT *RNNOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new RNNOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void RNNOptions::UnPackTo(RNNOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } - { auto _e = asymmetric_quantize_inputs(); _o->asymmetric_quantize_inputs = _e; } -} - -inline flatbuffers::Offset RNNOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const RNNOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateRNNOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateRNNOptions(flatbuffers::FlatBufferBuilder &_fbb, const RNNOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const RNNOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _fused_activation_function = _o->fused_activation_function; - auto _asymmetric_quantize_inputs = _o->asymmetric_quantize_inputs; - return tflite::CreateRNNOptions( - _fbb, - _fused_activation_function, - _asymmetric_quantize_inputs); -} - -inline SequenceRNNOptionsT *SequenceRNNOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new SequenceRNNOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void SequenceRNNOptions::UnPackTo(SequenceRNNOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = time_major(); _o->time_major = _e; } - { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } - { auto _e = asymmetric_quantize_inputs(); _o->asymmetric_quantize_inputs = _e; } -} - -inline flatbuffers::Offset SequenceRNNOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const SequenceRNNOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateSequenceRNNOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateSequenceRNNOptions(flatbuffers::FlatBufferBuilder &_fbb, const SequenceRNNOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const SequenceRNNOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _time_major = _o->time_major; - auto _fused_activation_function = _o->fused_activation_function; - auto _asymmetric_quantize_inputs = _o->asymmetric_quantize_inputs; - return tflite::CreateSequenceRNNOptions( - _fbb, - _time_major, - _fused_activation_function, - _asymmetric_quantize_inputs); -} - -inline BidirectionalSequenceRNNOptionsT *BidirectionalSequenceRNNOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new BidirectionalSequenceRNNOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void BidirectionalSequenceRNNOptions::UnPackTo(BidirectionalSequenceRNNOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = time_major(); _o->time_major = _e; } - { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } - { auto _e = merge_outputs(); _o->merge_outputs = _e; } - { auto _e = asymmetric_quantize_inputs(); _o->asymmetric_quantize_inputs = _e; } -} - -inline flatbuffers::Offset BidirectionalSequenceRNNOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const BidirectionalSequenceRNNOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateBidirectionalSequenceRNNOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateBidirectionalSequenceRNNOptions(flatbuffers::FlatBufferBuilder &_fbb, const BidirectionalSequenceRNNOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const BidirectionalSequenceRNNOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _time_major = _o->time_major; - auto _fused_activation_function = _o->fused_activation_function; - auto _merge_outputs = _o->merge_outputs; - auto _asymmetric_quantize_inputs = _o->asymmetric_quantize_inputs; - return tflite::CreateBidirectionalSequenceRNNOptions( - _fbb, - _time_major, - _fused_activation_function, - _merge_outputs, - _asymmetric_quantize_inputs); -} - -inline FullyConnectedOptionsT *FullyConnectedOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new FullyConnectedOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void FullyConnectedOptions::UnPackTo(FullyConnectedOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } - { auto _e = weights_format(); _o->weights_format = _e; } - { auto _e = keep_num_dims(); _o->keep_num_dims = _e; } - { auto _e = asymmetric_quantize_inputs(); _o->asymmetric_quantize_inputs = _e; } -} - -inline flatbuffers::Offset FullyConnectedOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const FullyConnectedOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateFullyConnectedOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateFullyConnectedOptions(flatbuffers::FlatBufferBuilder &_fbb, const FullyConnectedOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const FullyConnectedOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _fused_activation_function = _o->fused_activation_function; - auto _weights_format = _o->weights_format; - auto _keep_num_dims = _o->keep_num_dims; - auto _asymmetric_quantize_inputs = _o->asymmetric_quantize_inputs; - return tflite::CreateFullyConnectedOptions( - _fbb, - _fused_activation_function, - _weights_format, - _keep_num_dims, - _asymmetric_quantize_inputs); -} - -inline SoftmaxOptionsT *SoftmaxOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new SoftmaxOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void SoftmaxOptions::UnPackTo(SoftmaxOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = beta(); _o->beta = _e; } -} - -inline flatbuffers::Offset SoftmaxOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const SoftmaxOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateSoftmaxOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateSoftmaxOptions(flatbuffers::FlatBufferBuilder &_fbb, const SoftmaxOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const SoftmaxOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _beta = _o->beta; - return tflite::CreateSoftmaxOptions( - _fbb, - _beta); -} - -inline ConcatenationOptionsT *ConcatenationOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new ConcatenationOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void ConcatenationOptions::UnPackTo(ConcatenationOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = axis(); _o->axis = _e; } - { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } -} - -inline flatbuffers::Offset ConcatenationOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const ConcatenationOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateConcatenationOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateConcatenationOptions(flatbuffers::FlatBufferBuilder &_fbb, const ConcatenationOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const ConcatenationOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _axis = _o->axis; - auto _fused_activation_function = _o->fused_activation_function; - return tflite::CreateConcatenationOptions( - _fbb, - _axis, - _fused_activation_function); -} - -inline AddOptionsT *AddOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new AddOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void AddOptions::UnPackTo(AddOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } - { auto _e = pot_scale_int16(); _o->pot_scale_int16 = _e; } -} - -inline flatbuffers::Offset AddOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const AddOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateAddOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateAddOptions(flatbuffers::FlatBufferBuilder &_fbb, const AddOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const AddOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _fused_activation_function = _o->fused_activation_function; - auto _pot_scale_int16 = _o->pot_scale_int16; - return tflite::CreateAddOptions( - _fbb, - _fused_activation_function, - _pot_scale_int16); -} - -inline MulOptionsT *MulOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new MulOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void MulOptions::UnPackTo(MulOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } -} - -inline flatbuffers::Offset MulOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const MulOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateMulOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateMulOptions(flatbuffers::FlatBufferBuilder &_fbb, const MulOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const MulOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _fused_activation_function = _o->fused_activation_function; - return tflite::CreateMulOptions( - _fbb, - _fused_activation_function); -} - -inline L2NormOptionsT *L2NormOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new L2NormOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void L2NormOptions::UnPackTo(L2NormOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } -} - -inline flatbuffers::Offset L2NormOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const L2NormOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateL2NormOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateL2NormOptions(flatbuffers::FlatBufferBuilder &_fbb, const L2NormOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const L2NormOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _fused_activation_function = _o->fused_activation_function; - return tflite::CreateL2NormOptions( - _fbb, - _fused_activation_function); -} - -inline LocalResponseNormalizationOptionsT *LocalResponseNormalizationOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new LocalResponseNormalizationOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void LocalResponseNormalizationOptions::UnPackTo(LocalResponseNormalizationOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = radius(); _o->radius = _e; } - { auto _e = bias(); _o->bias = _e; } - { auto _e = alpha(); _o->alpha = _e; } - { auto _e = beta(); _o->beta = _e; } -} - -inline flatbuffers::Offset LocalResponseNormalizationOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const LocalResponseNormalizationOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateLocalResponseNormalizationOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateLocalResponseNormalizationOptions(flatbuffers::FlatBufferBuilder &_fbb, const LocalResponseNormalizationOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const LocalResponseNormalizationOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _radius = _o->radius; - auto _bias = _o->bias; - auto _alpha = _o->alpha; - auto _beta = _o->beta; - return tflite::CreateLocalResponseNormalizationOptions( - _fbb, - _radius, - _bias, - _alpha, - _beta); -} - -inline LSTMOptionsT *LSTMOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new LSTMOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void LSTMOptions::UnPackTo(LSTMOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } - { auto _e = cell_clip(); _o->cell_clip = _e; } - { auto _e = proj_clip(); _o->proj_clip = _e; } - { auto _e = kernel_type(); _o->kernel_type = _e; } - { auto _e = asymmetric_quantize_inputs(); _o->asymmetric_quantize_inputs = _e; } -} - -inline flatbuffers::Offset LSTMOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const LSTMOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateLSTMOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateLSTMOptions(flatbuffers::FlatBufferBuilder &_fbb, const LSTMOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const LSTMOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _fused_activation_function = _o->fused_activation_function; - auto _cell_clip = _o->cell_clip; - auto _proj_clip = _o->proj_clip; - auto _kernel_type = _o->kernel_type; - auto _asymmetric_quantize_inputs = _o->asymmetric_quantize_inputs; - return tflite::CreateLSTMOptions( - _fbb, - _fused_activation_function, - _cell_clip, - _proj_clip, - _kernel_type, - _asymmetric_quantize_inputs); -} - -inline UnidirectionalSequenceLSTMOptionsT *UnidirectionalSequenceLSTMOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new UnidirectionalSequenceLSTMOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void UnidirectionalSequenceLSTMOptions::UnPackTo(UnidirectionalSequenceLSTMOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } - { auto _e = cell_clip(); _o->cell_clip = _e; } - { auto _e = proj_clip(); _o->proj_clip = _e; } - { auto _e = time_major(); _o->time_major = _e; } - { auto _e = asymmetric_quantize_inputs(); _o->asymmetric_quantize_inputs = _e; } -} - -inline flatbuffers::Offset UnidirectionalSequenceLSTMOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const UnidirectionalSequenceLSTMOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateUnidirectionalSequenceLSTMOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateUnidirectionalSequenceLSTMOptions(flatbuffers::FlatBufferBuilder &_fbb, const UnidirectionalSequenceLSTMOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const UnidirectionalSequenceLSTMOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _fused_activation_function = _o->fused_activation_function; - auto _cell_clip = _o->cell_clip; - auto _proj_clip = _o->proj_clip; - auto _time_major = _o->time_major; - auto _asymmetric_quantize_inputs = _o->asymmetric_quantize_inputs; - return tflite::CreateUnidirectionalSequenceLSTMOptions( - _fbb, - _fused_activation_function, - _cell_clip, - _proj_clip, - _time_major, - _asymmetric_quantize_inputs); -} - -inline BidirectionalSequenceLSTMOptionsT *BidirectionalSequenceLSTMOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new BidirectionalSequenceLSTMOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void BidirectionalSequenceLSTMOptions::UnPackTo(BidirectionalSequenceLSTMOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } - { auto _e = cell_clip(); _o->cell_clip = _e; } - { auto _e = proj_clip(); _o->proj_clip = _e; } - { auto _e = merge_outputs(); _o->merge_outputs = _e; } - { auto _e = time_major(); _o->time_major = _e; } - { auto _e = asymmetric_quantize_inputs(); _o->asymmetric_quantize_inputs = _e; } -} - -inline flatbuffers::Offset BidirectionalSequenceLSTMOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const BidirectionalSequenceLSTMOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateBidirectionalSequenceLSTMOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateBidirectionalSequenceLSTMOptions(flatbuffers::FlatBufferBuilder &_fbb, const BidirectionalSequenceLSTMOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const BidirectionalSequenceLSTMOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _fused_activation_function = _o->fused_activation_function; - auto _cell_clip = _o->cell_clip; - auto _proj_clip = _o->proj_clip; - auto _merge_outputs = _o->merge_outputs; - auto _time_major = _o->time_major; - auto _asymmetric_quantize_inputs = _o->asymmetric_quantize_inputs; - return tflite::CreateBidirectionalSequenceLSTMOptions( - _fbb, - _fused_activation_function, - _cell_clip, - _proj_clip, - _merge_outputs, - _time_major, - _asymmetric_quantize_inputs); -} - -inline ResizeBilinearOptionsT *ResizeBilinearOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new ResizeBilinearOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void ResizeBilinearOptions::UnPackTo(ResizeBilinearOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = align_corners(); _o->align_corners = _e; } - { auto _e = half_pixel_centers(); _o->half_pixel_centers = _e; } -} - -inline flatbuffers::Offset ResizeBilinearOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const ResizeBilinearOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateResizeBilinearOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateResizeBilinearOptions(flatbuffers::FlatBufferBuilder &_fbb, const ResizeBilinearOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const ResizeBilinearOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _align_corners = _o->align_corners; - auto _half_pixel_centers = _o->half_pixel_centers; - return tflite::CreateResizeBilinearOptions( - _fbb, - _align_corners, - _half_pixel_centers); -} - -inline ResizeNearestNeighborOptionsT *ResizeNearestNeighborOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new ResizeNearestNeighborOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void ResizeNearestNeighborOptions::UnPackTo(ResizeNearestNeighborOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = align_corners(); _o->align_corners = _e; } - { auto _e = half_pixel_centers(); _o->half_pixel_centers = _e; } -} - -inline flatbuffers::Offset ResizeNearestNeighborOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const ResizeNearestNeighborOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateResizeNearestNeighborOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateResizeNearestNeighborOptions(flatbuffers::FlatBufferBuilder &_fbb, const ResizeNearestNeighborOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const ResizeNearestNeighborOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _align_corners = _o->align_corners; - auto _half_pixel_centers = _o->half_pixel_centers; - return tflite::CreateResizeNearestNeighborOptions( - _fbb, - _align_corners, - _half_pixel_centers); -} - -inline CallOptionsT *CallOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new CallOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void CallOptions::UnPackTo(CallOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = subgraph(); _o->subgraph = _e; } -} - -inline flatbuffers::Offset CallOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const CallOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateCallOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateCallOptions(flatbuffers::FlatBufferBuilder &_fbb, const CallOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const CallOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _subgraph = _o->subgraph; - return tflite::CreateCallOptions( - _fbb, - _subgraph); -} - -inline PadOptionsT *PadOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new PadOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void PadOptions::UnPackTo(PadOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset PadOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const PadOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreatePadOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreatePadOptions(flatbuffers::FlatBufferBuilder &_fbb, const PadOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const PadOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreatePadOptions( - _fbb); -} - -inline PadV2OptionsT *PadV2Options::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new PadV2OptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void PadV2Options::UnPackTo(PadV2OptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset PadV2Options::Pack(flatbuffers::FlatBufferBuilder &_fbb, const PadV2OptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreatePadV2Options(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreatePadV2Options(flatbuffers::FlatBufferBuilder &_fbb, const PadV2OptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const PadV2OptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreatePadV2Options( - _fbb); -} - -inline ReshapeOptionsT *ReshapeOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new ReshapeOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void ReshapeOptions::UnPackTo(ReshapeOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = new_shape(); if (_e) { _o->new_shape.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->new_shape[_i] = _e->Get(_i); } } } -} - -inline flatbuffers::Offset ReshapeOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const ReshapeOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateReshapeOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateReshapeOptions(flatbuffers::FlatBufferBuilder &_fbb, const ReshapeOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const ReshapeOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _new_shape = _o->new_shape.size() ? _fbb.CreateVector(_o->new_shape) : 0; - return tflite::CreateReshapeOptions( - _fbb, - _new_shape); -} - -inline SpaceToBatchNDOptionsT *SpaceToBatchNDOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new SpaceToBatchNDOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void SpaceToBatchNDOptions::UnPackTo(SpaceToBatchNDOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset SpaceToBatchNDOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const SpaceToBatchNDOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateSpaceToBatchNDOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateSpaceToBatchNDOptions(flatbuffers::FlatBufferBuilder &_fbb, const SpaceToBatchNDOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const SpaceToBatchNDOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateSpaceToBatchNDOptions( - _fbb); -} - -inline BatchToSpaceNDOptionsT *BatchToSpaceNDOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new BatchToSpaceNDOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void BatchToSpaceNDOptions::UnPackTo(BatchToSpaceNDOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset BatchToSpaceNDOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const BatchToSpaceNDOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateBatchToSpaceNDOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateBatchToSpaceNDOptions(flatbuffers::FlatBufferBuilder &_fbb, const BatchToSpaceNDOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const BatchToSpaceNDOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateBatchToSpaceNDOptions( - _fbb); -} - -inline SkipGramOptionsT *SkipGramOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new SkipGramOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void SkipGramOptions::UnPackTo(SkipGramOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = ngram_size(); _o->ngram_size = _e; } - { auto _e = max_skip_size(); _o->max_skip_size = _e; } - { auto _e = include_all_ngrams(); _o->include_all_ngrams = _e; } -} - -inline flatbuffers::Offset SkipGramOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const SkipGramOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateSkipGramOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateSkipGramOptions(flatbuffers::FlatBufferBuilder &_fbb, const SkipGramOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const SkipGramOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _ngram_size = _o->ngram_size; - auto _max_skip_size = _o->max_skip_size; - auto _include_all_ngrams = _o->include_all_ngrams; - return tflite::CreateSkipGramOptions( - _fbb, - _ngram_size, - _max_skip_size, - _include_all_ngrams); -} - -inline SpaceToDepthOptionsT *SpaceToDepthOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new SpaceToDepthOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void SpaceToDepthOptions::UnPackTo(SpaceToDepthOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = block_size(); _o->block_size = _e; } -} - -inline flatbuffers::Offset SpaceToDepthOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const SpaceToDepthOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateSpaceToDepthOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateSpaceToDepthOptions(flatbuffers::FlatBufferBuilder &_fbb, const SpaceToDepthOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const SpaceToDepthOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _block_size = _o->block_size; - return tflite::CreateSpaceToDepthOptions( - _fbb, - _block_size); -} - -inline DepthToSpaceOptionsT *DepthToSpaceOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new DepthToSpaceOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void DepthToSpaceOptions::UnPackTo(DepthToSpaceOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = block_size(); _o->block_size = _e; } -} - -inline flatbuffers::Offset DepthToSpaceOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const DepthToSpaceOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateDepthToSpaceOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateDepthToSpaceOptions(flatbuffers::FlatBufferBuilder &_fbb, const DepthToSpaceOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const DepthToSpaceOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _block_size = _o->block_size; - return tflite::CreateDepthToSpaceOptions( - _fbb, - _block_size); -} - -inline SubOptionsT *SubOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new SubOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void SubOptions::UnPackTo(SubOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } - { auto _e = pot_scale_int16(); _o->pot_scale_int16 = _e; } -} - -inline flatbuffers::Offset SubOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const SubOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateSubOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateSubOptions(flatbuffers::FlatBufferBuilder &_fbb, const SubOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const SubOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _fused_activation_function = _o->fused_activation_function; - auto _pot_scale_int16 = _o->pot_scale_int16; - return tflite::CreateSubOptions( - _fbb, - _fused_activation_function, - _pot_scale_int16); -} - -inline DivOptionsT *DivOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new DivOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void DivOptions::UnPackTo(DivOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } -} - -inline flatbuffers::Offset DivOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const DivOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateDivOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateDivOptions(flatbuffers::FlatBufferBuilder &_fbb, const DivOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const DivOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _fused_activation_function = _o->fused_activation_function; - return tflite::CreateDivOptions( - _fbb, - _fused_activation_function); -} - -inline TopKV2OptionsT *TopKV2Options::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new TopKV2OptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void TopKV2Options::UnPackTo(TopKV2OptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset TopKV2Options::Pack(flatbuffers::FlatBufferBuilder &_fbb, const TopKV2OptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateTopKV2Options(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateTopKV2Options(flatbuffers::FlatBufferBuilder &_fbb, const TopKV2OptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const TopKV2OptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateTopKV2Options( - _fbb); -} - -inline EmbeddingLookupSparseOptionsT *EmbeddingLookupSparseOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new EmbeddingLookupSparseOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void EmbeddingLookupSparseOptions::UnPackTo(EmbeddingLookupSparseOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = combiner(); _o->combiner = _e; } -} - -inline flatbuffers::Offset EmbeddingLookupSparseOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const EmbeddingLookupSparseOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateEmbeddingLookupSparseOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateEmbeddingLookupSparseOptions(flatbuffers::FlatBufferBuilder &_fbb, const EmbeddingLookupSparseOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const EmbeddingLookupSparseOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _combiner = _o->combiner; - return tflite::CreateEmbeddingLookupSparseOptions( - _fbb, - _combiner); -} - -inline GatherOptionsT *GatherOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new GatherOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void GatherOptions::UnPackTo(GatherOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = axis(); _o->axis = _e; } - { auto _e = batch_dims(); _o->batch_dims = _e; } -} - -inline flatbuffers::Offset GatherOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const GatherOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateGatherOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateGatherOptions(flatbuffers::FlatBufferBuilder &_fbb, const GatherOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const GatherOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _axis = _o->axis; - auto _batch_dims = _o->batch_dims; - return tflite::CreateGatherOptions( - _fbb, - _axis, - _batch_dims); -} - -inline TransposeOptionsT *TransposeOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new TransposeOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void TransposeOptions::UnPackTo(TransposeOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset TransposeOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const TransposeOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateTransposeOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateTransposeOptions(flatbuffers::FlatBufferBuilder &_fbb, const TransposeOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const TransposeOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateTransposeOptions( - _fbb); -} - -inline ExpOptionsT *ExpOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new ExpOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void ExpOptions::UnPackTo(ExpOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset ExpOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const ExpOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateExpOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateExpOptions(flatbuffers::FlatBufferBuilder &_fbb, const ExpOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const ExpOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateExpOptions( - _fbb); -} - -inline CosOptionsT *CosOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new CosOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void CosOptions::UnPackTo(CosOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset CosOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const CosOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateCosOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateCosOptions(flatbuffers::FlatBufferBuilder &_fbb, const CosOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const CosOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateCosOptions( - _fbb); -} - -inline ReducerOptionsT *ReducerOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new ReducerOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void ReducerOptions::UnPackTo(ReducerOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = keep_dims(); _o->keep_dims = _e; } -} - -inline flatbuffers::Offset ReducerOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const ReducerOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateReducerOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateReducerOptions(flatbuffers::FlatBufferBuilder &_fbb, const ReducerOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const ReducerOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _keep_dims = _o->keep_dims; - return tflite::CreateReducerOptions( - _fbb, - _keep_dims); -} - -inline SqueezeOptionsT *SqueezeOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new SqueezeOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void SqueezeOptions::UnPackTo(SqueezeOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = squeeze_dims(); if (_e) { _o->squeeze_dims.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->squeeze_dims[_i] = _e->Get(_i); } } } -} - -inline flatbuffers::Offset SqueezeOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const SqueezeOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateSqueezeOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateSqueezeOptions(flatbuffers::FlatBufferBuilder &_fbb, const SqueezeOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const SqueezeOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _squeeze_dims = _o->squeeze_dims.size() ? _fbb.CreateVector(_o->squeeze_dims) : 0; - return tflite::CreateSqueezeOptions( - _fbb, - _squeeze_dims); -} - -inline SplitOptionsT *SplitOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new SplitOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void SplitOptions::UnPackTo(SplitOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = num_splits(); _o->num_splits = _e; } -} - -inline flatbuffers::Offset SplitOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const SplitOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateSplitOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateSplitOptions(flatbuffers::FlatBufferBuilder &_fbb, const SplitOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const SplitOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _num_splits = _o->num_splits; - return tflite::CreateSplitOptions( - _fbb, - _num_splits); -} - -inline SplitVOptionsT *SplitVOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new SplitVOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void SplitVOptions::UnPackTo(SplitVOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = num_splits(); _o->num_splits = _e; } -} - -inline flatbuffers::Offset SplitVOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const SplitVOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateSplitVOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateSplitVOptions(flatbuffers::FlatBufferBuilder &_fbb, const SplitVOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const SplitVOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _num_splits = _o->num_splits; - return tflite::CreateSplitVOptions( - _fbb, - _num_splits); -} - -inline StridedSliceOptionsT *StridedSliceOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new StridedSliceOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void StridedSliceOptions::UnPackTo(StridedSliceOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = begin_mask(); _o->begin_mask = _e; } - { auto _e = end_mask(); _o->end_mask = _e; } - { auto _e = ellipsis_mask(); _o->ellipsis_mask = _e; } - { auto _e = new_axis_mask(); _o->new_axis_mask = _e; } - { auto _e = shrink_axis_mask(); _o->shrink_axis_mask = _e; } -} - -inline flatbuffers::Offset StridedSliceOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const StridedSliceOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateStridedSliceOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateStridedSliceOptions(flatbuffers::FlatBufferBuilder &_fbb, const StridedSliceOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const StridedSliceOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _begin_mask = _o->begin_mask; - auto _end_mask = _o->end_mask; - auto _ellipsis_mask = _o->ellipsis_mask; - auto _new_axis_mask = _o->new_axis_mask; - auto _shrink_axis_mask = _o->shrink_axis_mask; - return tflite::CreateStridedSliceOptions( - _fbb, - _begin_mask, - _end_mask, - _ellipsis_mask, - _new_axis_mask, - _shrink_axis_mask); -} - -inline LogSoftmaxOptionsT *LogSoftmaxOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new LogSoftmaxOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void LogSoftmaxOptions::UnPackTo(LogSoftmaxOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset LogSoftmaxOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const LogSoftmaxOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateLogSoftmaxOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateLogSoftmaxOptions(flatbuffers::FlatBufferBuilder &_fbb, const LogSoftmaxOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const LogSoftmaxOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateLogSoftmaxOptions( - _fbb); -} - -inline CastOptionsT *CastOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new CastOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void CastOptions::UnPackTo(CastOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = in_data_type(); _o->in_data_type = _e; } - { auto _e = out_data_type(); _o->out_data_type = _e; } -} - -inline flatbuffers::Offset CastOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const CastOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateCastOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateCastOptions(flatbuffers::FlatBufferBuilder &_fbb, const CastOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const CastOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _in_data_type = _o->in_data_type; - auto _out_data_type = _o->out_data_type; - return tflite::CreateCastOptions( - _fbb, - _in_data_type, - _out_data_type); -} - -inline DequantizeOptionsT *DequantizeOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new DequantizeOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void DequantizeOptions::UnPackTo(DequantizeOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset DequantizeOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const DequantizeOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateDequantizeOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateDequantizeOptions(flatbuffers::FlatBufferBuilder &_fbb, const DequantizeOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const DequantizeOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateDequantizeOptions( - _fbb); -} - -inline MaximumMinimumOptionsT *MaximumMinimumOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new MaximumMinimumOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void MaximumMinimumOptions::UnPackTo(MaximumMinimumOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset MaximumMinimumOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const MaximumMinimumOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateMaximumMinimumOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateMaximumMinimumOptions(flatbuffers::FlatBufferBuilder &_fbb, const MaximumMinimumOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const MaximumMinimumOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateMaximumMinimumOptions( - _fbb); -} - -inline TileOptionsT *TileOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new TileOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void TileOptions::UnPackTo(TileOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset TileOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const TileOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateTileOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateTileOptions(flatbuffers::FlatBufferBuilder &_fbb, const TileOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const TileOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateTileOptions( - _fbb); -} - -inline ArgMaxOptionsT *ArgMaxOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new ArgMaxOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void ArgMaxOptions::UnPackTo(ArgMaxOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = output_type(); _o->output_type = _e; } -} - -inline flatbuffers::Offset ArgMaxOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const ArgMaxOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateArgMaxOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateArgMaxOptions(flatbuffers::FlatBufferBuilder &_fbb, const ArgMaxOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const ArgMaxOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _output_type = _o->output_type; - return tflite::CreateArgMaxOptions( - _fbb, - _output_type); -} - -inline ArgMinOptionsT *ArgMinOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new ArgMinOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void ArgMinOptions::UnPackTo(ArgMinOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = output_type(); _o->output_type = _e; } -} - -inline flatbuffers::Offset ArgMinOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const ArgMinOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateArgMinOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateArgMinOptions(flatbuffers::FlatBufferBuilder &_fbb, const ArgMinOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const ArgMinOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _output_type = _o->output_type; - return tflite::CreateArgMinOptions( - _fbb, - _output_type); -} - -inline GreaterOptionsT *GreaterOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new GreaterOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void GreaterOptions::UnPackTo(GreaterOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset GreaterOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const GreaterOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateGreaterOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateGreaterOptions(flatbuffers::FlatBufferBuilder &_fbb, const GreaterOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const GreaterOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateGreaterOptions( - _fbb); -} - -inline GreaterEqualOptionsT *GreaterEqualOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new GreaterEqualOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void GreaterEqualOptions::UnPackTo(GreaterEqualOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset GreaterEqualOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const GreaterEqualOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateGreaterEqualOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateGreaterEqualOptions(flatbuffers::FlatBufferBuilder &_fbb, const GreaterEqualOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const GreaterEqualOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateGreaterEqualOptions( - _fbb); -} - -inline LessOptionsT *LessOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new LessOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void LessOptions::UnPackTo(LessOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset LessOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const LessOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateLessOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateLessOptions(flatbuffers::FlatBufferBuilder &_fbb, const LessOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const LessOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateLessOptions( - _fbb); -} - -inline LessEqualOptionsT *LessEqualOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new LessEqualOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void LessEqualOptions::UnPackTo(LessEqualOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset LessEqualOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const LessEqualOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateLessEqualOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateLessEqualOptions(flatbuffers::FlatBufferBuilder &_fbb, const LessEqualOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const LessEqualOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateLessEqualOptions( - _fbb); -} - -inline NegOptionsT *NegOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new NegOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void NegOptions::UnPackTo(NegOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset NegOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const NegOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateNegOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateNegOptions(flatbuffers::FlatBufferBuilder &_fbb, const NegOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const NegOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateNegOptions( - _fbb); -} - -inline SelectOptionsT *SelectOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new SelectOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void SelectOptions::UnPackTo(SelectOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset SelectOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const SelectOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateSelectOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateSelectOptions(flatbuffers::FlatBufferBuilder &_fbb, const SelectOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const SelectOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateSelectOptions( - _fbb); -} - -inline SliceOptionsT *SliceOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new SliceOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void SliceOptions::UnPackTo(SliceOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset SliceOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const SliceOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateSliceOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateSliceOptions(flatbuffers::FlatBufferBuilder &_fbb, const SliceOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const SliceOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateSliceOptions( - _fbb); -} - -inline TransposeConvOptionsT *TransposeConvOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new TransposeConvOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void TransposeConvOptions::UnPackTo(TransposeConvOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = padding(); _o->padding = _e; } - { auto _e = stride_w(); _o->stride_w = _e; } - { auto _e = stride_h(); _o->stride_h = _e; } -} - -inline flatbuffers::Offset TransposeConvOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const TransposeConvOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateTransposeConvOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateTransposeConvOptions(flatbuffers::FlatBufferBuilder &_fbb, const TransposeConvOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const TransposeConvOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _padding = _o->padding; - auto _stride_w = _o->stride_w; - auto _stride_h = _o->stride_h; - return tflite::CreateTransposeConvOptions( - _fbb, - _padding, - _stride_w, - _stride_h); -} - -inline ExpandDimsOptionsT *ExpandDimsOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new ExpandDimsOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void ExpandDimsOptions::UnPackTo(ExpandDimsOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset ExpandDimsOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const ExpandDimsOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateExpandDimsOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateExpandDimsOptions(flatbuffers::FlatBufferBuilder &_fbb, const ExpandDimsOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const ExpandDimsOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateExpandDimsOptions( - _fbb); -} - -inline SparseToDenseOptionsT *SparseToDenseOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new SparseToDenseOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void SparseToDenseOptions::UnPackTo(SparseToDenseOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = validate_indices(); _o->validate_indices = _e; } -} - -inline flatbuffers::Offset SparseToDenseOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const SparseToDenseOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateSparseToDenseOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateSparseToDenseOptions(flatbuffers::FlatBufferBuilder &_fbb, const SparseToDenseOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const SparseToDenseOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _validate_indices = _o->validate_indices; - return tflite::CreateSparseToDenseOptions( - _fbb, - _validate_indices); -} - -inline EqualOptionsT *EqualOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new EqualOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void EqualOptions::UnPackTo(EqualOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset EqualOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const EqualOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateEqualOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateEqualOptions(flatbuffers::FlatBufferBuilder &_fbb, const EqualOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const EqualOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateEqualOptions( - _fbb); -} - -inline NotEqualOptionsT *NotEqualOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new NotEqualOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void NotEqualOptions::UnPackTo(NotEqualOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset NotEqualOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const NotEqualOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateNotEqualOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateNotEqualOptions(flatbuffers::FlatBufferBuilder &_fbb, const NotEqualOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const NotEqualOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateNotEqualOptions( - _fbb); -} - -inline ShapeOptionsT *ShapeOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new ShapeOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void ShapeOptions::UnPackTo(ShapeOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = out_type(); _o->out_type = _e; } -} - -inline flatbuffers::Offset ShapeOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const ShapeOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateShapeOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateShapeOptions(flatbuffers::FlatBufferBuilder &_fbb, const ShapeOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const ShapeOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _out_type = _o->out_type; - return tflite::CreateShapeOptions( - _fbb, - _out_type); -} - -inline RankOptionsT *RankOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new RankOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void RankOptions::UnPackTo(RankOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset RankOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const RankOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateRankOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateRankOptions(flatbuffers::FlatBufferBuilder &_fbb, const RankOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const RankOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateRankOptions( - _fbb); -} - -inline PowOptionsT *PowOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new PowOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void PowOptions::UnPackTo(PowOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset PowOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const PowOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreatePowOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreatePowOptions(flatbuffers::FlatBufferBuilder &_fbb, const PowOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const PowOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreatePowOptions( - _fbb); -} - -inline FakeQuantOptionsT *FakeQuantOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new FakeQuantOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void FakeQuantOptions::UnPackTo(FakeQuantOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = min(); _o->min = _e; } - { auto _e = max(); _o->max = _e; } - { auto _e = num_bits(); _o->num_bits = _e; } - { auto _e = narrow_range(); _o->narrow_range = _e; } -} - -inline flatbuffers::Offset FakeQuantOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const FakeQuantOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateFakeQuantOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateFakeQuantOptions(flatbuffers::FlatBufferBuilder &_fbb, const FakeQuantOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const FakeQuantOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _min = _o->min; - auto _max = _o->max; - auto _num_bits = _o->num_bits; - auto _narrow_range = _o->narrow_range; - return tflite::CreateFakeQuantOptions( - _fbb, - _min, - _max, - _num_bits, - _narrow_range); -} - -inline PackOptionsT *PackOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new PackOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void PackOptions::UnPackTo(PackOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = values_count(); _o->values_count = _e; } - { auto _e = axis(); _o->axis = _e; } -} - -inline flatbuffers::Offset PackOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const PackOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreatePackOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreatePackOptions(flatbuffers::FlatBufferBuilder &_fbb, const PackOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const PackOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _values_count = _o->values_count; - auto _axis = _o->axis; - return tflite::CreatePackOptions( - _fbb, - _values_count, - _axis); -} - -inline LogicalOrOptionsT *LogicalOrOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new LogicalOrOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void LogicalOrOptions::UnPackTo(LogicalOrOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset LogicalOrOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const LogicalOrOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateLogicalOrOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateLogicalOrOptions(flatbuffers::FlatBufferBuilder &_fbb, const LogicalOrOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const LogicalOrOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateLogicalOrOptions( - _fbb); -} - -inline OneHotOptionsT *OneHotOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new OneHotOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void OneHotOptions::UnPackTo(OneHotOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = axis(); _o->axis = _e; } -} - -inline flatbuffers::Offset OneHotOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const OneHotOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateOneHotOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateOneHotOptions(flatbuffers::FlatBufferBuilder &_fbb, const OneHotOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const OneHotOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _axis = _o->axis; - return tflite::CreateOneHotOptions( - _fbb, - _axis); -} - -inline AbsOptionsT *AbsOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new AbsOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void AbsOptions::UnPackTo(AbsOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset AbsOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const AbsOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateAbsOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateAbsOptions(flatbuffers::FlatBufferBuilder &_fbb, const AbsOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const AbsOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateAbsOptions( - _fbb); -} - -inline HardSwishOptionsT *HardSwishOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new HardSwishOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void HardSwishOptions::UnPackTo(HardSwishOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset HardSwishOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const HardSwishOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateHardSwishOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateHardSwishOptions(flatbuffers::FlatBufferBuilder &_fbb, const HardSwishOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const HardSwishOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateHardSwishOptions( - _fbb); -} - -inline LogicalAndOptionsT *LogicalAndOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new LogicalAndOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void LogicalAndOptions::UnPackTo(LogicalAndOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset LogicalAndOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const LogicalAndOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateLogicalAndOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateLogicalAndOptions(flatbuffers::FlatBufferBuilder &_fbb, const LogicalAndOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const LogicalAndOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateLogicalAndOptions( - _fbb); -} - -inline LogicalNotOptionsT *LogicalNotOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new LogicalNotOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void LogicalNotOptions::UnPackTo(LogicalNotOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset LogicalNotOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const LogicalNotOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateLogicalNotOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateLogicalNotOptions(flatbuffers::FlatBufferBuilder &_fbb, const LogicalNotOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const LogicalNotOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateLogicalNotOptions( - _fbb); -} - -inline UnpackOptionsT *UnpackOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new UnpackOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void UnpackOptions::UnPackTo(UnpackOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = num(); _o->num = _e; } - { auto _e = axis(); _o->axis = _e; } -} - -inline flatbuffers::Offset UnpackOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const UnpackOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateUnpackOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateUnpackOptions(flatbuffers::FlatBufferBuilder &_fbb, const UnpackOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const UnpackOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _num = _o->num; - auto _axis = _o->axis; - return tflite::CreateUnpackOptions( - _fbb, - _num, - _axis); -} - -inline FloorDivOptionsT *FloorDivOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new FloorDivOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void FloorDivOptions::UnPackTo(FloorDivOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset FloorDivOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const FloorDivOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateFloorDivOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateFloorDivOptions(flatbuffers::FlatBufferBuilder &_fbb, const FloorDivOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const FloorDivOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateFloorDivOptions( - _fbb); -} - -inline SquareOptionsT *SquareOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new SquareOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void SquareOptions::UnPackTo(SquareOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset SquareOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const SquareOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateSquareOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateSquareOptions(flatbuffers::FlatBufferBuilder &_fbb, const SquareOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const SquareOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateSquareOptions( - _fbb); -} - -inline ZerosLikeOptionsT *ZerosLikeOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new ZerosLikeOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void ZerosLikeOptions::UnPackTo(ZerosLikeOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset ZerosLikeOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const ZerosLikeOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateZerosLikeOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateZerosLikeOptions(flatbuffers::FlatBufferBuilder &_fbb, const ZerosLikeOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const ZerosLikeOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateZerosLikeOptions( - _fbb); -} - -inline FillOptionsT *FillOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new FillOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void FillOptions::UnPackTo(FillOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset FillOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const FillOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateFillOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateFillOptions(flatbuffers::FlatBufferBuilder &_fbb, const FillOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const FillOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateFillOptions( - _fbb); -} - -inline FloorModOptionsT *FloorModOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new FloorModOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void FloorModOptions::UnPackTo(FloorModOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset FloorModOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const FloorModOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateFloorModOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateFloorModOptions(flatbuffers::FlatBufferBuilder &_fbb, const FloorModOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const FloorModOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateFloorModOptions( - _fbb); -} - -inline RangeOptionsT *RangeOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new RangeOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void RangeOptions::UnPackTo(RangeOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset RangeOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const RangeOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateRangeOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateRangeOptions(flatbuffers::FlatBufferBuilder &_fbb, const RangeOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const RangeOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateRangeOptions( - _fbb); -} - -inline LeakyReluOptionsT *LeakyReluOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new LeakyReluOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void LeakyReluOptions::UnPackTo(LeakyReluOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = alpha(); _o->alpha = _e; } -} - -inline flatbuffers::Offset LeakyReluOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const LeakyReluOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateLeakyReluOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateLeakyReluOptions(flatbuffers::FlatBufferBuilder &_fbb, const LeakyReluOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const LeakyReluOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _alpha = _o->alpha; - return tflite::CreateLeakyReluOptions( - _fbb, - _alpha); -} - -inline SquaredDifferenceOptionsT *SquaredDifferenceOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new SquaredDifferenceOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void SquaredDifferenceOptions::UnPackTo(SquaredDifferenceOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset SquaredDifferenceOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const SquaredDifferenceOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateSquaredDifferenceOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateSquaredDifferenceOptions(flatbuffers::FlatBufferBuilder &_fbb, const SquaredDifferenceOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const SquaredDifferenceOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateSquaredDifferenceOptions( - _fbb); -} - -inline MirrorPadOptionsT *MirrorPadOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new MirrorPadOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void MirrorPadOptions::UnPackTo(MirrorPadOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = mode(); _o->mode = _e; } -} - -inline flatbuffers::Offset MirrorPadOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const MirrorPadOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateMirrorPadOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateMirrorPadOptions(flatbuffers::FlatBufferBuilder &_fbb, const MirrorPadOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const MirrorPadOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _mode = _o->mode; - return tflite::CreateMirrorPadOptions( - _fbb, - _mode); -} - -inline UniqueOptionsT *UniqueOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new UniqueOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void UniqueOptions::UnPackTo(UniqueOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = idx_out_type(); _o->idx_out_type = _e; } -} - -inline flatbuffers::Offset UniqueOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const UniqueOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateUniqueOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateUniqueOptions(flatbuffers::FlatBufferBuilder &_fbb, const UniqueOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const UniqueOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _idx_out_type = _o->idx_out_type; - return tflite::CreateUniqueOptions( - _fbb, - _idx_out_type); -} - -inline ReverseV2OptionsT *ReverseV2Options::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new ReverseV2OptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void ReverseV2Options::UnPackTo(ReverseV2OptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset ReverseV2Options::Pack(flatbuffers::FlatBufferBuilder &_fbb, const ReverseV2OptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateReverseV2Options(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateReverseV2Options(flatbuffers::FlatBufferBuilder &_fbb, const ReverseV2OptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const ReverseV2OptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateReverseV2Options( - _fbb); -} - -inline AddNOptionsT *AddNOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new AddNOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void AddNOptions::UnPackTo(AddNOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset AddNOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const AddNOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateAddNOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateAddNOptions(flatbuffers::FlatBufferBuilder &_fbb, const AddNOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const AddNOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateAddNOptions( - _fbb); -} - -inline GatherNdOptionsT *GatherNdOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new GatherNdOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void GatherNdOptions::UnPackTo(GatherNdOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset GatherNdOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const GatherNdOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateGatherNdOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateGatherNdOptions(flatbuffers::FlatBufferBuilder &_fbb, const GatherNdOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const GatherNdOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateGatherNdOptions( - _fbb); -} - -inline WhereOptionsT *WhereOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new WhereOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void WhereOptions::UnPackTo(WhereOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset WhereOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const WhereOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateWhereOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateWhereOptions(flatbuffers::FlatBufferBuilder &_fbb, const WhereOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const WhereOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateWhereOptions( - _fbb); -} - -inline ReverseSequenceOptionsT *ReverseSequenceOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new ReverseSequenceOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void ReverseSequenceOptions::UnPackTo(ReverseSequenceOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = seq_dim(); _o->seq_dim = _e; } - { auto _e = batch_dim(); _o->batch_dim = _e; } -} - -inline flatbuffers::Offset ReverseSequenceOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const ReverseSequenceOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateReverseSequenceOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateReverseSequenceOptions(flatbuffers::FlatBufferBuilder &_fbb, const ReverseSequenceOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const ReverseSequenceOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _seq_dim = _o->seq_dim; - auto _batch_dim = _o->batch_dim; - return tflite::CreateReverseSequenceOptions( - _fbb, - _seq_dim, - _batch_dim); -} - -inline MatrixDiagOptionsT *MatrixDiagOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new MatrixDiagOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void MatrixDiagOptions::UnPackTo(MatrixDiagOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset MatrixDiagOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const MatrixDiagOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateMatrixDiagOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateMatrixDiagOptions(flatbuffers::FlatBufferBuilder &_fbb, const MatrixDiagOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const MatrixDiagOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateMatrixDiagOptions( - _fbb); -} - -inline QuantizeOptionsT *QuantizeOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new QuantizeOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void QuantizeOptions::UnPackTo(QuantizeOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset QuantizeOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const QuantizeOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateQuantizeOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateQuantizeOptions(flatbuffers::FlatBufferBuilder &_fbb, const QuantizeOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const QuantizeOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateQuantizeOptions( - _fbb); -} - -inline MatrixSetDiagOptionsT *MatrixSetDiagOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new MatrixSetDiagOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void MatrixSetDiagOptions::UnPackTo(MatrixSetDiagOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset MatrixSetDiagOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const MatrixSetDiagOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateMatrixSetDiagOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateMatrixSetDiagOptions(flatbuffers::FlatBufferBuilder &_fbb, const MatrixSetDiagOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const MatrixSetDiagOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateMatrixSetDiagOptions( - _fbb); -} - -inline IfOptionsT *IfOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new IfOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void IfOptions::UnPackTo(IfOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = then_subgraph_index(); _o->then_subgraph_index = _e; } - { auto _e = else_subgraph_index(); _o->else_subgraph_index = _e; } -} - -inline flatbuffers::Offset IfOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const IfOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateIfOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateIfOptions(flatbuffers::FlatBufferBuilder &_fbb, const IfOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const IfOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _then_subgraph_index = _o->then_subgraph_index; - auto _else_subgraph_index = _o->else_subgraph_index; - return tflite::CreateIfOptions( - _fbb, - _then_subgraph_index, - _else_subgraph_index); -} - -inline CallOnceOptionsT *CallOnceOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new CallOnceOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void CallOnceOptions::UnPackTo(CallOnceOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = init_subgraph_index(); _o->init_subgraph_index = _e; } -} - -inline flatbuffers::Offset CallOnceOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const CallOnceOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateCallOnceOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateCallOnceOptions(flatbuffers::FlatBufferBuilder &_fbb, const CallOnceOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const CallOnceOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _init_subgraph_index = _o->init_subgraph_index; - return tflite::CreateCallOnceOptions( - _fbb, - _init_subgraph_index); -} - -inline WhileOptionsT *WhileOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new WhileOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void WhileOptions::UnPackTo(WhileOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = cond_subgraph_index(); _o->cond_subgraph_index = _e; } - { auto _e = body_subgraph_index(); _o->body_subgraph_index = _e; } -} - -inline flatbuffers::Offset WhileOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const WhileOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateWhileOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateWhileOptions(flatbuffers::FlatBufferBuilder &_fbb, const WhileOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const WhileOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _cond_subgraph_index = _o->cond_subgraph_index; - auto _body_subgraph_index = _o->body_subgraph_index; - return tflite::CreateWhileOptions( - _fbb, - _cond_subgraph_index, - _body_subgraph_index); -} - -inline NonMaxSuppressionV4OptionsT *NonMaxSuppressionV4Options::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new NonMaxSuppressionV4OptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void NonMaxSuppressionV4Options::UnPackTo(NonMaxSuppressionV4OptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset NonMaxSuppressionV4Options::Pack(flatbuffers::FlatBufferBuilder &_fbb, const NonMaxSuppressionV4OptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateNonMaxSuppressionV4Options(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateNonMaxSuppressionV4Options(flatbuffers::FlatBufferBuilder &_fbb, const NonMaxSuppressionV4OptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const NonMaxSuppressionV4OptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateNonMaxSuppressionV4Options( - _fbb); -} - -inline NonMaxSuppressionV5OptionsT *NonMaxSuppressionV5Options::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new NonMaxSuppressionV5OptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void NonMaxSuppressionV5Options::UnPackTo(NonMaxSuppressionV5OptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset NonMaxSuppressionV5Options::Pack(flatbuffers::FlatBufferBuilder &_fbb, const NonMaxSuppressionV5OptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateNonMaxSuppressionV5Options(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateNonMaxSuppressionV5Options(flatbuffers::FlatBufferBuilder &_fbb, const NonMaxSuppressionV5OptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const NonMaxSuppressionV5OptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateNonMaxSuppressionV5Options( - _fbb); -} - -inline ScatterNdOptionsT *ScatterNdOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new ScatterNdOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void ScatterNdOptions::UnPackTo(ScatterNdOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset ScatterNdOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const ScatterNdOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateScatterNdOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateScatterNdOptions(flatbuffers::FlatBufferBuilder &_fbb, const ScatterNdOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const ScatterNdOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateScatterNdOptions( - _fbb); -} - -inline SelectV2OptionsT *SelectV2Options::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new SelectV2OptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void SelectV2Options::UnPackTo(SelectV2OptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset SelectV2Options::Pack(flatbuffers::FlatBufferBuilder &_fbb, const SelectV2OptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateSelectV2Options(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateSelectV2Options(flatbuffers::FlatBufferBuilder &_fbb, const SelectV2OptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const SelectV2OptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateSelectV2Options( - _fbb); -} - -inline DensifyOptionsT *DensifyOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new DensifyOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void DensifyOptions::UnPackTo(DensifyOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset DensifyOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const DensifyOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateDensifyOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateDensifyOptions(flatbuffers::FlatBufferBuilder &_fbb, const DensifyOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const DensifyOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateDensifyOptions( - _fbb); -} - -inline SegmentSumOptionsT *SegmentSumOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new SegmentSumOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void SegmentSumOptions::UnPackTo(SegmentSumOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset SegmentSumOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const SegmentSumOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateSegmentSumOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateSegmentSumOptions(flatbuffers::FlatBufferBuilder &_fbb, const SegmentSumOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const SegmentSumOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateSegmentSumOptions( - _fbb); -} - -inline BatchMatMulOptionsT *BatchMatMulOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new BatchMatMulOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void BatchMatMulOptions::UnPackTo(BatchMatMulOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = adj_x(); _o->adj_x = _e; } - { auto _e = adj_y(); _o->adj_y = _e; } - { auto _e = asymmetric_quantize_inputs(); _o->asymmetric_quantize_inputs = _e; } -} - -inline flatbuffers::Offset BatchMatMulOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const BatchMatMulOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateBatchMatMulOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateBatchMatMulOptions(flatbuffers::FlatBufferBuilder &_fbb, const BatchMatMulOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const BatchMatMulOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _adj_x = _o->adj_x; - auto _adj_y = _o->adj_y; - auto _asymmetric_quantize_inputs = _o->asymmetric_quantize_inputs; - return tflite::CreateBatchMatMulOptions( - _fbb, - _adj_x, - _adj_y, - _asymmetric_quantize_inputs); -} - -inline CumsumOptionsT *CumsumOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new CumsumOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void CumsumOptions::UnPackTo(CumsumOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = exclusive(); _o->exclusive = _e; } - { auto _e = reverse(); _o->reverse = _e; } -} - -inline flatbuffers::Offset CumsumOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const CumsumOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateCumsumOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateCumsumOptions(flatbuffers::FlatBufferBuilder &_fbb, const CumsumOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const CumsumOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _exclusive = _o->exclusive; - auto _reverse = _o->reverse; - return tflite::CreateCumsumOptions( - _fbb, - _exclusive, - _reverse); -} - -inline BroadcastToOptionsT *BroadcastToOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new BroadcastToOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void BroadcastToOptions::UnPackTo(BroadcastToOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset BroadcastToOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const BroadcastToOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateBroadcastToOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateBroadcastToOptions(flatbuffers::FlatBufferBuilder &_fbb, const BroadcastToOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const BroadcastToOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateBroadcastToOptions( - _fbb); -} - -inline Rfft2dOptionsT *Rfft2dOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new Rfft2dOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void Rfft2dOptions::UnPackTo(Rfft2dOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset Rfft2dOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const Rfft2dOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateRfft2dOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateRfft2dOptions(flatbuffers::FlatBufferBuilder &_fbb, const Rfft2dOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const Rfft2dOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateRfft2dOptions( - _fbb); -} - -inline HashtableOptionsT *HashtableOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new HashtableOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void HashtableOptions::UnPackTo(HashtableOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = table_id(); _o->table_id = _e; } - { auto _e = key_dtype(); _o->key_dtype = _e; } - { auto _e = value_dtype(); _o->value_dtype = _e; } -} - -inline flatbuffers::Offset HashtableOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const HashtableOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateHashtableOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateHashtableOptions(flatbuffers::FlatBufferBuilder &_fbb, const HashtableOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const HashtableOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _table_id = _o->table_id; - auto _key_dtype = _o->key_dtype; - auto _value_dtype = _o->value_dtype; - return tflite::CreateHashtableOptions( - _fbb, - _table_id, - _key_dtype, - _value_dtype); -} - -inline HashtableFindOptionsT *HashtableFindOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new HashtableFindOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void HashtableFindOptions::UnPackTo(HashtableFindOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset HashtableFindOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const HashtableFindOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateHashtableFindOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateHashtableFindOptions(flatbuffers::FlatBufferBuilder &_fbb, const HashtableFindOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const HashtableFindOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateHashtableFindOptions( - _fbb); -} - -inline HashtableImportOptionsT *HashtableImportOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new HashtableImportOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void HashtableImportOptions::UnPackTo(HashtableImportOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset HashtableImportOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const HashtableImportOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateHashtableImportOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateHashtableImportOptions(flatbuffers::FlatBufferBuilder &_fbb, const HashtableImportOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const HashtableImportOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateHashtableImportOptions( - _fbb); -} - -inline HashtableSizeOptionsT *HashtableSizeOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new HashtableSizeOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void HashtableSizeOptions::UnPackTo(HashtableSizeOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset HashtableSizeOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const HashtableSizeOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateHashtableSizeOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateHashtableSizeOptions(flatbuffers::FlatBufferBuilder &_fbb, const HashtableSizeOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const HashtableSizeOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateHashtableSizeOptions( - _fbb); -} - -inline VarHandleOptionsT *VarHandleOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new VarHandleOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void VarHandleOptions::UnPackTo(VarHandleOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = container(); if (_e) _o->container = _e->str(); } - { auto _e = shared_name(); if (_e) _o->shared_name = _e->str(); } -} - -inline flatbuffers::Offset VarHandleOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const VarHandleOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateVarHandleOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateVarHandleOptions(flatbuffers::FlatBufferBuilder &_fbb, const VarHandleOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const VarHandleOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _container = _o->container.empty() ? 0 : _fbb.CreateString(_o->container); - auto _shared_name = _o->shared_name.empty() ? 0 : _fbb.CreateString(_o->shared_name); - return tflite::CreateVarHandleOptions( - _fbb, - _container, - _shared_name); -} - -inline ReadVariableOptionsT *ReadVariableOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new ReadVariableOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void ReadVariableOptions::UnPackTo(ReadVariableOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset ReadVariableOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const ReadVariableOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateReadVariableOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateReadVariableOptions(flatbuffers::FlatBufferBuilder &_fbb, const ReadVariableOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const ReadVariableOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateReadVariableOptions( - _fbb); -} - -inline AssignVariableOptionsT *AssignVariableOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new AssignVariableOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void AssignVariableOptions::UnPackTo(AssignVariableOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset AssignVariableOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const AssignVariableOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateAssignVariableOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateAssignVariableOptions(flatbuffers::FlatBufferBuilder &_fbb, const AssignVariableOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const AssignVariableOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateAssignVariableOptions( - _fbb); -} - -inline RandomOptionsT *RandomOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new RandomOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void RandomOptions::UnPackTo(RandomOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = seed(); _o->seed = _e; } - { auto _e = seed2(); _o->seed2 = _e; } -} - -inline flatbuffers::Offset RandomOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const RandomOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateRandomOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateRandomOptions(flatbuffers::FlatBufferBuilder &_fbb, const RandomOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const RandomOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _seed = _o->seed; - auto _seed2 = _o->seed2; - return tflite::CreateRandomOptions( - _fbb, - _seed, - _seed2); -} - -inline BucketizeOptionsT *BucketizeOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new BucketizeOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void BucketizeOptions::UnPackTo(BucketizeOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = boundaries(); if (_e) { _o->boundaries.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->boundaries[_i] = _e->Get(_i); } } } -} - -inline flatbuffers::Offset BucketizeOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const BucketizeOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateBucketizeOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateBucketizeOptions(flatbuffers::FlatBufferBuilder &_fbb, const BucketizeOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const BucketizeOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _boundaries = _o->boundaries.size() ? _fbb.CreateVector(_o->boundaries) : 0; - return tflite::CreateBucketizeOptions( - _fbb, - _boundaries); -} - -inline GeluOptionsT *GeluOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new GeluOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void GeluOptions::UnPackTo(GeluOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = approximate(); _o->approximate = _e; } -} - -inline flatbuffers::Offset GeluOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const GeluOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateGeluOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateGeluOptions(flatbuffers::FlatBufferBuilder &_fbb, const GeluOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const GeluOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _approximate = _o->approximate; - return tflite::CreateGeluOptions( - _fbb, - _approximate); -} - -inline DynamicUpdateSliceOptionsT *DynamicUpdateSliceOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new DynamicUpdateSliceOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void DynamicUpdateSliceOptions::UnPackTo(DynamicUpdateSliceOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset DynamicUpdateSliceOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const DynamicUpdateSliceOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateDynamicUpdateSliceOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateDynamicUpdateSliceOptions(flatbuffers::FlatBufferBuilder &_fbb, const DynamicUpdateSliceOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const DynamicUpdateSliceOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateDynamicUpdateSliceOptions( - _fbb); -} - -inline UnsortedSegmentProdOptionsT *UnsortedSegmentProdOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new UnsortedSegmentProdOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void UnsortedSegmentProdOptions::UnPackTo(UnsortedSegmentProdOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset UnsortedSegmentProdOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentProdOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateUnsortedSegmentProdOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateUnsortedSegmentProdOptions(flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentProdOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const UnsortedSegmentProdOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateUnsortedSegmentProdOptions( - _fbb); -} - -inline UnsortedSegmentMaxOptionsT *UnsortedSegmentMaxOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new UnsortedSegmentMaxOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void UnsortedSegmentMaxOptions::UnPackTo(UnsortedSegmentMaxOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset UnsortedSegmentMaxOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentMaxOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateUnsortedSegmentMaxOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateUnsortedSegmentMaxOptions(flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentMaxOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const UnsortedSegmentMaxOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateUnsortedSegmentMaxOptions( - _fbb); -} - -inline UnsortedSegmentSumOptionsT *UnsortedSegmentSumOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new UnsortedSegmentSumOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void UnsortedSegmentSumOptions::UnPackTo(UnsortedSegmentSumOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset UnsortedSegmentSumOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentSumOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateUnsortedSegmentSumOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateUnsortedSegmentSumOptions(flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentSumOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const UnsortedSegmentSumOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateUnsortedSegmentSumOptions( - _fbb); -} - -inline ATan2OptionsT *ATan2Options::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new ATan2OptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void ATan2Options::UnPackTo(ATan2OptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset ATan2Options::Pack(flatbuffers::FlatBufferBuilder &_fbb, const ATan2OptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateATan2Options(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateATan2Options(flatbuffers::FlatBufferBuilder &_fbb, const ATan2OptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const ATan2OptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateATan2Options( - _fbb); -} - -inline UnsortedSegmentMinOptionsT *UnsortedSegmentMinOptions::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new UnsortedSegmentMinOptionsT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void UnsortedSegmentMinOptions::UnPackTo(UnsortedSegmentMinOptionsT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; -} - -inline flatbuffers::Offset UnsortedSegmentMinOptions::Pack(flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentMinOptionsT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateUnsortedSegmentMinOptions(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateUnsortedSegmentMinOptions(flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentMinOptionsT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const UnsortedSegmentMinOptionsT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - return tflite::CreateUnsortedSegmentMinOptions( - _fbb); -} - -inline OperatorCodeT *OperatorCode::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new OperatorCodeT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void OperatorCode::UnPackTo(OperatorCodeT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = deprecated_builtin_code(); _o->deprecated_builtin_code = _e; } - { auto _e = custom_code(); if (_e) _o->custom_code = _e->str(); } - { auto _e = version(); _o->version = _e; } - { auto _e = builtin_code(); _o->builtin_code = _e; } -} - -inline flatbuffers::Offset OperatorCode::Pack(flatbuffers::FlatBufferBuilder &_fbb, const OperatorCodeT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateOperatorCode(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateOperatorCode(flatbuffers::FlatBufferBuilder &_fbb, const OperatorCodeT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const OperatorCodeT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _deprecated_builtin_code = _o->deprecated_builtin_code; - auto _custom_code = _o->custom_code.empty() ? 0 : _fbb.CreateString(_o->custom_code); - auto _version = _o->version; - auto _builtin_code = _o->builtin_code; - return tflite::CreateOperatorCode( - _fbb, - _deprecated_builtin_code, - _custom_code, - _version, - _builtin_code); -} - -inline OperatorT *Operator::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new OperatorT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void Operator::UnPackTo(OperatorT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = opcode_index(); _o->opcode_index = _e; } - { auto _e = inputs(); if (_e) { _o->inputs.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->inputs[_i] = _e->Get(_i); } } } - { auto _e = outputs(); if (_e) { _o->outputs.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->outputs[_i] = _e->Get(_i); } } } - { auto _e = builtin_options_type(); _o->builtin_options.type = _e; } - { auto _e = builtin_options(); if (_e) _o->builtin_options.value = tflite::BuiltinOptionsUnion::UnPack(_e, builtin_options_type(), _resolver); } - { auto _e = custom_options(); if (_e) { _o->custom_options.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->custom_options[_i] = _e->Get(_i); } } } - { auto _e = custom_options_format(); _o->custom_options_format = _e; } - { auto _e = mutating_variable_inputs(); if (_e) { _o->mutating_variable_inputs.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->mutating_variable_inputs[_i] = _e->Get(_i) != 0; } } } - { auto _e = intermediates(); if (_e) { _o->intermediates.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->intermediates[_i] = _e->Get(_i); } } } -} - -inline flatbuffers::Offset Operator::Pack(flatbuffers::FlatBufferBuilder &_fbb, const OperatorT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateOperator(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateOperator(flatbuffers::FlatBufferBuilder &_fbb, const OperatorT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const OperatorT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _opcode_index = _o->opcode_index; - auto _inputs = _o->inputs.size() ? _fbb.CreateVector(_o->inputs) : 0; - auto _outputs = _o->outputs.size() ? _fbb.CreateVector(_o->outputs) : 0; - auto _builtin_options_type = _o->builtin_options.type; - auto _builtin_options = _o->builtin_options.Pack(_fbb); - auto _custom_options = _o->custom_options.size() ? _fbb.CreateVector(_o->custom_options) : 0; - auto _custom_options_format = _o->custom_options_format; - auto _mutating_variable_inputs = _o->mutating_variable_inputs.size() ? _fbb.CreateVector(_o->mutating_variable_inputs) : 0; - auto _intermediates = _o->intermediates.size() ? _fbb.CreateVector(_o->intermediates) : 0; - return tflite::CreateOperator( - _fbb, - _opcode_index, - _inputs, - _outputs, - _builtin_options_type, - _builtin_options, - _custom_options, - _custom_options_format, - _mutating_variable_inputs, - _intermediates); -} - -inline SubGraphT *SubGraph::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new SubGraphT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void SubGraph::UnPackTo(SubGraphT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = tensors(); if (_e) { _o->tensors.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->tensors[_i] = std::unique_ptr(_e->Get(_i)->UnPack(_resolver)); } } } - { auto _e = inputs(); if (_e) { _o->inputs.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->inputs[_i] = _e->Get(_i); } } } - { auto _e = outputs(); if (_e) { _o->outputs.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->outputs[_i] = _e->Get(_i); } } } - { auto _e = operators(); if (_e) { _o->operators.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->operators[_i] = std::unique_ptr(_e->Get(_i)->UnPack(_resolver)); } } } - { auto _e = name(); if (_e) _o->name = _e->str(); } -} - -inline flatbuffers::Offset SubGraph::Pack(flatbuffers::FlatBufferBuilder &_fbb, const SubGraphT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateSubGraph(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateSubGraph(flatbuffers::FlatBufferBuilder &_fbb, const SubGraphT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const SubGraphT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _tensors = _o->tensors.size() ? _fbb.CreateVector> (_o->tensors.size(), [](size_t i, _VectorArgs *__va) { return CreateTensor(*__va->__fbb, __va->__o->tensors[i].get(), __va->__rehasher); }, &_va ) : 0; - auto _inputs = _o->inputs.size() ? _fbb.CreateVector(_o->inputs) : 0; - auto _outputs = _o->outputs.size() ? _fbb.CreateVector(_o->outputs) : 0; - auto _operators = _o->operators.size() ? _fbb.CreateVector> (_o->operators.size(), [](size_t i, _VectorArgs *__va) { return CreateOperator(*__va->__fbb, __va->__o->operators[i].get(), __va->__rehasher); }, &_va ) : 0; - auto _name = _o->name.empty() ? 0 : _fbb.CreateString(_o->name); - return tflite::CreateSubGraph( - _fbb, - _tensors, - _inputs, - _outputs, - _operators, - _name); -} - -inline BufferT *Buffer::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new BufferT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void Buffer::UnPackTo(BufferT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = data(); if (_e) { _o->data.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->data[_i] = _e->Get(_i); } } } -} - -inline flatbuffers::Offset Buffer::Pack(flatbuffers::FlatBufferBuilder &_fbb, const BufferT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateBuffer(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateBuffer(flatbuffers::FlatBufferBuilder &_fbb, const BufferT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const BufferT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - _fbb.ForceVectorAlignment(_o->data.size(), sizeof(uint8_t), 16); - auto _data = _o->data.size() ? _fbb.CreateVector(_o->data) : 0; - return tflite::CreateBuffer( - _fbb, - _data); -} - -inline MetadataT *Metadata::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new MetadataT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void Metadata::UnPackTo(MetadataT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = name(); if (_e) _o->name = _e->str(); } - { auto _e = buffer(); _o->buffer = _e; } -} - -inline flatbuffers::Offset Metadata::Pack(flatbuffers::FlatBufferBuilder &_fbb, const MetadataT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateMetadata(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateMetadata(flatbuffers::FlatBufferBuilder &_fbb, const MetadataT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const MetadataT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _name = _o->name.empty() ? 0 : _fbb.CreateString(_o->name); - auto _buffer = _o->buffer; - return tflite::CreateMetadata( - _fbb, - _name, - _buffer); -} - -inline TensorMapT *TensorMap::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new TensorMapT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void TensorMap::UnPackTo(TensorMapT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = name(); if (_e) _o->name = _e->str(); } - { auto _e = tensor_index(); _o->tensor_index = _e; } -} - -inline flatbuffers::Offset TensorMap::Pack(flatbuffers::FlatBufferBuilder &_fbb, const TensorMapT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateTensorMap(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateTensorMap(flatbuffers::FlatBufferBuilder &_fbb, const TensorMapT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const TensorMapT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _name = _o->name.empty() ? 0 : _fbb.CreateString(_o->name); - auto _tensor_index = _o->tensor_index; - return tflite::CreateTensorMap( - _fbb, - _name, - _tensor_index); -} - -inline SignatureDefT *SignatureDef::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new SignatureDefT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void SignatureDef::UnPackTo(SignatureDefT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = inputs(); if (_e) { _o->inputs.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->inputs[_i] = std::unique_ptr(_e->Get(_i)->UnPack(_resolver)); } } } - { auto _e = outputs(); if (_e) { _o->outputs.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->outputs[_i] = std::unique_ptr(_e->Get(_i)->UnPack(_resolver)); } } } - { auto _e = signature_key(); if (_e) _o->signature_key = _e->str(); } - { auto _e = subgraph_index(); _o->subgraph_index = _e; } -} - -inline flatbuffers::Offset SignatureDef::Pack(flatbuffers::FlatBufferBuilder &_fbb, const SignatureDefT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateSignatureDef(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateSignatureDef(flatbuffers::FlatBufferBuilder &_fbb, const SignatureDefT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const SignatureDefT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _inputs = _o->inputs.size() ? _fbb.CreateVector> (_o->inputs.size(), [](size_t i, _VectorArgs *__va) { return CreateTensorMap(*__va->__fbb, __va->__o->inputs[i].get(), __va->__rehasher); }, &_va ) : 0; - auto _outputs = _o->outputs.size() ? _fbb.CreateVector> (_o->outputs.size(), [](size_t i, _VectorArgs *__va) { return CreateTensorMap(*__va->__fbb, __va->__o->outputs[i].get(), __va->__rehasher); }, &_va ) : 0; - auto _signature_key = _o->signature_key.empty() ? 0 : _fbb.CreateString(_o->signature_key); - auto _subgraph_index = _o->subgraph_index; - return tflite::CreateSignatureDef( - _fbb, - _inputs, - _outputs, - _signature_key, - _subgraph_index); -} - -inline ModelT *Model::UnPack(const flatbuffers::resolver_function_t *_resolver) const { - std::unique_ptr _o = std::unique_ptr(new ModelT()); - UnPackTo(_o.get(), _resolver); - return _o.release(); -} - -inline void Model::UnPackTo(ModelT *_o, const flatbuffers::resolver_function_t *_resolver) const { - (void)_o; - (void)_resolver; - { auto _e = version(); _o->version = _e; } - { auto _e = operator_codes(); if (_e) { _o->operator_codes.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->operator_codes[_i] = std::unique_ptr(_e->Get(_i)->UnPack(_resolver)); } } } - { auto _e = subgraphs(); if (_e) { _o->subgraphs.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->subgraphs[_i] = std::unique_ptr(_e->Get(_i)->UnPack(_resolver)); } } } - { auto _e = description(); if (_e) _o->description = _e->str(); } - { auto _e = buffers(); if (_e) { _o->buffers.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->buffers[_i] = std::unique_ptr(_e->Get(_i)->UnPack(_resolver)); } } } - { auto _e = metadata_buffer(); if (_e) { _o->metadata_buffer.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->metadata_buffer[_i] = _e->Get(_i); } } } - { auto _e = metadata(); if (_e) { _o->metadata.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->metadata[_i] = std::unique_ptr(_e->Get(_i)->UnPack(_resolver)); } } } - { auto _e = signature_defs(); if (_e) { _o->signature_defs.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->signature_defs[_i] = std::unique_ptr(_e->Get(_i)->UnPack(_resolver)); } } } -} - -inline flatbuffers::Offset Model::Pack(flatbuffers::FlatBufferBuilder &_fbb, const ModelT* _o, const flatbuffers::rehasher_function_t *_rehasher) { - return CreateModel(_fbb, _o, _rehasher); -} - -inline flatbuffers::Offset CreateModel(flatbuffers::FlatBufferBuilder &_fbb, const ModelT *_o, const flatbuffers::rehasher_function_t *_rehasher) { - (void)_rehasher; - (void)_o; - struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const ModelT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; - auto _version = _o->version; - auto _operator_codes = _o->operator_codes.size() ? _fbb.CreateVector> (_o->operator_codes.size(), [](size_t i, _VectorArgs *__va) { return CreateOperatorCode(*__va->__fbb, __va->__o->operator_codes[i].get(), __va->__rehasher); }, &_va ) : 0; - auto _subgraphs = _o->subgraphs.size() ? _fbb.CreateVector> (_o->subgraphs.size(), [](size_t i, _VectorArgs *__va) { return CreateSubGraph(*__va->__fbb, __va->__o->subgraphs[i].get(), __va->__rehasher); }, &_va ) : 0; - auto _description = _o->description.empty() ? 0 : _fbb.CreateString(_o->description); - auto _buffers = _o->buffers.size() ? _fbb.CreateVector> (_o->buffers.size(), [](size_t i, _VectorArgs *__va) { return CreateBuffer(*__va->__fbb, __va->__o->buffers[i].get(), __va->__rehasher); }, &_va ) : 0; - auto _metadata_buffer = _o->metadata_buffer.size() ? _fbb.CreateVector(_o->metadata_buffer) : 0; - auto _metadata = _o->metadata.size() ? _fbb.CreateVector> (_o->metadata.size(), [](size_t i, _VectorArgs *__va) { return CreateMetadata(*__va->__fbb, __va->__o->metadata[i].get(), __va->__rehasher); }, &_va ) : 0; - auto _signature_defs = _o->signature_defs.size() ? _fbb.CreateVector> (_o->signature_defs.size(), [](size_t i, _VectorArgs *__va) { return CreateSignatureDef(*__va->__fbb, __va->__o->signature_defs[i].get(), __va->__rehasher); }, &_va ) : 0; - return tflite::CreateModel( - _fbb, - _version, - _operator_codes, - _subgraphs, - _description, - _buffers, - _metadata_buffer, - _metadata, - _signature_defs); -} - -inline bool VerifyQuantizationDetails(flatbuffers::Verifier &verifier, const void *obj, QuantizationDetails type) { - switch (type) { - case QuantizationDetails_NONE: { - return true; - } - case QuantizationDetails_CustomQuantization: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - default: return true; - } -} - -inline bool VerifyQuantizationDetailsVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector> *values, const flatbuffers::Vector *types) { - if (!values || !types) return !values && !types; - if (values->size() != types->size()) return false; - for (flatbuffers::uoffset_t i = 0; i < values->size(); ++i) { - if (!VerifyQuantizationDetails( - verifier, values->Get(i), types->GetEnum(i))) { - return false; - } - } - return true; -} - -inline void *QuantizationDetailsUnion::UnPack(const void *obj, QuantizationDetails type, const flatbuffers::resolver_function_t *resolver) { - switch (type) { - case QuantizationDetails_CustomQuantization: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - default: return nullptr; - } -} - -inline flatbuffers::Offset QuantizationDetailsUnion::Pack(flatbuffers::FlatBufferBuilder &_fbb, const flatbuffers::rehasher_function_t *_rehasher) const { - switch (type) { - case QuantizationDetails_CustomQuantization: { - auto ptr = reinterpret_cast(value); - return CreateCustomQuantization(_fbb, ptr, _rehasher).Union(); - } - default: return 0; - } -} - -inline QuantizationDetailsUnion::QuantizationDetailsUnion(const QuantizationDetailsUnion &u) : type(u.type), value(nullptr) { - switch (type) { - case QuantizationDetails_CustomQuantization: { - value = new tflite::CustomQuantizationT(*reinterpret_cast(u.value)); - break; - } - default: - break; - } -} - -inline void QuantizationDetailsUnion::Reset() { - switch (type) { - case QuantizationDetails_CustomQuantization: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - default: break; - } - value = nullptr; - type = QuantizationDetails_NONE; -} - -inline bool VerifySparseIndexVector(flatbuffers::Verifier &verifier, const void *obj, SparseIndexVector type) { - switch (type) { - case SparseIndexVector_NONE: { - return true; - } - case SparseIndexVector_Int32Vector: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case SparseIndexVector_Uint16Vector: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case SparseIndexVector_Uint8Vector: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - default: return true; - } -} - -inline bool VerifySparseIndexVectorVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector> *values, const flatbuffers::Vector *types) { - if (!values || !types) return !values && !types; - if (values->size() != types->size()) return false; - for (flatbuffers::uoffset_t i = 0; i < values->size(); ++i) { - if (!VerifySparseIndexVector( - verifier, values->Get(i), types->GetEnum(i))) { - return false; - } - } - return true; -} - -inline void *SparseIndexVectorUnion::UnPack(const void *obj, SparseIndexVector type, const flatbuffers::resolver_function_t *resolver) { - switch (type) { - case SparseIndexVector_Int32Vector: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case SparseIndexVector_Uint16Vector: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case SparseIndexVector_Uint8Vector: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - default: return nullptr; - } -} - -inline flatbuffers::Offset SparseIndexVectorUnion::Pack(flatbuffers::FlatBufferBuilder &_fbb, const flatbuffers::rehasher_function_t *_rehasher) const { - switch (type) { - case SparseIndexVector_Int32Vector: { - auto ptr = reinterpret_cast(value); - return CreateInt32Vector(_fbb, ptr, _rehasher).Union(); - } - case SparseIndexVector_Uint16Vector: { - auto ptr = reinterpret_cast(value); - return CreateUint16Vector(_fbb, ptr, _rehasher).Union(); - } - case SparseIndexVector_Uint8Vector: { - auto ptr = reinterpret_cast(value); - return CreateUint8Vector(_fbb, ptr, _rehasher).Union(); - } - default: return 0; - } -} - -inline SparseIndexVectorUnion::SparseIndexVectorUnion(const SparseIndexVectorUnion &u) : type(u.type), value(nullptr) { - switch (type) { - case SparseIndexVector_Int32Vector: { - value = new tflite::Int32VectorT(*reinterpret_cast(u.value)); - break; - } - case SparseIndexVector_Uint16Vector: { - value = new tflite::Uint16VectorT(*reinterpret_cast(u.value)); - break; - } - case SparseIndexVector_Uint8Vector: { - value = new tflite::Uint8VectorT(*reinterpret_cast(u.value)); - break; - } - default: - break; - } -} - -inline void SparseIndexVectorUnion::Reset() { - switch (type) { - case SparseIndexVector_Int32Vector: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case SparseIndexVector_Uint16Vector: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case SparseIndexVector_Uint8Vector: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - default: break; - } - value = nullptr; - type = SparseIndexVector_NONE; -} - -inline bool VerifyBuiltinOptions(flatbuffers::Verifier &verifier, const void *obj, BuiltinOptions type) { - switch (type) { - case BuiltinOptions_NONE: { - return true; - } - case BuiltinOptions_Conv2DOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_DepthwiseConv2DOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_ConcatEmbeddingsOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_LSHProjectionOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_Pool2DOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_SVDFOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_RNNOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_FullyConnectedOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_SoftmaxOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_ConcatenationOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_AddOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_L2NormOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_LocalResponseNormalizationOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_LSTMOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_ResizeBilinearOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_CallOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_ReshapeOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_SkipGramOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_SpaceToDepthOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_EmbeddingLookupSparseOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_MulOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_PadOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_GatherOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_BatchToSpaceNDOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_SpaceToBatchNDOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_TransposeOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_ReducerOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_SubOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_DivOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_SqueezeOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_SequenceRNNOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_StridedSliceOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_ExpOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_TopKV2Options: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_SplitOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_LogSoftmaxOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_CastOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_DequantizeOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_MaximumMinimumOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_ArgMaxOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_LessOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_NegOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_PadV2Options: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_GreaterOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_GreaterEqualOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_LessEqualOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_SelectOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_SliceOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_TransposeConvOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_SparseToDenseOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_TileOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_ExpandDimsOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_EqualOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_NotEqualOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_ShapeOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_PowOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_ArgMinOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_FakeQuantOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_PackOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_LogicalOrOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_OneHotOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_LogicalAndOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_LogicalNotOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_UnpackOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_FloorDivOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_SquareOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_ZerosLikeOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_FillOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_BidirectionalSequenceLSTMOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_BidirectionalSequenceRNNOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_UnidirectionalSequenceLSTMOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_FloorModOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_RangeOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_ResizeNearestNeighborOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_LeakyReluOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_SquaredDifferenceOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_MirrorPadOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_AbsOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_SplitVOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_UniqueOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_ReverseV2Options: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_AddNOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_GatherNdOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_CosOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_WhereOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_RankOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_ReverseSequenceOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_MatrixDiagOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_QuantizeOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_MatrixSetDiagOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_HardSwishOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_IfOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_WhileOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_DepthToSpaceOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_NonMaxSuppressionV4Options: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_NonMaxSuppressionV5Options: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_ScatterNdOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_SelectV2Options: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_DensifyOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_SegmentSumOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_BatchMatMulOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_CumsumOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_CallOnceOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_BroadcastToOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_Rfft2dOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_Conv3DOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_HashtableOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_HashtableFindOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_HashtableImportOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_HashtableSizeOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_VarHandleOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_ReadVariableOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_AssignVariableOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_RandomOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_BucketizeOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_GeluOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_DynamicUpdateSliceOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_UnsortedSegmentProdOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_UnsortedSegmentMaxOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_UnsortedSegmentMinOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_UnsortedSegmentSumOptions: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - case BuiltinOptions_ATan2Options: { - auto ptr = reinterpret_cast(obj); - return verifier.VerifyTable(ptr); - } - default: return true; - } -} - -inline bool VerifyBuiltinOptionsVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector> *values, const flatbuffers::Vector *types) { - if (!values || !types) return !values && !types; - if (values->size() != types->size()) return false; - for (flatbuffers::uoffset_t i = 0; i < values->size(); ++i) { - if (!VerifyBuiltinOptions( - verifier, values->Get(i), types->GetEnum(i))) { - return false; - } - } - return true; -} - -inline void *BuiltinOptionsUnion::UnPack(const void *obj, BuiltinOptions type, const flatbuffers::resolver_function_t *resolver) { - switch (type) { - case BuiltinOptions_Conv2DOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_DepthwiseConv2DOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_ConcatEmbeddingsOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_LSHProjectionOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_Pool2DOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_SVDFOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_RNNOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_FullyConnectedOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_SoftmaxOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_ConcatenationOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_AddOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_L2NormOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_LocalResponseNormalizationOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_LSTMOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_ResizeBilinearOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_CallOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_ReshapeOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_SkipGramOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_SpaceToDepthOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_EmbeddingLookupSparseOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_MulOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_PadOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_GatherOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_BatchToSpaceNDOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_SpaceToBatchNDOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_TransposeOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_ReducerOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_SubOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_DivOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_SqueezeOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_SequenceRNNOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_StridedSliceOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_ExpOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_TopKV2Options: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_SplitOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_LogSoftmaxOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_CastOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_DequantizeOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_MaximumMinimumOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_ArgMaxOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_LessOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_NegOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_PadV2Options: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_GreaterOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_GreaterEqualOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_LessEqualOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_SelectOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_SliceOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_TransposeConvOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_SparseToDenseOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_TileOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_ExpandDimsOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_EqualOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_NotEqualOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_ShapeOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_PowOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_ArgMinOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_FakeQuantOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_PackOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_LogicalOrOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_OneHotOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_LogicalAndOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_LogicalNotOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_UnpackOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_FloorDivOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_SquareOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_ZerosLikeOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_FillOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_BidirectionalSequenceLSTMOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_BidirectionalSequenceRNNOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_UnidirectionalSequenceLSTMOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_FloorModOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_RangeOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_ResizeNearestNeighborOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_LeakyReluOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_SquaredDifferenceOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_MirrorPadOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_AbsOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_SplitVOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_UniqueOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_ReverseV2Options: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_AddNOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_GatherNdOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_CosOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_WhereOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_RankOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_ReverseSequenceOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_MatrixDiagOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_QuantizeOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_MatrixSetDiagOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_HardSwishOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_IfOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_WhileOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_DepthToSpaceOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_NonMaxSuppressionV4Options: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_NonMaxSuppressionV5Options: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_ScatterNdOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_SelectV2Options: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_DensifyOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_SegmentSumOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_BatchMatMulOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_CumsumOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_CallOnceOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_BroadcastToOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_Rfft2dOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_Conv3DOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_HashtableOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_HashtableFindOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_HashtableImportOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_HashtableSizeOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_VarHandleOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_ReadVariableOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_AssignVariableOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_RandomOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_BucketizeOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_GeluOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_DynamicUpdateSliceOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_UnsortedSegmentProdOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_UnsortedSegmentMaxOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_UnsortedSegmentMinOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_UnsortedSegmentSumOptions: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - case BuiltinOptions_ATan2Options: { - auto ptr = reinterpret_cast(obj); - return ptr->UnPack(resolver); - } - default: return nullptr; - } -} - -inline flatbuffers::Offset BuiltinOptionsUnion::Pack(flatbuffers::FlatBufferBuilder &_fbb, const flatbuffers::rehasher_function_t *_rehasher) const { - switch (type) { - case BuiltinOptions_Conv2DOptions: { - auto ptr = reinterpret_cast(value); - return CreateConv2DOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_DepthwiseConv2DOptions: { - auto ptr = reinterpret_cast(value); - return CreateDepthwiseConv2DOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_ConcatEmbeddingsOptions: { - auto ptr = reinterpret_cast(value); - return CreateConcatEmbeddingsOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_LSHProjectionOptions: { - auto ptr = reinterpret_cast(value); - return CreateLSHProjectionOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_Pool2DOptions: { - auto ptr = reinterpret_cast(value); - return CreatePool2DOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_SVDFOptions: { - auto ptr = reinterpret_cast(value); - return CreateSVDFOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_RNNOptions: { - auto ptr = reinterpret_cast(value); - return CreateRNNOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_FullyConnectedOptions: { - auto ptr = reinterpret_cast(value); - return CreateFullyConnectedOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_SoftmaxOptions: { - auto ptr = reinterpret_cast(value); - return CreateSoftmaxOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_ConcatenationOptions: { - auto ptr = reinterpret_cast(value); - return CreateConcatenationOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_AddOptions: { - auto ptr = reinterpret_cast(value); - return CreateAddOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_L2NormOptions: { - auto ptr = reinterpret_cast(value); - return CreateL2NormOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_LocalResponseNormalizationOptions: { - auto ptr = reinterpret_cast(value); - return CreateLocalResponseNormalizationOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_LSTMOptions: { - auto ptr = reinterpret_cast(value); - return CreateLSTMOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_ResizeBilinearOptions: { - auto ptr = reinterpret_cast(value); - return CreateResizeBilinearOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_CallOptions: { - auto ptr = reinterpret_cast(value); - return CreateCallOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_ReshapeOptions: { - auto ptr = reinterpret_cast(value); - return CreateReshapeOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_SkipGramOptions: { - auto ptr = reinterpret_cast(value); - return CreateSkipGramOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_SpaceToDepthOptions: { - auto ptr = reinterpret_cast(value); - return CreateSpaceToDepthOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_EmbeddingLookupSparseOptions: { - auto ptr = reinterpret_cast(value); - return CreateEmbeddingLookupSparseOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_MulOptions: { - auto ptr = reinterpret_cast(value); - return CreateMulOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_PadOptions: { - auto ptr = reinterpret_cast(value); - return CreatePadOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_GatherOptions: { - auto ptr = reinterpret_cast(value); - return CreateGatherOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_BatchToSpaceNDOptions: { - auto ptr = reinterpret_cast(value); - return CreateBatchToSpaceNDOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_SpaceToBatchNDOptions: { - auto ptr = reinterpret_cast(value); - return CreateSpaceToBatchNDOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_TransposeOptions: { - auto ptr = reinterpret_cast(value); - return CreateTransposeOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_ReducerOptions: { - auto ptr = reinterpret_cast(value); - return CreateReducerOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_SubOptions: { - auto ptr = reinterpret_cast(value); - return CreateSubOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_DivOptions: { - auto ptr = reinterpret_cast(value); - return CreateDivOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_SqueezeOptions: { - auto ptr = reinterpret_cast(value); - return CreateSqueezeOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_SequenceRNNOptions: { - auto ptr = reinterpret_cast(value); - return CreateSequenceRNNOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_StridedSliceOptions: { - auto ptr = reinterpret_cast(value); - return CreateStridedSliceOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_ExpOptions: { - auto ptr = reinterpret_cast(value); - return CreateExpOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_TopKV2Options: { - auto ptr = reinterpret_cast(value); - return CreateTopKV2Options(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_SplitOptions: { - auto ptr = reinterpret_cast(value); - return CreateSplitOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_LogSoftmaxOptions: { - auto ptr = reinterpret_cast(value); - return CreateLogSoftmaxOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_CastOptions: { - auto ptr = reinterpret_cast(value); - return CreateCastOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_DequantizeOptions: { - auto ptr = reinterpret_cast(value); - return CreateDequantizeOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_MaximumMinimumOptions: { - auto ptr = reinterpret_cast(value); - return CreateMaximumMinimumOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_ArgMaxOptions: { - auto ptr = reinterpret_cast(value); - return CreateArgMaxOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_LessOptions: { - auto ptr = reinterpret_cast(value); - return CreateLessOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_NegOptions: { - auto ptr = reinterpret_cast(value); - return CreateNegOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_PadV2Options: { - auto ptr = reinterpret_cast(value); - return CreatePadV2Options(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_GreaterOptions: { - auto ptr = reinterpret_cast(value); - return CreateGreaterOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_GreaterEqualOptions: { - auto ptr = reinterpret_cast(value); - return CreateGreaterEqualOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_LessEqualOptions: { - auto ptr = reinterpret_cast(value); - return CreateLessEqualOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_SelectOptions: { - auto ptr = reinterpret_cast(value); - return CreateSelectOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_SliceOptions: { - auto ptr = reinterpret_cast(value); - return CreateSliceOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_TransposeConvOptions: { - auto ptr = reinterpret_cast(value); - return CreateTransposeConvOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_SparseToDenseOptions: { - auto ptr = reinterpret_cast(value); - return CreateSparseToDenseOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_TileOptions: { - auto ptr = reinterpret_cast(value); - return CreateTileOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_ExpandDimsOptions: { - auto ptr = reinterpret_cast(value); - return CreateExpandDimsOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_EqualOptions: { - auto ptr = reinterpret_cast(value); - return CreateEqualOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_NotEqualOptions: { - auto ptr = reinterpret_cast(value); - return CreateNotEqualOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_ShapeOptions: { - auto ptr = reinterpret_cast(value); - return CreateShapeOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_PowOptions: { - auto ptr = reinterpret_cast(value); - return CreatePowOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_ArgMinOptions: { - auto ptr = reinterpret_cast(value); - return CreateArgMinOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_FakeQuantOptions: { - auto ptr = reinterpret_cast(value); - return CreateFakeQuantOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_PackOptions: { - auto ptr = reinterpret_cast(value); - return CreatePackOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_LogicalOrOptions: { - auto ptr = reinterpret_cast(value); - return CreateLogicalOrOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_OneHotOptions: { - auto ptr = reinterpret_cast(value); - return CreateOneHotOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_LogicalAndOptions: { - auto ptr = reinterpret_cast(value); - return CreateLogicalAndOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_LogicalNotOptions: { - auto ptr = reinterpret_cast(value); - return CreateLogicalNotOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_UnpackOptions: { - auto ptr = reinterpret_cast(value); - return CreateUnpackOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_FloorDivOptions: { - auto ptr = reinterpret_cast(value); - return CreateFloorDivOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_SquareOptions: { - auto ptr = reinterpret_cast(value); - return CreateSquareOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_ZerosLikeOptions: { - auto ptr = reinterpret_cast(value); - return CreateZerosLikeOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_FillOptions: { - auto ptr = reinterpret_cast(value); - return CreateFillOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_BidirectionalSequenceLSTMOptions: { - auto ptr = reinterpret_cast(value); - return CreateBidirectionalSequenceLSTMOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_BidirectionalSequenceRNNOptions: { - auto ptr = reinterpret_cast(value); - return CreateBidirectionalSequenceRNNOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_UnidirectionalSequenceLSTMOptions: { - auto ptr = reinterpret_cast(value); - return CreateUnidirectionalSequenceLSTMOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_FloorModOptions: { - auto ptr = reinterpret_cast(value); - return CreateFloorModOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_RangeOptions: { - auto ptr = reinterpret_cast(value); - return CreateRangeOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_ResizeNearestNeighborOptions: { - auto ptr = reinterpret_cast(value); - return CreateResizeNearestNeighborOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_LeakyReluOptions: { - auto ptr = reinterpret_cast(value); - return CreateLeakyReluOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_SquaredDifferenceOptions: { - auto ptr = reinterpret_cast(value); - return CreateSquaredDifferenceOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_MirrorPadOptions: { - auto ptr = reinterpret_cast(value); - return CreateMirrorPadOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_AbsOptions: { - auto ptr = reinterpret_cast(value); - return CreateAbsOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_SplitVOptions: { - auto ptr = reinterpret_cast(value); - return CreateSplitVOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_UniqueOptions: { - auto ptr = reinterpret_cast(value); - return CreateUniqueOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_ReverseV2Options: { - auto ptr = reinterpret_cast(value); - return CreateReverseV2Options(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_AddNOptions: { - auto ptr = reinterpret_cast(value); - return CreateAddNOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_GatherNdOptions: { - auto ptr = reinterpret_cast(value); - return CreateGatherNdOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_CosOptions: { - auto ptr = reinterpret_cast(value); - return CreateCosOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_WhereOptions: { - auto ptr = reinterpret_cast(value); - return CreateWhereOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_RankOptions: { - auto ptr = reinterpret_cast(value); - return CreateRankOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_ReverseSequenceOptions: { - auto ptr = reinterpret_cast(value); - return CreateReverseSequenceOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_MatrixDiagOptions: { - auto ptr = reinterpret_cast(value); - return CreateMatrixDiagOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_QuantizeOptions: { - auto ptr = reinterpret_cast(value); - return CreateQuantizeOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_MatrixSetDiagOptions: { - auto ptr = reinterpret_cast(value); - return CreateMatrixSetDiagOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_HardSwishOptions: { - auto ptr = reinterpret_cast(value); - return CreateHardSwishOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_IfOptions: { - auto ptr = reinterpret_cast(value); - return CreateIfOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_WhileOptions: { - auto ptr = reinterpret_cast(value); - return CreateWhileOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_DepthToSpaceOptions: { - auto ptr = reinterpret_cast(value); - return CreateDepthToSpaceOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_NonMaxSuppressionV4Options: { - auto ptr = reinterpret_cast(value); - return CreateNonMaxSuppressionV4Options(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_NonMaxSuppressionV5Options: { - auto ptr = reinterpret_cast(value); - return CreateNonMaxSuppressionV5Options(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_ScatterNdOptions: { - auto ptr = reinterpret_cast(value); - return CreateScatterNdOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_SelectV2Options: { - auto ptr = reinterpret_cast(value); - return CreateSelectV2Options(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_DensifyOptions: { - auto ptr = reinterpret_cast(value); - return CreateDensifyOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_SegmentSumOptions: { - auto ptr = reinterpret_cast(value); - return CreateSegmentSumOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_BatchMatMulOptions: { - auto ptr = reinterpret_cast(value); - return CreateBatchMatMulOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_CumsumOptions: { - auto ptr = reinterpret_cast(value); - return CreateCumsumOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_CallOnceOptions: { - auto ptr = reinterpret_cast(value); - return CreateCallOnceOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_BroadcastToOptions: { - auto ptr = reinterpret_cast(value); - return CreateBroadcastToOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_Rfft2dOptions: { - auto ptr = reinterpret_cast(value); - return CreateRfft2dOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_Conv3DOptions: { - auto ptr = reinterpret_cast(value); - return CreateConv3DOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_HashtableOptions: { - auto ptr = reinterpret_cast(value); - return CreateHashtableOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_HashtableFindOptions: { - auto ptr = reinterpret_cast(value); - return CreateHashtableFindOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_HashtableImportOptions: { - auto ptr = reinterpret_cast(value); - return CreateHashtableImportOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_HashtableSizeOptions: { - auto ptr = reinterpret_cast(value); - return CreateHashtableSizeOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_VarHandleOptions: { - auto ptr = reinterpret_cast(value); - return CreateVarHandleOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_ReadVariableOptions: { - auto ptr = reinterpret_cast(value); - return CreateReadVariableOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_AssignVariableOptions: { - auto ptr = reinterpret_cast(value); - return CreateAssignVariableOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_RandomOptions: { - auto ptr = reinterpret_cast(value); - return CreateRandomOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_BucketizeOptions: { - auto ptr = reinterpret_cast(value); - return CreateBucketizeOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_GeluOptions: { - auto ptr = reinterpret_cast(value); - return CreateGeluOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_DynamicUpdateSliceOptions: { - auto ptr = reinterpret_cast(value); - return CreateDynamicUpdateSliceOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_UnsortedSegmentProdOptions: { - auto ptr = reinterpret_cast(value); - return CreateUnsortedSegmentProdOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_UnsortedSegmentMaxOptions: { - auto ptr = reinterpret_cast(value); - return CreateUnsortedSegmentMaxOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_UnsortedSegmentMinOptions: { - auto ptr = reinterpret_cast(value); - return CreateUnsortedSegmentMinOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_UnsortedSegmentSumOptions: { - auto ptr = reinterpret_cast(value); - return CreateUnsortedSegmentSumOptions(_fbb, ptr, _rehasher).Union(); - } - case BuiltinOptions_ATan2Options: { - auto ptr = reinterpret_cast(value); - return CreateATan2Options(_fbb, ptr, _rehasher).Union(); - } - default: return 0; - } -} - -inline BuiltinOptionsUnion::BuiltinOptionsUnion(const BuiltinOptionsUnion &u) : type(u.type), value(nullptr) { - switch (type) { - case BuiltinOptions_Conv2DOptions: { - value = new tflite::Conv2DOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_DepthwiseConv2DOptions: { - value = new tflite::DepthwiseConv2DOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_ConcatEmbeddingsOptions: { - value = new tflite::ConcatEmbeddingsOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_LSHProjectionOptions: { - value = new tflite::LSHProjectionOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_Pool2DOptions: { - value = new tflite::Pool2DOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_SVDFOptions: { - value = new tflite::SVDFOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_RNNOptions: { - value = new tflite::RNNOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_FullyConnectedOptions: { - value = new tflite::FullyConnectedOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_SoftmaxOptions: { - value = new tflite::SoftmaxOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_ConcatenationOptions: { - value = new tflite::ConcatenationOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_AddOptions: { - value = new tflite::AddOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_L2NormOptions: { - value = new tflite::L2NormOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_LocalResponseNormalizationOptions: { - value = new tflite::LocalResponseNormalizationOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_LSTMOptions: { - value = new tflite::LSTMOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_ResizeBilinearOptions: { - value = new tflite::ResizeBilinearOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_CallOptions: { - value = new tflite::CallOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_ReshapeOptions: { - value = new tflite::ReshapeOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_SkipGramOptions: { - value = new tflite::SkipGramOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_SpaceToDepthOptions: { - value = new tflite::SpaceToDepthOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_EmbeddingLookupSparseOptions: { - value = new tflite::EmbeddingLookupSparseOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_MulOptions: { - value = new tflite::MulOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_PadOptions: { - value = new tflite::PadOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_GatherOptions: { - value = new tflite::GatherOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_BatchToSpaceNDOptions: { - value = new tflite::BatchToSpaceNDOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_SpaceToBatchNDOptions: { - value = new tflite::SpaceToBatchNDOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_TransposeOptions: { - value = new tflite::TransposeOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_ReducerOptions: { - value = new tflite::ReducerOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_SubOptions: { - value = new tflite::SubOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_DivOptions: { - value = new tflite::DivOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_SqueezeOptions: { - value = new tflite::SqueezeOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_SequenceRNNOptions: { - value = new tflite::SequenceRNNOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_StridedSliceOptions: { - value = new tflite::StridedSliceOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_ExpOptions: { - value = new tflite::ExpOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_TopKV2Options: { - value = new tflite::TopKV2OptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_SplitOptions: { - value = new tflite::SplitOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_LogSoftmaxOptions: { - value = new tflite::LogSoftmaxOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_CastOptions: { - value = new tflite::CastOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_DequantizeOptions: { - value = new tflite::DequantizeOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_MaximumMinimumOptions: { - value = new tflite::MaximumMinimumOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_ArgMaxOptions: { - value = new tflite::ArgMaxOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_LessOptions: { - value = new tflite::LessOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_NegOptions: { - value = new tflite::NegOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_PadV2Options: { - value = new tflite::PadV2OptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_GreaterOptions: { - value = new tflite::GreaterOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_GreaterEqualOptions: { - value = new tflite::GreaterEqualOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_LessEqualOptions: { - value = new tflite::LessEqualOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_SelectOptions: { - value = new tflite::SelectOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_SliceOptions: { - value = new tflite::SliceOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_TransposeConvOptions: { - value = new tflite::TransposeConvOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_SparseToDenseOptions: { - value = new tflite::SparseToDenseOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_TileOptions: { - value = new tflite::TileOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_ExpandDimsOptions: { - value = new tflite::ExpandDimsOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_EqualOptions: { - value = new tflite::EqualOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_NotEqualOptions: { - value = new tflite::NotEqualOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_ShapeOptions: { - value = new tflite::ShapeOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_PowOptions: { - value = new tflite::PowOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_ArgMinOptions: { - value = new tflite::ArgMinOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_FakeQuantOptions: { - value = new tflite::FakeQuantOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_PackOptions: { - value = new tflite::PackOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_LogicalOrOptions: { - value = new tflite::LogicalOrOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_OneHotOptions: { - value = new tflite::OneHotOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_LogicalAndOptions: { - value = new tflite::LogicalAndOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_LogicalNotOptions: { - value = new tflite::LogicalNotOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_UnpackOptions: { - value = new tflite::UnpackOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_FloorDivOptions: { - value = new tflite::FloorDivOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_SquareOptions: { - value = new tflite::SquareOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_ZerosLikeOptions: { - value = new tflite::ZerosLikeOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_FillOptions: { - value = new tflite::FillOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_BidirectionalSequenceLSTMOptions: { - value = new tflite::BidirectionalSequenceLSTMOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_BidirectionalSequenceRNNOptions: { - value = new tflite::BidirectionalSequenceRNNOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_UnidirectionalSequenceLSTMOptions: { - value = new tflite::UnidirectionalSequenceLSTMOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_FloorModOptions: { - value = new tflite::FloorModOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_RangeOptions: { - value = new tflite::RangeOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_ResizeNearestNeighborOptions: { - value = new tflite::ResizeNearestNeighborOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_LeakyReluOptions: { - value = new tflite::LeakyReluOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_SquaredDifferenceOptions: { - value = new tflite::SquaredDifferenceOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_MirrorPadOptions: { - value = new tflite::MirrorPadOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_AbsOptions: { - value = new tflite::AbsOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_SplitVOptions: { - value = new tflite::SplitVOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_UniqueOptions: { - value = new tflite::UniqueOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_ReverseV2Options: { - value = new tflite::ReverseV2OptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_AddNOptions: { - value = new tflite::AddNOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_GatherNdOptions: { - value = new tflite::GatherNdOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_CosOptions: { - value = new tflite::CosOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_WhereOptions: { - value = new tflite::WhereOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_RankOptions: { - value = new tflite::RankOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_ReverseSequenceOptions: { - value = new tflite::ReverseSequenceOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_MatrixDiagOptions: { - value = new tflite::MatrixDiagOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_QuantizeOptions: { - value = new tflite::QuantizeOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_MatrixSetDiagOptions: { - value = new tflite::MatrixSetDiagOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_HardSwishOptions: { - value = new tflite::HardSwishOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_IfOptions: { - value = new tflite::IfOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_WhileOptions: { - value = new tflite::WhileOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_DepthToSpaceOptions: { - value = new tflite::DepthToSpaceOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_NonMaxSuppressionV4Options: { - value = new tflite::NonMaxSuppressionV4OptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_NonMaxSuppressionV5Options: { - value = new tflite::NonMaxSuppressionV5OptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_ScatterNdOptions: { - value = new tflite::ScatterNdOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_SelectV2Options: { - value = new tflite::SelectV2OptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_DensifyOptions: { - value = new tflite::DensifyOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_SegmentSumOptions: { - value = new tflite::SegmentSumOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_BatchMatMulOptions: { - value = new tflite::BatchMatMulOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_CumsumOptions: { - value = new tflite::CumsumOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_CallOnceOptions: { - value = new tflite::CallOnceOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_BroadcastToOptions: { - value = new tflite::BroadcastToOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_Rfft2dOptions: { - value = new tflite::Rfft2dOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_Conv3DOptions: { - value = new tflite::Conv3DOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_HashtableOptions: { - value = new tflite::HashtableOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_HashtableFindOptions: { - value = new tflite::HashtableFindOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_HashtableImportOptions: { - value = new tflite::HashtableImportOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_HashtableSizeOptions: { - value = new tflite::HashtableSizeOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_VarHandleOptions: { - value = new tflite::VarHandleOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_ReadVariableOptions: { - value = new tflite::ReadVariableOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_AssignVariableOptions: { - value = new tflite::AssignVariableOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_RandomOptions: { - value = new tflite::RandomOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_BucketizeOptions: { - value = new tflite::BucketizeOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_GeluOptions: { - value = new tflite::GeluOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_DynamicUpdateSliceOptions: { - value = new tflite::DynamicUpdateSliceOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_UnsortedSegmentProdOptions: { - value = new tflite::UnsortedSegmentProdOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_UnsortedSegmentMaxOptions: { - value = new tflite::UnsortedSegmentMaxOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_UnsortedSegmentMinOptions: { - value = new tflite::UnsortedSegmentMinOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_UnsortedSegmentSumOptions: { - value = new tflite::UnsortedSegmentSumOptionsT(*reinterpret_cast(u.value)); - break; - } - case BuiltinOptions_ATan2Options: { - value = new tflite::ATan2OptionsT(*reinterpret_cast(u.value)); - break; - } - default: - break; - } -} - -inline void BuiltinOptionsUnion::Reset() { - switch (type) { - case BuiltinOptions_Conv2DOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_DepthwiseConv2DOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_ConcatEmbeddingsOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_LSHProjectionOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_Pool2DOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_SVDFOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_RNNOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_FullyConnectedOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_SoftmaxOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_ConcatenationOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_AddOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_L2NormOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_LocalResponseNormalizationOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_LSTMOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_ResizeBilinearOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_CallOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_ReshapeOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_SkipGramOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_SpaceToDepthOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_EmbeddingLookupSparseOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_MulOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_PadOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_GatherOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_BatchToSpaceNDOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_SpaceToBatchNDOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_TransposeOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_ReducerOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_SubOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_DivOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_SqueezeOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_SequenceRNNOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_StridedSliceOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_ExpOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_TopKV2Options: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_SplitOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_LogSoftmaxOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_CastOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_DequantizeOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_MaximumMinimumOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_ArgMaxOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_LessOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_NegOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_PadV2Options: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_GreaterOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_GreaterEqualOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_LessEqualOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_SelectOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_SliceOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_TransposeConvOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_SparseToDenseOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_TileOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_ExpandDimsOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_EqualOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_NotEqualOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_ShapeOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_PowOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_ArgMinOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_FakeQuantOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_PackOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_LogicalOrOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_OneHotOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_LogicalAndOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_LogicalNotOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_UnpackOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_FloorDivOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_SquareOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_ZerosLikeOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_FillOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_BidirectionalSequenceLSTMOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_BidirectionalSequenceRNNOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_UnidirectionalSequenceLSTMOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_FloorModOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_RangeOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_ResizeNearestNeighborOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_LeakyReluOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_SquaredDifferenceOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_MirrorPadOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_AbsOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_SplitVOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_UniqueOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_ReverseV2Options: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_AddNOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_GatherNdOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_CosOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_WhereOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_RankOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_ReverseSequenceOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_MatrixDiagOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_QuantizeOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_MatrixSetDiagOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_HardSwishOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_IfOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_WhileOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_DepthToSpaceOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_NonMaxSuppressionV4Options: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_NonMaxSuppressionV5Options: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_ScatterNdOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_SelectV2Options: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_DensifyOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_SegmentSumOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_BatchMatMulOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_CumsumOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_CallOnceOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_BroadcastToOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_Rfft2dOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_Conv3DOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_HashtableOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_HashtableFindOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_HashtableImportOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_HashtableSizeOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_VarHandleOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_ReadVariableOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_AssignVariableOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_RandomOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_BucketizeOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_GeluOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_DynamicUpdateSliceOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_UnsortedSegmentProdOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_UnsortedSegmentMaxOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_UnsortedSegmentMinOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_UnsortedSegmentSumOptions: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - case BuiltinOptions_ATan2Options: { - auto ptr = reinterpret_cast(value); - delete ptr; - break; - } - default: break; - } - value = nullptr; - type = BuiltinOptions_NONE; -} - -inline const tflite::Model *GetModel(const void *buf) { - return flatbuffers::GetRoot(buf); -} - -inline const tflite::Model *GetSizePrefixedModel(const void *buf) { - return flatbuffers::GetSizePrefixedRoot(buf); -} - -inline const char *ModelIdentifier() { - return "TFL3"; -} - -inline bool ModelBufferHasIdentifier(const void *buf) { - return flatbuffers::BufferHasIdentifier( - buf, ModelIdentifier()); -} - -inline bool VerifyModelBuffer( - flatbuffers::Verifier &verifier) { - return verifier.VerifyBuffer(ModelIdentifier()); -} - -inline bool VerifySizePrefixedModelBuffer( - flatbuffers::Verifier &verifier) { - return verifier.VerifySizePrefixedBuffer(ModelIdentifier()); -} - -inline const char *ModelExtension() { - return "tflite"; -} - -inline void FinishModelBuffer( - flatbuffers::FlatBufferBuilder &fbb, - flatbuffers::Offset root) { - fbb.Finish(root, ModelIdentifier()); -} - -inline void FinishSizePrefixedModelBuffer( - flatbuffers::FlatBufferBuilder &fbb, - flatbuffers::Offset root) { - fbb.FinishSizePrefixed(root, ModelIdentifier()); -} - -inline std::unique_ptr UnPackModel( - const void *buf, - const flatbuffers::resolver_function_t *res = nullptr) { - return std::unique_ptr(GetModel(buf)->UnPack(res)); -} - -inline std::unique_ptr UnPackSizePrefixedModel( - const void *buf, - const flatbuffers::resolver_function_t *res = nullptr) { - return std::unique_ptr(GetSizePrefixedModel(buf)->UnPack(res)); -} - -} // namespace tflite - -#endif // FLATBUFFERS_GENERATED_SCHEMA_TFLITE_H_ diff --git a/code/components/tflite-lib/tensorflow/lite/schema/schema_utils.cc b/code/components/tflite-lib/tensorflow/lite/schema/schema_utils.cc deleted file mode 100644 index fc19290b..00000000 --- a/code/components/tflite-lib/tensorflow/lite/schema/schema_utils.cc +++ /dev/null @@ -1,62 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#include "tensorflow/lite/schema/schema_utils.h" - -#include - -#include "tensorflow/lite/kernels/internal/compatibility.h" - -namespace tflite { - -// The following GetBuiltinCode methods are the utility methods for reading -// builtin operatore code, ensuring compatibility issues between v3 and v3a -// schema. Always the maximum value of the two fields always will be the correct -// value as follows: -// -// - Supporting schema version v3 models -// -// The `builtin_code` field is not available in the v3 models. Flatbuffer -// library will feed zero value, which is the default value in the v3a schema. -// The actual builtin operatore code value will exist in the -// `deprecated_builtin_code` field. At the same time, it implies that -// `deprecated_builtin_code` >= `builtin_code` and the maximum value of the two -// fields will be same with `deprecated_builtin_code'. -// -// - Supporting builtin operator codes beyonds 127 -// -// New builtin operators, whose operator code is larger than 127, can not be -// assigned to the `deprecated_builtin_code` field. In such cases, the -// value of the `builtin_code` field should be used for the builtin operator -// code. In the case, the maximum value of the two fields will be the value of -// the `builtin_code` as the right value. - -BuiltinOperator GetBuiltinCode(const OperatorCode* op_code) { - // Caller should guarantee that the given argument value is not a nullptr. - TFLITE_DCHECK(op_code != nullptr); - - return std::max( - op_code->builtin_code(), - static_cast(op_code->deprecated_builtin_code())); -} - -BuiltinOperator GetBuiltinCode(const OperatorCodeT* op_code) { - // Caller should guarantee that the given argument value is not a nullptr. - TFLITE_DCHECK(op_code != nullptr); - - return std::max(op_code->builtin_code, static_cast( - op_code->deprecated_builtin_code)); -} - -} // namespace tflite diff --git a/code/components/tflite-lib/tensorflow/lite/schema/schema_utils.h b/code/components/tflite-lib/tensorflow/lite/schema/schema_utils.h deleted file mode 100644 index 9cca36c7..00000000 --- a/code/components/tflite-lib/tensorflow/lite/schema/schema_utils.h +++ /dev/null @@ -1,33 +0,0 @@ -/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_LITE_SCHEMA_SCHEMA_UTILS_H_ -#define TENSORFLOW_LITE_SCHEMA_SCHEMA_UTILS_H_ - -#include "flatbuffers/flatbuffers.h" -#include "tensorflow/lite/schema/schema_generated.h" - -namespace tflite { - -// The following methods are introduced to resolve op builtin code shortage -// problem. The new builtin operator will be assigned to the extended builtin -// code field in the flatbuffer schema. Those methods helps to hide builtin code -// details. -BuiltinOperator GetBuiltinCode(const OperatorCode *op_code); - -BuiltinOperator GetBuiltinCode(const OperatorCodeT *op_code); - -} // namespace tflite - -#endif // TENSORFLOW_LITE_SCHEMA_SCHEMA_UTILS_H_ diff --git a/code/components/tflite-lib/third_party/flatbuffers/LICENSE.txt b/code/components/tflite-lib/third_party/flatbuffers/LICENSE.txt deleted file mode 100644 index d6456956..00000000 --- a/code/components/tflite-lib/third_party/flatbuffers/LICENSE.txt +++ /dev/null @@ -1,202 +0,0 @@ - - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. diff --git a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/allocator.h b/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/allocator.h deleted file mode 100644 index f4ef22db..00000000 --- a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/allocator.h +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2021 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FLATBUFFERS_ALLOCATOR_H_ -#define FLATBUFFERS_ALLOCATOR_H_ - -#include "flatbuffers/base.h" - -namespace flatbuffers { - -// Allocator interface. This is flatbuffers-specific and meant only for -// `vector_downward` usage. -class Allocator { - public: - virtual ~Allocator() {} - - // Allocate `size` bytes of memory. - virtual uint8_t *allocate(size_t size) = 0; - - // Deallocate `size` bytes of memory at `p` allocated by this allocator. - virtual void deallocate(uint8_t *p, size_t size) = 0; - - // Reallocate `new_size` bytes of memory, replacing the old region of size - // `old_size` at `p`. In contrast to a normal realloc, this grows downwards, - // and is intended specifcally for `vector_downward` use. - // `in_use_back` and `in_use_front` indicate how much of `old_size` is - // actually in use at each end, and needs to be copied. - virtual uint8_t *reallocate_downward(uint8_t *old_p, size_t old_size, - size_t new_size, size_t in_use_back, - size_t in_use_front) { - FLATBUFFERS_ASSERT(new_size > old_size); // vector_downward only grows - uint8_t *new_p = allocate(new_size); - memcpy_downward(old_p, old_size, new_p, new_size, in_use_back, - in_use_front); - deallocate(old_p, old_size); - return new_p; - } - - protected: - // Called by `reallocate_downward` to copy memory from `old_p` of `old_size` - // to `new_p` of `new_size`. Only memory of size `in_use_front` and - // `in_use_back` will be copied from the front and back of the old memory - // allocation. - void memcpy_downward(uint8_t *old_p, size_t old_size, uint8_t *new_p, - size_t new_size, size_t in_use_back, - size_t in_use_front) { - memcpy(new_p + new_size - in_use_back, old_p + old_size - in_use_back, - in_use_back); - memcpy(new_p, old_p, in_use_front); - } -}; - -} // namespace flatbuffers - -#endif // FLATBUFFERS_ALLOCATOR_H_ \ No newline at end of file diff --git a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/array.h b/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/array.h deleted file mode 100644 index d4b73fc9..00000000 --- a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/array.h +++ /dev/null @@ -1,243 +0,0 @@ -/* - * Copyright 2021 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FLATBUFFERS_ARRAY_H_ -#define FLATBUFFERS_ARRAY_H_ - -#include "flatbuffers/base.h" -#include "flatbuffers/stl_emulation.h" -#include "flatbuffers/vector.h" - -namespace flatbuffers { - -// This is used as a helper type for accessing arrays. -template class Array { - // Array can carry only POD data types (scalars or structs). - typedef typename flatbuffers::bool_constant::value> - scalar_tag; - typedef - typename flatbuffers::conditional::type - IndirectHelperType; - - public: - typedef uint16_t size_type; - typedef typename IndirectHelper::return_type return_type; - typedef VectorIterator const_iterator; - typedef VectorReverseIterator const_reverse_iterator; - - // If T is a LE-scalar or a struct (!scalar_tag::value). - static FLATBUFFERS_CONSTEXPR bool is_span_observable = - (scalar_tag::value && (FLATBUFFERS_LITTLEENDIAN || sizeof(T) == 1)) || - !scalar_tag::value; - - FLATBUFFERS_CONSTEXPR uint16_t size() const { return length; } - - return_type Get(uoffset_t i) const { - FLATBUFFERS_ASSERT(i < size()); - return IndirectHelper::Read(Data(), i); - } - - return_type operator[](uoffset_t i) const { return Get(i); } - - // If this is a Vector of enums, T will be its storage type, not the enum - // type. This function makes it convenient to retrieve value with enum - // type E. - template E GetEnum(uoffset_t i) const { - return static_cast(Get(i)); - } - - const_iterator begin() const { return const_iterator(Data(), 0); } - const_iterator end() const { return const_iterator(Data(), size()); } - - const_reverse_iterator rbegin() const { - return const_reverse_iterator(end()); - } - const_reverse_iterator rend() const { - return const_reverse_iterator(begin()); - } - - const_iterator cbegin() const { return begin(); } - const_iterator cend() const { return end(); } - - const_reverse_iterator crbegin() const { return rbegin(); } - const_reverse_iterator crend() const { return rend(); } - - // Get a mutable pointer to elements inside this array. - // This method used to mutate arrays of structs followed by a @p Mutate - // operation. For primitive types use @p Mutate directly. - // @warning Assignments and reads to/from the dereferenced pointer are not - // automatically converted to the correct endianness. - typename flatbuffers::conditional::type - GetMutablePointer(uoffset_t i) const { - FLATBUFFERS_ASSERT(i < size()); - return const_cast(&data()[i]); - } - - // Change elements if you have a non-const pointer to this object. - void Mutate(uoffset_t i, const T &val) { MutateImpl(scalar_tag(), i, val); } - - // The raw data in little endian format. Use with care. - const uint8_t *Data() const { return data_; } - - uint8_t *Data() { return data_; } - - // Similarly, but typed, much like std::vector::data - const T *data() const { return reinterpret_cast(Data()); } - T *data() { return reinterpret_cast(Data()); } - - // Copy data from a span with endian conversion. - // If this Array and the span overlap, the behavior is undefined. - void CopyFromSpan(flatbuffers::span src) { - const auto p1 = reinterpret_cast(src.data()); - const auto p2 = Data(); - FLATBUFFERS_ASSERT(!(p1 >= p2 && p1 < (p2 + length)) && - !(p2 >= p1 && p2 < (p1 + length))); - (void)p1; - (void)p2; - CopyFromSpanImpl(flatbuffers::bool_constant(), src); - } - - protected: - void MutateImpl(flatbuffers::true_type, uoffset_t i, const T &val) { - FLATBUFFERS_ASSERT(i < size()); - WriteScalar(data() + i, val); - } - - void MutateImpl(flatbuffers::false_type, uoffset_t i, const T &val) { - *(GetMutablePointer(i)) = val; - } - - void CopyFromSpanImpl(flatbuffers::true_type, - flatbuffers::span src) { - // Use std::memcpy() instead of std::copy() to avoid performance degradation - // due to aliasing if T is char or unsigned char. - // The size is known at compile time, so memcpy would be inlined. - std::memcpy(data(), src.data(), length * sizeof(T)); - } - - // Copy data from flatbuffers::span with endian conversion. - void CopyFromSpanImpl(flatbuffers::false_type, - flatbuffers::span src) { - for (size_type k = 0; k < length; k++) { Mutate(k, src[k]); } - } - - // This class is only used to access pre-existing data. Don't ever - // try to construct these manually. - // 'constexpr' allows us to use 'size()' at compile time. - // @note Must not use 'FLATBUFFERS_CONSTEXPR' here, as const is not allowed on - // a constructor. -#if defined(__cpp_constexpr) - constexpr Array(); -#else - Array(); -#endif - - uint8_t data_[length * sizeof(T)]; - - private: - // This class is a pointer. Copying will therefore create an invalid object. - // Private and unimplemented copy constructor. - Array(const Array &); - Array &operator=(const Array &); -}; - -// Specialization for Array[struct] with access using Offset pointer. -// This specialization used by idl_gen_text.cpp. -template class Array, length> { - static_assert(flatbuffers::is_same::value, "unexpected type T"); - - public: - typedef const void *return_type; - - const uint8_t *Data() const { return data_; } - - // Make idl_gen_text.cpp::PrintContainer happy. - return_type operator[](uoffset_t) const { - FLATBUFFERS_ASSERT(false); - return nullptr; - } - - private: - // This class is only used to access pre-existing data. - Array(); - Array(const Array &); - Array &operator=(const Array &); - - uint8_t data_[1]; -}; - -template -FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span make_span(Array &arr) - FLATBUFFERS_NOEXCEPT { - static_assert( - Array::is_span_observable, - "wrong type U, only plain struct, LE-scalar, or byte types are allowed"); - return span(arr.data(), N); -} - -template -FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span make_span( - const Array &arr) FLATBUFFERS_NOEXCEPT { - static_assert( - Array::is_span_observable, - "wrong type U, only plain struct, LE-scalar, or byte types are allowed"); - return span(arr.data(), N); -} - -template -FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span -make_bytes_span(Array &arr) FLATBUFFERS_NOEXCEPT { - static_assert(Array::is_span_observable, - "internal error, Array might hold only scalars or structs"); - return span(arr.Data(), sizeof(U) * N); -} - -template -FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span -make_bytes_span(const Array &arr) FLATBUFFERS_NOEXCEPT { - static_assert(Array::is_span_observable, - "internal error, Array might hold only scalars or structs"); - return span(arr.Data(), sizeof(U) * N); -} - -// Cast a raw T[length] to a raw flatbuffers::Array -// without endian conversion. Use with care. -// TODO: move these Cast-methods to `internal` namespace. -template -Array &CastToArray(T (&arr)[length]) { - return *reinterpret_cast *>(arr); -} - -template -const Array &CastToArray(const T (&arr)[length]) { - return *reinterpret_cast *>(arr); -} - -template -Array &CastToArrayOfEnum(T (&arr)[length]) { - static_assert(sizeof(E) == sizeof(T), "invalid enum type E"); - return *reinterpret_cast *>(arr); -} - -template -const Array &CastToArrayOfEnum(const T (&arr)[length]) { - static_assert(sizeof(E) == sizeof(T), "invalid enum type E"); - return *reinterpret_cast *>(arr); -} - -} // namespace flatbuffers - -#endif // FLATBUFFERS_ARRAY_H_ diff --git a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/base.h b/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/base.h deleted file mode 100644 index 371b6fdb..00000000 --- a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/base.h +++ /dev/null @@ -1,484 +0,0 @@ -#ifndef FLATBUFFERS_BASE_H_ -#define FLATBUFFERS_BASE_H_ - -// For TFLM, we always want FLATBUFFERS_LOCALE_INDEPENDENT to be defined as 0. -// We could achieve this by adding -DFLATBUFFERS_LOCALE_INDEPENDENT=0 to the -// TFLM Makefile. However, for (at least) the Arduino, adding additional build -// flags during the compilation can be a bit awkward. As such, we have instead -// made a decision to change the default to be FLATBUFFERS_LOCALE_INDEPENDENT=0 -// for TFLM to make it easier for external IDE integration. -#ifndef FLATBUFFERS_LOCALE_INDEPENDENT -#define FLATBUFFERS_LOCALE_INDEPENDENT 0 -#endif - -// clang-format off - -// If activate should be declared and included first. -#if defined(FLATBUFFERS_MEMORY_LEAK_TRACKING) && \ - defined(_MSC_VER) && defined(_DEBUG) - // The _CRTDBG_MAP_ALLOC inside will replace - // calloc/free (etc) to its debug version using #define directives. - #define _CRTDBG_MAP_ALLOC - #include - #include - // Replace operator new by trace-enabled version. - #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__) - #define new DEBUG_NEW -#endif - -#if !defined(FLATBUFFERS_ASSERT) -#include -#define FLATBUFFERS_ASSERT assert -#elif defined(FLATBUFFERS_ASSERT_INCLUDE) -// Include file with forward declaration -#include FLATBUFFERS_ASSERT_INCLUDE -#endif - -#ifndef ARDUINO -#include -#endif - -#include -#include -#include - -#if defined(ARDUINO) && !defined(ARDUINOSTL_M_H) - #include -#else - #include -#endif - -#include -#include -#include -#include -#include -#include -#include - -#if defined(__unix__) && !defined(FLATBUFFERS_LOCALE_INDEPENDENT) - #include -#endif - -#ifdef __ANDROID__ - #include -#endif - -#if defined(__ICCARM__) -#include -#endif - -// Note the __clang__ check is needed, because clang presents itself -// as an older GNUC compiler (4.2). -// Clang 3.3 and later implement all of the ISO C++ 2011 standard. -// Clang 3.4 and later implement all of the ISO C++ 2014 standard. -// http://clang.llvm.org/cxx_status.html - -// Note the MSVC value '__cplusplus' may be incorrect: -// The '__cplusplus' predefined macro in the MSVC stuck at the value 199711L, -// indicating (erroneously!) that the compiler conformed to the C++98 Standard. -// This value should be correct starting from MSVC2017-15.7-Preview-3. -// The '__cplusplus' will be valid only if MSVC2017-15.7-P3 and the `/Zc:__cplusplus` switch is set. -// Workaround (for details see MSDN): -// Use the _MSC_VER and _MSVC_LANG definition instead of the __cplusplus for compatibility. -// The _MSVC_LANG macro reports the Standard version regardless of the '/Zc:__cplusplus' switch. - -#if defined(__GNUC__) && !defined(__clang__) - #define FLATBUFFERS_GCC (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) -#else - #define FLATBUFFERS_GCC 0 -#endif - -#if defined(__clang__) - #define FLATBUFFERS_CLANG (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) -#else - #define FLATBUFFERS_CLANG 0 -#endif - -/// @cond FLATBUFFERS_INTERNAL -#if __cplusplus <= 199711L && \ - (!defined(_MSC_VER) || _MSC_VER < 1600) && \ - (!defined(__GNUC__) || \ - (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40400)) - #error A C++11 compatible compiler with support for the auto typing is \ - required for FlatBuffers. - #error __cplusplus _MSC_VER __GNUC__ __GNUC_MINOR__ __GNUC_PATCHLEVEL__ -#endif - -#if !defined(__clang__) && \ - defined(__GNUC__) && \ - (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40600) - // Backwards compatibility for g++ 4.4, and 4.5 which don't have the nullptr - // and constexpr keywords. Note the __clang__ check is needed, because clang - // presents itself as an older GNUC compiler. - #ifndef nullptr_t - const class nullptr_t { - public: - template inline operator T*() const { return 0; } - private: - void operator&() const; - } nullptr = {}; - #endif - #ifndef constexpr - #define constexpr const - #endif -#endif - -// The wire format uses a little endian encoding (since that's efficient for -// the common platforms). -#if defined(__s390x__) - #define FLATBUFFERS_LITTLEENDIAN 0 -#endif // __s390x__ -#if !defined(FLATBUFFERS_LITTLEENDIAN) - #if defined(__GNUC__) || defined(__clang__) || defined(__ICCARM__) - #if (defined(__BIG_ENDIAN__) || \ - (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)) - #define FLATBUFFERS_LITTLEENDIAN 0 - #else - #define FLATBUFFERS_LITTLEENDIAN 1 - #endif // __BIG_ENDIAN__ - #elif defined(_MSC_VER) - #if defined(_M_PPC) - #define FLATBUFFERS_LITTLEENDIAN 0 - #else - #define FLATBUFFERS_LITTLEENDIAN 1 - #endif - #else - #error Unable to determine endianness, define FLATBUFFERS_LITTLEENDIAN. - #endif -#endif // !defined(FLATBUFFERS_LITTLEENDIAN) - -#define FLATBUFFERS_VERSION_MAJOR 2 -#define FLATBUFFERS_VERSION_MINOR 0 -#define FLATBUFFERS_VERSION_REVISION 5 -#define FLATBUFFERS_STRING_EXPAND(X) #X -#define FLATBUFFERS_STRING(X) FLATBUFFERS_STRING_EXPAND(X) -namespace flatbuffers { - // Returns version as string "MAJOR.MINOR.REVISION". - const char* FLATBUFFERS_VERSION(); -} - -#if (!defined(_MSC_VER) || _MSC_VER > 1600) && \ - (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 407)) || \ - defined(__clang__) - #define FLATBUFFERS_FINAL_CLASS final - #define FLATBUFFERS_OVERRIDE override - #define FLATBUFFERS_EXPLICIT_CPP11 explicit - #define FLATBUFFERS_VTABLE_UNDERLYING_TYPE : flatbuffers::voffset_t -#else - #define FLATBUFFERS_FINAL_CLASS - #define FLATBUFFERS_OVERRIDE - #define FLATBUFFERS_EXPLICIT_CPP11 - #define FLATBUFFERS_VTABLE_UNDERLYING_TYPE -#endif - -#if (!defined(_MSC_VER) || _MSC_VER >= 1900) && \ - (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 406)) || \ - (defined(__cpp_constexpr) && __cpp_constexpr >= 200704) - #define FLATBUFFERS_CONSTEXPR constexpr - #define FLATBUFFERS_CONSTEXPR_CPP11 constexpr - #define FLATBUFFERS_CONSTEXPR_DEFINED -#else - #define FLATBUFFERS_CONSTEXPR const - #define FLATBUFFERS_CONSTEXPR_CPP11 -#endif - -#if (defined(__cplusplus) && __cplusplus >= 201402L) || \ - (defined(__cpp_constexpr) && __cpp_constexpr >= 201304) - #define FLATBUFFERS_CONSTEXPR_CPP14 FLATBUFFERS_CONSTEXPR_CPP11 -#else - #define FLATBUFFERS_CONSTEXPR_CPP14 -#endif - -#if (defined(__GXX_EXPERIMENTAL_CXX0X__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 406)) || \ - (defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 190023026)) || \ - defined(__clang__) - #define FLATBUFFERS_NOEXCEPT noexcept -#else - #define FLATBUFFERS_NOEXCEPT -#endif - -// NOTE: the FLATBUFFERS_DELETE_FUNC macro may change the access mode to -// private, so be sure to put it at the end or reset access mode explicitly. -#if (!defined(_MSC_VER) || _MSC_FULL_VER >= 180020827) && \ - (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 404)) || \ - defined(__clang__) - #define FLATBUFFERS_DELETE_FUNC(func) func = delete -#else - #define FLATBUFFERS_DELETE_FUNC(func) private: func -#endif - -#if (!defined(_MSC_VER) || _MSC_VER >= 1900) && \ - (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 409)) || \ - defined(__clang__) - #define FLATBUFFERS_DEFAULT_DECLARATION -#endif - -// Check if we can use template aliases -// Not possible if Microsoft Compiler before 2012 -// Possible is the language feature __cpp_alias_templates is defined well -// Or possible if the C++ std is C+11 or newer -#if (defined(_MSC_VER) && _MSC_VER > 1700 /* MSVC2012 */) \ - || (defined(__cpp_alias_templates) && __cpp_alias_templates >= 200704) \ - || (defined(__cplusplus) && __cplusplus >= 201103L) - #define FLATBUFFERS_TEMPLATES_ALIASES -#endif - -#ifndef FLATBUFFERS_HAS_STRING_VIEW - // Only provide flatbuffers::string_view if __has_include can be used - // to detect a header that provides an implementation - #if defined(__has_include) - // Check for std::string_view (in c++17) - #if __has_include() && (__cplusplus >= 201606 || (defined(_HAS_CXX17) && _HAS_CXX17)) - #include - namespace flatbuffers { - typedef std::string_view string_view; - } - #define FLATBUFFERS_HAS_STRING_VIEW 1 - // Check for std::experimental::string_view (in c++14, compiler-dependent) - #elif __has_include() && (__cplusplus >= 201411) - #include - namespace flatbuffers { - typedef std::experimental::string_view string_view; - } - #define FLATBUFFERS_HAS_STRING_VIEW 1 - // Check for absl::string_view - #elif __has_include("absl/strings/string_view.h") - #include "absl/strings/string_view.h" - namespace flatbuffers { - typedef absl::string_view string_view; - } - #define FLATBUFFERS_HAS_STRING_VIEW 1 - #endif - #endif // __has_include -#endif // !FLATBUFFERS_HAS_STRING_VIEW - -#ifndef FLATBUFFERS_GENERAL_HEAP_ALLOC_OK - // Allow heap allocations to be used - #define FLATBUFFERS_GENERAL_HEAP_ALLOC_OK 1 -#endif // !FLATBUFFERS_GENERAL_HEAP_ALLOC_OK - -#ifndef FLATBUFFERS_HAS_NEW_STRTOD - // Modern (C++11) strtod and strtof functions are available for use. - // 1) nan/inf strings as argument of strtod; - // 2) hex-float as argument of strtod/strtof. - #if (defined(_MSC_VER) && _MSC_VER >= 1900) || \ - (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 409)) || \ - (defined(__clang__)) - #define FLATBUFFERS_HAS_NEW_STRTOD 1 - #endif -#endif // !FLATBUFFERS_HAS_NEW_STRTOD - -#ifndef FLATBUFFERS_LOCALE_INDEPENDENT - // Enable locale independent functions {strtof_l, strtod_l,strtoll_l, strtoull_l}. - #if ((defined(_MSC_VER) && _MSC_VER >= 1800) || \ - (defined(_XOPEN_VERSION) && (_XOPEN_VERSION>=700)) && (!defined(__ANDROID_API__) || (defined(__ANDROID_API__) && (__ANDROID_API__>=21)))) - #define FLATBUFFERS_LOCALE_INDEPENDENT 1 - #else - #define FLATBUFFERS_LOCALE_INDEPENDENT 0 - #endif -#endif // !FLATBUFFERS_LOCALE_INDEPENDENT - -// Suppress Undefined Behavior Sanitizer (recoverable only). Usage: -// - __supress_ubsan__("undefined") -// - __supress_ubsan__("signed-integer-overflow") -#if defined(__clang__) && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >=7)) - #define __supress_ubsan__(type) __attribute__((no_sanitize(type))) -#elif defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 409) - #define __supress_ubsan__(type) __attribute__((no_sanitize_undefined)) -#else - #define __supress_ubsan__(type) -#endif - -// This is constexpr function used for checking compile-time constants. -// Avoid `#pragma warning(disable: 4127) // C4127: expression is constant`. -template FLATBUFFERS_CONSTEXPR inline bool IsConstTrue(T t) { - return !!t; -} - -// Enable C++ attribute [[]] if std:c++17 or higher. -#if ((__cplusplus >= 201703L) \ - || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L))) - // All attributes unknown to an implementation are ignored without causing an error. - #define FLATBUFFERS_ATTRIBUTE(attr) attr - - #define FLATBUFFERS_FALLTHROUGH() [[fallthrough]] -#else - #define FLATBUFFERS_ATTRIBUTE(attr) - - #if FLATBUFFERS_CLANG >= 30800 - #define FLATBUFFERS_FALLTHROUGH() [[clang::fallthrough]] - #elif FLATBUFFERS_GCC >= 70300 - #define FLATBUFFERS_FALLTHROUGH() [[gnu::fallthrough]] - #else - #define FLATBUFFERS_FALLTHROUGH() - #endif -#endif - -/// @endcond - -/// @file -namespace flatbuffers { - -/// @cond FLATBUFFERS_INTERNAL -// Our default offset / size type, 32bit on purpose on 64bit systems. -// Also, using a consistent offset type maintains compatibility of serialized -// offset values between 32bit and 64bit systems. -typedef uint32_t uoffset_t; - -// Signed offsets for references that can go in both directions. -typedef int32_t soffset_t; - -// Offset/index used in v-tables, can be changed to uint8_t in -// format forks to save a bit of space if desired. -typedef uint16_t voffset_t; - -typedef uintmax_t largest_scalar_t; - -// In 32bits, this evaluates to 2GB - 1 -#define FLATBUFFERS_MAX_BUFFER_SIZE ((1ULL << (sizeof(::flatbuffers::soffset_t) * 8 - 1)) - 1) - -// We support aligning the contents of buffers up to this size. -#define FLATBUFFERS_MAX_ALIGNMENT 16 - -/// @brief The length of a FlatBuffer file header. -static const size_t kFileIdentifierLength = 4; - -inline bool VerifyAlignmentRequirements(size_t align, size_t min_align = 1) { - return (min_align <= align) && (align <= (FLATBUFFERS_MAX_ALIGNMENT)) && - (align & (align - 1)) == 0; // must be power of 2 -} - -#if defined(_MSC_VER) - #pragma warning(disable: 4351) // C4351: new behavior: elements of array ... will be default initialized - #pragma warning(push) - #pragma warning(disable: 4127) // C4127: conditional expression is constant -#endif - -template T EndianSwap(T t) { - #if defined(_MSC_VER) - #define FLATBUFFERS_BYTESWAP16 _byteswap_ushort - #define FLATBUFFERS_BYTESWAP32 _byteswap_ulong - #define FLATBUFFERS_BYTESWAP64 _byteswap_uint64 - #elif defined(__ICCARM__) - #define FLATBUFFERS_BYTESWAP16 __REV16 - #define FLATBUFFERS_BYTESWAP32 __REV - #define FLATBUFFERS_BYTESWAP64(x) \ - ((__REV(static_cast(x >> 32U))) | (static_cast(__REV(static_cast(x)))) << 32U) - #else - #if defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ < 408 && !defined(__clang__) - // __builtin_bswap16 was missing prior to GCC 4.8. - #define FLATBUFFERS_BYTESWAP16(x) \ - static_cast(__builtin_bswap32(static_cast(x) << 16)) - #else - #define FLATBUFFERS_BYTESWAP16 __builtin_bswap16 - #endif - #define FLATBUFFERS_BYTESWAP32 __builtin_bswap32 - #define FLATBUFFERS_BYTESWAP64 __builtin_bswap64 - #endif - if (sizeof(T) == 1) { // Compile-time if-then's. - return t; - } else if (sizeof(T) == 2) { - union { T t; uint16_t i; } u = { t }; - u.i = FLATBUFFERS_BYTESWAP16(u.i); - return u.t; - } else if (sizeof(T) == 4) { - union { T t; uint32_t i; } u = { t }; - u.i = FLATBUFFERS_BYTESWAP32(u.i); - return u.t; - } else if (sizeof(T) == 8) { - union { T t; uint64_t i; } u = { t }; - u.i = FLATBUFFERS_BYTESWAP64(u.i); - return u.t; - } else { - FLATBUFFERS_ASSERT(0); - return t; - } -} - -#if defined(_MSC_VER) - #pragma warning(pop) -#endif - - -template T EndianScalar(T t) { - #if FLATBUFFERS_LITTLEENDIAN - return t; - #else - return EndianSwap(t); - #endif -} - -template -// UBSAN: C++ aliasing type rules, see std::bit_cast<> for details. -__supress_ubsan__("alignment") -T ReadScalar(const void *p) { - return EndianScalar(*reinterpret_cast(p)); -} - -// See https://github.com/google/flatbuffers/issues/5950 - -#if (FLATBUFFERS_GCC >= 100000) && (FLATBUFFERS_GCC < 110000) - #pragma GCC diagnostic push - #pragma GCC diagnostic ignored "-Wstringop-overflow" -#endif - -template -// UBSAN: C++ aliasing type rules, see std::bit_cast<> for details. -__supress_ubsan__("alignment") -void WriteScalar(void *p, T t) { - *reinterpret_cast(p) = EndianScalar(t); -} - -template struct Offset; -template __supress_ubsan__("alignment") void WriteScalar(void *p, Offset t) { - *reinterpret_cast(p) = EndianScalar(t.o); -} - -#if (FLATBUFFERS_GCC >= 100000) && (FLATBUFFERS_GCC < 110000) - #pragma GCC diagnostic pop -#endif - -// Computes how many bytes you'd have to pad to be able to write an -// "scalar_size" scalar if the buffer had grown to "buf_size" (downwards in -// memory). -__supress_ubsan__("unsigned-integer-overflow") -inline size_t PaddingBytes(size_t buf_size, size_t scalar_size) { - return ((~buf_size) + 1) & (scalar_size - 1); -} - -// Generic 'operator==' with conditional specialisations. -// T e - new value of a scalar field. -// T def - default of scalar (is known at compile-time). -template inline bool IsTheSameAs(T e, T def) { return e == def; } - -#if defined(FLATBUFFERS_NAN_DEFAULTS) && \ - defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0) -// Like `operator==(e, def)` with weak NaN if T=(float|double). -template inline bool IsFloatTheSameAs(T e, T def) { - return (e == def) || ((def != def) && (e != e)); -} -template<> inline bool IsTheSameAs(float e, float def) { - return IsFloatTheSameAs(e, def); -} -template<> inline bool IsTheSameAs(double e, double def) { - return IsFloatTheSameAs(e, def); -} -#endif - -// Check 'v' is out of closed range [low; high]. -// Workaround for GCC warning [-Werror=type-limits]: -// comparison is always true due to limited range of data type. -template -inline bool IsOutRange(const T &v, const T &low, const T &high) { - return (v < low) || (high < v); -} - -// Check 'v' is in closed range [low; high]. -template -inline bool IsInRange(const T &v, const T &low, const T &high) { - return !IsOutRange(v, low, high); -} - -} // namespace flatbuffers -#endif // FLATBUFFERS_BASE_H_ diff --git a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/buffer.h b/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/buffer.h deleted file mode 100644 index e8d2ce9c..00000000 --- a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/buffer.h +++ /dev/null @@ -1,142 +0,0 @@ -/* - * Copyright 2021 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FLATBUFFERS_BUFFER_H_ -#define FLATBUFFERS_BUFFER_H_ - -#include "flatbuffers/base.h" - -namespace flatbuffers { - -// Wrapper for uoffset_t to allow safe template specialization. -// Value is allowed to be 0 to indicate a null object (see e.g. AddOffset). -template struct Offset { - uoffset_t o; - Offset() : o(0) {} - Offset(uoffset_t _o) : o(_o) {} - Offset Union() const { return Offset(o); } - bool IsNull() const { return !o; } -}; - -inline void EndianCheck() { - int endiantest = 1; - // If this fails, see FLATBUFFERS_LITTLEENDIAN above. - FLATBUFFERS_ASSERT(*reinterpret_cast(&endiantest) == - FLATBUFFERS_LITTLEENDIAN); - (void)endiantest; -} - -template FLATBUFFERS_CONSTEXPR size_t AlignOf() { - // clang-format off - #ifdef _MSC_VER - return __alignof(T); - #else - #ifndef alignof - return __alignof__(T); - #else - return alignof(T); - #endif - #endif - // clang-format on -} - -// Lexicographically compare two strings (possibly containing nulls), and -// return true if the first is less than the second. -static inline bool StringLessThan(const char *a_data, uoffset_t a_size, - const char *b_data, uoffset_t b_size) { - const auto cmp = memcmp(a_data, b_data, (std::min)(a_size, b_size)); - return cmp == 0 ? a_size < b_size : cmp < 0; -} - -// When we read serialized data from memory, in the case of most scalars, -// we want to just read T, but in the case of Offset, we want to actually -// perform the indirection and return a pointer. -// The template specialization below does just that. -// It is wrapped in a struct since function templates can't overload on the -// return type like this. -// The typedef is for the convenience of callers of this function -// (avoiding the need for a trailing return decltype) -template struct IndirectHelper { - typedef T return_type; - typedef T mutable_return_type; - static const size_t element_stride = sizeof(T); - static return_type Read(const uint8_t *p, uoffset_t i) { - return EndianScalar((reinterpret_cast(p))[i]); - } -}; -template struct IndirectHelper> { - typedef const T *return_type; - typedef T *mutable_return_type; - static const size_t element_stride = sizeof(uoffset_t); - static return_type Read(const uint8_t *p, uoffset_t i) { - p += i * sizeof(uoffset_t); - return reinterpret_cast(p + ReadScalar(p)); - } -}; -template struct IndirectHelper { - typedef const T *return_type; - typedef T *mutable_return_type; - static const size_t element_stride = sizeof(T); - static return_type Read(const uint8_t *p, uoffset_t i) { - return reinterpret_cast(p + i * sizeof(T)); - } -}; - -/// @brief Get a pointer to the the file_identifier section of the buffer. -/// @return Returns a const char pointer to the start of the file_identifier -/// characters in the buffer. The returned char * has length -/// 'flatbuffers::FlatBufferBuilder::kFileIdentifierLength'. -/// This function is UNDEFINED for FlatBuffers whose schema does not include -/// a file_identifier (likely points at padding or the start of a the root -/// vtable). -inline const char *GetBufferIdentifier(const void *buf, - bool size_prefixed = false) { - return reinterpret_cast(buf) + - ((size_prefixed) ? 2 * sizeof(uoffset_t) : sizeof(uoffset_t)); -} - -// Helper to see if the identifier in a buffer has the expected value. -inline bool BufferHasIdentifier(const void *buf, const char *identifier, - bool size_prefixed = false) { - return strncmp(GetBufferIdentifier(buf, size_prefixed), identifier, - flatbuffers::kFileIdentifierLength) == 0; -} - -/// @cond FLATBUFFERS_INTERNAL -// Helpers to get a typed pointer to the root object contained in the buffer. -template T *GetMutableRoot(void *buf) { - EndianCheck(); - return reinterpret_cast( - reinterpret_cast(buf) + - EndianScalar(*reinterpret_cast(buf))); -} - -template T *GetMutableSizePrefixedRoot(void *buf) { - return GetMutableRoot(reinterpret_cast(buf) + - sizeof(uoffset_t)); -} - -template const T *GetRoot(const void *buf) { - return GetMutableRoot(const_cast(buf)); -} - -template const T *GetSizePrefixedRoot(const void *buf) { - return GetRoot(reinterpret_cast(buf) + sizeof(uoffset_t)); -} - -} // namespace flatbuffers - -#endif // FLATBUFFERS_BUFFER_H_ \ No newline at end of file diff --git a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/buffer_ref.h b/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/buffer_ref.h deleted file mode 100644 index ce302073..00000000 --- a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/buffer_ref.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2021 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FLATBUFFERS_BUFFER_REF_H_ -#define FLATBUFFERS_BUFFER_REF_H_ - -#include "flatbuffers/base.h" -#include "flatbuffers/verifier.h" - -namespace flatbuffers { - -// Convenient way to bundle a buffer and its length, to pass it around -// typed by its root. -// A BufferRef does not own its buffer. -struct BufferRefBase {}; // for std::is_base_of - -template struct BufferRef : BufferRefBase { - BufferRef() : buf(nullptr), len(0), must_free(false) {} - BufferRef(uint8_t *_buf, uoffset_t _len) - : buf(_buf), len(_len), must_free(false) {} - - ~BufferRef() { - if (must_free) free(buf); - } - - const T *GetRoot() const { return flatbuffers::GetRoot(buf); } - - bool Verify() { - Verifier verifier(buf, len); - return verifier.VerifyBuffer(nullptr); - } - - uint8_t *buf; - uoffset_t len; - bool must_free; -}; - -} // namespace flatbuffers - -#endif // FLATBUFFERS_BUFFER_REF_H_ \ No newline at end of file diff --git a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/default_allocator.h b/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/default_allocator.h deleted file mode 100644 index 975d9380..00000000 --- a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/default_allocator.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2021 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FLATBUFFERS_DEFAULT_ALLOCATOR_H_ -#define FLATBUFFERS_DEFAULT_ALLOCATOR_H_ - -#include "flatbuffers/allocator.h" -#include "flatbuffers/base.h" - -namespace flatbuffers { - -// DefaultAllocator uses new/delete to allocate memory regions -class DefaultAllocator : public Allocator { - public: - uint8_t *allocate(size_t size) FLATBUFFERS_OVERRIDE { - return new uint8_t[size]; - } - - void deallocate(uint8_t *p, size_t) FLATBUFFERS_OVERRIDE { delete[] p; } - - static void dealloc(void *p, size_t) { delete[] static_cast(p); } -}; - -// These functions allow for a null allocator to mean use the default allocator, -// as used by DetachedBuffer and vector_downward below. -// This is to avoid having a statically or dynamically allocated default -// allocator, or having to move it between the classes that may own it. -inline uint8_t *Allocate(Allocator *allocator, size_t size) { - return allocator->allocate(size); -} - -inline void Deallocate(Allocator *allocator, uint8_t *p, size_t size) { - allocator->deallocate(p, size); -} - -inline uint8_t *ReallocateDownward(Allocator *allocator, uint8_t *old_p, - size_t old_size, size_t new_size, - size_t in_use_back, size_t in_use_front) { - return allocator->reallocate_downward(old_p, old_size, new_size, in_use_back, - in_use_front); -} - -} // namespace flatbuffers - -#endif // FLATBUFFERS_DEFAULT_ALLOCATOR_H_ diff --git a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/detached_buffer.h b/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/detached_buffer.h deleted file mode 100644 index 760a0884..00000000 --- a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/detached_buffer.h +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright 2021 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FLATBUFFERS_DETACHED_BUFFER_H_ -#define FLATBUFFERS_DETACHED_BUFFER_H_ - -#include "flatbuffers/allocator.h" -#include "flatbuffers/base.h" -#include "flatbuffers/default_allocator.h" - -namespace flatbuffers { - -// DetachedBuffer is a finished flatbuffer memory region, detached from its -// builder. The original memory region and allocator are also stored so that -// the DetachedBuffer can manage the memory lifetime. -class DetachedBuffer { - public: - DetachedBuffer() - : allocator_(nullptr), - own_allocator_(false), - buf_(nullptr), - reserved_(0), - cur_(nullptr), - size_(0) {} - - DetachedBuffer(Allocator *allocator, bool own_allocator, uint8_t *buf, - size_t reserved, uint8_t *cur, size_t sz) - : allocator_(allocator), - own_allocator_(own_allocator), - buf_(buf), - reserved_(reserved), - cur_(cur), - size_(sz) {} - - DetachedBuffer(DetachedBuffer &&other) - : allocator_(other.allocator_), - own_allocator_(other.own_allocator_), - buf_(other.buf_), - reserved_(other.reserved_), - cur_(other.cur_), - size_(other.size_) { - other.reset(); - } - - DetachedBuffer &operator=(DetachedBuffer &&other) { - if (this == &other) return *this; - - destroy(); - - allocator_ = other.allocator_; - own_allocator_ = other.own_allocator_; - buf_ = other.buf_; - reserved_ = other.reserved_; - cur_ = other.cur_; - size_ = other.size_; - - other.reset(); - - return *this; - } - - ~DetachedBuffer() { destroy(); } - - const uint8_t *data() const { return cur_; } - - uint8_t *data() { return cur_; } - - size_t size() const { return size_; } - - // These may change access mode, leave these at end of public section - FLATBUFFERS_DELETE_FUNC(DetachedBuffer(const DetachedBuffer &other)); - FLATBUFFERS_DELETE_FUNC( - DetachedBuffer &operator=(const DetachedBuffer &other)); - - protected: - Allocator *allocator_; - bool own_allocator_; - uint8_t *buf_; - size_t reserved_; - uint8_t *cur_; - size_t size_; - - inline void destroy() { - if (buf_) Deallocate(allocator_, buf_, reserved_); - if (own_allocator_ && allocator_) { delete allocator_; } - reset(); - } - - inline void reset() { - allocator_ = nullptr; - own_allocator_ = false; - buf_ = nullptr; - reserved_ = 0; - cur_ = nullptr; - size_ = 0; - } -}; - -} // namespace flatbuffers - -#endif // FLATBUFFERS_DETACHED_BUFFER_H_ diff --git a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/flatbuffer_builder.h b/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/flatbuffer_builder.h deleted file mode 100644 index 8be4efbe..00000000 --- a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/flatbuffer_builder.h +++ /dev/null @@ -1,1187 +0,0 @@ -/* - * Copyright 2021 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FLATBUFFERS_FLATBUFFER_BUILDER_H_ -#define FLATBUFFERS_FLATBUFFER_BUILDER_H_ - -#include - -#include "flatbuffers/allocator.h" -#include "flatbuffers/array.h" -#include "flatbuffers/base.h" -#include "flatbuffers/buffer_ref.h" -#include "flatbuffers/default_allocator.h" -#include "flatbuffers/detached_buffer.h" -#include "flatbuffers/stl_emulation.h" -#include "flatbuffers/string.h" -#include "flatbuffers/struct.h" -#include "flatbuffers/table.h" -#include "flatbuffers/vector.h" -#include "flatbuffers/vector_downward.h" -#include "flatbuffers/verifier.h" - -namespace flatbuffers { - -// Converts a Field ID to a virtual table offset. -inline voffset_t FieldIndexToOffset(voffset_t field_id) { - // Should correspond to what EndTable() below builds up. - const int fixed_fields = 2; // Vtable size and Object Size. - return static_cast((field_id + fixed_fields) * sizeof(voffset_t)); -} - -template -const T *data(const std::vector &v) { - // Eventually the returned pointer gets passed down to memcpy, so - // we need it to be non-null to avoid undefined behavior. - static uint8_t t; - return v.empty() ? reinterpret_cast(&t) : &v.front(); -} -template T *data(std::vector &v) { - // Eventually the returned pointer gets passed down to memcpy, so - // we need it to be non-null to avoid undefined behavior. - static uint8_t t; - return v.empty() ? reinterpret_cast(&t) : &v.front(); -} - -/// @addtogroup flatbuffers_cpp_api -/// @{ -/// @class FlatBufferBuilder -/// @brief Helper class to hold data needed in creation of a FlatBuffer. -/// To serialize data, you typically call one of the `Create*()` functions in -/// the generated code, which in turn call a sequence of `StartTable`/ -/// `PushElement`/`AddElement`/`EndTable`, or the builtin `CreateString`/ -/// `CreateVector` functions. Do this is depth-first order to build up a tree to -/// the root. `Finish()` wraps up the buffer ready for transport. -class FlatBufferBuilder { - public: - /// @brief Default constructor for FlatBufferBuilder. - /// @param[in] initial_size The initial size of the buffer, in bytes. Defaults - /// to `1024`. - /// @param[in] allocator An `Allocator` to use. If null will use - /// `DefaultAllocator`. - /// @param[in] own_allocator Whether the builder/vector should own the - /// allocator. Defaults to / `false`. - /// @param[in] buffer_minalign Force the buffer to be aligned to the given - /// minimum alignment upon reallocation. Only needed if you intend to store - /// types with custom alignment AND you wish to read the buffer in-place - /// directly after creation. - explicit FlatBufferBuilder( - size_t initial_size = 1024, Allocator *allocator = nullptr, - bool own_allocator = false, - size_t buffer_minalign = AlignOf()) - : buf_(initial_size, allocator, own_allocator, buffer_minalign), - num_field_loc(0), - max_voffset_(0), - nested(false), - finished(false), - minalign_(1), - force_defaults_(false), - dedup_vtables_(true), - string_pool(nullptr) { - EndianCheck(); - } - - /// @brief Move constructor for FlatBufferBuilder. - FlatBufferBuilder(FlatBufferBuilder &&other) - : buf_(1024, nullptr, false, AlignOf()), - num_field_loc(0), - max_voffset_(0), - nested(false), - finished(false), - minalign_(1), - force_defaults_(false), - dedup_vtables_(true), - string_pool(nullptr) { - EndianCheck(); - // Default construct and swap idiom. - // Lack of delegating constructors in vs2010 makes it more verbose than - // needed. - Swap(other); - } - - /// @brief Move assignment operator for FlatBufferBuilder. - FlatBufferBuilder &operator=(FlatBufferBuilder &&other) { - // Move construct a temporary and swap idiom - FlatBufferBuilder temp(std::move(other)); - Swap(temp); - return *this; - } - - void Swap(FlatBufferBuilder &other) { - using std::swap; - buf_.swap(other.buf_); - swap(num_field_loc, other.num_field_loc); - swap(max_voffset_, other.max_voffset_); - swap(nested, other.nested); - swap(finished, other.finished); - swap(minalign_, other.minalign_); - swap(force_defaults_, other.force_defaults_); - swap(dedup_vtables_, other.dedup_vtables_); - swap(string_pool, other.string_pool); - } - - ~FlatBufferBuilder() { - if (string_pool) delete string_pool; - } - - void Reset() { - Clear(); // clear builder state - buf_.reset(); // deallocate buffer - } - - /// @brief Reset all the state in this FlatBufferBuilder so it can be reused - /// to construct another buffer. - void Clear() { - ClearOffsets(); - buf_.clear(); - nested = false; - finished = false; - minalign_ = 1; - if (string_pool) string_pool->clear(); - } - - /// @brief The current size of the serialized buffer, counting from the end. - /// @return Returns an `uoffset_t` with the current size of the buffer. - uoffset_t GetSize() const { return buf_.size(); } - - /// @brief Get the serialized buffer (after you call `Finish()`). - /// @return Returns an `uint8_t` pointer to the FlatBuffer data inside the - /// buffer. - uint8_t *GetBufferPointer() const { - Finished(); - return buf_.data(); - } - - /// @brief Get the serialized buffer (after you call `Finish()`) as a span. - /// @return Returns a constructed flatbuffers::span that is a view over the - /// FlatBuffer data inside the buffer. - flatbuffers::span GetBufferSpan() const { - Finished(); - return flatbuffers::span(buf_.data(), buf_.size()); - } - - /// @brief Get a pointer to an unfinished buffer. - /// @return Returns a `uint8_t` pointer to the unfinished buffer. - uint8_t *GetCurrentBufferPointer() const { return buf_.data(); } - - /// @brief Get the released pointer to the serialized buffer. - /// @warning Do NOT attempt to use this FlatBufferBuilder afterwards! - /// @return A `FlatBuffer` that owns the buffer and its allocator and - /// behaves similar to a `unique_ptr` with a deleter. - FLATBUFFERS_ATTRIBUTE([[deprecated("use Release() instead")]]) - DetachedBuffer ReleaseBufferPointer() { - Finished(); - return buf_.release(); - } - - /// @brief Get the released DetachedBuffer. - /// @return A `DetachedBuffer` that owns the buffer and its allocator. - DetachedBuffer Release() { - Finished(); - return buf_.release(); - } - - /// @brief Get the released pointer to the serialized buffer. - /// @param size The size of the memory block containing - /// the serialized `FlatBuffer`. - /// @param offset The offset from the released pointer where the finished - /// `FlatBuffer` starts. - /// @return A raw pointer to the start of the memory block containing - /// the serialized `FlatBuffer`. - /// @remark If the allocator is owned, it gets deleted when the destructor is - /// called.. - uint8_t *ReleaseRaw(size_t &size, size_t &offset) { - Finished(); - return buf_.release_raw(size, offset); - } - - /// @brief get the minimum alignment this buffer needs to be accessed - /// properly. This is only known once all elements have been written (after - /// you call Finish()). You can use this information if you need to embed - /// a FlatBuffer in some other buffer, such that you can later read it - /// without first having to copy it into its own buffer. - size_t GetBufferMinAlignment() const { - Finished(); - return minalign_; - } - - /// @cond FLATBUFFERS_INTERNAL - void Finished() const { - // If you get this assert, you're attempting to get access a buffer - // which hasn't been finished yet. Be sure to call - // FlatBufferBuilder::Finish with your root table. - // If you really need to access an unfinished buffer, call - // GetCurrentBufferPointer instead. - FLATBUFFERS_ASSERT(finished); - } - /// @endcond - - /// @brief In order to save space, fields that are set to their default value - /// don't get serialized into the buffer. - /// @param[in] fd When set to `true`, always serializes default values that - /// are set. Optional fields which are not set explicitly, will still not be - /// serialized. - void ForceDefaults(bool fd) { force_defaults_ = fd; } - - /// @brief By default vtables are deduped in order to save space. - /// @param[in] dedup When set to `true`, dedup vtables. - void DedupVtables(bool dedup) { dedup_vtables_ = dedup; } - - /// @cond FLATBUFFERS_INTERNAL - void Pad(size_t num_bytes) { buf_.fill(num_bytes); } - - void TrackMinAlign(size_t elem_size) { - if (elem_size > minalign_) minalign_ = elem_size; - } - - void Align(size_t elem_size) { - TrackMinAlign(elem_size); - buf_.fill(PaddingBytes(buf_.size(), elem_size)); - } - - void PushFlatBuffer(const uint8_t *bytes, size_t size) { - PushBytes(bytes, size); - finished = true; - } - - void PushBytes(const uint8_t *bytes, size_t size) { buf_.push(bytes, size); } - - void PopBytes(size_t amount) { buf_.pop(amount); } - - template void AssertScalarT() { - // The code assumes power of 2 sizes and endian-swap-ability. - static_assert(flatbuffers::is_scalar::value, "T must be a scalar type"); - } - - // Write a single aligned scalar to the buffer - template uoffset_t PushElement(T element) { - AssertScalarT(); - Align(sizeof(T)); - buf_.push_small(EndianScalar(element)); - return GetSize(); - } - - template uoffset_t PushElement(Offset off) { - // Special case for offsets: see ReferTo below. - return PushElement(ReferTo(off.o)); - } - - // When writing fields, we track where they are, so we can create correct - // vtables later. - void TrackField(voffset_t field, uoffset_t off) { - FieldLoc fl = { off, field }; - buf_.scratch_push_small(fl); - num_field_loc++; - if (field > max_voffset_) { - max_voffset_ = field; - } - } - - // Like PushElement, but additionally tracks the field this represents. - template void AddElement(voffset_t field, T e, T def) { - // We don't serialize values equal to the default. - if (IsTheSameAs(e, def) && !force_defaults_) return; - TrackField(field, PushElement(e)); - } - - template void AddElement(voffset_t field, T e) { - TrackField(field, PushElement(e)); - } - - template void AddOffset(voffset_t field, Offset off) { - if (off.IsNull()) return; // Don't store. - AddElement(field, ReferTo(off.o), static_cast(0)); - } - - template void AddStruct(voffset_t field, const T *structptr) { - if (!structptr) return; // Default, don't store. - Align(AlignOf()); - buf_.push_small(*structptr); - TrackField(field, GetSize()); - } - - void AddStructOffset(voffset_t field, uoffset_t off) { - TrackField(field, off); - } - - // Offsets initially are relative to the end of the buffer (downwards). - // This function converts them to be relative to the current location - // in the buffer (when stored here), pointing upwards. - uoffset_t ReferTo(uoffset_t off) { - // Align to ensure GetSize() below is correct. - Align(sizeof(uoffset_t)); - // Offset must refer to something already in buffer. - const uoffset_t size = GetSize(); - FLATBUFFERS_ASSERT(off && off <= size); - return size - off + static_cast(sizeof(uoffset_t)); - } - - void NotNested() { - // If you hit this, you're trying to construct a Table/Vector/String - // during the construction of its parent table (between the MyTableBuilder - // and table.Finish(). - // Move the creation of these sub-objects to above the MyTableBuilder to - // not get this assert. - // Ignoring this assert may appear to work in simple cases, but the reason - // it is here is that storing objects in-line may cause vtable offsets - // to not fit anymore. It also leads to vtable duplication. - FLATBUFFERS_ASSERT(!nested); - // If you hit this, fields were added outside the scope of a table. - FLATBUFFERS_ASSERT(!num_field_loc); - } - - // From generated code (or from the parser), we call StartTable/EndTable - // with a sequence of AddElement calls in between. - uoffset_t StartTable() { - NotNested(); - nested = true; - return GetSize(); - } - - // This finishes one serialized object by generating the vtable if it's a - // table, comparing it against existing vtables, and writing the - // resulting vtable offset. - uoffset_t EndTable(uoffset_t start) { - // If you get this assert, a corresponding StartTable wasn't called. - FLATBUFFERS_ASSERT(nested); - // Write the vtable offset, which is the start of any Table. - // We fill it's value later. - auto vtableoffsetloc = PushElement(0); - // Write a vtable, which consists entirely of voffset_t elements. - // It starts with the number of offsets, followed by a type id, followed - // by the offsets themselves. In reverse: - // Include space for the last offset and ensure empty tables have a - // minimum size. - max_voffset_ = - (std::max)(static_cast(max_voffset_ + sizeof(voffset_t)), - FieldIndexToOffset(0)); - buf_.fill_big(max_voffset_); - auto table_object_size = vtableoffsetloc - start; - // Vtable use 16bit offsets. - FLATBUFFERS_ASSERT(table_object_size < 0x10000); - WriteScalar(buf_.data() + sizeof(voffset_t), - static_cast(table_object_size)); - WriteScalar(buf_.data(), max_voffset_); - // Write the offsets into the table - for (auto it = buf_.scratch_end() - num_field_loc * sizeof(FieldLoc); - it < buf_.scratch_end(); it += sizeof(FieldLoc)) { - auto field_location = reinterpret_cast(it); - auto pos = static_cast(vtableoffsetloc - field_location->off); - // If this asserts, it means you've set a field twice. - FLATBUFFERS_ASSERT( - !ReadScalar(buf_.data() + field_location->id)); - WriteScalar(buf_.data() + field_location->id, pos); - } - ClearOffsets(); - auto vt1 = reinterpret_cast(buf_.data()); - auto vt1_size = ReadScalar(vt1); - auto vt_use = GetSize(); - // See if we already have generated a vtable with this exact same - // layout before. If so, make it point to the old one, remove this one. - if (dedup_vtables_) { - for (auto it = buf_.scratch_data(); it < buf_.scratch_end(); - it += sizeof(uoffset_t)) { - auto vt_offset_ptr = reinterpret_cast(it); - auto vt2 = reinterpret_cast(buf_.data_at(*vt_offset_ptr)); - auto vt2_size = ReadScalar(vt2); - if (vt1_size != vt2_size || 0 != memcmp(vt2, vt1, vt1_size)) continue; - vt_use = *vt_offset_ptr; - buf_.pop(GetSize() - vtableoffsetloc); - break; - } - } - // If this is a new vtable, remember it. - if (vt_use == GetSize()) { buf_.scratch_push_small(vt_use); } - // Fill the vtable offset we created above. - // The offset points from the beginning of the object to where the - // vtable is stored. - // Offsets default direction is downward in memory for future format - // flexibility (storing all vtables at the start of the file). - WriteScalar(buf_.data_at(vtableoffsetloc), - static_cast(vt_use) - - static_cast(vtableoffsetloc)); - - nested = false; - return vtableoffsetloc; - } - - FLATBUFFERS_ATTRIBUTE([[deprecated("call the version above instead")]]) - uoffset_t EndTable(uoffset_t start, voffset_t /*numfields*/) { - return EndTable(start); - } - - // This checks a required field has been set in a given table that has - // just been constructed. - template void Required(Offset table, voffset_t field); - - uoffset_t StartStruct(size_t alignment) { - Align(alignment); - return GetSize(); - } - - uoffset_t EndStruct() { return GetSize(); } - - void ClearOffsets() { - buf_.scratch_pop(num_field_loc * sizeof(FieldLoc)); - num_field_loc = 0; - max_voffset_ = 0; - } - - // Aligns such that when "len" bytes are written, an object can be written - // after it with "alignment" without padding. - void PreAlign(size_t len, size_t alignment) { - TrackMinAlign(alignment); - buf_.fill(PaddingBytes(GetSize() + len, alignment)); - } - template void PreAlign(size_t len) { - AssertScalarT(); - PreAlign(len, sizeof(T)); - } - /// @endcond - - /// @brief Store a string in the buffer, which can contain any binary data. - /// @param[in] str A const char pointer to the data to be stored as a string. - /// @param[in] len The number of bytes that should be stored from `str`. - /// @return Returns the offset in the buffer where the string starts. - Offset CreateString(const char *str, size_t len) { - NotNested(); - PreAlign(len + 1); // Always 0-terminated. - buf_.fill(1); - PushBytes(reinterpret_cast(str), len); - PushElement(static_cast(len)); - return Offset(GetSize()); - } - - /// @brief Store a string in the buffer, which is null-terminated. - /// @param[in] str A const char pointer to a C-string to add to the buffer. - /// @return Returns the offset in the buffer where the string starts. - Offset CreateString(const char *str) { - return CreateString(str, strlen(str)); - } - - /// @brief Store a string in the buffer, which is null-terminated. - /// @param[in] str A char pointer to a C-string to add to the buffer. - /// @return Returns the offset in the buffer where the string starts. - Offset CreateString(char *str) { - return CreateString(str, strlen(str)); - } - - /// @brief Store a string in the buffer, which can contain any binary data. - /// @param[in] str A const reference to a std::string to store in the buffer. - /// @return Returns the offset in the buffer where the string starts. - Offset CreateString(const std::string &str) { - return CreateString(str.c_str(), str.length()); - } - - // clang-format off - #ifdef FLATBUFFERS_HAS_STRING_VIEW - /// @brief Store a string in the buffer, which can contain any binary data. - /// @param[in] str A const string_view to copy in to the buffer. - /// @return Returns the offset in the buffer where the string starts. - Offset CreateString(flatbuffers::string_view str) { - return CreateString(str.data(), str.size()); - } - #endif // FLATBUFFERS_HAS_STRING_VIEW - // clang-format on - - /// @brief Store a string in the buffer, which can contain any binary data. - /// @param[in] str A const pointer to a `String` struct to add to the buffer. - /// @return Returns the offset in the buffer where the string starts - Offset CreateString(const String *str) { - return str ? CreateString(str->c_str(), str->size()) : 0; - } - - /// @brief Store a string in the buffer, which can contain any binary data. - /// @param[in] str A const reference to a std::string like type with support - /// of T::c_str() and T::length() to store in the buffer. - /// @return Returns the offset in the buffer where the string starts. - template Offset CreateString(const T &str) { - return CreateString(str.c_str(), str.length()); - } - - /// @brief Store a string in the buffer, which can contain any binary data. - /// If a string with this exact contents has already been serialized before, - /// instead simply returns the offset of the existing string. This uses a map - /// stored on the heap, but only stores the numerical offsets. - /// @param[in] str A const char pointer to the data to be stored as a string. - /// @param[in] len The number of bytes that should be stored from `str`. - /// @return Returns the offset in the buffer where the string starts. - Offset CreateSharedString(const char *str, size_t len) { - FLATBUFFERS_ASSERT(FLATBUFFERS_GENERAL_HEAP_ALLOC_OK); - if (!string_pool) - string_pool = new StringOffsetMap(StringOffsetCompare(buf_)); - auto size_before_string = buf_.size(); - // Must first serialize the string, since the set is all offsets into - // buffer. - auto off = CreateString(str, len); - auto it = string_pool->find(off); - // If it exists we reuse existing serialized data! - if (it != string_pool->end()) { - // We can remove the string we serialized. - buf_.pop(buf_.size() - size_before_string); - return *it; - } - // Record this string for future use. - string_pool->insert(off); - return off; - } - -#ifdef FLATBUFFERS_HAS_STRING_VIEW - /// @brief Store a string in the buffer, which can contain any binary data. - /// If a string with this exact contents has already been serialized before, - /// instead simply returns the offset of the existing string. This uses a map - /// stored on the heap, but only stores the numerical offsets. - /// @param[in] str A const std::string_view to store in the buffer. - /// @return Returns the offset in the buffer where the string starts - Offset CreateSharedString(const flatbuffers::string_view str) { - return CreateSharedString(str.data(), str.size()); - } -#else - /// @brief Store a string in the buffer, which null-terminated. - /// If a string with this exact contents has already been serialized before, - /// instead simply returns the offset of the existing string. This uses a map - /// stored on the heap, but only stores the numerical offsets. - /// @param[in] str A const char pointer to a C-string to add to the buffer. - /// @return Returns the offset in the buffer where the string starts. - Offset CreateSharedString(const char *str) { - return CreateSharedString(str, strlen(str)); - } - - /// @brief Store a string in the buffer, which can contain any binary data. - /// If a string with this exact contents has already been serialized before, - /// instead simply returns the offset of the existing string. This uses a map - /// stored on the heap, but only stores the numerical offsets. - /// @param[in] str A const reference to a std::string to store in the buffer. - /// @return Returns the offset in the buffer where the string starts. - Offset CreateSharedString(const std::string &str) { - return CreateSharedString(str.c_str(), str.length()); - } -#endif - - /// @brief Store a string in the buffer, which can contain any binary data. - /// If a string with this exact contents has already been serialized before, - /// instead simply returns the offset of the existing string. This uses a map - /// stored on the heap, but only stores the numerical offsets. - /// @param[in] str A const pointer to a `String` struct to add to the buffer. - /// @return Returns the offset in the buffer where the string starts - Offset CreateSharedString(const String *str) { - return CreateSharedString(str->c_str(), str->size()); - } - - /// @cond FLATBUFFERS_INTERNAL - uoffset_t EndVector(size_t len) { - FLATBUFFERS_ASSERT(nested); // Hit if no corresponding StartVector. - nested = false; - return PushElement(static_cast(len)); - } - - void StartVector(size_t len, size_t elemsize) { - NotNested(); - nested = true; - PreAlign(len * elemsize); - PreAlign(len * elemsize, elemsize); // Just in case elemsize > uoffset_t. - } - - // Call this right before StartVector/CreateVector if you want to force the - // alignment to be something different than what the element size would - // normally dictate. - // This is useful when storing a nested_flatbuffer in a vector of bytes, - // or when storing SIMD floats, etc. - void ForceVectorAlignment(size_t len, size_t elemsize, size_t alignment) { - FLATBUFFERS_ASSERT(VerifyAlignmentRequirements(alignment)); - PreAlign(len * elemsize, alignment); - } - - // Similar to ForceVectorAlignment but for String fields. - void ForceStringAlignment(size_t len, size_t alignment) { - FLATBUFFERS_ASSERT(VerifyAlignmentRequirements(alignment)); - PreAlign((len + 1) * sizeof(char), alignment); - } - - /// @endcond - - /// @brief Serialize an array into a FlatBuffer `vector`. - /// @tparam T The data type of the array elements. - /// @param[in] v A pointer to the array of type `T` to serialize into the - /// buffer as a `vector`. - /// @param[in] len The number of elements to serialize. - /// @return Returns a typed `Offset` into the serialized data indicating - /// where the vector is stored. - template Offset> CreateVector(const T *v, size_t len) { - // If this assert hits, you're specifying a template argument that is - // causing the wrong overload to be selected, remove it. - AssertScalarT(); - StartVector(len, sizeof(T)); - if (len == 0) { return Offset>(EndVector(len)); } - // clang-format off - #if FLATBUFFERS_LITTLEENDIAN - PushBytes(reinterpret_cast(v), len * sizeof(T)); - #else - if (sizeof(T) == 1) { - PushBytes(reinterpret_cast(v), len); - } else { - for (auto i = len; i > 0; ) { - PushElement(v[--i]); - } - } - #endif - // clang-format on - return Offset>(EndVector(len)); - } - - template - Offset>> CreateVector(const Offset *v, size_t len) { - StartVector(len, sizeof(Offset)); - for (auto i = len; i > 0;) { PushElement(v[--i]); } - return Offset>>(EndVector(len)); - } - - /// @brief Serialize a `std::vector` into a FlatBuffer `vector`. - /// @tparam T The data type of the `std::vector` elements. - /// @param v A const reference to the `std::vector` to serialize into the - /// buffer as a `vector`. - /// @return Returns a typed `Offset` into the serialized data indicating - /// where the vector is stored. - template - Offset> CreateVector(const std::vector &v) { - return CreateVector(data(v), v.size()); - } - - // vector may be implemented using a bit-set, so we can't access it as - // an array. Instead, read elements manually. - // Background: https://isocpp.org/blog/2012/11/on-vectorbool - Offset> CreateVector(const std::vector &v) { - StartVector(v.size(), sizeof(uint8_t)); - for (auto i = v.size(); i > 0;) { - PushElement(static_cast(v[--i])); - } - return Offset>(EndVector(v.size())); - } - - /// @brief Serialize values returned by a function into a FlatBuffer `vector`. - /// This is a convenience function that takes care of iteration for you. - /// @tparam T The data type of the `std::vector` elements. - /// @param f A function that takes the current iteration 0..vector_size-1 and - /// returns any type that you can construct a FlatBuffers vector out of. - /// @return Returns a typed `Offset` into the serialized data indicating - /// where the vector is stored. - template - Offset> CreateVector(size_t vector_size, - const std::function &f) { - FLATBUFFERS_ASSERT(FLATBUFFERS_GENERAL_HEAP_ALLOC_OK); - std::vector elems(vector_size); - for (size_t i = 0; i < vector_size; i++) elems[i] = f(i); - return CreateVector(elems); - } - - /// @brief Serialize values returned by a function into a FlatBuffer `vector`. - /// This is a convenience function that takes care of iteration for you. This - /// uses a vector stored on the heap to store the intermediate results of the - /// iteration. - /// @tparam T The data type of the `std::vector` elements. - /// @param f A function that takes the current iteration 0..vector_size-1, - /// and the state parameter returning any type that you can construct a - /// FlatBuffers vector out of. - /// @param state State passed to f. - /// @return Returns a typed `Offset` into the serialized data indicating - /// where the vector is stored. - template - Offset> CreateVector(size_t vector_size, F f, S *state) { - FLATBUFFERS_ASSERT(FLATBUFFERS_GENERAL_HEAP_ALLOC_OK); - std::vector elems(vector_size); - for (size_t i = 0; i < vector_size; i++) elems[i] = f(i, state); - return CreateVector(elems); - } - - /// @brief Serialize a `std::vector` into a FlatBuffer `vector`. - /// This is a convenience function for a common case. - /// @param v A const reference to the `std::vector` to serialize into the - /// buffer as a `vector`. - /// @return Returns a typed `Offset` into the serialized data indicating - /// where the vector is stored. - template - Offset>> CreateVectorOfStrings( - const std::vector &v) { - return CreateVectorOfStrings(v.cbegin(), v.cend()); - } - - /// @brief Serialize a collection of Strings into a FlatBuffer `vector`. - /// This is a convenience function for a common case. - /// @param begin The begining iterator of the collection - /// @param end The ending iterator of the collection - /// @return Returns a typed `Offset` into the serialized data indicating - /// where the vector is stored. - template - Offset>> CreateVectorOfStrings(It begin, It end) { - auto size = std::distance(begin, end); - auto scratch_buffer_usage = size * sizeof(Offset); - // If there is not enough space to store the offsets, there definitely won't - // be enough space to store all the strings. So ensuring space for the - // scratch region is OK, for it it fails, it would have failed later. - buf_.ensure_space(scratch_buffer_usage); - for (auto it = begin; it != end; ++it) { - buf_.scratch_push_small(CreateString(*it)); - } - StartVector(size, sizeof(Offset)); - for (auto i = 1; i <= size; i++) { - // Note we re-evaluate the buf location each iteration to account for any - // underlying buffer resizing that may occur. - PushElement(*reinterpret_cast *>( - buf_.scratch_end() - i * sizeof(Offset))); - } - buf_.scratch_pop(scratch_buffer_usage); - return Offset>>(EndVector(size)); - } - - /// @brief Serialize an array of structs into a FlatBuffer `vector`. - /// @tparam T The data type of the struct array elements. - /// @param[in] v A pointer to the array of type `T` to serialize into the - /// buffer as a `vector`. - /// @param[in] len The number of elements to serialize. - /// @return Returns a typed `Offset` into the serialized data indicating - /// where the vector is stored. - template - Offset> CreateVectorOfStructs(const T *v, size_t len) { - StartVector(len * sizeof(T) / AlignOf(), AlignOf()); - if (len > 0) { - PushBytes(reinterpret_cast(v), sizeof(T) * len); - } - return Offset>(EndVector(len)); - } - - /// @brief Serialize an array of native structs into a FlatBuffer `vector`. - /// @tparam T The data type of the struct array elements. - /// @tparam S The data type of the native struct array elements. - /// @param[in] v A pointer to the array of type `S` to serialize into the - /// buffer as a `vector`. - /// @param[in] len The number of elements to serialize. - /// @param[in] pack_func Pointer to a function to convert the native struct - /// to the FlatBuffer struct. - /// @return Returns a typed `Offset` into the serialized data indicating - /// where the vector is stored. - template - Offset> CreateVectorOfNativeStructs( - const S *v, size_t len, T (*const pack_func)(const S &)) { - FLATBUFFERS_ASSERT(pack_func); - auto structs = StartVectorOfStructs(len); - for (size_t i = 0; i < len; i++) { structs[i] = pack_func(v[i]); } - return EndVectorOfStructs(len); - } - - /// @brief Serialize an array of native structs into a FlatBuffer `vector`. - /// @tparam T The data type of the struct array elements. - /// @tparam S The data type of the native struct array elements. - /// @param[in] v A pointer to the array of type `S` to serialize into the - /// buffer as a `vector`. - /// @param[in] len The number of elements to serialize. - /// @return Returns a typed `Offset` into the serialized data indicating - /// where the vector is stored. - template - Offset> CreateVectorOfNativeStructs(const S *v, - size_t len) { - extern T Pack(const S &); - return CreateVectorOfNativeStructs(v, len, Pack); - } - - /// @brief Serialize an array of structs into a FlatBuffer `vector`. - /// @tparam T The data type of the struct array elements. - /// @param[in] filler A function that takes the current iteration - /// 0..vector_size-1 and a pointer to the struct that must be filled. - /// @return Returns a typed `Offset` into the serialized data indicating - /// where the vector is stored. - /// This is mostly useful when flatbuffers are generated with mutation - /// accessors. - template - Offset> CreateVectorOfStructs( - size_t vector_size, const std::function &filler) { - T *structs = StartVectorOfStructs(vector_size); - for (size_t i = 0; i < vector_size; i++) { - filler(i, structs); - structs++; - } - return EndVectorOfStructs(vector_size); - } - - /// @brief Serialize an array of structs into a FlatBuffer `vector`. - /// @tparam T The data type of the struct array elements. - /// @param[in] f A function that takes the current iteration 0..vector_size-1, - /// a pointer to the struct that must be filled and the state argument. - /// @param[in] state Arbitrary state to pass to f. - /// @return Returns a typed `Offset` into the serialized data indicating - /// where the vector is stored. - /// This is mostly useful when flatbuffers are generated with mutation - /// accessors. - template - Offset> CreateVectorOfStructs(size_t vector_size, F f, - S *state) { - T *structs = StartVectorOfStructs(vector_size); - for (size_t i = 0; i < vector_size; i++) { - f(i, structs, state); - structs++; - } - return EndVectorOfStructs(vector_size); - } - - /// @brief Serialize a `std::vector` of structs into a FlatBuffer `vector`. - /// @tparam T The data type of the `std::vector` struct elements. - /// @param[in] v A const reference to the `std::vector` of structs to - /// serialize into the buffer as a `vector`. - /// @return Returns a typed `Offset` into the serialized data indicating - /// where the vector is stored. - template - Offset> CreateVectorOfStructs( - const std::vector &v) { - return CreateVectorOfStructs(data(v), v.size()); - } - - /// @brief Serialize a `std::vector` of native structs into a FlatBuffer - /// `vector`. - /// @tparam T The data type of the `std::vector` struct elements. - /// @tparam S The data type of the `std::vector` native struct elements. - /// @param[in] v A const reference to the `std::vector` of structs to - /// serialize into the buffer as a `vector`. - /// @param[in] pack_func Pointer to a function to convert the native struct - /// to the FlatBuffer struct. - /// @return Returns a typed `Offset` into the serialized data indicating - /// where the vector is stored. - template - Offset> CreateVectorOfNativeStructs( - const std::vector &v, T (*const pack_func)(const S &)) { - return CreateVectorOfNativeStructs(data(v), v.size(), pack_func); - } - - /// @brief Serialize a `std::vector` of native structs into a FlatBuffer - /// `vector`. - /// @tparam T The data type of the `std::vector` struct elements. - /// @tparam S The data type of the `std::vector` native struct elements. - /// @param[in] v A const reference to the `std::vector` of structs to - /// serialize into the buffer as a `vector`. - /// @return Returns a typed `Offset` into the serialized data indicating - /// where the vector is stored. - template - Offset> CreateVectorOfNativeStructs( - const std::vector &v) { - return CreateVectorOfNativeStructs(data(v), v.size()); - } - - /// @cond FLATBUFFERS_INTERNAL - template struct StructKeyComparator { - bool operator()(const T &a, const T &b) const { - return a.KeyCompareLessThan(&b); - } - }; - /// @endcond - - /// @brief Serialize a `std::vector` of structs into a FlatBuffer `vector` - /// in sorted order. - /// @tparam T The data type of the `std::vector` struct elements. - /// @param[in] v A const reference to the `std::vector` of structs to - /// serialize into the buffer as a `vector`. - /// @return Returns a typed `Offset` into the serialized data indicating - /// where the vector is stored. - template - Offset> CreateVectorOfSortedStructs( - std::vector *v) { - return CreateVectorOfSortedStructs(data(*v), v->size()); - } - - /// @brief Serialize a `std::vector` of native structs into a FlatBuffer - /// `vector` in sorted order. - /// @tparam T The data type of the `std::vector` struct elements. - /// @tparam S The data type of the `std::vector` native struct elements. - /// @param[in] v A const reference to the `std::vector` of structs to - /// serialize into the buffer as a `vector`. - /// @return Returns a typed `Offset` into the serialized data indicating - /// where the vector is stored. - template - Offset> CreateVectorOfSortedNativeStructs( - std::vector *v) { - return CreateVectorOfSortedNativeStructs(data(*v), v->size()); - } - - /// @brief Serialize an array of structs into a FlatBuffer `vector` in sorted - /// order. - /// @tparam T The data type of the struct array elements. - /// @param[in] v A pointer to the array of type `T` to serialize into the - /// buffer as a `vector`. - /// @param[in] len The number of elements to serialize. - /// @return Returns a typed `Offset` into the serialized data indicating - /// where the vector is stored. - template - Offset> CreateVectorOfSortedStructs(T *v, size_t len) { - std::sort(v, v + len, StructKeyComparator()); - return CreateVectorOfStructs(v, len); - } - - /// @brief Serialize an array of native structs into a FlatBuffer `vector` in - /// sorted order. - /// @tparam T The data type of the struct array elements. - /// @tparam S The data type of the native struct array elements. - /// @param[in] v A pointer to the array of type `S` to serialize into the - /// buffer as a `vector`. - /// @param[in] len The number of elements to serialize. - /// @return Returns a typed `Offset` into the serialized data indicating - /// where the vector is stored. - template - Offset> CreateVectorOfSortedNativeStructs(S *v, - size_t len) { - extern T Pack(const S &); - auto structs = StartVectorOfStructs(len); - for (size_t i = 0; i < len; i++) { structs[i] = Pack(v[i]); } - std::sort(structs, structs + len, StructKeyComparator()); - return EndVectorOfStructs(len); - } - - /// @cond FLATBUFFERS_INTERNAL - template struct TableKeyComparator { - TableKeyComparator(vector_downward &buf) : buf_(buf) {} - TableKeyComparator(const TableKeyComparator &other) : buf_(other.buf_) {} - bool operator()(const Offset &a, const Offset &b) const { - auto table_a = reinterpret_cast(buf_.data_at(a.o)); - auto table_b = reinterpret_cast(buf_.data_at(b.o)); - return table_a->KeyCompareLessThan(table_b); - } - vector_downward &buf_; - - private: - FLATBUFFERS_DELETE_FUNC( - TableKeyComparator &operator=(const TableKeyComparator &other)); - }; - /// @endcond - - /// @brief Serialize an array of `table` offsets as a `vector` in the buffer - /// in sorted order. - /// @tparam T The data type that the offset refers to. - /// @param[in] v An array of type `Offset` that contains the `table` - /// offsets to store in the buffer in sorted order. - /// @param[in] len The number of elements to store in the `vector`. - /// @return Returns a typed `Offset` into the serialized data indicating - /// where the vector is stored. - template - Offset>> CreateVectorOfSortedTables(Offset *v, - size_t len) { - std::sort(v, v + len, TableKeyComparator(buf_)); - return CreateVector(v, len); - } - - /// @brief Serialize an array of `table` offsets as a `vector` in the buffer - /// in sorted order. - /// @tparam T The data type that the offset refers to. - /// @param[in] v An array of type `Offset` that contains the `table` - /// offsets to store in the buffer in sorted order. - /// @return Returns a typed `Offset` into the serialized data indicating - /// where the vector is stored. - template - Offset>> CreateVectorOfSortedTables( - std::vector, Alloc> *v) { - return CreateVectorOfSortedTables(data(*v), v->size()); - } - - /// @brief Specialized version of `CreateVector` for non-copying use cases. - /// Write the data any time later to the returned buffer pointer `buf`. - /// @param[in] len The number of elements to store in the `vector`. - /// @param[in] elemsize The size of each element in the `vector`. - /// @param[out] buf A pointer to a `uint8_t` pointer that can be - /// written to at a later time to serialize the data into a `vector` - /// in the buffer. - uoffset_t CreateUninitializedVector(size_t len, size_t elemsize, - uint8_t **buf) { - NotNested(); - StartVector(len, elemsize); - buf_.make_space(len * elemsize); - auto vec_start = GetSize(); - auto vec_end = EndVector(len); - *buf = buf_.data_at(vec_start); - return vec_end; - } - - /// @brief Specialized version of `CreateVector` for non-copying use cases. - /// Write the data any time later to the returned buffer pointer `buf`. - /// @tparam T The data type of the data that will be stored in the buffer - /// as a `vector`. - /// @param[in] len The number of elements to store in the `vector`. - /// @param[out] buf A pointer to a pointer of type `T` that can be - /// written to at a later time to serialize the data into a `vector` - /// in the buffer. - template - Offset> CreateUninitializedVector(size_t len, T **buf) { - AssertScalarT(); - return CreateUninitializedVector(len, sizeof(T), - reinterpret_cast(buf)); - } - - template - Offset> CreateUninitializedVectorOfStructs(size_t len, - T **buf) { - return CreateUninitializedVector(len, sizeof(T), - reinterpret_cast(buf)); - } - - // @brief Create a vector of scalar type T given as input a vector of scalar - // type U, useful with e.g. pre "enum class" enums, or any existing scalar - // data of the wrong type. - template - Offset> CreateVectorScalarCast(const U *v, size_t len) { - AssertScalarT(); - AssertScalarT(); - StartVector(len, sizeof(T)); - for (auto i = len; i > 0;) { PushElement(static_cast(v[--i])); } - return Offset>(EndVector(len)); - } - - /// @brief Write a struct by itself, typically to be part of a union. - template Offset CreateStruct(const T &structobj) { - NotNested(); - Align(AlignOf()); - buf_.push_small(structobj); - return Offset(GetSize()); - } - - /// @brief Finish serializing a buffer by writing the root offset. - /// @param[in] file_identifier If a `file_identifier` is given, the buffer - /// will be prefixed with a standard FlatBuffers file header. - template - void Finish(Offset root, const char *file_identifier = nullptr) { - Finish(root.o, file_identifier, false); - } - - /// @brief Finish a buffer with a 32 bit size field pre-fixed (size of the - /// buffer following the size field). These buffers are NOT compatible - /// with standard buffers created by Finish, i.e. you can't call GetRoot - /// on them, you have to use GetSizePrefixedRoot instead. - /// All >32 bit quantities in this buffer will be aligned when the whole - /// size pre-fixed buffer is aligned. - /// These kinds of buffers are useful for creating a stream of FlatBuffers. - template - void FinishSizePrefixed(Offset root, - const char *file_identifier = nullptr) { - Finish(root.o, file_identifier, true); - } - - void SwapBufAllocator(FlatBufferBuilder &other) { - buf_.swap_allocator(other.buf_); - } - - /// @brief The length of a FlatBuffer file header. - static const size_t kFileIdentifierLength = - ::flatbuffers::kFileIdentifierLength; - - protected: - // You shouldn't really be copying instances of this class. - FlatBufferBuilder(const FlatBufferBuilder &); - FlatBufferBuilder &operator=(const FlatBufferBuilder &); - - void Finish(uoffset_t root, const char *file_identifier, bool size_prefix) { - NotNested(); - buf_.clear_scratch(); - // This will cause the whole buffer to be aligned. - PreAlign((size_prefix ? sizeof(uoffset_t) : 0) + sizeof(uoffset_t) + - (file_identifier ? kFileIdentifierLength : 0), - minalign_); - if (file_identifier) { - FLATBUFFERS_ASSERT(strlen(file_identifier) == kFileIdentifierLength); - PushBytes(reinterpret_cast(file_identifier), - kFileIdentifierLength); - } - PushElement(ReferTo(root)); // Location of root. - if (size_prefix) { PushElement(GetSize()); } - finished = true; - } - - struct FieldLoc { - uoffset_t off; - voffset_t id; - }; - - vector_downward buf_; - - // Accumulating offsets of table members while it is being built. - // We store these in the scratch pad of buf_, after the vtable offsets. - uoffset_t num_field_loc; - // Track how much of the vtable is in use, so we can output the most compact - // possible vtable. - voffset_t max_voffset_; - - // Ensure objects are not nested. - bool nested; - - // Ensure the buffer is finished before it is being accessed. - bool finished; - - size_t minalign_; - - bool force_defaults_; // Serialize values equal to their defaults anyway. - - bool dedup_vtables_; - - struct StringOffsetCompare { - StringOffsetCompare(const vector_downward &buf) : buf_(&buf) {} - bool operator()(const Offset &a, const Offset &b) const { - auto stra = reinterpret_cast(buf_->data_at(a.o)); - auto strb = reinterpret_cast(buf_->data_at(b.o)); - return StringLessThan(stra->data(), stra->size(), strb->data(), - strb->size()); - } - const vector_downward *buf_; - }; - - // For use with CreateSharedString. Instantiated on first use only. - typedef std::set, StringOffsetCompare> StringOffsetMap; - StringOffsetMap *string_pool; - - private: - // Allocates space for a vector of structures. - // Must be completed with EndVectorOfStructs(). - template T *StartVectorOfStructs(size_t vector_size) { - StartVector(vector_size * sizeof(T) / AlignOf(), AlignOf()); - return reinterpret_cast(buf_.make_space(vector_size * sizeof(T))); - } - - // End the vector of structures in the flatbuffers. - // Vector should have previously be started with StartVectorOfStructs(). - template - Offset> EndVectorOfStructs(size_t vector_size) { - return Offset>(EndVector(vector_size)); - } -}; -/// @} - -/// Helpers to get a typed pointer to objects that are currently being built. -/// @warning Creating new objects will lead to reallocations and invalidates -/// the pointer! -template -T *GetMutableTemporaryPointer(FlatBufferBuilder &fbb, Offset offset) { - return reinterpret_cast(fbb.GetCurrentBufferPointer() + fbb.GetSize() - - offset.o); -} - -template -const T *GetTemporaryPointer(FlatBufferBuilder &fbb, Offset offset) { - return GetMutableTemporaryPointer(fbb, offset); -} - -template -void FlatBufferBuilder::Required(Offset table, voffset_t field) { - auto table_ptr = reinterpret_cast(buf_.data_at(table.o)); - bool ok = table_ptr->GetOptionalFieldOffset(field) != 0; - // If this fails, the caller will show what field needs to be set. - FLATBUFFERS_ASSERT(ok); - (void)ok; -} - -} // namespace flatbuffers - -#endif // FLATBUFFERS_VECTOR_DOWNWARD_H_ diff --git a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/flatbuffers.h b/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/flatbuffers.h deleted file mode 100644 index c903d646..00000000 --- a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/flatbuffers.h +++ /dev/null @@ -1,284 +0,0 @@ -/* - * Copyright 2014 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FLATBUFFERS_H_ -#define FLATBUFFERS_H_ - -// TODO: These includes are for mitigating the pains of users editing their -// source because they relied on flatbuffers.h to include everything for them. -#include "flatbuffers/array.h" -#include "flatbuffers/base.h" -#include "flatbuffers/buffer.h" -#include "flatbuffers/buffer_ref.h" -#include "flatbuffers/detached_buffer.h" -#include "flatbuffers/flatbuffer_builder.h" -#include "flatbuffers/stl_emulation.h" -#include "flatbuffers/string.h" -#include "flatbuffers/struct.h" -#include "flatbuffers/table.h" -#include "flatbuffers/vector.h" -#include "flatbuffers/vector_downward.h" -#include "flatbuffers/verifier.h" - -namespace flatbuffers { - -/// @brief This can compute the start of a FlatBuffer from a root pointer, i.e. -/// it is the opposite transformation of GetRoot(). -/// This may be useful if you want to pass on a root and have the recipient -/// delete the buffer afterwards. -inline const uint8_t *GetBufferStartFromRootPointer(const void *root) { - auto table = reinterpret_cast(root); - auto vtable = table->GetVTable(); - // Either the vtable is before the root or after the root. - auto start = (std::min)(vtable, reinterpret_cast(root)); - // Align to at least sizeof(uoffset_t). - start = reinterpret_cast(reinterpret_cast(start) & - ~(sizeof(uoffset_t) - 1)); - // Additionally, there may be a file_identifier in the buffer, and the root - // offset. The buffer may have been aligned to any size between - // sizeof(uoffset_t) and FLATBUFFERS_MAX_ALIGNMENT (see "force_align"). - // Sadly, the exact alignment is only known when constructing the buffer, - // since it depends on the presence of values with said alignment properties. - // So instead, we simply look at the next uoffset_t values (root, - // file_identifier, and alignment padding) to see which points to the root. - // None of the other values can "impersonate" the root since they will either - // be 0 or four ASCII characters. - static_assert(flatbuffers::kFileIdentifierLength == sizeof(uoffset_t), - "file_identifier is assumed to be the same size as uoffset_t"); - for (auto possible_roots = FLATBUFFERS_MAX_ALIGNMENT / sizeof(uoffset_t) + 1; - possible_roots; possible_roots--) { - start -= sizeof(uoffset_t); - if (ReadScalar(start) + start == - reinterpret_cast(root)) - return start; - } - // We didn't find the root, either the "root" passed isn't really a root, - // or the buffer is corrupt. - // Assert, because calling this function with bad data may cause reads - // outside of buffer boundaries. - FLATBUFFERS_ASSERT(false); - return nullptr; -} - -/// @brief This return the prefixed size of a FlatBuffer. -inline uoffset_t GetPrefixedSize(const uint8_t *buf) { - return ReadScalar(buf); -} - -// Base class for native objects (FlatBuffer data de-serialized into native -// C++ data structures). -// Contains no functionality, purely documentative. -struct NativeTable {}; - -/// @brief Function types to be used with resolving hashes into objects and -/// back again. The resolver gets a pointer to a field inside an object API -/// object that is of the type specified in the schema using the attribute -/// `cpp_type` (it is thus important whatever you write to this address -/// matches that type). The value of this field is initially null, so you -/// may choose to implement a delayed binding lookup using this function -/// if you wish. The resolver does the opposite lookup, for when the object -/// is being serialized again. -typedef uint64_t hash_value_t; -typedef std::function - resolver_function_t; -typedef std::function rehasher_function_t; - -// Helper function to test if a field is present, using any of the field -// enums in the generated code. -// `table` must be a generated table type. Since this is a template parameter, -// this is not typechecked to be a subclass of Table, so beware! -// Note: this function will return false for fields equal to the default -// value, since they're not stored in the buffer (unless force_defaults was -// used). -template -bool IsFieldPresent(const T *table, typename T::FlatBuffersVTableOffset field) { - // Cast, since Table is a private baseclass of any table types. - return reinterpret_cast(table)->CheckField( - static_cast(field)); -} - -// Utility function for reverse lookups on the EnumNames*() functions -// (in the generated C++ code) -// names must be NULL terminated. -inline int LookupEnum(const char **names, const char *name) { - for (const char **p = names; *p; p++) - if (!strcmp(*p, name)) return static_cast(p - names); - return -1; -} - -// These macros allow us to layout a struct with a guarantee that they'll end -// up looking the same on different compilers and platforms. -// It does this by disallowing the compiler to do any padding, and then -// does padding itself by inserting extra padding fields that make every -// element aligned to its own size. -// Additionally, it manually sets the alignment of the struct as a whole, -// which is typically its largest element, or a custom size set in the schema -// by the force_align attribute. -// These are used in the generated code only. - -// clang-format off -#if defined(_MSC_VER) - #define FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(alignment) \ - __pragma(pack(1)) \ - struct __declspec(align(alignment)) - #define FLATBUFFERS_STRUCT_END(name, size) \ - __pragma(pack()) \ - static_assert(sizeof(name) == size, "compiler breaks packing rules") -#elif defined(__GNUC__) || defined(__clang__) || defined(__ICCARM__) - #define FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(alignment) \ - _Pragma("pack(1)") \ - struct __attribute__((aligned(alignment))) - #define FLATBUFFERS_STRUCT_END(name, size) \ - _Pragma("pack()") \ - static_assert(sizeof(name) == size, "compiler breaks packing rules") -#else - #error Unknown compiler, please define structure alignment macros -#endif -// clang-format on - -// Minimal reflection via code generation. -// Besides full-fat reflection (see reflection.h) and parsing/printing by -// loading schemas (see idl.h), we can also have code generation for minimal -// reflection data which allows pretty-printing and other uses without needing -// a schema or a parser. -// Generate code with --reflect-types (types only) or --reflect-names (names -// also) to enable. -// See minireflect.h for utilities using this functionality. - -// These types are organized slightly differently as the ones in idl.h. -enum SequenceType { ST_TABLE, ST_STRUCT, ST_UNION, ST_ENUM }; - -// Scalars have the same order as in idl.h -// clang-format off -#define FLATBUFFERS_GEN_ELEMENTARY_TYPES(ET) \ - ET(ET_UTYPE) \ - ET(ET_BOOL) \ - ET(ET_CHAR) \ - ET(ET_UCHAR) \ - ET(ET_SHORT) \ - ET(ET_USHORT) \ - ET(ET_INT) \ - ET(ET_UINT) \ - ET(ET_LONG) \ - ET(ET_ULONG) \ - ET(ET_FLOAT) \ - ET(ET_DOUBLE) \ - ET(ET_STRING) \ - ET(ET_SEQUENCE) // See SequenceType. - -enum ElementaryType { - #define FLATBUFFERS_ET(E) E, - FLATBUFFERS_GEN_ELEMENTARY_TYPES(FLATBUFFERS_ET) - #undef FLATBUFFERS_ET -}; - -inline const char * const *ElementaryTypeNames() { - static const char * const names[] = { - #define FLATBUFFERS_ET(E) #E, - FLATBUFFERS_GEN_ELEMENTARY_TYPES(FLATBUFFERS_ET) - #undef FLATBUFFERS_ET - }; - return names; -} -// clang-format on - -// Basic type info cost just 16bits per field! -// We're explicitly defining the signedness since the signedness of integer -// bitfields is otherwise implementation-defined and causes warnings on older -// GCC compilers. -struct TypeCode { - // ElementaryType - unsigned short base_type : 4; - // Either vector (in table) or array (in struct) - unsigned short is_repeating : 1; - // Index into type_refs below, or -1 for none. - signed short sequence_ref : 11; -}; - -static_assert(sizeof(TypeCode) == 2, "TypeCode"); - -struct TypeTable; - -// Signature of the static method present in each type. -typedef const TypeTable *(*TypeFunction)(); - -struct TypeTable { - SequenceType st; - size_t num_elems; // of type_codes, values, names (but not type_refs). - const TypeCode *type_codes; // num_elems count - const TypeFunction *type_refs; // less than num_elems entries (see TypeCode). - const int16_t *array_sizes; // less than num_elems entries (see TypeCode). - const int64_t *values; // Only set for non-consecutive enum/union or structs. - const char *const *names; // Only set if compiled with --reflect-names. -}; - -// String which identifies the current version of FlatBuffers. -// flatbuffer_version_string is used by Google developers to identify which -// applications uploaded to Google Play are using this library. This allows -// the development team at Google to determine the popularity of the library. -// How it works: Applications that are uploaded to the Google Play Store are -// scanned for this version string. We track which applications are using it -// to measure popularity. You are free to remove it (of course) but we would -// appreciate if you left it in. - -// Weak linkage is culled by VS & doesn't work on cygwin. -// clang-format off -#if !defined(_WIN32) && !defined(__CYGWIN__) - -extern volatile __attribute__((weak)) const char *flatbuffer_version_string; -volatile __attribute__((weak)) const char *flatbuffer_version_string = - "FlatBuffers " - FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MAJOR) "." - FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MINOR) "." - FLATBUFFERS_STRING(FLATBUFFERS_VERSION_REVISION); - -#endif // !defined(_WIN32) && !defined(__CYGWIN__) - -#define FLATBUFFERS_DEFINE_BITMASK_OPERATORS(E, T)\ - inline E operator | (E lhs, E rhs){\ - return E(T(lhs) | T(rhs));\ - }\ - inline E operator & (E lhs, E rhs){\ - return E(T(lhs) & T(rhs));\ - }\ - inline E operator ^ (E lhs, E rhs){\ - return E(T(lhs) ^ T(rhs));\ - }\ - inline E operator ~ (E lhs){\ - return E(~T(lhs));\ - }\ - inline E operator |= (E &lhs, E rhs){\ - lhs = lhs | rhs;\ - return lhs;\ - }\ - inline E operator &= (E &lhs, E rhs){\ - lhs = lhs & rhs;\ - return lhs;\ - }\ - inline E operator ^= (E &lhs, E rhs){\ - lhs = lhs ^ rhs;\ - return lhs;\ - }\ - inline bool operator !(E rhs) \ - {\ - return !bool(T(rhs)); \ - } -/// @endcond -} // namespace flatbuffers - -// clang-format on - -#endif // FLATBUFFERS_H_ diff --git a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/flexbuffers.h b/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/flexbuffers.h deleted file mode 100644 index a51fcc92..00000000 --- a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/flexbuffers.h +++ /dev/null @@ -1,1920 +0,0 @@ -/* - * Copyright 2017 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FLATBUFFERS_FLEXBUFFERS_H_ -#define FLATBUFFERS_FLEXBUFFERS_H_ - -#include -// Used to select STL variant. -#include "flatbuffers/base.h" -// We use the basic binary writing functions from the regular FlatBuffers. -#include "flatbuffers/util.h" - -#ifdef _MSC_VER -# include -#endif - -#if defined(_MSC_VER) -# pragma warning(push) -# pragma warning(disable : 4127) // C4127: conditional expression is constant -#endif - -namespace flexbuffers { - -class Reference; -class Map; - -// These are used in the lower 2 bits of a type field to determine the size of -// the elements (and or size field) of the item pointed to (e.g. vector). -enum BitWidth { - BIT_WIDTH_8 = 0, - BIT_WIDTH_16 = 1, - BIT_WIDTH_32 = 2, - BIT_WIDTH_64 = 3, -}; - -// These are used as the upper 6 bits of a type field to indicate the actual -// type. -enum Type { - FBT_NULL = 0, - FBT_INT = 1, - FBT_UINT = 2, - FBT_FLOAT = 3, - // Types above stored inline, types below (except FBT_BOOL) store an offset. - FBT_KEY = 4, - FBT_STRING = 5, - FBT_INDIRECT_INT = 6, - FBT_INDIRECT_UINT = 7, - FBT_INDIRECT_FLOAT = 8, - FBT_MAP = 9, - FBT_VECTOR = 10, // Untyped. - FBT_VECTOR_INT = 11, // Typed any size (stores no type table). - FBT_VECTOR_UINT = 12, - FBT_VECTOR_FLOAT = 13, - FBT_VECTOR_KEY = 14, - // DEPRECATED, use FBT_VECTOR or FBT_VECTOR_KEY instead. - // Read test.cpp/FlexBuffersDeprecatedTest() for details on why. - FBT_VECTOR_STRING_DEPRECATED = 15, - FBT_VECTOR_INT2 = 16, // Typed tuple (no type table, no size field). - FBT_VECTOR_UINT2 = 17, - FBT_VECTOR_FLOAT2 = 18, - FBT_VECTOR_INT3 = 19, // Typed triple (no type table, no size field). - FBT_VECTOR_UINT3 = 20, - FBT_VECTOR_FLOAT3 = 21, - FBT_VECTOR_INT4 = 22, // Typed quad (no type table, no size field). - FBT_VECTOR_UINT4 = 23, - FBT_VECTOR_FLOAT4 = 24, - FBT_BLOB = 25, - FBT_BOOL = 26, - FBT_VECTOR_BOOL = - 36, // To Allow the same type of conversion of type to vector type - - FBT_MAX_TYPE = 37 -}; - -inline bool IsInline(Type t) { return t <= FBT_FLOAT || t == FBT_BOOL; } - -inline bool IsTypedVectorElementType(Type t) { - return (t >= FBT_INT && t <= FBT_STRING) || t == FBT_BOOL; -} - -inline bool IsTypedVector(Type t) { - return (t >= FBT_VECTOR_INT && t <= FBT_VECTOR_STRING_DEPRECATED) || - t == FBT_VECTOR_BOOL; -} - -inline bool IsFixedTypedVector(Type t) { - return t >= FBT_VECTOR_INT2 && t <= FBT_VECTOR_FLOAT4; -} - -inline Type ToTypedVector(Type t, size_t fixed_len = 0) { - FLATBUFFERS_ASSERT(IsTypedVectorElementType(t)); - switch (fixed_len) { - case 0: return static_cast(t - FBT_INT + FBT_VECTOR_INT); - case 2: return static_cast(t - FBT_INT + FBT_VECTOR_INT2); - case 3: return static_cast(t - FBT_INT + FBT_VECTOR_INT3); - case 4: return static_cast(t - FBT_INT + FBT_VECTOR_INT4); - default: FLATBUFFERS_ASSERT(0); return FBT_NULL; - } -} - -inline Type ToTypedVectorElementType(Type t) { - FLATBUFFERS_ASSERT(IsTypedVector(t)); - return static_cast(t - FBT_VECTOR_INT + FBT_INT); -} - -inline Type ToFixedTypedVectorElementType(Type t, uint8_t *len) { - FLATBUFFERS_ASSERT(IsFixedTypedVector(t)); - auto fixed_type = t - FBT_VECTOR_INT2; - *len = static_cast(fixed_type / 3 + - 2); // 3 types each, starting from length 2. - return static_cast(fixed_type % 3 + FBT_INT); -} - -// TODO: implement proper support for 8/16bit floats, or decide not to -// support them. -typedef int16_t half; -typedef int8_t quarter; - -// TODO: can we do this without conditionals using intrinsics or inline asm -// on some platforms? Given branch prediction the method below should be -// decently quick, but it is the most frequently executed function. -// We could do an (unaligned) 64-bit read if we ifdef out the platforms for -// which that doesn't work (or where we'd read into un-owned memory). -template -R ReadSizedScalar(const uint8_t *data, uint8_t byte_width) { - return byte_width < 4 - ? (byte_width < 2 - ? static_cast(flatbuffers::ReadScalar(data)) - : static_cast(flatbuffers::ReadScalar(data))) - : (byte_width < 8 - ? static_cast(flatbuffers::ReadScalar(data)) - : static_cast(flatbuffers::ReadScalar(data))); -} - -inline int64_t ReadInt64(const uint8_t *data, uint8_t byte_width) { - return ReadSizedScalar( - data, byte_width); -} - -inline uint64_t ReadUInt64(const uint8_t *data, uint8_t byte_width) { - // This is the "hottest" function (all offset lookups use this), so worth - // optimizing if possible. - // TODO: GCC apparently replaces memcpy by a rep movsb, but only if count is a - // constant, which here it isn't. Test if memcpy is still faster than - // the conditionals in ReadSizedScalar. Can also use inline asm. - // clang-format off - #if defined(_MSC_VER) && defined(_M_X64) && !defined(_M_ARM64EC) - // This is 64-bit Windows only, __movsb does not work on 32-bit Windows. - uint64_t u = 0; - __movsb(reinterpret_cast(&u), - reinterpret_cast(data), byte_width); - return flatbuffers::EndianScalar(u); - #else - return ReadSizedScalar( - data, byte_width); - #endif - // clang-format on -} - -inline double ReadDouble(const uint8_t *data, uint8_t byte_width) { - return ReadSizedScalar(data, - byte_width); -} - -inline const uint8_t *Indirect(const uint8_t *offset, uint8_t byte_width) { - return offset - ReadUInt64(offset, byte_width); -} - -template const uint8_t *Indirect(const uint8_t *offset) { - return offset - flatbuffers::ReadScalar(offset); -} - -inline BitWidth WidthU(uint64_t u) { -#define FLATBUFFERS_GET_FIELD_BIT_WIDTH(value, width) \ - { \ - if (!((u) & ~((1ULL << (width)) - 1ULL))) return BIT_WIDTH_##width; \ - } - FLATBUFFERS_GET_FIELD_BIT_WIDTH(u, 8); - FLATBUFFERS_GET_FIELD_BIT_WIDTH(u, 16); - FLATBUFFERS_GET_FIELD_BIT_WIDTH(u, 32); -#undef FLATBUFFERS_GET_FIELD_BIT_WIDTH - return BIT_WIDTH_64; -} - -inline BitWidth WidthI(int64_t i) { - auto u = static_cast(i) << 1; - return WidthU(i >= 0 ? u : ~u); -} - -inline BitWidth WidthF(double f) { - return static_cast(static_cast(f)) == f ? BIT_WIDTH_32 - : BIT_WIDTH_64; -} - -// Base class of all types below. -// Points into the data buffer and allows access to one type. -class Object { - public: - Object(const uint8_t *data, uint8_t byte_width) - : data_(data), byte_width_(byte_width) {} - - protected: - const uint8_t *data_; - uint8_t byte_width_; -}; - -// Object that has a size, obtained either from size prefix, or elsewhere. -class Sized : public Object { - public: - // Size prefix. - Sized(const uint8_t *data, uint8_t byte_width) - : Object(data, byte_width), size_(read_size()) {} - // Manual size. - Sized(const uint8_t *data, uint8_t byte_width, size_t sz) - : Object(data, byte_width), size_(sz) {} - size_t size() const { return size_; } - // Access size stored in `byte_width_` bytes before data_ pointer. - size_t read_size() const { - return static_cast(ReadUInt64(data_ - byte_width_, byte_width_)); - } - - protected: - size_t size_; -}; - -class String : public Sized { - public: - // Size prefix. - String(const uint8_t *data, uint8_t byte_width) : Sized(data, byte_width) {} - // Manual size. - String(const uint8_t *data, uint8_t byte_width, size_t sz) - : Sized(data, byte_width, sz) {} - - size_t length() const { return size(); } - const char *c_str() const { return reinterpret_cast(data_); } - std::string str() const { return std::string(c_str(), size()); } - - static String EmptyString() { - static const char *empty_string = ""; - return String(reinterpret_cast(empty_string), 1, 0); - } - bool IsTheEmptyString() const { return data_ == EmptyString().data_; } -}; - -class Blob : public Sized { - public: - Blob(const uint8_t *data_buf, uint8_t byte_width) - : Sized(data_buf, byte_width) {} - - static Blob EmptyBlob() { - static const uint8_t empty_blob[] = { 0 /*len*/ }; - return Blob(empty_blob + 1, 1); - } - bool IsTheEmptyBlob() const { return data_ == EmptyBlob().data_; } - const uint8_t *data() const { return data_; } -}; - -class Vector : public Sized { - public: - Vector(const uint8_t *data, uint8_t byte_width) : Sized(data, byte_width) {} - - Reference operator[](size_t i) const; - - static Vector EmptyVector() { - static const uint8_t empty_vector[] = { 0 /*len*/ }; - return Vector(empty_vector + 1, 1); - } - bool IsTheEmptyVector() const { return data_ == EmptyVector().data_; } -}; - -class TypedVector : public Sized { - public: - TypedVector(const uint8_t *data, uint8_t byte_width, Type element_type) - : Sized(data, byte_width), type_(element_type) {} - - Reference operator[](size_t i) const; - - static TypedVector EmptyTypedVector() { - static const uint8_t empty_typed_vector[] = { 0 /*len*/ }; - return TypedVector(empty_typed_vector + 1, 1, FBT_INT); - } - bool IsTheEmptyVector() const { - return data_ == TypedVector::EmptyTypedVector().data_; - } - - Type ElementType() { return type_; } - - friend Reference; - - private: - Type type_; - - friend Map; -}; - -class FixedTypedVector : public Object { - public: - FixedTypedVector(const uint8_t *data, uint8_t byte_width, Type element_type, - uint8_t len) - : Object(data, byte_width), type_(element_type), len_(len) {} - - Reference operator[](size_t i) const; - - static FixedTypedVector EmptyFixedTypedVector() { - static const uint8_t fixed_empty_vector[] = { 0 /* unused */ }; - return FixedTypedVector(fixed_empty_vector, 1, FBT_INT, 0); - } - bool IsTheEmptyFixedTypedVector() const { - return data_ == FixedTypedVector::EmptyFixedTypedVector().data_; - } - - Type ElementType() const { return type_; } - uint8_t size() const { return len_; } - - private: - Type type_; - uint8_t len_; -}; - -class Map : public Vector { - public: - Map(const uint8_t *data, uint8_t byte_width) : Vector(data, byte_width) {} - - Reference operator[](const char *key) const; - Reference operator[](const std::string &key) const; - - Vector Values() const { return Vector(data_, byte_width_); } - - TypedVector Keys() const { - const size_t num_prefixed_fields = 3; - auto keys_offset = data_ - byte_width_ * num_prefixed_fields; - return TypedVector(Indirect(keys_offset, byte_width_), - static_cast( - ReadUInt64(keys_offset + byte_width_, byte_width_)), - FBT_KEY); - } - - static Map EmptyMap() { - static const uint8_t empty_map[] = { - 0 /*keys_len*/, 0 /*keys_offset*/, 1 /*keys_width*/, 0 /*len*/ - }; - return Map(empty_map + 4, 1); - } - - bool IsTheEmptyMap() const { return data_ == EmptyMap().data_; } -}; - -template -void AppendToString(std::string &s, T &&v, bool keys_quoted) { - s += "[ "; - for (size_t i = 0; i < v.size(); i++) { - if (i) s += ", "; - v[i].ToString(true, keys_quoted, s); - } - s += " ]"; -} - -class Reference { - public: - Reference() - : data_(nullptr), - parent_width_(0), - byte_width_(0), - type_(FBT_NULL) {} - - Reference(const uint8_t *data, uint8_t parent_width, uint8_t byte_width, - Type type) - : data_(data), - parent_width_(parent_width), - byte_width_(byte_width), - type_(type) {} - - Reference(const uint8_t *data, uint8_t parent_width, uint8_t packed_type) - : data_(data), parent_width_(parent_width) { - byte_width_ = 1U << static_cast(packed_type & 3); - type_ = static_cast(packed_type >> 2); - } - - Type GetType() const { return type_; } - - bool IsNull() const { return type_ == FBT_NULL; } - bool IsBool() const { return type_ == FBT_BOOL; } - bool IsInt() const { return type_ == FBT_INT || type_ == FBT_INDIRECT_INT; } - bool IsUInt() const { - return type_ == FBT_UINT || type_ == FBT_INDIRECT_UINT; - } - bool IsIntOrUint() const { return IsInt() || IsUInt(); } - bool IsFloat() const { - return type_ == FBT_FLOAT || type_ == FBT_INDIRECT_FLOAT; - } - bool IsNumeric() const { return IsIntOrUint() || IsFloat(); } - bool IsString() const { return type_ == FBT_STRING; } - bool IsKey() const { return type_ == FBT_KEY; } - bool IsVector() const { return type_ == FBT_VECTOR || type_ == FBT_MAP; } - bool IsUntypedVector() const { return type_ == FBT_VECTOR; } - bool IsTypedVector() const { return flexbuffers::IsTypedVector(type_); } - bool IsFixedTypedVector() const { - return flexbuffers::IsFixedTypedVector(type_); - } - bool IsAnyVector() const { - return (IsTypedVector() || IsFixedTypedVector() || IsVector()); - } - bool IsMap() const { return type_ == FBT_MAP; } - bool IsBlob() const { return type_ == FBT_BLOB; } - bool AsBool() const { - return (type_ == FBT_BOOL ? ReadUInt64(data_, parent_width_) - : AsUInt64()) != 0; - } - - // Reads any type as a int64_t. Never fails, does most sensible conversion. - // Truncates floats, strings are attempted to be parsed for a number, - // vectors/maps return their size. Returns 0 if all else fails. - int64_t AsInt64() const { - if (type_ == FBT_INT) { - // A fast path for the common case. - return ReadInt64(data_, parent_width_); - } else - switch (type_) { - case FBT_INDIRECT_INT: return ReadInt64(Indirect(), byte_width_); - case FBT_UINT: return ReadUInt64(data_, parent_width_); - case FBT_INDIRECT_UINT: return ReadUInt64(Indirect(), byte_width_); - case FBT_FLOAT: - return static_cast(ReadDouble(data_, parent_width_)); - case FBT_INDIRECT_FLOAT: - return static_cast(ReadDouble(Indirect(), byte_width_)); - case FBT_NULL: return 0; - case FBT_STRING: return flatbuffers::StringToInt(AsString().c_str()); - case FBT_VECTOR: return static_cast(AsVector().size()); - case FBT_BOOL: return ReadInt64(data_, parent_width_); - default: - // Convert other things to int. - return 0; - } - } - - // TODO: could specialize these to not use AsInt64() if that saves - // extension ops in generated code, and use a faster op than ReadInt64. - int32_t AsInt32() const { return static_cast(AsInt64()); } - int16_t AsInt16() const { return static_cast(AsInt64()); } - int8_t AsInt8() const { return static_cast(AsInt64()); } - - uint64_t AsUInt64() const { - if (type_ == FBT_UINT) { - // A fast path for the common case. - return ReadUInt64(data_, parent_width_); - } else - switch (type_) { - case FBT_INDIRECT_UINT: return ReadUInt64(Indirect(), byte_width_); - case FBT_INT: return ReadInt64(data_, parent_width_); - case FBT_INDIRECT_INT: return ReadInt64(Indirect(), byte_width_); - case FBT_FLOAT: - return static_cast(ReadDouble(data_, parent_width_)); - case FBT_INDIRECT_FLOAT: - return static_cast(ReadDouble(Indirect(), byte_width_)); - case FBT_NULL: return 0; - case FBT_STRING: return flatbuffers::StringToUInt(AsString().c_str()); - case FBT_VECTOR: return static_cast(AsVector().size()); - case FBT_BOOL: return ReadUInt64(data_, parent_width_); - default: - // Convert other things to uint. - return 0; - } - } - - uint32_t AsUInt32() const { return static_cast(AsUInt64()); } - uint16_t AsUInt16() const { return static_cast(AsUInt64()); } - uint8_t AsUInt8() const { return static_cast(AsUInt64()); } - - double AsDouble() const { - if (type_ == FBT_FLOAT) { - // A fast path for the common case. - return ReadDouble(data_, parent_width_); - } else - switch (type_) { - case FBT_INDIRECT_FLOAT: return ReadDouble(Indirect(), byte_width_); - case FBT_INT: - return static_cast(ReadInt64(data_, parent_width_)); - case FBT_UINT: - return static_cast(ReadUInt64(data_, parent_width_)); - case FBT_INDIRECT_INT: - return static_cast(ReadInt64(Indirect(), byte_width_)); - case FBT_INDIRECT_UINT: - return static_cast(ReadUInt64(Indirect(), byte_width_)); - case FBT_NULL: return 0.0; - case FBT_STRING: { -#if 1 -#if !defined( _MSC_VER) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wnull-dereference" -#endif - // See b/173239141 for additional context. Patched via - // micro/tools/make/flexbuffers_download.sh - // Introduce a segfault for an unsupported code path for TFLM. - return *(static_cast(nullptr)); -#if !defined( _MSC_VER) -#pragma GCC diagnostic pop -#endif -#else - // This is the original code - double d; - flatbuffers::StringToNumber(AsString().c_str(), &d); - return d; -#endif - } - case FBT_VECTOR: return static_cast(AsVector().size()); - case FBT_BOOL: - return static_cast(ReadUInt64(data_, parent_width_)); - default: - // Convert strings and other things to float. - return 0; - } - } - - float AsFloat() const { return static_cast(AsDouble()); } - - const char *AsKey() const { - if (type_ == FBT_KEY || type_ == FBT_STRING) { - return reinterpret_cast(Indirect()); - } else { - return ""; - } - } - - // This function returns the empty string if you try to read something that - // is not a string or key. - String AsString() const { - if (type_ == FBT_STRING) { - return String(Indirect(), byte_width_); - } else if (type_ == FBT_KEY) { - auto key = Indirect(); - return String(key, byte_width_, - strlen(reinterpret_cast(key))); - } else { - return String::EmptyString(); - } - } - - // Unlike AsString(), this will convert any type to a std::string. - std::string ToString() const { - std::string s; - ToString(false, false, s); - return s; - } - - // Convert any type to a JSON-like string. strings_quoted determines if - // string values at the top level receive "" quotes (inside other values - // they always do). keys_quoted determines if keys are quoted, at any level. - // TODO(wvo): add further options to have indentation/newlines. - void ToString(bool strings_quoted, bool keys_quoted, std::string &s) const { - if (type_ == FBT_STRING) { - String str(Indirect(), byte_width_); - if (strings_quoted) { - flatbuffers::EscapeString(str.c_str(), str.length(), &s, true, false); - } else { - s.append(str.c_str(), str.length()); - } - } else if (IsKey()) { - auto str = AsKey(); - if (keys_quoted) { - flatbuffers::EscapeString(str, strlen(str), &s, true, false); - } else { - s += str; - } - } else if (IsInt()) { - s += flatbuffers::NumToString(AsInt64()); - } else if (IsUInt()) { - s += flatbuffers::NumToString(AsUInt64()); - } else if (IsFloat()) { - s += flatbuffers::NumToString(AsDouble()); - } else if (IsNull()) { - s += "null"; - } else if (IsBool()) { - s += AsBool() ? "true" : "false"; - } else if (IsMap()) { - s += "{ "; - auto m = AsMap(); - auto keys = m.Keys(); - auto vals = m.Values(); - for (size_t i = 0; i < keys.size(); i++) { - keys[i].ToString(true, keys_quoted, s); - s += ": "; - vals[i].ToString(true, keys_quoted, s); - if (i < keys.size() - 1) s += ", "; - } - s += " }"; - } else if (IsVector()) { - AppendToString(s, AsVector(), keys_quoted); - } else if (IsTypedVector()) { - AppendToString(s, AsTypedVector(), keys_quoted); - } else if (IsFixedTypedVector()) { - AppendToString(s, AsFixedTypedVector(), keys_quoted); - } else if (IsBlob()) { - auto blob = AsBlob(); - flatbuffers::EscapeString(reinterpret_cast(blob.data()), - blob.size(), &s, true, false); - } else { - s += "(?)"; - } - } - - // This function returns the empty blob if you try to read a not-blob. - // Strings can be viewed as blobs too. - Blob AsBlob() const { - if (type_ == FBT_BLOB || type_ == FBT_STRING) { - return Blob(Indirect(), byte_width_); - } else { - return Blob::EmptyBlob(); - } - } - - // This function returns the empty vector if you try to read a not-vector. - // Maps can be viewed as vectors too. - Vector AsVector() const { - if (type_ == FBT_VECTOR || type_ == FBT_MAP) { - return Vector(Indirect(), byte_width_); - } else { - return Vector::EmptyVector(); - } - } - - TypedVector AsTypedVector() const { - if (IsTypedVector()) { - auto tv = - TypedVector(Indirect(), byte_width_, ToTypedVectorElementType(type_)); - if (tv.type_ == FBT_STRING) { - // These can't be accessed as strings, since we don't know the bit-width - // of the size field, see the declaration of - // FBT_VECTOR_STRING_DEPRECATED above for details. - // We change the type here to be keys, which are a subtype of strings, - // and will ignore the size field. This will truncate strings with - // embedded nulls. - tv.type_ = FBT_KEY; - } - return tv; - } else { - return TypedVector::EmptyTypedVector(); - } - } - - FixedTypedVector AsFixedTypedVector() const { - if (IsFixedTypedVector()) { - uint8_t len = 0; - auto vtype = ToFixedTypedVectorElementType(type_, &len); - return FixedTypedVector(Indirect(), byte_width_, vtype, len); - } else { - return FixedTypedVector::EmptyFixedTypedVector(); - } - } - - Map AsMap() const { - if (type_ == FBT_MAP) { - return Map(Indirect(), byte_width_); - } else { - return Map::EmptyMap(); - } - } - - template T As() const; - - // Experimental: Mutation functions. - // These allow scalars in an already created buffer to be updated in-place. - // Since by default scalars are stored in the smallest possible space, - // the new value may not fit, in which case these functions return false. - // To avoid this, you can construct the values you intend to mutate using - // Builder::ForceMinimumBitWidth. - bool MutateInt(int64_t i) { - if (type_ == FBT_INT) { - return Mutate(data_, i, parent_width_, WidthI(i)); - } else if (type_ == FBT_INDIRECT_INT) { - return Mutate(Indirect(), i, byte_width_, WidthI(i)); - } else if (type_ == FBT_UINT) { - auto u = static_cast(i); - return Mutate(data_, u, parent_width_, WidthU(u)); - } else if (type_ == FBT_INDIRECT_UINT) { - auto u = static_cast(i); - return Mutate(Indirect(), u, byte_width_, WidthU(u)); - } else { - return false; - } - } - - bool MutateBool(bool b) { - return type_ == FBT_BOOL && Mutate(data_, b, parent_width_, BIT_WIDTH_8); - } - - bool MutateUInt(uint64_t u) { - if (type_ == FBT_UINT) { - return Mutate(data_, u, parent_width_, WidthU(u)); - } else if (type_ == FBT_INDIRECT_UINT) { - return Mutate(Indirect(), u, byte_width_, WidthU(u)); - } else if (type_ == FBT_INT) { - auto i = static_cast(u); - return Mutate(data_, i, parent_width_, WidthI(i)); - } else if (type_ == FBT_INDIRECT_INT) { - auto i = static_cast(u); - return Mutate(Indirect(), i, byte_width_, WidthI(i)); - } else { - return false; - } - } - - bool MutateFloat(float f) { - if (type_ == FBT_FLOAT) { - return MutateF(data_, f, parent_width_, BIT_WIDTH_32); - } else if (type_ == FBT_INDIRECT_FLOAT) { - return MutateF(Indirect(), f, byte_width_, BIT_WIDTH_32); - } else { - return false; - } - } - - bool MutateFloat(double d) { - if (type_ == FBT_FLOAT) { - return MutateF(data_, d, parent_width_, WidthF(d)); - } else if (type_ == FBT_INDIRECT_FLOAT) { - return MutateF(Indirect(), d, byte_width_, WidthF(d)); - } else { - return false; - } - } - - bool MutateString(const char *str, size_t len) { - auto s = AsString(); - if (s.IsTheEmptyString()) return false; - // This is very strict, could allow shorter strings, but that creates - // garbage. - if (s.length() != len) return false; - memcpy(const_cast(s.c_str()), str, len); - return true; - } - bool MutateString(const char *str) { return MutateString(str, strlen(str)); } - bool MutateString(const std::string &str) { - return MutateString(str.data(), str.length()); - } - - private: - const uint8_t *Indirect() const { - return flexbuffers::Indirect(data_, parent_width_); - } - - template - bool Mutate(const uint8_t *dest, T t, size_t byte_width, - BitWidth value_width) { - auto fits = static_cast(static_cast(1U) << value_width) <= - byte_width; - if (fits) { - t = flatbuffers::EndianScalar(t); - memcpy(const_cast(dest), &t, byte_width); - } - return fits; - } - - template - bool MutateF(const uint8_t *dest, T t, size_t byte_width, - BitWidth value_width) { - if (byte_width == sizeof(double)) - return Mutate(dest, static_cast(t), byte_width, value_width); - if (byte_width == sizeof(float)) - return Mutate(dest, static_cast(t), byte_width, value_width); - FLATBUFFERS_ASSERT(false); - return false; - } - - friend class Verifier; - - const uint8_t *data_; - uint8_t parent_width_; - uint8_t byte_width_; - Type type_; -}; - -// Template specialization for As(). -template<> inline bool Reference::As() const { return AsBool(); } - -template<> inline int8_t Reference::As() const { return AsInt8(); } -template<> inline int16_t Reference::As() const { return AsInt16(); } -template<> inline int32_t Reference::As() const { return AsInt32(); } -template<> inline int64_t Reference::As() const { return AsInt64(); } - -template<> inline uint8_t Reference::As() const { return AsUInt8(); } -template<> inline uint16_t Reference::As() const { - return AsUInt16(); -} -template<> inline uint32_t Reference::As() const { - return AsUInt32(); -} -template<> inline uint64_t Reference::As() const { - return AsUInt64(); -} - -template<> inline double Reference::As() const { return AsDouble(); } -template<> inline float Reference::As() const { return AsFloat(); } - -template<> inline String Reference::As() const { return AsString(); } -template<> inline std::string Reference::As() const { - return AsString().str(); -} - -template<> inline Blob Reference::As() const { return AsBlob(); } -template<> inline Vector Reference::As() const { return AsVector(); } -template<> inline TypedVector Reference::As() const { - return AsTypedVector(); -} -template<> inline FixedTypedVector Reference::As() const { - return AsFixedTypedVector(); -} -template<> inline Map Reference::As() const { return AsMap(); } - -inline uint8_t PackedType(BitWidth bit_width, Type type) { - return static_cast(bit_width | (type << 2)); -} - -inline uint8_t NullPackedType() { return PackedType(BIT_WIDTH_8, FBT_NULL); } - -// Vector accessors. -// Note: if you try to access outside of bounds, you get a Null value back -// instead. Normally this would be an assert, but since this is "dynamically -// typed" data, you may not want that (someone sends you a 2d vector and you -// wanted 3d). -// The Null converts seamlessly into a default value for any other type. -// TODO(wvo): Could introduce an #ifdef that makes this into an assert? -inline Reference Vector::operator[](size_t i) const { - auto len = size(); - if (i >= len) return Reference(nullptr, 1, NullPackedType()); - auto packed_type = (data_ + len * byte_width_)[i]; - auto elem = data_ + i * byte_width_; - return Reference(elem, byte_width_, packed_type); -} - -inline Reference TypedVector::operator[](size_t i) const { - auto len = size(); - if (i >= len) return Reference(nullptr, 1, NullPackedType()); - auto elem = data_ + i * byte_width_; - return Reference(elem, byte_width_, 1, type_); -} - -inline Reference FixedTypedVector::operator[](size_t i) const { - if (i >= len_) return Reference(nullptr, 1, NullPackedType()); - auto elem = data_ + i * byte_width_; - return Reference(elem, byte_width_, 1, type_); -} - -template int KeyCompare(const void *key, const void *elem) { - auto str_elem = reinterpret_cast( - Indirect(reinterpret_cast(elem))); - auto skey = reinterpret_cast(key); - return strcmp(skey, str_elem); -} - -inline Reference Map::operator[](const char *key) const { - auto keys = Keys(); - // We can't pass keys.byte_width_ to the comparison function, so we have - // to pick the right one ahead of time. - int (*comp)(const void *, const void *) = nullptr; - switch (keys.byte_width_) { - case 1: comp = KeyCompare; break; - case 2: comp = KeyCompare; break; - case 4: comp = KeyCompare; break; - case 8: comp = KeyCompare; break; - default: FLATBUFFERS_ASSERT(false); return Reference(); - } - auto res = std::bsearch(key, keys.data_, keys.size(), keys.byte_width_, comp); - if (!res) return Reference(nullptr, 1, NullPackedType()); - auto i = (reinterpret_cast(res) - keys.data_) / keys.byte_width_; - return (*static_cast(this))[i]; -} - -inline Reference Map::operator[](const std::string &key) const { - return (*this)[key.c_str()]; -} - -inline Reference GetRoot(const uint8_t *buffer, size_t size) { - // See Finish() below for the serialization counterpart of this. - // The root starts at the end of the buffer, so we parse backwards from there. - auto end = buffer + size; - auto byte_width = *--end; - auto packed_type = *--end; - end -= byte_width; // The root data item. - return Reference(end, byte_width, packed_type); -} - -inline Reference GetRoot(const std::vector &buffer) { - return GetRoot(buffer.data(), buffer.size()); -} - -// Flags that configure how the Builder behaves. -// The "Share" flags determine if the Builder automatically tries to pool -// this type. Pooling can reduce the size of serialized data if there are -// multiple maps of the same kind, at the expense of slightly slower -// serialization (the cost of lookups) and more memory use (std::set). -// By default this is on for keys, but off for strings. -// Turn keys off if you have e.g. only one map. -// Turn strings on if you expect many non-unique string values. -// Additionally, sharing key vectors can save space if you have maps with -// identical field populations. -enum BuilderFlag { - BUILDER_FLAG_NONE = 0, - BUILDER_FLAG_SHARE_KEYS = 1, - BUILDER_FLAG_SHARE_STRINGS = 2, - BUILDER_FLAG_SHARE_KEYS_AND_STRINGS = 3, - BUILDER_FLAG_SHARE_KEY_VECTORS = 4, - BUILDER_FLAG_SHARE_ALL = 7, -}; - -class Builder FLATBUFFERS_FINAL_CLASS { - public: - Builder(size_t initial_size = 256, - BuilderFlag flags = BUILDER_FLAG_SHARE_KEYS) - : buf_(initial_size), - finished_(false), - has_duplicate_keys_(false), - flags_(flags), - force_min_bit_width_(BIT_WIDTH_8), - key_pool(KeyOffsetCompare(buf_)), - string_pool(StringOffsetCompare(buf_)) { - buf_.clear(); - } - -#ifdef FLATBUFFERS_DEFAULT_DECLARATION - Builder(Builder &&) = default; - Builder &operator=(Builder &&) = default; -#endif - - /// @brief Get the serialized buffer (after you call `Finish()`). - /// @return Returns a vector owned by this class. - const std::vector &GetBuffer() const { - Finished(); - return buf_; - } - - // Size of the buffer. Does not include unfinished values. - size_t GetSize() const { return buf_.size(); } - - // Reset all state so we can re-use the buffer. - void Clear() { - buf_.clear(); - stack_.clear(); - finished_ = false; - // flags_ remains as-is; - force_min_bit_width_ = BIT_WIDTH_8; - key_pool.clear(); - string_pool.clear(); - } - - // All value constructing functions below have two versions: one that - // takes a key (for placement inside a map) and one that doesn't (for inside - // vectors and elsewhere). - - void Null() { stack_.push_back(Value()); } - void Null(const char *key) { - Key(key); - Null(); - } - - void Int(int64_t i) { stack_.push_back(Value(i, FBT_INT, WidthI(i))); } - void Int(const char *key, int64_t i) { - Key(key); - Int(i); - } - - void UInt(uint64_t u) { stack_.push_back(Value(u, FBT_UINT, WidthU(u))); } - void UInt(const char *key, uint64_t u) { - Key(key); - UInt(u); - } - - void Float(float f) { stack_.push_back(Value(f)); } - void Float(const char *key, float f) { - Key(key); - Float(f); - } - - void Double(double f) { stack_.push_back(Value(f)); } - void Double(const char *key, double d) { - Key(key); - Double(d); - } - - void Bool(bool b) { stack_.push_back(Value(b)); } - void Bool(const char *key, bool b) { - Key(key); - Bool(b); - } - - void IndirectInt(int64_t i) { PushIndirect(i, FBT_INDIRECT_INT, WidthI(i)); } - void IndirectInt(const char *key, int64_t i) { - Key(key); - IndirectInt(i); - } - - void IndirectUInt(uint64_t u) { - PushIndirect(u, FBT_INDIRECT_UINT, WidthU(u)); - } - void IndirectUInt(const char *key, uint64_t u) { - Key(key); - IndirectUInt(u); - } - - void IndirectFloat(float f) { - PushIndirect(f, FBT_INDIRECT_FLOAT, BIT_WIDTH_32); - } - void IndirectFloat(const char *key, float f) { - Key(key); - IndirectFloat(f); - } - - void IndirectDouble(double f) { - PushIndirect(f, FBT_INDIRECT_FLOAT, WidthF(f)); - } - void IndirectDouble(const char *key, double d) { - Key(key); - IndirectDouble(d); - } - - size_t Key(const char *str, size_t len) { - auto sloc = buf_.size(); - WriteBytes(str, len + 1); - if (flags_ & BUILDER_FLAG_SHARE_KEYS) { - auto it = key_pool.find(sloc); - if (it != key_pool.end()) { - // Already in the buffer. Remove key we just serialized, and use - // existing offset instead. - buf_.resize(sloc); - sloc = *it; - } else { - key_pool.insert(sloc); - } - } - stack_.push_back(Value(static_cast(sloc), FBT_KEY, BIT_WIDTH_8)); - return sloc; - } - - size_t Key(const char *str) { return Key(str, strlen(str)); } - size_t Key(const std::string &str) { return Key(str.c_str(), str.size()); } - - size_t String(const char *str, size_t len) { - auto reset_to = buf_.size(); - auto sloc = CreateBlob(str, len, 1, FBT_STRING); - if (flags_ & BUILDER_FLAG_SHARE_STRINGS) { - StringOffset so(sloc, len); - auto it = string_pool.find(so); - if (it != string_pool.end()) { - // Already in the buffer. Remove string we just serialized, and use - // existing offset instead. - buf_.resize(reset_to); - sloc = it->first; - stack_.back().u_ = sloc; - } else { - string_pool.insert(so); - } - } - return sloc; - } - size_t String(const char *str) { return String(str, strlen(str)); } - size_t String(const std::string &str) { - return String(str.c_str(), str.size()); - } - void String(const flexbuffers::String &str) { - String(str.c_str(), str.length()); - } - - void String(const char *key, const char *str) { - Key(key); - String(str); - } - void String(const char *key, const std::string &str) { - Key(key); - String(str); - } - void String(const char *key, const flexbuffers::String &str) { - Key(key); - String(str); - } - - size_t Blob(const void *data, size_t len) { - return CreateBlob(data, len, 0, FBT_BLOB); - } - size_t Blob(const std::vector &v) { - return CreateBlob(v.data(), v.size(), 0, FBT_BLOB); - } - - void Blob(const char *key, const void *data, size_t len) { - Key(key); - Blob(data, len); - } - void Blob(const char *key, const std::vector &v) { - Key(key); - Blob(v); - } - - // TODO(wvo): support all the FlexBuffer types (like flexbuffers::String), - // e.g. Vector etc. Also in overloaded versions. - // Also some FlatBuffers types? - - size_t StartVector() { return stack_.size(); } - size_t StartVector(const char *key) { - Key(key); - return stack_.size(); - } - size_t StartMap() { return stack_.size(); } - size_t StartMap(const char *key) { - Key(key); - return stack_.size(); - } - - // TODO(wvo): allow this to specify an alignment greater than the natural - // alignment. - size_t EndVector(size_t start, bool typed, bool fixed) { - auto vec = CreateVector(start, stack_.size() - start, 1, typed, fixed); - // Remove temp elements and return vector. - stack_.resize(start); - stack_.push_back(vec); - return static_cast(vec.u_); - } - - size_t EndMap(size_t start) { - // We should have interleaved keys and values on the stack. - // Make sure it is an even number: - auto len = stack_.size() - start; - FLATBUFFERS_ASSERT(!(len & 1)); - len /= 2; - // Make sure keys are all strings: - for (auto key = start; key < stack_.size(); key += 2) { - FLATBUFFERS_ASSERT(stack_[key].type_ == FBT_KEY); - } - // Now sort values, so later we can do a binary search lookup. - // We want to sort 2 array elements at a time. - struct TwoValue { - Value key; - Value val; - }; - // TODO(wvo): strict aliasing? - // TODO(wvo): allow the caller to indicate the data is already sorted - // for maximum efficiency? With an assert to check sortedness to make sure - // we're not breaking binary search. - // Or, we can track if the map is sorted as keys are added which would be - // be quite cheap (cheaper than checking it here), so we can skip this - // step automatically when appliccable, and encourage people to write in - // sorted fashion. - // std::sort is typically already a lot faster on sorted data though. - auto dict = reinterpret_cast(stack_.data() + start); - std::sort( - dict, dict + len, [&](const TwoValue &a, const TwoValue &b) -> bool { - auto as = reinterpret_cast(buf_.data() + a.key.u_); - auto bs = reinterpret_cast(buf_.data() + b.key.u_); - auto comp = strcmp(as, bs); - // We want to disallow duplicate keys, since this results in a - // map where values cannot be found. - // But we can't assert here (since we don't want to fail on - // random JSON input) or have an error mechanism. - // Instead, we set has_duplicate_keys_ in the builder to - // signal this. - // TODO: Have to check for pointer equality, as some sort - // implementation apparently call this function with the same - // element?? Why? - if (!comp && &a != &b) has_duplicate_keys_ = true; - return comp < 0; - }); - // First create a vector out of all keys. - // TODO(wvo): if kBuilderFlagShareKeyVectors is true, see if we can share - // the first vector. - auto keys = CreateVector(start, len, 2, true, false); - auto vec = CreateVector(start + 1, len, 2, false, false, &keys); - // Remove temp elements and return map. - stack_.resize(start); - stack_.push_back(vec); - return static_cast(vec.u_); - } - - // Call this after EndMap to see if the map had any duplicate keys. - // Any map with such keys won't be able to retrieve all values. - bool HasDuplicateKeys() const { return has_duplicate_keys_; } - - template size_t Vector(F f) { - auto start = StartVector(); - f(); - return EndVector(start, false, false); - } - template size_t Vector(F f, T &state) { - auto start = StartVector(); - f(state); - return EndVector(start, false, false); - } - template size_t Vector(const char *key, F f) { - auto start = StartVector(key); - f(); - return EndVector(start, false, false); - } - template - size_t Vector(const char *key, F f, T &state) { - auto start = StartVector(key); - f(state); - return EndVector(start, false, false); - } - - template void Vector(const T *elems, size_t len) { - if (flatbuffers::is_scalar::value) { - // This path should be a lot quicker and use less space. - ScalarVector(elems, len, false); - } else { - auto start = StartVector(); - for (size_t i = 0; i < len; i++) Add(elems[i]); - EndVector(start, false, false); - } - } - template - void Vector(const char *key, const T *elems, size_t len) { - Key(key); - Vector(elems, len); - } - template void Vector(const std::vector &vec) { - Vector(vec.data(), vec.size()); - } - - template size_t TypedVector(F f) { - auto start = StartVector(); - f(); - return EndVector(start, true, false); - } - template size_t TypedVector(F f, T &state) { - auto start = StartVector(); - f(state); - return EndVector(start, true, false); - } - template size_t TypedVector(const char *key, F f) { - auto start = StartVector(key); - f(); - return EndVector(start, true, false); - } - template - size_t TypedVector(const char *key, F f, T &state) { - auto start = StartVector(key); - f(state); - return EndVector(start, true, false); - } - - template size_t FixedTypedVector(const T *elems, size_t len) { - // We only support a few fixed vector lengths. Anything bigger use a - // regular typed vector. - FLATBUFFERS_ASSERT(len >= 2 && len <= 4); - // And only scalar values. - static_assert(flatbuffers::is_scalar::value, "Unrelated types"); - return ScalarVector(elems, len, true); - } - - template - size_t FixedTypedVector(const char *key, const T *elems, size_t len) { - Key(key); - return FixedTypedVector(elems, len); - } - - template size_t Map(F f) { - auto start = StartMap(); - f(); - return EndMap(start); - } - template size_t Map(F f, T &state) { - auto start = StartMap(); - f(state); - return EndMap(start); - } - template size_t Map(const char *key, F f) { - auto start = StartMap(key); - f(); - return EndMap(start); - } - template size_t Map(const char *key, F f, T &state) { - auto start = StartMap(key); - f(state); - return EndMap(start); - } - template void Map(const std::map &map) { - auto start = StartMap(); - for (auto it = map.begin(); it != map.end(); ++it) - Add(it->first.c_str(), it->second); - EndMap(start); - } - - // If you wish to share a value explicitly (a value not shared automatically - // through one of the BUILDER_FLAG_SHARE_* flags) you can do so with these - // functions. Or if you wish to turn those flags off for performance reasons - // and still do some explicit sharing. For example: - // builder.IndirectDouble(M_PI); - // auto id = builder.LastValue(); // Remember where we stored it. - // .. more code goes here .. - // builder.ReuseValue(id); // Refers to same double by offset. - // LastValue works regardless of whether the value has a key or not. - // Works on any data type. - struct Value; - Value LastValue() { return stack_.back(); } - void ReuseValue(Value v) { stack_.push_back(v); } - void ReuseValue(const char *key, Value v) { - Key(key); - ReuseValue(v); - } - - // Overloaded Add that tries to call the correct function above. - void Add(int8_t i) { Int(i); } - void Add(int16_t i) { Int(i); } - void Add(int32_t i) { Int(i); } - void Add(int64_t i) { Int(i); } - void Add(uint8_t u) { UInt(u); } - void Add(uint16_t u) { UInt(u); } - void Add(uint32_t u) { UInt(u); } - void Add(uint64_t u) { UInt(u); } - void Add(float f) { Float(f); } - void Add(double d) { Double(d); } - void Add(bool b) { Bool(b); } - void Add(const char *str) { String(str); } - void Add(const std::string &str) { String(str); } - void Add(const flexbuffers::String &str) { String(str); } - - template void Add(const std::vector &vec) { Vector(vec); } - - template void Add(const char *key, const T &t) { - Key(key); - Add(t); - } - - template void Add(const std::map &map) { - Map(map); - } - - template void operator+=(const T &t) { Add(t); } - - // This function is useful in combination with the Mutate* functions above. - // It forces elements of vectors and maps to have a minimum size, such that - // they can later be updated without failing. - // Call with no arguments to reset. - void ForceMinimumBitWidth(BitWidth bw = BIT_WIDTH_8) { - force_min_bit_width_ = bw; - } - - void Finish() { - // If you hit this assert, you likely have objects that were never included - // in a parent. You need to have exactly one root to finish a buffer. - // Check your Start/End calls are matched, and all objects are inside - // some other object. - FLATBUFFERS_ASSERT(stack_.size() == 1); - - // Write root value. - auto byte_width = Align(stack_[0].ElemWidth(buf_.size(), 0)); - WriteAny(stack_[0], byte_width); - // Write root type. - Write(stack_[0].StoredPackedType(), 1); - // Write root size. Normally determined by parent, but root has no parent :) - Write(byte_width, 1); - - finished_ = true; - } - - private: - void Finished() const { - // If you get this assert, you're attempting to get access a buffer - // which hasn't been finished yet. Be sure to call - // Builder::Finish with your root object. - FLATBUFFERS_ASSERT(finished_); - } - - // Align to prepare for writing a scalar with a certain size. - uint8_t Align(BitWidth alignment) { - auto byte_width = 1U << alignment; - buf_.insert(buf_.end(), flatbuffers::PaddingBytes(buf_.size(), byte_width), - 0); - return static_cast(byte_width); - } - - void WriteBytes(const void *val, size_t size) { - buf_.insert(buf_.end(), reinterpret_cast(val), - reinterpret_cast(val) + size); - } - - template void Write(T val, size_t byte_width) { - FLATBUFFERS_ASSERT(sizeof(T) >= byte_width); - val = flatbuffers::EndianScalar(val); - WriteBytes(&val, byte_width); - } - - void WriteDouble(double f, uint8_t byte_width) { - switch (byte_width) { - case 8: Write(f, byte_width); break; - case 4: Write(static_cast(f), byte_width); break; - // case 2: Write(static_cast(f), byte_width); break; - // case 1: Write(static_cast(f), byte_width); break; - default: FLATBUFFERS_ASSERT(0); - } - } - - void WriteOffset(uint64_t o, uint8_t byte_width) { - auto reloff = buf_.size() - o; - FLATBUFFERS_ASSERT(byte_width == 8 || reloff < 1ULL << (byte_width * 8)); - Write(reloff, byte_width); - } - - template void PushIndirect(T val, Type type, BitWidth bit_width) { - auto byte_width = Align(bit_width); - auto iloc = buf_.size(); - Write(val, byte_width); - stack_.push_back(Value(static_cast(iloc), type, bit_width)); - } - - static BitWidth WidthB(size_t byte_width) { - switch (byte_width) { - case 1: return BIT_WIDTH_8; - case 2: return BIT_WIDTH_16; - case 4: return BIT_WIDTH_32; - case 8: return BIT_WIDTH_64; - default: FLATBUFFERS_ASSERT(false); return BIT_WIDTH_64; - } - } - - template static Type GetScalarType() { - static_assert(flatbuffers::is_scalar::value, "Unrelated types"); - return flatbuffers::is_floating_point::value ? FBT_FLOAT - : flatbuffers::is_same::value - ? FBT_BOOL - : (flatbuffers::is_unsigned::value ? FBT_UINT : FBT_INT); - } - - public: - // This was really intended to be private, except for LastValue/ReuseValue. - struct Value { - union { - int64_t i_; - uint64_t u_; - double f_; - }; - - Type type_; - - // For scalars: of itself, for vector: of its elements, for string: length. - BitWidth min_bit_width_; - - Value() : i_(0), type_(FBT_NULL), min_bit_width_(BIT_WIDTH_8) {} - - Value(bool b) - : u_(static_cast(b)), - type_(FBT_BOOL), - min_bit_width_(BIT_WIDTH_8) {} - - Value(int64_t i, Type t, BitWidth bw) - : i_(i), type_(t), min_bit_width_(bw) {} - Value(uint64_t u, Type t, BitWidth bw) - : u_(u), type_(t), min_bit_width_(bw) {} - - Value(float f) - : f_(static_cast(f)), - type_(FBT_FLOAT), - min_bit_width_(BIT_WIDTH_32) {} - Value(double f) : f_(f), type_(FBT_FLOAT), min_bit_width_(WidthF(f)) {} - - uint8_t StoredPackedType(BitWidth parent_bit_width_ = BIT_WIDTH_8) const { - return PackedType(StoredWidth(parent_bit_width_), type_); - } - - BitWidth ElemWidth(size_t buf_size, size_t elem_index) const { - if (IsInline(type_)) { - return min_bit_width_; - } else { - // We have an absolute offset, but want to store a relative offset - // elem_index elements beyond the current buffer end. Since whether - // the relative offset fits in a certain byte_width depends on - // the size of the elements before it (and their alignment), we have - // to test for each size in turn. - for (size_t byte_width = 1; - byte_width <= sizeof(flatbuffers::largest_scalar_t); - byte_width *= 2) { - // Where are we going to write this offset? - auto offset_loc = buf_size + - flatbuffers::PaddingBytes(buf_size, byte_width) + - elem_index * byte_width; - // Compute relative offset. - auto offset = offset_loc - u_; - // Does it fit? - auto bit_width = WidthU(offset); - if (static_cast(static_cast(1U) << bit_width) == - byte_width) - return bit_width; - } - FLATBUFFERS_ASSERT(false); // Must match one of the sizes above. - return BIT_WIDTH_64; - } - } - - BitWidth StoredWidth(BitWidth parent_bit_width_ = BIT_WIDTH_8) const { - if (IsInline(type_)) { - return (std::max)(min_bit_width_, parent_bit_width_); - } else { - return min_bit_width_; - } - } - }; - - private: - void WriteAny(const Value &val, uint8_t byte_width) { - switch (val.type_) { - case FBT_NULL: - case FBT_INT: Write(val.i_, byte_width); break; - case FBT_BOOL: - case FBT_UINT: Write(val.u_, byte_width); break; - case FBT_FLOAT: WriteDouble(val.f_, byte_width); break; - default: WriteOffset(val.u_, byte_width); break; - } - } - - size_t CreateBlob(const void *data, size_t len, size_t trailing, Type type) { - auto bit_width = WidthU(len); - auto byte_width = Align(bit_width); - Write(len, byte_width); - auto sloc = buf_.size(); - WriteBytes(data, len + trailing); - stack_.push_back(Value(static_cast(sloc), type, bit_width)); - return sloc; - } - - template - size_t ScalarVector(const T *elems, size_t len, bool fixed) { - auto vector_type = GetScalarType(); - auto byte_width = sizeof(T); - auto bit_width = WidthB(byte_width); - // If you get this assert, you're trying to write a vector with a size - // field that is bigger than the scalars you're trying to write (e.g. a - // byte vector > 255 elements). For such types, write a "blob" instead. - // TODO: instead of asserting, could write vector with larger elements - // instead, though that would be wasteful. - FLATBUFFERS_ASSERT(WidthU(len) <= bit_width); - Align(bit_width); - if (!fixed) Write(len, byte_width); - auto vloc = buf_.size(); - for (size_t i = 0; i < len; i++) Write(elems[i], byte_width); - stack_.push_back(Value(static_cast(vloc), - ToTypedVector(vector_type, fixed ? len : 0), - bit_width)); - return vloc; - } - - Value CreateVector(size_t start, size_t vec_len, size_t step, bool typed, - bool fixed, const Value *keys = nullptr) { - FLATBUFFERS_ASSERT( - !fixed || - typed); // typed=false, fixed=true combination is not supported. - // Figure out smallest bit width we can store this vector with. - auto bit_width = (std::max)(force_min_bit_width_, WidthU(vec_len)); - auto prefix_elems = 1; - if (keys) { - // If this vector is part of a map, we will pre-fix an offset to the keys - // to this vector. - bit_width = (std::max)(bit_width, keys->ElemWidth(buf_.size(), 0)); - prefix_elems += 2; - } - Type vector_type = FBT_KEY; - // Check bit widths and types for all elements. - for (size_t i = start; i < stack_.size(); i += step) { - auto elem_width = - stack_[i].ElemWidth(buf_.size(), i - start + prefix_elems); - bit_width = (std::max)(bit_width, elem_width); - if (typed) { - if (i == start) { - vector_type = stack_[i].type_; - } else { - // If you get this assert, you are writing a typed vector with - // elements that are not all the same type. - FLATBUFFERS_ASSERT(vector_type == stack_[i].type_); - } - } - } - // If you get this assert, your typed types are not one of: - // Int / UInt / Float / Key. - FLATBUFFERS_ASSERT(!typed || IsTypedVectorElementType(vector_type)); - auto byte_width = Align(bit_width); - // Write vector. First the keys width/offset if available, and size. - if (keys) { - WriteOffset(keys->u_, byte_width); - Write(1ULL << keys->min_bit_width_, byte_width); - } - if (!fixed) Write(vec_len, byte_width); - // Then the actual data. - auto vloc = buf_.size(); - for (size_t i = start; i < stack_.size(); i += step) { - WriteAny(stack_[i], byte_width); - } - // Then the types. - if (!typed) { - for (size_t i = start; i < stack_.size(); i += step) { - buf_.push_back(stack_[i].StoredPackedType(bit_width)); - } - } - return Value(static_cast(vloc), - keys ? FBT_MAP - : (typed ? ToTypedVector(vector_type, fixed ? vec_len : 0) - : FBT_VECTOR), - bit_width); - } - - // You shouldn't really be copying instances of this class. - Builder(const Builder &); - Builder &operator=(const Builder &); - - std::vector buf_; - std::vector stack_; - - bool finished_; - bool has_duplicate_keys_; - - BuilderFlag flags_; - - BitWidth force_min_bit_width_; - - struct KeyOffsetCompare { - explicit KeyOffsetCompare(const std::vector &buf) : buf_(&buf) {} - bool operator()(size_t a, size_t b) const { - auto stra = reinterpret_cast(buf_->data() + a); - auto strb = reinterpret_cast(buf_->data() + b); - return strcmp(stra, strb) < 0; - } - const std::vector *buf_; - }; - - typedef std::pair StringOffset; - struct StringOffsetCompare { - explicit StringOffsetCompare(const std::vector &buf) - : buf_(&buf) {} - bool operator()(const StringOffset &a, const StringOffset &b) const { - auto stra = buf_->data() + a.first; - auto strb = buf_->data() + b.first; - auto cr = memcmp(stra, strb, (std::min)(a.second, b.second) + 1); - return cr < 0 || (cr == 0 && a.second < b.second); - } - const std::vector *buf_; - }; - - typedef std::set KeyOffsetMap; - typedef std::set StringOffsetMap; - - KeyOffsetMap key_pool; - StringOffsetMap string_pool; - - friend class Verifier; -}; - -// Helper class to verify the integrity of a FlexBuffer -class Verifier FLATBUFFERS_FINAL_CLASS { - public: - Verifier(const uint8_t *buf, size_t buf_len, - // Supplying this vector likely results in faster verification - // of larger buffers with many shared keys/strings, but - // comes at the cost of using additional memory the same size of - // the buffer being verified, so it is by default off. - std::vector *reuse_tracker = nullptr, - bool _check_alignment = true, - size_t max_depth = 64) - : buf_(buf), - size_(buf_len), - depth_(0), - max_depth_(max_depth), - num_vectors_(0), - max_vectors_(buf_len), - check_alignment_(_check_alignment), - reuse_tracker_(reuse_tracker) { - FLATBUFFERS_ASSERT(size_ < FLATBUFFERS_MAX_BUFFER_SIZE); - if (reuse_tracker_) { - reuse_tracker_->clear(); - reuse_tracker_->resize(size_, PackedType(BIT_WIDTH_8, FBT_NULL)); - } - } - - private: - // Central location where any verification failures register. - bool Check(bool ok) const { - // clang-format off - #ifdef FLATBUFFERS_DEBUG_VERIFICATION_FAILURE - FLATBUFFERS_ASSERT(ok); - #endif - // clang-format on - return ok; - } - - // Verify any range within the buffer. - bool VerifyFrom(size_t elem, size_t elem_len) const { - return Check(elem_len < size_ && elem <= size_ - elem_len); - } - bool VerifyBefore(size_t elem, size_t elem_len) const { - return Check(elem_len <= elem); - } - - bool VerifyFromPointer(const uint8_t *p, size_t len) { - auto o = static_cast(p - buf_); - return VerifyFrom(o, len); - } - bool VerifyBeforePointer(const uint8_t *p, size_t len) { - auto o = static_cast(p - buf_); - return VerifyBefore(o, len); - } - - bool VerifyByteWidth(size_t width) { - return Check(width == 1 || width == 2 || width == 4 || width == 8); - } - - bool VerifyType(int type) { - return Check(type >= 0 && type < FBT_MAX_TYPE); - } - - bool VerifyOffset(uint64_t off, const uint8_t *p) { - return Check(off <= static_cast(size_)) && - off <= static_cast(p - buf_); - } - - bool VerifyAlignment(const uint8_t *p, size_t size) const { - auto o = static_cast(p - buf_); - return Check((o & (size - 1)) == 0 || !check_alignment_); - } - - // Macro, since we want to escape from parent function & use lazy args. - #define FLEX_CHECK_VERIFIED(P, PACKED_TYPE) \ - if (reuse_tracker_) { \ - auto packed_type = PACKED_TYPE; \ - auto existing = (*reuse_tracker_)[P - buf_]; \ - if (existing == packed_type) return true; \ - /* Fail verification if already set with different type! */ \ - if (!Check(existing == 0)) return false; \ - (*reuse_tracker_)[P - buf_] = packed_type; \ - } - - bool VerifyVector(Reference r, const uint8_t *p, Type elem_type) { - // Any kind of nesting goes thru this function, so guard against that - // here, both with simple nesting checks, and the reuse tracker if on. - depth_++; - num_vectors_++; - if (!Check(depth_ <= max_depth_ && num_vectors_ <= max_vectors_)) - return false; - auto size_byte_width = r.byte_width_; - FLEX_CHECK_VERIFIED(p, PackedType(Builder::WidthB(size_byte_width), r.type_)); - if (!VerifyBeforePointer(p, size_byte_width)) - return false; - auto sized = Sized(p, size_byte_width); - auto num_elems = sized.size(); - auto elem_byte_width = - r.type_ == FBT_STRING || r.type_ == FBT_BLOB ? uint8_t(1) : r.byte_width_; - auto max_elems = SIZE_MAX / elem_byte_width; - if (!Check(num_elems < max_elems)) - return false; // Protect against byte_size overflowing. - auto byte_size = num_elems * elem_byte_width; - if (!VerifyFromPointer(p, byte_size)) - return false; - if (elem_type == FBT_NULL) { - // Verify type bytes after the vector. - if (!VerifyFromPointer(p + byte_size, num_elems)) return false; - auto v = Vector(p, size_byte_width); - for (size_t i = 0; i < num_elems; i++) - if (!VerifyRef(v[i])) return false; - } else if (elem_type == FBT_KEY) { - auto v = TypedVector(p, elem_byte_width, FBT_KEY); - for (size_t i = 0; i < num_elems; i++) - if (!VerifyRef(v[i])) return false; - } else { - FLATBUFFERS_ASSERT(IsInline(elem_type)); - } - depth_--; - return true; - } - - bool VerifyKeys(const uint8_t *p, uint8_t byte_width) { - // The vector part of the map has already been verified. - const size_t num_prefixed_fields = 3; - if (!VerifyBeforePointer(p, byte_width * num_prefixed_fields)) - return false; - p -= byte_width * num_prefixed_fields; - auto off = ReadUInt64(p, byte_width); - if (!VerifyOffset(off, p)) - return false; - auto key_byte_with = - static_cast(ReadUInt64(p + byte_width, byte_width)); - if (!VerifyByteWidth(key_byte_with)) - return false; - return VerifyVector(Reference(p, byte_width, key_byte_with, FBT_VECTOR_KEY), - p - off, FBT_KEY); - } - - bool VerifyKey(const uint8_t* p) { - FLEX_CHECK_VERIFIED(p, PackedType(BIT_WIDTH_8, FBT_KEY)); - while (p < buf_ + size_) - if (*p++) return true; - return false; - } - - #undef FLEX_CHECK_VERIFIED - - bool VerifyTerminator(const String &s) { - return VerifyFromPointer(reinterpret_cast(s.c_str()), - s.size() + 1); - } - - bool VerifyRef(Reference r) { - // r.parent_width_ and r.data_ already verified. - if (!VerifyByteWidth(r.byte_width_) || !VerifyType(r.type_)) { - return false; - } - if (IsInline(r.type_)) { - // Inline scalars, don't require further verification. - return true; - } - // All remaining types are an offset. - auto off = ReadUInt64(r.data_, r.parent_width_); - if (!VerifyOffset(off, r.data_)) - return false; - auto p = r.Indirect(); - if (!VerifyAlignment(p, r.byte_width_)) - return false; - switch (r.type_) { - case FBT_INDIRECT_INT: - case FBT_INDIRECT_UINT: - case FBT_INDIRECT_FLOAT: - return VerifyFromPointer(p, r.byte_width_); - case FBT_KEY: - return VerifyKey(p); - case FBT_MAP: - return VerifyVector(r, p, FBT_NULL) && - VerifyKeys(p, r.byte_width_); - case FBT_VECTOR: - return VerifyVector(r, p, FBT_NULL); - case FBT_VECTOR_INT: - return VerifyVector(r, p, FBT_INT); - case FBT_VECTOR_BOOL: - case FBT_VECTOR_UINT: - return VerifyVector(r, p, FBT_UINT); - case FBT_VECTOR_FLOAT: - return VerifyVector(r, p, FBT_FLOAT); - case FBT_VECTOR_KEY: - return VerifyVector(r, p, FBT_KEY); - case FBT_VECTOR_STRING_DEPRECATED: - // Use of FBT_KEY here intentional, see elsewhere. - return VerifyVector(r, p, FBT_KEY); - case FBT_BLOB: - return VerifyVector(r, p, FBT_UINT); - case FBT_STRING: - return VerifyVector(r, p, FBT_UINT) && - VerifyTerminator(String(p, r.byte_width_)); - case FBT_VECTOR_INT2: - case FBT_VECTOR_UINT2: - case FBT_VECTOR_FLOAT2: - case FBT_VECTOR_INT3: - case FBT_VECTOR_UINT3: - case FBT_VECTOR_FLOAT3: - case FBT_VECTOR_INT4: - case FBT_VECTOR_UINT4: - case FBT_VECTOR_FLOAT4: { - uint8_t len = 0; - auto vtype = ToFixedTypedVectorElementType(r.type_, &len); - if (!VerifyType(vtype)) - return false; - return VerifyFromPointer(p, r.byte_width_ * len); - } - default: - return false; - } - } - - public: - bool VerifyBuffer() { - if (!Check(size_ >= 3)) return false; - auto end = buf_ + size_; - auto byte_width = *--end; - auto packed_type = *--end; - return VerifyByteWidth(byte_width) && - Check(end - buf_ >= byte_width) && - VerifyRef(Reference(end - byte_width, byte_width, packed_type)); - } - - private: - const uint8_t *buf_; - size_t size_; - size_t depth_; - const size_t max_depth_; - size_t num_vectors_; - const size_t max_vectors_; - bool check_alignment_; - std::vector *reuse_tracker_; -}; - -// Utility function that contructs the Verifier for you, see above for parameters. -inline bool VerifyBuffer(const uint8_t *buf, size_t buf_len, - std::vector *reuse_tracker = nullptr) { - Verifier verifier(buf, buf_len, reuse_tracker); - return verifier.VerifyBuffer(); -} - - -#ifdef FLATBUFFERS_H_ -// This is a verifier utility function that works together with the -// FlatBuffers verifier, which should only be present if flatbuffer.h -// has been included (which it typically is in generated code). -inline bool VerifyNestedFlexBuffer(const flatbuffers::Vector *nv, - flatbuffers::Verifier &verifier) { - if (!nv) return true; - return verifier.Check( - flexbuffers::VerifyBuffer(nv->data(), nv->size(), - verifier.GetFlexReuseTracker())); -} -#endif - -} // namespace flexbuffers - -#if defined(_MSC_VER) -# pragma warning(pop) -#endif - -#endif // FLATBUFFERS_FLEXBUFFERS_H_ diff --git a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/stl_emulation.h b/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/stl_emulation.h deleted file mode 100644 index 75d13b29..00000000 --- a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/stl_emulation.h +++ /dev/null @@ -1,509 +0,0 @@ -/* - * Copyright 2017 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FLATBUFFERS_STL_EMULATION_H_ -#define FLATBUFFERS_STL_EMULATION_H_ - -// clang-format off -#include "flatbuffers/base.h" - -#include -#include -#include -#include -#include - -// Detect C++17 compatible compiler. -// __cplusplus >= 201703L - a compiler has support of 'static inline' variables. -#if defined(FLATBUFFERS_USE_STD_OPTIONAL) \ - || (defined(__cplusplus) && __cplusplus >= 201703L) \ - || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)) - #include - #ifndef FLATBUFFERS_USE_STD_OPTIONAL - #define FLATBUFFERS_USE_STD_OPTIONAL - #endif -#endif // defined(FLATBUFFERS_USE_STD_OPTIONAL) ... - -// The __cpp_lib_span is the predefined feature macro. -#if defined(FLATBUFFERS_USE_STD_SPAN) - #include -#elif defined(__cpp_lib_span) && defined(__has_include) - #if __has_include() - #include - #define FLATBUFFERS_USE_STD_SPAN - #endif -#else - // Disable non-trivial ctors if FLATBUFFERS_SPAN_MINIMAL defined. - #if !defined(FLATBUFFERS_TEMPLATES_ALIASES) - #define FLATBUFFERS_SPAN_MINIMAL - #else - // Enable implicit construction of a span from a std::array. - #include - #endif -#endif // defined(FLATBUFFERS_USE_STD_SPAN) - -// This header provides backwards compatibility for older versions of the STL. -namespace flatbuffers { - -#if defined(FLATBUFFERS_TEMPLATES_ALIASES) - template - using numeric_limits = std::numeric_limits; -#else - template class numeric_limits : - public std::numeric_limits {}; -#endif // defined(FLATBUFFERS_TEMPLATES_ALIASES) - -#if defined(FLATBUFFERS_TEMPLATES_ALIASES) - template using is_scalar = std::is_scalar; - template using is_same = std::is_same; - template using is_floating_point = std::is_floating_point; - template using is_unsigned = std::is_unsigned; - template using is_enum = std::is_enum; - template using make_unsigned = std::make_unsigned; - template - using conditional = std::conditional; - template - using integral_constant = std::integral_constant; - template - using bool_constant = integral_constant; - using true_type = std::true_type; - using false_type = std::false_type; -#else - // MSVC 2010 doesn't support C++11 aliases. - template struct is_scalar : public std::is_scalar {}; - template struct is_same : public std::is_same {}; - template struct is_floating_point : - public std::is_floating_point {}; - template struct is_unsigned : public std::is_unsigned {}; - template struct is_enum : public std::is_enum {}; - template struct make_unsigned : public std::make_unsigned {}; - template - struct conditional : public std::conditional {}; - template - struct integral_constant : public std::integral_constant {}; - template - struct bool_constant : public integral_constant {}; - typedef bool_constant true_type; - typedef bool_constant false_type; -#endif // defined(FLATBUFFERS_TEMPLATES_ALIASES) - -#if defined(FLATBUFFERS_TEMPLATES_ALIASES) - template using unique_ptr = std::unique_ptr; -#else - // MSVC 2010 doesn't support C++11 aliases. - // We're manually "aliasing" the class here as we want to bring unique_ptr - // into the flatbuffers namespace. We have unique_ptr in the flatbuffers - // namespace we have a completely independent implementation (see below) - // for C++98 STL implementations. - template class unique_ptr : public std::unique_ptr { - public: - unique_ptr() {} - explicit unique_ptr(T* p) : std::unique_ptr(p) {} - unique_ptr(std::unique_ptr&& u) { *this = std::move(u); } - unique_ptr(unique_ptr&& u) { *this = std::move(u); } - unique_ptr& operator=(std::unique_ptr&& u) { - std::unique_ptr::reset(u.release()); - return *this; - } - unique_ptr& operator=(unique_ptr&& u) { - std::unique_ptr::reset(u.release()); - return *this; - } - unique_ptr& operator=(T* p) { - return std::unique_ptr::operator=(p); - } - }; -#endif // defined(FLATBUFFERS_TEMPLATES_ALIASES) - -#ifdef FLATBUFFERS_USE_STD_OPTIONAL -template -using Optional = std::optional; -using nullopt_t = std::nullopt_t; -inline constexpr nullopt_t nullopt = std::nullopt; - -#else -// Limited implementation of Optional type for a scalar T. -// This implementation limited by trivial types compatible with -// std::is_arithmetic or std::is_enum type traits. - -// A tag to indicate an empty flatbuffers::optional. -struct nullopt_t { - explicit FLATBUFFERS_CONSTEXPR_CPP11 nullopt_t(int) {} -}; - -#if defined(FLATBUFFERS_CONSTEXPR_DEFINED) - namespace internal { - template struct nullopt_holder { - static constexpr nullopt_t instance_ = nullopt_t(0); - }; - template - constexpr nullopt_t nullopt_holder::instance_; - } - static constexpr const nullopt_t &nullopt = internal::nullopt_holder::instance_; - -#else - namespace internal { - template struct nullopt_holder { - static const nullopt_t instance_; - }; - template - const nullopt_t nullopt_holder::instance_ = nullopt_t(0); - } - static const nullopt_t &nullopt = internal::nullopt_holder::instance_; - -#endif - -template -class Optional FLATBUFFERS_FINAL_CLASS { - // Non-scalar 'T' would extremely complicated Optional. - // Use is_scalar checking because flatbuffers flatbuffers::is_arithmetic - // isn't implemented. - static_assert(flatbuffers::is_scalar::value, "unexpected type T"); - - public: - ~Optional() {} - - FLATBUFFERS_CONSTEXPR_CPP11 Optional() FLATBUFFERS_NOEXCEPT - : value_(), has_value_(false) {} - - FLATBUFFERS_CONSTEXPR_CPP11 Optional(nullopt_t) FLATBUFFERS_NOEXCEPT - : value_(), has_value_(false) {} - - FLATBUFFERS_CONSTEXPR_CPP11 Optional(T val) FLATBUFFERS_NOEXCEPT - : value_(val), has_value_(true) {} - - FLATBUFFERS_CONSTEXPR_CPP11 Optional(const Optional &other) FLATBUFFERS_NOEXCEPT - : value_(other.value_), has_value_(other.has_value_) {} - - FLATBUFFERS_CONSTEXPR_CPP14 Optional &operator=(const Optional &other) FLATBUFFERS_NOEXCEPT { - value_ = other.value_; - has_value_ = other.has_value_; - return *this; - } - - FLATBUFFERS_CONSTEXPR_CPP14 Optional &operator=(nullopt_t) FLATBUFFERS_NOEXCEPT { - value_ = T(); - has_value_ = false; - return *this; - } - - FLATBUFFERS_CONSTEXPR_CPP14 Optional &operator=(T val) FLATBUFFERS_NOEXCEPT { - value_ = val; - has_value_ = true; - return *this; - } - - void reset() FLATBUFFERS_NOEXCEPT { - *this = nullopt; - } - - void swap(Optional &other) FLATBUFFERS_NOEXCEPT { - std::swap(value_, other.value_); - std::swap(has_value_, other.has_value_); - } - - FLATBUFFERS_CONSTEXPR_CPP11 FLATBUFFERS_EXPLICIT_CPP11 operator bool() const FLATBUFFERS_NOEXCEPT { - return has_value_; - } - - FLATBUFFERS_CONSTEXPR_CPP11 bool has_value() const FLATBUFFERS_NOEXCEPT { - return has_value_; - } - - FLATBUFFERS_CONSTEXPR_CPP11 const T& operator*() const FLATBUFFERS_NOEXCEPT { - return value_; - } - - const T& value() const { - FLATBUFFERS_ASSERT(has_value()); - return value_; - } - - T value_or(T default_value) const FLATBUFFERS_NOEXCEPT { - return has_value() ? value_ : default_value; - } - - private: - T value_; - bool has_value_; -}; - -template -FLATBUFFERS_CONSTEXPR_CPP11 bool operator==(const Optional& opt, nullopt_t) FLATBUFFERS_NOEXCEPT { - return !opt; -} -template -FLATBUFFERS_CONSTEXPR_CPP11 bool operator==(nullopt_t, const Optional& opt) FLATBUFFERS_NOEXCEPT { - return !opt; -} - -template -FLATBUFFERS_CONSTEXPR_CPP11 bool operator==(const Optional& lhs, const U& rhs) FLATBUFFERS_NOEXCEPT { - return static_cast(lhs) && (*lhs == rhs); -} - -template -FLATBUFFERS_CONSTEXPR_CPP11 bool operator==(const T& lhs, const Optional& rhs) FLATBUFFERS_NOEXCEPT { - return static_cast(rhs) && (lhs == *rhs); -} - -template -FLATBUFFERS_CONSTEXPR_CPP11 bool operator==(const Optional& lhs, const Optional& rhs) FLATBUFFERS_NOEXCEPT { - return static_cast(lhs) != static_cast(rhs) - ? false - : !static_cast(lhs) ? false : (*lhs == *rhs); -} -#endif // FLATBUFFERS_USE_STD_OPTIONAL - - -// Very limited and naive partial implementation of C++20 std::span. -#if defined(FLATBUFFERS_USE_STD_SPAN) - inline constexpr std::size_t dynamic_extent = std::dynamic_extent; - template - using span = std::span; - -#else // !defined(FLATBUFFERS_USE_STD_SPAN) -FLATBUFFERS_CONSTEXPR std::size_t dynamic_extent = static_cast(-1); - -// Exclude this code if MSVC2010 or non-STL Android is active. -// The non-STL Android doesn't have `std::is_convertible` required for SFINAE. -#if !defined(FLATBUFFERS_SPAN_MINIMAL) -namespace internal { - // This is SFINAE helper class for checking of a common condition: - // > This overload only participates in overload resolution - // > Check whether a pointer to an array of U can be converted - // > to a pointer to an array of E. - // This helper is used for checking of 'U -> const U'. - template - struct is_span_convertable { - using type = - typename std::conditional::value - && (Extent == dynamic_extent || N == Extent), - int, void>::type; - }; - - template - struct SpanIterator { - // TODO: upgrade to std::random_access_iterator_tag. - using iterator_category = std::forward_iterator_tag; - using difference_type = std::ptrdiff_t; - using value_type = typename std::remove_cv::type; - using reference = T&; - using pointer = T*; - - // Convince MSVC compiler that this iterator is trusted (it is verified). - #ifdef _MSC_VER - using _Unchecked_type = pointer; - #endif // _MSC_VER - - SpanIterator(pointer ptr) : ptr_(ptr) {} - reference operator*() const { return *ptr_; } - pointer operator->() { return ptr_; } - SpanIterator& operator++() { ptr_++; return *this; } - SpanIterator operator++(int) { auto tmp = *this; ++(*this); return tmp; } - - friend bool operator== (const SpanIterator& lhs, const SpanIterator& rhs) { return lhs.ptr_ == rhs.ptr_; } - friend bool operator!= (const SpanIterator& lhs, const SpanIterator& rhs) { return lhs.ptr_ != rhs.ptr_; } - - private: - pointer ptr_; - }; -} // namespace internal -#endif // !defined(FLATBUFFERS_SPAN_MINIMAL) - -// T - element type; must be a complete type that is not an abstract -// class type. -// Extent - the number of elements in the sequence, or dynamic. -template -class span FLATBUFFERS_FINAL_CLASS { - public: - typedef T element_type; - typedef T& reference; - typedef const T& const_reference; - typedef T* pointer; - typedef const T* const_pointer; - typedef std::size_t size_type; - - static FLATBUFFERS_CONSTEXPR size_type extent = Extent; - - // Returns the number of elements in the span. - FLATBUFFERS_CONSTEXPR_CPP11 size_type size() const FLATBUFFERS_NOEXCEPT { - return count_; - } - - // Returns the size of the sequence in bytes. - FLATBUFFERS_CONSTEXPR_CPP11 - size_type size_bytes() const FLATBUFFERS_NOEXCEPT { - return size() * sizeof(element_type); - } - - // Checks if the span is empty. - FLATBUFFERS_CONSTEXPR_CPP11 bool empty() const FLATBUFFERS_NOEXCEPT { - return size() == 0; - } - - // Returns a pointer to the beginning of the sequence. - FLATBUFFERS_CONSTEXPR_CPP11 pointer data() const FLATBUFFERS_NOEXCEPT { - return data_; - } - - #if !defined(FLATBUFFERS_SPAN_MINIMAL) - using Iterator = internal::SpanIterator; - using ConstIterator = internal::SpanIterator; - - Iterator begin() const { return Iterator(data()); } - Iterator end() const { return Iterator(data() + size()); } - - ConstIterator cbegin() const { return ConstIterator(data()); } - ConstIterator cend() const { return ConstIterator(data() + size()); } - #endif - - // Returns a reference to the idx-th element of the sequence. - // The behavior is undefined if the idx is greater than or equal to size(). - FLATBUFFERS_CONSTEXPR_CPP11 reference operator[](size_type idx) const { - return data()[idx]; - } - - FLATBUFFERS_CONSTEXPR_CPP11 span(const span &other) FLATBUFFERS_NOEXCEPT - : data_(other.data_), count_(other.count_) {} - - FLATBUFFERS_CONSTEXPR_CPP14 span &operator=(const span &other) - FLATBUFFERS_NOEXCEPT { - data_ = other.data_; - count_ = other.count_; - } - - // Limited implementation of - // `template constexpr std::span(It first, size_type count);`. - // - // Constructs a span that is a view over the range [first, first + count); - // the resulting span has: data() == first and size() == count. - // The behavior is undefined if [first, first + count) is not a valid range, - // or if (extent != flatbuffers::dynamic_extent && count != extent). - FLATBUFFERS_CONSTEXPR_CPP11 - explicit span(pointer first, size_type count) FLATBUFFERS_NOEXCEPT - : data_ (Extent == dynamic_extent ? first : (Extent == count ? first : nullptr)), - count_(Extent == dynamic_extent ? count : (Extent == count ? Extent : 0)) { - // Make span empty if the count argument is incompatible with span. - } - - // Exclude this code if MSVC2010 is active. The MSVC2010 isn't C++11 - // compliant, it doesn't support default template arguments for functions. - #if defined(FLATBUFFERS_SPAN_MINIMAL) - FLATBUFFERS_CONSTEXPR_CPP11 span() FLATBUFFERS_NOEXCEPT : data_(nullptr), - count_(0) { - static_assert(extent == 0 || extent == dynamic_extent, "invalid span"); - } - - #else - // Constructs an empty span whose data() == nullptr and size() == 0. - // This overload only participates in overload resolution if - // extent == 0 || extent == flatbuffers::dynamic_extent. - // A dummy template argument N is need dependency for SFINAE. - template::type = 0> - FLATBUFFERS_CONSTEXPR_CPP11 span() FLATBUFFERS_NOEXCEPT : data_(nullptr), - count_(0) { - static_assert(extent == 0 || extent == dynamic_extent, "invalid span"); - } - - // Constructs a span that is a view over the array arr; the resulting span - // has size() == N and data() == std::data(arr). These overloads only - // participate in overload resolution if - // extent == std::dynamic_extent || N == extent is true and - // std::remove_pointer_t(*)[] - // is convertible to element_type (*)[]. - template::type = 0> - FLATBUFFERS_CONSTEXPR_CPP11 span(element_type (&arr)[N]) FLATBUFFERS_NOEXCEPT - : data_(arr), count_(N) {} - - template::type = 0> - FLATBUFFERS_CONSTEXPR_CPP11 span(std::array &arr) FLATBUFFERS_NOEXCEPT - : data_(arr.data()), count_(N) {} - - //template - //FLATBUFFERS_CONSTEXPR_CPP11 span(std::array &arr) FLATBUFFERS_NOEXCEPT - // : data_(arr.data()), count_(N) {} - - template::type = 0> - FLATBUFFERS_CONSTEXPR_CPP11 span(const std::array &arr) FLATBUFFERS_NOEXCEPT - : data_(arr.data()), count_(N) {} - - // Converting constructor from another span s; - // the resulting span has size() == s.size() and data() == s.data(). - // This overload only participates in overload resolution - // if extent == std::dynamic_extent || N == extent is true and U (*)[] - // is convertible to element_type (*)[]. - template::type = 0> - FLATBUFFERS_CONSTEXPR_CPP11 span(const flatbuffers::span &s) FLATBUFFERS_NOEXCEPT - : span(s.data(), s.size()) { - } - - #endif // !defined(FLATBUFFERS_SPAN_MINIMAL) - - private: - // This is a naive implementation with 'count_' member even if (Extent != dynamic_extent). - pointer const data_; - const size_type count_; -}; -#endif // defined(FLATBUFFERS_USE_STD_SPAN) - -#if !defined(FLATBUFFERS_SPAN_MINIMAL) -template -FLATBUFFERS_CONSTEXPR_CPP11 -flatbuffers::span make_span(U(&arr)[N]) FLATBUFFERS_NOEXCEPT { - return span(arr); -} - -template -FLATBUFFERS_CONSTEXPR_CPP11 -flatbuffers::span make_span(const U(&arr)[N]) FLATBUFFERS_NOEXCEPT { - return span(arr); -} - -template -FLATBUFFERS_CONSTEXPR_CPP11 -flatbuffers::span make_span(std::array &arr) FLATBUFFERS_NOEXCEPT { - return span(arr); -} - -template -FLATBUFFERS_CONSTEXPR_CPP11 -flatbuffers::span make_span(const std::array &arr) FLATBUFFERS_NOEXCEPT { - return span(arr); -} - -template -FLATBUFFERS_CONSTEXPR_CPP11 -flatbuffers::span make_span(U *first, std::size_t count) FLATBUFFERS_NOEXCEPT { - return span(first, count); -} - -template -FLATBUFFERS_CONSTEXPR_CPP11 -flatbuffers::span make_span(const U *first, std::size_t count) FLATBUFFERS_NOEXCEPT { - return span(first, count); -} -#endif // !defined(FLATBUFFERS_SPAN_MINIMAL) - -} // namespace flatbuffers - -#endif // FLATBUFFERS_STL_EMULATION_H_ diff --git a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/string.h b/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/string.h deleted file mode 100644 index 3db95fce..00000000 --- a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/string.h +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright 2021 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FLATBUFFERS_STRING_H_ -#define FLATBUFFERS_STRING_H_ - -#include "flatbuffers/base.h" -#include "flatbuffers/vector.h" - -namespace flatbuffers { - -struct String : public Vector { - const char *c_str() const { return reinterpret_cast(Data()); } - std::string str() const { return std::string(c_str(), size()); } - - // clang-format off - #ifdef FLATBUFFERS_HAS_STRING_VIEW - flatbuffers::string_view string_view() const { - return flatbuffers::string_view(c_str(), size()); - } - #endif // FLATBUFFERS_HAS_STRING_VIEW - // clang-format on - - bool operator<(const String &o) const { - return StringLessThan(this->data(), this->size(), o.data(), o.size()); - } -}; - -// Convenience function to get std::string from a String returning an empty -// string on null pointer. -static inline std::string GetString(const String *str) { - return str ? str->str() : ""; -} - -// Convenience function to get char* from a String returning an empty string on -// null pointer. -static inline const char *GetCstring(const String *str) { - return str ? str->c_str() : ""; -} - -#ifdef FLATBUFFERS_HAS_STRING_VIEW -// Convenience function to get string_view from a String returning an empty -// string_view on null pointer. -static inline flatbuffers::string_view GetStringView(const String *str) { - return str ? str->string_view() : flatbuffers::string_view(); -} -#endif // FLATBUFFERS_HAS_STRING_VIEW - -} // namespace flatbuffers - -#endif // FLATBUFFERS_STRING_H_ \ No newline at end of file diff --git a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/struct.h b/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/struct.h deleted file mode 100644 index d8753c84..00000000 --- a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/struct.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2021 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FLATBUFFERS_STRUCT_H_ -#define FLATBUFFERS_STRUCT_H_ - -#include "flatbuffers/base.h" - -namespace flatbuffers { - -// "structs" are flat structures that do not have an offset table, thus -// always have all members present and do not support forwards/backwards -// compatible extensions. - -class Struct FLATBUFFERS_FINAL_CLASS { - public: - template T GetField(uoffset_t o) const { - return ReadScalar(&data_[o]); - } - - template T GetStruct(uoffset_t o) const { - return reinterpret_cast(&data_[o]); - } - - const uint8_t *GetAddressOf(uoffset_t o) const { return &data_[o]; } - uint8_t *GetAddressOf(uoffset_t o) { return &data_[o]; } - - private: - // private constructor & copy constructor: you obtain instances of this - // class by pointing to existing data only - Struct(); - Struct(const Struct &); - Struct &operator=(const Struct &); - - uint8_t data_[1]; -}; - -} // namespace flatbuffers - -#endif // FLATBUFFERS_STRUCT_H_ \ No newline at end of file diff --git a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/table.h b/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/table.h deleted file mode 100644 index 42470693..00000000 --- a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/table.h +++ /dev/null @@ -1,166 +0,0 @@ -/* - * Copyright 2021 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FLATBUFFERS_TABLE_H_ -#define FLATBUFFERS_TABLE_H_ - -#include "flatbuffers/base.h" -#include "flatbuffers/verifier.h" - -namespace flatbuffers { - -// "tables" use an offset table (possibly shared) that allows fields to be -// omitted and added at will, but uses an extra indirection to read. -class Table { - public: - const uint8_t *GetVTable() const { - return data_ - ReadScalar(data_); - } - - // This gets the field offset for any of the functions below it, or 0 - // if the field was not present. - voffset_t GetOptionalFieldOffset(voffset_t field) const { - // The vtable offset is always at the start. - auto vtable = GetVTable(); - // The first element is the size of the vtable (fields + type id + itself). - auto vtsize = ReadScalar(vtable); - // If the field we're accessing is outside the vtable, we're reading older - // data, so it's the same as if the offset was 0 (not present). - return field < vtsize ? ReadScalar(vtable + field) : 0; - } - - template T GetField(voffset_t field, T defaultval) const { - auto field_offset = GetOptionalFieldOffset(field); - return field_offset ? ReadScalar(data_ + field_offset) : defaultval; - } - - template P GetPointer(voffset_t field) { - auto field_offset = GetOptionalFieldOffset(field); - auto p = data_ + field_offset; - return field_offset ? reinterpret_cast

(p + ReadScalar(p)) - : nullptr; - } - template P GetPointer(voffset_t field) const { - return const_cast(this)->GetPointer

(field); - } - - template P GetStruct(voffset_t field) const { - auto field_offset = GetOptionalFieldOffset(field); - auto p = const_cast(data_ + field_offset); - return field_offset ? reinterpret_cast

(p) : nullptr; - } - - template - flatbuffers::Optional GetOptional(voffset_t field) const { - auto field_offset = GetOptionalFieldOffset(field); - auto p = data_ + field_offset; - return field_offset ? Optional(static_cast(ReadScalar(p))) - : Optional(); - } - - template bool SetField(voffset_t field, T val, T def) { - auto field_offset = GetOptionalFieldOffset(field); - if (!field_offset) return IsTheSameAs(val, def); - WriteScalar(data_ + field_offset, val); - return true; - } - template bool SetField(voffset_t field, T val) { - auto field_offset = GetOptionalFieldOffset(field); - if (!field_offset) return false; - WriteScalar(data_ + field_offset, val); - return true; - } - - bool SetPointer(voffset_t field, const uint8_t *val) { - auto field_offset = GetOptionalFieldOffset(field); - if (!field_offset) return false; - WriteScalar(data_ + field_offset, - static_cast(val - (data_ + field_offset))); - return true; - } - - uint8_t *GetAddressOf(voffset_t field) { - auto field_offset = GetOptionalFieldOffset(field); - return field_offset ? data_ + field_offset : nullptr; - } - const uint8_t *GetAddressOf(voffset_t field) const { - return const_cast

(this)->GetAddressOf(field); - } - - bool CheckField(voffset_t field) const { - return GetOptionalFieldOffset(field) != 0; - } - - // Verify the vtable of this table. - // Call this once per table, followed by VerifyField once per field. - bool VerifyTableStart(Verifier &verifier) const { - return verifier.VerifyTableStart(data_); - } - - // Verify a particular field. - template - bool VerifyField(const Verifier &verifier, voffset_t field) const { - // Calling GetOptionalFieldOffset should be safe now thanks to - // VerifyTable(). - auto field_offset = GetOptionalFieldOffset(field); - // Check the actual field. - return !field_offset || verifier.Verify(data_, field_offset); - } - - // VerifyField for required fields. - template - bool VerifyFieldRequired(const Verifier &verifier, voffset_t field) const { - auto field_offset = GetOptionalFieldOffset(field); - return verifier.Check(field_offset != 0) && - verifier.Verify(data_, field_offset); - } - - // Versions for offsets. - bool VerifyOffset(const Verifier &verifier, voffset_t field) const { - auto field_offset = GetOptionalFieldOffset(field); - return !field_offset || verifier.VerifyOffset(data_, field_offset); - } - - bool VerifyOffsetRequired(const Verifier &verifier, voffset_t field) const { - auto field_offset = GetOptionalFieldOffset(field); - return verifier.Check(field_offset != 0) && - verifier.VerifyOffset(data_, field_offset); - } - - private: - // private constructor & copy constructor: you obtain instances of this - // class by pointing to existing data only - Table(); - Table(const Table &other); - Table &operator=(const Table &); - - uint8_t data_[1]; -}; - -// This specialization allows avoiding warnings like: -// MSVC C4800: type: forcing value to bool 'true' or 'false'. -template<> -inline flatbuffers::Optional Table::GetOptional( - voffset_t field) const { - auto field_offset = GetOptionalFieldOffset(field); - auto p = data_ + field_offset; - return field_offset ? Optional(ReadScalar(p) != 0) - : Optional(); -} - -} // namespace flatbuffers - -#endif // FLATBUFFERS_TABLE_H_ \ No newline at end of file diff --git a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/util.h b/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/util.h deleted file mode 100644 index e0392a95..00000000 --- a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/util.h +++ /dev/null @@ -1,696 +0,0 @@ -/* - * Copyright 2014 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FLATBUFFERS_UTIL_H_ -#define FLATBUFFERS_UTIL_H_ - -#include -#include - -#include "flatbuffers/base.h" -#include "flatbuffers/stl_emulation.h" - -// For TFLM we always want to use FLATBUFFERS_PREFER_PRINTF=1. See -// http://b/211811553 for more context. -#ifndef FLATBUFFERS_PREFER_PRINTF -#define FLATBUFFERS_PREFER_PRINTF 1 -#endif - -#ifndef FLATBUFFERS_PREFER_PRINTF -# include -# include -#else // FLATBUFFERS_PREFER_PRINTF -# include -# include -#endif // FLATBUFFERS_PREFER_PRINTF - -#include - -namespace flatbuffers { - -// @locale-independent functions for ASCII characters set. - -// Fast checking that character lies in closed range: [a <= x <= b] -// using one compare (conditional branch) operator. -inline bool check_ascii_range(char x, char a, char b) { - FLATBUFFERS_ASSERT(a <= b); - // (Hacker's Delight): `a <= x <= b` <=> `(x-a) <={u} (b-a)`. - // The x, a, b will be promoted to int and subtracted without overflow. - return static_cast(x - a) <= static_cast(b - a); -} - -// Case-insensitive isalpha -inline bool is_alpha(char c) { - // ASCII only: alpha to upper case => reset bit 0x20 (~0x20 = 0xDF). - return check_ascii_range(c & 0xDF, 'a' & 0xDF, 'z' & 0xDF); -} - -// Check for uppercase alpha -inline bool is_alpha_upper(char c) { return check_ascii_range(c, 'A', 'Z'); } - -// Check (case-insensitive) that `c` is equal to alpha. -inline bool is_alpha_char(char c, char alpha) { - FLATBUFFERS_ASSERT(is_alpha(alpha)); - // ASCII only: alpha to upper case => reset bit 0x20 (~0x20 = 0xDF). - return ((c & 0xDF) == (alpha & 0xDF)); -} - -// https://en.cppreference.com/w/cpp/string/byte/isxdigit -// isdigit and isxdigit are the only standard narrow character classification -// functions that are not affected by the currently installed C locale. although -// some implementations (e.g. Microsoft in 1252 codepage) may classify -// additional single-byte characters as digits. -inline bool is_digit(char c) { return check_ascii_range(c, '0', '9'); } - -inline bool is_xdigit(char c) { - // Replace by look-up table. - return is_digit(c) || check_ascii_range(c & 0xDF, 'a' & 0xDF, 'f' & 0xDF); -} - -// Case-insensitive isalnum -inline bool is_alnum(char c) { return is_alpha(c) || is_digit(c); } - -inline char CharToUpper(char c) { - return static_cast(::toupper(static_cast(c))); -} - -inline char CharToLower(char c) { - return static_cast(::tolower(static_cast(c))); -} - -// @end-locale-independent functions for ASCII character set - -#ifdef FLATBUFFERS_PREFER_PRINTF -template size_t IntToDigitCount(T t) { - size_t digit_count = 0; - // Count the sign for negative numbers - if (t < 0) digit_count++; - // Count a single 0 left of the dot for fractional numbers - if (-1 < t && t < 1) digit_count++; - // Count digits until fractional part - T eps = std::numeric_limits::epsilon(); - while (t <= (-1 + eps) || (1 - eps) <= t) { - t /= 10; - digit_count++; - } - return digit_count; -} - -template size_t NumToStringWidth(T t, int precision = 0) { - size_t string_width = IntToDigitCount(t); - // Count the dot for floating point numbers - if (precision) string_width += (precision + 1); - return string_width; -} - -template -std::string NumToStringImplWrapper(T t, const char *fmt, int precision = 0) { - size_t string_width = NumToStringWidth(t, precision); - std::string s(string_width, 0x00); - // Allow snprintf to use std::string trailing null to detect buffer overflow - snprintf(const_cast(s.data()), (s.size() + 1), fmt, string_width, t); - return s; -} -#endif // FLATBUFFERS_PREFER_PRINTF - -// Convert an integer or floating point value to a string. -// In contrast to std::stringstream, "char" values are -// converted to a string of digits, and we don't use scientific notation. -template std::string NumToString(T t) { - // clang-format off - - #ifndef FLATBUFFERS_PREFER_PRINTF - std::stringstream ss; - ss << t; - return ss.str(); - #else // FLATBUFFERS_PREFER_PRINTF - auto v = static_cast(t); - return NumToStringImplWrapper(v, "%.*lld"); - #endif // FLATBUFFERS_PREFER_PRINTF - // clang-format on -} -// Avoid char types used as character data. -template<> inline std::string NumToString(signed char t) { - return NumToString(static_cast(t)); -} -template<> inline std::string NumToString(unsigned char t) { - return NumToString(static_cast(t)); -} -template<> inline std::string NumToString(char t) { - return NumToString(static_cast(t)); -} - -// Special versions for floats/doubles. -template std::string FloatToString(T t, int precision) { - // clang-format off - - #ifndef FLATBUFFERS_PREFER_PRINTF - // to_string() prints different numbers of digits for floats depending on - // platform and isn't available on Android, so we use stringstream - std::stringstream ss; - // Use std::fixed to suppress scientific notation. - ss << std::fixed; - // Default precision is 6, we want that to be higher for doubles. - ss << std::setprecision(precision); - ss << t; - auto s = ss.str(); - #else // FLATBUFFERS_PREFER_PRINTF - auto v = static_cast(t); - auto s = NumToStringImplWrapper(v, "%0.*f", precision); - #endif // FLATBUFFERS_PREFER_PRINTF - // clang-format on - // Sadly, std::fixed turns "1" into "1.00000", so here we undo that. - auto p = s.find_last_not_of('0'); - if (p != std::string::npos) { - // Strip trailing zeroes. If it is a whole number, keep one zero. - s.resize(p + (s[p] == '.' ? 2 : 1)); - } - return s; -} - -template<> inline std::string NumToString(double t) { - return FloatToString(t, 12); -} -template<> inline std::string NumToString(float t) { - return FloatToString(t, 6); -} - -// Convert an integer value to a hexadecimal string. -// The returned string length is always xdigits long, prefixed by 0 digits. -// For example, IntToStringHex(0x23, 8) returns the string "00000023". -inline std::string IntToStringHex(int i, int xdigits) { - FLATBUFFERS_ASSERT(i >= 0); - // clang-format off - - #ifndef FLATBUFFERS_PREFER_PRINTF - std::stringstream ss; - ss << std::setw(xdigits) << std::setfill('0') << std::hex << std::uppercase - << i; - return ss.str(); - #else // FLATBUFFERS_PREFER_PRINTF - return NumToStringImplWrapper(i, "%.*X", xdigits); - #endif // FLATBUFFERS_PREFER_PRINTF - // clang-format on -} - -// clang-format off -// Use locale independent functions {strtod_l, strtof_l, strtoll_l, strtoull_l}. -#if defined(FLATBUFFERS_LOCALE_INDEPENDENT) && (FLATBUFFERS_LOCALE_INDEPENDENT > 0) - class ClassicLocale { - #ifdef _MSC_VER - typedef _locale_t locale_type; - #else - typedef locale_t locale_type; // POSIX.1-2008 locale_t type - #endif - ClassicLocale(); - ~ClassicLocale(); - locale_type locale_; - static ClassicLocale instance_; - public: - static locale_type Get() { return instance_.locale_; } - }; - - #ifdef _MSC_VER - #define __strtoull_impl(s, pe, b) _strtoui64_l(s, pe, b, ClassicLocale::Get()) - #define __strtoll_impl(s, pe, b) _strtoi64_l(s, pe, b, ClassicLocale::Get()) - #define __strtod_impl(s, pe) _strtod_l(s, pe, ClassicLocale::Get()) - #define __strtof_impl(s, pe) _strtof_l(s, pe, ClassicLocale::Get()) - #else - #define __strtoull_impl(s, pe, b) strtoull_l(s, pe, b, ClassicLocale::Get()) - #define __strtoll_impl(s, pe, b) strtoll_l(s, pe, b, ClassicLocale::Get()) - #define __strtod_impl(s, pe) strtod_l(s, pe, ClassicLocale::Get()) - #define __strtof_impl(s, pe) strtof_l(s, pe, ClassicLocale::Get()) - #endif -#else - #define __strtod_impl(s, pe) strtod(s, pe) - #define __strtof_impl(s, pe) static_cast(strtod(s, pe)) - #ifdef _MSC_VER - #define __strtoull_impl(s, pe, b) _strtoui64(s, pe, b) - #define __strtoll_impl(s, pe, b) _strtoi64(s, pe, b) - #else - #define __strtoull_impl(s, pe, b) strtoull(s, pe, b) - #define __strtoll_impl(s, pe, b) strtoll(s, pe, b) - #endif -#endif - -inline void strtoval_impl(int64_t *val, const char *str, char **endptr, - int base) { - *val = __strtoll_impl(str, endptr, base); -} - -inline void strtoval_impl(uint64_t *val, const char *str, char **endptr, - int base) { - *val = __strtoull_impl(str, endptr, base); -} - -inline void strtoval_impl(double *val, const char *str, char **endptr) { - *val = __strtod_impl(str, endptr); -} - -// UBSAN: double to float is safe if numeric_limits::is_iec559 is true. -__supress_ubsan__("float-cast-overflow") -inline void strtoval_impl(float *val, const char *str, char **endptr) { - *val = __strtof_impl(str, endptr); -} -#undef __strtoull_impl -#undef __strtoll_impl -#undef __strtod_impl -#undef __strtof_impl -// clang-format on - -// Adaptor for strtoull()/strtoll(). -// Flatbuffers accepts numbers with any count of leading zeros (-009 is -9), -// while strtoll with base=0 interprets first leading zero as octal prefix. -// In future, it is possible to add prefixed 0b0101. -// 1) Checks errno code for overflow condition (out of range). -// 2) If base <= 0, function try to detect base of number by prefix. -// -// Return value (like strtoull and strtoll, but reject partial result): -// - If successful, an integer value corresponding to the str is returned. -// - If full string conversion can't be performed, 0 is returned. -// - If the converted value falls out of range of corresponding return type, a -// range error occurs. In this case value MAX(T)/MIN(T) is returned. -template -inline bool StringToIntegerImpl(T *val, const char *const str, - const int base = 0, - const bool check_errno = true) { - // T is int64_t or uint64_T - FLATBUFFERS_ASSERT(str); - if (base <= 0) { - auto s = str; - while (*s && !is_digit(*s)) s++; - if (s[0] == '0' && is_alpha_char(s[1], 'X')) - return StringToIntegerImpl(val, str, 16, check_errno); - // if a prefix not match, try base=10 - return StringToIntegerImpl(val, str, 10, check_errno); - } else { - if (check_errno) errno = 0; // clear thread-local errno - auto endptr = str; - strtoval_impl(val, str, const_cast(&endptr), base); - if ((*endptr != '\0') || (endptr == str)) { - *val = 0; // erase partial result - return false; // invalid string - } - // errno is out-of-range, return MAX/MIN - if (check_errno && errno) return false; - return true; - } -} - -template -inline bool StringToFloatImpl(T *val, const char *const str) { - // Type T must be either float or double. - FLATBUFFERS_ASSERT(str && val); - auto end = str; - strtoval_impl(val, str, const_cast(&end)); - auto done = (end != str) && (*end == '\0'); - if (!done) *val = 0; // erase partial result - return done; -} - -// Convert a string to an instance of T. -// Return value (matched with StringToInteger64Impl and strtod): -// - If successful, a numeric value corresponding to the str is returned. -// - If full string conversion can't be performed, 0 is returned. -// - If the converted value falls out of range of corresponding return type, a -// range error occurs. In this case value MAX(T)/MIN(T) is returned. -template inline bool StringToNumber(const char *s, T *val) { - // Assert on `unsigned long` and `signed long` on LP64. - // If it is necessary, it could be solved with flatbuffers::enable_if. - static_assert(sizeof(T) < sizeof(int64_t), "unexpected type T"); - FLATBUFFERS_ASSERT(s && val); - int64_t i64; - // The errno check isn't needed, will return MAX/MIN on overflow. - if (StringToIntegerImpl(&i64, s, 0, false)) { - const int64_t max = (flatbuffers::numeric_limits::max)(); - const int64_t min = flatbuffers::numeric_limits::lowest(); - if (i64 > max) { - *val = static_cast(max); - return false; - } - if (i64 < min) { - // For unsigned types return max to distinguish from - // "no conversion can be performed" when 0 is returned. - *val = static_cast(flatbuffers::is_unsigned::value ? max : min); - return false; - } - *val = static_cast(i64); - return true; - } - *val = 0; - return false; -} - -template<> inline bool StringToNumber(const char *str, int64_t *val) { - return StringToIntegerImpl(val, str); -} - -template<> -inline bool StringToNumber(const char *str, uint64_t *val) { - if (!StringToIntegerImpl(val, str)) return false; - // The strtoull accepts negative numbers: - // If the minus sign was part of the input sequence, the numeric value - // calculated from the sequence of digits is negated as if by unary minus - // in the result type, which applies unsigned integer wraparound rules. - // Fix this behaviour (except -0). - if (*val) { - auto s = str; - while (*s && !is_digit(*s)) s++; - s = (s > str) ? (s - 1) : s; // step back to one symbol - if (*s == '-') { - // For unsigned types return the max to distinguish from - // "no conversion can be performed". - *val = (flatbuffers::numeric_limits::max)(); - return false; - } - } - return true; -} - -template<> inline bool StringToNumber(const char *s, float *val) { - return StringToFloatImpl(val, s); -} - -template<> inline bool StringToNumber(const char *s, double *val) { - return StringToFloatImpl(val, s); -} - -inline int64_t StringToInt(const char *s, int base = 10) { - int64_t val; - return StringToIntegerImpl(&val, s, base) ? val : 0; -} - -inline uint64_t StringToUInt(const char *s, int base = 10) { - uint64_t val; - return StringToIntegerImpl(&val, s, base) ? val : 0; -} - -typedef bool (*LoadFileFunction)(const char *filename, bool binary, - std::string *dest); -typedef bool (*FileExistsFunction)(const char *filename); - -LoadFileFunction SetLoadFileFunction(LoadFileFunction load_file_function); - -FileExistsFunction SetFileExistsFunction( - FileExistsFunction file_exists_function); - -// Check if file "name" exists. -bool FileExists(const char *name); - -// Check if "name" exists and it is also a directory. -bool DirExists(const char *name); - -// Load file "name" into "buf" returning true if successful -// false otherwise. If "binary" is false data is read -// using ifstream's text mode, otherwise data is read with -// no transcoding. -bool LoadFile(const char *name, bool binary, std::string *buf); - -// Save data "buf" of length "len" bytes into a file -// "name" returning true if successful, false otherwise. -// If "binary" is false data is written using ifstream's -// text mode, otherwise data is written with no -// transcoding. -bool SaveFile(const char *name, const char *buf, size_t len, bool binary); - -// Save data "buf" into file "name" returning true if -// successful, false otherwise. If "binary" is false -// data is written using ifstream's text mode, otherwise -// data is written with no transcoding. -inline bool SaveFile(const char *name, const std::string &buf, bool binary) { - return SaveFile(name, buf.c_str(), buf.size(), binary); -} - -// Functionality for minimalistic portable path handling. - -// The functions below behave correctly regardless of whether posix ('/') or -// Windows ('/' or '\\') separators are used. - -// Any new separators inserted are always posix. -FLATBUFFERS_CONSTEXPR char kPathSeparator = '/'; - -// Returns the path with the extension, if any, removed. -std::string StripExtension(const std::string &filepath); - -// Returns the extension, if any. -std::string GetExtension(const std::string &filepath); - -// Return the last component of the path, after the last separator. -std::string StripPath(const std::string &filepath); - -// Strip the last component of the path + separator. -std::string StripFileName(const std::string &filepath); - -// Concatenates a path with a filename, regardless of whether the path -// ends in a separator or not. -std::string ConCatPathFileName(const std::string &path, - const std::string &filename); - -// Replaces any '\\' separators with '/' -std::string PosixPath(const char *path); -std::string PosixPath(const std::string &path); - -// This function ensure a directory exists, by recursively -// creating dirs for any parts of the path that don't exist yet. -void EnsureDirExists(const std::string &filepath); - -// Obtains the absolute path from any other path. -// Returns the input path if the absolute path couldn't be resolved. -std::string AbsolutePath(const std::string &filepath); - -// Returns files relative to the --project_root path, prefixed with `//`. -std::string RelativeToRootPath(const std::string &project, - const std::string &filepath); - -// To and from UTF-8 unicode conversion functions - -// Convert a unicode code point into a UTF-8 representation by appending it -// to a string. Returns the number of bytes generated. -inline int ToUTF8(uint32_t ucc, std::string *out) { - FLATBUFFERS_ASSERT(!(ucc & 0x80000000)); // Top bit can't be set. - // 6 possible encodings: http://en.wikipedia.org/wiki/UTF-8 - for (int i = 0; i < 6; i++) { - // Max bits this encoding can represent. - uint32_t max_bits = 6 + i * 5 + static_cast(!i); - if (ucc < (1u << max_bits)) { // does it fit? - // Remaining bits not encoded in the first byte, store 6 bits each - uint32_t remain_bits = i * 6; - // Store first byte: - (*out) += static_cast((0xFE << (max_bits - remain_bits)) | - (ucc >> remain_bits)); - // Store remaining bytes: - for (int j = i - 1; j >= 0; j--) { - (*out) += static_cast(((ucc >> (j * 6)) & 0x3F) | 0x80); - } - return i + 1; // Return the number of bytes added. - } - } - FLATBUFFERS_ASSERT(0); // Impossible to arrive here. - return -1; -} - -// Converts whatever prefix of the incoming string corresponds to a valid -// UTF-8 sequence into a unicode code. The incoming pointer will have been -// advanced past all bytes parsed. -// returns -1 upon corrupt UTF-8 encoding (ignore the incoming pointer in -// this case). -inline int FromUTF8(const char **in) { - int len = 0; - // Count leading 1 bits. - for (int mask = 0x80; mask >= 0x04; mask >>= 1) { - if (**in & mask) { - len++; - } else { - break; - } - } - if ((static_cast(**in) << len) & 0x80) - return -1; // Bit after leading 1's must be 0. - if (!len) return *(*in)++; - // UTF-8 encoded values with a length are between 2 and 4 bytes. - if (len < 2 || len > 4) { return -1; } - // Grab initial bits of the code. - int ucc = *(*in)++ & ((1 << (7 - len)) - 1); - for (int i = 0; i < len - 1; i++) { - if ((**in & 0xC0) != 0x80) return -1; // Upper bits must 1 0. - ucc <<= 6; - ucc |= *(*in)++ & 0x3F; // Grab 6 more bits of the code. - } - // UTF-8 cannot encode values between 0xD800 and 0xDFFF (reserved for - // UTF-16 surrogate pairs). - if (ucc >= 0xD800 && ucc <= 0xDFFF) { return -1; } - // UTF-8 must represent code points in their shortest possible encoding. - switch (len) { - case 2: - // Two bytes of UTF-8 can represent code points from U+0080 to U+07FF. - if (ucc < 0x0080 || ucc > 0x07FF) { return -1; } - break; - case 3: - // Three bytes of UTF-8 can represent code points from U+0800 to U+FFFF. - if (ucc < 0x0800 || ucc > 0xFFFF) { return -1; } - break; - case 4: - // Four bytes of UTF-8 can represent code points from U+10000 to U+10FFFF. - if (ucc < 0x10000 || ucc > 0x10FFFF) { return -1; } - break; - } - return ucc; -} - -#ifndef FLATBUFFERS_PREFER_PRINTF -// Wraps a string to a maximum length, inserting new lines where necessary. Any -// existing whitespace will be collapsed down to a single space. A prefix or -// suffix can be provided, which will be inserted before or after a wrapped -// line, respectively. -inline std::string WordWrap(const std::string in, size_t max_length, - const std::string wrapped_line_prefix, - const std::string wrapped_line_suffix) { - std::istringstream in_stream(in); - std::string wrapped, line, word; - - in_stream >> word; - line = word; - - while (in_stream >> word) { - if ((line.length() + 1 + word.length() + wrapped_line_suffix.length()) < - max_length) { - line += " " + word; - } else { - wrapped += line + wrapped_line_suffix + "\n"; - line = wrapped_line_prefix + word; - } - } - wrapped += line; - - return wrapped; -} -#endif // !FLATBUFFERS_PREFER_PRINTF - -inline bool EscapeString(const char *s, size_t length, std::string *_text, - bool allow_non_utf8, bool natural_utf8) { - std::string &text = *_text; - text += "\""; - for (uoffset_t i = 0; i < length; i++) { - char c = s[i]; - switch (c) { - case '\n': text += "\\n"; break; - case '\t': text += "\\t"; break; - case '\r': text += "\\r"; break; - case '\b': text += "\\b"; break; - case '\f': text += "\\f"; break; - case '\"': text += "\\\""; break; - case '\\': text += "\\\\"; break; - default: - if (c >= ' ' && c <= '~') { - text += c; - } else { - // Not printable ASCII data. Let's see if it's valid UTF-8 first: - const char *utf8 = s + i; - int ucc = FromUTF8(&utf8); - if (ucc < 0) { - if (allow_non_utf8) { - text += "\\x"; - text += IntToStringHex(static_cast(c), 2); - } else { - // There are two cases here: - // - // 1) We reached here by parsing an IDL file. In that case, - // we previously checked for non-UTF-8, so we shouldn't reach - // here. - // - // 2) We reached here by someone calling GenerateText() - // on a previously-serialized flatbuffer. The data might have - // non-UTF-8 Strings, or might be corrupt. - // - // In both cases, we have to give up and inform the caller - // they have no JSON. - return false; - } - } else { - if (natural_utf8) { - // utf8 points to past all utf-8 bytes parsed - text.append(s + i, static_cast(utf8 - s - i)); - } else if (ucc <= 0xFFFF) { - // Parses as Unicode within JSON's \uXXXX range, so use that. - text += "\\u"; - text += IntToStringHex(ucc, 4); - } else if (ucc <= 0x10FFFF) { - // Encode Unicode SMP values to a surrogate pair using two \u - // escapes. - uint32_t base = ucc - 0x10000; - auto high_surrogate = (base >> 10) + 0xD800; - auto low_surrogate = (base & 0x03FF) + 0xDC00; - text += "\\u"; - text += IntToStringHex(high_surrogate, 4); - text += "\\u"; - text += IntToStringHex(low_surrogate, 4); - } - // Skip past characters recognized. - i = static_cast(utf8 - s - 1); - } - } - break; - } - } - text += "\""; - return true; -} - -inline std::string BufferToHexText(const void *buffer, size_t buffer_size, - size_t max_length, - const std::string &wrapped_line_prefix, - const std::string &wrapped_line_suffix) { - std::string text = wrapped_line_prefix; - size_t start_offset = 0; - const char *s = reinterpret_cast(buffer); - for (size_t i = 0; s && i < buffer_size; i++) { - // Last iteration or do we have more? - bool have_more = i + 1 < buffer_size; - text += "0x"; - text += IntToStringHex(static_cast(s[i]), 2); - if (have_more) { text += ','; } - // If we have more to process and we reached max_length - if (have_more && - text.size() + wrapped_line_suffix.size() >= start_offset + max_length) { - text += wrapped_line_suffix; - text += '\n'; - start_offset = text.size(); - text += wrapped_line_prefix; - } - } - text += wrapped_line_suffix; - return text; -} - -// Remove paired quotes in a string: "text"|'text' -> text. -std::string RemoveStringQuotes(const std::string &s); - -// Change th global C-locale to locale with name . -// Returns an actual locale name in <_value>, useful if locale_name is "" or -// null. -bool SetGlobalTestLocale(const char *locale_name, - std::string *_value = nullptr); - -// Read (or test) a value of environment variable. -bool ReadEnvironmentVariable(const char *var_name, - std::string *_value = nullptr); - -// MSVC specific: Send all assert reports to STDOUT to prevent CI hangs. -void SetupDefaultCRTReportMode(); - -} // namespace flatbuffers - -#endif // FLATBUFFERS_UTIL_H_ diff --git a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/vector.h b/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/vector.h deleted file mode 100644 index f8a5d88e..00000000 --- a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/vector.h +++ /dev/null @@ -1,370 +0,0 @@ -/* - * Copyright 2021 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FLATBUFFERS_VECTOR_H_ -#define FLATBUFFERS_VECTOR_H_ - -#include "flatbuffers/base.h" -#include "flatbuffers/buffer.h" - -namespace flatbuffers { - -struct String; - -// An STL compatible iterator implementation for Vector below, effectively -// calling Get() for every element. -template struct VectorIterator { - typedef std::random_access_iterator_tag iterator_category; - typedef IT value_type; - typedef ptrdiff_t difference_type; - typedef IT *pointer; - typedef IT &reference; - - VectorIterator(const uint8_t *data, uoffset_t i) - : data_(data + IndirectHelper::element_stride * i) {} - VectorIterator(const VectorIterator &other) : data_(other.data_) {} - VectorIterator() : data_(nullptr) {} - - VectorIterator &operator=(const VectorIterator &other) { - data_ = other.data_; - return *this; - } - - VectorIterator &operator=(VectorIterator &&other) { - data_ = other.data_; - return *this; - } - - bool operator==(const VectorIterator &other) const { - return data_ == other.data_; - } - - bool operator<(const VectorIterator &other) const { - return data_ < other.data_; - } - - bool operator!=(const VectorIterator &other) const { - return data_ != other.data_; - } - - difference_type operator-(const VectorIterator &other) const { - return (data_ - other.data_) / IndirectHelper::element_stride; - } - - // Note: return type is incompatible with the standard - // `reference operator*()`. - IT operator*() const { return IndirectHelper::Read(data_, 0); } - - // Note: return type is incompatible with the standard - // `pointer operator->()`. - IT operator->() const { return IndirectHelper::Read(data_, 0); } - - VectorIterator &operator++() { - data_ += IndirectHelper::element_stride; - return *this; - } - - VectorIterator operator++(int) { - VectorIterator temp(data_, 0); - data_ += IndirectHelper::element_stride; - return temp; - } - - VectorIterator operator+(const uoffset_t &offset) const { - return VectorIterator(data_ + offset * IndirectHelper::element_stride, - 0); - } - - VectorIterator &operator+=(const uoffset_t &offset) { - data_ += offset * IndirectHelper::element_stride; - return *this; - } - - VectorIterator &operator--() { - data_ -= IndirectHelper::element_stride; - return *this; - } - - VectorIterator operator--(int) { - VectorIterator temp(data_, 0); - data_ -= IndirectHelper::element_stride; - return temp; - } - - VectorIterator operator-(const uoffset_t &offset) const { - return VectorIterator(data_ - offset * IndirectHelper::element_stride, - 0); - } - - VectorIterator &operator-=(const uoffset_t &offset) { - data_ -= offset * IndirectHelper::element_stride; - return *this; - } - - private: - const uint8_t *data_; -}; - -template -struct VectorReverseIterator : public std::reverse_iterator { - explicit VectorReverseIterator(Iterator iter) - : std::reverse_iterator(iter) {} - - // Note: return type is incompatible with the standard - // `reference operator*()`. - typename Iterator::value_type operator*() const { - auto tmp = std::reverse_iterator::current; - return *--tmp; - } - - // Note: return type is incompatible with the standard - // `pointer operator->()`. - typename Iterator::value_type operator->() const { - auto tmp = std::reverse_iterator::current; - return *--tmp; - } -}; - -// This is used as a helper type for accessing vectors. -// Vector::data() assumes the vector elements start after the length field. -template class Vector { - public: - typedef VectorIterator::mutable_return_type> - iterator; - typedef VectorIterator::return_type> - const_iterator; - typedef VectorReverseIterator reverse_iterator; - typedef VectorReverseIterator const_reverse_iterator; - - typedef typename flatbuffers::bool_constant::value> - scalar_tag; - - static FLATBUFFERS_CONSTEXPR bool is_span_observable = - scalar_tag::value && (FLATBUFFERS_LITTLEENDIAN || sizeof(T) == 1); - - uoffset_t size() const { return EndianScalar(length_); } - - // Deprecated: use size(). Here for backwards compatibility. - FLATBUFFERS_ATTRIBUTE([[deprecated("use size() instead")]]) - uoffset_t Length() const { return size(); } - - typedef typename IndirectHelper::return_type return_type; - typedef typename IndirectHelper::mutable_return_type mutable_return_type; - typedef return_type value_type; - - return_type Get(uoffset_t i) const { - FLATBUFFERS_ASSERT(i < size()); - return IndirectHelper::Read(Data(), i); - } - - return_type operator[](uoffset_t i) const { return Get(i); } - - // If this is a Vector of enums, T will be its storage type, not the enum - // type. This function makes it convenient to retrieve value with enum - // type E. - template E GetEnum(uoffset_t i) const { - return static_cast(Get(i)); - } - - // If this a vector of unions, this does the cast for you. There's no check - // to make sure this is the right type! - template const U *GetAs(uoffset_t i) const { - return reinterpret_cast(Get(i)); - } - - // If this a vector of unions, this does the cast for you. There's no check - // to make sure this is actually a string! - const String *GetAsString(uoffset_t i) const { - return reinterpret_cast(Get(i)); - } - - const void *GetStructFromOffset(size_t o) const { - return reinterpret_cast(Data() + o); - } - - iterator begin() { return iterator(Data(), 0); } - const_iterator begin() const { return const_iterator(Data(), 0); } - - iterator end() { return iterator(Data(), size()); } - const_iterator end() const { return const_iterator(Data(), size()); } - - reverse_iterator rbegin() { return reverse_iterator(end()); } - const_reverse_iterator rbegin() const { - return const_reverse_iterator(end()); - } - - reverse_iterator rend() { return reverse_iterator(begin()); } - const_reverse_iterator rend() const { - return const_reverse_iterator(begin()); - } - - const_iterator cbegin() const { return begin(); } - - const_iterator cend() const { return end(); } - - const_reverse_iterator crbegin() const { return rbegin(); } - - const_reverse_iterator crend() const { return rend(); } - - // Change elements if you have a non-const pointer to this object. - // Scalars only. See reflection.h, and the documentation. - void Mutate(uoffset_t i, const T &val) { - FLATBUFFERS_ASSERT(i < size()); - WriteScalar(data() + i, val); - } - - // Change an element of a vector of tables (or strings). - // "val" points to the new table/string, as you can obtain from - // e.g. reflection::AddFlatBuffer(). - void MutateOffset(uoffset_t i, const uint8_t *val) { - FLATBUFFERS_ASSERT(i < size()); - static_assert(sizeof(T) == sizeof(uoffset_t), "Unrelated types"); - WriteScalar(data() + i, - static_cast(val - (Data() + i * sizeof(uoffset_t)))); - } - - // Get a mutable pointer to tables/strings inside this vector. - mutable_return_type GetMutableObject(uoffset_t i) const { - FLATBUFFERS_ASSERT(i < size()); - return const_cast(IndirectHelper::Read(Data(), i)); - } - - // The raw data in little endian format. Use with care. - const uint8_t *Data() const { - return reinterpret_cast(&length_ + 1); - } - - uint8_t *Data() { return reinterpret_cast(&length_ + 1); } - - // Similarly, but typed, much like std::vector::data - const T *data() const { return reinterpret_cast(Data()); } - T *data() { return reinterpret_cast(Data()); } - - template return_type LookupByKey(K key) const { - void *search_result = std::bsearch( - &key, Data(), size(), IndirectHelper::element_stride, KeyCompare); - - if (!search_result) { - return nullptr; // Key not found. - } - - const uint8_t *element = reinterpret_cast(search_result); - - return IndirectHelper::Read(element, 0); - } - - template mutable_return_type MutableLookupByKey(K key) { - return const_cast(LookupByKey(key)); - } - - protected: - // This class is only used to access pre-existing data. Don't ever - // try to construct these manually. - Vector(); - - uoffset_t length_; - - private: - // This class is a pointer. Copying will therefore create an invalid object. - // Private and unimplemented copy constructor. - Vector(const Vector &); - Vector &operator=(const Vector &); - - template static int KeyCompare(const void *ap, const void *bp) { - const K *key = reinterpret_cast(ap); - const uint8_t *data = reinterpret_cast(bp); - auto table = IndirectHelper::Read(data, 0); - - // std::bsearch compares with the operands transposed, so we negate the - // result here. - return -table->KeyCompareWithValue(*key); - } -}; - -template -FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span make_span(Vector &vec) - FLATBUFFERS_NOEXCEPT { - static_assert(Vector::is_span_observable, - "wrong type U, only LE-scalar, or byte types are allowed"); - return span(vec.data(), vec.size()); -} - -template -FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span make_span( - const Vector &vec) FLATBUFFERS_NOEXCEPT { - static_assert(Vector::is_span_observable, - "wrong type U, only LE-scalar, or byte types are allowed"); - return span(vec.data(), vec.size()); -} - -template -FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span make_bytes_span( - Vector &vec) FLATBUFFERS_NOEXCEPT { - static_assert(Vector::scalar_tag::value, - "wrong type U, only LE-scalar, or byte types are allowed"); - return span(vec.Data(), vec.size() * sizeof(U)); -} - -template -FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span make_bytes_span( - const Vector &vec) FLATBUFFERS_NOEXCEPT { - static_assert(Vector::scalar_tag::value, - "wrong type U, only LE-scalar, or byte types are allowed"); - return span(vec.Data(), vec.size() * sizeof(U)); -} - -// Represent a vector much like the template above, but in this case we -// don't know what the element types are (used with reflection.h). -class VectorOfAny { - public: - uoffset_t size() const { return EndianScalar(length_); } - - const uint8_t *Data() const { - return reinterpret_cast(&length_ + 1); - } - uint8_t *Data() { return reinterpret_cast(&length_ + 1); } - - protected: - VectorOfAny(); - - uoffset_t length_; - - private: - VectorOfAny(const VectorOfAny &); - VectorOfAny &operator=(const VectorOfAny &); -}; - -template -Vector> *VectorCast(Vector> *ptr) { - static_assert(std::is_base_of::value, "Unrelated types"); - return reinterpret_cast> *>(ptr); -} - -template -const Vector> *VectorCast(const Vector> *ptr) { - static_assert(std::is_base_of::value, "Unrelated types"); - return reinterpret_cast> *>(ptr); -} - -// Convenient helper function to get the length of any vector, regardless -// of whether it is null or not (the field is not set). -template static inline size_t VectorLength(const Vector *v) { - return v ? v->size() : 0; -} - -} // namespace flatbuffers - -#endif // FLATBUFFERS_VERIFIER_H_ diff --git a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/vector_downward.h b/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/vector_downward.h deleted file mode 100644 index 33913918..00000000 --- a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/vector_downward.h +++ /dev/null @@ -1,271 +0,0 @@ -/* - * Copyright 2021 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FLATBUFFERS_VECTOR_DOWNWARD_H_ -#define FLATBUFFERS_VECTOR_DOWNWARD_H_ - -#include "flatbuffers/base.h" -#include "flatbuffers/default_allocator.h" -#include "flatbuffers/detached_buffer.h" - -namespace flatbuffers { - -// This is a minimal replication of std::vector functionality, -// except growing from higher to lower addresses. i.e push_back() inserts data -// in the lowest address in the vector. -// Since this vector leaves the lower part unused, we support a "scratch-pad" -// that can be stored there for temporary data, to share the allocated space. -// Essentially, this supports 2 std::vectors in a single buffer. -class vector_downward { - public: - explicit vector_downward(size_t initial_size, Allocator *allocator, - bool own_allocator, size_t buffer_minalign) - : allocator_(allocator), - own_allocator_(own_allocator), - initial_size_(initial_size), - buffer_minalign_(buffer_minalign), - reserved_(0), - size_(0), - buf_(nullptr), - cur_(nullptr), - scratch_(nullptr) {} - - vector_downward(vector_downward &&other) - // clang-format on - : allocator_(other.allocator_), - own_allocator_(other.own_allocator_), - initial_size_(other.initial_size_), - buffer_minalign_(other.buffer_minalign_), - reserved_(other.reserved_), - size_(other.size_), - buf_(other.buf_), - cur_(other.cur_), - scratch_(other.scratch_) { - // No change in other.allocator_ - // No change in other.initial_size_ - // No change in other.buffer_minalign_ - other.own_allocator_ = false; - other.reserved_ = 0; - other.buf_ = nullptr; - other.cur_ = nullptr; - other.scratch_ = nullptr; - } - - vector_downward &operator=(vector_downward &&other) { - // Move construct a temporary and swap idiom - vector_downward temp(std::move(other)); - swap(temp); - return *this; - } - - ~vector_downward() { - clear_buffer(); - clear_allocator(); - } - - void reset() { - clear_buffer(); - clear(); - } - - void clear() { - if (buf_) { - cur_ = buf_ + reserved_; - } else { - reserved_ = 0; - cur_ = nullptr; - } - size_ = 0; - clear_scratch(); - } - - void clear_scratch() { scratch_ = buf_; } - - void clear_allocator() { - if (own_allocator_ && allocator_) { delete allocator_; } - allocator_ = nullptr; - own_allocator_ = false; - } - - void clear_buffer() { - if (buf_) Deallocate(allocator_, buf_, reserved_); - buf_ = nullptr; - } - - // Relinquish the pointer to the caller. - uint8_t *release_raw(size_t &allocated_bytes, size_t &offset) { - auto *buf = buf_; - allocated_bytes = reserved_; - offset = static_cast(cur_ - buf_); - - // release_raw only relinquishes the buffer ownership. - // Does not deallocate or reset the allocator. Destructor will do that. - buf_ = nullptr; - clear(); - return buf; - } - - // Relinquish the pointer to the caller. - DetachedBuffer release() { - // allocator ownership (if any) is transferred to DetachedBuffer. - DetachedBuffer fb(allocator_, own_allocator_, buf_, reserved_, cur_, - size()); - if (own_allocator_) { - allocator_ = nullptr; - own_allocator_ = false; - } - buf_ = nullptr; - clear(); - return fb; - } - - size_t ensure_space(size_t len) { - FLATBUFFERS_ASSERT(cur_ >= scratch_ && scratch_ >= buf_); - if (len > static_cast(cur_ - scratch_)) { reallocate(len); } - // Beyond this, signed offsets may not have enough range: - // (FlatBuffers > 2GB not supported). - FLATBUFFERS_ASSERT(size() < FLATBUFFERS_MAX_BUFFER_SIZE); - return len; - } - - inline uint8_t *make_space(size_t len) { - if (len) { - ensure_space(len); - cur_ -= len; - size_ += static_cast(len); - } - return cur_; - } - - // Returns nullptr if using the DefaultAllocator. - Allocator *get_custom_allocator() { return allocator_; } - - inline uoffset_t size() const { return size_; } - - uoffset_t scratch_size() const { - return static_cast(scratch_ - buf_); - } - - size_t capacity() const { return reserved_; } - - uint8_t *data() const { - FLATBUFFERS_ASSERT(cur_); - return cur_; - } - - uint8_t *scratch_data() const { - FLATBUFFERS_ASSERT(buf_); - return buf_; - } - - uint8_t *scratch_end() const { - FLATBUFFERS_ASSERT(scratch_); - return scratch_; - } - - uint8_t *data_at(size_t offset) const { return buf_ + reserved_ - offset; } - - void push(const uint8_t *bytes, size_t num) { - if (num > 0) { memcpy(make_space(num), bytes, num); } - } - - // Specialized version of push() that avoids memcpy call for small data. - template void push_small(const T &little_endian_t) { - make_space(sizeof(T)); - *reinterpret_cast(cur_) = little_endian_t; - } - - template void scratch_push_small(const T &t) { - ensure_space(sizeof(T)); - *reinterpret_cast(scratch_) = t; - scratch_ += sizeof(T); - } - - // fill() is most frequently called with small byte counts (<= 4), - // which is why we're using loops rather than calling memset. - void fill(size_t zero_pad_bytes) { - make_space(zero_pad_bytes); - for (size_t i = 0; i < zero_pad_bytes; i++) cur_[i] = 0; - } - - // Version for when we know the size is larger. - // Precondition: zero_pad_bytes > 0 - void fill_big(size_t zero_pad_bytes) { - memset(make_space(zero_pad_bytes), 0, zero_pad_bytes); - } - - void pop(size_t bytes_to_remove) { - cur_ += bytes_to_remove; - size_ -= static_cast(bytes_to_remove); - } - - void scratch_pop(size_t bytes_to_remove) { scratch_ -= bytes_to_remove; } - - void swap(vector_downward &other) { - using std::swap; - swap(allocator_, other.allocator_); - swap(own_allocator_, other.own_allocator_); - swap(initial_size_, other.initial_size_); - swap(buffer_minalign_, other.buffer_minalign_); - swap(reserved_, other.reserved_); - swap(size_, other.size_); - swap(buf_, other.buf_); - swap(cur_, other.cur_); - swap(scratch_, other.scratch_); - } - - void swap_allocator(vector_downward &other) { - using std::swap; - swap(allocator_, other.allocator_); - swap(own_allocator_, other.own_allocator_); - } - - private: - // You shouldn't really be copying instances of this class. - FLATBUFFERS_DELETE_FUNC(vector_downward(const vector_downward &)); - FLATBUFFERS_DELETE_FUNC(vector_downward &operator=(const vector_downward &)); - - Allocator *allocator_; - bool own_allocator_; - size_t initial_size_; - size_t buffer_minalign_; - size_t reserved_; - uoffset_t size_; - uint8_t *buf_; - uint8_t *cur_; // Points at location between empty (below) and used (above). - uint8_t *scratch_; // Points to the end of the scratchpad in use. - - void reallocate(size_t len) { - auto old_reserved = reserved_; - auto old_size = size(); - auto old_scratch_size = scratch_size(); - reserved_ += - (std::max)(len, old_reserved ? old_reserved / 2 : initial_size_); - reserved_ = (reserved_ + buffer_minalign_ - 1) & ~(buffer_minalign_ - 1); - if (buf_) { - buf_ = ReallocateDownward(allocator_, buf_, old_reserved, reserved_, - old_size, old_scratch_size); - } else { - buf_ = Allocate(allocator_, reserved_); - } - cur_ = buf_ + reserved_ - old_size; - scratch_ = buf_ + old_scratch_size; - } -}; - -} // namespace flatbuffers - -#endif // FLATBUFFERS_VECTOR_DOWNWARD_H_ diff --git a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/verifier.h b/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/verifier.h deleted file mode 100644 index dfa3da8a..00000000 --- a/code/components/tflite-lib/third_party/flatbuffers/include/flatbuffers/verifier.h +++ /dev/null @@ -1,280 +0,0 @@ -/* - * Copyright 2021 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FLATBUFFERS_VERIFIER_H_ -#define FLATBUFFERS_VERIFIER_H_ - -#include "flatbuffers/base.h" -#include "flatbuffers/util.h" -#include "flatbuffers/vector.h" - -namespace flatbuffers { - -// Helper class to verify the integrity of a FlatBuffer -class Verifier FLATBUFFERS_FINAL_CLASS { - public: - Verifier(const uint8_t *buf, size_t buf_len, uoffset_t _max_depth = 64, - uoffset_t _max_tables = 1000000, bool _check_alignment = true) - : buf_(buf), - size_(buf_len), - depth_(0), - max_depth_(_max_depth), - num_tables_(0), - max_tables_(_max_tables), - upper_bound_(0), - check_alignment_(_check_alignment), - flex_reuse_tracker_(nullptr) { - FLATBUFFERS_ASSERT(size_ < FLATBUFFERS_MAX_BUFFER_SIZE); - } - - // Central location where any verification failures register. - bool Check(bool ok) const { - // clang-format off - #ifdef FLATBUFFERS_DEBUG_VERIFICATION_FAILURE - FLATBUFFERS_ASSERT(ok); - #endif - #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE - if (!ok) - upper_bound_ = 0; - #endif - // clang-format on - return ok; - } - - // Verify any range within the buffer. - bool Verify(size_t elem, size_t elem_len) const { - // clang-format off - #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE - auto upper_bound = elem + elem_len; - if (upper_bound_ < upper_bound) - upper_bound_ = upper_bound; - #endif - // clang-format on - return Check(elem_len < size_ && elem <= size_ - elem_len); - } - - template bool VerifyAlignment(size_t elem) const { - return Check((elem & (sizeof(T) - 1)) == 0 || !check_alignment_); - } - - // Verify a range indicated by sizeof(T). - template bool Verify(size_t elem) const { - return VerifyAlignment(elem) && Verify(elem, sizeof(T)); - } - - bool VerifyFromPointer(const uint8_t *p, size_t len) { - auto o = static_cast(p - buf_); - return Verify(o, len); - } - - // Verify relative to a known-good base pointer. - bool Verify(const uint8_t *base, voffset_t elem_off, size_t elem_len) const { - return Verify(static_cast(base - buf_) + elem_off, elem_len); - } - - template - bool Verify(const uint8_t *base, voffset_t elem_off) const { - return Verify(static_cast(base - buf_) + elem_off, sizeof(T)); - } - - // Verify a pointer (may be NULL) of a table type. - template bool VerifyTable(const T *table) { - return !table || table->Verify(*this); - } - - // Verify a pointer (may be NULL) of any vector type. - template bool VerifyVector(const Vector *vec) const { - return !vec || VerifyVectorOrString(reinterpret_cast(vec), - sizeof(T)); - } - - // Verify a pointer (may be NULL) of a vector to struct. - template bool VerifyVector(const Vector *vec) const { - return VerifyVector(reinterpret_cast *>(vec)); - } - - // Verify a pointer (may be NULL) to string. - bool VerifyString(const String *str) const { - size_t end; - return !str || (VerifyVectorOrString(reinterpret_cast(str), - 1, &end) && - Verify(end, 1) && // Must have terminator - Check(buf_[end] == '\0')); // Terminating byte must be 0. - } - - // Common code between vectors and strings. - bool VerifyVectorOrString(const uint8_t *vec, size_t elem_size, - size_t *end = nullptr) const { - auto veco = static_cast(vec - buf_); - // Check we can read the size field. - if (!Verify(veco)) return false; - // Check the whole array. If this is a string, the byte past the array - // must be 0. - auto size = ReadScalar(vec); - auto max_elems = FLATBUFFERS_MAX_BUFFER_SIZE / elem_size; - if (!Check(size < max_elems)) - return false; // Protect against byte_size overflowing. - auto byte_size = sizeof(size) + elem_size * size; - if (end) *end = veco + byte_size; - return Verify(veco, byte_size); - } - - // Special case for string contents, after the above has been called. - bool VerifyVectorOfStrings(const Vector> *vec) const { - if (vec) { - for (uoffset_t i = 0; i < vec->size(); i++) { - if (!VerifyString(vec->Get(i))) return false; - } - } - return true; - } - - // Special case for table contents, after the above has been called. - template bool VerifyVectorOfTables(const Vector> *vec) { - if (vec) { - for (uoffset_t i = 0; i < vec->size(); i++) { - if (!vec->Get(i)->Verify(*this)) return false; - } - } - return true; - } - - __supress_ubsan__("unsigned-integer-overflow") bool VerifyTableStart( - const uint8_t *table) { - // Check the vtable offset. - auto tableo = static_cast(table - buf_); - if (!Verify(tableo)) return false; - // This offset may be signed, but doing the subtraction unsigned always - // gives the result we want. - auto vtableo = tableo - static_cast(ReadScalar(table)); - // Check the vtable size field, then check vtable fits in its entirety. - return VerifyComplexity() && Verify(vtableo) && - VerifyAlignment(ReadScalar(buf_ + vtableo)) && - Verify(vtableo, ReadScalar(buf_ + vtableo)); - } - - template - bool VerifyBufferFromStart(const char *identifier, size_t start) { - if (identifier && !Check((size_ >= 2 * sizeof(flatbuffers::uoffset_t) && - BufferHasIdentifier(buf_ + start, identifier)))) { - return false; - } - - // Call T::Verify, which must be in the generated code for this type. - auto o = VerifyOffset(start); - return o && reinterpret_cast(buf_ + start + o)->Verify(*this) - // clang-format off - #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE - && GetComputedSize() - #endif - ; - // clang-format on - } - - template - bool VerifyNestedFlatBuffer(const Vector *buf, - const char *identifier) { - if (!buf) return true; - Verifier nested_verifier(buf->data(), buf->size()); - return nested_verifier.VerifyBuffer(identifier); - } - - // Verify this whole buffer, starting with root type T. - template bool VerifyBuffer() { return VerifyBuffer(nullptr); } - - template bool VerifyBuffer(const char *identifier) { - return VerifyBufferFromStart(identifier, 0); - } - - template bool VerifySizePrefixedBuffer(const char *identifier) { - return Verify(0U) && - ReadScalar(buf_) == size_ - sizeof(uoffset_t) && - VerifyBufferFromStart(identifier, sizeof(uoffset_t)); - } - - uoffset_t VerifyOffset(size_t start) const { - if (!Verify(start)) return 0; - auto o = ReadScalar(buf_ + start); - // May not point to itself. - if (!Check(o != 0)) return 0; - // Can't wrap around / buffers are max 2GB. - if (!Check(static_cast(o) >= 0)) return 0; - // Must be inside the buffer to create a pointer from it (pointer outside - // buffer is UB). - if (!Verify(start + o, 1)) return 0; - return o; - } - - uoffset_t VerifyOffset(const uint8_t *base, voffset_t start) const { - return VerifyOffset(static_cast(base - buf_) + start); - } - - // Called at the start of a table to increase counters measuring data - // structure depth and amount, and possibly bails out with false if - // limits set by the constructor have been hit. Needs to be balanced - // with EndTable(). - bool VerifyComplexity() { - depth_++; - num_tables_++; - return Check(depth_ <= max_depth_ && num_tables_ <= max_tables_); - } - - // Called at the end of a table to pop the depth count. - bool EndTable() { - depth_--; - return true; - } - - // Returns the message size in bytes - size_t GetComputedSize() const { - // clang-format off - #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE - uintptr_t size = upper_bound_; - // Align the size to uoffset_t - size = (size - 1 + sizeof(uoffset_t)) & ~(sizeof(uoffset_t) - 1); - return (size > size_) ? 0 : size; - #else - // Must turn on FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE for this to work. - (void)upper_bound_; - FLATBUFFERS_ASSERT(false); - return 0; - #endif - // clang-format on - } - - std::vector *GetFlexReuseTracker() { - return flex_reuse_tracker_; - } - - void SetFlexReuseTracker(std::vector *rt) { - flex_reuse_tracker_ = rt; - } - - private: - const uint8_t *buf_; - size_t size_; - uoffset_t depth_; - uoffset_t max_depth_; - uoffset_t num_tables_; - uoffset_t max_tables_; - mutable size_t upper_bound_; - bool check_alignment_; - std::vector *flex_reuse_tracker_; -}; - -} // namespace flatbuffers - -#endif // FLATBUFFERS_VERIFIER_H_ diff --git a/code/components/tflite-lib/third_party/gemmlowp/LICENSE b/code/components/tflite-lib/third_party/gemmlowp/LICENSE deleted file mode 100644 index d6456956..00000000 --- a/code/components/tflite-lib/third_party/gemmlowp/LICENSE +++ /dev/null @@ -1,202 +0,0 @@ - - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. diff --git a/code/components/tflite-lib/third_party/gemmlowp/fixedpoint/fixedpoint.h b/code/components/tflite-lib/third_party/gemmlowp/fixedpoint/fixedpoint.h deleted file mode 100644 index 51b5aff4..00000000 --- a/code/components/tflite-lib/third_party/gemmlowp/fixedpoint/fixedpoint.h +++ /dev/null @@ -1,900 +0,0 @@ -// Copyright 2015 The Gemmlowp Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// fixedpoint.h: fixed-point arithmetic, with basic operations and -// a few math functions such as tanh. - -#ifndef GEMMLOWP_INTERNAL_FIXEDPOINT_H_ -#define GEMMLOWP_INTERNAL_FIXEDPOINT_H_ - -#include -#include -#include -#include -#include - -#include "../internal/detect_platform.h" - -namespace gemmlowp { - -// Part 1: Low-level integer-arithmetic primitives. -// The implementations here are generic implementations valid for -// scalar types (e.g. std::int32_t). Architecture-specific SIMD types -// (e.g. NEON int32x4_t) may be supported by providing -// specializations for them in separate files. -// -// The purpose of these primitives is two-fold: -// - They will be used to implement higher-level fixed-point -// abstractions, namely the FixedPoint class and its arithmetic -// operators. -// - They will be directly used to implement some more involved -// fixed-point computations, e.g. the fixed-point implementation -// of math functions such as tanh. - -// Some compile-time traits around raw types to handle SIMD aspects: -// number of lanes, underlying scalar type. -template -struct FixedPointRawTypeTraits {}; - -template <> -struct FixedPointRawTypeTraits { - typedef std::int32_t ScalarRawType; - static constexpr int kLanes = 1; -}; - -template <> -struct FixedPointRawTypeTraits { - typedef std::int16_t ScalarRawType; - static constexpr int kLanes = 1; -}; - -// Returns a SIMD value duplicating a scalar value across all lanes. -template -tRawType Dup(typename FixedPointRawTypeTraits::ScalarRawType x) { - return x; -} - -// Plain bit-wise AND -template -tIntegerType BitAnd(tIntegerType a, tIntegerType b) { - return a & b; -} - -// Plain bit-wise OR -template -tIntegerType BitOr(tIntegerType a, tIntegerType b) { - return a | b; -} - -// Plain bit-wise XOR -template -tIntegerType BitXor(tIntegerType a, tIntegerType b) { - return a ^ b; -} - -// Plain bit-wise NOT -template -tIntegerType BitNot(tIntegerType a) { - return ~a; -} - -// Integer addition. Not saturating. Overflow is undefined behavior. -template -tIntegerType Add(tIntegerType a, tIntegerType b) { - return a + b; -} - -// Integer subtraction. Not saturating. Overflow is undefined behavior. -template -tIntegerType Mul(tIntegerType a, tIntegerType b) { - return a * b; -} - -template -tIntegerType Sub(tIntegerType a, tIntegerType b) { - return a - b; -} - -// Integer unary negative. Not saturating. Overflow is undefined behavior. -template -tIntegerType Neg(tIntegerType a) { - return -a; -} - -// Integer arithmetic left-shift, equivalent to multiplying with a power of two. -// Negative values are OK. In case of overflow, no Undefined -// Behavior, but the results are implementation-defined (in practice, -// they currently are saturated, but we make no commitment to that). The idea -// is that the caller will want to implement the overflowing cases with -// saturation with compare-and-mask, so we don't care about the results -// in the overflow case, we just want to avoid undefined behavior. -// -// tIntegerType may be int32 or any narrower signed type. -template -tIntegerType ShiftLeft(tIntegerType a, int offset) { - const std::int64_t wide_a = static_cast(a); - const std::int64_t wide_shifted = wide_a * (1 << offset); - const auto min = std::numeric_limits::min(); - const auto max = std::numeric_limits::max(); - return wide_shifted < min - ? min - : wide_shifted > max ? max - : static_cast(wide_shifted); -} - -// Integer arithmetic right-shift. Not rounding. -// Relying on implementation-defined, but in-practice-consistent, -// C++ compiler behavior. -template -tIntegerType ShiftRight(tIntegerType a, int offset) { - return a >> offset; -} - -// Each bit of the result is set to the corresponding bit of either then_val or -// else_val depending on whether the corresponding bit of if_mask is set. -// Equivalent to the VBSL instruction in ARM NEON. -template -tIntegerType SelectUsingMask(tIntegerType if_mask, tIntegerType then_val, - tIntegerType else_val) { - return BitXor(BitAnd(if_mask, then_val), BitAnd(BitNot(if_mask), else_val)); -} - -// For each input scalar, the corresponding bits of the result are set if the -// input scalar is non-zero. -template -tIntegerType MaskIfNonZero(tIntegerType a) { - static constexpr tIntegerType zero = 0; - return a ? BitNot(zero) : zero; -} - -// For each input scalar, the corresponding bits of the result are set if the -// input scalar is zero. -template -tIntegerType MaskIfZero(tIntegerType a) { - return MaskIfNonZero(!a); -} - -// For each pair of input scalars, the corresponding bits of the result are -// set if the input scalars are equal. -template -tIntegerType MaskIfEqual(tIntegerType a, tIntegerType b) { - return MaskIfNonZero(a == b); -} - -// For each pair of input scalars, the corresponding bits of the result are -// set if the input scalars are not equal. -template -tIntegerType MaskIfNotEqual(tIntegerType a, tIntegerType b) { - return MaskIfNonZero(a != b); -} - -// For each pair of input scalars, the corresponding bits of the result are -// set if the input scalars a, b satisfy a > b. -template -tIntegerType MaskIfGreaterThan(tIntegerType a, tIntegerType b) { - return MaskIfNonZero(a > b); -} - -// For each pair of input scalars, the corresponding bits of the result are -// set if the input scalars a, b satisfy a >= b. -template -tIntegerType MaskIfGreaterThanOrEqual(tIntegerType a, tIntegerType b) { - return MaskIfNonZero(a >= b); -} - -// For each pair of input scalars, the corresponding bits of the result are -// set if the input scalars a, b satisfy a < b. -template -tIntegerType MaskIfLessThan(tIntegerType a, tIntegerType b) { - return MaskIfNonZero(a < b); -} - -// For each pair of input scalars, the corresponding bits of the result are -// set if the input scalars a, b satisfy a <= b. -template -tIntegerType MaskIfLessThanOrEqual(tIntegerType a, tIntegerType b) { - return MaskIfNonZero(a <= b); -} - -// Returns true if all of the input scalars are nonzero. -// This function may currently assume that each of the input scalars has either -// all or none of its bits set. Otherwise, its behavior is currently undefined. -template -bool All(tIntegerType a) { - return a; -} - -// Returns true if any of the input scalars are nonzero. -// This function may currently assume that each of the input scalars has either -// all or none of its bits set. Otherwise, its behavior is currently undefined. -template -bool Any(tIntegerType a) { - return a; -} - -// Returns (a+b)/2, rounded to the nearest integer. -// Equivalent to VRHADD in the ARM NEON instruction set. -template -IntegerType RoundingHalfSum(IntegerType a, IntegerType b) { - static_assert(std::is_same::value, "unimplemented"); - (void)b; - return a; -} - -template <> -inline std::int32_t RoundingHalfSum(std::int32_t a, std::int32_t b) { - std::int64_t a64 = a; - std::int64_t b64 = b; - std::int64_t sum = a64 + b64; - std::int64_t sign = sum >= 0 ? 1 : -1; - return static_cast((sum + sign) / 2); -} - -template <> -inline std::int16_t RoundingHalfSum(std::int16_t a, std::int16_t b) { - std::int32_t a32 = a; - std::int32_t b32 = b; - std::int32_t sum = a32 + b32; - std::int32_t sign = sum >= 0 ? 1 : -1; - return static_cast((sum + sign) / 2); -} - -template -IntegerType SaturatingAdd(IntegerType a, IntegerType b) { - static_assert(std::is_same::value, "unimplemented"); - (void)b; - return a; -} - -// So far this is only needed for int16. -template <> -inline std::int16_t SaturatingAdd(std::int16_t a, std::int16_t b) { - std::int32_t a32 = a; - std::int32_t b32 = b; - std::int32_t sum = a32 + b32; - return static_cast( - std::min(static_cast(32767), - std::max(static_cast(-32768), sum))); -} - -// Returns a+b, saturating if the integers are 16bit or narrower, -// otherwise just a plain addition. -template -struct AddSaturatingIf16BitImpl { - static IntegerType Run(IntegerType a, IntegerType b) { return Add(a, b); } -}; -template -struct AddSaturatingIf16BitImpl { - static IntegerType Run(IntegerType a, IntegerType b) { - return SaturatingAdd(a, b); - } -}; -template -IntegerType AddSaturatingIf16Bit(IntegerType a, IntegerType b) { - using ScalarType = - typename FixedPointRawTypeTraits::ScalarRawType; - return AddSaturatingIf16BitImpl::Run(a, - b); -} - -// Returns the integer that represents the product of two fixed-point -// numbers, interpreting all integers as fixed-point values in the -// interval [-1, 1), rounding to the nearest value, and saturating -// -1 * -1 to the maximum value (since 1 is not in the half-open -// interval [-1, 1)). -// -// [The explanation below specializes to std::int32_t for example purpose.] -// -// The mapping between IntegerType and the interval [-1, 1) is unique and -// implied by IntegerType, which is assumed to be signed. For example, -// for IntegerType==std::int32_t, the mapping is -// real_value = integer_value / 2^31. -// So in this case, and leaving aside rounding and saturating, this -// function computes ((a / 2^31) * (b / 2^31)) * 2^31, which simplifies to -// (a * b) / 2^31. -// -// The 'doubling' part in the name of this function comes from the fact that -// this operation is very close to a "multiply-high" operation, keeping only -// the top half bits, except that that would be effectively computing -// (a * b) / 2^32, -// so here we are computing 2x that, since -// 1/2^31 = 2 * 1/2^32. -// The idea is to use all of the available 32 bits in the destination int32 -// value. -// -// [End of the explanation specializing to int32.] -// -// This is equivalent to the VQRDMULH instruction in ARM NEON. -template -IntegerType SaturatingRoundingDoublingHighMul(IntegerType a, IntegerType b) { - static_assert(std::is_same::value, "unimplemented"); - (void)b; - return a; -} - -// This function implements the same computation as the ARMv7 NEON VQRDMULH -// instruction. -template <> -inline std::int32_t SaturatingRoundingDoublingHighMul(std::int32_t a, - std::int32_t b) { - bool overflow = a == b && a == std::numeric_limits::min(); - std::int64_t a_64(a); - std::int64_t b_64(b); - std::int64_t ab_64 = a_64 * b_64; - std::int32_t nudge = ab_64 >= 0 ? (1 << 30) : (1 - (1 << 30)); - std::int32_t ab_x2_high32 = - static_cast((ab_64 + nudge) / (1ll << 31)); - return overflow ? std::numeric_limits::max() : ab_x2_high32; -} - -template <> -inline std::int16_t SaturatingRoundingDoublingHighMul(std::int16_t a, - std::int16_t b) { - bool overflow = a == b && a == std::numeric_limits::min(); - std::int32_t a_32(a); - std::int32_t b_32(b); - std::int32_t ab_32 = a_32 * b_32; - std::int16_t nudge = ab_32 >= 0 ? (1 << 14) : (1 - (1 << 14)); - std::int16_t ab_x2_high16 = - static_cast((ab_32 + nudge) / (1 << 15)); - return overflow ? std::numeric_limits::max() : ab_x2_high16; -} - -// Correctly-rounded-to-nearest division by a power-of-two. -// Also known as a rounding arithmetic right shift. -template -inline IntegerType RoundingDivideByPOT(IntegerType x, int exponent) { - assert(exponent >= 0); - assert(exponent <= 31); - const IntegerType mask = Dup((1ll << exponent) - 1); - const IntegerType zero = Dup(0); - const IntegerType one = Dup(1); - const IntegerType remainder = BitAnd(x, mask); - const IntegerType threshold = - Add(ShiftRight(mask, 1), BitAnd(MaskIfLessThan(x, zero), one)); - return Add(ShiftRight(x, exponent), - BitAnd(MaskIfGreaterThan(remainder, threshold), one)); -} - -// Returns the product of a run-time integer value by a compile-time power -// of two, with either a positive exponent (equivalent to an arithmetic -// left shift, saturating) or a negative exponent (equivalent to an arithmetic -// right shift, rounding to nearest). -template 0 ? 1 : Exponent < 0 ? -1 : 0)> -struct ImplSaturatingRoundingMultiplyByPOT {}; - -template -struct ImplSaturatingRoundingMultiplyByPOT { - static IntegerType eval(IntegerType x) { return x; } -}; - -template -struct ImplSaturatingRoundingMultiplyByPOT { - static IntegerType eval(IntegerType x) { - using ScalarIntegerType = - typename FixedPointRawTypeTraits::ScalarRawType; - const IntegerType min = - Dup(std::numeric_limits::min()); - const IntegerType max = - Dup(std::numeric_limits::max()); - const int ScalarIntegerTypeBits = 8 * sizeof(ScalarIntegerType); - - const std::int32_t threshold = - ((1 << (ScalarIntegerTypeBits - 1 - Exponent)) - 1); - const IntegerType positive_mask = - MaskIfGreaterThan(x, Dup(threshold)); - const IntegerType negative_mask = - MaskIfLessThan(x, Dup(-threshold)); - - IntegerType result = ShiftLeft(x, Exponent); - result = SelectUsingMask(positive_mask, max, result); - result = SelectUsingMask(negative_mask, min, result); - return result; - } -}; - -template -struct ImplSaturatingRoundingMultiplyByPOT { - static IntegerType eval(IntegerType x) { - return RoundingDivideByPOT(x, -Exponent); - } -}; - -template -IntegerType SaturatingRoundingMultiplyByPOT(IntegerType x) { - return ImplSaturatingRoundingMultiplyByPOT::eval(x); -} - -// Part 2: the FixedPoint class. - -// A FixedPoint object represents a fixed-point value stored in the underlying -// integer type tRawType, if tRawType is a plain scalar integer type. -// Alternatively, tRawType may be a SIMD type (e.g. NEON int32x4_t) in which -// case a FixedPoint object represents a corresponding SIMD vector of fixed -// point values. -// -// tIntegerBits describes the range of the fixed-point format: if -// tIntegerBits == m then the range of representable values is the half-open -// interval [-2^m; 2^m) where the open boundary on the right side means that -// 2^m is not representable (how close the maximum representable value is to -// it, depends on bit-depth of tRawType). -// -// In "Q format notation", -// https://en.wikipedia.org/wiki/Q_(number_format) -// we are describing the format -// Qm.n -// where -// m = tIntegerBits -// and -// n = NumberOfBits(tRawType) - (m + 1) -// Note that the (m + 1) in the above line is because we adopt the convention -// that we count the integer bits exclusively of the sign bit; so (m + 1) is -// the total number of integer bits inclusive of the sign bit. -// -// Accordingly, the number of integral representable values in our range -// [-2^m ; 2^m) -// is equal to 2^(m+1). -template -class FixedPoint { - public: - typedef tRawType RawType; - - typedef FixedPointRawTypeTraits RawTypeTraits; - typedef typename RawTypeTraits::ScalarRawType ScalarRawType; - - static constexpr int kTotalBits = 8 * sizeof(ScalarRawType); - static constexpr int kIntegerBits = tIntegerBits; - static constexpr int kFractionalBits = kTotalBits - 1 - kIntegerBits; - static_assert(kIntegerBits >= 0 && kIntegerBits < kTotalBits, - "bad IntegerBits"); - - typedef FixedPoint ScalarFixedPointType; - - static const ScalarRawType ScalarRawMin() { - return std::numeric_limits::min(); - } - - static const ScalarRawType ScalarRawMax() { - return std::numeric_limits::max(); - } - - static const ScalarRawType RawMin() { - return VectorFromScalar(ScalarRawMin()); - } - - static const ScalarRawType RawMax() { - return VectorFromScalar(ScalarRawMax()); - } - - static FixedPoint FromRaw(RawType x) { - FixedPoint retval; - retval.raw() = x; - return retval; - } - - static FixedPoint FromScalarRaw(ScalarRawType x) { - FixedPoint retval; - retval.raw() = Dup(x); - return retval; - } - - static FixedPoint FromScalarFixedPoint(ScalarFixedPointType x) { - return FromScalarRaw(x.raw()); - } - - template - static FixedPoint ConstantPOT() { - static constexpr int kOffset = kFractionalBits + Exponent; - static_assert( - kOffset < 31, - "Constant not exactly representable in this fixed-point format"); - return FromScalarRaw(ScalarRawType(1) << kOffset); - } - - static FixedPoint Zero() { return FromScalarRaw(0); } - - static FixedPoint One() { - return FromScalarRaw( - kIntegerBits == 0 - ? ScalarRawMax() - : (ScalarRawType(1) << (kIntegerBits == 0 ? 0 : kFractionalBits))); - } - - static FixedPoint FromDouble(double x) { - const double min_bound = static_cast(ScalarRawMin()); - const double max_bound = static_cast(ScalarRawMax()); - return FromScalarRaw(static_cast(std::min( - std::max(round(x * static_cast(1ll << kFractionalBits)), - min_bound), - max_bound))); - } - - RawType raw() const { return i_; } - RawType& raw() { return i_; } - - private: - RawType i_; -}; - -// Part 3: implementation of arithmetic operators for the -// FixedPoint class, and a few related functions. - -// A FixedPoint multiplication is just a -// SaturatingRoundingDoublingHighMul operation on the underlying -// raw integer values. The IntegerBits simply add up, as is obvious -// from the fact that the range is [-2^IntegerBits, 2^IntegerBits). -template -FixedPoint operator*( - FixedPoint a, - FixedPoint b) { - FixedPoint c; - c.raw() = SaturatingRoundingDoublingHighMul(a.raw(), b.raw()); - return c; -} - -// Tweaking IntegerBits gives exact multiplication by a power of two. -template -FixedPoint ExactMulByPot( - FixedPoint a) { - FixedPoint c; - c.raw() = a.raw(); - return c; -} - -// If we want to leave IntegerBits fixed, then multiplication -// by a power of two has to be saturating/rounding, not exact anymore. -template -FixedPoint SaturatingRoundingMultiplyByPOT( - FixedPoint a) { - return FixedPoint::FromRaw( - SaturatingRoundingMultiplyByPOT(a.raw())); -} - -// Generic arithmetic operators. - -#define MAKE_FIXEDPOINT_UNARY_FUNC(FuncName, ImplFuncName) \ - template \ - FixedPoint FuncName( \ - FixedPoint a) { \ - return FixedPoint::FromRaw(ImplFuncName(a.raw())); \ - } - -#define MAKE_FIXEDPOINT_BINARY_FUNC(FuncName, ImplFuncName) \ - template \ - FixedPoint FuncName( \ - FixedPoint a, \ - FixedPoint b) { \ - return FixedPoint::FromRaw( \ - ImplFuncName(a.raw(), b.raw())); \ - } - -MAKE_FIXEDPOINT_UNARY_FUNC(operator-, Neg) -MAKE_FIXEDPOINT_UNARY_FUNC(operator~, BitNot) -MAKE_FIXEDPOINT_BINARY_FUNC(operator+, Add) -MAKE_FIXEDPOINT_BINARY_FUNC(operator-, Sub) -MAKE_FIXEDPOINT_BINARY_FUNC(operator&, BitAnd) -MAKE_FIXEDPOINT_BINARY_FUNC(operator^, BitXor) -MAKE_FIXEDPOINT_BINARY_FUNC(operator|, BitOr) -MAKE_FIXEDPOINT_BINARY_FUNC(RoundingHalfSum, RoundingHalfSum) - -#undef MAKE_FIXEDPOINT_UNARY_FUNC -#undef MAKE_FIXEDPOINT_BINARY_FUNC - -#define MAKE_FIXEDPOINT_UNARY_FUNC_RETURNING_RAW(FuncName) \ - template \ - tRawType FuncName(FixedPoint a) { \ - return FuncName(a.raw()); \ - } - -#define MAKE_FIXEDPOINT_BINARY_FUNC_RETURNING_RAW(FuncName) \ - template \ - tRawType FuncName(FixedPoint a, \ - FixedPoint b) { \ - return FuncName(a.raw(), b.raw()); \ - } - -MAKE_FIXEDPOINT_UNARY_FUNC_RETURNING_RAW(MaskIfZero) -MAKE_FIXEDPOINT_UNARY_FUNC_RETURNING_RAW(MaskIfNonZero) -MAKE_FIXEDPOINT_BINARY_FUNC_RETURNING_RAW(MaskIfEqual) -MAKE_FIXEDPOINT_BINARY_FUNC_RETURNING_RAW(MaskIfNotEqual) -MAKE_FIXEDPOINT_BINARY_FUNC_RETURNING_RAW(MaskIfGreaterThan) -MAKE_FIXEDPOINT_BINARY_FUNC_RETURNING_RAW(MaskIfGreaterThanOrEqual) -MAKE_FIXEDPOINT_BINARY_FUNC_RETURNING_RAW(MaskIfLessThan) -MAKE_FIXEDPOINT_BINARY_FUNC_RETURNING_RAW(MaskIfLessThanOrEqual) - -#undef MAKE_FIXEDPOINT_UNARY_FUNC_RETURNING_RAW -#undef MAKE_FIXEDPOINT_BINARY_FUNC_RETURNING_RAW - -template -FixedPoint SelectUsingMask( - tRawType if_mask, FixedPoint then_val, - FixedPoint else_val) { - return FixedPoint::FromRaw( - SelectUsingMask(if_mask, then_val.raw(), else_val.raw())); -} - -template -bool operator==(FixedPoint a, - FixedPoint b) { - return All(MaskIfEqual(a.raw(), b.raw())); -} - -template -bool operator!=(FixedPoint a, - FixedPoint b) { - return !(a == b); -} - -template -FixedPoint SaturatingAdd( - FixedPoint a, - FixedPoint b) { - return FixedPoint::FromRaw( - SaturatingAdd(a.raw(), b.raw())); -} - -template -FixedPoint AddSaturatingIf16Bit( - FixedPoint a, - FixedPoint b) { - return FixedPoint::FromRaw( - AddSaturatingIf16Bit(a.raw(), b.raw())); -} - -// Conversion to floating-point. -template -double ToDouble(FixedPoint x) { - static_assert(FixedPointRawTypeTraits::kLanes == 1, - "not applicable to SIMD types"); - typedef FixedPoint F; - return x.raw() / static_cast(1ll << F::kFractionalBits); -} - -// Rescale changes the number of IntegerBits and updates the underlying -// raw integer value accordingly. -template -FixedPoint Rescale( - FixedPoint x) { - static constexpr int kExponent = tIntegerBitsSrc - tIntegerBitsDst; - FixedPoint result; - result.raw() = SaturatingRoundingMultiplyByPOT(x.raw()); - return result; -} - -// CheckedFixedPointConstant allows to specify fixed-point constants -// initialized as real numbers, in a way that does not compile floating-point -// arithmetic in production code, yet still checks agreement with the -// floating-point expressions when asserts are enabled. -// -// The raw integer value provided is always a int32, encoding a 32-bit -// fixed-point value, regardless of the actual Scalar type. This allows -// writing generic code that applies just as well to the 32-bit and 16-bit -// cases. In the 16-bit case, the raw integer value is internally -// rounding-shifted by 16 bits to the right. -template -inline typename FixedPointType::ScalarRawType RescaleConstantInitializer( - std::int32_t int32_value) { - typedef typename FixedPointType::ScalarRawType ScalarRawType; - static constexpr int ScalarTypeBits = 8 * sizeof(ScalarRawType); - return static_cast( - RoundingDivideByPOT(int32_value, 32 - ScalarTypeBits)); -} -#ifdef GEMMLOWP_ENABLE_FIXEDPOINT_CONSTANTS_CHECKS -template -FixedPointType CheckedFixedPointConstant(std::int32_t raw_value, - double double_value) { - const FixedPointType result = FixedPointType::FromScalarRaw(raw_value); - assert(result == FixedPointType::FromDouble(double_value)); - return result; -} -#define GEMMLOWP_CHECKED_FIXEDPOINT_CONSTANT(FixedPointType, \ - ScalarRawInt32Value, DoubleValue) \ - (gemmlowp::CheckedFixedPointConstant( \ - gemmlowp::RescaleConstantInitializer( \ - ScalarRawInt32Value), \ - DoubleValue)) - -#else -#define GEMMLOWP_CHECKED_FIXEDPOINT_CONSTANT(FixedPointType, \ - ScalarRawInt32Value, DoubleValue) \ - (FixedPointType::FromScalarRaw( \ - gemmlowp::RescaleConstantInitializer( \ - ScalarRawInt32Value))) -#endif - -// Implementation of exponential function. - -// Returns exp(x) for x in [-1/4, 0). -template -FixedPoint exp_on_interval_between_negative_one_quarter_and_0_excl( - FixedPoint a) { - typedef FixedPoint F; - const F constant_term = - GEMMLOWP_CHECKED_FIXEDPOINT_CONSTANT(F, 1895147668, std::exp(-1.0 / 8.0)); - const F constant_1_over_3 = - GEMMLOWP_CHECKED_FIXEDPOINT_CONSTANT(F, 715827883, 1.0 / 3.0); - // We're evaluating a Taylor expansion around -1/8, so we do the change of - // variable: x = a + 1/8. - // In fixed-point with 0 integer bits, 1/8 is represented by 1 << 28. - F x = a + F::template ConstantPOT<-3>(); - F x2 = x * x; - F x3 = x2 * x; - F x4 = x2 * x2; - F x4_over_4 = SaturatingRoundingMultiplyByPOT<-2>(x4); - F x4_over_24_plus_x3_over_6_plus_x2_over_2 = - SaturatingRoundingMultiplyByPOT<-1>( - ((x4_over_4 + x3) * constant_1_over_3) + x2); - return AddSaturatingIf16Bit( - constant_term, - constant_term * (x + x4_over_24_plus_x3_over_6_plus_x2_over_2)); -} - -// Returns exp(x) for x < 0. -template -FixedPoint exp_on_negative_values( - FixedPoint a) { - typedef FixedPoint InputF; - typedef FixedPoint ResultF; - static constexpr int kFractionalBits = InputF::kFractionalBits; - static constexpr int kIntegerBits = InputF::kIntegerBits; - const InputF kOneQuarter = InputF::template ConstantPOT<-2>(); - InputF mask = kOneQuarter - InputF::FromScalarRaw(1); - InputF a_mod_quarter_minus_one_quarter = (a & mask) - kOneQuarter; - ResultF result = exp_on_interval_between_negative_one_quarter_and_0_excl( - Rescale<0>(a_mod_quarter_minus_one_quarter)); - tRawType remainder = (a_mod_quarter_minus_one_quarter - a).raw(); - -#define GEMMLOWP_EXP_BARREL_SHIFTER(Exponent, FixedPointMultiplier) \ - if (kIntegerBits > Exponent) { \ - const ResultF kMultiplier = GEMMLOWP_CHECKED_FIXEDPOINT_CONSTANT( \ - ResultF, FixedPointMultiplier, std::exp(-std::pow(2.0, Exponent))); \ - static constexpr int kShiftAmount = \ - kIntegerBits > Exponent ? kFractionalBits + Exponent : 0; \ - result = SelectUsingMask( \ - MaskIfNonZero(BitAnd(remainder, Dup(1 << kShiftAmount))), \ - result * kMultiplier, result); \ - } - - GEMMLOWP_EXP_BARREL_SHIFTER(-2, 1672461947); - GEMMLOWP_EXP_BARREL_SHIFTER(-1, 1302514674); - GEMMLOWP_EXP_BARREL_SHIFTER(+0, 790015084); - GEMMLOWP_EXP_BARREL_SHIFTER(+1, 290630308); - GEMMLOWP_EXP_BARREL_SHIFTER(+2, 39332535); - GEMMLOWP_EXP_BARREL_SHIFTER(+3, 720401); - GEMMLOWP_EXP_BARREL_SHIFTER(+4, 242); - -#undef GEMMLOWP_EXP_BARREL_SHIFTER - - static constexpr int clampB = kIntegerBits > 5 ? 36 - kIntegerBits : 0; - if (kIntegerBits > 5) { - const InputF clamp = - GEMMLOWP_CHECKED_FIXEDPOINT_CONSTANT(InputF, -(1 << clampB), -32.0); - result = SelectUsingMask(MaskIfLessThan(a, clamp), ResultF::Zero(), result); - } - - result = SelectUsingMask(MaskIfZero(a), ResultF::One(), result); - return result; -} - -// Implementation of tanh: (1 - exp(-2x)) / (1 + exp(-2x)). - -// Returns (1 - x) / (1 + x) for x in (0, 1). -template -FixedPoint one_minus_x_over_one_plus_x_for_x_in_0_1( - FixedPoint a) { - typedef FixedPoint F0; - typedef FixedPoint F2; - F0 half_denominator = RoundingHalfSum(a, F0::One()); - // Newton-Raphson division - // https://en.wikipedia.org/wiki/Division_algorithm#Newton.E2.80.93Raphson_division - // Refer to that page for the logic behind the 48/17 and 32/17 constants. - const F2 constant_48_over_17 = - GEMMLOWP_CHECKED_FIXEDPOINT_CONSTANT(F2, 1515870810, 48.0 / 17.0); - const F2 constant_neg_32_over_17 = - GEMMLOWP_CHECKED_FIXEDPOINT_CONSTANT(F2, -1010580540, -32.0 / 17.0); - F2 x = constant_48_over_17 + half_denominator * constant_neg_32_over_17; - for (int i = 0; i < 3; i++) { - F2 half_denominator_times_x = half_denominator * x; - F2 one_minus_half_denominator_times_x = - F2::One() - half_denominator_times_x; - x = x + Rescale<2>(x * one_minus_half_denominator_times_x); - } - return Rescale<0>(x - F2::One()); -} - -// Returns -tanh(x) for x < 0. -template -FixedPoint neg_tanh_on_negative_values( - FixedPoint a) { - return one_minus_x_over_one_plus_x_for_x_in_0_1( - exp_on_negative_values(ExactMulByPot<1>(a))); -} - -// Returns tanh(x) for any x. -template -FixedPoint tanh(FixedPoint a) { - typedef FixedPoint InputF; - typedef FixedPoint ResultF; - tRawType mask_if_negative = MaskIfLessThan(a, InputF::Zero()); - tRawType mask_if_zero = MaskIfZero(a); - InputF n = SelectUsingMask(mask_if_negative, a, -a); - ResultF t = neg_tanh_on_negative_values(n); - return SelectUsingMask(mask_if_zero, ResultF::Zero(), - SelectUsingMask(mask_if_negative, -t, t)); -} - -// Implementation of logistic function. - -// Returns 1 / (1 + x) for x in (0, 1). -template -FixedPoint one_over_one_plus_x_for_x_in_0_1( - FixedPoint a) { - typedef FixedPoint F0; - typedef FixedPoint F2; - F0 half_denominator = RoundingHalfSum(a, F0::One()); - // Newton-Raphson division - // https://en.wikipedia.org/wiki/Division_algorithm#Newton.E2.80.93Raphson_division - // Refer to that page for the logic behind the 48/17 and 32/17 constants. - const F2 constant_48_over_17 = - GEMMLOWP_CHECKED_FIXEDPOINT_CONSTANT(F2, 1515870810, 48.0 / 17.0); - const F2 constant_neg_32_over_17 = - GEMMLOWP_CHECKED_FIXEDPOINT_CONSTANT(F2, -1010580540, -32.0 / 17.0); - F2 x = constant_48_over_17 + half_denominator * constant_neg_32_over_17; - for (int i = 0; i < 3; i++) { - F2 half_denominator_times_x = half_denominator * x; - F2 one_minus_half_denominator_times_x = - F2::One() - half_denominator_times_x; - x = x + Rescale<2>(x * one_minus_half_denominator_times_x); - } - return Rescale<0>(ExactMulByPot<-1>(x)); -} - -// Returns logistic(x) = 1 / (1 + exp(-x)) for x > 0. -template -FixedPoint logistic_on_positive_values( - FixedPoint a) { - return one_over_one_plus_x_for_x_in_0_1(exp_on_negative_values(-a)); -} - -// Returns logistic(x) = 1 / (1 + exp(-x)) for any x. -template -FixedPoint logistic(FixedPoint a) { - typedef FixedPoint InputF; - typedef FixedPoint ResultF; - tRawType mask_if_positive = MaskIfGreaterThan(a, InputF::Zero()); - tRawType mask_if_zero = MaskIfZero(a); - InputF abs_input = SelectUsingMask(mask_if_positive, a, -a); - ResultF result_if_positive = logistic_on_positive_values(abs_input); - ResultF result_if_negative = ResultF::One() - result_if_positive; - const ResultF one_half = - GEMMLOWP_CHECKED_FIXEDPOINT_CONSTANT(ResultF, 1 << 30, 0.5); - return SelectUsingMask(mask_if_zero, one_half, - SelectUsingMask(mask_if_positive, result_if_positive, - result_if_negative)); -} - -} // end namespace gemmlowp - -#ifdef GEMMLOWP_NEON -#include "./fixedpoint_neon.h" -#elif defined(GEMMLOWP_AVX2) -#include "./fixedpoint_avx.h" -#elif defined(GEMMLOWP_SSE4) -#include "./fixedpoint_sse.h" -#elif defined(GEMMLOWP_MSA) -#include "./fixedpoint_msa.h" -#endif - -#endif // GEMMLOWP_INTERNAL_FIXEDPOINT_H_ diff --git a/code/components/tflite-lib/third_party/gemmlowp/fixedpoint/fixedpoint_neon.h b/code/components/tflite-lib/third_party/gemmlowp/fixedpoint/fixedpoint_neon.h deleted file mode 100644 index 646c5907..00000000 --- a/code/components/tflite-lib/third_party/gemmlowp/fixedpoint/fixedpoint_neon.h +++ /dev/null @@ -1,331 +0,0 @@ -// Copyright 2015 The Gemmlowp Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// fixedpoint_neon.h: optimized NEON specializations of the templates -// in fixedpoint.h. - -#ifndef GEMMLOWP_INTERNAL_FIXEDPOINT_NEON_H_ -#define GEMMLOWP_INTERNAL_FIXEDPOINT_NEON_H_ - -#include - -namespace gemmlowp { - -template <> -struct FixedPointRawTypeTraits { - typedef std::int32_t ScalarRawType; - static constexpr int kLanes = 4; -}; - -template <> -struct FixedPointRawTypeTraits { - typedef std::int16_t ScalarRawType; - static constexpr int kLanes = 8; -}; - -template <> -inline int32x4_t BitAnd(int32x4_t a, int32x4_t b) { - return vandq_s32(a, b); -} - -template <> -inline int16x8_t BitAnd(int16x8_t a, int16x8_t b) { - return vandq_s16(a, b); -} - -template <> -inline int32x4_t BitOr(int32x4_t a, int32x4_t b) { - return vorrq_s32(a, b); -} - -template <> -inline int16x8_t BitOr(int16x8_t a, int16x8_t b) { - return vorrq_s16(a, b); -} - -template <> -inline int32x4_t BitXor(int32x4_t a, int32x4_t b) { - return veorq_s32(a, b); -} - -template <> -inline int16x8_t BitXor(int16x8_t a, int16x8_t b) { - return veorq_s16(a, b); -} - -template <> -inline int32x4_t BitNot(int32x4_t a) { - return veorq_s32(a, vdupq_n_s32(-1)); -} - -template <> -inline int16x8_t BitNot(int16x8_t a) { - return veorq_s16(a, vdupq_n_s16(-1)); -} - -template <> -inline int32x4_t Add(int32x4_t a, int32x4_t b) { - return vaddq_s32(a, b); -} - -template <> -inline int16x8_t Add(int16x8_t a, int16x8_t b) { - return vaddq_s16(a, b); -} - -template <> -inline int32x4_t Sub(int32x4_t a, int32x4_t b) { - return vsubq_s32(a, b); -} - -template <> -inline int16x8_t Sub(int16x8_t a, int16x8_t b) { - return vsubq_s16(a, b); -} - -template <> -inline int32x4_t Neg(int32x4_t a) { - return vnegq_s32(a); -} - -template <> -inline int16x8_t Neg(int16x8_t a) { - return vnegq_s16(a); -} - -template <> -inline int32x4_t ShiftLeft(int32x4_t a, int offset) { - return vshlq_s32(a, vdupq_n_s32(offset)); -} - -template <> -inline int16x8_t ShiftLeft(int16x8_t a, int offset) { - return vshlq_s16(a, vdupq_n_s16(offset)); -} - -template <> -inline int32x4_t ShiftRight(int32x4_t a, int offset) { - return vshlq_s32(a, vdupq_n_s32(-offset)); -} - -template <> -inline int16x8_t ShiftRight(int16x8_t a, int offset) { - return vshlq_s16(a, vdupq_n_s16(-offset)); -} - -template <> -inline int32x4_t SelectUsingMask(int32x4_t if_mask, int32x4_t then_val, - int32x4_t else_val) { - return vbslq_s32(vreinterpretq_u32_s32(if_mask), then_val, else_val); -} - -template <> -inline int16x8_t SelectUsingMask(int16x8_t if_mask, int16x8_t then_val, - int16x8_t else_val) { - return vbslq_s16(vreinterpretq_u16_s16(if_mask), then_val, else_val); -} - -template <> -inline int32x4_t MaskIfEqual(int32x4_t a, int32x4_t b) { - return vreinterpretq_s32_u32(vceqq_s32(a, b)); -} - -template <> -inline int16x8_t MaskIfEqual(int16x8_t a, int16x8_t b) { - return vreinterpretq_s16_u16(vceqq_s16(a, b)); -} - -template <> -inline int32x4_t MaskIfNotEqual(int32x4_t a, int32x4_t b) { - return BitNot(MaskIfEqual(a, b)); -} - -template <> -inline int16x8_t MaskIfNotEqual(int16x8_t a, int16x8_t b) { - return BitNot(MaskIfEqual(a, b)); -} - -template <> -inline int32x4_t MaskIfZero(int32x4_t a) { - return MaskIfEqual(a, vdupq_n_s32(0)); -} - -template <> -inline int16x8_t MaskIfZero(int16x8_t a) { - return MaskIfEqual(a, vdupq_n_s16(0)); -} - -template <> -inline int32x4_t MaskIfNonZero(int32x4_t a) { - return vreinterpretq_s32_u32(vtstq_s32(a, a)); -} - -template <> -inline int16x8_t MaskIfNonZero(int16x8_t a) { - return vreinterpretq_s16_u16(vtstq_s16(a, a)); -} - -template <> -inline int32x4_t MaskIfGreaterThan(int32x4_t a, int32x4_t b) { - return vreinterpretq_s32_u32(vcgtq_s32(a, b)); -} - -template <> -inline int16x8_t MaskIfGreaterThan(int16x8_t a, int16x8_t b) { - return vreinterpretq_s16_u16(vcgtq_s16(a, b)); -} - -template <> -inline int32x4_t MaskIfGreaterThanOrEqual(int32x4_t a, int32x4_t b) { - return vreinterpretq_s32_u32(vcgeq_s32(a, b)); -} - -template <> -inline int16x8_t MaskIfGreaterThanOrEqual(int16x8_t a, int16x8_t b) { - return vreinterpretq_s16_u16(vcgeq_s16(a, b)); -} - -template <> -inline int32x4_t MaskIfLessThan(int32x4_t a, int32x4_t b) { - return vreinterpretq_s32_u32(vcltq_s32(a, b)); -} - -template <> -inline int16x8_t MaskIfLessThan(int16x8_t a, int16x8_t b) { - return vreinterpretq_s16_u16(vcltq_s16(a, b)); -} - -template <> -inline int32x4_t MaskIfLessThanOrEqual(int32x4_t a, int32x4_t b) { - return vreinterpretq_s32_u32(vcleq_s32(a, b)); -} - -template <> -inline int16x8_t MaskIfLessThanOrEqual(int16x8_t a, int16x8_t b) { - return vreinterpretq_s16_u16(vcleq_s16(a, b)); -} - -template <> -inline bool All(int32x4_t a) { - a = vandq_s32(a, vextq_s32(a, a, 1)); - a = vandq_s32(a, vextq_s32(a, a, 2)); - return vgetq_lane_s32(a, 0); -} - -template <> -inline bool All(int16x8_t a) { - a = vandq_s16(a, vextq_s16(a, a, 1)); - a = vandq_s16(a, vextq_s16(a, a, 2)); - a = vandq_s16(a, vextq_s16(a, a, 4)); - return vgetq_lane_s16(a, 0); -} - -template <> -inline bool Any(int32x4_t a) { - a = vorrq_s32(a, vextq_s32(a, a, 1)); - a = vorrq_s32(a, vextq_s32(a, a, 2)); - return vgetq_lane_s32(a, 0); -} - -template <> -inline bool Any(int16x8_t a) { - a = vorrq_s16(a, vextq_s16(a, a, 1)); - a = vorrq_s16(a, vextq_s16(a, a, 2)); - a = vorrq_s16(a, vextq_s16(a, a, 4)); - return vgetq_lane_s16(a, 0); -} - -template <> -inline int32x4_t RoundingHalfSum(int32x4_t a, int32x4_t b) { - return vrhaddq_s32(a, b); -} - -template <> -inline int16x8_t RoundingHalfSum(int16x8_t a, int16x8_t b) { - return vrhaddq_s16(a, b); -} - -template <> -inline int32x4_t SaturatingRoundingDoublingHighMul(int32x4_t a, int32x4_t b) { - return vqrdmulhq_s32(a, b); -} - -template <> -inline int16x8_t SaturatingRoundingDoublingHighMul(int16x8_t a, int16x8_t b) { - return vqrdmulhq_s16(a, b); -} - -template <> -inline int32x4_t RoundingDivideByPOT(int32x4_t x, int exponent) { - const int32x4_t shift_vec = vdupq_n_s32(-exponent); - const int32x4_t fixup = vshrq_n_s32(vandq_s32(x, shift_vec), 31); - const int32x4_t fixed_up_x = vqaddq_s32(x, fixup); - return vrshlq_s32(fixed_up_x, shift_vec); -} - -template <> -inline int16x8_t RoundingDivideByPOT(int16x8_t x, int exponent) { - const int16x8_t shift_vec = vdupq_n_s16(-exponent); - const int16x8_t fixup = vshrq_n_s16(vandq_s16(x, shift_vec), 15); - const int16x8_t fixed_up_x = vqaddq_s16(x, fixup); - return vrshlq_s16(fixed_up_x, shift_vec); -} - -template -struct ImplSaturatingRoundingMultiplyByPOT { - static int32x4_t eval(int32x4_t x) { return vqshlq_n_s32(x, Exponent); } -}; - -template -struct ImplSaturatingRoundingMultiplyByPOT { - static int32x4_t eval(int32x4_t x) { - const int32x4_t fixup = vshrq_n_s32(x, 31); - const int32x4_t fixed_up_x = vqaddq_s32(x, fixup); - return vrshrq_n_s32(fixed_up_x, -Exponent); - } -}; - -template -struct ImplSaturatingRoundingMultiplyByPOT { - static int16x8_t eval(int16x8_t x) { return vqshlq_n_s16(x, Exponent); } -}; - -template -struct ImplSaturatingRoundingMultiplyByPOT { - static int16x8_t eval(int16x8_t x) { - const int16x8_t fixup = vshrq_n_s16(x, 15); - const int16x8_t fixed_up_x = vqaddq_s16(x, fixup); - return vrshrq_n_s16(fixed_up_x, -Exponent); - } -}; - -template <> -inline int32x4_t Dup(std::int32_t x) { - return vdupq_n_s32(x); -} - -template <> -inline int16x8_t Dup(std::int16_t x) { - return vdupq_n_s16(x); -} - -// So far this is only needed for int16. -template <> -inline int16x8_t SaturatingAdd(int16x8_t a, int16x8_t b) { - return vqaddq_s16(a, b); -} - -} // end namespace gemmlowp - -#endif // GEMMLOWP_INTERNAL_FIXEDPOINT_NEON_H_ diff --git a/code/components/tflite-lib/third_party/gemmlowp/fixedpoint/fixedpoint_sse.h b/code/components/tflite-lib/third_party/gemmlowp/fixedpoint/fixedpoint_sse.h deleted file mode 100644 index a1fae32d..00000000 --- a/code/components/tflite-lib/third_party/gemmlowp/fixedpoint/fixedpoint_sse.h +++ /dev/null @@ -1,384 +0,0 @@ -// Copyright 2015 Google Inc. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// fixedpoint_SSE.h: optimized SSE specializations of the templates -// in fixedpoint.h. - -#ifndef GEMMLOWP_INTERNAL_FIXEDPOINT_SSE_H_ -#define GEMMLOWP_INTERNAL_FIXEDPOINT_SSE_H_ - -#include -#include "fixedpoint.h" - -namespace gemmlowp { - -// SSE intrinsics are not finely typed: there is a single __m128i vector -// type that does not distinguish between "int32x4" and "int16x8" use -// cases, unlike the NEON equivalents. Because we had initially focused -// on int32x4, we did not pay attention and specialized these fixedpoint -// templates directly for __m128i hardcoding the int32x4 semantics, -// not leaving room for int16x8 semantics. Amending that by adding a separate -// data type, int16x8_m128i, that wraps __m128i while being a separate -// type. -struct int16x8_m128i { - int16x8_m128i() {} - explicit int16x8_m128i(__m128i w) : v(w) {} - ~int16x8_m128i() {} - - __m128i v; -}; - -template <> -struct FixedPointRawTypeTraits<__m128i> { - typedef std::int32_t ScalarRawType; - static constexpr int kLanes = 4; -}; - -template <> -struct FixedPointRawTypeTraits { - typedef std::int16_t ScalarRawType; - static constexpr int kLanes = 8; -}; - -template <> -inline __m128i BitAnd(__m128i a, __m128i b) { - return _mm_and_si128(a, b); -} - -template <> -inline int16x8_m128i BitAnd(int16x8_m128i a, int16x8_m128i b) { - return int16x8_m128i(_mm_and_si128(a.v, b.v)); -} - -template <> -inline __m128i BitOr(__m128i a, __m128i b) { - return _mm_or_si128(a, b); -} - -template <> -inline int16x8_m128i BitOr(int16x8_m128i a, int16x8_m128i b) { - return int16x8_m128i(_mm_or_si128(a.v, b.v)); -} - -template <> -inline __m128i BitXor(__m128i a, __m128i b) { - return _mm_xor_si128(a, b); -} - -template <> -inline int16x8_m128i BitXor(int16x8_m128i a, int16x8_m128i b) { - return int16x8_m128i(_mm_xor_si128(a.v, b.v)); -} - -template <> -inline __m128i BitNot(__m128i a) { - return _mm_andnot_si128(a, _mm_set1_epi32(-1)); -} - -template <> -inline int16x8_m128i BitNot(int16x8_m128i a) { - return int16x8_m128i(_mm_andnot_si128(a.v, _mm_set1_epi16(-1))); -} - -template <> -inline __m128i Add(__m128i a, __m128i b) { - return _mm_add_epi32(a, b); -} - -template <> -inline int16x8_m128i Add(int16x8_m128i a, int16x8_m128i b) { - return int16x8_m128i(_mm_add_epi16(a.v, b.v)); -} - -template <> -inline __m128i Mul(__m128i a, __m128i b) { - return _mm_mullo_epi32(a, b); -} - -template <> -inline int16x8_m128i Mul(int16x8_m128i a, int16x8_m128i b) { - return int16x8_m128i(_mm_mullo_epi16(a.v, b.v)); -} - -template <> -inline __m128i Sub(__m128i a, __m128i b) { - return _mm_sub_epi32(a, b); -} - -template <> -inline int16x8_m128i Sub(int16x8_m128i a, int16x8_m128i b) { - return int16x8_m128i(_mm_sub_epi16(a.v, b.v)); -} - -template <> -inline __m128i Neg(__m128i a) { - return _mm_sign_epi32(a, _mm_set1_epi32(-1)); -} - -template <> -inline int16x8_m128i Neg(int16x8_m128i a) { - return int16x8_m128i(_mm_sign_epi16(a.v, _mm_set1_epi16(-1))); -} - -template <> -inline __m128i ShiftLeft(__m128i a, int offset) { - return _mm_slli_epi32(a, offset); -} - -template <> -inline int16x8_m128i ShiftLeft(int16x8_m128i a, int offset) { - return int16x8_m128i(_mm_slli_epi16(a.v, offset)); -} - -template <> -inline __m128i ShiftRight(__m128i a, int offset) { - return _mm_srai_epi32(a, offset); -} - -template <> -inline int16x8_m128i ShiftRight(int16x8_m128i a, int offset) { - return int16x8_m128i(_mm_srai_epi16(a.v, offset)); -} - -template <> -inline __m128i SelectUsingMask(__m128i if_mask, __m128i then_val, - __m128i else_val) { - // borrowed from Intel's arm_neon_sse.h header. - return _mm_or_si128(_mm_and_si128(if_mask, then_val), - _mm_andnot_si128(if_mask, else_val)); -} - -template <> -inline int16x8_m128i SelectUsingMask(int16x8_m128i if_mask, - int16x8_m128i then_val, - int16x8_m128i else_val) { - // borrowed from Intel's arm_neon_sse.h header. - return int16x8_m128i(SelectUsingMask(if_mask.v, then_val.v, else_val.v)); -} - -template <> -inline __m128i MaskIfEqual(__m128i a, __m128i b) { - return _mm_cmpeq_epi32(a, b); -} - -template <> -inline int16x8_m128i MaskIfEqual(int16x8_m128i a, int16x8_m128i b) { - return int16x8_m128i(_mm_cmpeq_epi16(a.v, b.v)); -} - -template <> -inline __m128i MaskIfNotEqual(__m128i a, __m128i b) { - return BitNot(MaskIfEqual(a, b)); -} - -template <> -inline int16x8_m128i MaskIfNotEqual(int16x8_m128i a, int16x8_m128i b) { - return BitNot(MaskIfEqual(a, b)); -} - -template <> -inline __m128i MaskIfZero(__m128i a) { - return MaskIfEqual(a, _mm_set1_epi32(0)); -} - -template <> -inline int16x8_m128i MaskIfZero(int16x8_m128i a) { - return MaskIfEqual(a, int16x8_m128i(_mm_set1_epi16(0))); -} - -template <> -inline __m128i MaskIfNonZero(__m128i a) { - return MaskIfNotEqual(a, _mm_set1_epi32(0)); -} - -template <> -inline int16x8_m128i MaskIfNonZero(int16x8_m128i a) { - return MaskIfNotEqual(a, int16x8_m128i(_mm_set1_epi16(0))); -} - -template <> -inline __m128i MaskIfGreaterThan(__m128i a, __m128i b) { - return _mm_cmpgt_epi32(a, b); -} - -template <> -inline int16x8_m128i MaskIfGreaterThan(int16x8_m128i a, int16x8_m128i b) { - return int16x8_m128i(_mm_cmpgt_epi16(a.v, b.v)); -} - -template <> -inline __m128i MaskIfLessThan(__m128i a, __m128i b) { - return _mm_cmplt_epi32(a, b); -} - -template <> -inline int16x8_m128i MaskIfLessThan(int16x8_m128i a, int16x8_m128i b) { - return int16x8_m128i(_mm_cmplt_epi16(a.v, b.v)); -} - -template <> -inline __m128i MaskIfGreaterThanOrEqual(__m128i a, __m128i b) { - return BitNot(MaskIfLessThan(a, b)); -} - -template <> -inline int16x8_m128i MaskIfGreaterThanOrEqual(int16x8_m128i a, - int16x8_m128i b) { - return BitNot(MaskIfLessThan(a, b)); -} - -template <> -inline __m128i MaskIfLessThanOrEqual(__m128i a, __m128i b) { - return BitNot(MaskIfGreaterThan(a, b)); -} - -template <> -inline int16x8_m128i MaskIfLessThanOrEqual(int16x8_m128i a, int16x8_m128i b) { - return BitNot(MaskIfGreaterThan(a, b)); -} - -/* Assumptions: - - All and Any are used on masks. - - masks are all_ones for true lanes, all_zeroes otherwise. -Hence, All means all 128bits set, and Any means any bit set. -*/ - -template <> -inline bool All(__m128i a) { - return _mm_testc_si128(a, a); -} - -template <> -inline bool All(int16x8_m128i a) { - return _mm_testc_si128(a.v, a.v); -} - -template <> -inline bool Any(__m128i a) { - return !_mm_testz_si128(a, a); -} - -template <> -inline bool Any(int16x8_m128i a) { - return !_mm_testz_si128(a.v, a.v); -} - -template <> -inline __m128i RoundingHalfSum(__m128i a, __m128i b) { - /* __m128i round_bit_mask, a_over_2, b_over_2, round_bit, sum; */ - /* We divide the inputs before the add to avoid the overflow and costly test - */ - /* of checking if an overflow occured on signed add */ - /* round_bit_mask = _mm_set1_epi32(1); */ - /* a_over_2 = _mm_srai_epi32(a, 1); */ - /* b_over_2 = _mm_srai_epi32(b, 1); */ - /* sum = Add(a_over_2, b_over_2); */ - /* round_bit = _mm_sign_epi32(BitAnd(BitOr(a,b), round_bit_mask), sum); */ - /* return Add(sum, round_bit); */ - - /* Other possibility detecting overflow and xor the sign if an overflow - * happened*/ - __m128i one, sign_bit_mask, sum, rounded_half_sum, overflow, result; - one = _mm_set1_epi32(1); - sign_bit_mask = _mm_set1_epi32(0x80000000); - sum = Add(a, b); - rounded_half_sum = _mm_srai_epi32(Add(sum, one), 1); - overflow = - BitAnd(BitAnd(BitXor(a, rounded_half_sum), BitXor(b, rounded_half_sum)), - sign_bit_mask); - result = BitXor(rounded_half_sum, overflow); - return result; -} - -template <> -inline int16x8_m128i RoundingHalfSum(int16x8_m128i a, int16x8_m128i b) { - // Idea: go to unsigned to use _mm_avg_epu16, - // borrowed from Intel's arm_neon_sse.h header. - __m128i constant_neg_32768 = _mm_set1_epi16(-32768); - __m128i a_unsigned = _mm_sub_epi16(a.v, constant_neg_32768); - __m128i b_unsigned = _mm_sub_epi16(b.v, constant_neg_32768); - __m128i avg_unsigned = _mm_avg_epu16(a_unsigned, b_unsigned); - __m128i avg = _mm_add_epi16(avg_unsigned, constant_neg_32768); - return int16x8_m128i(avg); -} - -template <> -inline __m128i SaturatingRoundingDoublingHighMul(__m128i a, __m128i b) { - __m128i min, saturation_mask, a0_a2, a1_a3, b0_b2, b1_b3; - __m128i a0b0_a2b2, a1b1_a3b3, a0b0_a2b2_rounded, a1b1_a3b3_rounded; - __m128i a0b0_a2b2_rounded_2x, a1b1_a3b3_rounded_2x, result; - __m128i nudge; - - // saturation only happen if a == b == INT_MIN - min = _mm_set1_epi32(std::numeric_limits::min()); - saturation_mask = BitAnd(MaskIfEqual(a, b), MaskIfEqual(a, min)); - - // a = a0 | a1 | a2 | a3 - // b = b0 | b1 | b2 | b3 - a0_a2 = a; - a1_a3 = _mm_srli_si128(a, 4); - b0_b2 = b; - b1_b3 = _mm_srli_si128(b, 4); - - a0b0_a2b2 = _mm_mul_epi32(a0_a2, b0_b2); - a1b1_a3b3 = _mm_mul_epi32(a1_a3, b1_b3); - - // do the rounding and take into account that it will be doubled - nudge = _mm_set1_epi64x(1 << 30); - a0b0_a2b2_rounded = _mm_add_epi64(a0b0_a2b2, nudge); - a1b1_a3b3_rounded = _mm_add_epi64(a1b1_a3b3, nudge); - - // do the doubling - a0b0_a2b2_rounded_2x = _mm_slli_epi64(a0b0_a2b2_rounded, 1); - a1b1_a3b3_rounded_2x = _mm_slli_epi64(a1b1_a3b3_rounded, 1); - - // get the high part of the products - result = _mm_blend_epi16(_mm_srli_si128(a0b0_a2b2_rounded_2x, 4), - a1b1_a3b3_rounded_2x, 0xcc); - - // saturate those which overflowed - return SelectUsingMask(saturation_mask, min, result); -} - -template <> -inline int16x8_m128i SaturatingRoundingDoublingHighMul(int16x8_m128i a, - int16x8_m128i b) { - // Idea: use _mm_mulhrs_epi16 then saturate with a bit-operation, - // borrowed from Intel's arm_neon_sse.h header. - __m128i result_unsaturated = _mm_mulhrs_epi16(a.v, b.v); - __m128i saturation_mask = - _mm_cmpeq_epi16(result_unsaturated, _mm_set1_epi16(0x8000)); - __m128i result = _mm_xor_si128(result_unsaturated, saturation_mask); - return int16x8_m128i(result); -} - -template <> -inline __m128i Dup<__m128i>(std::int32_t x) { - return _mm_set1_epi32(x); -} - -template <> -inline int16x8_m128i Dup(std::int16_t x) { - return int16x8_m128i(_mm_set1_epi16(x)); -} - -// So far this is only needed for int16. -template <> -inline int16x8_m128i SaturatingAdd(int16x8_m128i a, int16x8_m128i b) { - return int16x8_m128i(_mm_adds_epi16(a.v, b.v)); -} - -} // end namespace gemmlowp - -#endif // GEMMLOWP_INTERNAL_FIXEDPOINT_SSE_H_ diff --git a/code/components/tflite-lib/third_party/gemmlowp/internal/detect_platform.h b/code/components/tflite-lib/third_party/gemmlowp/internal/detect_platform.h deleted file mode 100644 index 6f06d19f..00000000 --- a/code/components/tflite-lib/third_party/gemmlowp/internal/detect_platform.h +++ /dev/null @@ -1,166 +0,0 @@ -// Copyright 2018 The Gemmlowp Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// detect_platform.h: Sets up macros that control architecture-specific -// features of gemmlowp's implementation. - -#ifndef GEMMLOWP_INTERNAL_DETECT_PLATFORM_H_ -#define GEMMLOWP_INTERNAL_DETECT_PLATFORM_H_ - -// Our inline assembly path assume GCC/Clang syntax. -// Native Client doesn't seem to support inline assembly(?). -#if defined(__GNUC__) && !defined(__native_client__) -#define GEMMLOWP_ALLOW_INLINE_ASM -#endif - -// Define macro statement that avoids inlining for GCC. -// For non-GCC, define as empty macro. -#if defined(__GNUC__) -#define GEMMLOWP_NOINLINE __attribute__((noinline)) -#else -#define GEMMLOWP_NOINLINE -#endif - -// Detect ARM, 32-bit or 64-bit -#ifdef __arm__ -#define GEMMLOWP_ARM_32 -#endif - -#ifdef __aarch64__ -#define GEMMLOWP_ARM_64 -#endif - -#if defined(GEMMLOWP_ARM_32) || defined(GEMMLOWP_ARM_64) -#define GEMMLOWP_ARM -#endif - -// Detect MIPS, 32-bit or 64-bit -#if defined(__mips) && !defined(__LP64__) -#define GEMMLOWP_MIPS_32 -#endif - -#if defined(__mips) && defined(__LP64__) -#define GEMMLOWP_MIPS_64 -#endif - -#if defined(GEMMLOWP_MIPS_32) || defined(GEMMLOWP_MIPS_64) -#define GEMMLOWP_MIPS -#endif - -// Detect x86, 32-bit or 64-bit -#if defined(__i386__) || defined(_M_IX86) || defined(_X86_) || defined(__i386) -#define GEMMLOWP_X86_32 -#endif - -#if defined(__x86_64__) || defined(_M_X64) || defined(__amd64) -#define GEMMLOWP_X86_64 -#endif - -#if defined(GEMMLOWP_X86_32) || defined(GEMMLOWP_X86_64) -#define GEMMLOWP_X86 -#endif - -// Some of our optimized paths use inline assembly and for -// now we don't bother enabling some other optimized paths using intrinddics -// where we can't use inline assembly paths. -#ifdef GEMMLOWP_ALLOW_INLINE_ASM - -// Detect NEON. It's important to check for both tokens. -#if (defined __ARM_NEON) || (defined __ARM_NEON__) -#define GEMMLOWP_NEON -#endif - -// Convenience NEON tokens for 32-bit or 64-bit -#if defined(GEMMLOWP_NEON) && defined(GEMMLOWP_ARM_32) -#define GEMMLOWP_NEON_32 -#endif - -#if defined(GEMMLOWP_NEON) && defined(GEMMLOWP_ARM_64) -#define GEMMLOWP_NEON_64 -#endif - -// Detect MIPS MSA. -// Limit MSA optimizations to little-endian CPUs for now. -// TODO: Perhaps, eventually support MSA optimizations on big-endian CPUs? -#if defined(GEMMLOWP_MIPS) && (__mips_isa_rev >= 5) && defined(__mips_msa) && \ - defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) -#define GEMMLOWP_MSA -#endif - -// Convenience MIPS MSA tokens for 32-bit or 64-bit. -#if defined(GEMMLOWP_MSA) && defined(GEMMLOWP_MIPS_32) -#define GEMMLOWP_MSA_32 -#endif - -#if defined(GEMMLOWP_MSA) && defined(GEMMLOWP_MIPS_64) -#define GEMMLOWP_MSA_64 -#endif - -// compiler define for AVX2 -D GEMMLOWP_ENABLE_AVX2 -// Detect AVX2 -#if defined(__AVX2__) && defined(GEMMLOWP_ENABLE_AVX2) -#define GEMMLOWP_AVX2 -// Detect SSE4. -// MSVC does not have __SSE4_1__ macro, but will enable SSE4 -// when AVX is turned on. -#elif defined(__SSE4_1__) || (defined(_MSC_VER) && defined(__AVX__)) -#define GEMMLOWP_SSE4 -// Detect SSE3. -#elif defined(__SSE3__) -#define GEMMLOWP_SSE3 -#endif - -// Convenience SSE4 tokens for 32-bit or 64-bit -#if defined(GEMMLOWP_SSE4) && defined(GEMMLOWP_X86_32) && \ - !defined(GEMMLOWP_DISABLE_SSE4) -#define GEMMLOWP_SSE4_32 -#endif - -#if defined(GEMMLOWP_SSE3) && defined(GEMMLOWP_X86_32) -#define GEMMLOWP_SSE3_32 -#endif - -#if defined(GEMMLOWP_SSE4) && defined(GEMMLOWP_X86_64) && \ - !defined(GEMMLOWP_DISABLE_SSE4) -#define GEMMLOWP_SSE4_64 -#endif - -#if defined(GEMMLOWP_SSE3) && defined(GEMMLOWP_X86_64) -#define GEMMLOWP_SSE3_64 -#endif - -#if defined(GEMMLOWP_AVX2) && defined(GEMMLOWP_X86_64) -#define GEMMLOWP_AVX2_64 -#endif - -#if defined(__has_feature) -#if __has_feature(memory_sanitizer) -#include -#define GEMMLOWP_MARK_MEMORY_AS_INITIALIZED __msan_unpoison -#elif __has_feature(address_sanitizer) -#include -#define GEMMLOWP_MARK_MEMORY_AS_INITIALIZED __asan_unpoison_memory_region -#endif -#endif - -#endif // GEMMLOWP_ALLOW_INLINE_ASM - -// Detect Android. Don't conflate with ARM - we care about tuning -// for non-ARM Android devices too. This can be used in conjunction -// with x86 to tune differently for mobile x86 CPUs (Atom) vs. desktop x86 CPUs. -#if defined(__ANDROID__) || defined(ANDROID) -#define GEMMLOWP_ANDROID -#endif - -#endif // GEMMLOWP_INTERNAL_DETECT_PLATFORM_H_ diff --git a/code/components/tflite-lib/third_party/kissfft/COPYING b/code/components/tflite-lib/third_party/kissfft/COPYING deleted file mode 100644 index 2fc6685a..00000000 --- a/code/components/tflite-lib/third_party/kissfft/COPYING +++ /dev/null @@ -1,11 +0,0 @@ -Copyright (c) 2003-2010 Mark Borgerding - -All rights reserved. - -Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/code/components/tflite-lib/third_party/kissfft/_kiss_fft_guts.h b/code/components/tflite-lib/third_party/kissfft/_kiss_fft_guts.h deleted file mode 100644 index 1a0f4c26..00000000 --- a/code/components/tflite-lib/third_party/kissfft/_kiss_fft_guts.h +++ /dev/null @@ -1,168 +0,0 @@ -#ifndef _KISS_FFT_GUTS_H -#define _KISS_FFT_GUTS_H - -/* -Copyright (c) 2003-2010, Mark Borgerding - -All rights reserved. - -Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -/* kiss_fft.h - defines kiss_fft_scalar as either short or a float type - and defines - typedef struct { kiss_fft_scalar r; kiss_fft_scalar i; }kiss_fft_cpx; */ -#include "kiss_fft.h" -#include - -#define MAXFACTORS 32 -/* e.g. an fft of length 128 has 4 factors - as far as kissfft is concerned - 4*4*4*2 - */ - -struct kiss_fft_state{ - int nfft; - int inverse; - int factors[2*MAXFACTORS]; - kiss_fft_cpx twiddles[1]; -}; - -/* - Explanation of macros dealing with complex math: - - C_MUL(m,a,b) : m = a*b - C_FIXDIV( c , div ) : if a fixed point impl., c /= div. noop otherwise - C_SUB( res, a,b) : res = a - b - C_SUBFROM( res , a) : res -= a - C_ADDTO( res , a) : res += a - * */ -#ifdef FIXED_POINT -#if (FIXED_POINT==32) -# define FRACBITS 31 -# define SAMPPROD int64_t -#define SAMP_MAX 2147483647 -#else -# define FRACBITS 15 -# define SAMPPROD int32_t -#define SAMP_MAX 32767 -#endif - -#define SAMP_MIN -SAMP_MAX - -#if defined(CHECK_OVERFLOW) -# define CHECK_OVERFLOW_OP(a,op,b) \ - if ( (SAMPPROD)(a) op (SAMPPROD)(b) > SAMP_MAX || (SAMPPROD)(a) op (SAMPPROD)(b) < SAMP_MIN ) { \ - fprintf(stderr,"WARNING:overflow @ " __FILE__ "(%d): (%d " #op" %d) = %ld\n",__LINE__,(a),(b),(SAMPPROD)(a) op (SAMPPROD)(b) ); } -#endif - - -# define smul(a,b) ( (SAMPPROD)(a)*(b) ) -# define sround( x ) (kiss_fft_scalar)( ( (x) + (1<<(FRACBITS-1)) ) >> FRACBITS ) - -# define S_MUL(a,b) sround( smul(a,b) ) - -# define C_MUL(m,a,b) \ - do{ (m).r = sround( smul((a).r,(b).r) - smul((a).i,(b).i) ); \ - (m).i = sround( smul((a).r,(b).i) + smul((a).i,(b).r) ); }while(0) - -# define DIVSCALAR(x,k) \ - (x) = sround( smul( x, SAMP_MAX/k ) ) - -# define C_FIXDIV(c,div) \ - do { DIVSCALAR( (c).r , div); \ - DIVSCALAR( (c).i , div); }while (0) - -# define C_MULBYSCALAR( c, s ) \ - do{ (c).r = sround( smul( (c).r , s ) ) ;\ - (c).i = sround( smul( (c).i , s ) ) ; }while(0) - -#else /* not FIXED_POINT*/ - -# define S_MUL(a,b) ( (a)*(b) ) -#define C_MUL(m,a,b) \ - do{ (m).r = (a).r*(b).r - (a).i*(b).i;\ - (m).i = (a).r*(b).i + (a).i*(b).r; }while(0) -# define C_FIXDIV(c,div) /* NOOP */ -# define C_MULBYSCALAR( c, s ) \ - do{ (c).r *= (s);\ - (c).i *= (s); }while(0) -#endif - -#ifndef CHECK_OVERFLOW_OP -# define CHECK_OVERFLOW_OP(a,op,b) /* noop */ -#endif - -#define C_ADD( res, a,b)\ - do { \ - CHECK_OVERFLOW_OP((a).r,+,(b).r)\ - CHECK_OVERFLOW_OP((a).i,+,(b).i)\ - (res).r=(a).r+(b).r; (res).i=(a).i+(b).i; \ - }while(0) -#define C_SUB( res, a,b)\ - do { \ - CHECK_OVERFLOW_OP((a).r,-,(b).r)\ - CHECK_OVERFLOW_OP((a).i,-,(b).i)\ - (res).r=(a).r-(b).r; (res).i=(a).i-(b).i; \ - }while(0) -#define C_ADDTO( res , a)\ - do { \ - CHECK_OVERFLOW_OP((res).r,+,(a).r)\ - CHECK_OVERFLOW_OP((res).i,+,(a).i)\ - (res).r += (a).r; (res).i += (a).i;\ - }while(0) - -#define C_SUBFROM( res , a)\ - do {\ - CHECK_OVERFLOW_OP((res).r,-,(a).r)\ - CHECK_OVERFLOW_OP((res).i,-,(a).i)\ - (res).r -= (a).r; (res).i -= (a).i; \ - }while(0) - - -#ifdef FIXED_POINT -# define KISS_FFT_COS(phase) floor(.5+SAMP_MAX * cos (phase)) -# define KISS_FFT_SIN(phase) floor(.5+SAMP_MAX * sin (phase)) -# define HALF_OF(x) ((x)>>1) -#elif defined(USE_SIMD) -# define KISS_FFT_COS(phase) _mm_set1_ps( cos(phase) ) -# define KISS_FFT_SIN(phase) _mm_set1_ps( sin(phase) ) -# define HALF_OF(x) ((x)*_mm_set1_ps(.5)) -#else -# define KISS_FFT_COS(phase) (kiss_fft_scalar) cos(phase) -# define KISS_FFT_SIN(phase) (kiss_fft_scalar) sin(phase) -# define HALF_OF(x) ((x)*(kiss_fft_scalar).5) -#endif - -#define kf_cexp(x,phase) \ - do{ \ - (x)->r = KISS_FFT_COS(phase);\ - (x)->i = KISS_FFT_SIN(phase);\ - }while(0) - - -/* a debugging function */ -#define pcpx(c)\ - fprintf(stderr,"%g + %gi\n",(double)((c)->r),(double)((c)->i) ) - - -#ifdef KISS_FFT_USE_ALLOCA -// define this to allow use of alloca instead of malloc for temporary buffers -// Temporary buffers are used in two case: -// 1. FFT sizes that have "bad" factors. i.e. not 2,3 and 5 -// 2. "in-place" FFTs. Notice the quotes, since kissfft does not really do an in-place transform. -#include -#define KISS_FFT_TMP_ALLOC(nbytes) alloca(nbytes) -#define KISS_FFT_TMP_FREE(ptr) -#else -#define KISS_FFT_TMP_ALLOC(nbytes) KISS_FFT_MALLOC(nbytes) -#define KISS_FFT_TMP_FREE(ptr) KISS_FFT_FREE(ptr) -#endif -#endif // _KISS_FFT_GUTS_H diff --git a/code/components/tflite-lib/third_party/kissfft/kiss_fft.c b/code/components/tflite-lib/third_party/kissfft/kiss_fft.c deleted file mode 100644 index 9133a013..00000000 --- a/code/components/tflite-lib/third_party/kissfft/kiss_fft.c +++ /dev/null @@ -1,408 +0,0 @@ -/* -Copyright (c) 2003-2010, Mark Borgerding - -All rights reserved. - -Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - - -#include "_kiss_fft_guts.h" -/* The guts header contains all the multiplication and addition macros that are defined for - fixed or floating point complex numbers. It also delares the kf_ internal functions. - */ - -static void kf_bfly2( - kiss_fft_cpx * Fout, - const size_t fstride, - const kiss_fft_cfg st, - int m - ) -{ - kiss_fft_cpx * Fout2; - kiss_fft_cpx * tw1 = st->twiddles; - kiss_fft_cpx t; - Fout2 = Fout + m; - do{ - C_FIXDIV(*Fout,2); C_FIXDIV(*Fout2,2); - - C_MUL (t, *Fout2 , *tw1); - tw1 += fstride; - C_SUB( *Fout2 , *Fout , t ); - C_ADDTO( *Fout , t ); - ++Fout2; - ++Fout; - }while (--m); -} - -static void kf_bfly4( - kiss_fft_cpx * Fout, - const size_t fstride, - const kiss_fft_cfg st, - const size_t m - ) -{ - kiss_fft_cpx *tw1,*tw2,*tw3; - kiss_fft_cpx scratch[6]; - size_t k=m; - const size_t m2=2*m; - const size_t m3=3*m; - - - tw3 = tw2 = tw1 = st->twiddles; - - do { - C_FIXDIV(*Fout,4); C_FIXDIV(Fout[m],4); C_FIXDIV(Fout[m2],4); C_FIXDIV(Fout[m3],4); - - C_MUL(scratch[0],Fout[m] , *tw1 ); - C_MUL(scratch[1],Fout[m2] , *tw2 ); - C_MUL(scratch[2],Fout[m3] , *tw3 ); - - C_SUB( scratch[5] , *Fout, scratch[1] ); - C_ADDTO(*Fout, scratch[1]); - C_ADD( scratch[3] , scratch[0] , scratch[2] ); - C_SUB( scratch[4] , scratch[0] , scratch[2] ); - C_SUB( Fout[m2], *Fout, scratch[3] ); - tw1 += fstride; - tw2 += fstride*2; - tw3 += fstride*3; - C_ADDTO( *Fout , scratch[3] ); - - if(st->inverse) { - Fout[m].r = scratch[5].r - scratch[4].i; - Fout[m].i = scratch[5].i + scratch[4].r; - Fout[m3].r = scratch[5].r + scratch[4].i; - Fout[m3].i = scratch[5].i - scratch[4].r; - }else{ - Fout[m].r = scratch[5].r + scratch[4].i; - Fout[m].i = scratch[5].i - scratch[4].r; - Fout[m3].r = scratch[5].r - scratch[4].i; - Fout[m3].i = scratch[5].i + scratch[4].r; - } - ++Fout; - }while(--k); -} - -static void kf_bfly3( - kiss_fft_cpx * Fout, - const size_t fstride, - const kiss_fft_cfg st, - size_t m - ) -{ - size_t k=m; - const size_t m2 = 2*m; - kiss_fft_cpx *tw1,*tw2; - kiss_fft_cpx scratch[5]; - kiss_fft_cpx epi3; - epi3 = st->twiddles[fstride*m]; - - tw1=tw2=st->twiddles; - - do{ - C_FIXDIV(*Fout,3); C_FIXDIV(Fout[m],3); C_FIXDIV(Fout[m2],3); - - C_MUL(scratch[1],Fout[m] , *tw1); - C_MUL(scratch[2],Fout[m2] , *tw2); - - C_ADD(scratch[3],scratch[1],scratch[2]); - C_SUB(scratch[0],scratch[1],scratch[2]); - tw1 += fstride; - tw2 += fstride*2; - - Fout[m].r = Fout->r - HALF_OF(scratch[3].r); - Fout[m].i = Fout->i - HALF_OF(scratch[3].i); - - C_MULBYSCALAR( scratch[0] , epi3.i ); - - C_ADDTO(*Fout,scratch[3]); - - Fout[m2].r = Fout[m].r + scratch[0].i; - Fout[m2].i = Fout[m].i - scratch[0].r; - - Fout[m].r -= scratch[0].i; - Fout[m].i += scratch[0].r; - - ++Fout; - }while(--k); -} - -static void kf_bfly5( - kiss_fft_cpx * Fout, - const size_t fstride, - const kiss_fft_cfg st, - int m - ) -{ - kiss_fft_cpx *Fout0,*Fout1,*Fout2,*Fout3,*Fout4; - int u; - kiss_fft_cpx scratch[13]; - kiss_fft_cpx * twiddles = st->twiddles; - kiss_fft_cpx *tw; - kiss_fft_cpx ya,yb; - ya = twiddles[fstride*m]; - yb = twiddles[fstride*2*m]; - - Fout0=Fout; - Fout1=Fout0+m; - Fout2=Fout0+2*m; - Fout3=Fout0+3*m; - Fout4=Fout0+4*m; - - tw=st->twiddles; - for ( u=0; ur += scratch[7].r + scratch[8].r; - Fout0->i += scratch[7].i + scratch[8].i; - - scratch[5].r = scratch[0].r + S_MUL(scratch[7].r,ya.r) + S_MUL(scratch[8].r,yb.r); - scratch[5].i = scratch[0].i + S_MUL(scratch[7].i,ya.r) + S_MUL(scratch[8].i,yb.r); - - scratch[6].r = S_MUL(scratch[10].i,ya.i) + S_MUL(scratch[9].i,yb.i); - scratch[6].i = -S_MUL(scratch[10].r,ya.i) - S_MUL(scratch[9].r,yb.i); - - C_SUB(*Fout1,scratch[5],scratch[6]); - C_ADD(*Fout4,scratch[5],scratch[6]); - - scratch[11].r = scratch[0].r + S_MUL(scratch[7].r,yb.r) + S_MUL(scratch[8].r,ya.r); - scratch[11].i = scratch[0].i + S_MUL(scratch[7].i,yb.r) + S_MUL(scratch[8].i,ya.r); - scratch[12].r = - S_MUL(scratch[10].i,yb.i) + S_MUL(scratch[9].i,ya.i); - scratch[12].i = S_MUL(scratch[10].r,yb.i) - S_MUL(scratch[9].r,ya.i); - - C_ADD(*Fout2,scratch[11],scratch[12]); - C_SUB(*Fout3,scratch[11],scratch[12]); - - ++Fout0;++Fout1;++Fout2;++Fout3;++Fout4; - } -} - -/* perform the butterfly for one stage of a mixed radix FFT */ -static void kf_bfly_generic( - kiss_fft_cpx * Fout, - const size_t fstride, - const kiss_fft_cfg st, - int m, - int p - ) -{ - int u,k,q1,q; - kiss_fft_cpx * twiddles = st->twiddles; - kiss_fft_cpx t; - int Norig = st->nfft; - - kiss_fft_cpx * scratch = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC(sizeof(kiss_fft_cpx)*p); - - for ( u=0; u=Norig) twidx-=Norig; - C_MUL(t,scratch[q] , twiddles[twidx] ); - C_ADDTO( Fout[ k ] ,t); - } - k += m; - } - } - KISS_FFT_TMP_FREE(scratch); -} - -static -void kf_work( - kiss_fft_cpx * Fout, - const kiss_fft_cpx * f, - const size_t fstride, - int in_stride, - int * factors, - const kiss_fft_cfg st - ) -{ - kiss_fft_cpx * Fout_beg=Fout; - const int p=*factors++; /* the radix */ - const int m=*factors++; /* stage's fft length/p */ - const kiss_fft_cpx * Fout_end = Fout + p*m; - -#ifdef _OPENMP - // use openmp extensions at the - // top-level (not recursive) - if (fstride==1 && p<=5) - { - int k; - - // execute the p different work units in different threads -# pragma omp parallel for - for (k=0;k floor_sqrt) - p = n; /* no more factors, skip to end */ - } - n /= p; - *facbuf++ = p; - *facbuf++ = n; - } while (n > 1); -} - -/* - * - * User-callable function to allocate all necessary storage space for the fft. - * - * The return value is a contiguous block of memory, allocated with malloc. As such, - * It can be freed with free(), rather than a kiss_fft-specific function. - * */ -kiss_fft_cfg kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem ) -{ - kiss_fft_cfg st=NULL; - size_t memneeded = sizeof(struct kiss_fft_state) - + sizeof(kiss_fft_cpx)*(nfft-1); /* twiddle factors*/ - - if ( lenmem==NULL ) { - st = ( kiss_fft_cfg)KISS_FFT_MALLOC( memneeded ); - }else{ - if (mem != NULL && *lenmem >= memneeded) - st = (kiss_fft_cfg)mem; - *lenmem = memneeded; - } - if (st) { - int i; - st->nfft=nfft; - st->inverse = inverse_fft; - - for (i=0;iinverse) - phase *= -1; - kf_cexp(st->twiddles+i, phase ); - } - - kf_factor(nfft,st->factors); - } - return st; -} - - -void kiss_fft_stride(kiss_fft_cfg st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout,int in_stride) -{ - if (fin == fout) { - //NOTE: this is not really an in-place FFT algorithm. - //It just performs an out-of-place FFT into a temp buffer - kiss_fft_cpx * tmpbuf = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC( sizeof(kiss_fft_cpx)*st->nfft); - kf_work(tmpbuf,fin,1,in_stride, st->factors,st); - /* memcpy(fout,tmpbuf,sizeof(kiss_fft_cpx)*st->nfft); */ - KISS_FFT_TMP_FREE(tmpbuf); - }else{ - kf_work( fout, fin, 1,in_stride, st->factors,st ); - } -} - -void kiss_fft(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout) -{ - kiss_fft_stride(cfg,fin,fout,1); -} - - -void kiss_fft_cleanup(void) -{ - // nothing needed any more -} - -int kiss_fft_next_fast_size(int n) -{ - while(1) { - int m=n; - while ( (m%2) == 0 ) m/=2; - while ( (m%3) == 0 ) m/=3; - while ( (m%5) == 0 ) m/=5; - if (m<=1) - break; /* n is completely factorable by twos, threes, and fives */ - n++; - } - return n; -} diff --git a/code/components/tflite-lib/third_party/kissfft/kiss_fft.h b/code/components/tflite-lib/third_party/kissfft/kiss_fft.h deleted file mode 100644 index 24e4d0c0..00000000 --- a/code/components/tflite-lib/third_party/kissfft/kiss_fft.h +++ /dev/null @@ -1,124 +0,0 @@ -#ifndef KISS_FFT_H -#define KISS_FFT_H - -#include -#include -#include -#include - -#ifdef __cplusplus -extern "C++" { -#endif - -/* - ATTENTION! - If you would like a : - -- a utility that will handle the caching of fft objects - -- real-only (no imaginary time component ) FFT - -- a multi-dimensional FFT - -- a command-line utility to perform ffts - -- a command-line utility to perform fast-convolution filtering - - Then see kfc.h kiss_fftr.h kiss_fftnd.h fftutil.c kiss_fastfir.c - in the tools/ directory. -*/ - -#ifdef USE_SIMD -# include -# define kiss_fft_scalar __m128 -#define KISS_FFT_MALLOC(nbytes) _mm_malloc(nbytes,16) -#define KISS_FFT_FREE _mm_free -#else -#define KISS_FFT_MALLOC(X) (void*)(0x0) /* Patched. */ -#define KISS_FFT_FREE(X) /* Patched. */ -#endif - - -#ifdef FIXED_POINT -#include /* Patched. */ -# if (FIXED_POINT == 32) -# define kiss_fft_scalar int32_t -# else -# define kiss_fft_scalar int16_t -# endif -#else -# ifndef kiss_fft_scalar -/* default is float */ -# define kiss_fft_scalar float -# endif -#endif - -typedef struct { - kiss_fft_scalar r; - kiss_fft_scalar i; -}kiss_fft_cpx; - -typedef struct kiss_fft_state* kiss_fft_cfg; - -/* - * kiss_fft_alloc - * - * Initialize a FFT (or IFFT) algorithm's cfg/state buffer. - * - * typical usage: kiss_fft_cfg mycfg=kiss_fft_alloc(1024,0,NULL,NULL); - * - * The return value from fft_alloc is a cfg buffer used internally - * by the fft routine or NULL. - * - * If lenmem is NULL, then kiss_fft_alloc will allocate a cfg buffer using malloc. - * The returned value should be free()d when done to avoid memory leaks. - * - * The state can be placed in a user supplied buffer 'mem': - * If lenmem is not NULL and mem is not NULL and *lenmem is large enough, - * then the function places the cfg in mem and the size used in *lenmem - * and returns mem. - * - * If lenmem is not NULL and ( mem is NULL or *lenmem is not large enough), - * then the function returns NULL and places the minimum cfg - * buffer size in *lenmem. - * */ - -kiss_fft_cfg kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem); - -/* - * kiss_fft(cfg,in_out_buf) - * - * Perform an FFT on a complex input buffer. - * for a forward FFT, - * fin should be f[0] , f[1] , ... ,f[nfft-1] - * fout will be F[0] , F[1] , ... ,F[nfft-1] - * Note that each element is complex and can be accessed like - f[k].r and f[k].i - * */ -void kiss_fft(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout); - -/* - A more generic version of the above function. It reads its input from every Nth sample. - * */ -void kiss_fft_stride(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout,int fin_stride); - -/* If kiss_fft_alloc allocated a buffer, it is one contiguous - buffer and can be simply free()d when no longer needed*/ -#define kiss_fft_free free - -/* - Cleans up some memory that gets managed internally. Not necessary to call, but it might clean up - your compiler output to call this before you exit. -*/ -void kiss_fft_cleanup(void); - - -/* - * Returns the smallest integer k, such that k>=n and k has only "fast" factors (2,3,5) - */ -int kiss_fft_next_fast_size(int n); - -/* for real ffts, we need an even size */ -#define kiss_fftr_next_fast_size_real(n) \ - (kiss_fft_next_fast_size( ((n)+1)>>1)<<1) - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/code/components/tflite-lib/third_party/kissfft/tools/kiss_fftr.c b/code/components/tflite-lib/third_party/kissfft/tools/kiss_fftr.c deleted file mode 100644 index 0d22a04d..00000000 --- a/code/components/tflite-lib/third_party/kissfft/tools/kiss_fftr.c +++ /dev/null @@ -1,159 +0,0 @@ -/* -Copyright (c) 2003-2004, Mark Borgerding - -All rights reserved. - -Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#include "kiss_fftr.h" -#include "_kiss_fft_guts.h" - -struct kiss_fftr_state{ - kiss_fft_cfg substate; - kiss_fft_cpx * tmpbuf; - kiss_fft_cpx * super_twiddles; -#ifdef USE_SIMD - void * pad; -#endif -}; - -kiss_fftr_cfg kiss_fftr_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem) -{ - int i; - kiss_fftr_cfg st = NULL; - size_t subsize, memneeded; - - if (nfft & 1) { - /* fprintf(stderr,"Real FFT optimization must be even.\n"); */ - return NULL; - } - nfft >>= 1; - - kiss_fft_alloc (nfft, inverse_fft, NULL, &subsize); - memneeded = sizeof(struct kiss_fftr_state) + subsize + sizeof(kiss_fft_cpx) * ( nfft * 3 / 2); - - if (lenmem == NULL) { - st = (kiss_fftr_cfg) KISS_FFT_MALLOC (memneeded); - } else { - if (*lenmem >= memneeded) - st = (kiss_fftr_cfg) mem; - *lenmem = memneeded; - } - if (!st) - return NULL; - - st->substate = (kiss_fft_cfg) (st + 1); /*just beyond kiss_fftr_state struct */ - st->tmpbuf = (kiss_fft_cpx *) (((char *) st->substate) + subsize); - st->super_twiddles = st->tmpbuf + nfft; - kiss_fft_alloc(nfft, inverse_fft, st->substate, &subsize); - - for (i = 0; i < nfft/2; ++i) { - double phase = - -3.14159265358979323846264338327 * ((double) (i+1) / nfft + .5); - if (inverse_fft) - phase *= -1; - kf_cexp (st->super_twiddles+i,phase); - } - return st; -} - -void kiss_fftr(kiss_fftr_cfg st,const kiss_fft_scalar *timedata,kiss_fft_cpx *freqdata) -{ - /* input buffer timedata is stored row-wise */ - int k,ncfft; - kiss_fft_cpx fpnk,fpk,f1k,f2k,tw,tdc; - - if ( st->substate->inverse) { - /* fprintf(stderr,"kiss fft usage error: improper alloc\n"); */ - return; /* exit(1); */ - } - - ncfft = st->substate->nfft; - - /*perform the parallel fft of two real signals packed in real,imag*/ - kiss_fft( st->substate , (const kiss_fft_cpx*)timedata, st->tmpbuf ); - /* The real part of the DC element of the frequency spectrum in st->tmpbuf - * contains the sum of the even-numbered elements of the input time sequence - * The imag part is the sum of the odd-numbered elements - * - * The sum of tdc.r and tdc.i is the sum of the input time sequence. - * yielding DC of input time sequence - * The difference of tdc.r - tdc.i is the sum of the input (dot product) [1,-1,1,-1... - * yielding Nyquist bin of input time sequence - */ - - tdc.r = st->tmpbuf[0].r; - tdc.i = st->tmpbuf[0].i; - C_FIXDIV(tdc,2); - CHECK_OVERFLOW_OP(tdc.r ,+, tdc.i); - CHECK_OVERFLOW_OP(tdc.r ,-, tdc.i); - freqdata[0].r = tdc.r + tdc.i; - freqdata[ncfft].r = tdc.r - tdc.i; -#ifdef USE_SIMD - freqdata[ncfft].i = freqdata[0].i = _mm_set1_ps(0); -#else - freqdata[ncfft].i = freqdata[0].i = 0; -#endif - - for ( k=1;k <= ncfft/2 ; ++k ) { - fpk = st->tmpbuf[k]; - fpnk.r = st->tmpbuf[ncfft-k].r; - fpnk.i = - st->tmpbuf[ncfft-k].i; - C_FIXDIV(fpk,2); - C_FIXDIV(fpnk,2); - - C_ADD( f1k, fpk , fpnk ); - C_SUB( f2k, fpk , fpnk ); - C_MUL( tw , f2k , st->super_twiddles[k-1]); - - freqdata[k].r = HALF_OF(f1k.r + tw.r); - freqdata[k].i = HALF_OF(f1k.i + tw.i); - freqdata[ncfft-k].r = HALF_OF(f1k.r - tw.r); - freqdata[ncfft-k].i = HALF_OF(tw.i - f1k.i); - } -} - -void kiss_fftri(kiss_fftr_cfg st,const kiss_fft_cpx *freqdata,kiss_fft_scalar *timedata) -{ - /* input buffer timedata is stored row-wise */ - int k, ncfft; - - if (st->substate->inverse == 0) { - /* fprintf (stderr, "kiss fft usage error: improper alloc\n"); */ - return; /* exit (1); */ - } - - ncfft = st->substate->nfft; - - st->tmpbuf[0].r = freqdata[0].r + freqdata[ncfft].r; - st->tmpbuf[0].i = freqdata[0].r - freqdata[ncfft].r; - C_FIXDIV(st->tmpbuf[0],2); - - for (k = 1; k <= ncfft / 2; ++k) { - kiss_fft_cpx fk, fnkc, fek, fok, tmp; - fk = freqdata[k]; - fnkc.r = freqdata[ncfft - k].r; - fnkc.i = -freqdata[ncfft - k].i; - C_FIXDIV( fk , 2 ); - C_FIXDIV( fnkc , 2 ); - - C_ADD (fek, fk, fnkc); - C_SUB (tmp, fk, fnkc); - C_MUL (fok, tmp, st->super_twiddles[k-1]); - C_ADD (st->tmpbuf[k], fek, fok); - C_SUB (st->tmpbuf[ncfft - k], fek, fok); -#ifdef USE_SIMD - st->tmpbuf[ncfft - k].i *= _mm_set1_ps(-1.0); -#else - st->tmpbuf[ncfft - k].i *= -1; -#endif - } - kiss_fft (st->substate, st->tmpbuf, (kiss_fft_cpx *) timedata); -} diff --git a/code/components/tflite-lib/third_party/kissfft/tools/kiss_fftr.h b/code/components/tflite-lib/third_party/kissfft/tools/kiss_fftr.h deleted file mode 100644 index b888a28b..00000000 --- a/code/components/tflite-lib/third_party/kissfft/tools/kiss_fftr.h +++ /dev/null @@ -1,46 +0,0 @@ -#ifndef KISS_FTR_H -#define KISS_FTR_H - -#include "kiss_fft.h" -#ifdef __cplusplus -extern "C++" { -#endif - - -/* - - Real optimized version can save about 45% cpu time vs. complex fft of a real seq. - - - - */ - -typedef struct kiss_fftr_state *kiss_fftr_cfg; - - -kiss_fftr_cfg kiss_fftr_alloc(int nfft,int inverse_fft,void * mem, size_t * lenmem); -/* - nfft must be even - - If you don't care to allocate space, use mem = lenmem = NULL -*/ - - -void kiss_fftr(kiss_fftr_cfg cfg,const kiss_fft_scalar *timedata,kiss_fft_cpx *freqdata); -/* - input timedata has nfft scalar points - output freqdata has nfft/2+1 complex points -*/ - -void kiss_fftri(kiss_fftr_cfg cfg,const kiss_fft_cpx *freqdata,kiss_fft_scalar *timedata); -/* - input freqdata has nfft/2+1 complex points - output timedata has nfft scalar points -*/ - -#define kiss_fftr_free free - -#ifdef __cplusplus -} -#endif -#endif diff --git a/code/components/tflite-lib/third_party/ruy/ruy/profiler/instrumentation.h b/code/components/tflite-lib/third_party/ruy/ruy/profiler/instrumentation.h deleted file mode 100644 index c4df1e68..00000000 --- a/code/components/tflite-lib/third_party/ruy/ruy/profiler/instrumentation.h +++ /dev/null @@ -1,203 +0,0 @@ -/* Copyright 2020 Google LLC. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef RUY_RUY_PROFILER_INSTRUMENTATION_H_ -#define RUY_RUY_PROFILER_INSTRUMENTATION_H_ - -#ifdef RUY_PROFILER -#include -#include -#include -#endif - -namespace ruy { -namespace profiler { - -#ifdef RUY_PROFILER - -// A label is how a code scope is annotated to appear in profiles. -// The stacks that are sampled by the profiler are stacks of such labels. -// A label consists of a literal string, plus optional integer arguments. -class Label { - public: - Label() {} - template - explicit Label(Args... args) { - Set(args...); - } - void Set(const char* format) { - format_ = format; - args_count_ = 0; - } - template - void Set(const char* format, Args... args) { - format_ = format; - args_count_ = sizeof...(args); - SetArgs(0, args...); - } - - void operator=(const Label& other); - - bool operator==(const Label& other) const; - - std::string Formatted() const; - const char* format() const { return format_; } - - private: - void SetArgs(int position, int arg0) { args_[position] = arg0; } - - template - void SetArgs(int position, int arg0, Args... args) { - SetArgs(position, arg0); - SetArgs(position + 1, args...); - } - - static constexpr int kMaxArgs = 4; - const char* format_ = nullptr; - int args_count_ = 0; - int args_[kMaxArgs]; -}; - -namespace detail { - -// Forward-declaration, see class ThreadStack below. -class ThreadStack; - -bool& GlobalIsProfilerRunning(); - -// Returns the global vector of pointers to all stacks, there being one stack -// per thread executing instrumented code. -std::vector* GlobalAllThreadStacks(); - -// Returns the mutex to be locked around any access to GlobalAllThreadStacks(). -std::mutex* GlobalsMutex(); - -// Returns the thread-local stack, specific to the current thread. -ThreadStack* ThreadLocalThreadStack(); - -// This 'stack' is what may be more appropriately called a 'pseudostack': -// It contains Label entries that are 'manually' entered by instrumentation -// code. It's unrelated to real call stacks. -struct Stack { - std::uint32_t id = 0; - static constexpr int kMaxSize = 64; - int size = 0; - Label labels[kMaxSize]; -}; - -// Returns the buffer byte size required by CopyToSample. -int GetBufferSize(const Stack& stack); - -// Copies this Stack into a byte buffer, called a 'sample'. -void CopyToBuffer(const Stack& stack, char* dst); - -// Populates this Stack from an existing sample buffer, typically -// produced by CopyToSample. -void ReadFromBuffer(const char* src, Stack* stack); - -// ThreadStack is meant to be used as a thread-local singleton, assigning to -// each thread a Stack object holding its pseudo-stack of profile labels, -// plus a mutex allowing to synchronize accesses to this pseudo-stack between -// this thread and a possible profiler thread sampling it. -class ThreadStack { - public: - ThreadStack(); - ~ThreadStack(); - - const Stack& stack() const { return stack_; } - - // Returns the mutex to lock around any access to this stack. Each stack is - // accessed by potentially two threads: the thread that it belongs to - // (which calls Push and Pop) and the profiler thread during profiling - // (which calls CopyToSample). - std::mutex& Mutex() const { return mutex_; } - - // Pushes a new label on the top of this Stack. - template - void Push(Args... args) { - // This mutex locking is needed to guard against race conditions as both - // the current thread and the profiler thread may be concurrently accessing - // this stack. In addition to that, this mutex locking also serves the other - // purpose of acting as a barrier (of compiler code reordering, of runtime - // CPU instruction reordering, and of memory access reordering), which - // gives a measure of correctness to this profiler. The downside is some - // latency. As this lock will be uncontended most of the times, the cost - // should be roughly that of an sequentially-consistent atomic access, - // comparable to an access to the level of CPU data cache that is shared - // among all cores, typically 60 cycles on current ARM CPUs, plus side - // effects from barrier instructions. - std::lock_guard lock(mutex_); - // Avoid overrunning the stack, even in 'release' builds. This profiling - // instrumentation code should not ship in release builds anyway, the - // overhead of this check is negligible, and overrunning a stack array would - // be bad. - if (stack_.size >= Stack::kMaxSize) { - abort(); - } - stack_.labels[stack_.size++].Set(args...); - } - - // Pops the top-most label from this Stack. - void Pop() { - // See the comment in Push about this lock. While it would be tempting to - // try to remove this lock and just atomically decrement size_ with a - // store-release, that would not necessarily be a substitute for all of the - // purposes that this lock serves, or if it was done carefully to serve all - // of the same purposes, then that wouldn't be faster than this (mostly - // uncontended) lock. - std::lock_guard lock(mutex_); - stack_.size--; - } - - private: - mutable std::mutex mutex_; - Stack stack_; -}; - -} // namespace detail - -// RAII user-facing way to construct Labels associated with their life scope -// and get them pushed to / popped from the current thread stack. -class ScopeLabel { - public: - template - ScopeLabel(Args... args) : thread_stack_(detail::ThreadLocalThreadStack()) { - thread_stack_->Push(args...); - } - - ~ScopeLabel() { thread_stack_->Pop(); } - - private: - detail::ThreadStack* thread_stack_; -}; - -#else // no RUY_PROFILER - -class ScopeLabel { - public: - template - explicit ScopeLabel(Args...) {} - - // This destructor is needed to consistently silence clang's -Wunused-variable - // which seems to trigger semi-randomly. - ~ScopeLabel() {} -}; - -#endif - -} // namespace profiler -} // namespace ruy - -#endif // RUY_RUY_PROFILER_INSTRUMENTATION_H_