#pragma once
#include <executorch/runtime/core/hierarchical_allocator.h>
#include <executorch/runtime/core/memory_allocator.h>
#include <executorch/runtime/core/portable_type/device.h>
#include <executorch/runtime/core/span.h>
namespace executorch {
namespace runtime {
class MemoryManager final {
public:
explicit MemoryManager(
MemoryAllocator* method_allocator,
HierarchicalAllocator* planned_memory = nullptr,
MemoryAllocator* temp_allocator = nullptr)
: method_allocator_(method_allocator),
planned_memory_(planned_memory),
temp_allocator_(temp_allocator) {
ET_CHECK_MSG(
method_allocator != temp_allocator,
"method allocator cannot be the same as temp allocator");
}
ET_DEPRECATED MemoryManager(
MemoryAllocator* constant_allocator,
HierarchicalAllocator* non_constant_allocator,
MemoryAllocator* runtime_allocator,
MemoryAllocator* temporary_allocator)
: MemoryManager(
runtime_allocator,
non_constant_allocator,
temporary_allocator) {
(void)constant_allocator; }
MemoryAllocator* method_allocator() const {
return method_allocator_;
}
HierarchicalAllocator* planned_memory() const {
return planned_memory_;
}
MemoryAllocator* temp_allocator() const {
return temp_allocator_;
}
Span<const etensor::Device> planned_buffer_devices() const {
if (planned_memory_ == nullptr) {
return {};
}
return planned_memory_->planned_buffer_devices();
}
bool has_device_memory() const {
return planned_buffer_devices().size() > 0;
}
private:
MemoryAllocator* method_allocator_;
HierarchicalAllocator* planned_memory_;
MemoryAllocator* temp_allocator_;
};
} }
namespace torch {
namespace executor {
using ::executorch::runtime::MemoryManager;
} }