Skip to content

Installer: GPU detection follow-ups after #6174 (poisoned venv repair, llama.cpp routing, probe bounds) - #6183

Merged
danielhanchen merged 6 commits into
mainfrom
gpu-detection-followups
Jun 11, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
gpu-detection-followups

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Follow-up to #6174 (#6172). That fix hardened the two torch-installer detectors; this PR ports the same hardening to the remaining GPU-detection pathways and adds recovery for venvs already poisoned by the original bug.

Changes

1. Poisoned venv repair (studio/install_python_stack.py)
A venv that got ROCm torch on an NVIDIA Linux host before #6174 stayed broken on unsloth studio update: a torch+rocm wheel satisfies the version constraint, so nothing reinstalled CUDA torch (a full install.sh rerun rebuilds the venv and was the only recovery). New _ensure_cuda_torch detects the poisoning signature (torch links HIP/ROCm while NVIDIA is the usable GPU) and force-reinstalls CUDA torch from the right cu* index, resolved with the same driver-version ladder as install.sh. Opt-outs respected: UNSLOTH_TORCH_BACKEND=rocm/cpu, UNSLOTH_ROCM_TORCH_INSTALLED=1, and CUDA_VISIBLE_DEVICES=-1 or empty (deliberately hidden GPU on mixed AMD+NVIDIA hosts). Healthy CUDA torch, deliberate CPU wheels, missing torch, macOS, and Windows are untouched.

2. llama.cpp prebuilt routing (studio/install_llama_prebuilt.py)
detect_host() was nvidia-smi-only; with smi missing or wedged, an NVIDIA host degraded to a CPU llama.cpp build, and with ROCm tools co-installed it could route to ROCm assets. It now has the /proc/driver/nvidia/gpus fallback and skips ROCm probes when NVIDIA is usable. Forwarded --rocm-gfx/--has-rocm overrides still win.

3. Studio setup routing (studio/setup.sh)
The GPU summary now classifies NVIDIA first through a timeout-bounded probe with the /proc fallback, and AMD probes (also bounded now) are skipped on usable-NVIDIA hosts. The AMD detection gains the KFD vendor_id 4098 sysfs fallback so AMD hosts without rocminfo/amd-smi are still recognized. The llama.cpp source-build fallback no longer picks GGML_CUDA/GGML_HIP from toolkit presence alone (nvcc in a CPU container, leftover hipcc) -- it requires the matching GPU to actually be detected.

4. Probe bounds (install.sh, install.ps1, studio/setup.ps1)
Every nvidia-smi invocation in install.sh and both PowerShell installers is now bounded (10s); a wedged driver can no longer hang the installer before the fallbacks run. The Windows probes additionally require a real GPU N: row from nvidia-smi -L instead of accepting bare exit code 0. Behavior is unchanged when probes are healthy or where the timeout binary is absent (macOS).

5. Backend export robustness (install.sh)
UNSLOTH_TORCH_BACKEND is now classified from the final path segment of TORCH_INDEX_URL, so a custom UNSLOTH_PYTORCH_MIRROR whose base path contains rocm or gfx cannot mislabel a cu*/cpu index as ROCm.

Test plan

  • 3 new test files, 53 new tests: test_cuda_repair.py (repair fires only on the poisoning signature; full index ladder; every opt-out), test_gpu_detection_followups.py (proc fallback, NVIDIA precedence, override interplay, setup.sh gating, backend leaf classification driven through sh with adversarial mirror URLs), test_probe_timeouts.py (source-level bounds plus a live hung-smi test)
  • Full tests/studio/install/ suite: 788 passed, 1 skipped
  • tests/sh/test_get_torch_index_url.sh: 32 passed
  • PowerShell AST parse of both .ps1 files plus behavioral simulation: exit-0-no-GPU rejected, MIG output still detected, hung smi killed at 10s
  • Cross-process handoff simulation: install.sh backend export reaches a child install_python_stack.py and gates the repair correctly for cuda/cpu/rocm
  • 95-row OS x GPU behavior matrix ([Linux, WSL, macOS, aarch64] x [NVIDIA, AMD, CPU, mixed] including smi-dead/timeout/proc-only variants) compared against main: all previously working pathways unchanged
  • Verify on a machine that was poisoned by the pre-fix: prevent ROCm torch from installing on NVIDIA Linux hosts #6174 bug that unsloth studio update now restores CUDA torch

Ports the NVIDIA-priority and /proc/driver/nvidia/gpus hardening from #6174
to the remaining pathways and adds recovery for already-poisoned venvs:

