ArmNN
 23.02
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 553 of file OnnxParser.cpp.

554  : m_Network(nullptr, nullptr)
555 {
556 }

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

746 {
747  ResetParser();
748  ModelPtr modelProto = LoadModelFromBinary(binaryContent);
749  return CreateNetworkFromModel(*modelProto);
750 }

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

754 {
755  ResetParser();
756  m_InputShapes = inputShapes;
757  ModelPtr modelProto = LoadModelFromBinary(binaryContent);
758  return CreateNetworkFromModel(*modelProto);
759 }

References OnnxParserImpl::LoadModelFromBinary().

◆ CreateNetworkFromBinaryFile() [1/2]

INetworkPtr CreateNetworkFromBinaryFile ( const char *  graphFile)

Create the network from a protobuf binary file on disk.

Definition at line 811 of file OnnxParser.cpp.

812 {
813  ResetParser();
814  ModelPtr modelProto = LoadModelFromBinaryFile(graphFile);
815  return CreateNetworkFromModel(*modelProto);
816 }

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

820 {
821  ResetParser();
822  m_InputShapes = inputShapes;
823  ModelPtr modelProto = LoadModelFromBinaryFile(graphFile);
824  return CreateNetworkFromModel(*modelProto);
825 }

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

847 {
848  ResetParser();
849  ModelPtr modelProto = LoadModelFromString(protoText);
850  return CreateNetworkFromModel(*modelProto);
851 }

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

855 {
856  ResetParser();
857  m_InputShapes = inputShapes;
858  ModelPtr modelProto = LoadModelFromString(protoText);
859  return CreateNetworkFromModel(*modelProto);
860 }

References OnnxParserImpl::LoadModelFromString().

◆ CreateNetworkFromTextFile() [1/2]

INetworkPtr CreateNetworkFromTextFile ( const char *  graphFile)

Create the network from a protobuf text file on disk.

Definition at line 729 of file OnnxParser.cpp.

730 {
731  ResetParser();
732  ModelPtr modelProto = LoadModelFromTextFile(graphFile);
733  return CreateNetworkFromModel(*modelProto);
734 }

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

738 {
739  ResetParser();
740  m_InputShapes = inputShapes;
741  ModelPtr modelProto = LoadModelFromTextFile(graphFile);
742  return CreateNetworkFromModel(*modelProto);
743 }

References OnnxParserImpl::LoadModelFromTextFile().

◆ GetInputs()

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

Retrieve inputs names.

Definition at line 2490 of file OnnxParser.cpp.

2491 {
2492  if(model == nullptr) {
2493  throw InvalidArgumentException(fmt::format("The given model cannot be null {}",
2494  CHECK_LOCATION().AsString()));
2495  }
2496 
2497  std::vector<std::string> inputNames;
2498  std::map<std::string, bool> isConstant;
2499  for(auto tensor : model->graph().initializer())
2500  {
2501  isConstant[tensor.name()] = true;
2502  }
2503  for(auto input : model->graph().input())
2504  {
2505  auto it = isConstant.find(input.name());
2506  if(it == isConstant.end())
2507  {
2508  inputNames.push_back(input.name());
2509  }
2510  }
2511  return inputNames;
2512 }

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

2453 {
2454  for(int i = 0; i < m_Graph->input_size(); ++i)
2455  {
2456  auto input = m_Graph->input(i);
2457  if(input.name() == name)
2458  {
2459  auto it = m_InputInfos.find(name);
2460 
2461  if (it != m_InputInfos.end())
2462  {
2463  return std::make_pair(static_cast<armnn::LayerBindingId>(i), it->second);
2464  }
2465  }
2466  }
2467  throw InvalidArgumentException(fmt::format("The input layer '{}' does not exist {}",
2468  name, CHECK_LOCATION().AsString()));
2469 }

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

2472 {
2473  for(int i = 0; i < m_Graph->output_size(); ++i)
2474  {
2475  auto output = m_Graph->output(i);
2476  if(output.name() == name)
2477  {
2478  auto it = m_OutputInfos.find(name);
2479 
2480  if (it != m_OutputInfos.end())
2481  {
2482  return std::make_pair(static_cast<armnn::LayerBindingId>(i), it->second);
2483  }
2484  }
2485  }
2486  throw InvalidArgumentException(fmt::format("The output layer '{}' does not exist {}",
2487  name, CHECK_LOCATION().AsString()));
2488 }

References CHECK_LOCATION.

◆ GetOutputs()

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

Retrieve outputs names.

Definition at line 2514 of file OnnxParser.cpp.

2515 {
2516  if(model == nullptr) {
2517  throw InvalidArgumentException(fmt::format("The given model cannot be null {}",
2518  CHECK_LOCATION().AsString()));
2519  }
2520 
2521  std::vector<std::string> outputNames;
2522  for(auto output : model->graph().output())
2523  {
2524  outputNames.push_back(output.name());
2525  }
2526  return outputNames;
2527 }

References CHECK_LOCATION.

◆ GetVersion()

const std::string GetVersion ( )
static

Retrieve version in X.Y.Z form.

Definition at line 2529 of file OnnxParser.cpp.

2530 {
2531  return ONNX_PARSER_VERSION;
2532 }

References ONNX_PARSER_VERSION.

◆ LoadModelFromBinary()

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

Definition at line 761 of file OnnxParser.cpp.

