29 #include <schema_generated.h> 31 #include <flatbuffers/flexbuffers.h> 33 #include <fmt/format.h> 42 #define ARMNN_THROW_PARSE_EXCEPTION(msg) \ 44 throw armnn::ParseException( static_cast<const std::stringstream&>( std::stringstream() << msg \ 46 << CHECK_LOCATION().AsString()).str()); \ 49 using namespace armnn;
55 pTfLiteParserImpl(
new TfLiteParserImpl(options)) {}
57 ITfLiteParser::~ITfLiteParser() =
default;
79 armnn::INetworkPtr ITfLiteParser::CreateNetworkFromBinary(
const std::vector<uint8_t> & binaryContent)
81 return pTfLiteParserImpl->CreateNetworkFromBinary(binaryContent);
85 const std::string& name)
const 87 return pTfLiteParserImpl->GetNetworkInputBindingInfo(subgraphId, name);
91 const std::string& name)
const 93 return pTfLiteParserImpl->GetNetworkOutputBindingInfo(subgraphId, name);
96 size_t ITfLiteParser::GetSubgraphCount()
const 98 return pTfLiteParserImpl->GetSubgraphCount();
101 std::vector<std::string> ITfLiteParser::GetSubgraphInputTensorNames(
size_t subgraphId)
const 103 return pTfLiteParserImpl->GetSubgraphInputTensorNames(subgraphId);
106 std::vector<std::string> ITfLiteParser::GetSubgraphOutputTensorNames(
size_t subgraphId)
const 108 return pTfLiteParserImpl->GetSubgraphOutputTensorNames(subgraphId);
114 const uint32_t VIRTUAL_OPERATOR_ID = std::numeric_limits<uint32_t>::max();
117 size_t subgraphIndex,
120 if (model.get() ==
nullptr)
123 fmt::format(
"{} was called with invalid (null) model. " 124 "Possible reason is that the model is not yet loaded and Unpack(ed). " 130 else if (subgraphIndex >= model->subgraphs.size())
133 fmt::format(
"{} was called with an invalid subgraph index. " 141 #define CHECK_SUBGRAPH(MODEL, SUBGRAPH_INDEX) \ 142 CheckSubgraph(MODEL, SUBGRAPH_INDEX, CHECK_LOCATION()) 145 size_t subgraphIndex,
146 size_t operatorIndex,
149 if (model.get() ==
nullptr)
152 fmt::format(
"{} was called with invalid (null) model. " 153 "Possible reason is that the model is not yet loaded and Unpack(ed). " 154 "subgraph:{} operator:{} at {}",
160 else if (subgraphIndex >= model->subgraphs.size())
163 fmt::format(
"{} was called with an invalid subgraph index. " 164 "subgraph:{} operator:{} at {}",
170 else if (operatorIndex >= model->subgraphs[subgraphIndex]->operators.size() &&
171 operatorIndex != VIRTUAL_OPERATOR_ID)
174 fmt::format(
"{} was called with an invalid operator index. " 175 "subgraph:{} operator:{} at {}",
183 #define CHECK_MODEL(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX) \ 184 CheckModel(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX, CHECK_LOCATION()) 187 size_t subgraphIndex,
193 ARMNN_ASSERT_MSG(model.get() !=
nullptr,
"Expecting a valid model in this function");
197 ARMNN_ASSERT_MSG(subgraphIndex < model->subgraphs.size(),
"Expecting a valid subgraph index");
200 if (tensorIndex >= model->subgraphs[subgraphIndex]->tensors.size())
203 fmt::format(
"{} was called with an invalid tensor index. " 204 "subgraph:{} tensor:{} at {}",
212 #define CHECK_TENSOR(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX) \ 213 CheckTensor(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX, CHECK_LOCATION()) 218 if (rawPtr ==
nullptr)
221 fmt::format(
"{} was called with a null tensor pointer at {}", location.
m_Function, location.
FileLine()));
225 #define CHECK_TENSOR_PTR(TENSOR_PTR) \ 226 CheckTensorPtr(TENSOR_PTR, CHECK_LOCATION()) 232 if (model.get() ==
nullptr)
235 fmt::format(
"{} was called with invalid (null) model. " 236 "Possible reason is that the model is not yet loaded and Unpack(ed). " 242 else if (bufferIndex >= model->buffers.size())
245 fmt::format(
"{} was called with an invalid buffer index. " 246 "buffer index:{} at {}",
251 else if (model->buffers[bufferIndex].get() ==
nullptr)
254 fmt::format(
"The buffer #{} is null. {}",
260 #define CHECK_BUFFER(MODEL, BUFFER_INDEX) \ 261 CheckBuffer(MODEL, BUFFER_INDEX, CHECK_LOCATION()) 263 void CheckBufferSize(TfLiteParserImpl::BufferRawPtr bufferPtr,
268 if (bufferPtr ==
nullptr)
271 fmt::format(
"BufferPtr is null for buffer:{}. {}",
278 std::stringstream ss;
279 ss <<
"Buffer #" << bufferId <<
" has " << bufferPtr->data.size() <<
" bytes. " 280 <<
"For tensor: " << tensorInfo.
GetShape()
281 <<
" expecting: " << tensorInfo.
GetNumBytes() <<
" bytes and " 287 #define CHECK_BUFFER_SIZE(BUFFER_PTR, TENSOR_INFO, BUFFER_ID) \ 288 CheckBufferSize(BUFFER_PTR, TENSOR_INFO, BUFFER_ID, CHECK_LOCATION()) 292 switch(activationType)
294 case tflite::ActivationFunctionType_NONE:
295 case tflite::ActivationFunctionType_RELU:
296 case tflite::ActivationFunctionType_RELU6:
297 case tflite::ActivationFunctionType_TANH:
308 #define CHECK_SUPPORTED_FUSED_ACTIVATION(OPTION, SUBGRAPH_INDEX, OPERATOR_INDEX) \ 310 if (IsActivationSupported(OPTION->fused_activation_function) == false) \ 312 throw ParseException( \ 313 fmt::format("TfLite parser doesn't suppport fused activation: " \ 314 "{}/{} in {} subgraph:{} operator:{} at {}", \ 315 OPTION->fused_activation_function, \ 316 tflite::EnumNameActivationFunctionType(\ 317 OPTION->fused_activation_function), \ 321 CHECK_LOCATION().FileLine())); \ 326 std::vector<unsigned int> AsUnsignedVector(
const std::vector<int32_t> & in)
328 std::vector<unsigned int> result;
329 result.reserve(in.size());
341 uint32_t& paddingFront,
342 uint32_t& paddingBack,
343 tflite::Padding padding)
347 if (padding == tflite::Padding_SAME)
349 uint32_t outputSize = (inputSize + stride - 1) / stride;
350 uint32_t dilatedSize = filterSize + (dilation - 1) * (filterSize - 1);
351 uint32_t temp = (outputSize - 1) * stride + dilatedSize;
352 if (temp > inputSize)
354 paddingFront = (temp - inputSize) / 2;
355 paddingBack = (temp - inputSize) - paddingFront;
361 const std::vector<unsigned int>& shapes,
363 const bool outputTensor =
false)
368 switch (tensorPtr->type)
370 case tflite::TensorType_UINT8:
373 case tflite::TensorType_FLOAT32:
376 case tflite::TensorType_INT8:
377 if (tensorPtr->quantization->zero_point.size() == 1)
388 case tflite::TensorType_INT16:
391 case tflite::TensorType_INT32:
394 case tflite::TensorType_INT64:
401 fmt::format(
"Unsupported data type {} = {} for tensor: {}. {}",
403 tflite::EnumNameTensorType(tensorPtr->type),
408 std::vector<unsigned int> safeShape = shapes;
409 bool isDynamic =
false;
410 if (safeShape.size() == 0)
412 safeShape.push_back(1);
419 float quantizationScale = 0.0f;
420 int32_t quantizationOffset = 0;
422 if (tensorPtr->quantization.get())
424 if (tensorPtr->quantization->scale.size() <= 1)
429 if (tensorPtr->quantization->scale.size() == 1)
431 quantizationScale = tensorPtr->quantization->scale[0];
433 if (tensorPtr->quantization->zero_point.size() == 1)
440 TensorShape tensorShape(armnn::numeric_cast<unsigned int>(safeShape.size()),
454 std::vector<float> quantizationScales;
455 std::vector<int32_t> quantizationOffsets;
458 std::copy(tensorPtr->quantization->scale.begin(),
459 tensorPtr->quantization->scale.end(),
460 std::back_inserter(quantizationScales));
463 TensorShape tensorShape(armnn::numeric_cast<unsigned int>(safeShape.size()),
472 dimensionMappings[armnn::numeric_cast<unsigned int>(
473 tensorPtr->quantization->quantized_dimension)]);
479 TensorShape tensorShape(armnn::numeric_cast<unsigned int>(safeShape.size()),
496 auto const & dimensions = AsUnsignedVector(tensorPtr->shape);
497 return ToTensorInfo(tensorPtr, dimensions, dimensionMappings);
501 const bool outputTensor)
503 auto const & dimensions = AsUnsignedVector(tensorPtr->shape);
505 return ToTensorInfo(tensorPtr, dimensions, dimensionMappings, outputTensor);
509 std::pair<armnn::ConstTensor, std::unique_ptr<T[]>>
510 CreateConstTensorImpl(TfLiteParserImpl::BufferRawPtr bufferPtr,
518 fmt::format(
"Buffer for buffer:{} is null", tensorPtr->buffer).c_str());
526 reinterpret_cast<const T*
>(bufferPtr->data.data()), data.get(),
sizeof(T));
530 ::memcpy(data.get(), bufferPtr->data.data(), tensorInfo.
GetNumBytes());
533 return std::make_pair(
ConstTensor(tensorInfo, data.get()), std::move(data));
546 if (actualSize != expected.size())
551 for (
unsigned int i = 0u; i < actualSize; i++)
553 if (expected[i] < 0 ||
554 actual[i] != static_cast<unsigned int>(expected[i]))
563 void CheckMatchingQuantization(
const TensorInfo& first,
565 const std::string& descName,
566 std::string
const& firstName,
567 std::string
const& secondName)
579 if (firstDataType != secondDataType)
582 " must be of the same quantized type, " +
590 " must have the same quantization space, " +
602 , m_Network(nullptr, nullptr)
606 m_ParserFunctions[tflite::BuiltinOperator_ADD] = &TfLiteParserImpl::ParseAdd;
607 m_ParserFunctions[tflite::BuiltinOperator_AVERAGE_POOL_2D] = &TfLiteParserImpl::ParseAveragePool2D;
608 m_ParserFunctions[tflite::BuiltinOperator_BATCH_TO_SPACE_ND] = &TfLiteParserImpl::ParseBatchToSpaceND;
609 m_ParserFunctions[tflite::BuiltinOperator_CONCATENATION] = &TfLiteParserImpl::ParseConcatenation;
610 m_ParserFunctions[tflite::BuiltinOperator_CONV_2D] = &TfLiteParserImpl::ParseConv2D;
611 m_ParserFunctions[tflite::BuiltinOperator_CUSTOM] = &TfLiteParserImpl::ParseCustomOperator;
612 m_ParserFunctions[tflite::BuiltinOperator_DEPTH_TO_SPACE] = &TfLiteParserImpl::ParseDepthToSpace;
613 m_ParserFunctions[tflite::BuiltinOperator_DEPTHWISE_CONV_2D] = &TfLiteParserImpl::ParseDepthwiseConv2D;
614 m_ParserFunctions[tflite::BuiltinOperator_DEQUANTIZE] = &TfLiteParserImpl::ParseDequantize;
615 m_ParserFunctions[tflite::BuiltinOperator_ELU] = &TfLiteParserImpl::ParseElu;
616 m_ParserFunctions[tflite::BuiltinOperator_EXP] = &TfLiteParserImpl::ParseExp;
617 m_ParserFunctions[tflite::BuiltinOperator_FULLY_CONNECTED] = &TfLiteParserImpl::ParseFullyConnected;
618 m_ParserFunctions[tflite::BuiltinOperator_GATHER] = &TfLiteParserImpl::ParseGather;
619 m_ParserFunctions[tflite::BuiltinOperator_HARD_SWISH] = &TfLiteParserImpl::ParseHardSwish;
620 m_ParserFunctions[tflite::BuiltinOperator_LEAKY_RELU] = &TfLiteParserImpl::ParseLeakyRelu;
621 m_ParserFunctions[tflite::BuiltinOperator_LOGISTIC] = &TfLiteParserImpl::ParseLogistic;
622 m_ParserFunctions[tflite::BuiltinOperator_L2_NORMALIZATION] = &TfLiteParserImpl::ParseL2Normalization;
623 m_ParserFunctions[tflite::BuiltinOperator_MAX_POOL_2D] = &TfLiteParserImpl::ParseMaxPool2D;
624 m_ParserFunctions[tflite::BuiltinOperator_MAXIMUM] = &TfLiteParserImpl::ParseMaximum;
625 m_ParserFunctions[tflite::BuiltinOperator_MEAN] = &TfLiteParserImpl::ParseMean;
626 m_ParserFunctions[tflite::BuiltinOperator_MINIMUM] = &TfLiteParserImpl::ParseMinimum;
627 m_ParserFunctions[tflite::BuiltinOperator_MUL] = &TfLiteParserImpl::ParseMul;
628 m_ParserFunctions[tflite::BuiltinOperator_NEG] = &TfLiteParserImpl::ParseNeg;
629 m_ParserFunctions[tflite::BuiltinOperator_PACK] = &TfLiteParserImpl::ParsePack;
630 m_ParserFunctions[tflite::BuiltinOperator_PAD] = &TfLiteParserImpl::ParsePad;
631 m_ParserFunctions[tflite::BuiltinOperator_QUANTIZE] = &TfLiteParserImpl::ParseQuantize;
632 m_ParserFunctions[tflite::BuiltinOperator_RELU] = &TfLiteParserImpl::ParseRelu;
633 m_ParserFunctions[tflite::BuiltinOperator_RELU6] = &TfLiteParserImpl::ParseRelu6;
634 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MAX] = &TfLiteParserImpl::ParseReduceMax;
635 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MIN] = &TfLiteParserImpl::ParseReduceMin;
636 m_ParserFunctions[tflite::BuiltinOperator_RESHAPE] = &TfLiteParserImpl::ParseReshape;
637 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_BILINEAR] = &TfLiteParserImpl::ParseResizeBilinear;
638 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_NEAREST_NEIGHBOR] = &TfLiteParserImpl::ParseResizeNearestNeighbor;
639 m_ParserFunctions[tflite::BuiltinOperator_SLICE] = &TfLiteParserImpl::ParseSlice;
640 m_ParserFunctions[tflite::BuiltinOperator_SOFTMAX] = &TfLiteParserImpl::ParseSoftmax;
641 m_ParserFunctions[tflite::BuiltinOperator_SPACE_TO_BATCH_ND] = &TfLiteParserImpl::ParseSpaceToBatchND;
642 m_ParserFunctions[tflite::BuiltinOperator_SPLIT] = &TfLiteParserImpl::ParseSplit;
643 m_ParserFunctions[tflite::BuiltinOperator_SPLIT_V] = &TfLiteParserImpl::ParseSplitV;
644 m_ParserFunctions[tflite::BuiltinOperator_SQUEEZE] = &TfLiteParserImpl::ParseSqueeze;
645 m_ParserFunctions[tflite::BuiltinOperator_STRIDED_SLICE] = &TfLiteParserImpl::ParseStridedSlice;
646 m_ParserFunctions[tflite::BuiltinOperator_SUB] = &TfLiteParserImpl::ParseSub;
647 m_ParserFunctions[tflite::BuiltinOperator_SUM] = &TfLiteParserImpl::ParseSum;
648 m_ParserFunctions[tflite::BuiltinOperator_TANH] = &TfLiteParserImpl::ParseTanH;
649 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE] = &TfLiteParserImpl::ParseTranspose;
650 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE_CONV] = &TfLiteParserImpl::ParseTransposeConv;
651 m_ParserFunctions[tflite::BuiltinOperator_UNPACK] = &TfLiteParserImpl::ParseUnpack;
652 m_ParserFunctions[tflite::BuiltinOperator_DIV] = &TfLiteParserImpl::ParseDiv;
653 m_ParserFunctions[tflite::BuiltinOperator_ARG_MAX] = &TfLiteParserImpl::ParseArgMax;
655 m_CustomParserFunctions[
"TFLite_Detection_PostProcess"] = &TfLiteParserImpl::ParseDetectionPostProcess;
658 void TfLiteParserImpl::ResetParser()
662 m_SubgraphConnections.clear();
669 return CreateNetworkFromModel();
676 return CreateNetworkFromModel();
679 INetworkPtr TfLiteParserImpl::CreateNetworkFromModel()
684 if (m_Options && m_Options.value().m_InferAndValidate)
688 {
"InferAndValidate",
true }
691 networkOptions.push_back(shapeInferenceMethodOption);
694 m_Network = INetwork::Create(networkOptions);
697 if (m_Model->subgraphs.size() != 1)
700 fmt::format(
"Current TfLite parser only supports 1 subgraph. Current one has: {} {}",
701 m_Model->subgraphs.size(),
705 size_t subgraphIndex = 0;
706 size_t operatorIndex = 0;
709 for (
SubgraphPtr const& subgraph : m_Model->subgraphs)
711 m_SubgraphConnections.emplace_back(subgraph->tensors.size());
714 auto const& opCodePtr = m_Model->operator_codes[op->opcode_index];
715 auto builtinCode = opCodePtr->builtin_code;
717 if (builtinCode > tflite::BuiltinOperator_MAX)
719 throw ParseException(fmt::format(
"Operator code {} is out of range 0-{}. " 720 "subgraph:{} operator idx:{}. {}",
721 builtinCode, tflite::BuiltinOperator_MAX, subgraphIndex,
726 auto& parserFunction = m_ParserFunctions[builtinCode];
727 (this->*parserFunction)(subgraphIndex, operatorIndex);
731 SetupInputLayers(subgraphIndex);
732 SetupOutputLayers(subgraphIndex);
733 SetupConstantLayers(subgraphIndex);
741 std::stringstream errorString;
742 errorString <<
"Failed to parse operator #" << operatorIndex <<
" within subgraph #" 743 << subgraphIndex <<
" error: " << e.
what();
745 std::stringstream errors;
746 errors << errorString.str() <<
"\n";
751 for (subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
753 for (
size_t tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
755 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot !=
nullptr)
757 for (
size_t inputSlotIdx = 0;
758 inputSlotIdx < m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size();
761 m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot->Connect(
762 *(m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots[inputSlotIdx]));
768 return std::move(m_Network);
771 void TfLiteParserImpl::RegisterProducerOfTensor(
size_t subgraphIndex,
776 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
777 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
779 TensorSlots & tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
782 if (tensorSlots.outputSlot !=
nullptr)
784 throw ParseException(fmt::format(
"Another layer has already registered itself as the producer of " 785 "subgraph:{} tensor:{} {}",
791 tensorSlots.outputSlot = slot;
794 void TfLiteParserImpl::RegisterConsumerOfTensor(
size_t subgraphIndex,
799 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
800 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
802 TensorSlots & tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
803 tensorSlots.inputSlots.push_back(slot);
806 void TfLiteParserImpl::ParseCustomOperator(
size_t subgraphIndex,
size_t operatorIndex)
808 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
811 auto customParserFunction = &TfLiteParserImpl::ParseUnsupportedOperator;
814 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
815 const auto& customCode = m_Model->operator_codes[operatorPtr->opcode_index]->custom_code;
818 auto iterator = m_CustomParserFunctions.find(customCode);
819 if (iterator != m_CustomParserFunctions.end())
821 customParserFunction = iterator->second;
825 (this->*customParserFunction)(subgraphIndex, operatorIndex);
828 void TfLiteParserImpl::ParseUnsupportedOperator(
size_t subgraphIndex,
size_t operatorIndex)
830 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
832 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
834 auto opcodeIndex = operatorPtr->opcode_index;
835 auto opcode = m_Model->operator_codes[opcodeIndex]->builtin_code;
837 if (!m_Options || !m_Options.value().m_StandInLayerForUnsupported)
841 fmt::format(
"Operator not supported. " 842 "subgraph:{} operator:{} " 843 "opcode_index:{} opcode:{} / {} {}",
848 tflite::EnumNameBuiltinOperator(opcode),
852 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
853 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
859 auto layerName = fmt::format(
"StandIn:{}:{}:{}", subgraphIndex, operatorIndex, opcode);
862 IConnectableLayer* layer = m_Network->AddStandInLayer(descriptor, layerName.c_str());
865 for (
unsigned int i = 0u; i < numOutputs; ++i)
870 auto inputTensorIds = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
871 auto outputTensorIds = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
873 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIds);
874 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIds);
877 void TfLiteParserImpl::ParseConv2D(
size_t subgraphIndex,
size_t operatorIndex)
879 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
881 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
882 const auto * options = operatorPtr->builtin_options.AsConv2DOptions();
894 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
897 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
904 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
905 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
909 unsigned int filterHeight = filterTensorInfo.
GetShape()[1];
910 unsigned int filterWidth = filterTensorInfo.
GetShape()[2];
917 auto filterTensorAndData = CreateConstTensor(inputs[1],
922 auto layerName = fmt::format(
"Conv2D:{}:{}", subgraphIndex, operatorIndex);
924 if (inputs.size() == 3)
928 auto biasTensorAndData = CreateConstTensor(inputs[2],
931 layer = m_Network->AddConvolution2dLayer(desc,
932 filterTensorAndData.first,
938 layer = m_Network->AddConvolution2dLayer(desc,
939 filterTensorAndData.first,
951 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
952 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
954 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
956 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
957 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
960 void TfLiteParserImpl::ParseDepthwiseConv2D(
size_t subgraphIndex,
size_t operatorIndex)
962 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
964 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
965 const auto * options = operatorPtr->builtin_options.AsDepthwiseConv2DOptions();
976 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
978 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
990 unsigned int inputHeight = inputTensorInfo.
GetShape()[1];
991 unsigned int inputWidth = inputTensorInfo.
GetShape()[2];
994 unsigned int filterHeight = filterTensorInfo.
GetShape()[1];
995 unsigned int filterWidth = filterTensorInfo.
GetShape()[2];
998 filterTensorInfo.
SetShape({ filterHeight,
1008 auto filterTensorAndData = CreateConstTensor(inputs[1], filterTensorInfo, permutationVector);
1010 auto layerName = fmt::format(
"DepthwiseConv2D:{}:{}", subgraphIndex, operatorIndex);
1012 if (inputs.size() == 3)
1016 auto biasTensorAndData = CreateConstTensor(inputs[2],
1019 layer = m_Network->AddDepthwiseConvolution2dLayer(desc,
1020 filterTensorAndData.first,
1026 layer = m_Network->AddDepthwiseConvolution2dLayer(desc,
1027 filterTensorAndData.first,
1038 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1039 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1041 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1043 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1044 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1047 void TfLiteParserImpl::ParseDequantize(
size_t subgraphIndex,
size_t operatorIndex)
1049 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1051 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1054 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1057 auto layerName = fmt::format(
"Dequantize:{}:{}", subgraphIndex, operatorIndex);
1065 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1066 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1068 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1069 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1072 void TfLiteParserImpl::ParseExp(
size_t subgraphIndex,
size_t operatorIndex)
1074 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1076 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1079 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1082 auto layerName = fmt::format(
"Exp:{}:{}", subgraphIndex, operatorIndex);
1086 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(desc, layerName.c_str());
1092 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1093 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1095 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1096 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1099 void TfLiteParserImpl::ParseTranspose(
size_t subgraphIndex,
size_t operatorIndex)
1101 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1103 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1106 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1109 auto layerName = fmt::format(
"Transpose:{}:{}", subgraphIndex, operatorIndex);
1112 if (inputs.size() == 2)
1117 std::vector<unsigned int> permuteShape(numPermVecElements);
1118 ::memcpy(permuteShape.data(), permuteBufferPtr->data.data(), permuteTensorInfo.
GetNumBytes());
1126 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1128 IConnectableLayer* layer = m_Network->AddTransposeLayer(desc, layerName.c_str());
1132 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1133 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1135 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1136 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1139 void TfLiteParserImpl::ParseTransposeConv(
size_t subgraphIndex,
size_t operatorIndex)
1141 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1143 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1144 const auto * options = operatorPtr->builtin_options.AsTransposeConvOptions();
1152 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1153 if (inputs.size() == 4)
1162 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1169 if (tensorInfo.
GetDataType() == DataType::Signed32)
1171 ::memcpy(output_shape.data(),
GetBuffer(m_Model, inputs[0]->buffer)->data.data(), tensorInfo.
GetNumBytes());
1173 if (tensorInfo.
GetDataType() == DataType::QAsymmU8)
1177 output_shape[i] =
GetBuffer(m_Model, inputs[0]->buffer)->data.data()[i];
1181 for (
int dimension : output_shape)
1183 desc.
m_OutputShape.push_back(static_cast<unsigned int>(dimension));
1191 const unsigned int inputHeight = inputTensorInfo.
GetShape()[1];
1192 const unsigned int inputWidth = inputTensorInfo.
GetShape()[2];
1194 const unsigned int filterHeight = filterTensorInfo.
GetShape()[1];
1195 const unsigned int filterWidth = filterTensorInfo.
GetShape()[2];
1213 auto filterTensorAndData = CreateConstTensor(inputs[1],
1218 auto layerName = fmt::format(
"TransposeConv:{}:{}", subgraphIndex, operatorIndex);
1223 auto biasConstTensor = CreateConstTensor(inputs[3],
1226 layer = m_Network->AddTransposeConvolution2dLayer(desc,
1227 filterTensorAndData.first,
1228 biasConstTensor.first,
1233 layer = m_Network->AddTransposeConvolution2dLayer(desc,
1234 filterTensorAndData.first,
1245 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1246 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[2]});
1248 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1249 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1252 void TfLiteParserImpl::ParseAveragePool2D(
size_t subgraphIndex,
size_t operatorIndex)
1254 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Average);
1257 void TfLiteParserImpl::ParseBatchToSpaceND(
size_t subgraphIndex,
size_t operatorIndex)
1259 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1261 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1264 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1273 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1274 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1276 std::vector<unsigned int> cropsVector(cropsTensorInfo.
GetNumElements());
1277 ::memcpy(cropsVector.data(), cropsBufferPtr->data.data(), cropsTensorInfo.
GetNumBytes());
1280 std::vector<std::pair<unsigned int, unsigned int>> crops;
1281 for (
unsigned int i = 0; i < cropsTensorInfo.
GetNumElements() / step; ++i)
1283 crops.emplace_back(cropsVector[i * step], cropsVector[i * step + 1]);
1291 auto layerName = fmt::format(
"BatchToSpaceND:{}:{}", subgraphIndex, operatorIndex);
1295 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1297 IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(desc, layerName.c_str());
1301 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1302 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1304 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1305 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1308 void TfLiteParserImpl::ParseL2Normalization(
size_t subgraphIndex,
size_t operatorIndex)
1310 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1312 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1315 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1320 auto layerName = fmt::format(
"L2Normalization:{}:{}", subgraphIndex, operatorIndex);
1321 IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(desc, layerName.c_str());
1328 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1329 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1331 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1332 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1335 void TfLiteParserImpl::ParseMaxPool2D(
size_t subgraphIndex,
size_t operatorIndex)
1337 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Max);
1340 void TfLiteParserImpl::ParseMaximum(
size_t subgraphIndex,
size_t operatorIndex)
1342 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1344 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1347 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1350 auto layerName = fmt::format(
"Maximum:{}:{}", subgraphIndex, operatorIndex);
1354 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName,
"Input 0",
"Input 1");
1357 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1363 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1364 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1366 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1367 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1370 void TfLiteParserImpl::ParseMinimum(
size_t subgraphIndex,
size_t operatorIndex)
1372 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1374 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1377 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1380 auto layerName = fmt::format(
"Minimum:{}:{}", subgraphIndex, operatorIndex);
1384 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName,
"Input 0",
"Input 1");
1387 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1393 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1394 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1396 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1397 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1400 void TfLiteParserImpl::ParsePool(
size_t subgraphIndex,
1401 size_t operatorIndex,
1404 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1406 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1407 const auto * options = operatorPtr->builtin_options.AsPool2DOptions();
1411 std::string layerName;
1415 case PoolingAlgorithm::Average:
1417 fmt::format(
"AveragePool2D:{}:{}", subgraphIndex, operatorIndex);
1419 case PoolingAlgorithm::Max:
1421 fmt::format(
"MaxPool2D:{}:{}", subgraphIndex, operatorIndex);
1438 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1443 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1444 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1451 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1455 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1457 IConnectableLayer* layer = m_Network->AddPooling2dLayer(desc, layerName.c_str());
1463 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1464 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1466 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1468 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1469 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1472 void TfLiteParserImpl::ParseSlice(
size_t subgraphIndex,
size_t operatorIndex)
1474 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1476 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1478 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1487 std::vector<unsigned int> begin(beginTensorInfo.
GetNumElements());
1488 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.
GetNumBytes());
1494 std::vector<unsigned int> size(sizeTensorInfo.GetNumElements());
1495 ::memcpy(size.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
1498 auto layerName = fmt::format(
"Slice:{}:{}", subgraphIndex, operatorIndex);
1502 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1504 IConnectableLayer*
const layer = m_Network->AddSliceLayer(desc, layerName.c_str());
1509 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1510 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1513 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1514 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1517 void TfLiteParserImpl::ParseSoftmax(
size_t subgraphIndex,
size_t operatorIndex)
1519 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1520 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1521 const auto * options = operatorPtr->builtin_options.AsSoftmaxOptions();
1524 desc.
m_Beta = options->beta;
1526 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1528 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1531 auto layerName = fmt::format(
"Softmax:{}:{}", subgraphIndex, operatorIndex);
1532 IConnectableLayer*
const layer = m_Network->AddSoftmaxLayer(desc, layerName.c_str());
1539 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1540 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1543 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1544 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1547 void TfLiteParserImpl::ParseSpaceToBatchND(
size_t subgraphIndex,
size_t operatorIndex)
1549 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1551 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1554 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1563 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1564 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1566 std::vector<unsigned int> padListVector(padListTensorInfo.
GetNumElements());
1567 ::memcpy(padListVector.data(), padListBufferPtr->data.data(), padListTensorInfo.
GetNumBytes());
1570 std::vector<std::pair<unsigned int, unsigned int>> padList;
1571 for (
unsigned int i = 0; i < padListTensorInfo.
GetNumElements() / step; ++i)
1573 padList.emplace_back(padListVector[i * step], padListVector[i * step + 1]);
1581 auto layerName = fmt::format(
"SpaceToBatchND:{}:{}", subgraphIndex, operatorIndex);
1585 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1587 IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(desc, layerName.c_str());
1591 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1592 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1594 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1595 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1602 std::vector<uint32_t> squeezeDims = squeezeDimsIn;
1603 static const uint32_t dimensionSequence[] = { 0, 1, 2, 3 };
1607 std::stringstream ss;
1608 ss <<
"Input tensor has unexpected number of dimensions:" << inputTensorInfo.
GetNumDimensions()
1609 <<
" shape:" << inputTensorInfo.
GetShape() <<
" " 1614 if (squeezeDims.empty())
1616 squeezeDims.assign(dimensionSequence,
1620 std::vector<uint32_t> outputDims;
1623 bool skipSqueeze = (std::find(squeezeDims.begin(), squeezeDims.end(), i) == squeezeDims.end());
1624 auto currentDimension = inputTensorInfo.
GetShape()[i];
1625 if (skipSqueeze || currentDimension != 1)
1627 outputDims.push_back(currentDimension);
1631 if (outputDims.size() > 4)
1633 std::stringstream ss;
1634 ss <<
"Output tensor has unexpected number of dimensions:" << inputTensorInfo.
GetNumDimensions()
1635 <<
" shape:" << inputTensorInfo.
GetShape() <<
" " 1647 return outTensorInfo;
1650 void TfLiteParserImpl::ParseSqueeze(
size_t subgraphIndex,
size_t operatorIndex)
1652 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1654 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1657 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1660 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1661 const auto * options = operatorPtr->builtin_options.AsSqueezeOptions();
1662 auto layerName = fmt::format(
"Squeeze:{}:{}", subgraphIndex, operatorIndex);
1668 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1673 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
1677 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1678 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1680 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1681 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1684 void TfLiteParserImpl::ParseStridedSlice(
size_t subgraphIndex,
size_t operatorIndex)
1686 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1688 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1691 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1694 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1695 const auto * options = operatorPtr->builtin_options.AsStridedSliceOptions();
1709 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.
GetNumBytes());
1714 std::vector<int> end(endTensorInfo.GetNumElements());
1715 ::memcpy(end.data(), endBufferPtr->data.data(), endTensorInfo.GetNumBytes());
1720 std::vector<int> stride(strideTensorInfo.GetNumElements());
1721 ::memcpy(stride.data(), strideBufferPtr->data.data(), strideTensorInfo.GetNumBytes());
1727 auto layerName = fmt::format(
"StridedSlice:{}:{}", subgraphIndex, operatorIndex);
1728 IConnectableLayer* layer = m_Network->AddStridedSliceLayer(desc, layerName.c_str());
1734 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1735 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1737 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1738 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1741 void TfLiteParserImpl::ParseSub(
size_t subgraphIndex,
size_t operatorIndex)
1743 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1745 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1746 const auto * options = operatorPtr->builtin_options.AsSubOptions();
1748 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1751 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1757 auto layerName = fmt::format(
"Sub:{}:{}", subgraphIndex, operatorIndex);
1764 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1765 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1767 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1769 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1770 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1773 void TfLiteParserImpl::ParseDiv(
size_t subgraphIndex,
size_t operatorIndex)
1775 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1777 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1778 const auto * options = operatorPtr->builtin_options.AsDivOptions();
1780 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1783 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1789 auto layerName = fmt::format(
"Div:{}:{}", subgraphIndex, operatorIndex);
1796 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1797 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1798 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1800 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1801 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1804 void TfLiteParserImpl::ParseAdd(
size_t subgraphIndex,
size_t operatorIndex)
1806 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1808 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1809 const auto * options = operatorPtr->builtin_options.AsAddOptions();
1811 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1814 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1820 auto layerName = fmt::format(
"Add:{}:{}", subgraphIndex, operatorIndex);
1827 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1828 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1829 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1831 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1832 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1835 void TfLiteParserImpl::ParseMul(
size_t subgraphIndex,
size_t operatorIndex)
1837 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1839 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1840 const auto * options = operatorPtr->builtin_options.AsMulOptions();
1842 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1845 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1851 auto layerName = fmt::format(
"Mul:{}:{}", subgraphIndex, operatorIndex);
1852 IConnectableLayer* layer = m_Network->AddMultiplicationLayer(layerName.c_str());
1858 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1859 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1860 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1862 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1863 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1866 void TfLiteParserImpl::ParseMean(
size_t subgraphIndex,
size_t operatorIndex)
1868 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1870 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1872 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1879 std::vector<unsigned int> axis(dimTensorInfo.GetNumElements());
1880 ::memcpy(axis.data(), bufferPtr->data.data(), dimTensorInfo.GetNumBytes());
1890 auto layerName = fmt::format(
"Mean:{}:{}", subgraphIndex, operatorIndex);
1896 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1897 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1899 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1900 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1903 void TfLiteParserImpl::ParseNeg(
size_t subgraphIndex,
size_t operatorIndex)
1905 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1907 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1910 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1913 auto layerName = fmt::format(
"Neg:{}:{}", subgraphIndex, operatorIndex);
1915 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(descriptor, layerName.c_str());
1921 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1922 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1924 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1925 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1928 void TfLiteParserImpl::ParsePad(
size_t subgraphIndex,
size_t operatorIndex)
1930 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1942 std::vector<unsigned int> padBuffer(padTensorInfo.
GetNumElements());
1943 ::memcpy(padBuffer.data(), bufferPtr->data.data(), padTensorInfo.
GetNumBytes());
1947 if (inputTensorInfo.IsQuantized())
1949 desc.
m_PadValue =
static_cast<float>(inputTensorInfo.GetQuantizationOffset());
1951 for (
unsigned int i = 0; i < padTensorInfo.
GetNumElements() / step; ++i)
1953 desc.
m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
1956 auto layerName = fmt::format(
"Pad:{}:{}", subgraphIndex, operatorIndex);
1963 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1964 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1966 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1967 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1970 void TfLiteParserImpl::ParseQuantize(
size_t subgraphIndex,
size_t operatorIndex)
1972 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1974 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1977 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1980 auto layerName = fmt::format(
"Quantize:{}:{}", subgraphIndex, operatorIndex);
1988 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1989 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1991 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1992 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1995 void TfLiteParserImpl::ParseRelu(
size_t subgraphIndex,
size_t operatorIndex)
1997 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::ReLu);
2000 void TfLiteParserImpl::ParseRelu6(
size_t subgraphIndex,
size_t operatorIndex)
2002 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::BoundedReLu);
2005 void TfLiteParserImpl::ParseLeakyRelu(
size_t subgraphIndex,
size_t operatorIndex)
2007 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::LeakyReLu);
2010 void TfLiteParserImpl::ParseLogistic(
size_t subgraphIndex,
size_t operatorIndex)
2012 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::Sigmoid);
2015 void TfLiteParserImpl::ParseTanH(
size_t subgraphIndex,
size_t operatorIndex)
2017 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::TanH);
2020 void TfLiteParserImpl::ParseElu(
size_t subgraphIndex,
size_t operatorIndex)
2022 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::Elu);
2025 void TfLiteParserImpl::ParseHardSwish(
size_t subgraphIndex,
size_t operatorIndex)
2027 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::HardSwish);
2030 void TfLiteParserImpl::ParseActivation(
size_t subgraphIndex,
size_t operatorIndex,
ActivationFunction activationType)
2032 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2033 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2036 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2039 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2042 auto layerName = fmt::format(
"Activation:");
2046 switch (activationType)
2048 case ActivationFunction::ReLu:
2050 layerName += fmt::format(
"RELU:{}:{}", subgraphIndex, operatorIndex);
2053 case ActivationFunction::BoundedReLu:
2055 layerName += fmt::format(
"RELU6:{}:{}", subgraphIndex, operatorIndex);
2056 activationDesc.
m_A = 6.0f;
2057 activationDesc.
m_B = 0.0f;
2060 case ActivationFunction::Sigmoid:
2062 layerName += fmt::format(
"SIGMOID:{}:{}", subgraphIndex, operatorIndex);
2065 case ActivationFunction::TanH:
2067 layerName += fmt::format(
"TANH:{}:{}", subgraphIndex, operatorIndex);
2068 activationDesc.
m_A = 1.0f;
2069 activationDesc.
m_B = 1.0f;
2072 case ActivationFunction::LeakyReLu:
2074 layerName += fmt::format(
"LEAKYRELU:{}:{}", subgraphIndex, operatorIndex);
2075 const auto * options = operatorPtr->builtin_options.AsLeakyReluOptions();
2076 activationDesc.
m_A = options->alpha;
2079 case ActivationFunction::Elu:
2081 layerName += fmt::format(
"ELU:{}:{}", subgraphIndex, operatorIndex);
2082 activationDesc.
m_A = 1.0f;
2085 case ActivationFunction::HardSwish:
2087 layerName += fmt::format(
"HARDSWISH:{}:{}", subgraphIndex, operatorIndex);
2093 fmt::format(
"Unexpected ActivationFunction[{}] when creating layerName {} ",
2098 IConnectableLayer*
const layer = m_Network->AddActivationLayer(activationDesc, layerName.c_str());
2105 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2106 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2109 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2110 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2113 const std::vector<int32_t> & targetDimsIn)
2115 std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
2116 const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
2118 if (stretchDim != targetDimsIn.end())
2120 if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
2123 fmt::format(
"At most one component of shape can be -1 {}",
CHECK_LOCATION().AsString()));
2126 auto targetNumElements =
2128 std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
2130 auto stretchIndex =
static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
2131 outputDims[stretchIndex] = inputTensorInfo.
GetNumElements() / targetNumElements;
2142 void TfLiteParserImpl::ParseReshape(
size_t subgraphIndex,
size_t operatorIndex)
2144 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2146 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2148 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2151 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2152 const auto * options = operatorPtr->builtin_options.AsReshapeOptions();
2153 auto layerName = fmt::format(
"Reshape:{}:{}", subgraphIndex, operatorIndex);
2157 CheckMatchingQuantization(inputTensorInfo, actualOutputTensorInfo, layerName,
"Input 0",
"Output 0");
2163 std::vector<int32_t> targetShape;
2164 bool targetShapeFound =
false;
2166 if (options !=
nullptr)
2169 if (options->new_shape.empty() ==
false)
2171 targetShape = options->new_shape;
2172 targetShapeFound =
true;
2177 if (!targetShapeFound)
2180 if (inputs.size() > 1 && inputs[1] !=
nullptr)
2182 if (inputs[1]->is_variable)
2187 if (inputs[1]->shape.size() != 1)
2192 if (inputs[1]->type != tflite::TensorType_INT32)
2198 auto bufferPtr =
GetBuffer(m_Model, inputs[1]->buffer);
2199 auto values =
reinterpret_cast<const int32_t*
>(bufferPtr->data.data());
2204 for (
int i=0; i < inputs[1]->shape[0]; ++i)
2206 targetShape.push_back(values[i]);
2212 "At least one method required");
2221 if (inputs.size() > 1 && !
CheckShape(reshapeOutputTensorShape, outputs[0]->shape))
2223 std::stringstream ss;
2224 ss <<
"New shape defined in reshape parameters " 2225 << reshapeOutputTensorShape
2226 <<
" does not equal output shape " 2227 << actualOutputTensorInfo.
GetShape()
2236 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
2240 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2241 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2243 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2244 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2247 void TfLiteParserImpl::ParseResizeBilinear(
size_t subgraphIndex,
size_t operatorIndex)
2249 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::Bilinear);
2252 void TfLiteParserImpl::ParseResizeNearestNeighbor(
size_t subgraphIndex,
size_t operatorIndex)
2254 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::NearestNeighbor);
2257 void TfLiteParserImpl::ParseResize(
size_t subgraphIndex,
size_t operatorIndex,
ResizeMethod resizeMethod)
2259 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2261 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2264 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2270 std::vector<int32_t> sizeTensorData(sizeTensorInfo.GetNumElements());
2273 ::memcpy(sizeTensorData.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
2277 desc.m_TargetHeight =
static_cast<uint32_t
> (sizeTensorData[0]);
2278 desc.m_TargetWidth =
static_cast<uint32_t
> (sizeTensorData[1]);
2281 auto layerName = fmt::format(
"Resize:");
2283 switch (resizeMethod)
2285 case ResizeMethod::Bilinear:
2287 layerName += fmt::format(
"BILINEAR:{}:{}", subgraphIndex, operatorIndex);
2289 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2290 const auto * options = operatorPtr->builtin_options.AsResizeBilinearOptions();
2292 desc.m_AlignCorners = options->align_corners;
2295 case ResizeMethod::NearestNeighbor:
2297 layerName += fmt::format(
"NEARESTNEIGHBOR:{}:{}", subgraphIndex, operatorIndex);
2303 fmt::format(
"Unexpected ResizeMethod[{}] when creating layerName {} ",
2310 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
2316 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2317 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2319 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2320 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2323 void TfLiteParserImpl::ParseConcatenation(
size_t subgraphIndex,
size_t operatorIndex)
2325 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2327 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2328 const auto * options = operatorPtr->builtin_options.AsConcatenationOptions();
2332 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2333 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2336 unsigned int numConcatView =
static_cast<unsigned int>(inputs.size());
2339 const unsigned int concatDimInput =
static_cast<unsigned int>(
2340 (
static_cast<int>(inputRank) + options->axis) %
static_cast<int>(inputRank));
2342 OriginsDescriptor concatDescriptor(static_cast<uint32_t>(numConcatView), inputRank);
2345 unsigned int mergeDimOrigin = 0;
2347 for (
unsigned int viewIndex = 0; viewIndex < numConcatView; ++viewIndex)
2353 inputTensorInfo, concatDescriptor, concatDimInput, viewIndex, mergeDimOrigin);
2356 auto layerName = fmt::format(
"Concatenation:{}:{}", subgraphIndex, operatorIndex);
2359 IConnectableLayer* layer = m_Network->AddConcatLayer(concatDescriptor, layerName.c_str());
2363 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2364 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
2367 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2369 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2370 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2373 void TfLiteParserImpl::ParseFullyConnected(
size_t subgraphIndex,
size_t operatorIndex)
2375 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2377 const auto & operatorRfr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2378 const auto options = operatorRfr->builtin_options.AsFullyConnectedOptions();
2386 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2387 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2393 int32_t weightsDimension =
static_cast<int32_t
>(filterTensorInfo.GetNumDimensions());
2394 if (weightsDimension != 2)
2397 fmt::format(
"Dimension {} for Fully Connected weights is not supported by Armnn. " 2403 auto filterTensorAndData = CreateConstTensor(inputs[1],
2407 auto layerName = fmt::format(
"FullyConnected:{}:{}", subgraphIndex, operatorIndex);
2409 if (inputs.size() == 3)
2413 auto biasTensorAndData = CreateConstTensor(inputs[2],
2416 layer = m_Network->AddFullyConnectedLayer(desc,
2417 filterTensorAndData.first,
2423 layer = m_Network->AddFullyConnectedLayer(desc,
2424 filterTensorAndData.first,
2432 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2440 std::vector<unsigned int> reshapedDimensions(2);
2441 reshapedDimensions[1] = filterTensorInfo.GetShape()[1];
2442 reshapedDimensions[0] = inputTensorInfo.
GetNumElements() / reshapedDimensions[1];
2444 if (inputTensorInfo.
GetNumElements() % reshapedDimensions[1] != 0)
2447 fmt::format(
"Failed to deduce input tensor shape from filter size {} {}",
2448 reshapedDimensions[1],
2455 std::string reshapeLayerName = fmt::format(
"Reshape_for:{}", layer->
GetName());
2457 desc.m_TargetShape = reshapedTensorInfo.
GetShape();
2463 RegisterInputSlots(subgraphIndex, operatorIndex, reshapeLayer, {inputTensorIndexes[0]});
2469 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2477 options->fused_activation_function);
2480 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2481 RegisterOutputSlots(subgraphIndex, operatorIndex, fusedActivationLayer, {outputTensorIndexes[0]});
2484 void TfLiteParserImpl::ParseDetectionPostProcess(
size_t subgraphIndex,
size_t operatorIndex)
2486 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2488 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2490 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2491 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2495 auto custom_options = operatorPtr->custom_options;
2496 const flexbuffers::Map& m = flexbuffers::GetRoot(custom_options.data(), custom_options.size()).AsMap();
2505 desc.
m_ScaleH = m[
"h_scale"].AsFloat();
2506 desc.
m_ScaleW = m[
"w_scale"].AsFloat();
2507 desc.
m_ScaleX = m[
"x_scale"].AsFloat();
2508 desc.
m_ScaleY = m[
"y_scale"].AsFloat();
2510 if (!(m[
"use_regular_nms"].IsNull()))
2514 if (!(m[
"detections_per_class"].IsNull()))
2522 "must be positive and less than or equal to 1.");
2526 auto anchorTensorAndData = CreateConstTensor(inputs[2], anchorTensorInfo,
2529 auto layerName = fmt::format(
"DetectionPostProcess:{}:{}", subgraphIndex, operatorIndex);
2530 IConnectableLayer* layer = m_Network->AddDetectionPostProcessLayer(desc, anchorTensorAndData.first,
2538 m_OverridenOutputShapes.push_back({ 1, numDetectedBox, 4 });
2539 m_OverridenOutputShapes.push_back({ 1, numDetectedBox });
2540 m_OverridenOutputShapes.push_back({ 1, numDetectedBox });
2541 m_OverridenOutputShapes.push_back({ 1 });
2543 for (
unsigned int i = 0 ; i < outputs.size() ; ++i)
2551 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2552 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2555 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2556 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0],
2557 outputTensorIndexes[1],
2558 outputTensorIndexes[2],
2559 outputTensorIndexes[3]});
2563 void TfLiteParserImpl::ParsePack(
size_t subgraphIndex,
size_t operatorIndex)
2565 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2567 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2568 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2571 if (inputs.size() < 1)
2576 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2577 const auto* options = operatorPtr->builtin_options.AsPackOptions();
2580 desc.
m_Axis =
static_cast<uint32_t
>(options->axis);
2581 desc.
m_NumInputs =
static_cast<uint32_t
>(inputs.size());
2587 auto layerName = fmt::format(
"Pack:{}:{}", subgraphIndex, operatorIndex);
2595 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2596 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
2598 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2599 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2602 void TfLiteParserImpl::ParseUnpack(
size_t subgraphIndex,
size_t operatorIndex)
2604 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2606 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2607 const auto * options = operatorPtr->builtin_options.AsUnpackOptions();
2612 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2617 if (unpackAxis >= inputTensorInfo.GetNumDimensions())
2620 fmt::format(
"The unpack axis: {} cannot be greater than or equal to " 2621 "the number of input dimension {} {}",
2623 inputTensorInfo.GetNumDimensions(),
2631 unpackNum = inputTensorInfo.GetShape()[unpackAxis];
2637 throw ParseException(
"Number to unpack must greater than zero.");
2640 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2643 auto inputDimSize = inputTensorInfo.GetNumDimensions();
2644 std::vector<unsigned int> unpackDimSizes(inputDimSize);
2647 for (
unsigned int i = 0; i < inputDimSize; ++i)
2649 unpackDimSizes[i] = inputTensorInfo.GetShape()[i];
2652 if (unpackDimSizes[unpackAxis] != unpackNum)
2654 throw ParseException(
"Number to unpack must be the same as length of the dimension to " 2658 unpackDimSizes[unpackAxis] /= unpackNum;
2660 SplitterDescriptor splitDesc(unpackNum, static_cast<unsigned int>(unpackDimSizes.size()));
2661 for (
unsigned int j = 0; j < unpackNum; ++j)
2664 for (
unsigned int dimIdx = 0; dimIdx < unpackDimSizes.size(); ++dimIdx)
2666 splitDesc.
SetViewSize(j, dimIdx, unpackDimSizes[dimIdx]);
2671 auto layerName = fmt::format(
"Unpack:{}:{}", subgraphIndex, operatorIndex);
2672 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
2676 unpackDimSizes.data());
2678 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2679 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2685 std::string reshapeLayerName = fmt::format(
"Reshape_for:{}", layer->
GetName());
2700 RegisterProducerOfTensor(subgraphIndex, reshapedOutputId, slot);
2704 void TfLiteParserImpl::ParseSplit(
size_t subgraphIndex,
size_t operatorIndex)
2706 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2708 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2709 const auto * options = operatorPtr->builtin_options.AsSplitOptions();
2716 throw ParseException(
"Number to splits must greater than zero.");
2719 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2721 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2728 std::vector<unsigned int> axisData(axisTensorInfo.
GetNumElements());
2729 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.
GetNumBytes());
2732 const unsigned int splitDim = axisData[0];
2734 auto inputDimSize = inputTensorInfo.GetNumDimensions();
2738 fmt::format(
"The number of dimensions: {} for input tensors of the split op cannot be greater than {} {}",
2739 inputTensorInfo.GetNumDimensions(),
2744 std::vector<unsigned int> splitterDimSizes(inputDimSize);
2747 for (
unsigned int i = 0; i < inputDimSize; ++i)
2749 splitterDimSizes[i] = inputTensorInfo.GetShape()[i];
2752 if (splitterDimSizes[splitDim] % numSplits != 0)
2754 throw ParseException(
"Number of splits must evenly divide the dimension");
2756 splitterDimSizes[splitDim] /= numSplits;
2759 for (
unsigned int j = 0; j < numSplits; ++j)
2762 for (
unsigned int dimIdx = 0; dimIdx < splitterDimSizes.size(); ++dimIdx)
2764 splitDesc.
SetViewSize(j, dimIdx, splitterDimSizes[dimIdx]);
2769 auto layerName = fmt::format(
"Split:{}:{}", subgraphIndex, operatorIndex);
2770 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
2773 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2774 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[1]});
2782 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2783 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2789 int v = idx < 0 ? numDims + idx : idx;
2793 return static_cast<unsigned int>(v);
2796 void TfLiteParserImpl::ParseSplitV(
size_t subgraphIndex,
size_t operatorIndex)
2798 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2800 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2801 const auto * options = operatorPtr->builtin_options.AsSplitVOptions();
2803 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2806 auto& inputTensor = inputs[0];
2807 auto& splitsTensor = inputs[1];
2808 auto& axisTensor = inputs[2];
2820 fmt::format(
"The number of dimensions: {} for input tensors of the " 2821 "SplitV op cannot be greater than {} {}",
2830 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.
GetNumBytes());
2835 unsigned int numSplits{0};
2851 std::vector<int> splitsData(numSplits);
2853 ::memcpy(splitsData.data(), splitsBufferPtr->data.data(), splitsInfo.
GetNumBytes());
2855 unsigned int idx = 0;
2857 unsigned int inferIdx{0};
2859 for (
auto split : splitsData)
2873 if (numInferred == 0)
2875 if (splitSum != armnn::numeric_cast<int>(inputTensorInfo.
GetShape()[splitDim]))
2877 throw ParseException(
"SplitV split_sizes does not sum to the dimension of value along split_dim.");
2880 else if (numInferred == 1)
2886 throw ParseException(
"Cannot infer split size for more than one split");
2890 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2895 unsigned int accumSplit = 0;
2896 for (
unsigned int j = 0; j < numSplits; ++j)
2901 for (
unsigned int dimIdx = 0; dimIdx < inputTensorInfo.
GetNumDimensions(); ++dimIdx)
2903 unsigned int dimSize = inputTensorInfo.
GetShape()[dimIdx];
2904 if (dimIdx == splitDim)
2906 dimSize = splitSize;
2908 splitDesc.SetViewSize(j, dimIdx, dimSize);
2911 splitDesc.SetViewOriginCoord(j, splitDim, accumSplit);
2912 accumSplit += splitSize;
2915 auto layerName = fmt::format(
"SplitV:{}:{}", subgraphIndex, operatorIndex);
2916 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
2919 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2920 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2928 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2929 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2932 void TfLiteParserImpl::ParseArgMax(
size_t subgraphIndex,
size_t operatorIndex)
2934 const auto &operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2935 const auto *options = operatorPtr->builtin_options.AsArgMaxOptions();
2937 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2938 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2941 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2944 auto layerName = fmt::format(
"ArgMax:{}:{}", subgraphIndex, operatorIndex);
2953 desc.
m_Axis = axisBufferPtr->data.data()[0];
2959 IConnectableLayer *layer = m_Network->AddArgMinMaxLayer(desc, layerName.c_str());
2965 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2966 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2969 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2970 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2973 void TfLiteParserImpl::ParseGather(
size_t subgraphIndex,
size_t operatorIndex)
2975 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2988 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2989 const auto * options = operatorPtr->builtin_options.AsGatherOptions();
2990 auto axis = options->axis;
2992 auto inputDimensions =
static_cast<int32_t
>(inputTensorInfo.GetNumDimensions());
2995 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
2998 fmt::format(
"Operation has invalid axis: {} It is out of bounds [ -{}, {} ) {}",
3000 inputDimensions, inputDimensions,
3003 if (outputDimensions != static_cast<unsigned int>(inputDimensions) + indicesDimensions - 1)
3006 fmt::format(
"Operation has invalid output dimensions: {} Output must be an ({} + {} - 1) -D tensor {}",
3008 inputDimensions, indicesDimensions,
3012 gatherDescriptor.
m_Axis = axis;
3014 auto layerName = fmt::format(
"Gather:{}:{}", subgraphIndex, operatorIndex);
3015 IConnectableLayer* layer = m_Network->AddGatherLayer(gatherDescriptor, layerName.c_str());
3019 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3020 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3022 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3023 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3026 void TfLiteParserImpl::ParseDepthToSpace(
size_t subgraphIndex,
size_t operatorIndex)
3028 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3037 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3038 const auto * options = operatorPtr->builtin_options.AsDepthToSpaceOptions();
3039 auto blockSize = options->block_size;
3043 fmt::format(
"Operation has invalid block size: {} Block size should be >= 2 {}",
3049 auto layerName = fmt::format(
"DepthToSpace:{}:{}", subgraphIndex, operatorIndex);
3050 IConnectableLayer* layer = m_Network->AddDepthToSpaceLayer(descriptor, layerName.c_str());
3055 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3056 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3058 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3059 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3062 void TfLiteParserImpl::ParseSum(
size_t subgraphIndex,
size_t operatorIndex)
3067 void TfLiteParserImpl::ParseReduceMax(
size_t subgraphIndex,
size_t operatorIndex)
3072 void TfLiteParserImpl::ParseReduceMin(
size_t subgraphIndex,
size_t operatorIndex)
3077 void TfLiteParserImpl::ParseReduce(
size_t subgraphIndex,
size_t operatorIndex,
ReduceOperation reduceOperation)
3079 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3081 const auto &operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3082 const auto *options = operatorPtr->builtin_options.AsReducerOptions();
3084 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3087 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3090 auto layerName = fmt::format(
"Reduce:{}:{}", subgraphIndex, operatorIndex);
3098 if (axisBufferPtr !=
nullptr)
3100 std::vector<int32_t> axisData(inputTensorInfo1.
GetNumElements());
3101 ::memcpy(axisData.data(), axisBufferPtr->data.data(), inputTensorInfo1.
GetNumBytes());
3105 std::set<unsigned int> uniqueAxis;
3106 std::transform(axisData.begin(),
3108 std::inserter(uniqueAxis, uniqueAxis.begin()),
3109 [rank](
int i)->unsigned
int{
3110 return static_cast<uint32_t
>(((i + rank) % rank)); });
3111 desc.
m_vAxis.assign(uniqueAxis.begin(), uniqueAxis.end());
3131 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3132 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3135 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3136 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3140 unsigned int outputSlot,
3141 tflite::ActivationFunctionType activationType)
3144 std::string layerName = prevLayer->
GetName();
3146 switch(activationType)
3148 case tflite::ActivationFunctionType_NONE:
3153 case tflite::ActivationFunctionType_RELU:
3155 activationDesc.
m_Function = ActivationFunction::ReLu;
3156 layerName +=
":RELU";
3159 case tflite::ActivationFunctionType_RELU6:
3161 activationDesc.
m_Function = ActivationFunction::BoundedReLu;
3162 activationDesc.
m_A = 6.0f;
3163 activationDesc.
m_B = 0.0f;
3164 layerName +=
":RELU6";
3167 case tflite::ActivationFunctionType_TANH:
3169 activationDesc.
m_Function = ActivationFunction::TanH;
3170 activationDesc.
m_A = 1.0f;
3171 activationDesc.
m_B = 1.0f;
3172 layerName +=
":TANH";
3177 case tflite::ActivationFunctionType_RELU_N1_TO_1:
3178 case tflite::ActivationFunctionType_SIGN_BIT:
3182 fmt::format(
"TfLite parser doesn't suppport fused activation: " 3185 tflite::EnumNameActivationFunctionType(activationType),
3192 m_Network->AddActivationLayer(activationDesc, layerName.c_str());
3194 auto & prevOutputSlot = prevLayer->
GetOutputSlot(outputSlot);
3197 return activationLayer;
3202 if (fileName ==
nullptr)
3207 std::error_code errorCode;
3208 fs::path pathToFile(fileName);
3209 if (!fs::exists(pathToFile, errorCode))
3212 std::stringstream msg;
3213 msg <<
"Cannot find the file (" << fileName <<
") errorCode: " << errorCode
3218 std::ifstream file(fileName, std::ios::binary);
3219 std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
3221 fileContent.size());
3226 if (binaryContent ==
nullptr)
3231 flatbuffers::Verifier verifier(binaryContent, len);
3232 if (verifier.VerifyBuffer<tflite::Model>() ==
false)
3235 fmt::format(
"Buffer doesn't conform to the expected Tensorflow Lite " 3236 "flatbuffers format. size:{} {}",
3240 return tflite::UnPackModel(binaryContent);
3244 size_t subgraphIndex,
3245 size_t operatorIndex)
3249 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3250 const auto & operatorPtr = subgraphPtr->operators[operatorIndex];
3252 size_t inputCount = operatorPtr->inputs.size();
3254 for (
size_t i=0; i<inputCount; ++i)
3257 result[i] = subgraphPtr->tensors[inputId].get();
3263 size_t subgraphIndex,
3264 size_t operatorIndex)
3268 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3269 const auto & operatorPtr = subgraphPtr->operators[operatorIndex];
3271 size_t outputCount = operatorPtr->outputs.size();
3273 for (
size_t i=0; i<outputCount; ++i)
3277 result[i] = subgraphPtr->tensors[outputId].get();
3283 size_t subgraphIndex)
3286 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3288 size_t inputCount = subgraphPtr->inputs.size();
3290 for (
size_t i=0; i<inputCount; ++i)
3294 result[i] = std::make_pair(inputId, subgraphPtr->tensors[inputId].get());
3300 size_t subgraphIndex)
3303 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3305 size_t outputCount = subgraphPtr->outputs.size();
3307 for (
size_t i=0; i<outputCount; ++i)
3310 result[i] = std::make_pair(outputId, subgraphPtr->tensors[outputId].get());
3316 size_t subgraphIndex,
3317 size_t operatorIndex)
3320 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3321 const auto & operatorPtr = subgraphPtr->operators[operatorIndex];
3322 return operatorPtr->inputs;
3326 size_t subgraphIndex,
3327 size_t operatorIndex)
3330 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3331 const auto & operatorPtr = subgraphPtr->operators[operatorIndex];
3332 return operatorPtr->outputs;
3335 void TfLiteParserImpl::RegisterInputSlots(
size_t subgraphIndex,
3336 size_t operatorIndex,
3338 const std::vector<unsigned int>& tensorIndexes)
3340 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3345 fmt::format(
"The number of tensor inputs ({}) does not match the number expected ({})" 3346 " for subgraph:{} operator index:{} {}",
3347 tensorIndexes.size(),
3354 for (
unsigned int slotIndex = 0; slotIndex < layer->
GetNumInputSlots(); ++slotIndex)
3356 unsigned int tensorIndex = tensorIndexes[slotIndex];
3358 RegisterConsumerOfTensor(subgraphIndex, tensorIndex, slot);
3362 void TfLiteParserImpl::RegisterOutputSlots(
size_t subgraphIndex,
3363 size_t operatorIndex,
3365 const std::vector<unsigned int>& tensorIndexes)
3367 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3372 fmt::format(
"The number of tensor outputs ({}) does not match the number expected ({})" 3373 " for subgraph:{} operator index:{} {}",
3374 tensorIndexes.size(),
3381 for (
unsigned int slotIndex = 0; slotIndex < layer->
GetNumOutputSlots(); ++slotIndex)
3383 unsigned int tensorIndex = tensorIndexes[slotIndex];
3385 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
3389 void TfLiteParserImpl::SetupInputLayers(
size_t subgraphIndex)
3394 for (
auto const & tensorIdAndPtr : inputs)
3396 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
3398 m_Network->AddInputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
3403 RegisterOutputSlots(subgraphIndex,
3404 VIRTUAL_OPERATOR_ID,
3406 {
static_cast<uint32_t
>(tensorIdAndPtr.first) });
3410 void TfLiteParserImpl::SetupOutputLayers(
size_t subgraphIndex)
3415 for (
auto const & tensorIdAndPtr : outputs)
3417 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
3419 m_Network->AddOutputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
3421 RegisterInputSlots(subgraphIndex,
3422 VIRTUAL_OPERATOR_ID,
3424 {
static_cast<uint32_t
>(tensorIdAndPtr.first) });
3428 void TfLiteParserImpl::SetupConstantLayers(
size_t subgraphIndex)
3432 const auto & subgraphPtr = m_Model->subgraphs[subgraphIndex];
3433 for (
unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
3435 for (
unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
3437 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot ==
nullptr &&
3438 m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0)
3440 TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get();
3442 auto tensorAndData = CreateConstTensor(tensorPtr,
3446 std::string layerName = fmt::format(
"Constant:{}", tensorPtr->name);
3448 m_Network->AddConstantLayer(tensorAndData.first, layerName.c_str());
3451 RegisterOutputSlots(subgraphIndex,
3452 VIRTUAL_OPERATOR_ID,
3465 return model->buffers[bufferIndex].get();
3468 template<
typename T>
3469 std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
3475 auto constData = CreateConstTensorImpl<T>(bufferPtr,
3479 TfLiteParserImpl::SupportedDataStorage storage(std::move(constData.second));
3480 return std::make_pair(constData.first, std::move(storage));
3483 std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
3484 TfLiteParserImpl::CreateConstTensor(
TensorRawPtr tensorPtr,
3489 auto bufferPtr =
GetBuffer(m_Model, tensorPtr->buffer);
3495 return CreateConstTensorAndStoreData<float>(bufferPtr,
3500 return CreateConstTensorAndStoreData<uint8_t>(bufferPtr,
3505 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
3510 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
3515 return CreateConstTensorAndStoreData<int32_t>(bufferPtr,
3521 std::stringstream errString;
3522 errString <<
"Unexpected datatype when creating const tensor: " 3524 <<
" shape:" << tensorInfo.GetShape()
3532 const std::string& name)
const 3536 for (
auto const & input : inputs)
3538 if (input.second->name == name)
3540 auto bindingId = GenerateLayerBindingId(subgraphId, input.first);
3541 return std::make_pair(bindingId,
ToTensorInfo(input.second));
3545 std::stringstream bindings;
3546 for (
auto const & input : inputs)
3548 bindings <<
"'" << input.second->name <<
"' ";
3552 fmt::format(
"No input binding found for subgraph:{} and name:{}. " 3553 "Possible inputs are: [{}] {}",
3561 const std::string& name)
const 3565 for (
unsigned int i = 0; i < outputs.size(); ++i)
3567 auto const output = outputs[i];
3568 if (output.second->name == name)
3570 auto bindingId = GenerateLayerBindingId(subgraphId, output.first);
3571 std::vector<unsigned int> shape = m_OverridenOutputShapes.size() > 0 ?
3572 m_OverridenOutputShapes[i] : AsUnsignedVector(output.second->shape);
3573 return std::make_pair(bindingId,
ToTensorInfo(output.second, shape));
3577 std::stringstream bindings;
3578 for (
auto const & output : outputs)
3580 bindings <<
"'" << output.second->name <<
"' ";
3584 fmt::format(
"No output binding found for subgraph:{} and name:{}. " 3585 "Possible outputs are: [{}] {}",
3594 return m_Model->subgraphs.size();
3601 std::vector<std::string> result;
3602 result.reserve(inputs.size());
3603 for (
auto const & input : inputs)
3605 result.push_back(input.second->name);
3614 std::vector<std::string> result;
3615 result.reserve(outputs.size());
3616 for (
auto const & output : outputs)
3618 result.push_back(output.second->name);
3628 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<
float[]> && data)
3629 : m_FloatData(std::move(data))
3630 , m_Uint8Data(
nullptr)
3631 , m_Int8Data(
nullptr)
3632 , m_Int32Data(
nullptr)
3636 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<uint8_t[]> && data)
3637 : m_FloatData(
nullptr)
3638 , m_Uint8Data(std::move(data))
3639 , m_Int8Data(
nullptr)
3640 , m_Int32Data(
nullptr)
3644 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int8_t[]> && data)
3645 : m_FloatData(
nullptr)
3646 , m_Uint8Data(
nullptr)
3647 , m_Int8Data(std::move(data))
3648 , m_Int32Data(
nullptr)
3652 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int32_t[]> && data)
3653 : m_FloatData(
nullptr)
3654 , m_Uint8Data(
nullptr)
3655 , m_Int8Data(
nullptr)
3656 , m_Int32Data(std::move(data))
uint32_t m_PadBottom
Padding bottom value in the height dimension.
bool m_BiasEnabled
Enable/disable bias.
#define CHECK_MODEL(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX)
std::unique_ptr< tflite::ModelT > ModelPtr
static TensorIdRawPtrVector GetSubgraphOutputs(const ModelPtr &model, size_t subgraphIndex)
virtual unsigned int GetNumOutputSlots() const =0
Returns the number of connectable output slots.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
UnaryOperation m_Operation
Specifies the elementwiseUnary operation to execute.
uint32_t m_Axis
0-based axis along which to stack the input tensors.
A ViewsDescriptor for the SplitterLayer.
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
float m_ScaleW
Center size encoding scale weight.
bool IsTypeSpaceMatch(const TensorInfo &other) const
Check that the types are the same and, if quantize, that the quantization parameters are the same...
uint32_t m_PadBottom
Padding bottom value in the height dimension.
bool m_BiasEnabled
Enable/disable bias.
virtual unsigned int GetNumInputSlots() const =0
Returns the number of connectable input slots.
A TransposeConvolution2dDescriptor for the TransposeConvolution2dLayer.
#define ARMNN_THROW_PARSE_EXCEPTION(msg)
const TensorShape & GetShape() const
uint32_t m_PadBottom
Padding bottom value in the height dimension.
uint32_t m_PadLeft
Padding left value in the width dimension.
const tflite::TensorT * TensorRawPtr
std::string AsString() const
int32_t m_ShrinkAxisMask
Shrink axis mask value. If set, the nth specification shrinks the dimensionality by 1...
A ReshapeDescriptor for the ReshapeLayer.
std::vector< int > m_Begin
Begin values for the input that will be sliced.
const tflite::BufferT * BufferRawPtr
float m_PadValue
Optional value to use for padding, defaults to 0.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
float m_ScaleX
Center size encoding scale x.
TensorShape m_InputShape
Required shape of all input tensors.
bool m_TransposeWeightMatrix
Enable/disable transpose weight matrix.
uint32_t m_PoolWidth
Pooling width value.
A Convolution2dDescriptor for the Convolution2dLayer.
uint32_t m_PadLeft
Padding left value in the width dimension.
bool m_KeepDims
if true then output shape has no change.
bool m_BiasEnabled
Enable/disable bias.
std::vector< unsigned int > m_OutputShape
unsigned int GetNumBytes() const
ResizeMethod m_Method
The Interpolation method to use (Bilinear, NearestNeighbor).
float m_Beta
Exponentiation value.
armnn::INetworkPtr CreateNetworkFromBinaryFile(const char *graphFile)
Create the network from a flatbuffers binary file on disk.
PaddingMethod m_PaddingMethod
The padding method to be used. (Exclude, IgnoreValue).
BindingPointInfo GetNetworkOutputBindingInfo(size_t subgraphId, const std::string &name) const
Retrieve binding info (layer id and tensor info) for the network output identified by the given layer...
ArgMinMaxFunction m_Function
Specify if the function is to find Min or Max.
uint32_t m_DetectionsPerClass
Detections per classes, used in Regular NMS.
bool m_OutputShapeEnabled
Output shape if it has been specified.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
#define CHECK_BUFFER(MODEL, BUFFER_INDEX)
virtual const char * what() const noexcept override
#define ARMNN_LOG(severity)
uint32_t m_PadTop
Padding top value in the height dimension.
std::vector< BackendOptions > NetworkOptions
std::vector< std::string > GetSubgraphOutputTensorNames(size_t subgraphId) const
Return the output tensor names for a given subgraph.
void ProcessConcatInputTensorInfo(armnn::TensorInfo &inputTensorInfo, armnn::OriginsDescriptor &concatDescriptor, const unsigned int &concatAxis, unsigned int inputIndex, unsigned int &mergeDimOrigin)
uint32_t m_PadRight
Padding right value in the width dimension.
std::vector< std::pair< unsigned int, unsigned int > > m_PadList
Specifies the padding for input dimension.
ReduceOperation m_ReduceOperation
Specifies the reduction operation to execute.
std::unique_ptr< ITfLiteParser, void(*)(ITfLiteParser *parser)> ITfLiteParserPtr
std::unique_ptr< tflite::OperatorT > OperatorPtr
unsigned int ComputeWrappedIndex(int idx, unsigned int numDimsIn)
Copyright (c) 2021 ARM Limited and Contributors.
void IgnoreUnused(Ts &&...)
uint32_t m_PadBottom
Padding bottom value in the height dimension.
int32_t m_BeginMask
Begin mask value.
static armnn::TensorInfo OutputShapeOfReshape(const armnn::TensorInfo &inputTensorInfo, const std::vector< int32_t > &targetDimsIn)
uint32_t m_DilationY
Dilation along y axis.
int32_t m_EndMask
End mask value.
A SpaceToDepthDescriptor for the SpaceToDepthLayer.
std::vector< std::pair< unsigned int, unsigned int > > m_PadList
Specifies the padding values for the input dimension: heightPad{top, bottom} widthPad{left, right}.
uint32_t m_DilationY
Dilation factor value for height dimension.
A BatchToSpaceNdDescriptor for the BatchToSpaceNdLayer.
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
int LayerBindingId
Type of identifiers for bindable layers (inputs, outputs).
#define TFLITE_PARSER_VERSION
TFLITE_PARSER_VERSION: "X.Y.Z" where: X = Major version number Y = Minor version number Z = Patch ver...
virtual void SetTensorInfo(const TensorInfo &tensorInfo)=0
#define CHECK_TENSOR(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX)
constexpr const char * GetDataTypeName(DataType dataType)
void SetShape(const TensorShape &newShape)
armnn::INetworkPtr CreateNetworkFromBinary(const std::vector< uint8_t > &binaryContent)
Create the network from a flatbuffers binary.
A ResizeDescriptor for the ResizeLayer.
static BufferRawPtr GetBuffer(const ModelPtr &model, size_t bufferIndex)
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
uint32_t m_MaxClassesPerDetection
Maximum numbers of classes per detection, used in Fast NMS.
std::vector< unsigned int > m_Axis
Values for the dimensions to reduce.
A StackDescriptor for the StackLayer.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
TensorShape m_TargetShape
Target shape value.
armnn::INetworkPtr CreateNetworkFromBinaryFile(const char *graphFile)
Create the network from a flatbuffers binary file on disk.
uint32_t m_PoolHeight
Pooling height value.
uint32_t m_PadTop
Padding top value in the height dimension.
uint32_t m_MaxDetections
Maximum numbers of detections.
A PadDescriptor for the PadLayer.
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
std::unique_ptr< onnx::ModelProto > ModelPtr
#define CHECK_SUBGRAPH(MODEL, SUBGRAPH_INDEX)
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
void CheckTensor(const ConstTensor &t)
BindingPointInfo GetNetworkInputBindingInfo(size_t subgraphId, const std::string &name) const
Retrieve binding info (layer id and tensor info) for the network input identified by the given layer ...
bool CheckShape(const armnn::TensorShape &actual, const std::vector< uint32_t > &expected)
static ModelPtr LoadModelFromBinary(const uint8_t *binaryContent, size_t len)
static armnn::TensorInfo OutputShapeOfSqueeze(const std::vector< uint32_t > &squeezeDims, const armnn::TensorInfo &inputTensorInfo)
float m_NmsIouThreshold
Intersection over union threshold.
uint32_t m_PadRight
Padding right value in the width dimension.
std::vector< TensorIdRawPtr > TensorIdRawPtrVector
uint32_t m_DilationX
Dilation factor value for width dimension.
uint32_t m_PadTop
Padding top value in the height dimension.
std::string FileLine() const
Status SetViewSize(uint32_t view, uint32_t coord, uint32_t value)
Set the size of the views.
#define ARMNN_ASSERT_MSG(COND, MSG)
int32_t m_NewAxisMask
New axis mask value.
bool m_KeepDims
Enable/disable keep dimensions. If true, then the reduced dimensions that are of length 1 are kept...
static std::vector< int32_t > & GetInputTensorIds(const ModelPtr &model, size_t subgraphIndex, size_t operatorIndex)
std::vector< unsigned int > m_BlockShape
Block shape values.
An output connection slot for a layer.
A L2NormalizationDescriptor for the L2NormalizationLayer.
int32_t GetQuantizationOffset() const
An ArgMinMaxDescriptor for ArgMinMaxLayer.
static const std::string GetVersion()
Retrieve version in X.Y.Z form.
float GetQuantizationScale() const
DataType GetDataType() const
An OriginsDescriptor for the ConcatLayer.
A ReduceDescriptor for the REDUCE operators.
bool has_value() const noexcept
A FullyConnectedDescriptor for the FullyConnectedLayer.
int32_t m_EllipsisMask
Ellipsis mask value.
bool m_BiasEnabled
Enable/disable bias.
static ModelPtr LoadModelFromFile(const char *fileName)
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
A GatherDescriptor for the GatherLayer.
#define CHECK_VALID_SIZE(ACTUAL,...)
uint32_t m_NumClasses
Number of classes.
#define CHECKED_NON_NEGATIVE(VALUE)
std::vector< TensorRawPtr > TensorRawPtrVector
size_t GetSubgraphCount() const
Return the number of subgraphs in the parsed model.
uint32_t m_PadTop
Padding top value in the height dimension.
void CalcPadding(uint32_t inputSize, uint32_t filterSize, uint32_t stride, uint32_t dilation, uint32_t &paddingFront, uint32_t &paddingBack, bool samePadding)
#define ARMNN_ASSERT(COND)
A StandInDescriptor for the StandIn layer.
bool m_UseRegularNms
Use Regular NMS.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
std::vector< unsigned int > m_BlockShape
Block shape value.
std::vector< int > m_Stride
Stride values for the input that will be sliced.
bool IsActivationSupported(const BackendId &backend, const TensorInfo &input, const TensorInfo &output, const ActivationDescriptor &descriptor, char *reasonIfUnsupported=nullptr, size_t reasonIfUnsupportedMaxLength=1024)
Deprecated in favor of IBackend and ILayerSupport interfaces.
An ActivationDescriptor for the ActivationLayer.
uint32_t m_NumInputs
Number of input tensors.
A SliceDescriptor for the SliceLayer.
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
std::unique_ptr< tflite::SubGraphT > SubgraphPtr
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
#define CHECK_TENSOR_PTR(TENSOR_PTR)
std::vector< uint32_t > m_vAxis
The indices of the dimensions to reduce.
float m_ScaleH
Center size encoding scale height.
std::vector< int > m_End
End values for the input that will be sliced.
A SpaceToBatchNdDescriptor for the SpaceToBatchNdLayer.
static TensorIdRawPtrVector GetSubgraphInputs(const ModelPtr &model, size_t subgraphIndex)
Struct for the users to pass backend specific options.
float m_A
Alpha upper bound value used by the activation functions. (BoundedReLu, Linear, TanH, Elu).
static TensorRawPtrVector GetInputs(const ModelPtr &model, size_t subgraphIndex, size_t operatorIndex)
uint32_t m_DilationX
Dilation along x axis.
const armnnSerializer::TensorInfo * TensorRawPtr
static TensorRawPtrVector GetOutputs(const ModelPtr &model, size_t subgraphIndex, size_t operatorIndex)
uint32_t m_PadLeft
Padding left value in the width dimension.
EmptyOptional is used to initialize the Optional class in case we want to have default value for an O...
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
static std::vector< int32_t > & GetOutputTensorIds(const ModelPtr &model, size_t subgraphIndex, size_t operatorIndex)
#define CHECK_SUPPORTED_FUSED_ACTIVATION(OPTION, SUBGRAPH_INDEX, OPERATOR_INDEX)
int32_t m_Axis
The axis in params to gather indices from.
A ElementwiseUnaryDescriptor for the ElementwiseUnaryLayer.
PoolingAlgorithm m_PoolType
The pooling algorithm to use (Max. Average, L2).
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
std::vector< std::pair< unsigned int, unsigned int > > m_Crops
The values to crop from the input dimension.
unsigned int GetNumDimensions() const
Function that returns the tensor rank.
OutputShapeRounding m_OutputShapeRounding
The rounding method for the output shape. (Floor, Ceiling).
void SetConcatAxis(unsigned int concatAxis)
Set the concatenation axis value.
virtual const IInputSlot & GetInputSlot(unsigned int index) const =0
Get a const input slot handle by slot index.
A MeanDescriptor for the MeanLayer.
armnn::BindingPointInfo BindingPointInfo
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
armnn::TensorInfo ToTensorInfo(TensorRawPtr tensorPtr)
uint32_t m_PadRight
Padding right value in the width dimension.
A TransposeDescriptor for the TransposeLayer.
A StridedSliceDescriptor for the StridedSliceLayer.
virtual const IOutputSlot & GetOutputSlot(unsigned int index) const =0
Get the const output slot handle by slot index.
int m_Axis
Axis to reduce across the input tensor.
virtual const char * GetName() const =0
Returns the name of the layer.
float m_ScaleY
Center size encoding scale y.
float m_NmsScoreThreshold
NMS score threshold.
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
virtual int Connect(IInputSlot &destination)=0
A Pooling2dDescriptor for the Pooling2dLayer.
std::vector< std::string > GetSubgraphInputTensorNames(size_t subgraphId) const
Return the input tensor names for a given subgraph.
unsigned int GetNumDimensions() const
#define CHECK_BUFFER_SIZE(BUFFER_PTR, TENSOR_INFO, BUFFER_ID)
float m_B
Beta lower bound value used by the activation functions. (BoundedReLu, Linear, TanH).
armnn::TensorShape Permuted(const armnn::TensorShape &srcShape, const armnn::PermutationVector &mappings)
A SoftmaxDescriptor for the SoftmaxLayer.
DataLayout::NCHW DataLayout::NCHW DataLayout::NHWC DataLayout::NHWC true
Status SetViewOriginCoord(uint32_t view, uint32_t coord, uint32_t value)
Set the view origin coordinates.
ActivationFunction m_Function
The activation function to use (Sigmoid, TanH, Linear, ReLu, BoundedReLu, SoftReLu, LeakyReLu, Abs, Sqrt, Square, Elu).
armnn::DataType m_Output_Type
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
A DepthwiseConvolution2dDescriptor for the DepthwiseConvolution2dLayer.
constexpr unsigned int MaxNumOfTensorDimensions
uint32_t m_PadLeft
Padding left value in the width dimension.
unsigned int GetNumElements() const
uint32_t m_PadRight
Padding right value in the width dimension.