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
#!/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
set -e
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
. "$SCRIPT_DIR/_harness.sh"
INSTALL_SH="$SCRIPT_DIR/../../install.sh"
# The hardcoded /usr/bin/nvidia-smi fallback is rewritten so the no-GPU case works on GPU hosts.
_FUNC_FILE=$(mktemp)
_FAKE_SMI_DIR=$(mktemp -d)
# Redirect /opt/rocm to an empty dir: the ROCm probes read it by absolute path and would
# leak a real host GPU into the harness.
_FAKE_ROCM_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 ""
# MUST stay in sync with install.sh: a missing helper makes the ROCm branch silently fall
# through to the CPU index.
sed -n '/^_rocm_torch_explicitly_requested()/,/^}/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 '/^_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 '/^_hsa_override_gfx_arch()/,/^}/p' "$INSTALL_SH"
echo ""
sed -n '/^_trim_index_path_slashes()/,/^}/p' "$INSTALL_SH"
echo ""
sed -n '/^_nvidia_library_inventory()/,/^}/p' "$INSTALL_SH"
echo ""
sed -n '/^_nvidia_driver_cuda_version()/,/^}/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 ""
# Same sync rule for the version sources and their resolver.
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 '/^_ROCM_BNB_GENERIC_FLOOR_TAG=/p' "$INSTALL_SH"
sed -n '/^_rocm_bnb_compatible_generic_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" \
-e "s|/proc/driver/nvidia/version|$_FAKE_SMI_DIR/proc-nvidia-version|g" \
> "$_FUNC_FILE"
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 _rocm_bnb_compatible_generic_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
_SYS_PATH="/usr/local/bin:/usr/bin:/bin"
# Mock nvidia-smi: $1 CUDA version, $2 compute caps (default one Ampere), $3 compute_cap exit code.
make_mock_smi() {
_dir=$(mktemp -d)
_caps="${2-8.6}"
_caps_rc="${3:-0}"
cat > "$_dir/nvidia-smi" <<MOCK
#!/bin/sh
case "\$1" in
-L)
echo "GPU 0: NVIDIA GeForce RTX 3090 (UUID: GPU-fake-uuid)"
;;
--query-gpu=compute_cap)
for _cap in $_caps; do echo "\$_cap"; done
exit $_caps_rc
;;
*)
cat <<'SMI_OUT'
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 550.54.15 Driver Version: 550.54.15 CUDA Version: $1 |
+-----------------------------------------------------------------------------------------+
SMI_OUT
;;
esac
MOCK
chmod +x "$_dir/nvidia-smi"
echo "$_dir"
}
# Newer drivers print a "CUDA UMD Version" header instead.
make_mock_smi_umd() {
_dir=$(mktemp -d)
cat > "$_dir/nvidia-smi" <<MOCK
#!/bin/sh
case "\$1" in
-L)
echo "GPU 0: NVIDIA GeForce RTX 5090 Laptop GPU (UUID: GPU-fake-uuid)"
;;
*)
cat <<'SMI_OUT'
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 610.47 KMD Version: 610.47 CUDA UMD Version: $1 |
+-----------------------------------------------------------------------------------------+
SMI_OUT
;;
esac
MOCK
chmod +x "$_dir/nvidia-smi"
echo "$_dir"
}
# Also answers `amd-smi list` so the GPU presence check succeeds.
make_mock_amd_smi() {
_dir=$(mktemp -d)
_gfx_arch="${2:-gfx1100}"
cat > "$_dir/amd-smi" <<MOCK
#!/bin/sh
case "\$1" in
list)
printf 'GPU: 0\\n BDF: 0000:03:00.0\\n NAME: $_gfx_arch\\n'
;;
*)
cat <<AMD_OUT
AMDSMI Tool: 25.0.1+2b74356 | AMDSMI Library version: 25.0.1.0 | ROCm version: $1
AMD_OUT
;;
esac
MOCK
chmod +x "$_dir/amd-smi"
echo "$_dir"
}
# The amd-smi mock above emits ONE gfx token, unlike a real host where rocminfo names each agent
# twice (Name and ISA Name); gfx checks must be tested with that shape; $1 ROCm version, $2 gfx
# arch, $3 agent count (default 1).
make_mock_rocminfo() {
_dir=$(make_mock_amd_smi "$1" "$2")
_rocm_gfx="$2"
_rocm_agents="${3:-1}"
{
echo '#!/bin/sh'
echo "cat <<'ROCMINFO_OUT'"
echo 'ROCk module is loaded'
echo 'Agent 1'
echo ' Name: AMD Ryzen 9 5950X'
echo ' Device Type: CPU'
_i=0
while [ "$_i" -lt "$_rocm_agents" ]; do
echo "Agent $((_i + 2))"
echo " Name: $_rocm_gfx"
echo ' Device Type: GPU'
echo ' ISA Info:'
echo " Name: amdgcn-amd-amdhsa--$_rocm_gfx:sramecc-:xnack-"
_i=$((_i + 1))
done
echo 'ROCMINFO_OUT'
} > "$_dir/rocminfo"
chmod +x "$_dir/rocminfo"
echo "$_dir"
}
# Same, for a mixed host: $2 is a space-separated arch list, one GPU agent each.
make_mock_rocminfo_multi() {
_dir=$(make_mock_amd_smi "$1" "${2%% *}")
_rocm_n=1
{
echo '#!/bin/sh'
echo "cat <<'ROCMINFO_OUT'"
echo 'ROCk module is loaded'
echo 'Agent 1'
echo ' Name: AMD Ryzen 9 5950X'
echo ' Device Type: CPU'
for _a in $2; do
_rocm_n=$((_rocm_n + 1))
echo "Agent $_rocm_n"
echo " Name: $_a"
echo ' Device Type: GPU'
echo ' ISA Info:'
echo " Name: amdgcn-amd-amdhsa--$_a:sramecc-:xnack-"
done
echo 'ROCMINFO_OUT'
} > "$_dir/rocminfo"
chmod +x "$_dir/rocminfo"
echo "$_dir"
}
# Build a minimal tools directory with symlinks to essential commands
# (uname, grep, head, etc.) but WITHOUT nvidia-smi or amd-smi.
_TOOLS_DIR=$(mktemp -d)
for _cmd in uname grep sed head sh bash cat awk printf tr; do
_real=$(command -v "$_cmd" 2>/dev/null || true)
[ -n "$_real" ] && ln -sf "$_real" "$_TOOLS_DIR/$_cmd"
done
# $1 = dir with mock nvidia-smi, or "none"; optional $2 sets CUDA_VISIBLE_DEVICES.
run_func() {
_mock_dir="$1"
if [ "$#" -ge 2 ]; then
_cvd_setup="export CUDA_VISIBLE_DEVICES='$2'"
else
_cvd_setup="unset CUDA_VISIBLE_DEVICES"
fi
# Pin the arch: the cap is x86_64-only.
_cvd_setup="$_cvd_setup; _ARCH=x86_64"
if [ "$_mock_dir" = "none" ]; then
PATH="$_TOOLS_DIR" bash -c "$_cvd_setup; . '$_FUNC_FILE'; get_torch_index_url" 2>/dev/null
else
PATH="$_mock_dir:$_TOOLS_DIR" bash -c "$_cvd_setup; . '$_FUNC_FILE'; get_torch_index_url" 2>/dev/null
fi
}
run_func_stderr() {
_mock_dir="$1"
if [ "$_mock_dir" = "none" ]; then
PATH="$_TOOLS_DIR" bash -c "unset CUDA_VISIBLE_DEVICES; _ARCH=x86_64; . '$_FUNC_FILE'; get_torch_index_url" 2>&1 >/dev/null
else
PATH="$_mock_dir:$_TOOLS_DIR" bash -c "unset CUDA_VISIBLE_DEVICES; _ARCH=x86_64; . '$_FUNC_FILE'; get_torch_index_url" 2>&1 >/dev/null
fi
}
echo "=== test_get_torch_index_url ==="
_result=$(run_func "none")
assert_eq "no nvidia-smi -> cpu" "https://download.pytorch.org/whl/cpu" "$_result"
_dir=$(make_mock_smi "12.6")
_result=$(run_func "$_dir")
assert_eq "CUDA 12.6 -> cu126" "https://download.pytorch.org/whl/cu126" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_smi "12.8")
_result=$(run_func "$_dir")
assert_eq "CUDA 12.8 -> cu128" "https://download.pytorch.org/whl/cu128" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_smi "13.0")
_result=$(run_func "$_dir")
assert_eq "CUDA 13.0 -> cu130" "https://download.pytorch.org/whl/cu130" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_smi "12.4")
_result=$(run_func "$_dir")
assert_eq "CUDA 12.4 -> cu124" "https://download.pytorch.org/whl/cu124" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_smi "11.8")
_result=$(run_func "$_dir")
assert_eq "CUDA 11.8 -> cu118" "https://download.pytorch.org/whl/cu118" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_smi "10.2")
_result=$(run_func "$_dir")
assert_eq "CUDA 10.2 -> cpu" "https://download.pytorch.org/whl/cpu" "$_result"
rm -rf "$_dir"
_dir=$(mktemp -d)
cat > "$_dir/nvidia-smi" <<'MOCK'
#!/bin/sh
case "$1" in
-L) echo "GPU 0: NVIDIA GeForce RTX 3090 (UUID: GPU-fake-uuid)" ;;
*) echo "something completely unexpected" ;;
esac
MOCK
chmod +x "$_dir/nvidia-smi"
_result=$(run_func "$_dir")
assert_eq "unparseable -> cu126" "https://download.pytorch.org/whl/cu126" "$_result"
rm -rf "$_dir"
# python3 stand-in for the driver-library probe, printing "<cuda> <caps>".
make_mock_probe() {
printf '#!/bin/sh\ncat >/dev/null\necho "%s"\n' "$2" > "$1/python3"
chmod +x "$1/python3"
}
_dir=$(mktemp -d)
cat > "$_dir/nvidia-smi" <<'MOCK'
#!/bin/sh
case "$1" in
-L) echo "GPU 0: NVIDIA GeForce RTX 5090 (UUID: GPU-fake-uuid)" ;;
*) echo "something completely unexpected" ;;
esac
MOCK
chmod +x "$_dir/nvidia-smi"
make_mock_probe "$_dir" "13.0 12.0"
_result=$(run_func "$_dir")
assert_eq "unparseable banner, library says 13.0 -> cu130" "https://download.pytorch.org/whl/cu130" "$_result"
make_mock_probe "$_dir" "12.8 6.1"
_result=$(run_func "$_dir")
assert_eq "library says 12.8 with sm_61 -> cu126" "https://download.pytorch.org/whl/cu126" "$_result"
_result=$(PATH="$_dir:$_TOOLS_DIR" bash -c "unset CUDA_VISIBLE_DEVICES; _ARCH=x86_64; UNSLOTH_NVIDIA_LIBRARY_PROBE=0; . '$_FUNC_FILE'; get_torch_index_url" 2>/dev/null)
assert_eq "probe off -> cu126 default" "https://download.pytorch.org/whl/cu126" "$_result"
rm -rf "$_dir"
_dir=$(mktemp -d)
make_mock_probe "$_dir" "12.9 8.9"
_result=$(run_func "$_dir")
assert_eq "no nvidia-smi, library says 12.9 -> cu128" "https://download.pytorch.org/whl/cu128" "$_result"
rm -rf "$_dir"
# The inventory is read once per run; the probe is not launched again.
_dir=$(mktemp -d)
printf '#!/bin/sh\ncat >/dev/null\nn=$(cat "%s/calls" 2>/dev/null || echo 0)\necho $((n + 1)) > "%s/calls"\n[ "$n" = 0 ] && echo "12.9 8.9"\n' "$_dir" "$_dir" > "$_dir/python3"
chmod +x "$_dir/python3"
_result=$(PATH="$_dir:$_TOOLS_DIR" bash -c "unset CUDA_VISIBLE_DEVICES; _ARCH=x86_64; . '$_FUNC_FILE'; _has_usable_nvidia_gpu && get_torch_index_url" 2>/dev/null)
assert_eq "memoised inventory -> cu128 from the first answer" "https://download.pytorch.org/whl/cu128" "$_result"
assert_eq "memoised inventory -> one probe launch" "1" "$(cat "$_dir/calls")"
rm -rf "$_dir"
# No system python3: the venv's interpreter reads the library once it exists.
_dir=$(mktemp -d)
mkdir -p "$_dir/venv/bin"
make_mock_probe "$_dir/venv/bin" "12.9 8.9"
mv "$_dir/venv/bin/python3" "$_dir/venv/bin/python"
_result=$(PATH="$_TOOLS_DIR" bash -c "unset CUDA_VISIBLE_DEVICES; _ARCH=x86_64; VENV_DIR='$_dir/venv'; . '$_FUNC_FILE'; get_torch_index_url" 2>/dev/null)
assert_eq "no python3, venv python reads 12.9 -> cu128" "https://download.pytorch.org/whl/cu128" "$_result"
_result=$(PATH="$_TOOLS_DIR" bash -c "unset CUDA_VISIBLE_DEVICES; _ARCH=x86_64; VENV_DIR='$_dir/none'; . '$_FUNC_FILE'; _has_usable_nvidia_gpu; VENV_DIR='$_dir/venv'; get_torch_index_url" 2>/dev/null)
assert_eq "no interpreter yet is not memoised -> cu128 once the venv exists" "https://download.pytorch.org/whl/cu128" "$_result"
rm -rf "$_dir"
# The memo lives in the parent shell, primed before the command substitution.
_prime=$(grep -n '^ _has_usable_nvidia_gpu >/dev/null 2>&1 || true$' "$INSTALL_SH" | head -1 | cut -d: -f1)
_guard=$(sed -n "$((_prime - 1))p" "$INSTALL_SH")
_assign=$(grep -n -F 'TORCH_INDEX_URL=$(get_torch_index_url)' "$INSTALL_SH" | head -1 | cut -d: -f1)
if [ -n "$_prime" ] && [ -n "$_assign" ] && [ "$_prime" -lt "$_assign" ]; then _result=ordered; else _result="prime=$_prime assign=$_assign"; fi
assert_eq "presence check primes the inventory before the index substitution" "ordered" "$_result"
assert_eq "the prime is skipped for a pinned index or no torch" 'if [ "$_torch_index_pinned" = false ] && [ "$SKIP_TORCH" = false ]; then' "$_guard"
_dir=$(mktemp -d)
cat > "$_dir/nvidia-smi" <<'MOCK'
#!/bin/sh
case "$1" in
-L) echo "GPU 0: NVIDIA B200 (UUID: GPU-fake-uuid)" ;;
*) echo "something completely unexpected" ;;
esac
MOCK
chmod +x "$_dir/nvidia-smi"
printf '#!/bin/sh\ncat >/dev/null\n[ "$2" = "-c" ] && echo "13.1" && exit 0\nexit 1\n' > "$_dir/python3"
chmod +x "$_dir/python3"
_result=$(run_func "$_dir")
assert_eq "no inventory, cuDriverGetVersion says 13.1 -> cu130" "https://download.pytorch.org/whl/cu130" "$_result"
_err=$(PATH="$_dir:$_TOOLS_DIR" bash -c "unset CUDA_VISIBLE_DEVICES; _ARCH=x86_64; . '$_FUNC_FILE'; get_torch_index_url" 2>&1 >/dev/null)
case "$_err" in *"Selecting the cu130 PyTorch wheels"*UNSLOTH_TORCH_INDEX_URL=*) _result=warned ;; *) _result="$_err" ;; esac
assert_eq "driver-only selection names the index and the override" "warned" "$_result"
# No interpreter answers: the /proc kernel module version bounds the CUDA version.
printf '#!/bin/sh\ncat >/dev/null\nexit 1\n' > "$_dir/python3"
for _case in "590.48.01 cu130" "575.51.03 cu128" "565.57.01 cu126" "535.183.01 cu124"; do
printf 'NVRM version: NVIDIA UNIX Open Kernel Module for x86_64 %s Release Build (dvs-builder@U22) Mon Dec 8 13:05:00 UTC 2025\n' "${_case% *}" > "$_FAKE_SMI_DIR/proc-nvidia-version"
_result=$(run_func "$_dir")
assert_eq "/proc driver ${_case% *} -> ${_case#* }" "https://download.pytorch.org/whl/${_case#* }" "$_result"
done
_result=$(PATH="$_dir:$_TOOLS_DIR" bash -c "unset CUDA_VISIBLE_DEVICES; _ARCH=x86_64; UNSLOTH_NVIDIA_LIBRARY_PROBE=0; . '$_FUNC_FILE'; get_torch_index_url" 2>/dev/null)
assert_eq "probe off ignores /proc -> cu126 default" "https://download.pytorch.org/whl/cu126" "$_result"
rm -f "$_FAKE_SMI_DIR/proc-nvidia-version"
_err=$(PATH="$_dir:$_TOOLS_DIR" bash -c "unset CUDA_VISIBLE_DEVICES; _ARCH=x86_64; . '$_FUNC_FILE'; get_torch_index_url" 2>&1 >/dev/null)
case "$_err" in *"defaulting to cu126"*"UNSLOTH_TORCH_INDEX_URL=https://download.pytorch.org/whl/cu128"*) _result=warned ;; *) _result="$_err" ;; esac
assert_eq "cu126 default names the override" "warned" "$_result"
rm -rf "$_dir"
# Fake `timeout` returns 124 on the first 10s call: nvidia-smi is retried with a longer bound.
_dir=$(make_mock_smi "13.0" "10.0")
cat > "$_dir/timeout" <<MOCK
#!/bin/sh
if [ "\$1" = 10 ] && [ "\$#" = 2 ] && [ ! -f "$_dir/timed-out" ]; then : > "$_dir/timed-out"; exit 124; fi
echo "\$1" >> "$_dir/bounds"
shift
exec "\$@"
MOCK
chmod +x "$_dir/timeout"
_result=$(run_func "$_dir")
assert_eq "banner timed out once, retry reads 13.0 -> cu130" "https://download.pytorch.org/whl/cu130" "$_result"
case "$(tr '\n' ' ' < "$_dir/bounds")" in *45*) _result=retried ;; *) _result="bounds: $(cat "$_dir/bounds")" ;; esac
assert_eq "the retry uses the 45s bound" "retried" "$_result"
rm -rf "$_dir"
# 9) ROCm 6.3 (no nvidia-smi) -> rocm6.4 for the generic BNB ABI floor
_dir=$(make_mock_amd_smi "6.3")
_result=$(run_func "$_dir")
assert_eq "ROCm 6.3 automatic generic floor -> rocm6.4" "https://download.pytorch.org/whl/rocm6.4" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_amd_smi "7.1")
_result=$(run_func "$_dir")
assert_eq "ROCm 7.1 -> rocm7.1" "https://download.pytorch.org/whl/rocm7.1" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_amd_smi "7.2")
_result=$(run_func "$_dir")
assert_eq "ROCm 7.2 -> rocm7.2" "https://download.pytorch.org/whl/rocm7.2" "$_result"
rm -rf "$_dir"
_cuda_dir=$(make_mock_smi "12.6")
_amd_dir=$(make_mock_amd_smi "6.3")
_combined_dir=$(mktemp -d)
ln -sf "$_cuda_dir/nvidia-smi" "$_combined_dir/nvidia-smi"
ln -sf "$_amd_dir/amd-smi" "$_combined_dir/amd-smi"
_result=$(run_func "$_combined_dir")
assert_eq "CUDA+ROCm -> CUDA precedence" "https://download.pytorch.org/whl/cu126" "$_result"
rm -rf "$_cuda_dir" "$_amd_dir" "$_combined_dir"
_result=$(run_func "none")
assert_eq "no GPU -> cpu" "https://download.pytorch.org/whl/cpu" "$_result"
# 14) ROCm 6.1 (no nvidia-smi) -> rocm6.4 for the generic BNB ABI floor
_dir=$(make_mock_amd_smi "6.1")
_result=$(run_func "$_dir")
assert_eq "ROCm 6.1 automatic generic floor -> rocm6.4" "https://download.pytorch.org/whl/rocm6.4" "$_result"
rm -rf "$_dir"
# 14a) The other old supported tags share the same automatic generic floor.
_dir=$(make_mock_amd_smi "6.0")
_result=$(run_func "$_dir")
assert_eq "ROCm 6.0 automatic generic floor -> rocm6.4" "https://download.pytorch.org/whl/rocm6.4" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_amd_smi "6.2")
_result=$(run_func "$_dir")
assert_eq "ROCm 6.2 automatic generic floor -> rocm6.4" "https://download.pytorch.org/whl/rocm6.4" "$_result"
rm -rf "$_dir"
# gfx906 is intentionally exempt from the generic floor; its later installer block
# keeps the legacy rocm6.3 route for hosts whose detected tag is newer than 6.3.
_dir=$(make_mock_amd_smi "6.1" "gfx906")
_result=$(run_func "$_dir")
assert_eq "gfx906 ROCm 6.1 keeps its legacy generic tag" "https://download.pytorch.org/whl/rocm6.1" "$_result"
rm -rf "$_dir"
# The exemption must survive the shape REAL discovery produces, not just the single
# token amd-smi emits. rocminfo names each agent twice, so one MI50 probes as
# "gfx906\ngfx906"; comparing the raw probe floored it to rocm6.4 and handed it to
# the legacy reroute, which then rewrote it to rocm6.3 instead of the literal tag.
_dir=$(make_mock_rocminfo "6.1" "gfx906" 1)
_result=$(run_func "$_dir")
assert_eq "gfx906 via rocminfo (agent named twice) keeps rocm6.1" "https://download.pytorch.org/whl/rocm6.1" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_rocminfo "6.1" "gfx906" 2)
_result=$(run_func "$_dir")
assert_eq "two gfx906 agents still keep rocm6.1" "https://download.pytorch.org/whl/rocm6.1" "$_result"
rm -rf "$_dir"
# A copied HIP gcnArchName in the override keeps its ISA suffix; every other gfx906
# comparison in install.sh strips it, so this one must too.
_dir=$(make_mock_amd_smi "6.1" "gfx906")
_result=$(UNSLOTH_ROCM_GFX_ARCH=gfx906:sramecc-:xnack- run_func "$_dir")
assert_eq "suffixed gfx906 override keeps rocm6.1" "https://download.pytorch.org/whl/rocm6.1" "$_result"
rm -rf "$_dir"
# The floor itself must still reach a non-gfx906 host through the same real shape.
_dir=$(make_mock_rocminfo "6.1" "gfx1100" 1)
_result=$(run_func "$_dir")
assert_eq "gfx1100 via rocminfo still floors to rocm6.4" "https://download.pytorch.org/whl/rocm6.4" "$_result"
rm -rf "$_dir"
# RDNA 1 / Polaris read fine through a real ROCm install and still have no wheels (#8529):
# every generic tag lacks their kernels, so the readable arch must not pick one.
_dir=$(make_mock_rocminfo "6.4" "gfx1010" 1)
_result=$(run_func "$_dir")
assert_eq "gfx1010 via rocminfo with ROCm 6.4 takes the CPU index" "https://download.pytorch.org/whl/cpu" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_rocminfo "7.1" "gfx1012" 2)
_result=$(run_func "$_dir")
assert_eq "two gfx1012 agents with ROCm 7.1 take the CPU index" "https://download.pytorch.org/whl/cpu" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_rocminfo "6.4" "gfx803" 1)
_result=$(run_func "$_dir")
assert_eq "gfx803 via rocminfo with ROCm 6.4 takes the CPU index" "https://download.pytorch.org/whl/cpu" "$_result"
rm -rf "$_dir"
# A covered card beside it still gets the generic wheels it can run.
_dir=$(make_mock_rocminfo_multi "6.4" "gfx1010 gfx1100")
_result=$(run_func "$_dir")
assert_eq "gfx1010 beside gfx1100 keeps rocm6.4" "https://download.pytorch.org/whl/rocm6.4" "$_result"
rm -rf "$_dir"
# A mixed host is not a sole gfx906 target, so it keeps the generic floor.
_dir=$(make_mock_rocminfo_multi "6.1" "gfx906 gfx1100")
_result=$(run_func "$_dir")
assert_eq "mixed gfx906 + gfx1100 host floors to rocm6.4" "https://download.pytorch.org/whl/rocm6.4" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_amd_smi "6.4")
_result=$(run_func "$_dir")
assert_eq "ROCm 6.4 -> rocm6.4" "https://download.pytorch.org/whl/rocm6.4" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_amd_smi "7.0")
_result=$(run_func "$_dir")
assert_eq "ROCm 7.0 -> rocm7.0" "https://download.pytorch.org/whl/rocm7.0" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_amd_smi "8.0")
_result=$(run_func "$_dir")
assert_eq "ROCm 8.0 -> rocm7.2 (capped)" "https://download.pytorch.org/whl/rocm7.2" "$_result"
rm -rf "$_dir"
_dir=$(mktemp -d)
cat > "$_dir/amd-smi" <<'MOCK'
#!/bin/sh
echo "AMDSMI Tool: 25.0.1 | AMDSMI Library version: 25.0.1.0 | ROCm version: "
MOCK
chmod +x "$_dir/amd-smi"
_result=$(run_func "$_dir")
assert_eq "empty amd-smi version -> cpu" "https://download.pytorch.org/whl/cpu" "$_result"
rm -rf "$_dir"
_dir=$(mktemp -d)
cat > "$_dir/amd-smi" <<'MOCK'
#!/bin/sh
echo "AMDSMI Tool: 25.0.1 | AMDSMI Library version: 25.0.1.0 | ROCm version: N/A"
MOCK
chmod +x "$_dir/amd-smi"
_result=$(run_func "$_dir")
assert_eq "N/A amd-smi version -> cpu" "https://download.pytorch.org/whl/cpu" "$_result"
rm -rf "$_dir"
# 20) ROCm version with trailing text (e.g. "6.3.1-beta") -> rocm6.4 floor
_dir=$(make_mock_amd_smi "6.3.1-beta")
_result=$(run_func "$_dir")
assert_eq "ROCm 6.3.1-beta automatic generic floor -> rocm6.4" "https://download.pytorch.org/whl/rocm6.4" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_smi "12.6")
_result=$(run_func "$_dir")
assert_eq "CUDA 12.6 regression -> cu126" "https://download.pytorch.org/whl/cu126" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_smi "13.0")
_result=$(run_func "$_dir")
assert_eq "CUDA 13.0 regression -> cu130" "https://download.pytorch.org/whl/cu130" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_smi "12.8")
_result=$(run_func "$_dir")
assert_eq "CUDA 12.8 regression -> cu128" "https://download.pytorch.org/whl/cu128" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_smi "12.6")
_result=$(UNSLOTH_PYTORCH_MIRROR="https://mirror.example.com/whl" run_func "$_dir")
assert_eq "mirror env + CUDA 12.6 -> mirror/cu126" "https://mirror.example.com/whl/cu126" "$_result"
rm -rf "$_dir"
_result=$(UNSLOTH_PYTORCH_MIRROR="https://mirror.example.com/whl" run_func "none")
assert_eq "mirror env + no GPU -> mirror/cpu" "https://mirror.example.com/whl/cpu" "$_result"
_result=$(UNSLOTH_PYTORCH_MIRROR="" run_func "none")
assert_eq "empty mirror env -> official/cpu" "https://download.pytorch.org/whl/cpu" "$_result"
_result=$(UNSLOTH_PYTORCH_MIRROR="https://mirror.example.com/whl/" run_func "none")
assert_eq "trailing slash stripped -> mirror/cpu" "https://mirror.example.com/whl/cpu" "$_result"
_dir=$(make_mock_smi_umd "13.3")
_result=$(run_func "$_dir")
assert_eq "CUDA UMD Version 13.3 -> cu130" "https://download.pytorch.org/whl/cu130" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_smi_umd "12.8")
_result=$(run_func "$_dir")
assert_eq "CUDA UMD Version 12.8 -> cu128" "https://download.pytorch.org/whl/cu128" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_smi_umd "11.8")
_result=$(run_func "$_dir")
assert_eq "CUDA UMD Version 11.8 -> cu118" "https://download.pytorch.org/whl/cu118" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_smi "13.3")
_result=$(run_func "$_dir")
assert_eq "CUDA Version 13.3 -> cu130" "https://download.pytorch.org/whl/cu130" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_smi "13.7")
_result=$(run_func "$_dir")
assert_eq "CUDA Version 13.7 -> cu130" "https://download.pytorch.org/whl/cu130" "$_result"
rm -rf "$_dir"
# Pre-Turing hosts cap at cu126: torch 2.11 cu128/cu130 start at sm_75.
_dir=$(make_mock_smi "13.0" "7.0")
_result=$(run_func "$_dir")
assert_eq "CUDA 13.0 + Volta -> cu126" "https://download.pytorch.org/whl/cu126" "$_result"
_result=$(run_func "$_dir" "0")
assert_eq "CUDA 13.0 + Volta + CVD=0 -> cu126" "https://download.pytorch.org/whl/cu126" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_smi "12.8" "6.1")
_result=$(run_func "$_dir")
assert_eq "CUDA 12.8 + Pascal -> cu126" "https://download.pytorch.org/whl/cu126" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_smi "13.0" "7.0 7.0")
_result=$(run_func "$_dir")
assert_eq "CUDA 13.0 + two Voltas -> cu126" "https://download.pytorch.org/whl/cu126" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_smi "13.0" "7.5")
_result=$(run_func "$_dir")
assert_eq "CUDA 13.0 + Turing -> cu130" "https://download.pytorch.org/whl/cu130" "$_result"
rm -rf "$_dir"
# cu126 spans sm_50-90, so a mixed host is served whole while its newest card is Hopper.
_dir=$(make_mock_smi "13.0" "7.0 8.6")
_result=$(run_func "$_dir")
assert_eq "CUDA 13.0 + Volta and Ampere -> cu126" "https://download.pytorch.org/whl/cu126" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_smi "13.0" "6.1 9.0")
_result=$(run_func "$_dir")
assert_eq "CUDA 13.0 + Pascal and Hopper -> cu126" "https://download.pytorch.org/whl/cu126" "$_result"
rm -rf "$_dir"
# No family covers a pre-Turing+Blackwell mix whole; the newer card keeps its wheels.
_dir=$(make_mock_smi "13.0" "7.0 12.0")
_result=$(run_func "$_dir")
assert_eq "CUDA 13.0 + Volta and Blackwell -> cu130" "https://download.pytorch.org/whl/cu130" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_smi "13.0" "3.7 8.6")
_result=$(run_func "$_dir")
assert_eq "CUDA 13.0 + Kepler and Ampere -> cu130" "https://download.pytorch.org/whl/cu130" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_smi "12.6" "7.0")
_result=$(run_func "$_dir")
assert_eq "CUDA 12.6 + Volta -> cu126" "https://download.pytorch.org/whl/cu126" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_smi "13.0" "7.0 N/A")
_result=$(run_func "$_dir")
assert_eq "unreadable capability row -> cu130" "https://download.pytorch.org/whl/cu130" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_smi "13.0" "7.0" 1)
_result=$(run_func "$_dir")
assert_eq "failed capability query -> cu130" "https://download.pytorch.org/whl/cu130" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_smi "13.0" "")
_result=$(run_func "$_dir")
assert_eq "empty capability inventory -> cu130" "https://download.pytorch.org/whl/cu130" "$_result"
rm -rf "$_dir"
# No aarch64 CUDA family ships sm_<80 kernels, so cu126 cannot help there.
_dir=$(make_mock_smi "13.0" "7.0")
_result=$(PATH="$_dir:$_TOOLS_DIR" bash -c \
"_ARCH=aarch64; . '$_FUNC_FILE'; _cap_cuda_family_for_pre_turing cu130 nvidia-smi" 2>/dev/null)
assert_eq "aarch64 keeps the driver family" "cu130" "$_result"
_result=$(PATH="$_dir:$_TOOLS_DIR" bash -c \
"_ARCH=x86_64; . '$_FUNC_FILE'; _cap_cuda_family_for_pre_turing cu130 nvidia-smi" 2>/dev/null)
assert_eq "x86_64 caps the driver family" "cu126" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_smi "12.8")
_result=$(run_func "$_dir" "")
assert_eq "CVD='' hides NVIDIA -> cpu" "https://download.pytorch.org/whl/cpu" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_smi "12.8")
_result=$(run_func "$_dir" "-1")
assert_eq "CVD=-1 hides NVIDIA -> cpu" "https://download.pytorch.org/whl/cpu" "$_result"
rm -rf "$_dir"
_cuda_dir=$(make_mock_smi "12.6")
_amd_dir=$(make_mock_amd_smi "6.4")
_combined_dir=$(mktemp -d)
ln -sf "$_cuda_dir/nvidia-smi" "$_combined_dir/nvidia-smi"
ln -sf "$_amd_dir/amd-smi" "$_combined_dir/amd-smi"
_result=$(run_func "$_combined_dir" "-1")
assert_eq "CUDA+ROCm with CVD=-1 -> rocm6.4" "https://download.pytorch.org/whl/rocm6.4" "$_result"
rm -rf "$_cuda_dir" "$_amd_dir" "$_combined_dir"
_dir=$(make_mock_smi "12.8")
_result=$(run_func "$_dir" "0")
assert_eq "CVD=0 keeps NVIDIA -> cu128" "https://download.pytorch.org/whl/cu128" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_smi "12.8")
_result=$(run_func "$_dir" " -1 ")
assert_eq "CVD=' -1 ' hides NVIDIA -> cpu" "https://download.pytorch.org/whl/cpu" "$_result"
rm -rf "$_dir"
_result=$(UNSLOTH_TORCH_INDEX_FAMILY="cu128" run_func "none")
assert_eq "family override (no GPU) -> cu128" "https://download.pytorch.org/whl/cu128" "$_result"
# Docker-build case: the builder sees the host driver but publishes a cu128 image.
_dir=$(make_mock_smi "12.6")
_result=$(UNSLOTH_TORCH_INDEX_FAMILY="cu128" run_func "$_dir")
assert_eq "family override beats detected 12.6 -> cu128" "https://download.pytorch.org/whl/cu128" "$_result"
rm -rf "$_dir"
_dir=$(make_mock_smi "12.6")
_result=$(UNSLOTH_TORCH_INDEX_URL="https://mirror.example.com/whl/cu999" run_func "$_dir")
assert_eq "url override beats detection -> verbatim" "https://mirror.example.com/whl/cu999" "$_result"
rm -rf "$_dir"
_result=$(UNSLOTH_PYTORCH_MIRROR="https://mirror.example.com/whl" UNSLOTH_TORCH_INDEX_FAMILY="cu128" run_func "none")
assert_eq "mirror + family override -> mirror/cu128" "https://mirror.example.com/whl/cu128" "$_result"
_result=$(UNSLOTH_TORCH_INDEX_URL="https://mirror.example.com/whl/cu128/" run_func "none")
assert_eq "url override trailing slash stripped" "https://mirror.example.com/whl/cu128" "$_result"
_result=$(UNSLOTH_TORCH_INDEX_URL="https://mirror.example.com/whl/cu130" UNSLOTH_TORCH_INDEX_FAMILY="cu128" run_func "none")
assert_eq "url override beats family override -> url" "https://mirror.example.com/whl/cu130" "$_result"
# 44a) Explicit ROCm pins remain authoritative and are not floored.
_result=$(UNSLOTH_TORCH_INDEX_FAMILY="rocm6.1" run_func "none")
assert_eq "explicit ROCm family pin remains rocm6.1" "https://download.pytorch.org/whl/rocm6.1" "$_result"
_result=$(UNSLOTH_TORCH_INDEX_URL="https://download.pytorch.org/whl/rocm6.1" run_func "none")
assert_eq "explicit ROCm URL pin remains rocm6.1" "https://download.pytorch.org/whl/rocm6.1" "$_result"
# 45) An empty override is ignored (falls through to normal detection).
_result=$(UNSLOTH_TORCH_INDEX_FAMILY="" UNSLOTH_TORCH_INDEX_URL="" run_func "none")
assert_eq "empty overrides ignored -> detected cpu" "https://download.pytorch.org/whl/cpu" "$_result"
_result=$(UNSLOTH_TORCH_INDEX_URL="https://mirror.example.com/whl/cu128///" run_func "none")
assert_eq "url override double slash stripped" "https://mirror.example.com/whl/cu128" "$_result"
_result=$(UNSLOTH_TORCH_INDEX_FAMILY="//cu128//" run_func "none")
assert_eq "family override slashes stripped" "https://download.pytorch.org/whl/cu128" "$_result"
# Only path slashes are trimmed, so a base64 query token ending in "/" survives.
_result=$(UNSLOTH_TORCH_INDEX_URL="https://mirror.example.com/whl/cu128?token=ab12cd/" run_func "none")
assert_eq "url override preserves query token slash" "https://mirror.example.com/whl/cu128?token=ab12cd/" "$_result"
_result=$(UNSLOTH_TORCH_INDEX_URL="https://mirror.example.com/whl/cu128//?token=ab12cd/" run_func "none")
assert_eq "url override path slash trimmed, query kept" "https://mirror.example.com/whl/cu128?token=ab12cd/" "$_result"
_result=$(UNSLOTH_TORCH_INDEX_URL="https://mirror.example.com/whl/cu128#anchor/" run_func "none")
assert_eq "url override preserves fragment slash" "https://mirror.example.com/whl/cu128#anchor/" "$_result"
_dir=$(make_mock_amd_smi "8.0")
_result=$(run_func_stderr "$_dir")
assert_contains "ROCm 8.0 cap is explained on stderr" "$_result" "No validated PyTorch for ROCm 8.0;"
assert_contains "ROCm 8.0 cap names the leaf installed instead" "$_result" "capping to the rocm7.2 index"
assert_contains "ROCm 8.0 cap says the wheels carry their own runtime" "$_result" "bundle their own runtime"
rm -rf "$_dir"
_dir=$(make_mock_amd_smi "7.14")
_result=$(run_func "$_dir")
assert_eq "ROCm 7.14 -> rocm7.2 (capped)" "https://download.pytorch.org/whl/rocm7.2" "$_result"
_result=$(run_func_stderr "$_dir")
assert_contains "ROCm 7.14 cap is explained on stderr" "$_result" "No validated PyTorch for ROCm 7.14;"
rm -rf "$_dir"
_dir=$(make_mock_amd_smi "7.2")
_result=$(run_func_stderr "$_dir")
assert_not_contains "ROCm 7.2 prints no cap note" "$_result" "No validated PyTorch"
rm -rf "$_dir"
_dir=$(make_mock_amd_smi "6.3")
_result=$(run_func_stderr "$_dir")
assert_not_contains "ROCm 6.3 prints no cap note" "$_result" "No validated PyTorch"
rm -rf "$_dir"
_dir=$(make_mock_amd_smi "6.5")
_result=$(run_func "$_dir")
assert_eq "ROCm 6.5 -> rocm6.4 (clipped)" "https://download.pytorch.org/whl/rocm6.4" "$_result"
_result=$(run_func_stderr "$_dir")
assert_contains "ROCm 6.5 clip is explained on stderr" "$_result" "No validated PyTorch for ROCm 6.5;"
rm -rf "$_dir"
# bash 3.2 (macOS /bin/sh) ends $(...) at a bare `pattern)`.
_bare_arms=$(sed -n '/_rocm_index=\$(case "\$_rocm_tag" in/,/^[[:space:]]*esac)/p' "$INSTALL_SH" \
| grep -v '^[[:space:]]*esac)' | grep -E '^[[:space:]]*[^([:space:]#][^[:space:]]*\)' || true)
_n_arms=$(sed -n '/_rocm_index=\$(case "\$_rocm_tag" in/,/^[[:space:]]*esac)/p' "$INSTALL_SH" | grep -cE '^[[:space:]]*\(' || true)
assert_eq "captured ROCm case has no bare pattern arms" "" "$_bare_arms"
assert_eq "captured ROCm case arms found" "yes" "$([ "${_n_arms:-0}" -ge 10 ] && echo yes)"
rm -f "$_FUNC_FILE"
rm -rf "$_FAKE_SMI_DIR"
rm -rf "$_FAKE_ROCM_DIR"
rm -rf "$_TOOLS_DIR"
summary