ArmNN
 23.08
OnnxParserImpl Class Reference

#include <OnnxParser.hpp>

Public Types

using GraphPtr = std::unique_ptr< onnx::GraphProto >
 

Public Member Functions

armnn::INetworkPtr CreateNetworkFromBinaryFile (const char *graphFile)
 Create the network from a protobuf binary file on disk. More...
 
armnn::INetworkPtr CreateNetworkFromBinaryFile (const char *graphFile, const std::map< std::string, armnn::TensorShape > &inputShapes)
 Create the network from a protobuf binary file on disk, with inputShapes specified. More...
 
armnn::INetworkPtr CreateNetworkFromBinary (const std::vector< uint8_t > &binaryContent)
 Create the network from a protobuf binary. More...
 
armnn::INetworkPtr CreateNetworkFromBinary (const std::vector< uint8_t > &binaryContent, const std::map< std::string, armnn::TensorShape > &inputShapes)
 Create the network from a protobuf binary, with inputShapes specified. More...
 
armnn::INetworkPtr CreateNetworkFromTextFile (const char *graphFile)
 Create the network from a protobuf text file on disk. More...
 
armnn::INetworkPtr CreateNetworkFromTextFile (const char *graphFile, const std::map< std::string, armnn::TensorShape > &inputShapes)
 Create the network from a protobuf text file on disk, with inputShapes specified. More...
 
armnn::INetworkPtr CreateNetworkFromString (const std::string &protoText)
 Create the network directly from protobuf text in a string. Useful for debugging/testing. More...
 
armnn::INetworkPtr CreateNetworkFromString (const std::string &protoText, const std::map< std::string, armnn::TensorShape > &inputShapes)
 Create the network directly from protobuf text in a string, with inputShapes specified. More...
 
BindingPointInfo GetNetworkInputBindingInfo (const std::string &name) const
 Retrieve binding info (layer id and tensor info) for the network input identified by the given layer name. More...
 
BindingPointInfo GetNetworkOutputBindingInfo (const std::string &name) const
 Retrieve binding info (layer id and tensor info) for the network output identified by the given layer name. More...
 
 OnnxParserImpl ()
 
 ~OnnxParserImpl ()=default
 
template<typename TypePair , typename Location >
void ValidateInputs (const onnx::NodeProto &node, TypePair validInputs, const Location &location)
 

Static Public Member Functions

static ModelPtr LoadModelFromBinary (const std::vector< uint8_t > &binaryContent)
 
static ModelPtr LoadModelFromBinaryFile (const char *fileName)
 
static ModelPtr LoadModelFromTextFile (const char *fileName)
 
static ModelPtr LoadModelFromString (const std::string &inputString)
 
static std::vector< std::string > GetInputs (ModelPtr &model)
 Retrieve inputs names. More...
 
static std::vector< std::string > GetOutputs (ModelPtr &model)
 Retrieve outputs names. More...
 
static const std::string GetVersion ()
 Retrieve version in X.Y.Z form. More...
 

Detailed Description

Definition at line 25 of file OnnxParser.hpp.

Member Typedef Documentation

◆ GraphPtr

using GraphPtr = std::unique_ptr<onnx::GraphProto>

Definition at line 32 of file OnnxParser.hpp.

Constructor & Destructor Documentation

◆ OnnxParserImpl()

Definition at line 562 of file OnnxParser.cpp.

563  : m_Network(nullptr, nullptr)
564 {
565 }

◆ ~OnnxParserImpl()

~OnnxParserImpl ( )
default

Member Function Documentation

◆ CreateNetworkFromBinary() [1/2]

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

Create the network from a protobuf binary.

Definition at line 757 of file OnnxParser.cpp.

758 {
759  ResetParser();
760  ModelPtr modelProto = LoadModelFromBinary(binaryContent);
761  return CreateNetworkFromModel(*modelProto);
762 }

References OnnxParserImpl::LoadModelFromBinary().

◆ CreateNetworkFromBinary() [2/2]

INetworkPtr CreateNetworkFromBinary ( const std::vector< uint8_t > &  binaryContent,
const std::map< std::string, armnn::TensorShape > &  inputShapes 
)

Create the network from a protobuf binary, with inputShapes specified.

Definition at line 764 of file OnnxParser.cpp.

766 {
767  ResetParser();
768  m_InputShapes = inputShapes;
769  ModelPtr modelProto = LoadModelFromBinary(binaryContent);
770  return CreateNetworkFromModel(*modelProto);
771 }

References OnnxParserImpl::LoadModelFromBinary().

