ArmNN
 23.08
TfLiteParserImpl Class Reference

#include <TfLiteParser.hpp>

Public Types

using ModelPtr = std::unique_ptr< tflite::ModelT >
 
using SubgraphPtr = std::unique_ptr< tflite::SubGraphT >
 
using OperatorPtr = std::unique_ptr< tflite::OperatorT >
 
using OperatorCodePtr = std::unique_ptr< tflite::OperatorCodeT >
 
using TensorPtr = std::unique_ptr< tflite::TensorT >
 
using TensorRawPtr = const tflite::TensorT *
 
using TensorRawPtrVector = std::vector< TensorRawPtr >
 
using TensorIdRawPtr = std::pair< size_t, TensorRawPtr >
 
using TensorIdRawPtrVector = std::vector< TensorIdRawPtr >
 
using BufferPtr = std::unique_ptr< tflite::BufferT >
 
using BufferRawPtr = const tflite::BufferT *
 

Public Member Functions

armnn::INetworkPtr CreateNetworkFromBinaryFile (const char *graphFile)
 Create the network from a flatbuffers binary file on disk. More...
 
armnn::INetworkPtr CreateNetworkFromBinary (const std::vector< uint8_t > &binaryContent)
 Create the network from a flatbuffers binary. More...
 
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 name and subgraph id. More...
 
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 name and subgraph id. More...
 
size_t GetSubgraphCount () const
 Return the number of subgraphs in the parsed model. More...
 
std::vector< std::string > GetSubgraphInputTensorNames (size_t subgraphId) const
 Return the input tensor names for a given subgraph. More...
 
std::vector< std::string > GetSubgraphOutputTensorNames (size_t subgraphId) const
 Return the output tensor names for a given subgraph. More...
 
 TfLiteParserImpl (const armnn::Optional< ITfLiteParser::TfLiteParserOptions > &options=armnn::EmptyOptional())
 
 ~TfLiteParserImpl ()=default
 
armnn::INetworkPtr CreateNetworkFromBinaryAsDynamic (const std::vector< uint8_t > &binaryContent)
 
armnn::INetworkPtr LoadModel (std::unique_ptr< tflite::ModelT > model)
 

Static Public Member Functions

static ModelPtr LoadModelFromFile (const char *fileName)
 
static ModelPtr LoadModelFromBinary (const uint8_t *binaryContent, size_t len)
 
static TensorRawPtrVector GetInputs (const ModelPtr &model, size_t subgraphIndex, size_t operatorIndex)
 
static TensorRawPtrVector GetOutputs (const ModelPtr &model, size_t subgraphIndex, size_t operatorIndex)
 
static TensorIdRawPtrVector GetSubgraphInputs (const ModelPtr &model, size_t subgraphIndex)
 
static TensorIdRawPtrVector GetSubgraphOutputs (const ModelPtr &model, size_t subgraphIndex)
 
static std::vector< int32_t > & GetInputTensorIds (const ModelPtr &model, size_t subgraphIndex, size_t operatorIndex)
 
static std::vector< int32_t > & GetOutputTensorIds (const ModelPtr &model, size_t subgraphIndex, size_t operatorIndex)
 
static BufferRawPtr GetBuffer (const ModelPtr &model, size_t bufferIndex)
 
static armnn::TensorInfo OutputShapeOfSqueeze (std::vector< uint32_t > squeezeDims, const armnn::TensorInfo &inputTensorInfo)
 
static armnn::TensorInfo OutputShapeOfReshape (const armnn::TensorInfo &inputTensorInfo, const std::vector< int32_t > &targetDimsIn)
 
static const std::string GetVersion ()
 Retrieve version in X.Y.Z form. More...
 

Detailed Description

Definition at line 26 of file TfLiteParser.hpp.

Member Typedef Documentation

◆ BufferPtr

using BufferPtr = std::unique_ptr<tflite::BufferT>

Definition at line 39 of file TfLiteParser.hpp.

◆ BufferRawPtr

using BufferRawPtr = const tflite::BufferT *

Definition at line 40 of file TfLiteParser.hpp.

◆ ModelPtr

using ModelPtr = std::unique_ptr<tflite::ModelT>

Definition at line 30 of file TfLiteParser.hpp.

◆ OperatorCodePtr

using OperatorCodePtr = std::unique_ptr<tflite::OperatorCodeT>

Definition at line 33 of file TfLiteParser.hpp.

◆ OperatorPtr

using OperatorPtr = std::unique_ptr<tflite::OperatorT>

Definition at line 32 of file TfLiteParser.hpp.

◆ SubgraphPtr

using SubgraphPtr = std::unique_ptr<tflite::SubGraphT>

Definition at line 31 of file TfLiteParser.hpp.

◆ TensorIdRawPtr

using TensorIdRawPtr = std::pair<size_t, TensorRawPtr>

Definition at line 37 of file TfLiteParser.hpp.

