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.

Unsloth Studio / Desktop (AMD): explain why an NVIDIA+AMD host installed with `CUDA_VISIBLE_DEVICES=""` shows no GPU (#12249)

Leo Borcherdingfe29f2357823
diff --git a/install.sh b/install.sh
index 6c48bfd06f1..99ccb34a86f 100755
--- a/install.sh
+++ b/install.sh
@@ -5893,6 +5893,22 @@ _torch_index_url_is_rocm() {
esac
}
+# HIP reads CUDA_VISIBLE_DEVICES when HIP_VISIBLE_DEVICES is unset, so the "" mask that steers a mixed NVIDIA + AMD host to ROCm torch also hides the AMD card at runtime.
+_warn_if_cuda_mask_hides_amd() {
+ _cvd_hides_nvidia || return 0
+ [ -z "${HIP_VISIBLE_DEVICES+x}" ] || return 0
+ _torch_index_url_is_rocm "${1:-}" || return 0
+ _amd_gpu_present_via_pci || return 0
+ ( unset CUDA_VISIBLE_DEVICES; _has_usable_nvidia_gpu ) >/dev/null 2>&1 || return 0
+ echo "" >&2
+ echo "[WARN] CUDA_VISIBLE_DEVICES=\"$CUDA_VISIBLE_DEVICES\" hid the NVIDIA GPU, so ROCm torch was selected." >&2
+ echo "[WARN] ROCm reads CUDA_VISIBLE_DEVICES too: left set, it also hides the AMD GPU and" >&2
+ echo "[WARN] Unsloth Studio / Desktop will run CPU-only. Unset it before launching" >&2
+ echo "[WARN] (HIP_VISIBLE_DEVICES=0 picks the AMD card), and reinstall with" >&2
+ echo "[WARN] UNSLOTH_FORCE_ROCM_TORCH=1 rather than the mask to ask for ROCm torch." >&2
+ echo "" >&2
+}
+
# True for an EXACT ROCm family leaf; one that merely starts with rocm/gfx is a custom pin.
_is_pip_rocm_family_leaf() {
case "$1" in
@@ -6580,6 +6596,9 @@ if [ "$_torch_index_pinned" = false ] && [ "$SKIP_TORCH" = false ] && \
fi
fi
fi
+if [ "$SKIP_TORCH" = false ]; then
+ _warn_if_cuda_mask_hides_amd "$TORCH_INDEX_URL"
+fi
# Export the resolved torch backend ("cuda", "rocm" or "cpu") so setup.sh and install_python_stack.py know what was chosen here and can skip ROCm-specific repair steps. Classify on the FINAL path segment only: a custom UNSLOTH_PYTORCH_MIRROR whose base path happens to contain "rocm" or "gfx" must not mislabel a cu*/cpu index as ROCm (radeon repo URLs end in rocm-rel-X.Y/, Strix overrides in gfxNNNN/, so the trailing slash is stripped first). Lowercase the leaf so every gfx*/rocm*/cu* arm matches regardless of case (the canonical AMD RDNA4 leaf is gfx120X-all). CUDA is branded only on a real cu[0-9]* leaf, so a mirror leaf (/current) does NOT commit a CUDA backend; an unknown leaf leaves the var unset so the stack probes the GPU. Query and fragment are dropped first, then ALL trailing slashes, in lockstep with the shared _torch_index_url_leaf extractor.
_torch_index_leaf="${TORCH_INDEX_URL%%\?*}"
_torch_index_leaf="${_torch_index_leaf%%#*}"
diff --git a/studio/backend/tests/test_rocm_mixed_host_emptied_cuda_mask.py b/studio/backend/tests/test_rocm_mixed_host_emptied_cuda_mask.py
new file mode 100644
index 00000000000..f4449f59b42
--- /dev/null
+++ b/studio/backend/tests/test_rocm_mixed_host_emptied_cuda_mask.py
@@ -0,0 +1,152 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""NVIDIA + AMD host launched with CUDA_VISIBLE_DEVICES="": log why ROCm torch sees no GPU."""
+
+from __future__ import annotations
+
+import sys
+import types
+from types import SimpleNamespace
+
+import pytest
+
+import utils.hardware.hardware as hw
+
+_NVIDIA = {"vendor": "nvidia", "name": "NVIDIA GeForce RTX 3080"}
+_AMD = {"vendor": "amd", "name": "AMD Radeon AI PRO R9700", "gfx_candidates": ["gfx1201"]}
+_HINT = "hides the AMD GPU from ROCm torch"
+
+
+def _fake_torch(flavor: str):
+ torch = types.ModuleType("torch")
+ if flavor == "rocm":
+ torch.version = SimpleNamespace(hip = "7.1.52802", cuda = None)
+ torch.__version__ = "2.9.1+rocm7.1"
+ else:
+ torch.version = SimpleNamespace(hip = None, cuda = "12.8")
+ torch.__version__ = "2.9.1+cu128"
+ torch.cuda = SimpleNamespace(is_available = lambda: False, device_count = lambda: 0)
+ return torch
+
+
+class _Recorder:
+ def __init__(self):
+ self.warnings: list[str] = []
+
+ def warning(self, msg, *args, **_kw):
+ self.warnings.append(msg % args if args else msg)
+
+ def info(self, *_a, **_kw):
+ pass
+
+ debug = error = exception = info
+
+
[email protected](autouse = True)
+def _pinned_host(monkeypatch, tmp_path):
+ for var in (
+ "CUDA_VISIBLE_DEVICES",
+ "HIP_VISIBLE_DEVICES",
+ "ROCR_VISIBLE_DEVICES",
+ "ZE_AFFINITY_MASK",
+ "UNSLOTH_FORCE_XPU",
+ "UNSLOTH_TORCH_INDEX_URL",
+ "UNSLOTH_TORCH_INDEX_FAMILY",
+ ):
+ monkeypatch.delenv(var, raising = False)
+ monkeypatch.setattr(hw, "TORCH_IMPORT_ERROR", None)
+ monkeypatch.setattr(hw, "IS_ROCM", False)
+ monkeypatch.setattr(hw.platform, "system", lambda: "Linux")
+ monkeypatch.setattr(hw.sys, "prefix", str(tmp_path))
+ monkeypatch.setattr(hw, "_torch_build_snapshot_cache", None)
+ monkeypatch.setattr(hw, "_physical_gpu_inventory_cache", None)
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("rocm"))
+
+
+def _inventory(monkeypatch, devices):
+ monkeypatch.setattr(
+ hw,
+ "get_physical_gpu_inventory",
+ lambda **_kw: {"available": bool(devices), "devices": devices, "unknown": False},
+ )
+
+
+def _detect(monkeypatch) -> _Recorder:
+ recorder = _Recorder()
+ monkeypatch.setattr(hw, "logger", recorder)
+ for name, value in (
+ ("DEVICE", None),
+ ("CHAT_ONLY", True),
+ ("CHAT_ONLY_REASON", None),
+ ("CHAT_ONLY_DETAIL", None),
+ ):
+ monkeypatch.setattr(hw, name, value)
+ with hw._DETECT_LOCK:
+ assert hw._detect_hardware_locked() == hw.DeviceType.CPU
+ return recorder
+
+
+def _hinted(recorder: _Recorder) -> bool:
+ return any(_HINT in line for line in recorder.warnings)
+
+
[email protected]("mask", ["", " ", "-1"])
+def test_the_reporters_mixed_host_is_told_why(monkeypatch, mask):
+ _inventory(monkeypatch, [_NVIDIA, _AMD])
+ monkeypatch.setenv("CUDA_VISIBLE_DEVICES", mask)
+
+ recorder = _detect(monkeypatch)
+
+ assert _hinted(recorder)
+ line = next(entry for entry in recorder.warnings if _HINT in entry)
+ assert "HIP_VISIBLE_DEVICES" in line
+ assert "UNSLOTH_FORCE_ROCM_TORCH=1" in line
+ assert hw.CHAT_ONLY_REASON == "no_gpu"
+ assert hw._torch_gpu_mismatch_report() == {}
+
+
+def test_an_amd_only_host_with_the_same_mask_stays_deliberate(monkeypatch):
+ _inventory(monkeypatch, [_AMD])
+ monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "")
+
+ recorder = _detect(monkeypatch)
+
+ assert not _hinted(recorder)
+ assert hw.CHAT_ONLY_REASON == "no_gpu"
+
+
+def test_hip_visible_devices_overrides_the_mask_so_no_hint(monkeypatch):
+ _inventory(monkeypatch, [_NVIDIA, _AMD])
+ monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "")
+ monkeypatch.setenv("HIP_VISIBLE_DEVICES", "0")
+
+ assert not _hinted(_detect(monkeypatch))
+ assert hw._emptied_cuda_mask_hides_amd_on_a_mixed_host() is False
+
+
+def test_a_cuda_wheel_on_the_mixed_host_gets_no_rocm_hint(monkeypatch):
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cuda"))
+ _inventory(monkeypatch, [_NVIDIA, _AMD])
+ monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "")
+
+ assert hw._emptied_cuda_mask_hides_amd_on_a_mixed_host() is False
+
+
[email protected]("mask", [None, "0"])
+def test_no_hint_without_an_emptied_mask(monkeypatch, mask):
+ _inventory(monkeypatch, [_NVIDIA, _AMD])
+ if mask is not None:
+ monkeypatch.setenv("CUDA_VISIBLE_DEVICES", mask)
+
+ assert hw._emptied_cuda_mask_hides_amd_on_a_mixed_host() is False
+
+
+def test_an_inventory_that_raises_gives_no_hint(monkeypatch):
+ def _boom(**_kw):
+ raise RuntimeError("probe failed")
+
+ monkeypatch.setattr(hw, "get_physical_gpu_inventory", _boom)
+ monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "")
+
+ assert hw._emptied_cuda_mask_hides_amd_on_a_mixed_host() is False
diff --git a/studio/backend/utils/hardware/hardware.py b/studio/backend/utils/hardware/hardware.py
index 685299c26e3..80fe3d2e79c 100644
--- a/studio/backend/utils/hardware/hardware.py
+++ b/studio/backend/utils/hardware/hardware.py
@@ -682,6 +682,19 @@ def _masks_hide_every_accelerator(*, block_inventory: bool = False) -> bool:
return True
+def _emptied_cuda_mask_hides_amd_on_a_mixed_host() -> bool:
+ """Emptied CUDA_VISIBLE_DEVICES hides the AMD card from ROCm torch on an NVIDIA + AMD host. A log line, not a mismatch: the mask is deliberate on AMD-only hosts. Blocks on the inventory."""
+ if not _mask_is_emptied("CUDA_VISIBLE_DEVICES") or "HIP_VISIBLE_DEVICES" in os.environ:
+ return False
+ if not _torch_reports_a_hip_runtime():
+ return False
+ try:
+ devices = get_physical_gpu_inventory().get("devices") or []
+ except Exception:
+ return False
+ return {"nvidia", "amd"} <= {device.get("vendor") for device in devices}
+
+
def _vendors_masked_off(*, block_inventory: bool = False) -> set:
"""Vendors whose devices are all hidden by a mask that can take effect here."""
relevant = _relevant_visibility_masks(block_inventory = block_inventory)
@@ -1522,6 +1535,15 @@ def _detect_hardware_locked() -> DeviceType:
_build_reason, _build_detail = _mismatch_verdict_for_this_host()
if _build_reason is not None:
CHAT_ONLY_REASON, CHAT_ONLY_DETAIL = _build_reason, _build_detail
+ elif _emptied_cuda_mask_hides_amd_on_a_mixed_host():
+ logger.warning(
+ "CUDA_VISIBLE_DEVICES=%r hides the AMD GPU from ROCm torch as well as the "
+ "NVIDIA one: HIP reads it when HIP_VISIBLE_DEVICES is unset. Unset "
+ "CUDA_VISIBLE_DEVICES (or set HIP_VISIBLE_DEVICES=0) before launching. "
+ "To get ROCm torch on an NVIDIA + AMD host without the mask, install "
+ "with UNSLOTH_FORCE_ROCM_TORCH=1.",
+ os.environ.get("CUDA_VISIBLE_DEVICES"),
+ )
print("Hardware detected: CPU training backend (no PyTorch/MLX GPU backend available)")
return DEVICE
diff --git a/tests/sh/test_rocm_cuda_mask_hides_amd_warning.sh b/tests/sh/test_rocm_cuda_mask_hides_amd_warning.sh
new file mode 100755
index 00000000000..3550c4de7ed
--- /dev/null
+++ b/tests/sh/test_rocm_cuda_mask_hides_amd_warning.sh
@@ -0,0 +1,63 @@
+#!/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
+#
+# install.sh warns only when CUDA_VISIBLE_DEVICES="" hid NVIDIA on a mixed host and ROCm torch was picked.
+set -e
+
+SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
+. "$SCRIPT_DIR/_harness.sh"
+INSTALL_SH="$SCRIPT_DIR/../../install.sh"
+
+_FN_FILE=$(mktemp)
+trap 'rm -f "$_FN_FILE"' EXIT
+for _fn in _cvd_hides_nvidia _torch_index_url_leaf _torch_index_url_is_rocm \
+ _warn_if_cuda_mask_hides_amd; do
+ sed -n "/^${_fn}()/,/^}/p" "$INSTALL_SH" >> "$_FN_FILE"
+done
+if ! grep -q '^_warn_if_cuda_mask_hides_amd()' "$_FN_FILE"; then
+ echo "FAIL: could not extract _warn_if_cuda_mask_hides_amd from install.sh"
+ exit 1
+fi
+
+_SH="${BASH:-/bin/bash}"
+ROCM_URL="https://download.pytorch.org/whl/rocm7.1"
+# $1 = physical NVIDIA card (1/0), $2 = index url, $3 = env assignments, $4 = AMD card on PCI (default 1). Stub honours the mask like the real probe.
+_run() {
+ env -u CUDA_VISIBLE_DEVICES -u HIP_VISIBLE_DEVICES -u ROCR_VISIBLE_DEVICES \
+ $3 "$_SH" -c "
+ _has_usable_nvidia_gpu() {
+ _cvd_hides_nvidia && return 1
+ [ '$1' = 1 ]
+ }
+ _amd_gpu_present_via_pci() { [ '${4:-1}' = 1 ]; }
+ . '$_FN_FILE'
+ _warn_if_cuda_mask_hides_amd '$2'
+ " 2>&1
+}
+
+echo "=== The reporter's shape: 3080 + R9700, CUDA_VISIBLE_DEVICES emptied, ROCm index ==="
+_out=$(_run 1 "$ROCM_URL" "CUDA_VISIBLE_DEVICES=")
+assert_contains "warns about the mask" "$_out" 'CUDA_VISIBLE_DEVICES=""'
+assert_contains "names the supported switch" "$_out" "UNSLOTH_FORCE_ROCM_TORCH=1"
+assert_contains "names the HIP override" "$_out" "HIP_VISIBLE_DEVICES=0"
+_out=$(_run 1 "$ROCM_URL" "CUDA_VISIBLE_DEVICES=-1")
+assert_contains "-1 is the same mask" "$_out" 'CUDA_VISIBLE_DEVICES="-1"'
+_out=$(_run 1 "https://repo.amd.com/rocm/whl/gfx120X-all/" "CUDA_VISIBLE_DEVICES=")
+assert_contains "a per-arch gfx index counts" "$_out" "UNSLOTH_FORCE_ROCM_TORCH=1"
+
+echo "=== Everything else stays quiet ==="
+assert_eq "AMD-only host: the mask is deliberate" "" "$(_run 0 "$ROCM_URL" "CUDA_VISIBLE_DEVICES=")"
+assert_eq "NVIDIA-only host with a pinned ROCm index" "" "$(_run 1 "$ROCM_URL" "CUDA_VISIBLE_DEVICES=" 0)"
+assert_eq "HIP_VISIBLE_DEVICES set: HIP ignores CUDA_VISIBLE_DEVICES" "" \
+ "$(_run 1 "$ROCM_URL" "CUDA_VISIBLE_DEVICES= HIP_VISIBLE_DEVICES=0")"
+assert_eq "no mask" "" "$(_run 1 "$ROCM_URL" "")"
+assert_eq "a mask naming a device" "" "$(_run 1 "$ROCM_URL" "CUDA_VISIBLE_DEVICES=0")"
+assert_eq "CUDA index" "" "$(_run 1 "https://download.pytorch.org/whl/cu128" "CUDA_VISIBLE_DEVICES=")"
+assert_eq "cpu index" "" "$(_run 1 "https://download.pytorch.org/whl/cpu" "CUDA_VISIBLE_DEVICES=")"
+assert_eq "mirror whose base path says rocm" "" \
+ "$(_run 1 "https://mirror.example/rocm/whl/cpu" "CUDA_VISIBLE_DEVICES=")"
+
+echo ""
+echo "PASS=$PASS FAIL=$FAIL"
+[ "$FAIL" -eq 0 ]