Skip to content

fix(studio/rocm): don't install for a shadowing iGPU on mixed AMD hosts (#7776) - #7778

Merged
danielhanchen merged 27 commits into
unslothai:mainfrom
Anai-Guo:fix/amd-igpu-shadows-dgpu-7776
Aug 6, 2026
Merged

danielhanchen merged 27 commits into
unslothai:mainfrom
Anai-Guo:fix/amd-igpu-shadows-dgpu-7776

Conversation

@Anai-Guo

@Anai-Guo Anai-Guo commented Aug 3, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #7776

The bug

On a board carrying both an AMD APU and a discrete Radeon, HIP enumerates the iGPU
first. _detect_windows_gfx_arch resolves the arch with
tokens[_pick_visible_index(len(tokens))], which is index 0 when no mask is set — so
the installer read the iGPU's arch and pulled its wheel family. In the report that
was a gfx1036 Raphael iGPU shadowing a gfx1200 RX 9060 XT: gfx103X-all wheels went
in, the discrete card was never loaded into, and the reporter only got the right wheels
after setting HIP_VISIBLE_DEVICES=1 and rerunning by hand.

The fix

In _dedup_pick, when no visible-device mask is pinned and more than one distinct
arch is enumerated:

  • skip a leading shadowing APU arch so the discrete card decides the wheel family;
  • print which GPU was selected and how to override it with HIP_VISIBLE_DEVICES.

An explicit HIP_VISIBLE_DEVICES / ROCR_VISIBLE_DEVICES value still wins verbatim
(_visible_devices_pinned() treats only "" and -1 as "no mask"), so a user who
pinned the iGPU on purpose keeps getting the iGPU.

The Strix arches (gfx1150/gfx1151/gfx1152) are deliberately excluded from the
skip set — they are first-class unified-memory training targets with their own
arch-specific index, so their selection is untouched. The skip set is only the legacy
APUs that share a board with a dGPU: gfx90c, gfx1013, gfx1035, gfx1036,
gfx1037, gfx1103.

This covers both halves of the ask in the issue — "have the installer detect this
situation and suggest setting HIP_VISIBLE_DEVICES"
. If you'd rather not have the
installer re-pick at all, dropping the _SHADOWING_INTEGRATED_GFX branch leaves just
the advisory message and the rest still applies.

Review follow-up (2d2ebc7)