◆ TensorIdRawPtrVector

using TensorIdRawPtrVector = std::vector<TensorIdRawPtr>

Definition at line 38 of file TfLiteParser.hpp.

◆ TensorPtr

using TensorPtr = std::unique_ptr<tflite::TensorT>

Definition at line 34 of file TfLiteParser.hpp.

◆ TensorRawPtr

using TensorRawPtr = const tflite::TensorT *

Definition at line 35 of file TfLiteParser.hpp.

◆ TensorRawPtrVector

using TensorRawPtrVector = std::vector<TensorRawPtr>

Definition at line 36 of file TfLiteParser.hpp.

Constructor & Destructor Documentation

◆ TfLiteParserImpl()

Definition at line 740 of file TfLiteParser.cpp.

741 : m_Options(options)
742 , m_Network(nullptr, nullptr)
743 , m_ParserFunctions(tflite::BuiltinOperator_MAX+1, &TfLiteParserImpl::ParseUnsupportedOperator)
744 {
745  // register supported operators
746  m_ParserFunctions[tflite::BuiltinOperator_ABS] = &TfLiteParserImpl::ParseAbs;
747  m_ParserFunctions[tflite::BuiltinOperator_ADD] = &TfLiteParserImpl::ParseAdd;
748  m_ParserFunctions[tflite::BuiltinOperator_ARG_MIN] = &TfLiteParserImpl::ParseArgMin;
749  m_ParserFunctions[tflite::BuiltinOperator_ARG_MAX] = &TfLiteParserImpl::ParseArgMax;
750  m_ParserFunctions[tflite::BuiltinOperator_AVERAGE_POOL_2D] = &TfLiteParserImpl::ParseAveragePool2D;
751  m_ParserFunctions[tflite::BuiltinOperator_BATCH_TO_SPACE_ND] = &TfLiteParserImpl::ParseBatchToSpaceND;
752  m_ParserFunctions[tflite::BuiltinOperator_BATCH_MATMUL] = &TfLiteParserImpl::ParseBatchMatMul;
753  m_ParserFunctions[tflite::BuiltinOperator_CEIL] = &TfLiteParserImpl::ParseCeil;
754  m_ParserFunctions[tflite::BuiltinOperator_CAST] = &TfLiteParserImpl::ParseCast;
755  m_ParserFunctions[tflite::BuiltinOperator_CONCATENATION] = &TfLiteParserImpl::ParseConcatenation;
756  m_ParserFunctions[tflite::BuiltinOperator_CONV_2D] = &TfLiteParserImpl::ParseConv2D;
757  // Conv3D support was added in TF 2.5, so for backwards compatibility a hash define is needed.
758  #if defined(ARMNN_POST_TFLITE_2_4)
759  m_ParserFunctions[tflite::BuiltinOperator_CONV_3D] = &TfLiteParserImpl::ParseConv3D;
760  #endif
761  m_ParserFunctions[tflite::BuiltinOperator_CUSTOM] = &TfLiteParserImpl::ParseCustomOperator;
762  m_ParserFunctions[tflite::BuiltinOperator_DEPTH_TO_SPACE] = &TfLiteParserImpl::ParseDepthToSpace;
763  m_ParserFunctions[tflite::BuiltinOperator_DEPTHWISE_CONV_2D] = &TfLiteParserImpl::ParseDepthwiseConv2D;
764  m_ParserFunctions[tflite::BuiltinOperator_DEQUANTIZE] = &TfLiteParserImpl::ParseDequantize;
765  m_ParserFunctions[tflite::BuiltinOperator_DIV] = &TfLiteParserImpl::ParseDiv;
766  m_ParserFunctions[tflite::BuiltinOperator_ELU] = &TfLiteParserImpl::ParseElu;
767  m_ParserFunctions[tflite::BuiltinOperator_EQUAL] = &TfLiteParserImpl::ParseEqual;
768  m_ParserFunctions[tflite::BuiltinOperator_EXP] = &TfLiteParserImpl::ParseExp;
769  m_ParserFunctions[tflite::BuiltinOperator_EXPAND_DIMS] = &TfLiteParserImpl::ParseExpandDims;
770  m_ParserFunctions[tflite::BuiltinOperator_FLOOR_DIV] = &TfLiteParserImpl::ParseFloorDiv;
771  m_ParserFunctions[tflite::BuiltinOperator_FULLY_CONNECTED] = &TfLiteParserImpl::ParseFullyConnected;
772  m_ParserFunctions[tflite::BuiltinOperator_GATHER] = &TfLiteParserImpl::ParseGather;
773  m_ParserFunctions[tflite::BuiltinOperator_GATHER_ND] = &TfLiteParserImpl::ParseGatherNd;
774  m_ParserFunctions[tflite::BuiltinOperator_GREATER] = &TfLiteParserImpl::ParseGreater;
775  m_ParserFunctions[tflite::BuiltinOperator_GREATER_EQUAL] = &TfLiteParserImpl::ParseGreaterOrEqual;
776  m_ParserFunctions[tflite::BuiltinOperator_HARD_SWISH] = &TfLiteParserImpl::ParseHardSwish;
777  m_ParserFunctions[tflite::BuiltinOperator_LEAKY_RELU] = &TfLiteParserImpl::ParseLeakyRelu;
778  m_ParserFunctions[tflite::BuiltinOperator_LESS] = &TfLiteParserImpl::ParseLess;
779  m_ParserFunctions[tflite::BuiltinOperator_LESS_EQUAL] = &TfLiteParserImpl::ParseLessOrEqual;
780  m_ParserFunctions[tflite::BuiltinOperator_LOCAL_RESPONSE_NORMALIZATION]
781  = &TfLiteParserImpl::ParseLocalResponseNormalization;
782  m_ParserFunctions[tflite::BuiltinOperator_LOG] = &TfLiteParserImpl::ParseLog;
783  m_ParserFunctions[tflite::BuiltinOperator_LOGICAL_NOT] = &TfLiteParserImpl::ParseLogicalNot;
784  m_ParserFunctions[tflite::BuiltinOperator_LOGISTIC] = &TfLiteParserImpl::ParseLogistic;
785  m_ParserFunctions[tflite::BuiltinOperator_LOG_SOFTMAX] = &TfLiteParserImpl::ParseLogSoftmax;
786  m_ParserFunctions[tflite::BuiltinOperator_L2_NORMALIZATION] = &TfLiteParserImpl::ParseL2Normalization;
787  m_ParserFunctions[tflite::BuiltinOperator_MAX_POOL_2D] = &TfLiteParserImpl::ParseMaxPool2D;
788  m_ParserFunctions[tflite::BuiltinOperator_MAXIMUM] = &TfLiteParserImpl::ParseMaximum;
789  m_ParserFunctions[tflite::BuiltinOperator_MEAN] = &TfLiteParserImpl::ParseMean;
790  m_ParserFunctions[tflite::BuiltinOperator_MINIMUM] = &TfLiteParserImpl::ParseMinimum;
791  m_ParserFunctions[tflite::BuiltinOperator_MIRROR_PAD] = &TfLiteParserImpl::ParseMirrorPad;
792  m_ParserFunctions[tflite::BuiltinOperator_MUL] = &TfLiteParserImpl::ParseMul;
793  m_ParserFunctions[tflite::BuiltinOperator_NEG] = &TfLiteParserImpl::ParseNeg;
794  m_ParserFunctions[tflite::BuiltinOperator_NOT_EQUAL] = &TfLiteParserImpl::ParseNotEqual;
795  m_ParserFunctions[tflite::BuiltinOperator_PACK] = &TfLiteParserImpl::ParsePack;
796  m_ParserFunctions[tflite::BuiltinOperator_PAD] = &TfLiteParserImpl::ParsePad;
797  m_ParserFunctions[tflite::BuiltinOperator_PADV2] = &TfLiteParserImpl::ParsePad;
798  m_ParserFunctions[tflite::BuiltinOperator_POW] = &TfLiteParserImpl::ParsePower;
799  m_ParserFunctions[tflite::BuiltinOperator_PRELU] = &TfLiteParserImpl::ParsePrelu;
800  m_ParserFunctions[tflite::BuiltinOperator_QUANTIZE] = &TfLiteParserImpl::ParseQuantize;
801  m_ParserFunctions[tflite::BuiltinOperator_RELU] = &TfLiteParserImpl::ParseRelu;
802  m_ParserFunctions[tflite::BuiltinOperator_RELU6] = &TfLiteParserImpl::ParseRelu6;
803  m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MAX] = &TfLiteParserImpl::ParseReduceMax;
804  m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MIN] = &TfLiteParserImpl::ParseReduceMin;
805  m_ParserFunctions[tflite::BuiltinOperator_REDUCE_PROD] = &TfLiteParserImpl::ParseReduceProd;
806  m_ParserFunctions[tflite::BuiltinOperator_RESHAPE] = &TfLiteParserImpl::ParseReshape;
807  m_ParserFunctions[tflite::BuiltinOperator_RESIZE_BILINEAR] = &TfLiteParserImpl::ParseResizeBilinear;
808  m_ParserFunctions[tflite::BuiltinOperator_RESIZE_NEAREST_NEIGHBOR] = &TfLiteParserImpl::ParseResizeNearestNeighbor;
809  m_ParserFunctions[tflite::BuiltinOperator_REVERSE_V2] = &TfLiteParserImpl::ParseReverseV2;
810  m_ParserFunctions[tflite::BuiltinOperator_RSQRT] = &TfLiteParserImpl::ParseRsqrt;
811  m_ParserFunctions[tflite::BuiltinOperator_SQRT] = &TfLiteParserImpl::ParseSqrt;
812  m_ParserFunctions[tflite::BuiltinOperator_SHAPE] = &TfLiteParserImpl::ParseShape;
813  m_ParserFunctions[tflite::BuiltinOperator_SIN] = &TfLiteParserImpl::ParseSin;
814  m_ParserFunctions[tflite::BuiltinOperator_SLICE] = &TfLiteParserImpl::ParseSlice;
815  m_ParserFunctions[tflite::BuiltinOperator_SOFTMAX] = &TfLiteParserImpl::ParseSoftmax;
816  m_ParserFunctions[tflite::BuiltinOperator_SPACE_TO_BATCH_ND] = &TfLiteParserImpl::ParseSpaceToBatchND;
817  m_ParserFunctions[tflite::BuiltinOperator_SPACE_TO_DEPTH] = &TfLiteParserImpl::ParseSpaceToDepth;
818  m_ParserFunctions[tflite::BuiltinOperator_SPLIT] = &TfLiteParserImpl::ParseSplit;
819  m_ParserFunctions[tflite::BuiltinOperator_SPLIT_V] = &TfLiteParserImpl::ParseSplitV;
820  m_ParserFunctions[tflite::BuiltinOperator_SQUEEZE] = &TfLiteParserImpl::ParseSqueeze;
821  m_ParserFunctions[tflite::BuiltinOperator_SQUARE] = &TfLiteParserImpl::ParseSquare;
822  m_ParserFunctions[tflite::BuiltinOperator_SQUARED_DIFFERENCE] = &TfLiteParserImpl::ParseSquaredDifference;
823  m_ParserFunctions[tflite::BuiltinOperator_STRIDED_SLICE] = &TfLiteParserImpl::ParseStridedSlice;
824  m_ParserFunctions[tflite::BuiltinOperator_SUB] = &TfLiteParserImpl::ParseSub;
825  m_ParserFunctions[tflite::BuiltinOperator_SUM] = &TfLiteParserImpl::ParseSum;
826  m_ParserFunctions[tflite::BuiltinOperator_TANH] = &TfLiteParserImpl::ParseTanH;
827  m_ParserFunctions[tflite::BuiltinOperator_TILE] = &TfLiteParserImpl::ParseTile;
828  m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE] = &TfLiteParserImpl::ParseTranspose;
829  m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE_CONV] = &TfLiteParserImpl::ParseTransposeConv;
830  m_ParserFunctions[tflite::BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM]
831  = &TfLiteParserImpl::ParseUnidirectionalSequenceLSTM;
832  m_ParserFunctions[tflite::BuiltinOperator_UNPACK] = &TfLiteParserImpl::ParseUnpack;
833 
834  // register supported custom operators
835  m_CustomParserFunctions["TFLite_Detection_PostProcess"] = &TfLiteParserImpl::ParseDetectionPostProcess;
836 }

