9 #include "core/framework/allocator_stats.h"
11 #include "core/session/onnxruntime_c_api.h"
28 arena_extend_strategy(arena_extend_strategy),
29 initial_chunk_size_bytes(initial_chunk_size_bytes),
30 max_dead_bytes_per_chunk(max_dead_bytes_per_chunk),
31 initial_growth_chunk_size_bytes(initial_growth_chunk_size_bytes),
32 max_power_of_two_extend_bytes(max_power_of_two_extend_bytes) {}
42 namespace onnxruntime {
43 constexpr
const char*
CPU =
"Cpu";
44 constexpr
const char*
CUDA =
"Cuda";
46 constexpr
const char*
CANN =
"Cann";
48 constexpr
const char*
DML =
"DML";
49 constexpr
const char*
HIP =
"Hip";
59 namespace synchronize {
81 virtual void Free(
void* p) = 0;
95 virtual void GetStats(AllocatorStats* ) {
return; }
110 size_t* out) noexcept;
122 template <
size_t alignment>
131 ORT_THROW(
"Invalid size requested for allocation: ", nmemb,
" * ", size);
140 template <
size_t alignment>
144 ORT_THROW(
"Invalid size requested for allocation: ", nmemb,
" * ", size,
" with alignment ", alignment);
160 template <
typename T>
162 bool use_reserve =
false,
164 ValidateAllocator(allocator);
170 size_t alloc_size = count_or_bytes;
177 alloc_size = ValidatedCalcMemSizeForArray(count_or_bytes, size);
182 ValidateAllocation(p, alloc_size);
185 [allocator = std::move(allocator)](
T* p) {
196 template <
typename T>
198 ValidateAllocator(ort_allocator);
200 size_t alloc_size = count_or_bytes;
206 alloc_size = ValidatedCalcMemSizeForArray(count_or_bytes, size);
209 T* p =
static_cast<T*
>(ort_allocator->Alloc(ort_allocator, alloc_size));
210 ValidateAllocation(p, alloc_size);
213 [ort_allocator](
T* p) {
214 ort_allocator->Free(ort_allocator, p);
223 template <
typename T>
224 static void ValidateAllocator(
const T& allocator) {
228 static size_t ValidatedCalcMemSizeForArray(
size_t count,
size_t size) {
229 size_t alloc_size = 0;
231 ORT_THROW(
"Invalid size requested for allocation: ", count,
" * ", size);
237 static void ValidateAllocation(
void* p,
size_t size) {
240 ORT_ENFORCE(p !=
nullptr || size == 0,
"Memory allocation failed. Size=", size);
246 template <
size_t alignment>
248 return CalcMemSizeForArrayWithAlignment(nmemb, size, alignment, out);
257 void*
Alloc(
size_t size)
override;
258 void Free(
void* p)
override;
virtual void Free(void *p)=0
constexpr const char * WEBGPU_BUFFER
void * AllocatorDefaultAlloc(size_t size)
constexpr const char * OpenVINO_GPU
constexpr const char * CPU
virtual void GetStats(AllocatorStats *)
constexpr size_t kAllocAlignment
constexpr const char * CUDA_PINNED
GLsizei const GLfloat * value
std::function< void(Stream &, synchronize::Notification &)> WaitNotificationFn
void * AllocateBufferWithOptions(IAllocator &allocator, size_t size, bool use_reserve, Stream *stream, WaitNotificationFn wait_fn)
#define ORT_ENFORCE(condition,...)
constexpr const char * CUDA
constexpr const char * HIP_PINNED
const OrtMemoryInfo & Info() const
constexpr const char * HIP
int initial_growth_chunk_size_bytes
OrtArenaCfg(size_t max_mem, int arena_extend_strategy, int initial_chunk_size_bytes, int max_dead_bytes_per_chunk, int initial_growth_chunk_size_bytes, int64_t max_power_of_two_extend_bytes)
static bool CalcMemSizeForArrayWithAlignment(size_t nmemb, size_t size, size_t alignment, size_t *out) noexcept
int arena_extend_strategy
static IAllocatorUniquePtr< T > MakeUniquePtr(std::shared_ptr< IAllocator > allocator, size_t count_or_bytes, bool use_reserve=false, Stream *stream=nullptr, WaitNotificationFn wait_fn=nullptr)
std::unique_ptr< T, std::function< void(T *)>> IAllocatorUniquePtr
static bool CalcMemSizeForArray(size_t nmemb, size_t size, size_t *out) noexcept
void Free(void *p) override
virtual void * Reserve(size_t size)
std::map< OrtDevice, AllocatorPtr > AllocatorMap
void AllocatorDefaultFree(void *p)
constexpr const char * CANN
CPUAllocator(const OrtMemoryInfo &memory_info)
void * AllocArray(size_t nmemb, size_t size)
int64_t max_power_of_two_extend_bytes
constexpr const char * DML
std::shared_ptr< IAllocator > AllocatorPtr
int initial_chunk_size_bytes
constexpr const char * CANN_PINNED
void * Alloc(size_t size) override
virtual ~IAllocator()=default
void * AllocArrayWithAlignment(size_t nmemb, size_t size)
int max_dead_bytes_per_chunk
IAllocator(const OrtMemoryInfo &info)
virtual void * Alloc(size_t size)=0
static IAllocatorUniquePtr< T > MakeUniquePtrFromOrtAllocator(OrtAllocator *ort_allocator, size_t count_or_bytes)
constexpr const char * OpenVINO_CPU