Skip to content

Commit

Permalink
Do not release buffers on exit (ml-explore#1142)
Browse files Browse the repository at this point in the history
  • Loading branch information
zcbenz authored Jul 15, 2024
1 parent 987785d commit 2f83d6e
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions mlx/backend/metal/allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,17 @@ void MetalAllocator::free(Buffer buffer) {
}

MetalAllocator& allocator() {
static MetalAllocator allocator_;
return allocator_;
// By creating the |allocator_| on heap, the destructor of MetalAllocator will
// not be called on exit and all the buffers will be leaked. This is necessary
// because releasing buffers can take more than 30sec when the program holds a
// lot of RAM (for example inferencing a LLM), and it would feel frozen to
// users when exiting.
// TODO(zcbenz): Consider using the `base::NoDestructor` class from Chromium
// when applying this pattern to more places, or when introducing sanitizers
// to MLX.
// https://source.chromium.org/chromium/chromium/src/+/main:base/no_destructor.h
static MetalAllocator* allocator_ = new MetalAllocator;
return *allocator_;
}

size_t set_cache_limit(size_t limit) {
Expand Down

0 comments on commit 2f83d6e

Please sign in to comment.