◆ CreateNetworkFromBinaryFile() [1/2]

INetworkPtr CreateNetworkFromBinaryFile ( const char *  graphFile)

Create the network from a protobuf binary file on disk.

Definition at line 823 of file OnnxParser.cpp.

824 {
825  ResetParser();
826  ModelPtr modelProto = LoadModelFromBinaryFile(graphFile);
827  return CreateNetworkFromModel(*modelProto);
828 }

References OnnxParserImpl::LoadModelFromBinaryFile().

◆ CreateNetworkFromBinaryFile() [2/2]

INetworkPtr CreateNetworkFromBinaryFile ( const char *  graphFile,
const std::map< std::string, armnn::TensorShape > &  inputShapes 
)

Create the network from a protobuf binary file on disk, with inputShapes specified.

Definition at line 830 of file OnnxParser.cpp.

832 {
833  ResetParser();
834  m_InputShapes = inputShapes;
835  ModelPtr modelProto = LoadModelFromBinaryFile(graphFile);
836  return CreateNetworkFromModel(*modelProto);
837 }

References OnnxParserImpl::LoadModelFromBinaryFile().

◆ CreateNetworkFromString() [1/2]

INetworkPtr CreateNetworkFromString ( const std::string &  protoText)

Create the network directly from protobuf text in a string. Useful for debugging/testing.

Definition at line 858 of file OnnxParser.cpp.

859 {
860  ResetParser();
861  ModelPtr modelProto = LoadModelFromString(protoText);
862  return CreateNetworkFromModel(*modelProto);
863 }

References OnnxParserImpl::LoadModelFromString().

◆ CreateNetworkFromString() [2/2]

INetworkPtr CreateNetworkFromString ( const std::string &  protoText,
const std::map< std::string, armnn::TensorShape > &  inputShapes 
)

Create the network directly from protobuf text in a string, with inputShapes specified.

Useful for debugging/testing

Definition at line 865 of file OnnxParser.cpp.

867 {
868  ResetParser();
869  m_InputShapes = inputShapes;
870  ModelPtr modelProto = LoadModelFromString(protoText);
871  return CreateNetworkFromModel(*modelProto);
872 }

References OnnxParserImpl::LoadModelFromString().

◆ CreateNetworkFromTextFile() [1/2]

INetworkPtr CreateNetworkFromTextFile ( const char *  graphFile)

Create the network from a protobuf text file on disk.

Definition at line 741 of file OnnxParser.cpp.

742 {
743  ResetParser();
744  ModelPtr modelProto = LoadModelFromTextFile(graphFile);
745  return CreateNetworkFromModel(*modelProto);
746 }

References OnnxParserImpl::LoadModelFromTextFile().

◆ CreateNetworkFromTextFile() [2/2]

INetworkPtr CreateNetworkFromTextFile ( const char *  graphFile,
const std::map< std::string, armnn::TensorShape > &  inputShapes 
)

Create the network from a protobuf text file on disk, with inputShapes specified.

Definition at line 748 of file OnnxParser.cpp.

750 {
751  ResetParser();
752  m_InputShapes = inputShapes;
753  ModelPtr modelProto = LoadModelFromTextFile(graphFile);
754  return CreateNetworkFromModel(*modelProto);
755 }

References OnnxParserImpl::LoadModelFromTextFile().

◆ GetInputs()

std::vector< std::string > GetInputs ( ModelPtr model)
static

Retrieve inputs names.

Definition at line 2645 of file OnnxParser.cpp.

2646 {
2647  if(model == nullptr) {
2648  throw InvalidArgumentException(fmt::format("The given model cannot be null {}",
2649  CHECK_LOCATION().AsString()));
2650  }
2651 
2652  std::vector<std::string> inputNames;
2653  std::map<std::string, bool> isConstant;
2654  for(auto tensor : model->graph().initializer())
2655  {
2656  isConstant[tensor.name()] = true;
2657  }
2658  for(auto input : model->graph().input())
2659  {
2660  auto it = isConstant.find(input.name());
2661  if(it == isConstant.end())
2662  {
2663  inputNames.push_back(input.name());
2664  }
2665  }
2666  return inputNames;
2667 }

References CHECK_LOCATION.

◆ GetNetworkInputBindingInfo()

BindingPointInfo GetNetworkInputBindingInfo ( const std::string &  name) const

Retrieve binding info (layer id and tensor info) for the network input identified by the given layer name.

Definition at line 2607 of file OnnxParser.cpp.

