ArmNN
 23.05
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 554 of file OnnxParser.cpp.

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

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

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

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

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

References OnnxParserImpl::LoadModelFromBinary().

◆ CreateNetworkFromBinaryFile() [1/2]

INetworkPtr CreateNetworkFromBinaryFile ( const char *  graphFile)

Create the network from a protobuf binary file on disk.

Definition at line 812 of file OnnxParser.cpp.

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

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

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

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

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

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

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

References OnnxParserImpl::LoadModelFromString().

◆ CreateNetworkFromTextFile() [1/2]

INetworkPtr CreateNetworkFromTextFile ( const char *  graphFile)

Create the network from a protobuf text file on disk.

Definition at line 730 of file OnnxParser.cpp.

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

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

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

References OnnxParserImpl::LoadModelFromTextFile().

◆ GetInputs()

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

Retrieve inputs names.

Definition at line 2546 of file OnnxParser.cpp.

2547 {
2548  if(model == nullptr) {
2549  throw InvalidArgumentException(fmt::format("The given model cannot be null {}",
2550  CHECK_LOCATION().AsString()));
2551  }
2552 
2553  std::vector<std::string> inputNames;
2554  std::map<std::string, bool> isConstant;
2555  for(auto tensor : model->graph().initializer())
2556  {
2557  isConstant[tensor.name()] = true;
2558  }
2559  for(auto input : model->graph().input())
2560  {
2561  auto it = isConstant.find(input.name());
2562  if(it == isConstant.end())
2563  {
2564  inputNames.push_back(input.name());
2565  }
2566  }
2567  return inputNames;
2568 }

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

2509 {
2510  for(int i = 0; i < m_Graph->input_size(); ++i)
2511  {
2512  auto input = m_Graph->input(i);
2513  if(input.name() == name)
2514  {
2515  auto it = m_InputInfos.find(name);
2516 
2517  if (it != m_InputInfos.end())
2518  {
2519  return std::make_pair(static_cast<armnn::LayerBindingId>(i), it->second);
2520  }
2521  }
2522  }
2523  throw InvalidArgumentException(fmt::format("The input layer '{}' does not exist {}",
2524  name, CHECK_LOCATION().AsString()));
2525 }

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

2528 {
2529  for(int i = 0; i < m_Graph->output_size(); ++i)
2530  {
2531  auto output = m_Graph->output(i);
2532  if(output.name() == name)
2533  {
2534  auto it = m_OutputInfos.find(name);
2535 
2536  if (it != m_OutputInfos.end())
2537  {
2538  return std::make_pair(static_cast<armnn::LayerBindingId>(i), it->second);
2539  }
2540  }
2541  }
2542  throw InvalidArgumentException(fmt::format("The output layer '{}' does not exist {}",
2543  name, CHECK_LOCATION().AsString()));
2544 }

References CHECK_LOCATION.

◆ GetOutputs()

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

Retrieve outputs names.

Definition at line 2570 of file OnnxParser.cpp.

2571 {
2572  if(model == nullptr) {
2573  throw InvalidArgumentException(fmt::format("The given model cannot be null {}",
2574  CHECK_LOCATION().AsString()));
2575  }
2576 
2577  std::vector<std::string> outputNames;
2578  for(auto output : model->graph().output())
2579  {
2580  outputNames.push_back(output.name());
2581  }
2582  return outputNames;
2583 }

References CHECK_LOCATION.

◆ GetVersion()

const std::string GetVersion ( )
static

Retrieve version in X.Y.Z form.

Definition at line 2585 of file OnnxParser.cpp.

2586 {
2587  return ONNX_PARSER_VERSION;
2588 }

References ONNX_PARSER_VERSION.

◆ LoadModelFromBinary()

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

Definition at line 762 of file OnnxParser.cpp.

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

References CHECK_LOCATION, and armnn::error.

Referenced by OnnxParserImpl::CreateNetworkFromBinary().

◆ LoadModelFromBinaryFile()

ModelPtr LoadModelFromBinaryFile ( const char *  fileName)
static

Definition at line 784 of file OnnxParser.cpp.

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

References CHECK_LOCATION, and armnn::error.

Referenced by OnnxParserImpl::CreateNetworkFromBinaryFile().

◆ LoadModelFromString()

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

Definition at line 828 of file OnnxParser.cpp.

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

References CHECK_LOCATION, and armnn::error.

Referenced by OnnxParserImpl::CreateNetworkFromString().

◆ LoadModelFromTextFile()

ModelPtr LoadModelFromTextFile ( const char *  fileName)
static

Definition at line 705 of file OnnxParser.cpp.

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

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:784
armnn::LayerBindingId
int LayerBindingId
Type of identifiers for bindable layers (inputs, outputs).
Definition: Types.hpp:301
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:828
armnnOnnxParser::OnnxParserImpl::LoadModelFromTextFile
static ModelPtr LoadModelFromTextFile(const char *fileName)
Definition: OnnxParser.cpp:705
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:762