30 #include <schema_generated.h>
32 #include <flatbuffers/flexbuffers.h>
34 #include <fmt/format.h>
41 #define ARMNN_THROW_PARSE_EXCEPTION(msg) \
43 throw armnn::ParseException( static_cast<const std::stringstream&>( std::stringstream() << msg \
45 << CHECK_LOCATION().AsString()).str()); \
48 using namespace armnn;
54 pTfLiteParserImpl(
new TfLiteParserImpl(options)) {}
56 ITfLiteParser::~ITfLiteParser() =
default;
78 armnn::INetworkPtr ITfLiteParser::CreateNetworkFromBinary(
const std::vector<uint8_t>& binaryContent)
80 return pTfLiteParserImpl->CreateNetworkFromBinary(binaryContent);
84 const std::string& name)
const
86 return pTfLiteParserImpl->GetNetworkInputBindingInfo(subgraphId, name);
90 const std::string& name)
const
92 return pTfLiteParserImpl->GetNetworkOutputBindingInfo(subgraphId, name);
95 size_t ITfLiteParser::GetSubgraphCount()
const
97 return pTfLiteParserImpl->GetSubgraphCount();
100 std::vector<std::string> ITfLiteParser::GetSubgraphInputTensorNames(
size_t subgraphId)
const
102 return pTfLiteParserImpl->GetSubgraphInputTensorNames(subgraphId);
105 std::vector<std::string> ITfLiteParser::GetSubgraphOutputTensorNames(
size_t subgraphId)
const
107 return pTfLiteParserImpl->GetSubgraphOutputTensorNames(subgraphId);
113 const uint32_t VIRTUAL_OPERATOR_ID = std::numeric_limits<uint32_t>::max();
116 size_t subgraphIndex,
119 if (model.get() ==
nullptr)
122 fmt::format(
"{} was called with invalid (null) model. "
123 "Possible reason is that the model is not yet loaded and Unpack(ed). "
129 else if (subgraphIndex >= model->subgraphs.size())
132 fmt::format(
"{} was called with an invalid subgraph index. "
140 #define CHECK_SUBGRAPH(MODEL, SUBGRAPH_INDEX) \
141 CheckSubgraph(MODEL, SUBGRAPH_INDEX, CHECK_LOCATION())
144 size_t subgraphIndex,
145 size_t operatorIndex,
148 if (model.get() ==
nullptr)
151 fmt::format(
"{} was called with invalid (null) model. "
152 "Possible reason is that the model is not yet loaded and Unpack(ed). "
153 "subgraph:{} operator:{} at {}",
159 else if (subgraphIndex >= model->subgraphs.size())
162 fmt::format(
"{} was called with an invalid subgraph index. "
163 "subgraph:{} operator:{} at {}",
169 else if (operatorIndex >= model->subgraphs[subgraphIndex]->operators.size() &&
170 operatorIndex != VIRTUAL_OPERATOR_ID)
173 fmt::format(
"{} was called with an invalid operator index. "
174 "subgraph:{} operator:{} at {}",
182 #define CHECK_MODEL(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX) \
183 CheckModel(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX, CHECK_LOCATION())
186 size_t subgraphIndex,
191 if (tensorIndex >= model->subgraphs[subgraphIndex]->tensors.size())
194 fmt::format(
"{} was called with an invalid tensor index. "
195 "subgraph:{} tensor:{} at {}",
203 #define CHECK_TENSOR(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX) \
204 CheckTensor(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX, CHECK_LOCATION())
209 if (rawPtr ==
nullptr)
212 fmt::format(
"{} was called with a null tensor pointer at {}", location.
m_Function, location.
FileLine()));
216 #define CHECK_TENSOR_PTR(TENSOR_PTR) \
217 CheckTensorPtr(TENSOR_PTR, CHECK_LOCATION())
223 if (model.get() ==
nullptr)
226 fmt::format(
"{} was called with invalid (null) model. "
227 "Possible reason is that the model is not yet loaded and Unpack(ed). "
233 else if (bufferIndex >= model->buffers.size())
236 fmt::format(
"{} was called with an invalid buffer index. "
237 "buffer index:{} at {}",
242 else if (model->buffers[bufferIndex].get() ==
nullptr)
245 fmt::format(
"The buffer #{} is null. {}",
251 #define CHECK_BUFFER(MODEL, BUFFER_INDEX) \
252 CheckBuffer(MODEL, BUFFER_INDEX, CHECK_LOCATION())
254 void CheckBufferSize(TfLiteParserImpl::BufferRawPtr bufferPtr,
259 if (bufferPtr ==
nullptr)
262 fmt::format(
"BufferPtr is null for buffer:{}. {}",
269 std::stringstream ss;
270 ss <<
"Buffer #" << bufferId <<
" has " << bufferPtr->data.size() <<
" bytes. "
271 <<
"For tensor: " << tensorInfo.
GetShape()
272 <<
" expecting: " << tensorInfo.
GetNumBytes() <<
" bytes and "
281 const auto& operatorPtr = model->subgraphs[subgraphIndex]->operators[operatorIndex];
282 auto opcodeIndex = operatorPtr->opcode_index;
285 #if defined(ARMNN_POST_TFLITE_2_3)
286 auto opcode = std::max(model->operator_codes[opcodeIndex]->builtin_code,
287 static_cast<tflite::BuiltinOperator
>(model->operator_codes[opcodeIndex]->deprecated_builtin_code));
289 auto opcode = model->operator_codes[opcodeIndex]->builtin_code;
298 TfLiteParserImpl::BufferRawPtr bufferPtr = TfLiteParserImpl::GetBuffer(model, bufferIndex);
299 std::vector<unsigned int> buffer(
info.GetNumElements());
303 ::memcpy(buffer.data(), bufferPtr->data.data(), bufferPtr->data.size());
307 std::vector<uint64_t> uint64Buffer(
info.GetNumElements());
308 ::memcpy(uint64Buffer.data(), bufferPtr->data.data(), bufferPtr->data.size());
309 buffer.assign(std::begin(uint64Buffer), std::end(uint64Buffer));
315 fmt::format(
"Unsupported data type for uint buffer {}, only Signed 32 or Signed 64 are supported. {}",
322 #define CHECK_BUFFER_SIZE(BUFFER_PTR, TENSOR_INFO, BUFFER_ID) \
323 CheckBufferSize(BUFFER_PTR, TENSOR_INFO, BUFFER_ID, CHECK_LOCATION())
325 bool IsActivationSupported(tflite::ActivationFunctionType activationType)
327 switch(activationType)
329 case tflite::ActivationFunctionType_NONE:
330 case tflite::ActivationFunctionType_RELU:
331 case tflite::ActivationFunctionType_RELU6:
332 case tflite::ActivationFunctionType_TANH:
343 #define CHECK_SUPPORTED_FUSED_ACTIVATION(OPTION, SUBGRAPH_INDEX, OPERATOR_INDEX) \
345 if (IsActivationSupported(OPTION->fused_activation_function) == false) \
347 throw ParseException( \
348 fmt::format("TfLite parser doesn't support fused activation: " \
349 "{}/{} in {} subgraph:{} operator:{} at {}", \
350 OPTION->fused_activation_function, \
351 tflite::EnumNameActivationFunctionType(\
352 OPTION->fused_activation_function), \
356 CHECK_LOCATION().FileLine())); \
361 std::vector<unsigned int> AsUnsignedVector(
const std::vector<int32_t>& in)
363 std::vector<unsigned int> result;
364 result.reserve(in.size());
377 bool IsOptionalOperandPresent(
int input)
382 void CalcPadding(uint32_t inputSize,
386 uint32_t& paddingFront,
387 uint32_t& paddingBack,
388 tflite::Padding padding)
392 if (padding == tflite::Padding_SAME)
394 uint32_t outputSize = (inputSize + stride - 1) / stride;
395 uint32_t dilatedSize = filterSize + (dilation - 1) * (filterSize - 1);
396 uint32_t temp = (outputSize - 1) * stride + dilatedSize;
397 if (temp > inputSize)
399 paddingFront = (temp - inputSize) / 2;
400 paddingBack = (temp - inputSize) - paddingFront;
408 void CalcPadding(uint32_t inputSize,
412 uint32_t& paddingFront,
413 uint32_t& paddingBack,
414 tflite::Padding padding,
420 if (padding == tflite::Padding_SAME)
422 uint32_t totalPadding = (inputSize - 1) * stride + filterSize - outputSize;
423 paddingFront = totalPadding / 2;
424 paddingBack = totalPadding - paddingFront;
429 const std::vector<unsigned int>& shape,
430 const bool outputTensor =
false)
435 switch (tensorPtr->type)
437 case tflite::TensorType_UINT8:
440 case tflite::TensorType_FLOAT32:
443 case tflite::TensorType_FLOAT16:
446 case tflite::TensorType_INT8:
447 if (tensorPtr->quantization->zero_point.size() == 1)
458 case tflite::TensorType_INT16:
461 case tflite::TensorType_INT32:
464 case tflite::TensorType_INT64:
467 case tflite::TensorType_BOOL:
474 fmt::format(
"Unsupported data type {} = {} for tensor: {}. {}",
476 tflite::EnumNameTensorType(tensorPtr->type),
483 std::vector<unsigned int> safeShape = shape;
484 if (shape.size() == 0)
486 safeShape.push_back(1);
491 tensorShape =
TensorShape(armnn::numeric_cast<unsigned int>(safeShape.size()), safeShape.data());
495 size_t shapeSignatureSize = tensorPtr->shape_signature.size();
498 if (shapeSignatureSize != 0)
501 if (shapeSignatureSize != shape.size())
505 for (
unsigned int i = 0; i < shapeSignatureSize; ++i)
507 unsigned int dim = tensorPtr->shape_signature[i] > -1 ?
508 static_cast<unsigned int>(tensorPtr->shape_signature[i]) : 0;
509 safeShape.push_back(dim);
513 std::unique_ptr<bool[]> dimMask = std::make_unique<bool[]>(tensorPtr->shape_signature.size());
514 bool batchOnly =
true;
515 for (
unsigned int i = 0; i < tensorPtr->shape_signature.size(); ++i)
517 dimMask[i] = tensorPtr->shape_signature[i] != -1;
519 if (i > 0 && !dimMask[i])
528 tensorShape =
TensorShape(
static_cast<unsigned int>(safeShape.size()), safeShape.data(), dimMask.get());
531 else if (shape.size() == 0)
537 tensorShape =
TensorShape(armnn::numeric_cast<unsigned int>(shape.size()), shape.data());
541 float quantizationScale = 1.0f;
542 int32_t quantizationOffset = 0;
544 if (tensorPtr->quantization.get())
546 if (tensorPtr->quantization->scale.size() <= 1)
551 if (tensorPtr->quantization->scale.size() == 1)
553 quantizationScale = tensorPtr->quantization->scale[0];
555 if (tensorPtr->quantization->zero_point.size() == 1)
559 quantizationOffset = armnn::numeric_cast<int32_t>(tensorPtr->quantization->zero_point[0]);
570 std::vector<float> quantizationScales;
571 std::vector<int32_t> quantizationOffsets;
574 std::copy(tensorPtr->quantization->scale.begin(),
575 tensorPtr->quantization->scale.end(),
576 std::back_inserter(quantizationScales));
582 armnn::numeric_cast<unsigned int>(tensorPtr->quantization->quantized_dimension));
597 const bool outputTensor =
false)
599 auto const& dimensions = AsUnsignedVector(tensorPtr->shape);
600 return ToTensorInfo(tensorPtr, dimensions, outputTensor);
604 std::pair<armnn::ConstTensor, std::unique_ptr<T[]>>
628 reinterpret_cast<const T*
>(bufferPtr->data.data()), data.get(),
sizeof(T));
632 ::memcpy(data.get(), bufferPtr->data.data(), tensorInfo.
GetNumBytes());
638 return std::make_pair(
ConstTensor(tensorInfo, data.get()), std::move(data));
651 if (actualSize != expected.size())
656 for (
unsigned int i = 0u; i < actualSize; i++)
658 if (expected[i] < 0 ||
659 actual[i] !=
static_cast<unsigned int>(expected[i]))
670 std::vector<int32_t> expectedVec;
673 expectedVec.push_back(expected[i]);
678 void CheckMatchingQuantization(
const TensorInfo& first,
680 const std::string& descName,
681 std::string
const& firstName,
682 std::string
const& secondName)
694 if (firstDataType != secondDataType)
697 " must be of the same quantized type, " +
705 " must have the same quantization space, " +
715 auto shape = tensorPtr->shape;
721 auto shapeSig = tensorPtr->shape_signature;
723 if (shapeSig.empty())
728 for (
unsigned int i = 0; i < shapeSig.size() ; ++i)
730 if (shapeSig[i] == -1)
742 , m_Network(nullptr, nullptr)
746 m_ParserFunctions[tflite::BuiltinOperator_ABS] = &TfLiteParserImpl::ParseAbs;
747 m_ParserFunctions[tflite::BuiltinOperator_ADD] = &TfLiteParserImpl::ParseAdd;
748 m_ParserFunctions[tflite::BuiltinOperator_ARG_MIN] = &TfLiteParserImpl::ParseArgMin;
749 m_ParserFunctions[tflite::BuiltinOperator_ARG_MAX] = &TfLiteParserImpl::ParseArgMax;
750 m_ParserFunctions[tflite::BuiltinOperator_AVERAGE_POOL_2D] = &TfLiteParserImpl::ParseAveragePool2D;
751 m_ParserFunctions[tflite::BuiltinOperator_BATCH_TO_SPACE_ND] = &TfLiteParserImpl::ParseBatchToSpaceND;
752 m_ParserFunctions[tflite::BuiltinOperator_BATCH_MATMUL] = &TfLiteParserImpl::ParseBatchMatMul;
753 m_ParserFunctions[tflite::BuiltinOperator_BROADCAST_TO] = &TfLiteParserImpl::ParseBroadcastTo;
754 m_ParserFunctions[tflite::BuiltinOperator_CEIL] = &TfLiteParserImpl::ParseCeil;
755 m_ParserFunctions[tflite::BuiltinOperator_CAST] = &TfLiteParserImpl::ParseCast;
756 m_ParserFunctions[tflite::BuiltinOperator_CONCATENATION] = &TfLiteParserImpl::ParseConcatenation;
757 m_ParserFunctions[tflite::BuiltinOperator_CONV_2D] = &TfLiteParserImpl::ParseConv2D;
759 #if defined(ARMNN_POST_TFLITE_2_4)
760 m_ParserFunctions[tflite::BuiltinOperator_CONV_3D] = &TfLiteParserImpl::ParseConv3D;
762 m_ParserFunctions[tflite::BuiltinOperator_CUSTOM] = &TfLiteParserImpl::ParseCustomOperator;
763 m_ParserFunctions[tflite::BuiltinOperator_DEPTH_TO_SPACE] = &TfLiteParserImpl::ParseDepthToSpace;
764 m_ParserFunctions[tflite::BuiltinOperator_DEPTHWISE_CONV_2D] = &TfLiteParserImpl::ParseDepthwiseConv2D;
765 m_ParserFunctions[tflite::BuiltinOperator_DEQUANTIZE] = &TfLiteParserImpl::ParseDequantize;
766 m_ParserFunctions[tflite::BuiltinOperator_DIV] = &TfLiteParserImpl::ParseDiv;
767 m_ParserFunctions[tflite::BuiltinOperator_ELU] = &TfLiteParserImpl::ParseElu;
768 m_ParserFunctions[tflite::BuiltinOperator_EQUAL] = &TfLiteParserImpl::ParseEqual;
769 m_ParserFunctions[tflite::BuiltinOperator_EXP] = &TfLiteParserImpl::ParseExp;
770 m_ParserFunctions[tflite::BuiltinOperator_EXPAND_DIMS] = &TfLiteParserImpl::ParseExpandDims;
771 m_ParserFunctions[tflite::BuiltinOperator_FLOOR_DIV] = &TfLiteParserImpl::ParseFloorDiv;
772 m_ParserFunctions[tflite::BuiltinOperator_FULLY_CONNECTED] = &TfLiteParserImpl::ParseFullyConnected;
773 m_ParserFunctions[tflite::BuiltinOperator_GATHER] = &TfLiteParserImpl::ParseGather;
774 m_ParserFunctions[tflite::BuiltinOperator_GELU] = &TfLiteParserImpl::ParseGelu;
775 m_ParserFunctions[tflite::BuiltinOperator_GATHER_ND] = &TfLiteParserImpl::ParseGatherNd;
776 m_ParserFunctions[tflite::BuiltinOperator_GREATER] = &TfLiteParserImpl::ParseGreater;
777 m_ParserFunctions[tflite::BuiltinOperator_GREATER_EQUAL] = &TfLiteParserImpl::ParseGreaterOrEqual;
778 m_ParserFunctions[tflite::BuiltinOperator_HARD_SWISH] = &TfLiteParserImpl::ParseHardSwish;
779 m_ParserFunctions[tflite::BuiltinOperator_LEAKY_RELU] = &TfLiteParserImpl::ParseLeakyRelu;
780 m_ParserFunctions[tflite::BuiltinOperator_LESS] = &TfLiteParserImpl::ParseLess;
781 m_ParserFunctions[tflite::BuiltinOperator_LESS_EQUAL] = &TfLiteParserImpl::ParseLessOrEqual;
782 m_ParserFunctions[tflite::BuiltinOperator_LOCAL_RESPONSE_NORMALIZATION]
783 = &TfLiteParserImpl::ParseLocalResponseNormalization;
784 m_ParserFunctions[tflite::BuiltinOperator_LOG] = &TfLiteParserImpl::ParseLog;
785 m_ParserFunctions[tflite::BuiltinOperator_LOGICAL_NOT] = &TfLiteParserImpl::ParseLogicalNot;
786 m_ParserFunctions[tflite::BuiltinOperator_LOGISTIC] = &TfLiteParserImpl::ParseLogistic;
787 m_ParserFunctions[tflite::BuiltinOperator_LOG_SOFTMAX] = &TfLiteParserImpl::ParseLogSoftmax;
788 m_ParserFunctions[tflite::BuiltinOperator_L2_NORMALIZATION] = &TfLiteParserImpl::ParseL2Normalization;
789 m_ParserFunctions[tflite::BuiltinOperator_MAX_POOL_2D] = &TfLiteParserImpl::ParseMaxPool2D;
790 m_ParserFunctions[tflite::BuiltinOperator_MAXIMUM] = &TfLiteParserImpl::ParseMaximum;
791 m_ParserFunctions[tflite::BuiltinOperator_MEAN] = &TfLiteParserImpl::ParseMean;
792 m_ParserFunctions[tflite::BuiltinOperator_MINIMUM] = &TfLiteParserImpl::ParseMinimum;
793 m_ParserFunctions[tflite::BuiltinOperator_MIRROR_PAD] = &TfLiteParserImpl::ParseMirrorPad;
794 m_ParserFunctions[tflite::BuiltinOperator_MUL] = &TfLiteParserImpl::ParseMul;
795 m_ParserFunctions[tflite::BuiltinOperator_NEG] = &TfLiteParserImpl::ParseNeg;
796 m_ParserFunctions[tflite::BuiltinOperator_NOT_EQUAL] = &TfLiteParserImpl::ParseNotEqual;
797 m_ParserFunctions[tflite::BuiltinOperator_PACK] = &TfLiteParserImpl::ParsePack;
798 m_ParserFunctions[tflite::BuiltinOperator_PAD] = &TfLiteParserImpl::ParsePad;
799 m_ParserFunctions[tflite::BuiltinOperator_PADV2] = &TfLiteParserImpl::ParsePad;
800 m_ParserFunctions[tflite::BuiltinOperator_POW] = &TfLiteParserImpl::ParsePower;
801 m_ParserFunctions[tflite::BuiltinOperator_PRELU] = &TfLiteParserImpl::ParsePrelu;
802 m_ParserFunctions[tflite::BuiltinOperator_QUANTIZE] = &TfLiteParserImpl::ParseQuantize;
803 m_ParserFunctions[tflite::BuiltinOperator_RELU] = &TfLiteParserImpl::ParseRelu;
804 m_ParserFunctions[tflite::BuiltinOperator_RELU6] = &TfLiteParserImpl::ParseRelu6;
805 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MAX] = &TfLiteParserImpl::ParseReduceMax;
806 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MIN] = &TfLiteParserImpl::ParseReduceMin;
807 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_PROD] = &TfLiteParserImpl::ParseReduceProd;
808 m_ParserFunctions[tflite::BuiltinOperator_RESHAPE] = &TfLiteParserImpl::ParseReshape;
809 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_BILINEAR] = &TfLiteParserImpl::ParseResizeBilinear;
810 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_NEAREST_NEIGHBOR] = &TfLiteParserImpl::ParseResizeNearestNeighbor;
811 m_ParserFunctions[tflite::BuiltinOperator_REVERSE_V2] = &TfLiteParserImpl::ParseReverseV2;
812 m_ParserFunctions[tflite::BuiltinOperator_RSQRT] = &TfLiteParserImpl::ParseRsqrt;
813 m_ParserFunctions[tflite::BuiltinOperator_SQRT] = &TfLiteParserImpl::ParseSqrt;
814 m_ParserFunctions[tflite::BuiltinOperator_SHAPE] = &TfLiteParserImpl::ParseShape;
815 m_ParserFunctions[tflite::BuiltinOperator_SIN] = &TfLiteParserImpl::ParseSin;
816 m_ParserFunctions[tflite::BuiltinOperator_SLICE] = &TfLiteParserImpl::ParseSlice;
817 m_ParserFunctions[tflite::BuiltinOperator_SOFTMAX] = &TfLiteParserImpl::ParseSoftmax;
818 m_ParserFunctions[tflite::BuiltinOperator_SPACE_TO_BATCH_ND] = &TfLiteParserImpl::ParseSpaceToBatchND;
819 m_ParserFunctions[tflite::BuiltinOperator_SPACE_TO_DEPTH] = &TfLiteParserImpl::ParseSpaceToDepth;
820 m_ParserFunctions[tflite::BuiltinOperator_SPLIT] = &TfLiteParserImpl::ParseSplit;
821 m_ParserFunctions[tflite::BuiltinOperator_SPLIT_V] = &TfLiteParserImpl::ParseSplitV;
822 m_ParserFunctions[tflite::BuiltinOperator_SQUEEZE] = &TfLiteParserImpl::ParseSqueeze;
823 m_ParserFunctions[tflite::BuiltinOperator_SQUARE] = &TfLiteParserImpl::ParseSquare;
824 m_ParserFunctions[tflite::BuiltinOperator_SQUARED_DIFFERENCE] = &TfLiteParserImpl::ParseSquaredDifference;
825 m_ParserFunctions[tflite::BuiltinOperator_STRIDED_SLICE] = &TfLiteParserImpl::ParseStridedSlice;
826 m_ParserFunctions[tflite::BuiltinOperator_SUB] = &TfLiteParserImpl::ParseSub;
827 m_ParserFunctions[tflite::BuiltinOperator_SUM] = &TfLiteParserImpl::ParseSum;
828 m_ParserFunctions[tflite::BuiltinOperator_TANH] = &TfLiteParserImpl::ParseTanH;
829 m_ParserFunctions[tflite::BuiltinOperator_TILE] = &TfLiteParserImpl::ParseTile;
830 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE] = &TfLiteParserImpl::ParseTranspose;
831 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE_CONV] = &TfLiteParserImpl::ParseTransposeConv;
832 m_ParserFunctions[tflite::BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM]
833 = &TfLiteParserImpl::ParseUnidirectionalSequenceLSTM;
834 m_ParserFunctions[tflite::BuiltinOperator_UNPACK] = &TfLiteParserImpl::ParseUnpack;
837 m_CustomParserFunctions[
"TFLite_Detection_PostProcess"] = &TfLiteParserImpl::ParseDetectionPostProcess;
841 size_t operatorIndex,
844 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
845 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
848 auto search = armnnTfLiteParser::TfLiteParserImpl::m_TensorInfos.find(inputId);
850 if (search != m_TensorInfos.end())
852 return m_TensorInfos[inputId];
857 m_TensorInfos.insert({ inputId, tensorInfo });
862 armnn::TensorInfo TfLiteParserImpl::OutputTensorInfoFromInputs(
size_t subgraphIndex,
863 size_t operatorIndex,
866 std::vector<int> inputs)
868 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
869 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
873 auto outputSearch = armnnTfLiteParser::TfLiteParserImpl::m_TensorInfos.find(outputId);
875 if (outputSearch != m_TensorInfos.end())
877 return m_TensorInfos[outputId];
880 const auto& outputTensorPtr = subgraphPtr->tensors[outputId].get();
883 if (IsDynamic(outputTensorPtr))
889 inputs.emplace_back(i);
893 std::vector<armnn::TensorShape> inputShapes;
895 for (
unsigned int i = 0; i < inputs.size(); ++i)
898 auto search = armnnTfLiteParser::TfLiteParserImpl::m_TensorInfos.find(inputId);
900 if (search != m_TensorInfos.end())
902 auto &inputTensorInfo = m_TensorInfos[inputId];
903 inputShapes.push_back(inputTensorInfo.GetShape());
908 m_TensorInfos.insert({ inputId, inputTensorInfo});
909 inputShapes.push_back(inputTensorInfo.GetShape());
915 m_TensorInfos.insert({ outputId, tensor});
919 armnn::TensorInfo TfLiteParserImpl::OutputTensorInfoFromShapes(
size_t subgraphIndex,
920 size_t operatorIndex,
923 std::vector<armnn::TensorShape> inputShapes)
925 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
926 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
929 const auto& outputTensorPtr = subgraphPtr->tensors[outputId].get();
932 if (IsDynamic(outputTensorPtr))
937 m_TensorInfos.insert({ outputId, tensor});
941 void TfLiteParserImpl::ResetParser()
945 m_SubgraphConnections.clear();
946 m_OverriddenOutputShapes.clear();
947 m_ConstantsToDequantize.clear();
948 m_ConstantsToBeCreated.clear();
949 m_TensorInfos.clear();
956 return CreateNetworkFromModel();
963 return CreateNetworkFromModel();
970 m_Model = std::move(model);
972 return CreateNetworkFromModel();
975 INetworkPtr TfLiteParserImpl::CreateNetworkFromModel()
982 if (m_Options.value().m_InferAndValidate)
986 {
"InferAndValidate",
true }
989 networkOptions.push_back(shapeInferenceMethodOption);
991 if (m_Options.value().m_AllowExpandedDims)
995 {
"AllowExpandedDims",
true }
998 networkOptions.push_back(shapeInferenceMethodOption);
1001 m_Network = INetwork::Create(networkOptions);
1003 if (m_Model.get() ==
nullptr)
1011 size_t subgraphIndex = 0;
1012 uint8_t usableSubgraphs = 0;
1013 for (
size_t i = 0; i < m_Model->subgraphs.size(); i++)
1015 if (m_Model->subgraphs[i]->name.rfind(
"VALIDATION:", 0) != 0)
1022 if (usableSubgraphs > 1)
1025 fmt::format(
"Current TfLite parser only supports 1 non validation subgraph. This model has: {} {}",
1029 size_t operatorIndex = 0;
1032 const SubgraphPtr& subgraph = m_Model->subgraphs[subgraphIndex];
1033 SetupInputLayerTensorInfos(subgraphIndex);
1034 SetupConstantLayerTensorInfos(subgraphIndex);
1036 m_SubgraphConnections.emplace_back(subgraph->tensors.size());
1039 const auto& opCodePtr = m_Model->operator_codes[op->opcode_index];
1042 #if defined(ARMNN_POST_TFLITE_2_3)
1043 auto builtinCode = std::max(opCodePtr->builtin_code,
1044 static_cast<tflite::BuiltinOperator
>(opCodePtr->deprecated_builtin_code));
1046 auto builtinCode = opCodePtr->builtin_code;
1049 if (builtinCode > tflite::BuiltinOperator_MAX)
1051 throw ParseException(fmt::format(
"Operator code {} is out of range 0-{}. "
1052 "subgraph:{} operator idx:{}. {}",
1053 builtinCode, tflite::BuiltinOperator_MAX, subgraphIndex,
1058 auto& parserFunction = m_ParserFunctions[builtinCode];
1059 (this->*parserFunction)(subgraphIndex, operatorIndex);
1063 SetupInputLayers(subgraphIndex);
1064 SetupOutputLayers(subgraphIndex);
1065 SetupConstantLayers(subgraphIndex);
1069 std::stringstream errorString;
1070 errorString <<
"Failed to parse operator #" << operatorIndex <<
" within subgraph #"
1071 << subgraphIndex <<
" error: " << e.
what();
1073 std::stringstream errors;
1074 errors << errorString.str() <<
"\n";
1079 for (subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
1081 for (
size_t tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
1083 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot !=
nullptr)
1085 for (
size_t inputSlotIdx = 0;
1086 inputSlotIdx < m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size();
1089 m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot->Connect(
1090 *(m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots[inputSlotIdx]));
1095 return std::move(m_Network);
1102 return (TfLiteParserImpl::IsConstTensor(tensorPtr) && inputDataType == DataType::Float32 &&
1103 (tensorDataType == DataType::QAsymmU8 ||
1104 tensorDataType == DataType::QAsymmS8 ||
1105 tensorDataType == DataType::QSymmS8 ||
1106 tensorDataType == DataType::Signed32 ||
1107 tensorDataType == DataType::Signed64));
1110 void TfLiteParserImpl::RegisterProducerOfTensor(
size_t subgraphIndex,
1116 TensorSlots & tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
1122 if (tensorSlots.outputSlot !=
nullptr)
1124 throw ParseException(fmt::format(
"Another layer has already registered itself as the producer of "
1125 "subgraph:{} tensor:{} {}",
1131 tensorSlots.outputSlot = slot;
1134 void TfLiteParserImpl::RegisterConsumerOfTensor(
size_t subgraphIndex,
1140 TensorSlots& tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
1141 tensorSlots.inputSlots.push_back(slot);
1144 void TfLiteParserImpl::ParseCustomOperator(
size_t subgraphIndex,
size_t operatorIndex)
1146 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1149 auto customParserFunction = &TfLiteParserImpl::ParseUnsupportedOperator;
1152 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1153 const auto& customCode = m_Model->operator_codes[operatorPtr->opcode_index]->custom_code;
1156 auto iterator = m_CustomParserFunctions.find(customCode);
1157 if (iterator != m_CustomParserFunctions.end())
1159 customParserFunction = iterator->second;
1163 (this->*customParserFunction)(subgraphIndex, operatorIndex);
1166 void TfLiteParserImpl::ParseUnsupportedOperator(
size_t subgraphIndex,
size_t operatorIndex)
1168 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1170 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1172 auto opcodeIndex = operatorPtr->opcode_index;
1175 #if defined(ARMNN_POST_TFLITE_2_3)
1176 auto opcode = std::max(m_Model->operator_codes[opcodeIndex]->builtin_code,
1177 static_cast<tflite::BuiltinOperator
>(m_Model->operator_codes[opcodeIndex]->deprecated_builtin_code));
1179 auto opcode = m_Model->operator_codes[opcodeIndex]->builtin_code;
1182 if (!m_Options || !m_Options.value().m_StandInLayerForUnsupported)
1186 fmt::format(
"Operator not supported. "
1187 "subgraph:{} operator:{} "
1188 "opcode_index:{} opcode:{} / {} {}",
1193 tflite::EnumNameBuiltinOperator(opcode),
1197 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1198 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1200 const unsigned int numInputs = armnn::numeric_cast<unsigned int>(inputs.size());
1201 const unsigned int numOutputs = armnn::numeric_cast<unsigned int>(outputs.size());
1204 auto layerName = fmt::format(
"StandIn:{}:{}:{}", subgraphIndex, operatorIndex, opcode);
1207 IConnectableLayer* layer = m_Network->AddStandInLayer(descriptor, layerName.c_str());
1215 for (
unsigned int i = 0u; i < numOutputs; ++i)
1220 auto inputTensorIds = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1221 auto outputTensorIds = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1223 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIds);
1224 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIds);
1227 void TfLiteParserImpl::ParseCast(
size_t subgraphIndex,
size_t operatorIndex)
1229 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1231 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1233 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1236 auto layerName = fmt::format(
"Cast:{}:{}", subgraphIndex, operatorIndex);
1246 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
1249 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1250 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1252 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1253 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1256 void TfLiteParserImpl::ParseConv2D(
size_t subgraphIndex,
size_t operatorIndex)
1258 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1260 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1261 const auto* options = operatorPtr->builtin_options.AsConv2DOptions();
1265 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1266 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1270 inputs.size() == 3 ?
1278 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1279 armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1282 unsigned int inputHeight = inputTensorInfo.
GetShape()[1];
1283 unsigned int inputWidth = inputTensorInfo.
GetShape()[2];
1287 unsigned int filterHeight = filterTensorInfo.
GetShape()[1];
1288 unsigned int filterWidth = filterTensorInfo.
GetShape()[2];
1290 CalcPadding(inputHeight, filterHeight, desc.
m_StrideY,
1292 CalcPadding(inputWidth, filterWidth, desc.
m_StrideX,
1297 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1298 std::vector<unsigned int> tensorIndexesToRegister = { inputTensorIndexes[0], inputTensorIndexes[1] };
1300 auto layerName = fmt::format(
"Conv2D:{}:{}", subgraphIndex, operatorIndex);
1303 if (ShouldConstantTensorBeConverted(inputs[1], inputTensorInfo.
GetDataType(), filterTensorInfo.
GetDataType()))
1305 m_ConstantsToDequantize.emplace_back(inputs[1]->buffer);
1310 armnn::TensorInfo biasTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
1313 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
1315 if (ShouldConstantTensorBeConverted(inputs[2], inputTensorInfo.
GetDataType(), biasTensorInfo.
GetDataType()))
1317 m_ConstantsToDequantize.emplace_back(inputs[2]->buffer);
1327 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
1332 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
1334 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1336 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1337 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, { outputTensorIndexes[0] });
1341 #if defined(ARMNN_POST_TFLITE_2_4)
1342 void TfLiteParserImpl::ParseConv3D(
size_t subgraphIndex,
size_t operatorIndex)
1344 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1346 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1347 const auto* options = operatorPtr->builtin_options.AsConv3DOptions();
1361 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1364 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1367 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1368 armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1371 unsigned int inputDepth = inputTensorInfo.
GetShape()[1];
1372 unsigned int inputHeight = inputTensorInfo.
GetShape()[2];
1373 unsigned int inputWidth = inputTensorInfo.
GetShape()[3];
1376 unsigned int filterDepth = filterTensorInfo.
GetShape()[0];
1377 unsigned int filterHeight = filterTensorInfo.
GetShape()[1];
1378 unsigned int filterWidth = filterTensorInfo.
GetShape()[2];
1380 CalcPadding(inputDepth, filterDepth, desc.
m_StrideZ,
1382 CalcPadding(inputHeight, filterHeight, desc.
m_StrideY,
1384 CalcPadding(inputWidth, filterWidth, desc.
m_StrideX,
1387 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo, inputTensorInfo.
GetDataType());
1389 auto layerName = fmt::format(
"Conv3D:{}:{}", subgraphIndex, operatorIndex);
1391 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1394 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0], inputTensorIndexes[1]};
1396 if (inputs.size() == 3)
1401 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
1412 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
1416 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
1418 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1420 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1421 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1425 void TfLiteParserImpl::ParseDepthwiseConv2D(
size_t subgraphIndex,
size_t operatorIndex)
1427 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1429 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1430 const auto* options = operatorPtr->builtin_options.AsDepthwiseConv2DOptions();
1440 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1442 if (inputs.size() == 3)
1447 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1452 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1453 armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1456 unsigned int inputHeight = inputTensorInfo.
GetShape()[1];
1457 unsigned int inputWidth = inputTensorInfo.
GetShape()[2];
1460 unsigned int filterHeight = filterTensorInfo.
GetShape()[1];
1461 unsigned int filterWidth = filterTensorInfo.
GetShape()[2];
1463 CalcPadding(inputHeight, filterHeight, desc.
m_StrideY,
1465 CalcPadding(inputWidth, filterWidth, desc.
m_StrideX,
1469 auto layerName = fmt::format(
"DepthwiseConv2D:{}:{}", subgraphIndex, operatorIndex);
1471 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1474 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0], inputTensorIndexes[1]};
1481 TensorInfo biasTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
1484 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
1493 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
1498 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
1500 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1502 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1503 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1506 void TfLiteParserImpl::ParseDequantize(
size_t subgraphIndex,
size_t operatorIndex)
1508 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1510 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1513 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1516 auto layerName = fmt::format(
"Dequantize:{}:{}", subgraphIndex, operatorIndex);
1526 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
1529 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1530 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1532 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1533 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1536 void TfLiteParserImpl::ParseExpandDims(
size_t subgraphIndex,
size_t operatorIndex)
1538 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1540 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1543 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1546 auto layerName = fmt::format(
"ExpandDims:{}:{}", subgraphIndex, operatorIndex);
1548 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1550 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1552 armnn::TensorInfo axisTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1555 if (axisBufferPtr ==
nullptr)
1557 throw ParseException(fmt::format(
"{}: Operation has invalid inputs. Failed to read axis.",
1562 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.
GetNumBytes());
1563 int32_t axis = axisData[0];
1566 auto outputRank = inputRank + 1;
1567 if((axis < -1 * outputRank) || (outputRank <= axis))
1569 throw ParseException(fmt::format(
"{}: Axis {} is not within [-{}, {}) range.",
1573 axis = axis < 0 ? (axis + outputRank) : axis;
1575 std::vector<unsigned int> shape(
static_cast<unsigned int>(outputRank));
1576 unsigned int inputShapeIndex = 0;
1577 for (
unsigned int i = 0; i < static_cast<unsigned int>(outputRank); ++i)
1579 if (i ==
static_cast<unsigned int>(axis))
1585 shape[i] = inputTensorInfo.
GetShape()[inputShapeIndex];
1594 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
1603 m_TensorInfos[outputTensorIds[0]] = outputTensorInfo;
1605 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1606 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1608 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1609 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1612 void TfLiteParserImpl::ParseTranspose(
size_t subgraphIndex,
size_t operatorIndex)
1614 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1616 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1619 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1622 auto layerName = fmt::format(
"Transpose:{}:{}", subgraphIndex, operatorIndex);
1625 if (inputs.size() == 2)
1627 armnn::TensorInfo permuteTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1630 std::vector<unsigned int> permuteShape(numPermVecElements);
1631 ::memcpy(permuteShape.data(), permuteBufferPtr->data.data(), permuteTensorInfo.
GetNumBytes());
1636 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1638 IConnectableLayer* layer = m_Network->AddTransposeLayer(desc, layerName.c_str());
1646 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
1647 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1650 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1651 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1653 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1654 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1657 void TfLiteParserImpl::ParseTransposeConv(
size_t subgraphIndex,
size_t operatorIndex)
1659 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1661 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1662 const auto* options = operatorPtr->builtin_options.AsTransposeConvOptions();
1670 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1671 if (inputs.size() == 4)
1680 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1684 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
1685 armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1688 const unsigned int inputHeight = inputTensorInfo.
GetShape()[1];
1689 const unsigned int inputWidth = inputTensorInfo.
GetShape()[2];
1691 const unsigned int filterHeight = filterTensorInfo.
GetShape()[1];
1692 const unsigned int filterWidth = filterTensorInfo.
GetShape()[2];
1698 if (inputs[0] && IsConstTensor(inputs[0]))
1700 armnn::TensorInfo tensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1703 if (tensorInfo.
GetDataType() == DataType::Signed32)
1705 ::memcpy(output_shape.data(),
GetBuffer(m_Model, inputs[0]->buffer)->data.data(), tensorInfo.
GetNumBytes());
1707 if (tensorInfo.
GetDataType() == DataType::QAsymmU8)
1711 output_shape[i] =
GetBuffer(m_Model, inputs[0]->buffer)->data.data()[i];
1715 for (
int dimension : output_shape)
1717 desc.
m_OutputShape.push_back(
static_cast<unsigned int>(dimension));
1725 CalcPadding(inputHeight,
1734 CalcPadding(inputWidth,
1745 CalcPadding(inputHeight,
1753 CalcPadding(inputWidth,
1762 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo, inputTensorInfo.
GetDataType());
1765 auto layerName = fmt::format(
"TransposeConv:{}:{}", subgraphIndex, operatorIndex);
1769 auto biasTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 3);
1770 auto biasConstTensor = CreateConstTensorNonPermuted(inputs[3], biasTensorInfo, inputTensorInfo.
GetDataType());
1771 layer = m_Network->AddTransposeConvolution2dLayer(desc,
1772 filterTensorAndData.first,
1773 biasConstTensor.first,
1778 layer = m_Network->AddTransposeConvolution2dLayer(desc,
1779 filterTensorAndData.first,
1790 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0 , { 2, 1 });
1794 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1795 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[2]});
1797 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1798 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1801 void TfLiteParserImpl::ParseAveragePool2D(
size_t subgraphIndex,
size_t operatorIndex)
1803 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Average);
1806 void TfLiteParserImpl::ParseBatchMatMul(
size_t subgraphIndex,
size_t operatorIndex)
1808 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1810 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1813 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1816 auto layerName = fmt::format(
"BatchMatMul:{}:{}", subgraphIndex, operatorIndex);
1818 TensorInfo inputXTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1819 TensorInfo inputYTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1821 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1822 const auto* options = operatorPtr->builtin_options.AsBatchMatMulOptions();
1831 IConnectableLayer* layer = m_Network->AddBatchMatMulLayer(descriptor, layerName.c_str());
1839 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
1842 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1843 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1845 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1846 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1849 void TfLiteParserImpl::ParseBatchToSpaceND(
size_t subgraphIndex,
size_t operatorIndex)
1851 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1853 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1856 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1859 armnn::TensorInfo blockShapeTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1862 armnn::TensorInfo cropsTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
1865 std::vector<unsigned int> blockShape(blockShapeTensorInfo.
GetNumElements());
1866 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.
GetNumBytes());
1868 std::vector<unsigned int> cropsVector(cropsTensorInfo.
GetNumElements());
1869 ::memcpy(cropsVector.data(), cropsBufferPtr->data.data(), cropsTensorInfo.
GetNumBytes());
1872 std::vector<std::pair<unsigned int, unsigned int>> crops;
1873 for (
unsigned int i = 0; i < cropsTensorInfo.
GetNumElements() / step; ++i)
1875 crops.emplace_back(cropsVector[i * step], cropsVector[i * step + 1]);
1883 auto layerName = fmt::format(
"BatchToSpaceND:{}:{}", subgraphIndex, operatorIndex);
1885 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1887 IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(desc, layerName.c_str());
1895 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
1896 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1899 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1900 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1902 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1903 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1906 void TfLiteParserImpl::ParseBroadcastTo(
size_t subgraphIndex,
size_t operatorIndex)
1908 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1910 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1913 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1920 auto layerName = fmt::format(
"Broadcast_to:{}:{}", subgraphIndex, operatorIndex);
1924 auto shapeBufferPtr =
GetBuffer(m_Model, inputs[1]->buffer);
1925 if (shapeBufferPtr !=
nullptr)
1927 std::vector<unsigned int> targetShape;
1929 auto shapeData =
reinterpret_cast<const int32_t*
>(shapeBufferPtr->data.data());
1932 for (
unsigned int i = 0; i < numElement; ++i)
1934 targetShape.push_back(armnn::numeric_cast<unsigned int>(shapeData[i]));
1944 "data and output shape are not found in the buffer.");
1954 IConnectableLayer* layer = m_Network->AddBroadcastToLayer(descriptor, layerName.c_str());
1959 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1960 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1962 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1963 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1966 void TfLiteParserImpl::ParseL2Normalization(
size_t subgraphIndex,
size_t operatorIndex)
1968 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1970 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1973 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1978 auto layerName = fmt::format(
"L2Normalization:{}:{}", subgraphIndex, operatorIndex);
1979 IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(desc, layerName.c_str());
1987 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
1990 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1991 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1993 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1994 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1997 void TfLiteParserImpl::ParseMaxPool2D(
size_t subgraphIndex,
size_t operatorIndex)
1999 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Max);
2002 void TfLiteParserImpl::ParseMaximum(
size_t subgraphIndex,
size_t operatorIndex)
2004 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2006 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2009 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2012 auto layerName = fmt::format(
"Maximum:{}:{}", subgraphIndex, operatorIndex);
2014 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2015 TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2016 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName,
"Input 0",
"Input 1");
2018 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Maximum, layerName.c_str());
2026 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
2027 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
2030 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2031 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2033 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2034 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2037 void TfLiteParserImpl::ParseMinimum(
size_t subgraphIndex,
size_t operatorIndex)
2039 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2041 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2044 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2047 auto layerName = fmt::format(
"Minimum:{}:{}", subgraphIndex, operatorIndex);
2049 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2050 TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2051 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName,
"Input 0",
"Input 1");
2053 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Minimum, layerName.c_str());
2061 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
2062 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
2065 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2066 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2068 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2069 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2072 void TfLiteParserImpl::ParsePool(
size_t subgraphIndex,
2073 size_t operatorIndex,
2076 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2078 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2079 const auto* options = operatorPtr->builtin_options.AsPool2DOptions();
2083 std::string layerName;
2087 case PoolingAlgorithm::Average:
2089 fmt::format(
"AveragePool2D:{}:{}", subgraphIndex, operatorIndex);
2091 case PoolingAlgorithm::Max:
2093 fmt::format(
"MaxPool2D:{}:{}", subgraphIndex, operatorIndex);
2110 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2112 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2115 unsigned int inputHeight = inputTensorInfo.
GetShape()[1];
2116 unsigned int inputWidth = inputTensorInfo.
GetShape()[2];
2123 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2126 IConnectableLayer* layer = m_Network->AddPooling2dLayer(desc, layerName.c_str());
2134 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2135 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
2140 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2141 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2143 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2145 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2146 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2149 void TfLiteParserImpl::ParseSlice(
size_t subgraphIndex,
size_t operatorIndex)
2151 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2153 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2155 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2161 armnn::TensorInfo beginTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2164 std::vector<unsigned int> begin(beginTensorInfo.
GetNumElements());
2165 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.
GetNumBytes());
2168 armnn::TensorInfo sizeTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
2174 if (sizeBufferPtr->data.data())
2176 ::memcpy(signedSize.data(), sizeBufferPtr->data.data(), sizeTensorInfo.
GetNumBytes());
2180 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2182 for (
unsigned int i = 0; i < signedSize.size(); ++i)
2184 int signedValue = signedSize[i];
2186 if (signedValue < -1 || signedValue >
static_cast<int>(inputTensorInfo.
GetShape()[i] - begin[i]))
2188 throw ParseException(fmt::format(
"Invalid value for size {} size must be in range "
2189 "[-1, inputDimSize - begin] [-1, {}] inclusive {}",
2191 inputTensorInfo.
GetShape()[i] - begin[i],
2195 if (signedValue == -1)
2197 size[i] = inputTensorInfo.
GetShape()[i] - begin[i];
2201 size[i] =
static_cast<unsigned int>(signedValue);
2207 auto layerName = fmt::format(
"Slice:{}:{}", subgraphIndex, operatorIndex);
2209 IConnectableLayer*
const layer = m_Network->AddSliceLayer(desc, layerName.c_str());
2211 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2212 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
2217 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2218 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2221 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2222 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2225 void TfLiteParserImpl::ParseSoftmax(
size_t subgraphIndex,
size_t operatorIndex)
2227 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2228 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2229 const auto* options = operatorPtr->builtin_options.AsSoftmaxOptions();
2232 desc.
m_Beta = options->beta;
2234 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2236 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2239 auto layerName = fmt::format(
"Softmax:{}:{}", subgraphIndex, operatorIndex);
2240 IConnectableLayer*
const layer = m_Network->AddSoftmaxLayer(desc, layerName.c_str());
2242 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2247 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2248 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2251 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2252 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2255 void TfLiteParserImpl::ParseLogSoftmax(
size_t subgraphIndex,
size_t operatorIndex)
2257 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2261 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2263 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2266 auto layerName = fmt::format(
"LogSoftmax:{}:{}", subgraphIndex, operatorIndex);
2267 IConnectableLayer*
const layer = m_Network->AddLogSoftmaxLayer(desc, layerName.c_str());
2269 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2274 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2275 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2278 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2279 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2282 void TfLiteParserImpl::ParseSpaceToBatchND(
size_t subgraphIndex,
size_t operatorIndex)
2284 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2286 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2289 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2292 armnn::TensorInfo blockShapeTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2295 armnn::TensorInfo padListTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
2298 std::vector<unsigned int> blockShape(blockShapeTensorInfo.
GetNumElements());
2299 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.
GetNumBytes());
2301 std::vector<unsigned int> padListVector(padListTensorInfo.
GetNumElements());
2302 ::memcpy(padListVector.data(), padListBufferPtr->data.data(), padListTensorInfo.
GetNumBytes());
2305 std::vector<std::pair<unsigned int, unsigned int>> padList;
2306 for (
unsigned int i = 0; i < padListTensorInfo.
GetNumElements() / step; ++i)
2308 padList.emplace_back(padListVector[i * step], padListVector[i * step + 1]);
2316 auto layerName = fmt::format(
"SpaceToBatchND:{}:{}", subgraphIndex, operatorIndex);
2318 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2320 IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(desc, layerName.c_str());
2328 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2329 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
2332 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2333 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2335 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2336 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2339 void TfLiteParserImpl::ParseSpaceToDepth(
size_t subgraphIndex,
size_t operatorIndex)
2341 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2350 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2351 const auto* options = operatorPtr->builtin_options.AsSpaceToDepthOptions();
2352 auto blockSize = options->block_size;
2356 fmt::format(
"Operation has invalid block size: {} Block size should be >= 2 {}",
2360 descriptor.
m_BlockSize = armnn::numeric_cast<uint32_t>(blockSize);
2362 auto layerName = fmt::format(
"SpaceToDepth:{}:{}", subgraphIndex, operatorIndex);
2363 IConnectableLayer* layer = m_Network->AddSpaceToDepthLayer(descriptor, layerName.c_str());
2371 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2374 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2375 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2377 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2378 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2385 static const uint32_t dimensionSequence[] = { 0, 1, 2, 3 };
2389 std::stringstream ss;
2390 ss <<
"Input tensor has unexpected number of dimensions:" << inputTensorInfo.
GetNumDimensions()
2391 <<
" shape:" << inputTensorInfo.
GetShape() <<
" "
2396 if (squeezeDims.empty())
2398 squeezeDims.assign(dimensionSequence,
2402 std::vector<uint32_t> outputDims;
2405 bool skipSqueeze = (std::find(squeezeDims.begin(), squeezeDims.end(), i) == squeezeDims.end());
2406 auto currentDimension = inputTensorInfo.
GetShape()[i];
2407 if (skipSqueeze || currentDimension != 1)
2409 outputDims.push_back(currentDimension);
2413 if (outputDims.size() > 4)
2415 std::stringstream ss;
2416 ss <<
"Output tensor has unexpected number of dimensions:" << inputTensorInfo.
GetNumDimensions()
2417 <<
" shape:" << inputTensorInfo.
GetShape() <<
" "
2429 return outTensorInfo;
2432 void TfLiteParserImpl::ParseShape(
size_t subgraphIndex,
size_t operatorIndex)
2434 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2436 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2438 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2441 auto layerName = fmt::format(
"Shape:{}:{}", subgraphIndex, operatorIndex);
2451 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2460 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
2464 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2465 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2467 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2468 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2471 void TfLiteParserImpl::ParseSqueeze(
size_t subgraphIndex,
size_t operatorIndex)
2473 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2475 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2478 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2481 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2482 const auto * options = operatorPtr->builtin_options.AsSqueezeOptions();
2483 auto layerName = fmt::format(
"Squeeze:{}:{}", subgraphIndex, operatorIndex);
2485 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2487 std::vector<uint32_t> squeezeDim;
2490 if (options->squeeze_dims.size() == 1 && options->squeeze_dims[0] < 0)
2493 squeezeDim.push_back(
static_cast<uint32_t
>(dim));
2497 squeezeDim = AsUnsignedVector(options->squeeze_dims);
2502 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
2508 m_TensorInfos[outputTensorIds[0]] = outputTensorInfo;
2510 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
2520 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2521 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2523 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2524 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2527 void TfLiteParserImpl::ParseStridedSlice(
size_t subgraphIndex,
size_t operatorIndex)
2529 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2531 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2534 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2537 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2538 const auto* options = operatorPtr->builtin_options.AsStridedSliceOptions();
2548 armnn::TensorInfo beginTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2552 if (beginBufferPtr->data.data() !=
nullptr)
2554 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.
GetNumBytes());
2558 throw ParseException(
"ParseStridedSlice: Invalid input - the begin vector is null");
2561 armnn::TensorInfo endTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
2565 if (endBufferPtr->data.data() !=
nullptr)
2567 ::memcpy(end.data(), endBufferPtr->data.data(), endTensorInfo.
GetNumBytes());
2571 throw ParseException(
"ParseStridedSlice: Invalid input - the end vector is null");
2574 armnn::TensorInfo strideTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 3);
2579 if (strideBufferPtr->data.data() !=
nullptr)
2581 ::memcpy(stride.data(), strideBufferPtr->data.data(), strideTensorInfo.
GetNumBytes());
2585 throw ParseException(
"ParseStridedSlice: Invalid input - the stride vector is null");
2592 auto layerName = fmt::format(
"StridedSlice:{}:{}", subgraphIndex, operatorIndex);
2593 IConnectableLayer* layer = m_Network->AddStridedSliceLayer(desc, layerName.c_str());
2601 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2604 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2605 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2607 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2608 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2611 void TfLiteParserImpl::ParseSub(
size_t subgraphIndex,
size_t operatorIndex)
2613 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2615 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2616 const auto* options = operatorPtr->builtin_options.AsSubOptions();
2618 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2621 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2624 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2625 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2627 auto layerName = fmt::format(
"Sub:{}:{}", subgraphIndex, operatorIndex);
2628 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Sub, layerName.c_str());
2636 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
2639 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2640 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2643 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2646 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2647 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2650 void TfLiteParserImpl::ParseDiv(
size_t subgraphIndex,
size_t operatorIndex)
2652 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2654 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2655 const auto* options = operatorPtr->builtin_options.AsDivOptions();
2657 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2660 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2663 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2664 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2666 auto layerName = fmt::format(
"Div:{}:{}", subgraphIndex, operatorIndex);
2667 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Div, layerName.c_str());
2675 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
2678 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2679 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2682 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2685 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2686 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2689 void TfLiteParserImpl::ParseFloorDiv(
size_t subgraphIndex,
size_t operatorIndex)
2691 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2693 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2696 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2699 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2700 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2702 auto layerName = fmt::format(
"Div:{}:{}", subgraphIndex, operatorIndex);
2703 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Div, layerName.c_str());
2711 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
2714 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2715 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2716 layer = AddFusedFloorLayer(layer, 0);
2718 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2719 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2722 void TfLiteParserImpl::ParseAdd(
size_t subgraphIndex,
size_t operatorIndex)
2724 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2726 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2727 const auto* options = operatorPtr->builtin_options.AsAddOptions();
2729 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2732 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2735 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2736 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2738 auto layerName = fmt::format(
"Add:{}:{}", subgraphIndex, operatorIndex);
2739 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Add, layerName.c_str());
2747 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
2750 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2751 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2754 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2757 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2758 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2761 void TfLiteParserImpl::ParseMul(
size_t subgraphIndex,
size_t operatorIndex)
2763 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2765 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2766 const auto* options = operatorPtr->builtin_options.AsMulOptions();
2768 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2771 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2774 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2775 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2777 auto layerName = fmt::format(
"Mul:{}:{}", subgraphIndex, operatorIndex);
2778 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Mul, layerName.c_str());
2786 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
2789 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2790 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2793 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2796 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2797 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2800 void TfLiteParserImpl::ParseMean(
size_t subgraphIndex,
size_t operatorIndex)
2802 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2804 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2806 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2809 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2810 TensorInfo dimTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2815 if (axisBufferPtr !=
nullptr)
2818 ::memcpy(axisData.data(), axisBufferPtr->data.data(), dimTensorInfo.
GetNumBytes());
2822 std::set<unsigned int> uniqueAxis;
2823 std::transform(axisData.begin(),
2825 std::inserter(uniqueAxis, uniqueAxis.begin()),
2826 [rank](
int i)->unsigned
int{
2827 return static_cast<uint32_t>(((i + rank) % rank)); });
2828 desc.
m_Axis.assign(uniqueAxis.begin(), uniqueAxis.end());
2834 desc.
m_Axis.push_back(i);
2842 auto layerName = fmt::format(
"Mean:{}:{}", subgraphIndex, operatorIndex);
2851 outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2854 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2855 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2857 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2858 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2861 void TfLiteParserImpl::ParsePad(
size_t subgraphIndex,
size_t operatorIndex)
2863 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2870 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2871 armnn::TensorInfo padTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2873 std::vector<unsigned int> padBuffer = GetUIntBuffer(padTensorInfo, m_Model, inputs[1]->buffer);
2877 auto opcode = GetOpCode(m_Model, subgraphIndex, operatorIndex);
2879 if (opcode == tflite::BuiltinOperator_PAD)
2888 else if (opcode == tflite::BuiltinOperator_PADV2)
2892 armnn::TensorInfo padValueTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
2901 if (padValueBufferPtr->data.size() > 0)
2907 std::vector<float> padValueBuffer(padValueTensorInfo.
GetNumElements());
2908 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2914 std::vector<uint8_t> padValueBuffer(padValueTensorInfo.
GetNumElements());
2915 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2916 desc.
m_PadValue = armnn::Dequantize<uint8_t>(padValueBuffer[0],
2924 std::vector<int8_t> padValueBuffer(padValueTensorInfo.
GetNumElements());
2925 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2926 desc.
m_PadValue = armnn::Dequantize<int8_t>(padValueBuffer[0],
2940 for (
unsigned int i = 0; i < padTensorInfo.
GetNumElements() / step; ++i)
2942 desc.
m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
2945 auto layerName = (opcode == tflite::BuiltinOperator_PAD) ? fmt::format(
"Pad:{}:{}", subgraphIndex, operatorIndex)
2946 : fmt::format(
"PadV2:{}:{}", subgraphIndex, operatorIndex);
2956 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2959 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2960 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2962 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2963 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2966 void TfLiteParserImpl::ParseMirrorPad(
size_t subgraphIndex,
size_t operatorIndex)
2968 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2976 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2978 armnn::TensorInfo padTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2981 std::vector<unsigned int> padBuffer(padTensorInfo.
GetNumElements());
2982 ::memcpy(padBuffer.data(), bufferPtr->data.data(), padTensorInfo.
GetNumBytes());
2986 for (
unsigned int i = 0; i < padTensorInfo.
GetNumElements() / step; ++i)
2988 desc.
m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
2991 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2992 const auto* options = operatorPtr->builtin_options.AsMirrorPadOptions();
2994 if (options->mode == tflite::MirrorPadMode_REFLECT)
2998 else if (options->mode == tflite::MirrorPadMode_SYMMETRIC)
3009 auto inputShape = inputTensorInfo.
GetShape();
3012 const unsigned int isReflect =
static_cast<unsigned int>(desc.
m_PaddingMode == PaddingMode::Reflect);
3013 for(
unsigned int i = 0; i < padList.size(); ++i)
3015 if(padList.at(i).first > (inputShape[i] - isReflect) ||
3016 padList.at(i).second > (inputShape[i] - isReflect))
3019 "equal (Symmetric) to the dimension size.");
3023 auto layerName = fmt::format(
"MirrorPad:{}:{}", subgraphIndex, operatorIndex);
3033 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
3036 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3037 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3039 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3040 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3043 void TfLiteParserImpl::ParsePrelu(
size_t subgraphIndex,
size_t operatorIndex)
3045 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3047 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3050 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3053 auto layerName = fmt::format(
"Prelu:{}:{}", subgraphIndex, operatorIndex);
3055 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3056 armnn::TensorInfo alphaTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
3066 if (IsConstTensor(inputs[1]))
3068 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3070 RegisterConsumerOfTensor(subgraphIndex, inputTensorIndexes[0], slot);
3072 auto alphaTensorAndData = CreateConstTensorNonPermuted(inputs[1], alphaTensorInfo,
3074 std::string constLayerName = fmt::format(
"Constant:{}", inputs[1]->name);
3076 m_Network->AddConstantLayer(alphaTensorAndData.first, constLayerName.c_str());
3086 RegisterOutputSlots(subgraphIndex,
3087 VIRTUAL_OPERATOR_ID,
3089 { inputTensorIndexes[1] });
3093 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3094 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIndexes);
3097 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
3100 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3101 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3104 void TfLiteParserImpl::ParseQuantize(
size_t subgraphIndex,
size_t operatorIndex)
3106 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3108 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3111 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3114 auto layerName = fmt::format(
"Quantize:{}:{}", subgraphIndex, operatorIndex);
3124 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
3127 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3128 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3130 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3131 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3134 void TfLiteParserImpl::ParseRelu(
size_t subgraphIndex,
size_t operatorIndex)
3136 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::ReLu);
3139 void TfLiteParserImpl::ParseRelu6(
size_t subgraphIndex,
size_t operatorIndex)
3141 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::BoundedReLu);
3144 void TfLiteParserImpl::ParseLeakyRelu(
size_t subgraphIndex,
size_t operatorIndex)
3146 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::LeakyReLu);
3149 void TfLiteParserImpl::ParseLogistic(
size_t subgraphIndex,
size_t operatorIndex)
3151 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::Sigmoid);
3154 void TfLiteParserImpl::ParseTanH(
size_t subgraphIndex,
size_t operatorIndex)
3156 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::TanH);
3159 void TfLiteParserImpl::ParseElu(
size_t subgraphIndex,
size_t operatorIndex)
3161 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::Elu);
3164 void TfLiteParserImpl::ParseHardSwish(
size_t subgraphIndex,
size_t operatorIndex)
3166 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::HardSwish);
3169 void TfLiteParserImpl::ParseGelu(
size_t subgraphIndex,
size_t operatorIndex)
3171 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::Gelu);
3174 void TfLiteParserImpl::ParseActivation(
size_t subgraphIndex,
size_t operatorIndex,
ActivationFunction activationType)
3176 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3177 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3180 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3183 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3186 auto layerName = fmt::format(
"Activation:");
3190 switch (activationType)
3192 case ActivationFunction::ReLu:
3194 layerName += fmt::format(
"RELU:{}:{}", subgraphIndex, operatorIndex);
3197 case ActivationFunction::BoundedReLu:
3199 layerName += fmt::format(
"RELU6:{}:{}", subgraphIndex, operatorIndex);
3200 activationDesc.
m_A = 6.0f;
3201 activationDesc.
m_B = 0.0f;
3204 case ActivationFunction::Sigmoid:
3206 layerName += fmt::format(
"SIGMOID:{}:{}", subgraphIndex, operatorIndex);
3209 case ActivationFunction::TanH:
3211 layerName += fmt::format(
"TANH:{}:{}", subgraphIndex, operatorIndex);
3212 activationDesc.
m_A = 1.0f;
3213 activationDesc.
m_B = 1.0f;
3216 case ActivationFunction::LeakyReLu:
3218 layerName += fmt::format(
"LEAKYRELU:{}:{}", subgraphIndex, operatorIndex);
3219 const auto* options = operatorPtr->builtin_options.AsLeakyReluOptions();
3220 activationDesc.
m_A = options->alpha;
3223 case ActivationFunction::Elu:
3225 layerName += fmt::format(
"ELU:{}:{}", subgraphIndex, operatorIndex);
3226 activationDesc.
m_A = 1.0f;
3229 case ActivationFunction::HardSwish:
3231 layerName += fmt::format(
"HARDSWISH:{}:{}", subgraphIndex, operatorIndex);
3234 case ActivationFunction::Gelu:
3236 layerName += fmt::format(
"GELU:{}:{}", subgraphIndex, operatorIndex);
3242 fmt::format(
"Unexpected ActivationFunction[{}] when creating layerName {} ",
3247 IConnectableLayer*
const layer = m_Network->AddActivationLayer(activationDesc, layerName.c_str());
3249 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
3254 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3255 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3258 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3259 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3263 const std::vector<int32_t>& targetDimsIn)
3265 std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
3266 const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
3268 if (stretchDim != targetDimsIn.end())
3270 if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
3273 fmt::format(
"At most one component of shape can be -1 {}",
CHECK_LOCATION().AsString()));
3276 auto targetNumElements =
3277 armnn::numeric_cast<unsigned int>(
3278 std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
3280 auto stretchIndex =
static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
3282 if (targetNumElements == 0)
3286 outputDims[stretchIndex] = 0;
3291 fmt::format(
"Input to reshape is a tensor with elements, but the requested shape has 0. {}",
3297 outputDims[stretchIndex] = inputTensorInfo.
GetNumElements() / targetNumElements;
3309 void TfLiteParserImpl::ParseReshape(
size_t subgraphIndex,
size_t operatorIndex)
3311 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3313 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3315 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3318 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3319 const auto* options = operatorPtr->builtin_options.AsReshapeOptions();
3320 auto layerName = fmt::format(
"Reshape:{}:{}", subgraphIndex, operatorIndex);
3322 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3324 CheckMatchingQuantization(inputTensorInfo, actualOutputTensorInfo, layerName,
"Input 0",
"Output 0");
3330 std::vector<int32_t> targetShape;
3331 bool targetShapeFound =
false;
3333 if (options !=
nullptr)
3336 if (options->new_shape.empty() ==
false)
3338 targetShape = options->new_shape;
3339 targetShapeFound =
true;
3344 if (!targetShapeFound)
3347 if (inputs.size() > 1 && inputs[1] !=
nullptr)
3349 if (inputs[1]->is_variable)
3354 if (inputs[1]->shape.size() != 1)
3359 if (inputs[1]->type != tflite::TensorType_INT32)
3365 auto bufferPtr =
GetBuffer(m_Model, inputs[1]->buffer);
3366 auto values =
reinterpret_cast<const int32_t*
>(bufferPtr->data.data());
3369 for (
int i = 0; i < inputs[1]->shape[0]; ++i)
3371 targetShape.push_back(values[i]);
3383 for (
unsigned int i = 0; i < actualOutputTensorInfo.
GetShape().GetNumDimensions(); ++i)
3385 targetShape.push_back(actualOutputTensorInfo.
GetShape()[i]);
3389 else if (reshapeShapes[0] > 2)
3391 throw ParseException(fmt::format(
"Invalid input shape '{}' in Reshape layer '{}' {}. "
3392 "When inferring during runtime, the parser only supports "
3393 "shape (batch, -1) or (-1) for target shape input.",
3400 const int32_t numInputElements = inputTensorInfo.
GetNumElements();
3401 const int32_t inputTensorShape = inputTensorInfo.
GetShape()[0];
3402 if (reshapeShapes[0] == 1)
3404 targetShape = {numInputElements};
3406 else if (reshapeShapes[0] == 2)
3408 targetShape = {inputTensorShape, numInputElements / inputTensorShape};
3412 catch (
const std::exception& exc)
3415 "Reshape operation. Reshape operator target shape input buffer data "
3416 "is null. " << exc.what());
3423 "At least one method required");
3436 if (inputs.size() > 1 && !
CheckShape(reshapeOutputTensorShape, outputs[0]->shape))
3440 std::vector<int32_t> secondaryOutputTargetShape = outputs[0]->shape_signature;
3447 if (!
CheckShape(reshapeOutputTensorShape, secondaryReshapeOutputTensorInfo.
GetShape()))
3449 std::stringstream ss;
3450 ss <<
"New shape defined in reshape parameters "
3451 << reshapeOutputTensorShape
3452 <<
" does not equal output shape "
3453 << actualOutputTensorInfo.
GetShape()
3463 m_TensorInfos[outputTensorIds[0]] = reshapeOutputTensorInfo;
3465 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
3475 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3476 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3478 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3479 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3482 void TfLiteParserImpl::ParseResizeBilinear(
size_t subgraphIndex,
size_t operatorIndex)
3484 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::Bilinear);
3487 void TfLiteParserImpl::ParseResizeNearestNeighbor(
size_t subgraphIndex,
size_t operatorIndex)
3489 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::NearestNeighbor);
3492 void TfLiteParserImpl::ParseResize(
size_t subgraphIndex,
size_t operatorIndex,
ResizeMethod resizeMethod)
3494 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3496 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3499 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3502 armnn::TensorInfo sizeTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
3505 std::vector<int32_t> sizeTensorData(sizeTensorInfo.
GetNumElements());
3508 ::memcpy(sizeTensorData.data(), sizeBufferPtr->data.data(), sizeTensorInfo.
GetNumBytes());
3513 desc.
m_TargetWidth =
static_cast<uint32_t
> (sizeTensorData[1]);
3516 auto layerName = fmt::format(
"Resize:");
3518 switch (resizeMethod)
3520 case ResizeMethod::Bilinear:
3522 layerName += fmt::format(
"BILINEAR:{}:{}", subgraphIndex, operatorIndex);
3524 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3525 const auto * options = operatorPtr->builtin_options.AsResizeBilinearOptions();
3530 case ResizeMethod::NearestNeighbor:
3532 layerName += fmt::format(
"NEARESTNEIGHBOR:{}:{}", subgraphIndex, operatorIndex);
3538 fmt::format(
"Unexpected ResizeMethod[{}] when creating layerName {} ",
3543 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3553 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
3554 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
3557 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3558 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3560 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3561 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3564 void TfLiteParserImpl::ParseReverseV2(
size_t subgraphIndex,
size_t operatorIndex)
3566 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3568 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3571 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3574 auto layerName = fmt::format(
"ReverseV2:{}:{}", subgraphIndex, operatorIndex);
3585 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3586 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3588 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3589 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3592 void TfLiteParserImpl::ParseTile(
size_t subgraphIndex,
size_t operatorIndex)
3594 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3596 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3599 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3606 auto layerName = fmt::format(
"Tile:{}:{}", subgraphIndex, operatorIndex);
3611 if (multiplesBufferPtr !=
nullptr)
3613 std::vector<int32_t> multiplesData(multiplesTensorInfo.
GetNumElements());
3614 ::memcpy(multiplesData.data(), multiplesBufferPtr->data.data(), multiplesTensorInfo.
GetNumBytes());
3615 descriptor.
m_Multiples.assign(multiplesData.begin(), multiplesData.end());
3622 IConnectableLayer* layer = m_Network->AddTileLayer(descriptor, layerName.c_str());
3627 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3628 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3630 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3631 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3634 void TfLiteParserImpl::ParseConcatenation(
size_t subgraphIndex,
size_t operatorIndex)
3636 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3638 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3639 const auto* options = operatorPtr->builtin_options.AsConcatenationOptions();
3643 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3644 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3649 unsigned int numConcatView =
static_cast<unsigned int>(inputs.size());
3650 uint32_t inputRank = InputTensorInfo(subgraphIndex, operatorIndex, 0).
GetNumDimensions();
3652 const unsigned int concatDimInput =
static_cast<unsigned int>(
3653 (
static_cast<int>(inputRank) + options->axis) %
static_cast<int>(inputRank));
3655 OriginsDescriptor concatDescriptor(
static_cast<uint32_t
>(numConcatView), inputRank);
3656 concatDescriptor.SetConcatAxis(concatDimInput);
3657 unsigned int mergeDimOrigin = 0;
3659 for (
unsigned int viewIndex = 0; viewIndex < numConcatView; ++viewIndex)
3661 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, viewIndex);
3665 inputTensorInfo, concatDescriptor, concatDimInput, viewIndex, mergeDimOrigin);
3668 auto layerName = fmt::format(
"Concatenation:{}:{}", subgraphIndex, operatorIndex);
3670 IConnectableLayer* layer = m_Network->AddConcatLayer(concatDescriptor, layerName.c_str());
3678 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {});
3681 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3682 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
3685 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
3687 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3688 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3691 void TfLiteParserImpl::ParseFullyConnected(
size_t subgraphIndex,
size_t operatorIndex)
3693 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3695 const auto& operatorRfr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3696 const auto options = operatorRfr->builtin_options.AsFullyConnectedOptions();
3704 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3705 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3708 armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
3711 int32_t weightsDimension =
static_cast<int32_t
>(filterTensorInfo.
GetNumDimensions());
3712 if (weightsDimension != 2)
3715 fmt::format(
"Dimension {} for Fully Connected weights is not supported by Armnn. "
3722 auto layerName = fmt::format(
"FullyConnected:{}:{}", subgraphIndex, operatorIndex);
3724 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3726 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0]};
3727 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3732 tensorIndexesToRegister.emplace_back(inputTensorIndexes[1]);
3734 if (ShouldConstantTensorBeConverted(inputs[1], inputTensorInfo.
GetDataType(), filterTensorInfo.
GetDataType()))
3736 m_ConstantsToDequantize.emplace_back(inputs[1]->buffer);
3739 if (inputs.size() == 3)
3742 armnn::TensorInfo biasTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
3745 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
3747 if (ShouldConstantTensorBeConverted(inputs[2], inputTensorInfo.
GetDataType(), biasTensorInfo.
GetDataType()))
3749 m_ConstantsToDequantize.emplace_back(inputs[2]->buffer);
3754 layer = m_Network->AddFullyConnectedLayer(desc, layerName.c_str());
3762 unsigned int startingSlotIndex = 0;
3769 std::vector<unsigned int> reshapedDimensions(2);
3770 reshapedDimensions[1] = filterTensorInfo.
GetShape()[1];
3771 reshapedDimensions[0] = inputTensorInfo.
GetNumElements() / reshapedDimensions[1];
3773 if (inputTensorInfo.
GetNumElements() % reshapedDimensions[1] != 0)
3776 fmt::format(
"Failed to deduce input tensor shape from filter size {} {}",
3777 reshapedDimensions[1],
3781 armnn::TensorInfo reshapedTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3783 inputTensorInfo = reshapedTensorInfo;
3785 std::string reshapeLayerName = fmt::format(
"Reshape_for:{}", layer->
GetName());
3789 reshapeLayerName.c_str());
3794 RegisterInputSlots(subgraphIndex, operatorIndex, reshapeLayer, {inputTensorIndexes[0]});
3796 tensorIndexesToRegister.erase(tensorIndexesToRegister.begin());
3797 startingSlotIndex = 1;
3800 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister, startingSlotIndex);
3802 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromShapes(subgraphIndex, operatorIndex, layer, 0,
3811 std::vector<unsigned int> reshapedDimensions(2);
3812 reshapedDimensions[1] = filterTensorInfo.
GetShape()[0];
3813 reshapedDimensions[0] = outputTensorInfo.
GetNumElements() / reshapedDimensions[1];
3815 if (outputTensorInfo.
GetNumElements() % reshapedDimensions[1] != 0)
3818 fmt::format(
"Failed to deduce output tensor shape from filter size {} {}",
3819 reshapedDimensions[1],
3825 std::string reshapeLayerName = fmt::format(
"ExpandDims:{}:{}", subgraphIndex, operatorIndex);
3826 layer = AddReshapeLayer(layer, 0, reshapeLayerName, outputTensorInfo);
3831 options->fused_activation_function);
3834 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3835 RegisterOutputSlots(subgraphIndex, operatorIndex, fusedActivationLayer, {outputTensorIndexes[0]});
3840 void TfLiteParserImpl::ParseDetectionPostProcess(
size_t subgraphIndex,
size_t operatorIndex)
3842 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3844 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3846 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3847 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3851 auto custom_options = operatorPtr->custom_options;
3852 const flexbuffers::Map& m = flexbuffers::GetRoot(custom_options.data(), custom_options.size()).AsMap();
3861 desc.
m_ScaleH = m[
"h_scale"].AsFloat();
3862 desc.
m_ScaleW = m[
"w_scale"].AsFloat();
3863 desc.
m_ScaleX = m[
"x_scale"].AsFloat();
3864 desc.
m_ScaleY = m[
"y_scale"].AsFloat();
3866 if (!(m[
"use_regular_nms"].IsNull()))
3870 if (!(m[
"detections_per_class"].IsNull()))
3878 "must be positive and less than or equal to 1.");
3881 armnn::TensorInfo anchorTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
3882 auto anchorTensorAndData = CreateConstTensorNonPermuted(inputs[2], anchorTensorInfo);
3884 auto layerName = fmt::format(
"DetectionPostProcess:{}:{}", subgraphIndex, operatorIndex);
3885 IConnectableLayer* layer = m_Network->AddDetectionPostProcessLayer(desc, anchorTensorAndData,
3897 m_OverriddenOutputShapes.push_back({ 1, numDetectedBox, 4 });
3898 m_OverriddenOutputShapes.push_back({ 1, numDetectedBox });
3899 m_OverriddenOutputShapes.push_back({ 1, numDetectedBox });
3900 m_OverriddenOutputShapes.push_back({ 1 });
3902 for (
unsigned int i = 0 ; i < outputs.size() ; ++i)
3910 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3911 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3914 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3915 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0],
3916 outputTensorIndexes[1],
3917 outputTensorIndexes[2],
3918 outputTensorIndexes[3]});
3922 void TfLiteParserImpl::ParsePack(
size_t subgraphIndex,
size_t operatorIndex)
3924 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3926 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3927 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3930 if (inputs.size() < 1)
3935 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3936 const auto* options = operatorPtr->builtin_options.AsPackOptions();
3939 desc.
m_Axis =
static_cast<uint32_t
>(options->axis);
3940 desc.
m_NumInputs =
static_cast<uint32_t
>(inputs.size());
3943 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3946 auto layerName = fmt::format(
"Pack:{}:{}", subgraphIndex, operatorIndex);
3955 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {});
3958 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3959 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
3961 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3962 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3965 void TfLiteParserImpl::ParseUnidirectionalSequenceLSTM(
size_t subgraphIndex,
size_t operatorIndex)
3967 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3969 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3970 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3972 if (inputs.size() < 2)
3974 throw ParseException(
"UnidirectionalSequenceLSTM must have at least 2 input.");
3977 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3978 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
3979 const auto nodeParams = operatorPtr->builtin_options.AsUnidirectionalSequenceLSTMOptions();
3981 auto inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3989 if (IsOptionalOperandPresent(operatorPtr->inputs[1]))
3991 params.
m_InputToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[1]].get(),
3992 inputTensorInfo).first;
3996 inputTensorInfo).first;
3997 params.
m_InputToCellWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[3]].get(),
3998 inputTensorInfo).first;
4000 inputTensorInfo).first;
4003 if (IsOptionalOperandPresent(operatorPtr->inputs[5]))
4006 inputTensorInfo).first;
4010 inputTensorInfo).first;
4012 inputTensorInfo).first;
4014 inputTensorInfo).first;
4017 if (IsOptionalOperandPresent(operatorPtr->inputs[9]))
4019 params.
m_CellToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[9]].get(),
4020 inputTensorInfo).first;
4023 if (IsOptionalOperandPresent(operatorPtr->inputs[10]))
4025 params.
m_CellToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[10]].get(),
4026 inputTensorInfo).first;
4029 if (IsOptionalOperandPresent(operatorPtr->inputs[11]))
4031 params.
m_CellToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[11]].get(),
4032 inputTensorInfo).first;
4036 if (IsOptionalOperandPresent(operatorPtr->inputs[12]))
4038 params.
m_InputGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[12]].get(),
4039 inputTensorInfo).first;
4042 params.
m_ForgetGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[13]].get(),
4043 inputTensorInfo).first;
4044 params.
m_CellBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[14]].get(),
4045 inputTensorInfo).first;
4046 params.
m_OutputGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[15]].get(),
4047 inputTensorInfo).first;
4050 if (IsOptionalOperandPresent(operatorPtr->inputs[16]))
4052 params.
m_ProjectionWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[16]].get(),
4053 inputTensorInfo).first;
4056 if (IsOptionalOperandPresent(operatorPtr->inputs[17]))
4058 params.
m_ProjectionBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[17]].get(),
4059 inputTensorInfo).first;
4064 m_ConstantsToBeCreated.push_back(operatorPtr->inputs[18]);
4066 m_ConstantsToBeCreated.push_back(operatorPtr->inputs[19]);
4069 if (inputs.size() >= 21 && IsOptionalOperandPresent(operatorPtr->inputs[20]))
4072 inputTensorInfo).first;
4075 if (inputs.size() >= 22 && IsOptionalOperandPresent(operatorPtr->inputs[21]))
4078 inputTensorInfo).first;
4081 if (inputs.size() >= 23 && IsOptionalOperandPresent(operatorPtr->inputs[22]))
4083 params.
m_CellLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[22]].get(),
4084 inputTensorInfo).first;
4087 if (inputs.size() >= 24 && IsOptionalOperandPresent(operatorPtr->inputs[23]))
4090 inputTensorInfo).first;
4111 auto inputIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[0]].get(),
4112 inputTensorInfo).first;
4113 auto inputIntermediateTensorInfo = inputIntermediate->GetInfo();
4116 auto forgetIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[1]].get(),
4117 inputTensorInfo).first;
4118 auto forgetIntermediateTensorInfo = forgetIntermediate->GetInfo();
4121 auto cellIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[2]].get(),
4122 inputTensorInfo).first;
4123 auto cellIntermediateTensorInfo = cellIntermediate->GetInfo();
4126 auto outputIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[3]].get(),
4127 inputTensorInfo).first;
4128 auto outputIntermediateTensorInfo = outputIntermediate->GetInfo();
4133 float defaultIntermediate = std::pow(2, -12);
4140 if (operatorPtr->intermediates.size() > 4)
4142 auto hiddentensor = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[4]].get(),
4143 inputTensorInfo).first;
4149 unsigned int outputSize = outputTensorInfo.
GetShape()[2];
4150 unsigned int numUnits = cellStateInInfo.
GetShape()[1];
4156 armnn::TensorInfo scratchBufferTensorInfo({batchSize, numUnits * 3}, dataType, qScale, qOffset);
4159 scratchBufferTensorInfo =
armnn::TensorInfo({batchSize, numUnits * 4}, dataType, qScale, qOffset);
4165 armnn::TensorInfo outputStateOutTensorInfo({batchSize, outputSize}, dataType, qScale, qOffset);
4215 auto layerName = fmt::format(
"UnidirectionalSequenceLSTM:{}:{}", subgraphIndex, operatorIndex);
4226 auto inputTensorIndexes = AsUnsignedVector({operatorPtr->inputs[0],
4227 operatorPtr->inputs[18],
4228 operatorPtr->inputs[19]});
4229 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0],
4230 inputTensorIndexes[1],
4231 inputTensorIndexes[2]});
4233 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4239 unsigned int tensorIndex = outputTensorIndexes[0];
4241 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
4244 void TfLiteParserImpl::ParseUnpack(
size_t subgraphIndex,
size_t operatorIndex)
4246 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4248 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4249 const auto* options = operatorPtr->builtin_options.AsUnpackOptions();
4254 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
4257 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4262 fmt::format(
"The unpack axis: {} cannot be greater than or equal to "
4263 "the number of input dimension {} {}",
4273 unpackNum = inputTensorInfo.
GetShape()[unpackAxis];
4279 throw ParseException(
"Number to unpack must greater than zero.");
4282 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
4286 std::vector<unsigned int> unpackDimSizes(inputDimSize);
4289 for (
unsigned int i = 0; i < inputDimSize; ++i)
4291 unpackDimSizes[i] = inputTensorInfo.
GetShape()[i];
4294 if (unpackDimSizes[unpackAxis] != unpackNum)
4296 throw ParseException(
"Number to unpack must be the same as length of the dimension to "
4300 unpackDimSizes[unpackAxis] /= unpackNum;
4302 SplitterDescriptor splitDesc(unpackNum,
static_cast<unsigned int>(unpackDimSizes.size()));
4303 for (
unsigned int j = 0; j < unpackNum; ++j)
4306 for (
unsigned int dimIdx = 0; dimIdx < unpackDimSizes.size(); ++dimIdx)
4308 splitDesc.SetViewSize(j, dimIdx, unpackDimSizes[dimIdx]);
4310 splitDesc.SetViewOriginCoord(j, unpackAxis, unpackDimSizes[unpackAxis] * j);
4312 splitDesc.SetAxis(unpackAxis);
4313 auto layerName = fmt::format(
"Unpack:{}:{}", subgraphIndex, operatorIndex);
4314 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
4323 unpackDimSizes.data());
4325 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4326 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4328 std::vector<unsigned int> reshapeDims;
4329 for (
unsigned int axis = 0; axis < splitOutShape.
GetNumDimensions(); ++axis)
4331 if (axis != unpackAxis)
4333 reshapeDims.push_back(splitOutShape[axis]);
4343 std::string reshapeLayerName = fmt::format(
"Reshape_for:{}", layer->
GetName());
4358 RegisterProducerOfTensor(subgraphIndex, reshapedOutputId, slot);
4362 void TfLiteParserImpl::ParseSplit(
size_t subgraphIndex,
size_t operatorIndex)
4364 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4366 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4367 const auto* options = operatorPtr->builtin_options.AsSplitOptions();
4374 throw ParseException(
"Number to splits must greater than zero.");
4377 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
4379 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
4382 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4383 armnn::TensorInfo axisTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4387 throw ParseException(fmt::format(
"Axis tensor can only have 1 element {}",
4392 if (axisBufferPtr ==
nullptr)
4395 fmt::format(
"Operation has invalid inputs. Failed to read axis. {}",
4400 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.
GetNumBytes());
4401 int32_t axis = axisData[0];
4403 auto inputDimensions =
static_cast<int32_t
>(inputTensorInfo.
GetNumDimensions());
4404 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4410 fmt::format(
"Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
4421 fmt::format(
"The number of dimensions: {} for input tensors of the split op cannot be greater than {} {}",
4427 std::vector<unsigned int> splitterDimSizes(inputDimSize);
4430 for (
unsigned int i = 0; i < inputDimSize; ++i)
4432 splitterDimSizes[i] = inputTensorInfo.
GetShape()[i];
4435 if (splitterDimSizes[splitDim] % numSplits != 0)
4437 throw ParseException(
"Number of splits must evenly divide the dimension");
4439 splitterDimSizes[splitDim] /= numSplits;
4442 for (
unsigned int j = 0; j < numSplits; ++j)
4445 for (
unsigned int dimIdx = 0; dimIdx < splitterDimSizes.size(); ++dimIdx)
4447 splitDesc.SetViewSize(j, dimIdx, splitterDimSizes[dimIdx]);
4449 splitDesc.SetViewOriginCoord(j, splitDim, splitterDimSizes[splitDim] * j);
4453 splitDesc.SetAxis(axis);
4455 auto layerName = fmt::format(
"Split:{}:{}", subgraphIndex, operatorIndex);
4456 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
4464 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4465 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[1]});
4473 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4474 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4479 int numDims = armnn::numeric_cast<int>(numDimsIn);
4480 int v = idx < 0 ? numDims + idx : idx;
4482 if (v < 0 || v > numDims)
4487 return static_cast<unsigned int>(v);
4490 void TfLiteParserImpl::ParseSplitV(
size_t subgraphIndex,
size_t operatorIndex)
4492 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4494 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4495 const auto* options = operatorPtr->builtin_options.AsSplitVOptions();
4497 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
4500 auto& inputTensor = inputs[0];
4501 auto& splitsTensor = inputs[1];
4502 auto& axisTensor = inputs[2];
4510 throw ParseException(fmt::format(
"Axis tensor can only have 1 element {}",
4519 fmt::format(
"The number of dimensions: {} for input tensors of the "
4520 "SplitV op cannot be greater than {} {}",
4528 if (axisBufferPtr ==
nullptr)
4531 fmt::format(
"Operation has invalid inputs. Failed to read axis. {}",
4536 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.
GetNumBytes());
4537 int32_t axis = axisData[0];
4539 auto inputDimensions =
static_cast<int32_t
>(inputTensorInfo.
GetNumDimensions());
4540 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4546 fmt::format(
"Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
4554 unsigned int numSplits{0};
4570 std::vector<int> splitsData(numSplits);
4572 ::memcpy(splitsData.data(), splitsBufferPtr->data.data(), splitsInfo.
GetNumBytes());
4574 unsigned int idx = 0;
4576 unsigned int inferIdx{0};
4578 for (
auto split : splitsData)
4592 if (numInferred == 0)
4594 if (splitSum != armnn::numeric_cast<int>(inputTensorInfo.
GetShape()[splitDim]))
4596 throw ParseException(
"SplitV split_sizes does not sum to the dimension of value along split_dim.");
4599 else if (numInferred == 1)
4601 splitsData[inferIdx] = armnn::numeric_cast<int>(inputTensorInfo.
GetShape()[splitDim]) - splitSum;
4605 throw ParseException(
"Cannot infer split size for more than one split");
4609 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
4614 unsigned int accumSplit = 0;
4615 for (
unsigned int j = 0; j < numSplits; ++j)
4617 unsigned int splitSize = armnn::numeric_cast<unsigned int>(splitsData[j]);
4620 for (
unsigned int dimIdx = 0; dimIdx < inputTensorInfo.
GetNumDimensions(); ++dimIdx)
4622 unsigned int dimSize = inputTensorInfo.
GetShape()[dimIdx];
4623 if (dimIdx == splitDim)
4625 dimSize = splitSize;
4627 splitDesc.SetViewSize(j, dimIdx, dimSize);
4630 splitDesc.SetViewOriginCoord(j, splitDim, accumSplit);
4631 accumSplit += splitSize;
4633 splitDesc.SetAxis(axis);
4635 auto layerName = fmt::format(
"SplitV:{}:{}", subgraphIndex, operatorIndex);
4636 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
4644 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4645 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4653 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4654 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4657 void TfLiteParserImpl::ParseArgMin(
size_t subgraphIndex,
size_t operatorIndex)
4662 void TfLiteParserImpl::ParseArgMax(
size_t subgraphIndex,
size_t operatorIndex)
4667 void TfLiteParserImpl::ParseArgMinMax(
size_t subgraphIndex,
size_t operatorIndex,
ArgMinMaxFunction argMinMaxFunction)
4669 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4670 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
4673 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
4676 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4677 armnn::TensorInfo axisTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4682 throw ParseException(fmt::format(
"Axis tensor can only have 1 element {}",
4692 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
4698 if (axisBufferPtr ==
nullptr)
4701 fmt::format(
"Operation has invalid inputs. Failed to read axis. {}",
4706 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.
GetNumBytes());
4707 int32_t axis = axisData.front();
4709 auto inputDimensions =
static_cast<int32_t
>(inputTensorInfo.
GetNumDimensions());
4710 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4716 fmt::format(
"Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
4726 auto layerName = argMinMaxFunction == ArgMinMaxFunction::Max ?
"ArgMax:{}:{}" :
"ArgMin:{}:{}";
4727 auto layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4728 IConnectableLayer *layer = m_Network->AddArgMinMaxLayer(desc, layerNameFormatted.c_str());
4736 outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
4740 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4741 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4744 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4745 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4748 void TfLiteParserImpl::ParseGather(
size_t subgraphIndex,
size_t operatorIndex)
4750 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4757 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4758 armnn::TensorInfo indicesTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4763 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4764 const auto* options = operatorPtr->builtin_options.AsGatherOptions();
4765 auto axis = options->axis;
4767 auto layerName = fmt::format(
"Gather:{}:{}", subgraphIndex, operatorIndex);
4769 auto inputDimensions =
static_cast<int32_t
>(inputTensorInfo.
GetNumDimensions());
4772 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4775 fmt::format(
"Operation has invalid axis: {} It is out of bounds [ -{}, {} ) {}",
4777 inputDimensions, inputDimensions,
4780 if (outputDimensions !=
static_cast<unsigned int>(inputDimensions) + indicesDimensions - 1)
4783 fmt::format(
"Operation has invalid output dimensions: {} Output must be an ({} + {} - 1) -D tensor {}",
4785 inputDimensions, indicesDimensions,
4789 gatherDescriptor.
m_Axis = axis;
4791 IConnectableLayer* layer = m_Network->AddGatherLayer(gatherDescriptor, layerName.c_str());
4799 outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
4802 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4803 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4805 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4806 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4809 void TfLiteParserImpl::ParseGatherNd(
size_t subgraphIndex,
size_t operatorIndex)
4811 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4818 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4819 armnn::TensorInfo indicesTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4821 auto layerName = fmt::format(
"GatherNd:{}:{}", subgraphIndex, operatorIndex);
4830 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
4833 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4834 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4836 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4837 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4840 void TfLiteParserImpl::ParseDepthToSpace(
size_t subgraphIndex,
size_t operatorIndex)
4842 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4851 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4852 const auto* options = operatorPtr->builtin_options.AsDepthToSpaceOptions();
4853 auto blockSize = options->block_size;
4857 fmt::format(
"Operation has invalid block size: {} Block size should be >= 2 {}",
4861 descriptor.
m_BlockSize = armnn::numeric_cast<uint32_t>(blockSize);
4863 auto layerName = fmt::format(
"DepthToSpace:{}:{}", subgraphIndex, operatorIndex);
4864 IConnectableLayer* layer = m_Network->AddDepthToSpaceLayer(descriptor, layerName.c_str());
4872 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
4875 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4876 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4878 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4879 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4882 void TfLiteParserImpl::ParseSum(
size_t subgraphIndex,
size_t operatorIndex)
4887 void TfLiteParserImpl::ParseReduceProd(
size_t subgraphIndex,
size_t operatorIndex)
4892 void TfLiteParserImpl::ParseReduceMax(
size_t subgraphIndex,
size_t operatorIndex)
4897 void TfLiteParserImpl::ParseReduceMin(
size_t subgraphIndex,
size_t operatorIndex)
4902 void TfLiteParserImpl::ParseReduce(
size_t subgraphIndex,
size_t operatorIndex,
ReduceOperation reduceOperation)
4904 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4906 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4907 const auto* options = operatorPtr->builtin_options.AsReducerOptions();
4909 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
4912 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
4915 auto layerName = fmt::format(
"Reduce:{}:{}", subgraphIndex, operatorIndex);
4917 armnn::TensorInfo inputTensorInfo0 = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4918 armnn::TensorInfo inputTensorInfo1 = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4923 if (axisBufferPtr !=
nullptr)
4925 std::vector<int32_t> axisData(inputTensorInfo1.
GetNumElements());
4926 ::memcpy(axisData.data(), axisBufferPtr->data.data(), inputTensorInfo1.
GetNumBytes());
4930 std::set<unsigned int> uniqueAxis;
4931 std::transform(axisData.begin(),
4933 std::inserter(uniqueAxis, uniqueAxis.begin()),
4934 [rank](
int i)->unsigned
int{
4935 return static_cast<uint32_t>(((i + rank) % rank)); });
4936 desc.
m_vAxis.assign(uniqueAxis.begin(), uniqueAxis.end());
4952 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
4956 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4957 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4960 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4961 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4964 void TfLiteParserImpl::ParseLocalResponseNormalization(
size_t subgraphIndex,
size_t operatorIndex)
4966 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4968 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
4971 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
4974 auto layerName = fmt::format(
"LRN:{}:{}", subgraphIndex, operatorIndex);
4975 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4977 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4979 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4980 const auto* options = operatorPtr->builtin_options.AsLocalResponseNormalizationOptions();
4986 descriptor.
m_NormSize =
static_cast<uint32_t
>(options->radius);
4987 descriptor.
m_K = options->bias;
4988 descriptor.
m_Alpha = options->alpha;
4989 descriptor.
m_Beta = options->beta;
4995 IConnectableLayer* layer = m_Network->AddNormalizationLayer(descriptor, layerNameFormatted.c_str());
5003 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
5006 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
5007 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
5009 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
5010 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
5013 void TfLiteParserImpl::ParseAbs(
size_t subgraphIndex,
size_t operatorIndex)
5018 void TfLiteParserImpl::ParseCeil(
size_t subgraphIndex,
size_t operatorIndex)
5023 void TfLiteParserImpl::ParseExp(
size_t subgraphIndex,
size_t operatorIndex)
5028 void TfLiteParserImpl::ParseLog(
size_t subgraphIndex,
size_t operatorIndex)
5033 void TfLiteParserImpl::ParseLogicalNot(
size_t subgraphIndex,
size_t operatorIndex)
5038 void TfLiteParserImpl::ParseNeg(
size_t subgraphIndex,
size_t operatorIndex)
5043 void TfLiteParserImpl::ParsePower(
size_t subgraphIndex,
size_t operatorIndex)
5045 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
5047 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
5050 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
5053 auto layerName = fmt::format(
"Power:{}:{}", subgraphIndex, operatorIndex);
5055 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
5056 TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
5057 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName,
"Input 0",
"Input 1");
5059 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Power, layerName.c_str());
5067 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
5068 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
5071 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
5072 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
5074 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
5075 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
5078 void TfLiteParserImpl::ParseRsqrt(
size_t subgraphIndex,
size_t operatorIndex)
5083 void TfLiteParserImpl::ParseSin(
size_t subgraphIndex,
size_t operatorIndex)
5088 void TfLiteParserImpl::ParseSqrt(
size_t subgraphIndex,
size_t operatorIndex)
5093 void TfLiteParserImpl::ParseSquare(
size_t subgraphIndex,
size_t operatorIndex)
5095 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
5097 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
5100 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
5103 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
5105 auto layerName = fmt::format(
"Square:{}:{}", subgraphIndex, operatorIndex);
5106 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Mul, layerName.c_str());
5109 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 0});
5110 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
5113 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
5114 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[0]});
5116 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
5117 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
5120 void TfLiteParserImpl::ParseSquaredDifference(
size_t subgraphIndex,
size_t operatorIndex)
5122 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
5124 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
5127 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
5130 auto layerName = fmt::format(
"SquaredDifference:{}:{}", subgraphIndex, operatorIndex);
5132 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
5133 TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
5135 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::SqDiff, layerName.c_str());
5143 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
5146 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
5147 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
5149 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
5150 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
5153 void TfLiteParserImpl::ParseElementwiseUnary(
size_t subgraphIndex,
size_t operatorIndex,
UnaryOperation unaryOperation)
5155 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
5157 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
5160 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
5164 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
5168 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(desc, layerNameFormatted.c_str());
5176 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
5179 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
5180 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
5182 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
5183 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
5186 void TfLiteParserImpl::ParseEqual(
size_t subgraphIndex,
size_t operatorIndex)
5191 void TfLiteParserImpl::ParseNotEqual(
size_t subgraphIndex,
size_t operatorIndex)
5196 void TfLiteParserImpl::ParseGreater(
size_t subgraphIndex,
size_t operatorIndex)
5201 void TfLiteParserImpl::ParseGreaterOrEqual(
size_t subgraphIndex,
size_t operatorIndex)
5206 void TfLiteParserImpl::ParseLess(
size_t subgraphIndex,
size_t operatorIndex)
5211 void TfLiteParserImpl::ParseLessOrEqual(
size_t subgraphIndex,
size_t operatorIndex)
5216 void TfLiteParserImpl::ParseComparison(
size_t subgraphIndex,
size_t operatorIndex,
5219 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
5221 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
5224 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
5228 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
5230 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
5231 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
5232 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerNameFormatted,
"Input 0",
"Input 1");
5236 IConnectableLayer* layer = m_Network->AddComparisonLayer(desc, layerNameFormatted.c_str());
5244 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
5247 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
5248 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
5250 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
5251 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
5255 unsigned int outputSlot,
5256 std::string reshapeLayerName,
5263 m_Network->AddReshapeLayer(desc, reshapeLayerName.c_str());
5268 return reshapeLayer;
5272 unsigned int outputSlot,
5273 tflite::ActivationFunctionType activationType)
5276 std::string layerName = prevLayer->
GetName();
5278 switch(activationType)
5280 case tflite::ActivationFunctionType_NONE:
5285 case tflite::ActivationFunctionType_RELU:
5287 activationDesc.
m_Function = ActivationFunction::ReLu;
5288 layerName +=
":RELU";
5291 case tflite::ActivationFunctionType_RELU6:
5293 activationDesc.
m_Function = ActivationFunction::BoundedReLu;
5294 activationDesc.
m_A = 6.0f;
5295 activationDesc.
m_B = 0.0f;
5296 layerName +=
":RELU6";
5299 case tflite::ActivationFunctionType_TANH:
5301 activationDesc.
m_Function = ActivationFunction::TanH;
5302 activationDesc.
m_A = 1.0f;
5303 activationDesc.
m_B = 1.0f;
5304 layerName +=
":TANH";
5309 case tflite::ActivationFunctionType_RELU_N1_TO_1:
5310 case tflite::ActivationFunctionType_SIGN_BIT:
5314 fmt::format(
"TfLite parser doesn't support fused activation: "
5317 tflite::EnumNameActivationFunctionType(activationType),
5324 m_Network->AddActivationLayer(activationDesc, layerName.c_str());
5326 auto & prevOutputSlot = prevLayer->
GetOutputSlot(outputSlot);
5329 return activationLayer;
5333 unsigned int outputSlot)
5336 auto& prevOutputSlot = prevLayer->
GetOutputSlot(outputSlot);
5339 if (dataType == DataType::Signed32)
5344 std::string layerName = prevLayer->
GetName();
5355 if (fileName ==
nullptr)
5360 std::error_code errorCode;
5361 fs::path pathToFile(fileName);
5362 if (!fs::exists(pathToFile, errorCode))
5365 std::stringstream msg;
5366 msg <<
"Cannot find the file (" << fileName <<
") errorCode: " << errorCode
5370 if (!fs::is_regular_file(pathToFile))
5374 pathToFile.c_str()));
5377 std::ifstream file(fileName, std::ios::binary);
5378 std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
5380 fileContent.size());
5385 if (binaryContent ==
nullptr)
5390 flatbuffers::Verifier verifier(binaryContent, len);
5394 fmt::format(
"Buffer doesn't conform to the expected Tensorflow Lite "
5395 "flatbuffers format. size:{} {}",
5399 return tflite::UnPackModel(binaryContent);
5403 size_t subgraphIndex,
5404 size_t operatorIndex)
5408 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5409 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
5411 size_t inputCount = operatorPtr->inputs.size();
5413 for (
size_t i = 0; i < inputCount; ++i)
5416 if (operatorPtr->inputs[i] == -1)
5423 result.push_back(subgraphPtr->tensors[inputId].get());
5430 size_t subgraphIndex,
5431 size_t operatorIndex)
5435 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5436 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
5438 size_t outputCount = operatorPtr->outputs.size();
5440 for (
size_t i = 0; i < outputCount; ++i)
5444 result[i] = subgraphPtr->tensors[outputId].get();
5450 size_t subgraphIndex)
5453 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5455 size_t inputCount = subgraphPtr->inputs.size();
5457 for (
size_t i = 0; i < inputCount; ++i)
5461 result[i] = std::make_pair(inputId, subgraphPtr->tensors[inputId].get());
5467 size_t subgraphIndex)
5470 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5472 size_t outputCount = subgraphPtr->outputs.size();
5474 for (
size_t i = 0; i < outputCount; ++i)
5477 result[i] = std::make_pair(outputId, subgraphPtr->tensors[outputId].get());
5483 size_t subgraphIndex,
5484 size_t operatorIndex)
5487 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5488 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
5489 return operatorPtr->inputs;
5493 size_t subgraphIndex,
5494 size_t operatorIndex)
5497 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5498 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
5499 return operatorPtr->outputs;
5502 void TfLiteParserImpl::RegisterInputSlots(
size_t subgraphIndex,
5503 size_t operatorIndex,
5505 const std::vector<unsigned int>& tensorIndexes,
5506 unsigned int startingSlotIndex)
5508 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
5519 fmt::format(
"The number of tensor inputs ({}) does not match the number expected ({})"
5520 " for subgraph:{} operator index:{} {}",
5521 tensorIndexes.size(),
5528 for (
unsigned int index = 0; index < tensorIndexes.size() ; ++index)
5530 unsigned int tensorIndex = tensorIndexes[index];
5532 RegisterConsumerOfTensor(subgraphIndex, tensorIndex, slot);
5536 void TfLiteParserImpl::RegisterOutputSlots(
size_t subgraphIndex,
5537 size_t operatorIndex,
5539 const std::vector<unsigned int>& tensorIndexes)
5541 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
5552 fmt::format(
"The number of tensor outputs ({}) does not match the number expected ({})"
5553 " for subgraph:{} operator index:{} {}",
5554 tensorIndexes.size(),
5561 for (
unsigned int slotIndex = 0; slotIndex < layer->
GetNumOutputSlots(); ++slotIndex)
5563 unsigned int tensorIndex = tensorIndexes[slotIndex];
5565 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
5569 void TfLiteParserImpl::SetupInputLayerTensorInfos(
size_t subgraphIndex)
5574 for (
auto const& tensorIdAndPtr : inputs)
5577 m_TensorInfos.insert({tensorIdAndPtr.first, tensorInfo});
5581 void TfLiteParserImpl::SetupInputLayers(
size_t subgraphIndex)
5586 for (
auto const& tensorIdAndPtr : inputs)
5588 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
5590 m_Network->AddInputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
5595 RegisterOutputSlots(subgraphIndex,
5596 VIRTUAL_OPERATOR_ID,
5598 {
static_cast<uint32_t
>(tensorIdAndPtr.first) });
5602 void TfLiteParserImpl::SetupOutputLayers(
size_t subgraphIndex)
5607 for (
auto const& tensorIdAndPtr : outputs)
5609 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
5611 m_Network->AddOutputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
5613 RegisterInputSlots(subgraphIndex,
5614 VIRTUAL_OPERATOR_ID,
5616 {
static_cast<uint32_t
>(tensorIdAndPtr.first) });
5620 void TfLiteParserImpl::SetupConstantLayerTensorInfos(
size_t subgraph)
5624 const auto & subgraphPtr = m_Model->subgraphs[subgraph];
5625 for (
unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
5627 for (
unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
5629 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot ==
nullptr &&
5630 m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0)
5632 TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get();
5636 m_TensorInfos.insert({tensorIndex, tensorInfo});
5642 void TfLiteParserImpl::SetupConstantLayers(
size_t subgraph)
5646 const auto & subgraphPtr = m_Model->subgraphs[subgraph];
5647 for (
unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
5649 for (
unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
5651 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot ==
nullptr &&
5652 m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0)
5654 TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get();
5656 if (IsConstTensor(tensorPtr))
5661 if (std::find(m_ConstantsToDequantize.begin(), m_ConstantsToDequantize.end(), tensorPtr->buffer)
5662 != m_ConstantsToDequantize.end())
5664 dataType = DataType::Float32;
5666 auto tensorAndData = CreateConstTensorNonPermuted(tensorPtr, tensorInfo, dataType);
5668 std::string layerName = fmt::format(
"Constant:{}", tensorPtr->name);
5669 IConnectableLayer *layer = m_Network->AddConstantLayer(tensorAndData.first, layerName.c_str());
5672 RegisterOutputSlots(subgraphIndex,
5673 VIRTUAL_OPERATOR_ID,
5677 else if (ShouldConstantTensorBeCreated(tensorIndex))
5682 if (std::find(m_ConstantsToDequantize.begin(), m_ConstantsToDequantize.end(), tensorPtr->buffer)
5683 != m_ConstantsToDequantize.end())
5685 dataType = DataType::Float32;
5693 std::string layerName = fmt::format(
"Constant:{}", tensorPtr->name);
5694 IConnectableLayer* layer = m_Network->AddConstantLayer(tensorAndData, layerName.c_str());
5697 RegisterOutputSlots(subgraphIndex,
5698 VIRTUAL_OPERATOR_ID,
5705 fmt::format(
"Invalid Tensor: Tensor should be constant. {}",
5717 return model->buffers[bufferIndex].get();
5720 template<
typename T>
5721 std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
5730 auto constData = CreateConstTensorImpl<T>(bufferPtr,
5734 TfLiteParserImpl::SupportedDataStorage storage(std::move(constData.second));
5735 return std::make_pair(constData.first, std::move(storage));
5738 bool TfLiteParserImpl::ShouldConstantTensorBeCreated(
unsigned int tensorIndex)
5741 return (std::find(m_ConstantsToBeCreated.begin(), m_ConstantsToBeCreated.end(), tensorIndex)
5742 != m_ConstantsToBeCreated.end());
5745 bool TfLiteParserImpl::IsConstTensor(
TensorRawPtr tensorPtr)
5748 bool isConst =
true;
5750 auto buffer =
GetBuffer(m_Model, tensorPtr->buffer);
5751 if (buffer->data.size() == 0)
5759 std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
5760 TfLiteParserImpl::CreateConstTensorPermuted(
TensorRawPtr tensorPtr,
5765 auto bufferPtr =
GetBuffer(m_Model, tensorPtr->buffer);
5774 return CreateConstTensorAndStoreData<float>(bufferPtr,
5779 return CreateConstTensorAndStoreData<uint8_t>(bufferPtr,
5784 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
5789 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
5794 return CreateConstTensorAndStoreData<int32_t>(bufferPtr,
5800 std::stringstream errString;
5801 errString <<
"Unexpected datatype when creating const tensor: "
5803 <<
" shape:" << tensorInfo.
GetShape()
5814 auto bufferPtr =
GetBuffer(m_Model, tensorPtr->buffer);
5820 return ConstTensor(tensorInfo, bufferPtr->data.data());
5823 std::pair<armnn::ConstTensor, std::unique_ptr<float[]>>
5824 TfLiteParserImpl::CreateConstTensorNonPermuted(
TensorRawPtr tensorPtr,
5829 auto bufferPtr =
GetBuffer(m_Model, tensorPtr->buffer);
5835 if (inputDataType == DataType::Float32 && tensorInfo.
GetDataType() != DataType::Float32)
5841 return std::make_pair(
ConstTensor(constTensorInfo, data.get()), std::move(data));
5846 fmt::format(
"Unsupported input/weights combination: Input {} not supported with Weights {}",
5854 return std::make_pair(
ConstTensor(tensorInfo, bufferPtr->data.data()), std::unique_ptr<
float[]>());
5858 std::pair<armnn::ConstTensor*, std::unique_ptr<float[]>>
5863 auto bufferPtr =
GetBuffer(m_Model, tensorPtr->buffer);
5875 return std::make_pair(
new ConstTensor(constTensorInfo, data.get()), std::move(data));
5880 fmt::format(
"Unsupported input/weights combination: Input {} not supported with Weights {}",
5888 return std::make_pair(
new ConstTensor(tensorInfo, bufferPtr->data.data()), std::unique_ptr<
float[]>());
5893 const std::string& name)
const
5897 for (
auto const& input : inputs)
5899 if (input.second->name == name)
5901 auto bindingId = GenerateLayerBindingId(subgraphId, input.first);
5905 return std::make_pair(bindingId, inputTensorInfo);
5909 std::stringstream bindings;
5910 for (
auto const& input : inputs)
5912 bindings <<
"'" << input.second->name <<
"' ";
5916 fmt::format(
"No input binding found for subgraph:{} and name:{}. "
5917 "Possible inputs are: [{}] {}",
5925 const std::string& name)
const
5929 for (
unsigned int i = 0; i < outputs.size(); ++i)
5931 auto const output = outputs[i];
5932 if (output.second->name == name)
5934 auto bindingId = GenerateLayerBindingId(subgraphId, output.first);
5935 std::vector<unsigned int> shape = m_OverriddenOutputShapes.size() > 0 ?
5936 m_OverriddenOutputShapes[i] : AsUnsignedVector(output.second->shape);
5937 return std::make_pair(bindingId,
ToTensorInfo(output.second, shape));
5941 std::stringstream bindings;
5942 for (
auto const& output : outputs)
5944 bindings <<
"'" << output.second->name <<
"' ";
5948 fmt::format(
"No output binding found for subgraph:{} and name:{}. "
5949 "Possible outputs are: [{}] {}",
5958 return m_Model->subgraphs.size();
5965 std::vector<std::string> result;
5966 result.reserve(inputs.size());
5967 for (
auto const& input : inputs)
5969 result.push_back(input.second->name);
5978 std::vector<std::string> result;
5979 result.reserve(outputs.size());
5980 for (
auto const& output : outputs)
5982 result.push_back(output.second->name);
5992 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<
float[]>&& data)
5993 : m_FloatData(
std::move(data))
5994 , m_Uint8Data(nullptr)
5995 , m_Int8Data(nullptr)
5996 , m_Int32Data(nullptr)
6000 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<uint8_t[]>&& data)
6001 : m_FloatData(nullptr)
6002 , m_Uint8Data(
std::move(data))
6003 , m_Int8Data(nullptr)
6004 , m_Int32Data(nullptr)
6008 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int8_t[]>&& data)
6009 : m_FloatData(nullptr)
6010 , m_Uint8Data(nullptr)
6011 , m_Int8Data(
std::move(data))
6012 , m_Int32Data(nullptr)
6016 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int32_t[]>&& data)
6017 : m_FloatData(nullptr)
6018 , m_Uint8Data(nullptr)
6019 , m_Int8Data(nullptr)
6020 , m_Int32Data(
std::move(data))