| |
| |
|
|
| #pragma once |
|
|
| #include "core/common/common.h" |
| #include "core/framework/allocator_stats.h" |
| #include "core/session/onnxruntime_c_api.h" |
| #include "ortdevice.h" |
| #include "ortmemoryinfo.h" |
|
|
| |
| |
| struct OrtArenaCfg { |
| OrtArenaCfg() : max_mem(0), |
| arena_extend_strategy(-1), |
| initial_chunk_size_bytes(-1), |
| max_dead_bytes_per_chunk(-1), |
| initial_growth_chunk_size_bytes(-1) {} |
| 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) |
| : max_mem(max_mem), |
| arena_extend_strategy(arena_extend_strategy), |
| initial_chunk_size_bytes(initial_chunk_size_bytes), |
| max_dead_bytes_per_chunk(max_dead_bytes_per_chunk), |
| initial_growth_chunk_size_bytes(initial_growth_chunk_size_bytes) {} |
|
|
| 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; |
| }; |
|
|
| namespace onnxruntime { |
| constexpr const char* CPU = "Cpu"; |
| constexpr const char* CUDA = "Cuda"; |
| constexpr const char* CUDA_PINNED = "CudaPinned"; |
| constexpr const char* CANN = "Cann"; |
| constexpr const char* CANN_PINNED = "CannPinned"; |
| constexpr const char* DML = "DML"; |
| constexpr const char* HIP = "Hip"; |
| constexpr const char* HIP_PINNED = "HipPinned"; |
| constexpr const char* OpenVINO_CPU = "OpenVINO_CPU"; |
| constexpr const char* OpenVINO_GPU = "OpenVINO_GPU"; |
|
|
| constexpr size_t kAllocAlignment = 256; |
|
|
| class IAllocator; |
| class Stream; |
| namespace synchronize { |
| class Notification; |
| } |
| using WaitNotificationFn = std::function<void(Stream&, synchronize::Notification&)>; |
| void* AllocateBufferWithOptions(IAllocator& allocator, size_t size, bool use_reserve, Stream* stream, WaitNotificationFn wait_fn); |
|
|
| template <typename T> |
| using IAllocatorUniquePtr = std::unique_ptr<T, std::function<void(T*)>>; |
|
|
| class IAllocator { |
| public: |
| IAllocator(const OrtMemoryInfo& info) : memory_info_(info) {} |
| virtual ~IAllocator() = default; |
| |
| |
| |
| virtual void* Alloc(size_t size) = 0; |
|
|
| virtual void Free(void* p) = 0; |
|
|
| |
| |
| |
| |
| |
| |
| |
| virtual void* Reserve(size_t size) { return Alloc(size); } |
|
|
| const OrtMemoryInfo& Info() const { return memory_info_; }; |
|
|
| |
| virtual void GetStats(AllocatorStats* ) { return; } |
|
|
| static bool CalcMemSizeForArray(size_t nmemb, size_t size, size_t* out) noexcept { |
| return CalcMemSizeForArrayWithAlignment(nmemb, size, 0, out); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| [[nodiscard]] static bool CalcMemSizeForArrayWithAlignment(size_t nmemb, size_t size, size_t alignment, size_t* out) noexcept; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| template <size_t alignment> |
| [[nodiscard]] static bool CalcMemSizeForArrayWithAlignment(size_t nmemb, size_t size, size_t* out) noexcept; |
|
|
| |
| |
| |
| void* AllocArray(size_t nmemb, size_t size) { |
| size_t len; |
| if (!CalcMemSizeForArray(nmemb, size, &len)) |
| return nullptr; |
| return Alloc(len); |
| } |
|
|
| |
| |
| |
| template <size_t alignment> |
| void* AllocArrayWithAlignment(size_t nmemb, size_t size) { |
| size_t len; |
| if (!CalcMemSizeForArrayWithAlignment(nmemb, size, alignment, &len)) |
| return nullptr; |
| return Alloc(len); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| template <typename T> |
| 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) { |
| if (allocator == nullptr) return nullptr; |
| |
| |
| |
|
|
| size_t alloc_size = count_or_bytes; |
|
|
| |
| if constexpr (!std::is_void<T>::value) { |
| |
| |
| if (!CalcMemSizeForArray( |
| count_or_bytes, sizeof(typename std::conditional<std::is_void<T>::value, void*, T>::type), &alloc_size)) { |
| return nullptr; |
| } |
| } |
|
|
| |
| T* p = static_cast<T*>(AllocateBufferWithOptions(*allocator, alloc_size, use_reserve, stream, std::move(wait_fn))); |
| return IAllocatorUniquePtr<T>{ |
| p, |
| [allocator = std::move(allocator)](T* p) { allocator->Free(p); }}; |
| } |
|
|
| private: |
| OrtMemoryInfo memory_info_; |
| }; |
|
|
| template <size_t alignment> |
| bool IAllocator::CalcMemSizeForArrayWithAlignment(size_t nmemb, size_t size, size_t* out) noexcept { |
| return CalcMemSizeForArrayWithAlignment(nmemb, size, alignment, out); |
| } |
|
|
| class CPUAllocator : public IAllocator { |
| public: |
| explicit CPUAllocator(const OrtMemoryInfo& memory_info) : IAllocator(memory_info) {} |
|
|
| CPUAllocator() : IAllocator(OrtMemoryInfo(CPU, OrtAllocatorType::OrtDeviceAllocator)) {} |
|
|
| void* Alloc(size_t size) override; |
| void Free(void* p) override; |
| }; |
|
|
| using AllocatorPtr = std::shared_ptr<IAllocator>; |
|
|
| void* AllocatorDefaultAlloc(size_t size); |
| void AllocatorDefaultFree(void* p); |
| } |
|
|