Skip to content

Studio: recognise a worker VRAM release that finished before the first settle poll - #16404

Merged
danielhanchen merged 21 commits into
mainfrom
perf/r24-unload-settle
Nov 2, 2026
Merged

danielhanchen merged 21 commits into
mainfrom
perf/r24-unload-settle

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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_subprocess has 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_mib helper 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:

arm settle wait median min / max free MiB after join free MiB after settle
base A 2.000 s 2.000 / 2.000 40441 40441
base B (A/A) 2.000 s 2.000 / 2.000 40441 40441
head without pre-kill sample 2.000 s 2.000 / 2.000 40441 40441
head, one stable poll (first revision) 0.250 s 0.250 / 0.250 40441 40441

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-smi call 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 in load_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.
  • Base vs head differential over 4000 seeded scripted telemetry sequences (1-2 devices, jumps, noise, unreadable samples, varied targets, deadlines, intervals and tolerances). Without a pre-kill sample, result, probe count and elapsed time are identical in all 4000. With one, head differs in 264 cases, and in each of them it only returns True earlier on the no-target path (never a different answer on a target).
  • Backend tests importing the orchestrator (95 files) on top of Studio: wait on worker exit instead of fixed sleeps when switching models #16344: 8076 passed, with the same 5 failures on base and head (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

  • The full load_model wiring 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 real load_model switch (a source-order test pins where it is taken); the CPU switch run never enters the settle loop.
  • ROCm, XPU and Windows telemetry were not run. They use the same get_visible_gpu_utilization read as before.
  • Apple Silicon is not helped: the parent's MLX reading cannot see the worker, so no release is ever seen and the 2 s wait stays. That is left for a separate change.

danielhanchen and others added 10 commits October 31, 2026 15:26
…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.
…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.
@danielhanchen danielhanchen self-assigned this Nov 2, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-11-02T14:09:46.900809Z
We couldn’t complete this review. Comment @codex review to try again.
a57b77f Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 032c057a93

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 51d7514946

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

Base automatically changed from perf/r18-model-switch-waits to main November 2, 2026 13:40
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: a57b77fa0c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@danielhanchen
danielhanchen marked this pull request as ready for review November 2, 2026 14:09
@danielhanchen
danielhanchen merged commit dfad9c5 into main Nov 2, 2026
@danielhanchen
danielhanchen deleted the perf/r24-unload-settle branch November 2, 2026 14:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant