30 #include <schema_generated.h> 32 #include <flatbuffers/flexbuffers.h> 34 #include <fmt/format.h> 41 #define ARMNN_THROW_PARSE_EXCEPTION(msg) \ 43 throw armnn::ParseException( static_cast<const std::stringstream&>( std::stringstream() << msg \ 45 << CHECK_LOCATION().AsString()).str()); \ 48 using namespace armnn;
54 pTfLiteParserImpl(
new TfLiteParserImpl(options)) {}
56 ITfLiteParser::~ITfLiteParser() =
default;
78 armnn::INetworkPtr ITfLiteParser::CreateNetworkFromBinary(
const std::vector<uint8_t>& binaryContent)
80 return pTfLiteParserImpl->CreateNetworkFromBinary(binaryContent);
84 const std::string& name)
const 86 return pTfLiteParserImpl->GetNetworkInputBindingInfo(subgraphId, name);
90 const std::string& name)
const 92 return pTfLiteParserImpl->GetNetworkOutputBindingInfo(subgraphId, name);
95 size_t ITfLiteParser::GetSubgraphCount()
const 97 return pTfLiteParserImpl->GetSubgraphCount();
100 std::vector<std::string> ITfLiteParser::GetSubgraphInputTensorNames(
size_t subgraphId)
const 102 return pTfLiteParserImpl->GetSubgraphInputTensorNames(subgraphId);
105 std::vector<std::string> ITfLiteParser::GetSubgraphOutputTensorNames(
size_t subgraphId)
const 107 return pTfLiteParserImpl->GetSubgraphOutputTensorNames(subgraphId);
113 const uint32_t VIRTUAL_OPERATOR_ID = std::numeric_limits<uint32_t>::max();
116 size_t subgraphIndex,
119 if (model.get() ==
nullptr)
122 fmt::format(
"{} was called with invalid (null) model. " 123 "Possible reason is that the model is not yet loaded and Unpack(ed). " 129 else if (subgraphIndex >= model->subgraphs.size())
132 fmt::format(
"{} was called with an invalid subgraph index. " 140 #define CHECK_SUBGRAPH(MODEL, SUBGRAPH_INDEX) \ 141 CheckSubgraph(MODEL, SUBGRAPH_INDEX, CHECK_LOCATION()) 144 size_t subgraphIndex,
145 size_t operatorIndex,
148 if (model.get() ==
nullptr)
151 fmt::format(
"{} was called with invalid (null) model. " 152 "Possible reason is that the model is not yet loaded and Unpack(ed). " 153 "subgraph:{} operator:{} at {}",
159 else if (subgraphIndex >= model->subgraphs.size())
162 fmt::format(
"{} was called with an invalid subgraph index. " 163 "subgraph:{} operator:{} at {}",
169 else if (operatorIndex >= model->subgraphs[subgraphIndex]->operators.size() &&
170 operatorIndex != VIRTUAL_OPERATOR_ID)
173 fmt::format(
"{} was called with an invalid operator index. " 174 "subgraph:{} operator:{} at {}",
182 #define CHECK_MODEL(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX) \ 183 CheckModel(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX, CHECK_LOCATION()) 186 size_t subgraphIndex,
192 ARMNN_ASSERT_MSG(model.get() !=
nullptr,
"Expecting a valid model in this function");
196 ARMNN_ASSERT_MSG(subgraphIndex < model->subgraphs.size(),
"Expecting a valid subgraph index");
199 if (tensorIndex >= model->subgraphs[subgraphIndex]->tensors.size())
202 fmt::format(
"{} was called with an invalid tensor index. " 203 "subgraph:{} tensor:{} at {}",
211 #define CHECK_TENSOR(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX) \ 212 CheckTensor(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX, CHECK_LOCATION()) 217 if (rawPtr ==
nullptr)
220 fmt::format(
"{} was called with a null tensor pointer at {}", location.
m_Function, location.
FileLine()));
224 #define CHECK_TENSOR_PTR(TENSOR_PTR) \ 225 CheckTensorPtr(TENSOR_PTR, CHECK_LOCATION()) 231 if (model.get() ==
nullptr)
234 fmt::format(
"{} was called with invalid (null) model. " 235 "Possible reason is that the model is not yet loaded and Unpack(ed). " 241 else if (bufferIndex >= model->buffers.size())
244 fmt::format(
"{} was called with an invalid buffer index. " 245 "buffer index:{} at {}",
250 else if (model->buffers[bufferIndex].get() ==
nullptr)
253 fmt::format(
"The buffer #{} is null. {}",
259 #define CHECK_BUFFER(MODEL, BUFFER_INDEX) \ 260 CheckBuffer(MODEL, BUFFER_INDEX, CHECK_LOCATION()) 262 void CheckBufferSize(TfLiteParserImpl::BufferRawPtr bufferPtr,
267 if (bufferPtr ==
nullptr)
270 fmt::format(
"BufferPtr is null for buffer:{}. {}",
277 std::stringstream ss;
278 ss <<
"Buffer #" << bufferId <<
" has " << bufferPtr->data.size() <<
" bytes. " 279 <<
"For tensor: " << tensorInfo.
GetShape()
280 <<
" expecting: " << tensorInfo.
GetNumBytes() <<
" bytes and " 289 const auto& operatorPtr = model->subgraphs[subgraphIndex]->operators[operatorIndex];
290 auto opcodeIndex = operatorPtr->opcode_index;
293 #if defined(ARMNN_POST_TFLITE_2_3) 294 auto opcode = std::max(model->operator_codes[opcodeIndex]->builtin_code,
295 static_cast<tflite::BuiltinOperator>(model->operator_codes[opcodeIndex]->deprecated_builtin_code));
297 auto opcode = model->operator_codes[opcodeIndex]->builtin_code;
306 TfLiteParserImpl::BufferRawPtr bufferPtr = TfLiteParserImpl::GetBuffer(model, bufferIndex);
311 ::memcpy(buffer.data(), bufferPtr->data.data(), bufferPtr->data.size());
316 ::memcpy(uint64Buffer.data(), bufferPtr->data.data(), bufferPtr->data.size());
317 buffer.assign(std::begin(uint64Buffer), std::end(uint64Buffer));
322 #define CHECK_BUFFER_SIZE(BUFFER_PTR, TENSOR_INFO, BUFFER_ID) \ 323 CheckBufferSize(BUFFER_PTR, TENSOR_INFO, BUFFER_ID, CHECK_LOCATION()) 327 switch(activationType)
329 case tflite::ActivationFunctionType_NONE:
330 case tflite::ActivationFunctionType_RELU:
331 case tflite::ActivationFunctionType_RELU6:
332 case tflite::ActivationFunctionType_TANH:
343 #define CHECK_SUPPORTED_FUSED_ACTIVATION(OPTION, SUBGRAPH_INDEX, OPERATOR_INDEX) \ 345 if (IsActivationSupported(OPTION->fused_activation_function) == false) \ 347 throw ParseException( \ 348 fmt::format("TfLite parser doesn't suppport fused activation: " \ 349 "{}/{} in {} subgraph:{} operator:{} at {}", \ 350 OPTION->fused_activation_function, \ 351 tflite::EnumNameActivationFunctionType(\ 352 OPTION->fused_activation_function), \ 356 CHECK_LOCATION().FileLine())); \ 361 std::vector<unsigned int> AsUnsignedVector(
const std::vector<int32_t>& in)
363 std::vector<unsigned int> result;
364 result.reserve(in.size());
377 bool IsOptionalOperandPresent(
int input)
382 void CalcPadding(uint32_t inputSize,
386 uint32_t& paddingFront,
387 uint32_t& paddingBack,
388 tflite::Padding padding)
392 if (padding == tflite::Padding_SAME)
394 uint32_t outputSize = (inputSize + stride - 1) / stride;
395 uint32_t dilatedSize = filterSize + (dilation - 1) * (filterSize - 1);
396 uint32_t temp = (outputSize - 1) * stride + dilatedSize;
397 if (temp > inputSize)
399 paddingFront = (temp - inputSize) / 2;
400 paddingBack = (temp - inputSize) - paddingFront;
406 const std::vector<unsigned int>& shape,
407 const bool outputTensor =
false)
412 switch (tensorPtr->type)
414 case tflite::TensorType_UINT8:
417 case tflite::TensorType_FLOAT32:
420 case tflite::TensorType_FLOAT16:
423 case tflite::TensorType_INT8:
424 if (tensorPtr->quantization->zero_point.size() == 1)
435 case tflite::TensorType_INT16:
438 case tflite::TensorType_INT32:
441 case tflite::TensorType_INT64:
444 case tflite::TensorType_BOOL:
451 fmt::format(
"Unsupported data type {} = {} for tensor: {}. {}",
453 tflite::EnumNameTensorType(tensorPtr->type),
460 std::vector<unsigned int> safeShape = shape;
461 if (shape.size() == 0)
463 safeShape.push_back(1);
468 tensorShape =
TensorShape(armnn::numeric_cast<unsigned int>(safeShape.size()), safeShape.data());
472 size_t shapeSignatureSize = tensorPtr->shape_signature.size();
475 if (shapeSignatureSize != 0)
478 if (shapeSignatureSize != shape.size())
482 for (
unsigned int i = 0; i < shapeSignatureSize; ++i)
484 unsigned int dim = tensorPtr->shape_signature[i] > -1 ?
485 static_cast<unsigned int>(tensorPtr->shape_signature[i]) : 0;
486 safeShape.push_back(dim);
490 std::unique_ptr<bool[]> dimMask = std::make_unique<bool[]>(tensorPtr->shape_signature.size());
491 for (
unsigned int i = 0; i < tensorPtr->shape_signature.size(); ++i)
493 dimMask[i] = tensorPtr->shape_signature[i] == -1 ? false :
true;
495 tensorShape =
TensorShape(static_cast<unsigned int>(safeShape.size()), safeShape.data(), dimMask.get());
498 else if (shape.size() == 0)
504 tensorShape =
TensorShape(armnn::numeric_cast<unsigned int>(shape.size()), shape.data());
508 float quantizationScale = 0.0f;
509 int32_t quantizationOffset = 0;
511 if (tensorPtr->quantization.get())
513 if (tensorPtr->quantization->scale.size() <= 1)
518 if (tensorPtr->quantization->scale.size() == 1)
520 quantizationScale = tensorPtr->quantization->scale[0];
522 if (tensorPtr->quantization->zero_point.size() == 1)
537 std::vector<float> quantizationScales;
538 std::vector<int32_t> quantizationOffsets;
541 std::copy(tensorPtr->quantization->scale.begin(),
542 tensorPtr->quantization->scale.end(),
543 std::back_inserter(quantizationScales));
549 armnn::numeric_cast<unsigned int>(tensorPtr->quantization->quantized_dimension));
565 auto const& dimensions = AsUnsignedVector(tensorPtr->shape);
570 const bool outputTensor)
572 auto const& dimensions = AsUnsignedVector(tensorPtr->shape);
573 return ToTensorInfo(tensorPtr, dimensions, outputTensor);
577 std::pair<armnn::ConstTensor, std::unique_ptr<T[]>>
586 fmt::format(
"Buffer for buffer:{} is null", tensorPtr->buffer).c_str());
594 reinterpret_cast<const T*
>(bufferPtr->data.data()), data.get(),
sizeof(T));
598 ::memcpy(data.get(), bufferPtr->data.data(), tensorInfo.
GetNumBytes());
604 return std::make_pair(
ConstTensor(tensorInfo, data.get()), std::move(data));
617 if (actualSize != expected.size())
622 for (
unsigned int i = 0u; i < actualSize; i++)
624 if (expected[i] < 0 ||
625 actual[i] != static_cast<unsigned int>(expected[i]))
634 void CheckMatchingQuantization(
const TensorInfo& first,
636 const std::string& descName,
637 std::string
const& firstName,
638 std::string
const& secondName)
650 if (firstDataType != secondDataType)
653 " must be of the same quantized type, " +
661 " must have the same quantization space, " +
673 , m_Network(nullptr, nullptr)
677 m_ParserFunctions[tflite::BuiltinOperator_ABS] = &TfLiteParserImpl::ParseAbs;
678 m_ParserFunctions[tflite::BuiltinOperator_ADD] = &TfLiteParserImpl::ParseAdd;
679 m_ParserFunctions[tflite::BuiltinOperator_ARG_MIN] = &TfLiteParserImpl::ParseArgMin;
680 m_ParserFunctions[tflite::BuiltinOperator_ARG_MAX] = &TfLiteParserImpl::ParseArgMax;
681 m_ParserFunctions[tflite::BuiltinOperator_AVERAGE_POOL_2D] = &TfLiteParserImpl::ParseAveragePool2D;
682 m_ParserFunctions[tflite::BuiltinOperator_BATCH_TO_SPACE_ND] = &TfLiteParserImpl::ParseBatchToSpaceND;
683 m_ParserFunctions[tflite::BuiltinOperator_CAST] = &TfLiteParserImpl::ParseCast;
684 m_ParserFunctions[tflite::BuiltinOperator_CONCATENATION] = &TfLiteParserImpl::ParseConcatenation;
685 m_ParserFunctions[tflite::BuiltinOperator_CONV_2D] = &TfLiteParserImpl::ParseConv2D;
687 #if defined(ARMNN_POST_TFLITE_2_4) 688 m_ParserFunctions[tflite::BuiltinOperator_CONV_3D] = &TfLiteParserImpl::ParseConv3D;
690 m_ParserFunctions[tflite::BuiltinOperator_CUSTOM] = &TfLiteParserImpl::ParseCustomOperator;
691 m_ParserFunctions[tflite::BuiltinOperator_DEPTH_TO_SPACE] = &TfLiteParserImpl::ParseDepthToSpace;
692 m_ParserFunctions[tflite::BuiltinOperator_DEPTHWISE_CONV_2D] = &TfLiteParserImpl::ParseDepthwiseConv2D;
693 m_ParserFunctions[tflite::BuiltinOperator_DEQUANTIZE] = &TfLiteParserImpl::ParseDequantize;
694 m_ParserFunctions[tflite::BuiltinOperator_DIV] = &TfLiteParserImpl::ParseDiv;
695 m_ParserFunctions[tflite::BuiltinOperator_ELU] = &TfLiteParserImpl::ParseElu;
696 m_ParserFunctions[tflite::BuiltinOperator_EQUAL] = &TfLiteParserImpl::ParseEqual;
697 m_ParserFunctions[tflite::BuiltinOperator_EXP] = &TfLiteParserImpl::ParseExp;
698 m_ParserFunctions[tflite::BuiltinOperator_EXPAND_DIMS] = &TfLiteParserImpl::ParseExpandDims;
699 m_ParserFunctions[tflite::BuiltinOperator_FLOOR_DIV] = &TfLiteParserImpl::ParseFloorDiv;
700 m_ParserFunctions[tflite::BuiltinOperator_FULLY_CONNECTED] = &TfLiteParserImpl::ParseFullyConnected;
701 m_ParserFunctions[tflite::BuiltinOperator_GATHER] = &TfLiteParserImpl::ParseGather;
702 m_ParserFunctions[tflite::BuiltinOperator_GATHER_ND] = &TfLiteParserImpl::ParseGatherNd;
703 m_ParserFunctions[tflite::BuiltinOperator_GREATER] = &TfLiteParserImpl::ParseGreater;
704 m_ParserFunctions[tflite::BuiltinOperator_GREATER_EQUAL] = &TfLiteParserImpl::ParseGreaterOrEqual;
705 m_ParserFunctions[tflite::BuiltinOperator_HARD_SWISH] = &TfLiteParserImpl::ParseHardSwish;
706 m_ParserFunctions[tflite::BuiltinOperator_LEAKY_RELU] = &TfLiteParserImpl::ParseLeakyRelu;
707 m_ParserFunctions[tflite::BuiltinOperator_LESS] = &TfLiteParserImpl::ParseLess;
708 m_ParserFunctions[tflite::BuiltinOperator_LESS_EQUAL] = &TfLiteParserImpl::ParseLessOrEqual;
709 m_ParserFunctions[tflite::BuiltinOperator_LOCAL_RESPONSE_NORMALIZATION]
710 = &TfLiteParserImpl::ParseLocalResponseNormalization;
711 m_ParserFunctions[tflite::BuiltinOperator_LOG] = &TfLiteParserImpl::ParseLog;
712 m_ParserFunctions[tflite::BuiltinOperator_LOGICAL_NOT] = &TfLiteParserImpl::ParseLogicalNot;
713 m_ParserFunctions[tflite::BuiltinOperator_LOGISTIC] = &TfLiteParserImpl::ParseLogistic;
714 m_ParserFunctions[tflite::BuiltinOperator_LOG_SOFTMAX] = &TfLiteParserImpl::ParseLogSoftmax;
715 m_ParserFunctions[tflite::BuiltinOperator_L2_NORMALIZATION] = &TfLiteParserImpl::ParseL2Normalization;
716 m_ParserFunctions[tflite::BuiltinOperator_MAX_POOL_2D] = &TfLiteParserImpl::ParseMaxPool2D;
717 m_ParserFunctions[tflite::BuiltinOperator_MAXIMUM] = &TfLiteParserImpl::ParseMaximum;
718 m_ParserFunctions[tflite::BuiltinOperator_MEAN] = &TfLiteParserImpl::ParseMean;
719 m_ParserFunctions[tflite::BuiltinOperator_MINIMUM] = &TfLiteParserImpl::ParseMinimum;
720 m_ParserFunctions[tflite::BuiltinOperator_MIRROR_PAD] = &TfLiteParserImpl::ParseMirrorPad;
721 m_ParserFunctions[tflite::BuiltinOperator_MUL] = &TfLiteParserImpl::ParseMul;
722 m_ParserFunctions[tflite::BuiltinOperator_NEG] = &TfLiteParserImpl::ParseNeg;
723 m_ParserFunctions[tflite::BuiltinOperator_NOT_EQUAL] = &TfLiteParserImpl::ParseNotEqual;
724 m_ParserFunctions[tflite::BuiltinOperator_PACK] = &TfLiteParserImpl::ParsePack;
725 m_ParserFunctions[tflite::BuiltinOperator_PAD] = &TfLiteParserImpl::ParsePad;
726 m_ParserFunctions[tflite::BuiltinOperator_PADV2] = &TfLiteParserImpl::ParsePad;
727 m_ParserFunctions[tflite::BuiltinOperator_PRELU] = &TfLiteParserImpl::ParsePrelu;
728 m_ParserFunctions[tflite::BuiltinOperator_QUANTIZE] = &TfLiteParserImpl::ParseQuantize;
729 m_ParserFunctions[tflite::BuiltinOperator_RELU] = &TfLiteParserImpl::ParseRelu;
730 m_ParserFunctions[tflite::BuiltinOperator_RELU6] = &TfLiteParserImpl::ParseRelu6;
731 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MAX] = &TfLiteParserImpl::ParseReduceMax;
732 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MIN] = &TfLiteParserImpl::ParseReduceMin;
733 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_PROD] = &TfLiteParserImpl::ParseReduceProd;
734 m_ParserFunctions[tflite::BuiltinOperator_RESHAPE] = &TfLiteParserImpl::ParseReshape;
735 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_BILINEAR] = &TfLiteParserImpl::ParseResizeBilinear;
736 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_NEAREST_NEIGHBOR] = &TfLiteParserImpl::ParseResizeNearestNeighbor;
737 m_ParserFunctions[tflite::BuiltinOperator_RSQRT] = &TfLiteParserImpl::ParseRsqrt;
738 m_ParserFunctions[tflite::BuiltinOperator_SQRT] = &TfLiteParserImpl::ParseSqrt;
739 m_ParserFunctions[tflite::BuiltinOperator_SHAPE] = &TfLiteParserImpl::ParseShape;
740 m_ParserFunctions[tflite::BuiltinOperator_SIN] = &TfLiteParserImpl::ParseSin;
741 m_ParserFunctions[tflite::BuiltinOperator_SLICE] = &TfLiteParserImpl::ParseSlice;
742 m_ParserFunctions[tflite::BuiltinOperator_SOFTMAX] = &TfLiteParserImpl::ParseSoftmax;
743 m_ParserFunctions[tflite::BuiltinOperator_SPACE_TO_BATCH_ND] = &TfLiteParserImpl::ParseSpaceToBatchND;
744 m_ParserFunctions[tflite::BuiltinOperator_SPLIT] = &TfLiteParserImpl::ParseSplit;
745 m_ParserFunctions[tflite::BuiltinOperator_SPLIT_V] = &TfLiteParserImpl::ParseSplitV;
746 m_ParserFunctions[tflite::BuiltinOperator_SQUEEZE] = &TfLiteParserImpl::ParseSqueeze;
747 m_ParserFunctions[tflite::BuiltinOperator_STRIDED_SLICE] = &TfLiteParserImpl::ParseStridedSlice;
748 m_ParserFunctions[tflite::BuiltinOperator_SUB] = &TfLiteParserImpl::ParseSub;
749 m_ParserFunctions[tflite::BuiltinOperator_SUM] = &TfLiteParserImpl::ParseSum;
750 m_ParserFunctions[tflite::BuiltinOperator_TANH] = &TfLiteParserImpl::ParseTanH;
751 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE] = &TfLiteParserImpl::ParseTranspose;
752 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE_CONV] = &TfLiteParserImpl::ParseTransposeConv;
753 m_ParserFunctions[tflite::BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM]
754 = &TfLiteParserImpl::ParseUnidirectionalSequenceLSTM;
755 m_ParserFunctions[tflite::BuiltinOperator_UNPACK] = &TfLiteParserImpl::ParseUnpack;
758 m_CustomParserFunctions[
"TFLite_Detection_PostProcess"] = &TfLiteParserImpl::ParseDetectionPostProcess;
761 void TfLiteParserImpl::ResetParser()
765 m_SubgraphConnections.clear();
766 m_OverridenOutputShapes.clear();
767 m_ConstantsToDequantize.clear();
768 m_ConstantsToBeCreated.clear();
775 return CreateNetworkFromModel();
782 return CreateNetworkFromModel();
789 m_Model = std::move(model);
791 return CreateNetworkFromModel();
794 INetworkPtr TfLiteParserImpl::CreateNetworkFromModel()
801 if (m_Options.value().m_InferAndValidate)
805 {
"InferAndValidate",
true }
808 networkOptions.push_back(shapeInferenceMethodOption);
810 if (m_Options.value().m_AllowExpandedDims)
814 {
"AllowExpandedDims",
true }
817 networkOptions.push_back(shapeInferenceMethodOption);
820 m_Network = INetwork::Create(networkOptions);
823 if (m_Model->subgraphs.size() != 1)
826 fmt::format(
"Current TfLite parser only supports 1 subgraph. Current one has: {} {}",
827 m_Model->subgraphs.size(),
831 size_t subgraphIndex = 0;
832 size_t operatorIndex = 0;
835 for (
SubgraphPtr const& subgraph : m_Model->subgraphs)
837 m_SubgraphConnections.emplace_back(subgraph->tensors.size());
840 auto const& opCodePtr = m_Model->operator_codes[op->opcode_index];
843 #if defined(ARMNN_POST_TFLITE_2_3) 844 auto builtinCode = std::max(opCodePtr->builtin_code,
845 static_cast<tflite::BuiltinOperator>(opCodePtr->deprecated_builtin_code));
847 auto builtinCode = opCodePtr->builtin_code;
850 if (builtinCode > tflite::BuiltinOperator_MAX)
852 throw ParseException(fmt::format(
"Operator code {} is out of range 0-{}. " 853 "subgraph:{} operator idx:{}. {}",
854 builtinCode, tflite::BuiltinOperator_MAX, subgraphIndex,
859 auto& parserFunction = m_ParserFunctions[builtinCode];
860 (this->*parserFunction)(subgraphIndex, operatorIndex);
864 SetupInputLayers(subgraphIndex);
865 SetupOutputLayers(subgraphIndex);
866 SetupConstantLayers(subgraphIndex);
874 std::stringstream errorString;
875 errorString <<
"Failed to parse operator #" << operatorIndex <<
" within subgraph #" 876 << subgraphIndex <<
" error: " << e.
what();
878 std::stringstream errors;
879 errors << errorString.str() <<
"\n";
884 for (subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
886 for (
size_t tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
888 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot !=
nullptr)
890 for (
size_t inputSlotIdx = 0;
891 inputSlotIdx < m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size();
894 m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot->Connect(
895 *(m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots[inputSlotIdx]));
900 return std::move(m_Network);
909 std::unique_ptr<float[]> buffer(
new float[tensorInfo.
GetNumElements()]);
914 auto axisDimensionality = tensorInfo.
GetShape()[axis];
919 unsigned int axisIndex = (i / axisFactor) % axisDimensionality;
935 fmt::format(
"Unsupported input/weights combination: Input {} not supported with Weights {}",
941 void TfLiteParserImpl::RegisterProducerOfTensor(
size_t subgraphIndex,
946 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
947 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
949 TensorSlots & tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
955 if (tensorSlots.outputSlot !=
nullptr)
957 throw ParseException(fmt::format(
"Another layer has already registered itself as the producer of " 958 "subgraph:{} tensor:{} {}",
964 tensorSlots.outputSlot = slot;
967 void TfLiteParserImpl::RegisterConsumerOfTensor(
size_t subgraphIndex,
972 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
973 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
975 TensorSlots& tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
976 tensorSlots.inputSlots.push_back(slot);
979 void TfLiteParserImpl::ParseCustomOperator(
size_t subgraphIndex,
size_t operatorIndex)
981 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
984 auto customParserFunction = &TfLiteParserImpl::ParseUnsupportedOperator;
987 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
988 const auto& customCode = m_Model->operator_codes[operatorPtr->opcode_index]->custom_code;
991 auto iterator = m_CustomParserFunctions.find(customCode);
992 if (iterator != m_CustomParserFunctions.end())
994 customParserFunction = iterator->second;
998 (this->*customParserFunction)(subgraphIndex, operatorIndex);
1001 void TfLiteParserImpl::ParseUnsupportedOperator(
size_t subgraphIndex,
size_t operatorIndex)
1003 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1005 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1007 auto opcodeIndex = operatorPtr->opcode_index;
1010 #if defined(ARMNN_POST_TFLITE_2_3) 1011 auto opcode = std::max(m_Model->operator_codes[opcodeIndex]->builtin_code,
1012 static_cast<tflite::BuiltinOperator>(m_Model->operator_codes[opcodeIndex]->deprecated_builtin_code));
1014 auto opcode = m_Model->operator_codes[opcodeIndex]->builtin_code;
1017 if (!m_Options || !m_Options.value().m_StandInLayerForUnsupported)
1021 fmt::format(
"Operator not supported. " 1022 "subgraph:{} operator:{} " 1023 "opcode_index:{} opcode:{} / {} {}",
1028 tflite::EnumNameBuiltinOperator(opcode),
1032 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1033 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1039 auto layerName = fmt::format(
"StandIn:{}:{}:{}", subgraphIndex, operatorIndex, opcode);
1042 IConnectableLayer* layer = m_Network->AddStandInLayer(descriptor, layerName.c_str());
1045 for (
unsigned int i = 0u; i < numOutputs; ++i)
1050 auto inputTensorIds = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1051 auto outputTensorIds = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1053 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIds);
1054 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIds);
1057 void TfLiteParserImpl::ParseCast(
size_t subgraphIndex,
size_t operatorIndex)
1059 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1061 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1063 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1066 auto layerName = fmt::format(
"Cast:{}:{}", subgraphIndex, operatorIndex);
1074 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1075 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1077 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1078 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1081 void TfLiteParserImpl::ParseConv2D(
size_t subgraphIndex,
size_t operatorIndex)
1083 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1085 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1086 const auto* options = operatorPtr->builtin_options.AsConv2DOptions();
1090 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1091 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1095 inputs.size() == 3 ?
1107 unsigned int inputHeight = inputTensorInfo.
GetShape()[1];
1108 unsigned int inputWidth = inputTensorInfo.
GetShape()[2];
1112 unsigned int filterHeight = filterTensorInfo.
GetShape()[1];
1113 unsigned int filterWidth = filterTensorInfo.
GetShape()[2];
1115 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1116 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1117 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1118 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
1122 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1123 std::vector<unsigned int> tensorIndexesToRegister = { inputTensorIndexes[0], inputTensorIndexes[1] };
1125 auto layerName = fmt::format(
"Conv2D:{}:{}", subgraphIndex, operatorIndex);
1128 if (IsConstTensor(inputs[1]) && inputTensorInfo.
GetDataType() == DataType::Float32 &&
1129 (filterTensorInfo.
GetDataType() == DataType::QAsymmU8 ||
1130 filterTensorInfo.
GetDataType() == DataType::QAsymmS8))
1132 m_ConstantsToDequantize.emplace_back(inputs[1]->buffer);
1135 if (desc.m_BiasEnabled)
1140 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
1142 if (IsConstTensor(inputs[2]) && inputTensorInfo.
GetDataType() == DataType::Float32 &&
1143 (filterTensorInfo.
GetDataType() == DataType::QAsymmU8 ||
1144 filterTensorInfo.
GetDataType() == DataType::QAsymmS8))
1146 m_ConstantsToDequantize.emplace_back(inputs[2]->buffer);
1157 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
1159 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1161 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1162 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, { outputTensorIndexes[0] });
1166 #if defined(ARMNN_POST_TFLITE_2_4) 1167 void TfLiteParserImpl::ParseConv3D(
size_t subgraphIndex,
size_t operatorIndex)
1169 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1171 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1172 const auto* options = operatorPtr->builtin_options.AsConv3DOptions();
1186 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1189 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1196 unsigned int inputDepth = inputTensorInfo.GetShape()[1];
1197 unsigned int inputHeight = inputTensorInfo.GetShape()[2];
1198 unsigned int inputWidth = inputTensorInfo.GetShape()[3];
1201 unsigned int filterDepth = filterTensorInfo.
GetShape()[0];
1202 unsigned int filterHeight = filterTensorInfo.
GetShape()[1];
1203 unsigned int filterWidth = filterTensorInfo.
GetShape()[2];
1205 CalcPadding(inputDepth, filterDepth, desc.
m_StrideZ,
1207 CalcPadding(inputHeight, filterHeight, desc.
m_StrideY,
1209 CalcPadding(inputWidth, filterWidth, desc.
m_StrideX,
1212 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo, inputTensorInfo.
GetDataType());
1214 auto layerName = fmt::format(
"Conv3D:{}:{}", subgraphIndex, operatorIndex);
1216 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1219 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0], inputTensorIndexes[1]};
1221 if (inputs.size() == 3)
1226 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
1236 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
1238 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1240 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1241 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1245 void TfLiteParserImpl::ParseDepthwiseConv2D(
size_t subgraphIndex,
size_t operatorIndex)
1247 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1249 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1250 const auto* options = operatorPtr->builtin_options.AsDepthwiseConv2DOptions();
1260 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1262 if (inputs.size() == 3)
1267 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1276 unsigned int inputHeight = inputTensorInfo.
GetShape()[1];
1277 unsigned int inputWidth = inputTensorInfo.
GetShape()[2];
1280 unsigned int filterHeight = filterTensorInfo.
GetShape()[1];
1281 unsigned int filterWidth = filterTensorInfo.
GetShape()[2];
1283 CalcPadding(inputHeight, filterHeight, desc.
m_StrideY,
1285 CalcPadding(inputWidth, filterWidth, desc.
m_StrideX,
1289 auto layerName = fmt::format(
"DepthwiseConv2D:{}:{}", subgraphIndex, operatorIndex);
1291 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1294 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0], inputTensorIndexes[1]};
1304 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
1313 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
1315 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1317 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1318 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1321 void TfLiteParserImpl::ParseDequantize(
size_t subgraphIndex,
size_t operatorIndex)
1323 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1325 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1328 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1331 auto layerName = fmt::format(
"Dequantize:{}:{}", subgraphIndex, operatorIndex);
1339 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1340 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1342 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1343 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1346 void TfLiteParserImpl::ParseExpandDims(
size_t subgraphIndex,
size_t operatorIndex)
1348 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1350 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1353 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1356 auto layerName = fmt::format(
"ExpandDims:{}:{}", subgraphIndex, operatorIndex);
1361 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1371 int32_t axis = inputs[1]->shape[0];
1375 if (axis > inputDimSize || axis < 0 - (inputDimSize + 1))
1377 throw ParseException(
"axis must be in range [0 - (inputDimSize + 1), inputDimSize] inclusive");
1382 axis = inputDimSize + axis + 1;
1385 std::vector<unsigned int> shape(static_cast<unsigned int>(inputDimSize) + 1);
1386 unsigned int inputShapeIndex = 0;
1387 for (
unsigned int i = 0; i < static_cast<unsigned int>(inputDimSize + 1); ++i)
1389 if (i == static_cast<unsigned int>(axis))
1395 shape[i] = inputTensorInfo.
GetShape()[inputShapeIndex];
1403 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
1405 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1407 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1408 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1410 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1411 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1414 void TfLiteParserImpl::ParseTranspose(
size_t subgraphIndex,
size_t operatorIndex)
1416 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1418 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1421 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1424 auto layerName = fmt::format(
"Transpose:{}:{}", subgraphIndex, operatorIndex);
1427 if (inputs.size() == 2)
1432 std::vector<unsigned int> permuteShape(numPermVecElements);
1433 ::memcpy(permuteShape.data(), permuteBufferPtr->data.data(), permuteTensorInfo.
GetNumBytes());
1441 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1443 IConnectableLayer* layer = m_Network->AddTransposeLayer(desc, layerName.c_str());
1447 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1448 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1450 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1451 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1454 void TfLiteParserImpl::ParseTransposeConv(
size_t subgraphIndex,
size_t operatorIndex)
1456 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1458 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1459 const auto* options = operatorPtr->builtin_options.AsTransposeConvOptions();
1467 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1468 if (inputs.size() == 4)
1477 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1484 if (tensorInfo.
GetDataType() == DataType::Signed32)
1486 ::memcpy(output_shape.data(),
GetBuffer(m_Model, inputs[0]->buffer)->data.data(), tensorInfo.
GetNumBytes());
1488 if (tensorInfo.
GetDataType() == DataType::QAsymmU8)
1492 output_shape[i] =
GetBuffer(m_Model, inputs[0]->buffer)->data.data()[i];
1496 for (
int dimension : output_shape)
1498 desc.
m_OutputShape.push_back(static_cast<unsigned int>(dimension));
1506 const unsigned int inputHeight = inputTensorInfo.
GetShape()[1];
1507 const unsigned int inputWidth = inputTensorInfo.
GetShape()[2];
1509 const unsigned int filterHeight = filterTensorInfo.
GetShape()[1];
1510 const unsigned int filterWidth = filterTensorInfo.
GetShape()[2];
1512 CalcPadding(inputHeight,
1520 CalcPadding(inputWidth,
1528 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo, inputTensorInfo.
GetDataType());
1531 auto layerName = fmt::format(
"TransposeConv:{}:{}", subgraphIndex, operatorIndex);
1536 auto biasConstTensor = CreateConstTensorNonPermuted(inputs[3], biasTensorInfo, inputTensorInfo.
GetDataType());
1537 layer = m_Network->AddTransposeConvolution2dLayer(desc,
1538 filterTensorAndData.first,
1539 biasConstTensor.first,
1544 layer = m_Network->AddTransposeConvolution2dLayer(desc,
1545 filterTensorAndData.first,
1553 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1556 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1557 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[2]});
1559 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1560 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1563 void TfLiteParserImpl::ParseAveragePool2D(
size_t subgraphIndex,
size_t operatorIndex)
1565 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Average);
1568 void TfLiteParserImpl::ParseBatchToSpaceND(
size_t subgraphIndex,
size_t operatorIndex)
1570 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1572 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1575 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1584 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1585 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1587 std::vector<unsigned int> cropsVector(cropsTensorInfo.
GetNumElements());
1588 ::memcpy(cropsVector.data(), cropsBufferPtr->data.data(), cropsTensorInfo.
GetNumBytes());
1591 std::vector<std::pair<unsigned int, unsigned int>> crops;
1592 for (
unsigned int i = 0; i < cropsTensorInfo.
GetNumElements() / step; ++i)
1594 crops.emplace_back(cropsVector[i * step], cropsVector[i * step + 1]);
1602 auto layerName = fmt::format(
"BatchToSpaceND:{}:{}", subgraphIndex, operatorIndex);
1606 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1608 IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(desc, layerName.c_str());
1612 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1613 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1615 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1616 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1619 void TfLiteParserImpl::ParseL2Normalization(
size_t subgraphIndex,
size_t operatorIndex)
1621 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1623 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1626 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1631 auto layerName = fmt::format(
"L2Normalization:{}:{}", subgraphIndex, operatorIndex);
1632 IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(desc, layerName.c_str());
1639 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1640 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::ParseMaxPool2D(
size_t subgraphIndex,
size_t operatorIndex)
1648 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Max);
1651 void TfLiteParserImpl::ParseMaximum(
size_t subgraphIndex,
size_t operatorIndex)
1653 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1655 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1658 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1661 auto layerName = fmt::format(
"Maximum:{}:{}", subgraphIndex, operatorIndex);
1665 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName,
"Input 0",
"Input 1");
1668 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1674 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1675 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1677 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1678 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1681 void TfLiteParserImpl::ParseMinimum(
size_t subgraphIndex,
size_t operatorIndex)
1683 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1685 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1688 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1691 auto layerName = fmt::format(
"Minimum:{}:{}", subgraphIndex, operatorIndex);
1695 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName,
"Input 0",
"Input 1");
1698 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1704 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1705 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1707 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1708 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1711 void TfLiteParserImpl::ParsePool(
size_t subgraphIndex,
1712 size_t operatorIndex,
1715 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1717 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1718 const auto* options = operatorPtr->builtin_options.AsPool2DOptions();
1722 std::string layerName;
1726 case PoolingAlgorithm::Average:
1728 fmt::format(
"AveragePool2D:{}:{}", subgraphIndex, operatorIndex);
1730 case PoolingAlgorithm::Max:
1732 fmt::format(
"MaxPool2D:{}:{}", subgraphIndex, operatorIndex);
1749 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1754 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1755 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1762 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1766 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1768 IConnectableLayer* layer = m_Network->AddPooling2dLayer(desc, layerName.c_str());
1774 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1775 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1777 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1779 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1780 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1783 void TfLiteParserImpl::ParseSlice(
size_t subgraphIndex,
size_t operatorIndex)
1785 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1787 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1789 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1798 std::vector<unsigned int> begin(beginTensorInfo.
GetNumElements());
1799 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.
GetNumBytes());
1805 std::vector<int> signedSize(sizeTensorInfo.GetNumElements());
1806 ::memcpy(signedSize.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
1807 std::vector<unsigned int> size(sizeTensorInfo.GetNumElements());
1810 for (
unsigned int i = 0; i < signedSize.size(); ++i)
1812 int signedValue = signedSize[i];
1814 if (signedValue < -1 || signedValue > static_cast<int>(inputTensorInfo.GetShape()[i] - begin[i]))
1816 throw ParseException(fmt::format(
"Invalid value for size {} size must be in range " 1817 "[-1, inputDimSize - begin] [-1, {}] inclusive {}",
1819 inputTensorInfo.GetShape()[i] - begin[i],
1823 if (signedValue == -1)
1825 size[i] = inputTensorInfo.GetShape()[i] - begin[i];
1829 size[i] =
static_cast<unsigned int>(signedValue);
1835 auto layerName = fmt::format(
"Slice:{}:{}", subgraphIndex, operatorIndex);
1838 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1840 IConnectableLayer*
const layer = m_Network->AddSliceLayer(desc, layerName.c_str());
1845 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1846 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1849 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1850 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1853 void TfLiteParserImpl::ParseSoftmax(
size_t subgraphIndex,
size_t operatorIndex)
1855 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1856 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1857 const auto* options = operatorPtr->builtin_options.AsSoftmaxOptions();
1860 desc.
m_Beta = options->beta;
1862 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1864 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1867 auto layerName = fmt::format(
"Softmax:{}:{}", subgraphIndex, operatorIndex);
1868 IConnectableLayer*
const layer = m_Network->AddSoftmaxLayer(desc, layerName.c_str());
1875 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1876 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1879 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1880 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1883 void TfLiteParserImpl::ParseLogSoftmax(
size_t subgraphIndex,
size_t operatorIndex)
1885 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1889 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1891 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1894 auto layerName = fmt::format(
"LogSoftmax:{}:{}", subgraphIndex, operatorIndex);
1895 IConnectableLayer*
const layer = m_Network->AddLogSoftmaxLayer(desc, layerName.c_str());
1902 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1903 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1906 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1907 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1910 void TfLiteParserImpl::ParseSpaceToBatchND(
size_t subgraphIndex,
size_t operatorIndex)
1912 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1914 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
1917 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
1926 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1927 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1929 std::vector<unsigned int> padListVector(padListTensorInfo.
GetNumElements());
1930 ::memcpy(padListVector.data(), padListBufferPtr->data.data(), padListTensorInfo.
GetNumBytes());
1933 std::vector<std::pair<unsigned int, unsigned int>> padList;
1934 for (
unsigned int i = 0; i < padListTensorInfo.
GetNumElements() / step; ++i)
1936 padList.emplace_back(padListVector[i * step], padListVector[i * step + 1]);
1944 auto layerName = fmt::format(
"SpaceToBatchND:{}:{}", subgraphIndex, operatorIndex);
1948 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
1950 IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(desc, layerName.c_str());
1954 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1955 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1957 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1958 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1965 static const uint32_t dimensionSequence[] = { 0, 1, 2, 3 };
1969 std::stringstream ss;
1970 ss <<
"Input tensor has unexpected number of dimensions:" << inputTensorInfo.
GetNumDimensions()
1971 <<
" shape:" << inputTensorInfo.
GetShape() <<
" " 1976 if (squeezeDims.empty())
1978 squeezeDims.assign(dimensionSequence,
1982 std::vector<uint32_t> outputDims;
1985 bool skipSqueeze = (std::find(squeezeDims.begin(), squeezeDims.end(), i) == squeezeDims.end());
1986 auto currentDimension = inputTensorInfo.
GetShape()[i];
1987 if (skipSqueeze || currentDimension != 1)
1989 outputDims.push_back(currentDimension);
1993 if (outputDims.size() > 4)
1995 std::stringstream ss;
1996 ss <<
"Output tensor has unexpected number of dimensions:" << inputTensorInfo.
GetNumDimensions()
1997 <<
" shape:" << inputTensorInfo.
GetShape() <<
" " 2009 return outTensorInfo;
2012 void TfLiteParserImpl::ParseShape(
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);
2021 auto layerName = fmt::format(
"Shape:{}:{}", subgraphIndex, operatorIndex);
2036 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
2040 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2041 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2043 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2044 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2047 void TfLiteParserImpl::ParseSqueeze(
size_t subgraphIndex,
size_t operatorIndex)
2049 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2051 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2054 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2057 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2058 const auto * options = operatorPtr->builtin_options.AsSqueezeOptions();
2059 auto layerName = fmt::format(
"Squeeze:{}:{}", subgraphIndex, operatorIndex);
2063 std::vector<uint32_t> squeezeDim;
2066 if (options->squeeze_dims.size() == 1 && options->squeeze_dims[0] < 0)
2069 squeezeDim.push_back(static_cast<uint32_t>(dim));
2073 squeezeDim = AsUnsignedVector(options->squeeze_dims);
2078 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
2083 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
2087 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2088 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2090 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2091 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2094 void TfLiteParserImpl::ParseStridedSlice(
size_t subgraphIndex,
size_t operatorIndex)
2096 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2098 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2101 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2104 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2105 const auto* options = operatorPtr->builtin_options.AsStridedSliceOptions();
2119 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.
GetNumBytes());
2124 std::vector<int> end(endTensorInfo.GetNumElements());
2125 ::memcpy(end.data(), endBufferPtr->data.data(), endTensorInfo.GetNumBytes());
2130 std::vector<int> stride(strideTensorInfo.GetNumElements());
2131 ::memcpy(stride.data(), strideBufferPtr->data.data(), strideTensorInfo.GetNumBytes());
2137 auto layerName = fmt::format(
"StridedSlice:{}:{}", subgraphIndex, operatorIndex);
2138 IConnectableLayer* layer = m_Network->AddStridedSliceLayer(desc, layerName.c_str());
2144 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2145 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2147 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2148 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2151 void TfLiteParserImpl::ParseSub(
size_t subgraphIndex,
size_t operatorIndex)
2153 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2155 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2156 const auto* options = operatorPtr->builtin_options.AsSubOptions();
2158 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2161 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2167 auto layerName = fmt::format(
"Sub:{}:{}", subgraphIndex, operatorIndex);
2174 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2175 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2177 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2179 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2180 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2183 void TfLiteParserImpl::ParseDiv(
size_t subgraphIndex,
size_t operatorIndex)
2185 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2187 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2188 const auto* options = operatorPtr->builtin_options.AsDivOptions();
2190 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2193 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2199 auto layerName = fmt::format(
"Div:{}:{}", subgraphIndex, operatorIndex);
2206 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2207 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2208 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2210 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2211 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2214 void TfLiteParserImpl::ParseFloorDiv(
size_t subgraphIndex,
size_t operatorIndex)
2216 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2218 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2221 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2227 auto layerName = fmt::format(
"Div:{}:{}", subgraphIndex, operatorIndex);
2234 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2235 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2236 layer = AddFusedFloorLayer(layer, 0);
2238 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2239 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2242 void TfLiteParserImpl::ParseAdd(
size_t subgraphIndex,
size_t operatorIndex)
2244 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2246 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2247 const auto* options = operatorPtr->builtin_options.AsAddOptions();
2249 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2252 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2258 auto layerName = fmt::format(
"Add:{}:{}", subgraphIndex, operatorIndex);
2265 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2266 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2267 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2269 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2270 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2273 void TfLiteParserImpl::ParseMul(
size_t subgraphIndex,
size_t operatorIndex)
2275 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2277 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2278 const auto* options = operatorPtr->builtin_options.AsMulOptions();
2280 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2283 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2289 auto layerName = fmt::format(
"Mul:{}:{}", subgraphIndex, operatorIndex);
2290 IConnectableLayer* layer = m_Network->AddMultiplicationLayer(layerName.c_str());
2296 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2297 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2298 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2300 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2301 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2304 void TfLiteParserImpl::ParseMean(
size_t subgraphIndex,
size_t operatorIndex)
2306 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2308 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2310 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2317 std::vector<unsigned int> axis(dimTensorInfo.GetNumElements());
2318 ::memcpy(axis.data(), bufferPtr->data.data(), dimTensorInfo.GetNumBytes());
2328 auto layerName = fmt::format(
"Mean:{}:{}", subgraphIndex, operatorIndex);
2334 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2335 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2337 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2338 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2341 void TfLiteParserImpl::ParsePad(
size_t subgraphIndex,
size_t operatorIndex)
2343 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2353 std::vector<unsigned int> padBuffer = GetUIntBuffer(padTensorInfo, m_Model, inputs[1]->buffer);
2357 auto opcode = GetOpCode(m_Model, subgraphIndex, operatorIndex);
2359 if (opcode == tflite::BuiltinOperator_PAD)
2363 if (inputTensorInfo.IsQuantized())
2365 desc.
m_PadValue =
static_cast<float>(inputTensorInfo.GetQuantizationOffset());
2368 else if (opcode == tflite::BuiltinOperator_PADV2)
2374 if (padValueTensorInfo.GetNumElements() != 1)
2381 if (padValueBufferPtr->data.size() > 0)
2383 switch (padValueTensorInfo.GetDataType())
2387 std::vector<float> padValueBuffer(padValueTensorInfo.GetNumElements());
2388 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2394 std::vector<uint8_t> padValueBuffer(padValueTensorInfo.GetNumElements());
2395 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2396 desc.
m_PadValue = armnn::Dequantize<uint8_t>(padValueBuffer[0],
2397 padValueTensorInfo.GetQuantizationScale(),
2398 padValueTensorInfo.GetQuantizationOffset());
2404 std::vector<int8_t> padValueBuffer(padValueTensorInfo.GetNumElements());
2405 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2406 desc.
m_PadValue = armnn::Dequantize<int8_t>(padValueBuffer[0],
2407 padValueTensorInfo.GetQuantizationScale(),
2408 padValueTensorInfo.GetQuantizationOffset());
2414 else if (inputTensorInfo.IsQuantized())
2416 desc.
m_PadValue =
static_cast<float>(inputTensorInfo.GetQuantizationOffset());
2420 for (
unsigned int i = 0; i < padTensorInfo.
GetNumElements() / step; ++i)
2422 desc.
m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
2425 auto layerName = (opcode == tflite::BuiltinOperator_PAD) ? fmt::format(
"Pad:{}:{}", subgraphIndex, operatorIndex)
2426 : fmt::format(
"PadV2:{}:{}", subgraphIndex, operatorIndex);
2433 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2434 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2436 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2437 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2440 void TfLiteParserImpl::ParseMirrorPad(
size_t subgraphIndex,
size_t operatorIndex)
2442 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2455 std::vector<unsigned int> padBuffer(padTensorInfo.
GetNumElements());
2456 ::memcpy(padBuffer.data(), bufferPtr->data.data(), padTensorInfo.
GetNumBytes());
2460 for (
unsigned int i = 0; i < padTensorInfo.
GetNumElements() / step; ++i)
2462 desc.
m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
2465 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2466 const auto* options = operatorPtr->builtin_options.AsMirrorPadOptions();
2468 if (options->mode == tflite::MirrorPadMode_REFLECT)
2472 else if (options->mode == tflite::MirrorPadMode_SYMMETRIC)
2483 auto inputShape = inputTensorInfo.GetShape();
2486 const unsigned int isReflect =
static_cast<unsigned int>(desc.
m_PaddingMode == PaddingMode::Reflect);
2487 for(
unsigned int i = 0; i < padList.size(); ++i)
2489 if(padList.at(i).first > (inputShape[i] - isReflect) ||
2490 padList.at(i).second > (inputShape[i] - isReflect))
2493 "equal (Symmetric) to the dimension size.");
2497 auto layerName = fmt::format(
"MirrorPad:{}:{}", subgraphIndex, operatorIndex);
2504 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2505 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2507 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2508 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2511 void TfLiteParserImpl::ParsePrelu(
size_t subgraphIndex,
size_t operatorIndex)
2513 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2515 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2518 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2521 auto layerName = fmt::format(
"Prelu:{}:{}", subgraphIndex, operatorIndex);
2526 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
2532 if (IsConstTensor(inputs[1]))
2534 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2536 RegisterConsumerOfTensor(subgraphIndex, inputTensorIndexes[0], slot);
2538 auto alphaTensorAndData = CreateConstTensorNonPermuted(inputs[1], alphaTensorInfo,
2540 std::string constLayerName = fmt::format(
"Constant:{}", inputs[1]->name);
2542 m_Network->AddConstantLayer(alphaTensorAndData.first, constLayerName.c_str());
2547 RegisterOutputSlots(subgraphIndex,
2548 VIRTUAL_OPERATOR_ID,
2550 { inputTensorIndexes[1] });
2554 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2555 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIndexes);
2558 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2559 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2562 void TfLiteParserImpl::ParseQuantize(
size_t subgraphIndex,
size_t operatorIndex)
2564 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2566 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2569 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2572 auto layerName = fmt::format(
"Quantize:{}:{}", subgraphIndex, operatorIndex);
2580 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2581 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2583 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2584 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2587 void TfLiteParserImpl::ParseRelu(
size_t subgraphIndex,
size_t operatorIndex)
2589 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::ReLu);
2592 void TfLiteParserImpl::ParseRelu6(
size_t subgraphIndex,
size_t operatorIndex)
2594 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::BoundedReLu);
2597 void TfLiteParserImpl::ParseLeakyRelu(
size_t subgraphIndex,
size_t operatorIndex)
2599 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::LeakyReLu);
2602 void TfLiteParserImpl::ParseLogistic(
size_t subgraphIndex,
size_t operatorIndex)
2604 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::Sigmoid);
2607 void TfLiteParserImpl::ParseTanH(
size_t subgraphIndex,
size_t operatorIndex)
2609 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::TanH);
2612 void TfLiteParserImpl::ParseElu(
size_t subgraphIndex,
size_t operatorIndex)
2614 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::Elu);
2617 void TfLiteParserImpl::ParseHardSwish(
size_t subgraphIndex,
size_t operatorIndex)
2619 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::HardSwish);
2622 void TfLiteParserImpl::ParseActivation(
size_t subgraphIndex,
size_t operatorIndex,
ActivationFunction activationType)
2624 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2625 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2628 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2631 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2634 auto layerName = fmt::format(
"Activation:");
2638 switch (activationType)
2640 case ActivationFunction::ReLu:
2642 layerName += fmt::format(
"RELU:{}:{}", subgraphIndex, operatorIndex);
2645 case ActivationFunction::BoundedReLu:
2647 layerName += fmt::format(
"RELU6:{}:{}", subgraphIndex, operatorIndex);
2648 activationDesc.
m_A = 6.0f;
2649 activationDesc.
m_B = 0.0f;
2652 case ActivationFunction::Sigmoid:
2654 layerName += fmt::format(
"SIGMOID:{}:{}", subgraphIndex, operatorIndex);
2657 case ActivationFunction::TanH:
2659 layerName += fmt::format(
"TANH:{}:{}", subgraphIndex, operatorIndex);
2660 activationDesc.
m_A = 1.0f;
2661 activationDesc.
m_B = 1.0f;
2664 case ActivationFunction::LeakyReLu:
2666 layerName += fmt::format(
"LEAKYRELU:{}:{}", subgraphIndex, operatorIndex);
2667 const auto* options = operatorPtr->builtin_options.AsLeakyReluOptions();
2668 activationDesc.
m_A = options->alpha;
2671 case ActivationFunction::Elu:
2673 layerName += fmt::format(
"ELU:{}:{}", subgraphIndex, operatorIndex);
2674 activationDesc.
m_A = 1.0f;
2677 case ActivationFunction::HardSwish:
2679 layerName += fmt::format(
"HARDSWISH:{}:{}", subgraphIndex, operatorIndex);
2685 fmt::format(
"Unexpected ActivationFunction[{}] when creating layerName {} ",
2690 IConnectableLayer*
const layer = m_Network->AddActivationLayer(activationDesc, layerName.c_str());
2697 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2698 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2701 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2702 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2705 const std::vector<int32_t>& targetDimsIn)
2707 std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
2708 const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
2710 if (stretchDim != targetDimsIn.end())
2712 if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
2715 fmt::format(
"At most one component of shape can be -1 {}",
CHECK_LOCATION().AsString()));
2718 auto targetNumElements =
2720 std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
2722 auto stretchIndex =
static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
2723 outputDims[stretchIndex] = inputTensorInfo.
GetNumElements() / targetNumElements;
2734 void TfLiteParserImpl::ParseReshape(
size_t subgraphIndex,
size_t operatorIndex)
2736 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2738 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2740 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2743 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2744 const auto* options = operatorPtr->builtin_options.AsReshapeOptions();
2745 auto layerName = fmt::format(
"Reshape:{}:{}", subgraphIndex, operatorIndex);
2749 CheckMatchingQuantization(inputTensorInfo, actualOutputTensorInfo, layerName,
"Input 0",
"Output 0");
2755 std::vector<int32_t> targetShape;
2756 bool targetShapeFound =
false;
2758 if (options !=
nullptr)
2761 if (options->new_shape.empty() ==
false)
2763 targetShape = options->new_shape;
2764 targetShapeFound =
true;
2769 if (!targetShapeFound)
2772 if (inputs.size() > 1 && inputs[1] !=
nullptr)
2774 if (inputs[1]->is_variable)
2779 if (inputs[1]->shape.size() != 1)
2784 if (inputs[1]->type != tflite::TensorType_INT32)
2790 auto bufferPtr =
GetBuffer(m_Model, inputs[1]->buffer);
2791 auto values =
reinterpret_cast<const int32_t*
>(bufferPtr->data.data());
2794 for (
int i = 0; i < inputs[1]->shape[0]; ++i)
2796 targetShape.push_back(values[i]);
2806 if (reshapeShapes[0] > 2)
2808 throw ParseException(fmt::format(
"Invalid input shape '{}' in Reshape layer '{}' {}. " 2809 "When inferring during runtime, the parser only supports " 2810 "shape (batch, -1) or (-1) for target shape input.",
2816 const int32_t numInputElements = inputTensorInfo.
GetNumElements();
2817 const int32_t inputTensorShape = inputTensorInfo.
GetShape()[0];
2818 if (reshapeShapes[0] == 1)
2820 targetShape = {numInputElements};
2822 else if (reshapeShapes[0] == 2)
2824 targetShape = {inputTensorShape, numInputElements / inputTensorShape};
2827 catch (
const std::exception& exc)
2830 "Reshape operation. Reshape operator target shape input buffer data " 2831 "is null. " << exc.what());
2838 "At least one method required");
2847 if (inputs.size() > 1 && !
CheckShape(reshapeOutputTensorShape, outputs[0]->shape))
2849 std::stringstream ss;
2850 ss <<
"New shape defined in reshape parameters " 2851 << reshapeOutputTensorShape
2852 <<
" does not equal output shape " 2853 << actualOutputTensorInfo.
GetShape()
2862 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
2866 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2867 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2869 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2870 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2873 void TfLiteParserImpl::ParseResizeBilinear(
size_t subgraphIndex,
size_t operatorIndex)
2875 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::Bilinear);
2878 void TfLiteParserImpl::ParseResizeNearestNeighbor(
size_t subgraphIndex,
size_t operatorIndex)
2880 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::NearestNeighbor);
2883 void TfLiteParserImpl::ParseResize(
size_t subgraphIndex,
size_t operatorIndex,
ResizeMethod resizeMethod)
2885 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2887 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2890 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2896 std::vector<int32_t> sizeTensorData(sizeTensorInfo.GetNumElements());
2899 ::memcpy(sizeTensorData.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
2903 desc.m_TargetHeight =
static_cast<uint32_t
> (sizeTensorData[0]);
2904 desc.m_TargetWidth =
static_cast<uint32_t
> (sizeTensorData[1]);
2907 auto layerName = fmt::format(
"Resize:");
2909 switch (resizeMethod)
2911 case ResizeMethod::Bilinear:
2913 layerName += fmt::format(
"BILINEAR:{}:{}", subgraphIndex, operatorIndex);
2915 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2916 const auto * options = operatorPtr->builtin_options.AsResizeBilinearOptions();
2918 desc.m_AlignCorners = options->align_corners;
2921 case ResizeMethod::NearestNeighbor:
2923 layerName += fmt::format(
"NEARESTNEIGHBOR:{}:{}", subgraphIndex, operatorIndex);
2929 fmt::format(
"Unexpected ResizeMethod[{}] when creating layerName {} ",
2936 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName,
"Input 0",
"Output 0");
2942 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2943 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2945 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2946 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2949 void TfLiteParserImpl::ParseConcatenation(
size_t subgraphIndex,
size_t operatorIndex)
2951 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2953 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2954 const auto* options = operatorPtr->builtin_options.AsConcatenationOptions();
2958 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
2959 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
2962 unsigned int numConcatView =
static_cast<unsigned int>(inputs.size());
2965 const unsigned int concatDimInput =
static_cast<unsigned int>(
2966 (
static_cast<int>(inputRank) + options->axis) %
static_cast<int>(inputRank));
2968 OriginsDescriptor concatDescriptor(static_cast<uint32_t>(numConcatView), inputRank);
2971 unsigned int mergeDimOrigin = 0;
2973 for (
unsigned int viewIndex = 0; viewIndex < numConcatView; ++viewIndex)
2979 inputTensorInfo, concatDescriptor, concatDimInput, viewIndex, mergeDimOrigin);
2982 auto layerName = fmt::format(
"Concatenation:{}:{}", subgraphIndex, operatorIndex);
2985 IConnectableLayer* layer = m_Network->AddConcatLayer(concatDescriptor, layerName.c_str());
2989 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2990 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
2993 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2995 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2996 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2999 void TfLiteParserImpl::ParseFullyConnected(
size_t subgraphIndex,
size_t operatorIndex)
3001 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3003 const auto& operatorRfr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3004 const auto options = operatorRfr->builtin_options.AsFullyConnectedOptions();
3012 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3013 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3019 int32_t weightsDimension =
static_cast<int32_t
>(filterTensorInfo.GetNumDimensions());
3020 if (weightsDimension != 2)
3023 fmt::format(
"Dimension {} for Fully Connected weights is not supported by Armnn. " 3030 auto layerName = fmt::format(
"FullyConnected:{}:{}", subgraphIndex, operatorIndex);
3032 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3034 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0]};
3035 std::vector<unsigned int> ignoreInputWhenRegister = {};
3041 tensorIndexesToRegister.emplace_back(inputTensorIndexes[1]);
3044 (filterTensorInfo.GetDataType() == DataType::QAsymmU8 ||
3045 filterTensorInfo.GetDataType() == DataType::QAsymmS8))
3047 m_ConstantsToDequantize.emplace_back(inputs[1]->buffer);
3050 if (inputs.size() == 3)
3056 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
3059 (biasTensorInfo.
GetDataType() == DataType::QAsymmU8 ||
3060 biasTensorInfo.
GetDataType() == DataType::QAsymmS8))
3062 m_ConstantsToDequantize.emplace_back(inputs[2]->buffer);
3067 layer = m_Network->AddFullyConnectedLayer(desc, layerName.c_str());
3071 unsigned int startingSlotIndex = 0;
3078 std::vector<unsigned int> reshapedDimensions(2);
3079 reshapedDimensions[1] = filterTensorInfo.GetShape()[1];
3080 reshapedDimensions[0] = inputTensorInfo.
GetNumElements() / reshapedDimensions[1];
3082 if (inputTensorInfo.
GetNumElements() % reshapedDimensions[1] != 0)
3085 fmt::format(
"Failed to deduce input tensor shape from filter size {} {}",
3086 reshapedDimensions[1],
3093 std::string reshapeLayerName = fmt::format(
"Reshape_for:{}", layer->
GetName());
3101 RegisterInputSlots(subgraphIndex, operatorIndex, reshapeLayer, {inputTensorIndexes[0]});
3103 tensorIndexesToRegister.erase(tensorIndexesToRegister.begin());
3104 startingSlotIndex = 1;
3107 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister, startingSlotIndex);
3114 options->fused_activation_function);
3117 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3118 RegisterOutputSlots(subgraphIndex, operatorIndex, fusedActivationLayer, {outputTensorIndexes[0]});
3121 void TfLiteParserImpl::ParseDetectionPostProcess(
size_t subgraphIndex,
size_t operatorIndex)
3123 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3125 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3127 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3128 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3132 auto custom_options = operatorPtr->custom_options;
3133 const flexbuffers::Map& m = flexbuffers::GetRoot(custom_options.data(), custom_options.size()).AsMap();
3142 desc.
m_ScaleH = m[
"h_scale"].AsFloat();
3143 desc.
m_ScaleW = m[
"w_scale"].AsFloat();
3144 desc.
m_ScaleX = m[
"x_scale"].AsFloat();
3145 desc.
m_ScaleY = m[
"y_scale"].AsFloat();
3147 if (!(m[
"use_regular_nms"].IsNull()))
3151 if (!(m[
"detections_per_class"].IsNull()))
3159 "must be positive and less than or equal to 1.");
3163 auto anchorTensorAndData = CreateConstTensorNonPermuted(inputs[2], anchorTensorInfo);
3165 auto layerName = fmt::format(
"DetectionPostProcess:{}:{}", subgraphIndex, operatorIndex);
3166 IConnectableLayer* layer = m_Network->AddDetectionPostProcessLayer(desc, anchorTensorAndData,
3174 m_OverridenOutputShapes.push_back({ 1, numDetectedBox, 4 });
3175 m_OverridenOutputShapes.push_back({ 1, numDetectedBox });
3176 m_OverridenOutputShapes.push_back({ 1, numDetectedBox });
3177 m_OverridenOutputShapes.push_back({ 1 });
3179 for (
unsigned int i = 0 ; i < outputs.size() ; ++i)
3187 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3188 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3191 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3192 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0],
3193 outputTensorIndexes[1],
3194 outputTensorIndexes[2],
3195 outputTensorIndexes[3]});
3199 void TfLiteParserImpl::ParsePack(
size_t subgraphIndex,
size_t operatorIndex)
3201 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3203 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3204 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3207 if (inputs.size() < 1)
3212 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3213 const auto* options = operatorPtr->builtin_options.AsPackOptions();
3216 desc.
m_Axis =
static_cast<uint32_t
>(options->axis);
3217 desc.
m_NumInputs =
static_cast<uint32_t
>(inputs.size());
3223 auto layerName = fmt::format(
"Pack:{}:{}", subgraphIndex, operatorIndex);
3231 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3232 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
3234 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3235 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3238 void TfLiteParserImpl::ParseUnidirectionalSequenceLSTM(
size_t subgraphIndex,
size_t operatorIndex)
3240 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3242 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3243 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3245 if (inputs.size() < 2)
3247 throw ParseException(
"UnidirectionalSequenceLSTM must have at least 2 input.");
3250 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3251 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
3252 const auto nodeParams = operatorPtr->builtin_options.AsUnidirectionalSequenceLSTMOptions();
3262 if (IsOptionalOperandPresent(operatorPtr->inputs[1]))
3264 params.
m_InputToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[1]].get(),
3265 inputTensorInfo).first;
3269 inputTensorInfo).first;
3270 params.
m_InputToCellWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[3]].get(),
3271 inputTensorInfo).first;
3273 inputTensorInfo).first;
3276 if (IsOptionalOperandPresent(operatorPtr->inputs[5]))
3279 inputTensorInfo).first;
3283 inputTensorInfo).first;
3285 inputTensorInfo).first;
3287 inputTensorInfo).first;
3290 if (IsOptionalOperandPresent(operatorPtr->inputs[9]))
3292 params.
m_CellToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[9]].get(),
3293 inputTensorInfo).first;
3296 if (IsOptionalOperandPresent(operatorPtr->inputs[10]))
3298 params.
m_CellToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[10]].get(),
3299 inputTensorInfo).first;
3302 if (IsOptionalOperandPresent(operatorPtr->inputs[11]))
3304 params.
m_CellToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[11]].get(),
3305 inputTensorInfo).first;
3309 if (IsOptionalOperandPresent(operatorPtr->inputs[12]))
3311 params.
m_InputGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[12]].get(),
3312 inputTensorInfo).first;
3315 params.
m_ForgetGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[13]].get(),
3316 inputTensorInfo).first;
3317 params.
m_CellBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[14]].get(),
3318 inputTensorInfo).first;
3319 params.
m_OutputGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[15]].get(),
3320 inputTensorInfo).first;
3323 if (IsOptionalOperandPresent(operatorPtr->inputs[16]))
3325 params.
m_ProjectionWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[16]].get(),
3326 inputTensorInfo).first;
3329 if (IsOptionalOperandPresent(operatorPtr->inputs[17]))
3331 params.
m_ProjectionBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[17]].get(),
3332 inputTensorInfo).first;
3337 m_ConstantsToBeCreated.push_back(operatorPtr->inputs[18]);
3339 m_ConstantsToBeCreated.push_back(operatorPtr->inputs[19]);
3342 if (inputs.size() >= 21 && IsOptionalOperandPresent(operatorPtr->inputs[20]))
3345 inputTensorInfo).first;
3348 if (inputs.size() >= 22 && IsOptionalOperandPresent(operatorPtr->inputs[21]))
3351 inputTensorInfo).first;
3354 if (inputs.size() >= 23 && IsOptionalOperandPresent(operatorPtr->inputs[22]))
3356 params.
m_CellLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[22]].get(),
3357 inputTensorInfo).first;
3360 if (inputs.size() >= 24 && IsOptionalOperandPresent(operatorPtr->inputs[23]))
3363 inputTensorInfo).first;
3369 desc.m_ClippingThresCell = nodeParams->cell_clip;
3370 desc.m_ClippingThresProj = nodeParams->proj_clip;
3380 desc.m_TimeMajor = nodeParams->time_major;
3382 if (operatorPtr->intermediates.size() > 3 && desc.m_LayerNormEnabled)
3384 auto inputIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[0]].get(),
3385 inputTensorInfo).first;
3386 auto inputIntermediateTensorInfo = inputIntermediate->GetInfo();
3387 desc.m_InputIntermediateScale = inputIntermediateTensorInfo.GetQuantizationScale();
3389 auto forgetIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[1]].get(),
3390 inputTensorInfo).first;
3391 auto forgetIntermediateTensorInfo = forgetIntermediate->GetInfo();
3392 desc.m_ForgetIntermediateScale = forgetIntermediateTensorInfo.GetQuantizationScale();
3394 auto cellIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[2]].get(),
3395 inputTensorInfo).first;
3396 auto cellIntermediateTensorInfo = cellIntermediate->GetInfo();
3397 desc.m_CellIntermediateScale = cellIntermediateTensorInfo.GetQuantizationScale();
3399 auto outputIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[3]].get(),
3400 inputTensorInfo).first;
3401 auto outputIntermediateTensorInfo = outputIntermediate->GetInfo();
3402 desc.m_OutputIntermediateScale = outputIntermediateTensorInfo.GetQuantizationScale();
3406 float defaultIntermediate = std::pow(2, -12);
3407 desc.m_InputIntermediateScale = defaultIntermediate;
3408 desc.m_ForgetIntermediateScale = defaultIntermediate;
3409 desc.m_CellIntermediateScale = defaultIntermediate;
3410 desc.m_OutputIntermediateScale = defaultIntermediate;
3413 if (operatorPtr->intermediates.size() > 4)
3415 auto hiddentensor = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[4]].get(),
3416 inputTensorInfo).first;
3418 desc.m_HiddenStateScale = hiddentensor->GetInfo().GetQuantizationScale();
3419 desc.m_HiddenStateZeroPoint = hiddentensor->GetInfo().GetQuantizationOffset();
3421 unsigned int batchSize = inputTensorInfo.GetShape()[0];
3422 unsigned int outputSize = outputTensorInfo.GetShape()[2];
3423 unsigned int numUnits = cellStateInInfo.
GetShape()[1];
3426 float qScale = inputTensorInfo.GetQuantizationScale();
3427 float qOffset = inputTensorInfo.GetQuantizationOffset();
3429 armnn::TensorInfo scratchBufferTensorInfo({batchSize, numUnits * 3}, dataType, qScale, qOffset);
3430 if (!desc.m_CifgEnabled)
3432 scratchBufferTensorInfo =
armnn::TensorInfo({batchSize, numUnits * 4}, dataType, qScale, qOffset);
3438 armnn::TensorInfo outputStateOutTensorInfo({batchSize, outputSize}, dataType, qScale, qOffset);
3451 if (!desc.m_CifgEnabled)
3462 if (desc.m_ProjectionEnabled)
3471 if (desc.m_PeepholeEnabled)
3477 if (desc.m_LayerNormEnabled)
3479 if(!desc.m_CifgEnabled)
3488 auto layerName = fmt::format(
"UnidirectionalSequenceLSTM:{}:{}", subgraphIndex, operatorIndex);
3494 auto inputTensorIndexes = AsUnsignedVector({operatorPtr->inputs[0],
3495 operatorPtr->inputs[18],
3496 operatorPtr->inputs[19]});
3497 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0],
3498 inputTensorIndexes[1],
3499 inputTensorIndexes[2]});
3501 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3507 unsigned int tensorIndex = outputTensorIndexes[0];
3509 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
3512 void TfLiteParserImpl::ParseUnpack(
size_t subgraphIndex,
size_t operatorIndex)
3514 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3516 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3517 const auto* options = operatorPtr->builtin_options.AsUnpackOptions();
3522 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3527 if (unpackAxis >= inputTensorInfo.GetNumDimensions())
3530 fmt::format(
"The unpack axis: {} cannot be greater than or equal to " 3531 "the number of input dimension {} {}",
3533 inputTensorInfo.GetNumDimensions(),
3541 unpackNum = inputTensorInfo.GetShape()[unpackAxis];
3547 throw ParseException(
"Number to unpack must greater than zero.");
3550 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3553 auto inputDimSize = inputTensorInfo.GetNumDimensions();
3554 std::vector<unsigned int> unpackDimSizes(inputDimSize);
3557 for (
unsigned int i = 0; i < inputDimSize; ++i)
3559 unpackDimSizes[i] = inputTensorInfo.GetShape()[i];
3562 if (unpackDimSizes[unpackAxis] != unpackNum)
3564 throw ParseException(
"Number to unpack must be the same as length of the dimension to " 3568 unpackDimSizes[unpackAxis] /= unpackNum;
3570 SplitterDescriptor splitDesc(unpackNum, static_cast<unsigned int>(unpackDimSizes.size()));
3571 for (
unsigned int j = 0; j < unpackNum; ++j)
3574 for (
unsigned int dimIdx = 0; dimIdx < unpackDimSizes.size(); ++dimIdx)
3576 splitDesc.
SetViewSize(j, dimIdx, unpackDimSizes[dimIdx]);
3581 auto layerName = fmt::format(
"Unpack:{}:{}", subgraphIndex, operatorIndex);
3582 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
3586 unpackDimSizes.data());
3588 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3589 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3591 std::vector<unsigned int> reshapeDims;
3592 for (
unsigned int axis = 0; axis < splitOutShape.
GetNumDimensions(); ++axis)
3594 if (axis != unpackAxis)
3596 reshapeDims.push_back(splitOutShape[axis]);
3606 std::string reshapeLayerName = fmt::format(
"Reshape_for:{}", layer->
GetName());
3621 RegisterProducerOfTensor(subgraphIndex, reshapedOutputId, slot);
3625 void TfLiteParserImpl::ParseSplit(
size_t subgraphIndex,
size_t operatorIndex)
3627 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3629 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3630 const auto* options = operatorPtr->builtin_options.AsSplitOptions();
3637 throw ParseException(
"Number to splits must greater than zero.");
3640 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3642 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3650 if (axisBufferPtr ==
nullptr)
3653 fmt::format(
"Operation has invalid inputs. Failed to read axis. {}",
3658 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.
GetNumBytes());
3659 int32_t axis = axisData[0];
3661 auto inputDimensions =
static_cast<int32_t
>(inputTensorInfo.GetNumDimensions());
3662 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3668 fmt::format(
"Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
3675 auto inputDimSize = inputTensorInfo.GetNumDimensions();
3679 fmt::format(
"The number of dimensions: {} for input tensors of the split op cannot be greater than {} {}",
3680 inputTensorInfo.GetNumDimensions(),
3685 std::vector<unsigned int> splitterDimSizes(inputDimSize);
3688 for (
unsigned int i = 0; i < inputDimSize; ++i)
3690 splitterDimSizes[i] = inputTensorInfo.GetShape()[i];
3693 if (splitterDimSizes[splitDim] % numSplits != 0)
3695 throw ParseException(
"Number of splits must evenly divide the dimension");
3697 splitterDimSizes[splitDim] /= numSplits;
3700 for (
unsigned int j = 0; j < numSplits; ++j)
3703 for (
unsigned int dimIdx = 0; dimIdx < splitterDimSizes.size(); ++dimIdx)
3705 splitDesc.
SetViewSize(j, dimIdx, splitterDimSizes[dimIdx]);
3710 auto layerName = fmt::format(
"Split:{}:{}", subgraphIndex, operatorIndex);
3711 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
3714 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3715 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[1]});
3723 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3724 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3730 int v = idx < 0 ? numDims + idx : idx;
3734 return static_cast<unsigned int>(v);
3737 void TfLiteParserImpl::ParseSplitV(
size_t subgraphIndex,
size_t operatorIndex)
3739 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3741 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3742 const auto* options = operatorPtr->builtin_options.AsSplitVOptions();
3744 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3747 auto& inputTensor = inputs[0];
3748 auto& splitsTensor = inputs[1];
3749 auto& axisTensor = inputs[2];
3761 fmt::format(
"The number of dimensions: {} for input tensors of the " 3762 "SplitV op cannot be greater than {} {}",
3770 if (axisBufferPtr ==
nullptr)
3773 fmt::format(
"Operation has invalid inputs. Failed to read axis. {}",
3778 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.
GetNumBytes());
3779 int32_t axis = axisData[0];
3781 auto inputDimensions =
static_cast<int32_t
>(inputTensorInfo.
GetNumDimensions());
3782 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3788 fmt::format(
"Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
3796 unsigned int numSplits{0};
3812 std::vector<int> splitsData(numSplits);
3814 ::memcpy(splitsData.data(), splitsBufferPtr->data.data(), splitsInfo.
GetNumBytes());
3816 unsigned int idx = 0;
3818 unsigned int inferIdx{0};
3820 for (
auto split : splitsData)
3834 if (numInferred == 0)
3836 if (splitSum != armnn::numeric_cast<int>(inputTensorInfo.
GetShape()[splitDim]))
3838 throw ParseException(
"SplitV split_sizes does not sum to the dimension of value along split_dim.");
3841 else if (numInferred == 1)
3847 throw ParseException(
"Cannot infer split size for more than one split");
3851 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3856 unsigned int accumSplit = 0;
3857 for (
unsigned int j = 0; j < numSplits; ++j)
3862 for (
unsigned int dimIdx = 0; dimIdx < inputTensorInfo.
GetNumDimensions(); ++dimIdx)
3864 unsigned int dimSize = inputTensorInfo.
GetShape()[dimIdx];
3865 if (dimIdx == splitDim)
3867 dimSize = splitSize;
3869 splitDesc.SetViewSize(j, dimIdx, dimSize);
3872 splitDesc.SetViewOriginCoord(j, splitDim, accumSplit);
3873 accumSplit += splitSize;
3876 auto layerName = fmt::format(
"SplitV:{}:{}", subgraphIndex, operatorIndex);
3877 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
3880 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3881 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3889 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3890 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3893 void TfLiteParserImpl::ParseArgMin(
size_t subgraphIndex,
size_t operatorIndex)
3898 void TfLiteParserImpl::ParseArgMax(
size_t subgraphIndex,
size_t operatorIndex)
3903 void TfLiteParserImpl::ParseArgMinMax(
size_t subgraphIndex,
size_t operatorIndex,
ArgMinMaxFunction argMinMaxFunction)
3905 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3906 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
3909 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
3923 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
3929 if (axisBufferPtr ==
nullptr)
3932 fmt::format(
"Operation has invalid inputs. Failed to read axis. {}",
3937 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.
GetNumBytes());
3938 int32_t axis = axisData.front();
3940 auto inputDimensions =
static_cast<int32_t
>(inputTensorInfo.GetNumDimensions());
3941 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3947 fmt::format(
"Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
3957 auto layerName = argMinMaxFunction == ArgMinMaxFunction::Max ?
"ArgMax:{}:{}" :
"ArgMin:{}:{}";
3958 auto layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
3959 IConnectableLayer *layer = m_Network->AddArgMinMaxLayer(desc, layerNameFormatted.c_str());
3964 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3965 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3968 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3969 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3972 void TfLiteParserImpl::ParseGather(
size_t subgraphIndex,
size_t operatorIndex)
3974 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3987 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3988 const auto* options = operatorPtr->builtin_options.AsGatherOptions();
3989 auto axis = options->axis;
3991 auto inputDimensions =
static_cast<int32_t
>(inputTensorInfo.GetNumDimensions());
3994 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3997 fmt::format(
"Operation has invalid axis: {} It is out of bounds [ -{}, {} ) {}",
3999 inputDimensions, inputDimensions,
4002 if (outputDimensions != static_cast<unsigned int>(inputDimensions) + indicesDimensions - 1)
4005 fmt::format(
"Operation has invalid output dimensions: {} Output must be an ({} + {} - 1) -D tensor {}",
4007 inputDimensions, indicesDimensions,
4011 gatherDescriptor.
m_Axis = axis;
4013 auto layerName = fmt::format(
"Gather:{}:{}", subgraphIndex, operatorIndex);
4014 IConnectableLayer* layer = m_Network->AddGatherLayer(gatherDescriptor, layerName.c_str());
4018 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4019 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4021 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4022 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4025 void TfLiteParserImpl::ParseGatherNd(
size_t subgraphIndex,
size_t operatorIndex)
4027 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4038 auto layerName = fmt::format(
"GatherNd:{}:{}", subgraphIndex, operatorIndex);
4043 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4044 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4046 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4047 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4050 void TfLiteParserImpl::ParseDepthToSpace(
size_t subgraphIndex,
size_t operatorIndex)
4052 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4061 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4062 const auto* options = operatorPtr->builtin_options.AsDepthToSpaceOptions();
4063 auto blockSize = options->block_size;
4067 fmt::format(
"Operation has invalid block size: {} Block size should be >= 2 {}",
4073 auto layerName = fmt::format(
"DepthToSpace:{}:{}", subgraphIndex, operatorIndex);
4074 IConnectableLayer* layer = m_Network->AddDepthToSpaceLayer(descriptor, layerName.c_str());
4079 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4080 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4082 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4083 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4086 void TfLiteParserImpl::ParseSum(
size_t subgraphIndex,
size_t operatorIndex)
4091 void TfLiteParserImpl::ParseReduceProd(
size_t subgraphIndex,
size_t operatorIndex)
4096 void TfLiteParserImpl::ParseReduceMax(
size_t subgraphIndex,
size_t operatorIndex)
4101 void TfLiteParserImpl::ParseReduceMin(
size_t subgraphIndex,
size_t operatorIndex)
4106 void TfLiteParserImpl::ParseReduce(
size_t subgraphIndex,
size_t operatorIndex,
ReduceOperation reduceOperation)
4108 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4110 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4111 const auto* options = operatorPtr->builtin_options.AsReducerOptions();
4113 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
4116 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
4119 auto layerName = fmt::format(
"Reduce:{}:{}", subgraphIndex, operatorIndex);
4127 if (axisBufferPtr !=
nullptr)
4129 std::vector<int32_t> axisData(inputTensorInfo1.
GetNumElements());
4130 ::memcpy(axisData.data(), axisBufferPtr->data.data(), inputTensorInfo1.
GetNumBytes());
4134 std::set<unsigned int> uniqueAxis;
4135 std::transform(axisData.begin(),
4137 std::inserter(uniqueAxis, uniqueAxis.begin()),
4138 [rank](
int i)->unsigned
int{
4139 return static_cast<uint32_t
>(((i + rank) % rank)); });
4140 desc.
m_vAxis.assign(uniqueAxis.begin(), uniqueAxis.end());
4160 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4161 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4164 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4165 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4168 void TfLiteParserImpl::ParseLocalResponseNormalization(
size_t subgraphIndex,
size_t operatorIndex)
4170 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4172 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
4175 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
4178 auto layerName = fmt::format(
"LRN:{}:{}", subgraphIndex, operatorIndex);
4179 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4183 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4184 const auto* options = operatorPtr->builtin_options.AsLocalResponseNormalizationOptions();
4190 descriptor.
m_NormSize =
static_cast<uint32_t
>(options->radius);
4191 descriptor.
m_K = options->bias;
4192 descriptor.
m_Alpha = options->alpha;
4193 descriptor.
m_Beta = options->beta;
4199 IConnectableLayer* layer = m_Network->AddNormalizationLayer(descriptor, layerNameFormatted.c_str());
4205 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4206 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4208 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4209 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4212 void TfLiteParserImpl::ParseAbs(
size_t subgraphIndex,
size_t operatorIndex)
4217 void TfLiteParserImpl::ParseExp(
size_t subgraphIndex,
size_t operatorIndex)
4222 void TfLiteParserImpl::ParseLog(
size_t subgraphIndex,
size_t operatorIndex)
4227 void TfLiteParserImpl::ParseLogicalNot(
size_t subgraphIndex,
size_t operatorIndex)
4232 void TfLiteParserImpl::ParseNeg(
size_t subgraphIndex,
size_t operatorIndex)
4237 void TfLiteParserImpl::ParseRsqrt(
size_t subgraphIndex,
size_t operatorIndex)
4242 void TfLiteParserImpl::ParseSin(
size_t subgraphIndex,
size_t operatorIndex)
4247 void TfLiteParserImpl::ParseSqrt(
size_t subgraphIndex,
size_t operatorIndex)
4252 void TfLiteParserImpl::ParseElementwiseUnary(
size_t subgraphIndex,
size_t operatorIndex,
UnaryOperation unaryOperation)
4254 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4256 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
4259 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
4263 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4267 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(desc, layerNameFormatted.c_str());
4273 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4274 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4276 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4277 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4280 void TfLiteParserImpl::ParseEqual(
size_t subgraphIndex,
size_t operatorIndex)
4285 void TfLiteParserImpl::ParseNotEqual(
size_t subgraphIndex,
size_t operatorIndex)
4290 void TfLiteParserImpl::ParseGreater(
size_t subgraphIndex,
size_t operatorIndex)
4295 void TfLiteParserImpl::ParseGreaterOrEqual(
size_t subgraphIndex,
size_t operatorIndex)
4300 void TfLiteParserImpl::ParseLess(
size_t subgraphIndex,
size_t operatorIndex)
4305 void TfLiteParserImpl::ParseLessOrEqual(
size_t subgraphIndex,
size_t operatorIndex)
4310 void TfLiteParserImpl::ParseComparison(
size_t subgraphIndex,
size_t operatorIndex,
4313 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4315 auto inputs =
GetInputs(m_Model, subgraphIndex, operatorIndex);
4318 auto outputs =
GetOutputs(m_Model, subgraphIndex, operatorIndex);
4322 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4326 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerNameFormatted,
"Input 0",
"Input 1");
4330 IConnectableLayer* layer = m_Network->AddComparisonLayer(desc, layerNameFormatted.c_str());
4336 auto inputTensorIndexes = AsUnsignedVector(
GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4337 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4339 auto outputTensorIndexes = AsUnsignedVector(
GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4340 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4344 unsigned int outputSlot,
4345 tflite::ActivationFunctionType activationType)
4348 std::string layerName = prevLayer->
GetName();
4350 switch(activationType)
4352 case tflite::ActivationFunctionType_NONE:
4357 case tflite::ActivationFunctionType_RELU:
4359 activationDesc.
m_Function = ActivationFunction::ReLu;
4360 layerName +=
":RELU";
4363 case tflite::ActivationFunctionType_RELU6:
4365 activationDesc.
m_Function = ActivationFunction::BoundedReLu;
4366 activationDesc.
m_A = 6.0f;
4367 activationDesc.
m_B = 0.0f;
4368 layerName +=
":RELU6";
4371 case tflite::ActivationFunctionType_TANH:
4373 activationDesc.
m_Function = ActivationFunction::TanH;
4374 activationDesc.
m_A = 1.0f;
4375 activationDesc.
m_B = 1.0f;
4376 layerName +=
":TANH";
4381 case tflite::ActivationFunctionType_RELU_N1_TO_1:
4382 case tflite::ActivationFunctionType_SIGN_BIT:
4386 fmt::format(
"TfLite parser doesn't suppport fused activation: " 4389 tflite::EnumNameActivationFunctionType(activationType),
4396 m_Network->AddActivationLayer(activationDesc, layerName.c_str());
4398 auto & prevOutputSlot = prevLayer->
GetOutputSlot(outputSlot);
4401 return activationLayer;
4405 unsigned int outputSlot)
4408 auto& prevOutputSlot = prevLayer->
GetOutputSlot(outputSlot);
4411 if (dataType == DataType::Signed32)
4416 std::string layerName = prevLayer->
GetName();
4427 if (fileName ==
nullptr)
4432 std::error_code errorCode;
4433 fs::path pathToFile(fileName);
4434 if (!fs::exists(pathToFile, errorCode))
4437 std::stringstream msg;
4438 msg <<
"Cannot find the file (" << fileName <<
") errorCode: " << errorCode
4443 std::ifstream file(fileName, std::ios::binary);
4444 std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
4446 fileContent.size());
4451 if (binaryContent ==
nullptr)
4456 flatbuffers::Verifier verifier(binaryContent, len);
4457 if (verifier.VerifyBuffer<tflite::Model>() ==
false)
4460 fmt::format(
"Buffer doesn't conform to the expected Tensorflow Lite " 4461 "flatbuffers format. size:{} {}",
4465 return tflite::UnPackModel(binaryContent);
4469 size_t subgraphIndex,
4470 size_t operatorIndex)
4474 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4475 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
4477 size_t inputCount = operatorPtr->inputs.size();
4479 for (
size_t i = 0; i < inputCount; ++i)
4482 if (operatorPtr->inputs[i] == -1)
4489 result.push_back(subgraphPtr->tensors[inputId].get());
4496 size_t subgraphIndex,
4497 size_t operatorIndex)
4501 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4502 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
4504 size_t outputCount = operatorPtr->outputs.size();
4506 for (
size_t i = 0; i < outputCount; ++i)
4510 result[i] = subgraphPtr->tensors[outputId].get();
4516 size_t subgraphIndex)
4519 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4521 size_t inputCount = subgraphPtr->inputs.size();
4523 for (
size_t i = 0; i < inputCount; ++i)
4527 result[i] = std::make_pair(inputId, subgraphPtr->tensors[inputId].get());
4533 size_t subgraphIndex)
4536 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4538 size_t outputCount = subgraphPtr->outputs.size();
4540 for (
size_t i = 0; i < outputCount; ++i)
4543 result[i] = std::make_pair(outputId, subgraphPtr->tensors[outputId].get());
4549 size_t subgraphIndex,
4550 size_t operatorIndex)
4553 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4554 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
4555 return operatorPtr->inputs;
4559 size_t subgraphIndex,
4560 size_t operatorIndex)
4563 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4564 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
4565 return operatorPtr->outputs;
4568 void TfLiteParserImpl::RegisterInputSlots(
size_t subgraphIndex,
4569 size_t operatorIndex,
4571 const std::vector<unsigned int>& tensorIndexes,
4572 unsigned int startingSlotIndex)
4574 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4580 fmt::format(
"The number of tensor inputs ({}) does not match the number expected ({})" 4581 " for subgraph:{} operator index:{} {}",
4582 tensorIndexes.size(),
4589 for (
unsigned int index = 0; index < tensorIndexes.size() ; ++index)
4591 unsigned int tensorIndex = tensorIndexes[index];
4593 RegisterConsumerOfTensor(subgraphIndex, tensorIndex, slot);
4597 void TfLiteParserImpl::RegisterOutputSlots(
size_t subgraphIndex,
4598 size_t operatorIndex,
4600 const std::vector<unsigned int>& tensorIndexes)
4602 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4607 fmt::format(
"The number of tensor outputs ({}) does not match the number expected ({})" 4608 " for subgraph:{} operator index:{} {}",
4609 tensorIndexes.size(),
4616 for (
unsigned int slotIndex = 0; slotIndex < layer->
GetNumOutputSlots(); ++slotIndex)
4618 unsigned int tensorIndex = tensorIndexes[slotIndex];
4620 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
4624 void TfLiteParserImpl::SetupInputLayers(
size_t subgraphIndex)
4629 for (
auto const& tensorIdAndPtr : inputs)
4631 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
4633 m_Network->AddInputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
4638 RegisterOutputSlots(subgraphIndex,
4639 VIRTUAL_OPERATOR_ID,
4641 {
static_cast<uint32_t
>(tensorIdAndPtr.first) });
4645 void TfLiteParserImpl::SetupOutputLayers(
size_t subgraphIndex)
4650 for (
auto const& tensorIdAndPtr : outputs)
4652 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
4654 m_Network->AddOutputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
4656 RegisterInputSlots(subgraphIndex,
4657 VIRTUAL_OPERATOR_ID,
4659 {
static_cast<uint32_t
>(tensorIdAndPtr.first) });
4663 void TfLiteParserImpl::SetupConstantLayers(
size_t subgraph)
4667 const auto & subgraphPtr = m_Model->subgraphs[subgraph];
4668 for (
unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
4670 for (
unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
4672 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot ==
nullptr &&
4673 m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0)
4675 TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get();
4677 if (IsConstTensor(tensorPtr))
4682 if (std::find(m_ConstantsToDequantize.begin(), m_ConstantsToDequantize.end(), tensorPtr->buffer)
4683 != m_ConstantsToDequantize.end())
4685 dataType = DataType::Float32;
4687 auto tensorAndData = CreateConstTensorNonPermuted(tensorPtr, tensorInfo, dataType);
4689 std::string layerName = fmt::format(
"Constant:{}", tensorPtr->name);
4690 IConnectableLayer *layer = m_Network->AddConstantLayer(tensorAndData.first, layerName.c_str());
4693 RegisterOutputSlots(subgraphIndex,
4694 VIRTUAL_OPERATOR_ID,
4698 else if (ShouldConstantTensorBeCreated(tensorIndex))
4703 if (std::find(m_ConstantsToDequantize.begin(), m_ConstantsToDequantize.end(), tensorPtr->buffer)
4704 != m_ConstantsToDequantize.end())
4706 dataType = DataType::Float32;
4714 std::string layerName = fmt::format(
"Constant:{}", tensorPtr->name);
4715 IConnectableLayer* layer = m_Network->AddConstantLayer(tensorAndData, layerName.c_str());
4718 RegisterOutputSlots(subgraphIndex,
4719 VIRTUAL_OPERATOR_ID,
4726 fmt::format(
"Invalid Tensor: Tensor should be constant. {}",
4738 return model->buffers[bufferIndex].get();
4741 template<
typename T>
4742 std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
4751 auto constData = CreateConstTensorImpl<T>(bufferPtr,
4755 TfLiteParserImpl::SupportedDataStorage storage(std::move(constData.second));
4756 return std::make_pair(constData.first, std::move(storage));
4759 bool TfLiteParserImpl::ShouldConstantTensorBeCreated(
unsigned int tensorIndex)
4762 return (std::find(m_ConstantsToBeCreated.begin(), m_ConstantsToBeCreated.end(), tensorIndex)
4763 != m_ConstantsToBeCreated.end());
4766 bool TfLiteParserImpl::IsConstTensor(
TensorRawPtr tensorPtr)
4769 bool isConst =
true;
4771 auto buffer =
GetBuffer(m_Model, tensorPtr->buffer);
4772 if (buffer->data.size() == 0)
4780 std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
4781 TfLiteParserImpl::CreateConstTensorPermuted(
TensorRawPtr tensorPtr,
4786 auto bufferPtr =
GetBuffer(m_Model, tensorPtr->buffer);
4795 return CreateConstTensorAndStoreData<float>(bufferPtr,
4800 return CreateConstTensorAndStoreData<uint8_t>(bufferPtr,
4805 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
4810 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
4815 return CreateConstTensorAndStoreData<int32_t>(bufferPtr,
4821 std::stringstream errString;
4822 errString <<
"Unexpected datatype when creating const tensor: " 4824 <<
" shape:" << tensorInfo.GetShape()
4835 auto bufferPtr =
GetBuffer(m_Model, tensorPtr->buffer);
4841 return ConstTensor(tensorInfo, bufferPtr->data.data());
4844 std::pair<armnn::ConstTensor, std::unique_ptr<float[]>>
4845 TfLiteParserImpl::CreateConstTensorNonPermuted(
TensorRawPtr tensorPtr,
4850 auto bufferPtr =
GetBuffer(m_Model, tensorPtr->buffer);
4856 if (inputDataType == DataType::Float32 && tensorInfo.
GetDataType() != DataType::Float32)
4859 std::unique_ptr<float[]> data =
AsFloatArray(bufferPtr, tensorInfo);
4860 return std::make_pair(
ConstTensor(constTensorInfo, data.get()), std::move(data));
4864 return std::make_pair(
ConstTensor(tensorInfo, bufferPtr->data.data()), std::unique_ptr<
float[]>());
4868 std::pair<armnn::ConstTensor*, std::unique_ptr<float[]>>
4873 auto bufferPtr =
GetBuffer(m_Model, tensorPtr->buffer);
4882 std::unique_ptr<float[]> data =
AsFloatArray(bufferPtr, tensorInfo);
4883 return std::make_pair(
new ConstTensor(constTensorInfo, data.get()), std::move(data));
4887 return std::make_pair(
new ConstTensor(tensorInfo, bufferPtr->data.data()), std::unique_ptr<
float[]>());
4892 const std::string& name)
const 4896 for (
auto const& input : inputs)
4898 if (input.second->name == name)
4900 auto bindingId = GenerateLayerBindingId(subgraphId, input.first);
4904 return std::make_pair(bindingId, inputTensorInfo);
4908 std::stringstream bindings;
4909 for (
auto const& input : inputs)
4911 bindings <<
"'" << input.second->name <<
"' ";
4915 fmt::format(
"No input binding found for subgraph:{} and name:{}. " 4916 "Possible inputs are: [{}] {}",
4924 const std::string& name)
const 4928 for (
unsigned int i = 0; i < outputs.size(); ++i)
4930 auto const output = outputs[i];
4931 if (output.second->name == name)
4933 auto bindingId = GenerateLayerBindingId(subgraphId, output.first);
4934 std::vector<unsigned int> shape = m_OverridenOutputShapes.size() > 0 ?
4935 m_OverridenOutputShapes[i] : AsUnsignedVector(output.second->shape);
4936 return std::make_pair(bindingId,
ToTensorInfo(output.second, shape));
4940 std::stringstream bindings;
4941 for (
auto const& output : outputs)
4943 bindings <<
"'" << output.second->name <<
"' ";
4947 fmt::format(
"No output binding found for subgraph:{} and name:{}. " 4948 "Possible outputs are: [{}] {}",
4957 return m_Model->subgraphs.size();
4964 std::vector<std::string> result;
4965 result.reserve(inputs.size());
4966 for (
auto const& input : inputs)
4968 result.push_back(input.second->name);
4977 std::vector<std::string> result;
4978 result.reserve(outputs.size());
4979 for (
auto const& output : outputs)
4981 result.push_back(output.second->name);
4991 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<
float[]>&& data)
4992 : m_FloatData(std::move(data))
4993 , m_Uint8Data(
nullptr)
4994 , m_Int8Data(
nullptr)
4995 , m_Int32Data(
nullptr)
4999 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<uint8_t[]>&& data)
5000 : m_FloatData(
nullptr)
5001 , m_Uint8Data(std::move(data))
5002 , m_Int8Data(
nullptr)
5003 , m_Int32Data(
nullptr)
5007 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int8_t[]>&& data)
5008 : m_FloatData(
nullptr)
5009 , m_Uint8Data(
nullptr)
5010 , m_Int8Data(std::move(data))
5011 , m_Int32Data(
nullptr)
5015 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int32_t[]>&& data)
5016 : m_FloatData(
nullptr)
5017 , m_Uint8Data(
nullptr)
5018 , m_Int8Data(
nullptr)
5019 , m_Int32Data(std::move(data))
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.
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.
float m_K
Kappa value used for the across channel normalization equation.
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
uint32_t m_PadBack
Padding back value in the depth dimension.
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.
bool HasPerAxisQuantization() const
uint32_t m_PoolWidth
Pooling width value.
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
A Convolution2dDescriptor for the Convolution2dLayer.
float m_Alpha
Alpha value for the normalization equation.
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
Optional< unsigned int > GetQuantizationDim() const
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
uint32_t m_PadBottom
Padding bottom value in the height dimension.
std::vector< std::string > GetSubgraphOutputTensorNames(size_t subgraphId) const
Return the output tensor names for a given subgraph.
bool m_BiasEnabled
Enable/disable bias.
void ProcessConcatInputTensorInfo(armnn::TensorInfo &inputTensorInfo, armnn::OriginsDescriptor &concatDescriptor, const unsigned int &concatAxis, unsigned int inputIndex, unsigned int &mergeDimOrigin)
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
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)
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}.
std::vector< float > GetQuantizationScales() const
uint32_t m_DilationX
Dilation along x axis.
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
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
NormalizationAlgorithmMethod m_NormMethodType
Normalization method algorithm to use (LocalBrightness, LocalContrast).
#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 ResizeBilinearDescriptor for the ResizeBilinearLayer.
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_MaxDetections
Maximum numbers of detections.
A PadDescriptor for the PadLayer.
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.
An LstmDescriptor for the LstmLayer.
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
virtual const IConnectableLayer & GetOwningIConnectableLayer() const =0
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.
uint32_t m_PadFront
Padding front value in the depth dimension.
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.
An ActivationDescriptor for the ActivationLayer.
const TensorInfo & GetInfo() const
void SetDataType(DataType type)
uint32_t m_NumInputs
Number of input tensors.
uint32_t m_PadLeft
Padding left value in the width dimension.
uint32_t m_ActivationFunc
The activation function to use.
A SliceDescriptor for the SliceLayer.
armnn::INetworkPtr LoadModel(std::unique_ptr< tflite::ModelT > model)
A Convolution3dDescriptor for the Convolution3dLayer.
std::unique_ptr< tflite::SubGraphT > SubgraphPtr
uint32_t m_PadRight
Padding right value in the width dimension.
virtual LayerType GetType() const =0
Returns the armnn::LayerType of this layer.
PaddingMode m_PaddingMode
Specifies the Padding mode (Constant, Reflect or Symmetric)
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)
DataLayout m_DataLayout
The data layout to be used (NDHWC, NCDHW).
Struct for the users to pass backend specific options.
NormalizationAlgorithmChannel m_NormChannelType
Normalization channel algorithm to use (Across, Within).
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)
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.
std::unique_ptr< float[]> AsFloatArray(TfLiteParserImpl::BufferRawPtr bufferPtr, const TensorInfo &tensorInfo)
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.
uint32_t m_PadTop
Padding top value in the height 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 TensorInfo & GetTensorInfo() const =0
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.
unsigned int GetNumElementsAfter(const armnn::TensorShape &shape, unsigned int axis)
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
Krichevsky 2012: Local Brightness Normalization.
A Pooling2dDescriptor for the Pooling2dLayer.
A NormalizationDescriptor for the NormalizationLayer.
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)
uint32_t m_DilationZ
Dilation along z axis.
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.
float m_Beta
Beta value for the normalization equation.
uint32_t m_StrideZ
Stride value when proceeding through input for the depth dimension.
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.
uint32_t m_NormSize
Depth radius value.
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_DilationY
Dilation along y axis.
unsigned int GetNumElements() const
uint32_t m_PadRight
Padding right value in the width dimension.
bool m_ConstantWeights
Enable/disable constant weights and biases.