TensorRT 11.1.0
NvInferSafeMemAllocator.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// Header file for the NVIDIA Safe Runtime Memory Allocator interface.
19// This file provides the user-implementable interface for the Memory Allocator feature in the NVIDIA Safe Runtime API.
20// It includes the necessary classes, functions, and definitions for creating and managing safe memory allocators,
21// including the ISafeMemAllocator interface.
22// Users should implement this interface to provide a custom memory allocator that will be used by the Safe Runtime
23// to allocate memory for both CPU and GPU resources. The allocator should handle memory allocation and deallocation
24// requests from the Safe Runtime, and ensure thread safety and proper memory management.
25// The implemented allocator will be responsible for allocating memory for various components of the Safe Runtime,
26// including tensors, plugins, and other internal data structures.
27
28#ifndef NV_INFER_SAFE_MEM_ALLOCATOR_H
29#define NV_INFER_SAFE_MEM_ALLOCATOR_H
30#include "NvInferSafeRecorder.h"
31#include <cstdint>
32
33namespace nvinfer2
34{
35namespace safe
36{
37class ISafeRecorder;
38
42constexpr uint64_t kMAXIMUM_ALLOC_SIZE{17179869184U};
43
48enum class MemoryPlacement : uint32_t
49{
51 kNONE = 0x0,
53 kMANAGED = 0x1,
55 kGPU = 0x2,
57 kCPU = 0x4,
59 kCPU_PINNED = 0x80
60};
61
66enum class MemoryUsage : uint32_t
67{
69 kGENERIC = 0x0,
71 kIMMUTABLE = 0x3,
73 kSCRATCH = 0x5,
75 kIOTENSOR = 0x6
76};
77
86{
87public:
120 virtual void* allocate(uint64_t const size, uint64_t const alignment, MemoryPlacement const flags,
121 MemoryUsage const usage, ISafeRecorder& recorder) noexcept = 0;
122
128 virtual ~ISafeMemAllocator() = default;
134 ISafeMemAllocator() = default;
135
164 virtual bool deallocate(void* const memory, MemoryPlacement const flags, ISafeRecorder& recorder) noexcept = 0;
165
166protected:
191};
192} // namespace safe
193} // namespace nvinfer2
194
195#endif /* NV_INFER_SAFE_MEM_ALLOCATOR_H */
Application-implemented class for controlling memory allocation on the GPU/CPU.
Definition: NvInferSafeMemAllocator.h:86
ISafeMemAllocator(ISafeMemAllocator const &)=default
Copy constructor (defaulted).
virtual bool deallocate(void *const memory, MemoryPlacement const flags, ISafeRecorder &recorder) noexcept=0
A thread-safe callback implemented by the application to handle release of GPU/CPU memory.
ISafeMemAllocator & operator=(ISafeMemAllocator &&) &=default
Move assignment operator (defaulted).
ISafeMemAllocator(ISafeMemAllocator &&)=default
Move constructor (defaulted).
ISafeMemAllocator & operator=(ISafeMemAllocator const &) &=default
Copy assignment operator (defaulted).
virtual ~ISafeMemAllocator()=default
Destructor for ISafeMemAllocator.
virtual void * allocate(uint64_t const size, uint64_t const alignment, MemoryPlacement const flags, MemoryUsage const usage, ISafeRecorder &recorder) noexcept=0
A thread-safe callback implemented by the application to handle acquisition of GPU/CPU memory.
ISafeMemAllocator()=default
Default constructor for ISafeMemAllocator.
Interface for extended recorder which allows error, warn, debug, or info messages to be recorded.
Definition: NvInferSafeRecorder.h:77
MemoryUsage
Enum to describe the usage of memory region.
Definition: NvInferSafeMemAllocator.h:67
@ kSCRATCH
Scratchpad memory per context used for intermediate tensors.
@ kIMMUTABLE
Memory (network weights) that is initialized once and can be shared across different graphs.
@ kIOTENSOR
Memory used for IO Tensors.
@ kGENERIC
Uncommitted memory usage.
MemoryPlacement
Enum to describe the placement of the memory region.
Definition: NvInferSafeMemAllocator.h:49
@ kMANAGED
CUDA managed memory (not used for safety)
@ kCPU
Regular (paged) host memory allocated via malloc, or aligned_alloc.
@ kGPU
Device memory allocated via cudaMalloc.
@ kCPU_PINNED
Page-locked host memory allocated via cudaHostAlloc, mappable to device (for zero-copy)
@ kNONE
Invalid or unspecified placement (used for error checking)
constexpr uint64_t kMAXIMUM_ALLOC_SIZE
Legacy constant (16 GiB) kept for TensorRT excessiveMemoryDetection test build compatibility.
Definition: NvInferSafeMemAllocator.h:42
Definition: NvInferConsistency.h:30

  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