ArmNN
 23.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 719 of file TfLiteParser.cpp.

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

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

931 {
932  ResetParser();
933  m_Model = LoadModelFromBinary(binaryContent.data(), binaryContent.size());
934  return CreateNetworkFromModel();
935 }

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

924 {
925  ResetParser();
926  m_Model = LoadModelFromFile(graphFile);
927  return CreateNetworkFromModel();
928 }

References TfLiteParserImpl::LoadModelFromFile().

◆ GetBuffer()

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

Definition at line 5019 of file TfLiteParser.cpp.

5020 {
5021  CHECK_BUFFER(model, bufferIndex);
5022  return model->buffers[bufferIndex].get();
5023 }

References CHECK_BUFFER.

◆ GetInputs()

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

Definition at line 4718 of file TfLiteParser.cpp.

4721 {
4722  CHECK_MODEL(model, subgraphIndex, operatorIndex);
4723 
4724  const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4725  const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
4726 
4727  size_t inputCount = operatorPtr->inputs.size();
4728  TensorRawPtrVector result;
4729  for (size_t i = 0; i < inputCount; ++i)
4730  {
4731  // If the input location is -1 then assume input is turned off.
4732  if (operatorPtr->inputs[i] == -1)
4733  {
4734  continue;
4735  }
4736  else
4737  {
4738  uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[i]);
4739  result.push_back(subgraphPtr->tensors[inputId].get());
4740  }
4741  }
4742  return result;
4743 }

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

4801 {
4802  CHECK_MODEL(model, subgraphIndex, operatorIndex);
4803  const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4804  const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
4805  return operatorPtr->inputs;
4806 }

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

5199 {
5200  CHECK_SUBGRAPH(m_Model, subgraphId);
5201  auto inputs = GetSubgraphInputs(m_Model, subgraphId);
5202  for (auto const& input : inputs)
5203  {
5204  if (input.second->name == name)
5205  {
5206  auto bindingId = GenerateLayerBindingId(subgraphId, input.first);
5207  auto inputTensorInfo = ToTensorInfo(input.second);
5208  // Input tensors are always treated as constant tensors during network execution.
5209  inputTensorInfo.SetConstant(true);
5210  return std::make_pair(bindingId, inputTensorInfo);
5211  }
5212  }
5213 
5214  std::stringstream bindings;
5215  for (auto const& input : inputs)
5216  {
5217  bindings << "'" << input.second->name << "' ";
5218  }
5219 
5220  throw ParseException(
5221  fmt::format("No input binding found for subgraph:{} and name:{}. "
5222  "Possible inputs are: [{}] {}",
5223  subgraphId,
5224  name,
5225  bindings.str(),
5226  CHECK_LOCATION().AsString()));
5227 }

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

5231 {
5232  CHECK_SUBGRAPH(m_Model, subgraphId);
5233  auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
5234  for (unsigned int i = 0; i < outputs.size(); ++i)
5235  {
5236  auto const output = outputs[i];
5237  if (output.second->name == name)
5238  {
5239  auto bindingId = GenerateLayerBindingId(subgraphId, output.first);
5240  std::vector<unsigned int> shape = m_OverriddenOutputShapes.size() > 0 ?
5241  m_OverriddenOutputShapes[i] : AsUnsignedVector(output.second->shape);
5242  return std::make_pair(bindingId, ToTensorInfo(output.second, shape));
5243  }
5244  }
5245 
5246  std::stringstream bindings;
5247  for (auto const& output : outputs)
5248  {
5249  bindings << "'" << output.second->name << "' ";
5250  }
5251 
5252  throw ParseException(
5253  fmt::format("No output binding found for subgraph:{} and name:{}. "
5254  "Possible outputs are: [{}] {}",
5255  subgraphId,
5256  name,
5257  bindings.str(),
5258  CHECK_LOCATION().AsString()));
5259 }

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

