Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Runtime] Support clear global memory allocators #16066

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/tvm/runtime/memory/memory_manager.h
Expand Up @@ -89,6 +89,8 @@ class Allocator {
* \param buffer The buffer to free.
*/
virtual void Free(const Buffer& buffer) = 0;
/*! \brief Clear the allocated memory. */
virtual void Clear();
/*! \brief The amount of memory currently allocated.
* \return The amount of memory currently allocated.
*/
Expand Down Expand Up @@ -119,6 +121,8 @@ class MemoryManager {
* \return The memory allocator.
*/
static Allocator* GetAllocator(Device dev, AllocatorType type);
/*! \brief Clear the allocators. */
static void Clear();

private:
MemoryManager() {}
Expand Down
19 changes: 19 additions & 0 deletions src/runtime/memory/memory_manager.cc
Expand Up @@ -22,6 +22,7 @@
* \brief Allocate and manage memory for the runtime.
*/
#include <tvm/runtime/memory/memory_manager.h>
#include <tvm/runtime/registry.h>

#include <memory>
#include <utility>
Expand Down Expand Up @@ -166,6 +167,16 @@ Allocator* MemoryManager::GetAllocator(Device dev, AllocatorType type) {
return it->second.at(type).get();
}

void MemoryManager::Clear() {
MemoryManager* m = MemoryManager::Global();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to add a Clear interface to base Allocator, and iterate over the allocators, to call Allocator->Clear() without removing any allocators

std::lock_guard<std::mutex> lock(m->mu_);
for (const auto& [device, allocators] : m->allocators_) {
for (const auto& [allocator_type, allocator] : allocators) {
allocator->Clear();
}
}
}

NDArray Allocator::Empty(ShapeTuple shape, DLDataType dtype, DLDevice dev,
Optional<String> mem_scope) {
VerifyDataType(dtype);
Expand Down Expand Up @@ -198,6 +209,14 @@ Buffer Allocator::Alloc(Device dev, ShapeTuple shape, DLDataType type_hint,
return {};
}

void Allocator::Clear() {
// This function by default does nothing.
// For naive allocator, no explicit manual clear is needed.
// Pooled allocator will override this method.
}

TVM_REGISTER_GLOBAL("vm.builtin.memory_manager.clear").set_body_typed(MemoryManager::Clear);

} // namespace memory
} // namespace runtime
} // namespace tvm
2 changes: 2 additions & 0 deletions src/runtime/memory/pooled_allocator.h
Expand Up @@ -90,6 +90,8 @@ class PooledAllocator final : public Allocator {
VLOG(1) << "reclaim buffer " << buffer.size;
}

void Clear() override { ReleaseAll(); }

size_t UsedMemory() const override { return used_memory_.load(std::memory_order_relaxed); }

private:
Expand Down