Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions mlx/backend/common/allocation_footprint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright © 2026 Apple Inc.

#pragma once

#include <algorithm>
#include <cstddef>
#include <limits>
#include <stdexcept>

namespace mlx::core::allocator {

inline size_t checked_allocation_add(size_t size, size_t extra) {
if (extra > std::numeric_limits<size_t>::max() - size) {
throw std::overflow_error("allocation footprint overflow");
}
return size + extra;
}

inline size_t round_allocation_size(size_t size, size_t alignment) {
if (alignment == 0) {
throw std::invalid_argument("allocation alignment is zero");
}
auto remainder = size % alignment;
return checked_allocation_add(size, remainder ? alignment - remainder : 0);
}

// Inclusive cache-reuse bound. A fresh buffer never exceeds this bound.
inline size_t maximum_reuse_size(size_t size, size_t page_size) {
if (size == 0) {
return 0;
}
auto two_pages = checked_allocation_add(page_size, page_size);
if (two_pages == 0) {
throw std::invalid_argument("cache page size is zero");
}
return checked_allocation_add(size, std::min(size - 1, two_pages - 1));
}

} // namespace mlx::core::allocator
7 changes: 6 additions & 1 deletion mlx/backend/common/buffer_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <functional>
#include <map>

#include "mlx/backend/common/allocation_footprint.h"