◆ ~TfLiteParserImpl()

~TfLiteParserImpl ( )
default

Member Function Documentation

◆ CreateNetworkFromBinary()

INetworkPtr CreateNetworkFromBinary ( const std::vector< uint8_t > &  binaryContent)

Create the network from a flatbuffers binary.

Definition at line 957 of file TfLiteParser.cpp.

958 {
959  ResetParser();
960  m_Model = LoadModelFromBinary(binaryContent.data(), binaryContent.size());
961  return CreateNetworkFromModel();
962 }

References TfLiteParserImpl::LoadModelFromBinary().

◆ CreateNetworkFromBinaryAsDynamic()

armnn::INetworkPtr CreateNetworkFromBinaryAsDynamic ( const std::vector< uint8_t > &  binaryContent)

◆ CreateNetworkFromBinaryFile()

INetworkPtr CreateNetworkFromBinaryFile ( const char *  graphFile)

Create the network from a flatbuffers binary file on disk.

Definition at line 950 of file TfLiteParser.cpp.

951 {
952  ResetParser();
953  m_Model = LoadModelFromFile(graphFile);
954  return CreateNetworkFromModel();
955 }

References TfLiteParserImpl::LoadModelFromFile().

◆ GetBuffer()

TfLiteParserImpl::BufferRawPtr GetBuffer ( const ModelPtr model,
size_t  bufferIndex 
)
static

