diff --git a/mlx/scheduler.cpp b/mlx/scheduler.cpp index 572e236bbe..b7586540d2 100644 --- a/mlx/scheduler.cpp +++ b/mlx/scheduler.cpp @@ -6,11 +6,14 @@ #include "mlx/backend/cpu/eval.h" #include "mlx/backend/gpu/eval.h" #include "mlx/compile_impl.h" +#include "mlx/memory.h" #include "mlx/scheduler.h" #include "mlx/utils.h" namespace mlx::core { +static constexpr int MAX_ACTIVE_TASKS = 10; + void synchronize(Stream s) { if (s.device == mlx::core::Device::cpu) { auto p = std::make_shared>(); @@ -91,6 +94,30 @@ struct StreamThread { } }; +void Scheduler::wait_for_capacity(const std::set& open_streams) { + auto over_memory_limit = [] { + return get_active_memory() > get_memory_limit(); + }; + int n = n_active_tasks_.load(); + if (n <= MAX_ACTIVE_TASKS && !(n > 0 && over_memory_limit())) { + return; + } + // Commit any open streams + for (auto& s : open_streams) { + if (s.device == Device::gpu) { + gpu::finalize(s); + } + } + // Wait for one task to finish, repeat while memory is above the limit + do { + int n_old = n = n_active_tasks_.load(); + while (n_old > 1 && n >= n_old) { + n_active_tasks_.wait(n); + n = n_active_tasks_.load(); + } + } while (n > 0 && over_memory_limit()); +} + Scheduler::Scheduler() { is_main_thread(); gpu::init(); diff --git a/mlx/scheduler.h b/mlx/scheduler.h index 7bce5a9fe6..5c1a71b02a 100644 --- a/mlx/scheduler.h +++ b/mlx/scheduler.h @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -34,45 +35,26 @@ class MLX_API Scheduler { void check_error(Stream s); void notify_new_task(const Stream& stream) { - { - std::lock_guard lk(mtx); - n_active_tasks_++; - } - completion_cv.notify_all(); + n_active_tasks_.fetch_add(1); } void notify_task_completion(const Stream& stream) { - { - std::lock_guard lk(mtx); - n_active_tasks_--; - } - completion_cv.notify_all(); + n_active_tasks_.fetch_sub(1); + n_active_tasks_.notify_all(); } - int n_active_tasks() const { - return n_active_tasks_; - } - - void wait_for_one() { - std::unique_lock lk(mtx); - int n_tasks_old = n_active_tasks(); - if (n_tasks_old > 1) { - completion_cv.wait(lk, [this, n_tasks_old] { - return this->n_active_tasks() < n_tasks_old; - }); - } - } + // Block the caller when too many tasks or too much memory is in flight. + // Commits the GPU streams in open_streams first so that tasks can finish. + void wait_for_capacity(const std::set& open_streams); private: friend Stream mlx::core::new_stream(Device d); StreamThread& get_thread(Stream s); - int n_active_tasks_{0}; + std::atomic n_active_tasks_{0}; std::unordered_map> threads_; std::shared_mutex threads_mtx_; - std::condition_variable completion_cv; - std::mutex mtx; }; MLX_API Scheduler& scheduler(); @@ -98,10 +80,6 @@ inline void check_error(Stream s) { scheduler().check_error(s); } -inline int n_active_tasks() { - return scheduler().n_active_tasks(); -} - inline void notify_new_task(const Stream& stream) { scheduler().notify_new_task(stream); } @@ -110,8 +88,8 @@ inline void notify_task_completion(const Stream& stream) { scheduler().notify_task_completion(stream); } -inline void wait_for_one() { - scheduler().wait_for_one(); +inline void wait_for_capacity(const std::set& open_streams) { + scheduler().wait_for_capacity(open_streams); } } // namespace mlx::core::scheduler diff --git a/mlx/transforms.cpp b/mlx/transforms.cpp index 24c91f3483..7d5689fbe4 100644 --- a/mlx/transforms.cpp +++ b/mlx/transforms.cpp @@ -24,8 +24,6 @@ namespace mlx::core { -static constexpr int MAX_ACTIVE_TASKS = 10; - namespace { // Get the stream of the first array that has a primitive, else the default. @@ -320,21 +318,7 @@ array eval_impl(std::vector outputs, bool async) { cpu::eval(arr); } - if (scheduler::n_active_tasks() > MAX_ACTIVE_TASKS || - (get_active_memory() > get_memory_limit() && - scheduler::n_active_tasks() > 0)) { - // Commit any open streams - for (auto& s : open_streams) { - if (s.device == Device::gpu) { - gpu::finalize(s); - } - } - scheduler::wait_for_one(); - while (get_active_memory() > get_memory_limit() && - scheduler::n_active_tasks() > 0) { - scheduler::wait_for_one(); - } - } + scheduler::wait_for_capacity(open_streams); auto maybe_update_fence = [&fences, &needs_fence, stream](const array& a) { diff --git a/tests/scheduler_tests.cpp b/tests/scheduler_tests.cpp index 2cc9325a0c..72de6df20b 100644 --- a/tests/scheduler_tests.cpp +++ b/tests/scheduler_tests.cpp @@ -2,6 +2,10 @@ #include "doctest/doctest.h" +#include +#include +#include + #include "mlx/mlx.h" #include "mlx/scheduler.h" @@ -326,6 +330,42 @@ TEST_CASE("test scheduler races") { eval(a, y); } +TEST_CASE("test eval reads the task count while cpu tasks complete") { + auto s = new_stream(Device::cpu); + auto y = zeros({100}, s); + for (int i = 0; i < 1000; ++i) { + y = exp(y, s); + } + eval(y); + CHECK_EQ(y.size(), 100); +} + +TEST_CASE("test wait for capacity blocks until a task completes") { + auto s = default_stream(default_device()); + synchronize(s); + for (int i = 0; i < 11; ++i) { + scheduler::notify_new_task(s); + } + std::atomic returned{false}; + std::thread waiter([&returned] { + scheduler::wait_for_capacity({}); + returned = true; + }); + std::this_thread::sleep_for(std::chrono::milliseconds(50)); + CHECK_FALSE(returned); + scheduler::notify_task_completion(s); + waiter.join(); + CHECK(returned); + for (int i = 0; i < 10; ++i) { + scheduler::notify_task_completion(s); + } +} + +TEST_CASE("test wait for capacity returns when under the limit") { + synchronize(default_stream(default_device())); + scheduler::wait_for_capacity({}); +} + // The fence orders work between two streams. Check that the consumer sees the // producer result for every combination of producer and consumer device. TEST_CASE("test cross stream fence ordering") {