Repository navigation
Studio: wake the dispatcher on stop instead of waiting out its queue poll - #16350
Conversation
…dels The live-worker switch slept 0.3 s in load_model and another 0.5 s in _shutdown_subprocess before sending shutdown. Send shutdown right after the cancel and wait for the worker to exit, draining replies while waiting so a late-cancelling stream cannot hold the child on a full pipe. The old 0.5 s grace is added to the exit budget, so the terminate fallback fires no earlier than before.
…itch swaps the worker Without the old 0.3 s + 0.5 s sleeps, the teardown drained a live stream's unread tokens and gen_done and swapped the worker handle before the stream caught up, so a direct or dispatched stream ended with a worker-crash error or a silently truncated reply. Route drained replies to the dispatched mailboxes, leave the queue to a direct reader, and wait (bounded by the old 0.8 s) only while a stream is still registered; an idle switch still waits for nothing.
…eps, not wall time
|
The |
…uality (#2336) ## Problem `tests/test_chunked_logprob.py::test_grpo_switches_keep_entropy_policy_and_reference_on_one_scorer[UNSLOTH_GRPO_CHUNKED_LOGPROBS]` fails on some GitHub runners in unsloth's `Core zoo (HF=latest + TRL=latest)` job, which runs this suite. It failed on unslothai/unsloth#16350 (an orchestrator change) and earlier on unslothai/unsloth#16312's staging run (a diffusion change), both at zoo `604c131a`. Both times the result was identical: ``` AssertionError: {'equal': False, 'maxdiff': 3.0040740966796875e-05} ``` The same value twice means the runner hardware determines it, not chance. The test runs the reference scorer (fully eager) and the recompute scorer (eager matmul, with an Inductor-compiled `post_forward`) on CPU, and asserts bit equality. On this host the two agree exactly at every thread count (1 to 4), every Inductor `simdlen` tried (1, 4, 8, 16), and with ATen pinned to AVX2. On those runners the two most likely reduce in a different order. I could not reproduce it here, so the exact cause is unconfirmed. Bit equality was also the only way this test could catch the regression it guards. On CPU, when the switch is off but the policy is still scored by the fused kernel, the drift from the old reference scorer is only 9.5e-7. That is smaller than the 3e-5 the two old-scorer paths differ by on those runners, so no tolerance separates the bug from the noise. ## Change The child process now wraps the two fused entry points, `fused_linear.fused_linear_logprobs` and `chunked_logprob.unsloth_chunked_logprobs`, and records every call. With either switch off, the test asserts that neither one ran, which is the actual meaning of "on one scorer". It also keeps a sanity bound of `maxdiff < 1e-3` on the values. ## Verification - Both parametrisations pass. The whole file passes, with 77 passed and 3 skipped under `-n 8`. - Sabotage 1: the recompute scorer's `fused_linear` default ignores the switches. The `UNSLOTH_GRPO_CHUNKED_LOGPROBS` case fails with `{'calls': ['fused_linear_logprobs'], 'maxdiff': 9.5367431640625e-07}`. - Sabotage 2: the reference scorer ignores the switches. That case fails with `{'calls': ['unsloth_chunked_logprobs', 'fused_linear_logprobs'], ...}`. - In both sabotages the `UNSLOTH_FUSED_LINEAR` case still passes, as it should: that flag also disables `chunked_logprob._FUSED_LINEAR` at import, which gates the fused path on its own. - The sabotage drift of 9.5e-7 shows that a plain tolerance in place of the old `torch.equal` would have let both regressions through.
…e wall-clock assert
[skip ci]
[skip ci]
[skip ci]
… a fixed 0.8 s [skip ci]
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Nice work! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Codex Review: Didn't find any major issues. Keep them coming! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Stacked on #16344 (perf/r18-model-switch-waits). Review that one first; this PR only adds the commit on top.
What changes
When the inference dispatcher thread is running (compare mode, concurrent requests), a model switch first calls
_stop_dispatcher. That sets the stop flag and joins the thread, but the thread sits inresp_queue.get(timeout=_DISPATCH_POLL_INTERVAL)(0.5 s) and only sees the flag when that poll times out. So every switch with a live dispatcher paid 0 to 500 ms of dead wait. The same goes for the idle stop path._stop_dispatchernow puts a wake message ({"type": "_dispatcher_wake"}) on the response queue after it sets the flag, and only if the thread is alive. The dispatcher gets it, goes back to its loop check, and exits. The poll interval stays the same.Why nothing gets dropped or reordered:
request_id, and the worker never sends this type. The three places that read the queue directly (_dispatcher_loop,_read_resp,_drain_queue) skip it. Every other reader goes through_read_respor_drain_queue._read_respkeeps reading until its original timeout runs out, so finding a wake message does not look like a timeout to the caller._CTX.Queue()in the spawn path), and shutdown sets_resp_queueto None, so a leftover never reaches the next worker's readers.put_nowaitdoes not block, any exception is logged at debug level, and the dispatcher then stops on its next poll timeout, the same as before.cancel_join_thread()is called on the parent's side of the queue first, so interpreter exit never waits on this one put if the pipe is full or dead. The parent sends nothing else on that queue.EOFError/OSError/ValueErrorstill break the loop, and routing errors are still logged and skipped). The spawn context is unchanged. On Windows the parent already holds both ends of the queue.Results
CPU only, Linux. Stub spawn worker with the same shutdown protocol. Times are for
_shutdown_subprocess(). Before each switch the dispatcher waits a random time between 0.05 and 0.55 s, with fixed seeds, so the poll phase is spread out. n=12 per arm, two runs per arm.That is about 260 ms off the mean of a switch with a live dispatcher, and up to about 480 ms in the worst case. With the dispatcher running, a switch now costs the same as one without it.
Tests
studio/backend/tests/test_orchestrator_dispatcher_wake.py:_read_respor_drain_queueand is never passed to the observer (fails on base)tests/test_orchestrator_switch_waits.py, includingtest_streaming_reader_keeps_its_tail_across_a_switch[direct|dispatched], plustests/test_inference_dispatcher_resilience.py: 28 passed, 3 repeats.python -m pytest -q tests/studio -k "orchestrator or dispatch or switch": 254 passed, 40 skipped on base and on head.orchestrator or dispatch or switch or stream or shutdown or unload or worker or inference: base 76 failed / 72 errors / 7258 passed, head 76 failed / 72 errors / 7263 passed. The failing IDs are the same on both (mmproj placement, pricing, diffusion compile and similar, all failing before this change). The extra 5 passes are the new tests.Independent checks: a worker sending 300 messages of about 70 KB while the parent sends 300 wakes delivers every worker message intact and in order (both sides share the queue's write lock under spawn). A stress run of 200 switches with two compare-mode streams in flight, half of them with the worker killed 0-30 ms into the stop, had no hangs, no out-of-order or missing tokens, no wake reaching a stream and no dispatcher left alive, with the same error outcomes as #16344's head.
Not covered
If the worker is killed while it holds the queue's write lock and a wake is still waiting to be written, the parent's queue feeder thread stays blocked until the process exits (one daemon thread and the queue's two pipe descriptors);
cancel_join_thread()keeps it from blocking interpreter exit, and the dispatcher stops at its next 0.5 s poll as before. This did not happen in 200 kill -9 iterations. Windows queues have no write lock, so it cannot happen there.No real Windows or macOS run. Spawn semantics are only argued from the code; the stub harness ran on Linux.
No real model worker or GPU. The latency numbers come from a stub worker, so a real switch still includes the model unload time, which this change does not touch.