Definition at line 5616 of file TfLiteParser.cpp.

5617 {
5618  CHECK_BUFFER(model, bufferIndex);
5619  return model->buffers[bufferIndex].get();
5620 }

References CHECK_BUFFER.

◆ GetInputs()

TfLiteParserImpl::TensorRawPtrVector GetInputs ( const ModelPtr model,
size_t  subgraphIndex,
size_t  operatorIndex 
)
static

Definition at line 5304 of file TfLiteParser.cpp.

5307 {
5308  CHECK_MODEL(model, subgraphIndex, operatorIndex);
5309 
5310  const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5311  const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
5312 
5313  size_t inputCount = operatorPtr->inputs.size();
5314  TensorRawPtrVector result;
5315  for (size_t i = 0; i < inputCount; ++i)
5316  {
5317  // If the input location is -1 then assume input is turned off.
5318  if (operatorPtr->inputs[i] == -1)
5319  {
5320  continue;
5321  }
5322  else
5323  {
5324  uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[i]);
5325  result.push_back(subgraphPtr->tensors[inputId].get());
5326  }
5327  }
5328  return result;
5329 }

References CHECK_MODEL, and CHECKED_NON_NEGATIVE.

◆ GetInputTensorIds()

std::vector< int32_t > & GetInputTensorIds ( const ModelPtr model,
size_t  subgraphIndex,
size_t  operatorIndex 
)
static

