ArmNN
 23.11
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 578 of file OnnxParser.cpp.

579  : m_Network(nullptr, nullptr)
580 {
581 }

◆ ~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 773 of file OnnxParser.cpp.

774 {
775  ResetParser();
776  ModelPtr modelProto = LoadModelFromBinary(binaryContent);
777  return CreateNetworkFromModel(*modelProto);
778 }

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 780 of file OnnxParser.cpp.

782 {
783  ResetParser();
784  m_InputShapes = inputShapes;
785  ModelPtr modelProto = LoadModelFromBinary(binaryContent);
786  return CreateNetworkFromModel(*modelProto);
787 }

References OnnxParserImpl::LoadModelFromBinary().

◆ CreateNetworkFromBinaryFile() [1/2]

INetworkPtr CreateNetworkFromBinaryFile ( const char *  graphFile)

Create the network from a protobuf binary file on disk.

Definition at line 839 of file OnnxParser.cpp.

840 {
841  ResetParser();
842  ModelPtr modelProto = LoadModelFromBinaryFile(graphFile);
843  return CreateNetworkFromModel(*modelProto);
844 }

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 846 of file OnnxParser.cpp.

848 {
849  ResetParser();
850  m_InputShapes = inputShapes;
851  ModelPtr modelProto = LoadModelFromBinaryFile(graphFile);
852  return CreateNetworkFromModel(*modelProto);
853 }

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 874 of file OnnxParser.cpp.

875 {
876  ResetParser();
877  ModelPtr modelProto = LoadModelFromString(protoText);
878  return CreateNetworkFromModel(*modelProto);
879 }

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 881 of file OnnxParser.cpp.

883 {
884  ResetParser();
885  m_InputShapes = inputShapes;
886  ModelPtr modelProto = LoadModelFromString(protoText);
887  return CreateNetworkFromModel(*modelProto);
888 }

References OnnxParserImpl::LoadModelFromString().

◆ CreateNetworkFromTextFile() [1/2]

INetworkPtr CreateNetworkFromTextFile ( const char *  graphFile)

Create the network from a protobuf text file on disk.

Definition at line 757 of file OnnxParser.cpp.

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

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 764 of file OnnxParser.cpp.

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

References OnnxParserImpl::LoadModelFromTextFile().

◆ GetInputs()

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

Retrieve inputs names.

Definition at line 2661 of file OnnxParser.cpp.

2662 {
2663  if(model == nullptr) {
2664  throw InvalidArgumentException(fmt::format("The given model cannot be null {}",
2665  CHECK_LOCATION().AsString()));
2666  }
2667 
2668  std::vector<std::string> inputNames;
2669  std::map<std::string, bool> isConstant;
2670  for(auto tensor : model->graph().initializer())
2671  {
2672  isConstant[tensor.name()] = true;
2673  }
2674  for(auto input : model->graph().input())
2675  {
2676  auto it = isConstant.find(input.name());
2677  if(it == isConstant.end())
2678  {
2679  inputNames.push_back(input.name());
2680  }
2681  }
2682  return inputNames;
2683 }

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 2623 of file OnnxParser.cpp.

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

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 2642 of file OnnxParser.cpp.

2643 {
2644  for(int i = 0; i < m_Graph->output_size(); ++i)
2645  {
2646  auto output = m_Graph->output(i);
2647  if(output.name() == name)
2648  {
2649  auto it = m_OutputInfos.find(name);
2650 
2651  if (it != m_OutputInfos.end())
2652  {
2653  return std::make_pair(static_cast<armnn::LayerBindingId>(i), it->second);
2654  }
2655  }
2656  }
2657  throw InvalidArgumentException(fmt::format("The output layer '{}' does not exist {}",
2658  name, CHECK_LOCATION().AsString()));
2659 }

References CHECK_LOCATION.

◆ GetOutputs()

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

Retrieve outputs names.

Definition at line 2685 of file OnnxParser.cpp.

2686 {
2687  if(model == nullptr) {
2688  throw InvalidArgumentException(fmt::format("The given model cannot be null {}",
2689  CHECK_LOCATION().AsString()));
2690  }
2691 
2692  std::vector<std::string> outputNames;
2693  for(auto output : model->graph().output())
2694  {
2695  outputNames.push_back(output.name());
2696  }
2697  return outputNames;
2698 }

References CHECK_LOCATION.

◆ GetVersion()

const std::string GetVersion ( )
static

Retrieve version in X.Y.Z form.

Definition at line 2700 of file OnnxParser.cpp.

2701 {
2702  return ONNX_PARSER_VERSION;
2703 }

References ONNX_PARSER_VERSION.

◆ LoadModelFromBinary()

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

Definition at line 789 of file OnnxParser.cpp.

