Skip to content

Studio: escape the dependency fast path when AMD torch is the wrong wheel - #9499

Merged
danielhanchen merged 8 commits into
unslothai:mainfrom
oobabooga:amd-rocm-fastpath-probe
Aug 23, 2026
Merged

danielhanchen merged 8 commits into
unslothai:mainfrom
oobabooga:amd-rocm-fastpath-probe

Conversation

@oobabooga

@oobabooga oobabooga commented Aug 21, 2026 •

Copy link
Copy Markdown
Member

Problem

studio/setup.sh skips the entire dependency pass when the installed unsloth version already matches PyPI. install_python_stack.py's _ensure_rocm_torch is the only thing that replaces a CPU or CUDA wheel with ROCm wheels, and it runs only inside that pass. So an AMD host whose venv received the wrong torch keeps it across every unsloth studio update, while setup prints dependencies up to date and the GPU line reports a healthy ROCm device. Training stays unavailable with nothing in the output pointing at why.

Reproduced on an AMD DevLab gfx1151 host: with torch 2.13.0+cpu in the venv and the package version current, nothing in the update path looks at the wheel.

Change

Before taking the fast path, setup.sh asks install_python_stack.py one question: is this venv's torch conclusively the wrong wheel for this host. Exit 0 forces the dependency pass, which then chooses and installs the wheel exactly as it does today. The repair itself is unchanged apart from calling the _generic_pytorch_rocm_tag helper it already ships instead of re-deriving that lookup inline, so both sides read the same table.

Conclusive means torch imported and reported a version carrying neither torch.version.hip nor a +rocm tag, on a host the repair will actually act on.