Definition at line 5384 of file TfLiteParser.cpp.

5387 {
5388  CHECK_MODEL(model, subgraphIndex, operatorIndex);
5389  const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5390  const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
5391  return operatorPtr->inputs;
5392 }

References CHECK_MODEL.

◆ GetNetworkInputBindingInfo()

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 name and subgraph id.

Definition at line 5794 of file TfLiteParser.cpp.

5796 {
5797  CHECK_SUBGRAPH(m_Model, subgraphId);
5798  auto inputs = GetSubgraphInputs(m_Model, subgraphId);
5799  for (auto const& input : inputs)
5800  {
5801  if (input.second->name == name)
5802  {
5803  auto bindingId = GenerateLayerBindingId(subgraphId, input.first);
5804  auto inputTensorInfo = ToTensorInfo(input.second);
5805  // Input tensors are always treated as constant tensors during network execution.
5806  inputTensorInfo.SetConstant(true);
5807  return std::make_pair(bindingId, inputTensorInfo);
5808  }
5809  }
5810 
5811  std::stringstream bindings;
5812  for (auto const& input : inputs)
5813  {
5814  bindings << "'" << input.second->name << "' ";
5815  }
5816 
5817  throw ParseException(
5818  fmt::format("No input binding found for subgraph:{} and name:{}. "
5819  "Possible inputs are: [{}] {}",
5820  subgraphId,
5821  name,
5822  bindings.str(),
5823  CHECK_LOCATION().AsString()));
5824 }

References CHECK_LOCATION, CHECK_SUBGRAPH, TfLiteParserImpl::GetSubgraphInputs(), TensorInfo::SetConstant(), and armnnDeserializer::ToTensorInfo().

◆ GetNetworkOutputBindingInfo()

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 name and subgraph id.

Definition at line 5826 of file TfLiteParser.cpp.

5828 {
5829  CHECK_SUBGRAPH(m_Model, subgraphId);
5830  auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
5831  for (unsigned int i = 0; i < outputs.size(); ++i)
5832  {
5833  auto const output = outputs[i];
5834  if (output.second->name == name)
5835  {
5836  auto bindingId = GenerateLayerBindingId(subgraphId, output.first);
5837  std::vector<unsigned int> shape = m_OverriddenOutputShapes.size() > 0 ?
5838  m_OverriddenOutputShapes[i] : AsUnsignedVector(output.second->shape);
5839  return std::make_pair(bindingId, ToTensorInfo(output.second, shape));
5840  }
5841  }
5842 
5843  std::stringstream bindings;
5844  for (auto const& output : outputs)
5845  {
5846  bindings << "'" << output.second->name << "' ";
5847  }
5848 
5849  throw ParseException(
5850  fmt::format("No output binding found for subgraph:{} and name:{}. "
5851  "Possible outputs are: [{}] {}",
5852  subgraphId,
5853  name,
5854  bindings.str(),
5855  CHECK_LOCATION().AsString()));
5856 }

References CHECK_LOCATION, CHECK_SUBGRAPH, TfLiteParserImpl::GetSubgraphOutputs(), and armnnDeserializer::ToTensorInfo().

◆ GetOutputs()

TfLiteParserImpl::TensorRawPtrVector GetOutputs ( const ModelPtr model,
size_t  subgraphIndex,
size_t  operatorIndex 
)
static

Definition at line 5331 of file TfLiteParser.cpp.

5334 {
5335  CHECK_MODEL(model, subgraphIndex, operatorIndex);
5336 
5337  const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5338  const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
5339 
5340  size_t outputCount = operatorPtr->outputs.size();
5341  TensorRawPtrVector result(outputCount);
5342  for (size_t i = 0; i < outputCount; ++i)
5343  {
5344  uint32_t outputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[i]);
5345  CHECK_TENSOR(model, subgraphIndex, outputId);
5346  result[i] = subgraphPtr->tensors[outputId].get();
5347  }
5348  return result;
5349 }

References CHECK_MODEL, CHECK_TENSOR, and CHECKED_NON_NEGATIVE.

◆ GetOutputTensorIds()