762 {
763  if (binaryContent.size() == 0)
764  {
765  throw ParseException(fmt::format("Missing binary content", CHECK_LOCATION().AsString()));
766  }
767  // Parse the file into a message
768  ModelPtr modelProto = std::make_unique<onnx::ModelProto>();
769 
770  google::protobuf::io::CodedInputStream codedStream(binaryContent.data(), static_cast<int>(binaryContent.size()));
771  codedStream.SetTotalBytesLimit(INT_MAX);
772  bool success = modelProto.get()->ParseFromCodedStream(&codedStream);
773 
774  if (!success)
775  {
776  std::stringstream error;
777  error << "Failed to parse graph";
778  throw ParseException(fmt::format("{} {}", error.str(), CHECK_LOCATION().AsString()));
779  }
780  return modelProto;
781 }

References CHECK_LOCATION, and armnn::error.

Referenced by OnnxParserImpl::CreateNetworkFromBinary().

◆ LoadModelFromBinaryFile()

ModelPtr LoadModelFromBinaryFile ( const char *  fileName)
static

Definition at line 783 of file OnnxParser.cpp.

784 {
785  FILE* fd = fopen(graphFile, "rb");
786 
787  if (fd == nullptr)
788  {
789  throw FileNotFoundException(fmt::format("Invalid (null) filename {}", CHECK_LOCATION().AsString()));
790  }
791 
792  // Parse the file into a message
793  ModelPtr modelProto = std::make_unique<onnx::ModelProto>();
794 
795  google::protobuf::io::FileInputStream inStream(fileno(fd));
796  google::protobuf::io::CodedInputStream codedStream(&inStream);
797  codedStream.SetTotalBytesLimit(INT_MAX);
798  bool success = modelProto.get()->ParseFromCodedStream(&codedStream);
799  fclose(fd);
800 
801  if (!success)
802  {
803  std::stringstream error;
804  error << "Failed to parse graph file";
805  throw ParseException(fmt::format("{} {}", error.str(), CHECK_LOCATION().AsString()));
806  }
807  return modelProto;
808 
809 }

References CHECK_LOCATION, and armnn::error.

Referenced by OnnxParserImpl::CreateNetworkFromBinaryFile().

◆ LoadModelFromString()

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

Definition at line 827 of file OnnxParser.cpp.

828 {
829  if (protoText == "")
830  {
831  throw InvalidArgumentException(fmt::format("Invalid (empty) string for model parameter {}",
832  CHECK_LOCATION().AsString()));
833  }
834  // Parse the string into a message
835  ModelPtr modelProto = std::make_unique<onnx::ModelProto>();
836  bool success = google::protobuf::TextFormat::ParseFromString(protoText, modelProto.get());
837  if (!success)
838  {
839  std::stringstream error;
840  error << "Failed to parse graph file";
841  throw ParseException(fmt::format("{} {}", error.str(), CHECK_LOCATION().AsString()));
842  }
843  return modelProto;
844 }

References CHECK_LOCATION, and armnn::error.

Referenced by OnnxParserImpl::CreateNetworkFromString().

◆ LoadModelFromTextFile()

ModelPtr LoadModelFromTextFile ( const char *  fileName)
static

Definition at line 704 of file OnnxParser.cpp.

705 {
706  FILE* fd = fopen(graphFile, "r");
707 
708  if (fd == nullptr)
709  {
710  throw FileNotFoundException(fmt::format("Invalid (null) filename {}", CHECK_LOCATION().AsString()));
711  }
712 
713  // Parse the file into a message
714  ModelPtr modelProto = std::make_unique<onnx::ModelProto>();
715  using google::protobuf::io::FileInputStream;
716  std::unique_ptr<FileInputStream> input = std::make_unique<FileInputStream>(fileno(fd));
717  bool success = google::protobuf::TextFormat::Parse(input.get(), modelProto.get());
718  fclose(fd);
719 
720  if (!success)
721  {
722  std::stringstream error;
723  error << "Failed to parse graph file";
724  throw ParseException(fmt::format("{} {}", error.str(), CHECK_LOCATION().AsString()));
725  }
726  return modelProto;
727 }

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:
CHECK_LOCATION
#define CHECK_LOCATION()
Definition: Exceptions.hpp:203
armnnOnnxParser::OnnxParserImpl::LoadModelFromBinaryFile
static ModelPtr LoadModelFromBinaryFile(const char *fileName)
Definition: OnnxParser.cpp:783
armnn::LayerBindingId
int LayerBindingId
Type of identifiers for bindable layers (inputs, outputs).
Definition: Types.hpp:290
armnnOnnxParser::ModelPtr
std::unique_ptr< onnx::ModelProto > ModelPtr
Definition: OnnxParser.hpp:23
armnn::ParseException
Definition: Exceptions.hpp:92
armnnOnnxParser::OnnxParserImpl::LoadModelFromString
static ModelPtr LoadModelFromString(const std::string &inputString)
Definition: OnnxParser.cpp:827
armnnOnnxParser::OnnxParserImpl::LoadModelFromTextFile
static ModelPtr LoadModelFromTextFile(const char *fileName)
Definition: OnnxParser.cpp:704
armnn::FileNotFoundException
Definition: Exceptions.hpp:86
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
armnn::InvalidArgumentException
Definition: Exceptions.hpp:80
armnn::BoostLogSeverityMapping::error
@ error
armnnOnnxParser::OnnxParserImpl::LoadModelFromBinary
static ModelPtr LoadModelFromBinary(const std::vector< uint8_t > &binaryContent)
Definition: OnnxParser.cpp:761