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());
342 void CalcPadding(uint32_t inputSize,
346 uint32_t& paddingFront,
347 uint32_t& paddingBack,
348 tflite::Padding padding)
352 if (padding == tflite::Padding_SAME)
354 uint32_t outputSize = (inputSize + stride - 1) / stride;
355 uint32_t dilatedSize = filterSize + (dilation - 1) * (filterSize - 1);
356 uint32_t temp = (outputSize - 1) * stride + dilatedSize;
357 if (temp > inputSize)
359 paddingFront = (temp - inputSize) / 2;
360 paddingBack = (temp - inputSize) - paddingFront;
366 const std::vector<unsigned int>& shape,
367 const bool outputTensor =
false)
372 switch (tensorPtr->type)
374 case tflite::TensorType_UINT8:
377 case tflite::TensorType_FLOAT32:
380 case tflite::TensorType_INT8:
381 if (tensorPtr->quantization->zero_point.size() == 1)
392 case tflite::TensorType_INT16:
395 case tflite::TensorType_INT32:
398 case tflite::TensorType_INT64:
401 case tflite::TensorType_BOOL:
408 fmt::format(
"Unsupported data type {} = {} for tensor: {}. {}",
410 tflite::EnumNameTensorType(tensorPtr->type),
417 std::vector<unsigned int> safeShape = shape;
418 if (shape.size() == 0)
420 safeShape.push_back(1);
425 tensorShape =
TensorShape(armnn::numeric_cast<unsigned int>(safeShape.size()), safeShape.data());
429 size_t shapeSignatureSize = tensorPtr->shape_signature.size();
432 if (shapeSignatureSize != 0)
435 if (shapeSignatureSize != shape.size())
439 for (
unsigned int i = 0; i < shapeSignatureSize; ++i)
441 unsigned int dim = tensorPtr->shape_signature[i] > -1 ?
442 static_cast<unsigned int>(tensorPtr->shape_signature[i]) : 0;
443 safeShape.push_back(dim);
447 std::unique_ptr<bool[]> dimMask = std::make_unique<bool[]>(tensorPtr->shape_signature.size());
448 for (
unsigned int i = 0; i < tensorPtr->shape_signature.size(); ++i)
450 dimMask[i] = tensorPtr->shape_signature[i] == -1 ? false :
true;
452 tensorShape =
TensorShape(static_cast<unsigned int>(safeShape.size()), safeShape.data(), dimMask.get());
455 else if (shape.size() == 0)
461 tensorShape =
TensorShape(armnn::numeric_cast<unsigned int>(shape.size()), shape.data());
465 float quantizationScale = 0.0f;
466 int32_t quantizationOffset = 0;
468 if (tensorPtr->quantization.get())
470 if (tensorPtr->quantization->scale.size() <= 1)
475 if (tensorPtr->quantization->scale.size() == 1)
477 quantizationScale = tensorPtr->quantization->scale[0];
479 if (tensorPtr->quantization->zero_point.size() == 1)
494 std::vector<float> quantizationScales;
495 std::vector<int32_t> quantizationOffsets;
498 std::copy(tensorPtr->quantization->scale.begin(),
499 tensorPtr->quantization->scale.end(),
500 std::back_inserter(quantizationScales));
506 armnn::numeric_cast<unsigned int>(tensorPtr->quantization->quantized_dimension));
522 auto const & dimensions = AsUnsignedVector(tensorPtr->shape);
527 const bool outputTensor)
529 auto const & dimensions = AsUnsignedVector(tensorPtr->shape);
530 return ToTensorInfo(tensorPtr, dimensions, outputTensor);
534 std::pair<armnn::ConstTensor, std::unique_ptr<T[]>>
543 fmt::format(
"Buffer for buffer:{} is null", tensorPtr->buffer).c_str());
551 reinterpret_cast<const T*
>(bufferPtr->data.data()), data.get(),
sizeof(T));
555 ::memcpy(data.get(), bufferPtr->data.data(), tensorInfo.
GetNumBytes());
561 return std::make_pair(
ConstTensor(tensorInfo, data.get()), std::move(data));
574 if (actualSize != expected.size())
579 for (
unsigned int i = 0u; i < actualSize; i++)
581 if (expected[i] < 0 ||
582 actual[i] != static_cast<unsigned int>(expected[i]))
591 void CheckMatchingQuantization(
const TensorInfo& first,
593 const std::string& descName,
594 std::string
const& firstName,
595 std::string
const& secondName)
607 if (firstDataType != secondDataType)
610 " must be of the same quantized type, " +
618 " must have the same quantization space, " +
630 , m_Network(nullptr, nullptr)
634 m_ParserFunctions[tflite::BuiltinOperator_ABS] = &TfLiteParserImpl::ParseAbs;
635 m_ParserFunctions[tflite::BuiltinOperator_ADD] = &TfLiteParserImpl::ParseAdd;
636 m_ParserFunctions[tflite::BuiltinOperator_ARG_MIN] = &TfLiteParserImpl::ParseArgMin;
637 m_ParserFunctions[tflite::BuiltinOperator_ARG_MAX] = &TfLiteParserImpl::ParseArgMax;
638 m_ParserFunctions[tflite::BuiltinOperator_AVERAGE_POOL_2D] = &TfLiteParserImpl::ParseAveragePool2D;
639 m_ParserFunctions[tflite::BuiltinOperator_BATCH_TO_SPACE_ND] = &TfLiteParserImpl::ParseBatchToSpaceND;
640 m_ParserFunctions[tflite::BuiltinOperator_CAST] = &TfLiteParserImpl::ParseCast;
641 m_ParserFunctions[tflite::BuiltinOperator_CONCATENATION] = &TfLiteParserImpl::ParseConcatenation;
642 m_ParserFunctions[tflite::BuiltinOperator_CONV_2D] = &TfLiteParserImpl::ParseConv2D;
643 m_ParserFunctions[tflite::BuiltinOperator_CUSTOM] = &TfLiteParserImpl::ParseCustomOperator;
644 m_ParserFunctions[tflite::BuiltinOperator_DEPTH_TO_SPACE] = &TfLiteParserImpl::ParseDepthToSpace;
645 m_ParserFunctions[tflite::BuiltinOperator_DEPTHWISE_CONV_2D] = &TfLiteParserImpl::ParseDepthwiseConv2D;
646 m_ParserFunctions[tflite::BuiltinOperator_DEQUANTIZE] = &TfLiteParserImpl::ParseDequantize;
647 m_ParserFunctions[tflite::BuiltinOperator_DIV] = &TfLiteParserImpl::ParseDiv;
648 m_ParserFunctions[tflite::BuiltinOperator_ELU] = &TfLiteParserImpl::ParseElu;
649 m_ParserFunctions[tflite::BuiltinOperator_EQUAL] = &TfLiteParserImpl::ParseEqual;
650 m_ParserFunctions[tflite::BuiltinOperator_EXP] = &TfLiteParserImpl::ParseExp;
651 m_ParserFunctions[tflite::BuiltinOperator_EXPAND_DIMS] = &TfLiteParserImpl::ParseExpandDims;
652 m_ParserFunctions[tflite::BuiltinOperator_FULLY_CONNECTED] = &TfLiteParserImpl::ParseFullyConnected;
653 m_ParserFunctions[tflite::BuiltinOperator_GATHER] = &TfLiteParserImpl::ParseGather;
654 m_ParserFunctions[tflite::BuiltinOperator_GREATER] = &TfLiteParserImpl::ParseGreater;
655 m_ParserFunctions[tflite::BuiltinOperator_GREATER_EQUAL] = &TfLiteParserImpl::ParseGreaterOrEqual;
656 m_ParserFunctions[tflite::BuiltinOperator_HARD_SWISH] = &TfLiteParserImpl::ParseHardSwish;
657 m_ParserFunctions[tflite::BuiltinOperator_LEAKY_RELU] = &TfLiteParserImpl::ParseLeakyRelu;
658 m_ParserFunctions[tflite::BuiltinOperator_LESS] = &TfLiteParserImpl::ParseLess;
659 m_ParserFunctions[tflite::BuiltinOperator_LESS_EQUAL] = &TfLiteParserImpl::ParseLessOrEqual;
660 m_ParserFunctions[tflite::BuiltinOperator_LOGICAL_NOT] = &TfLiteParserImpl::ParseLogicalNot;
661 m_ParserFunctions[tflite::BuiltinOperator_LOGISTIC] = &TfLiteParserImpl::ParseLogistic;
662 m_ParserFunctions[tflite::BuiltinOperator_L2_NORMALIZATION] = &TfLiteParserImpl::ParseL2Normalization;
663 m_ParserFunctions[tflite::BuiltinOperator_MAX_POOL_2D] = &TfLiteParserImpl::ParseMaxPool2D;
664 m_ParserFunctions[tflite::BuiltinOperator_MAXIMUM] = &TfLiteParserImpl::ParseMaximum;
665 m_ParserFunctions[tflite::BuiltinOperator_MEAN] = &TfLiteParserImpl::ParseMean;
666 m_ParserFunctions[tflite::BuiltinOperator_MINIMUM] = &TfLiteParserImpl::ParseMinimum;
667 m_ParserFunctions[tflite::BuiltinOperator_MUL] = &TfLiteParserImpl::ParseMul;
668 m_ParserFunctions[tflite::BuiltinOperator_NEG] = &TfLiteParserImpl::ParseNeg;
669 m_ParserFunctions[tflite::BuiltinOperator_NOT_EQUAL] = &TfLiteParserImpl::ParseNotEqual;
670 m_ParserFunctions[tflite::BuiltinOperator_PACK] = &TfLiteParserImpl::ParsePack;
671 m_ParserFunctions[tflite::BuiltinOperator_PAD] = &TfLiteParserImpl::ParsePad;
672 m_ParserFunctions[tflite::BuiltinOperator_PRELU] = &TfLiteParserImpl::ParsePrelu;
673 m_ParserFunctions[tflite::BuiltinOperator_QUANTIZE] = &TfLiteParserImpl::ParseQuantize;
674 m_ParserFunctions[tflite::BuiltinOperator_RELU] = &TfLiteParserImpl::ParseRelu;
675 m_ParserFunctions[tflite::BuiltinOperator_RELU6] = &TfLiteParserImpl::ParseRelu6;
676 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MAX] = &TfLiteParserImpl::ParseReduceMax;
677 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MIN] = &TfLiteParserImpl::ParseReduceMin;
678 m_ParserFunctions[tflite::BuiltinOperator_RESHAPE] = &TfLiteParserImpl::ParseReshape;
679 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_BILINEAR] = &TfLiteParserImpl::ParseResizeBilinear;
680 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_NEAREST_NEIGHBOR] = &TfLiteParserImpl::ParseResizeNearestNeighbor;
681 m_ParserFunctions[tflite::BuiltinOperator_RSQRT] = &TfLiteParserImpl::ParseRsqrt;
682 m_ParserFunctions[tflite::BuiltinOperator_SHAPE] = &TfLiteParserImpl::ParseShape;
683 m_ParserFunctions[tflite::BuiltinOperator_SLICE] = &TfLiteParserImpl::ParseSlice;
684 m_ParserFunctions[tflite::BuiltinOperator_SOFTMAX] = &TfLiteParserImpl::ParseSoftmax;
685 m_ParserFunctions[tflite::BuiltinOperator_SPACE_TO_BATCH_ND] = &TfLiteParserImpl::ParseSpaceToBatchND;
686 m_ParserFunctions[tflite::BuiltinOperator_SPLIT] = &TfLiteParserImpl::ParseSplit;
687 m_ParserFunctions[tflite::BuiltinOperator_SPLIT_V] = &TfLiteParserImpl::ParseSplitV;
688 m_ParserFunctions[tflite::BuiltinOperator_SQUEEZE] = &TfLiteParserImpl::ParseSqueeze;
689 m_ParserFunctions[tflite::BuiltinOperator_STRIDED_SLICE] = &TfLiteParserImpl::ParseStridedSlice;
690 m_ParserFunctions[tflite::BuiltinOperator_SUB] = &TfLiteParserImpl::ParseSub;
691 m_ParserFunctions[tflite::BuiltinOperator_SUM] = &TfLiteParserImpl::ParseSum;
692 m_ParserFunctions[tflite::BuiltinOperator_TANH] = &TfLiteParserImpl::ParseTanH;
693 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE] = &TfLiteParserImpl::ParseTranspose;
694 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE_CONV] = &TfLiteParserImpl::ParseTransposeConv;
695 m_ParserFunctions[tflite::BuiltinOperator_UNPACK] = &TfLiteParserImpl::ParseUnpack;
698 m_CustomParserFunctions[
"TFLite_Detection_PostProcess"] = &TfLiteParserImpl::ParseDetectionPostProcess;
701 void TfLiteParserImpl::ResetParser()
705 m_SubgraphConnections.clear();
712 return CreateNetworkFromModel();
719 return CreateNetworkFromModel();
726 m_Model = std::move(model);
728 return CreateNetworkFromModel();
731 INetworkPtr TfLiteParserImpl::CreateNetworkFromModel()
736 if (m_Options && m_Options.value().m_InferAndValidate)
740 {
"InferAndValidate",
true }
743 networkOptions.push_back(shapeInferenceMethodOption);
746 m_Network = INetwork::Create(networkOptions);
749 if (m_Model->subgraphs.size() != 1)
752 fmt::format(
"Current TfLite parser only supports 1 subgraph. Current one has: {} {}",
753 m_Model->subgraphs.size(),
757 size_t subgraphIndex = 0;
758 size_t operatorIndex = 0;
761 for (
SubgraphPtr const& subgraph : m_Model->subgraphs)
763 m_SubgraphConnections.emplace_back(subgraph->tensors.size());
766 auto const& opCodePtr = m_Model->operator_codes[op->opcode_index];
767 auto builtinCode = opCodePtr->builtin_code;
769 if (builtinCode > tflite::BuiltinOperator_MAX)
771 throw ParseException(fmt::format(
"Operator code {} is out of range 0-{}. " 772 "subgraph:{} operator idx:{}. {}",
773 builtinCode, tflite::BuiltinOperator_MAX, subgraphIndex,
778 auto& parserFunction = m_ParserFunctions[builtinCode];
779 (this->*parserFunction)(subgraphIndex, operatorIndex);
783 SetupInputLayers(subgraphIndex);
784 SetupOutputLayers(subgraphIndex);
785 SetupConstantLayers(subgraphIndex);
793 std::stringstream errorString;
794 errorString <<
"Failed to parse operator #" << operatorIndex <<
" within subgraph #" 795 << subgraphIndex <<
" error: " << e.
what();
797 std::stringstream errors;
798 errors << errorString.str() <<
"\n";
803 for (subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
805 for (
size_t tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
807 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot !=
nullptr)
809 for (
size_t inputSlotIdx = 0;
810 inputSlotIdx < m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size();
813 m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot->Connect(
814 *(m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots[inputSlotIdx]));
820 return std::move(m_Network);
823 void TfLiteParserImpl::RegisterProducerOfTensor(
size_t subgraphIndex,
828 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
829 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
831 TensorSlots & tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
834 if (tensorSlots.outputSlot !=
nullptr)
836 throw ParseException(fmt::format(
"Another layer has already registered itself as the producer of " 837 "subgraph:{} tensor:{} {}",
843 tensorSlots.outputSlot = slot;
846 void TfLiteParserImpl::RegisterConsumerOfTensor(
size_t subgraphIndex,
851 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
852 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
854 TensorSlots& tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
855 tensorSlots.inputSlots.push_back(slot);
858 void TfLiteParserImpl::ParseCustomOperator(
size_t subgraphIndex,
size_t operatorIndex)
860 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
863 auto customParserFunction = &TfLiteParserImpl::ParseUnsupportedOperator;
866 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
867 const auto& customCode = m_Model->operator_codes[operatorPtr->opcode_index]->custom_code;
870 auto iterator = m_CustomParserFunctions.find(customCode);
871 if (iterator != m_CustomParserFunctions.end())
873 customParserFunction = iterator->second;
877 (this->*customParserFunction)(subgraphIndex, operatorIndex);
880 void TfLiteParserImpl::ParseUnsupportedOperator(
size_t subgraphIndex,
size_t operatorIndex)
882 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
884 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
886 auto opcodeIndex = operatorPtr->opcode_index;
887 auto opcode = m_Model->operator_codes[opcodeIndex]->builtin_code;
889 if (!m_Options || !m_Options.value().m_StandInLayerForUnsupported)
893 fmt::format(
"Operator not supported. " 894 "subgraph:{} operator:{} " 895 "opcode_index:{} opcode:{} / {} {}",
900 tflite::EnumNameBuiltinOperator(opcode),
904 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
905 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
911 auto layerName = fmt::format(
"StandIn:{}:{}:{}", subgraphIndex, operatorIndex, opcode);
914 IConnectableLayer* layer = m_Network->AddStandInLayer(descriptor, layerName.c_str());
917 for (
unsigned int i = 0u; i < numOutputs; ++i)
922 auto inputTensorIds = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
923 auto outputTensorIds = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
925 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIds);
926 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIds);
929 void TfLiteParserImpl::ParseCast(
size_t subgraphIndex,
size_t operatorIndex)
931 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
933 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
935 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
938 auto layerName = fmt::format(
"Cast:{}:{}", subgraphIndex, operatorIndex);
946 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
947 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
949 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
950 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
953 void TfLiteParserImpl::ParseConv2D(
size_t subgraphIndex,
size_t operatorIndex)
955 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
957 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
958 const auto * options = operatorPtr->builtin_options.AsConv2DOptions();
970 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
973 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
980 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
981 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
985 unsigned int filterHeight = filterTensorInfo.
GetShape()[1];
986 unsigned int filterWidth = filterTensorInfo.
GetShape()[2];
988 CalcPadding(inputHeight, filterHeight, desc.
m_StrideY,
990 CalcPadding(inputWidth, filterWidth, desc.
m_StrideX,
993 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo);
996 auto layerName = fmt::format(
"Conv2D:{}:{}", subgraphIndex, operatorIndex);
998 if (inputs.size() == 3)
1002 auto biasTensorAndData = CreateConstTensorNonPermuted(inputs[2], biasTensorInfo);
1003 layer = m_Network->AddConvolution2dLayer(desc,
1004 filterTensorAndData,
1010 layer = m_Network->AddConvolution2dLayer(desc,
1011 filterTensorAndData,
1023 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1024 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1026 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1028 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1029 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1032 void TfLiteParserImpl::ParseDepthwiseConv2D(
size_t subgraphIndex,
size_t operatorIndex)
1034 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1036 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1037 const auto * options = operatorPtr->builtin_options.AsDepthwiseConv2DOptions();
1048 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1050 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1059 unsigned int inputHeight = inputTensorInfo.
GetShape()[1];
1060 unsigned int inputWidth = inputTensorInfo.
GetShape()[2];
1063 unsigned int filterHeight = filterTensorInfo.
GetShape()[1];
1064 unsigned int filterWidth = filterTensorInfo.
GetShape()[2];
1066 CalcPadding(inputHeight, filterHeight, desc.
m_StrideY,
1068 CalcPadding(inputWidth, filterWidth, desc.
m_StrideX,
1072 auto filterTensor = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo);
1074 auto layerName = fmt::format(
"DepthwiseConv2D:{}:{}", subgraphIndex, operatorIndex);
1076 if (inputs.size() == 3)
1080 auto biasTensorAndData = CreateConstTensorNonPermuted(inputs[2], biasTensorInfo);
1081 layer = m_Network->AddDepthwiseConvolution2dLayer(desc,
1088 layer = m_Network->AddDepthwiseConvolution2dLayer(desc,
1100 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1101 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1103 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1105 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1106 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1109 void TfLiteParserImpl::ParseDequantize(
size_t subgraphIndex,
size_t operatorIndex)
1111 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1113 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1116 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1119 auto layerName = fmt::format(
"Dequantize:{}:{}", subgraphIndex, operatorIndex);
1127 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1128 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1130 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1131 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1134 void TfLiteParserImpl::ParseExpandDims(
size_t subgraphIndex,
size_t operatorIndex)
1136 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1138 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1141 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1144 auto layerName = fmt::format(
"ExpandDims:{}:{}", subgraphIndex, operatorIndex);
1149 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1159 int32_t axis = inputs[1]->shape[0];
1163 if (axis > inputDimSize || axis < 0 - (inputDimSize + 1))
1165 throw ParseException(
"axis must be in range [0 - (inputDimSize + 1), inputDimSize] inclusive");
1170 axis = inputDimSize + axis + 1;
1173 std::vector<unsigned int> shape(static_cast<unsigned int>(inputDimSize) + 1);
1174 unsigned int inputShapeIndex = 0;
1175 for (
unsigned int i = 0; i < static_cast<unsigned int>(inputDimSize + 1); ++i)
1177 if (i == static_cast<unsigned int>(axis))
1183 shape[i] = inputTensorInfo.
GetShape()[inputShapeIndex];
1191 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
1193 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1195 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1196 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1198 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1199 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1202 void TfLiteParserImpl::ParseTranspose(
size_t subgraphIndex,
size_t operatorIndex)
1204 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1206 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1209 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1212 auto layerName = fmt::format(
"Transpose:{}:{}", subgraphIndex, operatorIndex);
1215 if (inputs.size() == 2)
1220 std::vector<unsigned int> permuteShape(numPermVecElements);
1221 ::memcpy(permuteShape.data(), permuteBufferPtr->data.data(), permuteTensorInfo.
GetNumBytes());
1229 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1231 IConnectableLayer* layer = m_Network->AddTransposeLayer(desc, layerName.c_str());
1235 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1236 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1238 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1239 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1242 void TfLiteParserImpl::ParseTransposeConv(
size_t subgraphIndex,
size_t operatorIndex)
1244 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1246 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1247 const auto * options = operatorPtr->builtin_options.AsTransposeConvOptions();
1255 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1256 if (inputs.size() == 4)
1265 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1272 if (tensorInfo.
GetDataType() == DataType::Signed32)
1274 ::memcpy(output_shape.data(),
GetBuffer(m_Model, inputs[0]->buffer)->data.data(), tensorInfo.
GetNumBytes());
1276 if (tensorInfo.
GetDataType() == DataType::QAsymmU8)
1280 output_shape[i] =
GetBuffer(m_Model, inputs[0]->buffer)->data.data()[i];
1284 for (
int dimension : output_shape)
1286 desc.
m_OutputShape.push_back(static_cast<unsigned int>(dimension));
1294 const unsigned int inputHeight = inputTensorInfo.
GetShape()[1];
1295 const unsigned int inputWidth = inputTensorInfo.
GetShape()[2];
1297 const unsigned int filterHeight = filterTensorInfo.
GetShape()[1];
1298 const unsigned int filterWidth = filterTensorInfo.
GetShape()[2];
1300 CalcPadding(inputHeight,
1308 CalcPadding(inputWidth,
1316 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo);
1319 auto layerName = fmt::format(
"TransposeConv:{}:{}", subgraphIndex, operatorIndex);
1324 auto biasConstTensor = CreateConstTensorNonPermuted(inputs[3], biasTensorInfo);
1325 layer = m_Network->AddTransposeConvolution2dLayer(desc,
1326 filterTensorAndData,
1332 layer = m_Network->AddTransposeConvolution2dLayer(desc,
1333 filterTensorAndData,
1344 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1345 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[2]});
1347 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1348 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1351 void TfLiteParserImpl::ParseAveragePool2D(
size_t subgraphIndex,
size_t operatorIndex)
1353 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Average);
1356 void TfLiteParserImpl::ParseBatchToSpaceND(
size_t subgraphIndex,
size_t operatorIndex)
1358 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1360 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1363 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1372 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1373 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1375 std::vector<unsigned int> cropsVector(cropsTensorInfo.
GetNumElements());
1376 ::memcpy(cropsVector.data(), cropsBufferPtr->data.data(), cropsTensorInfo.
GetNumBytes());
1379 std::vector<std::pair<unsigned int, unsigned int>> crops;
1380 for (
unsigned int i = 0; i < cropsTensorInfo.
GetNumElements() / step; ++i)
1382 crops.emplace_back(cropsVector[i * step], cropsVector[i * step + 1]);
1390 auto layerName = fmt::format(
"BatchToSpaceND:{}:{}", subgraphIndex, operatorIndex);
1394 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1396 IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(desc, layerName.c_str());
1400 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1401 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1403 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1404 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1407 void TfLiteParserImpl::ParseL2Normalization(
size_t subgraphIndex,
size_t operatorIndex)
1409 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1411 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1414 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1419 auto layerName = fmt::format(
"L2Normalization:{}:{}", subgraphIndex, operatorIndex);
1420 IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(desc, layerName.c_str());
1427 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1428 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1430 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1431 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1434 void TfLiteParserImpl::ParseMaxPool2D(
size_t subgraphIndex,
size_t operatorIndex)
1436 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Max);
1439 void TfLiteParserImpl::ParseMaximum(
size_t subgraphIndex,
size_t operatorIndex)
1441 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1443 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1446 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1449 auto layerName = fmt::format(
"Maximum:{}:{}", subgraphIndex, operatorIndex);
1453 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName,
"Input 0",
"Input 1");
1456 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1462 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1463 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1465 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1466 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1469 void TfLiteParserImpl::ParseMinimum(
size_t subgraphIndex,
size_t operatorIndex)
1471 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1473 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1476 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1479 auto layerName = fmt::format(
"Minimum:{}:{}", subgraphIndex, operatorIndex);
1483 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName,
"Input 0",
"Input 1");
1486 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1492 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1493 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1495 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1496 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1499 void TfLiteParserImpl::ParsePool(
size_t subgraphIndex,
1500 size_t operatorIndex,
1503 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1505 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1506 const auto * options = operatorPtr->builtin_options.AsPool2DOptions();
1510 std::string layerName;
1514 case PoolingAlgorithm::Average:
1516 fmt::format(
"AveragePool2D:{}:{}", subgraphIndex, operatorIndex);
1518 case PoolingAlgorithm::Max:
1520 fmt::format(
"MaxPool2D:{}:{}", subgraphIndex, operatorIndex);
1537 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1542 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1543 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1550 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1554 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1556 IConnectableLayer* layer = m_Network->AddPooling2dLayer(desc, layerName.c_str());
1562 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1563 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1565 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1567 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1568 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1571 void TfLiteParserImpl::ParseSlice(
size_t subgraphIndex,
size_t operatorIndex)
1573 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1575 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1577 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1586 std::vector<unsigned int> begin(beginTensorInfo.
GetNumElements());
1587 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.
GetNumBytes());
1593 std::vector<unsigned int> size(sizeTensorInfo.GetNumElements());
1594 ::memcpy(size.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
1597 auto layerName = fmt::format(
"Slice:{}:{}", subgraphIndex, operatorIndex);
1601 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1603 IConnectableLayer*
const layer = m_Network->AddSliceLayer(desc, layerName.c_str());
1608 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1609 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1612 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1613 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1616 void TfLiteParserImpl::ParseSoftmax(
size_t subgraphIndex,
size_t operatorIndex)
1618 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1619 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1620 const auto * options = operatorPtr->builtin_options.AsSoftmaxOptions();
1623 desc.
m_Beta = options->beta;
1625 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1627 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1630 auto layerName = fmt::format(
"Softmax:{}:{}", subgraphIndex, operatorIndex);
1631 IConnectableLayer*
const layer = m_Network->AddSoftmaxLayer(desc, layerName.c_str());
1638 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1639 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1642 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1643 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1646 void TfLiteParserImpl::ParseSpaceToBatchND(
size_t subgraphIndex,
size_t operatorIndex)
1648 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1650 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1653 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1662 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1663 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1665 std::vector<unsigned int> padListVector(padListTensorInfo.
GetNumElements());
1666 ::memcpy(padListVector.data(), padListBufferPtr->data.data(), padListTensorInfo.
GetNumBytes());
1669 std::vector<std::pair<unsigned int, unsigned int>> padList;
1670 for (
unsigned int i = 0; i < padListTensorInfo.
GetNumElements() / step; ++i)
1672 padList.emplace_back(padListVector[i * step], padListVector[i * step + 1]);
1680 auto layerName = fmt::format(
"SpaceToBatchND:{}:{}", subgraphIndex, operatorIndex);
1684 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1686 IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(desc, layerName.c_str());
1690 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1691 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1693 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1694 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1701 static const uint32_t dimensionSequence[] = { 0, 1, 2, 3 };
1705 std::stringstream ss;
1706 ss <<
"Input tensor has unexpected number of dimensions:" << inputTensorInfo.
GetNumDimensions()
1707 <<
" shape:" << inputTensorInfo.
GetShape() <<
" " 1712 if (squeezeDims.empty())
1714 squeezeDims.assign(dimensionSequence,
1718 std::vector<uint32_t> outputDims;
1721 bool skipSqueeze = (std::find(squeezeDims.begin(), squeezeDims.end(), i) == squeezeDims.end());
1722 auto currentDimension = inputTensorInfo.
GetShape()[i];
1723 if (skipSqueeze || currentDimension != 1)
1725 outputDims.push_back(currentDimension);
1729 if (outputDims.size() > 4)
1731 std::stringstream ss;
1732 ss <<
"Output tensor has unexpected number of dimensions:" << inputTensorInfo.
GetNumDimensions()
1733 <<
" shape:" << inputTensorInfo.
GetShape() <<
" " 1745 return outTensorInfo;
1748 void TfLiteParserImpl::ParseShape(
size_t subgraphIndex,
size_t operatorIndex)
1750 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1752 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1754 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1757 auto layerName = fmt::format(
"Shape:{}:{}", subgraphIndex, operatorIndex);
1772 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
1776 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1777 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1779 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1780 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1783 void TfLiteParserImpl::ParseSqueeze(
size_t subgraphIndex,
size_t operatorIndex)
1785 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1787 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1790 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1793 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1794 const auto * options = operatorPtr->builtin_options.AsSqueezeOptions();
1795 auto layerName = fmt::format(
"Squeeze:{}:{}", subgraphIndex, operatorIndex);
1799 std::vector<uint32_t> squeezeDim;
1802 if (options->squeeze_dims.size() == 1 && options->squeeze_dims[0] < 0)
1805 squeezeDim.push_back(static_cast<uint32_t>(dim));
1809 squeezeDim = AsUnsignedVector(options->squeeze_dims);
1814 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1819 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
1823 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1824 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1826 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1827 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1830 void TfLiteParserImpl::ParseStridedSlice(
size_t subgraphIndex,
size_t operatorIndex)
1832 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1834 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1837 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1840 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1841 const auto * options = operatorPtr->builtin_options.AsStridedSliceOptions();
1855 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.
GetNumBytes());
1860 std::vector<int> end(endTensorInfo.GetNumElements());
1861 ::memcpy(end.data(), endBufferPtr->data.data(), endTensorInfo.GetNumBytes());
1866 std::vector<int> stride(strideTensorInfo.GetNumElements());
1867 ::memcpy(stride.data(), strideBufferPtr->data.data(), strideTensorInfo.GetNumBytes());
1873 auto layerName = fmt::format(
"StridedSlice:{}:{}", subgraphIndex, operatorIndex);
1874 IConnectableLayer* layer = m_Network->AddStridedSliceLayer(desc, layerName.c_str());
1880 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1881 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1883 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1884 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1887 void TfLiteParserImpl::ParseSub(
size_t subgraphIndex,
size_t operatorIndex)
1889 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1891 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1892 const auto * options = operatorPtr->builtin_options.AsSubOptions();
1894 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1897 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1903 auto layerName = fmt::format(
"Sub:{}:{}", subgraphIndex, operatorIndex);
1910 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1911 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1913 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1915 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1916 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1919 void TfLiteParserImpl::ParseDiv(
size_t subgraphIndex,
size_t operatorIndex)
1921 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1923 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1924 const auto * options = operatorPtr->builtin_options.AsDivOptions();
1926 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1929 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1935 auto layerName = fmt::format(
"Div:{}:{}", subgraphIndex, operatorIndex);
1942 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1943 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1944 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1946 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1947 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1950 void TfLiteParserImpl::ParseAdd(
size_t subgraphIndex,
size_t operatorIndex)
1952 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1954 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1955 const auto * options = operatorPtr->builtin_options.AsAddOptions();
1957 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1960 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1966 auto layerName = fmt::format(
"Add:{}:{}", subgraphIndex, operatorIndex);
1973 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1974 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1975 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1977 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1978 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1981 void TfLiteParserImpl::ParseMul(
size_t subgraphIndex,
size_t operatorIndex)
1983 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1985 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1986 const auto * options = operatorPtr->builtin_options.AsMulOptions();
1988 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1991 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1997 auto layerName = fmt::format(
"Mul:{}:{}", subgraphIndex, operatorIndex);
1998 IConnectableLayer* layer = m_Network->AddMultiplicationLayer(layerName.c_str());
2004 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2005 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2006 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2008 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2009 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2012 void TfLiteParserImpl::ParseMean(
size_t subgraphIndex,
size_t operatorIndex)
2014 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2016 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2018 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2025 std::vector<unsigned int> axis(dimTensorInfo.GetNumElements());
2026 ::memcpy(axis.data(), bufferPtr->data.data(), dimTensorInfo.GetNumBytes());
2036 auto layerName = fmt::format(
"Mean:{}:{}", subgraphIndex, operatorIndex);
2042 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2043 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2045 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2046 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2049 void TfLiteParserImpl::ParsePad(
size_t subgraphIndex,
size_t operatorIndex)
2051 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2063 std::vector<unsigned int> padBuffer(padTensorInfo.
GetNumElements());
2064 ::memcpy(padBuffer.data(), bufferPtr->data.data(), padTensorInfo.
GetNumBytes());
2068 if (inputTensorInfo.IsQuantized())
2070 desc.
m_PadValue =
static_cast<float>(inputTensorInfo.GetQuantizationOffset());
2072 for (
unsigned int i = 0; i < padTensorInfo.
GetNumElements() / step; ++i)
2074 desc.
m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
2077 auto layerName = fmt::format(
"Pad:{}:{}", subgraphIndex, operatorIndex);
2084 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2085 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2087 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2088 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2091 void TfLiteParserImpl::ParsePrelu(
size_t subgraphIndex,
size_t operatorIndex)
2093 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2095 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2098 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2101 auto layerName = fmt::format(
"Prelu:{}:{}", subgraphIndex, operatorIndex);
2106 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
2112 if (IsConstTensor(inputs[1]))
2114 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2116 RegisterConsumerOfTensor(subgraphIndex, inputTensorIndexes[0], slot);
2118 auto alphaTensorAndData = CreateConstTensorNonPermuted(inputs[1], alphaTensorInfo);
2119 std::string constLayerName = fmt::format(
"Constant:{}", inputs[1]->name);
2121 m_Network->AddConstantLayer(alphaTensorAndData, constLayerName.c_str());
2126 RegisterOutputSlots(subgraphIndex,
2127 VIRTUAL_OPERATOR_ID,
2129 { inputTensorIndexes[1] });
2133 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2134 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIndexes);
2137 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2138 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2141 void TfLiteParserImpl::ParseQuantize(
size_t subgraphIndex,
size_t operatorIndex)
2143 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2145 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2148 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2151 auto layerName = fmt::format(
"Quantize:{}:{}", subgraphIndex, operatorIndex);
2159 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2160 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2162 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2163 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2166 void TfLiteParserImpl::ParseRelu(
size_t subgraphIndex,
size_t operatorIndex)
2168 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::ReLu);
2171 void TfLiteParserImpl::ParseRelu6(
size_t subgraphIndex,
size_t operatorIndex)
2173 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::BoundedReLu);
2176 void TfLiteParserImpl::ParseLeakyRelu(
size_t subgraphIndex,
size_t operatorIndex)
2178 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::LeakyReLu);
2181 void TfLiteParserImpl::ParseLogistic(
size_t subgraphIndex,
size_t operatorIndex)
2183 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::Sigmoid);
2186 void TfLiteParserImpl::ParseTanH(
size_t subgraphIndex,
size_t operatorIndex)
2188 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::TanH);
2191 void TfLiteParserImpl::ParseElu(
size_t subgraphIndex,
size_t operatorIndex)
2193 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::Elu);
2196 void TfLiteParserImpl::ParseHardSwish(
size_t subgraphIndex,
size_t operatorIndex)
2198 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::HardSwish);
2201 void TfLiteParserImpl::ParseActivation(
size_t subgraphIndex,
size_t operatorIndex,
ActivationFunction activationType)
2203 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2204 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2207 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2210 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2213 auto layerName = fmt::format(
"Activation:");
2217 switch (activationType)
2219 case ActivationFunction::ReLu:
2221 layerName += fmt::format(
"RELU:{}:{}", subgraphIndex, operatorIndex);
2224 case ActivationFunction::BoundedReLu:
2226 layerName += fmt::format(
"RELU6:{}:{}", subgraphIndex, operatorIndex);
2227 activationDesc.
m_A = 6.0f;
2228 activationDesc.
m_B = 0.0f;
2231 case ActivationFunction::Sigmoid:
2233 layerName += fmt::format(
"SIGMOID:{}:{}", subgraphIndex, operatorIndex);
2236 case ActivationFunction::TanH:
2238 layerName += fmt::format(
"TANH:{}:{}", subgraphIndex, operatorIndex);
2239 activationDesc.
m_A = 1.0f;
2240 activationDesc.
m_B = 1.0f;
2243 case ActivationFunction::LeakyReLu:
2245 layerName += fmt::format(
"LEAKYRELU:{}:{}", subgraphIndex, operatorIndex);
2246 const auto * options = operatorPtr->builtin_options.AsLeakyReluOptions();
2247 activationDesc.
m_A = options->alpha;
2250 case ActivationFunction::Elu:
2252 layerName += fmt::format(
"ELU:{}:{}", subgraphIndex, operatorIndex);
2253 activationDesc.
m_A = 1.0f;
2256 case ActivationFunction::HardSwish:
2258 layerName += fmt::format(
"HARDSWISH:{}:{}", subgraphIndex, operatorIndex);
2264 fmt::format(
"Unexpected ActivationFunction[{}] when creating layerName {} ",
2269 IConnectableLayer*
const layer = m_Network->AddActivationLayer(activationDesc, layerName.c_str());
2276 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2277 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2280 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2281 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2284 const std::vector<int32_t> & targetDimsIn)
2286 std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
2287 const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
2289 if (stretchDim != targetDimsIn.end())
2291 if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
2294 fmt::format(
"At most one component of shape can be -1 {}",
CHECK_LOCATION().AsString()));
2297 auto targetNumElements =
2299 std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
2301 auto stretchIndex =
static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
2302 outputDims[stretchIndex] = inputTensorInfo.
GetNumElements() / targetNumElements;
2313 void TfLiteParserImpl::ParseReshape(
size_t subgraphIndex,
size_t operatorIndex)
2315 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2317 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2319 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2322 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2323 const auto * options = operatorPtr->builtin_options.AsReshapeOptions();
2324 auto layerName = fmt::format(
"Reshape:{}:{}", subgraphIndex, operatorIndex);
2328 CheckMatchingQuantization(inputTensorInfo, actualOutputTensorInfo, layerName,
"Input 0",
"Output 0");
2334 std::vector<int32_t> targetShape;
2335 bool targetShapeFound =
false;
2337 if (options !=
nullptr)
2340 if (options->new_shape.empty() ==
false)
2342 targetShape = options->new_shape;
2343 targetShapeFound =
true;
2348 if (!targetShapeFound)
2351 if (inputs.size() > 1 && inputs[1] !=
nullptr)
2353 if (inputs[1]->is_variable)
2358 if (inputs[1]->shape.size() != 1)
2363 if (inputs[1]->type != tflite::TensorType_INT32)
2369 auto bufferPtr =
GetBuffer(m_Model, inputs[1]->buffer);
2370 auto values =
reinterpret_cast<const int32_t*
>(bufferPtr->data.data());
2375 for (
int i=0; i < inputs[1]->shape[0]; ++i)
2377 targetShape.push_back(values[i]);
2383 "At least one method required");
2392 if (inputs.size() > 1 && !
CheckShape(reshapeOutputTensorShape, outputs[0]->shape))
2394 std::stringstream ss;
2395 ss <<
"New shape defined in reshape parameters " 2396 << reshapeOutputTensorShape
2397 <<
" does not equal output shape " 2398 << actualOutputTensorInfo.
GetShape()
2407 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
2411 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2412 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2414 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2415 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2418 void TfLiteParserImpl::ParseResizeBilinear(
size_t subgraphIndex,
size_t operatorIndex)
2420 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::Bilinear);
2423 void TfLiteParserImpl::ParseResizeNearestNeighbor(
size_t subgraphIndex,
size_t operatorIndex)
2425 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::NearestNeighbor);
2428 void TfLiteParserImpl::ParseResize(
size_t subgraphIndex,
size_t operatorIndex,
ResizeMethod resizeMethod)
2430 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2432 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2435 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2441 std::vector<int32_t> sizeTensorData(sizeTensorInfo.GetNumElements());
2444 ::memcpy(sizeTensorData.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
2448 desc.m_TargetHeight =
static_cast<uint32_t
> (sizeTensorData[0]);
2449 desc.m_TargetWidth =
static_cast<uint32_t
> (sizeTensorData[1]);
2452 auto layerName = fmt::format(
"Resize:");
2454 switch (resizeMethod)
2456 case ResizeMethod::Bilinear:
2458 layerName += fmt::format(
"BILINEAR:{}:{}", subgraphIndex, operatorIndex);
2460 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2461 const auto * options = operatorPtr->builtin_options.AsResizeBilinearOptions();
2463 desc.m_AlignCorners = options->align_corners;
2466 case ResizeMethod::NearestNeighbor:
2468 layerName += fmt::format(
"NEARESTNEIGHBOR:{}:{}", subgraphIndex, operatorIndex);
2474 fmt::format(
"Unexpected ResizeMethod[{}] when creating layerName {} ",
2481 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
2487 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2488 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2490 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2491 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2494 void TfLiteParserImpl::ParseConcatenation(
size_t subgraphIndex,
size_t operatorIndex)
2496 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2498 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2499 const auto * options = operatorPtr->builtin_options.AsConcatenationOptions();
2503 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2504 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2507 unsigned int numConcatView =
static_cast<unsigned int>(inputs.size());
2510 const unsigned int concatDimInput =
static_cast<unsigned int>(
2511 (
static_cast<int>(inputRank) + options->axis) %
static_cast<int>(inputRank));
2513 OriginsDescriptor concatDescriptor(static_cast<uint32_t>(numConcatView), inputRank);
2516 unsigned int mergeDimOrigin = 0;
2518 for (
unsigned int viewIndex = 0; viewIndex < numConcatView; ++viewIndex)
2524 inputTensorInfo, concatDescriptor, concatDimInput, viewIndex, mergeDimOrigin);
2527 auto layerName = fmt::format(
"Concatenation:{}:{}", subgraphIndex, operatorIndex);
2530 IConnectableLayer* layer = m_Network->AddConcatLayer(concatDescriptor, layerName.c_str());
2534 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2535 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
2538 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2540 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2541 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2544 void TfLiteParserImpl::ParseFullyConnected(
size_t subgraphIndex,
size_t operatorIndex)
2546 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2548 const auto & operatorRfr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2549 const auto options = operatorRfr->builtin_options.AsFullyConnectedOptions();
2557 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2558 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2564 int32_t weightsDimension =
static_cast<int32_t
>(filterTensorInfo.GetNumDimensions());
2565 if (weightsDimension != 2)
2568 fmt::format(
"Dimension {} for Fully Connected weights is not supported by Armnn. " 2575 auto layerName = fmt::format(
"FullyConnected:{}:{}", subgraphIndex, operatorIndex);
2577 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2579 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0]};
2580 std::vector<unsigned int> ignoreInputWhenRegister = {};
2585 tensorIndexesToRegister.emplace_back(inputTensorIndexes[1]);
2587 if (inputs.size() == 3)
2592 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
2596 layer = m_Network->AddFullyConnectedLayer(desc, layerName.c_str());
2601 unsigned int startingSlotIndex = 0;
2608 std::vector<unsigned int> reshapedDimensions(2);
2609 reshapedDimensions[1] = filterTensorInfo.GetShape()[1];
2610 reshapedDimensions[0] = inputTensorInfo.
GetNumElements() / reshapedDimensions[1];
2612 if (inputTensorInfo.
GetNumElements() % reshapedDimensions[1] != 0)
2615 fmt::format(
"Failed to deduce input tensor shape from filter size {} {}",
2616 reshapedDimensions[1],
2623 std::string reshapeLayerName = fmt::format(
"Reshape_for:{}", layer->
GetName());
2631 RegisterInputSlots(subgraphIndex, operatorIndex, reshapeLayer, {inputTensorIndexes[0]});
2633 tensorIndexesToRegister.erase(tensorIndexesToRegister.begin());
2634 startingSlotIndex = 1;
2637 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister, startingSlotIndex);
2644 options->fused_activation_function);
2647 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2648 RegisterOutputSlots(subgraphIndex, operatorIndex, fusedActivationLayer, {outputTensorIndexes[0]});
2651 void TfLiteParserImpl::ParseDetectionPostProcess(
size_t subgraphIndex,
size_t operatorIndex)
2653 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2655 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2657 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2658 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2662 auto custom_options = operatorPtr->custom_options;
2663 const flexbuffers::Map& m = flexbuffers::GetRoot(custom_options.data(), custom_options.size()).AsMap();
2672 desc.
m_ScaleH = m[
"h_scale"].AsFloat();
2673 desc.
m_ScaleW = m[
"w_scale"].AsFloat();
2674 desc.
m_ScaleX = m[
"x_scale"].AsFloat();
2675 desc.
m_ScaleY = m[
"y_scale"].AsFloat();
2677 if (!(m[
"use_regular_nms"].IsNull()))
2681 if (!(m[
"detections_per_class"].IsNull()))
2689 "must be positive and less than or equal to 1.");
2693 auto anchorTensorAndData = CreateConstTensorNonPermuted(inputs[2], anchorTensorInfo);
2695 auto layerName = fmt::format(
"DetectionPostProcess:{}:{}", subgraphIndex, operatorIndex);
2696 IConnectableLayer* layer = m_Network->AddDetectionPostProcessLayer(desc, anchorTensorAndData,
2704 m_OverridenOutputShapes.push_back({ 1, numDetectedBox, 4 });
2705 m_OverridenOutputShapes.push_back({ 1, numDetectedBox });
2706 m_OverridenOutputShapes.push_back({ 1, numDetectedBox });
2707 m_OverridenOutputShapes.push_back({ 1 });
2709 for (
unsigned int i = 0 ; i < outputs.size() ; ++i)
2717 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2718 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2721 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2722 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0],
2723 outputTensorIndexes[1],
2724 outputTensorIndexes[2],
2725 outputTensorIndexes[3]});
2729 void TfLiteParserImpl::ParsePack(
size_t subgraphIndex,
size_t operatorIndex)
2731 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2733 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2734 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2737 if (inputs.size() < 1)
2742 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2743 const auto* options = operatorPtr->builtin_options.AsPackOptions();
2746 desc.
m_Axis =
static_cast<uint32_t
>(options->axis);
2747 desc.
m_NumInputs =
static_cast<uint32_t
>(inputs.size());
2753 auto layerName = fmt::format(
"Pack:{}:{}", subgraphIndex, operatorIndex);
2761 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2762 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
2764 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2765 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2768 void TfLiteParserImpl::ParseUnpack(
size_t subgraphIndex,
size_t operatorIndex)
2770 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2772 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2773 const auto * options = operatorPtr->builtin_options.AsUnpackOptions();
2778 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2783 if (unpackAxis >= inputTensorInfo.GetNumDimensions())
2786 fmt::format(
"The unpack axis: {} cannot be greater than or equal to " 2787 "the number of input dimension {} {}",
2789 inputTensorInfo.GetNumDimensions(),
2797 unpackNum = inputTensorInfo.GetShape()[unpackAxis];
2803 throw ParseException(
"Number to unpack must greater than zero.");
2806 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2809 auto inputDimSize = inputTensorInfo.GetNumDimensions();
2810 std::vector<unsigned int> unpackDimSizes(inputDimSize);
2813 for (
unsigned int i = 0; i < inputDimSize; ++i)
2815 unpackDimSizes[i] = inputTensorInfo.GetShape()[i];
2818 if (unpackDimSizes[unpackAxis] != unpackNum)
2820 throw ParseException(
"Number to unpack must be the same as length of the dimension to " 2824 unpackDimSizes[unpackAxis] /= unpackNum;
2826 SplitterDescriptor splitDesc(unpackNum, static_cast<unsigned int>(unpackDimSizes.size()));
2827 for (
unsigned int j = 0; j < unpackNum; ++j)
2830 for (
unsigned int dimIdx = 0; dimIdx < unpackDimSizes.size(); ++dimIdx)
2832 splitDesc.
SetViewSize(j, dimIdx, unpackDimSizes[dimIdx]);
2837 auto layerName = fmt::format(
"Unpack:{}:{}", subgraphIndex, operatorIndex);
2838 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
2842 unpackDimSizes.data());
2844 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2845 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2847 std::vector<unsigned int> reshapeDims;
2848 for (
unsigned int axis = 0; axis < splitOutShape.
GetNumDimensions(); ++axis)
2850 if (axis != unpackAxis)
2852 reshapeDims.push_back(splitOutShape[axis]);
2862 std::string reshapeLayerName = fmt::format(
"Reshape_for:{}", layer->
GetName());
2877 RegisterProducerOfTensor(subgraphIndex, reshapedOutputId, slot);
2881 void TfLiteParserImpl::ParseSplit(
size_t subgraphIndex,
size_t operatorIndex)
2883 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2885 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2886 const auto * options = operatorPtr->builtin_options.AsSplitOptions();
2893 throw ParseException(
"Number to splits must greater than zero.");
2896 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2898 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2906 if (axisBufferPtr ==
nullptr)
2909 fmt::format(
"Operation has invalid inputs. Failed to read axis. {}",
2914 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.
GetNumBytes());
2915 int32_t axis = axisData[0];
2917 auto inputDimensions =
static_cast<int32_t
>(inputTensorInfo.GetNumDimensions());
2918 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
2924 fmt::format(
"Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
2931 auto inputDimSize = inputTensorInfo.GetNumDimensions();
2935 fmt::format(
"The number of dimensions: {} for input tensors of the split op cannot be greater than {} {}",
2936 inputTensorInfo.GetNumDimensions(),
2941 std::vector<unsigned int> splitterDimSizes(inputDimSize);
2944 for (
unsigned int i = 0; i < inputDimSize; ++i)
2946 splitterDimSizes[i] = inputTensorInfo.GetShape()[i];
2949 if (splitterDimSizes[splitDim] % numSplits != 0)
2951 throw ParseException(
"Number of splits must evenly divide the dimension");
2953 splitterDimSizes[splitDim] /= numSplits;
2956 for (
unsigned int j = 0; j < numSplits; ++j)
2959 for (
unsigned int dimIdx = 0; dimIdx < splitterDimSizes.size(); ++dimIdx)
2961 splitDesc.
SetViewSize(j, dimIdx, splitterDimSizes[dimIdx]);
2966 auto layerName = fmt::format(
"Split:{}:{}", subgraphIndex, operatorIndex);
2967 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
2970 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2971 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[1]});
2979 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2980 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2986 int v = idx < 0 ? numDims + idx : idx;
2990 return static_cast<unsigned int>(v);
2993 void TfLiteParserImpl::ParseSplitV(
size_t subgraphIndex,
size_t operatorIndex)
2995 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2997 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2998 const auto * options = operatorPtr->builtin_options.AsSplitVOptions();
3000 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3003 auto& inputTensor = inputs[0];
3004 auto& splitsTensor = inputs[1];
3005 auto& axisTensor = inputs[2];
3017 fmt::format(
"The number of dimensions: {} for input tensors of the " 3018 "SplitV op cannot be greater than {} {}",
3026 if (axisBufferPtr ==
nullptr)
3029 fmt::format(
"Operation has invalid inputs. Failed to read axis. {}",
3034 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.
GetNumBytes());
3035 int32_t axis = axisData[0];
3037 auto inputDimensions =
static_cast<int32_t
>(inputTensorInfo.
GetNumDimensions());
3038 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3044 fmt::format(
"Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
3052 unsigned int numSplits{0};
3068 std::vector<int> splitsData(numSplits);
3070 ::memcpy(splitsData.data(), splitsBufferPtr->data.data(), splitsInfo.
GetNumBytes());
3072 unsigned int idx = 0;
3074 unsigned int inferIdx{0};
3076 for (
auto split : splitsData)
3090 if (numInferred == 0)
3092 if (splitSum != armnn::numeric_cast<int>(inputTensorInfo.
GetShape()[splitDim]))
3094 throw ParseException(
"SplitV split_sizes does not sum to the dimension of value along split_dim.");
3097 else if (numInferred == 1)
3103 throw ParseException(
"Cannot infer split size for more than one split");
3107 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3112 unsigned int accumSplit = 0;
3113 for (
unsigned int j = 0; j < numSplits; ++j)
3118 for (
unsigned int dimIdx = 0; dimIdx < inputTensorInfo.
GetNumDimensions(); ++dimIdx)
3120 unsigned int dimSize = inputTensorInfo.
GetShape()[dimIdx];
3121 if (dimIdx == splitDim)
3123 dimSize = splitSize;
3125 splitDesc.SetViewSize(j, dimIdx, dimSize);
3128 splitDesc.SetViewOriginCoord(j, splitDim, accumSplit);
3129 accumSplit += splitSize;
3132 auto layerName = fmt::format(
"SplitV:{}:{}", subgraphIndex, operatorIndex);
3133 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
3136 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3137 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3145 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3146 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3149 void TfLiteParserImpl::ParseArgMin(
size_t subgraphIndex,
size_t operatorIndex)
3154 void TfLiteParserImpl::ParseArgMax(
size_t subgraphIndex,
size_t operatorIndex)
3159 void TfLiteParserImpl::ParseArgMinMax(
size_t subgraphIndex,
size_t operatorIndex,
ArgMinMaxFunction argMinMaxFunction)
3161 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3162 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3165 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3179 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
3185 if (axisBufferPtr ==
nullptr)
3188 fmt::format(
"Operation has invalid inputs. Failed to read axis. {}",
3193 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.
GetNumBytes());
3194 int32_t axis = axisData.front();
3196 auto inputDimensions =
static_cast<int32_t
>(inputTensorInfo.GetNumDimensions());
3197 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3203 fmt::format(
"Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
3213 auto layerName = argMinMaxFunction == ArgMinMaxFunction::Max ?
"ArgMax:{}:{}" :
"ArgMin:{}:{}";
3214 auto layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
3215 IConnectableLayer *layer = m_Network->AddArgMinMaxLayer(desc, layerNameFormatted.c_str());
3220 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3221 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3224 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3225 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3228 void TfLiteParserImpl::ParseGather(
size_t subgraphIndex,
size_t operatorIndex)
3230 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3243 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3244 const auto * options = operatorPtr->builtin_options.AsGatherOptions();
3245 auto axis = options->axis;
3247 auto inputDimensions =
static_cast<int32_t
>(inputTensorInfo.GetNumDimensions());
3250 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3253 fmt::format(
"Operation has invalid axis: {} It is out of bounds [ -{}, {} ) {}",
3255 inputDimensions, inputDimensions,
3258 if (outputDimensions != static_cast<unsigned int>(inputDimensions) + indicesDimensions - 1)
3261 fmt::format(
"Operation has invalid output dimensions: {} Output must be an ({} + {} - 1) -D tensor {}",
3263 inputDimensions, indicesDimensions,
3267 gatherDescriptor.
m_Axis = axis;
3269 auto layerName = fmt::format(
"Gather:{}:{}", subgraphIndex, operatorIndex);
3270 IConnectableLayer* layer = m_Network->AddGatherLayer(gatherDescriptor, layerName.c_str());
3274 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3275 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3277 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3278 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3281 void TfLiteParserImpl::ParseDepthToSpace(
size_t subgraphIndex,
size_t operatorIndex)
3283 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3292 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3293 const auto * options = operatorPtr->builtin_options.AsDepthToSpaceOptions();
3294 auto blockSize = options->block_size;
3298 fmt::format(
"Operation has invalid block size: {} Block size should be >= 2 {}",
3304 auto layerName = fmt::format(
"DepthToSpace:{}:{}", subgraphIndex, operatorIndex);
3305 IConnectableLayer* layer = m_Network->AddDepthToSpaceLayer(descriptor, layerName.c_str());
3310 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3311 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3313 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3314 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3317 void TfLiteParserImpl::ParseSum(
size_t subgraphIndex,
size_t operatorIndex)
3322 void TfLiteParserImpl::ParseReduceMax(
size_t subgraphIndex,
size_t operatorIndex)
3327 void TfLiteParserImpl::ParseReduceMin(
size_t subgraphIndex,
size_t operatorIndex)
3332 void TfLiteParserImpl::ParseReduce(
size_t subgraphIndex,
size_t operatorIndex,
ReduceOperation reduceOperation)
3334 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3336 const auto &operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3337 const auto *options = operatorPtr->builtin_options.AsReducerOptions();
3339 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3342 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3345 auto layerName = fmt::format(
"Reduce:{}:{}", subgraphIndex, operatorIndex);
3353 if (axisBufferPtr !=
nullptr)
3355 std::vector<int32_t> axisData(inputTensorInfo1.
GetNumElements());
3356 ::memcpy(axisData.data(), axisBufferPtr->data.data(), inputTensorInfo1.
GetNumBytes());
3360 std::set<unsigned int> uniqueAxis;
3361 std::transform(axisData.begin(),
3363 std::inserter(uniqueAxis, uniqueAxis.begin()),
3364 [rank](
int i)->unsigned
int{
3365 return static_cast<uint32_t
>(((i + rank) % rank)); });
3366 desc.
m_vAxis.assign(uniqueAxis.begin(), uniqueAxis.end());
3386 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3387 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3390 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3391 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3394 void TfLiteParserImpl::ParseAbs(
size_t subgraphIndex,
size_t operatorIndex)
3399 void TfLiteParserImpl::ParseExp(
size_t subgraphIndex,
size_t operatorIndex)
3404 void TfLiteParserImpl::ParseLogicalNot(
size_t subgraphIndex,
size_t operatorIndex)
3409 void TfLiteParserImpl::ParseNeg(
size_t subgraphIndex,
size_t operatorIndex)
3414 void TfLiteParserImpl::ParseRsqrt(
size_t subgraphIndex,
size_t operatorIndex)
3419 void TfLiteParserImpl::ParseElementwiseUnary(
size_t subgraphIndex,
size_t operatorIndex,
UnaryOperation unaryOperation)
3421 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3423 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3426 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3430 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
3434 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(desc, layerNameFormatted.c_str());
3440 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3441 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3443 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3444 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3447 void TfLiteParserImpl::ParseEqual(
size_t subgraphIndex,
size_t operatorIndex)
3452 void TfLiteParserImpl::ParseNotEqual(
size_t subgraphIndex,
size_t operatorIndex)
3457 void TfLiteParserImpl::ParseGreater(
size_t subgraphIndex,
size_t operatorIndex)
3462 void TfLiteParserImpl::ParseGreaterOrEqual(
size_t subgraphIndex,
size_t operatorIndex)
3467 void TfLiteParserImpl::ParseLess(
size_t subgraphIndex,
size_t operatorIndex)
3472 void TfLiteParserImpl::ParseLessOrEqual(
size_t subgraphIndex,
size_t operatorIndex)
3477 void TfLiteParserImpl::ParseComparison(
size_t subgraphIndex,
size_t operatorIndex,
3480 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3482 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3485 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3489 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
3493 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerNameFormatted,
"Input 0",
"Input 1");
3497 IConnectableLayer* layer = m_Network->AddComparisonLayer(desc, layerNameFormatted.c_str());
3503 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3504 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3506 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3507 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3511 unsigned int outputSlot,
3512 tflite::ActivationFunctionType activationType)
3515 std::string layerName = prevLayer->
GetName();
3517 switch(activationType)
3519 case tflite::ActivationFunctionType_NONE:
3524 case tflite::ActivationFunctionType_RELU:
3526 activationDesc.
m_Function = ActivationFunction::ReLu;
3527 layerName +=
":RELU";
3530 case tflite::ActivationFunctionType_RELU6:
3532 activationDesc.
m_Function = ActivationFunction::BoundedReLu;
3533 activationDesc.
m_A = 6.0f;
3534 activationDesc.
m_B = 0.0f;
3535 layerName +=
":RELU6";
3538 case tflite::ActivationFunctionType_TANH:
3540 activationDesc.
m_Function = ActivationFunction::TanH;
3541 activationDesc.
m_A = 1.0f;
3542 activationDesc.
m_B = 1.0f;
3543 layerName +=
":TANH";
3548 case tflite::ActivationFunctionType_RELU_N1_TO_1:
3549 case tflite::ActivationFunctionType_SIGN_BIT:
3553 fmt::format(
"TfLite parser doesn't suppport fused activation: " 3556 tflite::EnumNameActivationFunctionType(activationType),
3563 m_Network->AddActivationLayer(activationDesc, layerName.c_str());
3565 auto & prevOutputSlot = prevLayer->
GetOutputSlot(outputSlot);
3568 return activationLayer;
3573 if (fileName ==
nullptr)
3578 std::error_code errorCode;
3579 fs::path pathToFile(fileName);
3580 if (!fs::exists(pathToFile, errorCode))
3583 std::stringstream msg;
3584 msg <<
"Cannot find the file (" << fileName <<
") errorCode: " << errorCode
3589 std::ifstream file(fileName, std::ios::binary);
3590 std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
3592 fileContent.size());
3597 if (binaryContent ==
nullptr)
3602 flatbuffers::Verifier verifier(binaryContent, len);
3603 if (verifier.VerifyBuffer<tflite::Model>() ==
false)
3606 fmt::format(
"Buffer doesn't conform to the expected Tensorflow Lite " 3607 "flatbuffers format. size:{} {}",
3611 return tflite::UnPackModel(binaryContent);
3615 size_t subgraphIndex,
3616 size_t operatorIndex)
3620 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3621 const auto & operatorPtr = subgraphPtr->operators[operatorIndex];
3623 size_t inputCount = operatorPtr->inputs.size();
3625 for (
size_t i=0; i<inputCount; ++i)
3628 if (operatorPtr->inputs[i] == -1)
3635 result.push_back(subgraphPtr->tensors[inputId].get());
3642 size_t subgraphIndex,
3643 size_t operatorIndex)
3647 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3648 const auto & operatorPtr = subgraphPtr->operators[operatorIndex];
3650 size_t outputCount = operatorPtr->outputs.size();
3652 for (
size_t i=0; i<outputCount; ++i)
3656 result[i] = subgraphPtr->tensors[outputId].get();
3662 size_t subgraphIndex)
3665 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3667 size_t inputCount = subgraphPtr->inputs.size();
3669 for (
size_t i=0; i<inputCount; ++i)
3673 result[i] = std::make_pair(inputId, subgraphPtr->tensors[inputId].get());
3679 size_t subgraphIndex)
3682 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3684 size_t outputCount = subgraphPtr->outputs.size();
3686 for (
size_t i=0; i<outputCount; ++i)
3689 result[i] = std::make_pair(outputId, subgraphPtr->tensors[outputId].get());
3695 size_t subgraphIndex,
3696 size_t operatorIndex)
3699 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3700 const auto & operatorPtr = subgraphPtr->operators[operatorIndex];
3701 return operatorPtr->inputs;
3705 size_t subgraphIndex,
3706 size_t operatorIndex)
3709 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3710 const auto & operatorPtr = subgraphPtr->operators[operatorIndex];
3711 return operatorPtr->outputs;
3714 void TfLiteParserImpl::RegisterInputSlots(
size_t subgraphIndex,
3715 size_t operatorIndex,
3717 const std::vector<unsigned int>& tensorIndexes,
3718 unsigned int startingSlotIndex)
3720 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3726 fmt::format(
"The number of tensor inputs ({}) does not match the number expected ({})" 3727 " for subgraph:{} operator index:{} {}",
3728 tensorIndexes.size(),
3735 for (
unsigned int index = 0; index < tensorIndexes.size() ; ++index)
3737 unsigned int tensorIndex = tensorIndexes[index];
3739 RegisterConsumerOfTensor(subgraphIndex, tensorIndex, slot);
3743 void TfLiteParserImpl::RegisterOutputSlots(
size_t subgraphIndex,
3744 size_t operatorIndex,
3746 const std::vector<unsigned int>& tensorIndexes)
3748 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3753 fmt::format(
"The number of tensor outputs ({}) does not match the number expected ({})" 3754 " for subgraph:{} operator index:{} {}",
3755 tensorIndexes.size(),
3762 for (
unsigned int slotIndex = 0; slotIndex < layer->
GetNumOutputSlots(); ++slotIndex)
3764 unsigned int tensorIndex = tensorIndexes[slotIndex];
3766 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
3770 void TfLiteParserImpl::SetupInputLayers(
size_t subgraphIndex)
3775 for (
auto const & tensorIdAndPtr : inputs)
3777 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
3779 m_Network->AddInputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
3784 RegisterOutputSlots(subgraphIndex,
3785 VIRTUAL_OPERATOR_ID,
3787 {
static_cast<uint32_t
>(tensorIdAndPtr.first) });
3791 void TfLiteParserImpl::SetupOutputLayers(
size_t subgraphIndex)
3796 for (
auto const & tensorIdAndPtr : outputs)
3798 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
3800 m_Network->AddOutputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
3802 RegisterInputSlots(subgraphIndex,
3803 VIRTUAL_OPERATOR_ID,
3805 {
static_cast<uint32_t
>(tensorIdAndPtr.first) });
3809 void TfLiteParserImpl::SetupConstantLayers(
size_t subgraphIndex)
3813 const auto & subgraphPtr = m_Model->subgraphs[subgraphIndex];
3814 for (
unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
3816 for (
unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
3818 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot ==
nullptr &&
3819 m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0)
3821 TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get();
3823 if(IsConstTensor(tensorPtr))
3826 auto tensorAndData = CreateConstTensorNonPermuted(tensorPtr, tensorInfo);
3828 std::string layerName = fmt::format(
"Constant:{}", tensorPtr->name);
3829 IConnectableLayer *layer = m_Network->AddConstantLayer(tensorAndData, layerName.c_str());
3832 RegisterOutputSlots(subgraphIndex,
3833 VIRTUAL_OPERATOR_ID,
3840 fmt::format(
"Invalid Tensor: Tensor should be constant. {}",
3852 return model->buffers[bufferIndex].get();
3855 template<
typename T>
3856 std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
3865 auto constData = CreateConstTensorImpl<T>(bufferPtr,
3869 TfLiteParserImpl::SupportedDataStorage storage(std::move(constData.second));
3870 return std::make_pair(constData.first, std::move(storage));
3873 bool TfLiteParserImpl::IsConstTensor(
TensorRawPtr tensorPtr)
3876 bool isConst =
true;
3878 auto buffer =
GetBuffer(m_Model, tensorPtr->buffer);
3879 if (buffer->data.size() == 0)
3887 std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
3888 TfLiteParserImpl::CreateConstTensorPermuted(
TensorRawPtr tensorPtr,
3893 auto bufferPtr =
GetBuffer(m_Model, tensorPtr->buffer);
3902 return CreateConstTensorAndStoreData<float>(bufferPtr,
3907 return CreateConstTensorAndStoreData<uint8_t>(bufferPtr,
3912 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
3917 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
3922 return CreateConstTensorAndStoreData<int32_t>(bufferPtr,
3928 std::stringstream errString;
3929 errString <<
"Unexpected datatype when creating const tensor: " 3931 <<
" shape:" << tensorInfo.GetShape()
3942 auto bufferPtr =
GetBuffer(m_Model, tensorPtr->buffer);
3948 return ConstTensor(tensorInfo, bufferPtr->data.data());
3952 const std::string& name)
const 3956 for (
auto const & input : inputs)
3958 if (input.second->name == name)
3960 auto bindingId = GenerateLayerBindingId(subgraphId, input.first);
3961 return std::make_pair(bindingId,
ToTensorInfo(input.second));
3965 std::stringstream bindings;
3966 for (
auto const & input : inputs)
3968 bindings <<
"'" << input.second->name <<
"' ";
3972 fmt::format(
"No input binding found for subgraph:{} and name:{}. " 3973 "Possible inputs are: [{}] {}",
3981 const std::string& name)
const 3985 for (
unsigned int i = 0; i < outputs.size(); ++i)
3987 auto const output = outputs[i];
3988 if (output.second->name == name)
3990 auto bindingId = GenerateLayerBindingId(subgraphId, output.first);
3991 std::vector<unsigned int> shape = m_OverridenOutputShapes.size() > 0 ?
3992 m_OverridenOutputShapes[i] : AsUnsignedVector(output.second->shape);
3993 return std::make_pair(bindingId,
ToTensorInfo(output.second, shape));
3997 std::stringstream bindings;
3998 for (
auto const & output : outputs)
4000 bindings <<
"'" << output.second->name <<
"' ";
4004 fmt::format(
"No output binding found for subgraph:{} and name:{}. " 4005 "Possible outputs are: [{}] {}",
4014 return m_Model->subgraphs.size();
4021 std::vector<std::string> result;
4022 result.reserve(inputs.size());
4023 for (
auto const & input : inputs)
4025 result.push_back(input.second->name);
4034 std::vector<std::string> result;
4035 result.reserve(outputs.size());
4036 for (
auto const & output : outputs)
4038 result.push_back(output.second->name);
4048 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<
float[]> && data)
4049 : m_FloatData(std::move(data))
4050 , m_Uint8Data(
nullptr)
4051 , m_Int8Data(
nullptr)
4052 , m_Int32Data(
nullptr)
4056 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<uint8_t[]> && data)
4057 : m_FloatData(
nullptr)
4058 , m_Uint8Data(std::move(data))
4059 , m_Int8Data(
nullptr)
4060 , m_Int32Data(
nullptr)
4064 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int8_t[]> && data)
4065 : m_FloatData(
nullptr)
4066 , m_Uint8Data(
nullptr)
4067 , m_Int8Data(std::move(data))
4068 , m_Int32Data(
nullptr)
4072 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int32_t[]> && data)
4073 : m_FloatData(
nullptr)
4074 , m_Uint8Data(
nullptr)
4075 , m_Int8Data(
nullptr)
4076 , 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.
bool AreAllDimensionsSpecified() const
Checks if there is at least one dimension not specified.
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).
A ComparisonDescriptor for the ComparisonLayer.
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).
constexpr char const * GetUnaryOperationAsCString(UnaryOperation operation)
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.
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)
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.
unsigned int GetUnsignedAxis(const unsigned int inputDimension, const int axis)
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.
#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.
armnn::INetworkPtr LoadModel(std::unique_ptr< tflite::ModelT > model)
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.
ComparisonOperation m_Operation
Specifies the comparison operation to execute.
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)
std::pair< armnn::ConstTensor, std::unique_ptr< T[]> > CreateConstTensorImpl(const T *bufferPtr, armnn::TensorInfo &tensorInfo, const armnn::Optional< armnn::PermutationVector &> permutationVector)
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).
constexpr char const * GetComparisonOperationAsCString(ComparisonOperation operation)
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.
void SetConstant(const bool IsConstant=true)
Marks the data corresponding to this tensor info as constant.
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.
static armnn::TensorInfo OutputShapeOfSqueeze(std::vector< uint32_t > squeezeDims, const armnn::TensorInfo &inputTensorInfo)
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.
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).
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.
bool m_ConstantWeights
Enable/disable constant weights and biases.