namespace mlx::core {

template <typename T>
Expand All @@ -28,10 +30,13 @@ class BufferCache {
BufferCache& operator=(const BufferCache&) = delete;

T* reuse_from_cache(size_t size) {
if (size == 0) {
return nullptr;
}
// Find the closest buffer in pool.
auto it = buffer_pool_.lower_bound(size);
if (it == buffer_pool_.end() ||
it->first >= std::min(2 * size, size + 2 * page_size_)) {
it->first > allocator::maximum_reuse_size(size, page_size_)) {
return nullptr;
}

Expand Down
41 changes: 34 additions & 7 deletions mlx/backend/cuda/allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ constexpr int page_size = 16384;
// Any allocations smaller than this will try to use the small pool
constexpr int small_block_size = 8;

size_t normalized_allocation_size(size_t size) {
if (size == 0) {
return 0;
}
if (size <= small_block_size) {
return 8;
}
if (size < page_size) {
return next_power_of_2(size);
}
return allocator::round_allocation_size(size, page_size);
}

// The small pool size in bytes. This should be a multiple of the host page
// size and small_block_size.
constexpr int small_pool_size = 4 * page_size;
Expand Down Expand Up @@ -168,13 +181,7 @@ CudaAllocator::malloc_async(size_t size, int device, cudaStream_t stream) {
return Buffer{new CudaBuffer{nullptr, 0, -1}};
}

if (size <= small_block_size) {
size = 8;
} else if (size < page_size) {
size = next_power_of_2(size);
} else {
size = page_size * ((size + page_size - 1) / page_size);
}
size = normalized_allocation_size(size);

if (size <= small_block_size || stream == nullptr) {
device = -1;
Expand Down Expand Up @@ -339,6 +346,11 @@ void CudaAllocator::reset_peak_memory() {
peak_memory_ = 0;
}

MemorySnapshot CudaAllocator::get_memory_snapshot() {
std::lock_guard lock(mutex_);
return {active_memory_, buffer_cache_.cache_size(), peak_memory_};
}

size_t CudaAllocator::get_memory_limit() {
return memory_limit_;
}
Expand Down Expand Up @@ -404,6 +416,21 @@ bool can_reuse_alien_buffer(void* ptr) {

} // namespace allocator

AllocationFootprintPolicy get_allocation_footprint_policy() noexcept {
return {cu::page_size, 0, cu::small_block_size, cu::page_size, cu::page_size};
}

size_t get_allocation_size_upper_bound(size_t size) {
size_t result;
if (!get_allocation_footprint_policy().upper_bound(size, result)) {
throw std::overflow_error("allocation footprint overflow");
}
return result;
}

MemorySnapshot get_memory_snapshot() {
return cu::allocator().get_memory_snapshot();
}
size_t get_active_memory() {
return cu::allocator().get_active_memory();
}
Expand Down
2 changes: 2 additions & 0 deletions mlx/backend/cuda/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#pragma once

#include "mlx/allocator.h"
#include "mlx/memory.h"
#include "mlx/backend/common/buffer_cache.h"
#include "mlx/backend/cuda/cuda_utils.h"

Expand Down Expand Up @@ -58,6 +59,7 @@ class CudaAllocator : public allocator::Allocator {
// host memory), and copy the data over. Pass |stream| to copy asynchronously.
void move_to_unified_memory(CudaBuffer& buf, cudaStream_t stream = nullptr);

MemorySnapshot get_memory_snapshot();
size_t get_active_memory() const;
size_t get_peak_memory() const;
void reset_peak_memory();
Expand Down
22 changes: 21 additions & 1 deletion mlx/backend/metal/allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ size_t MetalAllocator::set_memory_limit(size_t limit) {
return limit;
};

MemorySnapshot MetalAllocator::get_memory_snapshot() {
std::lock_guard lock(mutex_);
return {active_memory_, buffer_cache_.cache_size(), peak_memory_};
}

size_t MetalAllocator::get_memory_limit() {
return block_limit_;
}
Expand Down Expand Up @@ -149,7 +154,7 @@ Buffer MetalAllocator::malloc(size_t size) {

// Align up memory
if (size > vm_page_size) {
size = vm_page_size * ((size + vm_page_size - 1) / vm_page_size);
size = allocator::round_allocation_size(size, vm_page_size);
}

// Try the cache
Expand Down Expand Up @@ -291,6 +296,18 @@ MetalAllocator& allocator() {

} // namespace metal

AllocationFootprintPolicy get_allocation_footprint_policy() noexcept {
return {vm_page_size, vm_page_size, 0, 0, vm_page_size};
}

size_t get_allocation_size_upper_bound(size_t size) {
size_t result;
if (!get_allocation_footprint_policy().upper_bound(size, result)) {
throw std::overflow_error("allocation footprint overflow");
}
return result;
}

size_t set_cache_limit(size_t limit) {
return metal::allocator().set_cache_limit(limit);
}
Expand All @@ -309,6 +326,9 @@ size_t set_wired_limit(size_t limit) {
}
return metal::allocator().set_wired_limit(limit);
}
MemorySnapshot get_memory_snapshot() {
return metal::allocator().get_memory_snapshot();
}
size_t get_active_memory() {
return metal::allocator().get_active_memory();
}
Expand Down
2 changes: 2 additions & 0 deletions mlx/backend/metal/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <vector>

#include "mlx/allocator.h"
#include "mlx/memory.h"
#include "mlx/backend/common/buffer_cache.h"
#include "mlx/backend/metal/device.h"

Expand All @@ -23,6 +24,7 @@ class MetalAllocator : public allocator::Allocator {
virtual Buffer make_buffer(void* ptr, size_t size) override;
virtual void release(Buffer buffer) override;

MemorySnapshot get_memory_snapshot();
size_t get_active_memory() {
return active_memory_;
};
Expand Down
28 changes: 14 additions & 14 deletions mlx/backend/metal/kernels/quantized.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ inline U load_vector(const device T* x, thread U* x_thread) {

if (bits == 2) {
for (int i = 0; i < values_per_thread; i += 4) {
sum += x[i] + x[i + 1] + x[i + 2] + x[i + 3];
sum += U(x[i]) + U(x[i + 1]) + U(x[i + 2]) + U(x[i + 3]);
x_thread[i] = x[i];
x_thread[i + 1] = x[i + 1] / 4.0f;
x_thread[i + 2] = x[i + 2] / 16.0f;
Expand All @@ -46,8 +46,8 @@ inline U load_vector(const device T* x, thread U* x_thread) {

else if (bits == 3) {
for (int i = 0; i < values_per_thread; i += 8) {
sum += x[i] + x[i + 1] + x[i + 2] + x[i + 3] + x[i + 4] + x[i + 5] +
x[i + 6] + x[i + 7];
sum += U(x[i]) + U(x[i + 1]) + U(x[i + 2]) + U(x[i + 3]) + U(x[i + 4]) +
U(x[i + 5]) + U(x[i + 6]) + U(x[i + 7]);
x_thread[i] = x[i];
x_thread[i + 1] = x[i + 1] / 8.0f;
x_thread[i + 2] = x[i + 2] / 64.0f;
Expand All @@ -61,7 +61,7 @@ inline U load_vector(const device T* x, thread U* x_thread) {

else if (bits == 4) {
for (int i = 0; i < values_per_thread; i += 4) {
sum += x[i] + x[i + 1] + x[i + 2] + x[i + 3];
sum += U(x[i]) + U(x[i + 1]) + U(x[i + 2]) + U(x[i + 3]);
x_thread[i] = x[i];
x_thread[i + 1] = x[i + 1] / 16.0f;
x_thread[i + 2] = x[i + 2] / 256.0f;
Expand All @@ -71,8 +71,8 @@ inline U load_vector(const device T* x, thread U* x_thread) {

else if (bits == 5) {
for (int i = 0; i < values_per_thread; i += 8) {
sum += x[i] + x[i + 1] + x[i + 2] + x[i + 3] + x[i + 4] + x[i + 5] +
x[i + 6] + x[i + 7];
sum += U(x[i]) + U(x[i + 1]) + U(x[i + 2]) + U(x[i + 3]) + U(x[i + 4]) +
U(x[i + 5]) + U(x[i + 6]) + U(x[i + 7]);
x_thread[i] = x[i];
x_thread[i + 1] = x[i + 1] / 32.0f;
x_thread[i + 2] = x[i + 2] / 4.0f;
Expand All @@ -86,7 +86,7 @@ inline U load_vector(const device T* x, thread U* x_thread) {

else if (bits == 6) {
for (int i = 0; i < values_per_thread; i += 4) {
sum += x[i] + x[i + 1] + x[i + 2] + x[i + 3];
sum += U(x[i]) + U(x[i + 1]) + U(x[i + 2]) + U(x[i + 3]);
x_thread[i] = x[i];
x_thread[i + 1] = x[i + 1] / 64.0f;
x_thread[i + 2] = x[i + 2] / 16.0f;
Expand Down Expand Up @@ -115,7 +115,7 @@ inline U load_vector_safe(const device T* x, thread U* x_thread, int N) {

if (bits == 2) {
for (int i = 0; i < N; i += 4) {
sum += x[i] + x[i + 1] + x[i + 2] + x[i + 3];
sum += U(x[i]) + U(x[i + 1]) + U(x[i + 2]) + U(x[i + 3]);
x_thread[i] = x[i];
x_thread[i + 1] = x[i + 1] / 4.0f;
x_thread[i + 2] = x[i + 2] / 16.0f;
Expand All @@ -125,8 +125,8 @@ inline U load_vector_safe(const device T* x, thread U* x_thread, int N) {

else if (bits == 3) {
for (int i = 0; i < N; i += 8) {
sum += x[i] + x[i + 1] + x[i + 2] + x[i + 3] + x[i + 4] + x[i + 5] +
x[i + 6] + x[i + 7];
sum += U(x[i]) + U(x[i + 1]) + U(x[i + 2]) + U(x[i + 3]) + U(x[i + 4]) +
U(x[i + 5]) + U(x[i + 6]) + U(x[i + 7]);

x_thread[i] = x[i];
x_thread[i + 1] = x[i + 1] / 8.0f;
Expand All @@ -141,7 +141,7 @@ inline U load_vector_safe(const device T* x, thread U* x_thread, int N) {

else if (bits == 4) {
for (int i = 0; i < N; i += 4) {
sum += x[i] + x[i + 1] + x[i + 2] + x[i + 3];
sum += U(x[i]) + U(x[i + 1]) + U(x[i + 2]) + U(x[i + 3]);
x_thread[i] = x[i];
x_thread[i + 1] = x[i + 1] / 16.0f;
x_thread[i + 2] = x[i + 2] / 256.0f;
Expand All @@ -151,8 +151,8 @@ inline U load_vector_safe(const device T* x, thread U* x_thread, int N) {

else if (bits == 5) {
for (int i = 0; i < N; i += 8) {
sum += x[i] + x[i + 1] + x[i + 2] + x[i + 3] + x[i + 4] + x[i + 5] +
x[i + 6] + x[i + 7];
sum += U(x[i]) + U(x[i + 1]) + U(x[i + 2]) + U(x[i + 3]) + U(x[i + 4]) +
U(x[i + 5]) + U(x[i + 6]) + U(x[i + 7]);
x_thread[i] = x[i];
x_thread[i + 1] = x[i + 1] / 32.0f;
x_thread[i + 2] = x[i + 2] / 4.0f;
Expand All @@ -166,7 +166,7 @@ inline U load_vector_safe(const device T* x, thread U* x_thread, int N) {

else if (bits == 6) {
for (int i = 0; i < N; i += 4) {
sum += x[i] + x[i + 1] + x[i + 2] + x[i + 3];
sum += U(x[i]) + U(x[i + 1]) + U(x[i + 2]) + U(x[i + 3]);
x_thread[i] = x[i];
x_thread[i + 1] = x[i + 1] / 64.0f;
x_thread[i + 2] = x[i + 2] / 16.0f;
Expand Down
20 changes: 20 additions & 0 deletions mlx/backend/no_gpu/allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class CommonAllocator : public Allocator {
virtual void free(Buffer buffer) override;
virtual size_t size(Buffer buffer) const override;

MemorySnapshot get_memory_snapshot() const {
std::lock_guard lock(mutex_);
return {active_memory_, buffer_cache_.cache_size(), peak_memory_};
}

size_t get_active_memory() const {
return active_memory_;
};
Expand Down Expand Up @@ -184,6 +189,21 @@ bool can_reuse_alien_buffer(void*) {

} // namespace allocator

AllocationFootprintPolicy get_allocation_footprint_policy() noexcept {
return {1, 0, 0, 0, 4096};
}

size_t get_allocation_size_upper_bound(size_t size) {
size_t result;
if (!get_allocation_footprint_policy().upper_bound(size, result)) {
throw std::overflow_error("allocation footprint overflow");
}
return result;
}

MemorySnapshot get_memory_snapshot() {
return allocator::common_allocator().get_memory_snapshot();
}
size_t get_active_memory() {
return allocator::common_allocator().get_active_memory();
}
Expand Down
Loading