Skip to content

Studio: automatic GPU placement caches failed probes, skips MIG winners, never blocks the event loop - #14118

Merged
danielhanchen merged 6 commits into
mainfrom
r8-studio-auto-gpu-probe-caches
Oct 18, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
r8-studio-auto-gpu-probe-caches

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 18, 2026 •

Copy link
Copy Markdown
Member

Problem

Three small costs in the automatic GPU placement added by #14020, all on multi-GPU NVIDIA hosts:

  1. hardware._nvidia_mig_gpu_indices_or_none() ran its own nvidia-smi -L, bypassing the MIG discovery cache from Studio: read whole-GPU uuid masks from nvidia-smi rows and cache the MIG set #14017. A successful listing was served by gpu_query's cache, but a failed one had no backoff, and with UNSLOTH_GPU_QUERY_CACHE=0 every call spawned nvidia-smi (one per automatic route, two per catalog refresh).

  2. Under CUDA_DEVICE_ORDER=FASTEST_FIRST with a numeric mask, a failed bus-id join was never cached, so every auto_select_gpu_ids() re-spawned the driver-API child (about 300 ms here, up to its 15 s timeout on a hung driver).

  3. nvidia-smi CSV output is not quoted, so a GPU name containing a comma made get_card_identities() return nothing. Every catalog refresh then ranked live, and identical cards under FASTEST_FIRST got an explicit card 0 instead of staying unpinned.

  4. On a MIG host, automatic image/video placement ranked cards by their physical nvidia-smi rows and could return card 1, which CUDA maps to a MIG slice (or to nothing before driver R570). Main before Studio: automatic image/video loads pick the freest GPU, and GPU picks respect what each card can run #13928 stayed on the current card there.

  5. The event-loop gate auto_cuda_ranking_possible_cached() called get_device() for non-CUDA hosts, which waits on the hardware-detection lock while a detection pass runs, stalling the event loop (about 1.3 s per call in a spoofed run) on Mac, CPU and XPU hosts too.

Fix

  1. _nvidia_mig_gpu_indices_or_none() delegates to nvidia.get_mig_gpu_indices_or_none(), so it shares that cache, its failure backoff and its sequence-number rules.

  2. A failed bus-id join is remembered for 300 s per (mask, order, static generation); hardware re-detection still retires it at once.

  3. Card identities rejoin a name that spans several CSV fields, as the inventory parser already does.

  4. A row-ranked winner other than card 0 is only taken when MIG discovery confirms no MIG card among the visible ones; otherwise card 0 stays explicit.

  5. The cached gate answers from memory only: non-CUDA or ROCm is False, and detection still in flight defers to the worker thread.

Single-GPU, CPU, Apple and ROCm hosts never reach items 1-4.

Tests

  • New tests/test_media_auto_gpu_mig_and_event_loop.py: 6 of 7 fail on main, all pass here.

  • New tests/test_media_auto_gpu_probe_caches.py: 3 of its 5 tests fail on main, all pass here.

  • test_mig_listing_parses_slices disables gpu_query's cache in its stub now that the helper shares it.

  • GPU / hardware / media suites: 3467 passed, 3 skipped. The 3 failures in test_rocm_windows_vram_7452.py are the same on main.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 18, 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-18T11:18:31.046435Z d0f8e74 Manual request
🔒 Security Review ⚠️ Failed 2026-10-18T08:21:22.314186Z e5018d0 PR opened
ℹ️ 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: e5018d04a6

ℹ️ 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 changed the title Studio: automatic GPU placement caches failed probes and parses comma GPU names Studio: automatic GPU placement caches failed probes, skips MIG winners, never blocks the event loop Oct 18, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 46e0b445a9

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

Comment on lines +504 to +505
if not hw.DETECTION_COMPLETE.is_set():
return None

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Avoid re-entering hardware detection from the event loop

The completion check does not guarantee that the subsequent auto_cuda_ranking_possible() call is nonblocking: a forced detection can acquire _DETECT_LOCK and clear DETECTION_COMPLETE immediately after this check, after which auto_cuda_ranking_possible() calls get_device() and waits on that lock from the event-loop thread. This can still stall all async request handling for the duration of a slow detection pass; compute the cached result without calling get_device(), or move the potentially locking call to the worker.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in d0f8e74: auto_cuda_ranking_possible_cached no longer calls auto_cuda_ranking_possible(). It answers from memory only (the cached physical count unmasked, or the numeric CUDA mask parsed by the same rule as get_parent_visible_gpu_ids), so a forced re-detection holding _DETECT_LOCK cannot stall the event loop. A stray HIP/ROCR mask still defers to the worker gate. Added test_cached_gate_never_calls_get_device.

Comment on lines +471 to 474
mig = hw._nvidia_mig_gpu_indices_or_none()
if mig is None or set(visible) & mig:
return 0
return winner

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Detect MIG mode even when no instances exist

On a host where a physical GPU has MIG mode enabled but currently has no GPU/compute instances, nvidia-smi -L contains no indented MIG row, so _nvidia_mig_gpu_indices_or_none() returns an empty set and this branch accepts the physical-row winner. If that unavailable physical row wins as ordinal 1 while only card 0 is CUDA-visible, the returned ordinal does not exist and placement silently falls back unpinned after the scope/set-device failure. MIG mode itself, or the physical-row-to-CUDA-ordinal mapping, must be verified before returning the row-ranked winner.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in d0f8e74: _nvidia_mig_gpu_indices_or_none now unions the -L instance rows with cards whose mig.mode.current is Enabled (new nvidia.get_mig_mode_gpu_indices_or_none, cached per static generation with the same failure backoff as the -L probe). A MIG-mode card with no instances therefore keeps card 0, and a failed mode query reads as unknown, which also keeps card 0.

Comment on lines +69 to +70
t.join()
if was:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Restore the detection event to its original state

When this test starts with DETECTION_COMPLETE clear, detect_hardware() sets it and the cleanup never clears it again because it only handles the originally-set case. The first parameterized invocation therefore leaves the shared hardware module in the inconsistent state DEVICE is None (restored by monkeypatch) while the completion event remains set, making later hardware and route tests order-dependent; restore both event states in the finally block.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in d0f8e74: the finally block now clears DETECTION_COMPLETE when it started clear, so the event is restored either way.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

danielhanchen added a commit to Datta0/unsloth-staging-3 that referenced this pull request Oct 18, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: d0f8e74e4c

ℹ️ 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 a411258 into main Oct 18, 2026
@danielhanchen
danielhanchen deleted the r8-studio-auto-gpu-probe-caches branch October 18, 2026 12:33
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