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
27 changes: 27 additions & 0 deletions mlx/scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::promise<void>>();
Expand Down Expand Up @@ -91,6 +94,30 @@ struct StreamThread {
}
};

void Scheduler::wait_for_capacity(const std::set<Stream>& 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();
Expand Down
42 changes: 10 additions & 32 deletions mlx/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <atomic>
#include <queue>
#include <set>
#include <shared_mutex>
#include <unordered_map>

Expand Down Expand Up @@ -34,45 +35,26 @@ class MLX_API Scheduler {
void check_error(Stream s);

void notify_new_task(const Stream& stream) {
{
std::lock_guard<std::mutex> 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<std::mutex> 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<std::mutex> 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<Stream>& open_streams);

private:
friend Stream mlx::core::new_stream(Device d);

StreamThread& get_thread(Stream s);

int n_active_tasks_{0};
std::atomic<int> n_active_tasks_{0};
std::unordered_map<int, std::unique_ptr<StreamThread>> threads_;
std::shared_mutex threads_mtx_;
std::condition_variable completion_cv;
std::mutex mtx;
};

MLX_API Scheduler& scheduler();
Expand All @@ -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);
}
Expand All @@ -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<Stream>& open_streams) {
scheduler().wait_for_capacity(open_streams);
}

} // namespace mlx::core::scheduler
18 changes: 1 addition & 17 deletions mlx/transforms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -320,21 +318,7 @@ array eval_impl(std::vector<array> 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) {
Expand Down
40 changes: 40 additions & 0 deletions tests/scheduler_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

#include "doctest/doctest.h"

#include <atomic>
#include <chrono>
#include <thread>

#include "mlx/mlx.h"
#include "mlx/scheduler.h"

Expand Down Expand Up @@ -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<bool> 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") {
Expand Down
Loading