2608 {
2609  for(int i = 0; i < m_Graph->input_size(); ++i)
2610  {
2611  auto input = m_Graph->input(i);
2612  if(input.name() == name)
2613  {
2614  auto it = m_InputInfos.find(name);
2615 
2616  if (it != m_InputInfos.end())
2617  {
2618  return std::make_pair(static_cast<armnn::LayerBindingId>(i), it->second);
2619  }
2620  }
2621  }
2622  throw InvalidArgumentException(fmt::format("The input layer '{}' does not exist {}",
2623  name, CHECK_LOCATION().AsString()));
2624 }

References CHECK_LOCATION.

◆ GetNetworkOutputBindingInfo()

BindingPointInfo GetNetworkOutputBindingInfo ( const std::string &  name) const

Retrieve binding info (layer id and tensor info) for the network output identified by the given layer name.

Definition at line 2626 of file OnnxParser.cpp.

2627 {
2628  for(int i = 0; i < m_Graph->output_size(); ++i)
2629  {
2630  auto output = m_Graph->output(i);
2631  if(output.name() == name)
2632  {
2633  auto it = m_OutputInfos.find(name);
2634 
2635  if (it != m_OutputInfos.end())
2636  {
2637  return std::make_pair(static_cast<armnn::LayerBindingId>(i), it->second);
2638  }
2639  }
2640  }
2641  throw InvalidArgumentException(fmt::format("The output layer '{}' does not exist {}",
2642  name, CHECK_LOCATION().AsString()));
2643 }

References CHECK_LOCATION.

◆ GetOutputs()

std::vector< std::string > GetOutputs ( ModelPtr model)
static

Retrieve outputs names.

Definition at line 2669 of file OnnxParser.cpp.

2670 {
2671  if(model == nullptr) {
2672  throw InvalidArgumentException(fmt::format("The given model cannot be null {}",
2673  CHECK_LOCATION().AsString()));
2674  }
2675 
2676  std::vector<std::string> outputNames;
2677  for(auto output : model->graph().output())
2678  {
2679  outputNames.push_back(output.name());
2680  }
2681  return outputNames;
2682 }

References CHECK_LOCATION.

◆ GetVersion()

const std::string GetVersion ( )
static

Retrieve version in X.Y.Z form.

Definition at line 2684 of file OnnxParser.cpp.

2685 {
2686  return ONNX_PARSER_VERSION;
2687 }

References ONNX_PARSER_VERSION.

◆ LoadModelFromBinary()

ModelPtr LoadModelFromBinary ( const std::vector< uint8_t > &  binaryContent)
static

Definition at line 773 of file OnnxParser.cpp.

774 {
775  if (binaryContent.size() == 0)
776  {
777  throw ParseException(fmt::format("Missing binary content", CHECK_LOCATION().AsString()));
778  }
779  // Parse the file into a message
780  ModelPtr modelProto = std::make_unique<onnx::ModelProto>();
781 
782  google::protobuf::io::CodedInputStream codedStream(binaryContent.data(), static_cast<int>(binaryContent.size()));
783  codedStream.SetTotalBytesLimit(INT_MAX);
784  bool success = modelProto.get()->ParseFromCodedStream(&codedStream);
785 
786  if (!success)
787  {
788  std::stringstream error;
789  error << "Failed to parse graph";
790  throw ParseException(fmt::format("{} {}", error.str(), CHECK_LOCATION().AsString()));
791  }
792  return modelProto;
793 }

References CHECK_LOCATION, and armnn::error.

Referenced by OnnxParserImpl::CreateNetworkFromBinary().

◆ LoadModelFromBinaryFile()

ModelPtr LoadModelFromBinaryFile ( const char *  fileName)
static

Definition at line 795 of file OnnxParser.cpp.

796 {
797  FILE* fd = fopen(graphFile, "rb");
798 
799  if (fd == nullptr)
800  {
801  throw FileNotFoundException(fmt::format("Invalid (null) filename {}", CHECK_LOCATION().AsString()));
802  }
803 
804  // Parse the file into a message
805  ModelPtr modelProto = std::make_unique<onnx::ModelProto>();
806 
807  google::protobuf::io::FileInputStream inStream(fileno(fd));
808  google::protobuf::io::CodedInputStream codedStream(&inStream);
809  codedStream.SetTotalBytesLimit(INT_MAX);
810  bool success = modelProto.get()->ParseFromCodedStream(&codedStream);
811  fclose(fd);
812 
813  if (!success)
814  {
815  std::stringstream error;
816  error << "Failed to parse graph file";
817  throw ParseException(fmt::format("{} {}", error.str(), CHECK_LOCATION().AsString()));
818  }
819  return modelProto;
820 
821 }

