TensorRT 11.4.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
91 = uint32_t;
92
93enum class OnnxParserFlag : int32_t
94{
107 // This flag is set to be OFF by default.
116};
117
119template <>
120constexpr int32_t EnumMax<OnnxParserFlag>() noexcept
121{
122 return 4;
123}
124
131{
132public:
136 virtual ErrorCode code() const = 0;
140 virtual char const* desc() const = 0;
144 virtual char const* file() const = 0;
148 virtual int line() const = 0;
152 virtual char const* func() const = 0;
156 virtual int node() const = 0;
160 virtual char const* nodeName() const = 0;
164 virtual char const* nodeOperator() const = 0;
170 virtual char const* const* localFunctionStack() const = 0;
174 // a stack size of 0.
176 virtual int32_t localFunctionStackSize() const = 0;
177
178protected:
179 virtual ~IParserError() {}
180};
181
182class IRefitterObserver;
183
195{
196public:
211 virtual bool parse(
212 void const* serialized_onnx_model, size_t serialized_onnx_model_size, const char* model_path = nullptr) noexcept
213 = 0;
214
225 virtual bool parseFromFile(char const* onnxModelFile, int verbosity) noexcept = 0;
226
236 virtual bool supportsOperator(const char* op_name) const noexcept = 0;
237
244 virtual int getNbErrors() const noexcept = 0;
245
251 virtual IParserError const* getError(int index) const noexcept = 0;
252
258 virtual void clearErrors() noexcept = 0;
259
260 virtual ~IParser() noexcept = default;
261
278 virtual char const* const* getUsedVCPluginLibraries(int64_t& nbPluginLibs) const noexcept = 0;
279
291 virtual void setFlags(OnnxParserFlags onnxParserFlags) noexcept = 0;
292
300 virtual OnnxParserFlags getFlags() const noexcept = 0;
301
309 virtual void clearFlag(OnnxParserFlag onnxParserFlag) noexcept = 0;
310
318 virtual void setFlag(OnnxParserFlag onnxParserFlag) noexcept = 0;
319
327 virtual bool getFlag(OnnxParserFlag onnxParserFlag) const noexcept = 0;
328
341 virtual nvinfer1::ITensor const* getLayerOutputTensor(char const* name, int64_t i) noexcept = 0;
342
355 virtual bool supportsModelV2(
356 void const* serializedOnnxModel, size_t serializedOnnxModelSize, char const* modelPath = nullptr) noexcept = 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 = 0;
407
430 virtual bool loadInitializer(char const* name, void const* data, size_t size) noexcept = 0;
431
437 virtual bool parseModelProto() noexcept = 0;
438
444 virtual bool setBuilderConfig(const nvinfer1::IBuilderConfig* const builderConfig) noexcept = 0;
445
467 virtual void setRefitObserver(IRefitterObserver* observer) noexcept = 0;
468};
469
481enum class RefitTransformKind : int32_t
482{
484 kIDENTITY = 0,
485
488
492
496
499 kCONSTANT_NODE = 4,
500
504};
505
507template <>
508constexpr int32_t EnumMax<RefitTransformKind>() noexcept
509{
510 return 5;
511}
512
523{
525 char const* trtName;
526
529
534 int32_t onnxDtype;
535
540
542 int64_t count;
543
545 int32_t nbSources;
546
549 char const* const* sourceOnnxNames;
550
553 float epsilon;
554
559 void const* fixedData;
560
563};
564
575{
576public:
577 virtual ~IRefitterObserver() noexcept = default;
578
587 virtual void onRefittableWeight(RefitRecord const& record) noexcept = 0;
588};
589
598{
599public:
612 virtual bool refitFromBytes(
613 void const* serializedOnnxModel, size_t serializedOnnxModelSize, char const* modelPath = nullptr) noexcept
614 = 0;
615
626 virtual bool refitFromFile(char const* onnxModelFile) noexcept = 0;
627
633 virtual int32_t getNbErrors() const noexcept = 0;
634
640 virtual IParserError const* getError(int32_t index) const noexcept = 0;
641
647 virtual void clearErrors() = 0;
648
649 virtual ~IParserRefitter() noexcept = default;
650
666 virtual bool loadModelProto(
667 void const* serializedOnnxModel, size_t serializedOnnxModelSize, char const* modelPath = nullptr) noexcept = 0;
668
690 virtual bool loadInitializer(char const* name, void const* data, size_t size) noexcept = 0;
691
697 virtual bool refitModelProto() noexcept = 0;
698
708 virtual void setRefitObserver(IRefitterObserver* observer) noexcept = 0;
709};
710
711} // namespace nvonnxparser
712
713extern "C" TENSORRTAPI void* createNvOnnxParser_INTERNAL(void* network, void* logger, int version) noexcept;
715 void* refitter, void* logger, int32_t version) noexcept;
716extern "C" TENSORRTAPI int getNvOnnxParserVersion() noexcept;
717
718namespace nvonnxparser
719{
720
721namespace
722{
723
740{
741 return static_cast<IParser*>(createNvOnnxParser_INTERNAL(&network, &logger, NV_ONNX_PARSER_VERSION));
742}
743
754{
755 return static_cast<IParserRefitter*>(
756 createNvOnnxParserRefitter_INTERNAL(&refitter, &logger, NV_ONNX_PARSER_VERSION));
757}
758
759} // namespace
760
761} // namespace nvonnxparser
762
763#endif // NV_ONNX_PARSER_H
#define TENSORRTAPI
Definition: NvInferRuntimeBase.h:70
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:131
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 ~IParserError()
Definition: NvOnnxParser.h:179
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...
an object for parsing ONNX models into a TensorRT network definition
Definition: NvOnnxParser.h:195
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 supportsOperator(const char *op_name) const noexcept=0
Returns whether the specified operator may be supported by the parser.
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 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 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 bool setBuilderConfig(const nvinfer1::IBuilderConfig *const builderConfig) noexcept=0
Set the BuilderConfig for the parser.
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 bool parse(void const *serialized_onnx_model, size_t serialized_onnx_model_size, const char *model_path=nullptr) noexcept=0
Parse a serialized ONNX model into the TensorRT network. This method has very limited diagnostics....
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:598
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:575
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:151
IParser * createParser(nvinfer1::INetworkDefinition &network, nvinfer1::ILogger &logger) noexcept
Create a new parser object.
Definition: NvOnnxParser.h:739
IParserRefitter * createParserRefitter(nvinfer1::IRefitter &refitter, nvinfer1::ILogger &logger) noexcept
Create a new ONNX refitter object.
Definition: NvOnnxParser.h:753
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:91
constexpr int32_t EnumMax< RefitTransformKind >() noexcept
Specialization. See nvonnxparser::EnumMax() for details.
Definition: NvOnnxParser.h:508
ErrorCode
The type of error that the parser or refitter may return.
Definition: NvOnnxParser.h:59
OnnxParserFlag
Definition: NvOnnxParser.h:94
@ 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:482
@ 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:120
One refittable-weight description emitted by IRefitterObserver.
Definition: NvOnnxParser.h:523
int32_t nbSources
Number of source ONNX names supplied below. At most 4.
Definition: NvOnnxParser.h:545
size_t fixedDataSize
Length of fixedData in bytes, or 0 when fixedData is null.
Definition: NvOnnxParser.h:562
void const * fixedData
Definition: NvOnnxParser.h:559
RefitTransformKind kind
What transformation produces the refit data from the sources.
Definition: NvOnnxParser.h:528
int32_t onnxDtype
Definition: NvOnnxParser.h:534
char const *const * sourceOnnxNames
Definition: NvOnnxParser.h:549
float epsilon
Definition: NvOnnxParser.h:553
int64_t count
Element count of the refit data.
Definition: NvOnnxParser.h:542
nvinfer1::DataType trtDtype
Definition: NvOnnxParser.h:539
char const * trtName
Name to pass to nvinfer1::IRefitter::setNamedWeights for this weight. Always non-null.
Definition: NvOnnxParser.h:525

  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