Repository navigation
Studio: escape the dependency fast path when AMD torch is the wrong wheel - #9499
Conversation
|
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.
|
Reviewed this end to end and pushed one commit to the branch (0628807). The gap it fixes is real: What I changedThe all-hidden mask guard only looked at the first set variable. Three smaller ones:
TestsThe 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:
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- I also corrected the parity comment. Verification
One thing worth settling before merge, not a blocker. The final test is The one red check, |
|
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 firstForcing the dependency pass is not only "reinstall torch". and
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 Two smaller consequences of the same flip, for completeness: it also sets There is an in-repo precedent for the fix. The Windows arm of Wave 2 resultsTheory first, then simulations for each hypothesis. 41 further checks, all passing after two of my own harness bugs were corrected.
Cross-platform
No browser surface is involved: the four changed files are Totals across both rounds
|
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.
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.
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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.
|
Codex Review: Didn't find any major issues. Hooray! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
|
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 exactlyInstalling a plain CPU wheel gives the venv the same thing the original report described:
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 The mask matrix, 21 rows, all correctIndependent ground truth for the layeringI did not want the layering model to rest only on documentation, so I counted gfx agents straight from That settles the half of the model the stacked-mask fix depends on. Being straight about the limit: The rest of the job
One correction on my side rather than the PR's: the first run failed because my workflow ran the matrix under |
|
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 stateA plain CPU wheel in the venv, on real AMD hardware: Training is unavailable, which is the reported symptom. Before: the merge base cannot repair this hostAt After: the PR head forces the passThe repair the forced pass unlocks, run for realNot stubbed. 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: It converges rather than loopingThe worry with any forced pass is that it fires forever. It does not: Once the wheel is correct the probe goes quiet and the next Summary
Two harness bugs of mine along the way, neither a finding about the PR: the first attempt ran the matrix under |
Problem
studio/setup.shskips the entire dependency pass when the installedunslothversion already matches PyPI.install_python_stack.py's_ensure_rocm_torchis 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 everyunsloth studio update, while setup printsdependencies up to dateand 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+cpuin the venv and the package version current, nothing in the update path looks at the wheel.Change
Before taking the fast path,
setup.shasksinstall_python_stack.pyone 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_taghelper 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.hipnor a+rocmtag, on a host the repair will actually act on.The eligibility gates mirror
_ensure_rocm_torchin both directions:UNSLOTH_ROCM_GFX_ARCH([Bug] Strix Halo no GPU detected (Linux) #7301). Without the second,UNSLOTH_ROCM_GFX_ARCH=gfx1151on a host with no working runtime stayed on CPU torch.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:
HIP_VISIBLE_DEVICES,ROCR_VISIBLE_DEVICESorCUDA_VISIBLE_DEVICESis set to""or-1So 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_agreeruns both sides under the same stubs over a host matrix and asserts it.UNSLOTH_TORCH_BACKENDof 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 180and 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 ofsetup.share untouched.Verification
On an AMD DevLab gfx1151 host (
has_rocm_gpu True, ROCm(7, 2),inferred_gfx gfx1151), running the real flag:torch 2.13.0+cpuHIP_VISIBLE_DEVICES=""UNSLOTH_TORCH_BACKEND=cpuUNSLOTH_TORCH_INDEX_URL=.../cpuUNSLOTH_ROCM_GFX_ARCH=gfx1151pip freezehashed 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 thatpip_installis unreachable from the probe, plus 8 parity rows that run the preflight and_ensure_rocm_torchunder 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 ofsetup.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.tests/sh51/51;tests/studio/install/plustests/python/test_cross_platform_parity.pyandtests/python/test_windows_no_torch_setup.pyat 2620 passed, 118 skipped.tests/studio/install/fails the same 6 tests with and without the change, andtests/shthe 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.