- install_python_stack.py: add _ensure_cuda_torch so 'unsloth studio update'
  force-reinstalls CUDA torch when the venv carries a ROCm build on an NVIDIA
  Linux host (the pre-#6174 poisoning signature). Honors UNSLOTH_TORCH_BACKEND,
  UNSLOTH_ROCM_TORCH_INSTALLED, and CUDA_VISIBLE_DEVICES=-1/'' opt-outs; never
  touches healthy CUDA, deliberate CPU wheels, macOS, or Windows.
- install_llama_prebuilt.py: detect_host gains the /proc NVIDIA fallback and
  skips ROCm probes when NVIDIA is usable; forwarded --rocm-gfx/--has-rocm
  overrides still win.
- setup.sh: GPU summary classifies NVIDIA first through a timeout-bounded
  probe with the /proc fallback; AMD probes are bounded and gain a KFD
  vendor_id 4098 fallback; the llama.cpp source build only selects
  GGML_CUDA/GGML_HIP when the matching GPU is actually detected.
- install.sh: bound both nvidia-smi calls with a 10s timeout (no behavior
  change when healthy or when the timeout binary is absent); classify the
  exported UNSLOTH_TORCH_BACKEND on the final index path segment so custom
  mirrors containing 'rocm'/'gfx' in their base path are not mislabeled.
- install.ps1 + setup.ps1: NVIDIA probes now require a real 'GPU N:' row from
  nvidia-smi -L under a 10s bound instead of bare exit code 0; later CUDA
  version and compute_cap queries are bounded too.

Tests: 3 new test files (50+ tests), suite at 788 passed.
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@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: 5eeb892df9

ℹ️ 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 thread studio/setup.sh
Comment on lines +856 to +858
# host is classified; the AMD probes themselves run under _setup_run_smi.
if _setup_has_usable_nvidia_gpu; then
_setup_nvidia_usable=true

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 Honor hidden NVIDIA devices before preferring CUDA

On mixed AMD+NVIDIA hosts that set CUDA_VISIBLE_DEVICES='' to hide all NVIDIA devices, this new NVIDIA-first check still marks NVIDIA usable because nvidia-smi -L is not filtered by that env var. That skips the AMD probes below and later routes llama.cpp toward CUDA/source-build instead of ROCm; this repo's Python detector already treats both empty and -1 as hidden, so the setup helper/branch should do the same before suppressing AMD detection.

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.

Good catch. Fixed in 0e0739c. The hidden-device policy now lives inside the usable-NVIDIA helpers themselves instead of at individual gates, so every consumer agrees:

  • install.sh: new _cvd_hides_nvidia checked first in _has_usable_nvidia_gpu
  • studio/setup.sh: same via _setup_cvd_hides_nvidia; the two ad-hoc -1 gate conditions are now redundant and removed
  • studio/install_python_stack.py: _has_usable_nvidia_gpu returns False when CUDA_VISIBLE_DEVICES is set to "" or -1 (whitespace tolerated), matching install_llama_prebuilt.py

Both forms now suppress NVIDIA before the AMD probes are gated, so a mixed host steered to its AMD card keeps the ROCm route end to end: torch index selection, llama.cpp repo routing, and the source-build backend. Covered by new shell scenarios in tests/sh/test_get_torch_index_url.sh (including the mixed host with hidden NVIDIA restoring rocm6.4) and the TestHiddenCvdNotUsable class driving all three implementations.

tests/studio/test_resolve_cuda_toolkit.ps1 extracts Resolve-CudaToolkit alone
into a child pwsh and stubs nvidia-smi with a .ps1 script. The bounded runner
is not in scope there (and ProcessStartInfo cannot dispatch .ps1 stubs), so
the DriverMaxCuda parse silently returned nothing and the major-mismatch
scenarios failed. Fall back to direct invocation when Invoke-NvidiaSmiBounded
is unavailable; production setup.ps1 always has it defined and keeps the
10s bound.
danielhanchen and others added 3 commits June 11, 2026 08:56
The NVIDIA-first guards added in this branch only special-cased
CUDA_VISIBLE_DEVICES=-1 at two setup.sh gates and ignored the empty-string
form entirely, while the Python detector (install_llama_prebuilt.py)
already treats both as hidden. On a mixed AMD+NVIDIA host steered to the
AMD card via CUDA_VISIBLE_DEVICES, the guards suppressed the AMD probes,
so setup.sh fell to a CPU llama.cpp build and install.sh picked CUDA
wheels instead of ROCm.

Move the policy into the helpers so every consumer agrees:

- install.sh: new _cvd_hides_nvidia checked first in _has_usable_nvidia_gpu
- studio/setup.sh: same via _setup_cvd_hides_nvidia; the two ad-hoc
  CUDA_VISIBLE_DEVICES=-1 gate conditions are now redundant and removed
- studio/install_python_stack.py: _has_usable_nvidia_gpu returns False
  when CUDA_VISIBLE_DEVICES is set to  or -1 (whitespace tolerated)

Tests: 5 new sh scenarios (hidden via , -1, padded -1, visible device,
and mixed host with hidden NVIDIA restoring the ROCm route) plus a pytest
class covering all three implementations behaviourally.

Addresses the review comment on the NVIDIA-first setup.sh block.
@danielhanchen
danielhanchen merged commit 2db9fad into main Jun 11, 2026
39 of 40 checks passed
@danielhanchen
danielhanchen deleted the gpu-detection-followups branch June 11, 2026 12:06
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