std::vector< int32_t > & GetOutputTensorIds ( const ModelPtr model,
size_t  subgraphIndex,
size_t  operatorIndex 
)
static

Definition at line 5394 of file TfLiteParser.cpp.

5397 {
5398  CHECK_MODEL(model, subgraphIndex, operatorIndex);
5399  const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5400  const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
5401  return operatorPtr->outputs;
5402 }

References CHECK_MODEL.

◆ GetSubgraphCount()

size_t GetSubgraphCount ( ) const

Return the number of subgraphs in the parsed model.

Definition at line 5858 of file TfLiteParser.cpp.

5859 {
5860  return m_Model->subgraphs.size();
5861 }

◆ GetSubgraphInputs()

TfLiteParserImpl::TensorIdRawPtrVector GetSubgraphInputs ( const ModelPtr model,
size_t  subgraphIndex 
)
static

Definition at line 5351 of file TfLiteParser.cpp.

5353 {
5354  CHECK_SUBGRAPH(model, subgraphIndex);
5355  const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5356 
5357  size_t inputCount = subgraphPtr->inputs.size();
5358  TensorIdRawPtrVector result(inputCount);
5359  for (size_t i = 0; i < inputCount; ++i)
5360  {
5361  uint32_t inputId = CHECKED_NON_NEGATIVE(subgraphPtr->inputs[i]);
5362  CHECK_TENSOR(model, subgraphIndex, inputId);
5363  result[i] = std::make_pair(inputId, subgraphPtr->tensors[inputId].get());
5364  }
5365  return result;
5366 }

References CHECK_SUBGRAPH, CHECK_TENSOR, and CHECKED_NON_NEGATIVE.

Referenced by TfLiteParserImpl::GetNetworkInputBindingInfo(), and TfLiteParserImpl::GetSubgraphInputTensorNames().

◆ GetSubgraphInputTensorNames()

std::vector< std::string > GetSubgraphInputTensorNames ( size_t  subgraphId) const

Return the input tensor names for a given subgraph.

Definition at line 5863 of file TfLiteParser.cpp.

5864 {
5865  CHECK_SUBGRAPH(m_Model, subgraphId);
5866  auto inputs = GetSubgraphInputs(m_Model, subgraphId);
5867  std::vector<std::string> result;
5868  result.reserve(inputs.size());
5869  for (auto const& input : inputs)
5870  {
5871  result.push_back(input.second->name);
5872  }
5873  return result;
5874 }

References CHECK_SUBGRAPH, and TfLiteParserImpl::GetSubgraphInputs().

◆ GetSubgraphOutputs()

TfLiteParserImpl::TensorIdRawPtrVector GetSubgraphOutputs ( const ModelPtr model,
size_t  subgraphIndex 
)
static

Definition at line 5368 of file TfLiteParser.cpp.

5370 {
5371  CHECK_SUBGRAPH(model, subgraphIndex);
5372  const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5373 
5374  size_t outputCount = subgraphPtr->outputs.size();
5375  TensorIdRawPtrVector result(outputCount);
5376  for (size_t i = 0; i < outputCount; ++i)
5377  {
5378  uint32_t outputId = CHECKED_NON_NEGATIVE(subgraphPtr->outputs[i]);
5379  result[i] = std::make_pair(outputId, subgraphPtr->tensors[outputId].get());
5380  }
5381  return result;
5382 }

References CHECK_SUBGRAPH, and CHECKED_NON_NEGATIVE.

Referenced by TfLiteParserImpl::GetNetworkOutputBindingInfo(), and TfLiteParserImpl::GetSubgraphOutputTensorNames().

◆ GetSubgraphOutputTensorNames()

std::vector< std::string > GetSubgraphOutputTensorNames ( size_t  subgraphId) const

Return the output tensor names for a given subgraph.

Definition at line 5876 of file TfLiteParser.cpp.

5877 {
5878  CHECK_SUBGRAPH(m_Model, subgraphId);
5879  auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
5880  std::vector<std::string> result;
5881  result.reserve(outputs.size());
5882  for (auto const& output : outputs)
5883  {
5884  result.push_back(output.second->name);
5885  }
5886  return result;
5887 }

References CHECK_SUBGRAPH, and TfLiteParserImpl::GetSubgraphOutputs().

◆ GetVersion()

const std::string GetVersion ( )
static

Retrieve version in X.Y.Z form.

Definition at line 5889 of file TfLiteParser.cpp.

5890 {
5891  return TFLITE_PARSER_VERSION;
5892 }

References TFLITE_PARSER_VERSION.

◆ LoadModel()

armnn::INetworkPtr LoadModel ( std::unique_ptr< tflite::ModelT >  model)

Definition at line 965 of file TfLiteParser.cpp.

966 {
967  ResetParser();
968  m_Model = std::move(model);
969 
970  return CreateNetworkFromModel();
971 }

◆ LoadModelFromBinary()