Two gaps were raised in review and both are fixed here:

  • setup.ps1 never reached the repick. It resolves the arch itself (hipinfo and
    amd-smi paths) and builds $ROCmIndexUrl from it before invoking the Python stack
    installer, and _ensure_rocm_torch() then returns early on
    UNSLOTH_ROCM_TORCH_INSTALLED=1 — so a fresh Windows install still pulled the iGPU's
    wheel family. Resolve-ShadowingGfxPick mirrors _dedup_pick() and is applied at both
    PowerShell pick sites.
  • CUDA_VISIBLE_DEVICES was not treated as a pin. _pick_rocm_gfx_target in
    install_llama_prebuilt.py already resolves all three masks identically ("AMD's HIP
    runtime honours all three env vars with identical semantics"), so a ROCm install
    launched with only that var set was wrongly considered unpinned. It now counts as a
    pin on both sides; "" and -1 still mean "no mask".

Tests

Five regression tests in TestWindowsGfxArchDetection:

case expected
gfx1036 then gfx1200, no mask gfx1200 (discrete wins)
same, output check message names both arches + HIP_VISIBLE_DEVICES
same, HIP_VISIBLE_DEVICES=0 gfx1036 (explicit pin honoured)
gfx1151 then gfx1200, no mask gfx1151 (Strix unchanged)
gfx1036 alone gfx1036 (nothing to prefer)

| CUDA_VISIBLE_DEVICES=0 | gfx1036 (HIP honours it, so it is a pin) |
| CUDA_VISIBLE_DEVICES="" | gfx1200 (empty means no mask) |

Plus TestShadowingIntegratedGfxParity in test_rocm_arch_table_parity.py: the arch
list now exists in both setup.ps1 and install_python_stack.py, so it gets a parity
test like the repo's other duplicated tables, and an assertion that the Strix arches
stay out of both copies.

pytest test_rocm_support.py test_rocm_arch_table_parity.py → 484 passed, 3 skipped.
Across the whole tests/studio/install/ directory the failure count is identical
before and after this branch (47, all pre-existing Windows/macOS-path failures on this
host); the only delta is the 5 new tests passing.

🤖 Generated with Claude Code

…ts (unslothai#7776)

On a board with both an AMD APU and a discrete Radeon, HIP enumerates the
iGPU first, so _detect_windows_gfx_arch picked index 0 and the installer
pulled the iGPU's wheel family -- a gfx1036 Raphael iGPU shadowing a
gfx1200 RX 9060 XT, leaving the discrete card unused until the reporter
set HIP_VISIBLE_DEVICES=1 by hand.

When no visible-device mask is pinned and more than one distinct arch is
enumerated, skip a leading shadowing APU arch so the discrete card decides
the wheel family, and print which GPU was chosen plus the
HIP_VISIBLE_DEVICES override. An explicit HIP_VISIBLE_DEVICES /
ROCR_VISIBLE_DEVICES value still wins verbatim.

The Strix arches (gfx1150/1151/1152) are deliberately excluded from the
skip set: they are first-class unified-memory training targets, so their
selection is unchanged.

Signed-off-by: Tai An <[email protected]>
@Anai-Guo
Anai-Guo requested a review from danielhanchen as a code owner August 3, 2026 13:24
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…_VISIBLE_DEVICES

Addresses both review findings on unslothai#7778.

1. setup.ps1 resolved the gfx arch itself (hipinfo and amd-smi paths) and built
   $ROCmIndexUrl from it *before* invoking install_python_stack.py, and
   _ensure_rocm_torch() returns early once UNSLOTH_ROCM_TORCH_INSTALLED=1 -- so a
   fresh Windows install on a gfx1036 + gfx1200 host still received gfx103X-all
   wheels and never reached the Python-side repick. Resolve-ShadowingGfxPick
   mirrors _dedup_pick() and is applied at both PowerShell pick sites.

2. HIP honours CUDA_VISIBLE_DEVICES with the same semantics as its own masks --
   _pick_rocm_gfx_target in install_llama_prebuilt.py already resolves all three
   identically -- so a ROCm install launched with only CUDA_VISIBLE_DEVICES set
   was treated as unpinned and could be overridden by the iGPU skip. It now
   counts as a pin on both sides ("" / "-1" still mean "no mask").

Tests: CUDA_VISIBLE_DEVICES pin + empty-is-not-a-pin cases, a setup.ps1 <-> Python
parity check on the shadowing-arch list (the list now exists in two places), and
the pre-existing shadowing tests now clear CUDA_VISIBLE_DEVICES so CI runners that
export it cannot flip the assertions.

Signed-off-by: Tai An <[email protected]>
@Anai-Guo

Anai-Guo commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

Thanks — both findings were correct, and both are fixed in 2d2ebc7.

P1 — the repick never fired on a fresh Windows install. Confirmed: setup.ps1 resolves the arch itself ($_hipAllArches[$_hipVisIdx] at the hipinfo path, $allGfxArches[$gpuIdx] at the amd-smi path) and builds $ROCmIndexUrl from it at L3516-3521, all before install_python_stack.py is invoked — and _ensure_rocm_torch() then returns early on UNSLOTH_ROCM_TORCH_INSTALLED=1. So a gfx1036 + gfx1200 host really did keep getting gfx103X-all wheels.

Rather than duplicate the condition, I added Resolve-ShadowingGfxPick next to the existing $script:ROCmGfxArch declaration and applied it at both PowerShell pick sites — the amd-smi branch has the same defect, and _dedup_pick() already covers all three Python probe paths, so this keeps the two entry points symmetric.

P2 — CUDA_VISIBLE_DEVICES was not treated as a pin. Also confirmed: _pick_rocm_gfx_target in studio/install_llama_prebuilt.py (L2248) resolves HIP_VISIBLE_DEVICES / ROCR_VISIBLE_DEVICES / CUDA_VISIBLE_DEVICES identically, with the comment "AMD's HIP runtime honours all three env vars with identical semantics". _visible_devices_pinned() now includes it, as does the PowerShell helper. "" and -1 still mean "no mask", so they do not suppress the skip.

Tests

  • test_cuda_visible_devices_also_pins_the_igpu / test_empty_cuda_visible_devices_is_not_a_pin.
  • TestShadowingIntegratedGfxParity in test_rocm_arch_table_parity.py — the arch list now lives in two files, so it gets a parity test like the other duplicated tables, plus an assertion that the Strix arches stay out of both copies.
  • The five pre-existing shadowing tests now also delenv("CUDA_VISIBLE_DEVICES"). That was a latent flake once the var became a pin: this file already notes elsewhere that CI often exports it.

Verified locally: 484 passed / 3 skipped across test_rocm_support.py + test_rocm_arch_table_parity.py, and the PowerShell helper was exercised directly over 12 cases (unpinned mixed host, Strix untouched, single-APU host, dual same-arch APU, already-discrete pick, null pick, APU-only list, and HIP / CUDA / empty / -1 masks).

@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: 2d2ebc714e

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +651 to +653
"""The shadowing-APU skip (#7776) exists twice: studio/setup.ps1 resolves the
arch and builds $ROCmIndexUrl before it ever invokes the Python stack
installer, so both copies of the list have to agree or one entry point keeps

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 Cover the root Windows installer too

This new parity guard says the skip exists only in studio/setup.ps1 and install_python_stack.py, but the repository still has the root install.ps1 Windows resolver: it assigns $ROCmGfxArch from the first gcnArchName (install.ps1:2150-2153) and later builds $ROCmIndexUrl from that value (install.ps1:2524-2568). Users who install through install.ps1 on a gfx1036,gfx1200 host therefore still get gfx103X-all wheels while this test passes, so include that installer in the shadowing list/selection and parity check.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The observation is right that install.ps1 has the same first-gcnArchName pick, but the stated consequence does not hold, so I am leaving the parity test as is.

Traced the actual install path: install.ps1 resolves the arch and installs ROCm torch, then invokes unsloth studio setup. The ROCm block in setup.ps1 is NOT gated by SKIP_STUDIO_BASE and re-installs with --force-reinstall using the corrected arch, and $CuTag is still cpu on an AMD host because it derives from the pin plus nvidia-smi presence and never inspects the installed wheel. So a gfx1036,gfx1200 host does not end up on gfx103X-all wheels; it downloads them once and then gets gfx120X-all.

The real costs are a wasted multi-GB download, a misleading log line, and a genuine window if install.ps1 fails between its torch install and the setup call. Worth fixing, but it is a third copy of the resolver and install.sh plus studio/setup.sh have the same shape, so it belongs in a focused follow-up that ports the logic to all of them and widens the parity check to every installer at once. Adding the assertion here would just fail against code this PR does not touch.

@Anai-Guo

Anai-Guo commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author

CI triage on the red checks — none of them come from this PR.

Repo tests (CPU) — 2 failures, both pre-existing on main.
tests/studio/test_model_picker_contracts.py::test_a_pinned_cached_row_loads_from_the_id_the_backend_pinned and ::test_chat_autoload_scopes_variant_lookup_to_cached_repo_path. I checked out pristine up/main (5b24b8ac) with no changes from this branch and both fail identically:

2 failed, 112 deselected

Root cause is #7767, which split the query building out of chat-api.ts into ggufVariantsQuery() in features/chat/api/gguf-variants-request.ts and added the abort signal to the picker's call site. The invariants still hold — ggufVariantsQuery still does params.set("prefer_local_cache", "true") / params.set("local_path", localPath), and pickers.tsx:1171 still passes localPath without preferLocalCache — but the contract test still greps for the old inlined literals, so it went stale rather than catching a regression.

Already being fixed in #7629, which retargets both assertions at the new locations. Nothing for this PR to do.

Chat UI Tests (×3). Unsloth UI CI, Mac Studio UI CI and Windows Unsloth UI CI are all failing on main itself (run at 2026-08-03T14:20 on the same 5b24b8ac), so those are red independently of this branch too.

This PR only touches studio/setup.ps1, studio/install_python_stack.py and the two ROCm install tests; the 4-file footprint is unchanged. Core and the ROCm suites are green.

…indows wheels

The shadowing-iGPU preference returned the first non-integrated arch in the
enumeration regardless of whether AMD ships Windows wheels for it. On an
unpinned gfx1036 + gfx1010 host that deposed a supported Raphael APU for a
discrete card absent from _GFX_TO_AMD_INDEX_ARCH, so _windows_rocm_index_url
resolved to None and the install fell back to CPU -- strictly worse than the
shadowing the preference exists to undo.

Only prefer the discrete arch when it actually has an index, unless the
integrated pick has none either, in which case the swap costs nothing and the
discrete card still wins.

Both directions are covered: gfx1036+gfx1010 keeps the APU (fails without this
change, returning gfx1010), gfx1013+gfx1010 still yields to the discrete card
so the guard is not over-tightened.

Signed-off-by: Tai An <[email protected]>
@Anai-Guo

Anai-Guo commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author

Went through all four. One was a real regression this PR introduced — fixed in b1156d9. The other three are pre-existing gaps in paths this PR does not touch, and I'd rather not grow it further; details below.

✅ "Prefer supported dGPUs before dropping the iGPU" — correct, fixed

This one was mine and it was a genuine regression. The predicate returned the first non-integrated arch unconditionally, so on an unpinned gfx1036, gfx1010 host it deposed a supported Raphael APU for a discrete card that isn't in _GFX_TO_AMD_INDEX_ARCH at all. _windows_rocm_index_url then returned None and the install fell back to CPU — strictly worse than the shadowing this preference exists to undo.

Reproduced before fixing rather than taking it on faith:

assert result == "gfx1036"
E   AssertionError: assert 'gfx1010' == 'gfx1036'
   multiple AMD GPUs detected (gfx1036, gfx1010); installing for the discrete gfx1010 instead of the integrated gfx1036.

Now the discrete arch is preferred only when it actually has an index — unless the integrated pick has none either, in which case the swap costs nothing and the dGPU still wins. Both directions are covered (gfx1036+gfx1010 keeps the APU, gfx1013+gfx1010 still yields), so the guard isn't over-tightened. Full test_rocm_support.py: 408 passed, 3 skipped.

⏭️ The other three — real, but pre-existing and outside this PR

  • CUDA_VISIBLE_DEVICES in the arch index (setup.ps1): correct that $_hipVisIdx/$visGpu still resolve from the HIP/ROCR masks only. But that index predates this PR — I added CUDA to the pin gate specifically so a CUDA-masked host is treated as user-pinned and left alone, which is the conservative direction. Making CUDA address the index is a separate behavioural change to device selection generally.
  • Static amd-smi --asic fallback (setup.ps1): also correct — that branch never had the selection logic, and it's reached only when list shows GPU rows without gfx tokens. Worth fixing, but it's a distinct detection path with its own failure mode.
  • Root install.ps1 resolver: correct that install.ps1:2150 has the same first-gcnArchName pick. That's the third installer with this bug and it deserves the same treatment.

All three are the same class of issue as #7776 in code this PR doesn't modify. Happy to do them as a focused follow-up — @danielhanchen, say the word and I'll open one covering the static amd-smi path and the root install.ps1 resolver together, with the parity test widened to all three installers.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

Anai-Guo and others added 2 commits August 4, 2026 12:22
…erence

Both halves of the unslothai#7776 preference existed in install_python_stack.py but only
half of it in the PowerShell mirror, which resolves the arch and builds
$ROCmIndexUrl itself before the Python installer ever runs.

- Resolve-ShadowingGfxPick deposed a supported APU for any discrete arch, even
  one AMD ships no Windows wheels for (gfx1036 + an older gfx1010): the repick
  resolved to no index at all and dropped the host to CPU, strictly worse than
  the shadowing it undoes. It now consults $archFamilyMap, mirroring the
  _pick_has_wheels guard in _dedup_pick(). The map moves to script scope so
  detection can read it; contents are unchanged, so the four-way parity test
  still sees the same 18 entries.
- The WMI fallback took the first AMD adapter before name -> arch inference, so
  an Adrenalin-only host listing a 780M ahead of an RX 9060 XT still inferred
  gfx1103 and installed gfx110X-all. It now keeps every AMD adapter, infers an
  arch for each, and runs the same preference over the list.

Regression tests fail on the previous revision and pass on this one.
@LeoBorcherding

Copy link
Copy Markdown
Collaborator

🌊 Poseidon Review 🐚

Reviewed d0f538d.

Three findings remain open: one P2 (CUDA_VISIBLE_DEVICES pin/index mismatch causes wrong wheels on amd-smi/WMI paths), one P1 docstring that actively contradicts the new shadowing behaviour, and one P3 advisory parity gap. Every run except one raised the same two core issues, which are confirmed present at the current head. Fix the pin/index mismatch first: it is the only one that picks wrong wheels at runtime.

P2 - Add CUDA_VISIBLE_DEVICES to _pick_visible_index and the PS index-pick sites
studio/install_python_stack.py:678

_visible_devices_pinned() checks all three env vars (line 664), but _pick_visible_index() only reads HIP_VISIBLE_DEVICES and ROCR_VISIBLE_DEVICES (line 678). On the amd-smi or WMI fallback paths (which list all GPUs regardless of masks), CUDA_VISIBLE_DEVICES=1 on a gfx1036+gfx1200 host yields: _pick_visible_index returns 0 (gfx1036), _visible_devices_pinned returns True, shadowing is suppressed, and the user gets gfx103X wheels. Before this PR, shadowing would have fired and picked gfx1200, so this is a regression introduced by this diff.

The repo itself documents the gap: install_llama_prebuilt.py:2638 says setup.ps1's pick "is not fully visible-device aware (neither branch reads CUDA_VISIBLE_DEVICES)", and _pick_rocm_gfx_target at line 2248 already resolves all three vars identically. docs/hip-env-variables.md (line 55) lists HIP_VISIBLE_DEVICES or CUDA_VISIBLE_DEVICES together under device-index masks, and line 69 recommends CUDA_VISIBLE_DEVICES for cross-vendor portability.

The same gap exists in setup.ps1: $_hipVisIdx at line 1663 and $visGpu at line 1722 both omit CUDA_VISIBLE_DEVICES. Raised by 10 runs across both logic and standards passes.

# _pick_visible_index: match the three-var loop in _visible_devices_pinned
for _env in ("HIP_VISIBLE_DEVICES", "ROCR_VISIBLE_DEVICES", "CUDA_VISIBLE_DEVICES"):

And in setup.ps1, add $env:CUDA_VISIBLE_DEVICES as a third branch in both the $_hipVisIdx expression (line 1663) and the $visGpu expression (line 1722).

P1 - Update _detect_windows_gfx_arch docstring to match shadowing behaviour
studio/install_python_stack.py:706

Line 706 says "The first GPU is used when no env var is set." That is now false for exactly the scenario this PR adds: on an unpinned gfx1036+gfx1200 host, _dedup_pick returns gfx1200, not the first GPU. A developer debugging arch selection on a mixed system will read this docstring, conclude the shadowing logic does not exist, and file a bug against correct behaviour.

The same paragraph (lines 704-706) also omits CUDA_VISIBLE_DEVICES, which now controls whether the shadowing skip fires via _visible_devices_pinned(). Both issues live in the same three lines and one rewrite fixes both. Raised by 12+ runs, with several rating the "first GPU" claim as P1.

    On multi-GPU hosts, detected gfx tokens are deduplicated (preserving
    enumeration order). HIP_VISIBLE_DEVICES / ROCR_VISIBLE_DEVICES /
    CUDA_VISIBLE_DEVICES selects which to install for; without a pin and
    with a shadowing iGPU leading the enumeration, the discrete GPU is
    preferred instead.

P3 - Add multi-GPU advisory to Resolve-ShadowingGfxPick for non-shadowing hosts
studio/setup.ps1:1559

The Python _dedup_pick prints an advisory ("multiple AMD GPUs detected; installing for {pick}. Set HIP_VISIBLE_DEVICES...") at lines 747-751 whenever 2+ distinct arches are present and the pick is not a shadowing iGPU. Resolve-ShadowingGfxPick returns silently at line 1559 when $Picked is not in $ShadowingIntegratedGfx, so a fresh Windows install with two discrete AMD GPUs (e.g. gfx1100 + gfx1200) picks the first one with no hint to the user that another GPU exists or that HIP_VISIBLE_DEVICES can target it. The function's own comment says it "Mirrors _dedup_pick()".

After the early return at line 1559, when $distinctArches.Count -ge 2, add the same advisory substep that the shadowing branch uses, matching the Python path's unconditional message.

_visible_devices_pinned() treats CUDA_VISIBLE_DEVICES as a pin, but
_pick_visible_index() only read HIP/ROCR. On the probes that enumerate
every GPU regardless of the masks (amd-smi, WMI), CUDA_VISIBLE_DEVICES=1
on a gfx1036 + gfx1200 host therefore suppressed the shadowing skip *and*
resolved to index 0, installing the iGPU's wheels for the device the user
masked away. Same mismatch in setup.ps1's $_hipVisIdx and $visGpu picks.

All three masks are now read at every site, matching _pick_rocm_gfx_target
in install_llama_prebuilt.py. Also corrects the _detect_windows_gfx_arch
docstring, which still claimed the first GPU always wins without a mask.

Signed-off-by: Tai An <[email protected]>
@Anai-Guo

Anai-Guo commented Aug 5, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @LeoBorcherding — the P2 is right and I was wrong to defer it. Fixed in a7f0dae, together with the P1 docstring.

P2 — pin/index mismatch (real regression, this PR's)

The argument that convinced me is that _visible_devices_pinned() and _pick_visible_index() have to read the same set of masks or they disagree about which device the user chose. Adding CUDA_VISIBLE_DEVICES to only the first one is what created the new failure mode: on the probes that enumerate every GPU regardless of the masks (amd-smi, WMI), CUDA_VISIBLE_DEVICES=1 on a gfx1036 + gfx1200 host suppressed the shadowing skip and resolved to index 0 — gfx103X wheels for the card the user masked away. Before this PR the skip would have fired and landed on gfx1200, so it was strictly a regression I introduced.

Reproduced against the old source before fixing rather than taking it on faith:

assert result == "gfx1200"
E   AssertionError: assert 'gfx1036' == 'gfx1200'

All three masks are now read at every site — _pick_visible_index, and both PowerShell picks ($_hipVisIdx at the hipinfo branch, $visGpu at the amd-smi branch) — which also brings them in line with _pick_rocm_gfx_target in install_llama_prebuilt.py, the one resolver that already did this. The out-of-range warning now names CUDA_VISIBLE_DEVICES too.

Three regression tests, each verified failing on the pre-fix source and passing after:

  • test_cuda_visible_devices_indexes_the_enumeration — the gfx1036 + gfx1200 case above.
  • test_cuda_visible_devices_index_matches_the_other_masks — 1 / 1,0 / 7 / -1 / "" / UUID all resolve identically to the HIP spelling, so the fallbacks didn't drift.
  • test_index_picks_read_the_same_masks_as_the_pin_check — asserts both setup.ps1 index expressions read the third mask, so the PowerShell mirror can't silently fall behind again.

The existing test_cuda_visible_devices_also_pins_the_igpu still passes unchanged (CUDA_VISIBLE_DEVICES=0 → index 0 → gfx1036), so the pin semantics are unaffected. Full run: 493 passed, 3 skipped across test_rocm_support.py + test_rocm_arch_table_parity.py.

P1 — docstring

Rewritten, and it now names CUDA_VISIBLE_DEVICES alongside the other two:

Without a mask, the first GPU is used — except when it is a shadowing iGPU leading the enumeration, in which case the discrete GPU is preferred (issue #7776).

I also dropped the now-stale "ignores CUDA_VISIBLE_DEVICES" clause from the test_forwarded_gfx_does_not_clobber_probed_arch comment; the amd-smi branch still drops comma masks, so the rest of that comment stands. The same parenthetical in install_llama_prebuilt.py:2638 is now partly stale too, but that file isn't in this PR's footprint — happy to fix it in the follow-up.

P3 — advisory parity

Leaving this one out deliberately. Resolve-ShadowingGfxPick returning silently for two discrete AMD GPUs is a real gap, but it's a message-only change on a path this PR doesn't otherwise touch, and it belongs with the other two follow-ups I offered above (the static amd-smi --asic branch and the root install.ps1 resolver, which has the same first-gcnArchName pick). @danielhanchen — say the word and I'll open one PR covering all three with the parity test widened to every installer.

Footprint is unchanged at 4 files.

🤖 Generated with Claude Code

chatgpt-codex-connector[bot]

This comment was marked as resolved.

…7778

The shadowing-iGPU preference was correct, but each pick site still resolved
HIP/ROCR/CUDA_VISIBLE_DEVICES with its own inline expression and they disagreed,
so a mask the pin check honoured could resolve to a different GPU than the one
the user asked for. On a mixed host that lands on index 0, which is the iGPU the
preference exists to skip.

setup.ps1
- Add Resolve-VisibleGpuIndex and use it at all four pick sites (hipinfo,
  amd-smi list, amd-smi static --asic, WMI name inference). Previously the
  hipinfo expression rejected " 1 " and the amd-smi one rejected "1,0".
- The static --asic branch now collects every gfx token and runs the repick
  instead of taking the first regex match.
- WMI inference indexes the adapter list rather than the inferred arch list, so
  an unrecognised name cannot shift a mask onto the wrong physical card, and it
  only repicks when every adapter mapped: an unknown name may itself be the
  discrete card.
- Filter WMI adapters on ConfigManagerErrorCode so a disabled or driver-errored
  Radeon cannot depose a working iGPU. Get-CimInstance to match the rest of the
  repo.

install_python_stack.py
- _pick_visible_index now skips "" and "-1" and reads the next mask, matching
  _visible_devices_pinned. Before, HIP_VISIBLE_DEVICES="" with
  CUDA_VISIBLE_DEVICES=1 counted as pinned while the index resolved to GPU 0.
- Out-of-range and unparseable masks warn instead of silently using GPU 0.
- Strip the ":sramecc+:xnack-" suffix from gcnArchName like setup.ps1 does; a
  suffixed token matched neither the wheel table nor the skip set.
- Prefer a wheel-backed candidate whenever one exists, not only when the picked
  arch has wheels, so gfx1036 + gfx1010 + gfx1200 no longer stops at gfx1010
  and drops the host to CPU torch.

Arch list
- Drop gfx1037: it is not an AMDGPU target in LLVM, so no Windows tool emits it.
- Add gfx1033 (Van Gogh) and gfx1153 (Krackan Point 2), both APUs. gfx1033 has a
  wheel family, so leaving it out let it act as the "discrete" card.
- gfx1013 is Cyan Skillfish, not Van Gogh.

Both advisories now tell the user to setx HIP_VISIBLE_DEVICES so the chosen GPU
is used at runtime, not just at install time: the wheels alone do not change
which device HIP enumerates first.

Tests
- TestSetupPs1ShadowingBehaviour actually executes Resolve-ShadowingGfxPick and
  Resolve-VisibleGpuIndex under pwsh, slicing them out by AST. The existing
  parity class only greps text, so a rename failed it while a semantic bug
  passed.
- Regression tests for each fix above.
@danielhanchen

Copy link
Copy Markdown
Member

Thanks for this, the shadowing preference itself is the right call and the wheel-aware guard you added in b1156d9 is a good catch. I went through it in detail and pushed 6d0ac82 directly to the branch rather than leaving another round of comments, since most of what I found was the same root cause repeated.

The root cause

Every pick site resolved the visible-device masks with its own inline expression, and they did not agree with each other or with Resolve-ShadowingGfxPick's pin check. When the pin check honours a mask but the index expression cannot parse it, the index falls back to 0, which on a mixed host is exactly the iGPU the preference exists to skip.

Running the real expressions from setup.ps1 under pwsh:

mask hipinfo index amd-smi index expected
1 1 1 1
1,0 1 0 1
" 1 " 0 1 1

The two branches fail on opposite inputs. -match '^\d' rejects the leading space; '^\s*(\d+)\s*$' rejects the comma list.

So instead of patching each site, 6d0ac82 adds one Resolve-VisibleGpuIndex and uses it at all four: hipinfo, amd-smi list, amd-smi static --asic, and WMI name inference.

What else that fixed

WMI ignored the mask entirely. The call site always passed $nameArches[0], so Resolve-ShadowingGfxPick saw a pin and returned it unchanged. On a 780M + RX 9060 XT host, no value of any mask could select the discrete card, while the function was printing "Set HIP_VISIBLE_DEVICES to the GPU index you want". It now indexes the adapter list rather than the inferred arch list, so an unrecognised name cannot shift the mask onto the wrong physical card. Verified against the real block:

780M+RX9060XT unpinned   -> gfx1200
780M+RX9060XT HIP=1      -> gfx1200   (was gfx1103)
780M+RX9060XT HIP=0      -> gfx1103
780M+RX9060XT CUDA=1,0   -> gfx1200   (was gfx1103)

An unrecognised discrete card could hand the machine to the iGPU. A name not in $nameArchTable never entered $AllArches, so on a 780M + RX 5700 XT host the iGPU won silently. The repick now only runs when every adapter mapped, since an unknown name may itself be the discrete card.

Disabled adapters counted. Collecting every AMD adapter means a disabled or driver-errored Radeon could depose a working iGPU, so there is now a ConfigManagerErrorCode filter. Also switched to Get-CimInstance, which is what the rest of the repo uses and which does not silently no-op the whole block under PowerShell 7.

The static --asic branch now collects all tokens and runs the repick instead of taking the first regex match.

Python side

_pick_visible_index returned 0 on the first mask that was set at all, while _visible_devices_pinned skipped "" and -1 and read the next one. So HIP_VISIBLE_DEVICES="" with CUDA_VISIBLE_DEVICES=1 counted as pinned, suppressed the repick, and resolved to GPU 0. That is the exact failure the docstring you added says the change prevents. It now skips those spellings too.

Also: out-of-range and unparseable masks warn instead of silently using GPU 0; gcnArchName suffixes are stripped like setup.ps1 does, since gfx90a:sramecc+:xnack- matched neither the wheel table nor the skip set; and the candidate loop prefers a wheel-backed arch whenever one exists, so gfx1036, gfx1010, gfx1200 no longer stops at gfx1010 and drops the host to CPU torch.

Arch list

  • Removed gfx1037. It is not an AMDGPU target in LLVM, so no Windows tool emits it.
  • Added gfx1033 (Van Gogh) and gfx1153 (Krackan Point 2), both APUs upstream. gfx1033 matters because it has a wheel family, so leaving it out let it act as the "discrete" card that deposes another APU.
  • gfx1013 is Cyan Skillfish, not Van Gogh.

Tests

TestSetupPs1ShadowingBehaviour now actually executes Resolve-ShadowingGfxPick and Resolve-VisibleGpuIndex, slicing them out of setup.ps1 by AST so nothing is dot-sourced. The existing parity class only greps text, which means a rename fails it while a semantic bug passes. Plus regression tests for each fix above.

510 passed, 1 skipped on test_rocm_support.py + test_rocm_arch_table_parity.py. Across tests/studio/install/ the failure set is identical to main (3 pre-existing failures in test_managed_node_runtime.py and 1 pre-existing collection error in test_rocm_rdna_routing.py, all present on a clean main here). setup.ps1 parses with 0 AST errors and ruff is clean.

One thing worth deciding before merge

This fixes which wheels get installed. It does not change which GPU is used at runtime. Nothing under studio/backend/ writes a visibility mask except apply_gpu_ids, which no-ops when no ids are passed, and auto_select_gpu_ids ranks purely on free VRAM with no arch awareness. So on the reported host HIP still enumerates the gfx1036 as device 0 after a correct gfx120X-all install. The reporter's setx HIP_VISIBLE_DEVICES 1 was fixing that half.

I reworded both advisories to say setx and "at runtime too, not just at install time" so the message is at least accurate, but properly closing #7776 probably means persisting the install-time arch and applying the matching mask at startup. Happy to take that separately.

Left for follow-up

  • install.ps1 still has all four unfixed pick sites and its own $archFamilyMap. setup.ps1 re-installs afterwards with --force-reinstall and is not gated by SKIP_STUDIO_BASE, so the wheels do end up correct on the happy path, but it is a wasted multi-GB download and a wrong log line, and it is genuinely wrong if install.ps1 fails between its torch install and its studio setup call. Worth doing with install.sh and the parity test widened to all installers, as you offered.
  • _pick_rocm_gfx_target in install_llama_prebuilt.py has no shadowing preference, so _apply_host_overrides treats the forwarded gfx1200 as advisory and discards it. Net result on a mixed host is gfx120X torch with an iGPU-targeted llama.cpp bundle. I only corrected the now-stale comment there rather than growing this PR further.
  • $nameArchTable cannot produce 5 of the 6 skip-set arches, including the gfx1036 from the issue, so the WMI repick only really engages for gfx1103 today. Adding those rows touches the name table in five files, so it belongs with the parity work above.
  • Whether gfx1103 should be deposed at all is a policy call I did not change. It is a supported Windows wheel target and we treat Phoenix as a first-class training target elsewhere, so a 780M currently loses to a 4GB RX 6400. Your call.

@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: afa7ad76f9

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread studio/install_llama_prebuilt.py Outdated
Comment on lines +2638 to +2640
# setup.ps1 resolves all three masks through Resolve-VisibleGpuIndex, but it also
# applies the shadowing-iGPU preference (#7776) that _pick_rocm_gfx_target() does
# not, so the two can name different GPUs on a mixed APU + dGPU host.

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 setup's dGPU repick when selecting prebuilts

In the mixed APU+dGPU case described here, setup.ps1 now forwards the repicked dGPU arch (for example gfx1200), but _apply_host_overrides() still classifies any forwarded arch that appears in the probed physical list as advisory and returns the probed active arch unchanged when _active is the leading iGPU. As a result, the same fresh Windows install can install PyTorch for the dGPU while install_llama_prebuilt.py downloads the iGPU ROCm prebuilt, because the newly forwarded repick is discarded; either apply the same shadowing preference to the probe or treat setup's repicked --rocm-gfx as authoritative for the prebuilt selection.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked this one closely and I do not think it holds. On the host in the issue there is no iGPU prebuilt to download: _gfx_is_windows_hip_supported("gfx1036") is False and gfx1036 is absent from WINDOWS_HIP_PREBUILT_GFX_TARGETS (gfx1030/1031/1032/1034/1100/1101/1102/1103/1150/1151/1200/1201/908/90a), so the described split cannot occur there.

It does reach on a gfx1103 + gfx1200 host, but honouring the forward would make it worse, not better: the prebuilt has to run on HIP device 0, which is still the 780M until the user sets the mask. Installing the gfx1200 bundle there yields something unrunnable, whereas keeping gfx1103 runs. And it self-corrects: once HIP_VISIBLE_DEVICES=1 is set, hipinfo enumerates only gfx1200, _active equals the forward, and the forward becomes authoritative. That is the behaviour test_forwarded_gfx_does_not_clobber_probed_arch pins deliberately. Leaving as is.

danielhanchen and others added 2 commits August 6, 2026 07:13
Both found by simulating the change across the OS x GPU-vendor product rather
than by reading it.

- _dedup_pick now says so when an honoured pin selects a GPU AMD ships no
  Windows wheels for while another enumerated GPU has them. The pin is still
  honoured verbatim, but the install drops to CPU torch and the mask is the
  reason, which was previously invisible.

- _pick_visible_index takes warn=False for callers whose list is deduplicated.
  The Linux Strix reroute indexes _detect_amd_gfx_codes(), which collapses
  duplicates, so a dual same-arch box (two gfx1151) has a 1-element list and a
  perfectly valid HIP_VISIBLE_DEVICES=1 read as out of range. That printed a
  false "out of range" warning on a healthy Linux host. The Windows
  arch-selection path still warns, where the index space really is devices.
@danielhanchen

Copy link
Copy Markdown
Member

Simulated the branch rather than just reading it, because the risky part of this change is the paths it does not obviously touch. Two more fixes came out of that, pushed in 3ac47de.

Coverage

Simulation Cases Result
Python detection, 3-way differential (merge-base vs PR vs now) across 3 probes x 34 topologies x 20 mask spellings 1620 0 crashes
OS x GPU vendor: [Windows, Linux, WSL, macOS] x [NVIDIA, AMD, CPU-only] x masks 468 0 leaks off Windows
PowerShell, executing the real helpers and all four pick sites under pwsh 100 100/100
Upgrade and escape-hatch scenarios 11 0 unintended changes
Python 3.9 / 3.10 / 3.11 / 3.12 / 3.13 syntax, plus a clean uv venv with no third-party packages 6 all OK

Backwards compatibility

  • 800 single-GPU cases, 20 changed. All 20 are the same improvement: gfx90a:sramecc+:xnack- now resolves to gfx90a instead of failing every table lookup. That is CPU torch to working wheels, never the reverse.
  • 0 unmasked hosts lost wheels. With no mask set, every difference from the merge-base is either the intended iGPU to dGPU repick or a CPU to wheels gain.
  • 18 wheel index URLs, 0 drift. Every known arch resolves to exactly the same index as before.
  • Off-Windows is structurally unreachable. 468 OS x GPU combinations produced zero gfx arches on Linux, WSL and macOS, and zero calls into the Windows detector. NVIDIA hosts never enter the AMD block.
  • Strix gfx1150/1151/1152 preserved in every topology, single and mixed.
  • UNSLOTH_ROCM_GFX_ARCH still short-circuits every probe, and an existing setx HIP_VISIBLE_DEVICES 1 from the issue workaround still resolves to gfx1200.

The 40 cases that now install CPU torch

These are all a host where the user explicitly pinned a GPU that has no AMD Windows wheels, for example CUDA_VISIBLE_DEVICES=1 on gfx1036, gfx1010. All 40 land exactly on the token at the pinned index, so this is the mask being honoured rather than ignored, which is what it was before. It is still worth saying out loud, so _dedup_pick now prints which GPU was pinned, that it has no wheels, and which other GPU on the box does.

The regression the simulation caught

_pick_visible_index is shared with the Linux Strix reroute, and that caller indexes _detect_amd_gfx_codes(), which deduplicates. On a dual same-arch box (two gfx1151) the list has one element, so a perfectly valid HIP_VISIBLE_DEVICES=1 read as out of range and printed a false "out of range" warning on a healthy Linux host. That caller now passes warn=False; the Windows path, where the index space really is devices, still warns.

Not applicable

No browser surface is involved. The diff is 3 Python/PowerShell installer files plus tests, with no .ts/.tsx/.js/.html/.css and no import from the Studio API or route layer, so there is nothing for Firefox, Chrome, Safari or Edge to render differently.

Note on the underlying issue

Worth recording that this is a well-known failure class rather than a one-off report: ROCm/legacy-rocm-build#6167 documents gfx1036 appearing as an enumerable device only once a dGPU is installed, and ollama/ollama#11975 and lemonade-sdk/lemonade#2296 are the same iGPU-shadows-dGPU shape in other stacks. AMD's own guidance is HIP_VISIBLE_DEVICES on Windows and ROCR_VISIBLE_DEVICES on Linux, which is what the reworded advisory now tells people.

Sim scripts are throwaway and not in the diff; happy to attach them if useful.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 6, 2026
…fixes

Three review items on the last round, all confirmed against the code at head.

Mask semantics (mine to fix: 6d0ac82 got this wrong).
"" and "-1" do not mean "no mask", they select no GPU at all, so falling
through to the next variable was wrong. The ROCm runtime stores an explicitly
empty var as " " (clr flags.cpp), then picks the HIP mask whenever its first
byte is not NUL (paldevice.cpp on Windows, rocdevice.cpp on Linux), so an empty
HIP_VISIBLE_DEVICES shadows CUDA_VISIBLE_DEVICES rather than deferring to it;
parseRequestedDeviceList surfaces zero devices for " " and "-1", which ROCR
states outright in amd_filter_device.h. _visible_devices_pinned and
_pick_visible_index are now first-set-wins and treat any set value as a
deliberate selection, matching _pick_rocm_gfx_target in install_llama_prebuilt.py
and PyTorch's own _parse_visible_devices. Resolve-VisibleGpuIndex and
Resolve-ShadowingGfxPick mirror it. Three tests asserted the old premise and now
assert the runtime's.

Resolve-ShadowingGfxPick did not prefer wheel-backed cards when the APU has no
wheels either. The predicate went vacuously true and took the first
non-integrated arch, so gfx90c,gfx1010,gfx1200 picked gfx1010, left
$ROCmIndexUrl null and installed CPU torch despite the supported gfx1200. Now
mirrors _dedup_pick's `_withWheels or (...)`.

The Python WMI probe listed disabled adapters. setup.ps1 filters on
ConfigManagerErrorCode but `(Get-CimInstance Win32_VideoController).Name` did
not, so on a driver-only laptop a disabled RX 9060 could depose a working 780M
and pull wheels for a GPU Windows never exposes. Same filter both sides.
danielhanchen added a commit to Datta0/unsloth-staging-3 that referenced this pull request Aug 6, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

danielhanchen added a commit to Datta0/unsloth-staging-3 that referenced this pull request Aug 6, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@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: 1cb4f48d51

ℹ️ 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.ps1 Outdated
Comment on lines +1624 to +1626
# HIP still enumerates the iGPU as device 0 at runtime, so the wheels alone do
# not steer training to the dGPU. setx makes it persist across sessions.
substep "Run 'setx HIP_VISIBLE_DEVICES $discreteIdx' and reopen your terminal so Unsloth uses $discreteArch at runtime too, not just at install time" "Cyan"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Persist the dGPU selection for runtime

On an unmasked mixed APU+dGPU host such as gfx1036,gfx1200, this function selects and installs the gfx120X-only wheel family but merely prints the setx command. HIP therefore still exposes the incompatible iGPU as device 0 on subsequent launches unless the user notices and manually runs the command, so training can continue targeting the shadowing APU or fail because the installed wheel does not support it. Apply the selected mask automatically (or persist an equivalent Studio-specific runtime setting) when repicking the dGPU.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The gap is real and I want it on record, but I am not taking the proposed remedy in this PR.

Accurate parts: this changes install-time wheel selection only, and on an unmasked mixed host HIP still enumerates the APU as device 0 afterwards. I flagged that limitation when I first reviewed this PR, and it is why the advisory prints the setx line twice.

Why not apply the mask automatically. setx writes a persistent per-user environment variable into the registry. It affects every HIP and ROCm application on that machine, not just Unsloth, it outlives an uninstall, and it strands the user if they later remove the discrete card. Silently setting it also contradicts the rule the rest of this PR is built on, which is that a visible-device selection belongs to the user and is honoured verbatim; the shadowing repick deliberately never overrides one. An installer that writes the mask on the user's behalf is a product decision with side effects outside this project, not a correctness fix, so it is the maintainer's call rather than something to slip into a wheel-selection PR.

On the Studio-specific alternative: Studio already pins the mask for its own child processes at spawn (_pin_visible_gpu_env in studio/backend/core/inference/llama_cpp.py sets CUDA_VISIBLE_DEVICES plus a HIP or ROCR mirror, and the comment there is explicitly about a gfx1036 iGPU under a gfx103X prebuilt). So the runtime layer already has a device-selection mechanism, in files this PR does not touch. Making that layer capability-aware for mixed AMD hosts is worth doing and is a separate change with its own tests.

Net effect for the reported host is still a strict improvement rather than a regression: before, it installed gfx103X wheels that cannot drive the RX 9060 XT, which is what #7776 reported; now it installs wheels for the discrete card and tells the user the one command that makes the runtime match. Leaving this open as a follow-up rather than half-implementing it here.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 6, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 6, 2026
danielhanchen added a commit to Datta0/unsloth-staging-3 that referenced this pull request Aug 6, 2026
danielhanchen added a commit to Datta0/unsloth-staging-3 that referenced this pull request Aug 6, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

danielhanchen added a commit to Datta0/unsloth-staging-3 that referenced this pull request Aug 6, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: e2418911ee

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

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 6, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 6, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 6, 2026
@danielhanchen
danielhanchen merged commit 0c12d64 into unslothai:main Aug 6, 2026
1 of 68 checks passed
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.

[Bug] AMD GPU not loading into VRAM or detected

3 participants