4748 {
4749  CHECK_MODEL(model, subgraphIndex, operatorIndex);
4750 
4751  const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4752  const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
4753 
4754  size_t outputCount = operatorPtr->outputs.size();
4755  TensorRawPtrVector result(outputCount);
4756  for (size_t i = 0; i < outputCount; ++i)
4757  {
4758  uint32_t outputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[i]);
4759  CHECK_TENSOR(model, subgraphIndex, outputId);
4760  result[i] = subgraphPtr->tensors[outputId].get();
4761  }
4762  return result;
4763 }

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

4811 {
4812  CHECK_MODEL(model, subgraphIndex, operatorIndex);
4813  const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4814  const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
4815  return operatorPtr->outputs;
4816 }

References CHECK_MODEL.

◆ GetSubgraphCount()

size_t GetSubgraphCount ( ) const

Return the number of subgraphs in the parsed model.

Definition at line 5261 of file TfLiteParser.cpp.

5262 {
5263  return m_Model->subgraphs.size();
5264 }

◆ GetSubgraphInputs()

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

Definition at line 4765 of file TfLiteParser.cpp.

4767 {
4768  CHECK_SUBGRAPH(model, subgraphIndex);
4769  const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4770 
4771  size_t inputCount = subgraphPtr->inputs.size();
4772  TensorIdRawPtrVector result(inputCount);
4773  for (size_t i = 0; i < inputCount; ++i)
4774  {
4775  uint32_t inputId = CHECKED_NON_NEGATIVE(subgraphPtr->inputs[i]);
4776  CHECK_TENSOR(model, subgraphIndex, inputId);
4777  result[i] = std::make_pair(inputId, subgraphPtr->tensors[inputId].get());
4778  }
4779  return result;
4780 }

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

5267 {
5268  CHECK_SUBGRAPH(m_Model, subgraphId);
5269  auto inputs = GetSubgraphInputs(m_Model, subgraphId);
5270  std::vector<std::string> result;
5271  result.reserve(inputs.size());
5272  for (auto const& input : inputs)
5273  {
5274  result.push_back(input.second->name);
5275  }
5276  return result;
5277 }

References CHECK_SUBGRAPH, and TfLiteParserImpl::GetSubgraphInputs().

◆ GetSubgraphOutputs()

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

Definition at line 4782 of file TfLiteParser.cpp.

4784 {
4785  CHECK_SUBGRAPH(model, subgraphIndex);
4786  const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4787 
4788  size_t outputCount = subgraphPtr->outputs.size();
4789  TensorIdRawPtrVector result(outputCount);
4790  for (size_t i = 0; i < outputCount; ++i)
4791  {
4792  uint32_t outputId = CHECKED_NON_NEGATIVE(subgraphPtr->outputs[i]);
4793  result[i] = std::make_pair(outputId, subgraphPtr->tensors[outputId].get());
4794  }
4795  return result;
4796 }

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

5280 {
5281  CHECK_SUBGRAPH(m_Model, subgraphId);
5282  auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
5283  std::vector<std::string> result;
5284  result.reserve(outputs.size());
5285  for (auto const& output : outputs)
5286  {
5287  result.push_back(output.second->name);
5288  }
5289  return result;
5290 }

References CHECK_SUBGRAPH, and TfLiteParserImpl::GetSubgraphOutputs().

◆ GetVersion()

const std::string GetVersion ( )
static

Retrieve version in X.Y.Z form.

Definition at line 5292 of file TfLiteParser.cpp.

5293 {
5294  return TFLITE_PARSER_VERSION;
5295 }

References TFLITE_PARSER_VERSION.

◆ LoadModel()

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

Definition at line 938 of file TfLiteParser.cpp.

939 {
940  ResetParser();
941  m_Model = std::move(model);
942 
943  return CreateNetworkFromModel();
944 }

◆ LoadModelFromBinary()

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

Definition at line 4699 of file TfLiteParser.cpp.

