31 #include <schema_generated.h>
33 #include <flatbuffers/flexbuffers.h>
35 #include <fmt/format.h>
42 #define ARMNN_THROW_PARSE_EXCEPTION(msg) \
44 throw armnn::ParseException( static_cast<const std::stringstream&>( std::stringstream() << msg \
46 << CHECK_LOCATION().AsString()).str()); \
49 using namespace armnn;
55 pTfLiteParserImpl(
new TfLiteParserImpl(options)) {}
57 ITfLiteParser::~ITfLiteParser() =
default;
79 armnn::INetworkPtr ITfLiteParser::CreateNetworkFromBinary(
const std::vector<uint8_t>& binaryContent)
81 return pTfLiteParserImpl->CreateNetworkFromBinary(binaryContent);
85 const std::string& name)
const
87 return pTfLiteParserImpl->GetNetworkInputBindingInfo(subgraphId, name);
91 const std::string& name)
const
93 return pTfLiteParserImpl->GetNetworkOutputBindingInfo(subgraphId, name);
96 size_t ITfLiteParser::GetSubgraphCount()
const
98 return pTfLiteParserImpl->GetSubgraphCount();
101 std::vector<std::string> ITfLiteParser::GetSubgraphInputTensorNames(
size_t subgraphId)
const
103 return pTfLiteParserImpl->GetSubgraphInputTensorNames(subgraphId);
106 std::vector<std::string> ITfLiteParser::GetSubgraphOutputTensorNames(
size_t subgraphId)
const
108 return pTfLiteParserImpl->GetSubgraphOutputTensorNames(subgraphId);
114 const uint32_t VIRTUAL_OPERATOR_ID = std::numeric_limits<uint32_t>::max();
117 size_t subgraphIndex,
120 if (model.get() ==
nullptr)
123 fmt::format(
"{} was called with invalid (null) model. "
124 "Possible reason is that the model is not yet loaded and Unpack(ed). "
130 else if (subgraphIndex >= model->subgraphs.size())
133 fmt::format(
"{} was called with an invalid subgraph index. "
141 #define CHECK_SUBGRAPH(MODEL, SUBGRAPH_INDEX) \
142 CheckSubgraph(MODEL, SUBGRAPH_INDEX, CHECK_LOCATION())
145 size_t subgraphIndex,
146 size_t operatorIndex,
149 if (model.get() ==
nullptr)
152 fmt::format(
"{} was called with invalid (null) model. "
153 "Possible reason is that the model is not yet loaded and Unpack(ed). "
154 "subgraph:{} operator:{} at {}",
160 else if (subgraphIndex >= model->subgraphs.size())
163 fmt::format(
"{} was called with an invalid subgraph index. "
164 "subgraph:{} operator:{} at {}",
170 else if (operatorIndex >= model->subgraphs[subgraphIndex]->operators.size() &&
171 operatorIndex != VIRTUAL_OPERATOR_ID)
174 fmt::format(
"{} was called with an invalid operator index. "
175 "subgraph:{} operator:{} at {}",
183 #define CHECK_MODEL(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX) \
184 CheckModel(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX, CHECK_LOCATION())
187 size_t subgraphIndex,
193 ARMNN_ASSERT_MSG(model.get() !=
nullptr,
"Expecting a valid model in this function");
197 ARMNN_ASSERT_MSG(subgraphIndex < model->subgraphs.size(),
"Expecting a valid subgraph index");
200 if (tensorIndex >= model->subgraphs[subgraphIndex]->tensors.size())
203 fmt::format(
"{} was called with an invalid tensor index. "
204 "subgraph:{} tensor:{} at {}",
212 #define CHECK_TENSOR(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX) \
213 CheckTensor(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX, CHECK_LOCATION())
218 if (rawPtr ==
nullptr)
221 fmt::format(
"{} was called with a null tensor pointer at {}", location.
m_Function, location.
FileLine()));
225 #define CHECK_TENSOR_PTR(TENSOR_PTR) \
226 CheckTensorPtr(TENSOR_PTR, CHECK_LOCATION())
232 if (model.get() ==
nullptr)
235 fmt::format(
"{} was called with invalid (null) model. "
236 "Possible reason is that the model is not yet loaded and Unpack(ed). "
242 else if (bufferIndex >= model->buffers.size())
245 fmt::format(
"{} was called with an invalid buffer index. "
246 "buffer index:{} at {}",
251 else if (model->buffers[bufferIndex].get() ==
nullptr)
254 fmt::format(
"The buffer #{} is null. {}",
260 #define CHECK_BUFFER(MODEL, BUFFER_INDEX) \
261 CheckBuffer(MODEL, BUFFER_INDEX, CHECK_LOCATION())
263 void CheckBufferSize(TfLiteParserImpl::BufferRawPtr bufferPtr,
268 if (bufferPtr ==
nullptr)
271 fmt::format(
"BufferPtr is null for buffer:{}. {}",
278 std::stringstream ss;
279 ss <<
"Buffer #" << bufferId <<
" has " << bufferPtr->data.size() <<
" bytes. "
280 <<
"For tensor: " << tensorInfo.
GetShape()
281 <<
" expecting: " << tensorInfo.
GetNumBytes() <<
" bytes and "
290 const auto& operatorPtr = model->subgraphs[subgraphIndex]->operators[operatorIndex];
291 auto opcodeIndex = operatorPtr->opcode_index;
294 #if defined(ARMNN_POST_TFLITE_2_3)
295 auto opcode = std::max(model->operator_codes[opcodeIndex]->builtin_code,
296 static_cast<tflite::BuiltinOperator
>(model->operator_codes[opcodeIndex]->deprecated_builtin_code));
298 auto opcode = model->operator_codes[opcodeIndex]->builtin_code;
307 TfLiteParserImpl::BufferRawPtr bufferPtr = TfLiteParserImpl::GetBuffer(model, bufferIndex);
308 std::vector<unsigned int> buffer(
info.GetNumElements());
312 ::memcpy(buffer.data(), bufferPtr->data.data(), bufferPtr->data.size());
316 std::vector<uint64_t> uint64Buffer(
info.GetNumElements());
317 ::memcpy(uint64Buffer.data(), bufferPtr->data.data(), bufferPtr->data.size());
318 buffer.assign(std::begin(uint64Buffer), std::end(uint64Buffer));
324 fmt::format(
"Unsupported data type for uint buffer {}, only Signed 32 or Signed 64 are supported. {}",
331 #define CHECK_BUFFER_SIZE(BUFFER_PTR, TENSOR_INFO, BUFFER_ID) \
332 CheckBufferSize(BUFFER_PTR, TENSOR_INFO, BUFFER_ID, CHECK_LOCATION())
336 switch(activationType)
338 case tflite::ActivationFunctionType_NONE:
339 case tflite::ActivationFunctionType_RELU:
340 case tflite::ActivationFunctionType_RELU6:
341 case tflite::ActivationFunctionType_TANH:
352 #define CHECK_SUPPORTED_FUSED_ACTIVATION(OPTION, SUBGRAPH_INDEX, OPERATOR_INDEX) \
354 if (IsActivationSupported(OPTION->fused_activation_function) == false) \
356 throw ParseException( \
357 fmt::format("TfLite parser doesn't support fused activation: " \
358 "{}/{} in {} subgraph:{} operator:{} at {}", \
359 OPTION->fused_activation_function, \
360 tflite::EnumNameActivationFunctionType(\
361 OPTION->fused_activation_function), \
365 CHECK_LOCATION().FileLine())); \
370 std::vector<unsigned int> AsUnsignedVector(
const std::vector<int32_t>& in)
372 std::vector<unsigned int> result;
373 result.reserve(in.size());
386 bool IsOptionalOperandPresent(
int input)
391 void CalcPadding(uint32_t inputSize,
395 uint32_t& paddingFront,
396 uint32_t& paddingBack,
397 tflite::Padding padding)
401 if (padding == tflite::Padding_SAME)
403 uint32_t outputSize = (inputSize + stride - 1) / stride;
404 uint32_t dilatedSize = filterSize + (dilation - 1) * (filterSize - 1);
405 uint32_t temp = (outputSize - 1) * stride + dilatedSize;
406 if (temp > inputSize)
408 paddingFront = (temp - inputSize) / 2;
409 paddingBack = (temp - inputSize) - paddingFront;
415 const std::vector<unsigned int>& shape,
416 const bool outputTensor =
false)
421 switch (tensorPtr->type)
423 case tflite::TensorType_UINT8:
426 case tflite::TensorType_FLOAT32:
429 case tflite::TensorType_FLOAT16:
432 case tflite::TensorType_INT8:
433 if (tensorPtr->quantization->zero_point.size() == 1)
444 case tflite::TensorType_INT16:
447 case tflite::TensorType_INT32:
450 case tflite::TensorType_INT64:
453 case tflite::TensorType_BOOL:
460 fmt::format(
"Unsupported data type {} = {} for tensor: {}. {}",
462 tflite::EnumNameTensorType(tensorPtr->type),
469 std::vector<unsigned int> safeShape = shape;
470 if (shape.size() == 0)
472 safeShape.push_back(1);
477 tensorShape =
TensorShape(armnn::numeric_cast<unsigned int>(safeShape.size()), safeShape.data());
481 size_t shapeSignatureSize = tensorPtr->shape_signature.size();
484 if (shapeSignatureSize != 0)
487 if (shapeSignatureSize != shape.size())
491 for (
unsigned int i = 0; i < shapeSignatureSize; ++i)
493 unsigned int dim = tensorPtr->shape_signature[i] > -1 ?
494 static_cast<unsigned int>(tensorPtr->shape_signature[i]) : 0;
495 safeShape.push_back(dim);
499 std::unique_ptr<bool[]> dimMask = std::make_unique<bool[]>(tensorPtr->shape_signature.size());
500 bool batchOnly =
true;
501 for (
unsigned int i = 0; i < tensorPtr->shape_signature.size(); ++i)
503 dimMask[i] = tensorPtr->shape_signature[i] != -1;
505 if (i > 0 && !dimMask[i])
514 tensorShape =
TensorShape(
static_cast<unsigned int>(safeShape.size()), safeShape.data(), dimMask.get());
517 else if (shape.size() == 0)
523 tensorShape =
TensorShape(armnn::numeric_cast<unsigned int>(shape.size()), shape.data());
527 float quantizationScale = 0.0f;
528 int32_t quantizationOffset = 0;
530 if (tensorPtr->quantization.get())
532 if (tensorPtr->quantization->scale.size() <= 1)
537 if (tensorPtr->quantization->scale.size() == 1)
539 quantizationScale = tensorPtr->quantization->scale[0];
541 if (tensorPtr->quantization->zero_point.size() == 1)
545 quantizationOffset = armnn::numeric_cast<int32_t>(tensorPtr->quantization->zero_point[0]);
556 std::vector<float> quantizationScales;
557 std::vector<int32_t> quantizationOffsets;
560 std::copy(tensorPtr->quantization->scale.begin(),
561 tensorPtr->quantization->scale.end(),
562 std::back_inserter(quantizationScales));
568 armnn::numeric_cast<unsigned int>(tensorPtr->quantization->quantized_dimension));
583 const bool outputTensor =
false)
585 auto const& dimensions = AsUnsignedVector(tensorPtr->shape);
586 return ToTensorInfo(tensorPtr, dimensions, outputTensor);
590 std::pair<armnn::ConstTensor, std::unique_ptr<T[]>>
599 fmt::format(
"Buffer for buffer:{} is null", tensorPtr->buffer).c_str());
607 reinterpret_cast<const T*
>(bufferPtr->data.data()), data.get(),
sizeof(T));
611 ::memcpy(data.get(), bufferPtr->data.data(), tensorInfo.
GetNumBytes());
617 return std::make_pair(
ConstTensor(tensorInfo, data.get()), std::move(data));
630 if (actualSize != expected.size())
635 for (
unsigned int i = 0u; i < actualSize; i++)
637 if (expected[i] < 0 ||
638 actual[i] !=
static_cast<unsigned int>(expected[i]))
649 std::vector<int32_t> expectedVec;
652 expectedVec.push_back(expected[i]);
657 void CheckMatchingQuantization(
const TensorInfo& first,
659 const std::string& descName,
660 std::string
const& firstName,
661 std::string
const& secondName)
673 if (firstDataType != secondDataType)
676 " must be of the same quantized type, " +
684 " must have the same quantization space, " +
694 auto shape = tensorPtr->shape;
700 auto shapeSig = tensorPtr->shape_signature;
702 if (shapeSig.empty())
707 for (
unsigned int i = 0; i < shapeSig.size() ; ++i)
709 if (shapeSig[i] == -1)
721 , m_Network(nullptr, nullptr)
725 m_ParserFunctions[tflite::BuiltinOperator_ABS] = &TfLiteParserImpl::ParseAbs;
726 m_ParserFunctions[tflite::BuiltinOperator_ADD] = &TfLiteParserImpl::ParseAdd;
727 m_ParserFunctions[tflite::BuiltinOperator_ARG_MIN] = &TfLiteParserImpl::ParseArgMin;
728 m_ParserFunctions[tflite::BuiltinOperator_ARG_MAX] = &TfLiteParserImpl::ParseArgMax;
729 m_ParserFunctions[tflite::BuiltinOperator_AVERAGE_POOL_2D] = &TfLiteParserImpl::ParseAveragePool2D;
730 m_ParserFunctions[tflite::BuiltinOperator_BATCH_TO_SPACE_ND] = &TfLiteParserImpl::ParseBatchToSpaceND;
731 m_ParserFunctions[tflite::BuiltinOperator_BATCH_MATMUL] = &TfLiteParserImpl::ParseBatchMatMul;
732 m_ParserFunctions[tflite::BuiltinOperator_CAST] = &TfLiteParserImpl::ParseCast;
733 m_ParserFunctions[tflite::BuiltinOperator_CONCATENATION] = &TfLiteParserImpl::ParseConcatenation;
734 m_ParserFunctions[tflite::BuiltinOperator_CONV_2D] = &TfLiteParserImpl::ParseConv2D;
736 #if defined(ARMNN_POST_TFLITE_2_4)
737 m_ParserFunctions[tflite::BuiltinOperator_CONV_3D] = &TfLiteParserImpl::ParseConv3D;
739 m_ParserFunctions[tflite::BuiltinOperator_CUSTOM] = &TfLiteParserImpl::ParseCustomOperator;
740 m_ParserFunctions[tflite::BuiltinOperator_DEPTH_TO_SPACE] = &TfLiteParserImpl::ParseDepthToSpace;
741 m_ParserFunctions[tflite::BuiltinOperator_DEPTHWISE_CONV_2D] = &TfLiteParserImpl::ParseDepthwiseConv2D;
742 m_ParserFunctions[tflite::BuiltinOperator_DEQUANTIZE] = &TfLiteParserImpl::ParseDequantize;
743 m_ParserFunctions[tflite::BuiltinOperator_DIV] = &TfLiteParserImpl::ParseDiv;
744 m_ParserFunctions[tflite::BuiltinOperator_ELU] = &TfLiteParserImpl::ParseElu;
745 m_ParserFunctions[tflite::BuiltinOperator_EQUAL] = &TfLiteParserImpl::ParseEqual;
746 m_ParserFunctions[tflite::BuiltinOperator_EXP] = &TfLiteParserImpl::ParseExp;
747 m_ParserFunctions[tflite::BuiltinOperator_EXPAND_DIMS] = &TfLiteParserImpl::ParseExpandDims;
748 m_ParserFunctions[tflite::BuiltinOperator_FLOOR_DIV] = &TfLiteParserImpl::ParseFloorDiv;
749 m_ParserFunctions[tflite::BuiltinOperator_FULLY_CONNECTED] = &TfLiteParserImpl::ParseFullyConnected;
750 m_ParserFunctions[tflite::BuiltinOperator_GATHER] = &TfLiteParserImpl::ParseGather;
751 m_ParserFunctions[tflite::BuiltinOperator_GATHER_ND] = &TfLiteParserImpl::ParseGatherNd;
752 m_ParserFunctions[tflite::BuiltinOperator_GREATER] = &TfLiteParserImpl::ParseGreater;
753 m_ParserFunctions[tflite::BuiltinOperator_GREATER_EQUAL] = &TfLiteParserImpl::ParseGreaterOrEqual;
754 m_ParserFunctions[tflite::BuiltinOperator_HARD_SWISH] = &TfLiteParserImpl::ParseHardSwish;
755 m_ParserFunctions[tflite::BuiltinOperator_LEAKY_RELU] = &TfLiteParserImpl::ParseLeakyRelu;
756 m_ParserFunctions[tflite::BuiltinOperator_LESS] = &TfLiteParserImpl::ParseLess;
757 m_ParserFunctions[tflite::BuiltinOperator_LESS_EQUAL] = &TfLiteParserImpl::ParseLessOrEqual;
758 m_ParserFunctions[tflite::BuiltinOperator_LOCAL_RESPONSE_NORMALIZATION]
759 = &TfLiteParserImpl::ParseLocalResponseNormalization;
760 m_ParserFunctions[tflite::BuiltinOperator_LOG] = &TfLiteParserImpl::ParseLog;
761 m_ParserFunctions[tflite::BuiltinOperator_LOGICAL_NOT] = &TfLiteParserImpl::ParseLogicalNot;
762 m_ParserFunctions[tflite::BuiltinOperator_LOGISTIC] = &TfLiteParserImpl::ParseLogistic;
763 m_ParserFunctions[tflite::BuiltinOperator_LOG_SOFTMAX] = &TfLiteParserImpl::ParseLogSoftmax;
764 m_ParserFunctions[tflite::BuiltinOperator_L2_NORMALIZATION] = &TfLiteParserImpl::ParseL2Normalization;
765 m_ParserFunctions[tflite::BuiltinOperator_MAX_POOL_2D] = &TfLiteParserImpl::ParseMaxPool2D;
766 m_ParserFunctions[tflite::BuiltinOperator_MAXIMUM] = &TfLiteParserImpl::ParseMaximum;
767 m_ParserFunctions[tflite::BuiltinOperator_MEAN] = &TfLiteParserImpl::ParseMean;
768 m_ParserFunctions[tflite::BuiltinOperator_MINIMUM] = &TfLiteParserImpl::ParseMinimum;
769 m_ParserFunctions[tflite::BuiltinOperator_MIRROR_PAD] = &TfLiteParserImpl::ParseMirrorPad;
770 m_ParserFunctions[tflite::BuiltinOperator_MUL] = &TfLiteParserImpl::ParseMul;
771 m_ParserFunctions[tflite::BuiltinOperator_NEG] = &TfLiteParserImpl::ParseNeg;
772 m_ParserFunctions[tflite::BuiltinOperator_NOT_EQUAL] = &TfLiteParserImpl::ParseNotEqual;
773 m_ParserFunctions[tflite::BuiltinOperator_PACK] = &TfLiteParserImpl::ParsePack;
774 m_ParserFunctions[tflite::BuiltinOperator_PAD] = &TfLiteParserImpl::ParsePad;
775 m_ParserFunctions[tflite::BuiltinOperator_PADV2] = &TfLiteParserImpl::ParsePad;
776 m_ParserFunctions[tflite::BuiltinOperator_PRELU] = &TfLiteParserImpl::ParsePrelu;
777 m_ParserFunctions[tflite::BuiltinOperator_QUANTIZE] = &TfLiteParserImpl::ParseQuantize;
778 m_ParserFunctions[tflite::BuiltinOperator_RELU] = &TfLiteParserImpl::ParseRelu;
779 m_ParserFunctions[tflite::BuiltinOperator_RELU6] = &TfLiteParserImpl::ParseRelu6;
780 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MAX] = &TfLiteParserImpl::ParseReduceMax;
781 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MIN] = &TfLiteParserImpl::ParseReduceMin;
782 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_PROD] = &TfLiteParserImpl::ParseReduceProd;
783 m_ParserFunctions[tflite::BuiltinOperator_RESHAPE] = &TfLiteParserImpl::ParseReshape;
784 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_BILINEAR] = &TfLiteParserImpl::ParseResizeBilinear;
785 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_NEAREST_NEIGHBOR] = &TfLiteParserImpl::ParseResizeNearestNeighbor;
786 m_ParserFunctions[tflite::BuiltinOperator_RSQRT] = &TfLiteParserImpl::ParseRsqrt;
787 m_ParserFunctions[tflite::BuiltinOperator_SQRT] = &TfLiteParserImpl::ParseSqrt;
788 m_ParserFunctions[tflite::BuiltinOperator_SHAPE] = &TfLiteParserImpl::ParseShape;
789 m_ParserFunctions[tflite::BuiltinOperator_SIN] = &TfLiteParserImpl::ParseSin;
790 m_ParserFunctions[tflite::BuiltinOperator_SLICE] = &TfLiteParserImpl::ParseSlice;
791 m_ParserFunctions[tflite::BuiltinOperator_SOFTMAX] = &TfLiteParserImpl::ParseSoftmax;
792 m_ParserFunctions[tflite::BuiltinOperator_SPACE_TO_BATCH_ND] = &TfLiteParserImpl::ParseSpaceToBatchND;
793 m_ParserFunctions[tflite::BuiltinOperator_SPLIT] = &TfLiteParserImpl::ParseSplit;
794 m_ParserFunctions[tflite::BuiltinOperator_SPLIT_V] = &TfLiteParserImpl::ParseSplitV;
795 m_ParserFunctions[tflite::BuiltinOperator_SQUEEZE] = &TfLiteParserImpl::ParseSqueeze;
796 m_ParserFunctions[tflite::BuiltinOperator_STRIDED_SLICE] = &TfLiteParserImpl::ParseStridedSlice;
797 m_ParserFunctions[tflite::BuiltinOperator_SUB] = &TfLiteParserImpl::ParseSub;
798 m_ParserFunctions[tflite::BuiltinOperator_SUM] = &TfLiteParserImpl::ParseSum;
799 m_ParserFunctions[tflite::BuiltinOperator_TANH] = &TfLiteParserImpl::ParseTanH;
800 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE] = &TfLiteParserImpl::ParseTranspose;
801 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE_CONV] = &TfLiteParserImpl::ParseTransposeConv;
802 m_ParserFunctions[tflite::BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM]
803 = &TfLiteParserImpl::ParseUnidirectionalSequenceLSTM;
804 m_ParserFunctions[tflite::BuiltinOperator_UNPACK] = &TfLiteParserImpl::ParseUnpack;
807 m_CustomParserFunctions[
"TFLite_Detection_PostProcess"] = &TfLiteParserImpl::ParseDetectionPostProcess;
811 size_t operatorIndex,
814 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
815 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
818 auto search = armnnTfLiteParser::TfLiteParserImpl::m_TensorInfos.find(inputId);
820 if (search != m_TensorInfos.end())
822 return m_TensorInfos[inputId];
827 m_TensorInfos.insert({ inputId, tensorInfo });
832 armnn::TensorInfo TfLiteParserImpl::OutputTensorInfoFromInputs(
size_t subgraphIndex,
833 size_t operatorIndex,
836 std::vector<int> inputs)
838 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
839 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
843 auto outputSearch = armnnTfLiteParser::TfLiteParserImpl::m_TensorInfos.find(outputId);
845 if (outputSearch != m_TensorInfos.end())
847 return m_TensorInfos[outputId];
850 const auto& outputTensorPtr = subgraphPtr->tensors[outputId].get();
853 if (IsDynamic(outputTensorPtr))
859 inputs.emplace_back(i);
863 std::vector<armnn::TensorShape> inputShapes;
865 for (
unsigned int i = 0; i < inputs.size(); ++i)
868 auto search = armnnTfLiteParser::TfLiteParserImpl::m_TensorInfos.find(inputId);
870 if (search != m_TensorInfos.end())
872 auto &inputTensorInfo = m_TensorInfos[inputId];
873 inputShapes.push_back(inputTensorInfo.GetShape());
877 m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
879 m_TensorInfos.insert({ inputId, inputTensorInfo});
880 inputShapes.push_back(inputTensorInfo.GetShape());
886 m_TensorInfos.insert({ outputId, tensor});
890 armnn::TensorInfo TfLiteParserImpl::OutputTensorInfoFromShapes(
size_t subgraphIndex,
891 size_t operatorIndex,
894 std::vector<armnn::TensorShape> inputShapes)
896 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
897 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
900 const auto& outputTensorPtr = subgraphPtr->tensors[outputId].get();
903 if (IsDynamic(outputTensorPtr))
908 m_TensorInfos.insert({ outputId, tensor});
912 void TfLiteParserImpl::ResetParser()
916 m_SubgraphConnections.clear();
917 m_OverriddenOutputShapes.clear();
918 m_ConstantsToDequantize.clear();
919 m_ConstantsToBeCreated.clear();
920 m_TensorInfos.clear();
927 return CreateNetworkFromModel();
934 return CreateNetworkFromModel();
941 m_Model = std::move(model);
943 return CreateNetworkFromModel();
946 INetworkPtr TfLiteParserImpl::CreateNetworkFromModel()
953 if (m_Options.value().m_InferAndValidate)
957 {
"InferAndValidate",
true }
960 networkOptions.push_back(shapeInferenceMethodOption);
962 if (m_Options.value().m_AllowExpandedDims)
966 {
"AllowExpandedDims",
true }
969 networkOptions.push_back(shapeInferenceMethodOption);
972 m_Network = INetwork::Create(networkOptions);
975 if (m_Model->subgraphs.size() != 1)
978 fmt::format(
"Current TfLite parser only supports 1 subgraph. Current one has: {} {}",
979 m_Model->subgraphs.size(),
983 size_t subgraphIndex = 0;
984 size_t operatorIndex = 0;
987 for (
SubgraphPtr const& subgraph : m_Model->subgraphs)
989 SetupInputLayerTensorInfos(subgraphIndex);
990 SetupConstantLayerTensorInfos(subgraphIndex);
992 m_SubgraphConnections.emplace_back(subgraph->tensors.size());
995 auto const& opCodePtr = m_Model->operator_codes[op->opcode_index];
998 #if defined(ARMNN_POST_TFLITE_2_3)
999 auto builtinCode = std::max(opCodePtr->builtin_code,
1000 static_cast<tflite::BuiltinOperator
>(opCodePtr->deprecated_builtin_code));
1002 auto builtinCode = opCodePtr->builtin_code;
1005 if (builtinCode > tflite::BuiltinOperator_MAX)
1007 throw ParseException(fmt::format(
"Operator code {} is out of range 0-{}. "
1008 "subgraph:{} operator idx:{}. {}",
1009 builtinCode, tflite::BuiltinOperator_MAX, subgraphIndex,
1014 auto& parserFunction = m_ParserFunctions[builtinCode];
1015 (this->*parserFunction)(subgraphIndex, operatorIndex);
1019 SetupInputLayers(subgraphIndex);
1020 SetupOutputLayers(subgraphIndex);
1021 SetupConstantLayers(subgraphIndex);
1029 std::stringstream errorString;
1030 errorString <<
"Failed to parse operator #" << operatorIndex <<
" within subgraph #"
1031 << subgraphIndex <<
" error: " << e.
what();
1033 std::stringstream errors;
1034 errors << errorString.str() <<
"\n";
1039 for (subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
1041 for (
size_t tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
1043 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot !=
nullptr)
1045 for (
size_t inputSlotIdx = 0;
1046 inputSlotIdx < m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size();
1049 m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot->Connect(
1050 *(m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots[inputSlotIdx]));
1055 return std::move(m_Network);
1062 return (TfLiteParserImpl::IsConstTensor(tensorPtr) && inputDataType == DataType::Float32 &&
1063 (tensorDataType == DataType::QAsymmU8 ||
1064 tensorDataType == DataType::QAsymmS8 ||
1065 tensorDataType == DataType::QSymmS8 ||
1066 tensorDataType == DataType::Signed32 ||
1067 tensorDataType == DataType::Signed64));
1070 void TfLiteParserImpl::RegisterProducerOfTensor(
size_t subgraphIndex,
1075 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
1076 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
1078 TensorSlots & tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
1084 if (tensorSlots.outputSlot !=
nullptr)
1086 throw ParseException(fmt::format(
"Another layer has already registered itself as the producer of "
1087 "subgraph:{} tensor:{} {}",
1093 tensorSlots.outputSlot = slot;
1096 void TfLiteParserImpl::RegisterConsumerOfTensor(
size_t subgraphIndex,
1101 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
1102 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
1104 TensorSlots& tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
1105 tensorSlots.inputSlots.push_back(slot);
1108 void TfLiteParserImpl::ParseCustomOperator(
size_t subgraphIndex,
size_t operatorIndex)
1110 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1113 auto customParserFunction = &TfLiteParserImpl::ParseUnsupportedOperator;
1116 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1117 const auto& customCode = m_Model->operator_codes[operatorPtr->opcode_index]->custom_code;
1120 auto iterator = m_CustomParserFunctions.find(customCode);
1121 if (iterator != m_CustomParserFunctions.end())
1123 customParserFunction = iterator->second;
1127 (this->*customParserFunction)(subgraphIndex, operatorIndex);
1130 void TfLiteParserImpl::ParseUnsupportedOperator(
size_t subgraphIndex,
size_t operatorIndex)
1132 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1134 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1136 auto opcodeIndex = operatorPtr->opcode_index;
1139 #if defined(ARMNN_POST_TFLITE_2_3)
1140 auto opcode = std::max(m_Model->operator_codes[opcodeIndex]->builtin_code,
1141 static_cast<tflite::BuiltinOperator
>(m_Model->operator_codes[opcodeIndex]->deprecated_builtin_code));
1143 auto opcode = m_Model->operator_codes[opcodeIndex]->builtin_code;
1146 if (!m_Options || !m_Options.value().m_StandInLayerForUnsupported)
1150 fmt::format(
"Operator not supported. "
1151 "subgraph:{} operator:{} "
1152 "opcode_index:{} opcode:{} / {} {}",
1157 tflite::EnumNameBuiltinOperator(opcode),
1161 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1162 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1164 const unsigned int numInputs = armnn::numeric_cast<unsigned int>(inputs.size());
1165 const unsigned int numOutputs = armnn::numeric_cast<unsigned int>(outputs.size());
1168 auto layerName = fmt::format(
"StandIn:{}:{}:{}", subgraphIndex, operatorIndex, opcode);
1171 IConnectableLayer* layer = m_Network->AddStandInLayer(descriptor, layerName.c_str());
1174 for (
unsigned int i = 0u; i < numOutputs; ++i)
1179 auto inputTensorIds = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1180 auto outputTensorIds = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1182 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIds);
1183 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIds);
1186 void TfLiteParserImpl::ParseCast(
size_t subgraphIndex,
size_t operatorIndex)
1188 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1190 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1192 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1195 auto layerName = fmt::format(
"Cast:{}:{}", subgraphIndex, operatorIndex);
1200 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
1203 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1204 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1206 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1207 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1210 void TfLiteParserImpl::ParseConv2D(
size_t subgraphIndex,
size_t operatorIndex)
1212 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1214 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1215 const auto* options = operatorPtr->builtin_options.AsConv2DOptions();
1219 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1220 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1224 inputs.size() == 3 ?
1232 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1233 armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1236 unsigned int inputHeight = inputTensorInfo.
GetShape()[1];
1237 unsigned int inputWidth = inputTensorInfo.
GetShape()[2];
1241 unsigned int filterHeight = filterTensorInfo.
GetShape()[1];
1242 unsigned int filterWidth = filterTensorInfo.
GetShape()[2];
1244 CalcPadding(inputHeight, filterHeight, desc.
m_StrideY,
1246 CalcPadding(inputWidth, filterWidth, desc.
m_StrideX,
1251 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1252 std::vector<unsigned int> tensorIndexesToRegister = { inputTensorIndexes[0], inputTensorIndexes[1] };
1254 auto layerName = fmt::format(
"Conv2D:{}:{}", subgraphIndex, operatorIndex);
1257 if (ShouldConstantTensorBeConverted(inputs[1], inputTensorInfo.
GetDataType(), filterTensorInfo.
GetDataType()))
1259 m_ConstantsToDequantize.emplace_back(inputs[1]->buffer);
1264 armnn::TensorInfo biasTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
1267 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
1269 if (ShouldConstantTensorBeConverted(inputs[2], inputTensorInfo.
GetDataType(), biasTensorInfo.
GetDataType()))
1271 m_ConstantsToDequantize.emplace_back(inputs[2]->buffer);
1277 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
1282 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
1284 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1286 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1287 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, { outputTensorIndexes[0] });
1291 #if defined(ARMNN_POST_TFLITE_2_4)
1292 void TfLiteParserImpl::ParseConv3D(
size_t subgraphIndex,
size_t operatorIndex)
1294 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1296 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1297 const auto* options = operatorPtr->builtin_options.AsConv3DOptions();
1311 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1314 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1317 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1318 armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1321 unsigned int inputDepth = inputTensorInfo.
GetShape()[1];
1322 unsigned int inputHeight = inputTensorInfo.
GetShape()[2];
1323 unsigned int inputWidth = inputTensorInfo.
GetShape()[3];
1326 unsigned int filterDepth = filterTensorInfo.
GetShape()[0];
1327 unsigned int filterHeight = filterTensorInfo.
GetShape()[1];
1328 unsigned int filterWidth = filterTensorInfo.
GetShape()[2];
1330 CalcPadding(inputDepth, filterDepth, desc.
m_StrideZ,
1332 CalcPadding(inputHeight, filterHeight, desc.
m_StrideY,
1334 CalcPadding(inputWidth, filterWidth, desc.
m_StrideX,
1337 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo, inputTensorInfo.
GetDataType());
1339 auto layerName = fmt::format(
"Conv3D:{}:{}", subgraphIndex, operatorIndex);
1341 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1344 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0], inputTensorIndexes[1]};
1346 if (inputs.size() == 3)
1351 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
1357 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
1361 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
1363 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1365 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1366 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1370 void TfLiteParserImpl::ParseDepthwiseConv2D(
size_t subgraphIndex,
size_t operatorIndex)
1372 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1374 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1375 const auto* options = operatorPtr->builtin_options.AsDepthwiseConv2DOptions();
1385 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1387 if (inputs.size() == 3)
1392 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1397 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1398 armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1401 unsigned int inputHeight = inputTensorInfo.
GetShape()[1];
1402 unsigned int inputWidth = inputTensorInfo.
GetShape()[2];
1405 unsigned int filterHeight = filterTensorInfo.
GetShape()[1];
1406 unsigned int filterWidth = filterTensorInfo.
GetShape()[2];
1408 CalcPadding(inputHeight, filterHeight, desc.
m_StrideY,
1410 CalcPadding(inputWidth, filterWidth, desc.
m_StrideX,
1414 auto layerName = fmt::format(
"DepthwiseConv2D:{}:{}", subgraphIndex, operatorIndex);
1416 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1419 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0], inputTensorIndexes[1]};
1426 TensorInfo biasTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
1429 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
1433 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
1438 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
1440 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1442 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1443 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1446 void TfLiteParserImpl::ParseDequantize(
size_t subgraphIndex,
size_t operatorIndex)
1448 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1450 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1453 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1456 auto layerName = fmt::format(
"Dequantize:{}:{}", subgraphIndex, operatorIndex);
1461 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
1464 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1465 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1467 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1468 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1471 void TfLiteParserImpl::ParseExpandDims(
size_t subgraphIndex,
size_t operatorIndex)
1473 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1475 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1478 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1481 auto layerName = fmt::format(
"ExpandDims:{}:{}", subgraphIndex, operatorIndex);
1483 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1486 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1496 int32_t axis = inputs[1]->shape[0];
1500 if (axis > inputDimSize || axis < 0 - (inputDimSize + 1))
1502 throw ParseException(
"axis must be in range [0 - (inputDimSize + 1), inputDimSize] inclusive");
1507 axis = inputDimSize + axis + 1;
1510 std::vector<unsigned int> shape(
static_cast<unsigned int>(inputDimSize) + 1);
1511 unsigned int inputShapeIndex = 0;
1512 for (
unsigned int i = 0; i < static_cast<unsigned int>(inputDimSize + 1); ++i)
1514 if (i ==
static_cast<unsigned int>(axis))
1520 shape[i] = inputTensorInfo.
GetShape()[inputShapeIndex];
1528 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
1531 reshapeDesc.
m_TargetShape = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0}).GetShape();
1536 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1537 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1539 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1540 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1543 void TfLiteParserImpl::ParseTranspose(
size_t subgraphIndex,
size_t operatorIndex)
1545 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1547 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1550 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1553 auto layerName = fmt::format(
"Transpose:{}:{}", subgraphIndex, operatorIndex);
1556 if (inputs.size() == 2)
1558 armnn::TensorInfo permuteTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1561 std::vector<unsigned int> permuteShape(numPermVecElements);
1562 ::memcpy(permuteShape.data(), permuteBufferPtr->data.data(), permuteTensorInfo.
GetNumBytes());
1567 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1569 IConnectableLayer* layer = m_Network->AddTransposeLayer(desc, layerName.c_str());
1572 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
1573 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1576 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1577 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1579 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1580 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1583 void TfLiteParserImpl::ParseTransposeConv(
size_t subgraphIndex,
size_t operatorIndex)
1585 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1587 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1588 const auto* options = operatorPtr->builtin_options.AsTransposeConvOptions();
1596 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1597 if (inputs.size() == 4)
1606 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1611 armnn::TensorInfo tensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1614 if (tensorInfo.
GetDataType() == DataType::Signed32)
1616 ::memcpy(output_shape.data(),
GetBuffer(m_Model, inputs[0]->buffer)->data.data(), tensorInfo.
GetNumBytes());
1618 if (tensorInfo.
GetDataType() == DataType::QAsymmU8)
1622 output_shape[i] =
GetBuffer(m_Model, inputs[0]->buffer)->data.data()[i];
1626 for (
int dimension : output_shape)
1628 desc.
m_OutputShape.push_back(
static_cast<unsigned int>(dimension));
1632 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
1633 armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1636 const unsigned int inputHeight = inputTensorInfo.
GetShape()[1];
1637 const unsigned int inputWidth = inputTensorInfo.
GetShape()[2];
1639 const unsigned int filterHeight = filterTensorInfo.
GetShape()[1];
1640 const unsigned int filterWidth = filterTensorInfo.
GetShape()[2];
1642 CalcPadding(inputHeight,
1650 CalcPadding(inputWidth,
1658 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo, inputTensorInfo.
GetDataType());
1661 auto layerName = fmt::format(
"TransposeConv:{}:{}", subgraphIndex, operatorIndex);
1665 auto biasTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 3);
1666 auto biasConstTensor = CreateConstTensorNonPermuted(inputs[3], biasTensorInfo, inputTensorInfo.
GetDataType());
1667 layer = m_Network->AddTransposeConvolution2dLayer(desc,
1668 filterTensorAndData.first,
1669 biasConstTensor.first,
1674 layer = m_Network->AddTransposeConvolution2dLayer(desc,
1675 filterTensorAndData.first,
1682 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0 , { 2, 1 });
1686 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1687 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[2]});
1689 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1690 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1693 void TfLiteParserImpl::ParseAveragePool2D(
size_t subgraphIndex,
size_t operatorIndex)
1695 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Average);
1698 void TfLiteParserImpl::ParseBatchMatMul(
size_t subgraphIndex,
size_t operatorIndex)
1700 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1702 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1705 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1708 auto layerName = fmt::format(
"BatchMatMul:{}:{}", subgraphIndex, operatorIndex);
1710 TensorInfo inputXTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1711 TensorInfo inputYTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1713 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1714 const auto* options = operatorPtr->builtin_options.AsBatchMatMulOptions();
1723 IConnectableLayer* layer = m_Network->AddBatchMatMulLayer(descriptor, layerName.c_str());
1726 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
1729 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1730 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1732 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1733 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1736 void TfLiteParserImpl::ParseBatchToSpaceND(
size_t subgraphIndex,
size_t operatorIndex)
1738 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1740 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1743 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1746 armnn::TensorInfo blockShapeTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1749 armnn::TensorInfo cropsTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
1752 std::vector<unsigned int> blockShape(blockShapeTensorInfo.
GetNumElements());
1753 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.
GetNumBytes());
1755 std::vector<unsigned int> cropsVector(cropsTensorInfo.
GetNumElements());
1756 ::memcpy(cropsVector.data(), cropsBufferPtr->data.data(), cropsTensorInfo.
GetNumBytes());
1759 std::vector<std::pair<unsigned int, unsigned int>> crops;
1760 for (
unsigned int i = 0; i < cropsTensorInfo.
GetNumElements() / step; ++i)
1762 crops.emplace_back(cropsVector[i * step], cropsVector[i * step + 1]);
1770 auto layerName = fmt::format(
"BatchToSpaceND:{}:{}", subgraphIndex, operatorIndex);
1772 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1774 IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(desc, layerName.c_str());
1777 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
1778 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1781 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1782 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1784 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1785 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1788 void TfLiteParserImpl::ParseL2Normalization(
size_t subgraphIndex,
size_t operatorIndex)
1790 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1792 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1795 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1800 auto layerName = fmt::format(
"L2Normalization:{}:{}", subgraphIndex, operatorIndex);
1801 IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(desc, layerName.c_str());
1805 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
1808 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1809 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1811 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1812 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1815 void TfLiteParserImpl::ParseMaxPool2D(
size_t subgraphIndex,
size_t operatorIndex)
1817 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Max);
1820 void TfLiteParserImpl::ParseMaximum(
size_t subgraphIndex,
size_t operatorIndex)
1822 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1824 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1827 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1830 auto layerName = fmt::format(
"Maximum:{}:{}", subgraphIndex, operatorIndex);
1832 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1833 TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1834 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName,
"Input 0",
"Input 1");
1839 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
1840 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1843 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1844 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1846 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1847 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1850 void TfLiteParserImpl::ParseMinimum(
size_t subgraphIndex,
size_t operatorIndex)
1852 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1854 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1857 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1860 auto layerName = fmt::format(
"Minimum:{}:{}", subgraphIndex, operatorIndex);
1862 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1863 TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1864 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName,
"Input 0",
"Input 1");
1869 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
1870 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1873 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1874 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1876 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1877 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1880 void TfLiteParserImpl::ParsePool(
size_t subgraphIndex,
1881 size_t operatorIndex,
1884 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1886 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1887 const auto* options = operatorPtr->builtin_options.AsPool2DOptions();
1891 std::string layerName;
1895 case PoolingAlgorithm::Average:
1897 fmt::format(
"AveragePool2D:{}:{}", subgraphIndex, operatorIndex);
1899 case PoolingAlgorithm::Max:
1901 fmt::format(
"MaxPool2D:{}:{}", subgraphIndex, operatorIndex);
1918 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1920 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1923 unsigned int inputHeight = inputTensorInfo.
GetShape()[1];
1924 unsigned int inputWidth = inputTensorInfo.
GetShape()[2];
1931 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1934 IConnectableLayer* layer = m_Network->AddPooling2dLayer(desc, layerName.c_str());
1937 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
1938 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1943 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1944 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1946 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1948 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1949 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1952 void TfLiteParserImpl::ParseSlice(
size_t subgraphIndex,
size_t operatorIndex)
1954 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1956 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1958 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1964 armnn::TensorInfo beginTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1967 std::vector<unsigned int> begin(beginTensorInfo.
GetNumElements());
1968 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.
GetNumBytes());
1971 armnn::TensorInfo sizeTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
1977 if (sizeBufferPtr->data.data())
1979 ::memcpy(signedSize.data(), sizeBufferPtr->data.data(), sizeTensorInfo.
GetNumBytes());
1983 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1985 for (
unsigned int i = 0; i < signedSize.size(); ++i)
1987 int signedValue = signedSize[i];
1989 if (signedValue < -1 || signedValue >
static_cast<int>(inputTensorInfo.
GetShape()[i] - begin[i]))
1991 throw ParseException(fmt::format(
"Invalid value for size {} size must be in range "
1992 "[-1, inputDimSize - begin] [-1, {}] inclusive {}",
1994 inputTensorInfo.
GetShape()[i] - begin[i],
1998 if (signedValue == -1)
2000 size[i] = inputTensorInfo.
GetShape()[i] - begin[i];
2004 size[i] =
static_cast<unsigned int>(signedValue);
2010 auto layerName = fmt::format(
"Slice:{}:{}", subgraphIndex, operatorIndex);
2012 IConnectableLayer*
const layer = m_Network->AddSliceLayer(desc, layerName.c_str());
2014 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2015 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
2020 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2021 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2024 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2025 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2028 void TfLiteParserImpl::ParseSoftmax(
size_t subgraphIndex,
size_t operatorIndex)
2030 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2031 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2032 const auto* options = operatorPtr->builtin_options.AsSoftmaxOptions();
2035 desc.
m_Beta = options->beta;
2037 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2039 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2042 auto layerName = fmt::format(
"Softmax:{}:{}", subgraphIndex, operatorIndex);
2043 IConnectableLayer*
const layer = m_Network->AddSoftmaxLayer(desc, layerName.c_str());
2045 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2050 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2051 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2054 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2055 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2058 void TfLiteParserImpl::ParseLogSoftmax(
size_t subgraphIndex,
size_t operatorIndex)
2060 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2064 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2066 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2069 auto layerName = fmt::format(
"LogSoftmax:{}:{}", subgraphIndex, operatorIndex);
2070 IConnectableLayer*
const layer = m_Network->AddLogSoftmaxLayer(desc, layerName.c_str());
2072 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2077 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2078 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2081 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2082 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2085 void TfLiteParserImpl::ParseSpaceToBatchND(
size_t subgraphIndex,
size_t operatorIndex)
2087 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2089 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2092 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2095 armnn::TensorInfo blockShapeTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2098 armnn::TensorInfo padListTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
2101 std::vector<unsigned int> blockShape(blockShapeTensorInfo.
GetNumElements());
2102 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.
GetNumBytes());
2104 std::vector<unsigned int> padListVector(padListTensorInfo.
GetNumElements());
2105 ::memcpy(padListVector.data(), padListBufferPtr->data.data(), padListTensorInfo.
GetNumBytes());
2108 std::vector<std::pair<unsigned int, unsigned int>> padList;
2109 for (
unsigned int i = 0; i < padListTensorInfo.
GetNumElements() / step; ++i)
2111 padList.emplace_back(padListVector[i * step], padListVector[i * step + 1]);
2119 auto layerName = fmt::format(
"SpaceToBatchND:{}:{}", subgraphIndex, operatorIndex);
2121 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2123 IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(desc, layerName.c_str());
2126 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2127 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
2130 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2131 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2133 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2134 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2141 static const uint32_t dimensionSequence[] = { 0, 1, 2, 3 };
2145 std::stringstream ss;
2146 ss <<
"Input tensor has unexpected number of dimensions:" << inputTensorInfo.
GetNumDimensions()
2147 <<
" shape:" << inputTensorInfo.
GetShape() <<
" "
2152 if (squeezeDims.empty())
2154 squeezeDims.assign(dimensionSequence,
2158 std::vector<uint32_t> outputDims;
2161 bool skipSqueeze = (std::find(squeezeDims.begin(), squeezeDims.end(), i) == squeezeDims.end());
2162 auto currentDimension = inputTensorInfo.
GetShape()[i];
2163 if (skipSqueeze || currentDimension != 1)
2165 outputDims.push_back(currentDimension);
2169 if (outputDims.size() > 4)
2171 std::stringstream ss;
2172 ss <<
"Output tensor has unexpected number of dimensions:" << inputTensorInfo.
GetNumDimensions()
2173 <<
" shape:" << inputTensorInfo.
GetShape() <<
" "
2185 return outTensorInfo;
2188 void TfLiteParserImpl::ParseShape(
size_t subgraphIndex,
size_t operatorIndex)
2190 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(
"Shape:{}:{}", subgraphIndex, operatorIndex);
2202 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2211 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
2215 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2216 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2218 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2219 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2222 void TfLiteParserImpl::ParseSqueeze(
size_t subgraphIndex,
size_t operatorIndex)
2224 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2226 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2229 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2232 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2233 const auto * options = operatorPtr->builtin_options.AsSqueezeOptions();
2234 auto layerName = fmt::format(
"Squeeze:{}:{}", subgraphIndex, operatorIndex);
2236 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2238 std::vector<uint32_t> squeezeDim;
2241 if (options->squeeze_dims.size() == 1 && options->squeeze_dims[0] < 0)
2244 squeezeDim.push_back(
static_cast<uint32_t
>(dim));
2248 squeezeDim = AsUnsignedVector(options->squeeze_dims);
2253 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
2258 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
2262 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2263 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2265 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2266 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2269 void TfLiteParserImpl::ParseStridedSlice(
size_t subgraphIndex,
size_t operatorIndex)
2271 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2273 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2276 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2279 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2280 const auto* options = operatorPtr->builtin_options.AsStridedSliceOptions();
2290 armnn::TensorInfo beginTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2294 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.
GetNumBytes());
2296 armnn::TensorInfo endTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
2300 ::memcpy(end.data(), endBufferPtr->data.data(), endTensorInfo.
GetNumBytes());
2302 armnn::TensorInfo strideTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 3);
2306 ::memcpy(stride.data(), strideBufferPtr->data.data(), strideTensorInfo.
GetNumBytes());
2312 auto layerName = fmt::format(
"StridedSlice:{}:{}", subgraphIndex, operatorIndex);
2313 IConnectableLayer* layer = m_Network->AddStridedSliceLayer(desc, layerName.c_str());
2316 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2319 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2320 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2322 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2323 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2326 void TfLiteParserImpl::ParseSub(
size_t subgraphIndex,
size_t operatorIndex)
2328 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2330 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2331 const auto* options = operatorPtr->builtin_options.AsSubOptions();
2333 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2336 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2339 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2340 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2342 auto layerName = fmt::format(
"Sub:{}:{}", subgraphIndex, operatorIndex);
2346 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
2349 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2350 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2352 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2354 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2355 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2358 void TfLiteParserImpl::ParseDiv(
size_t subgraphIndex,
size_t operatorIndex)
2360 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2362 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2363 const auto* options = operatorPtr->builtin_options.AsDivOptions();
2365 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2368 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2371 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2372 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2374 auto layerName = fmt::format(
"Div:{}:{}", subgraphIndex, operatorIndex);
2378 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
2381 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2382 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2383 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2385 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2386 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2389 void TfLiteParserImpl::ParseFloorDiv(
size_t subgraphIndex,
size_t operatorIndex)
2391 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2393 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2396 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2399 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2400 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2402 auto layerName = fmt::format(
"Div:{}:{}", subgraphIndex, operatorIndex);
2406 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
2409 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2410 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2411 layer = AddFusedFloorLayer(layer, 0);
2413 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2414 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2417 void TfLiteParserImpl::ParseAdd(
size_t subgraphIndex,
size_t operatorIndex)
2419 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2421 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2422 const auto* options = operatorPtr->builtin_options.AsAddOptions();
2424 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2427 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2430 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2431 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2433 auto layerName = fmt::format(
"Add:{}:{}", subgraphIndex, operatorIndex);
2437 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
2440 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2441 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2442 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2444 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2445 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2448 void TfLiteParserImpl::ParseMul(
size_t subgraphIndex,
size_t operatorIndex)
2450 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2452 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2453 const auto* options = operatorPtr->builtin_options.AsMulOptions();
2455 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2458 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2461 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2462 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2464 auto layerName = fmt::format(
"Mul:{}:{}", subgraphIndex, operatorIndex);
2465 IConnectableLayer* layer = m_Network->AddMultiplicationLayer(layerName.c_str());
2468 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
2471 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2472 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2473 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2475 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2476 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2479 void TfLiteParserImpl::ParseMean(
size_t subgraphIndex,
size_t operatorIndex)
2481 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2483 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2485 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2488 armnn::TensorInfo dimTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2493 ::memcpy(axis.data(), bufferPtr->data.data(), dimTensorInfo.
GetNumBytes());
2496 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2503 auto layerName = fmt::format(
"Mean:{}:{}", subgraphIndex, operatorIndex);
2507 outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2510 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2511 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2513 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2514 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2517 void TfLiteParserImpl::ParsePad(
size_t subgraphIndex,
size_t operatorIndex)
2519 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2526 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2527 armnn::TensorInfo padTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2529 std::vector<unsigned int> padBuffer = GetUIntBuffer(padTensorInfo, m_Model, inputs[1]->buffer);
2533 auto opcode = GetOpCode(m_Model, subgraphIndex, operatorIndex);
2535 if (opcode == tflite::BuiltinOperator_PAD)
2544 else if (opcode == tflite::BuiltinOperator_PADV2)
2548 armnn::TensorInfo padValueTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
2557 if (padValueBufferPtr->data.size() > 0)
2563 std::vector<float> padValueBuffer(padValueTensorInfo.
GetNumElements());
2564 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2570 std::vector<uint8_t> padValueBuffer(padValueTensorInfo.
GetNumElements());
2571 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2572 desc.
m_PadValue = armnn::Dequantize<uint8_t>(padValueBuffer[0],
2580 std::vector<int8_t> padValueBuffer(padValueTensorInfo.
GetNumElements());
2581 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2582 desc.
m_PadValue = armnn::Dequantize<int8_t>(padValueBuffer[0],
2596 for (
unsigned int i = 0; i < padTensorInfo.
GetNumElements() / step; ++i)
2598 desc.
m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
2601 auto layerName = (opcode == tflite::BuiltinOperator_PAD) ? fmt::format(
"Pad:{}:{}", subgraphIndex, operatorIndex)
2602 : fmt::format(
"PadV2:{}:{}", subgraphIndex, operatorIndex);
2606 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2609 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2610 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2612 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2613 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2616 void TfLiteParserImpl::ParseMirrorPad(
size_t subgraphIndex,
size_t operatorIndex)
2618 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2626 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2628 armnn::TensorInfo padTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2631 std::vector<unsigned int> padBuffer(padTensorInfo.
GetNumElements());
2632 ::memcpy(padBuffer.data(), bufferPtr->data.data(), padTensorInfo.
GetNumBytes());
2636 for (
unsigned int i = 0; i < padTensorInfo.
GetNumElements() / step; ++i)
2638 desc.
m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
2641 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2642 const auto* options = operatorPtr->builtin_options.AsMirrorPadOptions();
2644 if (options->mode == tflite::MirrorPadMode_REFLECT)
2648 else if (options->mode == tflite::MirrorPadMode_SYMMETRIC)
2659 auto inputShape = inputTensorInfo.
GetShape();
2662 const unsigned int isReflect =
static_cast<unsigned int>(desc.
m_PaddingMode == PaddingMode::Reflect);
2663 for(
unsigned int i = 0; i < padList.size(); ++i)
2665 if(padList.at(i).first > (inputShape[i] - isReflect) ||
2666 padList.at(i).second > (inputShape[i] - isReflect))
2669 "equal (Symmetric) to the dimension size.");
2673 auto layerName = fmt::format(
"MirrorPad:{}:{}", subgraphIndex, operatorIndex);
2677 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2680 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2681 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2683 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2684 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2687 void TfLiteParserImpl::ParsePrelu(
size_t subgraphIndex,
size_t operatorIndex)
2689 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2691 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2694 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2697 auto layerName = fmt::format(
"Prelu:{}:{}", subgraphIndex, operatorIndex);
2699 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2700 armnn::TensorInfo alphaTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2706 if (IsConstTensor(inputs[1]))
2708 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2710 RegisterConsumerOfTensor(subgraphIndex, inputTensorIndexes[0], slot);
2712 auto alphaTensorAndData = CreateConstTensorNonPermuted(inputs[1], alphaTensorInfo,
2714 std::string constLayerName = fmt::format(
"Constant:{}", inputs[1]->name);
2716 m_Network->AddConstantLayer(alphaTensorAndData.first, constLayerName.c_str());
2721 RegisterOutputSlots(subgraphIndex,
2722 VIRTUAL_OPERATOR_ID,
2724 { inputTensorIndexes[1] });
2728 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2729 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIndexes);
2732 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
2733 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
2736 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2737 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2740 void TfLiteParserImpl::ParseQuantize(
size_t subgraphIndex,
size_t operatorIndex)
2742 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2744 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2747 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2750 auto layerName = fmt::format(
"Quantize:{}:{}", subgraphIndex, operatorIndex);
2755 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2758 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2759 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2761 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2762 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2765 void TfLiteParserImpl::ParseRelu(
size_t subgraphIndex,
size_t operatorIndex)
2767 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::ReLu);
2770 void TfLiteParserImpl::ParseRelu6(
size_t subgraphIndex,
size_t operatorIndex)
2772 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::BoundedReLu);
2775 void TfLiteParserImpl::ParseLeakyRelu(
size_t subgraphIndex,
size_t operatorIndex)
2777 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::LeakyReLu);
2780 void TfLiteParserImpl::ParseLogistic(
size_t subgraphIndex,
size_t operatorIndex)
2782 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::Sigmoid);
2785 void TfLiteParserImpl::ParseTanH(
size_t subgraphIndex,
size_t operatorIndex)
2787 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::TanH);
2790 void TfLiteParserImpl::ParseElu(
size_t subgraphIndex,
size_t operatorIndex)
2792 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::Elu);
2795 void TfLiteParserImpl::ParseHardSwish(
size_t subgraphIndex,
size_t operatorIndex)
2797 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::HardSwish);
2800 void TfLiteParserImpl::ParseActivation(
size_t subgraphIndex,
size_t operatorIndex,
ActivationFunction activationType)
2802 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2803 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2806 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2809 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2812 auto layerName = fmt::format(
"Activation:");
2816 switch (activationType)
2818 case ActivationFunction::ReLu:
2820 layerName += fmt::format(
"RELU:{}:{}", subgraphIndex, operatorIndex);
2823 case ActivationFunction::BoundedReLu:
2825 layerName += fmt::format(
"RELU6:{}:{}", subgraphIndex, operatorIndex);
2826 activationDesc.
m_A = 6.0f;
2827 activationDesc.
m_B = 0.0f;
2830 case ActivationFunction::Sigmoid:
2832 layerName += fmt::format(
"SIGMOID:{}:{}", subgraphIndex, operatorIndex);
2835 case ActivationFunction::TanH:
2837 layerName += fmt::format(
"TANH:{}:{}", subgraphIndex, operatorIndex);
2838 activationDesc.
m_A = 1.0f;
2839 activationDesc.
m_B = 1.0f;
2842 case ActivationFunction::LeakyReLu:
2844 layerName += fmt::format(
"LEAKYRELU:{}:{}", subgraphIndex, operatorIndex);
2845 const auto* options = operatorPtr->builtin_options.AsLeakyReluOptions();
2846 activationDesc.
m_A = options->alpha;
2849 case ActivationFunction::Elu:
2851 layerName += fmt::format(
"ELU:{}:{}", subgraphIndex, operatorIndex);
2852 activationDesc.
m_A = 1.0f;
2855 case ActivationFunction::HardSwish:
2857 layerName += fmt::format(
"HARDSWISH:{}:{}", subgraphIndex, operatorIndex);
2863 fmt::format(
"Unexpected ActivationFunction[{}] when creating layerName {} ",
2868 IConnectableLayer*
const layer = m_Network->AddActivationLayer(activationDesc, layerName.c_str());
2870 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2875 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2876 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2879 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2880 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2883 const std::vector<int32_t>& targetDimsIn)
2885 std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
2886 const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
2888 if (stretchDim != targetDimsIn.end())
2890 if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
2893 fmt::format(
"At most one component of shape can be -1 {}",
CHECK_LOCATION().AsString()));
2896 auto targetNumElements =
2897 armnn::numeric_cast<unsigned int>(
2898 std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
2900 auto stretchIndex =
static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
2901 outputDims[stretchIndex] = inputTensorInfo.
GetNumElements() / targetNumElements;
2912 void TfLiteParserImpl::ParseReshape(
size_t subgraphIndex,
size_t operatorIndex)
2914 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2916 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2918 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2921 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2922 const auto* options = operatorPtr->builtin_options.AsReshapeOptions();
2923 auto layerName = fmt::format(
"Reshape:{}:{}", subgraphIndex, operatorIndex);
2925 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2927 CheckMatchingQuantization(inputTensorInfo, actualOutputTensorInfo, layerName,
"Input 0",
"Output 0");
2933 std::vector<int32_t> targetShape;
2934 bool targetShapeFound =
false;
2936 if (options !=
nullptr)
2939 if (options->new_shape.empty() ==
false)
2941 targetShape = options->new_shape;
2942 targetShapeFound =
true;
2947 if (!targetShapeFound)
2950 if (inputs.size() > 1 && inputs[1] !=
nullptr)
2952 if (inputs[1]->is_variable)
2957 if (inputs[1]->shape.size() != 1)
2962 if (inputs[1]->type != tflite::TensorType_INT32)
2968 auto bufferPtr =
GetBuffer(m_Model, inputs[1]->buffer);
2969 auto values =
reinterpret_cast<const int32_t*
>(bufferPtr->data.data());
2972 for (
int i = 0; i < inputs[1]->shape[0]; ++i)
2974 targetShape.push_back(values[i]);
2986 for (
unsigned int i = 0; i < actualOutputTensorInfo.
GetShape().GetNumDimensions(); ++i)
2988 targetShape.push_back(actualOutputTensorInfo.
GetShape()[i]);
2992 else if (reshapeShapes[0] > 2)
2994 throw ParseException(fmt::format(
"Invalid input shape '{}' in Reshape layer '{}' {}. "
2995 "When inferring during runtime, the parser only supports "
2996 "shape (batch, -1) or (-1) for target shape input.",
3003 const int32_t numInputElements = inputTensorInfo.
GetNumElements();
3004 const int32_t inputTensorShape = inputTensorInfo.
GetShape()[0];
3005 if (reshapeShapes[0] == 1)
3007 targetShape = {numInputElements};
3009 else if (reshapeShapes[0] == 2)
3011 targetShape = {inputTensorShape, numInputElements / inputTensorShape};
3015 catch (
const std::exception& exc)
3018 "Reshape operation. Reshape operator target shape input buffer data "
3019 "is null. " << exc.what());
3026 "At least one method required");
3039 if (inputs.size() > 1 && !
CheckShape(reshapeOutputTensorShape, outputs[0]->shape))
3043 std::vector<int32_t> secondaryOutputTargetShape = outputs[0]->shape_signature;
3050 if (!
CheckShape(reshapeOutputTensorShape, secondaryReshapeOutputTensorInfo.
GetShape()))
3052 std::stringstream ss;
3053 ss <<
"New shape defined in reshape parameters "
3054 << reshapeOutputTensorShape
3055 <<
" does not equal output shape "
3056 << actualOutputTensorInfo.
GetShape()
3066 m_TensorInfos[outputTensorIds[0]] = reshapeOutputTensorInfo;
3068 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
3072 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3073 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3075 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3076 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3079 void TfLiteParserImpl::ParseResizeBilinear(
size_t subgraphIndex,
size_t operatorIndex)
3081 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::Bilinear);
3084 void TfLiteParserImpl::ParseResizeNearestNeighbor(
size_t subgraphIndex,
size_t operatorIndex)
3086 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::NearestNeighbor);
3089 void TfLiteParserImpl::ParseResize(
size_t subgraphIndex,
size_t operatorIndex,
ResizeMethod resizeMethod)
3091 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3093 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3096 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3099 armnn::TensorInfo sizeTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
3102 std::vector<int32_t> sizeTensorData(sizeTensorInfo.
GetNumElements());
3105 ::memcpy(sizeTensorData.data(), sizeBufferPtr->data.data(), sizeTensorInfo.
GetNumBytes());
3110 desc.
m_TargetWidth =
static_cast<uint32_t
> (sizeTensorData[1]);
3113 auto layerName = fmt::format(
"Resize:");
3115 switch (resizeMethod)
3117 case ResizeMethod::Bilinear:
3119 layerName += fmt::format(
"BILINEAR:{}:{}", subgraphIndex, operatorIndex);
3121 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3122 const auto * options = operatorPtr->builtin_options.AsResizeBilinearOptions();
3127 case ResizeMethod::NearestNeighbor:
3129 layerName += fmt::format(
"NEARESTNEIGHBOR:{}:{}", subgraphIndex, operatorIndex);
3135 fmt::format(
"Unexpected ResizeMethod[{}] when creating layerName {} ",
3140 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3144 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
3145 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
3148 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3149 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3151 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3152 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3155 void TfLiteParserImpl::ParseConcatenation(
size_t subgraphIndex,
size_t operatorIndex)
3157 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3159 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3160 const auto* options = operatorPtr->builtin_options.AsConcatenationOptions();
3164 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3165 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3170 unsigned int numConcatView =
static_cast<unsigned int>(inputs.size());
3171 uint32_t inputRank = InputTensorInfo(subgraphIndex, operatorIndex, 0).
GetNumDimensions();
3173 const unsigned int concatDimInput =
static_cast<unsigned int>(
3174 (
static_cast<int>(inputRank) + options->axis) %
static_cast<int>(inputRank));
3176 OriginsDescriptor concatDescriptor(
static_cast<uint32_t
>(numConcatView), inputRank);
3177 concatDescriptor.SetConcatAxis(concatDimInput);
3178 unsigned int mergeDimOrigin = 0;
3180 for (
unsigned int viewIndex = 0; viewIndex < numConcatView; ++viewIndex)
3182 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, viewIndex);
3186 inputTensorInfo, concatDescriptor, concatDimInput, viewIndex, mergeDimOrigin);
3189 auto layerName = fmt::format(
"Concatenation:{}:{}", subgraphIndex, operatorIndex);
3191 IConnectableLayer* layer = m_Network->AddConcatLayer(concatDescriptor, layerName.c_str());
3193 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {});
3196 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3197 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
3200 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
3202 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3203 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3206 void TfLiteParserImpl::ParseFullyConnected(
size_t subgraphIndex,
size_t operatorIndex)
3208 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3210 const auto& operatorRfr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3211 const auto options = operatorRfr->builtin_options.AsFullyConnectedOptions();
3219 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3220 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3223 armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
3226 int32_t weightsDimension =
static_cast<int32_t
>(filterTensorInfo.
GetNumDimensions());
3227 if (weightsDimension != 2)
3230 fmt::format(
"Dimension {} for Fully Connected weights is not supported by Armnn. "
3237 auto layerName = fmt::format(
"FullyConnected:{}:{}", subgraphIndex, operatorIndex);
3239 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3241 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0]};
3242 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3247 tensorIndexesToRegister.emplace_back(inputTensorIndexes[1]);
3249 if (ShouldConstantTensorBeConverted(inputs[1], inputTensorInfo.
GetDataType(), filterTensorInfo.
GetDataType()))
3251 m_ConstantsToDequantize.emplace_back(inputs[1]->buffer);
3254 if (inputs.size() == 3)
3257 armnn::TensorInfo biasTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
3260 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
3262 if (ShouldConstantTensorBeConverted(inputs[2], inputTensorInfo.
GetDataType(), biasTensorInfo.
GetDataType()))
3264 m_ConstantsToDequantize.emplace_back(inputs[2]->buffer);
3269 layer = m_Network->AddFullyConnectedLayer(desc, layerName.c_str());
3273 unsigned int startingSlotIndex = 0;
3280 std::vector<unsigned int> reshapedDimensions(2);
3281 reshapedDimensions[1] = filterTensorInfo.
GetShape()[1];
3282 reshapedDimensions[0] = inputTensorInfo.
GetNumElements() / reshapedDimensions[1];
3284 if (inputTensorInfo.
GetNumElements() % reshapedDimensions[1] != 0)
3287 fmt::format(
"Failed to deduce input tensor shape from filter size {} {}",
3288 reshapedDimensions[1],
3292 armnn::TensorInfo reshapedTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3294 inputTensorInfo = reshapedTensorInfo;
3296 std::string reshapeLayerName = fmt::format(
"Reshape_for:{}", layer->
GetName());
3300 reshapeLayerName.c_str());
3305 RegisterInputSlots(subgraphIndex, operatorIndex, reshapeLayer, {inputTensorIndexes[0]});
3307 tensorIndexesToRegister.erase(tensorIndexesToRegister.begin());
3308 startingSlotIndex = 1;
3311 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister, startingSlotIndex);
3313 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromShapes(subgraphIndex, operatorIndex, layer, 0,
3322 std::vector<unsigned int> reshapedDimensions(2);
3323 reshapedDimensions[1] = filterTensorInfo.
GetShape()[0];
3324 reshapedDimensions[0] = outputTensorInfo.
GetNumElements() / reshapedDimensions[1];
3326 if (outputTensorInfo.
GetNumElements() % reshapedDimensions[1] != 0)
3329 fmt::format(
"Failed to deduce output tensor shape from filter size {} {}",
3330 reshapedDimensions[1],
3336 std::string reshapeLayerName = fmt::format(
"ExpandDims:{}:{}", subgraphIndex, operatorIndex);
3337 layer = AddReshapeLayer(layer, 0, reshapeLayerName, outputTensorInfo);
3342 options->fused_activation_function);
3345 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3346 RegisterOutputSlots(subgraphIndex, operatorIndex, fusedActivationLayer, {outputTensorIndexes[0]});
3351 void TfLiteParserImpl::ParseDetectionPostProcess(
size_t subgraphIndex,
size_t operatorIndex)
3353 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3355 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3357 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3358 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3362 auto custom_options = operatorPtr->custom_options;
3363 const flexbuffers::Map& m = flexbuffers::GetRoot(custom_options.data(), custom_options.size()).AsMap();
3372 desc.
m_ScaleH = m[
"h_scale"].AsFloat();
3373 desc.
m_ScaleW = m[
"w_scale"].AsFloat();
3374 desc.
m_ScaleX = m[
"x_scale"].AsFloat();
3375 desc.
m_ScaleY = m[
"y_scale"].AsFloat();
3377 if (!(m[
"use_regular_nms"].IsNull()))
3381 if (!(m[
"detections_per_class"].IsNull()))
3389 "must be positive and less than or equal to 1.");
3392 armnn::TensorInfo anchorTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
3393 auto anchorTensorAndData = CreateConstTensorNonPermuted(inputs[2], anchorTensorInfo);
3395 auto layerName = fmt::format(
"DetectionPostProcess:{}:{}", subgraphIndex, operatorIndex);
3396 IConnectableLayer* layer = m_Network->AddDetectionPostProcessLayer(desc, anchorTensorAndData,
3404 m_OverriddenOutputShapes.push_back({ 1, numDetectedBox, 4 });
3405 m_OverriddenOutputShapes.push_back({ 1, numDetectedBox });
3406 m_OverriddenOutputShapes.push_back({ 1, numDetectedBox });
3407 m_OverriddenOutputShapes.push_back({ 1 });
3409 for (
unsigned int i = 0 ; i < outputs.size() ; ++i)
3417 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3418 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3421 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3422 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0],
3423 outputTensorIndexes[1],
3424 outputTensorIndexes[2],
3425 outputTensorIndexes[3]});
3429 void TfLiteParserImpl::ParsePack(
size_t subgraphIndex,
size_t operatorIndex)
3431 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3433 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3434 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3437 if (inputs.size() < 1)
3442 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3443 const auto* options = operatorPtr->builtin_options.AsPackOptions();
3446 desc.
m_Axis =
static_cast<uint32_t
>(options->axis);
3447 desc.
m_NumInputs =
static_cast<uint32_t
>(inputs.size());
3450 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3453 auto layerName = fmt::format(
"Pack:{}:{}", subgraphIndex, operatorIndex);
3458 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {});
3461 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3462 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
3464 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3465 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3468 void TfLiteParserImpl::ParseUnidirectionalSequenceLSTM(
size_t subgraphIndex,
size_t operatorIndex)
3470 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3472 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3473 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3475 if (inputs.size() < 2)
3477 throw ParseException(
"UnidirectionalSequenceLSTM must have at least 2 input.");
3480 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3481 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
3482 const auto nodeParams = operatorPtr->builtin_options.AsUnidirectionalSequenceLSTMOptions();
3484 auto inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3492 if (IsOptionalOperandPresent(operatorPtr->inputs[1]))
3494 params.
m_InputToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[1]].get(),
3495 inputTensorInfo).first;
3499 inputTensorInfo).first;
3500 params.
m_InputToCellWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[3]].get(),
3501 inputTensorInfo).first;
3503 inputTensorInfo).first;
3506 if (IsOptionalOperandPresent(operatorPtr->inputs[5]))
3509 inputTensorInfo).first;
3513 inputTensorInfo).first;
3515 inputTensorInfo).first;
3517 inputTensorInfo).first;
3520 if (IsOptionalOperandPresent(operatorPtr->inputs[9]))
3522 params.
m_CellToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[9]].get(),
3523 inputTensorInfo).first;
3526 if (IsOptionalOperandPresent(operatorPtr->inputs[10]))
3528 params.
m_CellToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[10]].get(),
3529 inputTensorInfo).first;
3532 if (IsOptionalOperandPresent(operatorPtr->inputs[11]))
3534 params.
m_CellToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[11]].get(),
3535 inputTensorInfo).first;
3539 if (IsOptionalOperandPresent(operatorPtr->inputs[12]))
3541 params.
m_InputGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[12]].get(),
3542 inputTensorInfo).first;
3545 params.
m_ForgetGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[13]].get(),
3546 inputTensorInfo).first;
3547 params.
m_CellBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[14]].get(),
3548 inputTensorInfo).first;
3549 params.
m_OutputGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[15]].get(),
3550 inputTensorInfo).first;
3553 if (IsOptionalOperandPresent(operatorPtr->inputs[16]))
3555 params.
m_ProjectionWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[16]].get(),
3556 inputTensorInfo).first;
3559 if (IsOptionalOperandPresent(operatorPtr->inputs[17]))
3561 params.
m_ProjectionBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[17]].get(),
3562 inputTensorInfo).first;
3567 m_ConstantsToBeCreated.push_back(operatorPtr->inputs[18]);
3569 m_ConstantsToBeCreated.push_back(operatorPtr->inputs[19]);
3572 if (inputs.size() >= 21 && IsOptionalOperandPresent(operatorPtr->inputs[20]))
3575 inputTensorInfo).first;
3578 if (inputs.size() >= 22 && IsOptionalOperandPresent(operatorPtr->inputs[21]))
3581 inputTensorInfo).first;
3584 if (inputs.size() >= 23 && IsOptionalOperandPresent(operatorPtr->inputs[22]))
3586 params.
m_CellLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[22]].get(),
3587 inputTensorInfo).first;
3590 if (inputs.size() >= 24 && IsOptionalOperandPresent(operatorPtr->inputs[23]))
3593 inputTensorInfo).first;
3614 auto inputIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[0]].get(),
3615 inputTensorInfo).first;
3616 auto inputIntermediateTensorInfo = inputIntermediate->GetInfo();
3619 auto forgetIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[1]].get(),
3620 inputTensorInfo).first;
3621 auto forgetIntermediateTensorInfo = forgetIntermediate->GetInfo();
3624 auto cellIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[2]].get(),
3625 inputTensorInfo).first;
3626 auto cellIntermediateTensorInfo = cellIntermediate->GetInfo();
3629 auto outputIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[3]].get(),
3630 inputTensorInfo).first;
3631 auto outputIntermediateTensorInfo = outputIntermediate->GetInfo();
3636 float defaultIntermediate = std::pow(2, -12);
3643 if (operatorPtr->intermediates.size() > 4)
3645 auto hiddentensor = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[4]].get(),
3646 inputTensorInfo).first;
3651 unsigned int batchSize = inputTensorInfo.
GetShape()[0];
3652 unsigned int outputSize = outputTensorInfo.
GetShape()[2];
3653 unsigned int numUnits = cellStateInInfo.
GetShape()[1];
3659 armnn::TensorInfo scratchBufferTensorInfo({batchSize, numUnits * 3}, dataType, qScale, qOffset);
3662 scratchBufferTensorInfo =
armnn::TensorInfo({batchSize, numUnits * 4}, dataType, qScale, qOffset);
3668 armnn::TensorInfo outputStateOutTensorInfo({batchSize, outputSize}, dataType, qScale, qOffset);
3718 auto layerName = fmt::format(
"UnidirectionalSequenceLSTM:{}:{}", subgraphIndex, operatorIndex);
3724 auto inputTensorIndexes = AsUnsignedVector({operatorPtr->inputs[0],
3725 operatorPtr->inputs[18],
3726 operatorPtr->inputs[19]});
3727 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0],
3728 inputTensorIndexes[1],
3729 inputTensorIndexes[2]});
3731 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3737 unsigned int tensorIndex = outputTensorIndexes[0];
3739 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
3742 void TfLiteParserImpl::ParseUnpack(
size_t subgraphIndex,
size_t operatorIndex)
3744 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3746 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3747 const auto* options = operatorPtr->builtin_options.AsUnpackOptions();
3752 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3755 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3760 fmt::format(
"The unpack axis: {} cannot be greater than or equal to "
3761 "the number of input dimension {} {}",
3771 unpackNum = inputTensorInfo.
GetShape()[unpackAxis];
3777 throw ParseException(
"Number to unpack must greater than zero.");
3780 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3784 std::vector<unsigned int> unpackDimSizes(inputDimSize);
3787 for (
unsigned int i = 0; i < inputDimSize; ++i)
3789 unpackDimSizes[i] = inputTensorInfo.
GetShape()[i];
3792 if (unpackDimSizes[unpackAxis] != unpackNum)
3794 throw ParseException(
"Number to unpack must be the same as length of the dimension to "
3798 unpackDimSizes[unpackAxis] /= unpackNum;
3800 SplitterDescriptor splitDesc(unpackNum,
static_cast<unsigned int>(unpackDimSizes.size()));
3801 for (
unsigned int j = 0; j < unpackNum; ++j)
3804 for (
unsigned int dimIdx = 0; dimIdx < unpackDimSizes.size(); ++dimIdx)
3806 splitDesc.SetViewSize(j, dimIdx, unpackDimSizes[dimIdx]);
3808 splitDesc.SetViewOriginCoord(j, unpackAxis, unpackDimSizes[unpackAxis] * j);
3811 auto layerName = fmt::format(
"Unpack:{}:{}", subgraphIndex, operatorIndex);
3812 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
3816 unpackDimSizes.data());
3818 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3819 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3821 std::vector<unsigned int> reshapeDims;
3822 for (
unsigned int axis = 0; axis < splitOutShape.
GetNumDimensions(); ++axis)
3824 if (axis != unpackAxis)
3826 reshapeDims.push_back(splitOutShape[axis]);
3836 std::string reshapeLayerName = fmt::format(
"Reshape_for:{}", layer->
GetName());
3851 RegisterProducerOfTensor(subgraphIndex, reshapedOutputId, slot);
3855 void TfLiteParserImpl::ParseSplit(
size_t subgraphIndex,
size_t operatorIndex)
3857 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3859 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3860 const auto* options = operatorPtr->builtin_options.AsSplitOptions();
3867 throw ParseException(
"Number to splits must greater than zero.");
3870 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3872 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3875 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
3876 armnn::TensorInfo axisTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3880 if (axisBufferPtr ==
nullptr)
3883 fmt::format(
"Operation has invalid inputs. Failed to read axis. {}",
3888 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.
GetNumBytes());
3889 int32_t axis = axisData[0];
3891 auto inputDimensions =
static_cast<int32_t
>(inputTensorInfo.
GetNumDimensions());
3892 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3898 fmt::format(
"Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
3909 fmt::format(
"The number of dimensions: {} for input tensors of the split op cannot be greater than {} {}",
3915 std::vector<unsigned int> splitterDimSizes(inputDimSize);
3918 for (
unsigned int i = 0; i < inputDimSize; ++i)
3920 splitterDimSizes[i] = inputTensorInfo.
GetShape()[i];
3923 if (splitterDimSizes[splitDim] % numSplits != 0)
3925 throw ParseException(
"Number of splits must evenly divide the dimension");
3927 splitterDimSizes[splitDim] /= numSplits;
3930 for (
unsigned int j = 0; j < numSplits; ++j)
3933 for (
unsigned int dimIdx = 0; dimIdx < splitterDimSizes.size(); ++dimIdx)
3935 splitDesc.SetViewSize(j, dimIdx, splitterDimSizes[dimIdx]);
3937 splitDesc.SetViewOriginCoord(j, splitDim, splitterDimSizes[splitDim] * j);
3940 auto layerName = fmt::format(
"Split:{}:{}", subgraphIndex, operatorIndex);
3941 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
3944 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3945 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[1]});
3953 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3954 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3959 int numDims = armnn::numeric_cast<int>(numDimsIn);
3960 int v = idx < 0 ? numDims + idx : idx;
3964 return static_cast<unsigned int>(v);
3967 void TfLiteParserImpl::ParseSplitV(
size_t subgraphIndex,
size_t operatorIndex)
3969 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3971 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3972 const auto* options = operatorPtr->builtin_options.AsSplitVOptions();
3974 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3977 auto& inputTensor = inputs[0];
3978 auto& splitsTensor = inputs[1];
3979 auto& axisTensor = inputs[2];
3991 fmt::format(
"The number of dimensions: {} for input tensors of the "
3992 "SplitV op cannot be greater than {} {}",
4000 if (axisBufferPtr ==
nullptr)
4003 fmt::format(
"Operation has invalid inputs. Failed to read axis. {}",
4008 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.
GetNumBytes());
4009 int32_t axis = axisData[0];
4011 auto inputDimensions =
static_cast<int32_t
>(inputTensorInfo.
GetNumDimensions());
4012 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4018 fmt::format(
"Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
4026 unsigned int numSplits{0};
4042 std::vector<int> splitsData(numSplits);
4044 ::memcpy(splitsData.data(), splitsBufferPtr->data.data(), splitsInfo.
GetNumBytes());
4046 unsigned int idx = 0;
4048 unsigned int inferIdx{0};
4050 for (
auto split : splitsData)
4064 if (numInferred == 0)
4066 if (splitSum != armnn::numeric_cast<int>(inputTensorInfo.
GetShape()[splitDim]))
4068 throw ParseException(
"SplitV split_sizes does not sum to the dimension of value along split_dim.");
4071 else if (numInferred == 1)
4073 splitsData[inferIdx] = armnn::numeric_cast<int>(inputTensorInfo.
GetShape()[splitDim]) - splitSum;
4077 throw ParseException(
"Cannot infer split size for more than one split");
4081 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
4086 unsigned int accumSplit = 0;
4087 for (
unsigned int j = 0; j < numSplits; ++j)
4089 unsigned int splitSize = armnn::numeric_cast<unsigned int>(splitsData[j]);
4092 for (
unsigned int dimIdx = 0; dimIdx < inputTensorInfo.
GetNumDimensions(); ++dimIdx)
4094 unsigned int dimSize = inputTensorInfo.
GetShape()[dimIdx];
4095 if (dimIdx == splitDim)
4097 dimSize = splitSize;
4099 splitDesc.SetViewSize(j, dimIdx, dimSize);
4102 splitDesc.SetViewOriginCoord(j, splitDim, accumSplit);
4103 accumSplit += splitSize;
4106 auto layerName = fmt::format(
"SplitV:{}:{}", subgraphIndex, operatorIndex);
4107 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
4110 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4111 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4119 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4120 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4123 void TfLiteParserImpl::ParseArgMin(
size_t subgraphIndex,
size_t operatorIndex)
4128 void TfLiteParserImpl::ParseArgMax(
size_t subgraphIndex,
size_t operatorIndex)
4133 void TfLiteParserImpl::ParseArgMinMax(
size_t subgraphIndex,
size_t operatorIndex,
ArgMinMaxFunction argMinMaxFunction)
4135 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4136 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
4139 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
4142 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4143 armnn::TensorInfo axisTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4153 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
4159 if (axisBufferPtr ==
nullptr)
4162 fmt::format(
"Operation has invalid inputs. Failed to read axis. {}",
4167 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.
GetNumBytes());
4168 int32_t axis = axisData.front();
4170 auto inputDimensions =
static_cast<int32_t
>(inputTensorInfo.
GetNumDimensions());
4171 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4177 fmt::format(
"Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
4187 auto layerName = argMinMaxFunction == ArgMinMaxFunction::Max ?
"ArgMax:{}:{}" :
"ArgMin:{}:{}";
4188 auto layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4189 IConnectableLayer *layer = m_Network->AddArgMinMaxLayer(desc, layerNameFormatted.c_str());
4191 outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
4195 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4196 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4199 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4200 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4203 void TfLiteParserImpl::ParseGather(
size_t subgraphIndex,
size_t operatorIndex)
4205 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4212 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4213 armnn::TensorInfo indicesTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4218 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4219 const auto* options = operatorPtr->builtin_options.AsGatherOptions();
4220 auto axis = options->axis;
4222 auto layerName = fmt::format(
"Gather:{}:{}", subgraphIndex, operatorIndex);
4224 auto inputDimensions =
static_cast<int32_t
>(inputTensorInfo.
GetNumDimensions());
4227 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4230 fmt::format(
"Operation has invalid axis: {} It is out of bounds [ -{}, {} ) {}",
4232 inputDimensions, inputDimensions,
4235 if (outputDimensions !=
static_cast<unsigned int>(inputDimensions) + indicesDimensions - 1)
4238 fmt::format(
"Operation has invalid output dimensions: {} Output must be an ({} + {} - 1) -D tensor {}",
4240 inputDimensions, indicesDimensions,
4244 gatherDescriptor.
m_Axis = axis;
4246 IConnectableLayer* layer = m_Network->AddGatherLayer(gatherDescriptor, layerName.c_str());
4248 outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
4251 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4252 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4254 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4255 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4258 void TfLiteParserImpl::ParseGatherNd(
size_t subgraphIndex,
size_t operatorIndex)
4260 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4267 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4268 armnn::TensorInfo indicesTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4270 auto layerName = fmt::format(
"GatherNd:{}:{}", subgraphIndex, operatorIndex);
4273 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
4276 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4277 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4279 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4280 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4283 void TfLiteParserImpl::ParseDepthToSpace(
size_t subgraphIndex,
size_t operatorIndex)
4285 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4294 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4295 const auto* options = operatorPtr->builtin_options.AsDepthToSpaceOptions();
4296 auto blockSize = options->block_size;
4300 fmt::format(
"Operation has invalid block size: {} Block size should be >= 2 {}",
4304 descriptor.
m_BlockSize = armnn::numeric_cast<uint32_t>(blockSize);
4306 auto layerName = fmt::format(
"DepthToSpace:{}:{}", subgraphIndex, operatorIndex);
4307 IConnectableLayer* layer = m_Network->AddDepthToSpaceLayer(descriptor, layerName.c_str());
4309 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
4312 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4313 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4315 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4316 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4319 void TfLiteParserImpl::ParseSum(
size_t subgraphIndex,
size_t operatorIndex)
4324 void TfLiteParserImpl::ParseReduceProd(
size_t subgraphIndex,
size_t operatorIndex)
4329 void TfLiteParserImpl::ParseReduceMax(
size_t subgraphIndex,
size_t operatorIndex)
4334 void TfLiteParserImpl::ParseReduceMin(
size_t subgraphIndex,
size_t operatorIndex)
4339 void TfLiteParserImpl::ParseReduce(
size_t subgraphIndex,
size_t operatorIndex,
ReduceOperation reduceOperation)
4341 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4343 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4344 const auto* options = operatorPtr->builtin_options.AsReducerOptions();
4346 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
4349 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
4352 auto layerName = fmt::format(
"Reduce:{}:{}", subgraphIndex, operatorIndex);
4354 armnn::TensorInfo inputTensorInfo0 = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4355 armnn::TensorInfo inputTensorInfo1 = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4360 if (axisBufferPtr !=
nullptr)
4362 std::vector<int32_t> axisData(inputTensorInfo1.
GetNumElements());
4363 ::memcpy(axisData.data(), axisBufferPtr->data.data(), inputTensorInfo1.
GetNumBytes());
4367 std::set<unsigned int> uniqueAxis;
4368 std::transform(axisData.begin(),
4370 std::inserter(uniqueAxis, uniqueAxis.begin()),
4371 [rank](
int i)->unsigned
int{
4372 return static_cast<uint32_t>(((i + rank) % rank)); });
4373 desc.
m_vAxis.assign(uniqueAxis.begin(), uniqueAxis.end());
4389 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
4393 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4394 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4397 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4398 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4401 void TfLiteParserImpl::ParseLocalResponseNormalization(
size_t subgraphIndex,
size_t operatorIndex)
4403 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4405 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
4408 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
4411 auto layerName = fmt::format(
"LRN:{}:{}", subgraphIndex, operatorIndex);
4412 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4414 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4416 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4417 const auto* options = operatorPtr->builtin_options.AsLocalResponseNormalizationOptions();
4423 descriptor.
m_NormSize =
static_cast<uint32_t
>(options->radius);
4424 descriptor.
m_K = options->bias;
4425 descriptor.
m_Alpha = options->alpha;
4426 descriptor.
m_Beta = options->beta;
4432 IConnectableLayer* layer = m_Network->AddNormalizationLayer(descriptor, layerNameFormatted.c_str());
4435 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
4438 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4439 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4441 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4442 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4445 void TfLiteParserImpl::ParseAbs(
size_t subgraphIndex,
size_t operatorIndex)
4450 void TfLiteParserImpl::ParseExp(
size_t subgraphIndex,
size_t operatorIndex)
4455 void TfLiteParserImpl::ParseLog(
size_t subgraphIndex,
size_t operatorIndex)
4460 void TfLiteParserImpl::ParseLogicalNot(
size_t subgraphIndex,
size_t operatorIndex)
4465 void TfLiteParserImpl::ParseNeg(
size_t subgraphIndex,
size_t operatorIndex)
4470 void TfLiteParserImpl::ParseRsqrt(
size_t subgraphIndex,
size_t operatorIndex)
4475 void TfLiteParserImpl::ParseSin(
size_t subgraphIndex,
size_t operatorIndex)
4480 void TfLiteParserImpl::ParseSqrt(
size_t subgraphIndex,
size_t operatorIndex)
4485 void TfLiteParserImpl::ParseElementwiseUnary(
size_t subgraphIndex,
size_t operatorIndex,
UnaryOperation unaryOperation)
4487 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4489 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
4492 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
4496 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4500 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(desc, layerNameFormatted.c_str());
4503 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
4506 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4507 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4509 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4510 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4513 void TfLiteParserImpl::ParseEqual(
size_t subgraphIndex,
size_t operatorIndex)
4518 void TfLiteParserImpl::ParseNotEqual(
size_t subgraphIndex,
size_t operatorIndex)
4523 void TfLiteParserImpl::ParseGreater(
size_t subgraphIndex,
size_t operatorIndex)
4528 void TfLiteParserImpl::ParseGreaterOrEqual(
size_t subgraphIndex,
size_t operatorIndex)
4533 void TfLiteParserImpl::ParseLess(
size_t subgraphIndex,
size_t operatorIndex)
4538 void TfLiteParserImpl::ParseLessOrEqual(
size_t subgraphIndex,
size_t operatorIndex)
4543 void TfLiteParserImpl::ParseComparison(
size_t subgraphIndex,
size_t operatorIndex,
4546 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4548 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
4551 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
4555 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4557 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4558 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4559 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerNameFormatted,
"Input 0",
"Input 1");
4563 IConnectableLayer* layer = m_Network->AddComparisonLayer(desc, layerNameFormatted.c_str());
4566 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
4569 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4570 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4572 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4573 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4577 unsigned int outputSlot,
4578 std::string reshapeLayerName,
4585 m_Network->AddReshapeLayer(desc, reshapeLayerName.c_str());
4590 return reshapeLayer;
4594 unsigned int outputSlot,
4595 tflite::ActivationFunctionType activationType)
4598 std::string layerName = prevLayer->
GetName();
4600 switch(activationType)
4602 case tflite::ActivationFunctionType_NONE:
4607 case tflite::ActivationFunctionType_RELU:
4609 activationDesc.
m_Function = ActivationFunction::ReLu;
4610 layerName +=
":RELU";
4613 case tflite::ActivationFunctionType_RELU6:
4615 activationDesc.
m_Function = ActivationFunction::BoundedReLu;
4616 activationDesc.
m_A = 6.0f;
4617 activationDesc.
m_B = 0.0f;
4618 layerName +=
":RELU6";
4621 case tflite::ActivationFunctionType_TANH:
4623 activationDesc.
m_Function = ActivationFunction::TanH;
4624 activationDesc.
m_A = 1.0f;
4625 activationDesc.
m_B = 1.0f;
4626 layerName +=
":TANH";
4631 case tflite::ActivationFunctionType_RELU_N1_TO_1:
4632 case tflite::ActivationFunctionType_SIGN_BIT:
4636 fmt::format(
"TfLite parser doesn't support fused activation: "
4639 tflite::EnumNameActivationFunctionType(activationType),
4646 m_Network->AddActivationLayer(activationDesc, layerName.c_str());
4648 auto & prevOutputSlot = prevLayer->
GetOutputSlot(outputSlot);
4651 return activationLayer;
4655 unsigned int outputSlot)
4658 auto& prevOutputSlot = prevLayer->
GetOutputSlot(outputSlot);
4661 if (dataType == DataType::Signed32)
4666 std::string layerName = prevLayer->
GetName();
4677 if (fileName ==
nullptr)
4682 std::error_code errorCode;
4683 fs::path pathToFile(fileName);
4684 if (!fs::exists(pathToFile, errorCode))
4687 std::stringstream msg;
4688 msg <<
"Cannot find the file (" << fileName <<
") errorCode: " << errorCode
4693 std::ifstream file(fileName, std::ios::binary);
4694 std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
4696 fileContent.size());
4701 if (binaryContent ==
nullptr)
4706 flatbuffers::Verifier verifier(binaryContent, len);
4710 fmt::format(
"Buffer doesn't conform to the expected Tensorflow Lite "
4711 "flatbuffers format. size:{} {}",
4715 return tflite::UnPackModel(binaryContent);
4719 size_t subgraphIndex,
4720 size_t operatorIndex)
4724 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4725 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
4727 size_t inputCount = operatorPtr->inputs.size();
4729 for (
size_t i = 0; i < inputCount; ++i)
4732 if (operatorPtr->inputs[i] == -1)
4739 result.push_back(subgraphPtr->tensors[inputId].get());
4746 size_t subgraphIndex,
4747 size_t operatorIndex)
4751 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4752 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
4754 size_t outputCount = operatorPtr->outputs.size();
4756 for (
size_t i = 0; i < outputCount; ++i)
4760 result[i] = subgraphPtr->tensors[outputId].get();
4766 size_t subgraphIndex)
4769 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4771 size_t inputCount = subgraphPtr->inputs.size();
4773 for (
size_t i = 0; i < inputCount; ++i)
4777 result[i] = std::make_pair(inputId, subgraphPtr->tensors[inputId].get());
4783 size_t subgraphIndex)
4786 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4788 size_t outputCount = subgraphPtr->outputs.size();
4790 for (
size_t i = 0; i < outputCount; ++i)
4793 result[i] = std::make_pair(outputId, subgraphPtr->tensors[outputId].get());
4799 size_t subgraphIndex,
4800 size_t operatorIndex)
4803 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4804 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
4805 return operatorPtr->inputs;
4809 size_t subgraphIndex,
4810 size_t operatorIndex)
4813 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4814 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
4815 return operatorPtr->outputs;
4818 void TfLiteParserImpl::RegisterInputSlots(
size_t subgraphIndex,
4819 size_t operatorIndex,
4821 const std::vector<unsigned int>& tensorIndexes,
4822 unsigned int startingSlotIndex)
4824 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4830 fmt::format(
"The number of tensor inputs ({}) does not match the number expected ({})"
4831 " for subgraph:{} operator index:{} {}",
4832 tensorIndexes.size(),
4839 for (
unsigned int index = 0; index < tensorIndexes.size() ; ++index)
4841 unsigned int tensorIndex = tensorIndexes[index];
4843 RegisterConsumerOfTensor(subgraphIndex, tensorIndex, slot);
4847 void TfLiteParserImpl::RegisterOutputSlots(
size_t subgraphIndex,
4848 size_t operatorIndex,
4850 const std::vector<unsigned int>& tensorIndexes)
4852 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4857 fmt::format(
"The number of tensor outputs ({}) does not match the number expected ({})"
4858 " for subgraph:{} operator index:{} {}",
4859 tensorIndexes.size(),
4866 for (
unsigned int slotIndex = 0; slotIndex < layer->
GetNumOutputSlots(); ++slotIndex)
4868 unsigned int tensorIndex = tensorIndexes[slotIndex];
4870 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
4874 void TfLiteParserImpl::SetupInputLayerTensorInfos(
size_t subgraphIndex)
4879 for (
auto const& tensorIdAndPtr : inputs)
4882 m_TensorInfos.insert({tensorIdAndPtr.first, tensorInfo});
4886 void TfLiteParserImpl::SetupInputLayers(
size_t subgraphIndex)
4891 for (
auto const& tensorIdAndPtr : inputs)
4893 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
4895 m_Network->AddInputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
4900 RegisterOutputSlots(subgraphIndex,
4901 VIRTUAL_OPERATOR_ID,
4903 {
static_cast<uint32_t
>(tensorIdAndPtr.first) });
4907 void TfLiteParserImpl::SetupOutputLayers(
size_t subgraphIndex)
4912 for (
auto const& tensorIdAndPtr : outputs)
4914 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
4916 m_Network->AddOutputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
4918 RegisterInputSlots(subgraphIndex,
4919 VIRTUAL_OPERATOR_ID,
4921 {
static_cast<uint32_t
>(tensorIdAndPtr.first) });
4925 void TfLiteParserImpl::SetupConstantLayerTensorInfos(
size_t subgraph)
4929 const auto & subgraphPtr = m_Model->subgraphs[subgraph];
4930 for (
unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
4932 for (
unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
4934 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot ==
nullptr &&
4935 m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0)
4937 TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get();
4941 m_TensorInfos.insert({tensorIndex, tensorInfo});
4947 void TfLiteParserImpl::SetupConstantLayers(
size_t subgraph)
4951 const auto & subgraphPtr = m_Model->subgraphs[subgraph];
4952 for (
unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
4954 for (
unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
4956 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot ==
nullptr &&
4957 m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0)
4959 TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get();
4961 if (IsConstTensor(tensorPtr))
4966 if (std::find(m_ConstantsToDequantize.begin(), m_ConstantsToDequantize.end(), tensorPtr->buffer)
4967 != m_ConstantsToDequantize.end())
4969 dataType = DataType::Float32;
4971 auto tensorAndData = CreateConstTensorNonPermuted(tensorPtr, tensorInfo, dataType);
4973 std::string layerName = fmt::format(
"Constant:{}", tensorPtr->name);
4974 IConnectableLayer *layer = m_Network->AddConstantLayer(tensorAndData.first, layerName.c_str());
4977 RegisterOutputSlots(subgraphIndex,
4978 VIRTUAL_OPERATOR_ID,
4982 else if (ShouldConstantTensorBeCreated(tensorIndex))
4987 if (std::find(m_ConstantsToDequantize.begin(), m_ConstantsToDequantize.end(), tensorPtr->buffer)
4988 != m_ConstantsToDequantize.end())
4990 dataType = DataType::Float32;
4998 std::string layerName = fmt::format(
"Constant:{}", tensorPtr->name);
4999 IConnectableLayer* layer = m_Network->AddConstantLayer(tensorAndData, layerName.c_str());
5002 RegisterOutputSlots(subgraphIndex,
5003 VIRTUAL_OPERATOR_ID,
5010 fmt::format(
"Invalid Tensor: Tensor should be constant. {}",
5022 return model->buffers[bufferIndex].get();
5025 template<
typename T>
5026 std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
5035 auto constData = CreateConstTensorImpl<T>(bufferPtr,
5039 TfLiteParserImpl::SupportedDataStorage storage(std::move(constData.second));
5040 return std::make_pair(constData.first, std::move(storage));
5043 bool TfLiteParserImpl::ShouldConstantTensorBeCreated(
unsigned int tensorIndex)
5046 return (std::find(m_ConstantsToBeCreated.begin(), m_ConstantsToBeCreated.end(), tensorIndex)
5047 != m_ConstantsToBeCreated.end());
5050 bool TfLiteParserImpl::IsConstTensor(
TensorRawPtr tensorPtr)
5053 bool isConst =
true;
5055 auto buffer =
GetBuffer(m_Model, tensorPtr->buffer);
5056 if (buffer->data.size() == 0)
5064 std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
5065 TfLiteParserImpl::CreateConstTensorPermuted(
TensorRawPtr tensorPtr,
5070 auto bufferPtr =
GetBuffer(m_Model, tensorPtr->buffer);
5079 return CreateConstTensorAndStoreData<float>(bufferPtr,
5084 return CreateConstTensorAndStoreData<uint8_t>(bufferPtr,
5089 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
5094 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
5099 return CreateConstTensorAndStoreData<int32_t>(bufferPtr,
5105 std::stringstream errString;
5106 errString <<
"Unexpected datatype when creating const tensor: "
5108 <<
" shape:" << tensorInfo.
GetShape()
5119 auto bufferPtr =
GetBuffer(m_Model, tensorPtr->buffer);
5125 return ConstTensor(tensorInfo, bufferPtr->data.data());
5128 std::pair<armnn::ConstTensor, std::unique_ptr<float[]>>
5129 TfLiteParserImpl::CreateConstTensorNonPermuted(
TensorRawPtr tensorPtr,
5134 auto bufferPtr =
GetBuffer(m_Model, tensorPtr->buffer);
5140 if (inputDataType == DataType::Float32 && tensorInfo.
GetDataType() != DataType::Float32)
5146 return std::make_pair(
ConstTensor(constTensorInfo, data.get()), std::move(data));
5151 fmt::format(
"Unsupported input/weights combination: Input {} not supported with Weights {}",
5159 return std::make_pair(
ConstTensor(tensorInfo, bufferPtr->data.data()), std::unique_ptr<
float[]>());
5163 std::pair<armnn::ConstTensor*, std::unique_ptr<float[]>>
5168 auto bufferPtr =
GetBuffer(m_Model, tensorPtr->buffer);
5180 return std::make_pair(
new ConstTensor(constTensorInfo, data.get()), std::move(data));
5185 fmt::format(
"Unsupported input/weights combination: Input {} not supported with Weights {}",
5193 return std::make_pair(
new ConstTensor(tensorInfo, bufferPtr->data.data()), std::unique_ptr<
float[]>());
5198 const std::string& name)
const
5202 for (
auto const& input : inputs)
5204 if (input.second->name == name)
5206 auto bindingId = GenerateLayerBindingId(subgraphId, input.first);
5210 return std::make_pair(bindingId, inputTensorInfo);
5214 std::stringstream bindings;
5215 for (
auto const& input : inputs)
5217 bindings <<
"'" << input.second->name <<
"' ";
5221 fmt::format(
"No input binding found for subgraph:{} and name:{}. "
5222 "Possible inputs are: [{}] {}",
5230 const std::string& name)
const
5234 for (
unsigned int i = 0; i < outputs.size(); ++i)
5236 auto const output = outputs[i];
5237 if (output.second->name == name)
5239 auto bindingId = GenerateLayerBindingId(subgraphId, output.first);
5240 std::vector<unsigned int> shape = m_OverriddenOutputShapes.size() > 0 ?
5241 m_OverriddenOutputShapes[i] : AsUnsignedVector(output.second->shape);
5242 return std::make_pair(bindingId,
ToTensorInfo(output.second, shape));
5246 std::stringstream bindings;
5247 for (
auto const& output : outputs)
5249 bindings <<
"'" << output.second->name <<
"' ";
5253 fmt::format(
"No output binding found for subgraph:{} and name:{}. "
5254 "Possible outputs are: [{}] {}",
5263 return m_Model->subgraphs.size();
5270 std::vector<std::string> result;
5271 result.reserve(inputs.size());
5272 for (
auto const& input : inputs)
5274 result.push_back(input.second->name);
5283 std::vector<std::string> result;
5284 result.reserve(outputs.size());
5285 for (
auto const& output : outputs)
5287 result.push_back(output.second->name);
5297 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<
float[]>&& data)
5298 : m_FloatData(
std::move(data))
5299 , m_Uint8Data(nullptr)
5300 , m_Int8Data(nullptr)
5301 , m_Int32Data(nullptr)
5305 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<uint8_t[]>&& data)
5306 : m_FloatData(nullptr)
5307 , m_Uint8Data(
std::move(data))
5308 , m_Int8Data(nullptr)
5309 , m_Int32Data(nullptr)
5313 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int8_t[]>&& data)
5314 : m_FloatData(nullptr)
5315 , m_Uint8Data(nullptr)
5316 , m_Int8Data(
std::move(data))
5317 , m_Int32Data(nullptr)
5321 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int32_t[]>&& data)
5322 : m_FloatData(nullptr)
5323 , m_Uint8Data(nullptr)
5324 , m_Int8Data(nullptr)
5325 , m_Int32Data(
std::move(data))