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

◆ ~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 959 of file TfLiteParser.cpp.

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

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 952 of file TfLiteParser.cpp.

953 {
954  ResetParser();
955  m_Model = LoadModelFromFile(graphFile);
956  return CreateNetworkFromModel();
957 }

References TfLiteParserImpl::LoadModelFromFile().

◆ GetBuffer()

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

Definition at line 5714 of file TfLiteParser.cpp.

5715 {
5716  CHECK_BUFFER(model, bufferIndex);
5717  return model->buffers[bufferIndex].get();
5718 }

References CHECK_BUFFER.

◆ GetInputs()

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

Definition at line 5402 of file TfLiteParser.cpp.

5405 {
5406  CHECK_MODEL(model, subgraphIndex, operatorIndex);
5407 
5408  const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5409  const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
5410 
5411  size_t inputCount = operatorPtr->inputs.size();
5412  TensorRawPtrVector result;
5413  for (size_t i = 0; i < inputCount; ++i)
5414  {
5415  // If the input location is -1 then assume input is turned off.
5416  if (operatorPtr->inputs[i] == -1)
5417  {
5418  continue;
5419  }
5420  else
5421  {
5422  uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[i]);
5423  result.push_back(subgraphPtr->tensors[inputId].get());
5424  }
5425  }
5426  return result;
5427 }

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 5482 of file TfLiteParser.cpp.

5485 {
5486  CHECK_MODEL(model, subgraphIndex, operatorIndex);
5487  const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5488  const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
5489  return operatorPtr->inputs;
5490 }

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 5892 of file TfLiteParser.cpp.

5894 {
5895  CHECK_SUBGRAPH(m_Model, subgraphId);
5896  auto inputs = GetSubgraphInputs(m_Model, subgraphId);
5897  for (auto const& input : inputs)
5898  {
5899  if (input.second->name == name)
5900  {
5901  auto bindingId = GenerateLayerBindingId(subgraphId, input.first);
5902  auto inputTensorInfo = ToTensorInfo(input.second);
5903  // Input tensors are always treated as constant tensors during network execution.
5904  inputTensorInfo.SetConstant(true);
5905  return std::make_pair(bindingId, inputTensorInfo);
5906  }
5907  }
5908 
5909  std::stringstream bindings;
5910  for (auto const& input : inputs)
5911  {
5912  bindings << "'" << input.second->name << "' ";
5913  }
5914 
5915  throw ParseException(
5916  fmt::format("No input binding found for subgraph:{} and name:{}. "
5917  "Possible inputs are: [{}] {}",
5918  subgraphId,
5919  name,
5920  bindings.str(),
5921  CHECK_LOCATION().AsString()));
5922 }

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 5924 of file TfLiteParser.cpp.

5926 {
5927  CHECK_SUBGRAPH(m_Model, subgraphId);
5928  auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
5929  for (unsigned int i = 0; i < outputs.size(); ++i)
5930  {
5931  auto const output = outputs[i];
5932  if (output.second->name == name)
5933  {
5934  auto bindingId = GenerateLayerBindingId(subgraphId, output.first);
5935  std::vector<unsigned int> shape = m_OverriddenOutputShapes.size() > 0 ?
5936  m_OverriddenOutputShapes[i] : AsUnsignedVector(output.second->shape);
5937  return std::make_pair(bindingId, ToTensorInfo(output.second, shape));
5938  }
5939  }
5940 
5941  std::stringstream bindings;
5942  for (auto const& output : outputs)
5943  {
5944  bindings << "'" << output.second->name << "' ";
5945  }
5946 
5947  throw ParseException(
5948  fmt::format("No output binding found for subgraph:{} and name:{}. "
5949  "Possible outputs are: [{}] {}",
5950  subgraphId,
5951  name,
5952  bindings.str(),
5953  CHECK_LOCATION().AsString()));
5954 }

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 5429 of file TfLiteParser.cpp.

5432 {
5433  CHECK_MODEL(model, subgraphIndex, operatorIndex);
5434 
5435  const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5436  const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
5437 
5438  size_t outputCount = operatorPtr->outputs.size();
5439  TensorRawPtrVector result(outputCount);
5440  for (size_t i = 0; i < outputCount; ++i)
5441  {
5442  uint32_t outputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[i]);
5443  CHECK_TENSOR(model, subgraphIndex, outputId);
5444  result[i] = subgraphPtr->tensors[outputId].get();
5445  }
5446  return result;
5447 }

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 5492 of file TfLiteParser.cpp.

5495 {
5496  CHECK_MODEL(model, subgraphIndex, operatorIndex);
5497  const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5498  const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
5499  return operatorPtr->outputs;
5500 }

References CHECK_MODEL.

