mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-07-27 20:02:15 +00:00
Remove IDeviceAllocator class as it doesn't extend IAllocator in any way. (#5067)
This commit is contained in:
parent
5b6643cefb
commit
3207de276c
18 changed files with 122 additions and 138 deletions
|
|
@ -135,33 +135,21 @@ bool IAllocator::CalcMemSizeForArrayWithAlignment(size_t nmemb, size_t size, siz
|
|||
return CalcMemSizeForArrayWithAlignment(nmemb, size, alignment, out);
|
||||
}
|
||||
|
||||
/**
|
||||
The resource allocator on a physical device.
|
||||
This allocator will directly allocate resource from system call
|
||||
*/
|
||||
class IDeviceAllocator : public IAllocator {
|
||||
class CPUAllocator : public IAllocator {
|
||||
public:
|
||||
IDeviceAllocator(const OrtMemoryInfo& info) : IAllocator(info) {}
|
||||
~IDeviceAllocator() override = default;
|
||||
void* Alloc(size_t size) override = 0;
|
||||
void Free(void* p) override = 0;
|
||||
};
|
||||
explicit CPUAllocator(const OrtMemoryInfo& memory_info) : IAllocator(memory_info) {}
|
||||
|
||||
class CPUAllocator : public IDeviceAllocator {
|
||||
public:
|
||||
explicit CPUAllocator(const OrtMemoryInfo& memory_info) : IDeviceAllocator(memory_info) {}
|
||||
|
||||
CPUAllocator() : IDeviceAllocator(OrtMemoryInfo(CPU, OrtAllocatorType::OrtDeviceAllocator)) {}
|
||||
CPUAllocator() : IAllocator(OrtMemoryInfo(CPU, OrtAllocatorType::OrtDeviceAllocator)) {}
|
||||
|
||||
void* Alloc(size_t size) override;
|
||||
void Free(void* p) override;
|
||||
};
|
||||
|
||||
#if defined(USE_MIMALLOC_ARENA_ALLOCATOR)
|
||||
class MiMallocAllocator : public IDeviceAllocator {
|
||||
class MiMallocAllocator : public IAllocator {
|
||||
public:
|
||||
explicit MiMallocAllocator(const OrtMemoryInfo& memory_info) : IDeviceAllocator(memory_info) {}
|
||||
MiMallocAllocator() : IDeviceAllocator(OrtMemoryInfo(CPU, OrtAllocatorType::OrtDeviceAllocator)) {}
|
||||
explicit MiMallocAllocator(const OrtMemoryInfo& memory_info) : IAllocator(memory_info) {}
|
||||
MiMallocAllocator() : IAllocator(OrtMemoryInfo(CPU, OrtAllocatorType::OrtDeviceAllocator)) {}
|
||||
|
||||
void* Alloc(size_t size) override;
|
||||
void Free(void* p) override;
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ namespace onnxruntime {
|
|||
using namespace common;
|
||||
|
||||
AllocatorPtr CreateAllocator(const AllocatorCreationInfo& info) {
|
||||
auto device_allocator = std::unique_ptr<IDeviceAllocator>(info.device_alloc_factory(info.device_id));
|
||||
auto device_allocator = std::unique_ptr<IAllocator>(info.device_alloc_factory(info.device_id));
|
||||
|
||||
if (info.use_arena) {
|
||||
size_t max_mem = info.arena_cfg.max_mem == 0 ? BFCArena::DEFAULT_MAX_MEM : info.arena_cfg.max_mem;
|
||||
|
|
|
|||
|
|
@ -10,10 +10,10 @@
|
|||
|
||||
namespace onnxruntime {
|
||||
|
||||
using DeviceAllocatorFactory = std::function<std::unique_ptr<IDeviceAllocator>(OrtDevice::DeviceId)>;
|
||||
using AllocatorFactory = std::function<std::unique_ptr<IAllocator>(OrtDevice::DeviceId)>;
|
||||
|
||||
struct AllocatorCreationInfo {
|
||||
AllocatorCreationInfo(DeviceAllocatorFactory device_alloc_factory0,
|
||||
AllocatorCreationInfo(AllocatorFactory device_alloc_factory0,
|
||||
OrtDevice::DeviceId device_id0 = 0,
|
||||
bool use_arena0 = true,
|
||||
OrtArenaCfg arena_cfg0 = {0, -1, -1, -1})
|
||||
|
|
@ -23,7 +23,7 @@ struct AllocatorCreationInfo {
|
|||
arena_cfg(arena_cfg0) {
|
||||
}
|
||||
|
||||
DeviceAllocatorFactory device_alloc_factory;
|
||||
AllocatorFactory device_alloc_factory;
|
||||
OrtDevice::DeviceId device_id;
|
||||
bool use_arena;
|
||||
OrtArenaCfg arena_cfg;
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
#include <type_traits>
|
||||
|
||||
namespace onnxruntime {
|
||||
BFCArena::BFCArena(std::unique_ptr<IDeviceAllocator> resource_allocator,
|
||||
BFCArena::BFCArena(std::unique_ptr<IAllocator> resource_allocator,
|
||||
size_t total_memory,
|
||||
ArenaExtendStrategy arena_extend_strategy,
|
||||
int initial_chunk_size_bytes,
|
||||
|
|
|
|||
|
|
@ -59,7 +59,7 @@ class BFCArena : public IArenaAllocator {
|
|||
static const int DEFAULT_MAX_DEAD_BYTES_PER_CHUNK = 128 * 1024 * 1024;
|
||||
static const size_t DEFAULT_MAX_MEM = std::numeric_limits<size_t>::max();
|
||||
|
||||
BFCArena(std::unique_ptr<IDeviceAllocator> resource_allocator,
|
||||
BFCArena(std::unique_ptr<IAllocator> resource_allocator,
|
||||
size_t total_memory,
|
||||
ArenaExtendStrategy arena_extend_strategy = DEFAULT_ARENA_EXTEND_STRATEGY,
|
||||
int initial_chunk_size_bytes = DEFAULT_INITIAL_CHUNK_SIZE_BYTES,
|
||||
|
|
@ -433,7 +433,7 @@ class BFCArena : public IArenaAllocator {
|
|||
// The size of the current region allocation.
|
||||
SafeInt<size_t> curr_region_allocation_bytes_;
|
||||
|
||||
std::unique_ptr<IDeviceAllocator> device_allocator_;
|
||||
std::unique_ptr<IAllocator> device_allocator_;
|
||||
|
||||
mutable OrtMutex lock_;
|
||||
|
||||
|
|
|
|||
|
|
@ -3,48 +3,48 @@
|
|||
#include "core/framework/mimalloc_arena.h"
|
||||
|
||||
namespace onnxruntime {
|
||||
MiMallocArena::MiMallocArena(std::unique_ptr<IDeviceAllocator> resource_allocator,
|
||||
size_t total_memory)
|
||||
MiMallocArena::MiMallocArena(std::unique_ptr<IAllocator> resource_allocator,
|
||||
size_t total_memory)
|
||||
: info_(resource_allocator->Info().name, OrtAllocatorType::OrtArenaAllocator, resource_allocator->Info().device, resource_allocator->Info().id, resource_allocator->Info().mem_type) {
|
||||
stats_.bytes_limit = total_memory;
|
||||
}
|
||||
|
||||
void* MiMallocArena::Alloc(size_t size) {
|
||||
#if (MI_STAT>1)
|
||||
stats_.num_allocs++;
|
||||
#endif
|
||||
return mi_malloc(size);
|
||||
}
|
||||
|
||||
void MiMallocArena::Free(void* p) {
|
||||
mi_free(p);
|
||||
}
|
||||
|
||||
void* MiMallocArena::Reserve(size_t size) {
|
||||
return mi_malloc(size);
|
||||
}
|
||||
|
||||
// mimalloc only maintains stats when compiled under debug (which in turn sets MI_STAT)
|
||||
void MiMallocArena::GetStats(AllocatorStats* stats) {
|
||||
#if (MI_STAT>1)
|
||||
auto current_stats = mi_heap_get_default()->tld->stats;
|
||||
stats_.bytes_in_use = current_stats.malloc.current;
|
||||
stats_.total_allocated_bytes = current_stats.reserved.current;
|
||||
stats_.max_bytes_in_use = current_stats.reserved.peak;
|
||||
#endif
|
||||
*stats = stats_;
|
||||
}
|
||||
|
||||
size_t MiMallocArena::Used() const {
|
||||
#if (MI_STAT>1)
|
||||
return mi_heap_get_default()->tld->stats.malloc.current;
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
size_t MiMallocArena::AllocatedSize(const void* ptr) {
|
||||
return mi_usable_size(ptr);
|
||||
}
|
||||
stats_.bytes_limit = total_memory;
|
||||
}
|
||||
|
||||
void* MiMallocArena::Alloc(size_t size) {
|
||||
#if (MI_STAT > 1)
|
||||
stats_.num_allocs++;
|
||||
#endif
|
||||
return mi_malloc(size);
|
||||
}
|
||||
|
||||
void MiMallocArena::Free(void* p) {
|
||||
mi_free(p);
|
||||
}
|
||||
|
||||
void* MiMallocArena::Reserve(size_t size) {
|
||||
return mi_malloc(size);
|
||||
}
|
||||
|
||||
// mimalloc only maintains stats when compiled under debug (which in turn sets MI_STAT)
|
||||
void MiMallocArena::GetStats(AllocatorStats* stats) {
|
||||
#if (MI_STAT > 1)
|
||||
auto current_stats = mi_heap_get_default()->tld->stats;
|
||||
stats_.bytes_in_use = current_stats.malloc.current;
|
||||
stats_.total_allocated_bytes = current_stats.reserved.current;
|
||||
stats_.max_bytes_in_use = current_stats.reserved.peak;
|
||||
#endif
|
||||
*stats = stats_;
|
||||
}
|
||||
|
||||
size_t MiMallocArena::Used() const {
|
||||
#if (MI_STAT > 1)
|
||||
return mi_heap_get_default()->tld->stats.malloc.current;
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
size_t MiMallocArena::AllocatedSize(const void* ptr) {
|
||||
return mi_usable_size(ptr);
|
||||
}
|
||||
} // namespace onnxruntime
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -5,32 +5,32 @@
|
|||
|
||||
namespace onnxruntime {
|
||||
class MiMallocArena : public IArenaAllocator {
|
||||
public:
|
||||
MiMallocArena(std::unique_ptr<IDeviceAllocator> resource_allocator, size_t total_memory);
|
||||
public:
|
||||
MiMallocArena(std::unique_ptr<IAllocator> resource_allocator, size_t total_memory);
|
||||
|
||||
void* Alloc(size_t size) override;
|
||||
void* Alloc(size_t size) override;
|
||||
|
||||
void Free(void* p) override;
|
||||
void Free(void* p) override;
|
||||
|
||||
// mimalloc only maintains stats when compiled under debug, or when MI_STAT >= 2
|
||||
void GetStats(AllocatorStats* stats);
|
||||
// mimalloc only maintains stats when compiled under debug, or when MI_STAT >= 2
|
||||
void GetStats(AllocatorStats* stats);
|
||||
|
||||
void* Reserve(size_t size) override;
|
||||
void* Reserve(size_t size) override;
|
||||
|
||||
size_t Used() const override;
|
||||
size_t Used() const override;
|
||||
|
||||
size_t Max() const override {
|
||||
return stats_.bytes_limit;
|
||||
}
|
||||
size_t Max() const override {
|
||||
return stats_.bytes_limit;
|
||||
}
|
||||
|
||||
const OrtMemoryInfo& Info() const override {
|
||||
return info_;
|
||||
}
|
||||
const OrtMemoryInfo& Info() const override {
|
||||
return info_;
|
||||
}
|
||||
|
||||
size_t AllocatedSize(const void* ptr);
|
||||
size_t AllocatedSize(const void* ptr);
|
||||
|
||||
OrtMemoryInfo info_;
|
||||
AllocatorStats stats_;
|
||||
OrtMemoryInfo info_;
|
||||
AllocatorStats stats_;
|
||||
};
|
||||
} // namespace onnxruntime
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -44,8 +44,8 @@ namespace onnxruntime {
|
|||
|
||||
ProviderHost* g_host{};
|
||||
|
||||
struct Provider_IAllocator_Impl : Provider_IAllocator {
|
||||
Provider_IAllocator_Impl(AllocatorPtr p) : Provider_IAllocator{p->Info()}, p_{p} {}
|
||||
struct Provider_AllocatorPtr_Impl : Provider_IAllocator {
|
||||
Provider_AllocatorPtr_Impl(AllocatorPtr p) : Provider_IAllocator{p->Info()}, p_{p} {}
|
||||
|
||||
void* Alloc(size_t size) override { return p_->Alloc(size); }
|
||||
void Free(void* p) override { return p_->Free(p); }
|
||||
|
|
@ -53,9 +53,9 @@ struct Provider_IAllocator_Impl : Provider_IAllocator {
|
|||
AllocatorPtr p_;
|
||||
};
|
||||
|
||||
// This is really a IDeviceAllocator, but we wrap it with this class to make it into a Provider_IDeviceAllocator
|
||||
struct Provider_IDeviceAllocator_Impl : Provider_IDeviceAllocator {
|
||||
Provider_IDeviceAllocator_Impl(std::unique_ptr<IDeviceAllocator> p) : Provider_IDeviceAllocator{p->Info()}, p_{std::move(p)} {}
|
||||
// This is really a IAllocator, but we wrap it with this class to make it into a Provider_IAllocator
|
||||
struct Provider_IAllocator_Impl : Provider_IAllocator {
|
||||
Provider_IAllocator_Impl(std::unique_ptr<IAllocator> p) : Provider_IAllocator{p->Info()}, p_{std::move(p)} {}
|
||||
|
||||
void* Alloc(size_t size) override { return p_->Alloc(size); }
|
||||
void Free(void* p) override { return p_->Free(p); }
|
||||
|
|
@ -64,19 +64,19 @@ struct Provider_IDeviceAllocator_Impl : Provider_IDeviceAllocator {
|
|||
|
||||
bool IsProviderInterface() const override { return false; }
|
||||
|
||||
std::unique_ptr<IDeviceAllocator> p_;
|
||||
std::unique_ptr<IAllocator> p_;
|
||||
};
|
||||
|
||||
// This is really a Provider_IDeviceAllocator, but we wrap it with this class to make it into a IDeviceAllocator
|
||||
struct ProviderAllocator : IDeviceAllocator {
|
||||
ProviderAllocator(std::shared_ptr<Provider_IDeviceAllocator> p) : IDeviceAllocator{p->memory_info_}, p_{std::move(p)} {}
|
||||
// This is really a Provider_IAllocator, but we wrap it with this class to make it into a IAllocator
|
||||
struct ProviderAllocator : IAllocator {
|
||||
ProviderAllocator(std::shared_ptr<Provider_IAllocator> p) : IAllocator{p->memory_info_}, p_{std::move(p)} {}
|
||||
|
||||
void* Alloc(size_t size) override { return p_->Alloc(size); }
|
||||
void Free(void* p) override { return p_->Free(p); }
|
||||
|
||||
FencePtr CreateFence(const SessionState* session_state) override { return p_->CreateFence(reinterpret_cast<const Provider_SessionState*>(session_state)); }
|
||||
|
||||
std::shared_ptr<Provider_IDeviceAllocator> p_;
|
||||
std::shared_ptr<Provider_IAllocator> p_;
|
||||
};
|
||||
|
||||
struct IDataTransfer_Wrapper : IDataTransfer {
|
||||
|
|
@ -195,14 +195,14 @@ struct Provider_IExecutionProvider_Router_Impl : Provider_IExecutionProvider_Rou
|
|||
}
|
||||
|
||||
Provider_AllocatorPtr Provider_GetAllocator(int id, OrtMemType mem_type) const override {
|
||||
return std::make_shared<Provider_IAllocator_Impl>(IExecutionProvider::GetAllocator(id, mem_type));
|
||||
return std::make_shared<Provider_AllocatorPtr_Impl>(IExecutionProvider::GetAllocator(id, mem_type));
|
||||
}
|
||||
|
||||
AllocatorPtr GetAllocator(int id, OrtMemType mem_type) const override {
|
||||
auto allocator = outer_->Provider_GetAllocator(id, mem_type);
|
||||
if (!allocator)
|
||||
return nullptr;
|
||||
return static_cast<Provider_IAllocator_Impl*>(allocator.get())->p_;
|
||||
return static_cast<Provider_AllocatorPtr_Impl*>(allocator.get())->p_;
|
||||
}
|
||||
|
||||
std::unique_ptr<Provider_IDataTransfer> Provider_GetDataTransfer() const override {
|
||||
|
|
@ -218,7 +218,7 @@ struct Provider_IExecutionProvider_Router_Impl : Provider_IExecutionProvider_Rou
|
|||
}
|
||||
|
||||
void Provider_InsertAllocator(Provider_AllocatorPtr allocator) override {
|
||||
IExecutionProvider::InsertAllocator(static_cast<Provider_IAllocator_Impl*>(allocator.get())->p_);
|
||||
IExecutionProvider::InsertAllocator(static_cast<Provider_AllocatorPtr_Impl*>(allocator.get())->p_);
|
||||
}
|
||||
|
||||
const logging::Logger* GetLogger() const override { return IExecutionProvider::GetLogger(); }
|
||||
|
|
@ -235,14 +235,14 @@ struct ProviderHostImpl : ProviderHost {
|
|||
|
||||
Provider_AllocatorPtr CreateAllocator(const Provider_AllocatorCreationInfo& info) override {
|
||||
AllocatorCreationInfo info_real{
|
||||
[&info](int value) -> std::unique_ptr<IDeviceAllocator> {
|
||||
[&info](int value) -> std::unique_ptr<IAllocator> {
|
||||
auto allocator = info.factory(value);
|
||||
// If the allocator is a provider interface, we need to wrap it with ProviderAllocator to turn it into an IDeviceAllocator
|
||||
// Otherwise it's really a Provider_IDeviceAllocator_Impl, so we can just unwrap it to get back to the IDeviceAllocator inside
|
||||
if (allocator->IsProviderInterface())
|
||||
return onnxruntime::make_unique<ProviderAllocator>(std::move(allocator));
|
||||
else
|
||||
return std::move(static_cast<Provider_IDeviceAllocator_Impl*>(&*allocator)->p_);
|
||||
return std::move(static_cast<Provider_IAllocator_Impl*>(&*allocator)->p_);
|
||||
},
|
||||
info.device_id,
|
||||
info.use_arena,
|
||||
|
|
@ -250,12 +250,12 @@ struct ProviderHostImpl : ProviderHost {
|
|||
|
||||
// info_real will always return a unique_ptr to an IAllocator, which might be a native IAllocator or a provider interface wrapped by ProviderAllocator.
|
||||
// Either way we wrap it in a Provider_IAllocator_Impl to be unwrapped by Provider_InsertAllocator
|
||||
return std::make_shared<Provider_IAllocator_Impl>(onnxruntime::CreateAllocator(info_real));
|
||||
return std::make_shared<Provider_AllocatorPtr_Impl>(onnxruntime::CreateAllocator(info_real));
|
||||
}
|
||||
|
||||
std::unique_ptr<Provider_IDeviceAllocator> CreateCPUAllocator(
|
||||
std::unique_ptr<Provider_IAllocator> CreateCPUAllocator(
|
||||
const OrtMemoryInfo& memory_info) override {
|
||||
return onnxruntime::make_unique<Provider_IDeviceAllocator_Impl>(
|
||||
return onnxruntime::make_unique<Provider_IAllocator_Impl>(
|
||||
onnxruntime::make_unique<CPUAllocator>(memory_info));
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -7,10 +7,10 @@
|
|||
|
||||
namespace onnxruntime {
|
||||
|
||||
class CUDAAllocator : public IDeviceAllocator {
|
||||
class CUDAAllocator : public IAllocator {
|
||||
public:
|
||||
CUDAAllocator(OrtDevice::DeviceId device_id, const char* name)
|
||||
: IDeviceAllocator(
|
||||
: IAllocator(
|
||||
OrtMemoryInfo(name, OrtAllocatorType::OrtDeviceAllocator,
|
||||
OrtDevice(OrtDevice::GPU, OrtDevice::MemType::DEFAULT, device_id),
|
||||
device_id, OrtMemTypeDefault)) {}
|
||||
|
|
@ -23,10 +23,10 @@ class CUDAAllocator : public IDeviceAllocator {
|
|||
};
|
||||
|
||||
//TODO: add a default constructor
|
||||
class CUDAPinnedAllocator : public IDeviceAllocator {
|
||||
class CUDAPinnedAllocator : public IAllocator {
|
||||
public:
|
||||
CUDAPinnedAllocator(OrtDevice::DeviceId device_id, const char* name)
|
||||
: IDeviceAllocator(
|
||||
: IAllocator(
|
||||
OrtMemoryInfo(name, OrtAllocatorType::OrtDeviceAllocator,
|
||||
OrtDevice(OrtDevice::CPU, OrtDevice::MemType::CUDA_PINNED, device_id),
|
||||
device_id, OrtMemTypeCPUOutput)) {}
|
||||
|
|
|
|||
|
|
@ -226,7 +226,7 @@ namespace Dml
|
|||
}
|
||||
|
||||
CPUAllocator::CPUAllocator(OrtMemType memType)
|
||||
: onnxruntime::IDeviceAllocator(
|
||||
: onnxruntime::IAllocator(
|
||||
OrtMemoryInfo("DML CPU",
|
||||
OrtAllocatorType::OrtDeviceAllocator,
|
||||
OrtDevice(OrtDevice::CPU, OrtDevice::MemType::DEFAULT, 0),
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@
|
|||
namespace Dml
|
||||
{
|
||||
|
||||
class CPUAllocator : public onnxruntime::IDeviceAllocator
|
||||
class CPUAllocator : public onnxruntime::IAllocator
|
||||
{
|
||||
public:
|
||||
explicit CPUAllocator(OrtMemType memType);
|
||||
|
|
|
|||
|
|
@ -7,10 +7,10 @@
|
|||
|
||||
namespace onnxruntime {
|
||||
|
||||
class HIPAllocator : public IDeviceAllocator {
|
||||
class HIPAllocator : public IAllocator {
|
||||
public:
|
||||
HIPAllocator(int device_id, const char* name)
|
||||
: IDeviceAllocator(
|
||||
: IAllocator(
|
||||
OrtMemoryInfo(name, OrtAllocatorType::OrtDeviceAllocator,
|
||||
OrtDevice(OrtDevice::GPU, OrtDevice::MemType::DEFAULT, device_id),
|
||||
device_id, OrtMemTypeDefault)) {}
|
||||
|
|
@ -24,10 +24,10 @@ class HIPAllocator : public IDeviceAllocator {
|
|||
};
|
||||
|
||||
//TODO: add a default constructor
|
||||
class HIPPinnedAllocator : public IDeviceAllocator {
|
||||
class HIPPinnedAllocator : public IAllocator {
|
||||
public:
|
||||
HIPPinnedAllocator(int device_id, const char* name)
|
||||
: IDeviceAllocator(
|
||||
: IAllocator(
|
||||
OrtMemoryInfo(name, OrtAllocatorType::OrtDeviceAllocator,
|
||||
OrtDevice(OrtDevice::CPU, OrtDevice::MemType::HIP_PINNED, device_id),
|
||||
device_id, OrtMemTypeCPUOutput)) {}
|
||||
|
|
|
|||
|
|
@ -103,7 +103,7 @@ class DataTypeImpl {
|
|||
template <typename T>
|
||||
using IAllocatorUniquePtr = std::unique_ptr<T, std::function<void(T*)>>;
|
||||
|
||||
std::unique_ptr<Provider_IDeviceAllocator> Provider_CreateCPUAllocator(const OrtMemoryInfo& memory_info);
|
||||
std::unique_ptr<Provider_IAllocator> Provider_CreateCPUAllocator(const OrtMemoryInfo& memory_info);
|
||||
Provider_AllocatorPtr CreateAllocator(const Provider_AllocatorCreationInfo& info);
|
||||
|
||||
std::string GetEnvironmentVar(const std::string& var_name);
|
||||
|
|
|
|||
|
|
@ -142,7 +142,7 @@ Provider_AllocatorPtr CreateAllocator(Provider_AllocatorCreationInfo info) {
|
|||
return g_host->CreateAllocator(info);
|
||||
}
|
||||
|
||||
std::unique_ptr<Provider_IDeviceAllocator> Provider_CreateCPUAllocator(const OrtMemoryInfo& info) {
|
||||
std::unique_ptr<Provider_IAllocator> Provider_CreateCPUAllocator(const OrtMemoryInfo& info) {
|
||||
return g_host->CreateCPUAllocator(info);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -145,16 +145,12 @@ struct Provider_IAllocator {
|
|||
void operator=(const Provider_IAllocator&) = delete;
|
||||
};
|
||||
|
||||
struct Provider_IDeviceAllocator : Provider_IAllocator {
|
||||
Provider_IDeviceAllocator(const OrtMemoryInfo& info) : Provider_IAllocator{info} {}
|
||||
};
|
||||
|
||||
using Provider_AllocatorPtr = std::shared_ptr<Provider_IAllocator>;
|
||||
using Provider_DeviceAllocatorFactory = std::function<std::unique_ptr<Provider_IDeviceAllocator>(int)>;
|
||||
using Provider_AllocatorFactory = std::function<std::unique_ptr<Provider_IAllocator>(int)>;
|
||||
|
||||
using DeviceId = int16_t;
|
||||
struct Provider_AllocatorCreationInfo {
|
||||
Provider_AllocatorCreationInfo(Provider_DeviceAllocatorFactory device_alloc_factory0,
|
||||
Provider_AllocatorCreationInfo(Provider_AllocatorFactory device_alloc_factory0,
|
||||
DeviceId device_id0 = 0,
|
||||
bool use_arena0 = true,
|
||||
OrtArenaCfg arena_cfg0 = {0, -1, -1, -1})
|
||||
|
|
@ -164,7 +160,7 @@ struct Provider_AllocatorCreationInfo {
|
|||
arena_cfg(arena_cfg0) {
|
||||
}
|
||||
|
||||
Provider_DeviceAllocatorFactory factory;
|
||||
Provider_AllocatorFactory factory;
|
||||
DeviceId device_id;
|
||||
bool use_arena;
|
||||
OrtArenaCfg arena_cfg;
|
||||
|
|
@ -288,7 +284,7 @@ struct ProviderHost {
|
|||
|
||||
virtual logging::Logger* LoggingManager_GetDefaultLogger() = 0;
|
||||
|
||||
virtual std::unique_ptr<Provider_IDeviceAllocator> CreateCPUAllocator(const OrtMemoryInfo& memory_info) = 0;
|
||||
virtual std::unique_ptr<Provider_IAllocator> CreateCPUAllocator(const OrtMemoryInfo& memory_info) = 0;
|
||||
|
||||
virtual std::unique_ptr<Provider_IExecutionProvider_Router> Create_IExecutionProvider_Router(Provider_IExecutionProvider* outer, const std::string& type) = 0;
|
||||
|
||||
|
|
|
|||
|
|
@ -5,10 +5,10 @@
|
|||
|
||||
namespace onnxruntime {
|
||||
|
||||
class CUDAAllocator : public Provider_IDeviceAllocator {
|
||||
class CUDAAllocator : public Provider_IAllocator {
|
||||
public:
|
||||
CUDAAllocator(OrtDevice::DeviceId device_id, const char* name)
|
||||
: Provider_IDeviceAllocator(
|
||||
: Provider_IAllocator(
|
||||
OrtMemoryInfo(name, OrtAllocatorType::OrtDeviceAllocator,
|
||||
OrtDevice(OrtDevice::GPU, OrtDevice::MemType::DEFAULT, device_id),
|
||||
device_id, OrtMemTypeDefault)) {}
|
||||
|
|
@ -21,10 +21,10 @@ class CUDAAllocator : public Provider_IDeviceAllocator {
|
|||
};
|
||||
|
||||
//TODO: add a default constructor
|
||||
class CUDAPinnedAllocator : public Provider_IDeviceAllocator {
|
||||
class CUDAPinnedAllocator : public Provider_IAllocator {
|
||||
public:
|
||||
CUDAPinnedAllocator(OrtDevice::DeviceId device_id, const char* name)
|
||||
: Provider_IDeviceAllocator(
|
||||
: Provider_IAllocator(
|
||||
OrtMemoryInfo(name, OrtAllocatorType::OrtDeviceAllocator,
|
||||
OrtDevice(OrtDevice::CPU, OrtDevice::MemType::CUDA_PINNED, device_id),
|
||||
device_id, OrtMemTypeCPUOutput)) {}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ namespace test {
|
|||
// Dummy Arena which just call underline device allocator directly.
|
||||
class DummyArena : public IArenaAllocator {
|
||||
public:
|
||||
explicit DummyArena(std::unique_ptr<IDeviceAllocator> resource_allocator)
|
||||
explicit DummyArena(std::unique_ptr<IAllocator> resource_allocator)
|
||||
: IArenaAllocator(OrtMemoryInfo(resource_allocator->Info().name,
|
||||
OrtAllocatorType::OrtArenaAllocator,
|
||||
resource_allocator->Info().device,
|
||||
|
|
@ -49,7 +49,7 @@ class DummyArena : public IArenaAllocator {
|
|||
private:
|
||||
ORT_DISALLOW_COPY_ASSIGNMENT_AND_MOVE(DummyArena);
|
||||
|
||||
std::unique_ptr<IDeviceAllocator> allocator_;
|
||||
std::unique_ptr<IAllocator> allocator_;
|
||||
};
|
||||
|
||||
static std::string GetAllocatorId(const std::string& name, const int id, const bool isArena) {
|
||||
|
|
@ -63,7 +63,7 @@ static std::string GetAllocatorId(const std::string& name, const int id, const b
|
|||
}
|
||||
|
||||
static Status RegisterAllocator(std::unordered_map<std::string, AllocatorPtr>& map,
|
||||
std::unique_ptr<IDeviceAllocator> allocator, size_t /*memory_limit*/,
|
||||
std::unique_ptr<IAllocator> allocator, size_t /*memory_limit*/,
|
||||
bool use_arena) {
|
||||
auto& info = allocator->Info();
|
||||
auto allocator_id = GetAllocatorId(info.name, info.id, use_arena);
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ static void CheckStats(BFCArena* a, int64_t num_allocs, int64_t bytes_in_use,
|
|||
}
|
||||
|
||||
TEST(BFCArenaTest, NoDups) {
|
||||
BFCArena a(std::unique_ptr<IDeviceAllocator>(new CPUAllocator()), 1 << 30);
|
||||
BFCArena a(std::unique_ptr<IAllocator>(new CPUAllocator()), 1 << 30);
|
||||
CheckStats(&a, 0, 0, 0, 0);
|
||||
|
||||
// Allocate a lot of raw pointers
|
||||
|
|
@ -48,7 +48,7 @@ TEST(BFCArenaTest, NoDups) {
|
|||
}
|
||||
|
||||
TEST(BFCArenaTest, AllocationsAndDeallocations) {
|
||||
BFCArena a(std::unique_ptr<IDeviceAllocator>(new CPUAllocator()), 1 << 30);
|
||||
BFCArena a(std::unique_ptr<IAllocator>(new CPUAllocator()), 1 << 30);
|
||||
// Allocate 256 raw pointers of sizes between 100 bytes and about a meg
|
||||
std::srand(static_cast<unsigned int>(std::time(nullptr)));
|
||||
|
||||
|
|
@ -104,7 +104,7 @@ TEST(BFCArenaTest, AllocationsAndDeallocations) {
|
|||
}
|
||||
|
||||
TEST(BFCArenaTest, ExerciseCoalescing) {
|
||||
BFCArena a(std::unique_ptr<IDeviceAllocator>(new CPUAllocator()), 1 << 30);
|
||||
BFCArena a(std::unique_ptr<IAllocator>(new CPUAllocator()), 1 << 30);
|
||||
CheckStats(&a, 0, 0, 0, 0);
|
||||
|
||||
void* first_ptr = a.Alloc(4096);
|
||||
|
|
@ -139,13 +139,13 @@ TEST(BFCArenaTest, ExerciseCoalescing) {
|
|||
}
|
||||
|
||||
TEST(BFCArenaTest, AllocateZeroBufSize) {
|
||||
BFCArena a(std::unique_ptr<IDeviceAllocator>(new CPUAllocator()), 1 << 30);
|
||||
BFCArena a(std::unique_ptr<IAllocator>(new CPUAllocator()), 1 << 30);
|
||||
void* ptr = a.Alloc(0);
|
||||
EXPECT_EQ(nullptr, ptr);
|
||||
}
|
||||
|
||||
TEST(BFCArenaTest, AllocatedVsRequested) {
|
||||
BFCArena a(std::unique_ptr<IDeviceAllocator>(new CPUAllocator()), 1 << 30);
|
||||
BFCArena a(std::unique_ptr<IAllocator>(new CPUAllocator()), 1 << 30);
|
||||
void* t1 = a.Alloc(4);
|
||||
EXPECT_EQ(4u, a.RequestedSize(t1));
|
||||
EXPECT_EQ(256u, a.AllocatedSize(t1));
|
||||
|
|
@ -165,7 +165,7 @@ void TestCustomMemoryLimit_ProcessException(const OnnxRuntimeException& ex) {
|
|||
TEST(BFCArenaTest, TestCustomMemoryLimit) {
|
||||
{
|
||||
// Configure a 1MiB byte limit
|
||||
BFCArena a(std::unique_ptr<IDeviceAllocator>(new CPUAllocator()), 1 << 20);
|
||||
BFCArena a(std::unique_ptr<IAllocator>(new CPUAllocator()), 1 << 20);
|
||||
|
||||
void* first_ptr = a.Alloc(sizeof(float) * (1 << 6));
|
||||
EXPECT_NE(nullptr, first_ptr);
|
||||
|
|
@ -189,7 +189,7 @@ TEST(BFCArenaTest, TestCustomMemoryLimit) {
|
|||
{
|
||||
// allow for the maximum amount of memory less 5MiB
|
||||
constexpr size_t available = std::numeric_limits<size_t>::max() - (5 * 1024 * 1024);
|
||||
BFCArena b(std::unique_ptr<IDeviceAllocator>(new CPUAllocator()), available,
|
||||
BFCArena b(std::unique_ptr<IAllocator>(new CPUAllocator()), available,
|
||||
ArenaExtendStrategy::kSameAsRequested); // need this strategy. kNextPowerOfTwo would overflow size_t
|
||||
|
||||
void* first_ptr = b.Alloc(sizeof(float) * (1 << 6));
|
||||
|
|
@ -215,7 +215,7 @@ TEST(BFCArenaTest, TestCustomMemoryLimit) {
|
|||
|
||||
TEST(BFCArenaTest, AllocationsAndDeallocationsWithGrowth) {
|
||||
// Max of 2GiB, but starts out small.
|
||||
BFCArena a(std::unique_ptr<IDeviceAllocator>(new CPUAllocator()), 1LL << 31);
|
||||
BFCArena a(std::unique_ptr<IAllocator>(new CPUAllocator()), 1LL << 31);
|
||||
|
||||
// Allocate 10 raw pointers of sizes between 100 bytes and about
|
||||
// 64 megs.
|
||||
|
|
@ -273,7 +273,7 @@ TEST(BFCArenaTest, AllocationsAndDeallocationsWithGrowth) {
|
|||
|
||||
TEST(BFCArenaTest, TestReserve) {
|
||||
// Configure a 1MiB byte limit
|
||||
BFCArena a(std::unique_ptr<IDeviceAllocator>(new CPUAllocator()), 1 << 30);
|
||||
BFCArena a(std::unique_ptr<IAllocator>(new CPUAllocator()), 1 << 30);
|
||||
|
||||
void* first_ptr = a.Alloc(sizeof(float) * (1 << 6));
|
||||
void* second_ptr = a.Reserve(sizeof(float) * (1 << 20));
|
||||
|
|
|
|||
Loading…
Reference in a new issue