TensorRT for RTX 1.6.1
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
193{
194public:
209 virtual bool parse(
210 void const* serialized_onnx_model, size_t serialized_onnx_model_size, const char* model_path = nullptr) noexcept
211 = 0;
212
223 virtual bool parseFromFile(char const* onnxModelFile, int verbosity) noexcept = 0;
224
234 virtual bool supportsOperator(const char* op_name) const noexcept = 0;
235
242 virtual int getNbErrors() const noexcept = 0;
243
249 virtual IParserError const* getError(int index) const noexcept = 0;
250
256 virtual void clearErrors() noexcept = 0;
257
258 virtual ~IParser() noexcept = default;
259
276 virtual char const* const* getUsedVCPluginLibraries(int64_t& nbPluginLibs) const noexcept = 0;
277
289 virtual void setFlags(OnnxParserFlags onnxParserFlags) noexcept = 0;
290
298 virtual OnnxParserFlags getFlags() const noexcept = 0;
299
307 virtual void clearFlag(OnnxParserFlag onnxParserFlag) noexcept = 0;
308
316 virtual void setFlag(OnnxParserFlag onnxParserFlag) noexcept = 0;
317
325 virtual bool getFlag(OnnxParserFlag onnxParserFlag) const noexcept = 0;
326
339 virtual nvinfer1::ITensor const* getLayerOutputTensor(char const* name, int64_t i) noexcept = 0;
340
353 virtual bool supportsModelV2(
354 void const* serializedOnnxModel, size_t serializedOnnxModelSize, char const* modelPath = nullptr) noexcept = 0;
355
363 virtual int64_t getNbSubgraphs() noexcept = 0;
364
373 virtual bool isSubgraphSupported(int64_t const index) noexcept = 0;
374
385 virtual int64_t* getSubgraphNodes(int64_t const index, int64_t& subgraphLength) noexcept = 0;
386
403 virtual bool loadModelProto(
404 void const* serializedOnnxModel, size_t serializedOnnxModelSize, char const* modelPath = nullptr) noexcept = 0;
405
428 virtual bool loadInitializer(char const* name, void const* data, size_t size) noexcept = 0;
429
435 virtual bool parseModelProto() noexcept = 0;
436
442 virtual bool setBuilderConfig(const nvinfer1::IBuilderConfig* const builderConfig) noexcept = 0;
443};
444
456enum class RefitTransformKind : int32_t
457{
459 kIDENTITY = 0,
460
463
467
471
474 kCONSTANT_NODE = 4,
475
479};
480
482template <>
483constexpr int32_t EnumMax<RefitTransformKind>() noexcept
484{
485 return 5;
486}
487
498{
500 char const* trtName;
501
504
509 int32_t onnxDtype;
510
515
517 int64_t count;
518
520 int32_t nbSources;
521
524 char const* const* sourceOnnxNames;
525
528 float epsilon;
529
534 void const* fixedData;
535
538};
539
550{
551public:
552 virtual ~IRefitterObserver() noexcept = default;
553
562 virtual void onRefittableWeight(RefitRecord const& record) noexcept = 0;
563};
564
573{
574public:
587 virtual bool refitFromBytes(
588 void const* serializedOnnxModel, size_t serializedOnnxModelSize, char const* modelPath = nullptr) noexcept
589 = 0;
590
601 virtual bool refitFromFile(char const* onnxModelFile) noexcept = 0;
602
608 virtual int32_t getNbErrors() const noexcept = 0;
609
615 virtual IParserError const* getError(int32_t index) const noexcept = 0;
616
622 virtual void clearErrors() = 0;
623
624 virtual ~IParserRefitter() noexcept = default;
625
641 virtual bool loadModelProto(
642 void const* serializedOnnxModel, size_t serializedOnnxModelSize, char const* modelPath = nullptr) noexcept = 0;
643
665 virtual bool loadInitializer(char const* name, void const* data, size_t size) noexcept = 0;
666
672 virtual bool refitModelProto() noexcept = 0;
673
683 virtual void setRefitObserver(IRefitterObserver* observer) noexcept = 0;
684};
685
686} // namespace nvonnxparser
687
688extern "C" TENSORRTAPI void* createNvOnnxParser_INTERNAL(void* network, void* logger, int version) noexcept;
690 void* refitter, void* logger, int32_t version) noexcept;
691extern "C" TENSORRTAPI int getNvOnnxParserVersion() noexcept;
692
693namespace nvonnxparser
694{
695
696namespace
697{
698
715{
716 return static_cast<IParser*>(createNvOnnxParser_INTERNAL(&network, &logger, NV_ONNX_PARSER_VERSION));
717}
718
729{
730 return static_cast<IParserRefitter*>(
731 createNvOnnxParserRefitter_INTERNAL(&refitter, &logger, NV_ONNX_PARSER_VERSION));
732}
733
734} // namespace
735
736} // namespace nvonnxparser
737
738#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:8101
Updates weights in an engine.
Definition: NvInferRuntime.h:2289
Application-implemented logging interface for the builder, refitter and runtime.
Definition: NvInferRuntime.h:1575
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:193
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 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:573
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:550
virtual ~IRefitterObserver() noexcept=default
The TensorRT API version 1 namespace.
Definition: NvInferPluginBase.h:29
DataType
The type of weights and tensors. The datatypes other than kBOOL, kINT32, and kINT64 are "activation d...
Definition: NvInferRuntimeBase.h:149
IParser * createParser(nvinfer1::INetworkDefinition &network, nvinfer1::ILogger &logger) noexcept
Create a new parser object.
Definition: NvOnnxParser.h:714
IParserRefitter * createParserRefitter(nvinfer1::IRefitter &refitter, nvinfer1::ILogger &logger) noexcept
Create a new ONNX refitter object.
Definition: NvOnnxParser.h:728
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:483
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:457
@ 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:498
int32_t nbSources
Number of source ONNX names supplied below. At most 4.
Definition: NvOnnxParser.h:520
size_t fixedDataSize
Length of fixedData in bytes, or 0 when fixedData is null.
Definition: NvOnnxParser.h:537
void const * fixedData
Definition: NvOnnxParser.h:534
RefitTransformKind kind
What transformation produces the refit data from the sources.
Definition: NvOnnxParser.h:503
int32_t onnxDtype
Definition: NvOnnxParser.h:509
char const *const * sourceOnnxNames
Definition: NvOnnxParser.h:524
float epsilon
Definition: NvOnnxParser.h:528
int64_t count
Element count of the refit data.
Definition: NvOnnxParser.h:517
nvinfer1::DataType trtDtype
Definition: NvOnnxParser.h:514
char const * trtName
Name to pass to nvinfer1::IRefitter::setNamedWeights for this weight. Always non-null.
Definition: NvOnnxParser.h:500

  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