References CHECK_LOCATION, and armnn::error.

Referenced by OnnxParserImpl::CreateNetworkFromBinaryFile().

◆ LoadModelFromString()

ModelPtr LoadModelFromString ( const std::string &  inputString)
static

Definition at line 839 of file OnnxParser.cpp.

840 {
841  if (protoText == "")
842  {
843  throw InvalidArgumentException(fmt::format("Invalid (empty) string for model parameter {}",
844  CHECK_LOCATION().AsString()));
845  }
846  // Parse the string into a message
847  ModelPtr modelProto = std::make_unique<onnx::ModelProto>();
848  bool success = google::protobuf::TextFormat::ParseFromString(protoText, modelProto.get());
849  if (!success)
850  {
851  std::stringstream error;
852  error << "Failed to parse graph file";
853  throw ParseException(fmt::format("{} {}", error.str(), CHECK_LOCATION().AsString()));
854  }
855  return modelProto;
856 }

References CHECK_LOCATION, and armnn::error.

Referenced by OnnxParserImpl::CreateNetworkFromString().

◆ LoadModelFromTextFile()

ModelPtr LoadModelFromTextFile ( const char *  fileName)
static

Definition at line 716 of file OnnxParser.cpp.

717 {
718  FILE* fd = fopen(graphFile, "r");
719 
720  if (fd == nullptr)
721  {
722  throw FileNotFoundException(fmt::format("Invalid (null) filename {}", CHECK_LOCATION().AsString()));
723  }
724 
725  // Parse the file into a message
726  ModelPtr modelProto = std::make_unique<onnx::ModelProto>();
727  using google::protobuf::io::FileInputStream;
728  std::unique_ptr<FileInputStream> input = std::make_unique<FileInputStream>(fileno(fd));
729  bool success = google::protobuf::TextFormat::Parse(input.get(), modelProto.get());
730  fclose(fd);
731 
732  if (!success)
733  {
734  std::stringstream error;
735  error << "Failed to parse graph file";
736  throw ParseException(fmt::format("{} {}", error.str(), CHECK_LOCATION().AsString()));
737  }
738  return modelProto;
739 }

References CHECK_LOCATION, and armnn::error.

Referenced by OnnxParserImpl::CreateNetworkFromTextFile().

◆ ValidateInputs()

void ValidateInputs ( const onnx::NodeProto &  node,
TypePair  validInputs,
const Location &  location 
)

Definition at line 478 of file OnnxParser.cpp.

481 {
482  for(auto input : node.input())
483  {
484  CheckValidDataType(validInputs.second,
485  m_TensorsInfo[input].m_dtype,
486  validInputs.first,
487  node.name(),
488  input,
489  location);
490  }
491 }

The documentation for this class was generated from the following files:
armnnOnnxParser::OnnxParserImpl::LoadModelFromBinaryFile
static ModelPtr LoadModelFromBinaryFile(const char *fileName)
Definition: OnnxParser.cpp:795
CHECK_LOCATION
#define CHECK_LOCATION()
Definition: Exceptions.hpp:203
ONNX_PARSER_VERSION
#define ONNX_PARSER_VERSION
ONNX_PARSER_VERSION: "X.Y.Z" where: X = Major version number Y = Minor version number Z = Patch versi...
Definition: Version.hpp:25
armnnOnnxParser::ModelPtr
std::unique_ptr< onnx::ModelProto > ModelPtr
Definition: OnnxParser.hpp:23
armnn::BoostLogSeverityMapping::error
@ error
armnn::InvalidArgumentException
Definition: Exceptions.hpp:80
armnn::LayerBindingId
int LayerBindingId
Type of identifiers for bindable layers (inputs, outputs).
Definition: Types.hpp:303
armnnOnnxParser::OnnxParserImpl::LoadModelFromBinary
static ModelPtr LoadModelFromBinary(const std::vector< uint8_t > &binaryContent)
Definition: OnnxParser.cpp:773
armnnOnnxParser::OnnxParserImpl::LoadModelFromString
static ModelPtr LoadModelFromString(const std::string &inputString)
Definition: OnnxParser.cpp:839
armnn::ParseException
Definition: Exceptions.hpp:92
armnn::FileNotFoundException
Definition: Exceptions.hpp:86
armnnOnnxParser::OnnxParserImpl::LoadModelFromTextFile
static ModelPtr LoadModelFromTextFile(const char *fileName)
Definition: OnnxParser.cpp:716