Skip to content

Studio: pick up a GPU whose driver loads after Studio starts - #13232

Merged
danielhanchen merged 4 commits into
mainfrom
studio-refresh-late-gpu-count
Oct 11, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
studio-refresh-late-gpu-count

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 10, 2026 •

Copy link
Copy Markdown
Member

Fixes the "extra GPU loads its driver late and Studio never notices" part of #9510

The bug

get_physical_gpu_count() caches the nvidia-smi -L count for the life of the process, and nothing outside the tests ever resets it. If a second GPU's driver finishes loading after Studio has started (common with autostart at login, eGPUs, or a slow second card), Studio keeps thinking the host has one GPU until it is restarted.

The cached count feeds _get_parent_visible_gpu_spec() when no visibility mask is set (numeric_ids = list(range(get_physical_gpu_count()))), so the late card is:

  • missing from the GPU picker (get_backend_visible_gpu_info() drops rows outside the parent-visible ids), and
  • refused when chosen by id, for chat loads, training, export and diffusion:
boot: physical 1 parent ids [0]
late: nvidia-smi says 2
late: physical 1 parent ids [0]
resolve([1]) ValueError: Invalid gpu_ids [1]: IDs must be physical GPU IDs between 0 and 0. Rejected IDs: [1]. Parent-visible GPUs: [0]
backend-visible devices (GPU picker): [0]

#9858 already handles the "no usable GPU at startup, one appears later" case through re-detection. It does not cover going from one GPU to two, because the count cache is not part of that reset.

The fix

On NVIDIA, a cached count is re-read at most once a minute and kept only if it grew:

  • The re-probe runs on a background thread (the same single-flight helper the GPU inventory refresh uses). Callers always get the cached count at once; growth shows on the next call. Async routes such as the GGUF gpu_ids resolver reach this, so a synchronous nvidia-smi there would stall the event loop (0.1 to 0.3 s here, up to the 5 s timeout on a busy driver).
  • A failed probe or a lower count keeps the old value, so detection only ever widens and a transient nvidia-smi timeout cannot hide a card.
  • Only unindented GPU N: rows of nvidia-smi -L count on a re-read, so MIG instances created after startup never look like new GPUs. Startup counting is unchanged.
  • The TTL is stamped after the probe returns, so the first expiry also outlives gpu_query's own 60 s cache entry for nvidia-smi -L (Studio: cache and coalesce nvidia-smi reads in the backend #11995).
  • If nvidia-smi failed at startup and the count came from the torch fallback, a later SMI answer that is larger replaces it, which is what a fresh start would have produced.
  • The per-GPU memory table (min_per_gpu_N) is sized from the parent-visible ids as well as the visible count, so auto-selection over a grown inventory still checks the per-GPU fit.
  • ROCm, XPU, MLX and CPU are unchanged: amd-smi is uncached and can take seconds, and torch cannot see devices that appear after CUDA initialises anyway.

After the fix, the same sequence gives:

Physical GPU count grew from 1 to 2; using the new GPUs
late: physical 2 parent ids [0, 1]
resolve([1]) = [1]
backend-visible devices (GPU picker): [0, 1]

Verification

  • studio/backend/tests/test_physical_gpu_count_refresh.py (13 cases): a late second GPU becomes selectable, no re-probe inside the TTL, a failed / lower / zero probe never hides a GPU, MIG child rows are not GPUs, the TTL starts after the probe, the memory table covers every parent-visible GPU, the re-probe never blocks the caller (a wedged driver returns the cached count in under 1 s), and MLX / ROCm are never re-probed. Each of them fails on main or on the earlier revision it guards against.
  • Real subprocess path on an 8x B200 host, with a stand-in nvidia-smi on PATH that adds MIG rows and then a second GPU mid-run (TTLs shortened to 2 s):
main this PR
MIG rows added count 1 count 1
second GPU added count stays 1, gpu_ids=[1] refused count 2 within one call, gpu_ids=[1] accepted
slowest get_physical_gpu_count() call 0.06 ms 1.5 ms (thread start, once per minute)
  • Real nvidia-smi on the same host: 8 GPUs, ids [0..7], unchanged.
  • Cached-path cost per call: 26 ns on main, 161 ns here; nothing calls it per token.
  • Every hardware / GPU / device / VRAM test file in studio/backend/tests (5,000+ tests) gives the same result on main and on this branch; the failures present on this host are on main too.

@danielhanchen danielhanchen self-assigned this Oct 10, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 10, 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 ✅ Completed 2026-10-11T03:33:45.746261Z 6fce981 Manual request
ℹ️ 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.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 10, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 52c70872e4

ℹ️ 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. Hooray!

Reviewed commit: 9082a9ba9c

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

@NilayYadav NilayYadav left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review + reproduction evidence

Pinned review: 52c70872e4911b5645b646b729ee4a95c646366f. No blocking findings; three non-blocking suggestions below.

