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
5 changes: 5 additions & 0 deletions mlx/scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ void synchronize(Stream s) {
std::future<void> f = p->get_future();
scheduler::enqueue(s, [p = std::move(p)]() { p->set_value(); });
f.wait();
scheduler::check_error(s);
} else {
gpu::synchronize(s);
}
Expand Down Expand Up @@ -140,6 +141,10 @@ void Scheduler::signal_event(
});
}

void Scheduler::check_error(Stream s) {
get_thread(s).error.check();
}

StreamThread& Scheduler::get_thread(Stream s) {
{
std::shared_lock lock(threads_mtx_);
Expand Down
6 changes: 6 additions & 0 deletions mlx/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class MLX_API Scheduler {
void enqueue(Stream s, std::function<void()> task);
void wait_event(Stream s, Event event, std::function<void(Event&)> task);
void signal_event(Stream s, Event event, std::function<void(Event&)> task);
void check_error(Stream s);

void notify_new_task(const Stream& stream) {
{
Expand Down Expand Up @@ -92,6 +93,11 @@ inline void signal_event(Stream s, Event event, F&& f) {
scheduler().signal_event(s, std::move(event), std::forward<F>(f));
}

// Throw and clear the error stored in the stream, if any.
inline void check_error(Stream s) {
scheduler().check_error(s);
}

inline int n_active_tasks() {
return scheduler().n_active_tasks();
}
Expand Down
9 changes: 9 additions & 0 deletions python/tests/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,15 @@ def test_eval_exception_does_not_corrupt_state(self):
x = mx.full((512,), 2.0)
self.assertEqual((x + 1.0).sum().item(), 512.0 * 3.0)

@unittest.skipIf(
mx.cuda.is_available(), "CUDA backend waits cpu stream synchronously"
)
def test_async_eval_error_in_synchronize(self):
a = mx.linalg.inv(mx.array([[1.0, 2.0], [2.0, 4.0]]), stream=mx.cpu)
mx.async_eval(a)
with self.assertRaises(RuntimeError):
mx.synchronize(mx.cpu)


if __name__ == "__main__":
mlx_tests.MLXTestRunner()
Loading