Repository navigation
Make the scheduler's active task count atomic - #4882
Conversation
zcbenz
left a comment
There was a problem hiding this comment.
I think we should not really expose n_active_tasks() as a public method since it is going to cause race condition when called separately from wait_for_one(), even with the fix in this PR. Can you move the code in
Lines 323 to 337 in 8f0f391
Scheduler and add proper locks?
Also since we are going to use std::atomic for n_active_tasks_, there is not much need for completion_cv and mtx, you can use the notification APIs of std::atomic for the same purpose.
|
Thanks for the feedback. I will work on this. I can remove the mutex and the condition variable now. When I discovered the race, I didn't want to make changes that could affect ordering. |
9f235c1 to
fe114cc
Compare
fe114cc to
c9bf935
Compare
`n_active_tasks()` reads `n_active_tasks_` without the mutex while stream threads update it under the lock. The unlocked read from eval_impl was a data race. This patch makes `n_active_tasks_` an atomic, removes the mutex, condition variable, and wait_for_one function.
c9bf935 to
aff8de3
Compare
zcbenz
left a comment
There was a problem hiding this comment.
Thanks for the nice fix & refactoring!
Ported: mlx/scheduler.h is Apple's in full. The fork's test "test concurrent scheduler active task observations" (osaurus-ai/mlx PR 15) is removed: it calls n_active_tasks() and wait_for_one(), which this commit removes. (cherry picked from commit a3a610b)
…tive tracer
One env var, read once as a level; unset or 0 is off and costs one
member load per eval.
1 [metal-command] JSON per committed command buffer and [mlx-evaluation]
JSON per eval_impl
2 also [metal-op] <primitive name> per GPU primitive
3 also its input -> output dtypes
Adapted for upstream ml-explore#4647 (get_default_max_ops_mb_per_buffer + env::*),
ml-explore#4711 (verify_graph), ml-explore#4882 (Scheduler::wait_for_capacity overload), and
ml-explore#5004 (tracing_counter).
n_active_tasks()readsn_active_tasks_without the mutex while stream threads update it under the lock. The unlocked read from eval_impl is a data race.Needs TSan-instrumented tests to verify.