ArmNN
 24.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 580 of file OnnxParser.cpp.

581  : m_Network(nullptr, nullptr)
582 {
583 }

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

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

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

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

References OnnxParserImpl::LoadModelFromBinary().

◆ CreateNetworkFromBinaryFile() [1/2]

INetworkPtr CreateNetworkFromBinaryFile ( const char *  graphFile)

Create the network from a protobuf binary file on disk.

Definition at line 841 of file OnnxParser.cpp.

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

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

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

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

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

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

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

References OnnxParserImpl::LoadModelFromString().

◆ CreateNetworkFromTextFile() [1/2]

INetworkPtr CreateNetworkFromTextFile ( const char *  graphFile)

Create the network from a protobuf text file on disk.

Definition at line 759 of file OnnxParser.cpp.

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

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

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

References OnnxParserImpl::LoadModelFromTextFile().

◆ GetInputs()

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

Retrieve inputs names.

Definition at line 2663 of file OnnxParser.cpp.

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

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

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

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

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

References CHECK_LOCATION.

◆ GetOutputs()

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

Retrieve outputs names.

Definition at line 2687 of file OnnxParser.cpp.

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

References CHECK_LOCATION.

◆ GetVersion()

const std::string GetVersion ( )
static

Retrieve version in X.Y.Z form.

Definition at line 2702 of file OnnxParser.cpp.

2703 {
2704  return ONNX_PARSER_VERSION;
2705 }

References ONNX_PARSER_VERSION.

◆ LoadModelFromBinary()

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

Definition at line 791 of file OnnxParser.cpp.

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

References CHECK_LOCATION, and armnn::error.

Referenced by OnnxParserImpl::CreateNetworkFromBinary().

◆ LoadModelFromBinaryFile()

ModelPtr LoadModelFromBinaryFile ( const char *  fileName)
static

Definition at line 813 of file OnnxParser.cpp.

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

References CHECK_LOCATION, and armnn::error.

Referenced by OnnxParserImpl::CreateNetworkFromBinaryFile().

◆ LoadModelFromString()

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

Definition at line 857 of file OnnxParser.cpp.

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

References CHECK_LOCATION, and armnn::error.

Referenced by OnnxParserImpl::CreateNetworkFromString().

◆ LoadModelFromTextFile()

ModelPtr LoadModelFromTextFile ( const char *  fileName)
static

Definition at line 734 of file OnnxParser.cpp.

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

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

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

The documentation for this class was generated from the following files:
armnnOnnxParser::OnnxParserImpl::LoadModelFromBinaryFile
static ModelPtr LoadModelFromBinaryFile(const char *fileName)
Definition: OnnxParser.cpp:813
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:791
armnnOnnxParser::OnnxParserImpl::LoadModelFromString
static ModelPtr LoadModelFromString(const std::string &inputString)
Definition: OnnxParser.cpp:857
armnn::ParseException
Definition: Exceptions.hpp:92
armnn::FileNotFoundException
Definition: Exceptions.hpp:86
armnnOnnxParser::OnnxParserImpl::LoadModelFromTextFile
static ModelPtr LoadModelFromTextFile(const char *fileName)
Definition: OnnxParser.cpp:734