◆ GetSubgraphCount()

size_t GetSubgraphCount ( ) const

Return the number of subgraphs in the parsed model.

Definition at line 5956 of file TfLiteParser.cpp.

5957 {
5958  return m_Model->subgraphs.size();
5959 }

◆ GetSubgraphInputs()

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

Definition at line 5449 of file TfLiteParser.cpp.

5451 {
5452  CHECK_SUBGRAPH(model, subgraphIndex);
5453  const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5454 
5455  size_t inputCount = subgraphPtr->inputs.size();
5456  TensorIdRawPtrVector result(inputCount);
5457  for (size_t i = 0; i < inputCount; ++i)
5458  {
5459  uint32_t inputId = CHECKED_NON_NEGATIVE(subgraphPtr->inputs[i]);
5460  CHECK_TENSOR(model, subgraphIndex, inputId);
5461  result[i] = std::make_pair(inputId, subgraphPtr->tensors[inputId].get());
5462  }
5463  return result;
5464 }

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 5961 of file TfLiteParser.cpp.

5962 {
5963  CHECK_SUBGRAPH(m_Model, subgraphId);
5964  auto inputs = GetSubgraphInputs(m_Model, subgraphId);
5965  std::vector<std::string> result;
5966  result.reserve(inputs.size());
5967  for (auto const& input : inputs)
5968  {
5969  result.push_back(input.second->name);
5970  }
5971  return result;
5972 }

References CHECK_SUBGRAPH, and TfLiteParserImpl::GetSubgraphInputs().

◆ GetSubgraphOutputs()

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

Definition at line 5466 of file TfLiteParser.cpp.

5468 {
5469  CHECK_SUBGRAPH(model, subgraphIndex);
5470  const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5471 
5472  size_t outputCount = subgraphPtr->outputs.size();
5473  TensorIdRawPtrVector result(outputCount);
5474  for (size_t i = 0; i < outputCount; ++i)
5475  {
5476  uint32_t outputId = CHECKED_NON_NEGATIVE(subgraphPtr->outputs[i]);
5477  result[i] = std::make_pair(outputId, subgraphPtr->tensors[outputId].get());
5478  }
5479  return result;
5480 }

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 5974 of file TfLiteParser.cpp.

5975 {
5976  CHECK_SUBGRAPH(m_Model, subgraphId);
5977  auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
5978  std::vector<std::string> result;
5979  result.reserve(outputs.size());
5980  for (auto const& output : outputs)
5981  {
5982  result.push_back(output.second->name);
5983  }
5984  return result;
5985 }

References CHECK_SUBGRAPH, and TfLiteParserImpl::GetSubgraphOutputs().

◆ GetVersion()

const std::string GetVersion ( )
static

Retrieve version in X.Y.Z form.

Definition at line 5987 of file TfLiteParser.cpp.

5988 {
5989  return TFLITE_PARSER_VERSION;
5990 }

References TFLITE_PARSER_VERSION.

◆ LoadModel()

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

Definition at line 967 of file TfLiteParser.cpp.

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

◆ LoadModelFromBinary()

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

Definition at line 5383 of file TfLiteParser.cpp.

5384 {
5385  if (binaryContent == nullptr)
5386  {
5387  throw InvalidArgumentException(fmt::format("Invalid (null) binary content {}",
5388  CHECK_LOCATION().AsString()));
5389  }
5390  flatbuffers::Verifier verifier(binaryContent, len);
5391  if (verifier.VerifyBuffer<tflite::Model>() == false)
5392  {
5393  throw ParseException(
5394  fmt::format("Buffer doesn't conform to the expected Tensorflow Lite "
5395  "flatbuffers format. size:{} {}",
5396  len,
5397  CHECK_LOCATION().AsString()));
5398  }
5399  return tflite::UnPackModel(binaryContent);
5400 }

References CHECK_LOCATION.

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

◆ LoadModelFromFile()

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

Definition at line 5353 of file TfLiteParser.cpp.

5354 {
5355  if (fileName == nullptr)
5356  {
5357  throw InvalidArgumentException(fmt::format("Invalid (null) file name {}",
5358  CHECK_LOCATION().AsString()));
5359  }
5360  std::error_code errorCode;
5361  fs::path pathToFile(fileName);
5362  if (!fs::exists(pathToFile, errorCode))
5363  {
5364  //fmt::format() could not be used here (format error)
5365  std::stringstream msg;
5366  msg << "Cannot find the file (" << fileName << ") errorCode: " << errorCode
5367  << " " << CHECK_LOCATION().AsString();
5368  throw FileNotFoundException(msg.str());
5369  }
5370  if (!fs::is_regular_file(pathToFile))
5371  {
5372  // Exclude non regular files.
5373  throw InvalidArgumentException(fmt::format("File \"{}\" is not a regular file and cannot be loaded.",
5374  pathToFile.c_str()));
5375  }
5376 
5377  std::ifstream file(fileName, std::ios::binary);
5378  std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
5379  return LoadModelFromBinary(reinterpret_cast<const uint8_t *>(fileContent.c_str()),
5380  fileContent.size());
5381 }

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 3262 of file TfLiteParser.cpp.

