TensorRT  7.2.0.9
NvOnnxParser.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef NV_ONNX_PARSER_H
24 #define NV_ONNX_PARSER_H
25 
26 #include "NvInfer.h"
27 #include <stddef.h>
28 #include <vector>
29 
35 
36 #define NV_ONNX_PARSER_MAJOR 0
37 #define NV_ONNX_PARSER_MINOR 1
38 #define NV_ONNX_PARSER_PATCH 0
39 
40 static const int NV_ONNX_PARSER_VERSION = ((NV_ONNX_PARSER_MAJOR * 10000) + (NV_ONNX_PARSER_MINOR * 100) + NV_ONNX_PARSER_PATCH);
41 
47 typedef std::pair<std::vector<size_t>, bool> SubGraph_t;
48 
54 typedef std::vector<SubGraph_t> SubGraphCollection_t;
55 
56 class onnxTensorDescriptorV1;
62 namespace nvonnxparser
63 {
64 
65 template <typename T>
66 inline int32_t EnumMax();
67 
72 enum class ErrorCode : int
73 {
74  kSUCCESS = 0,
75  kINTERNAL_ERROR = 1,
76  kMEM_ALLOC_FAILED = 2,
77  kMODEL_DESERIALIZE_FAILED = 3,
78  kINVALID_VALUE = 4,
79  kINVALID_GRAPH = 5,
80  kINVALID_NODE = 6,
81  kUNSUPPORTED_GRAPH = 7,
82  kUNSUPPORTED_NODE = 8
83 };
84 
85 template <>
86 inline int32_t EnumMax<ErrorCode>()
87 {
88  return 9;
89 }
90 
96 {
97 public:
100  virtual ErrorCode code() const = 0;
103  virtual const char* desc() const = 0;
106  virtual const char* file() const = 0;
109  virtual int line() const = 0;
112  virtual const char* func() const = 0;
115  virtual int node() const = 0;
116 
117 protected:
118  virtual ~IParserError() {}
119 };
120 
125 class IParser
126 {
127 public:
140  virtual bool parse(void const* serialized_onnx_model,
141  size_t serialized_onnx_model_size)
142  = 0;
143 
153  virtual bool parseFromFile(const char* onnxModelFile, int verbosity) = 0;
154 
163  virtual bool supportsModel(void const* serialized_onnx_model,
164  size_t serialized_onnx_model_size,
165  SubGraphCollection_t& sub_graph_collection)
166  = 0;
167 
179  virtual bool parseWithWeightDescriptors(
180  void const* serialized_onnx_model, size_t serialized_onnx_model_size,
181  uint32_t weight_count,
182  onnxTensorDescriptorV1 const* weight_descriptors)
183  = 0;
184 
193  virtual bool supportsOperator(const char* op_name) const = 0;
196  virtual void destroy() = 0;
202  virtual int getNbErrors() const = 0;
207  virtual IParserError const* getError(int index) const = 0;
212  virtual void clearErrors() = 0;
213 
214 protected:
215  virtual ~IParser() {}
216 };
217 
218 } // namespace nvonnxparser
219 
220 extern "C" TENSORRTAPI void* createNvOnnxParser_INTERNAL(void* network, void* logger, int version);
221 extern "C" TENSORRTAPI int getNvOnnxParserVersion();
222 
223 namespace nvonnxparser
224 {
225 
226 #ifdef SWIG
227 inline IParser* createParser(nvinfer1::INetworkDefinition* network,
228  nvinfer1::ILogger* logger)
229 {
230  return static_cast<IParser*>(
231  createNvOnnxParser_INTERNAL(network, logger, NV_ONNX_PARSER_VERSION));
232 }
233 #endif // SWIG
234 
235 namespace
236 {
237 
245 #ifdef _MSC_VER
247  nvinfer1::ILogger& logger)
248 #else
250  nvinfer1::ILogger& logger)
251 #endif
252 {
253  return static_cast<IParser*>(
254  createNvOnnxParser_INTERNAL(&network, &logger, NV_ONNX_PARSER_VERSION));
255 }
256 
257 } // namespace
258 
259 } // namespace nvonnxparser
260 
261 #endif // NV_ONNX_PARSER_H
nvonnxparser::anonymous_namespace{NvOnnxParser.h}::createParser
IParser * createParser(nvinfer1::INetworkDefinition &network, nvinfer1::ILogger &logger)
Create a new parser object.
Definition: NvOnnxParser.h:249
nvonnxparser::IParserError::file
virtual const char * file() const =0
source file in which the error occurred
nvonnxparser::IParser::supportsModel
virtual bool supportsModel(void const *serialized_onnx_model, size_t serialized_onnx_model_size, SubGraphCollection_t &sub_graph_collection)=0
Check whether TensorRT supports a particular ONNX model.
nvinfer1::INetworkDefinition
A network definition for input to the builder.
Definition: NvInfer.h:4926
nvonnxparser::IParser
an object for parsing ONNX models into a TensorRT network definition
Definition: NvOnnxParser.h:125
nvonnxparser::IParserError::func
virtual const char * func() const =0
source function in which the error occurred
nvonnxparser::IParser::parseWithWeightDescriptors
virtual bool parseWithWeightDescriptors(void const *serialized_onnx_model, size_t serialized_onnx_model_size, uint32_t weight_count, onnxTensorDescriptorV1 const *weight_descriptors)=0
Parse a serialized ONNX model into the TensorRT network with consideration of user provided weights.
nvinfer1::ILogger
Application-implemented logging interface for the builder, engine and runtime.
Definition: NvInferRuntimeCommon.h:1066
nvonnxparser
The TensorRT ONNX parser API namespace.
Definition: NvOnnxConfig.h:55
SubGraph_t
std::pair< std::vector< size_t >, bool > SubGraph_t
The data structure containing the parsing capability of a set of nodes in an ONNX graph.
Definition: NvOnnxParser.h:47
nvonnxparser::IParser::getError
virtual IParserError const * getError(int index) const =0
Get an error that occurred during prior calls to parse.
nvonnxparser::IParser::parse
virtual bool parse(void const *serialized_onnx_model, size_t serialized_onnx_model_size)=0
Parse a serialized ONNX model into the TensorRT network. This method has very limited diagnostic....
nvonnxparser::IParser::getNbErrors
virtual int getNbErrors() const =0
Get the number of errors that occurred during prior calls to parse.
nvonnxparser::IParser::parseFromFile
virtual bool parseFromFile(const char *onnxModelFile, int verbosity)=0
Parse an onnx model file, can be a binary protobuf or a text onnx model calls parse method inside.
nvonnxparser::IParserError::desc
virtual const char * desc() const =0
description of the error
nvonnxparser::IParser::clearErrors
virtual void clearErrors()=0
Clear errors from prior calls to parse.
SubGraphCollection_t
std::vector< SubGraph_t > SubGraphCollection_t
The data structure containing all SubGraph_t partitioned out of an ONNX graph.
Definition: NvOnnxParser.h:54
nvonnxparser::IParser::supportsOperator
virtual bool supportsOperator(const char *op_name) const =0
Returns whether the specified operator may be supported by the parser.
nvonnxparser::IParserError::line
virtual int line() const =0
source line at which the error occurred
nvonnxparser::IParserError
an object containing information about an error
Definition: NvOnnxParser.h:95
nvonnxparser::IParserError::node
virtual int node() const =0
index of the ONNX model node in which the error occurred
nvonnxparser::IParser::destroy
virtual void destroy()=0
destroy this object
NvInfer.h
nvonnxparser::IParserError::code
virtual ErrorCode code() const =0
the error code