Skip to content

Studio: prefer the discrete AMD GPU when an iGPU enumerates first on Linux - #11437

Merged
shimmyshimmer merged 2 commits into
mainfrom
amd-igpu-linux-setup-sh-discrete-preference
Sep 21, 2026
Merged

shimmyshimmer merged 2 commits into
mainfrom
amd-igpu-linux-setup-sh-discrete-preference

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Follow-up to a report on #11143: on Fedora with an integrated AMD GPU driving the desktop plus a discrete RX 9060 XT (gfx1200), Studio kept using the iGPU and ROCm even though the user had configured the discrete card and Vulkan. Desktop glitches, out of memory errors on settings that previously worked, and throughput dropping from 21 tok/s to 5 tok/s. Only a full uninstall and reinstall cleared it.

The gap

studio/setup.sh resolves the AMD gfx arch at visible index 0 and forwards it as --rocm-gfx, with no preference for a discrete card. install_llama_prebuilt.py reads a forwarded --rocm-gfx as implying the host has ROCm. So on a hybrid host whose iGPU enumerates first, the iGPU's arch is forwarded, matches no published linux-rocm bundle, and the install falls through to a source build.

The preference that avoids exactly this landed for #7776 in install.sh, studio/setup.ps1 and studio/install_python_stack.py. It never landed in studio/setup.sh, which is the script unsloth studio update runs in full, so the Linux update path was the one entry point still picking whatever enumerated first. That is also why the Windows and macOS reporters on the same thread did not see this.

What changed

Ported _amd_gfx_is_shadowing_integrated and _amd_prefer_discrete_gfx from install.sh into studio/setup.sh, applied before the arch reaches either consumer (_PREBUILT_CMD and _WHISPER_CMD). The table is copied verbatim so the existing parity test keeps both copies in step.

install.sh additionally filters candidates through a wheel-route predicate. That is deliberately not copied: there the consumer is a torch wheel family, here it is the llama.cpp prebuilt bundle. There is a comment saying so, so it does not get "restored" later.

Not affected

Verified by executing the helpers under both dash and bash:

host before after
gfx1036 iGPU first, gfx1200 discrete gfx1036 gfx1200
discrete already first gfx1200 gfx1200
single discrete gfx1200 gfx1200
single APU (Steam Deck class) gfx1036 gfx1036
every arch integrated gfx1036 gfx1036
only peer is gfx906 gfx1036 gfx1036
gfx1036, gfx906, gfx1100 gfx1036 gfx1100
Strix gfx1151 + gfx1100 gfx1151 gfx1151

Any of HIP_VISIBLE_DEVICES, ROCR_VISIBLE_DEVICES or CUDA_VISIBLE_DEVICES being set returns the pick unchanged, since masking a device is a deliberate choice and it is the documented workaround on #7624 and #7669. An iGPU only host keeps its iGPU, which is what ROCM_RDNA2_APU.md documents. UNSLOTH_ROCM_GFX_ARCH still wins outright. NVIDIA hosts never enter the branch.

The parity test guarded the wrong half

test_rocm_arch_table_parity.py kept the gfx tables in step across installers but never checked that anything applied them. Its own docstring describes install_llama_prebuilt.py as honouring "setup's repick", and on Linux there was no repick to honour. That is how this survived.

Extended it to assert the preference is reached on each shell entry point's resolution path, that it runs before the arch is forwarded to both consumers, that all three visibility masks are honoured, and that gfx906 is never a repick candidate. These fail on the current tree for studio/setup.sh and pass for install.sh, so the guard discriminates rather than failing blanket.

