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.

fix(AMD): route Fedora/Bazzite to per-arch ROCm wheels when no version is readable (#9152)

Leo Borcherding0833157cc527
diff --git a/install.sh b/install.sh
index 320ee76a18c..c7976892c4c 100755
--- a/install.sh
+++ b/install.sh
@@ -819,6 +819,7 @@ _cleanup_install_temporaries() {
[ -n "${_UIP_WORK:-}" ] && rm -rf "$_UIP_WORK" 2>/dev/null || true
[ -n "${_UIP_STAGE:-}" ] && rm -f "$_UIP_STAGE" 2>/dev/null || true
[ -n "${_UIP_STAGE2:-}" ] && rm -f "$_UIP_STAGE2" 2>/dev/null || true
+ [ -n "${_ROCM_TAG_MEMO_DIR:-}" ] && rm -rf "$_ROCM_TAG_MEMO_DIR" 2>/dev/null || true
}
_on_install_exit() {
@@ -848,6 +849,8 @@ _UNSLOTH_TORCH_OVERRIDES=""
_UIP_WORK=""
_UIP_STAGE=""
_UIP_STAGE2=""
+_ROCM_TAG_MEMO_DIR=""
+_ROCM_TAG_MEMO=""
trap _on_install_exit EXIT
trap '_on_install_signal 129' HUP
trap '_on_install_signal 130' INT
@@ -3310,6 +3313,34 @@ _amd_gpu_present_via_pci() {
return 1
}
+# rocminfo names each agent twice, so "gfx1201\ngfx1201" is one device, not two.
+_amd_probe_arches() {
+ printf '%s\n' "$1" | sed 's/:.*$//' | tr '[:upper:]' '[:lower:]' | awk 'NF' | sort -u
+}
+
+# The wheels must work on every AMD GPU in the box, so every agent has to land in the same
+# family: routing on whichever the kernel enumerated first puts gfx1151 wheels on a 9070 XT.
+_amd_agreed_index_family() {
+ _aif_family=""
+ for _aif_a in $(_amd_probe_arches "$1"); do
+ _aif_f=$(_amd_arch_index_family_for_gfx "$_aif_a") || return 1
+ [ -z "$_aif_family" ] || [ "$_aif_f" = "$_aif_family" ] || return 1
+ _aif_family="$_aif_f"
+ done
+ [ -n "$_aif_family" ] || return 1
+ printf '%s\n' "$_aif_family"
+}
+
+# setup.sh takes UNSLOTH_ROCM_GFX_ARCH over its own visibility-aware selection, so naming an
+# arbitrary member of an agreed family (gfx1200 beside gfx1201) overrules a correct choice.
+_amd_sole_index_arch() {
+ _sia=$(_amd_probe_arches "$1")
+ [ -n "$_sia" ] || return 1
+ [ "$(printf '%s\n' "$_sia" | awk 'END{print NR}')" -eq 1 ] || return 1
+ _amd_arch_index_family_for_gfx "$_sia" >/dev/null 2>&1 || return 1
+ printf '%s\n' "$_sia"
+}
+
# Map a gfx arch to the AMD pip index family (mirrors install.ps1 $archFamilyMap).
_amd_arch_index_family_for_gfx() {
case "$1" in
@@ -3670,9 +3701,35 @@ _rocm_tag_from_version_file() {
awk -F. '{print "rocm"$1"."$2; exit}' /opt/rocm/.info/version || return 0
}
+# Naming pacman unconditionally told the unslothai#8731 reporter to run it on an
+# immutable Fedora image, where it does not exist.
+_rocm_sdk_install_hint() {
+ if command -v pacman >/dev/null 2>&1; then
+ echo "sudo pacman -S rocm-hip-sdk"
+ elif command -v dnf >/dev/null 2>&1; then
+ echo "sudo dnf install rocm-hip rocm-runtime (rpm-ostree install ... on atomic images)"
+ elif command -v zypper >/dev/null 2>&1; then
+ echo "sudo zypper install rocm-hip"
+ elif command -v apt-get >/dev/null 2>&1; then
+ echo "see https://rocm.docs.amd.com/en/latest/deploy/linux/index.html for the AMD apt repo"
+ else
+ echo "https://rocm.docs.amd.com/en/latest/deploy/linux/index.html"
+ fi
+}
+
_rocm_tag_from_hipconfig() {
- command -v hipconfig >/dev/null 2>&1 || return 0
- _run_bounded hipconfig --version 2>/dev/null \
+ # AMD's own installer puts hipconfig in ROCM_PATH/bin and leaves it off PATH unless
+ # its profile.d snippet ran, so a PATH-only lookup misses a tree that is right there.
+ # Not the Fedora shape: Fedora's hipconfig is /usr/bin/hipconfig, owned by hipcc.
+ _rt_hipconfig=""
+ if command -v hipconfig >/dev/null 2>&1; then
+ _rt_hipconfig=hipconfig
+ elif [ -x "${ROCM_PATH:-/opt/rocm}/bin/hipconfig" ]; then
+ _rt_hipconfig="${ROCM_PATH:-/opt/rocm}/bin/hipconfig"
+ else
+ return 0
+ fi
+ _run_bounded "$_rt_hipconfig" --version 2>/dev/null \
| awk 'NR==1 && /^[0-9]/{split($1,a,"."); if(a[1]+0>0){print "rocm"a[1]"."a[2]}}' || return 0
}
@@ -3705,13 +3762,28 @@ _rocm_tag_from_dpkg() {
_rocm_tag_from_rpm() {
command -v rpm >/dev/null 2>&1 || return 0
- # `rpm -q` is not a lock-free read: a leftover /var/lib/rpm/__db.00* from a killed
- # rpm/yum wedges plain queries in futex on the BerkeleyDB backend (rpm < 4.16, i.e.
- # RHEL 8 / SLES 15; rhbz#485780, rhbz#73097), and rpm 6.0.x deadlocks `rpm --query`
- # against a running dnf (rhbz#2463435). _run_bounded no-ops where `timeout` is absent.
- _rt_ver=$(_run_bounded rpm -q --qf '%{VERSION}\n' rocm-core 2>/dev/null) || return 0
+ # Bounded, alone among the five, because this is the one source highest-wins
+ # newly made unconditional that can block forever: it used to be LAST in a
+ # first-answer-wins `||` chain, so /opt/rocm/.info/version answered at position
+ # two and rpm was never invoked on a normal RHEL/SLES install. `rpm -q` is not a
+ # lock-free read -- a leftover /var/lib/rpm/__db.00* from a killed rpm/yum wedges
+ # plain queries in futex on the BerkeleyDB backend (rpm < 4.16, i.e. RHEL 8 /
+ # SLES 15; rhbz#485780, rhbz#73097), and rpm 6.0.x deadlocks `rpm --query`
+ # against a running dnf (rhbz#2463435). A version probe must not hang the
+ # installer, and a timed-out probe is just a source that declined to answer.
+ # _run_bounded no-ops where `timeout` is absent, so this adds no dependency.
+ # Fedora ships rocm-core but nothing except the `rocm` metapackage requires it, so a
+ # host running rocm-hip/rocm-runtime answered nothing (unslothai#8731). All names in
+ # ONE query, since looping would pay the timeout above once per name; rpm reports
+ # misses on stdout, so keep only lines starting with a digit. Every installed
+ # component is emitted, not just the first: these are all AMD packages from the same
+ # repo, so a partial upgrade can leave rocm-core 5.7 beside rocm-runtime 6.4, and
+ # ranking by argument order would read that host as 5.7 and send a supported runtime
+ # to CPU wheels. _highest_rocm_tag ranks them, as it does across the other sources.
+ _rt_ver=$(_run_bounded rpm -q --qf '%{VERSION}\n' rocm-core rocm-runtime rocm-hip 2>/dev/null \
+ | awk '/^[0-9]/{print}') || return 0
[ -n "$_rt_ver" ] || return 0
- printf '%s\n' "$_rt_ver" | awk -F'[.-]' '{print "rocm"$1"."$2; exit}' || return 0
+ printf '%s\n' "$_rt_ver" | awk -F'[.-]' 'NF{print "rocm"$1"."$2}' || return 0
}
# Highest "rocmX.Y" line on stdin (major >= 1), or nothing when no line is usable.
@@ -3739,7 +3811,14 @@ _highest_rocm_tag() {
# releases, the normalisation below can only emit a leaf PyTorch publishes, package-manager
# sources that can report an uninstalled tree are filtered above, and any disagreement is
# named on stderr for the install log.
+
+# Path of the cross-subshell answer cache; set by the parent shell, empty when unused.
+_ROCM_TAG_MEMO=""
_detect_rocm_version_tag() {
+ if [ -n "${_ROCM_TAG_MEMO:-}" ] && [ -f "$_ROCM_TAG_MEMO" ]; then
+ cat "$_ROCM_TAG_MEMO"
+ return
+ fi
_rt_readings=$({
_rocm_tag_from_amd_smi
_rocm_tag_from_version_file
@@ -3758,6 +3837,9 @@ _detect_rocm_version_tag() {
*) echo "[WARN] ROCm version sources disagree (${_rt_seen% }) -- using the highest, $_rt_best." >&2 ;;
esac
fi
+ if [ -n "${_ROCM_TAG_MEMO:-}" ]; then
+ printf '%s\n' "$_rt_best" > "$_ROCM_TAG_MEMO" 2>/dev/null || true
+ fi
printf '%s\n' "$_rt_best"
}
@@ -3903,21 +3985,30 @@ get_torch_index_url() {
# fresh-install case: the GPU is real, but with no ROCm userspace the
# correct PyTorch build can't be selected. Warn with an actionable fix
# rather than silently installing CPU PyTorch.
- # A user-set UNSLOTH_ROCM_GFX_ARCH seeded the probe above, so rocminfo/
- # amd-smi may still be unable to see the GPU; when the named arch maps to
- # a wheel family, the runtime-less reroute (gated on the override) will
- # install the AMD per-arch wheels -- a CPU-only warning here would be
- # false for that path. Defer like the inferable-arch branch does.
- if [ -n "${UNSLOTH_ROCM_GFX_ARCH:-}" ] && \
- _amd_arch_index_family_for_gfx "$_amd_gfx_probe" >/dev/null 2>&1; then
- echo "[WARN] AMD GPU detected with no readable ROCm version, but UNSLOTH_ROCM_GFX_ARCH=$_amd_gfx_probe is set -- routing to AMD per-arch wheels." >&2
+ # The version only picks between the generic rocmX.Y leaves, so an arch with its
+ # own repo.amd.com/rocm/whl/gfx* index does not need one (unslothai#8731).
+ _amd_gfx_family=$(_amd_agreed_index_family "$_amd_gfx_probe") || _amd_gfx_family=""
+ if [ -n "$_amd_gfx_family" ]; then
+ _amd_gfx_first=$(_amd_sole_index_arch "$_amd_gfx_probe") || _amd_gfx_first=""
+ if [ -n "${UNSLOTH_ROCM_GFX_ARCH:-}" ]; then
+ echo "[WARN] AMD GPU detected with no readable ROCm version, but UNSLOTH_ROCM_GFX_ARCH=${_amd_gfx_first:-$_amd_gfx_family} is set -- routing to AMD per-arch wheels." >&2
+ else
+ echo "[WARN] AMD ${_amd_gfx_first:-$_amd_gfx_family} detected but no ROCm version could be read -- routing to AMD per-arch wheels, which do not need one." >&2
+ fi
echo "$_base/cpu"; return
fi
- echo "[WARN] AMD GPU detected, but no ROCm/HIP install was found to select the matching GPU PyTorch build -- falling back to CPU-only PyTorch." >&2
- echo "[WARN] Install the ROCm/HIP SDK, then re-run this installer:" >&2
- echo "[WARN] Arch / CachyOS : sudo pacman -S rocm-hip-sdk" >&2
- echo "[WARN] other distros : https://rocm.docs.amd.com/en/latest/deploy/linux/index.html" >&2
- echo "[WARN] Minimum required for version detection: amd-smi, hipconfig, a ROCm version file, or an installed runtime package (Debian: libhsa-runtime64-1)." >&2
+ echo "[WARN] AMD GPU detected, but no ROCm version could be read to select the matching GPU PyTorch build -- falling back to CPU-only PyTorch." >&2
+ if [ -d "${ROCM_PATH:-/opt/rocm}" ]; then
+ # Telling someone with a populated ROCm tree that "no ROCm install was found"
+ # sends them off to install a package they already have. Fedora is the OTHER
+ # branch: it owns no path under /opt/rocm, so it lands on the SDK hint.
+ echo "[WARN] ${ROCM_PATH:-/opt/rocm} exists, so ROCm is likely installed but not reporting a version this installer can read." >&2
+ echo "[WARN] Pin the wheels and re-run: UNSLOTH_TORCH_INDEX_FAMILY=rocm6.4 (a PyTorch wheel leaf: rocm6.0-6.4, rocm7.0-7.2)" >&2
+ else
+ echo "[WARN] Install the ROCm/HIP SDK, then re-run this installer:" >&2
+ echo "[WARN] $(_rocm_sdk_install_hint)" >&2
+ fi
+ echo "[WARN] Version sources checked: amd-smi, /opt/rocm/.info/version, hipconfig, dpkg, rpm (Debian runtime package: libhsa-runtime64-1)." >&2
echo "$_base/cpu"; return
fi
# Parse CUDA version from nvidia-smi output (POSIX-safe, no grep -P).
@@ -4494,6 +4585,12 @@ if [ -n "$_ti_url_trim" ] || [ -n "$_ti_family_trim" ]; then
fi
[ "$_torch_index_pinned" = true ] || _maybe_bootstrap_rocm_wsl || true
+# Created here, not inside get_torch_index_url: that runs in a command substitution, so only
+# a file outlives it. mktemp -d, never a $$-derived name -- a predictable path under a
+# world-writable /tmp can be pre-created as a symlink, feeding the probe a chosen version.
+_ROCM_TAG_MEMO_DIR=$(mktemp -d "${TMPDIR:-/tmp}/unsloth-rocm.XXXXXX" 2>/dev/null) \
+ && _ROCM_TAG_MEMO="$_ROCM_TAG_MEMO_DIR/tag" || _ROCM_TAG_MEMO=""
+
TORCH_INDEX_URL=$(get_torch_index_url)
# Linux: ROCm runtime missing but a supported AMD gfx arch is inferable (Strix Halo
@@ -4504,15 +4601,55 @@ TORCH_INDEX_URL=$(get_torch_index_url)
# env-independent KFD topology while rocminfo/amd-smi can't read its arch
# (KFD-only host, unslothai#7314 -- before the KFD detection fix these hosts
# reached this reroute via the false branch, so the empty-probe condition
-# preserves that routing). A */cpu index chosen WITH a readable gfx
-# (unsupported/unreadable ROCm version, after its own warning) is a deliberate
-# fallback -- rerouting it would contradict that decision, and stays excluded
-# because the shared probe returns its gfx. An explicit UNSLOTH_ROCM_GFX_ARCH
-# override stays authoritative either way.
+# preserves that routing). A */cpu index chosen WITH a readable gfx and a readable but
+# UNSUPPORTED ROCm version is a deliberate fallback, and stays excluded because the shared
+# probe returns its gfx. An UNREADABLE version is only a detection miss, so it gets its own
+# way in below (unslothai#8731). UNSLOTH_ROCM_GFX_ARCH stays authoritative either way.
+
+_amd_no_rocm_version_reroute=false
+_amd_probed_gfx_first=""
+case "$TORCH_INDEX_URL" in
+ */cpu)
+ if [ "$_torch_index_pinned" = false ] && [ "$SKIP_TORCH" = false ] && \
+ [ -z "${UNSLOTH_ROCM_GFX_ARCH:-}" ] && \
+ ! _has_usable_nvidia_gpu && _has_amd_rocm_gpu; then
+ _amd_probe_out=$(_probe_amd_gfx_arch)
+ # HSA_OVERRIDE_GFX_VERSION=11.0.0 is the standard Strix Halo workaround, and
+ # ROCr then reports the spoofed gfx1100. The llama.cpp path corrects that
+ # further down, which is too late for this branch: both the wheel family and
+ # the exported arch are derived from this probe, so an uncorrected gfx1151
+ # would take gfx110X-all wheels and export gfx1100 to setup.sh.
+ _amd_spoof_inferred=$(_infer_linux_amd_gfx_arch 2>/dev/null || true)
+ _amd_spoof_physical=$(_hsa_spoofed_physical_gfx "$_amd_spoof_inferred" "$_amd_probe_out")
+ if [ -n "${_amd_spoof_physical:-}" ]; then
+ _amd_probe_out="$_amd_spoof_physical"
+ fi
+ # An empty correction has two meanings and only one of them is "no spoof": the
+ # helper also declines whenever the KFD reports more than one GPU node, because
+ # the override can collapse several physical targets into one reported token.
+ # Counting NODES rather than distinct arches deliberately matches the helper's
+ # own rule -- it declines on two nodes even where they agree -- so a singleton
+ # probe it refused to vouch for never picks a family on its own.
+ if [ -z "${_amd_spoof_physical:-}" ] && [ -n "${HSA_OVERRIDE_GFX_VERSION:-}" ] && \
+ [ "$(_kfd_gfx_targets | awk 'NF { n++ } END { print n + 0 }')" -gt 1 ]; then
+ _amd_probe_out=""
+ fi
+ _amd_probed_family=$(_amd_agreed_index_family "$_amd_probe_out") \
+ || _amd_probed_family=""
+ _amd_probed_gfx_first=$(_amd_sole_index_arch "$_amd_probe_out") \
+ || _amd_probed_gfx_first=""
+ if [ -n "${_amd_probed_family:-}" ] && \
+ [ -z "$(_detect_rocm_version_tag 2>/dev/null)" ]; then
+ _amd_no_rocm_version_reroute=true
+ fi
+ fi
+ ;;
+esac
if [ "$_torch_index_pinned" = false ] && [ "$SKIP_TORCH" = false ] && \
! _has_usable_nvidia_gpu && \
{ [ -n "${UNSLOTH_ROCM_GFX_ARCH:-}" ] || ! _has_amd_rocm_gpu || \
- [ -z "$(_probe_amd_gfx_arch)" ]; } && \
+ [ -z "$(_probe_amd_gfx_arch)" ] || \
+ [ "${_amd_no_rocm_version_reroute:-false}" = true ]; } && \
case "$(uname -s)" in Linux) true ;; *) false ;; esac && \
case "$_ARCH" in x86_64|amd64) true ;; *) false ;; esac; then
# ROCm torch wheels are x86_64-only; get_torch_index_url returns CPU on other
@@ -4520,24 +4657,56 @@ if [ "$_torch_index_pinned" = false ] && [ "$SKIP_TORCH" = false ] && \
case "$TORCH_INDEX_URL" in
*/cpu)
_linux_inferred_gfx=$(_infer_linux_amd_gfx_arch 2>/dev/null || true)
- if [ -n "$_linux_inferred_gfx" ]; then
+ # Inference hands an explicit override back verbatim, and HIP's gcnArchName
+ # carries feature flags (gfx1201:sramecc+:xnack-) the index table has no arm
+ # for, so the suffix silently cost the reroute. Normalise as the caller did.
+ _linux_inferred_gfx=$(_amd_sole_index_arch "$_linux_inferred_gfx") \
+ || _linux_inferred_gfx=""
+ # Route on the arch the probe READ, not on lspci marketing-name inference:
+ # the two disagree on a mixed APU + discrete host, and inference's answer
+ # would install wheels for a GPU the reroute decision never looked at.
+ if [ "${_amd_no_rocm_version_reroute:-false}" = true ]; then
+ _linux_inferred_gfx="${_amd_probed_gfx_first:-}"
+ fi
+ _amd_family=""
+ if [ "${_amd_no_rocm_version_reroute:-false}" = true ]; then
+ _amd_family="${_amd_probed_family:-}"
+ elif [ -n "$_linux_inferred_gfx" ]; then
_amd_family=$(_amd_arch_index_family_for_gfx "$_linux_inferred_gfx") || _amd_family=""
- if [ -n "$_amd_family" ]; then
+ fi
+ if [ -n "$_amd_family" ]; then
_amd_mirror="${UNSLOTH_AMD_ROCM_MIRROR:-https://repo.amd.com/rocm/whl}"
while [ "${_amd_mirror%/}" != "$_amd_mirror" ]; do
_amd_mirror="${_amd_mirror%/}"
done
TORCH_INDEX_URL="${_amd_mirror}/${_amd_family}/"
- # Hand the inferred arch to setup.sh (llama.cpp): it re-probes
- # ROCm on its own, and on these runtime-less hosts its probes
- # find nothing, so without this it classifies the box as
- # non-ROCm and installs the CPU prebuilt while torch just got
- # AMD per-arch wheels. setup.sh and install_llama_prebuilt.py
- # both honor UNSLOTH_ROCM_GFX_ARCH, so exporting it is the
- # whole handoff (a user-set override re-exports unchanged).
- export UNSLOTH_ROCM_GFX_ARCH="$_linux_inferred_gfx"
- case "$_linux_inferred_gfx" in
- gfx1201|gfx1200|gfx1151|gfx1150|gfx1152)
+ # Hand the arch to setup.sh (llama.cpp): it re-probes ROCm on its
+ # own, and on these runtime-less hosts its probes find nothing, so
+ # without this it classifies the box as non-ROCm and installs the
+ # CPU prebuilt while torch just got AMD per-arch wheels. setup.sh
+ # and install_llama_prebuilt.py both honor UNSLOTH_ROCM_GFX_ARCH,
+ # so exporting it is the whole handoff (a user-set override
+ # re-exports unchanged).
+ if [ -n "$_linux_inferred_gfx" ]; then
+ export UNSLOTH_ROCM_GFX_ARCH="$_linux_inferred_gfx"
+ fi
+ # A corroborated spoof has to be cleared here as well. The rocm* leaf
+ # below does it, but this reroute produces a gfx* leaf, which that case
+ # never matches: the host would install native $_linux_inferred_gfx
+ # wheels while ROCr kept reporting the spoofed arch, leaving the kernels
+ # in those wheels unusable. SKIP_TORCH is false on this branch by its
+ # own guard, so the wheels really are going in.
+ if [ "${_amd_no_rocm_version_reroute:-false}" = true ] && \
+ [ -n "${_amd_spoof_physical:-}" ]; then
+ unset HSA_OVERRIDE_GFX_VERSION
+ echo " [WARN] Clearing HSA_OVERRIDE_GFX_VERSION for the rest of this install:" >&2
+ echo " [WARN] these wheels carry $_linux_inferred_gfx kernels, so the runtime has" >&2
+ echo " [WARN] to report the real arch. Remove the export from your shell profile" >&2
+ echo " [WARN] (~/.bashrc, ~/.profile) as well, or the next terminal restores it." >&2
+ fi
+ # Off the family, not the arch: no family straddles this boundary.
+ case "$_amd_family" in
+ gfx120X-all|gfx1151|gfx1150|gfx1152)
TORCH_CONSTRAINT="torch>=2.11.0,<2.12.0"
TORCHVISION_CONSTRAINT="torchvision>=0.26.0,<0.27.0"
TORCHAUDIO_CONSTRAINT="torchaudio>=2.11.0,<2.12.0"
@@ -4546,7 +4715,10 @@ if [ "$_torch_index_pinned" = false ] && [ "$SKIP_TORCH" = false ] && \
echo "" >&2
# KFD-only hosts reach this reroute with /dev/kfd present
# (that's what detected them), so don't claim it's missing.
- if _has_amd_rocm_gpu; then
+ if [ "${_amd_no_rocm_version_reroute:-false}" = true ]; then
+ echo " [WARN] AMD ${_linux_inferred_gfx:-$_amd_family} detected, but no ROCm version could be read (checked amd-smi, /opt/rocm/.info/version, hipconfig, dpkg, rpm)." >&2
+ echo " [WARN] The per-arch index is keyed on the arch alone, so the version is not needed." >&2
+ elif _has_amd_rocm_gpu; then
echo " [WARN] AMD GPU visible via the kernel driver (KFD) but rocminfo/amd-smi can't read its gfx arch; using $_linux_inferred_gfx." >&2
else
echo " [WARN] ROCm runtime not visible (/dev/kfd, rocminfo, amd-smi) but $_linux_inferred_gfx inferred." >&2
@@ -4554,9 +4726,12 @@ if [ "$_torch_index_pinned" = false ] && [ "$SKIP_TORCH" = false ] && \
echo " [WARN] Routing to AMD arch-specific wheels ($(_strip_index_url_credentials "$TORCH_INDEX_URL"))." >&2
echo " [WARN] These wheels bundle their own ROCm runtime; install the kernel stack for native compute:" >&2
echo " [WARN] https://docs.unsloth.ai/get-started/install-and-update/amd" >&2
- echo " [WARN] Tip: set UNSLOTH_ROCM_GFX_ARCH=$_linux_inferred_gfx to skip inference next time." >&2
+ if [ -n "$_linux_inferred_gfx" ]; then
+ echo " [WARN] Tip: set UNSLOTH_ROCM_GFX_ARCH=$_linux_inferred_gfx to skip inference next time." >&2
+ else
+ echo " [WARN] Two AMD GPUs of different archs share this wheel family; set UNSLOTH_ROCM_GFX_ARCH to name the one llama.cpp should build for." >&2
+ fi
echo "" >&2
- fi
fi
;;
esac
@@ -4672,8 +4847,13 @@ fi
# Radeon/Strix repos by GPU probing.
_amd_gpu_radeon=false
if [ "$_torch_index_pinned" = false ]; then
-case "$TORCH_INDEX_URL" in
- */rocm*)
+# On the LEAF, like every other index classifier here: the AMD per-arch mirror is
+# https://repo.amd.com/ROCM/whl/gfx120X-all/, so a whole-URL */rocm* glob brands every
+# per-arch reroute as Radeon and the summary then reports repo.radeon.com wheels that
+# were never fetched. The two older per-arch reroutes each clear the flag by hand
+# afterwards; matching the leaf is what stops the next one from having to.
+case "$_torch_index_leaf" in
+ rocm*)
if _has_amd_rocm_gpu && command -v rocminfo >/dev/null 2>&1 && \
rocminfo 2>/dev/null | grep -q 'Marketing Name:.*Radeon'; then
_amd_gpu_radeon=true
diff --git a/tests/sh/test_get_torch_index_url.sh b/tests/sh/test_get_torch_index_url.sh
index 0076bc6e5cf..af3fb91b68c 100755
--- a/tests/sh/test_get_torch_index_url.sh
+++ b/tests/sh/test_get_torch_index_url.sh
@@ -46,6 +46,12 @@ _FAKE_ROCM_DIR=$(mktemp -d)
sed -n '/^_infer_linux_amd_gfx_arch()/,/^}/p' "$INSTALL_SH"
echo ""
sed -n '/^_amd_arch_index_family_for_gfx()/,/^}/p' "$INSTALL_SH"
+ echo
+ sed -n '/^_amd_probe_arches()/,/^}/p' "$INSTALL_SH"
+ echo
+ sed -n '/^_amd_agreed_index_family()/,/^}/p' "$INSTALL_SH"
+ echo
+ sed -n '/^_amd_sole_index_arch()/,/^}/p' "$INSTALL_SH"
echo ""
sed -n '/^_trim_index_path_slashes()/,/^}/p' "$INSTALL_SH"
echo ""
diff --git a/tests/sh/test_rocm_no_version_arch_route.sh b/tests/sh/test_rocm_no_version_arch_route.sh
new file mode 100644
index 00000000000..e7fe41f3fa3
--- /dev/null
+++ b/tests/sh/test_rocm_no_version_arch_route.sh
@@ -0,0 +1,352 @@
+#!/bin/bash
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+# Regression tests for readable-arch / unreadable-version AMD hosts (issue #8731).
+set -e
+
+SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
+INSTALL_SH="$SCRIPT_DIR/../../install.sh"
+PASS=0
+FAIL=0
+
+_FUNC_FILE=$(mktemp)
+_FAKE_SMI_DIR=$(mktemp -d)
+_FAKE_ROCM_DIR=$(mktemp -d)
+_FAKE_PROC_NV_DIR=$(mktemp -d)
+{
+ sed -n '/^_run_bounded()/,/^}/p' "$INSTALL_SH"
+ echo ""
+ sed -n '/^_cvd_hides_nvidia()/,/^}/p' "$INSTALL_SH"
+ echo ""
+ sed -n '/^_has_amd_rocm_gpu()/,/^}/p' "$INSTALL_SH"
+ echo ""
+ sed -n '/^_has_usable_nvidia_gpu()/,/^}/p' "$INSTALL_SH"
+ echo ""
+ sed -n '/^_ensure_rocm_probe_env()/,/^}/p' "$INSTALL_SH"
+ echo ""
+ sed -n '/^_probe_amd_gfx_arch()/,/^}/p' "$INSTALL_SH"
+ echo ""
+ sed -n '/^_amd_gpu_present_via_pci()/,/^}/p' "$INSTALL_SH"
+ echo ""
+ sed -n '/^_infer_amd_gfx_arch_from_gpu_name()/,/^}/p' "$INSTALL_SH"
+ echo ""
+ sed -n '/^_infer_linux_amd_gfx_arch()/,/^}/p' "$INSTALL_SH"
+ echo ""
+ sed -n '/^_infer_unsupported_amd_gfx_arch_from_gpu_name()/,/^}/p' "$INSTALL_SH"
+ echo ""
+ sed -n '/^_infer_linux_unsupported_amd_gfx_arch()/,/^}/p' "$INSTALL_SH"
+ echo ""
+ sed -n '/^_amd_arch_index_family_for_gfx()/,/^}/p' "$INSTALL_SH"
+ echo
+ sed -n '/^_amd_probe_arches()/,/^}/p' "$INSTALL_SH"
+ echo
+ sed -n '/^_amd_agreed_index_family()/,/^}/p' "$INSTALL_SH"
+ echo
+ sed -n '/^_amd_sole_index_arch()/,/^}/p' "$INSTALL_SH"
+ echo ""
+ sed -n '/^_trim_index_path_slashes()/,/^}/p' "$INSTALL_SH"
+ echo ""
+ sed -n '/^_nvidia_cu126_verdict()/,/^}/p' "$INSTALL_SH"
+ echo ""
+ sed -n '/^_cap_cuda_family_for_pre_turing()/,/^}/p' "$INSTALL_SH"
+ echo ""
+ sed -n '/^_rocm_sdk_install_hint()/,/^}/p' "$INSTALL_SH"
+ echo ""
+ sed -n '/^_rocm_tag_from_amd_smi()/,/^}/p' "$INSTALL_SH"
+ echo ""
+ sed -n '/^_rocm_tag_from_version_file()/,/^}/p' "$INSTALL_SH"
+ echo ""
+ sed -n '/^_rocm_tag_from_hipconfig()/,/^}/p' "$INSTALL_SH"
+ echo ""
+ sed -n '/^_rocm_tag_from_dpkg()/,/^}/p' "$INSTALL_SH"
+ echo ""
+ sed -n '/^_rocm_tag_from_rpm()/,/^}/p' "$INSTALL_SH"
+ echo ""
+ sed -n '/^_highest_rocm_tag()/,/^}/p' "$INSTALL_SH"
+ echo ""
+ sed -n '/^_detect_rocm_version_tag()/,/^}/p' "$INSTALL_SH"
+ echo ""
+ sed -n '/^get_torch_index_url()/,/^}/p' "$INSTALL_SH"
+} | sed -e "s|/usr/bin/nvidia-smi|$_FAKE_SMI_DIR/nvidia-smi-absent|g" \
+ -e "s|/proc/driver/nvidia|$_FAKE_PROC_NV_DIR|g" \
+ -e "s|/opt/rocm|$_FAKE_ROCM_DIR|g" \
+ > "$_FUNC_FILE"
+
+for _fn in _rocm_sdk_install_hint _rocm_tag_from_hipconfig _rocm_tag_from_rpm \
+ _detect_rocm_version_tag _amd_arch_index_family_for_gfx get_torch_index_url; do
+ if ! grep -q "^$_fn()" "$_FUNC_FILE"; then
+ echo " FAIL: install.sh no longer defines $_fn() at column 0"
+ exit 1
+ fi
+done
+
+_TOOLS_DIR=$(mktemp -d)
+for _cmd in uname grep sed head sh bash cat awk printf tr ls sort timeout sleep; do
+ _real=$(command -v "$_cmd" 2>/dev/null || true)
+ [ -n "$_real" ] && ln -sf "$_real" "$_TOOLS_DIR/$_cmd"
+done
+
+cleanup() {
+ rm -rf "$_FUNC_FILE" "$_FAKE_SMI_DIR" "$_FAKE_ROCM_DIR" "$_FAKE_PROC_NV_DIR" "$_TOOLS_DIR" "$_MOCK_DIR"
+}
+trap cleanup EXIT
+
+assert_eq() {
+ _label="$1"; _expected="$2"; _actual="$3"
+ if [ "$_actual" = "$_expected" ]; then
+ echo " PASS: $_label"
+ PASS=$((PASS + 1))
+ else
+ echo " FAIL: $_label (expected '$_expected', got '$_actual')"
+ FAIL=$((FAIL + 1))
+ fi
+}
+
+assert_contains() {
+ _label="$1"; _needle="$2"; _haystack="$3"
+ case "$_haystack" in
+ *"$_needle"*) echo " PASS: $_label"; PASS=$((PASS + 1)) ;;
+ *) echo " FAIL: $_label (no '$_needle' in: $_haystack)"; FAIL=$((FAIL + 1)) ;;
+ esac
+}
+
+assert_lacks() {
+ _label="$1"; _needle="$2"; _haystack="$3"
+ case "$_haystack" in
+ *"$_needle"*) echo " FAIL: $_label (unexpected '$_needle' in: $_haystack)"; FAIL=$((FAIL + 1)) ;;
+ *) echo " PASS: $_label"; PASS=$((PASS + 1)) ;;
+ esac
+}
+
+_MOCK_DIR=$(mktemp -d)
+
+# $1 = gfx arch. Emitting NO ROCm version field is the fixture, not an oversight.
+reset_host() {
+ rm -rf "$_MOCK_DIR" "$_FAKE_ROCM_DIR/.info" "$_FAKE_ROCM_DIR/bin"
+ _MOCK_DIR=$(mktemp -d)
+ cat > "$_MOCK_DIR/amd-smi" <<MOCK
+#!/bin/sh
+case "\$1" in
+ list) printf 'GPU: 0\\n BDF: 0000:03:00.0\\n NAME: $1\\n' ;;
+ static) printf 'ASIC:\\n TARGET_GRAPHICS_VERSION: $1\\n' ;;
+ *) echo "AMDSMI Tool: 25.0.1 | AMDSMI Library version: 25.0.1.0" ;;
+esac
+MOCK
+ chmod +x "$_MOCK_DIR/amd-smi"
+}
+
+add_rocm_bin_hipconfig() {
+ mkdir -p "$_FAKE_ROCM_DIR/bin"
+ cat > "$_FAKE_ROCM_DIR/bin/hipconfig" <<MOCK
+#!/bin/sh
+echo "$1"
+MOCK
+ chmod +x "$_FAKE_ROCM_DIR/bin/hipconfig"
+}
+
+# install.sh queries all three package names in ONE rpm call, so the mock answers per argument: one that answered only the last would hide which name matched.
+add_rpm_package() {
+ printf '%s\n' "$1" > "$_MOCK_DIR/.rpm-pkg"
+ printf '%s\n' "$2" > "$_MOCK_DIR/.rpm-ver"
+ cat > "$_MOCK_DIR/rpm" <<'MOCK'
+#!/bin/sh
+_d=${0%/*}
+_known=$(cat "$_d/.rpm-pkg")
+_ver=$(cat "$_d/.rpm-ver")
+_hit=0
+_skip=0
+for _arg in "$@"; do
+ if [ "$_skip" = 1 ]; then _skip=0; continue; fi
+ case "$_arg" in
+ --qf|--queryformat) _skip=1 ;;
+ -*) : ;;
+ *)
+ if [ "$_arg" = "$_known" ]; then
+ printf '%s\n' "$_ver"; _hit=1
+ else
+ printf 'package %s is not installed\n' "$_arg"
+ fi
+ ;;
+ esac
+done
+[ "$_hit" = 1 ] || exit 1
+MOCK
+ chmod +x "$_MOCK_DIR/rpm"
+}
+
+reset_host_mixed() {
+ rm -rf "$_MOCK_DIR" "$_FAKE_ROCM_DIR/.info" "$_FAKE_ROCM_DIR/bin"
+ _MOCK_DIR=$(mktemp -d)
+ cat > "$_MOCK_DIR/amd-smi" <<MOCK
+#!/bin/sh
+case "\$1" in
+ list) printf 'GPU: 0\n BDF: 0000:03:00.0\n NAME: $1\nGPU: 1\n BDF: 0000:64:00.0\n NAME: $2\n' ;;
+ static) printf 'ASIC:\n TARGET_GRAPHICS_VERSION: $1\nASIC:\n TARGET_GRAPHICS_VERSION: $2\n' ;;
+ *) echo "AMDSMI Tool: 25.0.1 | AMDSMI Library version: 25.0.1.0" ;;
+esac
+MOCK
+ chmod +x "$_MOCK_DIR/amd-smi"
+}
+
+run_sole_arch() {
+ PATH="$_MOCK_DIR:$_TOOLS_DIR" bash -c ". '$_FUNC_FILE'; _amd_sole_index_arch \"\$1\" || echo REJECTED" _ "$1" 2>/dev/null
+}
+
+run_family() {
+ PATH="$_MOCK_DIR:$_TOOLS_DIR" bash -c ". '$_FUNC_FILE'; _amd_agreed_index_family \"\$1\" || echo REJECTED" _ "$1" 2>/dev/null
+}
+
+run_index() {
+ PATH="$_MOCK_DIR:$_TOOLS_DIR" bash -c \
+ "unset CUDA_VISIBLE_DEVICES UNSLOTH_ROCM_GFX_ARCH UNSLOTH_TORCH_INDEX_URL UNSLOTH_TORCH_INDEX_FAMILY ROCM_PATH
+ _ARCH=x86_64; . '$_FUNC_FILE'; get_torch_index_url" 2>/dev/null
+}
+
+run_warnings() {
+ PATH="$_MOCK_DIR:$_TOOLS_DIR" bash -c \
+ "unset CUDA_VISIBLE_DEVICES UNSLOTH_ROCM_GFX_ARCH UNSLOTH_TORCH_INDEX_URL UNSLOTH_TORCH_INDEX_FAMILY ROCM_PATH
+ _ARCH=x86_64; . '$_FUNC_FILE'; get_torch_index_url" 2>&1 >/dev/null | tr '\n' ' '
+}
+
+run_version_tag() {
+ PATH="$_MOCK_DIR:$_TOOLS_DIR" bash -c \
+ "unset ROCM_PATH
+ . '$_FUNC_FILE'; _detect_rocm_version_tag" 2>/dev/null
+}
+
+_BASE="https://download.pytorch.org/whl"
+
+echo "=== test_rocm_no_version_arch_route ==="
+
+# cpu is expected: get_torch_index_url runs in a command substitution, so it cannot set TORCH_INDEX_URL and defers to the parent-shell reroute.
+reset_host gfx1201
+assert_eq "no version + mapped arch still returns cpu from the function" \
+ "$_BASE/cpu" "$(run_index)"
+_w=$(run_warnings)
+assert_contains "no version + mapped arch announces the per-arch route" \
+ "routing to AMD per-arch wheels" "$_w"
+assert_lacks "no version + mapped arch drops the 'no ROCm install' claim" \
+ "no ROCm/HIP install was found" "$_w"
+assert_lacks "no version + mapped arch does not suggest pacman" "pacman" "$_w"
+
+reset_host gfx1201
+add_rocm_bin_hipconfig "5.7.31921-0"
+assert_eq "unsupported version keeps the cpu index" "$_BASE/cpu" "$(run_index)"
+_w=$(run_warnings)
+assert_contains "unsupported version says why" "require ROCm 6.0+" "$_w"
+assert_lacks "unsupported version does not claim a per-arch route" \
+ "routing to AMD per-arch wheels" "$_w"
+
+reset_host gfx1201
+add_rocm_bin_hipconfig "6.4.43483-0"
+assert_eq "supported version still picks the versioned leaf" \
+ "$_BASE/rocm6.4" "$(run_index)"
+
+# gfx906 has no per-arch index, only the generic rocmX.Y leaves, so without a version there is nothing to route to.
+reset_host gfx906
+assert_eq "unmapped arch with no version stays cpu" "$_BASE/cpu" "$(run_index)"
+_w=$(run_warnings)
+assert_lacks "unmapped arch does not claim a per-arch route" \
+ "routing to AMD per-arch wheels" "$_w"
+assert_contains "unmapped arch names the sources it tried" "Version sources checked" "$_w"
+
+reset_host gfx906
+add_rocm_bin_hipconfig "6.2.41134-0"
+assert_eq "hipconfig off PATH is read from ROCM_PATH/bin" "rocm6.2" "$(run_version_tag)"
+
+reset_host gfx906
+add_rpm_package rocm-runtime "6.3.1"
+assert_eq "rpm rocm-runtime answers where rocm-core does not" "rocm6.3" "$(run_version_tag)"
+reset_host gfx906
+add_rpm_package rocm-hip "7.0.2"
+assert_eq "rpm rocm-hip answers too" "rocm7.0" "$(run_version_tag)"
+reset_host gfx906
+add_rpm_package rocm-core "6.1.0"
+assert_eq "rpm rocm-core still answers" "rocm6.1" "$(run_version_tag)"
+
+reset_host gfx906
+mkdir -p "$_FAKE_ROCM_DIR/bin"
+_w=$(run_warnings)
+assert_contains "an existing ROCm tree is acknowledged" "so ROCm is likely installed" "$_w"
+assert_lacks "an existing ROCm tree is not called missing" "Install the ROCm/HIP SDK" "$_w"
+
+# Top-level installer code, so this grep proves wiring, not behaviour: it passes even if the gate always decides "no".
+# The routing itself is asserted in test_rocm_no_version_arch_route_e2e.sh, which splices that block out and RUNS it.
+_gate=$(grep -c '_amd_no_rocm_version_reroute' "$INSTALL_SH")
+assert_eq "install.sh wires the no-version reroute state" "yes" \
+ "$([ "$_gate" -ge 4 ] && echo yes || echo "no ($_gate refs)")"
+
+# Probes enumerate in kernel order, so taking the first token would put APU gfx1151 wheels on a 9070 XT listed behind it.
+assert_eq "duplicate agents agree on a family" "gfx120X-all" "$(run_family 'gfx1201
+gfx1201')"
+assert_eq "two cards in one family agree on it" "gfx120X-all" "$(run_family 'gfx1201
+gfx1200')"
+assert_eq "an APU beside a discrete card agrees on nothing" "REJECTED" "$(run_family 'gfx1151
+gfx1201')"
+assert_eq "an unmappable agent rejects the whole host" "REJECTED" "$(run_family 'gfx1201
+gfx906')"
+assert_eq "an empty probe is rejected" "REJECTED" "$(run_family '')"
+
+# setup.sh takes this concrete arch over its own visibility-aware pick, so it may only answer when there is one card to name.
+assert_eq "duplicate agents are one device and name it" "gfx1201" "$(run_sole_arch 'gfx1201
+gfx1201')"
+assert_eq "two cards in one family name neither" "REJECTED" "$(run_sole_arch 'gfx1201
+gfx1200')"
+assert_eq "an APU beside a discrete card names neither" "REJECTED" "$(run_sole_arch 'gfx1151
+gfx1201')"
+
+reset_host_mixed gfx1151 gfx1201
+assert_eq "mixed-arch host with no version keeps the cpu index" "$_BASE/cpu" "$(run_index)"
+_w=$(run_warnings)
+assert_lacks "mixed-arch host does not claim a per-arch route" "routing to AMD per-arch wheels" "$_w"
+
+reset_host_mixed gfx1201 gfx1200
+assert_eq "same-family pair still defers to the reroute" "$_BASE/cpu" "$(run_index)"
+_w=$(run_warnings)
+assert_contains "same-family pair still announces the per-arch route" "routing to AMD per-arch wheels" "$_w"
+assert_contains "same-family pair names the family, not a card" "gfx120X-all" "$_w"
+
+assert_eq "the export is guarded on a single named card" "yes" "$(grep -q 'export UNSLOTH_ROCM_GFX_ARCH="\$_linux_inferred_gfx"' "$INSTALL_SH" && grep -c 'if \[ -n "\$_linux_inferred_gfx" \]; then' "$INSTALL_SH" >/dev/null && echo yes || echo no)"
+assert_eq "the torch constraint is chosen off the family" "yes" "$(grep -q 'gfx120X-all|gfx1151|gfx1150|gfx1152)' "$INSTALL_SH" && echo yes || echo no)"
+
+# HIP reports gfx1201:sramecc+:xnack-, which no arm of the index case table matches; get_torch_index_url strips the suffix
+# and then promises per-arch wheels, so anything downstream that does not strip it strands the host on CPU.
+assert_eq "a gcnArchName suffix normalises to the bare arch" "gfx1201" "$(run_sole_arch 'gfx1201:sramecc+:xnack-')"
+assert_eq "the suffixed arch would not map on its own" "REJECTED" "$(PATH="$_MOCK_DIR:$_TOOLS_DIR" bash -c ". '$_FUNC_FILE'; _amd_arch_index_family_for_gfx 'gfx1201:sramecc+:xnack-' || echo REJECTED" 2>/dev/null)"
+assert_eq "the override reroute normalises before the case table" "yes" "$(grep -q '_linux_inferred_gfx=\$(_amd_sole_index_arch "\$_linux_inferred_gfx")' "$INSTALL_SH" && echo yes || echo no)"
+
+# The EXIT trap rm -rf's the memo path, so one inherited from the environment and never overwritten is an rm -rf on a caller-chosen path.
+_reset_block=$(sed -n '/^# Clear inherited cleanup targets before installing traps\./,/^trap _on_install_exit EXIT/p' "$INSTALL_SH")
+assert_contains "the memo dir is cleared before the traps" '_ROCM_TAG_MEMO_DIR=""' "$_reset_block"
+assert_contains "the memo path is cleared before the traps" '_ROCM_TAG_MEMO=""' "$_reset_block"
+
+reset_host gfx1201
+mkdir -p "$_FAKE_ROCM_DIR/bin"
+_COUNT_FILE="$_MOCK_DIR/hipconfig-calls"
+cat > "$_FAKE_ROCM_DIR/bin/hipconfig" <<MOCK
+#!/bin/sh
+echo x >> "$_COUNT_FILE"
+echo "6.4.43483-0"
+MOCK
+chmod +x "$_FAKE_ROCM_DIR/bin/hipconfig"
+_memo_out=$(PATH="$_MOCK_DIR:$_TOOLS_DIR" bash -c "unset ROCM_PATH
+ _ROCM_TAG_MEMO='$_MOCK_DIR/memo'
+ . '$_FUNC_FILE'
+ _detect_rocm_version_tag
+ _detect_rocm_version_tag" 2>/dev/null | tr '
+' ' ')
+assert_eq "both calls report the same tag" "rocm6.4 rocm6.4 " "$_memo_out"
+assert_eq "the second call did not re-probe" "1" "$(wc -l < "$_COUNT_FILE" | tr -d ' ')"
+
+rm -f "$_MOCK_DIR/memo" "$_COUNT_FILE"
+_nomemo_out=$(PATH="$_MOCK_DIR:$_TOOLS_DIR" bash -c "unset ROCM_PATH _ROCM_TAG_MEMO
+ . '$_FUNC_FILE'
+ _detect_rocm_version_tag
+ _detect_rocm_version_tag" 2>/dev/null | tr '
+' ' ')
+assert_eq "no memo path means no caching" "rocm6.4 rocm6.4 " "$_nomemo_out"
+assert_eq "and both calls really probed" "2" "$(wc -l < "$_COUNT_FILE" | tr -d ' ')"
+
+echo ""
+echo " passed: $PASS, failed: $FAIL"
+[ "$FAIL" -eq 0 ]
diff --git a/tests/sh/test_rocm_no_version_arch_route_e2e.sh b/tests/sh/test_rocm_no_version_arch_route_e2e.sh
new file mode 100755
index 00000000000..8b959ccc898
--- /dev/null
+++ b/tests/sh/test_rocm_no_version_arch_route_e2e.sh
@@ -0,0 +1,509 @@
+#!/bin/sh
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+# End-to-end routing assertions for the no-readable-version AMD reroute (issue #8731). Separate from
+# test_rocm_no_version_arch_route.sh because the reroute is TOP-LEVEL code: get_torch_index_url returns
+# */cpu and defers, so sourcing functions never runs the decision and grepping for its variable names
+# passes with the reroute deleted. The splice runs PAST it: Strix gfx115x and gfx906 still rewrite it.
+set -e
+
+SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
+INSTALL_SH="$SCRIPT_DIR/../../install.sh"
+PASS=0
+FAIL=0
+
+[ -r "$INSTALL_SH" ] || { echo " FAIL: no install.sh at $INSTALL_SH"; exit 1; }
+
+_ROOT=$(mktemp -d)
+cleanup() { rm -rf "$_ROOT"; }
+trap cleanup EXIT
+# The spliced block runs its own mktemp -d (the ROCm tag memo) without a cleanup trap, so keep it under $_ROOT.
+TMPDIR="$_ROOT/tmp"
+export TMPDIR
+mkdir -p "$TMPDIR"
+
+# Host paths are redirected into $_FAKE, or a real GPU on this box gets measured and the suite passes regardless.
+_FAKE="$_ROOT/fake"
+_F_ROCM="$_FAKE/opt-rocm"
+_F_PROCNV="$_FAKE/proc-driver-nvidia"
+_F_NVSMI="$_FAKE/usr-bin-nvidia-smi"
+_F_KFD="$_FAKE/dev-kfd"
+_F_SYSKFD="$_FAKE/sys-class-kfd"
+_F_PCI="$_FAKE/sys-bus-pci-devices"
+_F_CPUINFO="$_FAKE/proc-cpuinfo"
+_F_PROCVER="$_FAKE/proc-version"
+_F_DXG="$_FAKE/dev-dxg"
+
+_MOCK="$_ROOT/mock"
+_TOOLS="$_ROOT/tools"
+mkdir -p "$_MOCK" "$_TOOLS"
+for _cmd in uname grep sed awk head tail tr ls sort cat cut wc mktemp rm mkdir chmod \
+ dirname basename expr readlink id date find stat env timeout sleep sh bash \
+ printf echo test true false touch cp ln; do
+ _real=$(command -v "$_cmd" 2>/dev/null || true)
+ [ -n "$_real" ] && ln -sf "$_real" "$_TOOLS/$_cmd"
+done
+
+_redirect() {
+ sed -e "s|/usr/bin/nvidia-smi|$_F_NVSMI|g" \
+ -e "s|/proc/driver/nvidia|$_F_PROCNV|g" \
+ -e "s|/opt/rocm|$_F_ROCM|g" \
+ -e "s|/sys/class/kfd|$_F_SYSKFD|g" \
+ -e "s|/sys/bus/pci/devices|$_F_PCI|g" \
+ -e "s|/dev/kfd|$_F_KFD|g" \
+ -e "s|/dev/dxg|$_F_DXG|g" \
+ -e "s|/proc/cpuinfo|$_F_CPUINFO|g" \
+ -e "s|/proc/version|$_F_PROCVER|g"
+}
+
+_FUNCS="$_ROOT/funcs.sh"
+_BLOCK="$_ROOT/block.sh"
+
+: > "$_ROOT/funcs.raw"
+for _fn in $(grep -oE '^[A-Za-z_][A-Za-z0-9_]*\(\)[[:space:]]*\{[[:space:]]*$' "$INSTALL_SH" \
+ | sed 's/().*//' | sort -u); do
+ sed -n "/^$_fn()[[:space:]]*{[[:space:]]*\$/,/^}\$/p" "$INSTALL_SH" > "$_ROOT/.one"
+ # A heredoc with a bare `}` truncates this extraction into a syntax error; the check below covers the loss.
+ if bash -n "$_ROOT/.one" 2>/dev/null; then
+ cat "$_ROOT/.one" >> "$_ROOT/funcs.raw"
+ echo "" >> "$_ROOT/funcs.raw"
+ fi
+done
+rm -f "$_ROOT/.one"
+_redirect < "$_ROOT/funcs.raw" > "$_FUNCS"
+
+awk '
+ !started && (/^_ROCM_TAG_MEMO_DIR=\$\(mktemp/ || /^TORCH_INDEX_URL=\$\(get_torch_index_url\)$/) { started = 1 }
+ started && /^fi # _torch_index_pinned guard/ { print; exit }
+ started { print }
+' "$INSTALL_SH" > "$_ROOT/block.raw"
+_redirect < "$_ROOT/block.raw" > "$_BLOCK"
+
+_fatal() { echo " FAIL: $1"; echo ""; echo " passed: $PASS, failed: 1"; exit 1; }
+
+for _fn in get_torch_index_url _has_amd_rocm_gpu _has_usable_nvidia_gpu \
+ _probe_amd_gfx_arch _detect_rocm_version_tag _amd_arch_index_family_for_gfx \
+ _amd_agreed_index_family _amd_sole_index_arch _infer_linux_amd_gfx_arch \
+ _infer_amd_gfx_arch_from_gpu_name _amd_gpu_present_via_pci _run_bounded \
+ _rocm_tag_from_amd_smi _rocm_tag_from_hipconfig _rocm_tag_from_rpm \
+ _rocm_tag_from_dpkg _rocm_tag_from_version_file _highest_rocm_tag \
+ _trim_index_path_slashes _cvd_hides_nvidia _ensure_rocm_probe_env; do
+ grep -q "^$_fn()" "$_FUNCS" || \
+ _fatal "install.sh no longer defines $_fn() at column 0 (splice would silently answer cpu)"
+done
+tail -1 "$_ROOT/block.raw" | grep -q '^fi # _torch_index_pinned guard' || \
+ _fatal "the top-level block splice never reached its end marker in install.sh"
+grep -q 'TORCH_INDEX_URL=\$(get_torch_index_url)' "$_BLOCK" || \
+ _fatal "the block splice lost the get_torch_index_url call"
+grep -q 'UNSLOTH_AMD_ROCM_MIRROR' "$_BLOCK" || \
+ _fatal "the block splice lost the AMD per-arch mirror"
+bash -n "$_FUNCS" || _fatal "spliced functions do not parse"
+bash -n "$_BLOCK" || _fatal "spliced block does not parse"
+sh -n "$_FUNCS" || _fatal "spliced functions are not POSIX-parseable"
+sh -n "$_BLOCK" || _fatal "spliced block is not POSIX-parseable"
+
+assert_eq() {
+ _label="$1"; _expected="$2"; _actual="$3"
+ if [ "$_actual" = "$_expected" ]; then
+ echo " PASS: $_label"
+ PASS=$((PASS + 1))
+ else
+ echo " FAIL: $_label (expected '$_expected', got '$_actual')"
+ FAIL=$((FAIL + 1))
+ fi
+}
+
+reset_host() {
+ rm -rf "$_FAKE" "$_MOCK"
+ mkdir -p "$_FAKE" "$_MOCK" "$_F_PROCNV" "$_F_PCI" "$_F_ROCM"
+ : > "$_F_CPUINFO"
+ printf 'Linux version 7.1.5 (builder) #1 SMP\n' > "$_F_PROCVER"
+}
+
+# rocminfo names each agent twice, as the real tool does.
+mock_rocminfo() {
+ {
+ echo "ROCk module is loaded"
+ echo "Agent 1"
+ echo " Name: AMD EPYC CPU"
+ echo " Device Type: CPU"
+ _i=2
+ for _a in "$@"; do
+ echo "Agent $_i"
+ echo " Name: $_a"
+ echo " Marketing Name: AMD Radeon Graphics"
+ echo " Device Type: GPU"
+ echo " Name: $_a"
+ _i=$((_i + 1))
+ done
+ } > "$_MOCK/.rocminfo.out"
+ cat > "$_MOCK/rocminfo" <<'MOCK'
+#!/bin/sh
+cat "$(dirname "$0")/.rocminfo.out"
+MOCK
+ chmod +x "$_MOCK/rocminfo"
+}
+
+mock_amdsmi() {
+ _ver="$1"; shift
+ {
+ _i=0
+ for _a in "$@"; do
+ echo "GPU: $_i"
+ echo " BDF: 0000:0$_i:00.0"
+ echo " UUID: 00000000-0000-0000-0000-00000000000$_i"
+ _i=$((_i + 1))
+ done
+ } > "$_MOCK/.amdsmi.list"
+ {
+ for _a in "$@"; do
+ echo "ASIC:"
+ echo " MARKET_NAME: AMD Radeon Graphics"
+ echo " TARGET_GRAPHICS_VERSION: $_a"
+ done
+ } > "$_MOCK/.amdsmi.asic"
+ printf 'AMDSMI Tool: 25.0.1 | AMDSMI Library version: 25.0.1.0 | ROCm version: %s | amdgpu version: 6.10.10\n' \
+ "$_ver" > "$_MOCK/.amdsmi.version"
+ cat > "$_MOCK/amd-smi" <<'MOCK'
+#!/bin/sh
+_d=$(dirname "$0")
+case "${1:-}" in
+ list) cat "$_d/.amdsmi.list" ;;
+ static) cat "$_d/.amdsmi.asic" ;;
+ *) cat "$_d/.amdsmi.version" ;;
+esac
+MOCK
+ chmod +x "$_MOCK/amd-smi"
+}
+
+mock_hipconfig_offpath() {
+ mkdir -p "$_F_ROCM/bin"
+ cat > "$_F_ROCM/bin/hipconfig" <<MOCK
+#!/bin/sh
+printf '%s' '$1'
+[ -n '$1' ] && echo
+exit 0
+MOCK
+ chmod +x "$_F_ROCM/bin/hipconfig"
+}
+
+mock_rpm() {
+ if [ $# -eq 2 ]; then
+ printf '%s\n' "$1" > "$_MOCK/.rpm-pkg"; printf '%s\n' "$2" > "$_MOCK/.rpm-ver"
+ else
+ : > "$_MOCK/.rpm-pkg"; : > "$_MOCK/.rpm-ver"
+ fi
+ cat > "$_MOCK/rpm" <<'MOCK'
+#!/bin/sh
+_d=$(dirname "$0")
+_known=$(cat "$_d/.rpm-pkg"); _ver=$(cat "$_d/.rpm-ver")
+_hit=0; _skip=0
+for _arg in "$@"; do
+ if [ "$_skip" = 1 ]; then _skip=0; continue; fi
+ case "$_arg" in
+ --qf|--queryformat) _skip=1 ;;
+ -*) : ;;
+ *) if [ -n "$_known" ] && [ "$_arg" = "$_known" ]; then printf '%s\n' "$_ver"; _hit=1
+ else printf 'package %s is not installed\n' "$_arg"; fi ;;
+ esac
+done
+[ "$_hit" = 1 ] || exit 1
+MOCK
+ chmod +x "$_MOCK/rpm"
+}
+
+mock_dpkg() { # $1 = version of an installed rocm-core
+ printf '%s\n' "${1:-}" > "$_MOCK/.dpkg-ver"
+ cat > "$_MOCK/dpkg-query" <<'MOCK'
+#!/bin/sh
+_v=$(cat "$(dirname "$0")/.dpkg-ver")
+[ -n "$_v" ] || exit 1
+printf 'install ok installed %s\n' "$_v"
+MOCK
+ chmod +x "$_MOCK/dpkg-query"
+}
+
+mock_nvidia_smi() { # $1 = CUDA version, $2 = compute capability
+ printf '%s\n' "$1" > "$_MOCK/.cuda-ver"
+ printf '%s\n' "$2" > "$_MOCK/.compute-cap"
+ cat > "$_MOCK/nvidia-smi" <<'MOCK'
+#!/bin/sh
+_d=$(dirname "$0")
+for _a in "$@"; do
+ case "$_a" in
+ -L) echo "GPU 0: NVIDIA B200 (UUID: GPU-deadbeef)"; exit 0 ;;
+ --query-gpu=compute_cap) cat "$_d/.compute-cap"; exit 0 ;;
+ esac
+done
+printf 'NVIDIA-SMI 580.65.06 Driver Version: 580.65.06 CUDA Version: %s\n' "$(cat "$_d/.cuda-ver")"
+MOCK
+ chmod +x "$_MOCK/nvidia-smi"
+ cp "$_MOCK/nvidia-smi" "$_F_NVSMI"; chmod +x "$_F_NVSMI"
+ mkdir -p "$_F_PROCNV/gpus/0000:01:00.0"
+}
+
+mock_kfd() { # AMD GPU visible through the KFD topology
+ : > "$_F_KFD"
+ mkdir -p "$_F_SYSKFD/kfd/topology/nodes/1"
+ printf 'cpu_cores_count 0\nsimd_count 128\nvendor_id 4098\ndevice_id 29824\n' \
+ > "$_F_SYSKFD/kfd/topology/nodes/1/properties"
+}
+
+mock_pci_amd_display() {
+ mkdir -p "$_F_PCI/0000:03:00.0"
+ echo 0x1002 > "$_F_PCI/0000:03:00.0/vendor"
+ echo 0x030000 > "$_F_PCI/0000:03:00.0/class"
+}
+
+mock_lspci() { # $1 = the display-controller name lspci reports
+ printf '03:00.0 VGA compatible controller [0300]: Advanced Micro Devices, Inc. [AMD/ATI] %s [1002:7550]\n' \
+ "$1" > "$_MOCK/.lspci.out"
+ cat > "$_MOCK/lspci" <<'MOCK'
+#!/bin/sh
+cat "$(dirname "$0")/.lspci.out"
+MOCK
+ chmod +x "$_MOCK/lspci"
+}
+
+_RUN_SHELL=bash
+run_installer() { # $1 = _ARCH
+ cat > "$_ROOT/run.sh" <<EOF
+set -e
+unset CUDA_VISIBLE_DEVICES UNSLOTH_ROCM_GFX_ARCH UNSLOTH_TORCH_INDEX_URL \\
+ UNSLOTH_TORCH_INDEX_FAMILY UNSLOTH_AMD_ROCM_MIRROR UNSLOTH_PYTORCH_MIRROR \\
+ ROCM_PATH ROCR_VISIBLE_DEVICES HIP_VISIBLE_DEVICES 2>/dev/null || true
+_ARCH=$1
+_torch_index_pinned=false
+SKIP_TORCH=false
+TORCH_CONSTRAINT=""
+TORCHVISION_CONSTRAINT=""
+TORCHAUDIO_CONSTRAINT=""
+. "$_FUNCS"
+. "$_BLOCK"
+printf 'INDEX=%s\n' "\$TORCH_INDEX_URL"
+printf 'GFX=%s\n' "\${UNSLOTH_ROCM_GFX_ARCH:-}"
+printf 'RADEON=%s\n' "\${_amd_gpu_radeon:-}"
+printf 'HSA=%s\n' "\${HSA_OVERRIDE_GFX_VERSION:-}"
+EOF
+ PATH="$_MOCK:$_TOOLS" "$_RUN_SHELL" "$_ROOT/run.sh" 2>"$_ROOT/stderr.txt" || \
+ printf 'INDEX=<installer exited %s>\n' "$?"
+}
+
+run_index() { run_installer "${1:-x86_64}" | sed -n 's/^INDEX=//p'; }
+run_gfx() { run_installer "${1:-x86_64}" | sed -n 's/^GFX=//p'; }
+run_radeon() { run_installer "${1:-x86_64}" | sed -n 's/^RADEON=//p'; }
+run_hsa() { run_installer "${1:-x86_64}" | sed -n 's/^HSA=//p'; }
+
+_BASE="https://download.pytorch.org/whl"
+_AMD="https://repo.amd.com/rocm/whl"
+
+echo "=== test_rocm_no_version_arch_route_e2e ==="
+
+reset_host
+cat > "$_ROOT/nvcheck.sh" <<EOF
+unset CUDA_VISIBLE_DEVICES 2>/dev/null || true
+. "$_FUNCS"
+if _has_usable_nvidia_gpu; then echo SEEN; else echo HIDDEN; fi
+EOF
+assert_eq "the real host's NVIDIA driver is redirected away" "HIDDEN" \
+ "$(PATH="$_MOCK:$_TOOLS" bash "$_ROOT/nvcheck.sh" 2>/dev/null)"
+mock_nvidia_smi "12.8" "9.0"
+assert_eq "and a faked driver tree is still seen (the probe is reached)" "SEEN" \
+ "$(PATH="$_MOCK:$_TOOLS" bash "$_ROOT/nvcheck.sh" 2>/dev/null)"
+reset_host
+cat > "$_ROOT/amdcheck.sh" <<EOF
+. "$_FUNCS"
+if _has_amd_rocm_gpu; then echo SEEN; else echo HIDDEN; fi
+EOF
+assert_eq "no AMD GPU is seen on a bare fake host" "HIDDEN" \
+ "$(PATH="$_MOCK:$_TOOLS" bash "$_ROOT/amdcheck.sh" 2>/dev/null)"
+mock_kfd; mock_pci_amd_display
+assert_eq "and a faked KFD topology is seen" "SEEN" \
+ "$(PATH="$_MOCK:$_TOOLS" bash "$_ROOT/amdcheck.sh" 2>/dev/null)"
+
+fedora_no_version_host() { # $@ = gfx arches
+ reset_host
+ mock_rocminfo "$@"
+ mock_amdsmi "N/A" "$@"
+ mock_hipconfig_offpath ""
+ mock_rpm
+ mock_kfd
+ mock_pci_amd_display
+}
+
+fedora_no_version_host gfx1201
+assert_eq "gfx1201 with no readable version routes to the per-arch index" \
+ "$_AMD/gfx120X-all/" "$(run_index)"
+fedora_no_version_host gfx1201
+assert_eq "and the single named card is handed to setup.sh" \
+ "gfx1201" "$(run_gfx)"
+
+fedora_no_version_host gfx1100
+assert_eq "gfx1100 with no readable version routes to its own family" \
+ "$_AMD/gfx110X-all/" "$(run_index)"
+
+# gfx906 (MI50) is served only through a generic rocmX.Y leaf, so there is nothing to reroute to.
+fedora_no_version_host gfx906
+assert_eq "gfx906 with no readable version stays on cpu" "$_BASE/cpu" "$(run_index)"
+
+fedora_no_version_host gfx1201
+mock_hipconfig_offpath "5.7.31921-0"
+assert_eq "gfx1201 on unsupported ROCm 5.7 stays on cpu" "$_BASE/cpu" "$(run_index)"
+
+fedora_no_version_host gfx1201 gfx1036
+mock_lspci "Navi 48 [Radeon RX 9070 XT]"
+assert_eq "gfx1201 beside gfx1036 stays on cpu" "$_BASE/cpu" "$(run_index)"
+
+# UNSLOTH_ROCM_GFX_ARCH must stay unset: setup.sh makes a visibility-aware pick that naming a card overrules.
+fedora_no_version_host gfx1200 gfx1201
+mock_lspci "Navi 48 [Radeon RX 9070 XT]"
+assert_eq "a same-family pair routes to the shared family" \
+ "$_AMD/gfx120X-all/" "$(run_index)"
+fedora_no_version_host gfx1200 gfx1201
+mock_lspci "Navi 48 [Radeon RX 9070 XT]"
+assert_eq "a same-family pair names no single card" "" "$(run_gfx)"
+
+# rocminfo calls every consumer card "Radeon", and the AMD per-arch mirror is
+# repo.amd.com/ROCM/whl/gfx120X-all/, so branding the host off the whole URL sends the
+# rerouted install down the repo.radeon.com branch: the summary reports wheels it never
+# fetched and the Radeon path then warns it cannot read the ROCm version it does not need.
+fedora_no_version_host gfx1201
+mock_lspci "Navi 48 [Radeon RX 9070 XT]"
+assert_eq "a per-arch reroute is not branded a Radeon repo install" "false" "$(run_radeon)"
+fedora_no_version_host gfx1200 gfx1201
+assert_eq "nor is the same-family reroute" "false" "$(run_radeon)"
+
+# Real ROCm 7.x rocminfo for one gfx1201: the agent Name, an ISA triple carrying feature
+# flags, and a SECOND ISA "gfx12-generic" (ROCm/ROCm#6110). One card must not read as two,
+# and gfx12-generic must not read as an arch -- either way the sole-arch check rejects the
+# host and a working 9070 XT falls back to cpu.
+fedora_no_version_host gfx1201
+{
+ echo "ROCk module is loaded"
+ echo "Agent 1"
+ echo " Name: AMD Ryzen 9 9950X"
+ echo " Device Type: CPU"
+ echo "Agent 2"
+ echo " Name: gfx1201"
+ echo " Marketing Name: AMD Radeon RX 9070 XT"
+ echo " Device Type: GPU"
+ echo " ISA Info:"
+ echo " ISA 1"
+ echo " Name: amdgcn-amd-amdhsa--gfx1201:sramecc+:xnack-"
+ echo " ISA 2"
+ echo " Name: amdgcn-amd-amdhsa--gfx12-generic"
+} > "$_MOCK/.rocminfo.out"
+assert_eq "a ROCm 7.x gfx1201 with two ISAs is still one card" \
+ "$_AMD/gfx120X-all/" "$(run_index)"
+assert_eq "and gfx12-generic does not count as a second arch" "gfx1201" "$(run_gfx)"
+
+reset_host
+mock_nvidia_smi "12.8" "9.0"
+mock_hipconfig_offpath "6.4.43483-0"
+mock_rpm rocm-core 6.4.1
+mock_dpkg "6.4.1-1"
+assert_eq "an NVIDIA host with stale ROCm packages keeps its CUDA index" \
+ "$_BASE/cu128" "$(run_index)"
+
+reset_host
+mock_hipconfig_offpath "6.4.43483-0"
+mock_rpm rocm-core 6.4.1
+mock_dpkg "6.4.1-1"
+assert_eq "a GPU-less host with stale ROCm packages stays on cpu" \
+ "$_BASE/cpu" "$(run_index)"
+
+fedora_no_version_host gfx1201
+assert_eq "the same host on aarch64 does not reroute" "$_BASE/cpu" "$(run_index aarch64)"
+
+# install.sh is #!/bin/sh but CI runs these files with bash, so a bashism would surface only on a dash host.
+_SH_REAL=$(command -v sh 2>/dev/null || true)
+if [ -n "$_SH_REAL" ]; then
+ _RUN_SHELL="$_SH_REAL"
+ fedora_no_version_host gfx1201
+ assert_eq "under /bin/sh the reported host still routes per-arch" \
+ "$_AMD/gfx120X-all/" "$(run_index)"
+ fedora_no_version_host gfx906
+ assert_eq "under /bin/sh an unmapped arch still stays on cpu" \
+ "$_BASE/cpu" "$(run_index)"
+ _RUN_SHELL=bash
+else
+ echo " SKIP: no /bin/sh to cross-check"
+fi
+
+# HSA_OVERRIDE_GFX_VERSION=11.0.0 is the standard Strix Halo workaround, so ROCr reports
+# the spoofed gfx1100 on a physical gfx1151. The reroute derives BOTH the wheel family and
+# the exported arch from that probe, so without the correction this host takes gfx110X-all
+# wheels and hands setup.sh a gfx1100 to build llama.cpp for.
+mock_strix_cpuinfo() { printf 'model name\t: AMD Ryzen AI Max+ 395 w/ Radeon 8060S\n' > "$_F_CPUINFO"; }
+mock_kfd_gfx() { # $1 = gfx_target_version, which the override cannot reach
+ : > "$_F_KFD"
+ mkdir -p "$_F_SYSKFD/kfd/topology/nodes/1"
+ printf 'cpu_cores_count 0\nsimd_count 128\nvendor_id 4098\ndevice_id 29824\ngfx_target_version %s\n' \
+ "$1" > "$_F_SYSKFD/kfd/topology/nodes/1/properties"
+}
+
+fedora_no_version_host gfx1100
+mock_strix_cpuinfo; mock_kfd_gfx 110501
+assert_eq "an HSA-spoofed Strix Halo routes to the arch it really is" \
+ "$_AMD/gfx1151/" "$(HSA_OVERRIDE_GFX_VERSION=11.0.0 run_index)"
+fedora_no_version_host gfx1100
+mock_strix_cpuinfo; mock_kfd_gfx 110501
+assert_eq "and hands setup.sh the physical arch, not the spoof" \
+ "gfx1151" "$(HSA_OVERRIDE_GFX_VERSION=11.0.0 run_gfx)"
+
+# Controls. Without the override the same probe is the truth, and the correction must not
+# fire on a real gfx1100 that merely sits in a Ryzen AI Max chassis.
+fedora_no_version_host gfx1100
+mock_strix_cpuinfo; mock_kfd_gfx 110501
+assert_eq "with no override the probed arch is taken at face value" \
+ "$_AMD/gfx110X-all/" "$(run_index)"
+fedora_no_version_host gfx1100
+mock_strix_cpuinfo; mock_kfd_gfx 110000
+assert_eq "a real gfx1100 corroborated by the kernel keeps its own family" \
+ "$_AMD/gfx110X-all/" "$(HSA_OVERRIDE_GFX_VERSION=11.0.0 run_index)"
+
+
+mock_kfd_two() { # $1 $2 = gfx_target_version per KFD node
+ : > "$_F_KFD"
+ for _i in 1 2; do
+ mkdir -p "$_F_SYSKFD/kfd/topology/nodes/$_i"
+ done
+ printf 'cpu_cores_count 0\nsimd_count 128\nvendor_id 4098\ngfx_target_version %s\n' \
+ "$1" > "$_F_SYSKFD/kfd/topology/nodes/1/properties"
+ printf 'cpu_cores_count 0\nsimd_count 128\nvendor_id 4098\ngfx_target_version %s\n' \
+ "$2" > "$_F_SYSKFD/kfd/topology/nodes/2/properties"
+}
+
+# The override can collapse a mixed host into ONE reported arch, and a singleton probe then
+# agrees on a family neither physical GPU can run. The spoof helper already declines here;
+# an empty correction must not be read as "no spoof".
+fedora_no_version_host gfx1100
+mock_strix_cpuinfo; mock_kfd_two 110501 120001
+assert_eq "a spoof-collapsed mixed host does not reroute" \
+ "$_BASE/cpu" "$(HSA_OVERRIDE_GFX_VERSION=11.0.0 run_index)"
+fedora_no_version_host gfx1100
+mock_strix_cpuinfo; mock_kfd_two 110501 120001
+assert_eq "and names no arch for setup.sh to build" \
+ "" "$(HSA_OVERRIDE_GFX_VERSION=11.0.0 run_gfx)"
+
+# Two nodes that agree are still a decline, because the shipped helper declines on any
+# multi-node KFD. This asserts the conservative outcome rather than the ideal one:
+# correcting here would mean changing the helper, which the Strix path shares.
+fedora_no_version_host gfx1100
+mock_strix_cpuinfo; mock_kfd_two 110501 110501
+assert_eq "two nodes the helper will not vouch for do not reroute either" \
+ "$_BASE/cpu" "$(HSA_OVERRIDE_GFX_VERSION=11.0.0 run_index)"
+
+# Native wheels plus a live override is the worst of both: the kernels are there and ROCr
+# keeps reporting the arch they are not for. The rocm* leaf clears it; a gfx* leaf must too.
+fedora_no_version_host gfx1100
+mock_strix_cpuinfo; mock_kfd_gfx 110501
+assert_eq "a corroborated spoof is cleared once native wheels are chosen" \
+ "" "$(HSA_OVERRIDE_GFX_VERSION=11.0.0 run_hsa)"
+fedora_no_version_host gfx1201
+assert_eq "an override that corroborated nothing is left alone" \
+ "11.0.0" "$(HSA_OVERRIDE_GFX_VERSION=11.0.0 run_hsa)"
+
+echo ""
+echo " passed: $PASS, failed: $FAIL"
+[ "$FAIL" -eq 0 ]
diff --git a/tests/sh/test_rocm_version_source_disagreement.sh b/tests/sh/test_rocm_version_source_disagreement.sh
index f36f79f63e6..c3c500a98b5 100755
--- a/tests/sh/test_rocm_version_source_disagreement.sh
+++ b/tests/sh/test_rocm_version_source_disagreement.sh
@@ -46,6 +46,12 @@ _FAKE_PROC_NV_DIR=$(mktemp -d)
sed -n '/^_infer_linux_amd_gfx_arch()/,/^}/p' "$INSTALL_SH"
echo ""
sed -n '/^_amd_arch_index_family_for_gfx()/,/^}/p' "$INSTALL_SH"
+ echo
+ sed -n '/^_amd_probe_arches()/,/^}/p' "$INSTALL_SH"
+ echo
+ sed -n '/^_amd_agreed_index_family()/,/^}/p' "$INSTALL_SH"
+ echo
+ sed -n '/^_amd_sole_index_arch()/,/^}/p' "$INSTALL_SH"
echo ""
sed -n '/^_trim_index_path_slashes()/,/^}/p' "$INSTALL_SH"
echo ""
@@ -284,6 +290,23 @@ MOCK
chmod +x "$_MOCK_DIR/rpm"
}
+# $1 = rocm-core version, $2 = rocm-runtime version. A partial upgrade can leave these
+# at different versions; both are AMD packages from the same repo, so neither outranks
+# the other by provenance the way rocm-core outranks the distro's libhsa-runtime64-1.
+add_rpm_split_components() {
+ cat > "$_MOCK_DIR/rpm" <<MOCK
+#!/bin/sh
+for _a in "\$@"; do
+ case "\$_a" in
+ rocm-core) echo "$1" ;;
+ rocm-runtime) echo "$2" ;;
+ esac
+done
+exit 0
+MOCK
+ chmod +x "$_MOCK_DIR/rpm"
+}
+
add_wedged_rpm() {
# Stands in for `rpm -q` wedged on the rpmdb (stale BerkeleyDB __db locks on
# rpm < 4.16, i.e. RHEL 8 / SLES 15; the rpm 6.0.x deadlock against dnf).
@@ -503,14 +526,15 @@ assert_eq "the named override reaches this path -> rocm6.4" "$_BASE/rocm6.4" "$_
# ── 9. Every source missing: warn, do not die ───────────────────────────────
# rocminfo alone, a fresh AMD host with no ROCm userspace. Under set -e the whole
# detection must still succeed.
+# gfx1100 has its own repo.amd.com index, so since unslothai#8731 an unreadable version
+# routes on the arch rather than settling for CPU.
reset_sources
assert_eq "no version source at all -> cpu" "$_BASE/cpu" "$(run_index)"
assert_eq "no version source at all -> exit 0 under set -e" "0" "$(run_status_under_set_e)"
_warn=$(run_warnings)
-assert_contains "no-version host still reaches its actionable warning" \
- "no ROCm/HIP install was found" "$_warn"
-assert_contains "no-version warning still lists the detection sources" \
- "Minimum required for version detection" "$_warn"
+assert_contains "no-version host still reaches an actionable warning" \
+ "routing to AMD per-arch wheels" "$_warn"
+assert_contains "no-version warning names the arch it routed on" "gfx1100" "$_warn"
# 10. Sources present but every one of them unparseable: same contract.
reset_sources
@@ -519,8 +543,8 @@ add_hipconfig "unknown"
add_version_file "not-a-version"
assert_eq "unparseable sources -> cpu" "$_BASE/cpu" "$(run_index)"
assert_eq "unparseable sources -> exit 0 under set -e" "0" "$(run_status_under_set_e)"
-assert_contains "unparseable sources reach the no-version warning" \
- "no ROCm/HIP install was found" "$(run_warnings)"
+assert_contains "unparseable sources are treated as no version at all" \
+ "routing to AMD per-arch wheels" "$(run_warnings)"
# 11. A source reporting major 0 is garbage, not a version below every other.
reset_sources
@@ -594,6 +618,18 @@ else
"under 20s" "$((_t1 - _t0))s (outer bound fired)"
fi
+# A stale rocm-core beside a newer runtime must not decide the host's version. Ranking
+# the names by argument order read this box as ROCm 5.7 and sent a supported runtime to
+# CPU wheels, which is the failure #8731 is about, reached by a different route.
+reset_sources
+add_rpm_split_components "5.7.1" "6.4.1"
+assert_eq "a stale rocm-core beside a newer rocm-runtime resolves to the newer" \
+ "$_BASE/rocm6.4" "$(run_index)"
+reset_sources
+add_rpm_split_components "6.4.1" "5.7.1"
+assert_eq "and the ordering is by version, not by which name was queried first" \
+ "$_BASE/rocm6.4" "$(run_index)"
+
reset_sources
add_version_file "6.5.0-1"
assert_eq "a ROCm 6.5 host clipped to the rocm6.4 leaf still gets rocm-rel-6.5.0" \
diff --git a/tests/studio/install/test_rocm_gfx_spoof_7331.py b/tests/studio/install/test_rocm_gfx_spoof_7331.py
index 6de6b759f65..03e2dbf86e7 100644
--- a/tests/studio/install/test_rocm_gfx_spoof_7331.py
+++ b/tests/studio/install/test_rocm_gfx_spoof_7331.py
@@ -1001,15 +1001,29 @@ class TestConfirmedSpoofIsClearedBeforeLaunch:
"the clear must be guarded by the corroborated-spoof verdict, never "
"applied to a host whose override was not disproved"
)
- # Exactly one clear of the caller's own environment; the only other unset scopes
+ # Two clears of the caller's own environment, one per branch that installs native
+ # per-arch wheels: the Strix rocm* reroute above, and the no-version reroute, which
+ # produces a gfx* leaf and so never reaches this branch. The only other unset scopes
# three variables inside the re-probe's subshell. Every other branch keeps the
# override, which on generic wheels is what makes the GPU usable at all.
+ #
+ # The count alone is the weak half of this assertion, so each clear is also required
+ # to sit under a corroborated-spoof guard. Adding an unguarded third would keep the
+ # count honest and still strand a host whose override was never disproved.
_lasting = [
- ln.strip()
- for ln in source.splitlines()
+ i
+ for i, ln in enumerate(source.splitlines())
if ln.strip().startswith("unset HSA_OVERRIDE_GFX_VERSION")
]
- assert _lasting == ["unset HSA_OVERRIDE_GFX_VERSION"], _lasting
+ assert len(_lasting) == 2, _lasting
+ _lines = source.splitlines()
+ for _at in _lasting:
+ _guard = "\n".join(_lines[max(0, _at - 6) : _at])
+ assert "_spoof_physical" in _guard, (
+ f"the clear at line {_at + 1} is not guarded by a corroborated-spoof "
+ f"verdict; every clear must be, or a host whose override was not "
+ f"disproved loses the only thing making its GPU usable"
+ )
def _spoof_clear_guard() -> str:
diff --git a/tests/studio/install/test_rocm_support.py b/tests/studio/install/test_rocm_support.py
index da44dfdcbea..fd1a5564724 100644
--- a/tests/studio/install/test_rocm_support.py
+++ b/tests/studio/install/test_rocm_support.py
@@ -3016,7 +3016,15 @@ class TestInstallShStructure:
fn = _extract_sh_function_body(source, "get_torch_index_url")
probe_fn = _extract_sh_function_body(source, "_probe_amd_gfx_arch")
family_fn = _extract_sh_function_body(source, "_amd_arch_index_family_for_gfx")
- assert fn and probe_fn and family_fn
+ arch_fns = "\n".join(
+ _extract_sh_function_body(source, _n)
+ for _n in (
+ "_amd_probe_arches",
+ "_amd_agreed_index_family",
+ "_amd_sole_index_arch",
+ )
+ )
+ assert fn and probe_fn and family_fn and arch_fns
with tempfile.TemporaryDirectory() as d:
# uname -> Linux/x86_64 so the AMD branch runs on any dev host; the
# rocminfo/amd-smi shims enumerate nothing (KFD-only host).
@@ -3041,6 +3049,8 @@ class TestInstallShStructure:
+ "\n"
+ family_fn
+ "\n"
+ + arch_fns
+ + "\n"
+ fn
+ "\n"
"get_torch_index_url\n"
@@ -3101,6 +3111,14 @@ class TestInstallShStructure:
fn = _extract_sh_function_body(source, "get_torch_index_url")
probe_fn = _extract_sh_function_body(source, "_probe_amd_gfx_arch")
family_fn = _extract_sh_function_body(source, "_amd_arch_index_family_for_gfx")
+ arch_fns = "\n".join(
+ _extract_sh_function_body(source, _n)
+ for _n in (
+ "_amd_probe_arches",
+ "_amd_agreed_index_family",
+ "_amd_sole_index_arch",
+ )
+ )
# The version helpers must be extracted too: without them get_torch_index_url
# calls a missing command, the guarded assignment swallows the 127, and the
# no-version endpoint is reached for the wrong reason. Verified by mutation
@@ -3117,7 +3135,7 @@ class TestInstallShStructure:
"_detect_rocm_version_tag",
)
]
- assert fn and probe_fn and family_fn
+ assert fn and probe_fn and family_fn and arch_fns
assert all(version_fns), "ROCm version helpers not found in install.sh"
with tempfile.TemporaryDirectory() as d:
# Neutralise the host's real ROCm: the version chain reads
@@ -3152,6 +3170,8 @@ class TestInstallShStructure:
+ "\n"
+ family_fn
+ "\n"
+ + arch_fns
+ + "\n"
+ "\n".join(version_fns)
+ "\n"
+ fn
@@ -3197,12 +3217,23 @@ class TestInstallShStructure:
assert (
"falling back to CPU-only PyTorch" in r2.stderr
), f"an unmappable override must keep the CPU warning: {r2.stderr!r}"
- # Readable gfx, no override, no version: deliberate CPU fallback.
+ # unslothai#8731: gfx1151 has its own index, so the version only picks
+ # between generic rocmX.Y leaves and is a detection miss, not a decision.
r3 = run('echo " Name: gfx1151"\n')
assert r3.returncode == 0, f"readable-gfx case aborted: {r3.stderr}"
+ assert r3.stdout.strip().endswith("/cpu")
+ assert (
+ "falling back to CPU-only PyTorch" not in r3.stderr
+ ), f"a mapped arch must not get the CPU warning: {r3.stderr!r}"
+ assert (
+ "routing to AMD per-arch wheels" in r3.stderr
+ ), f"a mapped arch with no version defers to the reroute: {r3.stderr!r}"
+ # No per-arch index: picking a generic leaf needs a version.
+ r4 = run('echo " Name: gfx906"\n')
+ assert r4.returncode == 0, f"unmapped readable-gfx case aborted: {r4.stderr}"
assert (
- "falling back to CPU-only PyTorch" in r3.stderr
- ), f"a readable-gfx host without a version keeps the CPU warning: {r3.stderr!r}"
+ "falling back to CPU-only PyTorch" in r4.stderr
+ ), f"an unmapped arch without a version keeps the CPU warning: {r4.stderr!r}"
def test_reroute_gate_covers_kfd_only(self):
"""The runtime-less reroute must fire for a KFD-only host: _has_amd_rocm_gpu
@@ -3221,7 +3252,15 @@ class TestInstallShStructure:
)
assert block, "could not extract the runtime-less reroute block"
family_fn = _extract_sh_function_body(source, "_amd_arch_index_family_for_gfx")
- assert family_fn
+ arch_fns = "\n".join(
+ _extract_sh_function_body(source, _n)
+ for _n in (
+ "_amd_probe_arches",
+ "_amd_agreed_index_family",
+ "_amd_sole_index_arch",
+ )
+ )
+ assert family_fn and arch_fns
with tempfile.TemporaryDirectory() as d:
with open(os.path.join(d, "uname"), "w", encoding = "utf-8", newline = "\n") as f:
f.write('#!/bin/sh\ncase "${1:-}" in -m) echo x86_64 ;; *) echo Linux ;; esac\n')
@@ -3234,7 +3273,11 @@ class TestInstallShStructure:
f"_has_amd_rocm_gpu() {{ {gpu_stub}; }}\n"
f"_probe_amd_gfx_arch() {{ {probe_stub}; }}\n"
"_infer_linux_amd_gfx_arch() { echo gfx1100; }\n"
- "_strip_index_url_credentials() { printf '%s\\n' \"$1\"; }\n" + family_fn + "\n"
+ "_strip_index_url_credentials() { printf '%s\\n' \"$1\"; }\n"
+ + family_fn
+ + "\n"
+ + arch_fns
+ + "\n"
"_torch_index_pinned=false\nSKIP_TORCH=false\n_ARCH=x86_64\n"
"TORCH_INDEX_URL=https://download.pytorch.org/whl/cpu\n"
+ block.group(0)
@@ -3281,6 +3324,114 @@ class TestInstallShStructure:
"ROCm runtime not visible" in r3.stderr
), f"a truly runtime-invisible host keeps the original diagnostic: {r3.stderr!r}"
+ @staticmethod
+ def _family_for_probed(arch):
+ return {"gfx1151": "gfx1151", "gfx1100": "gfx110X-all"}.get(arch, "")
+
+ def test_no_version_reroute_routes_on_the_probed_arch(self):
+ """Routes on the arch rocminfo/amd-smi READ, not the lspci marketing-name
+ inference: the two disagree on a mixed APU + discrete host (unslothai#8731)."""
+ shell = shutil.which("bash")
+ if not shell:
+ pytest.skip("bash needed to execute the reroute block")
+ source = _INSTALL_SH_PATH.read_text(encoding = "utf-8")
+ block = re.search(
+ r'^if \[ "\$_torch_index_pinned" = false \] && \[ "\$SKIP_TORCH" = false \] && \\\n'
+ r".*?^fi\n",
+ source,
+ re.S | re.M,
+ )
+ assert block, "could not extract the runtime-less reroute block"
+ family_fn = _extract_sh_function_body(source, "_amd_arch_index_family_for_gfx")
+ arch_fns = "\n".join(
+ _extract_sh_function_body(source, _n)
+ for _n in (
+ "_amd_probe_arches",
+ "_amd_agreed_index_family",
+ "_amd_sole_index_arch",
+ )
+ )
+ assert family_fn and arch_fns
+ with tempfile.TemporaryDirectory() as d:
+ with open(os.path.join(d, "uname"), "w", encoding = "utf-8", newline = "\n") as f:
+ f.write('#!/bin/sh\ncase "${1:-}" in -m) echo x86_64 ;; *) echo Linux ;; esac\n')
+ os.chmod(os.path.join(d, "uname"), 0o755)
+
+ def run(
+ no_version_state,
+ probed_first,
+ probe_stub = "echo gfx1151",
+ probed_family = None,
+ inferred = "gfx1100",
+ override = "",
+ ):
+ script = (
+ "set -euo pipefail\n"
+ "_has_usable_nvidia_gpu() { return 1; }\n"
+ "_has_amd_rocm_gpu() { return 0; }\n"
+ f"_probe_amd_gfx_arch() {{ {probe_stub}; }}\n"
+ f"_infer_linux_amd_gfx_arch() {{ echo {inferred}; }}\n"
+ "_strip_index_url_credentials() { printf '%s\\n' \"$1\"; }\n"
+ + family_fn
+ + "\n"
+ + arch_fns
+ + "\n"
+ "_torch_index_pinned=false\nSKIP_TORCH=false\n_ARCH=x86_64\n"
+ f"_amd_no_rocm_version_reroute={no_version_state}\n"
+ f'_amd_probed_gfx_first="{probed_first}"\n'
+ f'_amd_probed_family="{probed_family if probed_family is not None else self._family_for_probed(probed_first)}"\n'
+ "TORCH_INDEX_URL=https://download.pytorch.org/whl/cpu\n"
+ + block.group(0)
+ + 'printf "URL:%s GFX:%s\\n" "$TORCH_INDEX_URL" "${UNSLOTH_ROCM_GFX_ARCH:-}"\n'
+ )
+ sp = os.path.join(d, "reroute_probe.sh")
+ with open(sp, "w", encoding = "utf-8", newline = "\n") as f:
+ f.write(script)
+ env = dict(os.environ, PATH = d + os.pathsep + os.environ.get("PATH", ""))
+ for var in ("UNSLOTH_ROCM_GFX_ARCH", "UNSLOTH_AMD_ROCM_MIRROR"):
+ env.pop(var, None)
+ # Gate's first disjunct: unset, an override case tests nothing.
+ if override:
+ env["UNSLOTH_ROCM_GFX_ARCH"] = override
+ return subprocess.run(
+ [shell, sp.replace("\\", "/")], env = env, capture_output = True, text = True
+ )
+
+ r = run("true", "gfx1151")
+ assert r.returncode == 0, f"no-version reroute aborted: {r.stderr}"
+ assert (
+ "URL:https://repo.amd.com/rocm/whl/gfx1151/ GFX:gfx1151" in r.stdout
+ ), f"the probed arch must win over lspci inference here: {r.stdout!r}"
+ # A same-family pair must export no arch: setup.sh prefers
+ # UNSLOTH_ROCM_GFX_ARCH and would build llama.cpp for a guessed card.
+ r_pair = run("true", "", probed_family = "gfx120X-all")
+ assert r_pair.returncode == 0, f"same-family pair aborted: {r_pair.stderr}"
+ assert (
+ "URL:https://repo.amd.com/rocm/whl/gfx120X-all/ GFX:\n" in r_pair.stdout
+ ), f"a same-family pair routes but must not export a card: {r_pair.stdout!r}"
+ r2 = run("false", "", probe_stub = "printf '\\n'")
+ assert r2.returncode == 0, f"inferred-arch case aborted: {r2.stderr}"
+ assert (
+ "URL:https://repo.amd.com/rocm/whl/gfx110X-all/ GFX:gfx1100" in r2.stdout
+ ), f"an empty-probe reroute keeps the inferred arch: {r2.stdout!r}"
+ r3 = run("false", "")
+ assert r3.returncode == 0, f"deliberate-fallback case aborted: {r3.stderr}"
+ assert (
+ "URL:https://download.pytorch.org/whl/cpu GFX:" in r3.stdout
+ ), f"a deliberate CPU fallback must stay un-rerouted: {r3.stdout!r}"
+ # HIP reports gfx1201:sramecc+:xnack- and the index case table has no arm
+ # for the suffix, so it cost the reroute get_torch_index_url promised.
+ r4 = run(
+ "false",
+ "",
+ inferred = "gfx1201:sramecc+:xnack-",
+ override = "gfx1201:sramecc+:xnack-",
+ )
+ assert r4.returncode == 0, f"suffixed override aborted: {r4.stderr}"
+ assert (
+ "URL:https://repo.amd.com/rocm/whl/gfx120X-all/ GFX:gfx1201" in r4.stdout
+ ), f"a gcnArchName suffix must not cost the reroute: {r4.stdout!r}"
+
def test_get_torch_index_url_uses_nvidia_detected_flag(self):
"""get_torch_index_url must track NVIDIA via _nvidia_detected (proc-only NVIDIA still picks CUDA)."""
sh_path = PACKAGE_ROOT / "install.sh"
diff --git a/tests/studio/install/test_unsupported_arch_routing_guards_8529.py b/tests/studio/install/test_unsupported_arch_routing_guards_8529.py
index cd9d95df2bb..c2e7c99d882 100644
--- a/tests/studio/install/test_unsupported_arch_routing_guards_8529.py
+++ b/tests/studio/install/test_unsupported_arch_routing_guards_8529.py
@@ -241,6 +241,12 @@ def _run_sh_get_torch_index_url(arch: str, rocm_tag: str = "") -> "tuple[str, st
script = (
_sh_function_body(src, "_amd_arch_index_family_for_gfx")
+ "\n"
+ + _sh_function_body(src, "_amd_probe_arches")
+ + "\n"
+ + _sh_function_body(src, "_amd_agreed_index_family")
+ + "\n"
+ + _sh_function_body(src, "_amd_sole_index_arch")
+ + "\n"
+ _sh_function_body(src, "get_torch_index_url")
+ "\n"
+ _INDEX_URL_STUBS
@@ -613,6 +619,9 @@ def _run_summary_guard(tmp_path, lspci_lines: "list[str]") -> str:
"_infer_linux_unsupported_amd_gfx_arch",
"_infer_linux_amd_gfx_arch",
"_amd_arch_index_family_for_gfx",
+ "_amd_probe_arches",
+ "_amd_agreed_index_family",
+ "_amd_sole_index_arch",
"_amd_gpu_present_via_pci",
)
)