Repository navigation
Give a mixed NVIDIA plus AMD host a way to ask for the ROCm torch stack - #10474
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
87620fd to
e0227a8
Compare
Every installer stops probing AMD the moment nvidia-smi lists a GPU. That is the right default, CUDA is the better supported stack and a false AMD positive would swap a working install, but on a host with both cards it left the AMD one with no route at all: chat can be pointed at Vulkan in Settings, and training reads whatever torch was installed, so nothing in the product can reach it. The only bypass was an index pin, which is undocumented and names a wheel family rather than a preference. UNSLOTH_FORCE_ROCM_TORCH=1 is that request, mirroring UNSLOTH_FORCE_VULKAN for the llama.cpp bundle and the explicit backend re-probe install_llama_prebuilt already does for ROCm. It relaxes which vendor wins, not whether there is a card to serve: the AMD presence test still has to pass, so a typo on a pure NVIDIA box installs nothing rather than turning it into a CPU one. One torch install serves one vendor, so this swaps the stack rather than adding to it, and it is opt in for exactly that reason. Set in install.sh and in install_python_stack.py at the four gates that return early on a usable NVIDIA GPU.
e0227a8 to
21e0775
Compare
…tered The first version relaxed _has_amd_rocm_gpu and the four gates that return early on a usable NVIDIA GPU, and none of them was reachable. get_torch_index_url sets _nvidia_detected=1 first and only consults AMD when it is 0, so a mixed host still resolved a cu* index; install.sh exports UNSLOTH_TORCH_BACKEND from the index it resolved, and _ensure_rocm_torch returns on its first line for a cuda backend. The request could not swap anything at all. It is now applied where the vendor is actually chosen: an explicit request on a host that really has an AMD ROCm GPU hands the AMD branch the same host it would judge on a machine with no NVIDIA card. The presence test still has to pass, so a request on a pure NVIDIA box selects nothing and falls through to CUDA, and the installer says plainly that one torch install serves one vendor. Two more from the same review. A standalone studio update leaves the backend empty, so the CUDA repair runs before the ROCm one, sees an NVIDIA GPU beside the requested HIP build, reads it as poisoning and reinstalls the CUDA trio for the ROCm path to undo again; it now stands down under the request unless a CUDA index is pinned by name. And Windows no longer reads the request at all: the wheel there is chosen by install.ps1 and re-asserted from setup.ps1's expected tag, so honouring it only in the Python layer would install ROCm and have it reverted. Windows needs the same change in both PowerShell installers and keeps the index pin until then. Six tests, three of which fail on the previous version.
The structural invariant this test encodes is that ROCm detection never runs ahead of CUDA. The explicit-request fix narrows that by design: an UNSLOTH_FORCE_ROCM_TORCH host reaches _has_amd_rocm_gpu before the no-NVIDIA branch, which is the only route a mixed host has to its AMD card, so the first-occurrence position check no longer holds. The assertion now says what the invariant became. The automatic path is still checked to sit inside the branch, and any AMD probe reached before it must be guarded by _rocm_torch_explicitly_requested and nothing else, so a bare probe hoisted above the branch still fails here. Comments are dropped before the scan, since the comment explaining the guard names the probe it guards, and continuation lines are joined, since the guard and the probe sit either side of a backslash.
…own for The selector fix moved the index, and stopped there. The two per-arch reroutes that rewrite a */cpu index to repo.amd.com/rocm/whl/gfx* run at top level and probe the host afresh, so get_torch_index_url clearing its own _nvidia_detected never reached them. On a mixed host with a supported arch and no readable ROCm version, get_torch_index_url returns the cpu index deliberately, for those reroutes to rewrite; they declined, and the request finished by installing CPU torch beside a working NVIDIA card. Worse than the no-op it replaced. _nvidia_gpu_wins_over_amd answers that question in one place, so selection and the reroutes cannot disagree about it. It keeps the AMD presence test, so a request on a pure NVIDIA box still answers "NVIDIA wins" and the automatic path is untouched. The GPU summary line reads it too, since reporting the NVIDIA card would contradict the wheels just chosen. The CUDA repair stood down on the request alone. Standing down needs a card to stand down for, which is the shell selector's rule as well: a request with no AMD GPU present selects nothing and falls through to CUDA. Without the second test, leaving the variable set on a host whose AMD card has since been removed silenced the repair while _ensure_rocm_torch also found no target, and a stale HIP build was left on a working NVIDIA GPU with nothing to fix it. Six tests. The reroute predicate is exercised through bash on four stubbed hosts, and one test reads install.sh to confirm the reroutes consult it: a bare _has_usable_nvidia_gpu at either site restores the gap while the other five still pass.
|
@codex review |
The request buys a swap, never a downgrade, and one route still ended in one. A ROCm too old for wheels, an architecture no index covers, and a non-x86_64 host all end the AMD branch at the cpu index, and after the two per-arch reroutes there is nothing left to rewrite it. Setting the variable on such a host replaced a working CUDA install with CPU torch, which is exactly the outcome this feature must not have. Asked of the resolved index rather than of the preconditions, so the check cannot drift out of step with the branch that made the choice, and answered by calling the same selector again with the request suppressed rather than by a second copy of the CUDA logic. It sits after both reroutes on purpose: the runtime-less host legitimately arrives there with a cpu index that the reroute turns into per-arch wheels, so running this earlier would undo the previous fix. Three tests. The dead-end case fails without the guard; the two controls pass with and without it, which is what makes them controls rather than extraction errors: a request that did reach ROCm wheels is left alone, and a CPU index chosen without the request is never reversed.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 244c5f8944
ℹ️ 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".
| _has_amd_rocm_gpu() { | ||
| _ensure_rocm_probe_env | ||
| if _has_usable_nvidia_gpu; then | ||
| if _has_usable_nvidia_gpu && ! _rocm_torch_explicitly_requested; then |
There was a problem hiding this comment.
Apply the ROCm request before the WSL setup stages
On a mixed WSL2 Strix host that still needs the Ubuntu 24.04 reroute or the librocdxg bootstrap, this request-aware detection is reached too late: _maybe_reroute_strixhalo_to_2404() has already returned on its unconditional NVIDIA check at line 2290, _maybe_bootstrap_rocm_wsl() returns on the same check at line 4523, and the cross-distro _rr_exports list does not carry UNSLOTH_FORCE_ROCM_TORCH. Consequently the AMD runtime never becomes visible and the later selector keeps CUDA despite the explicit request. Make both WSL NVIDIA gates request-aware and propagate the flag through the reroute.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Declining this one again, with the specifics you have added taken as read.
The facts are right. _maybe_reroute_strixhalo_to_2404 returns at line 2290 and _maybe_bootstrap_rocm_wsl at 4523, both before the request is consulted, and _rr_exports does not carry UNSLOTH_FORCE_ROCM_TORCH. But propagating the flag through _rr_exports would not fix it, because both gates return before anything reads it: the change being asked for is to make an env var trigger a distro reroute to 24.04 and a librocdxg bootstrap.
That is a materially different kind of change from the rest of this PR. Everything here relaxes which vendor wins when both are present, is reversible by unsetting one variable, and fails in the safe direction. Performing a WSL distro reroute off an env var is not reversible in that sense, and I have no WSL host to verify it on.
The current behaviour on such a host is that the request finds no ROCm route, and the guard added in round two keeps the CUDA build rather than installing CPU torch. That is a correct, safe outcome, not a corrupted one. Widening the feature to WSL bootstrap belongs in its own PR with a WSL machine behind it, and the PR description already scopes this one to the vendor-precedence half.
Three fixes to the request path, all in the same direction: the request buys a swap, never a downgrade, and the installer must not disagree with itself about whether it found one. The CUDA restore guard matched */rocm*|*/gfx* against the whole URL, so a mirror whose base path contains either word (https://mirror.local/rocm-cache/cpu) read as ROCm wheels and the guard stood down, leaving CPU torch on a working NVIDIA card. Classified on the final leaf now, through _torch_index_url_leaf, which the backend block three lines below already carries a comment warning about. It is deliberately broader than _is_pip_rocm_family_leaf: that helper answers the narrower "is this a pip ROCm FAMILY index" and declines repo.radeon.com's rocm-rel-6.4 leaf, which is a ROCm index and must not be replaced with CUDA. _ensure_cuda_torch stood down for any host with an AMD card, but a card is necessary and not sufficient. _ensure_rocm_torch declines an arch measured to compute incorrectly under ROCm and keeps CPU torch, and _ensure_cpu_torch then demotes the HIP build that is there, so a gfx1033-style host has a card, no route, and needs the repair. It now asks _miscomputing_arch_host, the same helper _ensure_rocm_torch bails on, so the two cannot disagree. The GPU detection summary asked the reroute predicate, which still answers "AMD wins" after the CUDA restore has fired, so the summary announced an AMD CPU fallback one line after warning that the CUDA build was being kept. It now reads the resolved index. Eight tests. Each of the three fails on the pre-fix code, and each has the control one step away that fails on the over-broad version of the fix: a mirrored ROCm leaf must still be left alone, the Radeon repo leaf must still count as ROCm, and the summary must still report AMD when the request won.
Condense the explanatory comment blocks added for UNSLOTH_FORCE_ROCM_TORCH in install.sh and studio/install_python_stack.py. Every point is kept, including the issue references and the two keep-in-sync notes; only the restatements go. No code changes: the non-comment lines of both files are identical to the previous commit.
|
Codex Review: Didn't find any major issues. 🚀 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". |
install.sh was the only conflict. Both sides edited the same gate in _has_amd_rocm_gpu: main added the "ignore-nvidia" argument, for a caller that has already established the run opens AMD nodes, and this branch added the UNSLOTH_FORCE_ROCM_TORCH opt in. They are independent reasons to skip the NVIDIA veto, so the resolution keeps both and the veto still fires only when neither applies. That call to _rocm_torch_explicitly_requested then broke an invariant main landed meanwhile: tests/sh lifts probes out of install.sh one function at a time by name, so _has_amd_rocm_gpu may only call helpers those harnesses lift, and test_the_rocm_probe_calls_nothing_the_shell_harnesses_do_not_lift failed on the merge. All five harnesses that lift the probe now lift the helper too, and the allowlist names it. Unlifted it would not have failed loudly. An undefined function exits non zero, which reads as "not requested", which is the default answer, so the harnesses would have kept passing while never reaching the branch the flag selects. The allowlist alone cannot catch that, since widening it silences the invariant whether or not the harnesses were updated, so a second test reads the harness files and requires every allowlisted name in each of them. Removing one lift fails it. Verification on the merge result: 7360 passed, 34 skipped across studio/backend/tests/test_rocm_torch_explicit_request.py, test_amd_device_node_permissions.py and tests/studio/install/. The six ROCm shell harnesses pass 347 assertions with no failures, and bash -n is clean on install.sh and on each edited harness.
… everywhere Four defects found by simulating the feature rather than re-reading it. Each is fixed with a test that fails when only that fix is reverted. 1. The request authorised a swap and then withdrew it. _forced_rocm_route_is_viable decides whether _ensure_cuda_torch stands down, and on the unreadable-version arm every term asks whether _ensure_rocm_torch would install SOMETHING. Once the wheel this request asked for is installed the honest answer is no, so viability flipped to False, the CUDA repair stopped standing down and force-reinstalled CUDA over the healthy ROCm build, and _ensure_rocm_torch swapped back on the next run. Measured on gfx950 with no readable ROCm version, where _rocm_torch_family_needs_repair is the only arm carrying the chain: viable=True with CUDA installed, False with ROCm installed, and _ensure_cuda_torch then reaches 'CUDA torch repair'. That is the multi-GB round trip on every `studio update` this stand-down exists to prevent. Viability is now a property of the host: an installed ROCm build is answered by the host route test, so a ROCm wheel on a card no index serves is still not a route. 2. The feature suffix was stripped in one of the two places that need it. _amd_arch_index_url strips it, which is what opens the inferred-arch branch for "gfx1151:xnack-" -- the spelling rocminfo prints and the one this PR's own comment says users copy into UNSLOTH_ROCM_GFX_ARCH. The package table two lines down was still keyed on the raw string, so it missed and installed unpinned torch, torchvision and torchaudio where the bare spelling installs torch>=2.11.0,<2.12.0 and matching companions. The ABI bound that table holds was lost on a host that had it before this branch, and _hsa_spoof_contradicts read an HSA override naming the same card as a spoof. Both now ask the bare arch. 3. The two halves read one mask string differently. `awk -v vis=...` escape-processes its operand, so HIP_VISIBLE_DEVICES='\061' arrived inside _amd_mask_survivors as the ordinal 1 and selected the second card, while the Python twin rejects the same string at int() and fails closed. The mask is passed through the environment now, which awk takes verbatim. 4. The miscomputing-arch gate could inherit a target. _AMD_REQUEST_TARGET_GFX is cleared by the helper that sets it, and that helper runs only when NVIDIA is detected AND the request is set; every other route reaches the gate with whatever an earlier direct call left in the shell, and the CUDA restore calls the selector a second time from exactly such a shell. A stale gfx1100/probe pair clears the Van Gogh gate on a gfx1033 host: measured, cpu becomes rocm6.4. Not reachable in the shipped order today, since the inner call keeps a usable NVIDIA card and returns above the gate, so this is the guard against the order changing. The selector clears both on entry. Also removes nine lines of comments that a condensation pass left describing amd-smi ordering above the GPU summary banner, and three duplicating the _rocminfo_gpu_records note above _amd_ordered_gfx_devices. Neither names code at its location, and both claim a keep-in-sync contract with studio/setup.sh for functions setup.sh does not have. Verification on this commit: 7369 passed, 34 skipped across test_rocm_torch_explicit_request.py, test_amd_device_node_permissions.py and tests/studio/install/. Six ROCm shell harnesses: 347 assertions, 0 failures. bash -n and compileall clean. Reverting fix 1 fails 2 tests, fix 2 fails 2, fix 3 fails 1, fix 4 fails 1, each only its own.
…he runner's The ubuntu-24.04-arm leg is a real CI job on this repo, and three of these cases failed there for a reason that is about the runner: ROCm wheels are x86_64 only, so get_torch_index_url returns the cpu index on any other `uname -m` and _ensure_rocm_torch returns on the same gate in Python. Differential against unmodified main on the same runner: main fails 29 of these files' tests on ARM64 and the branch fails 32, so 29 are pre-existing (the whole TestEnsureRocmTorch and TestProgressStepCountMatchesTotal families, which this branch does not touch) and exactly 3 are new, all of them here. Worse than the red: the cpu-index CONTROLS beside them passed on ARM64 for the same wrong reason, since everything answers cpu there. A case about which index a request selects cannot be carried by a host that has no ROCm wheels at all. _index_url now stubs uname to the Linux x86_64 host its cases describe, and the suffix case states platform.machine() the way the viability cases already did. Reproduced locally with a uname stub reporting aarch64 plus platform.machine patched: 2 failed before, all 194 in the file pass after, and the full suite is unchanged at 7369 passed, 34 skipped with 347 shell assertions green.
…hat they carry The suite stated the same mixed NVIDIA+AMD host five times in five helpers and again inline in the cases that used none of them. One _mixed_host builder now carries the defaults -- Linux x86_64, a usable NVIDIA card, one routable AMD card, ROCm 6.4, no masks, no pins -- and every case names only what it changes, so what a case is ABOUT is the part you read. _viable, _viable_masked, _rocm_repair_reached and _unreadable_version_host became four-line wrappers over it. Twelve groups of cases that differed only in their inputs became parametrize tables. No case is lost: a row is the old test, its id is the old name's distinguishing half, and the collected count went 194 to 198 (the four are new, below). Docstrings are cut to their first sentence, and the comment blocks in install.sh and install_python_stack.py to the fact they carry -- the issue numbers, the upstream citations, the keep-in-sync contracts and the reason a branch fails closed all stay; the worked examples and the restatements of the code go. test file 3080 -> 2494 lines install.sh 456 -> 412 added, 166 -> 122 of them comments install_python_stack 322 -> 283 added, 111 -> 72 of them comments PR total 3953 -> 3292 insertions A slimmer suite that still passes proves nothing, so this is measured by mutation instead: fifteen guards broken one at a time, each run against the whole suite. Before the refactor, fourteen were applied and fourteen caught. After it, two survived: - Removing _miscomputing_arch_host's early return changed nothing any case noticed. The builder had stubbed _detect_amd_gfx_codes where the old helper left it alone, so every case resolved a target and the narrower target-level refusal covered the host-level one. That gate is load-bearing on exactly one shape -- a host no probe can name a device on, a Steam Deck with no rocminfo -- and nothing tested it. It does now. - Dropping the request from install.sh's _has_amd_rocm_gpu was never covered at all. That is the shell twin of the gate _has_rocm_gpu carries in Python, and only the Python half had a case. This was not caused by the refactor; the mutation's pattern had drifted, so the baseline skipped it and the gap was invisible. Both are now tested with a control each, and the final run catches 15 of 15. Verification: 7373 passed, 34 skipped across the three suites; 347 shell assertions across six harnesses, 0 failures; bash -n and compileall clean.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5f1f7e60a6
ℹ️ 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".
| # UNSLOTH_FORCE_ROCM_TORCH is deliberately NOT read here: install.ps1 picks CUDA from the | ||
| # NVIDIA probe and setup.ps1 republishes a tag _ensure_expected_torch_flavor() restores, so | ||
| # honouring it only here is a multi-GB round trip. Windows needs both PowerShell installers. |
There was a problem hiding this comment.
Honor the ROCm request in the Windows installers
On a Windows host with both usable NVIDIA and AMD GPUs, this explicitly ignores the new request. install.ps1 remains NVIDIA-first, and its ROCm reroute at install.ps1:9278 runs only when the selected index is CPU, so a normal install chooses CUDA; a standalone update then returns at the NVIDIA guard immediately below these lines. Consequently UNSLOTH_FORCE_ROCM_TORCH=1 cannot select ROCm on Windows at all, despite the new variable being platform-neutral and the change targeting mixed hosts; the request must also be handled by the PowerShell selection/setup paths before this branch can honor it.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Deliberate, and the lines you quote say so. On Windows the request is a no-op by construction rather than a partial honouring: _amd_hardware_is_corroborated() returns False on IS_WINDOWS, so _forced_rocm_route_is_viable() is always False there, and _ensure_cuda_torch() returns at its "IS_MACOS or IS_WINDOWS or NO_TORCH" guard before it ever reads the request. The CUDA stack is never stood down on Windows, so the host cannot end up with neither stack. test_windows_is_not_swapped_by_the_request_alone pins that the Windows branch does not act on the flag.
Honouring it only in install_python_stack.py would be worse than ignoring it. install.ps1 picks the index from its NVIDIA probe, setup.ps1 publishes UNSLOTH_EXPECTED_TORCH_TAG (studio/setup.ps1:7815) and _ensure_expected_torch_flavor() restores that tag, so the ROCm wheels would be downloaded and then reverted on the next run. Making the flag work on Windows means install.ps1 and setup.ps1 have to select the family too, which is a change to the PowerShell installers and a separate PR, not something this one can do half of. The Windows route that exists today is an explicit UNSLOTH_TORCH_INDEX_URL pin, which that same branch honours ahead of the NVIDIA guard.
…back on its own function
… and stop probing for a host that did not ask
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Another round soon, please! 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". |
Addresses #10450.
The gap
Every installer returns early once
nvidia-smilists a GPU:install.sh:3118,install.ps1:4419, and four sites ininstall_python_stack.py. That is the right default, since CUDA is the better supported stack and a false AMD positive would swap a working install. On a host with both cards it leaves the AMD one unreachable: chat can be pointed at Vulkan in Settings, but training reads whatever torch was installed, so nothing in the product can use it. The only bypass was an index pin, which is undocumented and names a wheel family rather than a preference.install_llama_prebuilt.py:7997already has the right shape for the llama.cpp bundle: an explicit ROCm request re-probes even when CUDA is usable. This gives the torch stack the same thing.The change
UNSLOTH_FORCE_ROCM_TORCH=1(alsotrue,yes,on, case insensitive) relaxes which vendor wins:install.sh_has_amd_rocm_gpuno longer returns early on a usable NVIDIA GPU when the request is set.install_python_stack.pygets the same helper, applied at the four gates that return early:_has_rocm_gpu, the repair eligibility check, and the Windows and Linux_ensure_rocm_torchpaths.It relaxes which vendor wins, not whether there is a card to serve. The AMD presence test still has to pass, so setting it on a pure NVIDIA box installs nothing rather than turning a working CUDA machine into a CPU one. The first version of the Linux edit skipped the whole
_rocm_pin is Noneblock when the flag was set, including that presence check, which is exactly the failure it must not have; only the_has_usable_nvidia_gpu()line is bypassed now, and there is a test for it.The honest caveat
One torch install serves one vendor, so this swaps the stack rather than adding to it. That is why it is opt in and why it is spelled as a request rather than a detection change. A user with both cards who sets it gets ROCm torch and loses CUDA torch until they reinstall without it.
This is the installer half of #10450. The training UI half, letting a user pick which vendor's card a run goes to on a host that has both, is a separate piece and I have no mixed host to verify it on.
Verification
studio/backend/tests/test_rocm_torch_explicit_request.py, 15 cases, all passing. The suite importsinstall_python_stack.pyby path, since it is a top level installer script rather than a package module.The two that carry the result are a matched pair on an identical stubbed host, differing only in the environment variable:
test_the_request_lets_the_amd_probe_run_on_a_mixed_host(True) fails on the pre-change code, where_has_rocm_gpureturned False on any NVIDIA host before consulting a single AMD probe.test_the_same_host_without_the_request_still_answers_no(False) is its control, so a probe that started answering for some other reason fails here.Plus
test_the_request_does_not_invent_a_card(a host whose only agent isgfx000still answers no),test_an_nvidia_host_still_hides_the_amd_card_by_default, and the parsing cases including the empty string, which is how a shell passes "unset" and which must not read as a request.An earlier version of one test asserted
probed["ran"] or True, which can never fail; it was replaced with a realrocminfostub and true/false assertions. Withinstall_python_stack.pyreverted, 12 of the 15 fail. Six suites together report 470 passed, 1 skipped, andbash -n install.shis clean.