4700 {
4701  if (binaryContent == nullptr)
4702  {
4703  throw InvalidArgumentException(fmt::format("Invalid (null) binary content {}",
4704  CHECK_LOCATION().AsString()));
4705  }
4706  flatbuffers::Verifier verifier(binaryContent, len);
4707  if (verifier.VerifyBuffer<tflite::Model>() == false)
4708  {
4709  throw ParseException(
4710  fmt::format("Buffer doesn't conform to the expected Tensorflow Lite "
4711  "flatbuffers format. size:{} {}",
4712  len,
4713  CHECK_LOCATION().AsString()));
4714  }
4715  return tflite::UnPackModel(binaryContent);
4716 }

References CHECK_LOCATION.

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

◆ LoadModelFromFile()

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

Definition at line 4675 of file TfLiteParser.cpp.

4676 {
4677  if (fileName == nullptr)
4678  {
4679  throw InvalidArgumentException(fmt::format("Invalid (null) file name {}",
4680  CHECK_LOCATION().AsString()));
4681  }
4682  std::error_code errorCode;
4683  fs::path pathToFile(fileName);
4684  if (!fs::exists(pathToFile, errorCode))
4685  {
4686  //fmt::format() could not be used here (format error)
4687  std::stringstream msg;
4688  msg << "Cannot find the file (" << fileName << ") errorCode: " << errorCode
4689  << " " << CHECK_LOCATION().AsString();
4690 
4691  throw FileNotFoundException(msg.str());
4692  }
4693  std::ifstream file(fileName, std::ios::binary);
4694  std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
4695  return LoadModelFromBinary(reinterpret_cast<const uint8_t *>(fileContent.c_str()),
4696  fileContent.size());
4697 }

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

