TensorRT 11.3.0
NvInferSafeRuntime.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: Copyright (c) 1993-2026 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// @brief Main header file for the NVIDIA Safe Runtime API.
19// This file provides the primary interface for users to interact with the NVIDIA Safe Runtime API.
20// It includes the necessary definitions, classes, and functions for creating and managing safe graphs,
21// executing inference, and handling errors.
22// Users should include this header file in their application to access the Safe Runtime API functionality.
23
24#ifndef NV_INFER_SAFE_RUNTIME_H
25#define NV_INFER_SAFE_RUNTIME_H
27#include "NvInferSafePlugin.h"
28#include "NvInferSafeRecorder.h"
29#include <algorithm>
30#include <cuda_fp16.h>
31#include <iterator>
32
33namespace nvinfer2
34{
35namespace safe
36{
37using half_t = __half;
38
52{
53public:
54 TypedArray() noexcept
55 : mType(DataType::kFLOAT)
56 , mData(nullptr)
57 , mBufferSize(0U)
58 {
59 }
60 // Shallow copy of TypedArray is allowed
61 TypedArray(TypedArray const&) = default;
62 TypedArray(TypedArray&&) = default;
63 TypedArray& operator=(TypedArray const&) & = default;
65 ~TypedArray() noexcept = default;
66
68 TypedArray(float* ptr, uint64_t const bufferSize) noexcept
69 : mType(DataType::kFLOAT)
70 , mData(ptr)
71 , mBufferSize(bufferSize)
72 {
73 }
74
76 TypedArray(half_t* ptr, uint64_t const bufferSize) noexcept
77 : mType(DataType::kHALF)
78 , mData(ptr)
79 , mBufferSize(bufferSize)
80 {
81 }
82
84 TypedArray(int64_t* ptr, uint64_t const bufferSize) noexcept
85 : mType(DataType::kINT64)
86 , mData(ptr)
87 , mBufferSize(bufferSize)
88 {
89 }
90
92 TypedArray(int32_t* ptr, uint64_t const bufferSize) noexcept
93 : mType(DataType::kINT32)
94 , mData(ptr)
95 , mBufferSize(bufferSize)
96 {
97 }
98
100 TypedArray(int8_t* ptr, uint64_t const bufferSize) noexcept
101 : mType(DataType::kINT8)
102 , mData(ptr)
103 , mBufferSize(bufferSize)
104 {
105 }
106
108 TypedArray(uint8_t* ptr, uint64_t const bufferSize) noexcept
109 : mType(DataType::kUINT8)
110 , mData(ptr)
111 , mBufferSize(bufferSize)
112 {
113 }
115 TypedArray(bool* ptr, uint64_t const bufferSize) noexcept
116 : mType(DataType::kBOOL)
117 , mData(ptr)
118 , mBufferSize(bufferSize)
119 {
120 }
121
122 // NOLINTBEGIN to avoid clang-tidy requiring [[nodiscard]]
124 DataType getType() const noexcept
125 {
126 return mType;
127 }
128
131 float* getFloat() const noexcept
132 {
133 if (mType == DataType::kFLOAT)
134 {
135 return static_cast<float*>(mData);
136 }
137 return nullptr;
138 }
139
142 half_t* getHalf() const noexcept
143 {
144 if (mType == DataType::kHALF)
145 {
146 return static_cast<half_t*>(mData);
147 }
148 return nullptr;
149 }
150
153 int64_t* getInt64() const noexcept
154 {
155 if (mType == DataType::kINT64)
156 {
157 return static_cast<int64_t*>(mData);
158 }
159 return nullptr;
160 }
161
164 int32_t* getInt32() const noexcept
165 {
166 if (mType == DataType::kINT32)
167 {
168 return static_cast<int32_t*>(mData);
169 }
170 return nullptr;
171 }
172
175 int8_t* getInt8() const noexcept
176 {
177 if (mType == DataType::kINT8)
178 {
179 return static_cast<int8_t*>(mData);
180 }
181 return nullptr;
182 }
183
186 uint8_t* getUint8() const noexcept
187 {
188 if (mType == DataType::kUINT8)
189 {
190 return static_cast<uint8_t*>(mData);
191 }
192 return nullptr;
193 }
194
197 bool* getBool() const noexcept
198 {
199 if (mType == DataType::kBOOL)
200 {
201 return static_cast<bool*>(mData);
202 }
203 return nullptr;
204 }
205
207 uint64_t getSize() const noexcept
208 {
209 return mBufferSize;
210 }
211
214 void* getData() const noexcept
215 {
216 return mData;
217 }
218 // NOLINTEND
219
220private:
221 DataType mType; // This is the current type of the data.
222 void* mData; // This is the pointer that holds the data.
223 uint64_t mBufferSize; // This is the size of the buffer in bytes that holds the data.
224};
225
240{
241public:
244 static constexpr int32_t MAX_DIMS{9};
245
247 int32_t nbDims;
248
250 int64_t d[MAX_DIMS];
251};
252
253inline bool operator==(PhysicalDims const& d0, PhysicalDims const& d1) noexcept
254{
255 return d0.nbDims == d1.nbDims
256 && (d0.nbDims <= 0 || std::equal(std::cbegin(d0.d), std::cbegin(d0.d) + d0.nbDims, std::cbegin(d1.d)));
257}
258
259inline bool operator!=(PhysicalDims const& d0, PhysicalDims const& d1) noexcept
260{
261 return !(d0 == d1);
262}
263
287{
289 AsciiChar const* tensorName{nullptr};
297 DataType dataType{DataType::kFLOAT};
299 uint64_t bytesPerComponent{0U};
303 int64_t vectorizedDim{-1};
305 uint64_t sizeInBytes{0U};
307 TensorIOMode ioMode{TensorIOMode::kNONE};
316};
317
331{
332public:
333 ITRTGraph(ITRTGraph const&) = delete;
334 ITRTGraph(ITRTGraph&&) = delete;
335 ITRTGraph& operator=(ITRTGraph const&) & = delete;
337
341 virtual ~ITRTGraph() noexcept = default;
342
361 virtual ErrorCode clone(ITRTGraph*& graph, ISafeRecorder& recorder) noexcept = 0;
362
376 virtual ErrorCode getScratchMemorySize(size_t& size) const noexcept = 0;
377
388 virtual ErrorCode getTRTManagedScratch(bool& flag) const noexcept = 0;
389
405 virtual ErrorCode setScratchMemory(void* memory) noexcept = 0;
406
420 virtual ErrorCode getScratchMemory(void*& memory) noexcept = 0;
421
430 virtual ErrorCode getNbIOTensors(int64_t& nb) const noexcept = 0;
431
441 virtual ErrorCode getIOTensorName(AsciiChar const*& name, size_t const index) const noexcept = 0;
442
456 TensorDescriptor& desc, AsciiChar const* const tensorName) const noexcept = 0;
457
470 virtual ErrorCode getIOTensorDescriptor(TensorDescriptor& desc, int32_t const index) const noexcept = 0;
471
482 virtual ErrorCode setIOTensorAddress(AsciiChar const* const tensorName, TypedArray const& tensor) noexcept = 0;
483
494 virtual ErrorCode setIOTensorAddress(int32_t const index, TypedArray const& tensor) noexcept = 0;
495
506 virtual ErrorCode getIOTensorAddress(AsciiChar const* const tensorName, TypedArray& tensor) noexcept = 0;
507
518 virtual ErrorCode getIOTensorAddress(int32_t const index, TypedArray& tensor) noexcept = 0;
519
529 virtual ErrorCode setInputConsumedEvent(cudaEvent_t event) noexcept = 0;
530
541 virtual ErrorCode getInputConsumedEvent(cudaEvent_t& event) const noexcept = 0;
542
554 virtual ErrorCode getErrorBuffer(RuntimeErrorInformation*& buffer) const noexcept = 0;
555
564 virtual ErrorCode getSafeRecorder(ISafeRecorder*& recorder) const noexcept = 0;
565
576 virtual ErrorCode getNbIOProfiles(int64_t& nb) const noexcept = 0;
577
592 virtual ErrorCode setIOProfile(int64_t profileIndex) noexcept = 0;
593
603 virtual ErrorCode getIOProfile(int64_t& profileIndex) const noexcept = 0;
604
615 virtual ErrorCode getNbAuxStreams(int32_t& nbStreams) const noexcept = 0;
616
640 virtual ErrorCode setAuxStreams(cudaStream_t* auxStreams, int32_t nbStreams) noexcept = 0;
641
654 virtual ErrorCode executeAsync(cudaStream_t stream) noexcept = 0;
655
666 virtual ErrorCode sync() noexcept = 0;
667
668protected:
669 ITRTGraph() = default;
670};
671
718extern "C" ErrorCode createTRTGraph(ITRTGraph*& graph, void const* buffer, int64_t bufferSize,
719 AsciiChar const* companionSoPath, ISafeRecorder& recorder, bool trtManagedScratch = true,
720 ISafeMemAllocator* allocator = nullptr) noexcept;
721
739extern "C" ErrorCode destroyTRTGraph(ITRTGraph*& graph) noexcept;
740
741} // namespace safe
742} // namespace nvinfer2
743#endif /* NV_INFER_SAFE_RUNTIME_H */
Structure to define the dimensions of a tensor.
Application-implemented class for controlling memory allocation on the GPU/CPU.
Definition: NvInferSafeMemAllocator.h:86
Interface for extended recorder which allows error, warn, debug, or info messages to be recorded.
Definition: NvInferSafeRecorder.h:82
Abstract Interface for a functionally safe graph for executing inference on a built network.
Definition: NvInferSafeRuntime.h:331
virtual ErrorCode getNbIOTensors(int64_t &nb) const noexcept=0
This function returns the total number of input and output tensor for the current graph.
ITRTGraph & operator=(ITRTGraph const &) &=delete
virtual ErrorCode executeAsync(cudaStream_t stream) noexcept=0
execute one inference of this graph.
virtual ErrorCode getSafeRecorder(ISafeRecorder *&recorder) const noexcept=0
This function retrieves the ISafeRecorder for the current graph.
ITRTGraph(ITRTGraph const &)=delete
virtual ErrorCode setIOProfile(int64_t profileIndex) noexcept=0
This function selects the active IOProfile for the graph. If this function is not called,...
virtual ErrorCode setScratchMemory(void *memory) noexcept=0
This function sets the scratch memory for the graph. This should only be called if scratch memory is ...
virtual ErrorCode setInputConsumedEvent(cudaEvent_t event) noexcept=0
This function sets a cudaEvent on the current graph that triggers when the input is consumed....
virtual ErrorCode setIOTensorAddress(AsciiChar const *const tensorName, TypedArray const &tensor) noexcept=0
This function assigns a user allocated device memory block for an input tensor to the graph based on ...
virtual ErrorCode getIOProfile(int64_t &profileIndex) const noexcept=0
This function retrieves the index of the current active IOProfile for the graph.
virtual ErrorCode setAuxStreams(cudaStream_t *auxStreams, int32_t nbStreams) noexcept=0
Set the auxiliary streams that TensorRT should use to run kernels on.
virtual ErrorCode getErrorBuffer(RuntimeErrorInformation *&buffer) const noexcept=0
This function retrieves the RuntimeErrorInformation (for async error) buffer for the current graph....
ITRTGraph & operator=(ITRTGraph &&) &=delete
virtual ErrorCode getScratchMemorySize(size_t &size) const noexcept=0
This function returns the scratch memory size (in bytes) needed to store all the intermediate tensors...
virtual ErrorCode getTRTManagedScratch(bool &flag) const noexcept=0
This function returns the trtManagedScratch flag provided in createTRTGraph call.
ITRTGraph(ITRTGraph &&)=delete
virtual ErrorCode sync() noexcept=0
synchronize one inference of this graph.
virtual ErrorCode getNbAuxStreams(int32_t &nbStreams) const noexcept=0
Return the number of auxiliary streams used by this graph.
virtual ErrorCode getScratchMemory(void *&memory) noexcept=0
This function gets the scratch memory for the graph. This should only be called if scratch memory is ...
virtual ErrorCode getIOTensorName(AsciiChar const *&name, size_t const index) const noexcept=0
This function returns the name of a tensor for a given index.
virtual ErrorCode getIOTensorAddress(AsciiChar const *const tensorName, TypedArray &tensor) noexcept=0
This function gets the memory address for an user provided input tensor to the graph based on its nam...
virtual ~ITRTGraph() noexcept=default
A shallow destructor of ITRTGraph.
virtual ErrorCode getNbIOProfiles(int64_t &nb) const noexcept=0
This function returns the total number of IO tensor profiles for the current graph.
virtual ErrorCode getInputConsumedEvent(cudaEvent_t &event) const noexcept=0
This function retrieves the cudaEvent on the current graph that triggers when the input is fully cons...
virtual ErrorCode getIOTensorDescriptor(TensorDescriptor &desc, AsciiChar const *const tensorName) const noexcept=0
This function should return a TensorDescriptor which contains all the information about the tensor ba...
virtual ErrorCode clone(ITRTGraph *&graph, ISafeRecorder &recorder) noexcept=0
Specialized Graph shallow copy.
Structure to define the physical dimensions of a tensor with support for up to 9 dimensions.
Definition: NvInferSafeRuntime.h:240
int32_t nbDims
The rank (number of dimensions).
Definition: NvInferSafeRuntime.h:247
static constexpr int32_t MAX_DIMS
Definition: NvInferSafeRuntime.h:244
int64_t d[MAX_DIMS]
The extent of each dimension.
Definition: NvInferSafeRuntime.h:250
A standard_layout and trivially_copyable typed array that knows the data type and size it is holding.
Definition: NvInferSafeRuntime.h:52
TypedArray() noexcept
Definition: NvInferSafeRuntime.h:54
DataType getType() const noexcept
This method returns the current type of the data.
Definition: NvInferSafeRuntime.h:124
TypedArray & operator=(TypedArray const &) &=default
int32_t * getInt32() const noexcept
Retrieves the data as int32_t*. It should only be called when the current type is kINT32....
Definition: NvInferSafeRuntime.h:164
int8_t * getInt8() const noexcept
Retrieves the data as int8_t*. It should only be called when the current type is kINT8....
Definition: NvInferSafeRuntime.h:175
TypedArray(bool *ptr, uint64_t const bufferSize) noexcept
sets the data to a bool ptr. It also sets the current type to kBOOL
Definition: NvInferSafeRuntime.h:115
TypedArray(TypedArray &&)=default
bool * getBool() const noexcept
Retrieves the data as bool*. It should only be called when the current type is kBOOL....
Definition: NvInferSafeRuntime.h:197
half_t * getHalf() const noexcept
Retrieves the data as half_t*. It should only be called when the current type is kHALF....
Definition: NvInferSafeRuntime.h:142
void * getData() const noexcept
Retrieves the data regardless of the type.
Definition: NvInferSafeRuntime.h:214
TypedArray(half_t *ptr, uint64_t const bufferSize) noexcept
sets the data to a half_t ptr. It also sets the current type to kHALF
Definition: NvInferSafeRuntime.h:76
TypedArray(int32_t *ptr, uint64_t const bufferSize) noexcept
sets the data to a int32_t ptr. It also sets the current type to kINT32
Definition: NvInferSafeRuntime.h:92
uint8_t * getUint8() const noexcept
Retrieves the data as uint8_t*. It should only be called when the current type is kUINT8....
Definition: NvInferSafeRuntime.h:186
TypedArray(TypedArray const &)=default
~TypedArray() noexcept=default
uint64_t getSize() const noexcept
Retrieves the size of the array in bytes.
Definition: NvInferSafeRuntime.h:207
TypedArray & operator=(TypedArray &&) &=default
TypedArray(int64_t *ptr, uint64_t const bufferSize) noexcept
sets the data to a int64_t ptr. It also sets the current type to kINT64
Definition: NvInferSafeRuntime.h:84
int64_t * getInt64() const noexcept
Retrieves the data as int64_t*. It should only be called when the current type is kINT64....
Definition: NvInferSafeRuntime.h:153
TypedArray(int8_t *ptr, uint64_t const bufferSize) noexcept
sets the data to a int8_t ptr. It also sets the current type to kINT8
Definition: NvInferSafeRuntime.h:100
TypedArray(uint8_t *ptr, uint64_t const bufferSize) noexcept
sets the data to a uint8_t ptr. It also sets the current type to kUINT8
Definition: NvInferSafeRuntime.h:108
float * getFloat() const noexcept
Retrieves the data as float*. It should only be called when the current type is kFLOAT....
Definition: NvInferSafeRuntime.h:131
ErrorCode
Error codes that can be returned by TensorRT during execution.
Definition: NvInferRuntimeBase.h:317
TensorIOMode
Definition of tensor IO Mode.
Definition: NvInferRuntimeBase.h:664
char_t AsciiChar
Definition: NvInferRuntimeBase.h:116
DataType
The type of weights and tensors. The datatypes other than kBOOL, kINT32, and kINT64 are "activation d...
Definition: NvInferRuntimeBase.h:151
MemoryPlacement
Enum to describe the placement of the memory region.
Definition: NvInferSafeMemAllocator.h:49
@ kNONE
Invalid or unspecified placement (used for error checking)
bool operator==(PhysicalDims const &d0, PhysicalDims const &d1) noexcept
Definition: NvInferSafeRuntime.h:253
__half half_t
Definition: NvInferSafeRuntime.h:37
ErrorCode destroyTRTGraph(ITRTGraph *&graph) noexcept
Toplevel graph destructor.
ErrorCode createTRTGraph(ITRTGraph *&graph, void const *buffer, int64_t bufferSize, AsciiChar const *companionSoPath, ISafeRecorder &recorder, bool trtManagedScratch=true, ISafeMemAllocator *allocator=nullptr) noexcept
The C factory function which serves as an entry point to TRT that creates an instance of a ITRTGraph ...
bool operator!=(PhysicalDims const &d0, PhysicalDims const &d1) noexcept
Definition: NvInferSafeRuntime.h:259
Definition: NvInferConsistency.h:34
Holds information about runtime errors that occur during asynchronous kernel execution.
Definition: NvInferSafeRecorder.h:253
A simple record summarizing various properties of a network IO Tensor.
Definition: NvInferSafeRuntime.h:287
MemoryPlacement memPlacement
Enum to denote whether the Tensor memory is allocated on the GPU, CPU, or CPU_PINNED.
Definition: NvInferSafeRuntime.h:309
AsciiChar const * tensorName
Name of the IO Tensor.
Definition: NvInferSafeRuntime.h:289
uint64_t bytesPerComponent
The size of the tensor data type in bytes (4 for float and int32, 2 for half, 1 for int8)
Definition: NvInferSafeRuntime.h:299
DataType dataType
Definition: NvInferSafeRuntime.h:297
PhysicalDims strideOrder
The order in which the dimensions are laid out in memory.
Definition: NvInferSafeRuntime.h:311
Dims userShape
Definition: NvInferSafeRuntime.h:315
uint64_t sizeInBytes
Total size in bytes for the IO Tensor.
Definition: NvInferSafeRuntime.h:305
uint64_t componentsPerVector
The vector length (in scalars) for a vectorized tensor, 1 if the tensor is not vectorized.
Definition: NvInferSafeRuntime.h:301
int64_t vectorizedDim
The dimension index along which the tensor is vectorized, -1 if the tensor is not vectorized.
Definition: NvInferSafeRuntime.h:303
PhysicalDims shape
Definition: NvInferSafeRuntime.h:292
TensorIOMode ioMode
Enum to denote whether the Tensor is for input or output.
Definition: NvInferSafeRuntime.h:307
PhysicalDims stride
Stride vector for each element of the IO Tensor.
Definition: NvInferSafeRuntime.h:294

  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