One note for reviewers: the ordering assertion anchors on _PREBUILT_CMD+=(--rocm-gfx rather than a bare --rocm-gfx, because the flag is named in eight comments before the first real argv site and a substring search there cannot be satisfied by any correct placement.

Tests

tests/studio/install/: test_rocm_arch_table_parity.py 106 passed, plus test_rocm_support.py, test_selection_logic.py, test_rocm_rdna_routing.py, test_rocm_gfx_spoof_7331.py, test_gpu_detection_followups.py, test_unsupported_arch_routing_guards_8529.py and test_setup_keeps_gpu_prebuilt_over_cpu_source_fallback.py: 1259 passed, 1 skipped.

bash -n clean and shellcheck unchanged at 50 findings before and after. sh -n fails at line 124 both before and after this change: the file is #!/usr/bin/env bash and already uses bash arrays.

Scope and limits

This covers the rocminfo and amd-smi arch paths. It does not cover the name inference fallback: if a record set carries marketing names but no arches, device 0 can still yield an integrated arch. Fixing that means inferring an arch for every record and changes the inference path's behaviour, so it is left for a follow-up.

_pick_rocm_gfx_target in install_llama_prebuilt.py has the same missing preference and ends return _tokens[0]. That fix, the marker handling that erases an unsatisfied Vulkan choice, and the GPU pin handling are a stacked follow-up.

I could not reproduce the reporter's throughput recovery or their desktop glitches, which need that hardware.

…Linux

studio/setup.sh resolved the AMD gfx arch at visible index 0 and forwarded it
as --rocm-gfx, with no preference for a discrete card. On a hybrid host whose
integrated GPU enumerates first, that forwards the iGPU's arch, and
install_llama_prebuilt.py reads a forwarded --rocm-gfx as implying the host has
ROCm. The iGPU's arch then matches no published linux-rocm bundle, so the
install falls through to a source build.

The preference that avoids this landed for #7776 in install.sh, studio/setup.ps1
and studio/install_python_stack.py, but never in studio/setup.sh, which is the
script that "unsloth studio update" runs in full. So the Linux update path was
the one entry point still picking whatever enumerated first.

Port the two helpers from install.sh and apply the repick before the arch
reaches either consumer (_PREBUILT_CMD and _WHISPER_CMD). The table is copied
verbatim so the existing parity test keeps both copies in step. The wheel-route
filter is deliberately not copied: install.sh uses it to pick a torch wheel
family, while the consumer here is the llama.cpp prebuilt bundle.

Unchanged by construction: single GPU hosts, NVIDIA hosts, hosts whose first
arch is not a known shadowing iGPU, and any host where the user set
HIP_VISIBLE_DEVICES, ROCR_VISIBLE_DEVICES or CUDA_VISIBLE_DEVICES, since
masking a device is a deliberate choice the repick must not second guess. An
iGPU only host keeps its iGPU, which is what ROCM_RDNA2_APU.md documents.
UNSLOTH_ROCM_GFX_ARCH still wins outright.

The parity test guarded the gfx tables but not whether anything applied them,
which is how this gap survived: its own docstring describes
install_llama_prebuilt.py as honouring "setup's repick", and on Linux there was
no repick to honour. Extend it to assert the preference is reached on each
shell entry point's resolution path, that it runs before the arch is forwarded
to both consumers, that all three visibility masks are honoured, and that
gfx906 is never a repick candidate.

Reported on #11143 by a user on Fedora with an integrated GPU driving the
desktop and a discrete RX 9060 XT (gfx1200).
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-21T12:22:52.426201Z fe8a1b9 New commits
🔒 Security Review ✅ Completed 2026-09-21T11:56:16.157712Z 177ed3c Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@shimmyshimmer

Copy link
Copy Markdown
Member

@codex security review

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: 177ed3cc0f

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 177ed3cc0f

ℹ️ 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 and docstring only, no code change.
@shimmyshimmer
shimmyshimmer merged commit 17e1490 into main Sep 21, 2026
48 of 52 checks passed
@shimmyshimmer
shimmyshimmer deleted the amd-igpu-linux-setup-sh-discrete-preference branch September 21, 2026 14:00
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 21, 2026
…unslothai#11472)

test_setup_gpu_summary_probe_sources.sh builds a runnable block by pulling named
functions out of studio/setup.sh. unslothai#11437 added _amd_prefer_discrete_gfx and
_amd_gfx_is_shadowing_integrated and called them from the selection block, but not from
a function the test extracts, so the block ran with both undefined: 8 passed, 29 failed,
every case that reaches a discrete versus integrated choice dying on command not found.

Both helpers are now extracted into both halves the test builds, and a guard fails
loudly if either stops arriving, the way the existing ones do for the record parsers.
37 passed, 0 failed on main, and every other suite under tests/sh stays green.
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 22, 2026
…othai#11538)

The harness builds a runnable block by sed-ing named functions out of
studio/setup.sh. When setup.sh gains a helper and the selection block
calls it, nothing here notices until the block runs with it undefined,
and then every case that reaches the call dies on a bare "command not
found". unslothai#11437 did exactly that with _amd_prefer_discrete_gfx: 8 passed,
29 failed, and not one of the 29 named the cause.

unslothai#11472 added the helper to the sed lines and a named grep guard for it,
which fixes that helper and only that helper. The next one repeats it.

Give each extracted artifact a command_not_found_handle instead, so the
first call that is actually reached says which function is missing and
which sed line would supply it.

Reached, not merely present: setup.sh arms this test never takes do call
helpers it never extracts (the NVIDIA banner, the step/substep
printers), so a static scan of the text reports those as defects when
they are not. A static version of this guard failed the suite on all
three.
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
…Linux (unslothai#11437)

studio/setup.sh resolved the AMD gfx arch at visible-index 0 and forwarded it as --rocm-gfx, so on a hybrid host an integrated GPU that enumerated first decided the llama.cpp bundle for the whole machine while torch targeted the discrete card. install.sh, setup.ps1 and install_python_stack.py all applied the shadowing-iGPU preference; setup.sh had neither the table nor the preference, and it is the script `unsloth studio update` runs in full.

Ports _amd_gfx_is_shadowing_integrated and _amd_prefer_discrete_gfx into setup.sh and applies the repick before the UNSLOTH_ROCM_GFX_ARCH override. An explicit HIP_/ROCR_/CUDA_VISIBLE_DEVICES mask is never second-guessed, gfx906 is never a repick candidate, and an all-integrated host keeps its own arch.

Extends tests/studio/install/test_rocm_arch_table_parity.py to hold the APPLICATION of the preference in parity across installers, not just the table, which is how this gap escaped.
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
…unslothai#11472)

test_setup_gpu_summary_probe_sources.sh builds a runnable block by pulling named
functions out of studio/setup.sh. unslothai#11437 added _amd_prefer_discrete_gfx and
_amd_gfx_is_shadowing_integrated and called them from the selection block, but not from
a function the test extracts, so the block ran with both undefined: 8 passed, 29 failed,
every case that reaches a discrete versus integrated choice dying on command not found.

Both helpers are now extracted into both halves the test builds, and a guard fails
loudly if either stops arriving, the way the existing ones do for the record parsers.
37 passed, 0 failed on main, and every other suite under tests/sh stays green.
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
…othai#11538)

The harness builds a runnable block by sed-ing named functions out of
studio/setup.sh. When setup.sh gains a helper and the selection block
calls it, nothing here notices until the block runs with it undefined,
and then every case that reaches the call dies on a bare "command not
found". unslothai#11437 did exactly that with _amd_prefer_discrete_gfx: 8 passed,
29 failed, and not one of the 29 named the cause.

unslothai#11472 added the helper to the sed lines and a named grep guard for it,
which fixes that helper and only that helper. The next one repeats it.

Give each extracted artifact a command_not_found_handle instead, so the
first call that is actually reached says which function is missing and
which sed line would supply it.

Reached, not merely present: setup.sh arms this test never takes do call
helpers it never extracts (the NVIDIA banner, the step/substep
printers), so a static scan of the text reports those as defects when
they are not. A static version of this guard failed the suite on all
three.
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.

2 participants