3264 {
3265  std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
3266  const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
3267 
3268  if (stretchDim != targetDimsIn.end())
3269  {
3270  if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
3271  {
3272  throw ParseException(
3273  fmt::format("At most one component of shape can be -1 {}", CHECK_LOCATION().AsString()));
3274  }
3275 
3276  auto targetNumElements =
3277  armnn::numeric_cast<unsigned int>(
3278  std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
3279 
3280  auto stretchIndex = static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
3281 
3282  if (targetNumElements == 0)
3283  {
3284  if (inputTensorInfo.GetNumElements() == 0)
3285  {
3286  outputDims[stretchIndex] = 0;
3287  }
3288  else
3289  {
3290  throw ParseException(
3291  fmt::format("Input to reshape is a tensor with elements, but the requested shape has 0. {}",
3292  CHECK_LOCATION().AsString()));
3293  }
3294  }
3295  else
3296  {
3297  outputDims[stretchIndex] = inputTensorInfo.GetNumElements() / targetNumElements;
3298  }
3299  }
3300 
3301  TensorShape outputShape = TensorShape(static_cast<unsigned int>(outputDims.size()), outputDims.data());
3302 
3303  TensorInfo reshapeInfo = inputTensorInfo;
3304  reshapeInfo.SetShape(outputShape);
3305 
3306  return reshapeInfo;
3307 }

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 2381 of file TfLiteParser.cpp.

2383 {
2384  CHECK_VALID_SIZE(squeezeDims.size(), 0, 1, 2, 3, 4);
2385  static const uint32_t dimensionSequence[] = { 0, 1, 2, 3 };
2386 
2387  if (inputTensorInfo.GetNumDimensions() > 4)
2388  {
2389  std::stringstream ss;
2390  ss << "Input tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
2391  << " shape:" << inputTensorInfo.GetShape() << " "
2392  << CHECK_LOCATION().AsString();
2393  throw ParseException(ss.str());
2394  }
2395 
2396  if (squeezeDims.empty())
2397  {
2398  squeezeDims.assign(dimensionSequence,
2399  dimensionSequence+inputTensorInfo.GetNumDimensions());
2400  }
2401 
2402  std::vector<uint32_t> outputDims;
2403  for(unsigned int i = 0; i < inputTensorInfo.GetNumDimensions(); i++)
2404  {
2405  bool skipSqueeze = (std::find(squeezeDims.begin(), squeezeDims.end(), i) == squeezeDims.end());
2406  auto currentDimension = inputTensorInfo.GetShape()[i];
2407  if (skipSqueeze || currentDimension != 1)
2408  {
2409  outputDims.push_back(currentDimension);
2410  }
2411  }
2412 
2413  if (outputDims.size() > 4)
2414  {
2415  std::stringstream ss;
2416  ss << "Output tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
2417  << " shape:" << inputTensorInfo.GetShape() << " "
2418  << CHECK_LOCATION().AsString();
2419  throw ParseException(ss.str());
2420  }
2421 
2422  TensorShape outShape = TensorShape(static_cast<unsigned int>(outputDims.size()),
2423  outputDims.data());
2424 
2425  // we need to preserve the tensor type and the quantization data as well
2426  TensorInfo outTensorInfo = inputTensorInfo;
2427  outTensorInfo.SetShape(outShape);
2428 
2429  return outTensorInfo;
2430 }

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:198
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:5466
armnn::TensorInfo::GetNumDimensions
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:197
CHECK_LOCATION
#define CHECK_LOCATION()
Definition: Exceptions.hpp:203
armnnTfLiteParser::TfLiteParserImpl::LoadModelFromFile
static ModelPtr LoadModelFromFile(const char *fileName)
Definition: TfLiteParser.cpp:5353
armnnTfLiteParser::TfLiteParserImpl::LoadModelFromBinary
static ModelPtr LoadModelFromBinary(const uint8_t *binaryContent, size_t len)
Definition: TfLiteParser.cpp:5383
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:45
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:5449
armnnDeserializer::ToTensorInfo
armnn::TensorInfo ToTensorInfo(TensorRawPtr tensorPtr)
Definition: Deserializer.cpp:654
armnn::TensorInfo::GetShape
const TensorShape & GetShape() const
Definition: Tensor.hpp:193
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:195
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