Skip to content

fix(studio): resolve UUID-form CUDA_VISIBLE_DEVICES to physical GPU indices - #8917

Merged
danielhanchen merged 8 commits into
unslothai:mainfrom
rsd-darshan:fix/gpu-uuid-visible-devices
Oct 6, 2026
Merged

danielhanchen merged 8 commits into
unslothai:mainfrom
rsd-darshan:fix/gpu-uuid-visible-devices

Conversation

@rsd-darshan

@rsd-darshan rsd-darshan commented Aug 15, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #8873. On a multi-GPU NVIDIA host, setting CUDA_VISIBLE_DEVICES to GPU UUIDs (GPU-<uuid>,GPU-<uuid>) instead of indices made Studio treat the GPUs as unpinnable: the per-model GPUs picker never rendered and any explicit gpu_ids load was rejected with HTTP 400. Unpinned GGUF loads were also placed on cards the mask hid.

Changes

  • nvidia.resolve_uuid_mask() maps a full-GPU UUID mask to physical nvidia-smi indices in mask order, reusing the existing _uuid_visible_ordinal_map matcher and the cached gpu_query read. MIG entries, unknown or ambiguous UUIDs, mixed index/UUID masks and duplicates stay unresolved, exactly as before.
  • _get_parent_visible_gpu_spec() uses it on CUDA hosts while CUDA_DEVICE_ORDER is PCI_BUS_ID (Studio's default), so a resolvable UUID mask behaves exactly like the equivalent numeric mask. FASTEST_FIRST and ROCm keep the old path.
  • The llama.cpp backend's own mask parsers (_resolve_visible_physical_ids, _visible_devices_mask) fall back to the same resolution, so an unpinned GGUF load stays inside the mask instead of picking from every host GPU.
  • The resolution is cached for 30 s (failures included) and dropped on hardware re-detection, so a failing or hung nvidia-smi does not cost its timeout on every poll.

Testing

Real GPUs (B200 host), Studio base vs head, CUDA_VISIBLE_DEVICES=<uuid of GPU 5>,<uuid of GPU 2>, card verified with nvidia-smi --query-compute-apps:

Load main this PR this PR, numeric mask 5,2
GGUF, auto GPU 3 / 0 / 7 (outside the mask) GPU 5 GPU 5
HF, auto GPUs 2+5 GPU 2 or 5 GPU 5
GGUF / HF, gpu_ids=[5] HTTP 400 GPU 5 GPU 5
GGUF / HF, gpu_ids=[2] HTTP 400 GPU 2 GPU 2
  • Spoofed nvidia-smi matrix, 342 cells (19 masks x 3 CUDA_DEVICE_ORDER x working / failing / empty / missing / hung nvidia-smi, plus ROCm routing): 330 identical to main, 12 newly resolved and all match CUDA's own UUID semantics, 0 violations.
  • studio/backend/tests/test_gpu_selection.py covers resolution order, fail-closed cases, the FASTEST_FIRST / ROCm gates, the llama.cpp paths and the cache; the new tests fail with the fix reverted.
  • GPU / llama.cpp related backend suites: 3866 passed, the single failure also fails on main.
  • Studio regression switchboard base vs head: no functional regression.

Before / after

Run settings with GPU Memory set to Manual on a two-GPU UUID mask:

GPUs picker before and after

chatgpt-codex-connector[bot]

This comment was marked as outdated.

@rsd-darshan

This comment has been minimized.

Comment thread studio/backend/utils/hardware/nvidia.py Resolved
Comment thread studio/backend/utils/hardware/nvidia.py Resolved
chatgpt-codex-connector[bot]

This comment was marked as outdated.

Comment thread studio/backend/utils/hardware/nvidia.py Resolved
Comment thread studio/backend/utils/hardware/nvidia.py Resolved
Comment thread studio/backend/utils/hardware/nvidia.py Resolved
Comment thread studio/backend/utils/hardware/nvidia.py Resolved
Comment thread studio/backend/utils/hardware/nvidia.py Resolved
@rsd-darshan

This comment has been minimized.

chatgpt-codex-connector[bot]

This comment was marked as outdated.

Comment thread studio/backend/utils/hardware/nvidia.py Resolved
chatgpt-codex-connector[bot]

This comment was marked as outdated.

Comment thread studio/backend/utils/hardware/nvidia.py Resolved
Comment thread studio/backend/utils/hardware/nvidia.py Resolved
chatgpt-codex-connector[bot]

This comment was marked as outdated.

Comment thread studio/backend/utils/hardware/nvidia.py Resolved
chatgpt-codex-connector[bot]

This comment was marked as outdated.

Comment thread studio/backend/utils/hardware/nvidia.py Resolved
chatgpt-codex-connector[bot]

This comment was marked as outdated.

Comment thread studio/backend/utils/hardware/nvidia.py Resolved
chatgpt-codex-connector[bot]

This comment was marked as outdated.

Comment thread studio/backend/utils/hardware/nvidia.py Resolved
chatgpt-codex-connector[bot]

This comment was marked as outdated.

Comment thread studio/backend/models/inference.py Resolved
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 6, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 6, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 6, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 6, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 6, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 6, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 6, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 6, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 6, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 6, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 6, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 6, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 6, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 6, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 6, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 6, 2026
LlamaCppBackend parsed the visibility mask on its own and read a UUID mask as
no mask, so an unpinned GGUF load picked from every host GPU and re-emitted a
numeric CUDA_VISIBLE_DEVICES naming a card the parent had hidden. Fall back to
the parent GPU spec, which now resolves full-GPU UUID masks.
_visible_devices_mask read a UUID CUDA_VISIBLE_DEVICES as no mask, so the
unpinned GGUF pick still saw every host GPU. Reuse _resolve_visible_physical_ids.
gpu_query never caches a failed nvidia-smi read and the parent GPU spec is read
on every poll and load, so a failing or hung nvidia-smi under a UUID mask cost
42 ms to 5 s per call (spoofed nvidia-smi; 4 us on main). Now ~6 us after the
first read.
Key it on gpu_query's static generation, which invalidate_detection() bumps on
eGPU hotplug or a driver reset, so a re-detected inventory is read at once.
danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 6, 2026
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 6, 2026
_nvidia_inference_devices reparsed CUDA_VISIBLE_DEVICES with int() once the
mask resolved, raising on UUID tokens and dropping the inventory.
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 6, 2026
…olver

The torch-backend visibility spec reads HIP or ZE masks under ROCm or XPU torch,
so a CUDA llama.cpp backend there could map the UUID mask to the wrong card.
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 6, 2026
@danielhanchen

Copy link
Copy Markdown
Member

Review record for #8917, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#476.

Findings are quoted as posted and attributed to the account that posted them. The reactions shown are the triage recorded on the mirror; none were re-applied here. Commits are named as the mirror's, with this PR's equivalent where one was found.

Round 1: reviewed mirror c89bfc87e (no equivalent commit found on this PR)

chatgpt-codex-connector[bot] on studio/backend/utils/hardware/nvidia.py (on the mirror)

@@ -75,6 +76,50 @@ def _uuid_visible_ordinal_map(
     return visible_ordinals
 
 
+_UUID_MASK_TTL_S = 30.0
+_uuid_mask_cache: dict[str, tuple[float, Optional[list[int]]]] = {}
+
+
+def resolve_uuid_mask(parent_cuda_visible_devices: str) -> Optional[list[int]]:
+    """Physical nvidia-smi indices for a full-GPU UUID mask, in mask order; None if any token is unresolvable."""
+    # gpu_query never caches a failure, and the parent GPU spec is read on every poll and load:
+    # without this a hung nvidia-smi costs its full timeout per call.
+    hit = _uuid_mask_cache.get(parent_cuda_visible_devices)
+    if hit is not None and time.monotonic() - hit[0] < _UUID_MASK_TTL_S:
+        return None if hit[1] is None else list(hit[1])

P2 Badge Invalidate the UUID map during hardware re-detection

When an eGPU is attached/detached or the NVIDIA driver is reset, invalidate_detection() invalidates gpu_query's static inventory so changed device indices are observed immediately, but this separate cache is never invalidated. A repeated UUID mask can therefore return the old physical indices for up to 30 seconds without consulting the refreshed inventory, causing the picker or a load started during that window to target a missing or different GPU. Tie this cache to the hardware/static-query generation or clear it from the existing invalidation path.

Useful? React with 👍 / 👎.

Accepted on the mirror

Reply from danielhanchen:

Fixed in 55a6152: the cache is keyed on gpu_query's static generation, so invalidate_detection() drops it.

Round 2: reviewed mirror b00e566a6 (no equivalent commit found on this PR)

chatgpt-codex-connector[bot] on studio/backend/core/inference/llama_cpp.py (on the mirror)

@@ -12445,7 +12452,10 @@ def _visible_devices_mask(env_name: str) -> Optional[set[int]]:
         try:
             return set(int(x.strip()) for x in raw.split(",") if x.strip())
         except ValueError:
-            return None
+            if env_name != "CUDA_VISIBLE_DEVICES":
+                return None
+        ids = LlamaCppBackend._resolve_visible_physical_ids()
+        return set(ids) if ids is not None else None

P2 Badge Preserve resolved UUID order in cross-vendor inventory

When Studio uses a CUDA llama.cpp backend with a non-CUDA PyTorch backend (for example, a CPU-only torch install on an NVIDIA host) and CUDA_VISIBLE_DEVICES contains UUIDs, this newly returns a physical-ID set, so _nvidia_inference_devices() proceeds past its unresolved-mask guard but then still constructs order with int() from the raw UUID tokens at hardware.py:6041. The resulting ValueError is swallowed by get_cross_vendor_inference_gpu_info(), causing the CUDA inference inventory to disappear; return or otherwise propagate the resolved ordered IDs so that path does not reparse the UUID mask as integers.

Useful? React with 👍 / 👎.

Accepted on the mirror

Reply from danielhanchen:

Fixed in cc449b2: the order now comes from the resolved UUID mask instead of int() on the raw tokens.

Round 3: reviewed mirror 38189ff44 (no equivalent commit found on this PR)

chatgpt-codex-connector[bot] on studio/backend/core/inference/llama_cpp.py (on the mirror)

@@ -12445,7 +12452,10 @@ def _visible_devices_mask(env_name: str) -> Optional[set[int]]:
         try:
             return set(int(x.strip()) for x in raw.split(",") if x.strip())
         except ValueError:
-            return None
+            if env_name != "CUDA_VISIBLE_DEVICES":
+                return None
+        ids = LlamaCppBackend._resolve_visible_physical_ids()

P2 Badge Resolve CUDA UUIDs independently of the torch backend

When the installed llama.cpp backend is CUDA but PyTorch uses ROCm or XPU, this fallback delegates a CUDA mask to _resolve_visible_physical_ids(), which selects visibility according to the PyTorch backend rather than env_name. With ROCm and HIP_VISIBLE_DEVICES=0, for example, a UUID-form CUDA_VISIBLE_DEVICES is therefore reported as NVIDIA physical ID 0 regardless of which UUID it names; with XPU it remains unresolved because _get_parent_visible_gpu_spec() reads ZE_AFFINITY_MASK. Consequently the cross-vendor path in _nvidia_inference_devices() can estimate capacity for the wrong NVIDIA card or omit the CUDA inventory. Resolve the raw CUDA mask directly through the NVIDIA UUID resolver here instead of the active torch visibility specification.

Useful? React with 👍 / 👎.

Accepted on the mirror

Reply from danielhanchen:

Fixed in 1394dda: _visible_devices_mask resolves the raw CUDA mask through nvidia.resolve_uuid_mask, independent of the torch backend.

Round 4: reviewed mirror 321cd9962 (no equivalent commit found on this PR)

chatgpt-codex-connector[bot] (on the mirror)

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

Reviewed commit: 321cd99625

No triage reaction was recorded on the mirror

Verdict

The round converged at mirror 321cd9962 (no equivalent commit found on this PR).

This is the review as it stood at that commit. Anything pushed to this PR afterwards was not part of it.

@danielhanchen
danielhanchen merged commit 5169487 into unslothai:main Oct 6, 2026
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.

[Bug] UUID-form CUDA_VISIBLE_DEVICES silently hides the per-model GPU picker on a healthy multi-GPU CUDA host

2 participants