Repository navigation
Studio: recognise a worker VRAM release that finished before the first settle poll - #16404
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
…e wall-clock assert
[skip ci]
[skip ci]
… a fixed 0.8 s [skip ci]
…t settle poll Model switches with no free-VRAM target (explicit GPU pick, multi-GPU shard, unknown model size) waited the full 2 s settle deadline because the release had already landed by the first poll and so was never observed. Sample free VRAM before the old worker is stopped and count the gain since then as the release.
…ase seen only against it
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. You're on a roll. 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. Another round soon, 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 it up! 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 (it edits the same switch path in
load_model); review that one first.What changes
When Studio switches chat models it stops the old inference worker and then waits for its VRAM to come back (
_wait_for_worker_vram_settle). If the switch has a free-VRAM target (auto-selected single GPU with a size estimate), the first poll already passes and nothing changes here.Without a target (an explicit GPU pick, a model sharded over several GPUs, or a model whose size cannot be estimated), the wait only returns once it has seen memory come back between two polls and then one stable sample. By the time
_shutdown_subprocesshas joined the worker, the driver has already handed its memory back, so the first poll already reads the freed state. No release is ever seen between polls, and every such switch sits out the full 2 s deadline.This PR takes one free-VRAM sample while the old worker is still alive (right after the generation is cancelled, so cancellation never waits on telemetry), and only on the no-target path. If the first poll after the kill shows a gain past the existing tolerance (max(256 MiB, 2%)) compared with that sample, it counts as the release. Because that gain could be a partial release, it then needs two stable polls instead of one; a release seen between polls keeps the existing one-stable-poll rule. The probe body moved unchanged into a
_free_vram_mibhelper so both reads share it.Release semantics stay the same. The worker is still joined (terminate/kill ladder untouched), the settle wait still needs a release plus a stable sample, the free-VRAM target path is unchanged, and with no pre-kill sample the function behaves exactly as before.
Results (base vs head)
Real A100 (Colab), a spawned CUDA worker holding 1 GiB that exits and gets joined, then the settle wait with no target. 5 repeats per arm, with base run twice as an A/A check:
The worker exit code was 0 in every trial, and free VRAM matches between arms: the memory had already come back before either arm's first poll. The final rule asks for a second stable poll after a pre-kill-only release, which on this already-settled card returns at 0.50 s by construction (one extra 0.25 s poll; not re-measured on the GPU). On the switch path this saves about 1.5 s, minus one telemetry read (an
nvidia-smi/amd-smicall on that path, typically 50 to 200 ms, taken before the worker is joined).Tests
studio/backend/tests/test_inference_worker_vram_settle.py(new, 7 tests, including the order cancel, sample, shutdown and the no-target guard inload_model): a release before the first poll is recognised with a pre-kill sample (2.0 s -> 0.5 s); a small gain before the first poll followed by the bulk later returns at the same time as without the sample; no release, or a gain under the tolerance, still waits to the deadline; a late release still has to settle; a sample from other devices is ignored; the target path and the unreadable-telemetry answers are unchanged. These fail on base, which has no helper.test_mlx_bnb_substitution,test_cached_gguf_routes,test_export_reap, pre-existing on this host). The pre-kill sample is wrapped so an unreadable telemetry call keeps the plain wait.pytest tests/studio -k "orchestrator or settle or switch": 214 passed on both base and head.Not covered
load_modelwiring was not run end to end on a GPU (this session has no local GPU). The GPU run calls the settle wait with a real dead worker and a real NVML sample. No test drives the pre-kill sample through a realload_modelswitch (a source-order test pins where it is taken); the CPU switch run never enters the settle loop.get_visible_gpu_utilizationread as before.