TensorRT 11.5.0
NvOnnxParser.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: Copyright (c) 1993-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef NV_ONNX_PARSER_H
19#define NV_ONNX_PARSER_H
20
21#include "NvInfer.h"
22#include <stddef.h>
23
29
30#define NV_ONNX_PARSER_MAJOR 0
31#define NV_ONNX_PARSER_MINOR 2
32#define NV_ONNX_PARSER_PATCH 0
33
34static constexpr int32_t NV_ONNX_PARSER_VERSION
36
42namespace nvonnxparser
43{
44
50template <typename T>
51constexpr int32_t EnumMax() noexcept = delete;
52
58enum class ErrorCode : int
59{
60 kSUCCESS = 0,
61 kINTERNAL_ERROR = 1,
66 kINVALID_NODE = 6,
74 kREFIT_FAILED = 14
75};
76
78template <>
79constexpr int32_t EnumMax<ErrorCode>() noexcept
80{
81 return 14;
82}
83
90using OnnxParserFlags = uint32_t;
91
92enum class OnnxParserFlag : int32_t
93{
106 // This flag is set to be OFF by default.
115};
116
118template <>
119constexpr int32_t EnumMax<OnnxParserFlag>() noexcept
120{
121 return 4;
122}
123
130{
131public:
135 virtual ErrorCode code() const = 0;
139 virtual char const* desc() const = 0;
143 virtual char const* file() const = 0;
147 virtual int line() const = 0;
151 virtual char const* func() const = 0;
155 virtual int node() const = 0;
159 virtual char const* nodeName() const = 0;
163 virtual char const* nodeOperator() const = 0;
169 virtual char const* const* localFunctionStack() const = 0;
173 // a stack size of 0.
175 virtual int32_t localFunctionStackSize() const = 0;
176
177protected:
178 virtual ~IParserError() = default;
179};
180
182
194{
195public:
210 virtual bool parse(
211 void const* serialized_onnx_model, size_t serialized_onnx_model_size, char const* model_path = nullptr) noexcept
212 = 0;
213
224 virtual bool parseFromFile(char const* onnxModelFile, int verbosity) noexcept = 0;
225
235 virtual bool supportsOperator(char const* op_name) const noexcept = 0;
236
243 virtual int getNbErrors() const noexcept = 0;
244
250 virtual IParserError const* getError(int index) const noexcept = 0;
251
257 virtual void clearErrors() noexcept = 0;
258
259 virtual ~IParser() noexcept = default;
260
277 virtual char const* const* getUsedVCPluginLibraries(int64_t& nbPluginLibs) const noexcept = 0;
278
290 virtual void setFlags(OnnxParserFlags onnxParserFlags) noexcept = 0;
291
299 virtual OnnxParserFlags getFlags() const noexcept = 0;
300
308 virtual void clearFlag(OnnxParserFlag onnxParserFlag) noexcept = 0;
309
317 virtual void setFlag(OnnxParserFlag onnxParserFlag) noexcept = 0;
318
326 virtual bool getFlag(OnnxParserFlag onnxParserFlag) const noexcept = 0;
327
340 virtual nvinfer1::ITensor const* getLayerOutputTensor(char const* name, int64_t i) noexcept = 0;
341
354 virtual bool supportsModelV2(
355 void const* serializedOnnxModel, size_t serializedOnnxModelSize, char const* modelPath = nullptr) noexcept
356 = 0;
357
365 virtual int64_t getNbSubgraphs() noexcept = 0;
366
375 virtual bool isSubgraphSupported(int64_t const index) noexcept = 0;
376
387 virtual int64_t* getSubgraphNodes(int64_t const index, int64_t& subgraphLength) noexcept = 0;
388
405 virtual bool loadModelProto(
406 void const* serializedOnnxModel, size_t serializedOnnxModelSize, char const* modelPath = nullptr) noexcept
407 = 0;
408
431 virtual bool loadInitializer(char const* name, void const* data, size_t size) noexcept = 0;
432
438 virtual bool parseModelProto() noexcept = 0;
439
445 virtual bool setBuilderConfig(nvinfer1::IBuilderConfig const* const builderConfig) noexcept = 0;
446
487 virtual void setRefitObserver(IRefitterObserver* observer) noexcept = 0;
488};
489
501enum class RefitTransformKind : int32_t
502{
504 kIDENTITY = 0,
505
508
512
516
519 kCONSTANT_NODE = 4,
520
524};
525
527template <>
528constexpr int32_t EnumMax<RefitTransformKind>() noexcept
529{
530 return 5;
531}
532
543{
545 char const* trtName;
546
549
554 int32_t onnxDtype;
555
560
562 int64_t count;
563
565 int32_t nbSources;
566
569 char const* const* sourceOnnxNames;
570
573 float epsilon;
574
579 void const* fixedData;
580
583};
584
595{
596public:
597 virtual ~IRefitterObserver() noexcept = default;
598
607 virtual void onRefittableWeight(RefitRecord const& record) noexcept = 0;
608};
609
618{
619public:
632 virtual bool refitFromBytes(
633 void const* serializedOnnxModel, size_t serializedOnnxModelSize, char const* modelPath = nullptr) noexcept
634 = 0;
635
646 virtual bool refitFromFile(char const* onnxModelFile) noexcept = 0;
647
653 virtual int32_t getNbErrors() const noexcept = 0;
654
660 virtual IParserError const* getError(int32_t index) const noexcept = 0;
661
667 virtual void clearErrors() = 0;
668
669 virtual ~IParserRefitter() noexcept = default;
670
686 virtual bool loadModelProto(
687 void const* serializedOnnxModel, size_t serializedOnnxModelSize, char const* modelPath = nullptr) noexcept
688 = 0;
689
711 virtual bool loadInitializer(char const* name, void const* data, size_t size) noexcept = 0;
712
718 virtual bool refitModelProto() noexcept = 0;
719
729 virtual void setRefitObserver(IRefitterObserver* observer) noexcept = 0;
730};
731
732} // namespace nvonnxparser
733
734extern "C" TENSORRTAPI void* createNvOnnxParser_INTERNAL(void* network, void* logger, int version) noexcept;
736 void* refitter, void* logger, int32_t version) noexcept;
737extern "C" TENSORRTAPI int getNvOnnxParserVersion() noexcept;
738
739namespace nvonnxparser
740{
741
742namespace
743{
744
761{
762 return static_cast<IParser*>(createNvOnnxParser_INTERNAL(&network, &logger, NV_ONNX_PARSER_VERSION));
763}
764
775{
776 return static_cast<IParserRefitter*>(
777 createNvOnnxParserRefitter_INTERNAL(&refitter, &logger, NV_ONNX_PARSER_VERSION));
778}
779
780} // namespace
781
782} // namespace nvonnxparser
783
784#endif // NV_ONNX_PARSER_H
#define TENSORRTAPI
Definition: NvInferRuntimeBase.h:78
TENSORRTAPI void * createNvOnnxParserRefitter_INTERNAL(void *refitter, void *logger, int32_t version) noexcept
TENSORRTAPI void * createNvOnnxParser_INTERNAL(void *network, void *logger, int version) noexcept
TENSORRTAPI int getNvOnnxParserVersion() noexcept
#define NV_ONNX_PARSER_PATCH
Definition: NvOnnxParser.h:32
#define NV_ONNX_PARSER_MINOR
Definition: NvOnnxParser.h:31
#define NV_ONNX_PARSER_MAJOR
Definition: NvOnnxParser.h:30
A network definition for input to the builder.
Definition: NvInfer.h:8169
Updates weights in an engine.
Definition: NvInferRuntime.h:2238
Application-implemented logging interface for the builder, refitter and runtime.
Definition: NvInferRuntime.h:1614
an object containing information about an error
Definition: NvOnnxParser.h:130
virtual char const * nodeOperator() const =0
name of the node operation in which the error occurred.
virtual char const * func() const =0
source function in which the error occurred.
virtual int line() const =0
source line at which the error occurred.
virtual char const * desc() const =0
description of the error.
virtual ErrorCode code() const =0
the error code.
virtual char const * nodeName() const =0
name of the node in which the error occurred.
virtual char const *const * localFunctionStack() const =0
A list of the local function names, from the top level down, constituting the current stack trace in ...
virtual int node() const =0
index of the ONNX model node in which the error occurred.
virtual char const * file() const =0
source file in which the error occurred.
virtual int32_t localFunctionStackSize() const =0
The size of the stack of local functions at the point where the error occurred. A top-level node that...
virtual ~IParserError()=default
an object for parsing ONNX models into a TensorRT network definition
Definition: NvOnnxParser.h:194
virtual IParserError const * getError(int index) const noexcept=0
Get an error that occurred during prior calls to parse.
virtual char const *const * getUsedVCPluginLibraries(int64_t &nbPluginLibs) const noexcept=0
Query the plugin libraries needed to implement operations used by the parser in a version-compatible ...
virtual void setFlag(OnnxParserFlag onnxParserFlag) noexcept=0
Set a single parser flag.
virtual bool loadInitializer(char const *name, void const *data, size_t size) noexcept=0
Prompt the ONNX parser to load an initializer with user-provided binary data. The lifetime of the dat...
virtual bool parse(void const *serialized_onnx_model, size_t serialized_onnx_model_size, char const *model_path=nullptr) noexcept=0
Parse a serialized ONNX model into the TensorRT network. This method has very limited diagnostics....
virtual bool parseModelProto() noexcept=0
Begin the parsing and conversion process of the loaded ONNX model into a TensorRT INetworkDefinition.
virtual bool supportsModelV2(void const *serializedOnnxModel, size_t serializedOnnxModelSize, char const *modelPath=nullptr) noexcept=0
Check whether TensorRT supports a particular ONNX model. If the function returns True,...
virtual void clearErrors() noexcept=0
Clear errors from prior calls to parse.
virtual bool setBuilderConfig(nvinfer1::IBuilderConfig const *const builderConfig) noexcept=0
Set the BuilderConfig for the parser.
virtual void setRefitObserver(IRefitterObserver *observer) noexcept=0
Set or clear an optional observer notified once per refittable weight during parsing.
virtual bool getFlag(OnnxParserFlag onnxParserFlag) const noexcept=0
Returns true if the parser flag is set.
virtual void clearFlag(OnnxParserFlag onnxParserFlag) noexcept=0
clear a parser flag.
virtual bool supportsOperator(char const *op_name) const noexcept=0
Returns whether the specified operator may be supported by the parser.
virtual OnnxParserFlags getFlags() const noexcept=0
Get the parser flags. Defaults to 0.
virtual bool isSubgraphSupported(int64_t const index) noexcept=0
Returns whether the subgraph is supported. Calling this function before calling supportsModelV2 resul...
virtual int64_t getNbSubgraphs() noexcept=0
Get the number of subgraphs. Calling this function before calling supportsModelV2 results in undefine...
virtual bool loadModelProto(void const *serializedOnnxModel, size_t serializedOnnxModelSize, char const *modelPath=nullptr) noexcept=0
Load a serialized ONNX model into the parser. Unlike the parse() or parseFromFile() functions,...
virtual int64_t * getSubgraphNodes(int64_t const index, int64_t &subgraphLength) noexcept=0
Get the nodes of the specified subgraph. Calling this function before calling supportsModelV2 results...
virtual void setFlags(OnnxParserFlags onnxParserFlags) noexcept=0
Set the parser flags.
virtual bool parseFromFile(char const *onnxModelFile, int verbosity) noexcept=0
Parse an onnx model file, which can be a binary protobuf or a text onnx model calls parse method insi...
virtual int getNbErrors() const noexcept=0
Get the number of errors that occurred during prior calls to parse.
virtual nvinfer1::ITensor const * getLayerOutputTensor(char const *name, int64_t i) noexcept=0
Return the i-th output ITensor object for the ONNX layer "name".
An interface designed to refit weights from an ONNX model.
Definition: NvOnnxParser.h:618
virtual bool refitFromBytes(void const *serializedOnnxModel, size_t serializedOnnxModelSize, char const *modelPath=nullptr) noexcept=0
Load a serialized ONNX model from memory and perform weight refit.
Observer interface invoked by IParserRefitter once per refittable engine weight.
Definition: NvOnnxParser.h:595
virtual ~IRefitterObserver() noexcept=default
The TensorRT API version 1 namespace.
Definition: NvInferSafePlugin.h:33
DataType
The type of weights and tensors. The datatypes other than kBOOL, kINT32, and kINT64 are "activation d...
Definition: NvInferRuntimeBase.h:161
IParser * createParser(nvinfer1::INetworkDefinition &network, nvinfer1::ILogger &logger) noexcept
Create a new parser object.
Definition: NvOnnxParser.h:760
IParserRefitter * createParserRefitter(nvinfer1::IRefitter &refitter, nvinfer1::ILogger &logger) noexcept
Create a new ONNX refitter object.
Definition: NvOnnxParser.h:774
The TensorRT ONNX parser API namespace.
Definition: NvOnnxConfig.h:24
uint32_t OnnxParserFlags
Represents one or more OnnxParserFlag values using binary OR operations, e.g., 1U << OnnxParserFlag::...
Definition: NvOnnxParser.h:90
constexpr int32_t EnumMax< RefitTransformKind >() noexcept
Specialization. See nvonnxparser::EnumMax() for details.
Definition: NvOnnxParser.h:528
ErrorCode
The type of error that the parser or refitter may return.
Definition: NvOnnxParser.h:59
OnnxParserFlag
Definition: NvOnnxParser.h:93
@ kADJUST_FOR_DLA
Opportunistically rewrite or modify layers to make them more amenable to running on DLA.
constexpr int32_t EnumMax< ErrorCode >() noexcept
Specialization. See nvonnxparser::EnumMax() for details.
Definition: NvOnnxParser.h:79
constexpr int32_t EnumMax() noexcept=delete
RefitTransformKind
Identifies how a refittable engine weight is produced from one or more ONNX initializers or node attr...
Definition: NvOnnxParser.h:502
@ kDOUBLE_TO_FLOAT
Source is one ONNX initializer of DOUBLE type; refit data is the FLOAT cast.
constexpr int32_t EnumMax< OnnxParserFlag >() noexcept
Specialization. See nvonnxparser::EnumMax() for details.
Definition: NvOnnxParser.h:119
One refittable-weight description emitted by IRefitterObserver.
Definition: NvOnnxParser.h:543
int32_t nbSources
Number of source ONNX names supplied below. At most 4.
Definition: NvOnnxParser.h:565
size_t fixedDataSize
Length of fixedData in bytes, or 0 when fixedData is null.
Definition: NvOnnxParser.h:582
void const * fixedData
Definition: NvOnnxParser.h:579
RefitTransformKind kind
What transformation produces the refit data from the sources.
Definition: NvOnnxParser.h:548
int32_t onnxDtype
Definition: NvOnnxParser.h:554
char const *const * sourceOnnxNames
Definition: NvOnnxParser.h:569
float epsilon
Definition: NvOnnxParser.h:573
int64_t count
Element count of the refit data.
Definition: NvOnnxParser.h:562
nvinfer1::DataType trtDtype
Definition: NvOnnxParser.h:559
char const * trtName
Name to pass to nvinfer1::IRefitter::setNamedWeights for this weight. Always non-null.
Definition: NvOnnxParser.h:545

  Copyright © 2024 NVIDIA Corporation
  Privacy Policy | Manage My Privacy | Do Not Sell or Share My Data | Terms of Service | Accessibility | Corporate Policies | Product Security | Contact