TfLiteParserImpl::ModelPtr LoadModelFromBinary ( const uint8_t *  binaryContent,
size_t  len 
)
static

Definition at line 5285 of file TfLiteParser.cpp.

5286 {
5287  if (binaryContent == nullptr)
5288  {
5289  throw InvalidArgumentException(fmt::format("Invalid (null) binary content {}",
5290  CHECK_LOCATION().AsString()));
5291  }
5292  flatbuffers::Verifier verifier(binaryContent, len);
5293  if (verifier.VerifyBuffer<tflite::Model>() == false)
5294  {
5295  throw ParseException(
5296  fmt::format("Buffer doesn't conform to the expected Tensorflow Lite "
5297  "flatbuffers format. size:{} {}",
5298  len,
5299  CHECK_LOCATION().AsString()));
5300  }
5301  return tflite::UnPackModel(binaryContent);
5302 }

References CHECK_LOCATION.

Referenced by TfLiteParserImpl::CreateNetworkFromBinary(), and TfLiteParserImpl::LoadModelFromFile().

◆ LoadModelFromFile()

TfLiteParserImpl::ModelPtr LoadModelFromFile ( const char *  fileName)
static

Definition at line 5255 of file TfLiteParser.cpp.

5256 {
5257  if (fileName == nullptr)
5258  {
5259  throw InvalidArgumentException(fmt::format("Invalid (null) file name {}",
5260  CHECK_LOCATION().AsString()));
5261  }
5262  std::error_code errorCode;
5263  fs::path pathToFile(fileName);
5264  if (!fs::exists(pathToFile, errorCode))
5265  {
5266  //fmt::format() could not be used here (format error)
5267  std::stringstream msg;
5268  msg << "Cannot find the file (" << fileName << ") errorCode: " << errorCode
5269  << " " << CHECK_LOCATION().AsString();
5270  throw FileNotFoundException(msg.str());
5271  }
5272  if (!fs::is_regular_file(pathToFile))
5273  {
5274  // Exclude non regular files.
5275  throw InvalidArgumentException(fmt::format("File \"{}\" is not a regular file and cannot be loaded.",
5276  pathToFile.c_str()));
5277  }
5278 
5279  std::ifstream file(fileName, std::ios::binary);
5280  std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
5281  return LoadModelFromBinary(reinterpret_cast<const uint8_t *>(fileContent.c_str()),
5282  fileContent.size());
5283 }

References CHECK_LOCATION, and TfLiteParserImpl::LoadModelFromBinary().

Referenced by TfLiteParserImpl::CreateNetworkFromBinaryFile().

◆ OutputShapeOfReshape()

armnn::TensorInfo OutputShapeOfReshape ( const armnn::TensorInfo inputTensorInfo,
const std::vector< int32_t > &  targetDimsIn 
)
static

Definition at line 3183 of file TfLiteParser.cpp.

