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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Linux: resolve the ROCm version from every source, not the first that answers (#8414)
Daniel Han3fc67dcad647
diff --git a/install.sh b/install.shindex a966c2d5308..d0c0c93c108 100755--- a/install.sh+++ b/install.sh@@ -2909,6 +2909,114 @@ _cap_cuda_family_for_pre_turing() {printf '%s\n' "$1"}+# ── ROCm version sources ──+# One helper per source, each printing at most one "rocmX.Y" line, and each+# returning 0 unconditionally: install.sh runs under set -e, so a real AMD GPU+# with no ROCm userspace at all has to reach the actionable warning at the end of+# the ROCm branch rather than have a failing source kill the installer.+_rocm_tag_from_amd_smi() {+ command -v amd-smi >/dev/null 2>&1 || return 0+ # Cut at the field separator and require digits: the line is pipe-delimited+ # ("... | ROCm version: N/A | amdgpu version: 6.10.10 | ..."), so stripping every+ # non-digit fabricated rocm6.10 out of the amdgpu driver version. Position used to+ # hide that; under highest-wins a fabricated reading outvotes a real 6.1.+ amd-smi version 2>/dev/null | awk -F'ROCm version: ' \+ 'NF>1{v=$2; sub(/[ \t|].*$/, "", v); if (v ~ /^[0-9]+\.[0-9]+/) {split(v,a,"."); print "rocm"a[1]"."a[2]} exit}' || return 0+}++_rocm_tag_from_version_file() {+ [ -r /opt/rocm/.info/version ] || return 0+ awk -F. '{print "rocm"$1"."$2; exit}' /opt/rocm/.info/version || return 0+}++_rocm_tag_from_hipconfig() {+ command -v hipconfig >/dev/null 2>&1 || return 0+ 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+}++_rocm_tag_from_dpkg() {+ command -v dpkg-query >/dev/null 2>&1 || return 0+ # Require the status word "installed". dpkg-query -W lists every package in the+ # status database except purged ones, so a rocm-core taken out with `apt remove`+ # stays queryable in state "deinstall ok config-files" still reporting the version+ # it had, and under highest-wins that dead entry outranks every live source (ran+ # 7.0, downgraded to 6.1, never purged -> rocm7.0 off a tree that is gone).+ # ${Status} over ${db:Status-Status}: documented showformat field with no dpkg+ # version floor, and dpkg renders an unrecognised field as empty rather than+ # failing, so a dpkg lacking it goes silent instead of over-reporting.+ _rt_ver=$(dpkg-query -W -f='${Status} ${Version}\n' rocm-core 2>/dev/null \+ | awk '$3 == "installed" && $4 != "" { print $4; exit }') || return 0+ [ -n "$_rt_ver" ] || return 0+ printf '%s\n' "$_rt_ver" | sed 's/^[0-9]*://' \+ | awk -F'[.-]' '{print "rocm"$1"."$2; exit}' || return 0+}++_rocm_tag_from_rpm() {+ command -v rpm >/dev/null 2>&1 || 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.+ _rt_ver=$(_run_bounded rpm -q --qf '%{VERSION}\n' rocm-core 2>/dev/null) || return 0+ [ -n "$_rt_ver" ] || return 0+ printf '%s\n' "$_rt_ver" | awk -F'[.-]' '{print "rocm"$1"."$2; exit}' || return 0+}++# Highest "rocmX.Y" line on stdin (major >= 1), or nothing when no line is usable.+_highest_rocm_tag() {+ awk '+ /^rocm[0-9]+\.[0-9]+$/ {+ split(substr($0, 5), a, ".")+ maj = a[1] + 0; min = a[2] + 0+ if (maj < 1) next+ if (!seen || maj > best_maj || (maj == best_maj && min > best_min)) {+ best_maj = maj; best_min = min; seen = 1+ }+ }+ END { if (seen) printf "rocm%d.%d\n", best_maj, best_min }+ '+}++# Consult EVERY source and take the highest, not the first that answers. Distros+# with split ROCm packaging ship one component well behind the runtime the GPU+# actually uses: Debian 13 (and Linux Mint on top of it) packages hipconfig at+# 5.7.x next to a 6.1.x rocminfo/HSA, so first-answer resolution reported rocm5.7+# on a working gfx1100 and the 6.0+ gate below sent it to CPU-only wheels (issue+# #8402). A source reading lower than another on the same host is stale packaging,+# not a downgrade. The symmetric risk, overshoot, is bounded: PyTorch's ROCm wheels+# vendor their own userspace and need only an amdgpu/KFD driver AMD documents as+# compatible +/- 2 releases, the normalisation below can only emit a leaf PyTorch+# publishes, the one source that can report a tree that is not installed is+# filtered above, and any disagreement is named on stderr for the install log.+_detect_rocm_version_tag() {+ _rt_readings=$({+ _rocm_tag_from_amd_smi+ _rocm_tag_from_version_file+ _rocm_tag_from_hipconfig+ _rocm_tag_from_dpkg+ _rocm_tag_from_rpm+ } 2>/dev/null) || _rt_readings=""+ _rt_best=$(printf '%s\n' "$_rt_readings" | _highest_rocm_tag) || _rt_best=""+ if [ -n "$_rt_best" ]; then+ # Same shape gate as _highest_rocm_tag: a reading that was never a candidate+ # must not be named as a dissenting opinion.+ _rt_seen=$(printf '%s\n' "$_rt_readings" \+ | grep '^rocm[1-9][0-9]*\.[0-9][0-9]*$' | sort -u | tr '\n' ' ') || _rt_seen=""+ case "$_rt_seen" in+ ""|"$_rt_best ") : ;; # one reading, or every source agreeing+ *) echo "[WARN] ROCm version sources disagree (${_rt_seen% }) -- using the highest, $_rt_best." >&2 ;;+ esac+ fi+ printf '%s\n' "$_rt_best"+}+# ── Detect GPU and choose PyTorch index URL ──# Mirrors Get-TorchIndexUrl in install.ps1.# On CPU-only machines this returns the cpu index, avoiding the solver@@ -2987,26 +3095,14 @@ get_torch_index_url() {fi# AMD GPU confirmed -- detect ROCm version_rocm_tag=""- _rocm_tag=$({ command -v amd-smi >/dev/null 2>&1 && \- amd-smi version 2>/dev/null | awk -F'ROCm version: ' \- 'NF>1{gsub(/[^0-9.]/, "", $2); split($2,a,"."); print "rocm"a[1]"."a[2]; ok=1; exit} END{exit !ok}'; } || \- { [ -r /opt/rocm/.info/version ] && \- awk -F. '{print "rocm"$1"."$2; exit}' /opt/rocm/.info/version; } || \- { command -v hipconfig >/dev/null 2>&1 && \- hipconfig --version 2>/dev/null | awk 'NR==1 && /^[0-9]/{split($1,a,"."); if(a[1]+0>0){print "rocm"a[1]"."a[2]; found=1}} END{exit !found}'; } || \- { command -v dpkg-query >/dev/null 2>&1 && \- ver="$(dpkg-query -W -f='${Version}\n' rocm-core 2>/dev/null)" && \- [ -n "$ver" ] && \- printf '%s\n' "$ver" | sed 's/^[0-9]*://' | awk -F'[.-]' '{print "rocm"$1"."$2; exit}'; } || \- { command -v rpm >/dev/null 2>&1 && \- ver="$(rpm -q --qf '%{VERSION}\n' rocm-core 2>/dev/null)" && \- [ -n "$ver" ] && \- printf '%s\n' "$ver" | awk -F'[.-]' '{print "rocm"$1"."$2; exit}'; }) 2>/dev/null || _rocm_tag=""- # ^ || guard: when EVERY version source is missing (e.g. rocminfo present- # but rocm-core not installed, so dpkg-query/rpm exit 1), the whole ||- # chain fails and set -e would kill the installer BEFORE the actionable- # no-version WARN below -- exactly the fresh-install case it exists for.- # Validate _rocm_tag: must match "rocmX.Y" with major >= 1+ _rocm_tag=$(_detect_rocm_version_tag) || _rocm_tag=""+ # ^ || guard: belt and braces on the set -e contract the helpers hold, so a+ # fresh AMD host with no version source at all still reaches the actionable+ # no-version WARN below. stderr is deliberately not redirected: each source+ # already silences its own noise, leaving only the sources-disagree+ # breadcrumb, which belongs in the install log.+ # Shape gate on "rocmX.Y" with major >= 1: _highest_rocm_tag enforces it too,+ # kept so a future source cannot leak garbage into the cases below.case "$_rocm_tag" inrocm[1-9]*.[0-9]*) : ;; # valid (major >= 1)*) _rocm_tag="" ;; # reject malformed (empty, garbled, or major=0)@@ -3016,7 +3112,11 @@ get_torch_index_url() {case "$_rocm_tag" inrocm[1-5].*)echo "[WARN] ROCm $_rocm_tag detected but PyTorch ROCm wheels require ROCm 6.0+ -- falling back to CPU-only PyTorch" >&2+ echo "[WARN] $_rocm_tag is the HIGHEST version any of amd-smi, /opt/rocm/.info/version, hipconfig or rocm-core reported." >&2echo "[WARN] Upgrade ROCm: https://rocm.docs.amd.com/en/latest/deploy/linux/index.html" >&2+ echo "[WARN] If this host really runs ROCm 6.0+ and only its packaging says otherwise, pin the wheels and re-run:" >&2+ echo "[WARN] UNSLOTH_TORCH_INDEX_FAMILY=rocm6.4 (a PyTorch wheel leaf: rocm6.0-6.4, rocm7.0-7.2)" >&2+ echo "[WARN] UNSLOTH_TORCH_INDEX_URL=<full index URL> (takes precedence, used verbatim)" >&2echo "$_base/cpu"; return ;;esac# Supported tags; 6.5+ clips to rocm6.4, 7.3+ caps to rocm7.2.diff --git a/tests/sh/test_get_torch_index_url.sh b/tests/sh/test_get_torch_index_url.shindex 0d51e6c21f1..88615153e1f 100755--- a/tests/sh/test_get_torch_index_url.sh+++ b/tests/sh/test_get_torch_index_url.sh@@ -53,6 +53,22 @@ _FAKE_ROCM_DIR=$(mktemp -d)echo ""sed -n '/^_cap_cuda_family_for_pre_turing()/,/^}/p' "$INSTALL_SH"echo ""+ # Same sync rule as the gfx helpers above: miss one of the version sources or+ # the resolver they feed and the ROCm branch silently returns the CPU index.+ 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|/opt/rocm|$_FAKE_ROCM_DIR|g" \diff --git a/tests/sh/test_rocm_version_source_disagreement.sh b/tests/sh/test_rocm_version_source_disagreement.shnew file mode 100755index 00000000000..d407e603cb5--- /dev/null+++ b/tests/sh/test_rocm_version_source_disagreement.sh@@ -0,0 +1,526 @@+#!/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 ROCm version resolution in install.sh (issue #8402).+#+# Debian 13 (and Linux Mint on top of it) packages hipconfig at 5.7.x next to a+# 6.1.x rocminfo/HSA runtime. install.sh used to take the FIRST version source+# that answered, so it resolved rocm5.7, the "requires ROCm 6.0+" gate fired, and+# a working RX 7900 XTX (gfx1100) got CPU-only torch. Detection now reads EVERY+# source and takes the highest. Pinned here: a stale low source never shadows a+# higher one from any position; a genuine 5.x host still falls back to CPU; the+# sub-6.0 warning names the documented override; every source missing still warns+# without killing the installer (set -e); tag normalisation is unchanged.+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)+# The version sources, _ensure_rocm_probe_env's PATH append and the NVIDIA+# fallback all read absolute host paths, which would leak the test host's own+# ROCm or NVIDIA into every assertion. Redirect all three prefixes into empty+# temp dirs so the suite is hermetic on a ROCm, NVIDIA or bare box alike.+_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 '/^_amd_arch_index_family_for_gfx()/,/^}/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_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"++# Guard the extraction: a renamed helper would otherwise make every ROCm assertion+# below fail as a plain "cpu" with no hint why.+for _fn in _rocm_tag_from_amd_smi _rocm_tag_from_version_file _rocm_tag_from_hipconfig \+ _rocm_tag_from_dpkg _rocm_tag_from_rpm _highest_rocm_tag \+ _detect_rocm_version_tag 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++# Minimal tool set: no amd-smi, hipconfig, dpkg-query, rpm or rocminfo unless a+# scenario supplies one, so an unrelated host package cannot answer a probe.+# `timeout` and `sleep` must stay in it for case 14: _run_bounded looks up+# `timeout` on PATH, so dropping it silently turns the bounded probe back into an+# unbounded one and that case passes for the wrong reason.+_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+}++# ── Scenario builders ───────────────────────────────────────────────────────+# Every scenario starts from a clean mock dir and an empty fake /opt/rocm.+_MOCK_DIR=$(mktemp -d)++reset_sources() {+ rm -rf "$_MOCK_DIR" "$_FAKE_ROCM_DIR/.info"+ _MOCK_DIR=$(mktemp -d)+ # rocminfo is what makes this an AMD host at all: without a gfx name the ROCm+ # branch bails before any version source is consulted. gfx1100 = RX 7900 XTX,+ # the card in the report.+ cat > "$_MOCK_DIR/rocminfo" <<'MOCK'+#!/bin/sh+cat <<'ROCMINFO'+*******+Agent 2+*******+ Name: gfx1100+ Marketing Name: AMD Radeon RX 7900 XTX+ Device Type: GPU+ROCMINFO+MOCK+ chmod +x "$_MOCK_DIR/rocminfo"+}++# $1 = version string printed by "hipconfig --version" (e.g. 5.7.31921-0)+add_hipconfig() {+ cat > "$_MOCK_DIR/hipconfig" <<MOCK+#!/bin/sh+echo "$1"+MOCK+ chmod +x "$_MOCK_DIR/hipconfig"+}++# $1 = contents of /opt/rocm/.info/version (e.g. 6.1.2-98)+add_version_file() {+ mkdir -p "$_FAKE_ROCM_DIR/.info"+ printf '%s\n' "$1" > "$_FAKE_ROCM_DIR/.info/version"+}++# $1 = ROCm version reported by "amd-smi version"+add_amd_smi() {+ cat > "$_MOCK_DIR/amd-smi" <<MOCK+#!/bin/sh+case "\$1" in+ list) printf 'GPU: 0\\n BDF: 0000:03:00.0\\n NAME: gfx1100\\n' ;;+ *) echo "AMDSMI Tool: 25.0.1 | AMDSMI Library version: 25.0.1.0 | ROCm version: $1" ;;+esac+MOCK+ chmod +x "$_MOCK_DIR/amd-smi"+}++# $1 = the raw value of the "ROCm version:" field, e.g. "6.4.0" or "N/A". Unlike+# add_amd_smi this reproduces the WHOLE line, amdgpu driver version and all, so a+# parser that runs past the field separator is caught.+add_amd_smi_line() {+ cat > "$_MOCK_DIR/amd-smi" <<MOCK+#!/bin/sh+case "\$1" in+ list) printf 'GPU: 0\\n BDF: 0000:03:00.0\\n NAME: gfx1100\\n' ;;+ *) echo "AMDSMI Tool: 24.7.1+b446d6c-dirty | AMDSMI Library version: 24.7.2.0 | ROCm version: $1 | amdgpu version: 6.10.10 | hsmp version: 2.2" ;;+esac+MOCK+ chmod +x "$_MOCK_DIR/amd-smi"+}++# $1 = rocm-core version as dpkg reports it (epoch prefixes allowed, e.g. 1:6.2.4-1)+# $2 = dpkg status word, default "installed". "config-files" is where `apt remove`+# without `apt purge` leaves a package: still in /var/lib/dpkg/status, still+# carrying its old version, and still reported by `dpkg-query -W`.+# The mock renders the showformat string it is given rather than a bare version, so+# these assertions test how install.sh ASKS dpkg, not just how it parses the answer.+add_dpkg_rocm_core() {+ printf '%s\n' "$1" > "$_MOCK_DIR/.dpkg-version"+ printf '%s\n' "${2:-installed}" > "$_MOCK_DIR/.dpkg-status"+ cat > "$_MOCK_DIR/dpkg-query" <<'MOCK'+#!/bin/sh+_d=${0%/*}+_ver=$(cat "$_d/.dpkg-version")+_status=$(cat "$_d/.dpkg-status")+# Status is "<want> <error-flag> <status>": removed but not purged reads "deinstall ok config-files".+case "$_status" in installed) _want=install ;; *) _want=deinstall ;; esac+_fmt=''+_found=''+while [ $# -gt 0 ]; do+ case "$1" in+ -f=*) _fmt=${1#-f=} ;;+ --showformat=*) _fmt=${1#--showformat=} ;;+ -f|--showformat) shift; _fmt=$1 ;;+ -*) : ;;+ rocm-core) _found=1 ;;+ esac+ shift+done+[ -n "$_found" ] || exit 1+[ -n "$_fmt" ] || _fmt='${Package}\t${Version}\n'+# Unknown fields render empty, which is what real dpkg-query does.+_out=$(printf '%s' "$_fmt" | sed \+ -e "s|\${Package}|rocm-core|g" \+ -e "s|\${Status}|$_want ok $_status|g" \+ -e "s|\${db:Status-Status}|$_status|g" \+ -e "s|\${db:Status-Want}|$_want|g" \+ -e "s|\${db:Status-Eflag}|ok|g" \+ -e "s|\${Version}|$_ver|g" \+ -e "s|\${[^}]*}||g")+printf "$_out"+MOCK+ chmod +x "$_MOCK_DIR/dpkg-query"+}++# $1 = rocm-core version as rpm reports it+add_rpm_rocm_core() {+ cat > "$_MOCK_DIR/rpm" <<MOCK+#!/bin/sh+for _a in "\$@"; do+ case "\$_a" in rocm-core) echo "$1"; exit 0 ;; esac+done+exit 1+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).+ # Sleeps rather than really wedging so the suite stays hermetic and killable.+ cat > "$_MOCK_DIR/rpm" <<MOCK+#!/bin/sh+sleep 30+MOCK+ chmod +x "$_MOCK_DIR/rpm"+}++# run_index under an OUTER bound: an unbounded probe fails this in $1 seconds+# instead of hanging the suite.+run_index_outer_bounded() {+ PATH="$_MOCK_DIR:$_TOOLS_DIR" timeout "$1" bash -c \+ "unset CUDA_VISIBLE_DEVICES UNSLOTH_ROCM_GFX_ARCH UNSLOTH_TORCH_INDEX_URL UNSLOTH_TORCH_INDEX_FAMILY+ _ARCH=x86_64; . '$_FUNC_FILE'; get_torch_index_url" 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+ _ARCH=x86_64; . '$_FUNC_FILE'; get_torch_index_url" 2>/dev/null+}++# Same, but returns the warnings instead of the index, on one line.+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+ _ARCH=x86_64; . '$_FUNC_FILE'; get_torch_index_url" 2>&1 >/dev/null | tr '\n' ' '+}++# Same, under `set -e` like the real installer, reporting only the exit status:+# with every source missing, detection must return empty AND succeed or the+# installer dies before the actionable warning.+run_status_under_set_e() {+ PATH="$_MOCK_DIR:$_TOOLS_DIR" bash -c \+ "set -e+ unset CUDA_VISIBLE_DEVICES UNSLOTH_ROCM_GFX_ARCH UNSLOTH_TORCH_INDEX_URL UNSLOTH_TORCH_INDEX_FAMILY+ _ARCH=x86_64; . '$_FUNC_FILE'; get_torch_index_url >/dev/null 2>&1; echo \$?" \+ 2>/dev/null+}++_BASE="https://download.pytorch.org/whl"++echo "=== test_rocm_version_source_disagreement ==="++# ── 1. The reported host ────────────────────────────────────────────────────+# No /opt/rocm/.info/version, hipconfig from the distro's 5.7 packaging, rocm-core+# at 6.1 in dpkg. Before the fix hipconfig answered first and this returned cpu.+reset_sources+add_hipconfig "5.7.31921-0"+add_dpkg_rocm_core "1:6.1.2-1"+assert_eq "Debian 13 hipconfig 5.7 + rocm-core 6.1 -> rocm6.1" "$_BASE/rocm6.1" "$(run_index)"+_warn=$(run_warnings)+case "$_warn" in+ *"require ROCm 6.0+"*) assert_eq "the same host emits no 6.0+ gate warning" "" "$_warn" ;;+ *) assert_eq "the same host emits no 6.0+ gate warning" "ok" "ok" ;;+esac+# The breadcrumb that makes a wrong-HIGH reading diagnosable from an install log.+assert_contains "disagreeing sources are named" "sources disagree (rocm5.7 rocm6.1)" "$_warn"+assert_contains "the winning reading is named" "using the highest, rocm6.1" "$_warn"++# 2. Same shape from /opt/rocm/.info/version. This one already worked by position,+# so it is here to pin that the rewrite did not lose it.+reset_sources+add_hipconfig "5.7.31921-0"+add_version_file "6.1.2-98"+assert_eq "hipconfig 5.7 + version file 6.1 -> rocm6.1" "$_BASE/rocm6.1" "$(run_index)"++# 3. And from rpm, the last source in the order.+reset_sources+add_hipconfig "5.7.31921-0"+add_rpm_rocm_core "6.3.0"+assert_eq "hipconfig 5.7 + rocm-core 6.3 (rpm) -> rocm6.3" "$_BASE/rocm6.3" "$(run_index)"++# 4. Not only a floor problem: amd-smi answering first with a lower version must+# not shadow a newer runtime either.+reset_sources+add_amd_smi "6.1.0"+add_dpkg_rocm_core "6.4.1-1"+assert_eq "amd-smi 6.1 + rocm-core 6.4 -> rocm6.4 (highest, not first)" "$_BASE/rocm6.4" "$(run_index)"++# 5. Agreement is unchanged: all sources on 6.4 still resolve 6.4.+reset_sources+add_amd_smi "6.4.0"+add_version_file "6.4.0-1"+add_hipconfig "6.4.43482-0"+assert_eq "all sources agree on 6.4 -> rocm6.4" "$_BASE/rocm6.4" "$(run_index)"+assert_eq "agreeing sources emit no disagreement breadcrumb" "" "$(run_warnings)"++# ── 5b. Overshoot: highest-wins must not let a DEAD source pick the wheels ──+# Undershoot lands on CPU wheels, which work; overshoot installs wheels the runtime+# cannot load. The one host state that can manufacture a wrong-HIGH reading is+# dpkg's: `dpkg-query -W` reports packages left in "deinstall ok config-files" by+# `apt remove` without a purge, still carrying the version they had, so a box that+# ran ROCm 7.0 and went back to 6.1 offers a 7.0 that outranks every honest source.+# Detection must require the status word "installed".+reset_sources+add_hipconfig "6.1.40093-0"+add_dpkg_rocm_core "1:7.0.0-1" config-files+assert_eq "config-files rocm-core 7.0 on a 6.1 host -> rocm6.1, not rocm7.0" \+ "$_BASE/rocm6.1" "$(run_index)"+assert_eq "the dead dpkg entry is not even named as a disagreement" "" "$(run_warnings)"++# 5c. The live entry still has to win, or the fix for the reported bug is gone.+# This is what makes 5b non-vacuous: only the dpkg status word differs.+reset_sources+add_hipconfig "5.7.31921-0"+add_dpkg_rocm_core "1:6.1.2-1" installed+assert_eq "installed rocm-core 6.1 still beats hipconfig 5.7 -> rocm6.1" \+ "$_BASE/rocm6.1" "$(run_index)"++# 5d. The states an interrupted dpkg operation leaves behind are not "installed"+# either, and none of them describes a runtime the GPU can use.+for _dead in config-files half-installed unpacked half-configured; do+ reset_sources+ add_hipconfig "6.1.40093-0"+ add_dpkg_rocm_core "1:7.2.0-1" "$_dead"+ assert_eq "dpkg state '$_dead' at 7.2 does not select wheels -> rocm6.1" \+ "$_BASE/rocm6.1" "$(run_index)"+done++# ── 5e. A stale-HIGH reading in each source position, one at a time ─────────+# dpkg's was the only source with a documented over-reporting state: rpm drops a+# version on erase, .info/version belongs to whichever tree /opt/rocm resolves to,+# and amd-smi and hipconfig report the userspace they were run from. For those four+# a HIGH reading is deliberately taken as truth, bounded by the tag normalisation+# (never an index leaf PyTorch does not publish) and auditable from the log.+for _pos in amd-smi version-file hipconfig rpm; do+ reset_sources+ add_hipconfig "6.1.40093-0"+ case "$_pos" in+ amd-smi) add_amd_smi "9.9.0" ;;+ version-file) add_version_file "9.9.0-1" ;;+ hipconfig) add_hipconfig "9.9.31921-0" ;;+ rpm) add_rpm_rocm_core "9.9.0" ;;+ esac+ # Clipped to the newest leaf PyTorch actually publishes, never rocm9.9.+ assert_eq "a 9.9 reading from $_pos is capped to the newest published leaf" \+ "$_BASE/rocm7.2" "$(run_index)"+ if [ "$_pos" != "hipconfig" ]; then+ assert_contains "a 9.9 reading from $_pos is recorded in the log" \+ "sources disagree (rocm6.1 rocm9.9) -- using the highest, rocm9.9" "$(run_warnings)"+ fi+done++# 5f. dpkg in that same position: an INSTALLED high reading is the runtime, so the+# cap applies rather than a rejection.+reset_sources+add_hipconfig "6.1.40093-0"+add_dpkg_rocm_core "1:9.9.0-1" installed+assert_eq "an installed 9.9 rocm-core is capped, not rejected" "$_BASE/rocm7.2" "$(run_index)"++# ── 6. A genuine ROCm 5.x host still falls back to CPU ──────────────────────+reset_sources+add_hipconfig "5.7.31921-0"+add_version_file "5.7.1-90"+assert_eq "genuine ROCm 5.7 everywhere -> cpu" "$_BASE/cpu" "$(run_index)"+_warn=$(run_warnings)+assert_contains "5.x host warns about the 6.0+ requirement" "require ROCm 6.0+" "$_warn"+assert_contains "5.x warning names the resolved tag" "ROCm rocm5.7 detected" "$_warn"+assert_contains "5.x warning says it took the highest reading" "HIGHEST version" "$_warn"++# 7. Both overrides return early from get_torch_index_url, before any GPU probing,+# so naming them in the warning is honest.+assert_contains "5.x warning names UNSLOTH_TORCH_INDEX_FAMILY" \+ "UNSLOTH_TORCH_INDEX_FAMILY=rocm6.4" "$_warn"+assert_contains "5.x warning names UNSLOTH_TORCH_INDEX_URL" \+ "UNSLOTH_TORCH_INDEX_URL=" "$_warn"++# 8. And the override it names has to actually work on this same host.+reset_sources+add_hipconfig "5.7.31921-0"+add_version_file "5.7.1-90"+_result=$(PATH="$_MOCK_DIR:$_TOOLS_DIR" bash -c \+ "unset CUDA_VISIBLE_DEVICES UNSLOTH_ROCM_GFX_ARCH UNSLOTH_TORCH_INDEX_URL+ export UNSLOTH_TORCH_INDEX_FAMILY=rocm6.4+ _ARCH=x86_64; . '$_FUNC_FILE'; get_torch_index_url" 2>/dev/null)+assert_eq "the named override reaches this path -> rocm6.4" "$_BASE/rocm6.4" "$_result"++# ── 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.+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"++# 10. Sources present but every one of them unparseable: same contract.+reset_sources+add_amd_smi "N/A"+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)"++# 11. A source reporting major 0 is garbage, not a version below every other.+reset_sources+add_hipconfig "0.0.0"+add_version_file "6.2.0-1"+assert_eq "major-0 source ignored, 6.2 wins -> rocm6.2" "$_BASE/rocm6.2" "$(run_index)"++# ── 12. Supported-tag normalisation is unchanged ────────────────────────────+# PyTorch publishes major.minor index leaves only, so patch levels normalise;+# 6.5+ clips to the last 6.x wheel set and 7.3+ caps to the latest known.+for _case in "6.0.2:rocm6.0" "6.1.3:rocm6.1" "6.2.4:rocm6.2" "6.3.1:rocm6.3" \+ "6.4.1:rocm6.4" "7.0.1:rocm7.0" "7.1.0:rocm7.1" "7.2.1:rocm7.2" \+ "6.5.0:rocm6.4" "6.9.0:rocm6.4" "7.3.0:rocm7.2" "8.0.0:rocm7.2"; do+ _ver="${_case%%:*}"+ _want="${_case##*:}"+ reset_sources+ add_amd_smi "$_ver"+ assert_eq "amd-smi $_ver -> $_want" "$_BASE/$_want" "$(run_index)"+done++# ── 13. amd-smi reports one pipe-delimited line, and the ROCm field can be N/A ──+# The amdgpu driver version follows the ROCm one on the same line, so reading the+# field without stopping at the separator glued them together (N/A + amdgpu 6.10.10+# -> "rocm6.10"). Position used to hide that; under highest-wins a fabricated+# reading outvotes a correct source, so an unparseable field must yield nothing.+for _case in "N/A:" "6.4.0:rocm6.4" "7.0.2:rocm7.0" ":"; do+ _field="${_case%%:*}"+ _want="${_case##*:}"+ reset_sources+ add_amd_smi_line "$_field"+ if [ -n "$_want" ]; then+ assert_eq "amd-smi full line, ROCm field '$_field' -> $_want" \+ "$_BASE/$_want" "$(run_index)"+ else+ # Nothing else answers, so an unusable field must reach the CPU fallback.+ assert_eq "amd-smi full line, ROCm field '$_field' -> no reading" \+ "$_BASE/cpu" "$(run_index)"+ fi+done++# The driver version must never win the vote over a real, lower ROCm reading.+reset_sources+add_amd_smi_line "N/A"+add_version_file "6.1.3-42"+assert_eq "amd-smi N/A beside amdgpu 6.10 does not outvote a real 6.1" \+ "$_BASE/rocm6.1" "$(run_index)"++# ── 14. A wedged rpmdb must not hang the installer ─────────────────────────+# Highest-wins short-circuits nothing, so `rpm -q` now always runs where it used to+# be LAST in a first-answer chain and /opt/rocm/.info/version answered ahead of it+# on any normal RHEL/SLES install. Alone among the sources it can block forever on+# the rpmdb (stale BerkeleyDB __db locks on rpm < 4.16; the rpm 6.0.x read-lock+# deadlock against dnf), and an installer that hangs is worse than one that+# mis-detects, so this probe is bounded and a timed-out source declines to answer.+reset_sources+add_version_file "6.4.0-1"+add_wedged_rpm+_t0=$(date +%s)+# `|| _res=""` so the outer bound firing is a FAIL below rather than taking the+# suite down through set -e with no verdict printed.+_res=$(run_index_outer_bounded 20) || _res=""+_t1=$(date +%s)+# The wedged source must not take the answer down with it. Empty here means the+# outer bound fired, i.e. the rpm probe ran unbounded.+assert_eq "a wedged rpm does not stop the version file resolving the host" \+ "$_BASE/rocm6.4" "$_res"+if [ "$((_t1 - _t0))" -lt 20 ]; then+ assert_eq "the rpm probe is bounded, not left to block the installer" "ok" "ok"+else+ assert_eq "the rpm probe is bounded, not left to block the installer" \+ "under 20s" "$((_t1 - _t0))s (outer bound fired)"+fi++echo ""+echo "Results: $PASS passed, $FAIL failed"+[ "$FAIL" -eq 0 ]diff --git a/tests/studio/install/test_rocm_support.py b/tests/studio/install/test_rocm_support.pyindex 12cfb9ef87d..6ae55a88559 100644--- a/tests/studio/install/test_rocm_support.py+++ b/tests/studio/install/test_rocm_support.py@@ -78,6 +78,53 @@ def _extract_sh_function_body(source: str, name: str) -> str:return source[start:]+# A dpkg-query -W stand-in that renders whichever showformat string it is handed,+# so this tests how install.sh ASKS for the version, not only how it parses the+# answer. It answers for a package in ANY state because the real tool does: only+# purged ones are left out, so a rocm-core removed with `apt remove` and never+# purged keeps reporting the version it had.+_DPKG_QUERY_STUB = r"""#!/bin/sh+_status='__STATUS__'+_ver='__VERSION__'+# dpkg's Status field is "<want> <error-flag> <status>".+case "$_status" in installed) _want=install ;; *) _want=deinstall ;; esac+_fmt=''+_found=''+while [ $# -gt 0 ]; do+ case "$1" in+ -f=*) _fmt=${1#-f=} ;;+ --showformat=*) _fmt=${1#--showformat=} ;;+ -f|--showformat) shift; _fmt=$1 ;;+ -*) : ;;+ rocm-core) _found=1 ;;+ esac+ shift+done+[ -n "$_found" ] || exit 1+[ -n "$_fmt" ] || _fmt='${Package}\t${Version}\n'+# Unrecognised fields render empty, like the real dpkg-query.+_out=$(printf '%s' "$_fmt" | sed \+ -e "s|\${Package}|rocm-core|g" \+ -e "s|\${Status}|$_want ok $_status|g" \+ -e "s|\${db:Status-Status}|$_status|g" \+ -e "s|\${db:Status-Want}|$_want|g" \+ -e "s|\${db:Status-Eflag}|ok|g" \+ -e "s|\${Version}|$_ver|g" \+ -e "s|\${[^}]*}||g")+printf "$_out"+"""+++def _write_dpkg_query_stub(+ path: str,+ version: str,+ status: str = "installed",+) -> None:+ with open(path, "w", encoding = "utf-8") as f:+ f.write(_DPKG_QUERY_STUB.replace("__STATUS__", status).replace("__VERSION__", version))+ os.chmod(path, 0o755)++# ── Helper: build HostInfo for different scenarios ──────────────────────────@@ -2020,23 +2067,56 @@ class TestInstallShStructure:'[ "$_torch_index_pinned" = true ]' in source[summary - 700 : summary]), "the gpu summary must not claim no usable ROCm for a pinned index"+ _ROCM_VERSION_SOURCES = (+ "_rocm_tag_from_amd_smi",+ "_rocm_tag_from_version_file",+ "_rocm_tag_from_hipconfig",+ "_rocm_tag_from_dpkg",+ "_rocm_tag_from_rpm",+ )++ def _rocm_version_detection_script(self, source: str, rocm_prefix: str) -> str:+ """The version-source helpers plus the resolver and the guarded+ assignment that get_torch_index_url makes, as a runnable script.++ /opt/rocm is redirected to `rocm_prefix` so the version file is a source+ the test controls: a real ROCm host would otherwise answer one of the+ probes and make the assertions machine-dependent."""+ parts = []+ # _run_bounded first: _rocm_tag_from_rpm routes its query through it, so+ # omitting it makes that source answer nothing and the per-position+ # assertions below pass for the wrong reason.+ for name in (+ "_run_bounded",+ *self._ROCM_VERSION_SOURCES,+ "_highest_rocm_tag",+ "_detect_rocm_version_tag",+ ):+ body = _extract_sh_function_body(source, name)+ assert body, f"install.sh no longer defines {name}()"+ parts.append(body)+ assignment = re.search(+ r'^ _rocm_tag=\$\(_detect_rocm_version_tag.*?\|\| _rocm_tag=""\n',+ source,+ re.S | re.M,+ )+ assert assignment, "could not extract the guarded _rocm_tag assignment"+ parts.append(assignment.group(0))+ return "\n".join(parts).replace("/opt/rocm", rocm_prefix)+def test_rocm_version_chain_survives_no_source_under_set_e(self):"""When every ROCm version source is missing (e.g. rocminfo present but- rocm-core not installed, so dpkg-query/rpm exit 1), the _rocm_tag ||- chain fails as a whole; without the || guard set -e kills the installer- BEFORE the actionable no-version WARN it feeds. Executed, not text."""+ rocm-core not installed, so dpkg-query/rpm exit 1), detection must still+ return empty AND succeed; otherwise set -e kills the installer BEFORE the+ actionable no-version WARN it feeds. Executed, not text."""shell = shutil.which("bash")if not shell:pytest.skip("bash needed to execute the version chain")sh_path = PACKAGE_ROOT / "install.sh"source = sh_path.read_text(encoding = "utf-8")- chain = re.search(- r'^ _rocm_tag=\$\(\{ command -v amd-smi.*?\|\| _rocm_tag=""\n',- source,- re.S | re.M,- )- assert chain, "could not extract the guarded _rocm_tag chain"with tempfile.TemporaryDirectory() as d:+ # Nothing is created under this prefix, so every source comes up empty.+ script_body = self._rocm_version_detection_script(source, os.path.join(d, "rocm"))# Tools exist on PATH but yield nothing usable, like a box with the# probe tools installed and no rocm-core package.for name in ("amd-smi", "hipconfig", "dpkg-query", "rpm"):@@ -2045,14 +2125,102 @@ class TestInstallShStructure:f.write("#!/bin/sh\nexit 1\n")os.chmod(p, 0o755)script = (- "set -euo pipefail\n" + chain.group(0) + '\nprintf "SURVIVED:%s\\n" "$_rocm_tag"\n'+ "set -euo pipefail\n" + script_body + '\nprintf "SURVIVED:%s\\n" "$_rocm_tag"\n')env = dict(os.environ, PATH = d + os.pathsep + os.environ.get("PATH", ""))r = subprocess.run([shell, "-c", script], env = env, capture_output = True, text = True)- assert r.returncode == 0, f"version chain aborted under set -e: {r.stderr}"- assert r.stdout.startswith("SURVIVED:"), r.stdout+ assert r.returncode == 0, f"version detection aborted under set -e: {r.stderr}"+ assert r.stdout.strip() == "SURVIVED:", r.stdoutassert "rocm" in source.lower()+ def test_rocm_version_detection_takes_the_highest_source(self):+ """Issue #8402: Debian 13 ships hipconfig 5.7 next to a 6.1 runtime, so+ stopping at the first source that answered gated a working gfx1100 out to+ CPU wheels. Every source is read and the highest wins. Executed, not+ text, and asserted per source position so no single ordering passes by+ accident."""+ shell = shutil.which("bash")+ if not shell:+ pytest.skip("bash needed to execute the version chain")+ source = (PACKAGE_ROOT / "install.sh").read_text(encoding = "utf-8")+ # Each source answers; whichever position holds the 6.4 reading must win.+ # "version-file" is /opt/rocm/.info/version rather than a PATH tool.+ outputs = {+ "amd-smi": "AMDSMI Tool: 25.0.1 | ROCm version: {v}",+ "version-file": "{v}.1-98",+ "hipconfig": "{v}.31921-0",+ "dpkg-query": "1:{v}.4-1",+ "rpm": "{v}.4-1",+ }+ for winner in outputs:+ with tempfile.TemporaryDirectory() as d:+ rocm_prefix = os.path.join(d, "rocm")+ script_body = self._rocm_version_detection_script(source, rocm_prefix)+ for name, template in outputs.items():+ line = template.format(v = "6.4" if name == winner else "5.7")+ if name == "version-file":+ os.makedirs(os.path.join(rocm_prefix, ".info"), exist_ok = True)+ with open(+ os.path.join(rocm_prefix, ".info", "version"), "w", encoding = "utf-8"+ ) as f:+ f.write(line + "\n")+ continue+ if name == "dpkg-query":+ _write_dpkg_query_stub(os.path.join(d, name), line, "installed")+ continue+ p = os.path.join(d, name)+ with open(p, "w", encoding = "utf-8") as f:+ f.write(f"#!/bin/sh\necho '{line}'\n")+ os.chmod(p, 0o755)+ script = "set -euo pipefail\n" + script_body + '\nprintf "TAG:%s\\n" "$_rocm_tag"\n'+ env = dict(os.environ, PATH = d + os.pathsep + os.environ.get("PATH", ""))+ r = subprocess.run([shell, "-c", script], env = env, capture_output = True, text = True)+ assert r.returncode == 0, r.stderr+ assert r.stdout.strip() == "TAG:rocm6.4", (+ f"{winner} reported 6.4 while every other source reported 5.7, "+ f"but detection resolved {r.stdout.strip()}"+ )++ def test_removed_dpkg_rocm_core_cannot_pick_the_wheels(self):+ """Highest-wins fixed the undershoot in #8402 and opened the symmetric+ hole: a source reading HIGHER than the runtime now wins outright.+ Undershoot lands on CPU wheels, which work; overshoot installs wheels the+ runtime cannot load. `dpkg-query -W` reports packages left in "deinstall+ ok config-files" by `apt remove` without `apt purge`, still carrying the+ version they had, so a host that ran ROCm 7.0 and went back to 6.1 hands+ dpkg a 7.0 that beats every live source. Only "installed" counts.+ Executed, not text, and paired with the installed case so the assertion+ cannot pass on the version alone."""+ shell = shutil.which("bash")+ if not shell:+ pytest.skip("bash needed to execute the version chain")+ source = (PACKAGE_ROOT / "install.sh").read_text(encoding = "utf-8")+ for status, expected in (("config-files", "TAG:rocm6.1"), ("installed", "TAG:rocm7.0")):+ with tempfile.TemporaryDirectory() as d:+ rocm_prefix = os.path.join(d, "rocm")+ script_body = self._rocm_version_detection_script(source, rocm_prefix)+ # The live runtime, via the version file: ROCm 6.1.+ os.makedirs(os.path.join(rocm_prefix, ".info"), exist_ok = True)+ with open(+ os.path.join(rocm_prefix, ".info", "version"), "w", encoding = "utf-8"+ ) as f:+ f.write("6.1.2-98\n")+ _write_dpkg_query_stub(os.path.join(d, "dpkg-query"), "1:7.0.0-1", status)+ # Silence the rest so a real ROCm on the test host cannot answer.+ for name in ("amd-smi", "hipconfig", "rpm"):+ p = os.path.join(d, name)+ with open(p, "w", encoding = "utf-8") as f:+ f.write("#!/bin/sh\nexit 1\n")+ os.chmod(p, 0o755)+ script = "set -euo pipefail\n" + script_body + '\nprintf "TAG:%s\\n" "$_rocm_tag"\n'+ env = dict(os.environ, PATH = d + os.pathsep + os.environ.get("PATH", ""))+ r = subprocess.run([shell, "-c", script], env = env, capture_output = True, text = True)+ assert r.returncode == 0, r.stderr+ assert r.stdout.strip() == expected, (+ f"dpkg rocm-core 7.0 in state {status!r} next to a 6.1 version file "+ f"resolved {r.stdout.strip()}, expected {expected}"+ )+def test_cuda_precedence(self):"""ROCm detection runs only when NVIDIA is absent (check runtime ordering in get_torch_index_url)."""sh_path = PACKAGE_ROOT / "install.sh"@@ -2471,13 +2639,35 @@ 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")+ # 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+ # (making _detect_rocm_version_tag return a real tag left this test green).+ version_fns = [+ _extract_sh_function_body(source, name)+ for name in (+ "_rocm_tag_from_amd_smi",+ "_rocm_tag_from_version_file",+ "_rocm_tag_from_hipconfig",+ "_rocm_tag_from_dpkg",+ "_rocm_tag_from_rpm",+ "_highest_rocm_tag",+ "_detect_rocm_version_tag",+ )+ ]assert fn and probe_fn and family_fn+ 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# /opt/rocm/.info/version directly (no tool to shim), which resolves# a tag on a real ROCm box and skips the no-version endpoint under# test. Same path-substitution technique as the KFD topology tests.- fn = fn.replace("/opt/rocm/.info/version", Path(d, "no-rocm-version").as_posix())+ # The read moved into _rocm_tag_from_version_file, so the rewrite has to+ # land on the helper text; applying it to fn alone is a no-op.+ version_fns = [+ body.replace("/opt/rocm/.info/version", Path(d, "no-rocm-version").as_posix())+ for body in version_fns+ ]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')# Silence every ROCm version source, not just amd-smi: a dev box with@@ -2501,6 +2691,8 @@ class TestInstallShStructure:+ "\n"+ family_fn+ "\n"+ + "\n".join(version_fns)+ + "\n"+ fn+ "\n""get_torch_index_url\n"