30 #include <schema_generated.h>
32 #include <flatbuffers/flexbuffers.h>
34 #include <fmt/format.h>
41 #define ARMNN_THROW_PARSE_EXCEPTION(msg) \
43 throw armnn::ParseException( static_cast<const std::stringstream&>( std::stringstream() << msg \
45 << CHECK_LOCATION().AsString()).str()); \
48 using namespace armnn;
54 pTfLiteParserImpl(
new TfLiteParserImpl(options)) {}
56 ITfLiteParser::~ITfLiteParser() =
default;
78 armnn::INetworkPtr ITfLiteParser::CreateNetworkFromBinary(
const std::vector<uint8_t>& binaryContent)
80 return pTfLiteParserImpl->CreateNetworkFromBinary(binaryContent);
84 const std::string& name)
const
86 return pTfLiteParserImpl->GetNetworkInputBindingInfo(subgraphId, name);
90 const std::string& name)
const
92 return pTfLiteParserImpl->GetNetworkOutputBindingInfo(subgraphId, name);
95 size_t ITfLiteParser::GetSubgraphCount()
const
97 return pTfLiteParserImpl->GetSubgraphCount();
100 std::vector<std::string> ITfLiteParser::GetSubgraphInputTensorNames(
size_t subgraphId)
const
102 return pTfLiteParserImpl->GetSubgraphInputTensorNames(subgraphId);
105 std::vector<std::string> ITfLiteParser::GetSubgraphOutputTensorNames(
size_t subgraphId)
const
107 return pTfLiteParserImpl->GetSubgraphOutputTensorNames(subgraphId);
113 const uint32_t VIRTUAL_OPERATOR_ID = std::numeric_limits<uint32_t>::max();
116 size_t subgraphIndex,
119 if (model.get() ==
nullptr)
122 fmt::format(
"{} was called with invalid (null) model. "
123 "Possible reason is that the model is not yet loaded and Unpack(ed). "
129 else if (subgraphIndex >= model->subgraphs.size())
132 fmt::format(
"{} was called with an invalid subgraph index. "
140 #define CHECK_SUBGRAPH(MODEL, SUBGRAPH_INDEX) \
141 CheckSubgraph(MODEL, SUBGRAPH_INDEX, CHECK_LOCATION())
144 size_t subgraphIndex,
145 size_t operatorIndex,
148 if (model.get() ==
nullptr)
151 fmt::format(
"{} was called with invalid (null) model. "
152 "Possible reason is that the model is not yet loaded and Unpack(ed). "
153 "subgraph:{} operator:{} at {}",
159 else if (subgraphIndex >= model->subgraphs.size())
162 fmt::format(
"{} was called with an invalid subgraph index. "
163 "subgraph:{} operator:{} at {}",
169 else if (operatorIndex >= model->subgraphs[subgraphIndex]->operators.size() &&
170 operatorIndex != VIRTUAL_OPERATOR_ID)
173 fmt::format(
"{} was called with an invalid operator index. "
174 "subgraph:{} operator:{} at {}",
182 #define CHECK_MODEL(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX) \
183 CheckModel(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX, CHECK_LOCATION())
186 size_t subgraphIndex,
191 if (tensorIndex >= model->subgraphs[subgraphIndex]->tensors.size())
194 fmt::format(
"{} was called with an invalid tensor index. "
195 "subgraph:{} tensor:{} at {}",
203 #define CHECK_TENSOR(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX) \
204 CheckTensor(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX, CHECK_LOCATION())
209 if (rawPtr ==
nullptr)
212 fmt::format(
"{} was called with a null tensor pointer at {}", location.
m_Function, location.
FileLine()));
216 #define CHECK_TENSOR_PTR(TENSOR_PTR) \
217 CheckTensorPtr(TENSOR_PTR, CHECK_LOCATION())
223 if (model.get() ==
nullptr)
226 fmt::format(
"{} was called with invalid (null) model. "
227 "Possible reason is that the model is not yet loaded and Unpack(ed). "
233 else if (bufferIndex >= model->buffers.size())
236 fmt::format(
"{} was called with an invalid buffer index. "
237 "buffer index:{} at {}",
242 else if (model->buffers[bufferIndex].get() ==
nullptr)
245 fmt::format(
"The buffer #{} is null. {}",
251 #define CHECK_BUFFER(MODEL, BUFFER_INDEX) \
252 CheckBuffer(MODEL, BUFFER_INDEX, CHECK_LOCATION())
254 void CheckBufferSize(TfLiteParserImpl::BufferRawPtr bufferPtr,
259 if (bufferPtr ==
nullptr)
262 fmt::format(
"BufferPtr is null for buffer:{}. {}",
269 std::stringstream ss;
270 ss <<
"Buffer #" << bufferId <<
" has " << bufferPtr->data.size() <<
" bytes. "
271 <<
"For tensor: " << tensorInfo.
GetShape()
272 <<
" expecting: " << tensorInfo.
GetNumBytes() <<
" bytes and "
281 const auto& operatorPtr = model->subgraphs[subgraphIndex]->operators[operatorIndex];
282 auto opcodeIndex = operatorPtr->opcode_index;
285 #if defined(ARMNN_POST_TFLITE_2_3)
286 auto opcode = std::max(model->operator_codes[opcodeIndex]->builtin_code,
287 static_cast<tflite::BuiltinOperator
>(model->operator_codes[opcodeIndex]->deprecated_builtin_code));
289 auto opcode = model->operator_codes[opcodeIndex]->builtin_code;
298 TfLiteParserImpl::BufferRawPtr bufferPtr = TfLiteParserImpl::GetBuffer(model, bufferIndex);
299 std::vector<unsigned int> buffer(
info.GetNumElements());
303 ::memcpy(buffer.data(), bufferPtr->data.data(), bufferPtr->data.size());
307 std::vector<uint64_t> uint64Buffer(
info.GetNumElements());
308 ::memcpy(uint64Buffer.data(), bufferPtr->data.data(), bufferPtr->data.size());
309 buffer.assign(std::begin(uint64Buffer), std::end(uint64Buffer));
315 fmt::format(
"Unsupported data type for uint buffer {}, only Signed 32 or Signed 64 are supported. {}",
322 #define CHECK_BUFFER_SIZE(BUFFER_PTR, TENSOR_INFO, BUFFER_ID) \
323 CheckBufferSize(BUFFER_PTR, TENSOR_INFO, BUFFER_ID, CHECK_LOCATION())
325 bool IsActivationSupported(tflite::ActivationFunctionType activationType)
327 switch(activationType)
329 case tflite::ActivationFunctionType_NONE:
330 case tflite::ActivationFunctionType_RELU:
331 case tflite::ActivationFunctionType_RELU6:
332 case tflite::ActivationFunctionType_TANH:
343 #define CHECK_SUPPORTED_FUSED_ACTIVATION(OPTION, SUBGRAPH_INDEX, OPERATOR_INDEX) \
345 if (IsActivationSupported(OPTION->fused_activation_function) == false) \
347 throw ParseException( \
348 fmt::format("TfLite parser doesn't support fused activation: " \
349 "{}/{} in {} subgraph:{} operator:{} at {}", \
350 OPTION->fused_activation_function, \
351 tflite::EnumNameActivationFunctionType(\
352 OPTION->fused_activation_function), \
356 CHECK_LOCATION().FileLine())); \
361 std::vector<unsigned int> AsUnsignedVector(
const std::vector<int32_t>& in)
363 std::vector<unsigned int> result;
364 result.reserve(in.size());
377 bool IsOptionalOperandPresent(
int input)
382 void CalcPadding(uint32_t inputSize,
386 uint32_t& paddingFront,
387 uint32_t& paddingBack,
388 tflite::Padding padding)
392 if (padding == tflite::Padding_SAME)
394 uint32_t outputSize = (inputSize + stride - 1) / stride;
395 uint32_t dilatedSize = filterSize + (dilation - 1) * (filterSize - 1);
396 uint32_t temp = (outputSize - 1) * stride + dilatedSize;
397 if (temp > inputSize)
399 paddingFront = (temp - inputSize) / 2;
400 paddingBack = (temp - inputSize) - paddingFront;
408 void CalcPadding(uint32_t inputSize,
412 uint32_t& paddingFront,
413 uint32_t& paddingBack,
414 tflite::Padding padding,
420 if (padding == tflite::Padding_SAME)
422 uint32_t totalPadding = (inputSize - 1) * stride + filterSize - outputSize;
423 paddingFront = totalPadding / 2;
424 paddingBack = totalPadding - paddingFront;
429 const std::vector<unsigned int>& shape,
430 const bool outputTensor =
false)
435 switch (tensorPtr->type)
437 case tflite::TensorType_UINT8:
440 case tflite::TensorType_FLOAT32:
443 case tflite::TensorType_FLOAT16:
446 case tflite::TensorType_INT8:
447 if (tensorPtr->quantization->zero_point.size() == 1)
458 case tflite::TensorType_INT16:
461 case tflite::TensorType_INT32:
464 case tflite::TensorType_INT64:
467 case tflite::TensorType_BOOL:
474 fmt::format(
"Unsupported data type {} = {} for tensor: {}. {}",
476 tflite::EnumNameTensorType(tensorPtr->type),
483 std::vector<unsigned int> safeShape = shape;
484 if (shape.size() == 0)
486 safeShape.push_back(1);
491 tensorShape =
TensorShape(armnn::numeric_cast<unsigned int>(safeShape.size()), safeShape.data());
495 size_t shapeSignatureSize = tensorPtr->shape_signature.size();
498 if (shapeSignatureSize != 0)
501 if (shapeSignatureSize != shape.size())
505 for (
unsigned int i = 0; i < shapeSignatureSize; ++i)
507 unsigned int dim = tensorPtr->shape_signature[i] > -1 ?
508 static_cast<unsigned int>(tensorPtr->shape_signature[i]) : 0;
509 safeShape.push_back(dim);
513 std::unique_ptr<bool[]> dimMask = std::make_unique<bool[]>(tensorPtr->shape_signature.size());
514 bool batchOnly =
true;
515 for (
unsigned int i = 0; i < tensorPtr->shape_signature.size(); ++i)
517 dimMask[i] = tensorPtr->shape_signature[i] != -1;
519 if (i > 0 && !dimMask[i])
528 tensorShape =
TensorShape(
static_cast<unsigned int>(safeShape.size()), safeShape.data(), dimMask.get());
531 else if (shape.size() == 0)
537 tensorShape =
TensorShape(armnn::numeric_cast<unsigned int>(shape.size()), shape.data());
541 float quantizationScale = 1.0f;
542 int32_t quantizationOffset = 0;
544 if (tensorPtr->quantization.get())
546 if (tensorPtr->quantization->scale.size() <= 1)
551 if (tensorPtr->quantization->scale.size() == 1)
553 quantizationScale = tensorPtr->quantization->scale[0];
555 if (tensorPtr->quantization->zero_point.size() == 1)
559 quantizationOffset = armnn::numeric_cast<int32_t>(tensorPtr->quantization->zero_point[0]);
570 std::vector<float> quantizationScales;
571 std::vector<int32_t> quantizationOffsets;
574 std::copy(tensorPtr->quantization->scale.begin(),
575 tensorPtr->quantization->scale.end(),
576 std::back_inserter(quantizationScales));
582 armnn::numeric_cast<unsigned int>(tensorPtr->quantization->quantized_dimension));
597 const bool outputTensor =
false)
599 auto const& dimensions = AsUnsignedVector(tensorPtr->shape);
600 return ToTensorInfo(tensorPtr, dimensions, outputTensor);
604 std::pair<armnn::ConstTensor, std::unique_ptr<T[]>>
628 reinterpret_cast<const T*
>(bufferPtr->data.data()), data.get(),
sizeof(T));
632 ::memcpy(data.get(), bufferPtr->data.data(), tensorInfo.
GetNumBytes());
638 return std::make_pair(
ConstTensor(tensorInfo, data.get()), std::move(data));
651 if (actualSize != expected.size())
656 for (
unsigned int i = 0u; i < actualSize; i++)
658 if (expected[i] < 0 ||
659 actual[i] !=
static_cast<unsigned int>(expected[i]))
670 std::vector<int32_t> expectedVec;
673 expectedVec.push_back(expected[i]);
678 void CheckMatchingQuantization(
const TensorInfo& first,
680 const std::string& descName,
681 std::string
const& firstName,
682 std::string
const& secondName)
694 if (firstDataType != secondDataType)
697 " must be of the same quantized type, " +
705 " must have the same quantization space, " +
715 auto shape = tensorPtr->shape;
721 auto shapeSig = tensorPtr->shape_signature;
723 if (shapeSig.empty())
728 for (
unsigned int i = 0; i < shapeSig.size() ; ++i)
730 if (shapeSig[i] == -1)
742 , m_Network(nullptr, nullptr)
746 m_ParserFunctions[tflite::BuiltinOperator_ABS] = &TfLiteParserImpl::ParseAbs;
747 m_ParserFunctions[tflite::BuiltinOperator_ADD] = &TfLiteParserImpl::ParseAdd;
748 m_ParserFunctions[tflite::BuiltinOperator_ARG_MIN] = &TfLiteParserImpl::ParseArgMin;
749 m_ParserFunctions[tflite::BuiltinOperator_ARG_MAX] = &TfLiteParserImpl::ParseArgMax;
750 m_ParserFunctions[tflite::BuiltinOperator_AVERAGE_POOL_2D] = &TfLiteParserImpl::ParseAveragePool2D;
751 m_ParserFunctions[tflite::BuiltinOperator_BATCH_TO_SPACE_ND] = &TfLiteParserImpl::ParseBatchToSpaceND;
752 m_ParserFunctions[tflite::BuiltinOperator_BATCH_MATMUL] = &TfLiteParserImpl::ParseBatchMatMul;
753 m_ParserFunctions[tflite::BuiltinOperator_CEIL] = &TfLiteParserImpl::ParseCeil;
754 m_ParserFunctions[tflite::BuiltinOperator_CAST] = &TfLiteParserImpl::ParseCast;
755 m_ParserFunctions[tflite::BuiltinOperator_CONCATENATION] = &TfLiteParserImpl::ParseConcatenation;
756 m_ParserFunctions[tflite::BuiltinOperator_CONV_2D] = &TfLiteParserImpl::ParseConv2D;
758 #if defined(ARMNN_POST_TFLITE_2_4)
759 m_ParserFunctions[tflite::BuiltinOperator_CONV_3D] = &TfLiteParserImpl::ParseConv3D;
761 m_ParserFunctions[tflite::BuiltinOperator_CUSTOM] = &TfLiteParserImpl::ParseCustomOperator;
762 m_ParserFunctions[tflite::BuiltinOperator_DEPTH_TO_SPACE] = &TfLiteParserImpl::ParseDepthToSpace;
763 m_ParserFunctions[tflite::BuiltinOperator_DEPTHWISE_CONV_2D] = &TfLiteParserImpl::ParseDepthwiseConv2D;
764 m_ParserFunctions[tflite::BuiltinOperator_DEQUANTIZE] = &TfLiteParserImpl::ParseDequantize;
765 m_ParserFunctions[tflite::BuiltinOperator_DIV] = &TfLiteParserImpl::ParseDiv;
766 m_ParserFunctions[tflite::BuiltinOperator_ELU] = &TfLiteParserImpl::ParseElu;
767 m_ParserFunctions[tflite::BuiltinOperator_EQUAL] = &TfLiteParserImpl::ParseEqual;
768 m_ParserFunctions[tflite::BuiltinOperator_EXP] = &TfLiteParserImpl::ParseExp;
769 m_ParserFunctions[tflite::BuiltinOperator_EXPAND_DIMS] = &TfLiteParserImpl::ParseExpandDims;
770 m_ParserFunctions[tflite::BuiltinOperator_FLOOR_DIV] = &TfLiteParserImpl::ParseFloorDiv;
771 m_ParserFunctions[tflite::BuiltinOperator_FULLY_CONNECTED] = &TfLiteParserImpl::ParseFullyConnected;
772 m_ParserFunctions[tflite::BuiltinOperator_GATHER] = &TfLiteParserImpl::ParseGather;
773 m_ParserFunctions[tflite::BuiltinOperator_GATHER_ND] = &TfLiteParserImpl::ParseGatherNd;
774 m_ParserFunctions[tflite::BuiltinOperator_GREATER] = &TfLiteParserImpl::ParseGreater;
775 m_ParserFunctions[tflite::BuiltinOperator_GREATER_EQUAL] = &TfLiteParserImpl::ParseGreaterOrEqual;
776 m_ParserFunctions[tflite::BuiltinOperator_HARD_SWISH] = &TfLiteParserImpl::ParseHardSwish;
777 m_ParserFunctions[tflite::BuiltinOperator_LEAKY_RELU] = &TfLiteParserImpl::ParseLeakyRelu;
778 m_ParserFunctions[tflite::BuiltinOperator_LESS] = &TfLiteParserImpl::ParseLess;
779 m_ParserFunctions[tflite::BuiltinOperator_LESS_EQUAL] = &TfLiteParserImpl::ParseLessOrEqual;
780 m_ParserFunctions[tflite::BuiltinOperator_LOCAL_RESPONSE_NORMALIZATION]
781 = &TfLiteParserImpl::ParseLocalResponseNormalization;
782 m_ParserFunctions[tflite::BuiltinOperator_LOG] = &TfLiteParserImpl::ParseLog;
783 m_ParserFunctions[tflite::BuiltinOperator_LOGICAL_NOT] = &TfLiteParserImpl::ParseLogicalNot;
784 m_ParserFunctions[tflite::BuiltinOperator_LOGISTIC] = &TfLiteParserImpl::ParseLogistic;
785 m_ParserFunctions[tflite::BuiltinOperator_LOG_SOFTMAX] = &TfLiteParserImpl::ParseLogSoftmax;
786 m_ParserFunctions[tflite::BuiltinOperator_L2_NORMALIZATION] = &TfLiteParserImpl::ParseL2Normalization;
787 m_ParserFunctions[tflite::BuiltinOperator_MAX_POOL_2D] = &TfLiteParserImpl::ParseMaxPool2D;
788 m_ParserFunctions[tflite::BuiltinOperator_MAXIMUM] = &TfLiteParserImpl::ParseMaximum;
789 m_ParserFunctions[tflite::BuiltinOperator_MEAN] = &TfLiteParserImpl::ParseMean;
790 m_ParserFunctions[tflite::BuiltinOperator_MINIMUM] = &TfLiteParserImpl::ParseMinimum;
791 m_ParserFunctions[tflite::BuiltinOperator_MIRROR_PAD] = &TfLiteParserImpl::ParseMirrorPad;
792 m_ParserFunctions[tflite::BuiltinOperator_MUL] = &TfLiteParserImpl::ParseMul;
793 m_ParserFunctions[tflite::BuiltinOperator_NEG] = &TfLiteParserImpl::ParseNeg;
794 m_ParserFunctions[tflite::BuiltinOperator_NOT_EQUAL] = &TfLiteParserImpl::ParseNotEqual;
795 m_ParserFunctions[tflite::BuiltinOperator_PACK] = &TfLiteParserImpl::ParsePack;
796 m_ParserFunctions[tflite::BuiltinOperator_PAD] = &TfLiteParserImpl::ParsePad;
797 m_ParserFunctions[tflite::BuiltinOperator_PADV2] = &TfLiteParserImpl::ParsePad;
798 m_ParserFunctions[tflite::BuiltinOperator_POW] = &TfLiteParserImpl::ParsePower;
799 m_ParserFunctions[tflite::BuiltinOperator_PRELU] = &TfLiteParserImpl::ParsePrelu;
800 m_ParserFunctions[tflite::BuiltinOperator_QUANTIZE] = &TfLiteParserImpl::ParseQuantize;
801 m_ParserFunctions[tflite::BuiltinOperator_RELU] = &TfLiteParserImpl::ParseRelu;
802 m_ParserFunctions[tflite::BuiltinOperator_RELU6] = &TfLiteParserImpl::ParseRelu6;
803 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MAX] = &TfLiteParserImpl::ParseReduceMax;
804 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MIN] = &TfLiteParserImpl::ParseReduceMin;
805 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_PROD] = &TfLiteParserImpl::ParseReduceProd;
806 m_ParserFunctions[tflite::BuiltinOperator_RESHAPE] = &TfLiteParserImpl::ParseReshape;
807 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_BILINEAR] = &TfLiteParserImpl::ParseResizeBilinear;
808 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_NEAREST_NEIGHBOR] = &TfLiteParserImpl::ParseResizeNearestNeighbor;
809 m_ParserFunctions[tflite::BuiltinOperator_REVERSE_V2] = &TfLiteParserImpl::ParseReverseV2;
810 m_ParserFunctions[tflite::BuiltinOperator_RSQRT] = &TfLiteParserImpl::ParseRsqrt;
811 m_ParserFunctions[tflite::BuiltinOperator_SQRT] = &TfLiteParserImpl::ParseSqrt;
812 m_ParserFunctions[tflite::BuiltinOperator_SHAPE] = &TfLiteParserImpl::ParseShape;
813 m_ParserFunctions[tflite::BuiltinOperator_SIN] = &TfLiteParserImpl::ParseSin;
814 m_ParserFunctions[tflite::BuiltinOperator_SLICE] = &TfLiteParserImpl::ParseSlice;
815 m_ParserFunctions[tflite::BuiltinOperator_SOFTMAX] = &TfLiteParserImpl::ParseSoftmax;
816 m_ParserFunctions[tflite::BuiltinOperator_SPACE_TO_BATCH_ND] = &TfLiteParserImpl::ParseSpaceToBatchND;
817 m_ParserFunctions[tflite::BuiltinOperator_SPACE_TO_DEPTH] = &TfLiteParserImpl::ParseSpaceToDepth;
818 m_ParserFunctions[tflite::BuiltinOperator_SPLIT] = &TfLiteParserImpl::ParseSplit;
819 m_ParserFunctions[tflite::BuiltinOperator_SPLIT_V] = &TfLiteParserImpl::ParseSplitV;
820 m_ParserFunctions[tflite::BuiltinOperator_SQUEEZE] = &TfLiteParserImpl::ParseSqueeze;
821 m_ParserFunctions[tflite::BuiltinOperator_SQUARE] = &TfLiteParserImpl::ParseSquare;
822 m_ParserFunctions[tflite::BuiltinOperator_SQUARED_DIFFERENCE] = &TfLiteParserImpl::ParseSquaredDifference;
823 m_ParserFunctions[tflite::BuiltinOperator_STRIDED_SLICE] = &TfLiteParserImpl::ParseStridedSlice;
824 m_ParserFunctions[tflite::BuiltinOperator_SUB] = &TfLiteParserImpl::ParseSub;
825 m_ParserFunctions[tflite::BuiltinOperator_SUM] = &TfLiteParserImpl::ParseSum;
826 m_ParserFunctions[tflite::BuiltinOperator_TANH] = &TfLiteParserImpl::ParseTanH;
827 m_ParserFunctions[tflite::BuiltinOperator_TILE] = &TfLiteParserImpl::ParseTile;
828 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE] = &TfLiteParserImpl::ParseTranspose;
829 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE_CONV] = &TfLiteParserImpl::ParseTransposeConv;
830 m_ParserFunctions[tflite::BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM]
831 = &TfLiteParserImpl::ParseUnidirectionalSequenceLSTM;
832 m_ParserFunctions[tflite::BuiltinOperator_UNPACK] = &TfLiteParserImpl::ParseUnpack;
835 m_CustomParserFunctions[
"TFLite_Detection_PostProcess"] = &TfLiteParserImpl::ParseDetectionPostProcess;
839 size_t operatorIndex,
842 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
843 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
846 auto search = armnnTfLiteParser::TfLiteParserImpl::m_TensorInfos.find(inputId);
848 if (search != m_TensorInfos.end())
850 return m_TensorInfos[inputId];
855 m_TensorInfos.insert({ inputId, tensorInfo });
860 armnn::TensorInfo TfLiteParserImpl::OutputTensorInfoFromInputs(
size_t subgraphIndex,
861 size_t operatorIndex,
864 std::vector<int> inputs)
866 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
867 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
871 auto outputSearch = armnnTfLiteParser::TfLiteParserImpl::m_TensorInfos.find(outputId);
873 if (outputSearch != m_TensorInfos.end())
875 return m_TensorInfos[outputId];
878 const auto& outputTensorPtr = subgraphPtr->tensors[outputId].get();
881 if (IsDynamic(outputTensorPtr))
887 inputs.emplace_back(i);
891 std::vector<armnn::TensorShape> inputShapes;
893 for (
unsigned int i = 0; i < inputs.size(); ++i)
896 auto search = armnnTfLiteParser::TfLiteParserImpl::m_TensorInfos.find(inputId);
898 if (search != m_TensorInfos.end())
900 auto &inputTensorInfo = m_TensorInfos[inputId];
901 inputShapes.push_back(inputTensorInfo.GetShape());
906 m_TensorInfos.insert({ inputId, inputTensorInfo});
907 inputShapes.push_back(inputTensorInfo.GetShape());
913 m_TensorInfos.insert({ outputId, tensor});
917 armnn::TensorInfo TfLiteParserImpl::OutputTensorInfoFromShapes(
size_t subgraphIndex,
918 size_t operatorIndex,
921 std::vector<armnn::TensorShape> inputShapes)
923 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
924 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
927 const auto& outputTensorPtr = subgraphPtr->tensors[outputId].get();
930 if (IsDynamic(outputTensorPtr))
935 m_TensorInfos.insert({ outputId, tensor});
939 void TfLiteParserImpl::ResetParser()
943 m_SubgraphConnections.clear();
944 m_OverriddenOutputShapes.clear();
945 m_ConstantsToDequantize.clear();
946 m_ConstantsToBeCreated.clear();
947 m_TensorInfos.clear();
954 return CreateNetworkFromModel();
961 return CreateNetworkFromModel();
968 m_Model = std::move(model);
970 return CreateNetworkFromModel();
973 INetworkPtr TfLiteParserImpl::CreateNetworkFromModel()
980 if (m_Options.value().m_InferAndValidate)
984 {
"InferAndValidate",
true }
987 networkOptions.push_back(shapeInferenceMethodOption);
989 if (m_Options.value().m_AllowExpandedDims)
993 {
"AllowExpandedDims",
true }
996 networkOptions.push_back(shapeInferenceMethodOption);
999 m_Network = INetwork::Create(networkOptions);
1001 if (m_Model.get() ==
nullptr)
1006 if (m_Model->subgraphs.size() != 1)
1009 fmt::format(
"Current TfLite parser only supports 1 subgraph. Current one has: {} {}",
1010 m_Model->subgraphs.size(),
1014 size_t subgraphIndex = 0;
1015 size_t operatorIndex = 0;
1018 for (
SubgraphPtr const& subgraph : m_Model->subgraphs)
1020 SetupInputLayerTensorInfos(subgraphIndex);
1021 SetupConstantLayerTensorInfos(subgraphIndex);
1023 m_SubgraphConnections.emplace_back(subgraph->tensors.size());
1026 auto const& opCodePtr = m_Model->operator_codes[op->opcode_index];
1029 #if defined(ARMNN_POST_TFLITE_2_3)
1030 auto builtinCode = std::max(opCodePtr->builtin_code,
1031 static_cast<tflite::BuiltinOperator
>(opCodePtr->deprecated_builtin_code));
1033 auto builtinCode = opCodePtr->builtin_code;
1036 if (builtinCode > tflite::BuiltinOperator_MAX)
1038 throw ParseException(fmt::format(
"Operator code {} is out of range 0-{}. "
1039 "subgraph:{} operator idx:{}. {}",
1040 builtinCode, tflite::BuiltinOperator_MAX, subgraphIndex,
1045 auto& parserFunction = m_ParserFunctions[builtinCode];
1046 (this->*parserFunction)(subgraphIndex, operatorIndex);
1050 SetupInputLayers(subgraphIndex);
1051 SetupOutputLayers(subgraphIndex);
1052 SetupConstantLayers(subgraphIndex);
1060 std::stringstream errorString;
1061 errorString <<
"Failed to parse operator #" << operatorIndex <<
" within subgraph #"
1062 << subgraphIndex <<
" error: " << e.
what();
1064 std::stringstream errors;
1065 errors << errorString.str() <<
"\n";
1070 for (subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
1072 for (
size_t tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
1074 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot !=
nullptr)
1076 for (
size_t inputSlotIdx = 0;
1077 inputSlotIdx < m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size();
1080 m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot->Connect(
1081 *(m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots[inputSlotIdx]));
1086 return std::move(m_Network);
1093 return (TfLiteParserImpl::IsConstTensor(tensorPtr) && inputDataType == DataType::Float32 &&
1094 (tensorDataType == DataType::QAsymmU8 ||
1095 tensorDataType == DataType::QAsymmS8 ||
1096 tensorDataType == DataType::QSymmS8 ||
1097 tensorDataType == DataType::Signed32 ||
1098 tensorDataType == DataType::Signed64));
1101 void TfLiteParserImpl::RegisterProducerOfTensor(
size_t subgraphIndex,
1107 TensorSlots & tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
1113 if (tensorSlots.outputSlot !=
nullptr)
1115 throw ParseException(fmt::format(
"Another layer has already registered itself as the producer of "
1116 "subgraph:{} tensor:{} {}",
1122 tensorSlots.outputSlot = slot;
1125 void TfLiteParserImpl::RegisterConsumerOfTensor(
size_t subgraphIndex,
1131 TensorSlots& tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
1132 tensorSlots.inputSlots.push_back(slot);
1135 void TfLiteParserImpl::ParseCustomOperator(
size_t subgraphIndex,
size_t operatorIndex)
1137 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1140 auto customParserFunction = &TfLiteParserImpl::ParseUnsupportedOperator;
1143 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1144 const auto& customCode = m_Model->operator_codes[operatorPtr->opcode_index]->custom_code;
1147 auto iterator = m_CustomParserFunctions.find(customCode);
1148 if (iterator != m_CustomParserFunctions.end())
1150 customParserFunction = iterator->second;
1154 (this->*customParserFunction)(subgraphIndex, operatorIndex);
1157 void TfLiteParserImpl::ParseUnsupportedOperator(
size_t subgraphIndex,
size_t operatorIndex)
1159 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1161 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1163 auto opcodeIndex = operatorPtr->opcode_index;
1166 #if defined(ARMNN_POST_TFLITE_2_3)
1167 auto opcode = std::max(m_Model->operator_codes[opcodeIndex]->builtin_code,
1168 static_cast<tflite::BuiltinOperator
>(m_Model->operator_codes[opcodeIndex]->deprecated_builtin_code));
1170 auto opcode = m_Model->operator_codes[opcodeIndex]->builtin_code;
1173 if (!m_Options || !m_Options.value().m_StandInLayerForUnsupported)
1177 fmt::format(
"Operator not supported. "
1178 "subgraph:{} operator:{} "
1179 "opcode_index:{} opcode:{} / {} {}",
1184 tflite::EnumNameBuiltinOperator(opcode),
1188 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1189 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1191 const unsigned int numInputs = armnn::numeric_cast<unsigned int>(inputs.size());
1192 const unsigned int numOutputs = armnn::numeric_cast<unsigned int>(outputs.size());
1195 auto layerName = fmt::format(
"StandIn:{}:{}:{}", subgraphIndex, operatorIndex, opcode);
1198 IConnectableLayer* layer = m_Network->AddStandInLayer(descriptor, layerName.c_str());
1206 for (
unsigned int i = 0u; i < numOutputs; ++i)
1211 auto inputTensorIds = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1212 auto outputTensorIds = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1214 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIds);
1215 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIds);
1218 void TfLiteParserImpl::ParseCast(
size_t subgraphIndex,
size_t operatorIndex)
1220 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1222 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1224 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1227 auto layerName = fmt::format(
"Cast:{}:{}", subgraphIndex, operatorIndex);
1237 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
1240 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1241 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1243 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1244 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1247 void TfLiteParserImpl::ParseConv2D(
size_t subgraphIndex,
size_t operatorIndex)
1249 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1251 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1252 const auto* options = operatorPtr->builtin_options.AsConv2DOptions();
1256 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1257 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1261 inputs.size() == 3 ?
1269 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1270 armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1273 unsigned int inputHeight = inputTensorInfo.
GetShape()[1];
1274 unsigned int inputWidth = inputTensorInfo.
GetShape()[2];
1278 unsigned int filterHeight = filterTensorInfo.
GetShape()[1];
1279 unsigned int filterWidth = filterTensorInfo.
GetShape()[2];
1281 CalcPadding(inputHeight, filterHeight, desc.
m_StrideY,
1283 CalcPadding(inputWidth, filterWidth, desc.
m_StrideX,
1288 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1289 std::vector<unsigned int> tensorIndexesToRegister = { inputTensorIndexes[0], inputTensorIndexes[1] };
1291 auto layerName = fmt::format(
"Conv2D:{}:{}", subgraphIndex, operatorIndex);
1294 if (ShouldConstantTensorBeConverted(inputs[1], inputTensorInfo.
GetDataType(), filterTensorInfo.
GetDataType()))
1296 m_ConstantsToDequantize.emplace_back(inputs[1]->buffer);
1301 armnn::TensorInfo biasTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
1304 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
1306 if (ShouldConstantTensorBeConverted(inputs[2], inputTensorInfo.
GetDataType(), biasTensorInfo.
GetDataType()))
1308 m_ConstantsToDequantize.emplace_back(inputs[2]->buffer);
1318 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
1323 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
1325 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1327 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1328 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, { outputTensorIndexes[0] });
1332 #if defined(ARMNN_POST_TFLITE_2_4)
1333 void TfLiteParserImpl::ParseConv3D(
size_t subgraphIndex,
size_t operatorIndex)
1335 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1337 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1338 const auto* options = operatorPtr->builtin_options.AsConv3DOptions();
1352 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1355 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1358 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1359 armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1362 unsigned int inputDepth = inputTensorInfo.
GetShape()[1];
1363 unsigned int inputHeight = inputTensorInfo.
GetShape()[2];
1364 unsigned int inputWidth = inputTensorInfo.
GetShape()[3];
1367 unsigned int filterDepth = filterTensorInfo.
GetShape()[0];
1368 unsigned int filterHeight = filterTensorInfo.
GetShape()[1];
1369 unsigned int filterWidth = filterTensorInfo.
GetShape()[2];
1371 CalcPadding(inputDepth, filterDepth, desc.
m_StrideZ,
1373 CalcPadding(inputHeight, filterHeight, desc.
m_StrideY,
1375 CalcPadding(inputWidth, filterWidth, desc.
m_StrideX,
1378 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo, inputTensorInfo.
GetDataType());
1380 auto layerName = fmt::format(
"Conv3D:{}:{}", subgraphIndex, operatorIndex);
1382 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1385 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0], inputTensorIndexes[1]};
1387 if (inputs.size() == 3)
1392 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
1403 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
1407 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
1409 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1411 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1412 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1416 void TfLiteParserImpl::ParseDepthwiseConv2D(
size_t subgraphIndex,
size_t operatorIndex)
1418 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1420 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1421 const auto* options = operatorPtr->builtin_options.AsDepthwiseConv2DOptions();
1431 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1433 if (inputs.size() == 3)
1438 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1443 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1444 armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1447 unsigned int inputHeight = inputTensorInfo.
GetShape()[1];
1448 unsigned int inputWidth = inputTensorInfo.
GetShape()[2];
1451 unsigned int filterHeight = filterTensorInfo.
GetShape()[1];
1452 unsigned int filterWidth = filterTensorInfo.
GetShape()[2];
1454 CalcPadding(inputHeight, filterHeight, desc.
m_StrideY,
1456 CalcPadding(inputWidth, filterWidth, desc.
m_StrideX,
1460 auto layerName = fmt::format(
"DepthwiseConv2D:{}:{}", subgraphIndex, operatorIndex);
1462 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1465 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0], inputTensorIndexes[1]};
1472 TensorInfo biasTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
1475 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
1484 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
1489 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
1491 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1493 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1494 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1497 void TfLiteParserImpl::ParseDequantize(
size_t subgraphIndex,
size_t operatorIndex)
1499 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1501 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1504 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1507 auto layerName = fmt::format(
"Dequantize:{}:{}", subgraphIndex, operatorIndex);
1517 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
1520 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1521 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1523 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1524 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1527 void TfLiteParserImpl::ParseExpandDims(
size_t subgraphIndex,
size_t operatorIndex)
1529 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1531 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1534 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1537 auto layerName = fmt::format(
"ExpandDims:{}:{}", subgraphIndex, operatorIndex);
1539 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1541 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1543 armnn::TensorInfo axisTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1546 if (axisBufferPtr ==
nullptr)
1548 throw ParseException(fmt::format(
"{}: Operation has invalid inputs. Failed to read axis.",
1553 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.
GetNumBytes());
1554 int32_t axis = axisData[0];
1557 auto outputRank = inputRank + 1;
1558 if((axis < -1 * outputRank) || (outputRank <= axis))
1560 throw ParseException(fmt::format(
"{}: Axis {} is not within [-{}, {}) range.",
1564 axis = axis < 0 ? (axis + outputRank) : axis;
1566 std::vector<unsigned int> shape(
static_cast<unsigned int>(outputRank));
1567 unsigned int inputShapeIndex = 0;
1568 for (
unsigned int i = 0; i < static_cast<unsigned int>(outputRank); ++i)
1570 if (i ==
static_cast<unsigned int>(axis))
1576 shape[i] = inputTensorInfo.
GetShape()[inputShapeIndex];
1585 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
1594 m_TensorInfos[outputTensorIds[0]] = outputTensorInfo;
1596 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1597 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1599 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1600 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1603 void TfLiteParserImpl::ParseTranspose(
size_t subgraphIndex,
size_t operatorIndex)
1605 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1607 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1610 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1613 auto layerName = fmt::format(
"Transpose:{}:{}", subgraphIndex, operatorIndex);
1616 if (inputs.size() == 2)
1618 armnn::TensorInfo permuteTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1621 std::vector<unsigned int> permuteShape(numPermVecElements);
1622 ::memcpy(permuteShape.data(), permuteBufferPtr->data.data(), permuteTensorInfo.
GetNumBytes());
1627 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1629 IConnectableLayer* layer = m_Network->AddTransposeLayer(desc, layerName.c_str());
1637 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
1638 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1641 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1642 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1644 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1645 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1648 void TfLiteParserImpl::ParseTransposeConv(
size_t subgraphIndex,
size_t operatorIndex)
1650 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1652 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1653 const auto* options = operatorPtr->builtin_options.AsTransposeConvOptions();
1661 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1662 if (inputs.size() == 4)
1671 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1675 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
1676 armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1679 const unsigned int inputHeight = inputTensorInfo.
GetShape()[1];
1680 const unsigned int inputWidth = inputTensorInfo.
GetShape()[2];
1682 const unsigned int filterHeight = filterTensorInfo.
GetShape()[1];
1683 const unsigned int filterWidth = filterTensorInfo.
GetShape()[2];
1689 if (inputs[0] && IsConstTensor(inputs[0]))
1691 armnn::TensorInfo tensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1694 if (tensorInfo.
GetDataType() == DataType::Signed32)
1696 ::memcpy(output_shape.data(),
GetBuffer(m_Model, inputs[0]->buffer)->data.data(), tensorInfo.
GetNumBytes());
1698 if (tensorInfo.
GetDataType() == DataType::QAsymmU8)
1702 output_shape[i] =
GetBuffer(m_Model, inputs[0]->buffer)->data.data()[i];
1706 for (
int dimension : output_shape)
1708 desc.
m_OutputShape.push_back(
static_cast<unsigned int>(dimension));
1716 CalcPadding(inputHeight,
1725 CalcPadding(inputWidth,
1736 CalcPadding(inputHeight,
1744 CalcPadding(inputWidth,
1753 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo, inputTensorInfo.
GetDataType());
1756 auto layerName = fmt::format(
"TransposeConv:{}:{}", subgraphIndex, operatorIndex);
1760 auto biasTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 3);
1761 auto biasConstTensor = CreateConstTensorNonPermuted(inputs[3], biasTensorInfo, inputTensorInfo.
GetDataType());
1762 layer = m_Network->AddTransposeConvolution2dLayer(desc,
1763 filterTensorAndData.first,
1764 biasConstTensor.first,
1769 layer = m_Network->AddTransposeConvolution2dLayer(desc,
1770 filterTensorAndData.first,
1781 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0 , { 2, 1 });
1785 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1786 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[2]});
1788 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1789 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1792 void TfLiteParserImpl::ParseAveragePool2D(
size_t subgraphIndex,
size_t operatorIndex)
1794 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Average);
1797 void TfLiteParserImpl::ParseBatchMatMul(
size_t subgraphIndex,
size_t operatorIndex)
1799 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1801 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1804 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1807 auto layerName = fmt::format(
"BatchMatMul:{}:{}", subgraphIndex, operatorIndex);
1809 TensorInfo inputXTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1810 TensorInfo inputYTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1812 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1813 const auto* options = operatorPtr->builtin_options.AsBatchMatMulOptions();
1822 IConnectableLayer* layer = m_Network->AddBatchMatMulLayer(descriptor, layerName.c_str());
1830 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
1833 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1834 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1836 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1837 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1840 void TfLiteParserImpl::ParseBatchToSpaceND(
size_t subgraphIndex,
size_t operatorIndex)
1842 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1844 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1847 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1850 armnn::TensorInfo blockShapeTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1853 armnn::TensorInfo cropsTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
1856 std::vector<unsigned int> blockShape(blockShapeTensorInfo.
GetNumElements());
1857 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.
GetNumBytes());
1859 std::vector<unsigned int> cropsVector(cropsTensorInfo.
GetNumElements());
1860 ::memcpy(cropsVector.data(), cropsBufferPtr->data.data(), cropsTensorInfo.
GetNumBytes());
1863 std::vector<std::pair<unsigned int, unsigned int>> crops;
1864 for (
unsigned int i = 0; i < cropsTensorInfo.
GetNumElements() / step; ++i)
1866 crops.emplace_back(cropsVector[i * step], cropsVector[i * step + 1]);
1874 auto layerName = fmt::format(
"BatchToSpaceND:{}:{}", subgraphIndex, operatorIndex);
1876 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1878 IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(desc, layerName.c_str());
1886 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
1887 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1890 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1891 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1893 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1894 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1897 void TfLiteParserImpl::ParseL2Normalization(
size_t subgraphIndex,
size_t operatorIndex)
1899 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1901 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1904 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1909 auto layerName = fmt::format(
"L2Normalization:{}:{}", subgraphIndex, operatorIndex);
1910 IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(desc, layerName.c_str());
1918 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
1921 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1922 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1924 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1925 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1928 void TfLiteParserImpl::ParseMaxPool2D(
size_t subgraphIndex,
size_t operatorIndex)
1930 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Max);
1933 void TfLiteParserImpl::ParseMaximum(
size_t subgraphIndex,
size_t operatorIndex)
1935 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1937 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1940 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1943 auto layerName = fmt::format(
"Maximum:{}:{}", subgraphIndex, operatorIndex);
1945 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1946 TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1947 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName,
"Input 0",
"Input 1");
1949 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Maximum, layerName.c_str());
1957 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
1958 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1961 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1962 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1964 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1965 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1968 void TfLiteParserImpl::ParseMinimum(
size_t subgraphIndex,
size_t operatorIndex)
1970 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1972 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1975 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1978 auto layerName = fmt::format(
"Minimum:{}:{}", subgraphIndex, operatorIndex);
1980 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1981 TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1982 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName,
"Input 0",
"Input 1");
1984 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Minimum, layerName.c_str());
1992 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
1993 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1996 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1997 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1999 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2000 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2003 void TfLiteParserImpl::ParsePool(
size_t subgraphIndex,
2004 size_t operatorIndex,
2007 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2009 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2010 const auto* options = operatorPtr->builtin_options.AsPool2DOptions();
2014 std::string layerName;
2018 case PoolingAlgorithm::Average:
2020 fmt::format(
"AveragePool2D:{}:{}", subgraphIndex, operatorIndex);
2022 case PoolingAlgorithm::Max:
2024 fmt::format(
"MaxPool2D:{}:{}", subgraphIndex, operatorIndex);
2041 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2043 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2046 unsigned int inputHeight = inputTensorInfo.
GetShape()[1];
2047 unsigned int inputWidth = inputTensorInfo.
GetShape()[2];
2054 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2057 IConnectableLayer* layer = m_Network->AddPooling2dLayer(desc, layerName.c_str());
2065 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2066 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
2071 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2072 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2074 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2076 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2077 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2080 void TfLiteParserImpl::ParseSlice(
size_t subgraphIndex,
size_t operatorIndex)
2082 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2084 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2086 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2092 armnn::TensorInfo beginTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2095 std::vector<unsigned int> begin(beginTensorInfo.
GetNumElements());
2096 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.
GetNumBytes());
2099 armnn::TensorInfo sizeTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
2105 if (sizeBufferPtr->data.data())
2107 ::memcpy(signedSize.data(), sizeBufferPtr->data.data(), sizeTensorInfo.
GetNumBytes());
2111 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2113 for (
unsigned int i = 0; i < signedSize.size(); ++i)
2115 int signedValue = signedSize[i];
2117 if (signedValue < -1 || signedValue >
static_cast<int>(inputTensorInfo.
GetShape()[i] - begin[i]))
2119 throw ParseException(fmt::format(
"Invalid value for size {} size must be in range "
2120 "[-1, inputDimSize - begin] [-1, {}] inclusive {}",
2122 inputTensorInfo.
GetShape()[i] - begin[i],
2126 if (signedValue == -1)
2128 size[i] = inputTensorInfo.
GetShape()[i] - begin[i];
2132 size[i] =
static_cast<unsigned int>(signedValue);
2138 auto layerName = fmt::format(
"Slice:{}:{}", subgraphIndex, operatorIndex);
2140 IConnectableLayer*
const layer = m_Network->AddSliceLayer(desc, layerName.c_str());
2142 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2143 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
2148 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2149 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2152 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2153 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2156 void TfLiteParserImpl::ParseSoftmax(
size_t subgraphIndex,
size_t operatorIndex)
2158 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2159 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2160 const auto* options = operatorPtr->builtin_options.AsSoftmaxOptions();
2163 desc.
m_Beta = options->beta;
2165 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2167 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2170 auto layerName = fmt::format(
"Softmax:{}:{}", subgraphIndex, operatorIndex);
2171 IConnectableLayer*
const layer = m_Network->AddSoftmaxLayer(desc, layerName.c_str());
2173 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2178 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2179 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2182 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2183 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2186 void TfLiteParserImpl::ParseLogSoftmax(
size_t subgraphIndex,
size_t operatorIndex)
2188 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2192 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2194 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2197 auto layerName = fmt::format(
"LogSoftmax:{}:{}", subgraphIndex, operatorIndex);
2198 IConnectableLayer*
const layer = m_Network->AddLogSoftmaxLayer(desc, layerName.c_str());
2200 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2205 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2206 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2209 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2210 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2213 void TfLiteParserImpl::ParseSpaceToBatchND(
size_t subgraphIndex,
size_t operatorIndex)
2215 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2217 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2220 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2223 armnn::TensorInfo blockShapeTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2226 armnn::TensorInfo padListTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
2229 std::vector<unsigned int> blockShape(blockShapeTensorInfo.
GetNumElements());
2230 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.
GetNumBytes());
2232 std::vector<unsigned int> padListVector(padListTensorInfo.
GetNumElements());
2233 ::memcpy(padListVector.data(), padListBufferPtr->data.data(), padListTensorInfo.
GetNumBytes());
2236 std::vector<std::pair<unsigned int, unsigned int>> padList;
2237 for (
unsigned int i = 0; i < padListTensorInfo.
GetNumElements() / step; ++i)
2239 padList.emplace_back(padListVector[i * step], padListVector[i * step + 1]);
2247 auto layerName = fmt::format(
"SpaceToBatchND:{}:{}", subgraphIndex, operatorIndex);
2249 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2251 IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(desc, layerName.c_str());
2259 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2260 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
2263 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2264 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2266 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2267 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2270 void TfLiteParserImpl::ParseSpaceToDepth(
size_t subgraphIndex,
size_t operatorIndex)
2272 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2281 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2282 const auto* options = operatorPtr->builtin_options.AsSpaceToDepthOptions();
2283 auto blockSize = options->block_size;
2287 fmt::format(
"Operation has invalid block size: {} Block size should be >= 2 {}",
2291 descriptor.
m_BlockSize = armnn::numeric_cast<uint32_t>(blockSize);
2293 auto layerName = fmt::format(
"SpaceToDepth:{}:{}", subgraphIndex, operatorIndex);
2294 IConnectableLayer* layer = m_Network->AddSpaceToDepthLayer(descriptor, layerName.c_str());
2302 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2305 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2306 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2308 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2309 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2316 static const uint32_t dimensionSequence[] = { 0, 1, 2, 3 };
2320 std::stringstream ss;
2321 ss <<
"Input tensor has unexpected number of dimensions:" << inputTensorInfo.
GetNumDimensions()
2322 <<
" shape:" << inputTensorInfo.
GetShape() <<
" "
2327 if (squeezeDims.empty())
2329 squeezeDims.assign(dimensionSequence,
2333 std::vector<uint32_t> outputDims;
2336 bool skipSqueeze = (std::find(squeezeDims.begin(), squeezeDims.end(), i) == squeezeDims.end());
2337 auto currentDimension = inputTensorInfo.
GetShape()[i];
2338 if (skipSqueeze || currentDimension != 1)
2340 outputDims.push_back(currentDimension);
2344 if (outputDims.size() > 4)
2346 std::stringstream ss;
2347 ss <<
"Output tensor has unexpected number of dimensions:" << inputTensorInfo.
GetNumDimensions()
2348 <<
" shape:" << inputTensorInfo.
GetShape() <<
" "
2360 return outTensorInfo;
2363 void TfLiteParserImpl::ParseShape(
size_t subgraphIndex,
size_t operatorIndex)
2365 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2367 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2369 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2372 auto layerName = fmt::format(
"Shape:{}:{}", subgraphIndex, operatorIndex);
2382 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2391 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
2395 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2396 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2398 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2399 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2402 void TfLiteParserImpl::ParseSqueeze(
size_t subgraphIndex,
size_t operatorIndex)
2404 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2406 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2409 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2412 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2413 const auto * options = operatorPtr->builtin_options.AsSqueezeOptions();
2414 auto layerName = fmt::format(
"Squeeze:{}:{}", subgraphIndex, operatorIndex);
2416 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2418 std::vector<uint32_t> squeezeDim;
2421 if (options->squeeze_dims.size() == 1 && options->squeeze_dims[0] < 0)
2424 squeezeDim.push_back(
static_cast<uint32_t
>(dim));
2428 squeezeDim = AsUnsignedVector(options->squeeze_dims);
2433 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
2439 m_TensorInfos[outputTensorIds[0]] = outputTensorInfo;
2441 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
2451 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2452 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2454 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2455 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2458 void TfLiteParserImpl::ParseStridedSlice(
size_t subgraphIndex,
size_t operatorIndex)
2460 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2462 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2465 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2468 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2469 const auto* options = operatorPtr->builtin_options.AsStridedSliceOptions();
2479 armnn::TensorInfo beginTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2483 if (beginBufferPtr->data.data() !=
nullptr)
2485 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.
GetNumBytes());
2489 throw ParseException(
"ParseStridedSlice: Invalid input - the begin vector is null");
2492 armnn::TensorInfo endTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
2496 if (endBufferPtr->data.data() !=
nullptr)
2498 ::memcpy(end.data(), endBufferPtr->data.data(), endTensorInfo.
GetNumBytes());
2502 throw ParseException(
"ParseStridedSlice: Invalid input - the end vector is null");
2505 armnn::TensorInfo strideTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 3);
2510 if (strideBufferPtr->data.data() !=
nullptr)
2512 ::memcpy(stride.data(), strideBufferPtr->data.data(), strideTensorInfo.
GetNumBytes());
2516 throw ParseException(
"ParseStridedSlice: Invalid input - the stride vector is null");
2523 auto layerName = fmt::format(
"StridedSlice:{}:{}", subgraphIndex, operatorIndex);
2524 IConnectableLayer* layer = m_Network->AddStridedSliceLayer(desc, layerName.c_str());
2532 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2535 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2536 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2538 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2539 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2542 void TfLiteParserImpl::ParseSub(
size_t subgraphIndex,
size_t operatorIndex)
2544 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2546 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2547 const auto* options = operatorPtr->builtin_options.AsSubOptions();
2549 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2552 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2555 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2556 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2558 auto layerName = fmt::format(
"Sub:{}:{}", subgraphIndex, operatorIndex);
2559 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Sub, layerName.c_str());
2567 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
2570 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2571 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2574 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2577 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2578 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2581 void TfLiteParserImpl::ParseDiv(
size_t subgraphIndex,
size_t operatorIndex)
2583 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2585 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2586 const auto* options = operatorPtr->builtin_options.AsDivOptions();
2588 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2591 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2594 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2595 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2597 auto layerName = fmt::format(
"Div:{}:{}", subgraphIndex, operatorIndex);
2598 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Div, layerName.c_str());
2606 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
2609 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2610 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2613 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2616 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2617 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2620 void TfLiteParserImpl::ParseFloorDiv(
size_t subgraphIndex,
size_t operatorIndex)
2622 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2624 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2627 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2630 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2631 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2633 auto layerName = fmt::format(
"Div:{}:{}", subgraphIndex, operatorIndex);
2634 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Div, layerName.c_str());
2642 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
2645 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2646 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2647 layer = AddFusedFloorLayer(layer, 0);
2649 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2650 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2653 void TfLiteParserImpl::ParseAdd(
size_t subgraphIndex,
size_t operatorIndex)
2655 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2657 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2658 const auto* options = operatorPtr->builtin_options.AsAddOptions();
2660 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2663 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2666 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2667 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2669 auto layerName = fmt::format(
"Add:{}:{}", subgraphIndex, operatorIndex);
2670 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Add, layerName.c_str());
2678 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
2681 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2682 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2685 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2688 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2689 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2692 void TfLiteParserImpl::ParseMul(
size_t subgraphIndex,
size_t operatorIndex)
2694 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2696 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2697 const auto* options = operatorPtr->builtin_options.AsMulOptions();
2699 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2702 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2705 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2706 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2708 auto layerName = fmt::format(
"Mul:{}:{}", subgraphIndex, operatorIndex);
2709 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Mul, layerName.c_str());
2717 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
2720 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2721 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2724 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2727 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2728 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2731 void TfLiteParserImpl::ParseMean(
size_t subgraphIndex,
size_t operatorIndex)
2733 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2735 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2737 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2740 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2741 TensorInfo dimTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2746 if (axisBufferPtr !=
nullptr)
2749 ::memcpy(axisData.data(), axisBufferPtr->data.data(), dimTensorInfo.
GetNumBytes());
2753 std::set<unsigned int> uniqueAxis;
2754 std::transform(axisData.begin(),
2756 std::inserter(uniqueAxis, uniqueAxis.begin()),
2757 [rank](
int i)->unsigned
int{
2758 return static_cast<uint32_t>(((i + rank) % rank)); });
2759 desc.
m_Axis.assign(uniqueAxis.begin(), uniqueAxis.end());
2765 desc.
m_Axis.push_back(i);
2773 auto layerName = fmt::format(
"Mean:{}:{}", subgraphIndex, operatorIndex);
2782 outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2785 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2786 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2788 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2789 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2792 void TfLiteParserImpl::ParsePad(
size_t subgraphIndex,
size_t operatorIndex)
2794 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2801 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2802 armnn::TensorInfo padTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2804 std::vector<unsigned int> padBuffer = GetUIntBuffer(padTensorInfo, m_Model, inputs[1]->buffer);
2808 auto opcode = GetOpCode(m_Model, subgraphIndex, operatorIndex);
2810 if (opcode == tflite::BuiltinOperator_PAD)
2819 else if (opcode == tflite::BuiltinOperator_PADV2)
2823 armnn::TensorInfo padValueTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
2832 if (padValueBufferPtr->data.size() > 0)
2838 std::vector<float> padValueBuffer(padValueTensorInfo.
GetNumElements());
2839 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2845 std::vector<uint8_t> padValueBuffer(padValueTensorInfo.
GetNumElements());
2846 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2847 desc.
m_PadValue = armnn::Dequantize<uint8_t>(padValueBuffer[0],
2855 std::vector<int8_t> padValueBuffer(padValueTensorInfo.
GetNumElements());
2856 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2857 desc.
m_PadValue = armnn::Dequantize<int8_t>(padValueBuffer[0],
2871 for (
unsigned int i = 0; i < padTensorInfo.
GetNumElements() / step; ++i)
2873 desc.
m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
2876 auto layerName = (opcode == tflite::BuiltinOperator_PAD) ? fmt::format(
"Pad:{}:{}", subgraphIndex, operatorIndex)
2877 : fmt::format(
"PadV2:{}:{}", subgraphIndex, operatorIndex);
2887 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2890 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2891 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2893 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2894 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2897 void TfLiteParserImpl::ParseMirrorPad(
size_t subgraphIndex,
size_t operatorIndex)
2899 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2907 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2909 armnn::TensorInfo padTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2912 std::vector<unsigned int> padBuffer(padTensorInfo.
GetNumElements());
2913 ::memcpy(padBuffer.data(), bufferPtr->data.data(), padTensorInfo.
GetNumBytes());
2917 for (
unsigned int i = 0; i < padTensorInfo.
GetNumElements() / step; ++i)
2919 desc.
m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
2922 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2923 const auto* options = operatorPtr->builtin_options.AsMirrorPadOptions();
2925 if (options->mode == tflite::MirrorPadMode_REFLECT)
2929 else if (options->mode == tflite::MirrorPadMode_SYMMETRIC)
2940 auto inputShape = inputTensorInfo.
GetShape();
2943 const unsigned int isReflect =
static_cast<unsigned int>(desc.
m_PaddingMode == PaddingMode::Reflect);
2944 for(
unsigned int i = 0; i < padList.size(); ++i)
2946 if(padList.at(i).first > (inputShape[i] - isReflect) ||
2947 padList.at(i).second > (inputShape[i] - isReflect))
2950 "equal (Symmetric) to the dimension size.");
2954 auto layerName = fmt::format(
"MirrorPad:{}:{}", subgraphIndex, operatorIndex);
2964 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2967 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2968 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2970 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2971 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2974 void TfLiteParserImpl::ParsePrelu(
size_t subgraphIndex,
size_t operatorIndex)
2976 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2978 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2981 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2984 auto layerName = fmt::format(
"Prelu:{}:{}", subgraphIndex, operatorIndex);
2986 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2987 armnn::TensorInfo alphaTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2997 if (IsConstTensor(inputs[1]))
2999 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3001 RegisterConsumerOfTensor(subgraphIndex, inputTensorIndexes[0], slot);
3003 auto alphaTensorAndData = CreateConstTensorNonPermuted(inputs[1], alphaTensorInfo,
3005 std::string constLayerName = fmt::format(
"Constant:{}", inputs[1]->name);
3007 m_Network->AddConstantLayer(alphaTensorAndData.first, constLayerName.c_str());
3017 RegisterOutputSlots(subgraphIndex,
3018 VIRTUAL_OPERATOR_ID,
3020 { inputTensorIndexes[1] });
3024 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3025 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIndexes);
3028 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
3029 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
3032 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3033 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3036 void TfLiteParserImpl::ParseQuantize(
size_t subgraphIndex,
size_t operatorIndex)
3038 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3040 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3043 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3046 auto layerName = fmt::format(
"Quantize:{}:{}", subgraphIndex, operatorIndex);
3056 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
3059 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3060 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3062 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3063 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3066 void TfLiteParserImpl::ParseRelu(
size_t subgraphIndex,
size_t operatorIndex)
3068 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::ReLu);
3071 void TfLiteParserImpl::ParseRelu6(
size_t subgraphIndex,
size_t operatorIndex)
3073 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::BoundedReLu);
3076 void TfLiteParserImpl::ParseLeakyRelu(
size_t subgraphIndex,
size_t operatorIndex)
3078 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::LeakyReLu);
3081 void TfLiteParserImpl::ParseLogistic(
size_t subgraphIndex,
size_t operatorIndex)
3083 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::Sigmoid);
3086 void TfLiteParserImpl::ParseTanH(
size_t subgraphIndex,
size_t operatorIndex)
3088 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::TanH);
3091 void TfLiteParserImpl::ParseElu(
size_t subgraphIndex,
size_t operatorIndex)
3093 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::Elu);
3096 void TfLiteParserImpl::ParseHardSwish(
size_t subgraphIndex,
size_t operatorIndex)
3098 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::HardSwish);
3101 void TfLiteParserImpl::ParseActivation(
size_t subgraphIndex,
size_t operatorIndex,
ActivationFunction activationType)
3103 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3104 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3107 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3110 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3113 auto layerName = fmt::format(
"Activation:");
3117 switch (activationType)
3119 case ActivationFunction::ReLu:
3121 layerName += fmt::format(
"RELU:{}:{}", subgraphIndex, operatorIndex);
3124 case ActivationFunction::BoundedReLu:
3126 layerName += fmt::format(
"RELU6:{}:{}", subgraphIndex, operatorIndex);
3127 activationDesc.
m_A = 6.0f;
3128 activationDesc.
m_B = 0.0f;
3131 case ActivationFunction::Sigmoid:
3133 layerName += fmt::format(
"SIGMOID:{}:{}", subgraphIndex, operatorIndex);
3136 case ActivationFunction::TanH:
3138 layerName += fmt::format(
"TANH:{}:{}", subgraphIndex, operatorIndex);
3139 activationDesc.
m_A = 1.0f;
3140 activationDesc.
m_B = 1.0f;
3143 case ActivationFunction::LeakyReLu:
3145 layerName += fmt::format(
"LEAKYRELU:{}:{}", subgraphIndex, operatorIndex);
3146 const auto* options = operatorPtr->builtin_options.AsLeakyReluOptions();
3147 activationDesc.
m_A = options->alpha;
3150 case ActivationFunction::Elu:
3152 layerName += fmt::format(
"ELU:{}:{}", subgraphIndex, operatorIndex);
3153 activationDesc.
m_A = 1.0f;
3156 case ActivationFunction::HardSwish:
3158 layerName += fmt::format(
"HARDSWISH:{}:{}", subgraphIndex, operatorIndex);
3164 fmt::format(
"Unexpected ActivationFunction[{}] when creating layerName {} ",
3169 IConnectableLayer*
const layer = m_Network->AddActivationLayer(activationDesc, layerName.c_str());
3171 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
3176 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3177 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3180 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3181 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3184 const std::vector<int32_t>& targetDimsIn)
3186 std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
3187 const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
3189 if (stretchDim != targetDimsIn.end())
3191 if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
3194 fmt::format(
"At most one component of shape can be -1 {}",
CHECK_LOCATION().AsString()));
3197 auto targetNumElements =
3198 armnn::numeric_cast<unsigned int>(
3199 std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
3201 auto stretchIndex =
static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
3202 outputDims[stretchIndex] = inputTensorInfo.
GetNumElements() / targetNumElements;
3213 void TfLiteParserImpl::ParseReshape(
size_t subgraphIndex,
size_t operatorIndex)
3215 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3217 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3219 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3222 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3223 const auto* options = operatorPtr->builtin_options.AsReshapeOptions();
3224 auto layerName = fmt::format(
"Reshape:{}:{}", subgraphIndex, operatorIndex);
3226 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3228 CheckMatchingQuantization(inputTensorInfo, actualOutputTensorInfo, layerName,
"Input 0",
"Output 0");
3234 std::vector<int32_t> targetShape;
3235 bool targetShapeFound =
false;
3237 if (options !=
nullptr)
3240 if (options->new_shape.empty() ==
false)
3242 targetShape = options->new_shape;
3243 targetShapeFound =
true;
3248 if (!targetShapeFound)
3251 if (inputs.size() > 1 && inputs[1] !=
nullptr)
3253 if (inputs[1]->is_variable)
3258 if (inputs[1]->shape.size() != 1)
3263 if (inputs[1]->type != tflite::TensorType_INT32)
3269 auto bufferPtr =
GetBuffer(m_Model, inputs[1]->buffer);
3270 auto values =
reinterpret_cast<const int32_t*
>(bufferPtr->data.data());
3273 for (
int i = 0; i < inputs[1]->shape[0]; ++i)
3275 targetShape.push_back(values[i]);
3287 for (
unsigned int i = 0; i < actualOutputTensorInfo.
GetShape().GetNumDimensions(); ++i)
3289 targetShape.push_back(actualOutputTensorInfo.
GetShape()[i]);
3293 else if (reshapeShapes[0] > 2)
3295 throw ParseException(fmt::format(
"Invalid input shape '{}' in Reshape layer '{}' {}. "
3296 "When inferring during runtime, the parser only supports "
3297 "shape (batch, -1) or (-1) for target shape input.",
3304 const int32_t numInputElements = inputTensorInfo.
GetNumElements();
3305 const int32_t inputTensorShape = inputTensorInfo.
GetShape()[0];
3306 if (reshapeShapes[0] == 1)
3308 targetShape = {numInputElements};
3310 else if (reshapeShapes[0] == 2)
3312 targetShape = {inputTensorShape, numInputElements / inputTensorShape};
3316 catch (
const std::exception& exc)
3319 "Reshape operation. Reshape operator target shape input buffer data "
3320 "is null. " << exc.what());
3327 "At least one method required");
3340 if (inputs.size() > 1 && !
CheckShape(reshapeOutputTensorShape, outputs[0]->shape))
3344 std::vector<int32_t> secondaryOutputTargetShape = outputs[0]->shape_signature;
3351 if (!
CheckShape(reshapeOutputTensorShape, secondaryReshapeOutputTensorInfo.
GetShape()))
3353 std::stringstream ss;
3354 ss <<
"New shape defined in reshape parameters "
3355 << reshapeOutputTensorShape
3356 <<
" does not equal output shape "
3357 << actualOutputTensorInfo.
GetShape()
3367 m_TensorInfos[outputTensorIds[0]] = reshapeOutputTensorInfo;
3369 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
3379 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3380 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3382 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3383 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3386 void TfLiteParserImpl::ParseResizeBilinear(
size_t subgraphIndex,
size_t operatorIndex)
3388 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::Bilinear);
3391 void TfLiteParserImpl::ParseResizeNearestNeighbor(
size_t subgraphIndex,
size_t operatorIndex)
3393 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::NearestNeighbor);
3396 void TfLiteParserImpl::ParseResize(
size_t subgraphIndex,
size_t operatorIndex,
ResizeMethod resizeMethod)
3398 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3400 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3403 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3406 armnn::TensorInfo sizeTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
3409 std::vector<int32_t> sizeTensorData(sizeTensorInfo.
GetNumElements());
3412 ::memcpy(sizeTensorData.data(), sizeBufferPtr->data.data(), sizeTensorInfo.
GetNumBytes());
3417 desc.
m_TargetWidth =
static_cast<uint32_t
> (sizeTensorData[1]);
3420 auto layerName = fmt::format(
"Resize:");
3422 switch (resizeMethod)
3424 case ResizeMethod::Bilinear:
3426 layerName += fmt::format(
"BILINEAR:{}:{}", subgraphIndex, operatorIndex);
3428 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3429 const auto * options = operatorPtr->builtin_options.AsResizeBilinearOptions();
3434 case ResizeMethod::NearestNeighbor:
3436 layerName += fmt::format(
"NEARESTNEIGHBOR:{}:{}", subgraphIndex, operatorIndex);
3442 fmt::format(
"Unexpected ResizeMethod[{}] when creating layerName {} ",
3447 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3457 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
3458 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
3461 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3462 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3464 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3465 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3468 void TfLiteParserImpl::ParseReverseV2(
size_t subgraphIndex,
size_t operatorIndex)
3470 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3472 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3475 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3478 auto layerName = fmt::format(
"ReverseV2:{}:{}", subgraphIndex, operatorIndex);
3489 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3490 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3492 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3493 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3496 void TfLiteParserImpl::ParseTile(
size_t subgraphIndex,
size_t operatorIndex)
3498 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3500 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3503 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3510 auto layerName = fmt::format(
"Tile:{}:{}", subgraphIndex, operatorIndex);
3515 if (multiplesBufferPtr !=
nullptr)
3517 std::vector<int32_t> multiplesData(multiplesTensorInfo.
GetNumElements());
3518 ::memcpy(multiplesData.data(), multiplesBufferPtr->data.data(), multiplesTensorInfo.
GetNumBytes());
3519 descriptor.
m_Multiples.assign(multiplesData.begin(), multiplesData.end());
3526 IConnectableLayer* layer = m_Network->AddTileLayer(descriptor, layerName.c_str());
3531 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3532 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3534 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3535 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3538 void TfLiteParserImpl::ParseConcatenation(
size_t subgraphIndex,
size_t operatorIndex)
3540 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3542 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3543 const auto* options = operatorPtr->builtin_options.AsConcatenationOptions();
3547 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3548 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3553 unsigned int numConcatView =
static_cast<unsigned int>(inputs.size());
3554 uint32_t inputRank = InputTensorInfo(subgraphIndex, operatorIndex, 0).
GetNumDimensions();
3556 const unsigned int concatDimInput =
static_cast<unsigned int>(
3557 (
static_cast<int>(inputRank) + options->axis) %
static_cast<int>(inputRank));
3559 OriginsDescriptor concatDescriptor(
static_cast<uint32_t
>(numConcatView), inputRank);
3560 concatDescriptor.SetConcatAxis(concatDimInput);
3561 unsigned int mergeDimOrigin = 0;
3563 for (
unsigned int viewIndex = 0; viewIndex < numConcatView; ++viewIndex)
3565 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, viewIndex);
3569 inputTensorInfo, concatDescriptor, concatDimInput, viewIndex, mergeDimOrigin);
3572 auto layerName = fmt::format(
"Concatenation:{}:{}", subgraphIndex, operatorIndex);
3574 IConnectableLayer* layer = m_Network->AddConcatLayer(concatDescriptor, layerName.c_str());
3582 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {});
3585 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3586 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
3589 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
3591 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3592 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3595 void TfLiteParserImpl::ParseFullyConnected(
size_t subgraphIndex,
size_t operatorIndex)
3597 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3599 const auto& operatorRfr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3600 const auto options = operatorRfr->builtin_options.AsFullyConnectedOptions();
3608 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3609 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3612 armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
3615 int32_t weightsDimension =
static_cast<int32_t
>(filterTensorInfo.
GetNumDimensions());
3616 if (weightsDimension != 2)
3619 fmt::format(
"Dimension {} for Fully Connected weights is not supported by Armnn. "
3626 auto layerName = fmt::format(
"FullyConnected:{}:{}", subgraphIndex, operatorIndex);
3628 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3630 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0]};
3631 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3636 tensorIndexesToRegister.emplace_back(inputTensorIndexes[1]);
3638 if (ShouldConstantTensorBeConverted(inputs[1], inputTensorInfo.
GetDataType(), filterTensorInfo.
GetDataType()))
3640 m_ConstantsToDequantize.emplace_back(inputs[1]->buffer);
3643 if (inputs.size() == 3)
3646 armnn::TensorInfo biasTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
3649 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
3651 if (ShouldConstantTensorBeConverted(inputs[2], inputTensorInfo.
GetDataType(), biasTensorInfo.
GetDataType()))
3653 m_ConstantsToDequantize.emplace_back(inputs[2]->buffer);
3658 layer = m_Network->AddFullyConnectedLayer(desc, layerName.c_str());
3666 unsigned int startingSlotIndex = 0;
3673 std::vector<unsigned int> reshapedDimensions(2);
3674 reshapedDimensions[1] = filterTensorInfo.
GetShape()[1];
3675 reshapedDimensions[0] = inputTensorInfo.
GetNumElements() / reshapedDimensions[1];
3677 if (inputTensorInfo.
GetNumElements() % reshapedDimensions[1] != 0)
3680 fmt::format(
"Failed to deduce input tensor shape from filter size {} {}",
3681 reshapedDimensions[1],
3685 armnn::TensorInfo reshapedTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3687 inputTensorInfo = reshapedTensorInfo;
3689 std::string reshapeLayerName = fmt::format(
"Reshape_for:{}", layer->
GetName());
3693 reshapeLayerName.c_str());
3698 RegisterInputSlots(subgraphIndex, operatorIndex, reshapeLayer, {inputTensorIndexes[0]});
3700 tensorIndexesToRegister.erase(tensorIndexesToRegister.begin());
3701 startingSlotIndex = 1;
3704 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister, startingSlotIndex);
3706 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromShapes(subgraphIndex, operatorIndex, layer, 0,
3715 std::vector<unsigned int> reshapedDimensions(2);
3716 reshapedDimensions[1] = filterTensorInfo.
GetShape()[0];
3717 reshapedDimensions[0] = outputTensorInfo.
GetNumElements() / reshapedDimensions[1];
3719 if (outputTensorInfo.
GetNumElements() % reshapedDimensions[1] != 0)
3722 fmt::format(
"Failed to deduce output tensor shape from filter size {} {}",
3723 reshapedDimensions[1],
3729 std::string reshapeLayerName = fmt::format(
"ExpandDims:{}:{}", subgraphIndex, operatorIndex);
3730 layer = AddReshapeLayer(layer, 0, reshapeLayerName, outputTensorInfo);
3735 options->fused_activation_function);
3738 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3739 RegisterOutputSlots(subgraphIndex, operatorIndex, fusedActivationLayer, {outputTensorIndexes[0]});
3744 void TfLiteParserImpl::ParseDetectionPostProcess(
size_t subgraphIndex,
size_t operatorIndex)
3746 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3748 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3750 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3751 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3755 auto custom_options = operatorPtr->custom_options;
3756 const flexbuffers::Map& m = flexbuffers::GetRoot(custom_options.data(), custom_options.size()).AsMap();
3765 desc.
m_ScaleH = m[
"h_scale"].AsFloat();
3766 desc.
m_ScaleW = m[
"w_scale"].AsFloat();
3767 desc.
m_ScaleX = m[
"x_scale"].AsFloat();
3768 desc.
m_ScaleY = m[
"y_scale"].AsFloat();
3770 if (!(m[
"use_regular_nms"].IsNull()))
3774 if (!(m[
"detections_per_class"].IsNull()))
3782 "must be positive and less than or equal to 1.");
3785 armnn::TensorInfo anchorTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
3786 auto anchorTensorAndData = CreateConstTensorNonPermuted(inputs[2], anchorTensorInfo);
3788 auto layerName = fmt::format(
"DetectionPostProcess:{}:{}", subgraphIndex, operatorIndex);
3789 IConnectableLayer* layer = m_Network->AddDetectionPostProcessLayer(desc, anchorTensorAndData,
3801 m_OverriddenOutputShapes.push_back({ 1, numDetectedBox, 4 });
3802 m_OverriddenOutputShapes.push_back({ 1, numDetectedBox });
3803 m_OverriddenOutputShapes.push_back({ 1, numDetectedBox });
3804 m_OverriddenOutputShapes.push_back({ 1 });
3806 for (
unsigned int i = 0 ; i < outputs.size() ; ++i)
3814 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3815 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3818 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3819 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0],
3820 outputTensorIndexes[1],
3821 outputTensorIndexes[2],
3822 outputTensorIndexes[3]});
3826 void TfLiteParserImpl::ParsePack(
size_t subgraphIndex,
size_t operatorIndex)
3828 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3830 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3831 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3834 if (inputs.size() < 1)
3839 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3840 const auto* options = operatorPtr->builtin_options.AsPackOptions();
3843 desc.
m_Axis =
static_cast<uint32_t
>(options->axis);
3844 desc.
m_NumInputs =
static_cast<uint32_t
>(inputs.size());
3847 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3850 auto layerName = fmt::format(
"Pack:{}:{}", subgraphIndex, operatorIndex);
3859 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {});
3862 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3863 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
3865 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3866 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3869 void TfLiteParserImpl::ParseUnidirectionalSequenceLSTM(
size_t subgraphIndex,
size_t operatorIndex)
3871 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3873 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3874 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3876 if (inputs.size() < 2)
3878 throw ParseException(
"UnidirectionalSequenceLSTM must have at least 2 input.");
3881 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3882 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
3883 const auto nodeParams = operatorPtr->builtin_options.AsUnidirectionalSequenceLSTMOptions();
3885 auto inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3893 if (IsOptionalOperandPresent(operatorPtr->inputs[1]))
3895 params.
m_InputToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[1]].get(),
3896 inputTensorInfo).first;
3900 inputTensorInfo).first;
3901 params.
m_InputToCellWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[3]].get(),
3902 inputTensorInfo).first;
3904 inputTensorInfo).first;
3907 if (IsOptionalOperandPresent(operatorPtr->inputs[5]))
3910 inputTensorInfo).first;
3914 inputTensorInfo).first;
3916 inputTensorInfo).first;
3918 inputTensorInfo).first;
3921 if (IsOptionalOperandPresent(operatorPtr->inputs[9]))
3923 params.
m_CellToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[9]].get(),
3924 inputTensorInfo).first;
3927 if (IsOptionalOperandPresent(operatorPtr->inputs[10]))
3929 params.
m_CellToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[10]].get(),
3930 inputTensorInfo).first;
3933 if (IsOptionalOperandPresent(operatorPtr->inputs[11]))
3935 params.
m_CellToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[11]].get(),
3936 inputTensorInfo).first;
3940 if (IsOptionalOperandPresent(operatorPtr->inputs[12]))
3942 params.
m_InputGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[12]].get(),
3943 inputTensorInfo).first;
3946 params.
m_ForgetGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[13]].get(),
3947 inputTensorInfo).first;
3948 params.
m_CellBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[14]].get(),
3949 inputTensorInfo).first;
3950 params.
m_OutputGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[15]].get(),
3951 inputTensorInfo).first;
3954 if (IsOptionalOperandPresent(operatorPtr->inputs[16]))
3956 params.
m_ProjectionWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[16]].get(),
3957 inputTensorInfo).first;
3960 if (IsOptionalOperandPresent(operatorPtr->inputs[17]))
3962 params.
m_ProjectionBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[17]].get(),
3963 inputTensorInfo).first;
3968 m_ConstantsToBeCreated.push_back(operatorPtr->inputs[18]);
3970 m_ConstantsToBeCreated.push_back(operatorPtr->inputs[19]);
3973 if (inputs.size() >= 21 && IsOptionalOperandPresent(operatorPtr->inputs[20]))
3976 inputTensorInfo).first;
3979 if (inputs.size() >= 22 && IsOptionalOperandPresent(operatorPtr->inputs[21]))
3982 inputTensorInfo).first;
3985 if (inputs.size() >= 23 && IsOptionalOperandPresent(operatorPtr->inputs[22]))
3987 params.
m_CellLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[22]].get(),
3988 inputTensorInfo).first;
3991 if (inputs.size() >= 24 && IsOptionalOperandPresent(operatorPtr->inputs[23]))
3994 inputTensorInfo).first;
4015 auto inputIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[0]].get(),
4016 inputTensorInfo).first;
4017 auto inputIntermediateTensorInfo = inputIntermediate->GetInfo();
4020 auto forgetIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[1]].get(),
4021 inputTensorInfo).first;
4022 auto forgetIntermediateTensorInfo = forgetIntermediate->GetInfo();
4025 auto cellIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[2]].get(),
4026 inputTensorInfo).first;
4027 auto cellIntermediateTensorInfo = cellIntermediate->GetInfo();
4030 auto outputIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[3]].get(),
4031 inputTensorInfo).first;
4032 auto outputIntermediateTensorInfo = outputIntermediate->GetInfo();
4037 float defaultIntermediate = std::pow(2, -12);
4044 if (operatorPtr->intermediates.size() > 4)
4046 auto hiddentensor = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[4]].get(),
4047 inputTensorInfo).first;
4052 unsigned int batchSize = inputTensorInfo.
GetShape()[0];
4053 unsigned int outputSize = outputTensorInfo.
GetShape()[2];
4054 unsigned int numUnits = cellStateInInfo.
GetShape()[1];
4060 armnn::TensorInfo scratchBufferTensorInfo({batchSize, numUnits * 3}, dataType, qScale, qOffset);
4063 scratchBufferTensorInfo =
armnn::TensorInfo({batchSize, numUnits * 4}, dataType, qScale, qOffset);
4069 armnn::TensorInfo outputStateOutTensorInfo({batchSize, outputSize}, dataType, qScale, qOffset);
4119 auto layerName = fmt::format(
"UnidirectionalSequenceLSTM:{}:{}", subgraphIndex, operatorIndex);
4130 auto inputTensorIndexes = AsUnsignedVector({operatorPtr->inputs[0],
4131 operatorPtr->inputs[18],
4132 operatorPtr->inputs[19]});
4133 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0],
4134 inputTensorIndexes[1],
4135 inputTensorIndexes[2]});
4137 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4143 unsigned int tensorIndex = outputTensorIndexes[0];
4145 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
4148 void TfLiteParserImpl::ParseUnpack(
size_t subgraphIndex,
size_t operatorIndex)
4150 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4152 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4153 const auto* options = operatorPtr->builtin_options.AsUnpackOptions();
4158 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
4161 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4166 fmt::format(
"The unpack axis: {} cannot be greater than or equal to "
4167 "the number of input dimension {} {}",
4177 unpackNum = inputTensorInfo.
GetShape()[unpackAxis];
4183 throw ParseException(
"Number to unpack must greater than zero.");
4186 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
4190 std::vector<unsigned int> unpackDimSizes(inputDimSize);
4193 for (
unsigned int i = 0; i < inputDimSize; ++i)
4195 unpackDimSizes[i] = inputTensorInfo.
GetShape()[i];
4198 if (unpackDimSizes[unpackAxis] != unpackNum)
4200 throw ParseException(
"Number to unpack must be the same as length of the dimension to "
4204 unpackDimSizes[unpackAxis] /= unpackNum;
4206 SplitterDescriptor splitDesc(unpackNum,
static_cast<unsigned int>(unpackDimSizes.size()));
4207 for (
unsigned int j = 0; j < unpackNum; ++j)
4210 for (
unsigned int dimIdx = 0; dimIdx < unpackDimSizes.size(); ++dimIdx)
4212 splitDesc.SetViewSize(j, dimIdx, unpackDimSizes[dimIdx]);
4214 splitDesc.SetViewOriginCoord(j, unpackAxis, unpackDimSizes[unpackAxis] * j);
4217 auto layerName = fmt::format(
"Unpack:{}:{}", subgraphIndex, operatorIndex);
4218 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
4227 unpackDimSizes.data());
4229 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4230 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4232 std::vector<unsigned int> reshapeDims;
4233 for (
unsigned int axis = 0; axis < splitOutShape.
GetNumDimensions(); ++axis)
4235 if (axis != unpackAxis)
4237 reshapeDims.push_back(splitOutShape[axis]);
4247 std::string reshapeLayerName = fmt::format(
"Reshape_for:{}", layer->
GetName());
4262 RegisterProducerOfTensor(subgraphIndex, reshapedOutputId, slot);
4266 void TfLiteParserImpl::ParseSplit(
size_t subgraphIndex,
size_t operatorIndex)
4268 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4270 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4271 const auto* options = operatorPtr->builtin_options.AsSplitOptions();
4278 throw ParseException(
"Number to splits must greater than zero.");
4281 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
4283 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
4286 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4287 armnn::TensorInfo axisTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4291 throw ParseException(fmt::format(
"Axis tensor can only have 1 element {}",
4296 if (axisBufferPtr ==
nullptr)
4299 fmt::format(
"Operation has invalid inputs. Failed to read axis. {}",
4304 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.
GetNumBytes());
4305 int32_t axis = axisData[0];
4307 auto inputDimensions =
static_cast<int32_t
>(inputTensorInfo.
GetNumDimensions());
4308 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4314 fmt::format(
"Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
4325 fmt::format(
"The number of dimensions: {} for input tensors of the split op cannot be greater than {} {}",
4331 std::vector<unsigned int> splitterDimSizes(inputDimSize);
4334 for (
unsigned int i = 0; i < inputDimSize; ++i)
4336 splitterDimSizes[i] = inputTensorInfo.
GetShape()[i];
4339 if (splitterDimSizes[splitDim] % numSplits != 0)
4341 throw ParseException(
"Number of splits must evenly divide the dimension");
4343 splitterDimSizes[splitDim] /= numSplits;
4346 for (
unsigned int j = 0; j < numSplits; ++j)
4349 for (
unsigned int dimIdx = 0; dimIdx < splitterDimSizes.size(); ++dimIdx)
4351 splitDesc.SetViewSize(j, dimIdx, splitterDimSizes[dimIdx]);
4353 splitDesc.SetViewOriginCoord(j, splitDim, splitterDimSizes[splitDim] * j);
4356 auto layerName = fmt::format(
"Split:{}:{}", subgraphIndex, operatorIndex);
4357 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
4365 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4366 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[1]});
4374 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4375 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4380 int numDims = armnn::numeric_cast<int>(numDimsIn);
4381 int v = idx < 0 ? numDims + idx : idx;
4383 if (v < 0 || v > numDims)
4388 return static_cast<unsigned int>(v);
4391 void TfLiteParserImpl::ParseSplitV(
size_t subgraphIndex,
size_t operatorIndex)
4393 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4395 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4396 const auto* options = operatorPtr->builtin_options.AsSplitVOptions();
4398 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
4401 auto& inputTensor = inputs[0];
4402 auto& splitsTensor = inputs[1];
4403 auto& axisTensor = inputs[2];
4411 throw ParseException(fmt::format(
"Axis tensor can only have 1 element {}",
4420 fmt::format(
"The number of dimensions: {} for input tensors of the "
4421 "SplitV op cannot be greater than {} {}",
4429 if (axisBufferPtr ==
nullptr)
4432 fmt::format(
"Operation has invalid inputs. Failed to read axis. {}",
4437 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.
GetNumBytes());
4438 int32_t axis = axisData[0];
4440 auto inputDimensions =
static_cast<int32_t
>(inputTensorInfo.
GetNumDimensions());
4441 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4447 fmt::format(
"Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
4455 unsigned int numSplits{0};
4471 std::vector<int> splitsData(numSplits);
4473 ::memcpy(splitsData.data(), splitsBufferPtr->data.data(), splitsInfo.
GetNumBytes());
4475 unsigned int idx = 0;
4477 unsigned int inferIdx{0};
4479 for (
auto split : splitsData)
4493 if (numInferred == 0)
4495 if (splitSum != armnn::numeric_cast<int>(inputTensorInfo.
GetShape()[splitDim]))
4497 throw ParseException(
"SplitV split_sizes does not sum to the dimension of value along split_dim.");
4500 else if (numInferred == 1)
4502 splitsData[inferIdx] = armnn::numeric_cast<int>(inputTensorInfo.
GetShape()[splitDim]) - splitSum;
4506 throw ParseException(
"Cannot infer split size for more than one split");
4510 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
4515 unsigned int accumSplit = 0;
4516 for (
unsigned int j = 0; j < numSplits; ++j)
4518 unsigned int splitSize = armnn::numeric_cast<unsigned int>(splitsData[j]);
4521 for (
unsigned int dimIdx = 0; dimIdx < inputTensorInfo.
GetNumDimensions(); ++dimIdx)
4523 unsigned int dimSize = inputTensorInfo.
GetShape()[dimIdx];
4524 if (dimIdx == splitDim)
4526 dimSize = splitSize;
4528 splitDesc.SetViewSize(j, dimIdx, dimSize);
4531 splitDesc.SetViewOriginCoord(j, splitDim, accumSplit);
4532 accumSplit += splitSize;
4535 auto layerName = fmt::format(
"SplitV:{}:{}", subgraphIndex, operatorIndex);
4536 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
4544 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4545 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4553 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4554 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4557 void TfLiteParserImpl::ParseArgMin(
size_t subgraphIndex,
size_t operatorIndex)
4562 void TfLiteParserImpl::ParseArgMax(
size_t subgraphIndex,
size_t operatorIndex)
4567 void TfLiteParserImpl::ParseArgMinMax(
size_t subgraphIndex,
size_t operatorIndex,
ArgMinMaxFunction argMinMaxFunction)
4569 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4570 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
4573 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
4576 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4577 armnn::TensorInfo axisTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4582 throw ParseException(fmt::format(
"Axis tensor can only have 1 element {}",
4592 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
4598 if (axisBufferPtr ==
nullptr)
4601 fmt::format(
"Operation has invalid inputs. Failed to read axis. {}",
4606 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.
GetNumBytes());
4607 int32_t axis = axisData.front();
4609 auto inputDimensions =
static_cast<int32_t
>(inputTensorInfo.
GetNumDimensions());
4610 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4616 fmt::format(
"Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
4626 auto layerName = argMinMaxFunction == ArgMinMaxFunction::Max ?
"ArgMax:{}:{}" :
"ArgMin:{}:{}";
4627 auto layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4628 IConnectableLayer *layer = m_Network->AddArgMinMaxLayer(desc, layerNameFormatted.c_str());
4636 outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
4640 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4641 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4644 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4645 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4648 void TfLiteParserImpl::ParseGather(
size_t subgraphIndex,
size_t operatorIndex)
4650 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4657 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4658 armnn::TensorInfo indicesTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4663 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4664 const auto* options = operatorPtr->builtin_options.AsGatherOptions();
4665 auto axis = options->axis;
4667 auto layerName = fmt::format(
"Gather:{}:{}", subgraphIndex, operatorIndex);
4669 auto inputDimensions =
static_cast<int32_t
>(inputTensorInfo.
GetNumDimensions());
4672 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4675 fmt::format(
"Operation has invalid axis: {} It is out of bounds [ -{}, {} ) {}",
4677 inputDimensions, inputDimensions,
4680 if (outputDimensions !=
static_cast<unsigned int>(inputDimensions) + indicesDimensions - 1)
4683 fmt::format(
"Operation has invalid output dimensions: {} Output must be an ({} + {} - 1) -D tensor {}",
4685 inputDimensions, indicesDimensions,
4689 gatherDescriptor.
m_Axis = axis;
4691 IConnectableLayer* layer = m_Network->AddGatherLayer(gatherDescriptor, layerName.c_str());
4699 outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
4702 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4703 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4705 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4706 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4709 void TfLiteParserImpl::ParseGatherNd(
size_t subgraphIndex,
size_t operatorIndex)
4711 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4718 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4719 armnn::TensorInfo indicesTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4721 auto layerName = fmt::format(
"GatherNd:{}:{}", subgraphIndex, operatorIndex);
4730 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
4733 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4734 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4736 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4737 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4740 void TfLiteParserImpl::ParseDepthToSpace(
size_t subgraphIndex,
size_t operatorIndex)
4742 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4751 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4752 const auto* options = operatorPtr->builtin_options.AsDepthToSpaceOptions();
4753 auto blockSize = options->block_size;
4757 fmt::format(
"Operation has invalid block size: {} Block size should be >= 2 {}",
4761 descriptor.
m_BlockSize = armnn::numeric_cast<uint32_t>(blockSize);
4763 auto layerName = fmt::format(
"DepthToSpace:{}:{}", subgraphIndex, operatorIndex);
4764 IConnectableLayer* layer = m_Network->AddDepthToSpaceLayer(descriptor, layerName.c_str());
4772 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
4775 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4776 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4778 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4779 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4782 void TfLiteParserImpl::ParseSum(
size_t subgraphIndex,
size_t operatorIndex)
4787 void TfLiteParserImpl::ParseReduceProd(
size_t subgraphIndex,
size_t operatorIndex)
4792 void TfLiteParserImpl::ParseReduceMax(
size_t subgraphIndex,
size_t operatorIndex)
4797 void TfLiteParserImpl::ParseReduceMin(
size_t subgraphIndex,
size_t operatorIndex)
4802 void TfLiteParserImpl::ParseReduce(
size_t subgraphIndex,
size_t operatorIndex,
ReduceOperation reduceOperation)
4804 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4806 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4807 const auto* options = operatorPtr->builtin_options.AsReducerOptions();
4809 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
4812 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
4815 auto layerName = fmt::format(
"Reduce:{}:{}", subgraphIndex, operatorIndex);
4817 armnn::TensorInfo inputTensorInfo0 = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4818 armnn::TensorInfo inputTensorInfo1 = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4823 if (axisBufferPtr !=
nullptr)
4825 std::vector<int32_t> axisData(inputTensorInfo1.
GetNumElements());
4826 ::memcpy(axisData.data(), axisBufferPtr->data.data(), inputTensorInfo1.
GetNumBytes());
4830 std::set<unsigned int> uniqueAxis;
4831 std::transform(axisData.begin(),
4833 std::inserter(uniqueAxis, uniqueAxis.begin()),
4834 [rank](
int i)->unsigned
int{
4835 return static_cast<uint32_t>(((i + rank) % rank)); });
4836 desc.
m_vAxis.assign(uniqueAxis.begin(), uniqueAxis.end());
4852 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
4856 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4857 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4860 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4861 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4864 void TfLiteParserImpl::ParseLocalResponseNormalization(
size_t subgraphIndex,
size_t operatorIndex)
4866 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4868 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
4871 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
4874 auto layerName = fmt::format(
"LRN:{}:{}", subgraphIndex, operatorIndex);
4875 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4877 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4879 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4880 const auto* options = operatorPtr->builtin_options.AsLocalResponseNormalizationOptions();
4886 descriptor.
m_NormSize =
static_cast<uint32_t
>(options->radius);
4887 descriptor.
m_K = options->bias;
4888 descriptor.
m_Alpha = options->alpha;
4889 descriptor.
m_Beta = options->beta;
4895 IConnectableLayer* layer = m_Network->AddNormalizationLayer(descriptor, layerNameFormatted.c_str());
4903 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
4906 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4907 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4909 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4910 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4913 void TfLiteParserImpl::ParseAbs(
size_t subgraphIndex,
size_t operatorIndex)
4918 void TfLiteParserImpl::ParseCeil(
size_t subgraphIndex,
size_t operatorIndex)
4923 void TfLiteParserImpl::ParseExp(
size_t subgraphIndex,
size_t operatorIndex)
4928 void TfLiteParserImpl::ParseLog(
size_t subgraphIndex,
size_t operatorIndex)
4933 void TfLiteParserImpl::ParseLogicalNot(
size_t subgraphIndex,
size_t operatorIndex)
4938 void TfLiteParserImpl::ParseNeg(
size_t subgraphIndex,
size_t operatorIndex)
4943 void TfLiteParserImpl::ParsePower(
size_t subgraphIndex,
size_t operatorIndex)
4945 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4947 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
4950 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
4953 auto layerName = fmt::format(
"Power:{}:{}", subgraphIndex, operatorIndex);
4955 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4956 TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4957 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName,
"Input 0",
"Input 1");
4959 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Power, layerName.c_str());
4967 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
4968 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
4971 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4972 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4974 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4975 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4978 void TfLiteParserImpl::ParseRsqrt(
size_t subgraphIndex,
size_t operatorIndex)
4983 void TfLiteParserImpl::ParseSin(
size_t subgraphIndex,
size_t operatorIndex)
4988 void TfLiteParserImpl::ParseSqrt(
size_t subgraphIndex,
size_t operatorIndex)
4993 void TfLiteParserImpl::ParseSquare(
size_t subgraphIndex,
size_t operatorIndex)
4995 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4997 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
5000 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
5003 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
5005 auto layerName = fmt::format(
"Square:{}:{}", subgraphIndex, operatorIndex);
5006 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Mul, layerName.c_str());
5009 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 0});
5010 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
5013 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
5014 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[0]});
5016 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
5017 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
5020 void TfLiteParserImpl::ParseSquaredDifference(
size_t subgraphIndex,
size_t operatorIndex)
5022 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
5024 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
5027 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
5030 auto layerName = fmt::format(
"SquaredDifference:{}:{}", subgraphIndex, operatorIndex);
5032 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
5033 TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
5034 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName,
"Input 0",
"Input 1");
5036 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::SqDiff, layerName.c_str());
5044 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
5045 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
5048 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
5049 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
5051 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
5052 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
5055 void TfLiteParserImpl::ParseElementwiseUnary(
size_t subgraphIndex,
size_t operatorIndex,
UnaryOperation unaryOperation)
5057 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
5059 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
5062 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
5066 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
5070 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(desc, layerNameFormatted.c_str());
5078 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
5081 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
5082 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
5084 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
5085 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
5088 void TfLiteParserImpl::ParseEqual(
size_t subgraphIndex,
size_t operatorIndex)
5093 void TfLiteParserImpl::ParseNotEqual(
size_t subgraphIndex,
size_t operatorIndex)
5098 void TfLiteParserImpl::ParseGreater(
size_t subgraphIndex,
size_t operatorIndex)
5103 void TfLiteParserImpl::ParseGreaterOrEqual(
size_t subgraphIndex,
size_t operatorIndex)
5108 void TfLiteParserImpl::ParseLess(
size_t subgraphIndex,
size_t operatorIndex)
5113 void TfLiteParserImpl::ParseLessOrEqual(
size_t subgraphIndex,
size_t operatorIndex)
5118 void TfLiteParserImpl::ParseComparison(
size_t subgraphIndex,
size_t operatorIndex,
5121 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
5123 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
5126 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
5130 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
5132 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
5133 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
5134 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerNameFormatted,
"Input 0",
"Input 1");
5138 IConnectableLayer* layer = m_Network->AddComparisonLayer(desc, layerNameFormatted.c_str());
5146 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
5149 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
5150 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
5152 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
5153 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
5157 unsigned int outputSlot,
5158 std::string reshapeLayerName,
5165 m_Network->AddReshapeLayer(desc, reshapeLayerName.c_str());
5170 return reshapeLayer;
5174 unsigned int outputSlot,
5175 tflite::ActivationFunctionType activationType)
5178 std::string layerName = prevLayer->
GetName();
5180 switch(activationType)
5182 case tflite::ActivationFunctionType_NONE:
5187 case tflite::ActivationFunctionType_RELU:
5189 activationDesc.
m_Function = ActivationFunction::ReLu;
5190 layerName +=
":RELU";
5193 case tflite::ActivationFunctionType_RELU6:
5195 activationDesc.
m_Function = ActivationFunction::BoundedReLu;
5196 activationDesc.
m_A = 6.0f;
5197 activationDesc.
m_B = 0.0f;
5198 layerName +=
":RELU6";
5201 case tflite::ActivationFunctionType_TANH:
5203 activationDesc.
m_Function = ActivationFunction::TanH;
5204 activationDesc.
m_A = 1.0f;
5205 activationDesc.
m_B = 1.0f;
5206 layerName +=
":TANH";
5211 case tflite::ActivationFunctionType_RELU_N1_TO_1:
5212 case tflite::ActivationFunctionType_SIGN_BIT:
5216 fmt::format(
"TfLite parser doesn't support fused activation: "
5219 tflite::EnumNameActivationFunctionType(activationType),
5226 m_Network->AddActivationLayer(activationDesc, layerName.c_str());
5228 auto & prevOutputSlot = prevLayer->
GetOutputSlot(outputSlot);
5231 return activationLayer;
5235 unsigned int outputSlot)
5238 auto& prevOutputSlot = prevLayer->
GetOutputSlot(outputSlot);
5241 if (dataType == DataType::Signed32)
5246 std::string layerName = prevLayer->
GetName();
5257 if (fileName ==
nullptr)
5262 std::error_code errorCode;
5263 fs::path pathToFile(fileName);
5264 if (!fs::exists(pathToFile, errorCode))
5267 std::stringstream msg;
5268 msg <<
"Cannot find the file (" << fileName <<
") errorCode: " << errorCode
5272 if (!fs::is_regular_file(pathToFile))
5276 pathToFile.c_str()));
5279 std::ifstream file(fileName, std::ios::binary);
5280 std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
5282 fileContent.size());
5287 if (binaryContent ==
nullptr)
5292 flatbuffers::Verifier verifier(binaryContent, len);
5296 fmt::format(
"Buffer doesn't conform to the expected Tensorflow Lite "
5297 "flatbuffers format. size:{} {}",
5301 return tflite::UnPackModel(binaryContent);
5305 size_t subgraphIndex,
5306 size_t operatorIndex)
5310 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5311 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
5313 size_t inputCount = operatorPtr->inputs.size();
5315 for (
size_t i = 0; i < inputCount; ++i)
5318 if (operatorPtr->inputs[i] == -1)
5325 result.push_back(subgraphPtr->tensors[inputId].get());
5332 size_t subgraphIndex,
5333 size_t operatorIndex)
5337 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5338 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
5340 size_t outputCount = operatorPtr->outputs.size();
5342 for (
size_t i = 0; i < outputCount; ++i)
5346 result[i] = subgraphPtr->tensors[outputId].get();
5352 size_t subgraphIndex)
5355 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5357 size_t inputCount = subgraphPtr->inputs.size();
5359 for (
size_t i = 0; i < inputCount; ++i)
5363 result[i] = std::make_pair(inputId, subgraphPtr->tensors[inputId].get());
5369 size_t subgraphIndex)
5372 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5374 size_t outputCount = subgraphPtr->outputs.size();
5376 for (
size_t i = 0; i < outputCount; ++i)
5379 result[i] = std::make_pair(outputId, subgraphPtr->tensors[outputId].get());
5385 size_t subgraphIndex,
5386 size_t operatorIndex)
5389 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5390 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
5391 return operatorPtr->inputs;
5395 size_t subgraphIndex,
5396 size_t operatorIndex)
5399 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5400 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
5401 return operatorPtr->outputs;
5404 void TfLiteParserImpl::RegisterInputSlots(
size_t subgraphIndex,
5405 size_t operatorIndex,
5407 const std::vector<unsigned int>& tensorIndexes,
5408 unsigned int startingSlotIndex)
5410 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
5421 fmt::format(
"The number of tensor inputs ({}) does not match the number expected ({})"
5422 " for subgraph:{} operator index:{} {}",
5423 tensorIndexes.size(),
5430 for (
unsigned int index = 0; index < tensorIndexes.size() ; ++index)
5432 unsigned int tensorIndex = tensorIndexes[index];
5434 RegisterConsumerOfTensor(subgraphIndex, tensorIndex, slot);
5438 void TfLiteParserImpl::RegisterOutputSlots(
size_t subgraphIndex,
5439 size_t operatorIndex,
5441 const std::vector<unsigned int>& tensorIndexes)
5443 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
5454 fmt::format(
"The number of tensor outputs ({}) does not match the number expected ({})"
5455 " for subgraph:{} operator index:{} {}",
5456 tensorIndexes.size(),
5463 for (
unsigned int slotIndex = 0; slotIndex < layer->
GetNumOutputSlots(); ++slotIndex)
5465 unsigned int tensorIndex = tensorIndexes[slotIndex];
5467 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
5471 void TfLiteParserImpl::SetupInputLayerTensorInfos(
size_t subgraphIndex)
5476 for (
auto const& tensorIdAndPtr : inputs)
5479 m_TensorInfos.insert({tensorIdAndPtr.first, tensorInfo});
5483 void TfLiteParserImpl::SetupInputLayers(
size_t subgraphIndex)
5488 for (
auto const& tensorIdAndPtr : inputs)
5490 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
5492 m_Network->AddInputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
5497 RegisterOutputSlots(subgraphIndex,
5498 VIRTUAL_OPERATOR_ID,
5500 {
static_cast<uint32_t
>(tensorIdAndPtr.first) });
5504 void TfLiteParserImpl::SetupOutputLayers(
size_t subgraphIndex)
5509 for (
auto const& tensorIdAndPtr : outputs)
5511 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
5513 m_Network->AddOutputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
5515 RegisterInputSlots(subgraphIndex,
5516 VIRTUAL_OPERATOR_ID,
5518 {
static_cast<uint32_t
>(tensorIdAndPtr.first) });
5522 void TfLiteParserImpl::SetupConstantLayerTensorInfos(
size_t subgraph)
5526 const auto & subgraphPtr = m_Model->subgraphs[subgraph];
5527 for (
unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
5529 for (
unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
5531 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot ==
nullptr &&
5532 m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0)
5534 TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get();
5538 m_TensorInfos.insert({tensorIndex, tensorInfo});
5544 void TfLiteParserImpl::SetupConstantLayers(
size_t subgraph)
5548 const auto & subgraphPtr = m_Model->subgraphs[subgraph];
5549 for (
unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
5551 for (
unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
5553 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot ==
nullptr &&
5554 m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0)
5556 TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get();
5558 if (IsConstTensor(tensorPtr))
5563 if (std::find(m_ConstantsToDequantize.begin(), m_ConstantsToDequantize.end(), tensorPtr->buffer)
5564 != m_ConstantsToDequantize.end())
5566 dataType = DataType::Float32;
5568 auto tensorAndData = CreateConstTensorNonPermuted(tensorPtr, tensorInfo, dataType);
5570 std::string layerName = fmt::format(
"Constant:{}", tensorPtr->name);
5571 IConnectableLayer *layer = m_Network->AddConstantLayer(tensorAndData.first, layerName.c_str());
5574 RegisterOutputSlots(subgraphIndex,
5575 VIRTUAL_OPERATOR_ID,
5579 else if (ShouldConstantTensorBeCreated(tensorIndex))
5584 if (std::find(m_ConstantsToDequantize.begin(), m_ConstantsToDequantize.end(), tensorPtr->buffer)
5585 != m_ConstantsToDequantize.end())
5587 dataType = DataType::Float32;
5595 std::string layerName = fmt::format(
"Constant:{}", tensorPtr->name);
5596 IConnectableLayer* layer = m_Network->AddConstantLayer(tensorAndData, layerName.c_str());
5599 RegisterOutputSlots(subgraphIndex,
5600 VIRTUAL_OPERATOR_ID,
5607 fmt::format(
"Invalid Tensor: Tensor should be constant. {}",
5619 return model->buffers[bufferIndex].get();
5622 template<
typename T>
5623 std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
5632 auto constData = CreateConstTensorImpl<T>(bufferPtr,
5636 TfLiteParserImpl::SupportedDataStorage storage(std::move(constData.second));
5637 return std::make_pair(constData.first, std::move(storage));
5640 bool TfLiteParserImpl::ShouldConstantTensorBeCreated(
unsigned int tensorIndex)
5643 return (std::find(m_ConstantsToBeCreated.begin(), m_ConstantsToBeCreated.end(), tensorIndex)
5644 != m_ConstantsToBeCreated.end());
5647 bool TfLiteParserImpl::IsConstTensor(
TensorRawPtr tensorPtr)
5650 bool isConst =
true;
5652 auto buffer =
GetBuffer(m_Model, tensorPtr->buffer);
5653 if (buffer->data.size() == 0)
5661 std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
5662 TfLiteParserImpl::CreateConstTensorPermuted(
TensorRawPtr tensorPtr,
5667 auto bufferPtr =
GetBuffer(m_Model, tensorPtr->buffer);
5676 return CreateConstTensorAndStoreData<float>(bufferPtr,
5681 return CreateConstTensorAndStoreData<uint8_t>(bufferPtr,
5686 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
5691 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
5696 return CreateConstTensorAndStoreData<int32_t>(bufferPtr,
5702 std::stringstream errString;
5703 errString <<
"Unexpected datatype when creating const tensor: "
5705 <<
" shape:" << tensorInfo.
GetShape()
5716 auto bufferPtr =
GetBuffer(m_Model, tensorPtr->buffer);
5722 return ConstTensor(tensorInfo, bufferPtr->data.data());
5725 std::pair<armnn::ConstTensor, std::unique_ptr<float[]>>
5726 TfLiteParserImpl::CreateConstTensorNonPermuted(
TensorRawPtr tensorPtr,
5731 auto bufferPtr =
GetBuffer(m_Model, tensorPtr->buffer);
5737 if (inputDataType == DataType::Float32 && tensorInfo.
GetDataType() != DataType::Float32)
5743 return std::make_pair(
ConstTensor(constTensorInfo, data.get()), std::move(data));
5748 fmt::format(
"Unsupported input/weights combination: Input {} not supported with Weights {}",
5756 return std::make_pair(
ConstTensor(tensorInfo, bufferPtr->data.data()), std::unique_ptr<
float[]>());
5760 std::pair<armnn::ConstTensor*, std::unique_ptr<float[]>>
5765 auto bufferPtr =
GetBuffer(m_Model, tensorPtr->buffer);
5777 return std::make_pair(
new ConstTensor(constTensorInfo, data.get()), std::move(data));
5782 fmt::format(
"Unsupported input/weights combination: Input {} not supported with Weights {}",
5790 return std::make_pair(
new ConstTensor(tensorInfo, bufferPtr->data.data()), std::unique_ptr<
float[]>());
5795 const std::string& name)
const
5799 for (
auto const& input : inputs)
5801 if (input.second->name == name)
5803 auto bindingId = GenerateLayerBindingId(subgraphId, input.first);
5807 return std::make_pair(bindingId, inputTensorInfo);
5811 std::stringstream bindings;
5812 for (
auto const& input : inputs)
5814 bindings <<
"'" << input.second->name <<
"' ";
5818 fmt::format(
"No input binding found for subgraph:{} and name:{}. "
5819 "Possible inputs are: [{}] {}",
5827 const std::string& name)
const
5831 for (
unsigned int i = 0; i < outputs.size(); ++i)
5833 auto const output = outputs[i];
5834 if (output.second->name == name)
5836 auto bindingId = GenerateLayerBindingId(subgraphId, output.first);
5837 std::vector<unsigned int> shape = m_OverriddenOutputShapes.size() > 0 ?
5838 m_OverriddenOutputShapes[i] : AsUnsignedVector(output.second->shape);
5839 return std::make_pair(bindingId,
ToTensorInfo(output.second, shape));
5843 std::stringstream bindings;
5844 for (
auto const& output : outputs)
5846 bindings <<
"'" << output.second->name <<
"' ";
5850 fmt::format(
"No output binding found for subgraph:{} and name:{}. "
5851 "Possible outputs are: [{}] {}",
5860 return m_Model->subgraphs.size();
5867 std::vector<std::string> result;
5868 result.reserve(inputs.size());
5869 for (
auto const& input : inputs)
5871 result.push_back(input.second->name);
5880 std::vector<std::string> result;
5881 result.reserve(outputs.size());
5882 for (
auto const& output : outputs)
5884 result.push_back(output.second->name);
5894 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<
float[]>&& data)
5895 : m_FloatData(
std::move(data))
5896 , m_Uint8Data(nullptr)
5897 , m_Int8Data(nullptr)
5898 , m_Int32Data(nullptr)
5902 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<uint8_t[]>&& data)
5903 : m_FloatData(nullptr)
5904 , m_Uint8Data(
std::move(data))
5905 , m_Int8Data(nullptr)
5906 , m_Int32Data(nullptr)
5910 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int8_t[]>&& data)
5911 : m_FloatData(nullptr)
5912 , m_Uint8Data(nullptr)
5913 , m_Int8Data(
std::move(data))
5914 , m_Int32Data(nullptr)
5918 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int32_t[]>&& data)
5919 : m_FloatData(nullptr)
5920 , m_Uint8Data(nullptr)
5921 , m_Int8Data(nullptr)
5922 , m_Int32Data(
std::move(data))