3185 {
3186  std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
3187  const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
3188 
3189  if (stretchDim != targetDimsIn.end())
3190  {
3191  if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
3192  {
3193  throw ParseException(
3194  fmt::format("At most one component of shape can be -1 {}", CHECK_LOCATION().AsString()));
3195  }
3196 
3197  auto targetNumElements =
3198  armnn::numeric_cast<unsigned int>(
3199  std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
3200 
3201  auto stretchIndex = static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
3202  outputDims[stretchIndex] = inputTensorInfo.GetNumElements() / targetNumElements;
3203  }
3204 
3205  TensorShape outputShape = TensorShape(static_cast<unsigned int>(outputDims.size()), outputDims.data());
3206 
3207  TensorInfo reshapeInfo = inputTensorInfo;
3208  reshapeInfo.SetShape(outputShape);
3209 
3210  return reshapeInfo;
3211 }

References CHECK_LOCATION, TensorInfo::GetNumElements(), and TensorInfo::SetShape().

◆ OutputShapeOfSqueeze()

armnn::TensorInfo OutputShapeOfSqueeze ( std::vector< uint32_t >  squeezeDims,
const armnn::TensorInfo inputTensorInfo 
)
static

Definition at line 2312 of file TfLiteParser.cpp.

2314 {
2315  CHECK_VALID_SIZE(squeezeDims.size(), 0, 1, 2, 3, 4);
2316  static const uint32_t dimensionSequence[] = { 0, 1, 2, 3 };
2317 
2318  if (inputTensorInfo.GetNumDimensions() > 4)
2319  {
2320  std::stringstream ss;
2321  ss << "Input tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
2322  << " shape:" << inputTensorInfo.GetShape() << " "
2323  << CHECK_LOCATION().AsString();
2324  throw ParseException(ss.str());
2325  }
2326 
2327  if (squeezeDims.empty())
2328  {
2329  squeezeDims.assign(dimensionSequence,
2330  dimensionSequence+inputTensorInfo.GetNumDimensions());
2331  }
2332 
2333  std::vector<uint32_t> outputDims;
2334  for(unsigned int i = 0; i < inputTensorInfo.GetNumDimensions(); i++)
2335  {
2336  bool skipSqueeze = (std::find(squeezeDims.begin(), squeezeDims.end(), i) == squeezeDims.end());
2337  auto currentDimension = inputTensorInfo.GetShape()[i];
2338  if (skipSqueeze || currentDimension != 1)
2339  {
2340  outputDims.push_back(currentDimension);
2341  }
2342  }
2343 
2344  if (outputDims.size() > 4)
2345  {
2346  std::stringstream ss;
2347  ss << "Output tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
2348  << " shape:" << inputTensorInfo.GetShape() << " "
2349  << CHECK_LOCATION().AsString();
2350  throw ParseException(ss.str());
2351  }
2352 
2353  TensorShape outShape = TensorShape(static_cast<unsigned int>(outputDims.size()),
2354  outputDims.data());
2355 
2356  // we need to preserve the tensor type and the quantization data as well
2357  TensorInfo outTensorInfo = inputTensorInfo;
2358  outTensorInfo.SetShape(outShape);
2359 
2360  return outTensorInfo;
2361 }

References CHECK_LOCATION, CHECK_VALID_SIZE, TensorInfo::GetNumDimensions(), TensorInfo::GetShape(), and TensorInfo::SetShape().


The documentation for this class was generated from the following files:
armnn::TensorInfo::GetNumElements
unsigned int GetNumElements() const
Definition: Tensor.hpp:196
armnnTfLiteParser::TfLiteParserImpl::TensorRawPtrVector
std::vector< TensorRawPtr > TensorRawPtrVector
Definition: TfLiteParser.hpp:36
armnn::TensorInfo
Definition: Tensor.hpp:152
armnnTfLiteParser::TfLiteParserImpl::GetSubgraphOutputs
static TensorIdRawPtrVector GetSubgraphOutputs(const ModelPtr &model, size_t subgraphIndex)
Definition: TfLiteParser.cpp:5368
armnn::TensorInfo::GetNumDimensions
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:195
CHECK_LOCATION
#define CHECK_LOCATION()
Definition: Exceptions.hpp:203
armnnTfLiteParser::TfLiteParserImpl::LoadModelFromFile
static ModelPtr LoadModelFromFile(const char *fileName)
Definition: TfLiteParser.cpp:5255
armnnTfLiteParser::TfLiteParserImpl::LoadModelFromBinary
static ModelPtr LoadModelFromBinary(const uint8_t *binaryContent, size_t len)
Definition: TfLiteParser.cpp:5285
CHECKED_NON_NEGATIVE
#define CHECKED_NON_NEGATIVE(VALUE)
Definition: VerificationHelpers.hpp:35
armnn::TensorShape
Definition: Tensor.hpp:20
CHECK_TENSOR
#define CHECK_TENSOR(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX)
Definition: TfLiteParser.cpp:203
armnn_driver::Model
::android::nn::Model Model
Helper classes.
Definition: ConversionUtils.hpp:43
armnn::InvalidArgumentException
Definition: Exceptions.hpp:80
CHECK_MODEL
#define CHECK_MODEL(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX)
Definition: TfLiteParser.cpp:182
armnnTfLiteParser::TfLiteParserImpl::TensorIdRawPtrVector
std::vector< TensorIdRawPtr > TensorIdRawPtrVector
Definition: TfLiteParser.hpp:38
armnnTfLiteParser::TfLiteParserImpl::GetSubgraphInputs
static TensorIdRawPtrVector GetSubgraphInputs(const ModelPtr &model, size_t subgraphIndex)
Definition: TfLiteParser.cpp:5351
armnnDeserializer::ToTensorInfo
armnn::TensorInfo ToTensorInfo(TensorRawPtr tensorPtr)
Definition: Deserializer.cpp:652
armnn::TensorInfo::GetShape
const TensorShape & GetShape() const
Definition: Tensor.hpp:191
armnn::ParseException
Definition: Exceptions.hpp:92
TFLITE_PARSER_VERSION
#define TFLITE_PARSER_VERSION
TFLITE_PARSER_VERSION: "X.Y.Z" where: X = Major version number Y = Minor version number Z = Patch ver...
Definition: Version.hpp:25
armnn::TensorInfo::SetShape
void SetShape(const TensorShape &newShape)
Definition: Tensor.hpp:193
CHECK_VALID_SIZE
#define CHECK_VALID_SIZE(ACTUAL,...)
Definition: VerificationHelpers.hpp:32
CHECK_SUBGRAPH
#define CHECK_SUBGRAPH(MODEL, SUBGRAPH_INDEX)
Definition: TfLiteParser.cpp:140
CHECK_BUFFER
#define CHECK_BUFFER(MODEL, BUFFER_INDEX)
Definition: TfLiteParser.cpp:251
armnn::FileNotFoundException
Definition: Exceptions.hpp:86