790 {
791  if (binaryContent.size() == 0)
792  {
793  throw ParseException(fmt::format("Missing binary content", CHECK_LOCATION().AsString()));
794  }
795  // Parse the file into a message
796  ModelPtr modelProto = std::make_unique<onnx::ModelProto>();
797 
798  google::protobuf::io::CodedInputStream codedStream(binaryContent.data(), static_cast<int>(binaryContent.size()));
799  codedStream.SetTotalBytesLimit(INT_MAX);
800  bool success = modelProto.get()->ParseFromCodedStream(&codedStream);
801 
802  if (!success)
803  {
804  std::stringstream error;
805  error << "Failed to parse graph";
806  throw ParseException(fmt::format("{} {}", error.str(), CHECK_LOCATION().AsString()));
807  }
808  return modelProto;
809 }

References CHECK_LOCATION, and armnn::error.

Referenced by OnnxParserImpl::CreateNetworkFromBinary().

◆ LoadModelFromBinaryFile()

ModelPtr LoadModelFromBinaryFile ( const char *  fileName)
static

Definition at line 811 of file OnnxParser.cpp.

812 {
813  FILE* fd = fopen(graphFile, "rb");
814 
815  if (fd == nullptr)
816  {
817  throw FileNotFoundException(fmt::format("Invalid (null) filename {}", CHECK_LOCATION().AsString()));
818  }
819 
820  // Parse the file into a message
821  ModelPtr modelProto = std::make_unique<onnx::ModelProto>();
822 
823  google::protobuf::io::FileInputStream inStream(fileno(fd));
824  google::protobuf::io::CodedInputStream codedStream(&inStream);
825  codedStream.SetTotalBytesLimit(INT_MAX);
826  bool success = modelProto.get()->ParseFromCodedStream(&codedStream);
827  fclose(fd);
828 
829  if (!success)
830  {
831  std::stringstream error;
832  error << "Failed to parse graph file";
833  throw ParseException(fmt::format("{} {}", error.str(), CHECK_LOCATION().AsString()));
834  }
835  return modelProto;
836 
837 }

References CHECK_LOCATION, and armnn::error.

Referenced by OnnxParserImpl::CreateNetworkFromBinaryFile().

◆ LoadModelFromString()

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

Definition at line 855 of file OnnxParser.cpp.

856 {
857  if (protoText == "")
858  {
859  throw InvalidArgumentException(fmt::format("Invalid (empty) string for model parameter {}",
860  CHECK_LOCATION().AsString()));
861  }
862  // Parse the string into a message
863  ModelPtr modelProto = std::make_unique<onnx::ModelProto>();
864  bool success = google::protobuf::TextFormat::ParseFromString(protoText, modelProto.get());
865  if (!success)
866  {
867  std::stringstream error;
868  error << "Failed to parse graph file";
869  throw ParseException(fmt::format("{} {}", error.str(), CHECK_LOCATION().AsString()));
870  }
871  return modelProto;
872 }

References CHECK_LOCATION, and armnn::error.

Referenced by OnnxParserImpl::CreateNetworkFromString().

◆ LoadModelFromTextFile()

ModelPtr LoadModelFromTextFile ( const char *  fileName)
static

Definition at line 732 of file OnnxParser.cpp.

733 {
734  FILE* fd = fopen(graphFile, "r");
735 
736  if (fd == nullptr)
737  {
738  throw FileNotFoundException(fmt::format("Invalid (null) filename {}", CHECK_LOCATION().AsString()));
739  }
740 
741  // Parse the file into a message
742  ModelPtr modelProto = std::make_unique<onnx::ModelProto>();
743  using google::protobuf::io::FileInputStream;
744  std::unique_ptr<FileInputStream> input = std::make_unique<FileInputStream>(fileno(fd));
745  bool success = google::protobuf::TextFormat::Parse(input.get(), modelProto.get());
746  fclose(fd);
747 
748  if (!success)
749  {
750  std::stringstream error;
751  error << "Failed to parse graph file";
752  throw ParseException(fmt::format("{} {}", error.str(), CHECK_LOCATION().AsString()));
753  }
754  return modelProto;
755 }

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 494 of file OnnxParser.cpp.

497 {
498  for(auto input : node.input())
499  {
500  CheckValidDataType(validInputs.second,
501  m_TensorsInfo[input].m_dtype,
502  validInputs.first,
503  node.name(),
504  input,
505  location);
506  }
507 }

The documentation for this class was generated from the following files:
armnnOnnxParser::OnnxParserImpl::LoadModelFromBinaryFile
static ModelPtr LoadModelFromBinaryFile(const char *fileName)
Definition: OnnxParser.cpp:811
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:309
armnnOnnxParser::OnnxParserImpl::LoadModelFromBinary
static ModelPtr LoadModelFromBinary(const std::vector< uint8_t > &binaryContent)
Definition: OnnxParser.cpp:789
armnnOnnxParser::OnnxParserImpl::LoadModelFromString
static ModelPtr LoadModelFromString(const std::string &inputString)
Definition: OnnxParser.cpp:855
armnn::ParseException
Definition: Exceptions.hpp:92
armnn::FileNotFoundException
Definition: Exceptions.hpp:86
armnnOnnxParser::OnnxParserImpl::LoadModelFromTextFile
static ModelPtr LoadModelFromTextFile(const char *fileName)
Definition: OnnxParser.cpp:732