2884 {
2885  std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
2886  const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
2887 
2888  if (stretchDim != targetDimsIn.end())
2889  {
2890  if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
2891  {
2892  throw ParseException(
2893  fmt::format("At most one component of shape can be -1 {}", CHECK_LOCATION().AsString()));
2894  }
2895 
2896  auto targetNumElements =
2897  armnn::numeric_cast<unsigned int>(
2898  std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
2899 
2900  auto stretchIndex = static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
2901  outputDims[stretchIndex] = inputTensorInfo.GetNumElements() / targetNumElements;
2902  }
2903 
2904  TensorShape outputShape = TensorShape(static_cast<unsigned int>(outputDims.size()), outputDims.data());
2905 
2906  TensorInfo reshapeInfo = inputTensorInfo;
2907  reshapeInfo.SetShape(outputShape);
2908 
2909  return reshapeInfo;
2910 }

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

2139 {
2140  CHECK_VALID_SIZE(squeezeDims.size(), 0, 1, 2, 3, 4);
2141  static const uint32_t dimensionSequence[] = { 0, 1, 2, 3 };
2142 
2143  if (inputTensorInfo.GetNumDimensions() > 4)
2144  {
2145  std::stringstream ss;
2146  ss << "Input tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
2147  << " shape:" << inputTensorInfo.GetShape() << " "
2148  << CHECK_LOCATION().AsString();
2149  throw ParseException(ss.str());
2150  }
2151 
2152  if (squeezeDims.empty())
2153  {
2154  squeezeDims.assign(dimensionSequence,
2155  dimensionSequence+inputTensorInfo.GetNumDimensions());
2156  }
2157 
2158  std::vector<uint32_t> outputDims;
2159  for(unsigned int i = 0; i < inputTensorInfo.GetNumDimensions(); i++)
2160  {
2161  bool skipSqueeze = (std::find(squeezeDims.begin(), squeezeDims.end(), i) == squeezeDims.end());
2162  auto currentDimension = inputTensorInfo.GetShape()[i];
2163  if (skipSqueeze || currentDimension != 1)
2164  {
2165  outputDims.push_back(currentDimension);
2166  }
2167  }
2168 
2169  if (outputDims.size() > 4)
2170  {
2171  std::stringstream ss;
2172  ss << "Output tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
2173  << " shape:" << inputTensorInfo.GetShape() << " "
2174  << CHECK_LOCATION().AsString();
2175  throw ParseException(ss.str());
2176  }
2177 
2178  TensorShape outShape = TensorShape(static_cast<unsigned int>(outputDims.size()),
2179  outputDims.data());
2180 
2181  // we need to preserve the tensor type and the quantization data as well
2182  TensorInfo outTensorInfo = inputTensorInfo;
2183  outTensorInfo.SetShape(outShape);
2184 
2185  return outTensorInfo;
2186 }

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


The documentation for this class was generated from the following files:
armnnTfLiteParser::TfLiteParserImpl::GetSubgraphOutputs
static TensorIdRawPtrVector GetSubgraphOutputs(const ModelPtr &model, size_t subgraphIndex)
Definition: TfLiteParser.cpp:4782
armnnTfLiteParser::TfLiteParserImpl::LoadModelFromFile
static ModelPtr LoadModelFromFile(const char *fileName)
Definition: TfLiteParser.cpp:4675
CHECK_BUFFER
#define CHECK_BUFFER(MODEL, BUFFER_INDEX)
Definition: TfLiteParser.cpp:260
CHECK_LOCATION
#define CHECK_LOCATION()
Definition: Exceptions.hpp:203
armnn::ParseException
Definition: Exceptions.hpp:92
armnn_driver::Model
::android::nn::Model Model
Helper classes.
Definition: ConversionUtils.hpp:43
CHECK_TENSOR
#define CHECK_TENSOR(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX)
Definition: TfLiteParser.cpp:212
armnn::TensorShape
Definition: Tensor.hpp:20
armnnTfLiteParser::TfLiteParserImpl::LoadModelFromBinary
static ModelPtr LoadModelFromBinary(const uint8_t *binaryContent, size_t len)
Definition: TfLiteParser.cpp:4699
armnn::TensorInfo::GetNumDimensions
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:195
CHECK_SUBGRAPH
#define CHECK_SUBGRAPH(MODEL, SUBGRAPH_INDEX)
Definition: TfLiteParser.cpp:141
armnn::TensorInfo::GetNumElements
unsigned int GetNumElements() const
Definition: Tensor.hpp:196
armnn::TensorInfo
Definition: Tensor.hpp:152
CHECKED_NON_NEGATIVE
#define CHECKED_NON_NEGATIVE(VALUE)
Definition: VerificationHelpers.hpp:35
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::GetShape
const TensorShape & GetShape() const
Definition: Tensor.hpp:191
armnnTfLiteParser::TfLiteParserImpl::TensorRawPtrVector
std::vector< TensorRawPtr > TensorRawPtrVector
Definition: TfLiteParser.hpp:36
CHECK_VALID_SIZE
#define CHECK_VALID_SIZE(ACTUAL,...)
Definition: VerificationHelpers.hpp:32
armnn::FileNotFoundException
Definition: Exceptions.hpp:86
armnnTfLiteParser::TfLiteParserImpl::TensorIdRawPtrVector
std::vector< TensorIdRawPtr > TensorIdRawPtrVector
Definition: TfLiteParser.hpp:38
armnnDeserializer::ToTensorInfo
armnn::TensorInfo ToTensorInfo(TensorRawPtr tensorPtr)
Definition: Deserializer.cpp:616
armnnTfLiteParser::TfLiteParserImpl::GetSubgraphInputs
static TensorIdRawPtrVector GetSubgraphInputs(const ModelPtr &model, size_t subgraphIndex)
Definition: TfLiteParser.cpp:4765
armnn::TensorInfo::SetShape
void SetShape(const TensorShape &newShape)
Definition: Tensor.hpp:193
CHECK_MODEL
#define CHECK_MODEL(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX)
Definition: TfLiteParser.cpp:183
armnn::InvalidArgumentException
Definition: Exceptions.hpp:80