The eligibility gates mirror _ensure_rocm_torch in both directions:

  • An explicit ROCm pin commits to ROCm wheels regardless of the visible GPU, so it skips the hardware gates, as the repair does.
  • Otherwise the host qualifies on either of the two signals the repair accepts: a ROCm runtime that enumerates a GPU, or an arch read from the CPU model or named by UNSLOTH_ROCM_GFX_ARCH ([Bug] Strix Halo no GPU detected (Linux) #7301). Without the second, UNSLOTH_ROCM_GFX_ARCH=gfx1151 on a host with no working runtime stayed on CPU torch.
  • The two places the repair declines after its own gates pass are gates here too: an unreadable ROCm version, and a ROCm with no published wheel family. Forcing a pass the repair declines re-runs the whole dependency install and moves nothing.

On top of that parity, the preflight fails closed on cases the repair would act on but where a fast-path check has no business guessing. These are one-directional on purpose:

  • the probe did not run, torch did not import, or no version came back
  • HIP_VISIBLE_DEVICES, ROCR_VISIBLE_DEVICES or CUDA_VISIBLE_DEVICES is set to "" or -1
  • more than one gfx arch is visible, where the device the runtime selects decides the wheel family and rocminfo, amd-smi and the HIP layer disagree about how a mask renumbers devices

So the guarantee is one-way: a forced pass always has work to do, while some repairable hosts keep the fast path. test_the_preflight_and_the_repair_agree runs both sides under the same stubs over a host matrix and asserts it.

UNSLOTH_TORCH_BACKEND of cpu, cuda or xpu, a pin that is not a ROCm family, GGUF-only mode, non-Linux and non-x86_64 all keep the fast path as well.

The probe runs under timeout -k 5 180 and any non-zero status, a timeout included, keeps the fast path. A stalled probe on a wedged HIP runtime is not evidence about the wheel. It installs nothing.

Scope note: a torch that fails to import is left alone. It may well need repair, but "cannot be probed" is not the same claim as "is the wrong wheel", and forcing several gigabytes of reinstall on it belongs in its own change.

Cost is one interpreter start on the fast path, measured at 0.11s on an NVIDIA Linux host, where the answer is settled before torch is probed. The block sits outside the version-check if, not among the pin escapes, so the two existing tests that slice that region out of setup.sh are untouched.

Verification

On an AMD DevLab gfx1151 host (has_rocm_gpu True, ROCm (7, 2), inferred_gfx gfx1151), running the real flag:

venv / env exit meaning
no torch installed 1 unknown, fast path kept
torch 2.13.0+cpu 0 force the dependency pass
HIP_VISIBLE_DEVICES="" 1 every device hidden
UNSLOTH_TORCH_BACKEND=cpu 1 resolved backend wins
UNSLOTH_TORCH_INDEX_URL=.../cpu 1 another repair path owns the pin
UNSLOTH_ROCM_GFX_ARCH=gfx1151 0 named arch, same as the repair

pip freeze hashed identically before and after all six, so the probe changed nothing.

  • tests/studio/install/test_amd_fastpath_probe.py: 46 tests. 38 over the eligibility matrix, including that pip_install is unreachable from the probe, plus 8 parity rows that run the preflight and _ensure_rocm_torch under the same stubs and assert they agree. Removing either new gate makes the parity rows fail (checked by mutating each one).
  • tests/sh/test_setup_amd_fastpath_escape.sh: 14 assertions driving the real block out of setup.sh, covering exit 0, exit 1, exits 2/124/125/126/127/137, that the probe is spawned exactly once with only the module and the flag, and that a missing venv interpreter spawns nothing.
  • Locally: tests/sh 51/51; tests/studio/install/ plus tests/python/test_cross_platform_parity.py and tests/python/test_windows_no_torch_setup.py at 2620 passed, 118 skipped.
  • On the AMD host, tests/studio/install/ fails the same 6 tests with and without the change, and tests/sh the same 1. That baseline was measured with this change and Studio: name the GPU agent, not the CPU, in the rocminfo probe #9498 both applied.
  • bash -n studio/setup.sh, ruff check, git diff --check.

Related: #8473. Replaces the fast-path half of #8606.

@danielhanchen

Copy link
Copy Markdown
Member

Confirmed the gap is real on main, where _ensure_rocm_torch only runs inside the dependency pass that studio/setup.sh skips, so an AMD venv keeps a CPU or CUDA wheel across updates; the read-only preflight and the parity test against the repair look like the right shape, will get this reviewed.

…ai#9499

The all-hidden guard read only the first set visible-device variable. ROCr
filters before HIP and the two stack, so ROCR_VISIBLE_DEVICES=-1 behind a
HIP_VISIBLE_DEVICES=0 left every device hidden while the probe still forced a
multi-gigabyte dependency pass. Any hidden layer now keeps the fast path, which
only ever removes forced passes.

Also: keep the CLI answering under -P / PYTHONSAFEPATH, where the script
directory is dropped from sys.path and the probe died with a traceback that
setup.sh read as "keep the fast path"; exit 2 on a malformed probe call instead
of falling through into a full install; and name UNSLOTH_TORCH_BACKEND=cpu in
the repair message, as the CUDA repair already does.

Tests: stacked masks in both directions, an NVIDIA host with an inferable AMD
arch (the NVIDIA gate had no coverage, and _infer_linux_amd_gfx_arch does not
check NVIDIA itself), a vendor ROCm wheel carrying torch.version.hip with no
+rocm tag, the CLI exit-0 path end to end over a stub torch plus a no-stderr
assertion so an import failure can no longer pass as exit 1, the malformed-argv
guard, two parity rows for the UNSLOTH_ROCM_GFX_ARCH rescue and the generic
pytorch.org arm, and the no-timeout fallback branch plus BusyBox status 143.
Corrects the parity comment: the matrix asserts both directions, so the
documented conservative divergences do not belong in it.
@danielhanchen

Copy link
Copy Markdown
Member

Reviewed this end to end and pushed one commit to the branch (0628807). The gap it fixes is real: _ensure_rocm_torch only runs inside the dependency pass, setup.sh skips that pass on a version match, so a standalone unsloth studio update on an AMD host keeps a CPU or CUDA wheel forever while printing dependencies up to date. The preflight is the right shape, and I confirmed the central guarantee holds.

What I changed

The all-hidden mask guard only looked at the first set variable. _first_set_visible_mask() is first-set-wins, so HIP_VISIBLE_DEVICES=0 with ROCR_VISIBLE_DEVICES=-1 was judged on HIP alone and forced the pass, even though ROCr had already hidden every device. Per AMD's GPU isolation docs, ROCr filters first and renumbers, then HIP filters on top, so the two stack rather than override. Any hidden layer now keeps the fast path. This only ever removes forced passes, never adds one, so it cannot weaken the invariant.

Three smaller ones:

  • Under -P or PYTHONSAFEPATH=1 the script directory is dropped from sys.path, so import install_manifest raised and the probe exited 1 with a traceback that setup.sh reads as "keep the fast path". Silent end to end. The script directory is now inserted explicitly alongside backend/.
  • Any argv other than the exact flag fell through to install_python_stack(). Nothing passes that today, but a typo starting a multi-gigabyte install is a sharp edge for one line. It now exits 2.
  • The repair message does not mention UNSLOTH_TORCH_BACKEND=cpu, unlike the CUDA repair at install_python_stack.py:2440. Since the manifest persists no_torch but not the backend, a deliberate CPU-on-AMD choice was overridden with no hint at how to keep it.

Tests

The suite was good, not vacuous: of 13 gate mutations I ran, 10 were already caught, several by precisely targeted assertions. Three survived, and all three matter:

  • Deleting the NVIDIA gate left all 46 tests green, because _host stubs _has_rocm_gpu as rocm_gpu and not nvidia, so the NVIDIA row returns False at the no-AMD-signal gate and never reaches it. The gate is load-bearing: _infer_linux_amd_gfx_arch has no NVIDIA check of its own, so on an AMD APU plus NVIDIA dGPU the probe would force a pass the repair declines.
  • Deleting _hip from the final check survived. AMD and source builds report torch.version.hip with no +rocm local version, and without that term they would be reinstalled on every update.
  • Deleting the UNSLOTH_ROCM_GFX_ARCH term from _inferred_arm survived, because parity rows 4 and 5 are behaviourally identical: both set rocm_gpu=False, so the not _rocm_visible disjunct already carries them and the env var is inert.

Added coverage for those three, for stacked masks in both directions, for the CLI exit-0 path end to end over a stub torch, for the argv guard, for the no-timeout fallback branch, and for BusyBox deadline status 143. The CLI test also now asserts empty stderr, since returncode == 1 alone is indistinguishable from an import traceback under its scrubbed env. Two parity rows added: the UNSLOTH_ROCM_GFX_ARCH rescue with a visible GPU, and a gfx1030 row that finally exercises the generic download.pytorch.org arm that _generic_pytorch_rocm_tag feeds. All 13 mutations are now caught.

I also corrected the parity comment. :288 asserts not installs for every False row, which makes the matrix a biconditional, while the comment and the description say the guarantee is one-way. It passes only because no conservative row is present, and all four documented ones would fail it if added. The comment now says the matrix is for rows the two sides agree on.

Verification

  • 1,617,408 simulated combinations across [Linux, WSL, macOS, Windows] x [NVIDIA, AMD, CPU-only] crossed with wheel family, pin, mask, backend, GGUF-only and named arch, running the preflight and then _ensure_rocm_torch under identical stubs. Zero forced passes with nothing to install, zero installs from the probe, and nothing forced on non-Linux, non-x86_64, GGUF-only, a resolved cpu/cuda/xpu backend, an NVIDIA host, or a torch that already carries a ROCm marker. The stacked-mask violations went from 1,224 to 0, and the forced-pass count dropped by exactly that, confirming the change is purely subtractive.
  • 28 end-to-end checks against isolated uv venv installs driving the real CLI and the real setup.sh block: fresh venv with no torch, stub CPU / ROCm / hip-only wheels, every documented escape, an old GGUF-only install whose mode lives in the manifest rather than the env, the hardened interpreter, and the malformed-argv guard.
  • Full suites on Linux x86_64 CUDA: tests/studio/install plus test_cross_platform_parity.py, test_windows_no_torch_setup.py and test_ci_shell_suite_coverage.py at 2801 passed, 3 skipped, 0 failed; the whole tests/sh loop as Backend CI runs it, 43 files, all green; the new suites at 61 and 18. ruff, bash -n, git diff --check clean.
  • Old installs are unaffected: the probe is skipped entirely unless the fast path was already taken, and a venv predating this change with no torch, an unimportable torch, or a recorded GGUF-only mode all keep the fast path. setup.ps1 needs no counterpart, since it already has its own AMD escape at 4809 and the probe is Linux and x86_64 gated. No frontend or browser surface is touched.

One thing worth settling before merge, not a blocker. The final test is not (_hip or "rocm" in version), which asks "is this a ROCm wheel at all". A wheel that is ROCm but the wrong family stays stuck across every future update: rocm6.4 under a rocm7.2 pin, a generic wheel on Strix that needs the per-arch index, and a new-family wheel on gfx906 that needs the legacy one. That is the same permanently stuck bug this PR fixes, one layer in. I narrowed the docstring to say so rather than widen the scope here. Happy to take it as a follow-up.

The one red check, (Python 3.13) at 45m, is the main-side xdist hang fixed after this run by #9530, not anything here. A rebase should clear it.

@danielhanchen

Copy link
Copy Markdown
Member

Second pass on this, covering the things the first round did not prove. No further code changes needed on top of 0628807, but there is one risk below that I think is worth a decision before merge rather than a silent merge.

The one thing I would want settled first

Forcing the dependency pass is not only "reinstall torch". install_python_stack() deletes the install manifest up front and rewrites it last, deliberately:

# Drop it up front: a missing manifest is what tells the CLI, setup.sh and
# the preflight that an interrupted run left the venv half-built.

and

# 15. Record success. Written last so an earlier kill leaves none.

run() exits on any failing pip command, so a failure anywhere between those two points leaves no manifest. I confirmed what that then means:

verify_install on a venv with no manifest:
  {'ok': False, 'manifest_ok': False, 'reason': 'studio_install_incomplete'}

setup.sh:1840 then forces the pass on every subsequent run, and the Tauri preflight reports the managed install as stale via studio_install_ok.

So the exposure is: before this PR, an eligible AMD host had a complete install with the wrong torch, and Studio launched. After it, an unattended unsloth studio update on that host starts a multi-gigabyte download from repo.amd.com or download.pytorch.org, and if it fails, the host lands in studio_install_incomplete, which is worse than where it started. That is exactly the host class this feature targets, so the failure is not hypothetical.

Two smaller consequences of the same flip, for completeness: it also sets _NEED_T5_INSTALL=true at setup.sh:2015, which rm -rfs and rebuilds the three transformers sidecar venvs, and it force-reinstalls bitsandbytes on every successful ROCm pass.

There is an in-repo precedent for the fix. The Windows arm of _ensure_rocm_torch uses pip_install_try and prints "keeping the existing torch build, re-run later" rather than aborting. The Linux arm uses pip_install, which is fatal. I did not change that myself: it would alter behaviour for every caller including a fresh install, where a hard failure is the correct outcome, so making it conditional on the fast-path escape is a design call that belongs to you rather than something I should slip in. Flagging it as the main open question.

Wave 2 results

Theory first, then simulations for each hypothesis. 41 further checks, all passing after two of my own harness bugs were corrected.

  • Old interpreters. A Studio venv predating this change could be on an older Python, and a SyntaxError would exit non-zero, which setup.sh reads as "keep the fast path", silently never repairing the host. Ran the real CLI under uv venvs on 3.9, 3.10, 3.11, 3.12 and 3.13: correct answer and no traceback on all five.
  • Manifest shapes. _infer_no_torch() reads the manifest at import, so a malformed one would traceback into the same silent exit 1. Tested {"no_torch": true}, false, "yes", "off", {}, empty, invalid JSON and a JSON array: all answer correctly with no traceback. "yes" correctly reads as GGUF-only, since it is in install_manifest.NO_TORCH_TRUTHY.
  • Forward compatibility. torch 3.0 on ROCm 8, an unreleased +rocm7.20.0 per-arch wheel, an uppercase +ROCM7.3 tag, and bare future versions all classify correctly. Worth noting that torch.version.hip semantics have been in flux upstream ([Issue] torch.version.hip changed from 7.2.x to 7.13.x breaks ABI detection ROCm/TheRock#5425 moved it from 7.2.x to 7.13.x and added a separate torch.version.rocm), which is precisely why keeping the _hip term alongside the +rocm string test matters, and why I added a test pinning it.
  • A real hang. Wave 1 only simulated exit 124. This time I drove the real block with a shortened deadline against a probe that never returns: the fast path is kept, the probe is killed at the deadline, and GNU timeout signals the whole process group, so the inner import torch is not orphaned. I verified that separately for the exact python-parent-with-subprocess.run-child shape the probe has.
  • Hostile environments. No HOME, read-only HOME, LC_ALL=C, empty PATH, PYTHONDONTWRITEBYTECODE with -O, and PYTHONWARNINGS=error all return a clean answer with empty stderr.
  • Concurrency. Eight concurrent probes leave pip freeze byte-identical.
  • sys.path safety. My _STUDIO_DIR insert only adds install_manifest, install_llama_prebuilt, install_node_prebuilt, install_sd_cpp_prebuilt, install_whisper_prebuilt, prebuilt_core and backend. I checked all of them against sys.stdlib_module_names and against every installed distribution: no collisions, so nothing can be shadowed.
  • The argv guard is safe. The only two invocations in the tree are the bare python "$SCRIPT_DIR/install_python_stack.py" at setup.sh:1444 and setup.ps1:5288, plus the probe flag. Nothing passes a dash-prefixed argument.

Cross-platform

setup.ps1 already has its own AMD escape at 4813, gated on $script:ROCmGfxArch, so Windows is covered and needs no counterpart here. It keys on torch.cuda.is_available(), a runtime signal that is false on a correct ROCm wheel with a wedged driver, where the new Linux probe keys on the wheel family and is the more precise of the two. Pre-existing asymmetry, not something to change in this PR. setup.ps1 parses clean under pwsh and is untouched. macOS and non-x86_64 Linux keep the fast path unconditionally, which the stubbed matrix covers.

No browser surface is involved: the four changed files are studio/setup.sh, studio/install_python_stack.py and two test files, with no frontend, template or served-UI change, so Firefox, Safari, Chrome and Edge cannot be affected.

Totals across both rounds

  • 1,617,408 stubbed host combinations over [Linux, WSL, macOS, Windows] x [NVIDIA, AMD, CPU-only] crossed with wheel family, pin, mask, backend, GGUF-only and named arch. All eight invariants hold: no forced pass without work, no install from the probe, and nothing forced on non-Linux, non-x86_64, GGUF-only, a resolved cpu/cuda/xpu backend, an NVIDIA host, or a torch already carrying a ROCm marker.
  • 28 end-to-end checks in wave 1 and 41 in wave 2, against isolated uv venv installs driving the real CLI and the real setup.sh block.
  • 2801 passed, 3 skipped, 0 failed on the install and parity suites; all 50 tests/sh files green; the new suites at 61 and 18.
  • 13 of 13 gate mutations caught, up from 10 of 13 before this branch's last commit.

Opening comment-reduction pass over the comments this PR adds, plus the
SPDX header tests/studio/install/test_amd_fastpath_probe.py was missing
while its sibling shell test already carried one. Comments and docstrings
only, AST-verified against c8b0ff9.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

CUDA_VISIBLE_DEVICES is HIP's alias at the same layer, not a layer beneath
it, so reading all three masks as independent made HIP_VISIBLE_DEVICES=0 with
CUDA_VISIBLE_DEVICES=-1 look like a hidden host and kept the fast path on a
GPU the repair can act on. Hiding NVIDIA that way while pinning HIP is a real
mixed-host pattern. ROCr stays an independent check, since it filters and
renumbers beneath HIP; the CUDA alias is now read only when HIP is unset,
matching _pick_visible_index's documented first-set-wins.
danielhanchen added a commit to Datta0/unsloth-staging-3 that referenced this pull request Aug 23, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 81e26a9de6

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

The mask ordering is explained where it is used, so the constant only needs
to name the rule. Comments and docstrings only, AST-verified against 3c409f0.
@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: c5b74b3517

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

Copy link
Copy Markdown
Member

Ran this on real AMD hardware, on an ephemeral AMD DevLab runner (Ryzen AI MAX+ 395 with Radeon 8060S, gfx1151, ROCm 7.2, Ubuntu 24.04). Everything up to now had been stubbed on an NVIDIA box, so this is the first time the detectors, the probe and the setup.sh block have run against a real HIP stack. All green.

The host reproduces the reported state exactly

Installing a plain CPU wheel gives the venv the same thing the original report described:

detector value on the DevLab host
_has_usable_nvidia_gpu False
_has_rocm_gpu True
_detect_rocm_version (7, 2)
_infer_linux_amd_gfx_arch 'gfx1151'
_detect_amd_gfx_codes ['gfx1151']
_probe_torch_runtime (True, True, '2.13.0+cpu', '', '')
_generic_pytorch_rocm_tag 'rocm7.2'
_amd_arch_index_url https://repo.amd.com/rocm/whl/gfx1151/

With that venv, the probe exits 0 and forces the dependency pass, which is the whole point of the change. Cost was 0.82s wall on this host, against 0.09s on an NVIDIA box, the difference being the real rocminfo and amd-smi calls plus a real import torch.

The mask matrix, 21 rows, all correct

=== unmasked and single-layer masks ===
  PASS  (no mask)                                        rc=0
  PASS  HIP_VISIBLE_DEVICES=                             rc=1
  PASS  HIP_VISIBLE_DEVICES=-1                           rc=1
  PASS  HIP_VISIBLE_DEVICES=0                            rc=0
  PASS  ROCR_VISIBLE_DEVICES=-1                          rc=1
  PASS  CUDA_VISIBLE_DEVICES=-1                          rc=1
=== stacked: ROCr is an independent layer beneath HIP ===
  PASS  HIP_VISIBLE_DEVICES=0 ROCR_VISIBLE_DEVICES=-1    rc=1
  PASS  HIP_VISIBLE_DEVICES=0 ROCR_VISIBLE_DEVICES=      rc=1
  PASS  HIP_VISIBLE_DEVICES=0 ROCR_VISIBLE_DEVICES=0     rc=0
=== a set HIP mask shadows its CUDA alias ===
  PASS  HIP_VISIBLE_DEVICES=0 CUDA_VISIBLE_DEVICES=-1    rc=0
  PASS  HIP_VISIBLE_DEVICES=0 CUDA_VISIBLE_DEVICES=      rc=0
  PASS  HIP_VISIBLE_DEVICES=-1 CUDA_VISIBLE_DEVICES=0    rc=1
=== the documented escapes ===
  PASS  UNSLOTH_TORCH_BACKEND=cpu / cuda / xpu           rc=1
  PASS  UNSLOTH_NO_TORCH=1                               rc=1
  PASS  UNSLOTH_TORCH_INDEX_URL=.../whl/cpu              rc=1
  PASS  UNSLOTH_TORCH_INDEX_URL=.../whl/rocm6.4          rc=0
  PASS  UNSLOTH_ROCM_GFX_ARCH=gfx1151                    rc=0
failures: 0

Independent ground truth for the layering

I did not want the layering model to rest only on documentation, so I counted gfx agents straight from rocminfo under each mask on the same host:

  rocminfo gfx agents  (no mask)                   1
  rocminfo gfx agents  ROCR_VISIBLE_DEVICES=0      1
  rocminfo gfx agents  ROCR_VISIBLE_DEVICES=-1     0
  rocminfo gfx agents  HIP_VISIBLE_DEVICES=-1      1
  rocminfo gfx agents  CUDA_VISIBLE_DEVICES=-1     1

That settles the half of the model the stacked-mask fix depends on. ROCR_VISIBLE_DEVICES=-1 really does empty the agent list at the ROCr layer, so checking ROCr independently is correct, and neither HIP_VISIBLE_DEVICES nor CUDA_VISIBLE_DEVICES is visible to ROCr, confirming both sit above it.

Being straight about the limit: hipinfo is not in the DevLab image, and a CPU torch cannot enumerate HIP devices, so I could not measure HIP-versus-CUDA precedence on the metal. That half still rests on AMD's HIP environment-variable reference, which lists the pair as a single combined entry, and on _pick_visible_index in this file, which already implements first-set-wins for the same three variables. The hardware is consistent with it and contradicts nothing.

The rest of the job

  • The real setup.sh escape block, sourced from the actual file and driven against the real CLI: no env gives _SKIP_PYTHON_DEPS=false, UNSLOTH_TORCH_BACKEND=cpu gives true, HIP_VISIBLE_DEVICES=0 CUDA_VISIBLE_DEVICES=-1 gives false, HIP_VISIBLE_DEVICES=0 ROCR_VISIBLE_DEVICES=-1 gives true.
  • tests/sh/test_setup_amd_fastpath_escape.sh: 18 passed on AMD.
  • tests/studio/install/test_amd_fastpath_probe.py: 64 passed on AMD.
  • pip freeze hashed identically before and after all 25 probe invocations, so nothing the probe did touched the venv.

One correction on my side rather than the PR's: the first run failed because my workflow ran the matrix under set -e, so the first intentionally non-zero probe killed the step. That was a harness bug, not a finding; the re-run with the guard is the one above. The runner is ephemeral and the disposable workflow branch has been deleted.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 23, 2026
@danielhanchen

Copy link
Copy Markdown
Member

My earlier AMD run only proved the detection half, that the probe says "force the pass" on a broken host. It never showed the update actually repairs the wheel, which is the thing this PR is for. So I ran the full before and after on an ephemeral AMD DevLab runner (Ryzen AI MAX+ 395, Radeon 8060S, gfx1151, ROCm 7.2). It fixes the issue.

The broken state

A plain CPU wheel in the venv, on real AMD hardware:

BROKEN STATE: 2.13.0+cpu   hip: None   cuda_avail: False

Training is unavailable, which is the reported symptom.

Before: the merge base cannot repair this host

files mentioning the probe flag: 0 (expect 0)
merge base: 6 escapes | PR head: 7 escapes

At 7a0ecc2a5 the flag does not exist anywhere, and the six escapes that can clear _SKIP_PYTHON_DEPS are anyio, the incomplete manifest, the desktop backend version and three XPU ones. ROCm appears in that region only as leaf-name classification inside _setup_known_nonxpu_leaf, which serves the XPU escapes, not as an AMD hardware trigger. So on a version match the dependency pass is skipped, and since _ensure_rocm_torch is only ever called from install_python_stack(), the repair is unreachable. The wheel stays CPU across every update.

After: the PR head forces the pass

_SKIP_PYTHON_DEPS=false

The repair the forced pass unlocks, run for real

Not stubbed. _ensure_rocm_torch() from the PR head, against the real venv on the real host:

host: gfx1151 (7, 2)
repair took 48s

=== torch after the repair ===
  version: 2.11.0+rocm7.13.0
  hip    : 7.13.99004
  avail  : True
  device : Radeon 8060S Graphics

It routed to the per-arch index for this host and installed a real ROCm build. The GPU is now usable, confirmed with actual compute rather than a flag:

matmul on Radeon 8060S Graphics -> 261076.359

It converges rather than looping

The worry with any forced pass is that it fires forever. It does not:

probe now exits 1 (want 1: keep the fast path)
setup.sh keeps the fast path: _SKIP_PYTHON_DEPS=true

Once the wheel is correct the probe goes quiet and the next unsloth studio update takes the fast path again, so the repair is one-shot.

Summary

stage torch GPU usable
broken host 2.13.0+cpu no
merge base after an update 2.13.0+cpu, repair unreachable no
PR head after an update 2.11.0+rocm7.13.0 yes
PR head, next update unchanged, fast path kept yes

Two harness bugs of mine along the way, neither a finding about the PR: the first attempt ran the matrix under set -e so the first intended non-zero probe killed the step, and the second cloned a blob-filtered partial clone with --shared, which cannot serve its blobs. Both fixed; the run above is the clean one. Runners are ephemeral and both disposable branches have been deleted.

@danielhanchen
danielhanchen merged commit 47670fa into unslothai:main Aug 23, 2026
67 of 69 checks passed
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.

3 participants