Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

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

Daniel Han17e14909f27b
diff --git a/studio/setup.sh b/studio/setup.sh
index 72e52b5deb2..6e5c6a6740a 100755
--- a/studio/setup.sh
+++ b/studio/setup.sh
@@ -3501,6 +3501,48 @@ _setup_supported_gfx_from_name() {
printf '%s\n' "$_sup_gfx_out"
}
+# Mirror of install.sh's table, held to it by tests/studio/install/test_rocm_arch_table_parity.py,
+# which looks both helpers up by install.sh's names: keep the _amd_* prefix, not this file's _setup_*.
+_amd_gfx_is_shadowing_integrated() {
+ case "$1" in
+ gfx90c|gfx1013|gfx1033|gfx1035|gfx1036|gfx1103|gfx1153) return 0 ;;
+ esac
+ return 1
+}
+
+# Pick the card to install for when enumeration put an integrated GPU first (#7776). gfx906 is
+# never a candidate: naming it on a mixed host strands BOTH cards. install.sh also requires a
+# torch wheel route; deliberately omitted, since this arch selects a llama.cpp bundle resolved
+# per gfx installer-side, not a repo.amd.com wheel family. Do not "restore" that filter.
+_amd_prefer_discrete_gfx() {
+ _apdg_devs="$1"
+ _apdg_sel="$2"
+ if [ -z "$_apdg_sel" ] || ! _amd_gfx_is_shadowing_integrated "$_apdg_sel"; then
+ printf '%s' "$_apdg_sel"
+ return 0
+ fi
+ # A mask the user SET is their own device choice: `+x`, not `-n`, so empty still counts.
+ if [ -n "${HIP_VISIBLE_DEVICES+x}" ] || [ -n "${ROCR_VISIBLE_DEVICES+x}" ] || \
+ [ -n "${CUDA_VISIBLE_DEVICES+x}" ]; then
+ printf '%s' "$_apdg_sel"
+ return 0
+ fi
+ # Here-doc, not a pipe: a piped `while` is a subshell, and an early break SIGPIPEs under pipefail.
+ _apdg_pick=""
+ while IFS= read -r _apdg_g; do
+ if [ -n "$_apdg_g" ] && [ "$_apdg_g" != gfx906 ] && \
+ ! _amd_gfx_is_shadowing_integrated "$_apdg_g"; then
+ _apdg_pick="$_apdg_g"
+ break
+ fi
+ done <<EOF
+$_apdg_devs
+EOF
+ # All-integrated host, or no list: keep the pick. Never returns empty for a nonempty selection.
+ [ -n "$_apdg_pick" ] || _apdg_pick="$_apdg_sel"
+ printf '%s' "$_apdg_pick"
+}
+
# NVIDIA priority: classify NVIDIA first and skip the AMD probes entirely on
# a usable-NVIDIA host (mirrors _has_rocm_gpu in install_python_stack.py).
# This also keeps a wedged rocminfo/amd-smi from hanging setup before the
@@ -3592,6 +3634,27 @@ elif [ "$_setup_amd_detected" = true ]; then
_setup_gfx=$(printf '%s\n' "$_setup_gfx_all" | awk -v idx="$_setup_vis_idx" \
'NF && !seen[$0]++ { a[n++]=$0 } END { if(idx>=n) idx=0; if(n>0) print a[idx+0] }')
fi
+ # The arch resolved above is what --rocm-gfx forwards below, so an iGPU enumerated first
+ # takes the ROCm bundle while torch targets the dGPU (#7776; #11143 saw gfx1036 ahead of a
+ # gfx1200). Runs before, and is skipped under, the UNSLOTH_ROCM_GFX_ARCH override, so a
+ # declared arch wins with no repick line printed above the line that overrules it.
+ # Candidates: the records, else $_setup_gfx_all, the only inventory left on the amd-smi path.
+ _setup_gfx_pref=""
+ if [ -z "${UNSLOTH_ROCM_GFX_ARCH:-}" ]; then
+ _setup_gfx_cands="$_setup_gfx_all"
+ if [ -n "$_setup_amd_records" ]; then
+ _setup_gfx_cands=$(printf '%s\n' "$_setup_amd_records" | awk -F'|' '$1 != "" { print $1 }')
+ fi
+ _setup_gfx_pref=$(_amd_prefer_discrete_gfx "$_setup_gfx_cands" "$_setup_gfx")
+ fi
+ if [ -n "$_setup_gfx_pref" ] && [ "$_setup_gfx_pref" != "$_setup_gfx" ]; then
+ substep "Integrated $_setup_gfx enumerated first; installing for discrete $_setup_gfx_pref"
+ substep "Set UNSLOTH_ROCM_GFX_ARCH=$_setup_gfx to target the integrated GPU instead."
+ _setup_gfx="$_setup_gfx_pref"
+ # Re-pair the banner name; empty beats pairing the new arch with the APU's name.
+ _setup_mkt=$(printf '%s\n' "$_setup_amd_records" | awk -F'|' -v gfx="$_setup_gfx" \
+ '$1 == gfx { print $2; exit }')
+ fi
# UNSLOTH_ROCM_GFX_ARCH env override (mirrors setup.ps1)
if [ -n "${UNSLOTH_ROCM_GFX_ARCH:-}" ]; then
_setup_gfx="${UNSLOTH_ROCM_GFX_ARCH}"
diff --git a/tests/studio/install/test_rocm_arch_table_parity.py b/tests/studio/install/test_rocm_arch_table_parity.py
index bb303cfbae0..2141c136cb9 100644
--- a/tests/studio/install/test_rocm_arch_table_parity.py
+++ b/tests/studio/install/test_rocm_arch_table_parity.py
@@ -767,5 +767,106 @@ class TestShadowingIntegratedGfxParity:
assert not (self._STRIX & self._install_sh_list())
+def _sh_call_site_offsets(source: str, helper: str) -> list[int]:
+ """Offsets where `helper` is invoked, excluding its definition and comments."""
+ offsets = []
+ for match in re.finditer(re.escape(helper), source):
+ line_start = source.rfind("\n", 0, match.start()) + 1
+ line_end = source.find("\n", match.start())
+ line = source[line_start : line_end if line_end != -1 else len(source)]
+ before = line[: match.start() - line_start]
+ if "#" in before:
+ continue
+ if re.match(rf"\s*{re.escape(helper)}\s*\(\)", line):
+ continue
+ offsets.append(match.start())
+ return offsets
+
+
+class TestShadowingPreferenceIsApplied:
+ """A table can be in parity while nothing calls the helper that reads it: that was
+ #11143, where studio/setup.sh (all `unsloth studio update` runs) had neither, so
+ install_llama_prebuilt.py had no repick to honour on Linux. These assert the
+ preference is REACHED on each entry point's path, not merely defined in the file.
+ """
+
+ _VISIBILITY_ENV = ("HIP_VISIBLE_DEVICES", "ROCR_VISIBLE_DEVICES", "CUDA_VISIBLE_DEVICES")
+
+ _SHELL_RESOLVERS = ("install.sh", "studio/setup.sh")
+
+ def _shadowing_arm(self, path: Path) -> set[str]:
+ body = _sh_function_body(
+ path.read_text(encoding = "utf-8"), "_amd_gfx_is_shadowing_integrated"
+ )
+ m = re.search(r"\n\s*(gfx[^)]*)\)\s*return 0", body)
+ assert m, f"the shadowing-APU arm was not found in {path.name}"
+ return {arch.strip() for arch in m.group(1).split("|")}
+
+ def test_setup_sh_defines_the_same_shadowing_table(self):
+ """studio/setup.sh was the copy that did not exist at all (#11143)."""
+ assert self._shadowing_arm(_SETUP_SH) == set(stack_mod._SHADOWING_INTEGRATED_GFX)
+
+ @pytest.mark.parametrize("rel", _SHELL_RESOLVERS)
+ def test_every_shell_gfx_resolver_calls_the_preference(self, rel):
+ source = (PACKAGE_ROOT / rel).read_text(encoding = "utf-8")
+ assert "_amd_prefer_discrete_gfx() {" in source, (
+ f"{rel} resolves an AMD gfx arch but does not define _amd_prefer_discrete_gfx; "
+ "a table without the preference leaves a hybrid host on its iGPU (#7776, #11143)"
+ )
+ assert _sh_call_site_offsets(source, "_amd_prefer_discrete_gfx"), (
+ f"{rel} defines _amd_prefer_discrete_gfx but never calls it, so the arch it "
+ "forwards is still whatever enumerated first"
+ )
+
+ # The argv-append sites, not a bare "--rocm-gfx", which matches an earlier comment
+ # and so cannot be satisfied by any correct placement.
+ _ROCM_GFX_FORWARDS = ("_PREBUILT_CMD+=(--rocm-gfx", "_WHISPER_CMD+=(--rocm-gfx")
+
+ def test_setup_sh_applies_the_preference_before_forwarding_rocm_gfx(self):
+ """Both consumers read $_setup_gfx, so the repick must precede both."""
+ source = _SETUP_SH.read_text(encoding = "utf-8")
+ calls = _sh_call_site_offsets(source, "_amd_prefer_discrete_gfx")
+ assert calls, "studio/setup.sh never calls _amd_prefer_discrete_gfx"
+ forwards = {}
+ for needle in self._ROCM_GFX_FORWARDS:
+ at = source.find(needle)
+ assert at != -1, f"studio/setup.sh no longer forwards via {needle}"
+ forwards[needle] = at
+ late = {n: at for n, at in forwards.items() if at < min(calls)}
+ assert not late, (
+ f"studio/setup.sh repicks the arch only after forwarding it via {sorted(late)}, "
+ "so that consumer still receives the iGPU's arch"
+ )
+
+ @pytest.mark.parametrize("rel", _SHELL_RESOLVERS)
+ def test_an_explicit_visibility_mask_is_honoured_by_each_shell_copy(self, rel):
+ """`HIP_VISIBLE_DEVICES=1` is the documented #7624 / #7669 workaround; a repick
+ that second-guesses a mask would break it."""
+ body = _sh_function_body(
+ (PACKAGE_ROOT / rel).read_text(encoding = "utf-8"), "_amd_prefer_discrete_gfx"
+ )
+ missing = [env for env in self._VISIBILITY_ENV if env not in body]
+ assert not missing, f"{rel}'s _amd_prefer_discrete_gfx ignores {missing}"
+
+ def test_pick_rocm_gfx_target_honours_an_explicit_visibility_mask(self):
+ source = _PREBUILT_PY.read_text(encoding = "utf-8")
+ tree = ast.parse(source)
+ for node in ast.walk(tree):
+ if isinstance(node, ast.FunctionDef) and node.name == "_pick_rocm_gfx_target":
+ body = ast.get_source_segment(source, node) or ""
+ missing = [env for env in self._VISIBILITY_ENV if env not in body]
+ assert not missing, f"_pick_rocm_gfx_target ignores {missing}"
+ return
+ raise AssertionError("_pick_rocm_gfx_target not found in install_llama_prebuilt.py")
+
+ def test_gfx906_is_never_a_repick_candidate(self):
+ """Naming gfx906 on a mixed host strands BOTH cards."""
+ for rel in self._SHELL_RESOLVERS:
+ body = _sh_function_body(
+ (PACKAGE_ROOT / rel).read_text(encoding = "utf-8"), "_amd_prefer_discrete_gfx"
+ )
+ assert "gfx906" in body, f"{rel}'s repick does not exclude gfx906"
+
+
if __name__ == "__main__":
pytest.main([__file__, "-v"])