I reproduced the late second GPU on fresh upstream main (Linux, 2 x NVIDIA L4, Chromium 1440x900). The second card was hidden from nvidia-smi at startup and exposed mid-session, which stands in for a driver that finishes loading after Studio starts:

  • main b15cda912a: more than 3 minutes after the second card appeared, /api/system/gpu-visibility still returned parent_visible_gpu_ids: [0] with one device. Settings > System listed only GPU 0, with the VRAM tile at 0 GiB / 22.5 GiB. Loading unsloth/Qwen3-0.6B-GGUF with gpu_ids: [1] returned HTTP 400: Invalid gpu_ids [1]: IDs must be physical GPU IDs between 0 and 0. Rejected IDs: [1]. Parent-visible GPUs: [0].
  • PR head 52c70872e4: 87 s after the card appeared, the log printed Physical GPU count grew from 1 to 2; using the new GPUs. parent_visible_gpu_ids became [0, 1], Settings > System listed GPU 0 and GPU 1, and the VRAM tile read 0 GiB / 45 GiB. The same load with gpu_ids: [1] returned HTTP 200 "status": "loaded", "gpu_ids": [1]. nvidia-smi showed the 976 MiB llama-server on GPU 1, and a chat completion against it answered normally.

PR 13232 before/after

  • Unchanged flows: at startup both sides were identical: one row, [0], and gpu_ids: [0] loads on GPU 0 (HTTP 200, 976 MiB on GPU 0). The Studio logs had the same warnings on both sides; the PR added only the single "grew" info line.
  • Tests: the new test_physical_gpu_count_refresh.py gives 12 passed on the PR and 6 failed on main (growth, torch-fallback replacement, MIG rows, TTL stamp, memory table). Across the 21 existing test files that touch these helpers: 2080 passed and 7 skipped on both main and the PR. The one failure, test_gpu_query_cache::test_studio_load_unload_and_training_methods_invalidate, fails on main too.
  • Other platforms: ROCm, XPU, MLX and CPU never stamp the refresh timestamp, so they take main's branch unchanged.
  • Cost: steady state is one nvidia-smi -L per minute per process. The cached path stays in the nanoseconds.

Suggestions (non-blocking, not reproduced on this hardware):

  1. Diffusers loads on a late card. detect_hardware() initialises CUDA in the Studio process (torch.cuda.get_device_properties(0)), and diffusers image loads run in that process (core/inference/diffusion_device.py:364). With a real late driver, gpu_ids: [1] now passes validation for an in-process diffusers load, but that process's torch cannot open the card. The load would then fail with invalid device ordinal after the resident model is evicted, where main gave a clean 400. llama-server, training and export spawn fresh processes and are fine. A guard in resolve_selected_cuda_ordinal would turn this back into a clear "restart Studio to use this GPU" message: when CUDA is already initialised, refuse an ordinal at or above torch.cuda.device_count().
  2. Event loop. _resolve_gguf_gpu_ids_for_request is async and calls resolve_requested_gpu_ids inline (routes/inference.py:15558). That call can now run nvidia-smi -L once a minute, so a slow driver would stall the loop for up to the 5 s timeout. Its siblings in the same helper already use asyncio.to_thread:
        resolved = await asyncio.to_thread(
            resolve_requested_gpu_ids,
            gpu_ids,
            is_vulkan = ids_are_vulkan_ordinals,
        )
  1. Log noise. On CUDA hosts with no working nvidia-smi (for example NVIDIA_DRIVER_CAPABILITIES=compute containers), the first call still stamps _physical_gpu_count_checked_at. Every later refresh then logs nvidia-smi -L failed at warning level once a minute. Skipping the stamp when the startup probe returned None would avoid this.

Conclusion: real defect (issue #9510, item 4), fixed on the reported path with no regression in the startup or GPU 0 flows. Ready to merge once upstream CI finishes green. The suggestions above can follow separately.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 10, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 10, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 10, 2026
@unslothai unslothai deleted a comment from danielhanchen Oct 10, 2026
@unslothai unslothai deleted a comment from danielhanchen Oct 10, 2026
@unslothai unslothai deleted a comment from danielhanchen Oct 10, 2026
@unslothai unslothai deleted a comment from danielhanchen Oct 10, 2026
@unslothai unslothai deleted a comment from danielhanchen Oct 10, 2026
@unslothai unslothai deleted a comment from danielhanchen Oct 10, 2026
@unslothai unslothai deleted a comment from danielhanchen Oct 10, 2026
@unslothai unslothai deleted a comment from danielhanchen Oct 10, 2026
@unslothai unslothai deleted a comment from danielhanchen Oct 10, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 6fce981196

ℹ️ 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 merged commit c0f64cd into main Oct 11, 2026
@danielhanchen
danielhanchen deleted the studio-refresh-late-gpu-count branch October 11, 2026 05:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants