mlx: drop carried metal residency patch now that it is upstream (#18854)

This commit is contained in:
Daniel Hiltgen 2026-10-07 15:22:54 -07:00 • committed by GitHub
parent f9f4af6ce0
commit cf2a313a29
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 1 additions and 85 deletions

View file

@ -1 +1 @@
264c14fe650e1dd9bb0c3902dc06b08033d8f89c
a59cc23190f1e26fabcba8693607bb4c34b2ac2b

View file

@ -1,84 +0,0 @@
diff --git a/mlx/backend/metal/resident.cpp b/mlx/backend/metal/resident.cpp
--- a/mlx/backend/metal/resident.cpp
+++ b/mlx/backend/metal/resident.cpp
@@ -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>
@@ -43,10 +44,38 @@
}
throw std::runtime_error(msg.str());
}
- }
-}
-
-ResidencySets::~ResidencySets() = default;
+ 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() {
+ 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;
diff --git a/mlx/backend/metal/resident.h b/mlx/backend/metal/resident.h
--- a/mlx/backend/metal/resident.h
+++ b/mlx/backend/metal/resident.h
@@ -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>
@@ -119,6 +121,9 @@
// 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