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
10 changes: 10 additions & 0 deletions docs/src/usage/environment_variables.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ handled by the CUDA runtime.
Metal
-----

.. envvar:: MLX_METAL_RESIDENCY_REFRESH_INTERVAL_MS

Re-request residency for existing wired allocations at this interval in
milliseconds. This works around macOS releasing residency while the GPU is
idle. The default is ``0`` (disabled); nonpositive values disable renewal.
For example, ``1000`` renews once per second. This does not change the wired
limit set by :func:`mlx.core.set_wired_limit` or submit GPU work. Renewal
continues while the Metal device exists and can keep memory wired during
long idle periods. Requires a Metal 3 GPU on macOS 15 or iOS 18 or later.

.. envvar:: MLX_METAL_FAST_SYNCH

Enable the faster Metal CPU/GPU synchronization path. The default is ``0``.
Expand Down
33 changes: 31 additions & 2 deletions mlx/backend/metal/resident.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright © 2024 Apple Inc.
// Copyright © 2024-2026 Apple Inc.

#include <algorithm>
#include <cassert>
#include <chrono>
#include <cstdio>
#include <sstream>
#include <stdexcept>
Expand Down Expand Up @@ -43,10 +44,38 @@ ResidencySets::ResidencySets(MTL::Device* d) {
}
throw std::runtime_error(msg.str());
}
if (int interval =
env::get_var("MLX_METAL_RESIDENCY_REFRESH_INTERVAL_MS", 0);
interval > 0) {
refresh_thread_ = std::thread([this, interval] {
std::unique_lock<std::mutex> lock(mtx_);
while (!refresh_cv_.wait_for(
lock, std::chrono::milliseconds(interval), [this] {
return stop_refresh_;
})) {
auto pool = new_scoped_memory_pool();
// macOS can drop residency after GPU idle despite a standing request.
for (auto& s : sets_) {
if (s.size != 0) {
s.set->requestResidency();
}
}
}
});
}
}
}

ResidencySets::~ResidencySets() = default;
ResidencySets::~ResidencySets() {
if (refresh_thread_.joinable()) {
{
std::lock_guard<std::mutex> lock(mtx_);
stop_refresh_ = true;
}
refresh_cv_.notify_one();
refresh_thread_.join();
}
}

bool ResidencySets::add_set_locked(std::string* error_out) {
NS::SharedPtr<MTL::ResidencySet> set;
Expand Down
7 changes: 6 additions & 1 deletion mlx/backend/metal/resident.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Copyright © 2024 Apple Inc.
// Copyright © 2024-2026 Apple Inc.

#pragma once

#include <atomic>
#include <condition_variable>
#include <cstdint>
#include <mutex>
#include <string>
#include <thread>
#include <unordered_map>
#include <vector>

Expand Down Expand Up @@ -119,6 +121,9 @@ class ResidencySets {
// taking the lock.
std::atomic<uint64_t> num_sets_{0};
mutable std::mutex mtx_;
std::condition_variable refresh_cv_;
bool stop_refresh_{false};
std::thread refresh_thread_;
};

} // namespace mlx::core::metal
Loading