Skip to content

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

Studio: report the GPUs the OS sees, not just the ones PyTorch opened (#9858)

Daniel Hanc303050c67c4
diff --git a/install.sh b/install.sh
index f6e0e4b336a..320ee76a18c 100755
--- a/install.sh
+++ b/install.sh
@@ -4582,6 +4582,17 @@ while [ -n "$_torch_index_leaf" ] && [ "${_torch_index_leaf%/}" != "$_torch_inde
done
_torch_index_leaf="${_torch_index_leaf##*/}"
_torch_index_leaf=$(printf '%s' "$_torch_index_leaf" | tr '[:upper:]' '[:lower:]')
+# Whether the caller had already STATED a backend before the assignment below overwrites it.
+# setup.sh documents UNSLOTH_TORCH_BACKEND=cpu as the way to keep a deliberate CPU install,
+# and on a GPU-less host the resolved value is cpu too, so without this the manifest cannot
+# tell a stated choice from the automatic answer.
+if [ -n "${UNSLOTH_TORCH_BACKEND:-}" ]; then
+ _torch_backend_was_stated=true
+ _torch_backend_stated_value=$(printf '%s' "$UNSLOTH_TORCH_BACKEND" | tr '[:upper:]' '[:lower:]')
+else
+ _torch_backend_was_stated=false
+ _torch_backend_stated_value=""
+fi
case "$_torch_index_leaf" in
rocm*|gfx*) export UNSLOTH_TORCH_BACKEND="rocm" ;;
cpu) export UNSLOTH_TORCH_BACKEND="cpu" ;;
@@ -4591,6 +4602,22 @@ case "$_torch_index_leaf" in
*) unset UNSLOTH_TORCH_BACKEND ;;
esac
+# Derived from the index this script RESOLVED, which on a GPU-less machine is "cpu" whether
+# or not anyone asked. Without the marker every ordinary Linux CPU install is recorded as a
+# deliberate choice, and a machine that later gains a GPU is never offered the repair.
+# Only when the stated family SURVIVED the resolution. The case above has already
+# overwritten the variable, so a caller who said "cuda" on a machine with no visible GPU
+# now carries the resolved "cpu" -- and treating that as stated records a deliberate CPU
+# flavor for a host that asked for CUDA and did not get it. It would then be denied the
+# repair for good if the GPU ever became visible.
+if [ -n "${UNSLOTH_TORCH_BACKEND:-}" ] &&
+ { [ "$_torch_backend_was_stated" != true ] ||
+ [ "$_torch_backend_stated_value" != "$UNSLOTH_TORCH_BACKEND" ]; }; then
+ export UNSLOTH_TORCH_BACKEND_SOURCE="resolved"
+else
+ unset UNSLOTH_TORCH_BACKEND_SOURCE
+fi
+
# Whether TORCH_INDEX_URL names an actual pip ROCm family (rocm<digit>* / gfx*), gating the
# ROCm-only side effects below (AMD bitsandbytes, ROCm-torch repair). Digit-gated so a leaf
# merely STARTING with "rocm" isn't force-repaired from the wrong path.
diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.py
index ae3be1fecb3..459e4c8f311 100644
--- a/studio/backend/core/inference/llama_cpp.py
+++ b/studio/backend/core/inference/llama_cpp.py
@@ -10773,6 +10773,10 @@ class LlamaCppBackend:
# only when an axis is actually quantized.
_tensor_quant_kv_unsupported_binaries: set[tuple[str, int]] = set()
+ # Binary dirs already reported by _warn_missing_windows_cuda_runtime. The env is rebuilt
+ # for every launch and every --list-devices probe, so one line per binary is enough.
+ _missing_cuda_runtime_warned: set[str] = set()
+
@classmethod
def _binary_key(cls, binary: Optional[str]) -> Optional[tuple[str, int]]:
"""(path, mtime_ns); ns mtime re-probes a same-second binary swap."""
@@ -10896,6 +10900,47 @@ class LlamaCppBackend:
_add(site_packages / "torch" / "lib")
return out
+ @classmethod
+ def _warn_missing_windows_cuda_runtime(cls, binary_dir: str, path_dirs: list[str]) -> None:
+ """Say so when a CUDA llama-server has no cudart to load. Diagnostic only.
+
+ The CUDA prebuilt links ``cudart64_*.dll`` / ``cublas64_*.dll`` and takes them
+ from the managed venv -- ``torch/lib`` or the ``nvidia/*`` wheels, per
+ _windows_pip_nvidia_dll_dirs. A 2.11.0+cpu torch ships neither, so ggml cannot
+ load its CUDA backend and ``llama-server.exe --list-devices`` prints
+ ``Available devices: (none)`` while UNSLOTH_PREBUILT_INFO.json still says
+ ``backend cuda`` (#8473, HF discussion 87). Today that is entirely silent.
+
+ Changes nothing about the launch: the process still starts, still falls back to
+ CPU, and a custom build with the DLLs somewhere else is not second-guessed.
+ Never raises -- a diagnostic must not be able to stop a load.
+ """
+ try:
+ if binary_dir in cls._missing_cuda_runtime_warned:
+ return
+ # Same identification _installed_ggml_backends uses: the official prebuilts are
+ # single-backend, so the ggml CUDA lib beside llama-server IS the build.
+ ggml_cuda = os.path.join(binary_dir, "ggml-cuda.dll")
+ if not os.path.isfile(ggml_cuda):
+ return
+ for directory in path_dirs:
+ try:
+ names = os.listdir(directory)
+ except OSError:
+ continue
+ if any(name.lower().startswith("cudart64_") for name in names):
+ return
+ cls._missing_cuda_runtime_warned.add(binary_dir)
+ logger.warning(
+ "llama.cpp is the CUDA build (%s) but no cudart64_*.dll was found on its "
+ "DLL search path. The CUDA ggml backend will not load and llama-server "
+ "will report no devices. This is what a CPU-only PyTorch in the managed "
+ "environment looks like; repair the installation to restore GPU support.",
+ ggml_cuda,
+ )
+ except Exception as e:
+ logger.debug(f"CUDA runtime DLL diagnostic failed: {e}")
+
@staticmethod
def _build_windows_path_dirs(binary_dir: str, prefix: str, cuda_path: str) -> list[str]:
"""Ordered PATH entries prepended so llama-server.exe resolves cudart /
@@ -10934,6 +10979,13 @@ class LlamaCppBackend:
)
existing_path = env.get("PATH", "")
env["PATH"] = ";".join(path_dirs) + ";" + existing_path
+ # Warn against the FULL search path, inherited entries included: a hand-installed CUDA
+ # toolkit puts cudart64_*.dll on PATH without the venv or CUDA_PATH knowing, and warning
+ # on the prepended directories alone told working custom setups to repair a fine install.
+ LlamaCppBackend._warn_missing_windows_cuda_runtime(
+ binary_dir,
+ path_dirs + [d for d in existing_path.split(";") if d],
+ )
# ROCm: the prebuilt bundles rocblas.dll but NOT the Tensile
# kernel files (rocblas/library/*.dat + *.hsaco); the DLL searches
diff --git a/studio/backend/main.py b/studio/backend/main.py
index b022dd30984..4d261ae7dc6 100644
--- a/studio/backend/main.py
+++ b/studio/backend/main.py
@@ -1573,11 +1573,15 @@ def _hardware_snapshot() -> Optional[tuple[bool, Optional[str], Optional[str]]]:
generation = _hw_module.DETECTION_GENERATION
device = _hw_module.DEVICE
chat_only = bool(_hw_module.CHAT_ONLY)
- reason = getattr(_hw_module, "CHAT_ONLY_REASON", None)
- # Inside the guarded read, with the reason it belongs to. Read after it, a forced
- # re-detect starting in between would pair this reply's reason with a detail from
- # a different pass, or with none at all.
- detail = getattr(_hw_module, "CHAT_ONLY_DETAIL", None)
+ # Refreshed, not the frozen global: the three inventory-sensitive verdicts can change
+ # after startup (an eGPU attached, a driver that finished restarting). Reason and detail
+ # come back together, or a forced re-detect starting in between would pair this reply's
+ # reason with a detail from a different pass.
+ try:
+ reason, detail = _hw_module.current_chat_only_verdict()
+ except Exception:
+ reason = getattr(_hw_module, "CHAT_ONLY_REASON", None)
+ detail = getattr(_hw_module, "CHAT_ONLY_DETAIL", None)
if (
device is not None
and _hw_module.DETECTION_COMPLETE.is_set()
@@ -2069,8 +2073,9 @@ def _get_cached_system_gpu_info(logger) -> tuple[dict[str, Any], dict[str, Any]]
logger.debug(f"Could not resolve gpu_ids support: {e}")
llama_uses_vulkan = False
gpu_ids_supported = True
- # Preserve backend/index metadata from the visibility probe: a CPU training host can expose
- # a Vulkan inference GPU, and the UI must label it Vulkan, not the top-level CPU backend.
+ # The spread also carries `physical_devices` and `mismatch`: GPUs the OS sees that this PyTorch
+ # cannot open (#8473). They stay their own fields, because `devices` below is the runtime-usable
+ # list that model fit budgets against and the training device picker pins from.
gpu_info = {
**visibility_info,
"available": visibility_info.get("available", False),
diff --git a/studio/backend/tests/conftest.py b/studio/backend/tests/conftest.py
index b7935300f07..dd419ee57c5 100644
--- a/studio/backend/tests/conftest.py
+++ b/studio/backend/tests/conftest.py
@@ -942,3 +942,28 @@ def real_prequant_safe_globals(monkeypatch):
monkeypatch.setattr(pq, "_SAFE_GLOBALS_REGISTERED", None)
monkeypatch.setattr(pq, "_RESOLVED_SAFE_GLOBALS", set())
return resolver
+
+
[email protected](autouse = True)
+def _no_carried_over_hardware_measurements():
+ """Both hardware caches start empty for every test, as they do in a fresh process.
+
+ The torch build snapshot and the physical GPU inventory are module globals with a
+ 60 second TTL, so one test's host -- a suite that makes `import torch` fail, say --
+ would otherwise answer for every test that ran within a minute of it. Cleared
+ afterwards as well, so a test that warms one deliberately does not leak either.
+ """
+ from utils.hardware import hardware as _hw
+
+ def _clear():
+ # Under the locks: a non-blocking read hands the refresh to a daemon thread that holds
+ # these while it writes, so clearing without waiting lets a previous test's REAL host land
+ # in the cache a moment later. Torch lock FIRST, then the inventory lock, because that is
+ # the order the background refresh takes them in.
+ with _hw._torch_build_snapshot_lock, _hw._physical_gpu_inventory_lock:
+ _hw._torch_build_snapshot_cache = None
+ _hw._physical_gpu_inventory_cache = None
+
+ _clear()
+ yield
+ _clear()
diff --git a/studio/backend/tests/test_torch_cpu_build_on_nvidia_host.py b/studio/backend/tests/test_torch_cpu_build_on_nvidia_host.py
new file mode 100644
index 00000000000..521eff3fdf3
--- /dev/null
+++ b/studio/backend/tests/test_torch_cpu_build_on_nvidia_host.py
@@ -0,0 +1,2818 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""A host whose GPUs PyTorch cannot use must not be reported as having no GPUs.
+
+Every GPU field in utils/hardware is gated on ``torch.cuda.is_available()``, so a
+managed environment that ends up with a CPU-only wheel goes silent about hardware
+that is plainly still there. Two Windows users hit exactly that after an in-app
+update resolved torch from PyPI (whose Windows default is ``2.11.0+cpu``) over a
+``cu124`` wheel: Settings > System showed ``VRAM --`` and "No visible GPU" while
+``nvidia-smi`` listed both RTX A4000s, ``UNSLOTH_PREBUILT_INFO.json`` still said
+``backend cuda``, and models ran on CPU (#8473, HF discussion 87).
+
+What these pin:
+ * the nvidia-smi inventory is read even though ``DEVICE`` is CPU;
+ * ``devices`` stays EMPTY regardless. That list is what the model-fit estimate
+ budgets against and what the training device picker pins from, so a card torch
+ cannot open must never be merged into it. The inventory travels beside it, in
+ ``physical_devices`` / ``mismatch``;
+ * ``CHAT_ONLY_REASON`` stops being ``"no_gpu"``, because on this host that is
+ false and the advice it implies ("get a GPU") is not the fix;
+ * a CPU-only wheel and an accelerator wheel whose runtime will not start stay
+ distinct reasons -- one is repaired by reinstalling torch, the other by the
+ driver;
+ * a host that genuinely has no GPU is unaffected, and a probe that cannot answer
+ (no nvidia-smi, malformed CSV, a comma inside a device name) degrades to a
+ structured unavailable result rather than raising out of the endpoint.
+
+No AMD/Windows hardware exists here, so torch and nvidia-smi are faked in the shapes
+the reports describe; the Windows adapter half is exercised through its own map.
+"""
+
+from __future__ import annotations
+
+import inspect
+import json
+import pathlib
+import subprocess
+import types
+from types import SimpleNamespace
+from unittest.mock import patch
+
+import pytest
+
+import main
+import utils.hardware as hardware_pkg
+import utils.hardware.hardware as hw
+from utils.hardware import nvidia
+
+# nvidia-smi rows for User A's box: two A4000s, the second carrying a comma in its name.
+_TWO_A4000_ROWS = "\n".join(
+ [
+ "0, NVIDIA RTX A4000, 16376",
+ "1, NVIDIA RTX A4000, Founders Edition, 16376",
+ ]
+)
+
+
+def _fake_torch(vendor: str):
+ """A fake ``torch``, in the vendor shapes the reports describe.
+
+ "cpu" -- the PyPI Windows default an unconstrained update resolves to.
+ "cuda_dead" -- a cu124 wheel whose runtime refuses to initialise.
+ "cuda" -- a healthy CUDA wheel.
+ """
+ torch = types.ModuleType("torch")
+ if vendor == "cpu":
+ torch.version = SimpleNamespace(hip = None, cuda = None)
+ torch.__version__ = "2.11.0+cpu"
+ available = False
+ elif vendor == "cuda_dead":
+ torch.version = SimpleNamespace(hip = None, cuda = "12.4")
+ torch.__version__ = "2.6.0+cu124"
+ available = False
+ else:
+ torch.version = SimpleNamespace(hip = None, cuda = "12.8")
+ torch.__version__ = "2.9.1+cu128"
+ available = True
+ torch.cuda = SimpleNamespace(
+ is_available = lambda: available,
+ device_count = lambda: 2 if available else 0,
+ )
+ return torch
+
+
+def _smi(
+ monkeypatch,
+ stdout: str,
+ *,
+ returncode: int = 0,
+ raises: "type[BaseException] | None" = None,
+):
+ """Pin what nvidia-smi answers, at the subprocess boundary.
+
+ The procfs count goes with it. A CLI that cannot answer falls back to
+ /proc/driver/nvidia/gpus, and this suite runs on a real NVIDIA host, so a test
+ simulating a machine with no cards has to simulate the kernel driver's absence too.
+ The fallback's own tests set it back.
+ """
+
+ def _run(*_args, **_kwargs):
+ # `raises` is how an ABSENT binary is spelled: FileNotFoundError is an answer
+ # (every AMD, Intel and CPU host), while a nonzero exit is a probe that failed.
+ if raises is not None:
+ raise raises("nvidia-smi")
+ return SimpleNamespace(returncode = returncode, stdout = stdout)
+
+ monkeypatch.setattr(nvidia.subprocess, "run", _run)
+ monkeypatch.setattr(nvidia, "_linux_nvidia_procfs_gpu_count", lambda: 0)
+
+
[email protected](autouse = True)
+def _no_inherited_visibility_mask(monkeypatch):
+ """An emptied mask is a deliberate CPU pin and suppresses the whole report, so a
+ runner that exports one (a GPU-partitioning CI job) would silently void these."""
+ for var in ("CUDA_VISIBLE_DEVICES", "HIP_VISIBLE_DEVICES", "ROCR_VISIBLE_DEVICES"):
+ monkeypatch.delenv(var, raising = False)
+
+
[email protected](autouse = True)
+def _no_carried_over_torch_measurement(monkeypatch):
+ """The torch snapshot is cached with a TTL, so one test's fake host would answer
+ for the next one. Both caches start empty here, as they do in a fresh process."""
+ monkeypatch.setattr(hw, "_torch_build_snapshot_cache", None)
+ monkeypatch.setattr(hw, "_physical_gpu_inventory_cache", None)
+
+
[email protected](autouse = True)
+def _no_background_inventory_refresh(monkeypatch):
+ """Keep the non-blocking refresh from probing the REAL host mid-test.
+
+ get_physical_gpu_inventory(block=False) hands a stale or cold cache to a daemon
+ thread, which is the whole point on a request path, but in a test it races the
+ assertions and can drop this machine's actual nvidia-smi output into the cache.
+ The tests that assert on the scheduling patch Thread themselves.
+ """
+
+ class _NoThread:
+ def __init__(self, *args, **kwargs):
+ pass
+
+ def start(self):
+ pass
+
+ monkeypatch.setattr(hw.threading, "Thread", _NoThread)
+ monkeypatch.setattr(hw, "_physical_gpu_inventory_refreshing", False)
+
+
+def cpu_torch_on_an_nvidia_host(monkeypatch):
+ """torch reports no accelerator; nvidia-smi lists two A4000s."""
+ monkeypatch.setitem(__import__("sys").modules, "torch", _fake_torch("cpu"))
+ monkeypatch.setattr(hw, "TORCH_IMPORT_ERROR", None)
+ monkeypatch.setattr(hw, "IS_ROCM", False)
+ monkeypatch.setattr(hw.platform, "system", lambda: "Linux")
+ # Cached for 60s, so a test that did not clear it would read the real host's answer.
+ _smi(monkeypatch, _TWO_A4000_ROWS)
+
+
+def test_a_card_whose_capacity_is_unreported_is_still_a_card(monkeypatch):
+ """ "[N/A]" for memory.total is a missing metric, not a missing GPU.
+
+ Dropping the row took the card out of the whole inventory, and the Linux procfs
+ fallback does not cover it either: that answers for a query that FAILED, not one that
+ came back short. The host lost its mismatch and its repair guidance over a size.
+ """
+ monkeypatch.setattr(hw.platform, "system", lambda: "Linux")
+ monkeypatch.setattr(hw, "_physical_gpu_inventory_cache", None)
+ _smi(monkeypatch, "0, NVIDIA RTX A4000, [N/A]\n1, NVIDIA RTX A4000, 16376\n")
+
+ inventory = hw.get_physical_gpu_inventory()
+
+ assert [d["name"] for d in inventory["devices"]] == ["NVIDIA RTX A4000"] * 2
+ assert [d["memory_total_gb"] for d in inventory["devices"]] == [None, 15.99]
+ assert inventory["unknown"] is False
+ assert hw._devices_that_can_establish_a_mismatch(inventory["devices"]) == inventory["devices"]
+
+
+def test_the_physical_probe_runs_without_a_cuda_device(cpu_torch_on_an_nvidia_host):
+ inventory = hw.get_physical_gpu_inventory()
+
+ assert inventory["available"] is True
+ assert inventory["sources"] == ["nvidia-smi"]
+ assert [device["name"] for device in inventory["devices"]] == [
+ "NVIDIA RTX A4000",
+ "NVIDIA RTX A4000, Founders Edition",
+ ]
+ assert [device["memory_total_gb"] for device in inventory["devices"]] == [15.99, 15.99]
+ assert {device["vendor"] for device in inventory["devices"]} == {"nvidia"}
+
+
+ ("stdout", "returncode", "failure"),
+ [
+ ("", 0, None),
+ ("", 9, None),
+ ("nonsense\n0, only two columns\n", 0, None),
+ ("", 0, FileNotFoundError("nvidia-smi")),
+ ("", 0, subprocess.TimeoutExpired("nvidia-smi", 10)),
+ ],
+)
+def test_a_probe_that_cannot_answer_returns_a_result_rather_than_raising(
+ monkeypatch, stdout, returncode, failure
+):
+ if failure is None:
+ _smi(monkeypatch, stdout, returncode = returncode)
+ else:
+
+ def _raise(*_args, **_kwargs):
+ raise failure
+
+ monkeypatch.setattr(nvidia.subprocess, "run", _raise)
+ # This suite runs on a real NVIDIA host, so pin procfs empty: the case under test is a
+ # machine with no NVIDIA driver at all.
+ monkeypatch.setattr(nvidia, "_linux_nvidia_procfs_gpu_count", lambda: 0)
+ monkeypatch.setattr(hw.platform, "system", lambda: "Linux")
+
+ result = nvidia.get_physical_gpu_inventory()
+ assert result["available"] is False
+ assert result["devices"] == []
+
+ inventory = hw.get_physical_gpu_inventory()
+ assert inventory["available"] is False
+ assert inventory["devices"] == []
+ assert inventory["sources"] == []
+ # "The driver answered and there are no cards" is not "no probe could answer". An absent
+ # nvidia-smi is the exception: it is the normal state of every AMD, Intel and CPU host.
+ _could_not_answer = returncode != 0 or (
+ failure is not None and not isinstance(failure, FileNotFoundError)
+ )
+ assert inventory["unknown"] is _could_not_answer
+
+
+def test_the_windows_amd_adapters_are_inventoried_too(monkeypatch):
+ # No vendor CLI is guaranteed on Windows AMD, so the DirectX registry map is the source.
+ monkeypatch.setattr(hw.platform, "system", lambda: "Windows")
+ _smi(monkeypatch, "", returncode = 9)
+ monkeypatch.setattr(
+ hw,
+ "_windows_live_adapter_names",
+ lambda: ["AMD Radeon RX 7900 XT", "AMD Radeon(TM) Graphics"],
+ )
+ monkeypatch.setattr(
+ hw,
+ "_windows_amd_adapter_records_by_luid",
+ lambda vendor_id = hw._AMD_PCI_VENDOR_ID, **_kw: (
+ {
+ 0x24CF5: {
+ "name": "AMD Radeon RX 7900 XT",
+ "dedicated_memory_bytes": 20 * 1024**3,
+ },
+ # No dedicated-memory value: unknown capacity, not an empty card.
+ 0x14CF5: {"name": "AMD Radeon(TM) Graphics"},
+ }
+ if vendor_id == hw._AMD_PCI_VENDOR_ID
+ else {}
+ ),
+ )
+
+ inventory = hw.get_physical_gpu_inventory()
+
+ assert inventory["sources"] == ["directx-registry"]
+ assert [(d["name"], d["memory_total_gb"]) for d in inventory["devices"]] == [
+ ("AMD Radeon(TM) Graphics", None),
+ ("AMD Radeon RX 7900 XT", 20.0),
+ ]
+
+
+def test_a_cpu_wheel_and_a_dead_cuda_wheel_are_different_reasons(monkeypatch):
+ import sys
+
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cpu"))
+ assert hw.classify_torch_build() == "torch_cpu_build"
+
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cuda_dead"))
+ assert hw.classify_torch_build() == "torch_cuda_unavailable"
+
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cuda"))
+ assert hw.classify_torch_build() is None
+
+
+def test_an_untagged_wheel_is_only_a_cpu_build_when_it_names_no_runtime(monkeypatch):
+ import sys
+
+ untagged = _fake_torch("cpu")
+ untagged.__version__ = "2.9.0"
+ monkeypatch.setitem(sys.modules, "torch", untagged)
+ assert hw.classify_torch_build() == "torch_cpu_build"
+
+ conda = _fake_torch("cuda_dead")
+ conda.__version__ = "2.6.0"
+ monkeypatch.setitem(sys.modules, "torch", conda)
+ assert hw.classify_torch_build() == "torch_cuda_unavailable"
+
+
[email protected]("var", ["CUDA_VISIBLE_DEVICES"])
[email protected]("mask", ["", " ", "-1"])
+def test_a_deliberately_emptied_mask_is_not_a_broken_install(monkeypatch, var, mask):
+ # Hiding the GPUs produces exactly the shape this feature keys on (torch sees none,
+ # nvidia-smi sees them all) without anything being broken.
+ import sys
+
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cuda_dead"))
+ _smi(monkeypatch, _TWO_A4000_ROWS)
+ monkeypatch.setenv(var, mask)
+
+ assert hw.classify_torch_build() is None
+ assert hw._torch_gpu_mismatch_report() == {}
+
+ monkeypatch.setenv(var, "0")
+ assert hw.classify_torch_build() == "torch_cuda_unavailable"
+
+
+def test_a_runtime_that_raises_on_its_own_probe_counts_as_unavailable(monkeypatch):
+ import sys
+
+ hostile = _fake_torch("cuda_dead")
+
+ def _boom():
+ raise RuntimeError("CUDA driver version is insufficient for CUDA runtime version")
+
+ hostile.cuda.is_available = _boom
+ monkeypatch.setitem(sys.modules, "torch", hostile)
+ assert hw.classify_torch_build() == "torch_cuda_unavailable"
+
+
+def _detect(monkeypatch):
+ """Run one detection pass with the globals restored afterwards."""
+ 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:
+ return hw._detect_hardware_locked()
+
+
+def test_chat_only_stops_claiming_this_host_has_no_gpu(monkeypatch, cpu_torch_on_an_nvidia_host):
+ device = _detect(monkeypatch)
+
+ assert device == hw.DeviceType.CPU
+ assert hw.CHAT_ONLY is True
+ assert hw.CHAT_ONLY_REASON == "torch_cpu_build"
+ assert hw.CHAT_ONLY_REASON != "no_gpu"
+ assert hw.CHAT_ONLY_DETAIL == "2.11.0+cpu"
+
+
+def test_a_host_that_really_has_no_gpu_still_reads_no_gpu(monkeypatch):
+ import sys
+
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cpu"))
+ monkeypatch.setattr(hw, "TORCH_IMPORT_ERROR", None)
+ monkeypatch.setattr(hw, "IS_ROCM", False)
+ monkeypatch.setattr(hw.platform, "system", lambda: "Linux")
+ _smi(monkeypatch, "", returncode = 9)
+
+ _detect(monkeypatch)
+
+ assert hw.CHAT_ONLY_REASON == "no_gpu"
+ assert hw.CHAT_ONLY_DETAIL is None
+ assert hw._torch_gpu_mismatch_report() == {}
+
+
+def test_a_healthy_cuda_host_reports_no_mismatch_at_all(monkeypatch):
+ import sys
+
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cuda"))
+ _smi(monkeypatch, _TWO_A4000_ROWS)
+
+ assert hw._torch_gpu_mismatch_report() == {}
+
+
+def _system_gpu_info(monkeypatch):
+ """(gpu, inference_gpu) from main, with the real visibility probe in place."""
+ monkeypatch.setattr(
+ hardware_pkg,
+ "get_visible_gpu_utilization",
+ lambda: {"available": False, "backend": "cpu", "devices": []},
+ )
+ monkeypatch.setattr(hardware_pkg, "get_vulkan_inference_gpu_info", lambda: None)
+ monkeypatch.setattr(hw, "get_device", lambda: hw.DeviceType.CPU)
+ monkeypatch.setattr(main, "_system_gpu_cache", None)
+ # Warm the inventory the way startup does: the mismatch report reads it WITHOUT
+ # blocking, because /api/system is polled every three seconds, so a cold cache answers
+ # unknown. In a running backend _detect_hardware_locked has already blocked once.
+ hw.get_physical_gpu_inventory()
+ hw.torch_build_snapshot()
+ return main._get_cached_system_gpu_info(SimpleNamespace(debug = lambda *args: None))
+
+
+def test_the_system_endpoint_names_the_cards_without_offering_them(
+ monkeypatch, cpu_torch_on_an_nvidia_host
+):
+ gpu, _inference_gpu = _system_gpu_info(monkeypatch)
+
+ # The hard constraint: `devices` is the runtime-usable list that model fit budgets
+ # against and the training picker pins from.
+ assert gpu["devices"] == []
+ assert gpu["available"] is False
+
+ assert gpu["mismatch"]["reason"] == "torch_cpu_build"
+ assert gpu["mismatch"]["torch_version"] == "2.11.0+cpu"
+ assert gpu["mismatch"]["physical_count"] == 2
+ assert gpu["mismatch"]["sources"] == ["nvidia-smi"]
+ assert [device["name"] for device in gpu["physical_devices"]] == [
+ "NVIDIA RTX A4000",
+ "NVIDIA RTX A4000, Founders Edition",
+ ]
+ assert hw.get_backend_visible_gpu_info()["physical_devices"] == gpu["physical_devices"]
+
+
+def test_a_cpu_host_with_no_cards_publishes_neither_field(monkeypatch):
+ import sys
+
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cpu"))
+ monkeypatch.setattr(hw, "TORCH_IMPORT_ERROR", None)
+ monkeypatch.setattr(hw, "IS_ROCM", False)
+ monkeypatch.setattr(hw.platform, "system", lambda: "Linux")
+ _smi(monkeypatch, "", returncode = 9)
+
+ gpu, _inference_gpu = _system_gpu_info(monkeypatch)
+
+ assert gpu["devices"] == []
+ assert "mismatch" not in gpu
+ assert "physical_devices" not in gpu
+
+
[email protected]("local", ["cpu", "cpu.cxx11.abi", "cpu.cxx11abi", "CPU"])
+def test_extended_cpu_local_tags_are_still_cpu_builds(monkeypatch, local):
+ """PyTorch publishes CPU wheels whose local tag carries a suffix.
+
+ An exact "cpu" match called those CUDA wheels whose runtime failed to start, and
+ the UI then pointed the user at a driver rather than at the reinstall that is the
+ actual fix.
+ """
+ import sys
+
+ wheel = _fake_torch("cpu")
+ wheel.__version__ = f"2.8.0+{local}"
+ monkeypatch.setitem(sys.modules, "torch", wheel)
+ assert hw.classify_torch_build() == "torch_cpu_build"
+
+
+def test_a_cuda_local_tag_is_not_mistaken_for_a_cpu_one(monkeypatch):
+ import sys
+
+ wheel = _fake_torch("cuda_dead")
+ wheel.__version__ = "2.6.0+cu124"
+ monkeypatch.setitem(sys.modules, "torch", wheel)
+ assert hw.classify_torch_build() == "torch_cuda_unavailable"
+
+
+def test_export_and_video_stop_saying_no_accelerator_was_found(monkeypatch):
+ """Those two pages render the message verbatim, so it has to match the System tab.
+
+ Telling a two-A4000 host that no supported accelerator was found contradicts the
+ inventory the same server just published, and points at hardware instead of at the
+ repair.
+ """
+ monkeypatch.setattr(hw, "DEVICE", hw.DeviceType.CPU)
+ monkeypatch.setattr(hw, "CHAT_ONLY_REASON", "torch_cpu_build")
+ monkeypatch.setattr(hw, "CHAT_ONLY_DETAIL", "2.11.0+cpu")
+ monkeypatch.setattr(hw, "_has_torch", lambda: True)
+ monkeypatch.setattr(hw, "is_apple_silicon", lambda: False)
+ monkeypatch.setattr(hw.platform, "system", lambda: "Linux")
+ # The verdict is re-derived against the live inventory, so the cards have to be in
+ # it. Without them the honest answer really is that this host has no GPU.
+ monkeypatch.setattr(
+ hw, "current_chat_only_verdict", lambda: (hw.CHAT_ONLY_REASON, hw.CHAT_ONLY_DETAIL)
+ )
+
+ export = hw.export_capability()
+ assert export["export_supported"] is False
+ assert export["export_unsupported_reason"] == "torch_cpu_build"
+ assert "2.11.0+cpu" in export["export_unsupported_message"]
+ assert "No supported" not in export["export_unsupported_message"]
+
+ video = hw.video_capability()
+ assert video["video_supported"] is False
+ assert video["video_unsupported_reason"] == "torch_cpu_build"
+ assert "No supported accelerator" not in video["video_unsupported_message"]
+
+ monkeypatch.setattr(hw, "CHAT_ONLY_REASON", "torch_cuda_unavailable")
+ monkeypatch.setattr(hw, "CHAT_ONLY_DETAIL", "2.6.0+cu124")
+ assert hw.export_capability()["export_unsupported_reason"] == "torch_cuda_unavailable"
+ assert "driver" in hw.video_capability()["video_unsupported_message"]
+
+
+def test_a_genuinely_gpu_less_host_keeps_the_old_wording(monkeypatch):
+ monkeypatch.setattr(hw, "DEVICE", hw.DeviceType.CPU)
+ # This suite runs on a real NVIDIA box, so without these the verdict refresh correctly
+ # notices the accelerator this test is pretending not to have.
+ monkeypatch.setattr(hw, "get_device", lambda: hw.DeviceType.CPU)
+ monkeypatch.setattr(hw, "_torch_reports_a_usable_accelerator", lambda: False)
+ monkeypatch.setattr(hw, "classify_torch_build", lambda **_kw: None)
+ monkeypatch.setattr(
+ hw,
+ "get_physical_gpu_inventory",
+ lambda **_kw: {"available": False, "devices": [], "unknown": False},
+ )
+ monkeypatch.setattr(hw, "CHAT_ONLY_REASON", "no_gpu")
+ monkeypatch.setattr(hw, "CHAT_ONLY_DETAIL", None)
+ monkeypatch.setattr(hw, "_has_torch", lambda: True)
+ monkeypatch.setattr(hw, "is_apple_silicon", lambda: False)
+ monkeypatch.setattr(hw.platform, "system", lambda: "Linux")
+
+ assert hw.export_capability()["export_unsupported_reason"] == "no_accelerator"
+ assert hw.video_capability()["video_unsupported_reason"] == "no_accelerator"
+
+
+def test_nvidia_smi_is_resolved_from_the_standard_windows_locations(monkeypatch):
+ """A driver install can leave nvidia-smi.exe off PATH entirely.
+
+ The bare name then raises FileNotFoundError and the inventory comes back empty on
+ exactly the host this feature exists for. setup.ps1 already falls back to these two
+ paths, so the backend has to as well.
+ """
+ monkeypatch.setattr(nvidia.platform, "system", lambda: "Windows")
+ monkeypatch.setattr(nvidia.shutil, "which", lambda _name: None)
+ monkeypatch.setenv("ProgramFiles", r"C:\Program Files")
+ monkeypatch.setenv("SystemRoot", r"C:\Windows")
+
+ nvsmi = nvidia.os.path.join(r"C:\Program Files", r"NVIDIA Corporation\NVSMI\nvidia-smi.exe")
+ monkeypatch.setattr(nvidia.os.path, "isfile", lambda p: p == nvsmi)
+ assert nvidia._nvidia_smi_executable() == nvsmi
+
+ system32 = nvidia.os.path.join(r"C:\Windows", r"System32\nvidia-smi.exe")
+ monkeypatch.setattr(nvidia.os.path, "isfile", lambda p: p == system32)
+ assert nvidia._nvidia_smi_executable() == system32
+
+ monkeypatch.setattr(nvidia.os.path, "isfile", lambda _p: False)
+ assert nvidia._nvidia_smi_executable() == "nvidia-smi"
+
+
+def test_path_resolution_is_a_no_op_off_windows_and_when_path_has_it(monkeypatch):
+ monkeypatch.setattr(nvidia.platform, "system", lambda: "Linux")
+ monkeypatch.setattr(nvidia.shutil, "which", lambda _name: None)
+ assert nvidia._nvidia_smi_executable() == "nvidia-smi"
+
+ monkeypatch.setattr(nvidia.platform, "system", lambda: "Windows")
+ monkeypatch.setattr(nvidia.shutil, "which", lambda _name: "/usr/bin/nvidia-smi")
+ assert nvidia._nvidia_smi_executable() == "/usr/bin/nvidia-smi"
+
+
[email protected]("mask", ["", " ", "-1"])
[email protected]("var", ["HIP_VISIBLE_DEVICES", "ROCR_VISIBLE_DEVICES"])
+def test_a_hip_mask_only_counts_where_it_can_hide_something(monkeypatch, var, mask):
+ """A mask that cannot take effect must not silence the mismatch.
+
+ Windows HIP has no ROCr layer, and this module's own visibility resolver already
+ ignores ROCR_VISIBLE_DEVICES there. A stray empty one on a Windows NVIDIA host
+ would otherwise restore exactly the "no GPU" verdict this feature exists to
+ correct. The HIP variables likewise address AMD devices, so on an NVIDIA-only
+ inventory they mask nothing.
+ """
+ import sys
+
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cuda_dead"))
+ _smi(monkeypatch, _TWO_A4000_ROWS)
+ monkeypatch.setattr(hw.platform, "system", lambda: "Linux")
+ # sys.platform too, not just platform.system(): the ROCR row is gated on sys.platform
+ # (hardware.py's own resolver reads it), so on a Windows runner ROCR is correctly
+ # ignored and this row asserted the opposite of what that host should do.
+ monkeypatch.setattr(hw.sys, "platform", "linux")
+ monkeypatch.setenv(var, mask)
+ # Prime the cache: the mask set reads the inventory WITHOUT blocking (it is reached
+ # from the verdict /api/liveness reads), and a cold cache keeps every mask.
+ hw.get_physical_gpu_inventory()
+
+ assert hw.classify_torch_build() == "torch_cuda_unavailable"
+
+ monkeypatch.setattr(
+ hw,
+ "get_physical_gpu_inventory",
+ lambda **_kw: {"available": True, "devices": [{"vendor": "amd"}], "sources": ["x"]},
+ )
+ assert hw.classify_torch_build() is None
+
+
+def test_rocr_is_ignored_on_windows_even_on_an_amd_host(monkeypatch):
+ import sys
+
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cuda_dead"))
+ monkeypatch.setattr(hw.sys, "platform", "win32")
+ monkeypatch.setattr(
+ hw,
+ "get_physical_gpu_inventory",
+ lambda **_kw: {"available": True, "devices": [{"vendor": "amd"}], "sources": ["x"]},
+ )
+ monkeypatch.setenv("ROCR_VISIBLE_DEVICES", "")
+ assert hw.classify_torch_build() == "torch_cuda_unavailable"
+
+ monkeypatch.delenv("ROCR_VISIBLE_DEVICES")
+ monkeypatch.setenv("HIP_VISIBLE_DEVICES", "")
+ assert hw.classify_torch_build() is None
+
+
+def test_an_inventory_that_answers_nothing_keeps_every_mask(monkeypatch):
+ import sys
+
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cuda_dead"))
+ monkeypatch.setattr(hw.sys, "platform", "linux")
+ monkeypatch.setattr(
+ hw, "get_physical_gpu_inventory", lambda **_kw: {"available": False, "devices": []}
+ )
+ monkeypatch.setenv("ROCR_VISIBLE_DEVICES", "")
+ assert hw.classify_torch_build() is None
+
+
+def test_a_deliberate_cpu_install_is_not_reported_as_broken(monkeypatch, tmp_path):
+ """Pinning /cpu on a machine that has a GPU is a supported thing to do.
+
+ No mask is empty in that case, so the classifier called the wheel the user asked
+ for broken, and the UI offered a repair whose only effect would be to replace it.
+ """
+ import sys
+
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cpu"))
+ monkeypatch.delenv("UNSLOTH_TORCH_INDEX_FAMILY", raising = False)
+ monkeypatch.delenv("UNSLOTH_TORCH_INDEX_URL", raising = False)
+ monkeypatch.setattr(hw.sys, "prefix", str(tmp_path))
+
+ assert hw.classify_torch_build() == "torch_cpu_build"
+
+ monkeypatch.setenv("UNSLOTH_TORCH_INDEX_FAMILY", "cpu")
+ assert hw.classify_torch_build() is None
+ monkeypatch.delenv("UNSLOTH_TORCH_INDEX_FAMILY")
+
+ monkeypatch.setenv("UNSLOTH_TORCH_INDEX_URL", "https://download.pytorch.org/whl/cpu/")
+ assert hw.classify_torch_build() is None
+ monkeypatch.delenv("UNSLOTH_TORCH_INDEX_URL")
+
+ monkeypatch.setenv("UNSLOTH_TORCH_INDEX_FAMILY", "cu124")
+ assert hw.classify_torch_build() == "torch_cpu_build"
+ monkeypatch.delenv("UNSLOTH_TORCH_INDEX_FAMILY")
+
+ # A recorded cpu counts only when the record says someone NAMED it: setup.ps1 selects
+ # /cpu automatically on a GPU-less host and records it identically.
+ manifest = tmp_path / "unsloth_install_manifest.json"
+ manifest.write_text('{"schema": 1, "expected_torch_tag": "cpu"}', encoding = "utf-8")
+ assert hw.classify_torch_build() == "torch_cpu_build"
+
+ manifest.write_text(
+ '{"schema": 1, "expected_torch_tag": "cpu", "expected_torch_tag_pinned": true}',
+ encoding = "utf-8",
+ )
+ assert hw.classify_torch_build() is None
+
+ manifest.write_text(
+ '{"schema": 1, "expected_torch_tag": "cpu", "expected_torch_tag_pinned": false}',
+ encoding = "utf-8",
+ )
+ assert (
+ hw.classify_torch_build() == "torch_cpu_build"
+ ), "an automatic CPU selection is not a choice to protect"
+
+ manifest.write_text('{"schema": 1, "expected_torch_tag": "cu124"}', encoding = "utf-8")
+ assert hw.classify_torch_build() == "torch_cpu_build"
+
+ manifest.write_text('{"schema": 1}', encoding = "utf-8")
+ assert hw.classify_torch_build() == "torch_cpu_build"
+ manifest.write_text("{not json", encoding = "utf-8")
+ assert hw.classify_torch_build() == "torch_cpu_build"
+
+
+def test_a_dead_accelerator_wheel_is_unaffected_by_a_cpu_record(monkeypatch, tmp_path):
+ import sys
+
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cuda_dead"))
+ monkeypatch.setattr(hw.sys, "prefix", str(tmp_path))
+ monkeypatch.setenv("UNSLOTH_TORCH_INDEX_FAMILY", "cpu")
+ assert hw.classify_torch_build() is None # the pin is honoured first
+
+ monkeypatch.delenv("UNSLOTH_TORCH_INDEX_FAMILY")
+ assert hw.classify_torch_build() == "torch_cuda_unavailable"
+
+
+def test_linux_amd_and_intel_cards_are_inventoried_from_sysfs(monkeypatch, tmp_path):
+ """The AMD/Linux shape of #8473: nvidia-smi contributes nothing.
+
+ sysfs rather than amd-smi, because amd-smi is separate ROCm userspace and this is
+ the host whose ROCm install is in question. vendor 0x1002 and a byte-valued
+ mem_info_vram_total are both documented amdgpu interfaces.
+ """
+ drm = tmp_path / "drm"
+ for name, vendor, vram in (
+ ("card0", "0x1002\n", str(16 * 1024**3)),
+ ("card1", "0x1002\n", "0"), # an APU with no dedicated VRAM
+ ("card2", "0x10de\n", str(1024**3)), # NVIDIA: nvidia-smi's business, not this
+ ("card4", "0x8086\n", ""), # an Arc card: no vram total published
+ ("card3", "0x1002\n", "not a number"),
+ ):
+ device = drm / name / "device"
+ device.mkdir(parents = True)
+ (device / "vendor").write_text(vendor, encoding = "utf-8")
+ (device / "mem_info_vram_total").write_text(vram, encoding = "utf-8")
+ (drm / "card0-DP-1").mkdir()
+ (drm / "card9").mkdir()
+
+ real_listdir = hw.os.listdir
+ monkeypatch.setattr(
+ hw.os,
+ "listdir",
+ lambda p: real_listdir(str(drm)) if p == "/sys/class/drm" else real_listdir(p),
+ )
+ real_join = hw.os.path.join
+ monkeypatch.setattr(
+ hw.os.path,
+ "join",
+ lambda *parts: (
+ real_join(str(drm), *parts[1:]) if parts[0] == "/sys/class/drm" else real_join(*parts)
+ ),
+ )
+
+ records = hw._linux_drm_sysfs_records()
+ assert [r["index"] for r in records] == [0, 1, 2, 3]
+ assert [r["vendor"] for r in records] == ["amd", "amd", "amd", "intel"]
+ assert all(r["source"] == "sysfs-drm" for r in records)
+ assert records[0]["memory_total_gb"] == 16.0
+ assert records[1]["memory_total_gb"] is None
+ assert records[2]["memory_total_gb"] is None
+ assert records[3]["memory_total_gb"] is None
+
+
+def test_the_sysfs_probe_is_silent_where_there_is_no_sysfs(monkeypatch):
+ monkeypatch.setattr(hw.os, "listdir", lambda _p: (_ for _ in ()).throw(OSError("no such path")))
+ assert hw._linux_drm_sysfs_records() == []
+
+
+def test_a_token_authenticated_cpu_pin_is_still_a_cpu_pin(monkeypatch, tmp_path):
+ """A pinned index may carry its credential in the query, which is supported.
+
+ A raw final-segment split sees "cpu?token=..." there, so the deliberate CPU build on
+ a GPU host was reported as broken and offered a repair that would replace it.
+ """
+ import sys
+
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cpu"))
+ monkeypatch.setattr(hw.sys, "prefix", str(tmp_path))
+ monkeypatch.delenv("UNSLOTH_TORCH_INDEX_FAMILY", raising = False)
+
+ for pinned in (
+ "https://download.pytorch.org/whl/cpu?token=abc/",
+ "https://mirror.corp.example/whl/cpu#sha256=deadbeef",
+ "https://mirror.corp.example/whl/cpu//",
+ "https://user:[email protected]/whl/CPU",
+ ):
+ monkeypatch.setenv("UNSLOTH_TORCH_INDEX_URL", pinned)
+ assert hw.classify_torch_build() is None, pinned
+
+ monkeypatch.setenv("UNSLOTH_TORCH_INDEX_URL", "https://mirror.corp.example/whl/cu128?token=abc")
+ assert hw.classify_torch_build() == "torch_cpu_build"
+
+
+ "url,leaf",
+ [
+ ("https://download.pytorch.org/whl/cpu?token=x/", "cpu"),
+ ("https://download.pytorch.org/whl/cu128//", "cu128"),
+ ("https://download.pytorch.org/whl/cpu#f", "cpu"),
+ ("", ""),
+ (" ", ""),
+ ],
+)
+def test_the_leaf_reader_matches_the_installers(url, leaf):
+ assert hw._torch_index_leaf(url) == leaf
+
+
+def test_the_chat_only_verdict_follows_the_inventory(monkeypatch):
+ """The inventory refreshes on a 60s TTL; the startup verdict never did.
+
+ An eGPU attached after launch, or a driver that finished restarting, left the
+ sidebar and the Export and Video pages saying no accelerator exists while
+ /api/system listed the card and published a mismatch.
+ """
+ import sys
+
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cpu"))
+ monkeypatch.setattr(hw, "CHAT_ONLY_REASON", "no_gpu")
+ monkeypatch.setattr(hw, "CHAT_ONLY_DETAIL", None)
+ monkeypatch.setattr(
+ hw, "get_physical_gpu_inventory", lambda **_kw: {"devices": [{"vendor": "nvidia"}]}
+ )
+ # The verdict never probes torch inline; measure this fake host as detection would.
+ hw.torch_build_snapshot()
+ assert hw.current_chat_only_verdict() == ("torch_cpu_build", "2.11.0+cpu")
+
+ monkeypatch.setattr(hw, "CHAT_ONLY_REASON", "torch_cpu_build")
+ monkeypatch.setattr(hw, "CHAT_ONLY_DETAIL", "2.11.0+cpu")
+ monkeypatch.setattr(hw, "get_physical_gpu_inventory", lambda **_kw: {"devices": []})
+ assert hw.current_chat_only_verdict() == ("no_gpu", None)
+
+
[email protected]("frozen", ["mlx_unavailable", "detection_failed", "intel_mac", None])
+def test_the_other_verdicts_are_left_exactly_as_detection_set_them(monkeypatch, frozen):
+ # A 60 second probe cannot change any of these, and re-deriving them would fight
+ # detect_hardware() rather than follow it.
+ monkeypatch.setattr(hw, "CHAT_ONLY_REASON", frozen)
+ monkeypatch.setattr(hw, "CHAT_ONLY_DETAIL", "whatever detection recorded")
+ monkeypatch.setattr(
+ hw, "get_physical_gpu_inventory", lambda **_kw: {"devices": [{"vendor": "nvidia"}]}
+ )
+ assert hw.current_chat_only_verdict() == (frozen, "whatever detection recorded")
+
+
+def test_a_probe_that_raises_keeps_the_frozen_verdict(monkeypatch):
+ monkeypatch.setattr(hw, "CHAT_ONLY_REASON", "torch_cpu_build")
+ monkeypatch.setattr(hw, "CHAT_ONLY_DETAIL", "2.11.0+cpu")
+ monkeypatch.setattr(
+ hw, "classify_torch_build", lambda **_kw: (_ for _ in ()).throw(RuntimeError("boom"))
+ )
+ assert hw.current_chat_only_verdict() == ("torch_cpu_build", "2.11.0+cpu")
+
+
+def test_export_and_video_read_the_refreshed_verdict(monkeypatch):
+ import sys
+
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cpu"))
+ monkeypatch.setattr(hw, "DEVICE", hw.DeviceType.CPU)
+ monkeypatch.setattr(hw, "CHAT_ONLY_REASON", "no_gpu") # the STALE verdict
+ monkeypatch.setattr(hw, "CHAT_ONLY_DETAIL", None)
+ monkeypatch.setattr(hw, "_has_torch", lambda: True)
+ monkeypatch.setattr(hw, "is_apple_silicon", lambda: False)
+ monkeypatch.setattr(hw.platform, "system", lambda: "Linux")
+ monkeypatch.setattr(
+ hw, "get_physical_gpu_inventory", lambda **_kw: {"devices": [{"vendor": "nvidia"}]}
+ )
+
+ hw.torch_build_snapshot()
+ assert hw.export_capability()["export_unsupported_reason"] == "torch_cpu_build"
+ assert hw.video_capability()["video_unsupported_reason"] == "torch_cpu_build"
+
+
+def test_windows_intel_adapters_are_inventoried_too(monkeypatch):
+ """An Arc host whose XPU wheel was replaced has exactly this shape.
+
+ nvidia-smi contributes nothing there either, and the registry scan was filtered to
+ AMD, so the inventory came back empty and the mismatch was discarded.
+ """
+ monkeypatch.setattr(hw.platform, "system", lambda: "Windows")
+ _smi(monkeypatch, "", returncode = 9)
+ monkeypatch.setattr(
+ hw, "_windows_live_adapter_names", lambda: ["Intel(R) Arc(TM) A770 Graphics"]
+ )
+ monkeypatch.setattr(
+ hw,
+ "_windows_amd_adapter_records_by_luid",
+ lambda vendor_id = hw._AMD_PCI_VENDOR_ID, **_kw: (
+ {0x1: {"name": "Intel(R) Arc(TM) A770", "dedicated_memory_bytes": 16 * 1024**3}}
+ if vendor_id == hw._INTEL_PCI_VENDOR_ID
+ else {}
+ ),
+ )
+
+ inventory = hw.get_physical_gpu_inventory()
+
+ assert [d["vendor"] for d in inventory["devices"]] == ["intel"]
+ assert inventory["devices"][0]["memory_total_gb"] == 16.0
+ assert inventory["available"] is True
+ assert inventory["unknown"] is True
+
+ _smi(monkeypatch, "", returncode = 0)
+ monkeypatch.setattr(hw, "_physical_gpu_inventory_cache", None)
+ inventory = hw.get_physical_gpu_inventory()
+ assert [d["vendor"] for d in inventory["devices"]] == ["intel"]
+ assert inventory["unknown"] is False
+
+
+def test_a_transient_probe_failure_does_not_retire_a_settled_mismatch(monkeypatch):
+ """nvidia-smi timing out is not the GPU going away.
+
+ The aggregate probe returns a structured empty result rather than raising, so the
+ refreshed verdict read it as "no cards" and handed the user the opposite advice for
+ a whole cache interval.
+ """
+ monkeypatch.setattr(hw, "CHAT_ONLY_REASON", "torch_cpu_build")
+ monkeypatch.setattr(hw, "CHAT_ONLY_DETAIL", "2.11.0+cpu")
+ monkeypatch.setattr(hw, "classify_torch_build", lambda **_kw: "torch_cpu_build")
+ monkeypatch.setattr(hw, "_torch_reports_a_usable_accelerator", lambda: False)
+ monkeypatch.setattr(
+ hw,
+ "get_physical_gpu_inventory",
+ lambda **_kw: {"available": False, "devices": [], "unknown": True},
+ )
+ hw.torch_build_snapshot()
+ assert hw.current_chat_only_verdict() == ("torch_cpu_build", "2.11.0+cpu")
+
+ monkeypatch.setattr(
+ hw,
+ "get_physical_gpu_inventory",
+ lambda **_kw: {"available": False, "devices": [], "unknown": False},
+ )
+ assert hw.current_chat_only_verdict() == ("no_gpu", None)
+
+
+def test_only_uncertainty_about_the_mismatched_vendor_holds_the_verdict(monkeypatch):
+ """A broken probe for a vendor that never had a card cannot pin the old mismatch.
+
+ Detach an AMD eGPU: sysfs conclusively reports no AMD card, while nvidia-smi is broken
+ on a host that never had an NVIDIA one. The carry-forward has nothing to re-add, so
+ holding "your GPU is unusable" here asserts a card is present with nothing to point at,
+ and it holds for as long as that unrelated probe stays broken.
+ """
+ monkeypatch.setattr(hw, "CHAT_ONLY_REASON", "torch_cpu_build")
+ monkeypatch.setattr(hw, "CHAT_ONLY_DETAIL", "2.11.0+cpu")
+ monkeypatch.setattr(hw, "CHAT_ONLY_MISMATCH_VENDORS", frozenset({"amd"}))
+ monkeypatch.setattr(hw, "classify_torch_build", lambda **_kw: "torch_cpu_build")
+ monkeypatch.setattr(hw, "_torch_reports_a_usable_accelerator", lambda: False)
+ hw.torch_build_snapshot()
+
+ def _inventory(unanswered):
+ monkeypatch.setattr(
+ hw,
+ "get_physical_gpu_inventory",
+ lambda **_kw: {
+ "available": False,
+ "devices": [],
+ "unknown": True,
+ "unanswered": unanswered,
+ },
+ )
+
+ _inventory(["nvidia"])
+ assert hw.current_chat_only_verdict() == ("no_gpu", None)
+
+ # The vendor the mismatch DID come from, so the card may still be there and unread.
+ _inventory(["amd"])
+ assert hw.current_chat_only_verdict() == ("torch_cpu_build", "2.11.0+cpu")
+
+ # An unknown that names nobody is a cold cache, which is silent about who could not
+ # answer rather than saying everyone did.
+ _inventory([])
+ assert hw.current_chat_only_verdict() == ("torch_cpu_build", "2.11.0+cpu")
+
+
+def test_the_recorded_vendors_follow_the_mismatch_when_it_moves(monkeypatch):
+ """The mismatch can change vendor inside one process: swap an AMD eGPU for an NVIDIA one.
+
+ Holding the startup answer would later freeze the verdict on a vendor a refresh has
+ already watched disappear, which is the same stale-scope bug one level up.
+ """
+ monkeypatch.setattr(hw, "CHAT_ONLY_REASON", "torch_cpu_build")
+ monkeypatch.setattr(hw, "CHAT_ONLY_DETAIL", "2.11.0+cpu")
+ monkeypatch.setattr(hw, "CHAT_ONLY_MISMATCH_VENDORS", frozenset({"amd"}))
+ monkeypatch.setattr(hw, "classify_torch_build", lambda **_kw: "torch_cpu_build")
+ monkeypatch.setattr(hw, "_torch_reports_a_usable_accelerator", lambda: False)
+ monkeypatch.setattr(
+ hw,
+ "get_physical_gpu_inventory",
+ lambda **_kw: {
+ "available": True,
+ "devices": [{"vendor": "nvidia", "name": "NVIDIA RTX A4000"}],
+ "unknown": False,
+ },
+ )
+ hw.torch_build_snapshot()
+
+ # Only the reason: the detail is re-read from the live torch, which is this host's.
+ assert hw.current_chat_only_verdict()[0] == "torch_cpu_build"
+ assert hw.CHAT_ONLY_MISMATCH_VENDORS == frozenset({"nvidia"})
+
+ # And the AMD probe going unreadable now proves nothing about the card that is here.
+ monkeypatch.setattr(
+ hw,
+ "get_physical_gpu_inventory",
+ lambda **_kw: {
+ "available": False,
+ "devices": [],
+ "unknown": True,
+ "unanswered": ["amd"],
+ },
+ )
+ assert hw.current_chat_only_verdict() == ("no_gpu", None)
+
+
+def test_the_health_path_never_waits_on_the_gpu_probe(monkeypatch):
+ """/api/health and /api/liveness both read the chat-only verdict.
+
+ The NVIDIA half shells out with a 10 second timeout and the lock makes concurrent
+ callers queue, so a hung driver -- the exact host this feature is for -- would stall
+ the event loop every time the TTL expired, against a 2 second desktop timeout.
+ """
+ import sys
+
+ calls = {"blocking": 0, "threads": 0}
+
+ def _probe():
+ calls["blocking"] += 1
+ return {
+ "available": True,
+ "devices": [{"vendor": "nvidia"}],
+ "sources": ["x"],
+ "unknown": False,
+ }
+
+ class _Thread:
+ def __init__(self, *a, **k):
+ calls["threads"] += 1
+
+ def start(self):
+ pass
+
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cpu"))
+ monkeypatch.setattr(hw, "_probe_physical_gpu_inventory", _probe)
+ monkeypatch.setattr(hw.threading, "Thread", _Thread)
+ monkeypatch.setattr(hw, "_physical_gpu_inventory_refreshing", False)
+ monkeypatch.setattr(hw, "CHAT_ONLY_REASON", "torch_cpu_build")
+ monkeypatch.setattr(hw, "CHAT_ONLY_DETAIL", "2.11.0+cpu")
+
+ hw.torch_build_snapshot()
+ # That warm-up IS the blocking pass, standing in for detection. What the request path
+ # may not do is shell out again, so count from here.
+ calls["blocking"] = 0
+ monkeypatch.setattr(hw, "_physical_gpu_inventory_cache", None)
+ monkeypatch.setattr(hw, "_physical_gpu_inventory_refreshing", False)
+
+ # Cold inventory cache: no probe inline, the refresh goes to a thread, and the explicit
+ # unknown keeps the frozen verdict.
+ assert hw.current_chat_only_verdict() == ("torch_cpu_build", "2.11.0+cpu")
+ assert calls["blocking"] == 0, "nothing may shell out on the request path"
+ assert calls["threads"] >= 1
+
+ monkeypatch.setattr(hw, "_physical_gpu_inventory_refreshing", True)
+ before = calls["threads"]
+ hw.get_physical_gpu_inventory(block = False)
+ assert calls["threads"] == before
+
+
+def test_a_stale_cache_is_served_rather_than_re_probed_off_the_request_path(monkeypatch):
+ warm = {
+ "available": True,
+ "devices": [{"vendor": "nvidia"}],
+ "sources": ["x"],
+ "unknown": False,
+ }
+ calls = {"n": 0}
+
+ def _probe():
+ calls["n"] += 1
+ return warm
+
+ class _Thread:
+ def __init__(self, *a, **k):
+ pass
+
+ def start(self):
+ pass
+
+ monkeypatch.setattr(hw, "_probe_physical_gpu_inventory", _probe)
+ monkeypatch.setattr(hw.threading, "Thread", _Thread)
+ monkeypatch.setattr(hw, "_physical_gpu_inventory_refreshing", False)
+ monkeypatch.setattr(hw, "_physical_gpu_inventory_cache", (hw.time.monotonic() - 3600, warm))
+
+ assert hw.get_physical_gpu_inventory(block = False) is warm
+ assert calls["n"] == 0, "a stale answer beats a subprocess on the request path"
+
+ assert hw.get_physical_gpu_inventory() is warm
+ assert calls["n"] == 1
+
+
+def test_a_process_that_cannot_start_a_thread_keeps_the_stale_answer(monkeypatch):
+ warm = {
+ "available": True,
+ "devices": [{"vendor": "nvidia"}],
+ "sources": ["x"],
+ "unknown": False,
+ }
+ monkeypatch.setattr(hw, "_physical_gpu_inventory_refreshing", False)
+ monkeypatch.setattr(hw, "_physical_gpu_inventory_cache", (hw.time.monotonic() - 3600, warm))
+
+ def _boom(*_a, **_k):
+ raise RuntimeError("can't start new thread")
+
+ monkeypatch.setattr(hw.threading, "Thread", _boom)
+ assert hw.get_physical_gpu_inventory(block = False) is warm
+ assert hw._physical_gpu_inventory_refreshing is False
+
+
+def test_a_stale_registry_record_is_not_reported_as_a_gpu(monkeypatch):
+ """The DirectX registry outlives the hardware.
+
+ setup.ps1 says so and uses these records only to RE-LABEL an adapter its live WMI
+ scan also returned, never to add one. A CPU-only machine with a driver record left
+ behind would otherwise be told it has an unusable GPU and offered a repair that
+ cannot restore absent hardware.
+ """
+ monkeypatch.setattr(hw.platform, "system", lambda: "Windows")
+ _smi(monkeypatch, "", returncode = 9)
+ monkeypatch.setattr(hw, "_windows_live_adapter_names", lambda: ["Microsoft Basic Display"])
+ monkeypatch.setattr(
+ hw,
+ "_windows_amd_adapter_records_by_luid",
+ lambda vendor_id = hw._AMD_PCI_VENDOR_ID, **_kw: (
+ {0x1: {"name": "AMD Radeon RX 6800", "dedicated_memory_bytes": 16 * 1024**3}}
+ if vendor_id == hw._AMD_PCI_VENDOR_ID
+ else {}
+ ),
+ )
+
+ inventory = hw.get_physical_gpu_inventory()
+ assert inventory["devices"] == []
+ assert inventory["sources"] == []
+
+
+def test_a_live_scan_that_cannot_answer_reports_unknown_rather_than_guessing(monkeypatch):
+ monkeypatch.setattr(hw.platform, "system", lambda: "Windows")
+ _smi(monkeypatch, "", returncode = 9)
+ monkeypatch.setattr(hw, "_windows_live_adapter_names", lambda: None)
+ monkeypatch.setattr(
+ hw,
+ "_windows_amd_adapter_records_by_luid",
+ lambda vendor_id = hw._AMD_PCI_VENDOR_ID, **_kw: (
+ {0x1: {"name": "AMD Radeon RX 6800"}} if vendor_id == hw._AMD_PCI_VENDOR_ID else {}
+ ),
+ )
+
+ inventory = hw.get_physical_gpu_inventory()
+ assert inventory["devices"] == []
+ assert inventory["unknown"] is True
+
+
+ "registry,live,matched",
+ [
+ ("AMD Radeon RX 7900 XT", "AMD Radeon RX 7900 XT", True),
+ ("AMD Radeon RX 7900 XT", "AMD Radeon RX 7900 XT Graphics", True),
+ ("Intel(R) Arc(TM) A770 Graphics", "Intel(R) Arc(TM) A770", True),
+ ("AMD Radeon RX 6800", "NVIDIA GeForce RTX 4090", False),
+ ("", "AMD Radeon RX 6800", False),
+ ("AMD Radeon RX 6800", "", False),
+ ],
+)
+def test_the_registry_to_live_join_matches_setup_ps1(registry, live, matched):
+ assert hw._adapter_name_is_live(registry, [live]) is matched
+
+
+def test_an_ordinary_intel_igpu_does_not_establish_a_mismatch(monkeypatch, tmp_path):
+ """setup.sh does not autodetect Linux XPU, and setup.ps1 limits it to Arc.
+
+ An Intel UHD iGPU beside a CPU wheel is the correct state of that machine, so
+ counting it would report a broken install and offer a repair that reinstalls the
+ very CPU build it just replaced.
+ """
+ import sys
+
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cpu"))
+ monkeypatch.setattr(hw.sys, "prefix", str(tmp_path))
+ monkeypatch.delenv("UNSLOTH_TORCH_INDEX_FAMILY", raising = False)
+ monkeypatch.delenv("UNSLOTH_TORCH_INDEX_URL", raising = False)
+
+ igpu = [{"vendor": "intel", "name": None, "index": 0}]
+ assert hw._devices_that_can_establish_a_mismatch(igpu) == []
+
+ arc = [{"vendor": "intel", "name": "Intel(R) Arc(TM) A770 Graphics", "index": 0}]
+ assert hw._devices_that_can_establish_a_mismatch(arc) == arc
+ others = [
+ {"vendor": "nvidia", "index": 0},
+ {"vendor": "amd", "index": 0, "gfx_candidates": ["gfx1100"]},
+ ]
+ assert hw._devices_that_can_establish_a_mismatch(others) == others
+
+
+def test_a_nameless_intel_card_counts_once_xpu_was_actually_chosen(monkeypatch, tmp_path):
+ import sys
+
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cpu"))
+ monkeypatch.setattr(hw.sys, "prefix", str(tmp_path))
+ monkeypatch.delenv("UNSLOTH_TORCH_INDEX_URL", raising = False)
+ nameless = [{"vendor": "intel", "name": None, "index": 0}]
+
+ monkeypatch.setenv("UNSLOTH_TORCH_INDEX_FAMILY", "xpu")
+ assert hw._devices_that_can_establish_a_mismatch(nameless) == nameless
+ monkeypatch.delenv("UNSLOTH_TORCH_INDEX_FAMILY")
+
+ manifest = tmp_path / "unsloth_install_manifest.json"
+ manifest.write_text('{"schema": 1, "expected_torch_tag": "xpu"}', encoding = "utf-8")
+ assert hw._devices_that_can_establish_a_mismatch(nameless) == nameless
+
+ manifest.write_text('{"schema": 1, "expected_torch_tag": "cpu"}', encoding = "utf-8")
+ assert hw._devices_that_can_establish_a_mismatch(nameless) == []
+
+ xpu_torch = _fake_torch("cpu")
+ xpu_torch.__version__ = "2.9.0+xpu"
+ monkeypatch.setitem(sys.modules, "torch", xpu_torch)
+ assert hw._devices_that_can_establish_a_mismatch(nameless) == nameless
+
+
+def test_an_accelerator_that_came_back_retires_the_cached_verdict(monkeypatch):
+ """Only reason and detail are refreshed here; DEVICE and CHAT_ONLY are not.
+
+ So a driver that finished restarting left Train and Export disabled AND the UI
+ saying there is no GPU, which is worse than the stale mismatch it replaced.
+ """
+ import sys
+
+ monkeypatch.setattr(hw, "CHAT_ONLY_REASON", "torch_cuda_unavailable")
+ monkeypatch.setattr(hw, "CHAT_ONLY_DETAIL", "2.6.0+cu124")
+ monkeypatch.setattr(hw, "_REDETECTION_REQUESTED", False)
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cuda"))
+ calls = {"n": 0}
+ monkeypatch.setattr(hw, "invalidate_detection", lambda: calls.__setitem__("n", calls["n"] + 1))
+ # The recovery is measured, not probed inline. The recovery path drops the snapshot
+ # again, so the fresh pass re-reads this fake host rather than the pre-recovery answer.
+ hw.torch_build_snapshot()
+
+ assert hw.current_chat_only_verdict() == ("torch_cuda_unavailable", "2.6.0+cu124")
+ assert calls["n"] == 1
+
+ hw.current_chat_only_verdict()
+ hw.current_chat_only_verdict()
+ assert calls["n"] == 1
+
+
+def test_a_host_whose_accelerator_never_came_back_is_not_re_detected(monkeypatch):
+ import sys
+
+ monkeypatch.setattr(hw, "CHAT_ONLY_REASON", "torch_cpu_build")
+ monkeypatch.setattr(hw, "CHAT_ONLY_DETAIL", "2.11.0+cpu")
+ monkeypatch.setattr(hw, "_REDETECTION_REQUESTED", False)
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cpu"))
+ monkeypatch.setattr(
+ hw, "get_physical_gpu_inventory", lambda **_kw: {"devices": [{"vendor": "nvidia"}]}
+ )
+ calls = {"n": 0}
+ monkeypatch.setattr(hw, "invalidate_detection", lambda: calls.__setitem__("n", calls["n"] + 1))
+
+ assert hw.current_chat_only_verdict() == ("torch_cpu_build", "2.11.0+cpu")
+ assert calls["n"] == 0
+
+
+def test_a_healthy_xpu_wheel_is_not_called_unavailable(monkeypatch):
+ """An Intel host that started while its runtime was down and later recovered.
+
+ Asking only about CUDA classified a perfectly healthy +xpu wheel as
+ torch_cuda_unavailable forever, so the recovery branch was never reached and the
+ process stayed chat-only until restart.
+ """
+ import sys
+
+ xpu = _fake_torch("cuda_dead")
+ xpu.__version__ = "2.9.0+xpu"
+ xpu.xpu = types.SimpleNamespace(is_available = lambda: True)
+ monkeypatch.setitem(sys.modules, "torch", xpu)
+ assert hw.classify_torch_build() is None
+
+ xpu.xpu = types.SimpleNamespace(is_available = lambda: False)
+ assert hw.classify_torch_build() == "torch_cuda_unavailable"
+
+ def _boom():
+ raise RuntimeError("Level Zero not initialised")
+
+ xpu.xpu = types.SimpleNamespace(is_available = _boom)
+ assert hw.classify_torch_build() == "torch_cuda_unavailable"
+
+
[email protected]("mask", ["", " ", "-1"])
+def test_an_emptied_xpu_mask_is_a_deliberate_hide(monkeypatch, mask):
+ import sys
+
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cuda_dead"))
+ monkeypatch.setattr(
+ hw,
+ "get_physical_gpu_inventory",
+ lambda **_kw: {"available": True, "devices": [{"vendor": "intel"}]},
+ )
+ monkeypatch.setenv("ZE_AFFINITY_MASK", mask)
+ assert hw.classify_torch_build() is None
+
+ monkeypatch.setenv("ZE_AFFINITY_MASK", "0")
+ assert hw.classify_torch_build() == "torch_cuda_unavailable"
+
+
+def test_the_xpu_mask_is_ignored_on_an_nvidia_only_host(monkeypatch):
+ import sys
+
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cuda_dead"))
+ monkeypatch.setattr(
+ hw,
+ "get_physical_gpu_inventory",
+ lambda **_kw: {"available": True, "devices": [{"vendor": "nvidia"}]},
+ )
+ monkeypatch.setenv("ZE_AFFINITY_MASK", "")
+ assert hw.classify_torch_build() == "torch_cuda_unavailable"
+
+
+def test_duplicate_registry_records_are_claimed_one_to_one(monkeypatch):
+ """Two identical cards leave two identical registry records.
+
+ Remove one and WMI reports a single live adapter, which a reusable predicate matched
+ to BOTH: a GPU reported that is gone, and its VRAM counted twice.
+ """
+ monkeypatch.setattr(hw.platform, "system", lambda: "Windows")
+ # nvidia-smi ANSWERS (absent), so only the registry vendors go unanswered here.
+ _smi(monkeypatch, "", raises = FileNotFoundError)
+ monkeypatch.setattr(hw, "_windows_live_adapter_names", lambda: ["AMD Radeon RX 7900 XT"])
+ monkeypatch.setattr(
+ hw,
+ "_windows_amd_adapter_records_by_luid",
+ lambda vendor_id = hw._AMD_PCI_VENDOR_ID, **_kw: (
+ {
+ 0x1: {"name": "AMD Radeon RX 7900 XT", "dedicated_memory_bytes": 20 * 1024**3},
+ 0x2: {"name": "AMD Radeon RX 7900 XT", "dedicated_memory_bytes": 20 * 1024**3},
+ }
+ if vendor_id == hw._AMD_PCI_VENDOR_ID
+ else {}
+ ),
+ )
+
+ inventory = hw.get_physical_gpu_inventory()
+ assert len(inventory["devices"]) == 1, "one live adapter corroborates one record"
+ assert inventory["devices"][0]["memory_total_gb"] == 20.0
+
+
+def test_both_records_survive_when_both_cards_are_live(monkeypatch):
+ monkeypatch.setattr(hw.platform, "system", lambda: "Windows")
+ _smi(monkeypatch, "", returncode = 9)
+ monkeypatch.setattr(
+ hw,
+ "_windows_live_adapter_names",
+ lambda: ["AMD Radeon RX 7900 XT", "AMD Radeon RX 7900 XT"],
+ )
+ monkeypatch.setattr(
+ hw,
+ "_windows_amd_adapter_records_by_luid",
+ lambda vendor_id = hw._AMD_PCI_VENDOR_ID, **_kw: (
+ {
+ 0x1: {"name": "AMD Radeon RX 7900 XT"},
+ 0x2: {"name": "AMD Radeon RX 7900 XT"},
+ }
+ if vendor_id == hw._AMD_PCI_VENDOR_ID
+ else {}
+ ),
+ )
+
+ assert len(hw.get_physical_gpu_inventory()["devices"]) == 2
+
+
+def test_the_live_scan_stops_on_a_cim_failure_rather_than_reporting_none(monkeypatch):
+ """-ErrorAction SilentlyContinue exits 0 with empty stdout.
+
+ That is indistinguishable from "no adapters", so the corroboration would have
+ dropped every real card on a host with damaged WMI, recreating the false no-GPU
+ result this whole change exists to remove.
+ """
+ source = inspect.getsource(hw._windows_live_adapter_names)
+ command = "".join(line for line in source.splitlines() if not line.strip().startswith("#"))
+ assert "SilentlyContinue" not in command
+ assert "$ErrorActionPreference='Stop'" in command
+
+
+def test_a_missing_nvidia_smi_does_not_warn_every_refresh(monkeypatch, capsys):
+ """The normal state of every CPU-only, AMD and Intel host.
+
+ The inventory calls this on a 60 second refresh reached from the health and system
+ polls, so a warning here is a line a minute on a machine that is working correctly.
+ """
+
+ def _missing(*_a, **_k):
+ raise FileNotFoundError("nvidia-smi")
+
+ monkeypatch.setattr(nvidia.subprocess, "run", _missing)
+ capsys.readouterr()
+ assert (
+ nvidia._query_gpu_inventory("test") is nvidia.NVIDIA_SMI_ABSENT
+ ), "an absent CLI is its own answer, not the None that means a probe failed"
+ assert '"level": "warning"' not in capsys.readouterr().out
+
+ def _hang(*_a, **_k):
+ raise subprocess.TimeoutExpired("nvidia-smi", 10)
+
+ monkeypatch.setattr(nvidia.subprocess, "run", _hang)
+ capsys.readouterr()
+ assert nvidia._query_gpu_inventory("test") is None
+ assert '"level": "warning"' in capsys.readouterr().out
+
+
+def test_the_recovery_actually_starts_a_detection_pass(monkeypatch):
+ """Retiring the epoch alone did nothing, so the round-six fix never took effect.
+
+ invalidate_detection leaves DEVICE set and DETECTION_COMPLETE raised, so /api/health
+ kept reading the settled snapshot and start_background_detection returned at once
+ because DEVICE was not None. The process stayed chat-only until restart, which is
+ exactly what the fix claimed to solve.
+ """
+ import sys
+
+ calls = []
+ monkeypatch.setattr(hw, "CHAT_ONLY_REASON", "torch_cuda_unavailable")
+ monkeypatch.setattr(hw, "CHAT_ONLY_DETAIL", "2.6.0+cu124")
+ monkeypatch.setattr(hw, "_REDETECTION_REQUESTED", False)
+ monkeypatch.setattr(hw, "DEVICE", hw.DeviceType.CPU)
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cuda"))
+ monkeypatch.setattr(hw, "invalidate_detection", lambda: calls.append("epoch") or 1)
+ monkeypatch.setattr(hw, "_discard_detection_locked", lambda: calls.append("discard"))
+ monkeypatch.setattr(hw, "start_background_detection", lambda: calls.append("start"))
+
+ # detect_hardware() warms the snapshot on the real path; the verdict only ever reads it
+ # non-blocking, so measure this fake host the same way first.
+ hw.torch_build_snapshot()
+ hw.current_chat_only_verdict()
+
+ # Order matters: discarding after the pass started would race it, and starting before
+ # the discard is the no-op this fixes.
+ assert calls == ["epoch", "discard", "start"]
+
+
+def test_the_system_mismatch_report_does_not_block(monkeypatch):
+ # GET /api/system is polled every three seconds with _system_gpu_cache_lock held for the
+ # whole call, so a hung nvidia-smi would queue every concurrent read behind it.
+ source = inspect.getsource(hw._torch_gpu_mismatch_report)
+ assert "get_physical_gpu_inventory(block=False)" in source.replace(" ", "")
+
+
+def test_an_unimportable_torch_still_reports_the_cards(monkeypatch, tmp_path):
+ """A CUDA wheel whose native runtime will not load.
+
+ Returning None meant the mismatch report never ran, so a GPU host published no
+ physical_devices and the System tab said no visible GPU while nvidia-smi could
+ enumerate the card: the central failure, for the runtime-failure case.
+ """
+ import importlib.abc
+ import importlib.util
+ import sys
+
+ # A real import that raises, not an object in sys.modules whose attribute access does.
+ # Reading __spec__ off an existing sys.modules entry is import-machinery behaviour that
+ # changed in 3.13: on 3.10-3.12 `import torch` hands back the stub untouched, so the
+ # premise below silently inverted and this test passed for the wrong reason.
+ class _WillNotLoad(importlib.abc.Loader):
+ def create_module(self, spec):
+ return None
+
+ def exec_module(self, module):
+ raise OSError("[WinError 126] cudart64_12.dll could not be loaded")
+
+ class _Finder(importlib.abc.MetaPathFinder):
+ def find_spec(
+ self,
+ fullname,
+ path = None,
+ target = None,
+ ):
+ if fullname == "torch":
+ return importlib.util.spec_from_loader("torch", _WillNotLoad())
+ return None
+
+ monkeypatch.delitem(sys.modules, "torch", raising = False)
+ monkeypatch.setattr(sys, "meta_path", [_Finder(), *sys.meta_path])
+ # _has_torch() is NOT forced here: it reports False for a wheel that will not import,
+ # and the early return on it used to keep this host from the on-disk fallback below.
+ assert hw._has_torch() is False, "the premise: an unimportable torch reads as absent"
+
+ monkeypatch.setattr(hw, "_installed_torch_label_on_disk", lambda: "2.6.0+cu124")
+ assert hw.classify_torch_build() == "torch_cuda_unavailable"
+
+ for label in ("2.11.0+rocm7.2", "2.9.1+xpu"):
+ monkeypatch.setattr(hw, "_installed_torch_label_on_disk", lambda label = label: label)
+ assert hw.classify_torch_build() == "torch_cuda_unavailable"
+
+ monkeypatch.setattr(hw, "_installed_torch_label_on_disk", lambda: "2.11.0+cpu")
+ assert hw.classify_torch_build() == "torch_cpu_build"
+
+ monkeypatch.setattr(hw, "_installed_torch_label_on_disk", lambda: "")
+ assert hw.classify_torch_build() is None
+
+
+def test_the_disk_label_reader_needs_no_interpreter(tmp_path):
+ pkg = tmp_path / "torch"
+ pkg.mkdir()
+ (pkg / "version.py").write_text(
+ '__version__ = "2.6.0+cu124"\ncuda = "12.4"\n', encoding = "utf-8"
+ )
+ with patch.object(
+ hw.importlib.util,
+ "find_spec",
+ return_value = SimpleNamespace(submodule_search_locations = [str(pkg)]),
+ ):
+ assert hw._installed_torch_label_on_disk() == "2.6.0+cu124"
+
+ with patch.object(hw.importlib.util, "find_spec", return_value = None):
+ assert hw._installed_torch_label_on_disk() == ""
+
+ with patch.object(hw.importlib.util, "find_spec", side_effect = ValueError("boom")):
+ assert hw._installed_torch_label_on_disk() == ""
+
+
+def test_an_untagged_xpu_build_is_not_called_a_cpu_wheel(monkeypatch):
+ """torch.version.xpu is the marker, and an untagged wheel can carry it.
+
+ An Arc host whose Level Zero runtime is down has no local version tag and neither
+ a cuda nor a hip one, so it read as a CPU wheel and the UI offered a reinstall,
+ which is the one remedy that cannot help. _torch_reports_an_xpu_runtime() already
+ knew better, so the card established a mismatch and then got the wrong advice.
+ """
+ import sys
+
+ torch = types.ModuleType("torch")
+ torch.__version__ = "2.9.1" # untagged, as a source or vendor build is
+ torch.version = SimpleNamespace(cuda = None, hip = None, xpu = "20250101")
+ torch.cuda = SimpleNamespace(is_available = lambda: False)
+ torch.xpu = SimpleNamespace(is_available = lambda: False)
+ monkeypatch.setitem(sys.modules, "torch", torch)
+ assert hw.classify_torch_build() == "torch_cuda_unavailable"
+
+ torch.version = SimpleNamespace(cuda = None, hip = None, xpu = None)
+ assert hw.classify_torch_build() == "torch_cpu_build"
+
+
+def test_the_health_path_never_imports_torch_inline(monkeypatch):
+ """classify_torch_build() imports torch and asks CUDA and XPU if they are available.
+
+ Both block while a driver is wedged or restarting, which is the state
+ torch_cuda_unavailable names, and /api/health and /api/liveness reach them through
+ the chat-only verdict against a two second desktop timeout. Making only the
+ inventory lookup non-blocking still left this on the request thread.
+ """
+ calls = {"probes": 0, "threads": 0}
+
+ def _classify(**_kw):
+ calls["probes"] += 1
+ return "torch_cpu_build"
+
+ class _Thread:
+ def __init__(self, *a, **k):
+ calls["threads"] += 1
+
+ def start(self):
+ pass
+
+ monkeypatch.setattr(hw, "classify_torch_build", _classify)
+ monkeypatch.setattr(hw, "_torch_reports_a_usable_accelerator", lambda: False)
+ monkeypatch.setattr(hw.threading, "Thread", _Thread)
+ monkeypatch.setattr(hw, "_torch_build_snapshot_refreshing", False)
+ monkeypatch.setattr(hw, "CHAT_ONLY_REASON", "torch_cpu_build")
+ monkeypatch.setattr(hw, "CHAT_ONLY_DETAIL", "2.11.0+cpu")
+ monkeypatch.setattr(
+ hw, "get_physical_gpu_inventory", lambda **_kw: {"devices": [], "unknown": True}
+ )
+
+ assert hw.current_chat_only_verdict() == ("torch_cpu_build", "2.11.0+cpu")
+ assert calls["probes"] == 0, "no torch probe may run on the health path"
+ assert calls["threads"] == 1
+
+ hw.torch_build_snapshot()
+ assert calls["probes"] == 1
+ assert hw.current_chat_only_verdict() == ("torch_cpu_build", "2.11.0+cpu")
+ assert calls["probes"] == 1
+
+
+def test_a_recovery_re_measures_torch_rather_than_reusing_the_old_answer(monkeypatch):
+ """The snapshot that said the accelerator was missing must not outlive it.
+
+ Left in place, the fresh detection pass the recovery starts would read the same
+ stale measurement for the rest of its TTL and settle on the verdict it just retired.
+ """
+ import sys
+
+ monkeypatch.setattr(hw, "CHAT_ONLY_REASON", "torch_cuda_unavailable")
+ monkeypatch.setattr(hw, "CHAT_ONLY_DETAIL", "2.6.0+cu124")
+ monkeypatch.setattr(hw, "_REDETECTION_REQUESTED", False)
+ monkeypatch.setattr(hw, "DEVICE", hw.DeviceType.CPU)
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cuda"))
+ monkeypatch.setattr(hw, "invalidate_detection", lambda: 1)
+ monkeypatch.setattr(hw, "_discard_detection_locked", lambda: None)
+ monkeypatch.setattr(hw, "start_background_detection", lambda: None)
+
+ hw.torch_build_snapshot()
+ assert hw._torch_build_snapshot_cache is not None
+ hw.current_chat_only_verdict()
+ assert (
+ hw._torch_build_snapshot_cache is None
+ ), "the recovery must drop the measurement it was taken before"
+
+
+def test_a_vendor_mask_does_not_hide_another_vendors_card(monkeypatch):
+ """A hybrid NVIDIA + Arc host with ZE_AFFINITY_MASK="".
+
+ The mask hides the Arc and nothing else, but it was cancelling the whole
+ classification, so a CPU-only wheel went unreported for the NVIDIA card the user
+ never masked. The Arc still has to drop out of the mismatch inventory: it is
+ hidden on purpose, and counting it would offer a repair for a chosen configuration.
+ """
+ import sys
+
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cpu"))
+ monkeypatch.setenv("ZE_AFFINITY_MASK", "")
+ hybrid = [
+ {"vendor": "nvidia", "name": "NVIDIA RTX A4000"},
+ {"vendor": "intel", "name": "Intel(R) Arc(TM) A770"},
+ ]
+ monkeypatch.setattr(
+ hw, "get_physical_gpu_inventory", lambda **_kw: {"devices": hybrid, "unknown": False}
+ )
+
+ assert hw.classify_torch_build() == "torch_cpu_build"
+ kept = hw._devices_that_can_establish_a_mismatch(hybrid)
+ assert [d["vendor"] for d in kept] == ["nvidia"]
+
+ monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "")
+ assert hw.classify_torch_build() is None
+ assert hw._devices_that_can_establish_a_mismatch(hybrid) == []
+
+
+def test_an_intel_only_host_still_reads_an_emptied_ze_mask_as_deliberate(monkeypatch):
+ import sys
+
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cpu"))
+ monkeypatch.setenv("ZE_AFFINITY_MASK", "-1")
+ monkeypatch.setattr(
+ hw,
+ "get_physical_gpu_inventory",
+ lambda **_kw: {"devices": [{"vendor": "intel", "name": "Arc A770"}], "unknown": False},
+ )
+ assert hw.classify_torch_build() is None
+
+
+def test_an_untagged_conda_cuda_build_that_will_not_import_is_not_a_cpu_wheel(monkeypatch):
+ """torch/version.py records the runtime even when the wheel carries no local tag.
+
+ The importable path already reads exactly these attributes, so reading only
+ __version__ on the failure path gave the same installation the opposite diagnosis
+ and sent the user to reinstall a GPU wheel it already has.
+ """
+ monkeypatch.setattr(hw, "_installed_torch_label_on_disk", lambda: "2.9.1")
+ monkeypatch.setattr(
+ hw, "_installed_torch_markers_on_disk", lambda: {"cuda": "12.8", "hip": None, "xpu": None}
+ )
+ assert hw._classification_from_disk_label() == "torch_cuda_unavailable"
+
+ for marker in ("hip", "xpu"):
+ monkeypatch.setattr(
+ hw,
+ "_installed_torch_markers_on_disk",
+ lambda marker = marker: {"cuda": None, "hip": None, "xpu": None} | {marker: "1.0"},
+ )
+ assert hw._classification_from_disk_label() == "torch_cuda_unavailable"
+
+ monkeypatch.setattr(
+ hw, "_installed_torch_markers_on_disk", lambda: {"cuda": None, "hip": None, "xpu": None}
+ )
+ assert hw._classification_from_disk_label() == "torch_cpu_build"
+
+ monkeypatch.setattr(hw, "_installed_torch_label_on_disk", lambda: "")
+ assert hw._classification_from_disk_label() is None
+
+
+def test_the_marker_reader_parses_both_shapes_torch_has_shipped(tmp_path):
+ """Parsed, not executed: this runs when importing torch is the thing that fails."""
+ package = tmp_path / "torch"
+ package.mkdir()
+ (package / "version.py").write_text(
+ "from typing import Optional\n"
+ "__version__ = '2.9.1'\n"
+ "debug = False\n"
+ "cuda: Optional[str] = '12.8'\n"
+ "hip = None\n"
+ "xpu: Optional[str] = None\n",
+ encoding = "utf-8",
+ )
+
+ class _Spec:
+ submodule_search_locations = [str(package)]
+
+ import types as _types
+
+ original = hw.importlib.util.find_spec
+ hw.importlib.util.find_spec = lambda name: _Spec() if name == "torch" else original(name)
+ try:
+ assert hw._installed_torch_markers_on_disk() == {
+ "cuda": "12.8",
+ "hip": None,
+ "xpu": None,
+ }
+ finally:
+ hw.importlib.util.find_spec = original
+ assert isinstance(_types, _types.ModuleType)
+
+
+def test_an_unimportable_gpu_wheel_is_a_mismatch_rather_than_a_detection_failure():
+ """detection_failed sends the user to the server log; the mismatch offers the repair.
+
+ _has_torch() is False for a wheel whose runtime will not load, so detection took
+ the TORCH_IMPORT_ERROR arm and never reached the classification -- and the verdict
+ refresh deliberately freezes detection_failed, so nothing revisited it later.
+ """
+ import pathlib
+
+ source = pathlib.Path(hw.__file__).read_text(encoding = "utf-8")
+ branch = source[source.index("elif TORCH_IMPORT_ERROR is not None:") :]
+ branch = branch[: branch.index('elif platform.system() == "Darwin":')]
+ assert (
+ "_classification_from_disk_label()" in branch
+ ), "the broken-runtime host must still be classified from the wheel on disk"
+ assert "torch_build_snapshot()" not in branch, (
+ "and classified WITHOUT probing: importing torch is what failed here, it takes "
+ "seconds to fail on a real broken wheel, and a retry re-runs torch/__init__ "
+ "against the partial module tree the first attempt left behind"
+ )
+ assert branch.index("detection_failed") < branch.index("_mismatch_verdict_for_this_host"), (
+ "detection_failed stays the default; the mismatch only replaces it when the "
+ "inventory actually found a card"
+ )
+
+
+def test_the_mismatch_verdict_names_the_wheel_it_could_not_import(monkeypatch):
+ monkeypatch.setattr(
+ hw,
+ "torch_build_snapshot",
+ lambda **_kw: {
+ "reason": "torch_cuda_unavailable",
+ "usable": False,
+ "unknown": False,
+ },
+ )
+ monkeypatch.setattr(
+ hw,
+ "get_physical_gpu_inventory",
+ lambda **_kw: {"devices": [{"vendor": "nvidia", "name": "A4000"}], "unknown": False},
+ )
+ monkeypatch.setattr(hw, "_torch_version_label", lambda: None)
+ monkeypatch.setattr(hw, "_installed_torch_label_on_disk", lambda: "2.6.0+cu124")
+
+ assert hw._mismatch_verdict_for_this_host() == ("torch_cuda_unavailable", "2.6.0+cu124")
+
+ monkeypatch.setattr(hw, "get_physical_gpu_inventory", lambda **_kw: {"devices": []})
+ assert hw._mismatch_verdict_for_this_host() == (None, None)
+
+
+def test_cuda_visible_devices_masks_an_amd_host_too(monkeypatch):
+ """HIP honours CUDA_VISIBLE_DEVICES alongside its own variables.
+
+ This module's own visibility resolver reads all three together on an AMD host, so
+ mapping the variable to NVIDIA alone had an AMD-only box launched with
+ CUDA_VISIBLE_DEVICES="" reported as broken and offered a repair for a mask its
+ owner set on purpose.
+ """
+ import sys
+
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cpu"))
+ amd = [{"vendor": "amd", "name": "Radeon RX 7900 XT", "gfx_candidates": ["gfx1100"]}]
+ monkeypatch.setattr(
+ hw, "get_physical_gpu_inventory", lambda **_kw: {"devices": amd, "unknown": False}
+ )
+
+ monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "")
+ assert hw.classify_torch_build() is None
+ assert hw._devices_that_can_establish_a_mismatch(amd) == []
+
+ monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "0")
+ assert hw.classify_torch_build() == "torch_cpu_build"
+ assert hw._devices_that_can_establish_a_mismatch(amd) == amd
+
+
+def test_the_health_path_never_retries_a_failed_torch_import(monkeypatch):
+ """The label was read with _torch_version_label(), which imports torch.
+
+ On the broken-runtime host that import is what fails: it can take seconds, and
+ _has_torch() purges the partial module afterwards, so each call genuinely re-runs
+ the native load. /api/health, /api/liveness and GET /api/system all reach it.
+ """
+ calls = {"n": 0}
+
+ def _label():
+ calls["n"] += 1
+ return "should not be reached"
+
+ monkeypatch.setattr(hw, "TORCH_IMPORT_ERROR", "OSError('libcudart.so.12')")
+ monkeypatch.setattr(hw, "_torch_version_label", _label)
+ monkeypatch.setattr(hw, "_installed_torch_label_on_disk", lambda: "2.6.0+cu124")
+ monkeypatch.setattr(hw, "CHAT_ONLY_REASON", "torch_cuda_unavailable")
+ monkeypatch.setattr(hw, "CHAT_ONLY_DETAIL", "2.6.0+cu124")
+ monkeypatch.setattr(hw, "classify_torch_build", lambda **_kw: "torch_cuda_unavailable")
+ monkeypatch.setattr(hw, "_torch_reports_a_usable_accelerator", lambda: False)
+ monkeypatch.setattr(
+ hw,
+ "get_physical_gpu_inventory",
+ lambda **_kw: {
+ "devices": [{"vendor": "nvidia", "name": "A4000"}],
+ "sources": ["nvidia-smi"],
+ "unknown": False,
+ },
+ )
+ hw.torch_build_snapshot()
+
+ assert hw.current_chat_only_verdict() == ("torch_cuda_unavailable", "2.6.0+cu124")
+ report = hw._torch_gpu_mismatch_report()
+ assert report["mismatch"]["torch_version"] == "2.6.0+cu124"
+ assert calls["n"] == 0, "the failed import must not be retried on a request path"
+
+ monkeypatch.setattr(hw, "TORCH_IMPORT_ERROR", None)
+ monkeypatch.setattr(hw, "_torch_version_label", lambda: "2.11.0+cpu")
+ assert hw._reported_torch_label() == "2.11.0+cpu"
+
+
+def test_an_amd_card_the_installers_decline_is_not_a_broken_install(monkeypatch):
+ """setup.sh ships a ROCm wheel only for RDNA 2 and newer.
+
+ A Polaris or RDNA 1 card is left on CPU torch on purpose, and the DRM sysfs walk
+ reports it as vendor amd like any other, so it was being counted as evidence of a
+ broken install and offered a repair that cannot make that GPU usable.
+ """
+ monkeypatch.setattr(hw, "_expected_rocm_flavor_was_chosen", lambda: False)
+ monkeypatch.setattr(hw, "_torch_reports_a_hip_runtime", lambda: False)
+
+ polaris = [{"vendor": "amd", "gfx_candidates": ["gfx803"]}]
+ assert hw._devices_that_can_establish_a_mismatch(polaris) == []
+
+ supported = [{"vendor": "amd", "gfx_candidates": ["gfx1100"]}]
+ assert hw._devices_that_can_establish_a_mismatch(supported) == supported
+
+ # A host with no ROCm userspace names no arch at all, and setup.sh detects AMD only
+ # through rocminfo and amd-smi, so that machine was never going to get a ROCm wheel.
+ unnamed = [{"vendor": "amd"}]
+ assert hw._devices_that_can_establish_a_mismatch(unnamed) == []
+
+ assert hw._devices_that_can_establish_a_mismatch([{"vendor": "amd", "gfx": "gfx1201"}])
+ assert hw._devices_that_can_establish_a_mismatch([{"vendor": "amd", "gfx": "gfx900"}]) == []
+
+
+def test_a_rocm_expectation_outranks_the_arch_table(monkeypatch):
+ """If this install asked for a ROCm wheel, its absence is a fault either way."""
+ monkeypatch.setattr(hw, "_torch_reports_a_hip_runtime", lambda: False)
+ monkeypatch.setattr(hw, "_expected_rocm_flavor_was_chosen", lambda: True)
+ old = [{"vendor": "amd", "gfx_candidates": ["gfx803"]}]
+ assert hw._devices_that_can_establish_a_mismatch(old) == old
+
+ monkeypatch.setattr(hw, "_expected_rocm_flavor_was_chosen", lambda: False)
+ monkeypatch.setattr(hw, "_torch_reports_a_hip_runtime", lambda: True)
+ assert hw._devices_that_can_establish_a_mismatch(old) == old
+
+
+def test_the_supported_arch_set_matches_the_installer(monkeypatch):
+ """One table in two places drifts, and it drifts BOTH ways.
+
+ install.sh's _amd_arch_index_family_for_gfx is the map that decides whether this
+ stack ships a ROCm wheel for a card (it mirrors install.ps1's $archFamilyMap). A gfx
+ it lists but the backend does not is a supported card reported as no_gpu with no
+ repair; one the backend lists but it does not is a repair that cannot help.
+ """
+ import pathlib
+ import re
+
+ root = pathlib.Path(hw.__file__).resolve().parents[4]
+ install_sh = (root / "install.sh").read_text(encoding = "utf-8")
+ block = install_sh[install_sh.index("_amd_arch_index_family_for_gfx()") :]
+ block = block[: block.index("esac")]
+ # Only the case LABELS: the values on the right are index families (gfx103X-all), whose
+ # prefixes a bare regex over the block would collect.
+ shipped = {
+ gfx
+ for line in block.splitlines()
+ for gfx in re.findall(r"gfx[0-9a-f]+", line.split(")")[0])
+ }
+ assert len(shipped) > 10, "the installer's arch map moved"
+
+ extra = {"gfx906"}
+ assert set(hw._ROCM_SUPPORTED_GFX) == shipped | extra, (
+ "the backend must accept exactly the architectures the installers ship a wheel "
+ f"for; installer has {sorted(shipped | extra)}"
+ )
+ stack = (root / "studio" / "install_python_stack.py").read_text(encoding = "utf-8")
+ assert "gfx906" in stack, "the gfx906 path this set carries has gone"
+
+
+def test_the_gfx_probe_answers_nothing_without_a_rocm_userspace(monkeypatch):
+ """No rocminfo and no amd-smi is the common case on the host in question."""
+
+ def _missing(*_a, **_k):
+ raise FileNotFoundError("rocminfo")
+
+ monkeypatch.setattr(hw.subprocess, "run", _missing)
+ assert hw._linux_amd_gfx_candidates() == []
+
+ class _Result:
+ stdout = " Name: gfx1030\n Name: gfx1030\n"
+
+ monkeypatch.setattr(hw.subprocess, "run", lambda *_a, **_k: _Result())
+ assert hw._linux_amd_gfx_candidates() == ["gfx1030"]
+
+
+def test_a_cold_start_measures_the_inventory_before_honouring_a_mask(monkeypatch):
+ """An irrelevant empty mask must not decide the verdict from a cold cache.
+
+ HIP_VISIBLE_DEVICES="" on an NVIDIA-only host is not a statement about the NVIDIA
+ card, but on the first pass the non-blocking read answers unknown, every mask counts
+ as relevant, and the classification was suppressed and cached as "torch is fine" for
+ a whole TTL -- withholding the repair from a host nvidia-smi describes a moment
+ later.
+ """
+ import sys
+
+ probes = {"n": 0}
+
+ def _probe():
+ probes["n"] += 1
+ return {
+ "available": True,
+ "devices": [{"vendor": "nvidia", "name": "A4000"}],
+ "sources": ["nvidia-smi"],
+ "unknown": False,
+ }
+
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cpu"))
+ monkeypatch.setattr(hw, "_probe_physical_gpu_inventory", _probe)
+ monkeypatch.setenv("HIP_VISIBLE_DEVICES", "")
+
+ snapshot = hw.torch_build_snapshot()
+
+ assert probes["n"] >= 1, "the cached pass must measure rather than read a cold cache"
+ assert (
+ snapshot["reason"] == "torch_cpu_build"
+ ), "a HIP mask says nothing about an NVIDIA card and must not suppress the repair"
+
+
+def test_an_absent_nvidia_smi_is_an_answer_not_a_failed_probe(monkeypatch):
+ """An AMD-only host has no nvidia-smi by design.
+
+ Marking its inventory unknown kept a settled mismatch alive for good once the AMD
+ card that established it was detached: the sysfs walk correctly found nothing, and
+ the verdict refresh read the unknown as "the probe declined" and preserved the old
+ reason, while /api/system had already dropped the device rows.
+ """
+
+ def _missing(*_a, **_k):
+ raise FileNotFoundError("nvidia-smi")
+
+ monkeypatch.setattr(nvidia.subprocess, "run", _missing)
+ monkeypatch.setattr(nvidia, "_linux_nvidia_procfs_gpu_count", lambda: 0)
+ monkeypatch.setattr(hw.platform, "system", lambda: "Linux")
+ monkeypatch.setattr(hw, "_linux_drm_sysfs_records", lambda **_kw: [])
+
+ inventory = hw.get_physical_gpu_inventory()
+ assert inventory["devices"] == []
+ assert inventory["unknown"] is False, (
+ "no nvidia-smi is the normal state of an AMD or CPU-only host, not a probe "
+ "that could not answer"
+ )
+
+ monkeypatch.setattr(hw, "CHAT_ONLY_REASON", "torch_cpu_build")
+ monkeypatch.setattr(hw, "CHAT_ONLY_DETAIL", "2.11.0+cpu")
+ monkeypatch.setattr(hw, "classify_torch_build", lambda **_kw: "torch_cpu_build")
+ monkeypatch.setattr(hw, "_torch_reports_a_usable_accelerator", lambda: False)
+ hw.torch_build_snapshot()
+ assert hw.current_chat_only_verdict() == ("no_gpu", None)
+
+ def _hang(*_a, **_k):
+ raise subprocess.TimeoutExpired("nvidia-smi", 10)
+
+ monkeypatch.setattr(nvidia.subprocess, "run", _hang)
+ monkeypatch.setattr(hw, "_physical_gpu_inventory_cache", None)
+ assert hw.get_physical_gpu_inventory()["unknown"] is True
+
+
+def test_a_second_recovery_can_still_ask_for_a_pass(monkeypatch):
+ """The guard is one request per recovery, not one per process.
+
+ It was cleared only by detect_hardware(), while the recovery starts its pass through
+ start_background_detection(), which runs ensure_hardware_detected(). So the first
+ recovery raised the guard for good: a driver that flapped, or any later lifecycle
+ that published an inventory-sensitive CPU verdict, could never get another pass.
+ """
+ import sys
+
+ monkeypatch.setattr(hw, "_REDETECTION_REQUESTED", False)
+ monkeypatch.setattr(hw, "CHAT_ONLY_REASON", "torch_cuda_unavailable")
+ monkeypatch.setattr(hw, "CHAT_ONLY_DETAIL", "2.6.0+cu124")
+ monkeypatch.setattr(hw, "DEVICE", hw.DeviceType.CPU)
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cuda"))
+ starts = {"n": 0}
+ monkeypatch.setattr(hw, "invalidate_detection", lambda: 1)
+ monkeypatch.setattr(hw, "_discard_detection_locked", lambda: None)
+ monkeypatch.setattr(
+ hw, "start_background_detection", lambda: starts.__setitem__("n", starts["n"] + 1)
+ )
+
+ hw.torch_build_snapshot()
+ hw.current_chat_only_verdict()
+ assert starts["n"] == 1
+ assert hw._REDETECTION_REQUESTED is True, "one request per recovery, while it runs"
+
+ source = _hardware_source()
+ ensure = source[source.index("def ensure_hardware_detected(") :]
+ ensure = ensure[: ensure.index("def _detect_hardware_locked(")]
+ assert (
+ "_REDETECTION_REQUESTED = False" in ensure
+ ), "a settled background pass must release the guard, or there is never a second one"
+ assert ensure.index("DETECTION_COMPLETE.set()") < ensure.index(
+ "_REDETECTION_REQUESTED = False"
+ ), "released only once the pass has actually published"
+
+
+def test_one_capability_response_describes_one_host(monkeypatch):
+ """Three reads of a verdict that refreshes on a TTL can disagree.
+
+ An eGPU attached or removed mid-response could give the reason from one host and the
+ message from another, or a reason with no message at all.
+ """
+ verdicts = [
+ ("torch_cpu_build", "2.11.0+cpu"),
+ ("no_gpu", None),
+ (None, None),
+ ]
+ monkeypatch.setattr(hw, "DEVICE", hw.DeviceType.CPU)
+ monkeypatch.setattr(hw, "get_device", lambda: hw.DeviceType.CPU)
+ monkeypatch.setattr(hw, "_has_torch", lambda: True)
+ monkeypatch.setattr(hw, "is_apple_silicon", lambda: False)
+ monkeypatch.setattr(hw.platform, "system", lambda: "Linux")
+ monkeypatch.setattr(
+ hw, "current_chat_only_verdict", lambda: verdicts.pop(0) if verdicts else (None, None)
+ )
+
+ export = hw.export_capability()
+ assert export["export_unsupported_reason"] == "torch_cpu_build"
+ assert "CPU-only build" in export["export_unsupported_message"]
+ assert len(verdicts) == 2, "the response must be built from ONE reading of the verdict"
+
+
+def _hardware_source() -> str:
+ import pathlib
+ return pathlib.Path(hw.__file__).resolve().read_text(encoding = "utf-8")
+
+
+def test_the_hardware_module_loads_without_the_rest_of_the_package(tmp_path):
+ """tests/python/test_e2e_no_torch_sandbox.py executes this module on its own.
+
+ It builds a minimal stub tree -- loggers, structlog, utils/hardware -- and nothing
+ else, so a top-level import of anything further inside utils makes the module
+ unloadable there, which is how a Windows-only console-hiding helper broke hardware
+ detection on a host with no torch at all.
+ """
+ import pathlib
+ import re as _re
+
+ source = pathlib.Path(hw.__file__).resolve().read_text(encoding = "utf-8")
+ header = source[: source.index("logger = get_logger(__name__)")]
+ imports = _re.findall(r"^\s*(?:from|import)\s+([\w.]+)", header, _re.MULTILINE)
+ for module in imports:
+ if module == "utils.hardware" or module.startswith("utils.hardware."):
+ continue
+ assert not module.startswith("utils"), (
+ f"{module} is imported at module scope; the no-torch sandbox stubs only "
+ "utils.hardware, so import it inside the function that needs it"
+ )
+
+
+def test_a_driver_without_the_cli_still_reports_its_cards(monkeypatch):
+ """/proc/driver/nvidia/gpus is published whatever nvidia-smi's state is.
+
+ install_python_stack._has_usable_nvidia_gpu() falls back to it for the same reason,
+ so without this the installer could select or repair a CUDA wheel on a host where
+ the backend insisted there was no card, and the user got no_gpu with no repair.
+ """
+
+ def _missing(*_a, **_k):
+ raise FileNotFoundError("nvidia-smi")
+
+ monkeypatch.setattr(nvidia.subprocess, "run", _missing)
+ monkeypatch.setattr(nvidia, "_linux_nvidia_procfs_gpu_count", lambda: 2)
+
+ result = nvidia.get_physical_gpu_inventory()
+ assert result["available"] is True
+ assert result["absent"] is False
+ assert [d["vendor"] for d in result["devices"]] == ["nvidia", "nvidia"]
+ assert [d["name"] for d in result["devices"]] == [None, None]
+ assert [d["memory_total_gb"] for d in result["devices"]] == [None, None]
+ assert result["source"] == "proc-driver-nvidia"
+
+
+def test_a_kfd_confirmed_amd_card_stays_eligible(monkeypatch):
+ """A minimal AMD host with /dev/kfd but no rocminfo or amd-smi.
+
+ install_python_stack._has_rocm_gpu() uses the KFD topology as its fallback exactly
+ so `studio update` can repair CPU-only torch there, so a card the installer would
+ repair cannot be dropped from the evidence here.
+ """
+ monkeypatch.setattr(hw, "_expected_rocm_flavor_was_chosen", lambda: False)
+ monkeypatch.setattr(hw, "_torch_reports_a_hip_runtime", lambda: False)
+ unnamed = [{"vendor": "amd"}]
+
+ monkeypatch.setattr(hw, "_linux_kfd_reports_an_amd_gpu", lambda: True)
+ assert hw._devices_that_can_establish_a_mismatch(unnamed) == unnamed
+
+ monkeypatch.setattr(hw, "_linux_kfd_reports_an_amd_gpu", lambda: False)
+ assert hw._devices_that_can_establish_a_mismatch(unnamed) == []
+
+ monkeypatch.setattr(hw, "_linux_kfd_reports_an_amd_gpu", lambda: True)
+ assert (
+ hw._devices_that_can_establish_a_mismatch([{"vendor": "amd", "gfx_candidates": ["gfx803"]}])
+ == []
+ )
+
+
+def test_the_kfd_probe_rejects_a_non_amd_node(monkeypatch, tmp_path):
+ """The NVIDIA open kernel module registers KFD nodes of its own.
+
+ Their vendor_id is 4318, not AMD's 4098, and the installer guards on exactly that,
+ so an NVIDIA-only host must not read as AMD here either.
+ """
+ nodes = tmp_path / "nodes"
+ for name, gpu_id, vendor in (("0", "0", "4098"), ("1", "5555", "4318")):
+ node = nodes / name
+ node.mkdir(parents = True)
+ (node / "gpu_id").write_text(gpu_id, encoding = "utf-8")
+ (node / "properties").write_text(f"vendor_id {vendor}\n", encoding = "utf-8")
+
+ monkeypatch.setattr(hw.platform, "system", lambda: "Linux")
+ real_listdir, real_open = hw.os.listdir, open
+ monkeypatch.setattr(
+ hw.os,
+ "listdir",
+ lambda path: real_listdir(nodes) if "kfd" in str(path) else real_listdir(path),
+ )
+ monkeypatch.setattr(
+ hw.os.path,
+ "join",
+ lambda *parts: str(nodes.joinpath(*parts[1:])) if "kfd" in parts[0] else "/".join(parts),
+ )
+ assert (
+ hw._linux_kfd_reports_an_amd_gpu() is False
+ ), "a CPU node and an NVIDIA-owned node are not an AMD GPU"
+
+ (nodes / "1" / "properties").write_text("vendor_id 4098\n", encoding = "utf-8")
+ assert hw._linux_kfd_reports_an_amd_gpu() is True
+ assert real_open is open
+
+
+def test_the_installer_records_who_named_the_flavor(tmp_path, monkeypatch):
+ """The manifest has to carry the provenance, or the backend cannot ask.
+
+ setup.ps1 publishes UNSLOTH_EXPECTED_TORCH_TAG for an automatic /cpu choice exactly
+ as it does for a pinned one, so the handover variable alone is not evidence.
+ """
+ import importlib.util
+ import pathlib
+
+ root = pathlib.Path(hw.__file__).resolve().parents[3]
+ spec = importlib.util.spec_from_file_location(
+ "_install_manifest_probe", root / "install_manifest.py"
+ )
+ manifest_mod = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(manifest_mod)
+
+ monkeypatch.setattr(manifest_mod, "venv_root", lambda: tmp_path)
+ monkeypatch.setattr(manifest_mod, "manifest_path", lambda root = None: tmp_path / "m.json")
+ monkeypatch.setattr(manifest_mod, "requirement_digests", lambda *_a, **_k: {})
+ monkeypatch.setattr(manifest_mod, "installed_requirements_root", lambda *_a, **_k: None)
+ monkeypatch.setattr(manifest_mod, "_installed_version", lambda *_a, **_k: "0")
+
+ import json as _json
+
+ manifest_mod.write_manifest(expected_torch_tag = "cpu", expected_torch_tag_pinned = False)
+ written = _json.loads((tmp_path / "m.json").read_text(encoding = "utf-8"))
+ assert written["expected_torch_tag"] == "cpu"
+ assert written["expected_torch_tag_pinned"] is False
+
+ manifest_mod.write_manifest(expected_torch_tag = "cpu", expected_torch_tag_pinned = True)
+ written = _json.loads((tmp_path / "m.json").read_text(encoding = "utf-8"))
+ assert written["expected_torch_tag_pinned"] is True
+
+ manifest_mod.write_manifest(expected_torch_tag = "cpu")
+ written = _json.loads((tmp_path / "m.json").read_text(encoding = "utf-8"))
+ assert "expected_torch_tag_pinned" not in written
+ monkeypatch.setattr(manifest_mod, "read_manifest", lambda root = None: written)
+ assert manifest_mod.recorded_torch_flavor_was_pinned() is False
+
+
+def test_a_vendor_that_did_not_answer_keeps_the_inventory_unknown(monkeypatch):
+ """A hybrid Intel iGPU plus NVIDIA dGPU host with nvidia-smi timing out.
+
+ The Intel row cancelled the unknown, and _devices_that_can_establish_a_mismatch then
+ discarded the iGPU as ineligible, so the verdict saw neither an eligible card nor an
+ unanswered probe and downgraded a settled mismatch to no_gpu for a cache interval --
+ hiding the repair while the NVIDIA probe was merely unavailable.
+ """
+ monkeypatch.setattr(hw.platform, "system", lambda: "Linux")
+ monkeypatch.setattr(
+ hw,
+ "_linux_drm_sysfs_records",
+ lambda **_kw: [{"vendor": "intel", "name": None, "index": 0}],
+ )
+
+ def _timeout(*_a, **_k):
+ raise subprocess.TimeoutExpired("nvidia-smi", 10)
+
+ monkeypatch.setattr(nvidia.subprocess, "run", _timeout)
+ monkeypatch.setattr(nvidia, "_linux_nvidia_procfs_gpu_count", lambda: 0)
+
+ inventory = hw.get_physical_gpu_inventory()
+ assert [d["vendor"] for d in inventory["devices"]] == ["intel"]
+ assert inventory["unknown"] is True
+
+ _smi(monkeypatch, "0, NVIDIA RTX A4000, 16376\n")
+ monkeypatch.setattr(hw, "_physical_gpu_inventory_cache", None)
+ assert hw.get_physical_gpu_inventory()["unknown"] is False
+
+
+def test_a_stale_registry_record_cannot_claim_a_longer_named_live_card(monkeypatch):
+ """ "RX 7900 XT" is a prefix of a live "RX 7900 XTX".
+
+ Records are walked in LUID order and consume the first live name they match, so the
+ stale XT could claim the XTX that is really installed and the inventory would
+ publish the removed card's name and its VRAM.
+ """
+ live = ["AMD Radeon RX 7900 XTX"]
+ assert hw._claim_live_adapter("AMD Radeon RX 7900 XTX", live) == 0
+
+ both = ["AMD Radeon RX 7900 XTX", "AMD Radeon RX 7900 XT"]
+ assert both[hw._claim_live_adapter("AMD Radeon RX 7900 XT", both)] == "AMD Radeon RX 7900 XT"
+ assert both[hw._claim_live_adapter("AMD Radeon RX 7900 XTX", both)] == "AMD Radeon RX 7900 XTX"
+
+ # The prefix rule still has to work: the registry description and the WMI display name
+ # spell the same card differently, and one is routinely a prefix of the other.
+ assert hw._claim_live_adapter("AMD Radeon RX 7900 XTX", ["AMD Radeon RX 7900 XTX 24GB"]) == 0
+ assert hw._claim_live_adapter("Something Else", live) is None
+
+
+def test_a_broken_nvidia_smi_still_reports_the_kernel_driver_cards(monkeypatch):
+ """Absent is not the only way the CLI fails to answer.
+
+ A nvidia-smi that hangs past its timeout, or exits non-zero, leaves the kernel
+ driver enumerating cards regardless. On a cold start there is no settled verdict for
+ the resulting unknown to protect, so the host was reported as having no GPU at all,
+ with no repair, for as long as the CLI stayed broken.
+ """
+ monkeypatch.setattr(nvidia, "_linux_nvidia_procfs_gpu_count", lambda: 1)
+
+ def _hang(*_a, **_k):
+ raise subprocess.TimeoutExpired("nvidia-smi", 10)
+
+ monkeypatch.setattr(nvidia.subprocess, "run", _hang)
+ result = nvidia.get_physical_gpu_inventory()
+ assert [d["vendor"] for d in result["devices"]] == ["nvidia"]
+ assert result["source"] == "proc-driver-nvidia"
+
+ monkeypatch.setattr(
+ nvidia.subprocess,
+ "run",
+ lambda *_a, **_k: SimpleNamespace(returncode = 9, stdout = ""),
+ )
+ assert nvidia.get_physical_gpu_inventory()["available"] is True
+
+ monkeypatch.setattr(nvidia, "_linux_nvidia_procfs_gpu_count", lambda: 0)
+ assert nvidia.get_physical_gpu_inventory()["error"] is not None
+
+
+def test_hip_visible_devices_outranks_the_cuda_alias(monkeypatch):
+ """HIP reads its own variables first; the alias is only a fallback.
+
+ An AMD host that NAMES its devices in HIP_VISIBLE_DEVICES while inheriting an empty
+ CUDA_VISIBLE_DEVICES has not hidden anything, and _get_parent_visible_gpu_spec in
+ this same module already applies that precedence.
+ """
+ import sys
+
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cpu"))
+ amd = [{"vendor": "amd", "gfx_candidates": ["gfx1100"]}]
+ monkeypatch.setattr(
+ hw, "get_physical_gpu_inventory", lambda **_kw: {"devices": amd, "unknown": False}
+ )
+
+ monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "")
+ monkeypatch.setenv("HIP_VISIBLE_DEVICES", "0")
+ assert hw.classify_torch_build() == "torch_cpu_build"
+ assert hw._devices_that_can_establish_a_mismatch(amd) == amd
+
+ monkeypatch.setenv("HIP_VISIBLE_DEVICES", "")
+ assert hw.classify_torch_build() is None
+ assert hw._devices_that_can_establish_a_mismatch(amd) == []
+
+ monkeypatch.delenv("HIP_VISIBLE_DEVICES")
+ assert hw.classify_torch_build() is None
+
+
+def test_only_the_highest_priority_mask_that_is_set_decides(monkeypatch):
+ """A lower-priority variable naming devices does not un-hide them.
+
+ HIP stops at the first of the three that is SET, so HIP_VISIBLE_DEVICES=-1 hides
+ every AMD card however ROCR_VISIBLE_DEVICES is spelled. Reading the ROCR value there
+ would report a mismatch, and offer a repair, for a host masked exactly as asked.
+ """
+ import sys
+
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch("cpu"))
+ amd = [{"vendor": "amd", "gfx_candidates": ["gfx1100"]}]
+ monkeypatch.setattr(
+ hw, "get_physical_gpu_inventory", lambda **_kw: {"devices": amd, "unknown": False}
+ )
+ monkeypatch.delenv("CUDA_VISIBLE_DEVICES", raising = False)
+
+ monkeypatch.setenv("HIP_VISIBLE_DEVICES", "-1")
+ monkeypatch.setenv("ROCR_VISIBLE_DEVICES", "0")
+ assert hw.classify_torch_build() is None
+ assert hw._devices_that_can_establish_a_mismatch(amd) == []
+
+ # The other direction, so this is precedence rather than "an emptied one always wins":
+ # ROCR only decides when HIP is unset, and then a named device is a real expectation.
+ monkeypatch.delenv("HIP_VISIBLE_DEVICES")
+ assert hw.classify_torch_build() == "torch_cpu_build"
+ assert hw._devices_that_can_establish_a_mismatch(amd) == amd
+
+
+# =============================================== a refresh that cannot answer is not news
+
+
+def test_an_unanswerable_refresh_keeps_the_cards_it_already_found(monkeypatch):
+ """nvidia-smi timing out must not read as "the GPUs were removed".
+
+ Overwriting the cache with the empty failure holds for a whole TTL, and the two
+ consumers then disagree inside one response: current_chat_only_verdict() keeps its
+ frozen mismatch on ``unknown`` while _torch_gpu_mismatch_report() reads the devices,
+ finds none, and drops physical_devices and mismatch from /api/system.
+ """
+ monkeypatch.setattr(hw.platform, "system", lambda: "Linux")
+ monkeypatch.setattr(hw, "_physical_gpu_inventory_cache", None)
+ _smi(monkeypatch, _TWO_A4000_ROWS)
+ good = hw.get_physical_gpu_inventory()
+ assert len(good["devices"]) == 2 and good["unknown"] is False
+
+ monkeypatch.setattr(hw, "_physical_gpu_inventory_cache", (0.0, good))
+ _smi(monkeypatch, "", returncode = 9)
+ after = hw.get_physical_gpu_inventory()
+
+ assert [d["name"] for d in after["devices"]] == [d["name"] for d in good["devices"]]
+ assert after["available"] is True
+ # Still unknown: these rows describe the host as it was, not as this pass measured it.
+ assert after["unknown"] is True
+ assert after["unanswered"] == ["nvidia"]
+
+
+def test_a_vendor_that_answered_none_is_allowed_to_lose_its_cards(monkeypatch):
+ """The carry-forward is per unanswered vendor only, so a real removal still lands."""
+ monkeypatch.setattr(hw.platform, "system", lambda: "Linux")
+ _smi(monkeypatch, _TWO_A4000_ROWS)
+ good = hw.get_physical_gpu_inventory()
+
+ monkeypatch.setattr(hw, "_physical_gpu_inventory_cache", (0.0, good))
+ _smi(monkeypatch, "", raises = FileNotFoundError)
+ after = hw.get_physical_gpu_inventory()
+
+ assert after["devices"] == []
+ assert after["unknown"] is False
+
+
+def test_a_registry_that_cannot_be_read_is_not_a_host_without_adapters(monkeypatch):
+ """`{}` from the DirectX helper covers both, so the inventory has to ask which."""
+ monkeypatch.setattr(hw.platform, "system", lambda: "Windows")
+ # nvidia-smi ANSWERS (absent), so only the registry vendors go unanswered here.
+ _smi(monkeypatch, "", raises = FileNotFoundError)
+ monkeypatch.setattr(hw, "_windows_live_adapter_names", lambda: ["AMD Radeon RX 7900 XT"])
+ monkeypatch.setattr(
+ hw,
+ "_windows_amd_adapter_records_by_luid",
+ lambda vendor_id = hw._AMD_PCI_VENDOR_ID, **kw: (
+ None if kw.get("distinguish_failure") else {}
+ ),
+ )
+
+ inventory = hw.get_physical_gpu_inventory()
+
+ assert inventory["devices"] == []
+ assert inventory["unknown"] is True
+ assert inventory["unanswered"] == ["amd", "intel"]
+
+
+def test_the_ranking_callers_still_see_an_empty_map(monkeypatch):
+ """Only the inventory asked for the distinction; nothing else changed shape."""
+ monkeypatch.setattr(hw.platform, "system", lambda: "Windows")
+ monkeypatch.setattr(hw, "_windows_amd_adapter_records_or_none", lambda *a, **k: None)
+ assert hw._windows_amd_adapter_records_by_luid() == {}
+ assert hw._windows_amd_adapter_records_by_luid(distinguish_failure = True) is None
+
+
+# ============================================ a Windows AMD card the registry did not name
+
+
+ "name,eligible",
+ [
+ ("AMD Radeon RX 7900 XT", True),
+ ("AMD Radeon RX 9070 XT", True),
+ ("AMD Radeon 780M Graphics", True),
+ # Polaris and RDNA 1: no wheel family covers them, so a repair could not change
+ # anything and this host is on CPU torch on purpose.
+ ("AMD Radeon RX 580", False),
+ ("AMD Radeon RX 5700 XT", False),
+ ("", False),
+ ],
+)
+def test_a_windows_adapter_with_no_adapter_family_is_read_from_its_name(
+ monkeypatch, name, eligible
+):
+ """AdapterFamily is written by the driver, so the reported RX 7900 XT arrives bare.
+
+ Falling through to the KFD probe discards it: that probe is Linux-only and returns
+ False on Windows, which made Windows accidentally stricter than Linux for the exact
+ card this feature was written for.
+ """
+ monkeypatch.setattr(hw.platform, "system", lambda: "Windows")
+ monkeypatch.setattr(hw, "_linux_kfd_reports_an_amd_gpu", lambda: False)
+ device = {"vendor": "amd", "name": name, "source": "directx-registry"}
+ assert hw._amd_device_can_establish_a_mismatch(device) is eligible
+
+
+def test_a_named_arch_still_wins_over_the_marketing_name(monkeypatch):
+ """The name table only fills a gap; it never overrides what the driver reported."""
+ monkeypatch.setattr(hw, "_linux_kfd_reports_an_amd_gpu", lambda: True)
+ assert (
+ hw._amd_device_can_establish_a_mismatch(
+ {"vendor": "amd", "name": "AMD Radeon RX 7900 XT", "gfx": "gfx803"}
+ )
+ is False
+ )
+
+
+# ================================================== only a real ROCm family is a ROCm pin
+
+
+ "leaf,chosen",
+ [
+ ("rocm6.4", True),
+ ("rocm7", True),
+ ("gfx1151", True),
+ ("gfx120X-all", True),
+ # Suffixed leaves are custom pins the installer routes verbatim. Reading one as ROCm
+ # waives the supported-architecture filter and calls a deliberate install broken.
+ ("rocm-rel-7.2.1", False),
+ ("rocm7.2-private", False),
+ ("gfx-mirror", False),
+ ("cpu", False),
+ ],
+)
+def test_only_the_installers_rocm_families_count_as_a_rocm_choice(monkeypatch, leaf, chosen):
+ monkeypatch.delenv("UNSLOTH_TORCH_INDEX_URL", raising = False)
+ monkeypatch.setenv("UNSLOTH_TORCH_INDEX_FAMILY", f"https://example.invalid/whl/{leaf}")
+ monkeypatch.setattr(hw.sys, "prefix", "/nonexistent-prefix-for-this-test")
+ assert hw._expected_rocm_flavor_was_chosen() is chosen
+
+
+def test_the_backend_and_the_installer_agree_on_the_family_predicate():
+ """One vocabulary, two files: drift here is a silent behaviour split."""
+ for leaf in (
+ "rocm6.4",
+ "rocm7",
+ "gfx1151",
+ "gfx120X-all",
+ "rocm-rel-7.2.1",
+ "rocm7.2-private",
+ "gfx-mirror",
+ "cpu",
+ "cu124",
+ "",
+ ):
+ assert hw._is_pip_rocm_family_leaf(leaf) == _installer_rocm_family(leaf), leaf
+
+
+def _installer_rocm_family(leaf: str) -> bool:
+ """install_python_stack._is_pip_rocm_family_leaf, loaded without importing the module."""
+ import ast
+ import pathlib
+ import re as _re
+
+ source = (pathlib.Path(__file__).resolve().parents[2] / "install_python_stack.py").read_text(
+ encoding = "utf-8"
+ )
+ tree = ast.parse(source)
+ fn = next(
+ node
+ for node in tree.body
+ if isinstance(node, ast.FunctionDef) and node.name == "_is_pip_rocm_family_leaf"
+ )
+ namespace: dict = {"re": _re}
+ exec(compile(ast.Module(body = [fn], type_ignores = []), "<installer>", "exec"), namespace)
+ return namespace["_is_pip_rocm_family_leaf"](leaf)
+
+
+# ======================================= a detection failure the disk already explained
+
+
+def test_a_disk_classified_detection_failure_transitions_when_the_probe_recovers(monkeypatch):
+ """torch will not import AND the OS probe timed out: only the inventory was missing.
+
+ detect_hardware() classifies the wheel from disk for exactly this host, then publishes
+ detection_failed when the inventory cannot corroborate it. Freezing that forever leaves
+ /api/system reporting the recovered mismatch while the sidebar, Export and Video keep
+ saying detection failed and never offer the repair.
+ """
+ monkeypatch.setattr(hw, "CHAT_ONLY_REASON", "detection_failed")
+ monkeypatch.setattr(hw, "CHAT_ONLY_DETAIL", None)
+ monkeypatch.setattr(hw, "TORCH_IMPORT_ERROR", OSError("[WinError 126] cudart64_12.dll"))
+ monkeypatch.setattr(hw, "_installed_torch_label_on_disk", lambda: "2.11.0+cpu")
+ monkeypatch.setattr(
+ hw,
+ "torch_build_snapshot",
+ lambda **_kw: {"reason": "torch_cpu_build", "usable": False, "unknown": False},
+ )
+ monkeypatch.setattr(
+ hw,
+ "get_physical_gpu_inventory",
+ lambda **_kw: {
+ "devices": [{"vendor": "nvidia", "name": "NVIDIA RTX A4000"}],
+ "unknown": False,
+ },
+ )
+
+ assert hw.current_chat_only_verdict() == ("torch_cpu_build", "2.11.0+cpu")
+
+
+def test_a_detection_failure_never_degrades_into_no_gpu(monkeypatch):
+ """A host that never measured cannot be told it has no GPU; that IS the claim
+ detect_hardware() refused to make for it."""
+ monkeypatch.setattr(hw, "CHAT_ONLY_REASON", "detection_failed")
+ monkeypatch.setattr(hw, "CHAT_ONLY_DETAIL", None)
+ monkeypatch.setattr(hw, "TORCH_IMPORT_ERROR", OSError("boom"))
+ monkeypatch.setattr(
+ hw,
+ "torch_build_snapshot",
+ lambda **_kw: {"reason": "torch_cpu_build", "usable": False, "unknown": False},
+ )
+ monkeypatch.setattr(
+ hw, "get_physical_gpu_inventory", lambda **_kw: {"devices": [], "unknown": False}
+ )
+
+ assert hw.current_chat_only_verdict() == ("detection_failed", None)
+
+
[email protected]("reason", ["mlx_unavailable", "intel_mac"])
+def test_the_other_frozen_verdicts_stay_frozen(monkeypatch, reason):
+ """Only the disk-classified failure moved; a 60 second probe still cannot speak to these."""
+ monkeypatch.setattr(hw, "CHAT_ONLY_REASON", reason)
+ monkeypatch.setattr(hw, "CHAT_ONLY_DETAIL", "detail")
+ monkeypatch.setattr(hw, "TORCH_IMPORT_ERROR", OSError("boom"))
+ assert hw.current_chat_only_verdict() == (reason, "detail")
+
+
+def test_a_detection_failure_with_an_importable_torch_stays_frozen(monkeypatch):
+ """Nothing classified this host, so there is nothing for the inventory to confirm."""
+ monkeypatch.setattr(hw, "CHAT_ONLY_REASON", "detection_failed")
+ monkeypatch.setattr(hw, "CHAT_ONLY_DETAIL", None)
+ monkeypatch.setattr(hw, "TORCH_IMPORT_ERROR", None)
+ assert hw.current_chat_only_verdict() == ("detection_failed", None)
+
+
+def test_an_unreadable_drm_walk_is_not_a_host_without_amd_cards(monkeypatch, tmp_path):
+ """/sys/class/drm going unreadable is the driver restart the TTL exists to tolerate.
+
+ Swallowing that into an empty list made the failure look like a successful "no
+ cards", so nothing marked the vendor unanswered and the carry-forward had nothing
+ to act on: the previously detected cards were dropped for a whole TTL.
+ """
+
+ def denied(_root):
+ raise PermissionError("EACCES during driver restart")
+
+ monkeypatch.setattr(hw.os, "listdir", denied)
+ assert hw._linux_drm_sysfs_records() == [], "the ranking callers keep the old shape"
+ assert hw._linux_drm_sysfs_records(distinguish_failure = True) is None
+
+
+def test_a_host_with_no_drm_subsystem_has_answered(monkeypatch):
+ """Missing is not unreadable: a container or a machine with no DRM said "no cards"."""
+
+ def absent(_root):
+ raise FileNotFoundError("/sys/class/drm")
+
+ monkeypatch.setattr(hw.os, "listdir", absent)
+ assert hw._linux_drm_sysfs_records(distinguish_failure = True) == []
+
+
+def test_a_card_whose_vendor_cannot_be_read_makes_the_walk_partial(monkeypatch):
+ """A partial answer published as a complete one drops that card for a TTL."""
+ monkeypatch.setattr(hw.os, "listdir", lambda _root: ["card0"])
+ real_open = open
+
+ def blocked(path, *a, **k):
+ if str(path).endswith("vendor"):
+ raise PermissionError("EACCES")
+ return real_open(path, *a, **k)
+
+ monkeypatch.setattr("builtins.open", blocked)
+ assert hw._linux_drm_sysfs_records(distinguish_failure = True) is None
+ assert hw._linux_drm_sysfs_records() == []
+
+
+def test_a_cardn_with_no_pci_vendor_is_not_a_lost_card(monkeypatch):
+ """A virtual or platform cardN publishes no vendor file and never was a GPU."""
+ monkeypatch.setattr(hw.os, "listdir", lambda _root: ["card0"])
+ real_open = open
+
+ def missing(path, *a, **k):
+ if str(path).endswith("vendor"):
+ raise FileNotFoundError(path)
+ return real_open(path, *a, **k)
+
+ monkeypatch.setattr("builtins.open", missing)
+ assert hw._linux_drm_sysfs_records(distinguish_failure = True) == []
+
+
+def test_the_inventory_marks_both_sysfs_vendors_unanswered(monkeypatch):
+ monkeypatch.setattr(hw.platform, "system", lambda: "Linux")
+ _smi(monkeypatch, "", raises = FileNotFoundError)
+ monkeypatch.setattr(hw, "_linux_drm_sysfs_records", lambda **_kw: None)
+
+ inventory = hw.get_physical_gpu_inventory()
+
+ assert inventory["unanswered"] == ["amd", "intel"]
+ assert inventory["unknown"] is True
+
+
+def test_a_previously_seen_amd_card_survives_an_unreadable_walk(monkeypatch):
+ """End to end: the distinction is only worth having if the carry-forward uses it."""
+ monkeypatch.setattr(hw.platform, "system", lambda: "Linux")
+ _smi(monkeypatch, "", raises = FileNotFoundError)
+ card = {
+ "vendor": "amd",
+ "index": 0,
+ "name": None,
+ "memory_total_gb": 20.0,
+ "source": "sysfs-drm",
+ }
+ monkeypatch.setattr(hw, "_linux_drm_sysfs_records", lambda **_kw: [dict(card)])
+ good = hw.get_physical_gpu_inventory()
+ assert [d["vendor"] for d in good["devices"]] == ["amd"]
+
+ monkeypatch.setattr(hw, "_physical_gpu_inventory_cache", (0.0, good))
+ monkeypatch.setattr(hw, "_linux_drm_sysfs_records", lambda **_kw: None)
+ after = hw.get_physical_gpu_inventory()
+
+ assert [d["vendor"] for d in after["devices"]] == ["amd"]
+ assert after["unknown"] is True
+
+
+# =========================== the URL outranks the family, as install.sh's resolver does
+
+
+ "chosen,url,family",
+ [
+ # install.sh returns on UNSLOTH_TORCH_INDEX_URL without ever reading the family, so
+ # when the two disagree the family is not a second opinion, it is dead. A stale
+ # ..._FAMILY=cpu beside a new ..._URL=.../cu128 suppressed a real CPU-wheel mismatch.
+ (False, "https://download.pytorch.org/whl/cu128", "cpu"),
+ (True, "https://download.pytorch.org/whl/cpu", "cu128"),
+ (True, "", "cpu"),
+ (True, "https://download.pytorch.org/whl/cpu", ""),
+ (False, "", "cu128"),
+ (False, "", ""),
+ # Whitespace-only is unset, the way install.sh trims it.
+ (True, " ", "cpu"),
+ ],
+)
+def test_a_cpu_choice_reads_the_url_before_the_family(monkeypatch, chosen, url, family):
+ monkeypatch.setenv("UNSLOTH_TORCH_INDEX_URL", url)
+ monkeypatch.setenv("UNSLOTH_TORCH_INDEX_FAMILY", family)
+ monkeypatch.setattr(hw.sys, "prefix", "/nonexistent-prefix-for-this-test")
+ assert hw._expected_cpu_flavor_was_chosen() is chosen
+
+
+ "helper,url,family,chosen",
+ [
+ (
+ "_expected_rocm_flavor_was_chosen",
+ "https://download.pytorch.org/whl/cu128",
+ "rocm6.4",
+ False,
+ ),
+ (
+ "_expected_rocm_flavor_was_chosen",
+ "https://download.pytorch.org/whl/rocm6.4",
+ "cpu",
+ True,
+ ),
+ ("_expected_xpu_flavor_was_chosen", "https://download.pytorch.org/whl/cu128", "xpu", False),
+ ("_expected_xpu_flavor_was_chosen", "https://download.pytorch.org/whl/xpu", "cpu", True),
+ ],
+)
+def test_the_rocm_and_xpu_helpers_use_the_same_precedence(monkeypatch, helper, url, family, chosen):
+ monkeypatch.setenv("UNSLOTH_TORCH_INDEX_URL", url)
+ monkeypatch.setenv("UNSLOTH_TORCH_INDEX_FAMILY", family)
+ monkeypatch.setattr(hw.sys, "prefix", "/nonexistent-prefix-for-this-test")
+ assert getattr(hw, helper)() is chosen
+
+
+def test_the_backend_precedence_matches_install_sh():
+ """One rule, two languages: install.sh returns on the URL and never reads the family."""
+ source = (pathlib.Path(hw.__file__).resolve().parents[4] / "install.sh").read_text(
+ encoding = "utf-8"
+ )
+ block = source[source.index('_url="${UNSLOTH_TORCH_INDEX_URL:-}"') :]
+ block = block[: block.index('_family="${UNSLOTH_TORCH_INDEX_FAMILY:-}"')]
+ assert (
+ 'echo "$_url"; return' in block
+ ), "install.sh no longer short-circuits on the URL; the backend mirrors that rule"
+
+
+# ================== a deliberate CPU install whose torch will not import is not broken
+
+
+def _broken_torch_on_a_gpu_host(monkeypatch):
+ """detect_hardware()'s disk-only branch: torch is installed and will not import."""
+ monkeypatch.setattr(hw, "TORCH_IMPORT_ERROR", OSError("[WinError 126]"))
+ monkeypatch.setattr(hw, "_installed_torch_label_on_disk", lambda: "2.11.0+cpu")
+ # The real venv's torch DOES carry CUDA markers, and the classifier reads those too.
+ monkeypatch.setattr(
+ hw, "_installed_torch_markers_on_disk", lambda: {"hip": "", "cuda": "", "xpu": ""}
+ )
+ monkeypatch.setattr(
+ hw,
+ "get_physical_gpu_inventory",
+ lambda **_kw: {
+ "devices": [{"vendor": "nvidia", "name": "NVIDIA RTX A4000"}],
+ "unknown": False,
+ },
+ )
+ for var in (
+ "UNSLOTH_TORCH_INDEX_URL",
+ "UNSLOTH_TORCH_INDEX_FAMILY",
+ "CUDA_VISIBLE_DEVICES",
+ "HIP_VISIBLE_DEVICES",
+ "ROCR_VISIBLE_DEVICES",
+ "ZE_AFFINITY_MASK",
+ ):
+ monkeypatch.delenv(var, raising = False)
+ monkeypatch.setattr(hw.sys, "prefix", "/nonexistent-prefix-for-this-test")
+
+
+def test_a_broken_torch_on_an_unpinned_host_still_reports_the_mismatch(
+ monkeypatch, _broken_torch_on_a_gpu_host
+):
+ assert hw._classification_from_disk_label() == "torch_cpu_build"
+ assert hw._expected_cpu_flavor_was_chosen() is False
+
+
+def test_a_deliberate_cpu_install_is_not_offered_a_gpu_repair(
+ monkeypatch, _broken_torch_on_a_gpu_host
+):
+ """The repair the UI would offer reinstalls the very wheel that was asked for.
+
+ classify_torch_build() suppresses this before its own disk fallback; the branch in
+ detect_hardware() reached _classification_from_disk_label() directly and did not.
+ """
+ monkeypatch.setenv("UNSLOTH_TORCH_INDEX_URL", "https://download.pytorch.org/whl/cpu")
+ assert hw._expected_cpu_flavor_was_chosen() is True
+
+ source = inspect.getsource(hw._detect_hardware_locked).replace(" ", "")
+ assert (
+ "_expected_cpu_flavor_was_chosen()" in source
+ ), "the disk-only branch has to apply the same suppression classify_torch_build does"
+ assert (
+ "_masks_hide_every_accelerator" in source
+ ), "and the mask suppression beside it: an emptied mask is a deliberate CPU pin too"
+
+
+# ============================ the recovery the message names has to exist where it is read
+
+
[email protected]("reason", ["torch_cpu_build", "torch_cuda_unavailable"])
+def test_the_repair_advice_names_a_route_every_deployment_has(monkeypatch, reason):
+ """Settings carries the repair row only inside the desktop app, and only for a backend
+ it manages: DesktopRepairControl returns null without a Tauri repair context and null
+ for an externally started server. A browser-hosted Studio, or a desktop attached to a
+ server started from a terminal, was told to use a control that is not on its page."""
+ monkeypatch.setattr(hw, "CHAT_ONLY_REASON", reason)
+ monkeypatch.setattr(hw, "CHAT_ONLY_DETAIL", "2.11.0+cpu")
+ message = hw._gpu_present_but_unusable_message("export", (reason, "2.11.0+cpu"))
+
+ assert message is not None
+ assert (
+ "installer" in message.lower()
+ ), "the only route a browser-hosted or externally-attached Studio has is the installer"
+ assert (
+ "desktop app" in message
+ ), "and the Settings route has to say where it lives, or it reads as universal"
+
+
+def test_the_control_that_advice_points_at_still_has_both_gates():
+ """If either gate is dropped the message could go back to naming Settings alone."""
+ source = (
+ pathlib.Path(hw.__file__).resolve().parents[3]
+ / "frontend"
+ / "src"
+ / "features"
+ / "settings"
+ / "components"
+ / "desktop-repair-control.tsx"
+ ).read_text(encoding = "utf-8")
+ assert "if (!repair || repair.isExternalServer) return null;" in source
+
+
+ "pinned,chosen",
+ [
+ (True, True),
+ (False, False),
+ # bool("false") is True, so a migrated or hand-edited manifest carrying the string
+ # would read as a deliberate pin and suppress the repair on a host that never chose
+ # one. Unknown provenance gets the same answer an absent key gets.
+ ("false", False),
+ ("true", False),
+ (1, False),
+ (None, False),
+ ],
+)
+def test_only_a_real_boolean_records_a_deliberate_cpu_choice(monkeypatch, tmp_path, pinned, chosen):
+ manifest = {"schema": 1, "expected_torch_tag": "cpu"}
+ if pinned is not None:
+ manifest["expected_torch_tag_pinned"] = pinned
+ (tmp_path / "unsloth_install_manifest.json").write_text(json.dumps(manifest), encoding = "utf-8")
+ monkeypatch.setattr(hw.sys, "prefix", str(tmp_path))
+ for var in ("UNSLOTH_TORCH_INDEX_URL", "UNSLOTH_TORCH_INDEX_FAMILY"):
+ monkeypatch.delenv(var, raising = False)
+
+ assert hw._recorded_install_flavor() == ("cpu", chosen)
+ assert hw._expected_cpu_flavor_was_chosen() is chosen
diff --git a/studio/backend/utils/hardware/hardware.py b/studio/backend/utils/hardware/hardware.py
index a092b43f995..2ff938f3b40 100644
--- a/studio/backend/utils/hardware/hardware.py
+++ b/studio/backend/utils/hardware/hardware.py
@@ -16,15 +16,19 @@ Usage:
...
"""
+import ast
import copy
import gc
import glob
+import importlib.util
+import json
import os
import platform
import re
import subprocess
import sys
import threading
+import time
import types
from contextlib import contextmanager
from importlib.metadata import PackageNotFoundError, version as pkg_version
@@ -76,7 +80,10 @@ CHAT_ONLY: bool = True # No CUDA GPU -> GGUF chat only (Mac, CPU-only, etc.)
# Why CHAT_ONLY is True (Train/Export disabled). None when training is enabled.
# "mlx_unavailable": Apple Silicon but the MLX stack is missing, too old, or broken
# (the usual cause of "Train/Export greyed out" on Macs after a reinstall dropped MLX);
-# "intel_mac": Intel Mac (no PyTorch/MLX); "no_gpu": CPU-only non-Mac host.
+# "intel_mac": Intel Mac (no PyTorch/MLX); "no_gpu": CPU-only non-Mac host;
+# "torch_cpu_build" / "torch_cuda_unavailable": the host HAS GPUs, this PyTorch cannot
+# use them -- see classify_torch_build(). Those two must not read as "no_gpu": the fix
+# is repairing the install, not buying a GPU.
CHAT_ONLY_REASON: Optional[str] = None
# What exactly blocked the reason above, when there is something specific to say. Only
# "mlx_unavailable" sets it today: the gate is all-or-nothing across mlx, mlx-lm and
@@ -84,6 +91,11 @@ CHAT_ONLY_REASON: Optional[str] = None
# had just run it. Naming the package that is missing, too old, or refusing to import
# is the difference between a dead end and a fix. Never shown on its own.
CHAT_ONLY_DETAIL: Optional[str] = None
+# The vendors whose cards established the two mismatch reasons above, empty otherwise.
+# current_chat_only_verdict() freezes a mismatch while the inventory cannot answer; without
+# this it freezes on uncertainty about an unrelated vendor, so a detached AMD eGPU stays
+# "unusable" for as long as nvidia-smi is broken on a host that never had an NVIDIA card.
+CHAT_ONLY_MISMATCH_VENDORS: frozenset = frozenset()
IS_ROCM: bool = False # True when running on AMD ROCm (HIP) -- routes GPU monitoring to amd.py
# Detection has concurrent callers (the warm thread plus any early get_device()).
@@ -133,10 +145,12 @@ def owning_detection_epoch(epoch: Optional[int]):
def _discard_detection_locked() -> None:
"""Drop a verdict produced for an epoch that has been retired."""
global DEVICE, CHAT_ONLY, CHAT_ONLY_REASON, CHAT_ONLY_DETAIL, IS_ROCM
+ global CHAT_ONLY_MISMATCH_VENDORS
DEVICE = None
CHAT_ONLY = True
CHAT_ONLY_REASON = None
CHAT_ONLY_DETAIL = None
+ CHAT_ONLY_MISMATCH_VENDORS = frozenset()
IS_ROCM = False
DETECTION_COMPLETE.clear()
@@ -331,6 +345,1196 @@ def _mlx_stack_detail() -> Optional[str]:
return "; ".join(blockers[:3])
+# Every other GPU probe here starts from get_device(), so all go quiet the moment
+# torch.cuda.is_available() is False (issue #8473). This one asks what the OS sees.
+# Display-only: nothing here may be merged into the runtime device lists, which feed the
+# model-fit estimate and the training device pin. TTL cache, since an eGPU changes it.
+_PHYSICAL_GPU_INVENTORY_TTL_SECONDS = 60.0
+_physical_gpu_inventory_lock = threading.Lock()
+_physical_gpu_inventory_refresh_lock = threading.Lock()
+_physical_gpu_inventory_refreshing = False
+# One re-detection request per recovery, or a poll every few seconds starves detection.
+_REDETECTION_REQUESTED = False
+_physical_gpu_inventory_cache: Optional[tuple[float, Dict[str, Any]]] = None
+# Same treatment for the torch side: classify_torch_build() calls availability probes that
+# block on a wedged driver, and /api/health and /api/liveness reach it through the verdict.
+_TORCH_BUILD_SNAPSHOT_TTL_SECONDS = 60.0
+_torch_build_snapshot_lock = threading.Lock()
+_torch_build_snapshot_refresh_lock = threading.Lock()
+_torch_build_snapshot_refreshing = False
+_torch_build_snapshot_cache: Optional[tuple[float, Dict[str, Any]]] = None
+# "No measurement yet", distinct from a measured {"reason": None} meaning torch is fine.
+_UNKNOWN_TORCH_BUILD_SNAPSHOT: Dict[str, Any] = {
+ "reason": None,
+ "usable": False,
+ "unknown": True,
+}
+
+
+def _probe_physical_gpu_inventory() -> Dict[str, Any]:
+ """One uncached pass over the vendor probes. Never raises.
+
+ ``index`` is the probe's own row number and is vendor-local, so it identifies a
+ device only together with ``vendor``. It is not a pin: see the note above.
+ """
+ devices: list[Dict[str, Any]] = []
+ sources: list[str] = []
+ # "No devices" and "no probe could answer" are different facts: collapsing them let a
+ # transient nvidia-smi timeout read as "the GPU disappeared". PER VENDOR, so an Intel
+ # sysfs row cannot answer for an NVIDIA probe that timed out.
+ unanswered: set = set()
+
+ try:
+ from . import nvidia
+ result = nvidia.get_physical_gpu_inventory()
+ except Exception as e:
+ logger.debug("NVIDIA physical inventory probe failed: %s", e)
+ unanswered.add("nvidia")
+ else:
+ # An absent nvidia-smi is an answer: the normal state of every AMD, Intel and CPU host.
+ if result.get("error") and not result.get("absent"):
+ unanswered.add("nvidia")
+ nvidia_devices = result.get("devices") or []
+ if nvidia_devices:
+ devices.extend(nvidia_devices)
+ sources.append(result.get("source") or "nvidia-smi")
+
+ # Windows AMD has no vendor CLI guaranteed present (amd-smi elevates a child without a
+ # HIP SDK), but the DirectX registry records every adapter with its dedicated memory.
+ if platform.system() == "Windows":
+ # The registry outlives the hardware, so a record only RE-LABELS an adapter the live WMI
+ # scan also returned, never adds one (setup.ps1 has the same rule).
+ _live = _windows_live_adapter_names()
+ for _vendor, _vendor_id in (
+ ("amd", _AMD_PCI_VENDOR_ID),
+ # Same registry, one more vendor id: an Arc host whose XPU wheel became a CPU one.
+ ("intel", _INTEL_PCI_VENDOR_ID),
+ ):
+ try:
+ records = _windows_amd_adapter_records_by_luid(_vendor_id, distinguish_failure = True)
+ except Exception as e:
+ logger.debug("Windows %s adapter inventory probe failed: %s", _vendor, e)
+ records = None
+ if records is None:
+ # A registry key that could not be read is not "this vendor has no adapters":
+ # the aggregate would publish unknown=False with nothing in it, and the settled
+ # mismatch would collapse to no_gpu for a whole TTL.
+ unanswered.add(_vendor)
+ continue
+ if not records:
+ continue
+ if _live is None:
+ # The live scan could not answer, so contribute nothing and say so.
+ unanswered.add(_vendor)
+ continue
+ # Consumed one-to-one, or a reusable predicate matches one surviving adapter to both.
+ _unclaimed = list(_live)
+ _corroborated = []
+ for luid in sorted(records):
+ _match = _claim_live_adapter(records[luid].get("name"), _unclaimed)
+ if _match is not None:
+ _unclaimed.pop(_match)
+ _corroborated.append(luid)
+ for ordinal, luid in enumerate(_corroborated):
+ record = records[luid]
+ dedicated = record.get("dedicated_memory_bytes")
+ devices.append(
+ {
+ "vendor": _vendor,
+ "index": ordinal,
+ "name": record.get("name"),
+ "memory_total_gb": (round(dedicated / 1024**3, 2) if dedicated else None),
+ **({"gfx": record["gfx"]} if record.get("gfx") else {}),
+ "source": "directx-registry",
+ }
+ )
+ if _corroborated and "directx-registry" not in sources:
+ sources.append("directx-registry")
+
+ # Linux AMD, the shape #8473 actually reported. sysfs rather than amd-smi: amd-smi is a
+ # separate ROCm userspace package, and the ROCm install is exactly what is in question
+ # here; the amdgpu kernel driver publishes these files with no subprocess at all.
+ if platform.system() == "Linux":
+ try:
+ sysfs_devices = _linux_drm_sysfs_records(distinguish_failure = True)
+ except Exception as e:
+ logger.debug("Linux DRM sysfs inventory probe failed: %s", e)
+ sysfs_devices = None
+ if sysfs_devices is None:
+ sysfs_devices = []
+ unanswered.update(("amd", "intel"))
+ if sysfs_devices:
+ if any(device.get("vendor") == "amd" for device in sysfs_devices):
+ # The installers only ship a ROCm wheel for _ROCM_SUPPORTED_GFX, so an older card left on
+ # CPU torch on purpose must not read as broken. Attached to every AMD record, not matched
+ # one-to-one: setup.sh says the orderings do not have to agree.
+ _gfx = _linux_amd_gfx_candidates()
+ if _gfx:
+ for device in sysfs_devices:
+ if device.get("vendor") == "amd":
+ device["gfx_candidates"] = _gfx
+ devices.extend(sysfs_devices)
+ sources.append("sysfs-drm")
+
+ _unanswered = sorted(unanswered - {device.get("vendor") for device in devices})
+ return {
+ "available": bool(devices),
+ "devices": devices,
+ "sources": sources,
+ "unknown": bool(_unanswered),
+ # Which vendors, not just that one exists: the cache carries the previous answer
+ # forward per vendor, and only a vendor named here may be carried.
+ "unanswered": _unanswered,
+ }
+
+
+def _linux_amd_gfx_candidates() -> list[str]:
+ """gfx targets the ROCm userspace reports for this host, sorted, or [].
+
+ rocminfo is what studio/setup.sh itself reads to pick an arch, and amd-smi is its
+ fallback there too. Neither is required to exist: a host with no ROCm userspace at
+ all reports nothing here, and the caller then keeps the card rather than guessing
+ an arch for it. Bounded and never raises; this runs inside the inventory probe,
+ which is cached and refreshed off the request path.
+ """
+ for command, pattern in (
+ (["rocminfo"], r"\bgfx[0-9a-f]+\b"),
+ (["amd-smi", "static", "--asic"], r"\bgfx[0-9a-f]+\b"),
+ ):
+ try:
+ result = subprocess.run(
+ command,
+ capture_output = True,
+ text = True,
+ encoding = "utf-8",
+ errors = "replace",
+ timeout = 10,
+ check = False,
+ )
+ except FileNotFoundError:
+ continue
+ except (OSError, subprocess.TimeoutExpired) as e:
+ logger.debug("%s could not report a gfx target: %s", command[0], e)
+ continue
+ found = {match.group(0).lower() for match in re.finditer(pattern, result.stdout or "")}
+ if found:
+ return sorted(found)
+ return []
+
+
+def _hidden_console_kwargs() -> Dict[str, Any]:
+ """subprocess kwargs that keep a console window from flashing on Windows.
+
+ Imported here rather than at module scope on purpose: this module is loaded very
+ early, and tests/python/test_e2e_no_torch_sandbox.py executes it against a minimal
+ stub tree with no utils.subprocess_compat in it, so a top-level import would make
+ the module unloadable on a host without the rest of the package. Empty kwargs are
+ the correct answer everywhere but Windows anyway.
+ """
+ try:
+ from utils.subprocess_compat import windows_hidden_subprocess_kwargs
+ except Exception:
+ return {}
+ return windows_hidden_subprocess_kwargs()
+
+
+def _windows_live_adapter_names() -> Optional[list[str]]:
+ """Display adapter names Windows reports as PRESENT, or None when it could not say.
+
+ Win32_VideoController is the same live source setup.ps1 scans. None is the important
+ third answer: "the scan failed" must not read as "no adapters", or a corroboration
+ step would silently drop every real card on a host whose WMI is unavailable.
+ """
+ if platform.system() != "Windows":
+ return None
+ try:
+ ps = (
+ # -ErrorAction Stop, NOT SilentlyContinue: the suppressed form exits 0 with empty
+ # stdout, indistinguishable from "no adapters" on a host with damaged WMI.
+ "$ErrorActionPreference='Stop';"
+ "(Get-CimInstance Win32_VideoController"
+ ' | Select-Object -ExpandProperty Name) -join "`n"'
+ )
+ r = subprocess.run(
+ ["powershell", "-NoProfile", "-NonInteractive", "-Command", ps],
+ capture_output = True,
+ text = True,
+ encoding = "utf-8",
+ errors = "replace",
+ timeout = 10,
+ **_hidden_console_kwargs(),
+ )
+ except (OSError, subprocess.SubprocessError) as e:
+ logger.debug("Live Windows adapter scan failed: %s", e)
+ return None
+ if r.returncode != 0:
+ return None
+ return [line.strip() for line in (r.stdout or "").splitlines() if line.strip()]
+
+
+def _claim_live_adapter(name: Optional[str], live_names: list[str]) -> Optional[int]:
+ """Index of the first unclaimed live adapter this record matches, or None.
+
+ Returned rather than a bool so the caller can consume the entry: see the
+ same-named-duplicates note at the call site.
+ """
+ candidate = (name or "").strip().lower()
+ if not candidate:
+ return None
+ # Exact first: a stale "RX 7900 XT" record would otherwise claim a live "RX 7900 XTX". The
+ # prefix rule still has to exist, because the registry carries the driver description and
+ # WMI the display name.
+ for index, live in enumerate(live_names):
+ if live.strip().lower() == candidate:
+ return index
+ for index, live in enumerate(live_names):
+ if _adapter_name_is_live(name, [live]):
+ return index
+ return None
+
+
+def _adapter_name_is_live(name: Optional[str], live_names: list[str]) -> bool:
+ """Whether a registry adapter name corresponds to one the live scan returned.
+
+ Substring in either direction, because the two sources spell the same card
+ differently: the registry carries the driver's own description while WMI carries the
+ display name, and one is often a prefix of the other. setup.ps1 joins them the same
+ way, with Contains.
+ """
+ candidate = (name or "").strip().lower()
+ if not candidate:
+ return False
+ return any(
+ candidate in live.lower() or live.lower() in candidate
+ for live in live_names
+ if live.strip()
+ )
+
+
+def _linux_drm_sysfs_records(*, distinguish_failure: bool = False) -> "list[Dict[str, Any]] | None":
+ """Every AMD or Intel card the DRM drivers have bound, from /sys/class/drm.
+
+ ``vendor`` is 0x1002 for AMD/ATI and 0x8086 for Intel, and amdgpu's
+ ``mem_info_vram_total`` is a byte count, both documented kernel interfaces. Intel
+ publishes no equivalent total on the discrete path, so an Arc card is reported with
+ an unknown capacity rather than left out: knowing the GPU exists is the point.
+ Only ``cardN`` is walked: the connector entries (``card0-DP-1``) and render nodes
+ point at the same device and would double-count.
+
+ No name is reported. The kernel publishes no marketing string here, and inventing
+ one, or borrowing an amd-smi row whose ordering is not guaranteed to match this
+ walk, would attach the wrong name to a card. A card with no name still tells the
+ user their GPU exists, which is the whole point.
+
+ Never raises: an unreadable or malformed file skips that card.
+
+ ``distinguish_failure`` returns None when the walk could not be trusted, so the
+ inventory marks AMD and Intel unanswered rather than publishing "no cards" for a TTL.
+ A MISSING /sys/class/drm is an answer, not a failure. The limit: a driver that unbinds
+ its cards leaves the directory readable and empty, which reads as a host with none.
+ """
+ root = "/sys/class/drm"
+ unreadable = False
+ try:
+ entries = sorted(os.listdir(root))
+ except FileNotFoundError:
+ return []
+ except OSError:
+ return None if distinguish_failure else []
+ records: list[Dict[str, Any]] = []
+ for entry in entries:
+ if not re.fullmatch(r"card\d+", entry):
+ continue
+ device = os.path.join(root, entry, "device")
+ try:
+ with open(os.path.join(device, "vendor"), encoding = "utf-8") as fh:
+ vendor = fh.read().strip().lower()
+ except FileNotFoundError:
+ # A cardN with no PCI vendor is a virtual or platform device, not a card
+ # this walk lost.
+ continue
+ except OSError:
+ unreadable = True
+ continue
+ vendors = {"0x1002": "amd", "0x8086": "intel"}
+ if vendor not in vendors:
+ continue
+ total_gb = None
+ try:
+ with open(os.path.join(device, "mem_info_vram_total"), encoding = "utf-8") as fh:
+ total_bytes = int(fh.read().strip())
+ total_gb = round(total_bytes / 1024**3, 2) if total_bytes > 0 else None
+ except (OSError, ValueError):
+ pass
+ records.append(
+ {
+ "vendor": vendors[vendor],
+ "index": len(records),
+ "name": None,
+ "memory_total_gb": total_gb,
+ "source": "sysfs-drm",
+ }
+ )
+ if unreadable and distinguish_failure:
+ # One card that could not be read makes the whole walk a partial answer, and a
+ # partial answer published as a complete one drops a card for a TTL.
+ return None
+ return records
+
+
+_UNKNOWN_PHYSICAL_GPU_INVENTORY: Dict[str, Any] = {
+ "available": False,
+ "devices": [],
+ "sources": [],
+ "unknown": True,
+ "unanswered": [],
+}
+
+
+def _carry_unanswered_vendors_forward(
+ inventory: Dict[str, Any], previous: Optional[Dict[str, Any]]
+) -> Dict[str, Any]:
+ """Re-add the devices only a vendor this pass could not ask had reported.
+
+ A refresh that cannot reach nvidia-smi is not news that the cards left, but caching it
+ as the new inventory says exactly that for a TTL: the Resources tab then reports no GPU
+ on the same response where the sidebar says PyTorch cannot use one.
+
+ Only for vendors in ``unanswered``: a vendor that answered "none" gave an answer, and a
+ card that really was removed has to be allowed to disappear.
+ """
+ stale_vendors = set(inventory.get("unanswered") or ())
+ if not stale_vendors or not previous:
+ return inventory
+ carried = [
+ device
+ for device in (previous.get("devices") or [])
+ if device.get("vendor") in stale_vendors
+ ]
+ if not carried:
+ return inventory
+ merged = dict(inventory)
+ merged["devices"] = list(inventory.get("devices") or []) + carried
+ merged["available"] = True
+ merged["sources"] = list(inventory.get("sources") or []) + [
+ source
+ for source in (previous.get("sources") or [])
+ if source not in (inventory.get("sources") or [])
+ ]
+ # Still unknown: these rows describe the host as it was, not as this pass measured it.
+ return merged
+
+
+def _run_physical_gpu_inventory_probe() -> Dict[str, Any]:
+ """One probe pass, stored in the cache. Never raises."""
+ global _physical_gpu_inventory_cache
+ previous = _physical_gpu_inventory_cache
+ try:
+ inventory = _probe_physical_gpu_inventory()
+ except Exception as e:
+ logger.debug("Physical GPU inventory probe failed: %s", e)
+ inventory = dict(_UNKNOWN_PHYSICAL_GPU_INVENTORY)
+ # A pass that raised asked nobody, so every vendor is unanswered.
+ inventory["unanswered"] = ["amd", "intel", "nvidia"]
+ inventory = _carry_unanswered_vendors_forward(
+ inventory, previous[1] if previous is not None else None
+ )
+ _physical_gpu_inventory_cache = (time.monotonic(), inventory)
+ return inventory
+
+
+def get_physical_gpu_inventory(*, block: bool = True) -> Dict[str, Any]:
+ """GPUs the OS enumerates, whether or not PyTorch can use them.
+
+ Returns ``{available, devices, sources, unknown}``. Runs regardless of ``DEVICE``
+ and never raises: a probe that cannot answer contributes nothing rather than
+ failing the caller, so an endpoint reaching here still returns a body.
+
+ ``block=False`` for anything on a request path. The NVIDIA half shells out with a
+ 10 second timeout and the lock makes concurrent callers queue behind it, so a hung
+ or restarting driver -- the exact host this inventory exists for -- would stall the
+ event loop for ten seconds every time the TTL expired. /api/health and
+ /api/liveness both reach here through the chat-only verdict, and the desktop gives
+ a local request two seconds. A non-blocking caller takes the last known answer,
+ kicks the refresh onto a daemon thread, and gets the new value on a later call;
+ with nothing cached at all it gets the explicit unknown, which every consumer
+ already reads as "keep what you had".
+ """
+ now = time.monotonic()
+ cached_entry = _physical_gpu_inventory_cache
+ if cached_entry is not None and now - cached_entry[0] < _PHYSICAL_GPU_INVENTORY_TTL_SECONDS:
+ return cached_entry[1]
+ if not block:
+ _schedule_physical_gpu_inventory_refresh()
+ return (
+ cached_entry[1] if cached_entry is not None else dict(_UNKNOWN_PHYSICAL_GPU_INVENTORY)
+ )
+ with _physical_gpu_inventory_lock:
+ cached_entry = _physical_gpu_inventory_cache
+ if (
+ cached_entry is not None
+ and time.monotonic() - cached_entry[0] < _PHYSICAL_GPU_INVENTORY_TTL_SECONDS
+ ):
+ return cached_entry[1]
+ return _run_physical_gpu_inventory_probe()
+
+
+def _schedule_single_flight_refresh(
+ flag: str, refresh_lock, work_lock, run, thread_name: str, what: str
+) -> None:
+ """Run ``run`` off the caller's thread, one pass at a time.
+
+ Single-flight through ``refresh_lock``, tried without waiting: a refresh already
+ running is what a second caller wants, so this returns rather than queueing a
+ duplicate behind it. ``work_lock`` is the blocking path's lock, so the background
+ pass cannot publish underneath one.
+
+ ``flag`` names the module global holding the in-flight bit, kept a real global so a
+ test can reset it. Shared by the inventory and the torch snapshot, which carried
+ identical copies of this and would have drifted apart under a later fix.
+ """
+ with refresh_lock:
+ if globals()[flag]:
+ return
+ globals()[flag] = True
+
+ def _refresh() -> None:
+ try:
+ with work_lock:
+ run()
+ except Exception as e:
+ logger.debug("Background %s refresh failed: %s", what, e)
+ finally:
+ with refresh_lock:
+ globals()[flag] = False
+
+ try:
+ threading.Thread(target = _refresh, name = thread_name, daemon = True).start()
+ except Exception as e:
+ logger.debug("Could not start the %s refresh thread: %s", what, e)
+ with refresh_lock:
+ globals()[flag] = False
+
+
+def _schedule_physical_gpu_inventory_refresh() -> None:
+ """Refresh the inventory off the caller's thread, one pass at a time."""
+ _schedule_single_flight_refresh(
+ "_physical_gpu_inventory_refreshing",
+ _physical_gpu_inventory_refresh_lock,
+ _physical_gpu_inventory_lock,
+ _run_physical_gpu_inventory_probe,
+ "gpu-inventory-refresh",
+ "inventory",
+ )
+
+
+def _reported_torch_label(published: Optional[str] = None) -> Optional[str]:
+ """The version to show for this torch, without ever retrying a failed import.
+
+ ``_torch_version_label()`` imports torch, and on a host whose native runtime will
+ not load that import is the thing that fails: it can take seconds, and _has_torch()
+ purges the partial module afterwards so the next attempt genuinely re-runs the
+ native load. /api/health and /api/liveness reach the label through the chat-only
+ verdict, so on exactly the host this feature exists for it would block the event
+ loop on every call. Whatever detection already published wins, then the label the
+ wheel carries on disk.
+ """
+ if TORCH_IMPORT_ERROR is not None:
+ return published or _installed_torch_label_on_disk() or None
+ return _torch_version_label() or _installed_torch_label_on_disk() or None
+
+
+def _torch_version_label() -> Optional[str]:
+ """``torch.__version__`` when it can be read, else None. Never raises."""
+ try:
+ import torch
+ return str(torch.__version__)
+ except Exception:
+ return None
+
+
+# A mask hides that vendor's devices and nothing else.
+_VISIBILITY_MASK_VENDORS: Dict[str, frozenset] = {
+ # HIP honours CUDA_VISIBLE_DEVICES too, so an AMD-only box launched with it emptied is a
+ # deliberately masked host, not a broken one.
+ "CUDA_VISIBLE_DEVICES": frozenset({"nvidia", "amd"}),
+ "HIP_VISIBLE_DEVICES": frozenset({"amd"}),
+ "ROCR_VISIBLE_DEVICES": frozenset({"amd"}),
+ "ZE_AFFINITY_MASK": frozenset({"intel"}),
+}
+
+
+def _mask_is_emptied(var: str) -> bool:
+ """True when ``var`` is set to a value that hides every device it addresses.
+
+ Set-but-empty and "-1" are the two spellings; a mask NAMING devices is not this,
+ since that host expects those devices to work.
+ """
+ value = os.environ.get(var)
+ return value is not None and value.strip() in ("", "-1")
+
+
+def _masks_hide_every_accelerator(*, block_inventory: bool = False) -> bool:
+ """True when the masks account for every accelerator this host has.
+
+ Then torch reporting none is the configuration working, not a broken install, and
+ the whole classification is suppressed. A mask that covers only SOME of the cards
+ is not this: an emptied ZE_AFFINITY_MASK beside an unmasked NVIDIA GPU hides the
+ Arc and nothing else, and cancelling the classification there would let a CPU-only
+ wheel go unreported for a card the user never masked. Those cards are dropped from
+ the mismatch inventory instead.
+
+ An inventory that found nothing, or could not answer, stays conservative: a mask
+ may well be hiding the only accelerator, and that is the case this existed for.
+ """
+ masked = _vendors_masked_off(block_inventory = block_inventory)
+ if not masked:
+ return False
+ try:
+ inventory = get_physical_gpu_inventory(block = block_inventory)
+ except Exception:
+ inventory = dict(_UNKNOWN_PHYSICAL_GPU_INVENTORY)
+ devices = inventory.get("devices") or []
+ if devices:
+ return all(device.get("vendor") in masked for device in devices)
+ # An inventory that could not ANSWER stays conservative; one that answered nothing has no
+ # card to hide. A cold non-blocking read is unknown, and an irrelevant empty mask would
+ # then cache "torch is fine" for a TTL, so the caching path passes block_inventory = True.
+ return True
+
+
+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)
+ masked: set = set()
+ for var in relevant:
+ if _mask_is_emptied(var):
+ masked |= _VISIBILITY_MASK_VENDORS.get(var, frozenset())
+ # HIP reads these three in order and stops at the first one SET (the precedence
+ # amd._first_visible_amd_gpu_id applies), so only that variable decides. Reading a
+ # lower-priority one would offer a repair for a host masked exactly as asked:
+ # HIP_VISIBLE_DEVICES=-1 beside ROCR_VISIBLE_DEVICES=0 hides every AMD card.
+ amd_mask = next(
+ (
+ var
+ for var in ("HIP_VISIBLE_DEVICES", "ROCR_VISIBLE_DEVICES", "CUDA_VISIBLE_DEVICES")
+ if var in relevant and os.environ.get(var) is not None
+ ),
+ None,
+ )
+ if amd_mask is not None and _mask_is_emptied(amd_mask):
+ masked.add("amd")
+ else:
+ masked.discard("amd")
+ return masked
+
+
+def _relevant_visibility_masks(*, block_inventory: bool = False) -> tuple[str, ...]:
+ """The visibility variables that can actually hide a GPU on THIS host.
+
+ A mask that cannot take effect must not silence the mismatch. ROCR_VISIBLE_DEVICES
+ is the concrete case: Windows HIP has no ROCr layer, and this module's own
+ visibility resolver already ignores the variable there, so a stray empty one would
+ otherwise restore the "no GPU" verdict on a Windows NVIDIA host with a CPU wheel.
+
+ The HIP-layer variables are likewise only consulted when an AMD card is actually
+ present, so a leftover HIP_VISIBLE_DEVICES on an NVIDIA-only box cannot mask
+ anything it does not address. An inventory that found nothing, or could not answer,
+ keeps every variable: unknown must stay conservative rather than start ignoring
+ masks that may well be real.
+ """
+ masks = ["CUDA_VISIBLE_DEVICES"]
+ hip_masks = ["HIP_VISIBLE_DEVICES"]
+ if sys.platform != "win32":
+ hip_masks.append("ROCR_VISIBLE_DEVICES")
+ try:
+ # block=False: this is reached from the chat-only verdict, which /api/health and
+ # /api/liveness read. A vendor set does not change between refreshes.
+ devices = get_physical_gpu_inventory(block = block_inventory).get("devices") or []
+ except Exception:
+ devices = []
+ if not devices or any(d.get("vendor") == "amd" for d in devices):
+ masks.extend(hip_masks)
+ # ZE_AFFINITY_MASK is the XPU equivalent; the rest of this module already reads an
+ # emptied one as hiding every Intel device.
+ if not devices or any(d.get("vendor") == "intel" for d in devices):
+ masks.append("ZE_AFFINITY_MASK")
+ return tuple(masks)
+
+
+def _torch_index_leaf(url: str) -> str:
+ """Final path segment of a torch index URL, lowercased, query and fragment removed.
+
+ A token-authenticated pin is supported and common (.../whl/cpu?token=...), and a raw
+ final-segment split sees "cpu?token=..." there, so a deliberate CPU install on a GPU
+ host would be reported as broken and offered a repair. Trailing slashes come off the
+ PATH only, or a token ending in "/" is corrupted. Mirrors the installer's
+ _torch_index_leaf / _trim_index_path_slashes, which the same variable is read by.
+ """
+ value = str(url).strip().lower()
+ if not value:
+ return ""
+ path = re.fullmatch(r"([^?#]*)([?#].*)?", value)
+ path = path.group(1) if path else value
+ return path.rstrip("/").rsplit("/", 1)[-1]
+
+
+def _stated_torch_index_source() -> str:
+ """The torch index this install was told to use, or "".
+
+ URL first, and the family ONLY when no URL is set: install.sh's get_torch_index_url()
+ returns on UNSLOTH_TORCH_INDEX_URL without ever reading UNSLOTH_TORCH_INDEX_FAMILY, so
+ a family that disagrees is dead, not a second opinion. Taking either let a stale
+ ..._FAMILY=cpu beside a new ..._URL=.../cu128 suppress the mismatch on a CUDA host.
+ """
+ url = (os.environ.get("UNSLOTH_TORCH_INDEX_URL") or "").strip()
+ if url:
+ return url
+ return (os.environ.get("UNSLOTH_TORCH_INDEX_FAMILY") or "").strip()
+
+
+def _recorded_install_flavor() -> "tuple[str, bool]":
+ """``(expected_torch_tag, expected_torch_tag_pinned)`` from the venv's manifest.
+
+ ``("", False)`` when there is no manifest, it cannot be read, or the tag is not a
+ string: nothing recorded is not a choice, which is the rule
+ install_manifest.recorded_torch_flavor documents. Read straight off disk rather than
+ by importing that module, which lives outside the backend package. Never raises.
+ """
+ try:
+ path = os.path.join(sys.prefix, "unsloth_install_manifest.json")
+ with open(path, encoding = "utf-8") as fh:
+ manifest = json.load(fh)
+ recorded = manifest.get("expected_torch_tag")
+ pinned = manifest.get("expected_torch_tag_pinned")
+ except (OSError, ValueError, AttributeError):
+ return "", False
+ if not isinstance(recorded, str):
+ return "", False
+ # `is True`, not bool(): bool("false") is True, so a hand-edited manifest carrying the
+ # string would read as a deliberate pin. install_manifest's reader applies the same rule.
+ return recorded.strip().lower(), pinned is True
+
+
+def _expected_cpu_flavor_was_chosen() -> bool:
+ """Whether THIS install deliberately selected a CPU wheel.
+
+ Two sources, both meaning "the user named cpu": an explicit index pin set for the
+ running process, and the flavor the last completed install recorded in the venv's
+ manifest, which install_python_stack.py writes from the same expectation the
+ Windows flavor invariant enforces.
+
+ Only "cpu" is acted on: an unknown or absent record means nothing was recorded and
+ must not be read as a choice. Never raises.
+ """
+ if _torch_index_leaf(_stated_torch_index_source()) == "cpu":
+ return True
+ # A recorded cpu is not by itself a choice: setup.ps1 selects /cpu automatically on a
+ # GPU-less host and records it exactly as it records a pinned one.
+ recorded, pinned = _recorded_install_flavor()
+ return recorded == "cpu" and pinned
+
+
+def classify_torch_build(*, block_inventory: bool = False) -> Optional[str]:
+ """Why this PyTorch exposes no accelerator, when the build itself is the reason.
+
+ "torch_cpu_build" -- a CPU-only wheel: ``2.11.0+cpu``, or an untagged build
+ with neither ``torch.version.cuda`` nor ``.hip``. No
+ driver update fixes it; only reinstalling torch from
+ the right index does.
+ "torch_cuda_unavailable" -- an accelerator wheel (``+cu124``, ``+rocm6.4``,
+ ``+xpu``) whose runtime refuses to initialise: driver
+ too old, no permission on the device nodes, a cudart
+ that will not load. The wheel is right, the
+ environment is not.
+ None -- torch is missing, unimportable, or has a working
+ accelerator. Nothing to say.
+
+ Kept as two reasons rather than one flag because the advice differs, and telling
+ someone with a healthy cu124 wheel to reinstall torch sends them the wrong way.
+ Never raises.
+ """
+ # An emptied visibility mask has the exact shape of a broken install without anything
+ # being broken. A mask NAMING devices is not this: that host expects those to work.
+ if _masks_hide_every_accelerator(block_inventory = block_inventory):
+ return None
+ if _expected_cpu_flavor_was_chosen():
+ return None
+ if not _has_torch():
+ # Absent and unimportable differ, and the second is what this exists for. _has_torch()
+ # collapses both to False.
+ return _classification_from_disk_label()
+ try:
+ import torch
+
+ # XPU as well as CUDA, or a recovered Intel host stays torch_cuda_unavailable forever.
+ for _available in (
+ getattr(getattr(torch, "cuda", None), "is_available", None),
+ getattr(getattr(torch, "xpu", None), "is_available", None),
+ ):
+ try:
+ if callable(_available) and _available():
+ return None
+ except Exception:
+ continue
+ version = str(getattr(torch, "__version__", ""))
+ # The installers' vocabulary: "2.11.0+cpu" -> "cpu", "2.6.0+cu124" -> "cu124".
+ local = version.partition("+")[2].strip().lower()
+ cuda_tag = getattr(getattr(torch, "version", None), "cuda", None)
+ hip_tag = getattr(getattr(torch, "version", None), "hip", None)
+ # An untagged wheel that sets torch.version.xpu is a GPU build whose runtime is down.
+ xpu_tag = getattr(getattr(torch, "version", None), "xpu", None)
+ if local == "cpu" or local.startswith("cpu."):
+ # PyTorch publishes extended CPU local tags such as "2.8.0+cpu.cxx11.abi".
+ return "torch_cpu_build"
+ if not local and cuda_tag is None and hip_tag is None and xpu_tag is None:
+ # Untagged and built against no GPU runtime: the PyPI macOS/CPU wheel shape. An untagged
+ # wheel that DOES set version.cuda (conda) belongs in the second case.
+ return "torch_cpu_build"
+ return "torch_cuda_unavailable"
+ except Exception as e:
+ # torch is INSTALLED but will not import. Classify from the wheel on disk instead.
+ logger.debug("torch build classification fell back to the on-disk label: %s", e)
+ return _classification_from_disk_label()
+
+
+def _classification_from_disk_label() -> Optional[str]:
+ """Classify from the wheel's own version label, with no interpreter started.
+
+ ``None`` when nothing is installed to read: an absent torch is not a mismatch.
+ """
+ label = _installed_torch_label_on_disk()
+ markers = _installed_torch_markers_on_disk()
+ if not label and not any(markers.values()):
+ return None
+ if "+cu" in label or "+rocm" in label or "+xpu" in label:
+ return "torch_cuda_unavailable"
+ if any(markers.values()):
+ return "torch_cuda_unavailable"
+ return "torch_cpu_build"
+
+
+# setup.ps1's rule: only Arc and Data Center GPU autodetect as XPU. An ordinary Intel
+# iGPU is a display adapter, not a training device.
+_XPU_ADAPTER_NAME_RE = re.compile(r"intel.*(arc|data center gpu)", re.IGNORECASE)
+
+
+def _devices_that_can_establish_a_mismatch(devices: list[Dict[str, Any]]) -> list[Dict[str, Any]]:
+ """The subset of the inventory whose presence means PyTorch OUGHT to have a GPU.
+
+ NVIDIA and AMD qualify outright. Intel does not, by itself: setup.sh does not
+ autodetect Linux XPU at all, and setup.ps1 limits automatic XPU detection to Arc and
+ Data Center GPU by name. An Intel UHD iGPU beside a CPU wheel is the expected,
+ correct state of that machine, so counting it would report a mismatch and offer a
+ repair that reinstalls the very CPU build it just replaced.
+
+ An Intel card still counts when it is one this stack would have installed XPU torch
+ for: a name that matches setup.ps1's rule, or an XPU expectation recorded for this
+ venv, or a torch that carries an XPU runtime. The Linux sysfs walk publishes no
+ name, which is exactly why the expectation and the runtime are consulted too.
+ """
+ xpu_expected = _expected_xpu_flavor_was_chosen() or _torch_reports_an_xpu_runtime()
+ # Per vendor, not host-wide: an emptied ZE_AFFINITY_MASK beside an unmasked NVIDIA
+ # card hides the Arc and nothing else.
+ masked_off = _vendors_masked_off()
+ # A ROCm expectation, or a torch carrying a HIP runtime, settles it for every AMD
+ # card: this stack asked for that wheel, whatever the arch table says.
+ rocm_expected = _expected_rocm_flavor_was_chosen() or _torch_reports_a_hip_runtime()
+ keep: list[Dict[str, Any]] = []
+ for device in devices:
+ if device.get("vendor") in masked_off:
+ continue
+ if device.get("vendor") == "amd":
+ if rocm_expected or _amd_device_can_establish_a_mismatch(device):
+ keep.append(device)
+ continue
+ if device.get("vendor") != "intel":
+ keep.append(device)
+ continue
+ if xpu_expected or _XPU_ADAPTER_NAME_RE.search(str(device.get("name") or "")):
+ keep.append(device)
+ return keep
+
+
+# The gfx targets this stack will actually install a ROCm wheel for: install.sh's
+# _amd_arch_index_family_for_gfx, plus gfx906 from the ROCm 6.3 path. A card outside this
+# set (Polaris gfx803, RDNA 1 gfx101x) is left on CPU torch ON PURPOSE.
+_ROCM_SUPPORTED_GFX = frozenset(
+ {
+ "gfx906",
+ "gfx908",
+ "gfx90a",
+ "gfx1030",
+ "gfx1031",
+ "gfx1032",
+ "gfx1033",
+ "gfx1034",
+ "gfx1035",
+ "gfx1036",
+ "gfx1100",
+ "gfx1101",
+ "gfx1102",
+ "gfx1103",
+ "gfx1150",
+ "gfx1151",
+ "gfx1152",
+ "gfx1200",
+ "gfx1201",
+ }
+)
+
+
+def _linux_kfd_reports_an_amd_gpu() -> bool:
+ """Whether the KFD topology enumerates an AMD GPU node. Never raises.
+
+ The same probe, and the same vendor guard, install_python_stack._has_rocm_gpu()
+ uses: gpu_id 0 is a CPU node, and the NVIDIA open kernel module registers KFD nodes
+ of its own with vendor_id 4318, so AMD ownership has to be confirmed rather than
+ assumed. A node whose properties cannot be read is skipped for that reason.
+ """
+ if platform.system() != "Linux":
+ return False
+ nodes = "/sys/class/kfd/kfd/topology/nodes"
+ try:
+ entries = os.listdir(nodes)
+ except OSError:
+ return False
+ for entry in entries:
+ try:
+ with open(os.path.join(nodes, entry, "gpu_id"), encoding = "utf-8") as fh:
+ gpu_id = fh.read().strip()
+ except (OSError, UnicodeDecodeError):
+ continue
+ if not gpu_id or gpu_id == "0":
+ continue
+ try:
+ with open(os.path.join(nodes, entry, "properties"), encoding = "utf-8") as fh:
+ properties = fh.read()
+ except (OSError, UnicodeDecodeError):
+ continue
+ if re.search(r"\bvendor_id\s+4098\b", properties):
+ return True
+ return False
+
+
+def _is_pip_rocm_family_leaf(leaf: str) -> bool:
+ """True when a lowercased leaf names a pip ROCm family: EXACTLY rocm<digits>[.<digits>]
+ or gfx<digit>. install_python_stack._is_pip_rocm_family_leaf, kept in step with it.
+
+ A suffixed leaf (rocm-rel-7.2.1, gfx-mirror) is a custom pin the installer routes
+ verbatim. Reading one as ROCm waives the supported-architecture filter, so a gfx803
+ host deliberately left on CPU torch gets told its own install is broken.
+ """
+ return bool(re.fullmatch(r"rocm\d+(?:\.\d+)?", leaf)) or bool(re.match(r"gfx\d", leaf))
+
+
+def _expected_rocm_flavor_was_chosen() -> bool:
+ """Whether this install selected a ROCm wheel, by pin or by recorded flavor."""
+ if _is_pip_rocm_family_leaf(_torch_index_leaf(_stated_torch_index_source())):
+ return True
+ return _recorded_install_flavor()[0].startswith("rocm")
+
+
+def _torch_reports_a_hip_runtime() -> bool:
+ """Whether the installed torch is a ROCm build, however unusable it currently is."""
+ if TORCH_IMPORT_ERROR is not None:
+ return "+rocm" in _installed_torch_label_on_disk() or bool(
+ _installed_torch_markers_on_disk()["hip"]
+ )
+ try:
+ import torch
+ if "+rocm" in str(getattr(torch, "__version__", "")).lower():
+ return True
+ return getattr(getattr(torch, "version", None), "hip", None) is not None
+ except Exception:
+ return False
+
+
+# Marketing name -> gfx, mirroring setup.ps1's $nameArchTable and
+# install_python_stack._WIN_GPU_NAME_ARCH_TABLE. Only names those two route to a wheel
+# family: this decides whether a repair could change anything, so a card no index covers
+# must not match. Most specific first.
+_GPU_NAME_GFX_TABLE: "list[tuple[str, str]]" = [
+ (r"9070|9080|R9700", "gfx1201"),
+ (r"9060", "gfx1200"),
+ (r"8065S|8060S|8050S|8040S|Strix Halo|Ryzen AI Max|AI Max", "gfx1151"),
+ (r"890M|880M|Strix Point|HX 37[05]|AI 9 HX|AI 9 36[05]", "gfx1150"),
+ (r"860M|840M|Krackan|AI 7 35[05]|AI 5 34[05]|AI 7 PRO 35|AI 5 33", "gfx1152"),
+ (r"RX 7900|PRO W7900|PRO W7800", "gfx1100"),
+ (r"RX 7800|RX 7700(?!S)|PRO W7700|PRO V710", "gfx1101"),
+ (r"RX 7600|RX 7700S|RX 7650|PRO W7600|PRO W7500", "gfx1102"),
+ (r"780M|760M|740M|Phoenix|Hawk Point|Z1 Extreme|Z2 Extreme", "gfx1103"),
+ (r"RX 6900|RX 6800|RX 6750|RX 6700|PRO W6800|PRO W6900", "gfx1030"),
+ (r"RX 6650|RX 6600|PRO W6600|PRO W6650", "gfx1032"),
+ (r"RX 6500|RX 6400|RX 6300|PRO W6400|PRO W6500", "gfx1034"),
+]
+
+
+def _rocm_supported_gfx_from_gpu_name(name: str) -> Optional[str]:
+ """The gfx arch this marketing name maps to, when the ROCm wheels cover it."""
+ if not name:
+ return None
+ for pattern, arch in _GPU_NAME_GFX_TABLE:
+ if re.search(pattern, name, re.IGNORECASE) and arch in _ROCM_SUPPORTED_GFX:
+ return arch
+ return None
+
+
+def _amd_device_can_establish_a_mismatch(device: Dict[str, Any]) -> bool:
+ """Whether this AMD adapter is one the installers would have given a ROCm wheel.
+
+ A stack that deliberately declines to support a card cannot then call the CPU wheel
+ beside it a fault. The arch is consulted only when something could name it: the DRM
+ sysfs walk publishes no name, so a host with no ROCm userspace at all reports no
+ candidates and stays counted, which is the conservative direction and the shape the
+ original report had (an RX 7900 XT, which IS supported).
+ """
+ candidates = [
+ str(gfx).lower()
+ for gfx in (
+ device.get("gfx_candidates") or ([device.get("gfx")] if device.get("gfx") else [])
+ )
+ if gfx
+ ]
+ if not candidates:
+ # The DirectX registry publishes AdapterFamily only when the driver wrote one, so a
+ # supported Windows card (the reported RX 7900 XT) can arrive with no arch. setup.ps1
+ # answers that from the marketing name; the probe below can only speak for Linux.
+ _named = _rocm_supported_gfx_from_gpu_name(device.get("name") or "")
+ if _named:
+ return True
+ # Nothing NAMES the card, so ask what the installer asks: _has_rocm_gpu() falls back to
+ # the KFD topology, and a card `studio update` would repair is eligible here.
+ return _linux_kfd_reports_an_amd_gpu()
+ return any(gfx in _ROCM_SUPPORTED_GFX for gfx in candidates)
+
+
+def _expected_xpu_flavor_was_chosen() -> bool:
+ """Whether this install selected an XPU wheel, by pin or by recorded flavor."""
+ if _torch_index_leaf(_stated_torch_index_source()) == "xpu":
+ return True
+ return _recorded_install_flavor()[0] == "xpu"
+
+
+def _torch_reports_an_xpu_runtime() -> bool:
+ """Whether the installed torch is an XPU build, however unusable it currently is."""
+ if TORCH_IMPORT_ERROR is not None:
+ # Asking costs a full torch/__init__ against the partial module tree the import left.
+ return "+xpu" in _installed_torch_label_on_disk() or bool(
+ _installed_torch_markers_on_disk()["xpu"]
+ )
+ try:
+ import torch
+ if "+xpu" in str(getattr(torch, "__version__", "")).lower():
+ return True
+ return getattr(getattr(torch, "version", None), "xpu", None) is not None
+ except Exception:
+ return False
+
+
+def _installed_torch_label_on_disk() -> str:
+ """``torch.__version__`` read out of the installed torch/version.py, or "".
+
+ No interpreter is started, which is the point: this is reached when importing torch
+ is the thing that fails. The installers read the same file for the same reason.
+ Never raises.
+ """
+ try:
+ spec = importlib.util.find_spec("torch")
+ except Exception:
+ return ""
+ locations = list(getattr(spec, "submodule_search_locations", None) or []) if spec else []
+ for location in locations:
+ try:
+ with open(os.path.join(location, "version.py"), encoding = "utf-8") as fh:
+ for line in fh:
+ if line.startswith("__version__"):
+ return line.partition("=")[2].strip().strip("\"'").lower()
+ except OSError:
+ continue
+ return ""
+
+
+def _installed_torch_markers_on_disk() -> Dict[str, Optional[str]]:
+ """``{cuda, hip, xpu}`` as recorded in the installed torch/version.py.
+
+ The version LABEL is not the whole story: a conda or source CUDA build is untagged
+ and records its runtime here instead, and the importable path already reads exactly
+ these three attributes. Without them the failure path gave the same installation the
+ opposite diagnosis, telling the user to reinstall a GPU wheel it already has rather
+ than to fix the driver. Parsed, not executed, for the same reason as the label.
+ Never raises; a value the file does not set reads as None.
+ """
+ markers: Dict[str, Optional[str]] = {"cuda": None, "hip": None, "xpu": None}
+ try:
+ spec = importlib.util.find_spec("torch")
+ except Exception:
+ return markers
+ locations = list(getattr(spec, "submodule_search_locations", None) or []) if spec else []
+ for location in locations:
+ try:
+ with open(os.path.join(location, "version.py"), encoding = "utf-8") as fh:
+ source = fh.read()
+ except OSError:
+ continue
+ try:
+ tree = ast.parse(source)
+ except SyntaxError:
+ continue
+ for node in tree.body:
+ # torch has shipped both `cuda = '12.8'` and `cuda: Optional[str] = '12.8'`.
+ if isinstance(node, ast.Assign):
+ names = [n.id for n in node.targets if isinstance(n, ast.Name)]
+ elif isinstance(node, ast.AnnAssign) and isinstance(node.target, ast.Name):
+ names = [node.target.id]
+ else:
+ continue
+ value = getattr(node, "value", None)
+ if not isinstance(value, ast.Constant) or not isinstance(value.value, str):
+ continue
+ for name in names:
+ if name in markers:
+ markers[name] = value.value
+ return markers
+ return markers
+
+
+def _run_torch_build_snapshot() -> Dict[str, Any]:
+ """One uncached pass over torch, cached on the way out. Never raises."""
+ global _torch_build_snapshot_cache
+ snapshot = {
+ # block_inventory: never a request path, so masks compare against a MEASURED inventory.
+ "reason": classify_torch_build(block_inventory = True),
+ "usable": _torch_reports_a_usable_accelerator(),
+ "unknown": False,
+ }
+ _torch_build_snapshot_cache = (time.monotonic(), snapshot)
+ return snapshot
+
+
+def torch_build_snapshot(*, block: bool = True) -> Dict[str, Any]:
+ """``{reason, usable, unknown}`` for this venv's torch, cached with a TTL.
+
+ ``block=False`` for anything on a request path: both probes inside import torch and
+ ask the CUDA and XPU runtimes whether they are available, and a wedged or
+ restarting driver -- the very state ``torch_cuda_unavailable`` names -- can hold
+ those for as long as the driver takes. /api/health and /api/liveness reach here
+ through the chat-only verdict, against a two second desktop timeout, so a
+ non-blocking caller takes the last measurement, kicks the refresh onto a daemon
+ thread, and reads the new one later.
+
+ With nothing measured yet it gets ``unknown``, never a guess: detect_hardware()
+ takes the blocking path on exactly the hosts whose verdict can be re-derived, so
+ the cache is warm before any request can consult it.
+ """
+ now = time.monotonic()
+ cached_entry = _torch_build_snapshot_cache
+ if cached_entry is not None and now - cached_entry[0] < _TORCH_BUILD_SNAPSHOT_TTL_SECONDS:
+ return cached_entry[1]
+ if not block:
+ _schedule_torch_build_snapshot_refresh()
+ return cached_entry[1] if cached_entry is not None else dict(_UNKNOWN_TORCH_BUILD_SNAPSHOT)
+ with _torch_build_snapshot_lock:
+ cached_entry = _torch_build_snapshot_cache
+ if (
+ cached_entry is not None
+ and time.monotonic() - cached_entry[0] < _TORCH_BUILD_SNAPSHOT_TTL_SECONDS
+ ):
+ return cached_entry[1]
+ return _run_torch_build_snapshot()
+
+
+def _schedule_torch_build_snapshot_refresh() -> None:
+ """Refresh the torch snapshot off the caller's thread, one pass at a time."""
+ _schedule_single_flight_refresh(
+ "_torch_build_snapshot_refreshing",
+ _torch_build_snapshot_refresh_lock,
+ _torch_build_snapshot_lock,
+ _run_torch_build_snapshot,
+ "torch-build-refresh",
+ "torch build",
+ )
+
+
+def _seed_torch_build_snapshot(reason: Optional[str]) -> None:
+ """Record a classification that was reached without probing torch.
+
+ The broken-runtime host is classified from the wheel on disk, and that answer is
+ exactly what the request paths need; letting them re-measure would put the import
+ that already failed back on the health thread.
+ """
+ global _torch_build_snapshot_cache
+ _torch_build_snapshot_cache = (
+ time.monotonic(),
+ {"reason": reason, "usable": False, "unknown": False},
+ )
+
+
+def invalidate_torch_build_snapshot() -> None:
+ """Drop the cached measurement so the next blocking caller re-probes."""
+ global _torch_build_snapshot_cache
+ _torch_build_snapshot_cache = None
+
+
+def _mismatch_verdict_for_this_host(
+ reason: Optional[str] = None,
+) -> tuple[Optional[str], Optional[str]]:
+ """``(reason, detail)`` when this host's GPUs are real but PyTorch cannot use them.
+
+ ``(None, None)`` otherwise. Blocking, and only detection calls it: it runs off the
+ request path, and the blocking pass is what warms the caches that /api/health then
+ reads without probing anything itself.
+ """
+ if reason is None:
+ reason = torch_build_snapshot()["reason"]
+ if reason is None:
+ return None, None
+ establishing = _devices_that_can_establish_a_mismatch(
+ get_physical_gpu_inventory().get("devices") or []
+ )
+ if not establishing:
+ return None, None
+ # Which vendors said so, for the frozen verdict's own uncertainty test.
+ _remember_the_vendors_behind_the_mismatch(establishing)
+ detail = _reported_torch_label()
+ logger.warning(
+ "GPUs are present on this host but PyTorch cannot use them (%s%s); "
+ "Train/Export disabled (chat-only). Repair the installation to restore GPU "
+ "support.",
+ reason,
+ f", installed {detail}" if detail else "",
+ )
+ return reason, detail
+
+
+def _torch_gpu_mismatch_report() -> Dict[str, Any]:
+ """``physical_devices`` + ``mismatch`` for a host whose GPUs PyTorch cannot use.
+
+ ``{}`` when there is nothing to report: torch is absent, unimportable or healthy,
+ or no probe found a physical card. Both keys sit BESIDE ``devices`` in the
+ visibility payload and never inside it -- ``devices`` is the runtime-usable list
+ that model fit budgets against and that the training device picker pins from.
+ """
+ # block=False: request path, and the torch probes hold as long as a wedged driver.
+ reason = torch_build_snapshot(block = False)["reason"]
+ if reason is None:
+ return {}
+ # block=False: GET /api/system holds _system_gpu_cache_lock for the whole call.
+ inventory = get_physical_gpu_inventory(block = False)
+ physical = _devices_that_can_establish_a_mismatch(inventory.get("devices") or [])
+ if not physical:
+ return {}
+ return {
+ "physical_devices": physical,
+ "mismatch": {
+ "reason": reason,
+ "torch_version": _reported_torch_label(CHAT_ONLY_DETAIL),
+ "physical_count": len(physical),
+ "sources": inventory.get("sources") or [],
+ },
+ }
+
+
def verdict_pending_mlx_repair(chat_only: bool, reason: Optional[str]) -> bool:
"""True when this settled verdict is one the MLX self-heal is about to overturn.
@@ -479,6 +1683,9 @@ def detect_hardware() -> DeviceType:
return device
DETECTION_GENERATION += 1
DETECTION_COMPLETE.set()
+ # A pass has settled, so a later recovery may ask for another one.
+ global _REDETECTION_REQUESTED
+ _REDETECTION_REQUESTED = False
return device
@@ -496,6 +1703,7 @@ def ensure_hardware_detected(epoch: Optional[int] = None) -> DeviceType:
reading it here would bind the pass to the retirement it must lose to. Direct callers
pass nothing and own the current epoch."""
global DEVICE, CHAT_ONLY, CHAT_ONLY_REASON, CHAT_ONLY_DETAIL, DETECTION_GENERATION
+ global _REDETECTION_REQUESTED
with _DETECT_LOCK:
if epoch is None:
# A nested read inside an owning scope belongs to that pass, not to whatever
@@ -540,16 +1748,22 @@ def ensure_hardware_detected(epoch: Optional[int] = None) -> DeviceType:
# waiter trusting it could publish training-enabled for a CPU/chat-only host.
# Unconditional, unlike the counter: re-setting is a no-op and a late waiter needs it.
DETECTION_COMPLETE.set()
+ if produced_here:
+ # A pass has settled, so a later recovery may ask for another one. Cleared here as well as
+ # in detect_hardware(), because recovery starts its pass through this function. After the
+ # epoch check, so a retired pass does not release a guard it did not satisfy.
+ _REDETECTION_REQUESTED = False
return DEVICE
def _detect_hardware_locked() -> DeviceType:
"""detect_hardware() body. Call only with _DETECT_LOCK held."""
global DEVICE, CHAT_ONLY, CHAT_ONLY_REASON, CHAT_ONLY_DETAIL, IS_ROCM
- global _MLX_BLOCKERS_MEASURED
+ global _MLX_BLOCKERS_MEASURED, CHAT_ONLY_MISMATCH_VENDORS
CHAT_ONLY = True # reset -- only CUDA/ROCm/XPU/MLX sets it to False
CHAT_ONLY_REASON = None
CHAT_ONLY_DETAIL = None
+ CHAT_ONLY_MISMATCH_VENDORS = frozenset()
_MLX_BLOCKERS_MEASURED = None
IS_ROCM = False
@@ -663,10 +1877,34 @@ def _detect_hardware_locked() -> DeviceType:
elif TORCH_IMPORT_ERROR is not None:
# torch installed but broken, so this host was never measured. "no_gpu" would lie.
CHAT_ONLY_REASON = "detection_failed"
+ # Still measurable from the wheel on disk, and this host needs it most: detection_failed
+ # otherwise sends the user to the server log instead of offering the repair, and the
+ # verdict refresh deliberately freezes it. From DISK, because the import is what failed.
+ #
+ # Both suppressions classify_torch_build() applies before its own disk fallback: a
+ # deliberately CPU-only install is not broken, and the repair offered for it would
+ # reinstall the wheel that was asked for. Applied here rather than through that
+ # function, which goes via _has_torch() and would re-run the import that just failed.
+ _disk_reason = None
+ if not (
+ _masks_hide_every_accelerator(block_inventory = True) or _expected_cpu_flavor_was_chosen()
+ ):
+ _disk_reason = _classification_from_disk_label()
+ _seed_torch_build_snapshot(_disk_reason)
+ _build_reason, _build_detail = _mismatch_verdict_for_this_host(_disk_reason)
+ if _build_reason is not None:
+ CHAT_ONLY_REASON, CHAT_ONLY_DETAIL = _build_reason, _build_detail
elif platform.system() == "Darwin":
CHAT_ONLY_REASON = "intel_mac" # Intel Mac: no PyTorch/MLX -> GGUF-only by design.
else:
+ # torch imported cleanly and reported no accelerator, which is NOT "this host has no GPU":
+ # a Windows update installs PyPI's 2.11.0+cpu over cu124 while nvidia-smi lists every
+ # card (#8473). Ask the OS before blaming hardware.
CHAT_ONLY_REASON = "no_gpu"
+ if torch_ok:
+ _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
print("Hardware detected: CPU training backend (no PyTorch/MLX GPU backend available)")
return DEVICE
@@ -682,6 +1920,179 @@ def get_device() -> DeviceType:
return ensure_hardware_detected()
+def _torch_reports_a_usable_accelerator() -> bool:
+ """Whether torch can open a GPU right now. Never raises."""
+ try:
+ import torch
+ for probe in (
+ getattr(getattr(torch, "cuda", None), "is_available", None),
+ getattr(getattr(torch, "xpu", None), "is_available", None),
+ ):
+ try:
+ if callable(probe) and probe():
+ return True
+ except Exception:
+ continue
+ except Exception:
+ return False
+ return False
+
+
+def _request_hardware_redetection() -> None:
+ """Ask for a fresh detection pass, at most one per recovery. Never raises.
+
+ Retiring the epoch is what the rest of this module already uses to mean "the
+ published verdict is stale"; recomputing DEVICE here instead would publish from a
+ request thread and race the detection lock.
+ """
+ global _REDETECTION_REQUESTED
+ if _REDETECTION_REQUESTED:
+ return
+ try:
+ _REDETECTION_REQUESTED = True
+ # Retiring the epoch alone is NOT enough: invalidate_detection leaves DEVICE set and
+ # DETECTION_COMPLETE raised, so start_background_detection returns immediately. Both under
+ # the lock, so a running pass cannot publish over the reset.
+ invalidate_detection()
+ invalidate_torch_build_snapshot()
+ with _DETECT_LOCK:
+ _discard_detection_locked()
+ start_background_detection()
+ logger.info(
+ "An accelerator became usable after startup; discarded the cached hardware "
+ "verdict and started a fresh detection pass."
+ )
+ except Exception as e:
+ _REDETECTION_REQUESTED = False
+ logger.debug("Could not request hardware re-detection: %s", e)
+
+
+def _remember_the_vendors_behind_the_mismatch(devices: list[Dict[str, Any]]) -> None:
+ """Record which vendors' cards establish the mismatch being reported right now."""
+ global CHAT_ONLY_MISMATCH_VENDORS
+ CHAT_ONLY_MISMATCH_VENDORS = frozenset(
+ device.get("vendor") for device in devices if device.get("vendor")
+ )
+
+
+def _uncertainty_could_hide_the_frozen_mismatch(inventory: Dict[str, Any]) -> bool:
+ """Whether an inventory that could not answer might still hold the mismatched card.
+
+ Only for the vendors the mismatch came from. _carry_unanswered_vendors_forward has
+ already re-added what an unanswered vendor last reported, so nothing left here means
+ every vendor that named a card answered "none" this pass. Holding the mismatch on an
+ unrelated vendor's broken probe would then assert a GPU with nothing to point at: a
+ detached AMD eGPU staying "unusable" while nvidia-smi is broken on a host that never
+ had an NVIDIA card.
+
+ True when no vendor was recorded (every reason but the two mismatches, so those are
+ unchanged), and true for an unknown that names nobody, which is how a cold cache reads.
+ """
+ if not CHAT_ONLY_MISMATCH_VENDORS:
+ return True
+ unanswered = set(inventory.get("unanswered") or ())
+ if not unanswered:
+ return True
+ return bool(unanswered & CHAT_ONLY_MISMATCH_VENDORS)
+
+
+def current_chat_only_verdict() -> tuple[Optional[str], Optional[str]]:
+ """``(reason, detail)``, re-derived when the physical inventory can still change it.
+
+ detect_hardware() runs once at startup, but the inventory it consulted refreshes on
+ a 60 second TTL. An eGPU attached after launch, or a driver that finished restarting
+ after the first probe, flips the answer while the frozen verdict keeps saying
+ ``no_gpu``: /api/system would list the card and publish a mismatch while the sidebar
+ and the Export and Video pages went on insisting no accelerator exists. The reverse
+ is the same bug -- a card that goes away leaves a mismatch nobody can act on.
+
+ Only the three inventory-sensitive verdicts are re-derived, plus the one
+ detection_failed that is not really unmeasured: a torch that will not import was
+ classified from its wheel on disk at startup, so the inventory is the only thing still
+ missing, and freezing that host is the same split this function exists to close.
+ mlx_unavailable, intel_mac and a detection_failed with no importable torch and no
+ readable wheel describe things a 60 second probe cannot change, and re-deriving those
+ would fight detect_hardware() rather than follow it.
+
+ Never raises: a probe that cannot answer keeps the frozen verdict, as does an
+ inventory whose uncertainty is about the vendor the frozen mismatch came from.
+ """
+ reason, detail = CHAT_ONLY_REASON, CHAT_ONLY_DETAIL
+ frozen_but_measurable = reason == "detection_failed" and TORCH_IMPORT_ERROR is not None
+ if reason not in ("no_gpu", "torch_cpu_build", "torch_cuda_unavailable"):
+ if not frozen_but_measurable:
+ return reason, detail
+ try:
+ snapshot = torch_build_snapshot(block = False)
+ if snapshot["unknown"]:
+ # Nothing measured yet is not "torch is fine"; keep what detection published.
+ return reason, detail
+ build_reason = snapshot["reason"]
+ if build_reason is None and snapshot["usable"]:
+ # The accelerator came BACK. Only reason and detail refresh here, so DEVICE and CHAT_ONLY
+ # would stay frozen at CPU while this reported no_gpu, until a restart. Retire the
+ # detection instead and let the next pass publish the real answer.
+ _request_hardware_redetection()
+ return reason, detail
+ # block=False: /api/health and /api/liveness reach here, and the NVIDIA half shells out
+ # with a 10 second timeout on exactly the hung-driver host this exists for.
+ inventory = get_physical_gpu_inventory(block = False)
+ establishing = (
+ _devices_that_can_establish_a_mismatch(inventory.get("devices") or [])
+ if build_reason is not None
+ else []
+ )
+ if establishing:
+ # Re-record who says so: the mismatch can MOVE between vendors inside one process
+ # (swap an AMD eGPU for an NVIDIA one), and the startup answer would then freeze
+ # the verdict on a vendor a later refresh has already watched disappear.
+ _remember_the_vendors_behind_the_mismatch(establishing)
+ return build_reason, _reported_torch_label(detail)
+ if inventory.get("unknown") and _uncertainty_could_hide_the_frozen_mismatch(inventory):
+ return reason, detail
+ except Exception as e:
+ logger.debug("chat-only verdict refresh failed: %s", e)
+ return reason, detail
+ # "No GPU here" is a measurement, and a host whose torch will not import never made
+ # one: detect_hardware() refused to say it, so this cannot say it either.
+ return (reason, detail) if frozen_but_measurable else ("no_gpu", None)
+
+
+def _gpu_present_but_unusable_message(
+ feature: str, verdict: Optional[tuple[Optional[str], Optional[str]]] = None
+) -> Optional[str]:
+ """The capability message for a host whose GPUs are real but unreachable by torch.
+
+ ``None`` when this host is not in that state. detect_hardware() records
+ ``torch_cpu_build`` / ``torch_cuda_unavailable`` only after the OS inventory has
+ actually found a card, so reaching this point means the "no supported accelerator
+ was found" wording below would contradict the System tab and send the user after
+ hardware they already own. Both reasons are surfaced verbatim by the Export and
+ Video pages and by rejected export API calls.
+ """
+ # ``verdict`` lets one response read it ONCE: two reads across a TTL boundary can describe
+ # different hosts.
+ reason, detail = verdict if verdict is not None else current_chat_only_verdict()
+ if reason not in ("torch_cpu_build", "torch_cuda_unavailable"):
+ return None
+ installed = f" (installed {detail})" if detail else ""
+ # Both routes, always. The repair row exists only in the desktop app and only for a
+ # backend it manages, so a browser-hosted Studio, or a desktop attached to a server
+ # someone started from a terminal, was being sent to a control that is not on the page.
+ if reason == "torch_cpu_build":
+ return (
+ f"This host has a GPU, but the installed PyTorch is a CPU-only build{installed}, "
+ f"so {feature} cannot use it. Reinstall the GPU build: use Repair installation "
+ f"in Settings in the desktop app, or re-run the Unsloth installer."
+ )
+ return (
+ f"This host has a GPU, but the installed PyTorch{installed} cannot initialise it, so "
+ f"{feature} cannot use it. This is usually a driver or runtime mismatch; reinstalling "
+ f"a matching PyTorch build fixes it. Use Repair installation in Settings in the "
+ f"desktop app, or re-run the Unsloth installer."
+ )
+
+
def export_capability() -> dict:
"""Whether model export can run here, with a torch-aware reason when it cannot.
@@ -697,9 +2108,10 @@ def export_capability() -> dict:
"export_unsupported_reason": None,
"export_unsupported_message": None,
}
+ verdict = current_chat_only_verdict()
# No accelerator: name the blocker. Detection failure first -- the branches below all
# describe a measured host, so a broken probe would tell a GPU box to install PyTorch.
- if CHAT_ONLY_REASON == "detection_failed":
+ if verdict[0] == "detection_failed":
reason = "detection_failed"
message = (
"Hardware detection failed on this host, so export is disabled. The server log records "
@@ -711,6 +2123,11 @@ def export_capability() -> dict:
"Export on Apple Silicon requires the MLX stack, which is unavailable or too old. Run "
"`unsloth studio update` to restore MLX and enable export."
)
+ elif _gpu_present_but_unusable_message("export", verdict) is not None:
+ # BEFORE _has_torch(), which reports an unimportable wheel as absent while re-running the
+ # seconds-long import that already failed.
+ reason = verdict[0]
+ message = _gpu_present_but_unusable_message("export", verdict)
elif not _has_torch():
reason = "pytorch_not_installed"
message = (
@@ -749,7 +2166,8 @@ def video_capability() -> dict:
}
# Detection failure first, as in export_capability: the branches below all describe a
# measured host, so a broken probe would tell a GPU box to go buy a GPU.
- if CHAT_ONLY_REASON == "detection_failed":
+ verdict = current_chat_only_verdict()
+ if verdict[0] == "detection_failed":
reason = "detection_failed"
message = (
"Hardware detection failed on this host, so video generation is disabled. The server "
@@ -792,6 +2210,9 @@ def video_capability() -> dict:
"Video generation requires Apple Silicon. This Intel Mac has no Metal (MPS) device "
"for the video pipelines to run on."
)
+ elif _gpu_present_but_unusable_message("video generation", verdict) is not None:
+ reason = verdict[0]
+ message = _gpu_present_but_unusable_message("video generation", verdict)
elif not _has_torch():
reason = "pytorch_not_installed"
message = (
@@ -2015,6 +3436,7 @@ def _rocm_linux_sysfs_vram_gb() -> tuple[Optional[float], Optional[float]]:
# 0x1002. NVIDIA's open kernel module also registers KFD nodes (vendor_id 0x10DE);
# a non-AMD node is not a HIP device and must never take an ordinal.
_AMD_PCI_VENDOR_ID = 4098
+_INTEL_PCI_VENDOR_ID = 0x8086
def _rocm_kfd_gpu_pci_ids() -> list[str]:
@@ -2259,8 +3681,14 @@ def _parse_adapter_family_gfx(family: str) -> str:
return ""
-def _windows_amd_adapter_records_by_luid() -> dict[int, Dict[str, Any]]:
- """DirectX registry metadata for AMD adapters, keyed by LUID.
+def _windows_amd_adapter_records_by_luid(
+ vendor_id_filter: int = _AMD_PCI_VENDOR_ID, *, distinguish_failure: bool = False
+) -> "dict[int, Dict[str, Any]] | None":
+ """DirectX registry metadata for one vendor's adapters, keyed by LUID.
+
+ ``vendor_id_filter`` defaults to AMD, which is every pre-existing caller. The
+ physical inventory passes Intel's id as well, so an Arc host whose XPU wheel was
+ replaced is reported rather than going silent.
``gfx`` is absent when the driver wrote no ``AdapterFamily``.
``dedicated_memory_bytes`` is absent when neither dedicated-memory value is available.
@@ -2268,16 +3696,31 @@ def _windows_amd_adapter_records_by_luid() -> dict[int, Dict[str, Any]]:
All or nothing: a record this cannot read makes the map incomplete, and an
incomplete map is indistinguishable from a complete one at the join, which
would then pair a visible card with a hidden same-named card's counter. So
- any failure past the point where a subkey is known to be an adapter returns
- ``{}``, which drops the caller back to capacity ranking. Same for off
- Windows or without the key.
+ any failure past the point where a subkey is known to be an adapter gives up
+ on the whole map, which drops the caller back to capacity ranking. Same for
+ off Windows or without the key.
+
+ The ranking callers only need "no usable map", so they get ``{}``.
+ ``distinguish_failure`` returns ``None`` for that case instead: the inventory has
+ to tell a vendor with no adapters from a vendor it could not ask, because
+ publishing the first for the second erases a settled mismatch.
"""
- if platform.system() != "Windows":
+ records = _windows_amd_adapter_records_or_none(vendor_id_filter)
+ if records is None and not distinguish_failure:
return {}
+ return records
+
+
+def _windows_amd_adapter_records_or_none(
+ vendor_id_filter: int = _AMD_PCI_VENDOR_ID,
+) -> "dict[int, Dict[str, Any]] | None":
+ """The read itself. ``None`` whenever the registry could not answer."""
+ if platform.system() != "Windows":
+ return None
try:
import winreg
except ImportError:
- return {}
+ return None
by_luid: dict[int, Dict[str, Any]] = {}
try:
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, _WINDOWS_DIRECTX_KEY) as dx_key:
@@ -2289,7 +3732,7 @@ def _windows_amd_adapter_records_by_luid() -> dict[int, Dict[str, Any]]:
continue
with winreg.OpenKey(dx_key, subkey) as adapter_key:
vendor_id, _ = winreg.QueryValueEx(adapter_key, "VendorId")
- if int(vendor_id) != _AMD_PCI_VENDOR_ID:
+ if int(vendor_id) != vendor_id_filter:
continue
luid, _ = winreg.QueryValueEx(adapter_key, "AdapterLuid")
description, _ = winreg.QueryValueEx(adapter_key, "Description")
@@ -2311,8 +3754,7 @@ def _windows_amd_adapter_records_by_luid() -> dict[int, Dict[str, Any]]:
pass
name = str(description).strip()
if not name:
- # An AMD adapter this cannot name: see the all-or-nothing note.
- return {}
+ return None
record = {"name": name}
gfx = _parse_adapter_family_gfx(str(family))
if gfx:
@@ -2322,7 +3764,7 @@ def _windows_amd_adapter_records_by_luid() -> dict[int, Dict[str, Any]]:
by_luid[int(luid)] = record
except Exception as e:
logger.debug("DirectX adapter registry read declined: %s", e)
- return {}
+ return None
return by_luid
@@ -4868,6 +6310,9 @@ def get_backend_visible_gpu_info() -> Dict[str, Any]:
"parent_visible_gpu_ids": [],
"devices": [],
"index_kind": "vulkan",
+ # Physically present cards this PyTorch cannot open, reported ALONGSIDE the empty
+ # `devices` above and never merged into it.
+ **_torch_gpu_mismatch_report(),
}
diff --git a/studio/backend/utils/hardware/nvidia.py b/studio/backend/utils/hardware/nvidia.py
index adefc121e37..7fa67607a49 100644
--- a/studio/backend/utils/hardware/nvidia.py
+++ b/studio/backend/utils/hardware/nvidia.py
@@ -1,6 +1,9 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+import os
+import platform
+import shutil
import subprocess
from typing import Any, Optional
@@ -236,24 +239,58 @@ def get_visible_gpu_utilization(
}
-def get_backend_visible_gpu_info(
- parent_visible_ids: Optional[list[int]], backend_cuda_visible_devices: Optional[str]
-) -> dict[str, Any]:
- # parent_visible_ids None (UUID/MIG mask): can't map nvidia-smi rows to
- # visible devices.
- if parent_visible_ids is None:
- return {
- "available": False,
- "backend_cuda_visible_devices": backend_cuda_visible_devices,
- "parent_visible_gpu_ids": [],
- "devices": [],
- "index_kind": "unresolved",
- }
- visible_ordinals = _visible_ordinal_map(parent_visible_ids)
+def _nvidia_smi_executable() -> str:
+ """The nvidia-smi to run, resolving the standard Windows locations off PATH.
+
+ A driver install can leave nvidia-smi.exe in the NVSMI directory or the driver
+ store without putting either on PATH, and a bare "nvidia-smi" then raises
+ FileNotFoundError. That would leave the physical inventory empty on exactly the
+ host this inventory exists for: real GPUs, a PyTorch that cannot see them. Same
+ two locations setup.ps1 already falls back to. Returns the bare name when nothing
+ better is found, so the caller's existing OSError handling still applies.
+ """
+ found = shutil.which("nvidia-smi")
+ if found:
+ return found
+ if platform.system() != "Windows":
+ return "nvidia-smi"
+ for base, tail in (
+ (os.environ.get("ProgramFiles"), r"NVIDIA Corporation\NVSMI\nvidia-smi.exe"),
+ (os.environ.get("SystemRoot"), r"System32\nvidia-smi.exe"),
+ ):
+ if not base:
+ continue
+ candidate = os.path.join(base, tail)
+ if os.path.isfile(candidate):
+ return candidate
+ return "nvidia-smi"
+
+
+# "nvidia-smi is not on this machine" is a conclusive answer, not a failed probe: it is the
+# normal state of every CPU-only, AMD and Intel host, and the installers read the same
+# absence the same way. Distinct from None, which means a probe that WAS found could not
+# answer -- a hung driver, a permission fault, a non-zero exit.
+NVIDIA_SMI_ABSENT = object()
+
+
+def _query_gpu_inventory(caller: str) -> Any:
+ """``[{index, name, memory_total_gb}]`` for every GPU nvidia-smi enumerates.
+
+ ``None`` when the query could not be answered at all -- no nvidia-smi on PATH, a
+ driver that hung past the timeout, a non-zero exit. Callers report that as
+ "unknown", which is not the same as the empty list a working driver with no
+ cards returns. Never raises.
+
+ Split out of get_backend_visible_gpu_info so the same rows can be read WITHOUT a
+ ``DeviceType.CUDA`` precondition: get_physical_gpu_inventory below is reached on
+ exactly the host where torch reports no CUDA device, and that host still has its
+ GPUs. Rows a caller cannot make sense of are dropped, not raised on -- a name
+ holding commas is rejoined, and a malformed index or memory column skips the row.
+ """
try:
result = subprocess.run(
[
- "nvidia-smi",
+ _nvidia_smi_executable(),
"--query-gpu=index,name,memory.total",
"--format=csv,noheader,nounits",
],
@@ -265,16 +302,138 @@ def get_backend_visible_gpu_info(
env = child_env_without_native_path_secret(),
**_windows_hidden_subprocess_kwargs(),
)
+ except FileNotFoundError as e:
+ # No nvidia-smi at all, the NORMAL state of every CPU-only, AMD and Intel host. This is
+ # called on a 60 second refresh reached from the health and system polls, so warning here
+ # would log a line every minute on machines that are working correctly.
+ logger.debug("nvidia-smi is not installed (%s): %s", caller, e)
+ return NVIDIA_SMI_ABSENT
except (OSError, subprocess.TimeoutExpired) as e:
- logger.warning("nvidia-smi query failed in get_backend_visible_gpu_info: %s", e)
+ # Past this point an nvidia-smi WAS found, so a failure is a real fault on this host.
+ logger.warning("nvidia-smi query failed in %s: %s", caller, e)
+ return None
+ if result.returncode != 0:
+ return None
+
+ rows: list[dict[str, Any]] = []
+ for line in result.stdout.strip().splitlines():
+ parts = [p.strip() for p in line.split(",")]
+ if len(parts) < 3:
+ continue
+ try:
+ idx = int(parts[0])
+ except (ValueError, TypeError):
+ continue
+ # Rejoin in case the GPU name contains commas
+ name = parts[1] if len(parts) == 3 else ", ".join(parts[1:-1])
+ # A capacity this build of nvidia-smi will not report ("[N/A]", as _parse_smi_value
+ # already recognises) is a missing metric, not a missing card. Dropping the row hid
+ # the GPU from the whole inventory, and on Linux the procfs fallback does not run
+ # either, since that answers for a query that FAILED rather than one that came back
+ # short: the host lost its mismatch and its repair guidance over an unknown size.
+ mem_total_mb = _parse_smi_value(parts[-1])
+ rows.append(
+ {
+ "index": idx,
+ "name": name,
+ "memory_total_gb": round(mem_total_mb / 1024, 2)
+ if mem_total_mb is not None
+ else None,
+ }
+ )
+ return rows
+
+
+def _linux_nvidia_procfs_gpu_count() -> int:
+ """How many GPUs the NVIDIA kernel driver enumerates under /proc, or 0.
+
+ One subdirectory per GPU, published whatever nvidia-smi's state is, which is why the
+ installer falls back to it too. Never raises; 0 on any platform without it.
+ """
+ if platform.system() != "Linux":
+ return 0
+ try:
+ entries = os.listdir("/proc/driver/nvidia/gpus")
+ except OSError:
+ return 0
+ return len(entries)
+
+
+def get_physical_gpu_inventory() -> dict[str, Any]:
+ """Every NVIDIA GPU the driver enumerates, with no visibility mask and no torch.
+
+ Display-only inventory: ``index`` is nvidia-smi's own row number, which is a
+ physical id and NOT something a caller may pin, because the whole point of this
+ probe is that PyTorch cannot open these devices. A failed probe comes back as a
+ structured unavailable result, so this never raises out of an endpoint.
+ """
+ rows = _query_gpu_inventory("get_physical_gpu_inventory")
+ # Either way the CLI could not answer. The kernel driver publishes its cards regardless,
+ # and on a cold start there is no settled verdict for the resulting unknown to protect.
+ if (rows is NVIDIA_SMI_ABSENT or rows is None) and _linux_nvidia_procfs_gpu_count():
+ # The kernel driver is loaded and enumerating cards; only the CLI is missing.
+ # _has_usable_nvidia_gpu() reads the same directory, so without this the installer can
+ # repair a CUDA wheel on a host the backend insists has no card. No name and no capacity:
+ # procfs gives neither, and an invented one would be worse than an honest blank.
+ return {
+ "available": True,
+ "source": "proc-driver-nvidia",
+ "devices": [
+ {
+ "vendor": "nvidia",
+ "index": ordinal,
+ "name": None,
+ "memory_total_gb": None,
+ "source": "proc-driver-nvidia",
+ }
+ for ordinal in range(_linux_nvidia_procfs_gpu_count())
+ ],
+ "error": None,
+ "absent": False,
+ }
+ if rows is NVIDIA_SMI_ABSENT:
+ # An answer, and the caller must not read it as "some probe failed": an AMD-only host has
+ # no nvidia-smi by design.
+ return {
+ "available": False,
+ "source": "nvidia-smi",
+ "devices": [],
+ "error": "nvidia-smi is not installed",
+ "absent": True,
+ }
+ if rows is None:
+ return {
+ "available": False,
+ "source": "nvidia-smi",
+ "devices": [],
+ "error": "nvidia-smi did not answer",
+ "absent": False,
+ }
+ return {
+ "available": bool(rows),
+ "source": "nvidia-smi",
+ "devices": [{**row, "vendor": "nvidia", "source": "nvidia-smi"} for row in rows],
+ "error": None,
+ "absent": False,
+ }
+
+
+def get_backend_visible_gpu_info(
+ parent_visible_ids: Optional[list[int]], backend_cuda_visible_devices: Optional[str]
+) -> dict[str, Any]:
+ # parent_visible_ids None (UUID/MIG mask): can't map nvidia-smi rows to
+ # visible devices.
+ if parent_visible_ids is None:
return {
"available": False,
"backend_cuda_visible_devices": backend_cuda_visible_devices,
- "parent_visible_gpu_ids": parent_visible_ids or [],
+ "parent_visible_gpu_ids": [],
"devices": [],
- "index_kind": "physical",
+ "index_kind": "unresolved",
}
- if result.returncode != 0:
+ visible_ordinals = _visible_ordinal_map(parent_visible_ids)
+ rows = _query_gpu_inventory("get_backend_visible_gpu_info")
+ if rows is None or rows is NVIDIA_SMI_ABSENT:
return {
"available": False,
"backend_cuda_visible_devices": backend_cuda_visible_devices,
@@ -284,22 +443,10 @@ def get_backend_visible_gpu_info(
}
devices = []
- for line in result.stdout.strip().splitlines():
- parts = [p.strip() for p in line.split(",")]
- if len(parts) < 3:
- continue
- try:
- idx = int(parts[0])
- except (ValueError, TypeError):
- continue
+ for row in rows:
+ idx = row["index"]
if visible_ordinals is not None and idx not in visible_ordinals:
continue
- # Rejoin in case the GPU name contains commas
- name = parts[1] if len(parts) == 3 else ", ".join(parts[1:-1])
- try:
- mem_total_mb = int(parts[-1])
- except (ValueError, TypeError):
- continue
devices.append(
{
"index": idx,
@@ -307,8 +454,8 @@ def get_backend_visible_gpu_info(
"visible_ordinal": (
visible_ordinals[idx] if visible_ordinals is not None else len(devices)
),
- "name": name,
- "memory_total_gb": round(mem_total_mb / 1024, 2),
+ "name": row["name"],
+ "memory_total_gb": row["memory_total_gb"],
}
)
diff --git a/studio/frontend/src/app/provider.tsx b/studio/frontend/src/app/provider.tsx
index d51a639a925..abc02112923 100644
--- a/studio/frontend/src/app/provider.tsx
+++ b/studio/frontend/src/app/provider.tsx
@@ -31,6 +31,7 @@ import {
useTheme,
} from "@/features/settings";
import { SttDownloadPrompt } from "@/features/settings/components/stt-download-prompt";
+import { TauriRepairContext } from "@/hooks/tauri-repair-context";
import { TauriUpdateContext } from "@/hooks/tauri-update-context";
import { type BackendStatus, useTauriBackend } from "@/hooks/use-tauri-backend";
import { useTauriUpdate } from "@/hooks/use-tauri-update";
@@ -43,6 +44,7 @@ import {
type CSSProperties,
type ReactNode,
useEffect,
+ useMemo,
useRef,
useState,
} from "react";
@@ -555,8 +557,26 @@ function TauriWrapper({ children }: { children: ReactNode }) {
retryInstall,
approveElevation,
copyDiagnostics,
+ startRepair,
} = useTauriBackend();
+ // Settings' manual repair reruns the INSTALLER, not `studio update`: an update reuses the
+ // environment it finds, so a venv whose PyTorch was replaced by a CPU-only wheel comes back
+ // from a successful update still CPU-only.
+ //
+ // Through a ref, not a dependency: startRepair is a plain function declaration rebuilt on
+ // every render, so listing it would give the context a new identity on each status tick and
+ // pinning it with [] would freeze the first render's closure.
+ const startRepairRef = useRef(startRepair);
+ startRepairRef.current = startRepair;
+ const repairController = useMemo(
+ () => ({
+ repairInstall: () => startRepairRef.current({ forceInstaller: true }),
+ isExternalServer,
+ }),
+ [isExternalServer],
+ );
+
const appliedWindowModeRef = useRef<TauriWindowMode | null>(null);
const hasEnteredAppModeRef = useRef(false);
const windowLayoutGenerationRef = useRef(0);
@@ -781,10 +801,10 @@ function TauriWrapper({ children }: { children: ReactNode }) {
// alike, and a declined quit puts the user back where they were rather than remounting
// the tree under them.
const content = (
- <>
+ <TauriRepairContext.Provider value={repairController}>
{shell}
{closing && <ClosingScreen />}
- </>
+ </TauriRepairContext.Provider>
);
const chromeVars = (
diff --git a/studio/frontend/src/components/app-sidebar.tsx b/studio/frontend/src/components/app-sidebar.tsx
index 7b2022658e4..22b0b31581e 100644
--- a/studio/frontend/src/components/app-sidebar.tsx
+++ b/studio/frontend/src/components/app-sidebar.tsx
@@ -588,6 +588,29 @@ const SIDEBAR_SELECTOR = '[data-slot="sidebar"]';
const VERDICT_UNKNOWN_POLL_MS = 3000;
const SELF_HEAL_POLL_MS = 15000;
const VERDICT_POLL_STALL_MS = 30000;
+// The backend reclassifies a host without a restart on a 60s TTL: attach an eGPU to a
+// CPU-torch machine and no_gpu becomes torch_cpu_build. Nothing else re-reads the verdict.
+// Matched to that TTL, since polling faster than the answer can change is pure traffic.
+const INVENTORY_POLL_MS = 60000;
+// The health path reads those snapshots non-blocking: the first read past expiry SCHEDULES
+// the refresh and returns the stale entry, so the new answer lands a moment later. On the
+// TTL alone the read that triggers the refresh is a whole interval from the read that
+// consumes it, leaving an attached eGPU invisible for close to two minutes. One short
+// follow-up read collects it instead.
+const INVENTORY_FOLLOW_UP_MS = 4000;
+// The verdicts the inventory can still move. Everything else describes something a probe
+// cannot change (an Intel Mac stays an Intel Mac).
+const INVENTORY_SENSITIVE_REASONS = new Set([
+ "no_gpu",
+ "torch_cpu_build",
+ "torch_cuda_unavailable",
+ // A torch that will not import is classified from its wheel on disk, so the backend can
+ // replace this with torch_cpu_build or torch_cuda_unavailable once the OS probe recovers.
+ // Leaving it out treated that first answer as settled and stopped the only forced health
+ // re-read, so the sidebar and navigation stayed on the failure for the rest of the session
+ // while /api/system already reported the mismatch.
+ "detection_failed",
+]);
/** One workflow in the list under the Images row. */
function WorkflowChoice({
@@ -810,9 +833,16 @@ export function AppSidebar() {
: "Training needs MLX. Run `unsloth studio update` to enable Train."
: chatOnlyReason === "intel_mac"
? "Training needs Apple Silicon or a GPU. Intel Macs are chat-only."
- : chatOnlyReason === "no_gpu"
- ? "Training needs an NVIDIA or AMD GPU."
- : undefined;
+ : chatOnlyReason === "torch_cpu_build" ||
+ chatOnlyReason === "torch_cuda_unavailable"
+ ? // The host HAS GPUs; this PyTorch cannot open them. "Get a GPU" is both wrong
+ // and unactionable here, so name the installed build and point at the repair.
+ chatOnlyDetail
+ ? `Training needs a working PyTorch GPU build. This machine's GPUs were detected but PyTorch ${chatOnlyDetail} cannot use them; repair the installation.`
+ : "Training needs a working PyTorch GPU build. This machine's GPUs were detected but PyTorch cannot use them; repair the installation."
+ : chatOnlyReason === "no_gpu"
+ ? "Training needs an NVIDIA or AMD GPU."
+ : undefined;
// Everything without a hint reaches VideoPage, which answers from the backend's video verdict.
const videoDisabledHint = videoNavHint(chatOnlyMeasured, chatOnlyReason);
const videoDisabled = videoDisabledHint !== undefined;
@@ -832,13 +862,20 @@ export function AppSidebar() {
// recovery poll in the app, and the sidebar is mounted on every route that gates on the
// verdict (studio-page reads the same store, so it recovers with it; video-page reads the
// backend's video verdict instead and needs nothing from here).
- if (selfHealSettled && !capabilitiesUnknown) return;
+ const inventorySensitive =
+ chatOnly && INVENTORY_SENSITIVE_REASONS.has(chatOnlyReason ?? "");
+ if (selfHealSettled && !capabilitiesUnknown && !inventorySensitive) return;
let pollingSince = 0;
// Which read currently owns the guard. A read that outlived the stall window is replaced,
// and the replacement takes the guard with it; without an owner the abandoned read's
// `finally` would clear a guard it no longer holds and let the next tick stack another
// forced read onto the slow backend, every interval, which is the pile-up this prevents.
let pollOwner = 0;
+ // Cleared on unmount with the interval: a follow-up outliving the effect would read
+ // against a verdict this effect no longer describes. Through `window`, like the
+ // interval beside it, and 0 for "none" because that is what window.setTimeout never
+ // returns.
+ let followUp = 0;
const id = window.setInterval(() => {
// A backend still importing torch answers slowly, so skip while a re-read is outstanding
// rather than stacking them against it. Bounded, or a request that never settles would
@@ -849,10 +886,27 @@ export function AppSidebar() {
void fetchDeviceType({ force: true })
.catch(() => undefined)
.finally(() => {
- if (owned === pollOwner) pollingSince = 0;
+ if (owned !== pollOwner) return;
+ pollingSince = 0;
+ // Only where a background refresh is what we are waiting on. The unknown poll is
+ // already fast enough, and the self-heal poll is not waiting on a TTL at all.
+ if (!selfHealSettled || capabilitiesUnknown) return;
+ if (followUp) window.clearTimeout(followUp);
+ followUp = window.setTimeout(() => {
+ followUp = 0;
+ void fetchDeviceType({ force: true }).catch(() => undefined);
+ }, INVENTORY_FOLLOW_UP_MS);
});
- }, capabilitiesUnknown ? VERDICT_UNKNOWN_POLL_MS : SELF_HEAL_POLL_MS);
- return () => window.clearInterval(id);
+ }, capabilitiesUnknown
+ ? VERDICT_UNKNOWN_POLL_MS
+ : selfHealSettled
+ ? INVENTORY_POLL_MS
+ : SELF_HEAL_POLL_MS);
+ const stopPolling = () => {
+ window.clearInterval(id);
+ if (followUp) window.clearTimeout(followUp);
+ };
+ return () => stopPolling();
}, [capabilitiesUnknown, chatOnly, chatOnlyReason, detectionDeferred]);
const [shutdownOpen, setShutdownOpen] = useState(false);
diff --git a/studio/frontend/src/config/hardware-verdict.ts b/studio/frontend/src/config/hardware-verdict.ts
index 6ab06253574..57638dc6465 100644
--- a/studio/frontend/src/config/hardware-verdict.ts
+++ b/studio/frontend/src/config/hardware-verdict.ts
@@ -50,6 +50,13 @@ export function videoNavHint(
// that reads as a fix that would not work. Mirrors the backend's message for this host.
if (chatOnlyReason === "intel_mac")
return "Video generation requires Apple Silicon. This Intel Mac has no Metal device to run it.";
+ // The GPU is there; PyTorch is what cannot reach it (a Windows update that resolved torch
+ // from PyPI leaves a +cpu wheel beside two working cards), so "get a GPU" is a lie.
+ if (
+ chatOnlyReason === "torch_cpu_build" ||
+ chatOnlyReason === "torch_cuda_unavailable"
+ )
+ return "Video generation needs a working PyTorch GPU build. This machine's GPUs were detected but PyTorch cannot use them; repair the installation.";
if (chatOnlyReason === "no_gpu") return "Video generation needs an NVIDIA or AMD GPU.";
return undefined;
}
diff --git a/studio/frontend/src/features/settings/components/desktop-repair-control.tsx b/studio/frontend/src/features/settings/components/desktop-repair-control.tsx
new file mode 100644
index 00000000000..289236d9a84
--- /dev/null
+++ b/studio/frontend/src/features/settings/components/desktop-repair-control.tsx
@@ -0,0 +1,87 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import { Button } from "@/components/ui/button";
+import {
+ Dialog,
+ DialogContent,
+ DialogDescription,
+ DialogFooter,
+ DialogHeader,
+ DialogTitle,
+} from "@/components/ui/dialog";
+import { useTauriRepairController } from "@/hooks/tauri-repair-context";
+import { useT } from "@/i18n";
+import { type ReactElement, useState } from "react";
+import { SettingsRow } from "./settings-row";
+
+/**
+ * Reruns the bundled installer over the managed environment.
+ *
+ * Exists because an update cannot repair everything an update can break. `studio update`
+ * reuses the environment it finds; only the installer re-selects the PyTorch index and
+ * force-reinstalls the trio. A managed venv that ended up with a CPU-only PyTorch wheel
+ * therefore survives any number of successful updates, and until now the only way out was
+ * to paste the install one-liner into a terminal.
+ *
+ * Desktop-only: outside Tauri there is no controller in context and the row renders nothing.
+ * Hidden for an externally started backend too: start_managed_repair refuses to mutate an
+ * environment the desktop does not manage, but the refusal arrives after startRepair has
+ * already cleared the external-server state and swapped the shell to the repairing screen,
+ * so a connected user would end up on the repair-error screen instead of on their server.
+ * Confirmed before it runs, because it stops the backend and rewrites the environment.
+ */
+export function DesktopRepairControl(): ReactElement | null {
+ const t = useT();
+ const repair = useTauriRepairController();
+ const [confirmOpen, setConfirmOpen] = useState(false);
+ if (!repair || repair.isExternalServer) return null;
+
+ return (
+ <>
+ <SettingsRow
+ destructive={true}
+ label={t("settings.general.repairInstall.label")}
+ description={t("settings.general.repairInstall.description")}
+ >
+ <Button
+ variant="outline"
+ size="sm"
+ onClick={() => setConfirmOpen(true)}
+ className="text-destructive hover:text-destructive hover:border-destructive/60"
+ >
+ {t("settings.general.repairInstall.action")}
+ </Button>
+ </SettingsRow>
+
+ <Dialog open={confirmOpen} onOpenChange={setConfirmOpen}>
+ <DialogContent className="max-w-md">
+ <DialogHeader>
+ <DialogTitle>
+ {t("settings.general.repairInstall.confirmTitle")}
+ </DialogTitle>
+ <DialogDescription>
+ {t("settings.general.repairInstall.confirmDescription")}
+ </DialogDescription>
+ </DialogHeader>
+ <DialogFooter>
+ <Button variant="outline" onClick={() => setConfirmOpen(false)}>
+ {t("common.cancel")}
+ </Button>
+ <Button
+ onClick={() => {
+ // Closed first: the repair swaps the app over to the repairing screen, so a dialog
+ // still mounted would sit on top of it.
+ setConfirmOpen(false);
+ void repair.repairInstall();
+ }}
+ className="bg-destructive hover:bg-destructive/90 text-destructive-foreground"
+ >
+ {t("settings.general.repairInstall.confirmAction")}
+ </Button>
+ </DialogFooter>
+ </DialogContent>
+ </Dialog>
+ </>
+ );
+}
diff --git a/studio/frontend/src/features/settings/settings-search.ts b/studio/frontend/src/features/settings/settings-search.ts
index 2a21a4c8ac6..a5b807186c8 100644
--- a/studio/frontend/src/features/settings/settings-search.ts
+++ b/studio/frontend/src/features/settings/settings-search.ts
@@ -260,6 +260,12 @@ export function createSettingsSearchIndex({
"settings.general.startup.sectionTitle",
"settings.general.startup.launchAtLogin",
...(closeToTray ? (["settings.general.startup.closeToTray"] as const) : []),
+ // Desktop only, like the row itself: DesktopRepairControl renders nothing without a
+ // Tauri repair controller, so indexing it on the web build would scroll to a row
+ // that is not there. Worth indexing at all because the capability message for a host
+ // whose PyTorch cannot use its GPUs says to "use Repair installation in Settings",
+ // and searching Settings for "repair" answered "No settings found."
+ "settings.general.repairInstall.label",
],
about: SETTINGS_SEARCH_INDEX.about.filter(
(key) => key !== "settings.about.updates",
diff --git a/studio/frontend/src/features/settings/tabs/general-tab.tsx b/studio/frontend/src/features/settings/tabs/general-tab.tsx
index 9faf44af18e..a26c760edc5 100644
--- a/studio/frontend/src/features/settings/tabs/general-tab.tsx
+++ b/studio/frontend/src/features/settings/tabs/general-tab.tsx
@@ -59,6 +59,7 @@ import {
import { loadCloseToTray, updateCloseToTray } from "../api/close-to-tray";
import { loadLaunchAtLogin, updateLaunchAtLogin } from "../api/launch-at-login";
import { ChangePasswordDialog } from "../components/change-password-dialog";
+import { DesktopRepairControl } from "../components/desktop-repair-control";
import {
DesktopUpdateControl,
DesktopUpdateNote,
@@ -742,6 +743,10 @@ export function GeneralTab() {
{t("settings.general.resetPreferences.action")}
</Button>
</SettingsRow>
+ {/* Same section as the reset row: both rewrite state the user cannot easily put
+ back, and the desktop-only repair renders nothing on the web build, which
+ would leave a section header with no rows under it if it had its own. */}
+ <DesktopRepairControl />
</SettingsSection>
<Dialog open={confirmOpen} onOpenChange={setConfirmOpen}>
diff --git a/studio/frontend/src/features/settings/tabs/resources-tab.tsx b/studio/frontend/src/features/settings/tabs/resources-tab.tsx
index f3dfb7f54ba..ce40eec7e8a 100644
--- a/studio/frontend/src/features/settings/tabs/resources-tab.tsx
+++ b/studio/frontend/src/features/settings/tabs/resources-tab.tsx
@@ -19,6 +19,7 @@ import {
aggregateGpuMemoryTotalGb,
useSystemInfo,
type GpuDevice,
+ type SystemGpuInfo,
} from "@/hooks/use-system";
import { isTauri } from "@/lib/api-base";
import { copyToClipboard } from "@/lib/copy-to-clipboard";
@@ -193,6 +194,33 @@ function deviceOrdinal(device: GpuDevice): number | undefined {
return device.visible_ordinal ?? device.index;
}
+/** A GPU the OS enumerates that this PyTorch cannot open, from /api/system's
+ * `gpu.physical_devices`. `index` is the probe's own row number, vendor-local and
+ * NOT a pin. Declared here instead of widened onto SystemGpuInfo on purpose: these
+ * are display-only, and that shared type is what model fit budgets against and what
+ * the training device picker pins from, where an unusable card must never appear. */
+interface PhysicalGpuDevice {
+ vendor?: string;
+ index?: number;
+ name?: string | null;
+ memory_total_gb?: number | null;
+ source?: string;
+}
+
+/** Why the devices above are unusable, from /api/system's `gpu.mismatch`. Absent on
+ * a healthy host and on one that genuinely has no GPU, so its presence is the whole
+ * signal. `reason` is "torch_cpu_build" or "torch_cuda_unavailable". */
+interface GpuTorchMismatch {
+ reason?: string;
+ torch_version?: string | null;
+ physical_count?: number;
+}
+
+type GpuPhysicalInventory = SystemGpuInfo & {
+ physical_devices?: PhysicalGpuDevice[];
+ mismatch?: GpuTorchMismatch | null;
+};
+
export function ResourcesTab() {
const t = useT();
const liveUpdates = useSettingsPanelPrefsStore((s) => s.resourcesLiveUpdates);
@@ -401,6 +429,25 @@ export function ResourcesTab() {
: formatGiB(metrics.vramTotal);
// The placeholder's cpu backend and empty package list are not facts about the host either.
const hostReading = (value: string) => (hostUnread ? unknownLabel : value);
+ // systemInfo.gpu rather than displayedGpu: this describes the TRAINING view of the host,
+ // and a Vulkan llama.cpp makes displayedGpu fall back to the inference inventory, which is
+ // precisely the host that must be told. Gated on the read having settled.
+ const gpuInventory = hostUnread
+ ? null
+ : ((systemInfo.gpu ?? null) as GpuPhysicalInventory | null);
+ const gpuMismatch = gpuInventory?.mismatch ?? null;
+ const physicalDevices = gpuMismatch
+ ? (gpuInventory?.physical_devices ?? [])
+ : [];
+ // A CPU-only wheel is fixed by reinstalling torch, a dead runtime by the driver.
+ const gpuMismatchMessage = gpuMismatch
+ ? t(
+ gpuMismatch.reason === "torch_cpu_build"
+ ? "settings.resources.gpu.mismatchCpuBuild"
+ : "settings.resources.gpu.mismatchUnavailable",
+ { version: gpuMismatch.torch_version ?? unknownLabel },
+ )
+ : null;
return (
<div className="flex flex-col gap-6">
@@ -489,7 +536,9 @@ export function ResourcesTab() {
? metrics.vramUsageKnown
? `${formatGiB(metrics.vramUsed)} / ${vramCapacityLabel}`
: `${unknownLabel} / ${vramCapacityLabel}`
- : t("settings.resources.liveMonitor.noGpu")
+ : gpuMismatch
+ ? t("settings.resources.liveMonitor.gpuUnusable")
+ : t("settings.resources.liveMonitor.noGpu")
}
detail={
gpuUnknown
@@ -500,7 +549,9 @@ export function ResourcesTab() {
value: formatGiB(metrics.vramFree),
})
: unknownLabel
- : backendLabel
+ : gpuMismatch
+ ? t("settings.resources.liveMonitor.gpuUnusableDetail")
+ : backendLabel
}
percent={metrics.vramUsageKnown ? metrics.vramPercent : null}
/>
@@ -508,6 +559,33 @@ export function ResourcesTab() {
</SettingsSection>
<SettingsSection title={t("settings.resources.gpu.title")}>
+ {/* Physically present, torch-unusable cards. Their own block, above the
+ device rows and visually separate from them, because nothing here is
+ selectable: the rows below are what a model can be loaded onto. */}
+ {gpuMismatch ? (
+ <div className="flex flex-col gap-2 border-b border-border/60 py-3">
+ <p className="text-sm text-amber-600 dark:text-amber-400">
+ {gpuMismatchMessage}
+ </p>
+ {physicalDevices.map((device, index) => (
+ <div
+ key={`${device.vendor ?? "gpu"}-${device.index ?? index}-${device.name ?? ""}`}
+ className="flex min-w-0 items-center justify-between gap-4 text-xs text-muted-foreground opacity-70"
+ >
+ <span className="min-w-0 truncate">
+ {device.name ?? t("settings.resources.gpu.unknownDevice")}
+ </span>
+ <span className="shrink-0 font-mono tabular-nums">
+ {`${
+ isFiniteNumber(device.memory_total_gb)
+ ? formatGiB(device.memory_total_gb)
+ : unknownLabel
+ } · ${t("settings.resources.gpu.unusableDevice")}`}
+ </span>
+ </div>
+ ))}
+ </div>
+ ) : null}
{separateInferenceGpu && (
<div className="flex items-center justify-between gap-4 border-b border-border/60 py-3 text-sm">
<span className="text-muted-foreground">
@@ -617,6 +695,12 @@ export function ResourcesTab() {
</div>
);
})
+ ) : gpuMismatch ? (
+ // Not "no visible GPU": the cards are listed directly above. Its own branch, so the
+ // CPU-only host's line below stays exactly as it was.
+ <div className="py-3 text-sm text-muted-foreground">
+ {t("settings.resources.gpu.noUsableGpu")}
+ </div>
) : (
<div className="py-3 text-sm text-muted-foreground">
{gpuUnknown ? gpuUnknownLabel : t("settings.resources.gpu.noGpu")}
diff --git a/studio/frontend/src/hooks/tauri-repair-context.ts b/studio/frontend/src/hooks/tauri-repair-context.ts
new file mode 100644
index 00000000000..baf86048b44
--- /dev/null
+++ b/studio/frontend/src/hooks/tauri-repair-context.ts
@@ -0,0 +1,29 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import { createContext, useContext } from "react";
+
+/** What Settings needs from the backend controller to offer a manual repair. */
+export type TauriRepairController = {
+ /** Reruns the bundled installer over the managed environment, then restarts the backend. */
+ repairInstall: () => Promise<void>;
+ /**
+ * True when the desktop is attached to a backend someone started in a terminal.
+ *
+ * start_managed_repair refuses to touch that environment, but startRepair switches the
+ * whole shell to the repairing screen before the refusal comes back, so an offered
+ * action would drop a connected user on the repair-error screen. The consumer hides
+ * itself instead. The settings update row already suppresses its action this way.
+ */
+ isExternalServer: boolean;
+};
+
+// Null outside Tauri and on the startup screen, where the app shell has not mounted. The
+// consumer renders nothing rather than offering an action that cannot run.
+export const TauriRepairContext = createContext<TauriRepairController | null>(
+ null,
+);
+
+export function useTauriRepairController(): TauriRepairController | null {
+ return useContext(TauriRepairContext);
+}
diff --git a/studio/frontend/src/hooks/use-tauri-backend.ts b/studio/frontend/src/hooks/use-tauri-backend.ts
index ab7800c10ef..2612fee9426 100644
--- a/studio/frontend/src/hooks/use-tauri-backend.ts
+++ b/studio/frontend/src/hooks/use-tauri-backend.ts
@@ -164,6 +164,9 @@ export function useTauriBackend() {
const externalPollAbortedRef = useRef(false);
const authFailureRef = useRef<string | null>(getTauriAuthFailure());
const elevationResumeRef = useRef<"install" | "repair" | null>(null);
+ // Whether the repair in flight was asked to skip straight to the installer. Read back by
+ // approveElevation, which restarts the repair after the system packages land.
+ const forcedRepairRef = useRef(false);
const [tauriEventsReady, setTauriEventsReady] = useState(!isTauri);
// Read through rather than mirrored into state: the app-closing listener is registered
// inside the long event effect below, which cannot reach a setState from this render.
@@ -369,7 +372,14 @@ export function useTauriBackend() {
startingRef.current = false;
}
- async function startRepair() {
+ // `forceInstaller` runs the bundled installer without trying `studio update` first. The
+ // automatic callers leave it off, because an out-of-date venv is the common case. Settings'
+ // manual repair turns it on: an update reuses the environment it finds, so a venv whose
+ // PyTorch was replaced by a CPU-only wheel comes back from one still CPU-only.
+ async function startRepair(options?: { forceInstaller?: boolean }) {
+ const forceInstaller = options?.forceInstaller ?? false;
+ // Survives the elevation round trip: approveElevation resumes by calling this again.
+ forcedRepairRef.current = forceInstaller;
elevationResumeRef.current = null;
setCurrentStepIndex(-1);
setProgressDetail(null);
@@ -384,7 +394,7 @@ export function useTauriBackend() {
const { invoke } = await import("@tauri-apps/api/core");
try {
- await invoke("start_managed_repair");
+ await invoke("start_managed_repair", { forceInstaller });
setBackendStatus("starting");
elevationResumeRef.current = null;
@@ -467,6 +477,14 @@ export function useTauriBackend() {
}
const retry = useCallback(() => {
+ // Retry on a FORCED repair has to re-run that repair, not the preflight. The installer
+ // is transactional, so a failed attempt over an existing install restores the desktop-ready
+ // environment it found: checkInstallAndStart() then sees a ready install and restarts the
+ // same CPU-only backend the user pressed Repair about, and the button does nothing.
+ // Elevation resumes already preserve this; the error path did not.
+ const resumeForcedRepair =
+ statusRef.current === "repair-error" && forcedRepairRef.current;
+ forcedRepairRef.current = false;
clearAuthFailure();
clearServerStopIntent();
setError(null);
@@ -481,6 +499,10 @@ export function useTauriBackend() {
setIsExternalServer(false);
stopExternalServerPoll();
seenStepsRef.current.clear();
+ if (resumeForcedRepair) {
+ void startRepair({ forceInstaller: true });
+ return;
+ }
checkInstallAndStart();
}, []);
@@ -515,7 +537,7 @@ export function useTauriBackend() {
setProgressDetail(null);
elevationResumeRef.current = null;
if (resume === "repair") {
- await startRepair();
+ await startRepair({ forceInstaller: forcedRepairRef.current });
} else {
await startInstall();
}
@@ -724,5 +746,8 @@ export function useTauriBackend() {
currentStepIndex, progressDetail, startupMessage, elevationPackages,
startServer, stopServer, startInstall,
retry, retryInstall, approveElevation, copyDiagnostics,
+ // The same function startup uses, so a manual repair renders the same repairing screen
+ // and restarts the backend afterwards rather than leaving it stopped.
+ startRepair,
};
}
diff --git a/studio/frontend/src/i18n/locales/ar.ts b/studio/frontend/src/i18n/locales/ar.ts
index 65361275383..5f6fbbe0ffb 100644
--- a/studio/frontend/src/i18n/locales/ar.ts
+++ b/studio/frontend/src/i18n/locales/ar.ts
@@ -831,6 +831,16 @@ export const ar = {
openError: "تعذّر فتح المجلد",
copyError: "تعذّر نسخ المسار",
},
+ repairInstall: {
+ label: "إصلاح التثبيت",
+ description:
+ "يعيد تشغيل المثبّت على البيئة المُدارة. استخدمه إذا لم يتم اكتشاف وحدة معالجة الرسومات أو إذا تعذّر بدء التطبيق.",
+ action: "إصلاح التثبيت",
+ confirmTitle: "هل تريد إصلاح هذا التثبيت؟",
+ confirmDescription:
+ "يوقف الخادم ويعيد تشغيل المثبّت، الذي يعيد تثبيت PyTorch لوحدة معالجة الرسومات في هذا الجهاز. تُحفظ المحادثات والإعدادات. قد يستغرق ذلك عدة دقائق.",
+ confirmAction: "إصلاح الآن",
+ },
resetPreferences: {
sectionTitle: "منطقة الخطر",
label: "إعادة تعيين جميع التفضيلات المحلية",
@@ -1086,6 +1096,8 @@ export const ar = {
currentLoad: "الحمل الحالي",
free: "{value} متاح",
noGpu: "لا يوجد GPU مرئي",
+ gpuUnusable: "GPU غير قابل للاستخدام",
+ gpuUnusableDetail: "تم اكتشافه، لكن PyTorch لا يستطيع استخدامه",
},
gpu: {
title: "أجهزة GPU",
@@ -1094,6 +1106,12 @@ export const ar = {
detecting: "جارٍ البحث عن وحدات GPU...",
unreadable: "تعذّرت قراءة عتاد هذا الخادم.",
noGpu: "لم يُكتشف أي GPU مرئي. تُعرض موارد CPU فقط أعلاه.",
+ noUsableGpu: "لا يوجد على هذا الجهاز أي GPU يمكن لـ PyTorch استخدامه.",
+ mismatchCpuBuild:
+ "PyTorch إصدار للـ CPU فقط ({version})، لذا لا يمكن استخدام وحدات GPU أدناه. أصلح التثبيت لاستعادة دعم GPU.",
+ mismatchUnavailable:
+ "لا يستطيع PyTorch ({version}) تهيئة وحدات GPU أدناه، لذا لا يمكن استخدامها. تحقق من تعريف كرت الشاشة أو أصلح التثبيت.",
+ unusableDevice: "غير قابل للاستخدام",
unknownDevice: "GPU غير معروف",
deviceWithIndex: "GPU {index}",
vramUtilization: "VRAM",
diff --git a/studio/frontend/src/i18n/locales/de.ts b/studio/frontend/src/i18n/locales/de.ts
index b3a17b8e380..cfb79302325 100644
--- a/studio/frontend/src/i18n/locales/de.ts
+++ b/studio/frontend/src/i18n/locales/de.ts
@@ -855,6 +855,16 @@ export const de = {
openError: "Der Ordner konnte nicht geöffnet werden",
copyError: "Der Pfad konnte nicht kopiert werden",
},
+ repairInstall: {
+ label: "Installation reparieren",
+ description:
+ "Führt das Installationsprogramm erneut über die verwaltete Umgebung aus. Nützlich, wenn die GPU nicht erkannt wird oder die App nicht startet.",
+ action: "Installation reparieren",
+ confirmTitle: "Diese Installation reparieren?",
+ confirmDescription:
+ "Stoppt den Server und führt das Installationsprogramm erneut aus, wodurch PyTorch für die GPU dieses Rechners neu installiert wird. Chats und Einstellungen bleiben erhalten. Dies kann einige Minuten dauern.",
+ confirmAction: "Jetzt reparieren",
+ },
resetPreferences: {
sectionTitle: "Gefahrenzone",
label: "Alle lokalen Einstellungen zurücksetzen",
@@ -1112,6 +1122,8 @@ export const de = {
currentLoad: "Aktuelle Auslastung",
free: "{value} frei",
noGpu: "Keine sichtbare GPU",
+ gpuUnusable: "GPU nicht nutzbar",
+ gpuUnusableDetail: "Erkannt, aber PyTorch kann sie nicht nutzen",
},
gpu: {
title: "GPU-Geräte",
@@ -1121,6 +1133,12 @@ export const de = {
unreadable: "Die Hardware dieses Servers konnte nicht gelesen werden.",
noGpu:
"Keine sichtbare GPU erkannt. Oben werden nur die CPU-Ressourcen angezeigt.",
+ noUsableGpu: "Auf diesem Rechner ist keine GPU für PyTorch nutzbar.",
+ mismatchCpuBuild:
+ "PyTorch ist ein reiner CPU-Build ({version}), daher können die GPUs unten nicht genutzt werden. Reparieren Sie die Installation, um die GPU-Unterstützung wiederherzustellen.",
+ mismatchUnavailable:
+ "PyTorch ({version}) kann die GPUs unten nicht initialisieren, daher sind sie nicht nutzbar. Prüfen Sie den Grafiktreiber oder reparieren Sie die Installation.",
+ unusableDevice: "nicht nutzbar",
unknownDevice: "Unbekannte GPU",
deviceWithIndex: "GPU {index}",
vramUtilization: "VRAM",
diff --git a/studio/frontend/src/i18n/locales/en.ts b/studio/frontend/src/i18n/locales/en.ts
index 1715543fe07..741b0b166b8 100644
--- a/studio/frontend/src/i18n/locales/en.ts
+++ b/studio/frontend/src/i18n/locales/en.ts
@@ -832,6 +832,16 @@ export const en = {
openError: "Couldn't open the folder",
copyError: "Couldn't copy the path",
},
+ repairInstall: {
+ label: "Repair installation",
+ description:
+ "Reruns the installer over the managed environment. Use this if the GPU is not detected or the app will not start.",
+ action: "Repair install",
+ confirmTitle: "Repair this installation?",
+ confirmDescription:
+ "Stops the server and reruns the installer, which reinstalls PyTorch for this machine's GPU. Chats and settings are kept. This can take several minutes.",
+ confirmAction: "Repair now",
+ },
resetPreferences: {
sectionTitle: "Danger zone",
label: "Reset all local preferences",
@@ -1081,6 +1091,8 @@ export const en = {
currentLoad: "Current load",
free: "{value} free",
noGpu: "No visible GPU",
+ gpuUnusable: "GPU unusable",
+ gpuUnusableDetail: "Detected, but PyTorch cannot use it",
},
gpu: {
title: "GPU devices",
@@ -1089,6 +1101,12 @@ export const en = {
detecting: "Checking for GPUs...",
unreadable: "Could not read this server's hardware.",
noGpu: "No visible GPU detected. CPU-only resources are shown above.",
+ noUsableGpu: "No GPU on this machine is usable by PyTorch.",
+ mismatchCpuBuild:
+ "PyTorch is a CPU-only build ({version}), so the GPUs below cannot be used. Repair the installation to restore GPU support.",
+ mismatchUnavailable:
+ "PyTorch ({version}) cannot initialise the GPUs below, so they cannot be used. Check the GPU driver, or repair the installation.",
+ unusableDevice: "unusable",
unknownDevice: "Unknown GPU",
deviceWithIndex: "GPU {index}",
vramUtilization: "VRAM",
diff --git a/studio/frontend/src/i18n/locales/es.ts b/studio/frontend/src/i18n/locales/es.ts
index f7320158a07..7325b72fc11 100644
--- a/studio/frontend/src/i18n/locales/es.ts
+++ b/studio/frontend/src/i18n/locales/es.ts
@@ -850,6 +850,16 @@ export const es = {
openError: "No se pudo abrir la carpeta",
copyError: "No se pudo copiar la ruta",
},
+ repairInstall: {
+ label: "Reparar la instalación",
+ description:
+ "Vuelve a ejecutar el instalador sobre el entorno gestionado. Útil si no se detecta la GPU o si la aplicación no arranca.",
+ action: "Reparar instalación",
+ confirmTitle: "¿Reparar esta instalación?",
+ confirmDescription:
+ "Detiene el servidor y vuelve a ejecutar el instalador, que reinstala PyTorch para la GPU de este equipo. Se conservan las conversaciones y los ajustes. Puede tardar varios minutos.",
+ confirmAction: "Reparar ahora",
+ },
resetPreferences: {
sectionTitle: "Zona de peligro",
label: "Restablecer todas las preferencias locales",
@@ -1107,6 +1117,8 @@ export const es = {
currentLoad: "Carga actual",
free: "Libre: {value}",
noGpu: "No hay GPU visible",
+ gpuUnusable: "GPU no utilizable",
+ gpuUnusableDetail: "Detectada, pero PyTorch no puede usarla",
},
gpu: {
title: "Dispositivos GPU",
@@ -1116,6 +1128,12 @@ export const es = {
unreadable: "No se pudo leer el hardware de este servidor.",
noGpu:
"No se detectó ninguna GPU visible. Arriba se muestran los recursos solo de CPU.",
+ noUsableGpu: "Ninguna GPU de esta máquina se puede usar con PyTorch.",
+ mismatchCpuBuild:
+ "PyTorch es una compilación solo para CPU ({version}), por lo que las GPU de abajo no se pueden usar. Repare la instalación para restaurar la compatibilidad con GPU.",
+ mismatchUnavailable:
+ "PyTorch ({version}) no puede inicializar las GPU de abajo, por lo que no se pueden usar. Revise el controlador de la GPU o repare la instalación.",
+ unusableDevice: "no utilizable",
unknownDevice: "GPU desconocida",
deviceWithIndex: "GPU {index}",
vramUtilization: "VRAM",
diff --git a/studio/frontend/src/i18n/locales/fr.ts b/studio/frontend/src/i18n/locales/fr.ts
index a10d7ac6d6a..475fde15a8f 100644
--- a/studio/frontend/src/i18n/locales/fr.ts
+++ b/studio/frontend/src/i18n/locales/fr.ts
@@ -853,6 +853,16 @@ export const fr = {
openError: "Impossible d'ouvrir le dossier",
copyError: "Impossible de copier le chemin",
},
+ repairInstall: {
+ label: "Réparer l'installation",
+ description:
+ "Relance le programme d'installation sur l'environnement géré. Utile si le GPU n'est pas détecté ou si l'application ne démarre pas.",
+ action: "Réparer l'installation",
+ confirmTitle: "Réparer cette installation ?",
+ confirmDescription:
+ "Arrête le serveur et relance le programme d'installation, qui réinstalle PyTorch pour le GPU de cette machine. Les discussions et les paramètres sont conservés. Cela peut prendre plusieurs minutes.",
+ confirmAction: "Réparer maintenant",
+ },
resetPreferences: {
sectionTitle: "Zone de danger",
label: "Réinitialiser toutes les préférences locales",
@@ -1111,6 +1121,8 @@ export const fr = {
currentLoad: "Charge actuelle",
free: "Disponible : {value}",
noGpu: "Aucun GPU visible",
+ gpuUnusable: "GPU inutilisable",
+ gpuUnusableDetail: "Détecté, mais PyTorch ne peut pas l'utiliser",
},
gpu: {
title: "Périphériques GPU",
@@ -1120,6 +1132,12 @@ export const fr = {
unreadable: "Impossible de lire le matériel de ce serveur.",
noGpu:
"Aucun GPU visible n'a été détecté. Seules les ressources du CPU sont affichées ci-dessus.",
+ noUsableGpu: "Aucun GPU de cette machine n'est utilisable par PyTorch.",
+ mismatchCpuBuild:
+ "PyTorch est une version CPU uniquement ({version}), les GPU ci-dessous ne peuvent donc pas être utilisés. Réparez l'installation pour rétablir la prise en charge du GPU.",
+ mismatchUnavailable:
+ "PyTorch ({version}) ne parvient pas à initialiser les GPU ci-dessous, ils ne peuvent donc pas être utilisés. Vérifiez le pilote graphique ou réparez l'installation.",
+ unusableDevice: "inutilisable",
unknownDevice: "GPU inconnu",
deviceWithIndex: "GPU {index}",
vramUtilization: "VRAM",
diff --git a/studio/frontend/src/i18n/locales/hi.ts b/studio/frontend/src/i18n/locales/hi.ts
index fc72c8106f5..b5f71690cc0 100644
--- a/studio/frontend/src/i18n/locales/hi.ts
+++ b/studio/frontend/src/i18n/locales/hi.ts
@@ -834,6 +834,16 @@ export const hi = {
openError: "फ़ोल्डर नहीं खोला जा सका",
copyError: "पथ कॉपी नहीं किया जा सका",
},
+ repairInstall: {
+ label: "इंस्टॉलेशन ठीक करें",
+ description:
+ "प्रबंधित परिवेश पर इंस्टॉलर को फिर से चलाता है। यदि GPU का पता नहीं चल रहा है या ऐप शुरू नहीं हो रहा है तो इसका उपयोग करें।",
+ action: "इंस्टॉल ठीक करें",
+ confirmTitle: "क्या यह इंस्टॉलेशन ठीक करना है?",
+ confirmDescription:
+ "सर्वर रोकता है और इंस्टॉलर फिर से चलाता है, जो इस मशीन के GPU के लिए PyTorch को दोबारा इंस्टॉल करता है। चैट और सेटिंग्स सुरक्षित रहती हैं। इसमें कई मिनट लग सकते हैं।",
+ confirmAction: "अभी ठीक करें",
+ },
resetPreferences: {
sectionTitle: "खतरनाक क्षेत्र",
label: "सभी स्थानीय प्राथमिकताएं रीसेट करें",
@@ -1089,6 +1099,8 @@ export const hi = {
currentLoad: "वर्तमान लोड",
free: "{value} खाली",
noGpu: "कोई दृश्यमान GPU नहीं",
+ gpuUnusable: "GPU अनुपयोगी",
+ gpuUnusableDetail: "पहचाना गया, लेकिन PyTorch इसका उपयोग नहीं कर सकता",
},
gpu: {
title: "GPU डिवाइस",
@@ -1097,6 +1109,12 @@ export const hi = {
detecting: "GPU खोजे जा रहे हैं...",
unreadable: "इस सर्वर का हार्डवेयर नहीं पढ़ा जा सका।",
noGpu: "कोई दृश्यमान GPU नहीं मिला। केवल-CPU संसाधन ऊपर दिखाए गए हैं।",
+ noUsableGpu: "इस मशीन का कोई भी GPU PyTorch के लिए उपयोग योग्य नहीं है।",
+ mismatchCpuBuild:
+ "PyTorch केवल-CPU बिल्ड ({version}) है, इसलिए नीचे दिए गए GPU उपयोग नहीं किए जा सकते। GPU समर्थन बहाल करने के लिए इंस्टॉलेशन की मरम्मत करें।",
+ mismatchUnavailable:
+ "PyTorch ({version}) नीचे दिए गए GPU को आरंभ नहीं कर पा रहा है, इसलिए उनका उपयोग नहीं किया जा सकता। GPU ड्राइवर जाँचें या इंस्टॉलेशन की मरम्मत करें।",
+ unusableDevice: "अनुपयोगी",
unknownDevice: "अज्ञात GPU",
deviceWithIndex: "GPU {index}",
vramUtilization: "VRAM",
diff --git a/studio/frontend/src/i18n/locales/it.ts b/studio/frontend/src/i18n/locales/it.ts
index c16df90eb32..17ef7f6e533 100644
--- a/studio/frontend/src/i18n/locales/it.ts
+++ b/studio/frontend/src/i18n/locales/it.ts
@@ -825,6 +825,16 @@ export const it = {
openError: "Impossibile aprire la cartella",
copyError: "Impossibile copiare il percorso",
},
+ repairInstall: {
+ label: "Ripara l'installazione",
+ description:
+ "Riesegue il programma di installazione sull'ambiente gestito. Utile se la GPU non viene rilevata o se l'app non si avvia.",
+ action: "Ripara installazione",
+ confirmTitle: "Riparare questa installazione?",
+ confirmDescription:
+ "Arresta il server e riesegue il programma di installazione, che reinstalla PyTorch per la GPU di questa macchina. Le chat e le impostazioni vengono mantenute. L'operazione può richiedere alcuni minuti.",
+ confirmAction: "Ripara ora",
+ },
resetPreferences: {
sectionTitle: "Zona pericolosa",
label: "Reimposta tutte le preferenze locali",
@@ -1078,6 +1088,8 @@ export const it = {
currentLoad: "Carico attuale",
free: "Disponibili: {value}",
noGpu: "Nessuna GPU visibile",
+ gpuUnusable: "GPU non utilizzabile",
+ gpuUnusableDetail: "Rilevata, ma PyTorch non può usarla",
},
gpu: {
title: "Dispositivi GPU",
@@ -1087,6 +1099,12 @@ export const it = {
unreadable: "Impossibile leggere l'hardware di questo server.",
noGpu:
"Nessuna GPU visibile rilevata. Sopra sono mostrate le risorse della sola CPU.",
+ noUsableGpu: "Nessuna GPU di questa macchina è utilizzabile da PyTorch.",
+ mismatchCpuBuild:
+ "PyTorch è una build solo CPU ({version}), quindi le GPU sottostanti non possono essere usate. Ripara l'installazione per ripristinare il supporto GPU.",
+ mismatchUnavailable:
+ "PyTorch ({version}) non riesce a inizializzare le GPU sottostanti, quindi non possono essere usate. Controlla il driver della GPU o ripara l'installazione.",
+ unusableDevice: "non utilizzabile",
unknownDevice: "GPU sconosciuta",
deviceWithIndex: "GPU {index}",
vramUtilization: "VRAM",
diff --git a/studio/frontend/src/i18n/locales/ja.ts b/studio/frontend/src/i18n/locales/ja.ts
index 623d793cc6b..26c866c8ee2 100644
--- a/studio/frontend/src/i18n/locales/ja.ts
+++ b/studio/frontend/src/i18n/locales/ja.ts
@@ -818,6 +818,16 @@ export const ja = {
openError: "フォルダを開けませんでした",
copyError: "パスをコピーできませんでした",
},
+ repairInstall: {
+ label: "インストールを修復",
+ description:
+ "管理環境に対してインストーラーを再実行します。GPU が検出されない場合やアプリが起動しない場合に使用します。",
+ action: "インストールを修復",
+ confirmTitle: "このインストールを修復しますか?",
+ confirmDescription:
+ "サーバーを停止してインストーラーを再実行し、このマシンの GPU に合わせて PyTorch を再インストールします。チャットと設定は保持されます。数分かかる場合があります。",
+ confirmAction: "今すぐ修復",
+ },
resetPreferences: {
sectionTitle: "危険ゾーン",
label: "すべてのローカル設定をリセット",
@@ -1067,6 +1077,8 @@ export const ja = {
currentLoad: "現在の負荷",
free: "{value} 空き",
noGpu: "認識できる GPU がありません",
+ gpuUnusable: "GPU を利用できません",
+ gpuUnusableDetail: "検出されましたが、PyTorch から利用できません",
},
gpu: {
title: "GPU デバイス",
@@ -1076,6 +1088,12 @@ export const ja = {
unreadable: "このサーバーのハードウェアを読み取れませんでした。",
noGpu:
"利用可能な GPU が検出されませんでした。CPU のみの環境向けのリソース情報は上に表示されています。",
+ noUsableGpu: "このマシンの GPU はいずれも PyTorch から利用できません。",
+ mismatchCpuBuild:
+ "PyTorch は CPU 専用ビルド ({version}) のため、下の GPU は利用できません。インストールを修復すると GPU サポートが復元されます。",
+ mismatchUnavailable:
+ "PyTorch ({version}) は下の GPU を初期化できないため、利用できません。GPU ドライバーを確認するか、インストールを修復してください。",
+ unusableDevice: "利用不可",
unknownDevice: "不明な GPU",
deviceWithIndex: "GPU {index}",
vramUtilization: "VRAM",
diff --git a/studio/frontend/src/i18n/locales/ko.ts b/studio/frontend/src/i18n/locales/ko.ts
index b2e81f482e2..f5a17b5bceb 100644
--- a/studio/frontend/src/i18n/locales/ko.ts
+++ b/studio/frontend/src/i18n/locales/ko.ts
@@ -827,6 +827,16 @@ export const ko = {
openError: "폴더를 열지 못했습니다",
copyError: "경로를 복사하지 못했습니다",
},
+ repairInstall: {
+ label: "설치 복구",
+ description:
+ "관리 환경에 대해 설치 프로그램을 다시 실행합니다. GPU가 감지되지 않거나 앱이 시작되지 않을 때 사용하세요.",
+ action: "설치 복구",
+ confirmTitle: "이 설치를 복구할까요?",
+ confirmDescription:
+ "서버를 중지하고 설치 프로그램을 다시 실행하여 이 컴퓨터의 GPU에 맞는 PyTorch를 다시 설치합니다. 채팅과 설정은 유지됩니다. 몇 분 정도 걸릴 수 있습니다.",
+ confirmAction: "지금 복구",
+ },
resetPreferences: {
sectionTitle: "위험 구역",
label: "모든 로컬 환경설정 재설정",
@@ -1082,6 +1092,8 @@ export const ko = {
currentLoad: "현재 부하",
free: "{value} 여유",
noGpu: "인식되는 GPU 없음",
+ gpuUnusable: "GPU 사용 불가",
+ gpuUnusableDetail: "감지되었지만 PyTorch에서 사용할 수 없습니다",
},
gpu: {
title: "GPU 장치",
@@ -1090,6 +1102,12 @@ export const ko = {
detecting: "GPU를 확인하는 중...",
unreadable: "이 서버의 하드웨어를 읽을 수 없습니다.",
noGpu: "인식되는 GPU가 없습니다. 위에는 CPU 관련 리소스만 표시됩니다.",
+ noUsableGpu: "이 컴퓨터의 GPU 중 PyTorch에서 사용할 수 있는 것이 없습니다.",
+ mismatchCpuBuild:
+ "PyTorch가 CPU 전용 빌드({version})이므로 아래 GPU를 사용할 수 없습니다. 설치를 복구하면 GPU 지원이 복원됩니다.",
+ mismatchUnavailable:
+ "PyTorch({version})가 아래 GPU를 초기화하지 못해 사용할 수 없습니다. GPU 드라이버를 확인하거나 설치를 복구하세요.",
+ unusableDevice: "사용 불가",
unknownDevice: "알 수 없는 GPU",
deviceWithIndex: "GPU {index}",
vramUtilization: "VRAM",
diff --git a/studio/frontend/src/i18n/locales/pt-br.ts b/studio/frontend/src/i18n/locales/pt-br.ts
index c5d07a4a457..1885b182f8b 100644
--- a/studio/frontend/src/i18n/locales/pt-br.ts
+++ b/studio/frontend/src/i18n/locales/pt-br.ts
@@ -842,6 +842,16 @@ export const ptBR = {
openError: "Não foi possível abrir a pasta",
copyError: "Não foi possível copiar o caminho",
},
+ repairInstall: {
+ label: "Reparar a instalação",
+ description:
+ "Executa o instalador novamente sobre o ambiente gerenciado. Útil se a GPU não for detectada ou se o aplicativo não iniciar.",
+ action: "Reparar instalação",
+ confirmTitle: "Reparar esta instalação?",
+ confirmDescription:
+ "Para o servidor e executa o instalador novamente, que reinstala o PyTorch para a GPU desta máquina. As conversas e as configurações são mantidas. Isso pode levar alguns minutos.",
+ confirmAction: "Reparar agora",
+ },
resetPreferences: {
sectionTitle: "Zona de perigo",
label: "Redefinir todas as preferências locais",
@@ -1096,6 +1106,8 @@ export const ptBR = {
currentLoad: "Carga atual",
free: "{value} livres",
noGpu: "Nenhuma GPU visível",
+ gpuUnusable: "GPU inutilizável",
+ gpuUnusableDetail: "Detectada, mas o PyTorch não consegue usá-la",
},
gpu: {
title: "Dispositivos de GPU",
@@ -1104,6 +1116,12 @@ export const ptBR = {
detecting: "Procurando GPUs...",
unreadable: "Não foi possível ler o hardware deste servidor.",
noGpu: "Nenhuma GPU visível detectada. Os recursos somente CPU aparecem acima.",
+ noUsableGpu: "Nenhuma GPU desta máquina pode ser usada pelo PyTorch.",
+ mismatchCpuBuild:
+ "O PyTorch é uma compilação somente CPU ({version}), então as GPUs abaixo não podem ser usadas. Repare a instalação para restaurar o suporte a GPU.",
+ mismatchUnavailable:
+ "O PyTorch ({version}) não consegue inicializar as GPUs abaixo, então elas não podem ser usadas. Verifique o driver da GPU ou repare a instalação.",
+ unusableDevice: "inutilizável",
unknownDevice: "GPU desconhecida",
deviceWithIndex: "GPU {index}",
vramUtilization: "VRAM",
diff --git a/studio/frontend/src/i18n/locales/ru.ts b/studio/frontend/src/i18n/locales/ru.ts
index e3e3be3f37b..36b73939726 100644
--- a/studio/frontend/src/i18n/locales/ru.ts
+++ b/studio/frontend/src/i18n/locales/ru.ts
@@ -835,6 +835,16 @@ export const ru = {
openError: "Не удалось открыть папку",
copyError: "Не удалось скопировать путь",
},
+ repairInstall: {
+ label: "Восстановить установку",
+ description:
+ "Повторно запускает установщик для управляемой среды. Пригодится, если графический процессор не определяется или приложение не запускается.",
+ action: "Восстановить установку",
+ confirmTitle: "Восстановить эту установку?",
+ confirmDescription:
+ "Останавливает сервер и повторно запускает установщик, который переустанавливает PyTorch для графического процессора этого компьютера. Чаты и настройки сохраняются. Это может занять несколько минут.",
+ confirmAction: "Восстановить",
+ },
resetPreferences: {
sectionTitle: "Опасная зона",
label: "Сбросить все локальные настройки",
@@ -1091,6 +1101,8 @@ export const ru = {
currentLoad: "Текущая нагрузка",
free: "{value} свободно",
noGpu: "GPU не обнаружен",
+ gpuUnusable: "GPU недоступен",
+ gpuUnusableDetail: "Обнаружен, но PyTorch не может его использовать",
},
gpu: {
title: "Устройства GPU",
@@ -1100,6 +1112,12 @@ export const ru = {
unreadable: "Не удалось прочитать оборудование этого сервера.",
noGpu:
"Доступные GPU не обнаружены. Выше показаны ресурсы только для CPU.",
+ noUsableGpu: "Ни один GPU на этой машине недоступен для PyTorch.",
+ mismatchCpuBuild:
+ "PyTorch собран только для CPU ({version}), поэтому GPU ниже использовать нельзя. Восстановите установку, чтобы вернуть поддержку GPU.",
+ mismatchUnavailable:
+ "PyTorch ({version}) не может инициализировать GPU ниже, поэтому использовать их нельзя. Проверьте драйвер видеокарты или восстановите установку.",
+ unusableDevice: "недоступен",
unknownDevice: "Неизвестный GPU",
deviceWithIndex: "GPU {index}",
vramUtilization: "VRAM",
diff --git a/studio/frontend/src/i18n/locales/zh-CN.ts b/studio/frontend/src/i18n/locales/zh-CN.ts
index dcacd0fb1bb..4789c3b9bdf 100644
--- a/studio/frontend/src/i18n/locales/zh-CN.ts
+++ b/studio/frontend/src/i18n/locales/zh-CN.ts
@@ -811,6 +811,16 @@ export const zhCN = {
openError: "无法打开文件夹",
copyError: "无法复制路径",
},
+ repairInstall: {
+ label: "修复安装",
+ description:
+ "对托管环境重新运行安装程序。如果未检测到 GPU 或应用无法启动,可使用此功能。",
+ action: "修复安装",
+ confirmTitle: "要修复此安装吗?",
+ confirmDescription:
+ "停止服务器并重新运行安装程序,为本机 GPU 重新安装 PyTorch。聊天记录和设置会保留。这可能需要几分钟。",
+ confirmAction: "立即修复",
+ },
resetPreferences: {
sectionTitle: "危险区域",
label: "重置所有本地偏好设置",
@@ -1061,6 +1071,8 @@ export const zhCN = {
currentLoad: "当前负载",
free: "{value} 可用",
noGpu: "无可见 GPU",
+ gpuUnusable: "GPU 不可用",
+ gpuUnusableDetail: "已检测到,但 PyTorch 无法使用",
},
gpu: {
title: "GPU 设备",
@@ -1069,6 +1081,10 @@ export const zhCN = {
detecting: "正在检查 GPU...",
unreadable: "无法读取此服务器的硬件信息。",
noGpu: "未检测到可见 GPU。上方仅显示 CPU 资源。",
+ noUsableGpu: "本机没有 PyTorch 可用的 GPU。",
+ mismatchCpuBuild: "PyTorch 是仅 CPU 版本({version}),因此无法使用下方的 GPU。修复安装即可恢复 GPU 支持。",
+ mismatchUnavailable: "PyTorch({version})无法初始化下方的 GPU,因此无法使用。请检查显卡驱动,或修复安装。",
+ unusableDevice: "不可用",
unknownDevice: "未知 GPU",
deviceWithIndex: "GPU {index}",
vramUtilization: "VRAM",
diff --git a/studio/frontend/tests/forced-repair-retry.test.ts b/studio/frontend/tests/forced-repair-retry.test.ts
new file mode 100644
index 00000000000..1310683a03a
--- /dev/null
+++ b/studio/frontend/tests/forced-repair-retry.test.ts
@@ -0,0 +1,108 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+// Retry, on a repair the user forced from Settings, has to re-run that repair.
+//
+// startRepair({ forceInstaller: true }) skips `studio update` and runs the bundled
+// installer, which is the only thing that replaces a CPU-only wheel. The installer is
+// transactional: a failed attempt over an existing install restores the desktop-ready
+// environment it found. So the generic retry path -- clear state, run the preflight --
+// finds a ready install and restarts the very backend the user pressed Repair about.
+// The elevation resume already carried the flag; the error path did not.
+//
+// The hook cannot be rendered here, so the callback is lifted by regex and evaluated,
+// the way gpu-torch-mismatch.test.ts and system-status-verdict.test.ts do beside it.
+
+import assert from "node:assert/strict";
+import { readFile } from "node:fs/promises";
+import test from "node:test";
+
+const src = await readFile(
+ new URL("../src/hooks/use-tauri-backend.ts", import.meta.url),
+ "utf8",
+);
+
+function lift(pattern: RegExp, what: string): string {
+ const found = pattern.exec(src);
+ assert.ok(found, `could not find ${what} in use-tauri-backend.ts`);
+ return found[0];
+}
+
+const body = lift(
+ /const retry = useCallback\(\(\) => \{[\s\S]*?\n \}, \[\]\);/,
+ "the retry callback",
+);
+
+type Run = { repairs: boolean[]; preflights: number; forcedAfter: boolean };
+
+function runRetry(status: string, forced: boolean): Run {
+ const repairs: boolean[] = [];
+ const forcedRepairRef = { current: forced };
+ let preflights = 0;
+ const noop = () => {};
+ const scope = {
+ statusRef: { current: status },
+ forcedRepairRef,
+ startRepair: (options?: { forceInstaller?: boolean }) => {
+ // The real one records the flag first; the fake mirrors that so the test can see
+ // whether it survived the state reset that now runs ahead of the call.
+ forcedRepairRef.current = options?.forceInstaller === true;
+ repairs.push(options?.forceInstaller === true);
+ return Promise.resolve();
+ },
+ checkInstallAndStart: () => {
+ preflights += 1;
+ },
+ elevationResumeRef: { current: null as string | null },
+ startingRef: { current: true },
+ portRef: { current: 1 as number | null },
+ startTimedOutRef: { current: true },
+ seenStepsRef: { current: new Set<string>() },
+ useCallback: (fn: unknown) => fn,
+ clearAuthFailure: noop,
+ clearServerStopIntent: noop,
+ setError: noop,
+ setLogs: noop,
+ setCurrentStepIndex: noop,
+ setProgressDetail: noop,
+ setElevationPackages: noop,
+ setIsExternalServer: noop,
+ stopExternalServerPoll: noop,
+ };
+ const keys = Object.keys(scope);
+ new Function(
+ ...keys,
+ `${body.replace(/^const retry = /, "return ")}`.replace(/;\s*$/, ";"),
+ )(...keys.map((key) => (scope as Record<string, unknown>)[key]))();
+ return { repairs, preflights, forcedAfter: forcedRepairRef.current };
+}
+
+test("retry after a forced repair re-runs the forced repair", () => {
+ const run = runRetry("repair-error", true);
+ assert.deepEqual(run.repairs, [true], "the retry must force the installer again");
+ assert.equal(run.preflights, 0, "the preflight would restart the same broken backend");
+ // The ref is read into a local and cleared with the rest of the state, so the flag
+ // reaches startRepair (which sets it again) without surviving as a latch.
+ assert.equal(run.forcedAfter, true, "startRepair re-arms it for the elevation resume");
+});
+
+test("retry after an automatic repair still runs the preflight", () => {
+ // The automatic callers leave forceInstaller off, and an out-of-date venv really is
+ // the common case there: nothing about that path changed.
+ const run = runRetry("repair-error", false);
+ assert.deepEqual(run.repairs, []);
+ assert.equal(run.preflights, 1);
+});
+
+test("retry from any other failure is untouched", () => {
+ for (const status of ["error", "install-error", "not-installed", "stopped"]) {
+ const run = runRetry(status, true);
+ assert.deepEqual(run.repairs, [], `${status} must not start a repair`);
+ assert.equal(run.preflights, 1, `${status} must still run the preflight`);
+ assert.equal(
+ run.forcedAfter,
+ false,
+ `${status} leaves the generic path, so the forced flag must not survive it`,
+ );
+ }
+});
diff --git a/studio/frontend/tests/gpu-torch-mismatch.test.ts b/studio/frontend/tests/gpu-torch-mismatch.test.ts
new file mode 100644
index 00000000000..371dde7a164
--- /dev/null
+++ b/studio/frontend/tests/gpu-torch-mismatch.test.ts
@@ -0,0 +1,451 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+// What the UI says about a host whose GPUs PyTorch cannot use.
+//
+// A Windows in-app update that resolved torch from PyPI left a 2.11.0+cpu wheel beside two
+// working RTX A4000s (#8473). The backend now reports those cards in gpu.physical_devices
+// with a gpu.mismatch reason, and three places have to stop saying the opposite: the System
+// tab's VRAM tile and GPU section, the sidebar's Train hint, and videoNavHint.
+//
+// The derivations live in .tsx files that pull in the whole app, so they are lifted by
+// regex and evaluated, as system-status-verdict.test.ts does beside them.
+
+import assert from "node:assert/strict";
+import { readFile } from "node:fs/promises";
+import test from "node:test";
+
+import { registerBundlerResolver } from "./helpers/kit.ts";
+
+registerBundlerResolver();
+
+const { videoNavHint } = await import("../src/config/hardware-verdict.ts");
+const { en } = await import("../src/i18n/locales/en.ts");
+
+const tabSrc = await readFile(
+ new URL("../src/features/settings/tabs/resources-tab.tsx", import.meta.url),
+ "utf8",
+);
+const sidebarSrc = await readFile(
+ new URL("../src/components/app-sidebar.tsx", import.meta.url),
+ "utf8",
+);
+
+function lift(
+ src: string,
+ pattern: RegExp,
+ what: string,
+ where: string,
+): string {
+ const found = pattern.exec(src);
+ assert.ok(found, `could not find ${what} in ${where}`);
+ return found[0];
+}
+
+const t = (key: string) => key;
+
+const CPU_BUILD = "settings.resources.gpu.mismatchCpuBuild";
+const UNAVAILABLE = "settings.resources.gpu.mismatchUnavailable";
+const NO_USABLE_GPU = "settings.resources.gpu.noUsableGpu";
+const NO_GPU = "settings.resources.gpu.noGpu";
+const UNKNOWN = "settings.resources.environment.unknown";
+
+// gpuInventory carries a TS cast, so it is asserted on as source below and supplied here
+// as an input instead.
+const derivation = [
+ lift(
+ tabSrc,
+ /const gpuMismatch = [\s\S]*?;/,
+ "gpuMismatch",
+ "resources-tab.tsx",
+ ),
+ lift(
+ tabSrc,
+ /const physicalDevices = [\s\S]*?;/,
+ "physicalDevices",
+ "resources-tab.tsx",
+ ),
+ lift(
+ tabSrc,
+ /const gpuMismatchMessage = [\s\S]*?;/,
+ "gpuMismatchMessage",
+ "resources-tab.tsx",
+ ),
+].join("\n");
+
+interface Inventory {
+ mismatch?: { reason?: string; torch_version?: string | null } | null;
+ physical_devices?: { name?: string }[];
+}
+
+function mismatchFor(gpuInventory: Inventory | null) {
+ const run = new Function(
+ "gpuInventory",
+ "t",
+ "unknownLabel",
+ `${derivation}
+ return { gpuMismatch, physicalDevices, gpuMismatchMessage };`,
+ );
+ return run(gpuInventory, t, UNKNOWN) as {
+ gpuMismatch: { reason?: string } | null;
+ physicalDevices: { name?: string }[];
+ gpuMismatchMessage: string | null;
+ };
+}
+
+test("a CPU-only wheel and a dead accelerator wheel get different sentences", () => {
+ const cpuBuild = mismatchFor({
+ mismatch: { reason: "torch_cpu_build", torch_version: "2.11.0+cpu" },
+ physical_devices: [
+ { name: "NVIDIA RTX A4000" },
+ { name: "NVIDIA RTX A4000" },
+ ],
+ });
+ assert.equal(cpuBuild.gpuMismatchMessage, CPU_BUILD);
+ assert.equal(cpuBuild.physicalDevices.length, 2);
+
+ // Reinstalling torch is the wrong advice for a healthy wheel whose runtime will not
+ // start, so the two reasons must not collapse into one string.
+ const dead = mismatchFor({
+ mismatch: {
+ reason: "torch_cuda_unavailable",
+ torch_version: "2.6.0+cu124",
+ },
+ physical_devices: [{ name: "NVIDIA RTX A4000" }],
+ });
+ assert.equal(dead.gpuMismatchMessage, UNAVAILABLE);
+});
+
+test("a healthy host, and one that really has no GPU, get no banner at all", () => {
+ for (const inventory of [
+ null,
+ {},
+ { mismatch: null },
+ ] as (Inventory | null)[]) {
+ const out = mismatchFor(inventory);
+ assert.equal(out.gpuMismatch, null);
+ assert.equal(out.gpuMismatchMessage, null);
+ assert.deepEqual(out.physicalDevices, []);
+ }
+ const strayRows = mismatchFor({
+ physical_devices: [{ name: "NVIDIA RTX A4000" }],
+ });
+ assert.deepEqual(strayRows.physicalDevices, []);
+});
+
+test("the verdict is taken from a settled read only, and from the training view", () => {
+ // The placeholder useSystemInfo starts from is shaped like a CPU-only host, so a banner
+ // derived from it would accuse a host nobody has measured yet.
+ const inventory = lift(
+ tabSrc,
+ /const gpuInventory = [\s\S]*?;\n/,
+ "gpuInventory",
+ "resources-tab.tsx",
+ );
+ assert.match(
+ inventory,
+ /hostUnread\s*\n?\s*\?\s*null/,
+ "gated on the read having settled",
+ );
+ // systemInfo.gpu, NOT displayedGpu: a Vulkan llama.cpp makes displayedGpu fall back to the
+ // inference inventory, and that host is exactly the second report in #8473.
+ assert.match(inventory, /systemInfo\.gpu/);
+ assert.doesNotMatch(inventory, /displayedGpu/);
+});
+
+test("the GPU section stops telling this host there is no GPU", () => {
+ assert.match(
+ tabSrc,
+ new RegExp(`\\) : gpuMismatch \\? \\([\\s\\S]*?t\\("${NO_USABLE_GPU}"\\)`),
+ "a host with unusable cards gets its own line",
+ );
+ assert.match(
+ tabSrc,
+ /gpuUnknown \? gpuUnknownLabel : t\("settings\.resources\.gpu\.noGpu"\)/,
+ "and a host that really has no GPU still gets the CPU-only one",
+ );
+});
+
+test("the VRAM tile stops reading as a CPU-only host", () => {
+ const tiles = tabSrc.match(/<MetricTile\b[\s\S]*?\/>/g) ?? [];
+ const vram = tiles.find((tile) => tile.includes("liveMonitor.vram"));
+ assert.ok(vram, "the VRAM tile");
+ const mismatchAt = vram.indexOf("liveMonitor.gpuUnusable");
+ const noGpuAt = vram.indexOf("liveMonitor.noGpu");
+ assert.ok(mismatchAt > -1, "the tile has a mismatch state");
+ assert.ok(noGpuAt > -1, "and still has the CPU-only state");
+ assert.ok(mismatchAt < noGpuAt, "the mismatch state is reached first");
+});
+
+test("the physically detected cards are shown, and never offered as devices", () => {
+ // The banner renders physicalDevices and the selectable rows metrics.devices. If the
+ // banner ever read metrics.devices the two would merge, which the field split prevents.
+ const banner = lift(
+ tabSrc,
+ /\{gpuMismatch \? \(\n[\s\S]*?\n\s*\) : null\}/,
+ "the mismatch banner",
+ "resources-tab.tsx",
+ );
+ assert.match(banner, /physicalDevices\.map/);
+ assert.doesNotMatch(banner, /metrics\.devices/);
+ assert.match(banner, /settings\.resources\.gpu\.unusableDevice/);
+});
+
+
+test("videoNavHint stops telling a two-GPU host to get a GPU", () => {
+ for (const reason of ["torch_cpu_build", "torch_cuda_unavailable"]) {
+ const hint = videoNavHint(true, reason);
+ assert.ok(hint, `${reason} explains the disabled Video row`);
+ assert.doesNotMatch(
+ hint,
+ /needs an NVIDIA or AMD GPU/,
+ `${reason} is not a missing-GPU host`,
+ );
+ assert.match(hint, /PyTorch/, `${reason} names what is actually wrong`);
+ assert.equal(videoNavHint(false, reason), undefined);
+ }
+ // And the genuine no-GPU host keeps the sentence that is true for it.
+ assert.equal(
+ videoNavHint(true, "no_gpu"),
+ "Video generation needs an NVIDIA or AMD GPU.",
+ );
+});
+
+test("the sidebar's Train hint stops doing the same", () => {
+ const hint = lift(
+ sidebarSrc,
+ /const trainDisabledHint: string \| undefined = [\s\S]*?\n\s*: undefined;/,
+ "trainDisabledHint",
+ "app-sidebar.tsx",
+ );
+ const forReason = (chatOnlyReason: string, chatOnlyDetail: string | null) =>
+ new Function(
+ "chatOnlyMeasured",
+ "chatOnlyReason",
+ "chatOnlyDetail",
+ `${hint.replace(": string | undefined", "")}\nreturn trainDisabledHint;`,
+ )(true, chatOnlyReason, chatOnlyDetail) as string | undefined;
+
+ for (const reason of ["torch_cpu_build", "torch_cuda_unavailable"]) {
+ const withDetail = forReason(reason, "2.11.0+cpu");
+ assert.ok(withDetail);
+ assert.doesNotMatch(withDetail, /needs an NVIDIA or AMD GPU/);
+ // The installed build is what makes this actionable to someone whose update already ran.
+ assert.match(withDetail, /2\.11\.0\+cpu/);
+ const withoutDetail = forReason(reason, null);
+ assert.ok(withoutDetail);
+ assert.doesNotMatch(withoutDetail, /needs an NVIDIA or AMD GPU/);
+ }
+ assert.equal(
+ forReason("no_gpu", null),
+ "Training needs an NVIDIA or AMD GPU.",
+ );
+ assert.equal(forReason("detection_failed", null), undefined);
+});
+
+
+test("every string the banner reaches for exists", () => {
+ const gpu = en.settings.resources.gpu as Record<string, string>;
+ const liveMonitor = en.settings.resources.liveMonitor as Record<
+ string,
+ string
+ >;
+ for (const key of [
+ "noUsableGpu",
+ "mismatchCpuBuild",
+ "mismatchUnavailable",
+ "unusableDevice",
+ ]) {
+ assert.equal(typeof gpu[key], "string", `settings.resources.gpu.${key}`);
+ }
+ for (const key of ["gpuUnusable", "gpuUnusableDetail"]) {
+ assert.equal(
+ typeof liveMonitor[key],
+ "string",
+ `settings.resources.liveMonitor.${key}`,
+ );
+ }
+ // The version is what a user can check against their own install, so both sentences
+ // have to carry it.
+ assert.match(gpu.mismatchCpuBuild, /\{version\}/);
+ assert.match(gpu.mismatchUnavailable, /\{version\}/);
+ // And the CPU-only host's line is still the one it always was.
+ assert.equal(t(NO_GPU), NO_GPU);
+ assert.match(gpu.noGpu, /No visible GPU detected/);
+});
+
+// The repair row must not be offered for a backend the desktop does not manage.
+//
+// start_managed_repair rejects that mutation, but only after startRepair has cleared
+// isExternalServer, stopped the external-server poll and swapped the shell to the repairing
+// screen, so a connected user lands on the repair-error screen instead of on their server.
+test("the repair row hides itself for an externally started backend", async () => {
+ const source = await readFile(
+ new URL(
+ "../src/features/settings/components/desktop-repair-control.tsx",
+ import.meta.url,
+ ),
+ "utf8",
+ );
+ assert.match(
+ source,
+ /if\s*\(!repair\s*\|\|\s*repair\.isExternalServer\)\s*return null;/,
+ "the control must bail out on an external server as well as outside Tauri",
+ );
+
+ const context = await readFile(
+ new URL("../src/hooks/tauri-repair-context.ts", import.meta.url),
+ "utf8",
+ );
+ assert.match(
+ context,
+ /isExternalServer:\s*boolean;/,
+ "the controller has to carry the flag for the control to read it",
+ );
+
+ const provider = await readFile(
+ new URL("../src/app/provider.tsx", import.meta.url),
+ "utf8",
+ );
+ const memo = provider.slice(provider.indexOf("const repairController"));
+ assert.match(
+ memo.slice(0, 300),
+ /isExternalServer,/,
+ "the provider has to publish the flag",
+ );
+ assert.match(
+ memo.slice(0, 300),
+ /\[isExternalServer\]/,
+ "and list it as a dependency, or the context freezes on the first render's value",
+ );
+});
+
+// The verdict can change without a restart, so the sidebar has to keep asking.
+//
+// The backend refreshes its physical inventory on a 60s TTL: attach an eGPU to a CPU-torch
+// machine and no_gpu becomes torch_cpu_build. The polling effect stopped at the first
+// settled verdict, so the new hint was unreachable for the rest of the session.
+test("the sidebar keeps polling while the inventory can still change the verdict", async () => {
+ const source = await readFile(
+ new URL("../src/components/app-sidebar.tsx", import.meta.url),
+ "utf8",
+ );
+
+ assert.match(
+ source,
+ /INVENTORY_SENSITIVE_REASONS = new Set\(\[[^\]]*"no_gpu"[^\]]*"torch_cpu_build"[^\]]*"torch_cuda_unavailable"/s,
+ "the three verdicts the inventory can move must all keep the poll alive",
+ );
+
+ const set = source.slice(
+ source.indexOf("INVENTORY_SENSITIVE_REASONS = new Set(["),
+ );
+ const listed = set.slice(0, set.indexOf("]"));
+ for (const settled of ["mlx_unavailable", "intel_mac"]) {
+ assert.ok(
+ !listed.includes(settled),
+ `${settled} cannot change on a probe and must not keep polling`,
+ );
+ }
+ // detection_failed IS listed. current_chat_only_verdict() can replace it once the
+ // inventory recovers, for the host whose torch will not import but whose wheel was
+ // classified from disk, so treating it as settled froze the sidebar on the failure.
+ assert.ok(listed.includes("detection_failed"));
+
+ assert.match(
+ source,
+ /if \(selfHealSettled && !capabilitiesUnknown && !inventorySensitive\) return;/,
+ "the early return has to consider the inventory-sensitive case",
+ );
+ assert.match(source, /const INVENTORY_POLL_MS = 60000;/);
+ assert.match(
+ source,
+ /selfHealSettled\s*\?\s*INVENTORY_POLL_MS\s*:\s*SELF_HEAL_POLL_MS/,
+ "a settled host polls at the inventory cadence, not the self-heal one",
+ );
+});
+
+// The poll decision itself, evaluated rather than pattern-matched.
+//
+// The test above pins the shape of the early return; this one runs it. A regression that
+// keeps the guard's text but inverts its sense would give every working install a forced
+// /api/system read a minute for the life of the session, which is the opposite of what
+// this change is for.
+test("only a host the inventory can still reclassify keeps polling", () => {
+ const guard = lift(
+ sidebarSrc,
+ /const inventorySensitive =[\s\S]*?if \(selfHealSettled && !capabilitiesUnknown && !inventorySensitive\) return;/,
+ "the polling guard",
+ "app-sidebar.tsx",
+ );
+ const reasons = lift(
+ sidebarSrc,
+ /const INVENTORY_SENSITIVE_REASONS = new Set\(\[[\s\S]*?\]\);/,
+ "INVENTORY_SENSITIVE_REASONS",
+ "app-sidebar.tsx",
+ );
+ const polls = (
+ chatOnly: boolean,
+ chatOnlyReason: string | null,
+ selfHealSettled = true,
+ capabilitiesUnknown = false,
+ ) =>
+ new Function(
+ "chatOnly",
+ "chatOnlyReason",
+ "selfHealSettled",
+ "capabilitiesUnknown",
+ `${reasons}
+ ${guard}
+ return true;`,
+ )(chatOnly, chatOnlyReason, selfHealSettled, capabilitiesUnknown) === true;
+
+ // The hosts this change exists for. Their verdict moves on the next inventory refresh.
+ assert.ok(polls(true, "torch_cpu_build"));
+ assert.ok(polls(true, "torch_cuda_unavailable"));
+ assert.ok(polls(true, "no_gpu"), "an eGPU can arrive on a CPU-only box");
+
+ // And the hosts that were working before this PR and must keep working the same way.
+ assert.ok(
+ !polls(false, null),
+ "a healthy GPU host must not gain a forced read a minute",
+ );
+ assert.ok(!polls(true, "intel_mac"), "an Intel Mac stays an Intel Mac");
+ // detection_failed is NOT settled when torch is the thing that failed: the backend
+ // classifies the wheel from disk and swaps in the mismatch once the inventory recovers,
+ // so stopping the poll froze the sidebar on the failure for the session.
+ assert.ok(
+ polls(true, "detection_failed"),
+ "the backend can still replace this one, so the read has to keep happening",
+ );
+
+ // The two pre-existing polls are untouched.
+ assert.ok(polls(true, "mlx_unavailable", false), "the MLX self-heal poll");
+ assert.ok(polls(false, null, true, true), "the unknown-verdict poll");
+});
+
+test("a settled inventory poll collects the refresh it triggered", () => {
+ // The backend's caches carry their own 60 second TTL and the health path reads them
+ // non-blocking, so the read that finds them expired only SCHEDULES the refresh and
+ // returns the stale entry. Polling on the TTL alone puts the read that collects the
+ // new answer a whole interval later, leaving an attached eGPU invisible for close to
+ // two minutes.
+ assert.match(sidebarSrc, /const INVENTORY_FOLLOW_UP_MS = (\d+);/);
+ const followUp = Number(
+ /const INVENTORY_FOLLOW_UP_MS = (\d+);/.exec(sidebarSrc)![1],
+ );
+ const interval = Number(/const INVENTORY_POLL_MS = (\d+);/.exec(sidebarSrc)![1]);
+ assert.ok(
+ followUp > 0 && followUp < interval,
+ `the follow-up (${followUp}ms) has to land inside the interval (${interval}ms)`,
+ );
+
+ // Scoped to the settled inventory case: the unknown poll is already fast, and the
+ // self-heal poll is not waiting on a TTL.
+ assert.match(sidebarSrc, /if \(!selfHealSettled \|\| capabilitiesUnknown\) return;/);
+ // And torn down with the interval, or it would outlive the verdict it was scheduled for.
+ assert.match(
+ sidebarSrc,
+ /window\.clearInterval\(id\);\s*\n\s*if \(followUp\) window\.clearTimeout\(followUp\);/,
+ );
+});
diff --git a/studio/frontend/tests/hardware-verdict-recovery.test.ts b/studio/frontend/tests/hardware-verdict-recovery.test.ts
index 6911f83d6b9..988750c3e84 100644
--- a/studio/frontend/tests/hardware-verdict-recovery.test.ts
+++ b/studio/frontend/tests/hardware-verdict-recovery.test.ts
@@ -137,11 +137,18 @@ test("the recovery poll runs while the verdict is unknown, on every platform", a
/capabilitiesUnknown/,
"the effect does not re-run when the verdict lands, so the interval outlives it",
);
+ // The cleanup now also cancels the follow-up read, so it went through stopPolling.
+ // What this pins is unchanged: the effect tears the interval down.
assert.match(
effect,
- /return \(\) => window\.clearInterval\(id\);/,
+ /return \(\) => stopPolling\(\);/,
"the interval is left running once the verdict is known",
);
+ assert.match(
+ effect,
+ /const stopPolling = \(\) => \{\s*\n\s*window\.clearInterval\(id\);/,
+ "stopPolling no longer clears the interval",
+ );
});
test("the poll is mounted on every route that gates on the verdict", async () => {
diff --git a/studio/frontend/tests/settings-search.test.ts b/studio/frontend/tests/settings-search.test.ts
index a754486613b..2f22d276a25 100644
--- a/studio/frontend/tests/settings-search.test.ts
+++ b/studio/frontend/tests/settings-search.test.ts
@@ -76,6 +76,18 @@ test("desktop startup entries are absent from browser search", () => {
}
});
+test("the repair row is searchable on the desktop, where it exists", () => {
+ // The capability message for a host whose PyTorch cannot use its GPUs sends the user to
+ // "Repair installation in Settings", so searching Settings for it has to find it. Only
+ // on the desktop: DesktopRepairControl renders nothing in a browser, and an index entry
+ // there would scroll to a row that is not on the page.
+ const desktop = createSettingsSearchIndex({ desktop: true, closeToTray: true });
+ const browser = createSettingsSearchIndex({ desktop: false, closeToTray: false });
+
+ assert.ok(desktop.general.includes("settings.general.repairInstall.label"));
+ assert.ok(!browser.general.includes("settings.general.repairInstall.label"));
+});
+
test("close to tray is searchable only on supported desktops", () => {
const supported = createSettingsSearchIndex({ desktop: true, closeToTray: true });
const mac = createSettingsSearchIndex({ desktop: true, closeToTray: false });
diff --git a/studio/frontend/tests/verdict-poll-stall-guard.test.ts b/studio/frontend/tests/verdict-poll-stall-guard.test.ts
index 31838d4d127..162dd9202b7 100644
--- a/studio/frontend/tests/verdict-poll-stall-guard.test.ts
+++ b/studio/frontend/tests/verdict-poll-stall-guard.test.ts
@@ -23,7 +23,9 @@ const src = await readFile(
// The interval, verbatim, from the marker it opens with to the cleanup it returns.
const START = "let pollingSince = 0;";
-const END = "return () => window.clearInterval(id);";
+// A single-line marker that appears once: the follow-up cancel is written twice, in
+// the finally and in stopPolling, so it cannot delimit the block.
+const END = "return () => stopPolling();";
const from = src.indexOf(START);
const to = src.indexOf(END, from);
assert.ok(from > 0 && to > from, "the recovery poll's interval moved out of app-sidebar.tsx");
@@ -40,6 +42,7 @@ function constant(name: string): number {
}
const STALL_MS = constant("VERDICT_POLL_STALL_MS");
const POLL_MS = constant("VERDICT_UNKNOWN_POLL_MS");
+const FOLLOW_UP_MS = constant("INVENTORY_FOLLOW_UP_MS");
const startPoll = new Function(
"window",
@@ -49,15 +52,30 @@ const startPoll = new Function(
"VERDICT_POLL_STALL_MS",
"VERDICT_UNKNOWN_POLL_MS",
"SELF_HEAL_POLL_MS",
+ // The follow-up read the settled-inventory branch schedules. This scenario is the
+ // unknown verdict, which does not take that branch, so the timer is never armed here;
+ // the bindings exist so the lifted block still evaluates. The timer itself goes
+ // through the injected window, like the interval.
+ "selfHealSettled",
+ "INVENTORY_FOLLOW_UP_MS",
body,
) as (
- window: { setInterval: (fn: () => void, ms: number) => number; clearInterval: (id: number) => void },
+ window: {
+ setInterval: (fn: () => void, ms: number) => number;
+ clearInterval: (id: number) => void;
+ // The follow-up read's timer. This scenario never arms it, but the lifted block
+ // reaches for it through the injected window and the type has to allow that.
+ setTimeout: (fn: () => void, ms: number) => number;
+ clearTimeout: (id: number) => void;
+ },
date: { now: () => number },
fetchDeviceType: () => Promise<void>,
capabilitiesUnknown: boolean,
stallMs: number,
unknownPollMs: number,
selfHealPollMs: number,
+ selfHealSettled: boolean,
+ followUpMs: number,
) => () => void;
/** The interval, wired to a clock and a queue of reads the test settles when it chooses. */
@@ -78,6 +96,8 @@ function harness() {
clearInterval: () => {
cleared += 1;
},
+ setTimeout: () => 0,
+ clearTimeout: () => undefined,
},
{ now: () => clock },
() =>
@@ -89,6 +109,8 @@ function harness() {
POLL_MS,
// The self-heal cadence is the other branch; this scenario is the unknown verdict.
15000,
+ false,
+ FOLLOW_UP_MS,
);
assert.ok(tick, "the poll never scheduled an interval");
diff --git a/studio/install_manifest.py b/studio/install_manifest.py
index 3a341ae6b09..446b85282cc 100644
--- a/studio/install_manifest.py
+++ b/studio/install_manifest.py
@@ -275,6 +275,7 @@ def write_manifest(
package_name: str = "unsloth",
no_torch: Optional[bool] = None,
expected_torch_tag: Optional[str] = None,
+ expected_torch_tag_pinned: Optional[bool] = None,
) -> Optional[Path]:
"""Record a completed install. Never raises: no manifest reads as incomplete,
which is the safe answer."""
@@ -307,6 +308,12 @@ def write_manifest(
# by verify-install, desktop-capabilities and the setup fast path.
if expected_torch_tag:
payload["expected_torch_tag"] = str(expected_torch_tag).strip().lower()
+ # Whether that flavor was NAMED by whoever ran the install, or merely what the selection
+ # landed on: setup.ps1 picks /cpu automatically on a GPU-less host and publishes it exactly
+ # as it publishes a pinned one, and reading the automatic case as deliberate leaves a later
+ # eGPU with no repair offered. Absent means unknown, as with every other additive key.
+ if expected_torch_tag_pinned is not None:
+ payload["expected_torch_tag_pinned"] = bool(expected_torch_tag_pinned)
path = manifest_path(root)
try:
tmp = path.with_suffix(".json.tmp")
@@ -403,6 +410,26 @@ def recorded_torch_flavor(root: Optional[Path] = None) -> Optional[str]:
return value or None
+def recorded_torch_flavor_was_pinned(root: Optional[Path] = None) -> bool:
+ """Whether the recorded flavor was NAMED rather than automatically selected.
+
+ False when nothing recorded it, including a manifest written before the key
+ existed. That is the safe direction here and the opposite of the usual "unknown
+ falls back to the old behaviour": treating an unproven CPU record as deliberate is
+ what leaves a host that has since gained a GPU with no repair offered at all, which
+ is the failure this whole field exists to distinguish. A repair is something the
+ user can decline; a silently CPU-only GPU box is not.
+ """
+ manifest = read_manifest(root)
+ if manifest is None:
+ return False
+ # An ACTUAL boolean. bool("false") is True, so a migrated or hand-edited manifest
+ # carrying the string would read as a deliberate pin and suppress the repair on a
+ # host that never chose one. Anything that is not a bool is unknown provenance, and
+ # the safe answer for unknown is the same False an absent key gets.
+ return manifest.get("expected_torch_tag_pinned") is True
+
+
def _parse_requirement_line(line: str) -> Optional[Tuple[str, str, str]]:
"""(distribution name, marker, specifier) for a requirement, or None.
diff --git a/studio/install_python_stack.py b/studio/install_python_stack.py
index 0654c9c02ed..86806df14c8 100644
--- a/studio/install_python_stack.py
+++ b/studio/install_python_stack.py
@@ -366,6 +366,9 @@ def _select_torchao_spec(torch_version: str | None) -> str:
# pip_install_try(), the only things here that change what is installed. None means
# "not probed yet".
_TORCH_RUNTIME_PROBE: "tuple[bool, bool, str | None, str, str] | None" = None
+# Not in the tuple above: thirteen call sites unpack that, and only the GPU-build
+# verdict needs this one.
+_TORCH_RUNTIME_XPU: str = ""
# Prefix on the probe's own stdout line. Import chatter can arrive before the answer, and
# an atexit handler or a CUDA teardown notice can arrive after it, so "the last non-empty
@@ -424,7 +427,9 @@ def _probe_torch_runtime() -> "tuple[bool, bool, str | None, str, str]":
"_v = getattr(torch, 'version', None); "
"h = getattr(_v, 'hip', '') or ''; "
"c = getattr(_v, 'cuda', '') or ''; "
- f"print('{_TORCH_PROBE_MARKER}' + '|'.join((v, h, c)))"
+ # XPU too: such a wheel carries its runtime here and nowhere else.
+ "x = getattr(_v, 'xpu', '') or ''; "
+ f"print('{_TORCH_PROBE_MARKER}' + '|'.join((v, h, c, x)))"
),
],
stdout = subprocess.PIPE,
@@ -450,9 +455,12 @@ def _probe_torch_runtime() -> "tuple[bool, bool, str | None, str, str]":
]
version: "str | None" = None
hip = cuda = ""
+ global _TORCH_RUNTIME_XPU
if _marked:
_fields = _marked[-1][len(_TORCH_PROBE_MARKER) :].split("|")
- version, hip, cuda = (_fields + ["", ""])[:3]
+ version, hip, cuda = (_fields + ["", "", ""])[:3]
+ # Beside the tuple, not in it: thirteen call sites unpack five values.
+ _TORCH_RUNTIME_XPU = (_fields + ["", "", "", ""])[3]
_TORCH_RUNTIME_PROBE = (True, probe.returncode == 0, version, hip, cuda)
return _TORCH_RUNTIME_PROBE
@@ -3429,7 +3437,11 @@ def _ensure_cpu_torch() -> None:
if _version is None:
return # unreadable -- the base install step handles a missing torch
# '+xpu' too: an XPU wheel sets neither torch.version.cuda nor .hip, so without it a
- # working Intel build reads as "cpu" and the CPU pin over it does nothing.
+ # working Intel build reads as "cpu" and the CPU pin over it does nothing. And
+ # torch.version.xpu beside it, for the untagged source, conda or private-index XPU
+ # wheel. _installed_flavor_tag_now reads that marker, so omitting it here made the two
+ # disagree: the check said "still xpu" and failed the update while this predicate had
+ # already declined to reinstall anything.
_ver = _version.lower()
_is_gpu_build = (
bool(_hip)
@@ -3437,6 +3449,7 @@ def _ensure_cpu_torch() -> None:
or bool(_cuda)
or bool(re.search(r"\+cu\d+", _ver))
or "+xpu" in _ver
+ or bool(_TORCH_RUNTIME_XPU)
)
if not _is_gpu_build:
return # already a CPU build
@@ -3487,6 +3500,22 @@ def _torch_flavor_tag(version: str) -> str:
return "cpu"
+def _gpu_family_from_runtime_markers(hip: str, cuda: str) -> str:
+ """Which GPU family an untagged wheel's runtime markers name.
+
+ torch.version.xpu alongside .hip and .cuda, for the reason _torch_build_is_gpu already
+ reads all three: an untagged source, conda or private-index XPU build carries its
+ runtime only there. Omitting it let an explicit /cpu pin over such a wheel compare
+ equal, return success without replacing anything, and then record a PINNED cpu flavor
+ for an environment that still holds an XPU build.
+ """
+ if hip:
+ return "rocm"
+ if cuda:
+ return "cuda"
+ return "xpu"
+
+
def _torch_build_is_gpu() -> bool:
"""Whether the installed torch can use a GPU at all, on the evidence available.
@@ -3502,7 +3531,14 @@ def _torch_build_is_gpu() -> bool:
"""
_ran, _importable, _version, _hip, _cuda = _probe_torch_runtime()
if _ran and _importable and _version:
- return _is_gpu_torch_label(_version.lower()) or bool(_hip) or bool(_cuda)
+ return (
+ _is_gpu_torch_label(_version.lower())
+ or bool(_hip)
+ or bool(_cuda)
+ # torch.version.xpu, for the same reason .cuda and .hip are here: an untagged
+ # source, conda or private-index XPU build carries its runtime there and nowhere else.
+ or bool(_TORCH_RUNTIME_XPU)
+ )
label = _installed_torch_label_on_disk()
return (not label) or _is_gpu_torch_label(label)
@@ -3539,6 +3575,28 @@ def _expected_torch_flavor_tag() -> str:
return "rocm"
if _is_cuda_family_leaf(leaf) or leaf in ("xpu", "cpu"):
return leaf
+ # A resolved backend is a stated choice, not a probe result: an AMD host taking setup.sh's
+ # documented UNSLOTH_TORCH_BACKEND=cpu recorded nothing here, because the NVIDIA probe
+ # answers "" for it, and the next launch called the deliberate install broken. Only when
+ # the family agrees with the wheel actually installed. Ahead of the manifest, which
+ # describes the PREVIOUS install, or a reinstall that changes flavor re-records the old tag.
+ if _TORCH_BACKEND in ("cpu", "cuda", "rocm", "xpu"):
+ _installed = _torch_flavor_tag(_installed_torch_version_label())
+ # "cuda" names a family, not a leaf, so the wheel's own cu tag is the flavor. Without
+ # this arm a REMOVED CPU pin lived on: install.sh resolves cuda and installs a CUDA
+ # wheel, the manifest still says cpu, and the healthy venv is recorded as deliberately
+ # CPU-only -- so a later update that swaps in a CPU wheel reads as working as asked.
+ if _TORCH_BACKEND == "cuda":
+ if _is_cuda_family_leaf(_installed):
+ return _installed
+ elif _installed == _TORCH_BACKEND:
+ return _TORCH_BACKEND
+ # An unknown-family pin (a corporate /simple mirror, /current) was applied verbatim, and
+ # nothing below it can name a family for this venv: the manifest describes the install
+ # the mirror replaced. The setup handover above still outranks this, because it describes
+ # the index this run actually installed from.
+ if _explicit_unknown_family_torch_index_url() is not None:
+ return ""
if _RECORDED_TORCH_TAG:
return _RECORDED_TORCH_TAG
# An absent NVIDIA GPU with no pin means no CUDA expectation exists to enforce.
@@ -3562,6 +3620,108 @@ def _expected_torch_flavor_is_explicit() -> bool:
return bool(_RECORDED_TORCH_TAG)
+def _recordable_torch_flavor_tag(resolved: str) -> str:
+ """The flavor worth writing to the manifest, or "" when nothing is.
+
+ Normally the flavor this run resolved, falling back to the previous install's so an
+ update does not erase a record it simply had no occasion to recompute. An explicit
+ pin whose leaf names no family (a corporate /simple mirror, /current) breaks that
+ fallback: the wheel now in the venv came from that mirror, the old record describes
+ a venv that no longer exists, and carrying it forward would hand a later unpinned run
+ a flavor to "repair" the mirror's build back to.
+ """
+ if resolved:
+ return resolved
+ if _explicit_unknown_family_torch_index_url() is not None:
+ return ""
+ return _RECORDED_TORCH_TAG or ""
+
+
+def _index_leaf_flavor_family(leaf: str) -> str:
+ """The flavor family a pip index leaf names: cpu, xpu, rocm, cuda, or "" for none."""
+ leaf = (leaf or "").strip().lower()
+ if leaf in ("cpu", "xpu"):
+ return leaf
+ if _is_pip_rocm_family_leaf(leaf):
+ return "rocm"
+ if _is_cuda_family_leaf(leaf):
+ return "cuda"
+ return ""
+
+
+def _flavor_tag_family(tag: str) -> str:
+ """The family a flavor tag belongs to. cu124 and cu128 are both "cuda"."""
+ tag = (tag or "").strip().lower()
+ return "cuda" if tag.startswith("cu") else tag
+
+
+def _expected_torch_flavor_was_pinned(flavor: str = "") -> bool:
+ """Whether ``flavor`` was NAMED by whoever ran this install.
+
+ Distinct from _expected_torch_flavor_is_explicit(), which counts setup.ps1's
+ handover variable. setup.ps1 publishes that variable for an AUTOMATIC /cpu choice on
+ a GPU-less host exactly as it does for a pinned one, so the handover cannot answer
+ this question. An index pin, an index family, and UNSLOTH_TORCH_BACKEND all can:
+ each of them is someone saying which build they want. Carried forward from the
+ previous manifest, so an update that names nothing does not erase the fact.
+
+ Each of them only answers for the family it NAMES. setup.ps1 falls back to the CPU
+ index when a pinned ROCm or XPU install fails, and publishes the resolved cpu tag
+ while the original GPU pin is still in the environment: counting that pin would
+ record a pinned CPU flavor, and _expected_cpu_flavor_was_chosen() would then read a
+ failed install as a deliberate one and suppress the repair guidance for good.
+ ``flavor`` empty means nobody asked about a specific one, and every pin counts.
+ """
+
+ def _names_it(family: str) -> bool:
+ return True if not flavor else family == _flavor_tag_family(flavor)
+
+ # _explicit_torch_index_url() already covers both variables WITH install.sh's precedence:
+ # the URL wins outright and the family is read only when no URL was supplied. Reading the
+ # family separately undid that, so an authoritative corporate /simple URL whose leaf names
+ # no family fell through to a stale ..._FAMILY=cpu and recorded a GPU-less host's CPU
+ # wheel as deliberately pinned -- and a later eGPU there would get no mismatch or repair.
+ pin = _explicit_torch_index_url()
+ if pin is not None and _names_it(_index_leaf_flavor_family(_torch_index_leaf(pin))):
+ return True
+ # install.sh derives UNSLOTH_TORCH_BACKEND from the index it RESOLVED -- "cpu" on any GPU-less
+ # machine, asked for or not -- and marks it derived. Only an unmarked value is a preference.
+ if (
+ _TORCH_BACKEND in ("cpu", "cuda", "rocm", "xpu")
+ and os.environ.get("UNSLOTH_TORCH_BACKEND_SOURCE", "").strip().lower() != "resolved"
+ and _names_it(_TORCH_BACKEND)
+ ):
+ return True
+ # A record can only speak for a run that said nothing to contradict it. A GPU family asked
+ # for HERE that settled on CPU because the GPU install failed is the same failed-pin case
+ # the arms above refuse to call deliberate, and reviving the old CPU provenance underneath
+ # them re-records the venv as pinned CPU anyway. Only families this run NAMED count:
+ # install.sh's derived backend is not a preference, and a leaf that names no family has
+ # contradicted nothing.
+ if flavor and any(
+ family and family != _flavor_tag_family(flavor)
+ for family in _flavor_families_this_run_named()
+ ):
+ return False
+ return bool(_RECORDED_TORCH_TAG_PINNED) and _names_it(
+ _flavor_tag_family(_RECORDED_TORCH_TAG or "")
+ )
+
+
+def _flavor_families_this_run_named() -> tuple[str, ...]:
+ """The torch families the CURRENT run was told to install, in no particular order."""
+ named: list[str] = []
+ pin = _explicit_torch_index_url()
+ if pin is not None:
+ named.append(_index_leaf_flavor_family(_torch_index_leaf(pin)))
+ if (
+ _TORCH_BACKEND in ("cpu", "cuda", "rocm", "xpu")
+ and os.environ.get("UNSLOTH_TORCH_BACKEND_SOURCE", "").strip().lower() != "resolved"
+ ):
+ named.append(_TORCH_BACKEND)
+ return tuple(named)
+
+
def _expected_torch_index_url(tag: str) -> str:
"""The wheel index to repair `tag` from.
@@ -3618,16 +3778,16 @@ def _installed_flavor_tag_now(expected: str = "") -> str:
_ran, _importable, _version, _hip, _cuda = _probe_torch_runtime()
if _ran and _importable and _version:
tag = _torch_flavor_tag(_version)
- if expected == "cpu" and tag == "cpu" and (_hip or _cuda):
- return "rocm" if _hip else "cuda"
+ if expected == "cpu" and tag == "cpu" and (_hip or _cuda or _TORCH_RUNTIME_XPU):
+ return _gpu_family_from_runtime_markers(_hip, _cuda)
return tag
if _ran:
- # The probe ANSWERED, and the answer was that this torch does not import. That is
- # not the ambiguity the disk fallback exists for: version.py would report the
- # requested +cu*/+xpu tag from a half-written or DLL-less wheel, the caller would
- # read tag == expected, and the update would write a completion manifest over a
- # torch nothing can import. A torch that failed to import BEFORE the repair never
- # reaches here -- the pre-repair chain returns early on it.
+ # The probe ANSWERED, and its answer is that this torch does not import -- not the
+ # ambiguity the disk fallback exists for. version.py would report the requested
+ # +cu*/+xpu tag from a half-written or DLL-less wheel, the caller would read
+ # tag == expected, and the update would write a completion manifest over a torch
+ # nothing can import. A pre-repair import failure never reaches here: that chain
+ # returns early on it.
return _TORCH_TAG_UNIMPORTABLE
label = _installed_torch_label_on_disk()
return _torch_flavor_tag(label) if label else ""
@@ -3764,10 +3924,8 @@ def _resync_torch_coupled_packages(label_before: str) -> bool:
# Across a CUDA-major move the resident build is not merely slower:
# _select_torchao_spec exists because a torchao compiled for CUDA 12
# cannot load its cpp under cu130. Remove it, the way the xFormers arm
- # below removes a build made for a torch that is gone, rather than
- # completing the update with a package that may fail on import. A
- # release-only move keeps the old warning: that one really is the
- # slow path.
+ # below removes a build made for a torch that is gone. A release-only
+ # move keeps the old warning: that one really is just the slow path.
if _cuda_moved and _uninstall_distribution("torchao"):
_note(
f"removed the torchao built for a different CUDA major; "
@@ -3851,8 +4009,8 @@ def _ensure_expected_torch_flavor(expected: "str | None" = None) -> bool:
installed = _torch_flavor_tag(installed_version)
# _torch_flavor_tag reads untagged as "cpu", so a private index's untagged CUDA build
# compares equal under a /cpu pin.
- if expected == "cpu" and installed == "cpu" and (_hip or _cuda):
- installed = "rocm" if _hip else "cuda"
+ if expected == "cpu" and installed == "cpu" and (_hip or _cuda or _TORCH_RUNTIME_XPU):
+ installed = _gpu_family_from_runtime_markers(_hip, _cuda)
if installed == expected:
return True
@@ -4869,6 +5027,9 @@ NO_TORCH = _infer_no_torch()
# Read at import: install_python_stack() drops the manifest before its dependency pass.
_RECORDED_TORCH_TAG = install_manifest.recorded_torch_flavor()
+# Whether that record came from someone NAMING a flavor: setup.ps1 publishes an automatic
+# /cpu choice the same way it publishes a pinned one.
+_RECORDED_TORCH_TAG_PINNED = install_manifest.recorded_torch_flavor_was_pinned()
# UNSLOTH_TORCH_BACKEND is set by install.sh after get_torch_index_url() ("cuda", "rocm",
# "cpu"; empty = standalone `studio update`, where we re-detect).
@@ -7271,6 +7432,11 @@ def install_python_stack() -> int:
# A direct run has no setup.ps1 postlude to swap triton back. After the invariant,
# because the swap keys off the installed +xpu label.
_ensure_xpu_triton()
+ elif not NO_TORCH:
+ # Resolve it on the other platforms too, for the RECORD only. Without this a Linux GPU box
+ # installed with a transient explicit CPU pin looks, on the next launch, like a CPU wheel
+ # beside a physical GPU.
+ torch_flavor_tag = _expected_torch_flavor_tag()
# 14. Final check (silent; third-party conflicts are expected)
subprocess.run(
@@ -7311,8 +7477,12 @@ def install_python_stack() -> int:
steps_total = _TOTAL,
package_name = package_name,
no_torch = NO_TORCH,
- # A platform that never resolves a flavor carries the old record forward.
- expected_torch_tag = torch_flavor_tag or _RECORDED_TORCH_TAG,
+ # A platform that never resolves a flavor carries the old record forward. An unknown-family
+ # pin is the exception: the previous record describes a venv that no longer exists, and
+ # writing it back would give a later unpinned run a flavor to "repair" the mirror's to.
+ expected_torch_tag = _recordable_torch_flavor_tag(torch_flavor_tag),
+ expected_torch_tag_pinned = bool(_recordable_torch_flavor_tag(torch_flavor_tag))
+ and _expected_torch_flavor_was_pinned(_recordable_torch_flavor_tag(torch_flavor_tag)),
)
is None
):
diff --git a/studio/src-tauri/src/commands.rs b/studio/src-tauri/src/commands.rs
index ee7206ecd2c..ba16a1493f1 100644
--- a/studio/src-tauri/src/commands.rs
+++ b/studio/src-tauri/src/commands.rs
@@ -933,6 +933,15 @@ pub async fn native_path_leases_usable(
)
}
+/// `force_installer` skips the update attempt and runs the bundled installer directly.
+///
+/// The automatic repair tries `studio update` first because it is the cheap fix for a venv
+/// one release behind. But an update reuses the environment it finds, so a managed venv
+/// whose PyTorch has been replaced by a CPU-only wheel comes back from a successful update
+/// still CPU-only. Only install.ps1 / install.sh re-select the torch index.
+///
+/// Settings' manual "Repair installation" therefore passes true. Absent (the startup
+/// auto-repair path) reads as false, so that path is unchanged.
#[tauri::command]
pub async fn start_managed_repair(
app: AppHandle,
@@ -941,8 +950,13 @@ pub async fn start_managed_repair(
update_state: tauri::State<'_, update::UpdateState>,
install_state: tauri::State<'_, install::InstallState>,
diagnostics: tauri::State<'_, DiagnosticsState>,
+ force_installer: Option<bool>,
) -> Result<(), String> {
- info!("start_managed_repair command called");
+ let force_installer = force_installer.unwrap_or(false);
+ info!(
+ "start_managed_repair command called (force_installer={})",
+ force_installer
+ );
if install_state
.lock()
@@ -979,29 +993,38 @@ pub async fn start_managed_repair(
let repair_group_id = install::take_pending_repair_group_for_resume(&install_state)
.unwrap_or_else(|| diagnostics::begin_repair_group(&diagnostics_state));
- let _ = app.emit("repair-progress", "Updating existing Unsloth install...");
- let update_app = app.clone();
- let update_state = update_state.inner().clone();
- let update_diagnostics = diagnostics_state.clone();
- let update_repair_group_id = repair_group_id.clone();
- let update_result = tokio::task::spawn_blocking(move || {
- update::run_backend_update_for_repair(
- update_app,
- update_state,
- update_diagnostics,
- update_repair_group_id,
- )
- })
- .await
- .map_err(|e| format!("Repair update task panicked: {e}"))?;
+ // Ok(()) rather than skipping the match: a second copy of the fallback under a
+ // `if force_installer` would be the one place a future change could miss.
+ let update_result = if force_installer {
+ let _ = app.emit("repair-progress", "Running bundled installer...");
+ Ok(())
+ } else {
+ let _ = app.emit("repair-progress", "Updating existing Unsloth install...");
+ let update_app = app.clone();
+ let update_state = update_state.inner().clone();
+ let update_diagnostics = diagnostics_state.clone();
+ let update_repair_group_id = repair_group_id.clone();
+ tokio::task::spawn_blocking(move || {
+ update::run_backend_update_for_repair(
+ update_app,
+ update_state,
+ update_diagnostics,
+ update_repair_group_id,
+ )
+ })
+ .await
+ .map_err(|e| format!("Repair update task panicked: {e}"))?
+ };
match update_result {
- Ok(()) if managed_install_ready_after_repair().await => {
+ Ok(()) if !force_installer && managed_install_ready_after_repair().await => {
info!("Managed repair complete after update");
diagnostics::finish_repair_group(&diagnostics_state, &repair_group_id, "success", None);
let _ = app.emit("repair-complete", ());
return Ok(());
}
+ // The forced path already emitted its own progress line and ran no update.
+ Ok(()) if force_installer => {}
Ok(()) => {
warn!("Managed repair update finished, but preflight is still not ready; falling back to installer");
let _ = app.emit(
diff --git a/tests/python/test_install_python_stack.py b/tests/python/test_install_python_stack.py
index 6094cfb7142..3794cbc8df8 100644
--- a/tests/python/test_install_python_stack.py
+++ b/tests/python/test_install_python_stack.py
@@ -2584,6 +2584,45 @@ class TestExpectedTorchFlavorResolution:
with mock.patch.object(ips, "_RECORDED_TORCH_TAG", "cu128"):
assert ips._expected_torch_flavor_tag() == "cu128"
+ def test_a_resolved_cuda_backend_outranks_a_stale_cpu_record(self):
+ """A CPU pin that was REMOVED must not outlive the install that replaced it.
+
+ install.sh resolves the backend to cuda and installs a CUDA wheel; reading the old
+ manifest here recorded the healthy venv as deliberately CPU-only, and
+ _expected_cpu_flavor_was_chosen() would then read a later CPU wheel as the install
+ working as asked and suppress the mismatch and its repair. The wheel's own tag is
+ what says which cu index this venv came from.
+ """
+ with self._env():
+ with (
+ mock.patch.object(ips, "_TORCH_BACKEND", "cuda"),
+ mock.patch.object(ips, "_RECORDED_TORCH_TAG", "cpu"),
+ mock.patch.object(
+ ips, "_installed_torch_version_label", return_value = "2.9.1+cu128"
+ ),
+ ):
+ assert ips._expected_torch_flavor_tag() == "cu128"
+ # And the stale provenance goes with it: pinned answers per family, so the
+ # cpu record cannot speak for a cuda tag. install.sh marks the backend it
+ # derived, which is why the derived cuda does not count as pinned either.
+ with (
+ mock.patch.dict(os.environ, {"UNSLOTH_TORCH_BACKEND_SOURCE": "resolved"}),
+ mock.patch.object(ips, "_RECORDED_TORCH_TAG_PINNED", True),
+ ):
+ assert ips._expected_torch_flavor_was_pinned("cu128") is False
+
+ def test_a_resolved_cuda_backend_over_a_cpu_wheel_still_reads_the_manifest(self):
+ # Only when the family agrees with the wheel actually installed, the same rule the
+ # cpu/rocm/xpu arm applies: a resolved cuda beside a CPU wheel is the mismatch this
+ # whole path exists to repair, not a reason to call the venv CUDA.
+ with self._env():
+ with (
+ mock.patch.object(ips, "_TORCH_BACKEND", "cuda"),
+ mock.patch.object(ips, "_RECORDED_TORCH_TAG", "cu124"),
+ mock.patch.object(ips, "_installed_torch_version_label", return_value = "2.11.0+cpu"),
+ ):
+ assert ips._expected_torch_flavor_tag() == "cu124"
+
def test_a_gpuless_host_with_nothing_recorded_says_nothing(self):
# Inventing a CUDA expectation from an absent GPU would reinstall CUDA torch
# onto a CPU box on every update.
@@ -2634,5 +2673,9 @@ class TestExpectedTorchFlavorResolution:
def test_no_index_url_is_ever_persisted_by_the_manifest_write(self):
# The manifest lives in the venv, so a token in a pinned URL must not reach it.
source = inspect.getsource(ips.install_python_stack)
- assert "expected_torch_tag = torch_flavor_tag or _RECORDED_TORCH_TAG," in source
+ assert "expected_torch_tag = _recordable_torch_flavor_tag(torch_flavor_tag)," in source
assert "torch_index_url" not in source
+ # And the helper that answers it records a FLAVOR, never a URL, for the same
+ # reason: it is reached with the pin still in the environment.
+ helper = inspect.getsource(ips._recordable_torch_flavor_tag)
+ assert "return" in helper and "_explicit_torch_index_url()" not in helper
diff --git a/tests/studio/install/test_cuda_repair.py b/tests/studio/install/test_cuda_repair.py
index b237dca729a..6789975539c 100644
--- a/tests/studio/install/test_cuda_repair.py
+++ b/tests/studio/install/test_cuda_repair.py
@@ -1863,6 +1863,182 @@ class TestThePostRepairCheckUsesTheSameRuleAsThePreRepairOne:
assert stack_mod._installed_flavor_tag_now("cu124") == "cpu"
assert stack_mod._installed_flavor_tag_now() == "cpu"
+ def test_an_untagged_xpu_wheel_does_not_satisfy_a_cpu_expectation(self):
+ """torch.version.xpu is where an untagged source, conda or private-index XPU
+ build carries its runtime -- .hip and .cuda are both empty there. Reading only
+ those two accepted the XPU wheel under a /cpu pin, returned success without
+ replacing it, and then recorded a PINNED cpu flavor for a venv still holding it.
+ """
+ with (
+ patch.object(
+ stack_mod,
+ "_probe_torch_runtime",
+ return_value = (True, True, "2.9.0", "", ""),
+ ),
+ patch.object(stack_mod, "_TORCH_RUNTIME_XPU", "20250101"),
+ ):
+ assert stack_mod._installed_flavor_tag_now("cpu") == "xpu"
+
+ def test_the_cpu_pin_repair_agrees_with_the_check_that_triggers_it(self):
+ """_ensure_cpu_torch's own GPU-build predicate has to see the untagged XPU build
+ too. Reading only the tag and .hip/.cuda made it return without reinstalling, so
+ the post-repair check saw xpu again and failed the update instead of honouring
+ the pin: the detection improved and the repair did not follow it."""
+ source = inspect.getsource(stack_mod._ensure_cpu_torch)
+ predicate = source[source.index("_is_gpu_build = ") :]
+ predicate = predicate[: predicate.index("if not _is_gpu_build")]
+ assert (
+ "_TORCH_RUNTIME_XPU" in predicate
+ ), "an untagged XPU wheel carries its runtime only in torch.version.xpu"
+ for marker in ("_hip", "_cuda", "+xpu", "rocm"):
+ assert marker in predicate, f"{marker} must still count"
+
+ def test_the_gpu_family_reading_prefers_the_explicit_runtimes(self):
+ # An XPU marker beside a CUDA or HIP one names the accelerator that wheel was
+ # BUILT for; xpu is the answer only when it is the sole marker.
+ assert stack_mod._gpu_family_from_runtime_markers("6.4", "12.4") == "rocm"
+ assert stack_mod._gpu_family_from_runtime_markers("", "12.4") == "cuda"
+ assert stack_mod._gpu_family_from_runtime_markers("", "") == "xpu"
+
+
+class TestAFailedGpuPinIsNotADeliberateCpuChoice:
+ """setup.ps1 falls back to the CPU index when a pinned ROCm or XPU install fails and
+ publishes the resolved cpu tag, while the original GPU pin is still in the
+ environment. Recording that as pinned makes _expected_cpu_flavor_was_chosen() read a
+ failed install as an intentional one and suppress the repair guidance for good."""
+
+ @staticmethod
+ def _pinned(
+ monkeypatch,
+ flavor,
+ *,
+ url = "",
+ family = "",
+ backend = "",
+ recorded = None,
+ ):
+ for var in (
+ "UNSLOTH_TORCH_INDEX_URL",
+ "UNSLOTH_TORCH_INDEX_FAMILY",
+ "UNSLOTH_TORCH_INSTALL_INDEX_URL",
+ ):
+ monkeypatch.delenv(var, raising = False)
+ if url:
+ monkeypatch.setenv("UNSLOTH_TORCH_INDEX_URL", url)
+ if family:
+ monkeypatch.setenv("UNSLOTH_TORCH_INDEX_FAMILY", family)
+ monkeypatch.delenv("UNSLOTH_TORCH_BACKEND_SOURCE", raising = False)
+ monkeypatch.setattr(stack_mod, "_TORCH_BACKEND", backend)
+ monkeypatch.setattr(stack_mod, "_RECORDED_TORCH_TAG", (recorded or ("", False))[0])
+ monkeypatch.setattr(stack_mod, "_RECORDED_TORCH_TAG_PINNED", (recorded or ("", False))[1])
+ return stack_mod._expected_torch_flavor_was_pinned(flavor)
+
+ def test_a_rocm_pin_that_settled_on_cpu_is_not_a_cpu_choice(self, monkeypatch):
+ assert (
+ self._pinned(monkeypatch, "cpu", url = "https://download.pytorch.org/whl/rocm6.4")
+ is False
+ )
+
+ def test_an_xpu_family_that_settled_on_cpu_is_not_a_cpu_choice(self, monkeypatch):
+ assert self._pinned(monkeypatch, "cpu", family = "xpu") is False
+
+ def test_a_gpu_backend_that_settled_on_cpu_is_not_a_cpu_choice(self, monkeypatch):
+ assert self._pinned(monkeypatch, "cpu", backend = "rocm") is False
+
+ def test_a_carried_forward_gpu_record_does_not_pin_a_cpu_fallback(self, monkeypatch):
+ assert self._pinned(monkeypatch, "cpu", recorded = ("cu124", True)) is False
+
+ def test_a_cpu_pin_that_settled_on_cpu_still_counts(self, monkeypatch):
+ assert self._pinned(monkeypatch, "cpu", url = "https://download.pytorch.org/whl/cpu") is True
+
+ def test_a_rocm_pin_that_settled_on_rocm_still_counts(self, monkeypatch):
+ assert (
+ self._pinned(monkeypatch, "rocm", url = "https://download.pytorch.org/whl/rocm6.4")
+ is True
+ )
+
+ def test_a_cuda_pin_counts_for_any_cuda_flavor(self, monkeypatch):
+ # cu124 and cu128 are both the cuda family: the pin names the family, and the
+ # recorded tag names the exact index within it.
+ assert (
+ self._pinned(monkeypatch, "cu128", url = "https://download.pytorch.org/whl/cu124") is True
+ )
+
+ def test_a_carried_forward_cpu_record_still_counts_for_cpu(self, monkeypatch):
+ assert self._pinned(monkeypatch, "cpu", recorded = ("cpu", True)) is True
+
+ def test_a_gpu_request_this_run_retires_the_old_cpu_record(self, monkeypatch):
+ """The record can only speak for a run that said nothing to contradict it.
+
+ A ROCm pin that settled on CPU is the failed-pin case the arms above refuse to call
+ deliberate, and reviving the old CPU provenance underneath them re-records the venv
+ as pinned CPU anyway: the mismatch is then suppressed for good once the requested
+ GPU works.
+ """
+ assert (
+ self._pinned(
+ monkeypatch,
+ "cpu",
+ url = "https://download.pytorch.org/whl/rocm6.4",
+ recorded = ("cpu", True),
+ )
+ is False
+ )
+ assert self._pinned(monkeypatch, "cpu", backend = "cuda", recorded = ("cpu", True)) is False
+
+ def test_a_derived_backend_does_not_retire_the_old_cpu_record(self, monkeypatch):
+ # install.sh marks the backend it resolved, and "cpu" on a GPU-less machine is not a
+ # preference either way, so it contradicts nothing the previous run recorded.
+ monkeypatch.setenv("UNSLOTH_TORCH_BACKEND_SOURCE", "resolved")
+ monkeypatch.setattr(stack_mod, "_TORCH_BACKEND", "cuda")
+ monkeypatch.setattr(stack_mod, "_RECORDED_TORCH_TAG", "cpu")
+ monkeypatch.setattr(stack_mod, "_RECORDED_TORCH_TAG_PINNED", True)
+ for var in ("UNSLOTH_TORCH_INDEX_URL", "UNSLOTH_TORCH_INDEX_FAMILY"):
+ monkeypatch.delenv(var, raising = False)
+ assert stack_mod._expected_torch_flavor_was_pinned("cpu") is True
+
+ def test_an_authoritative_url_silences_a_stale_family(self, monkeypatch):
+ """install.sh returns on the URL and never reads the family, so a family that
+ disagrees is dead. An unknown-family corporate /simple URL names no flavor, and
+ falling through to a stale ..._FAMILY=cpu recorded the CPU wheel a GPU-less host
+ legitimately got as DELIBERATE. A later eGPU there gets no mismatch and no repair.
+ """
+ assert (
+ self._pinned(
+ monkeypatch,
+ "cpu",
+ url = "https://mirror.corp.invalid/simple",
+ family = "cpu",
+ )
+ is False
+ )
+
+ def test_the_family_still_answers_when_no_url_was_supplied(self, monkeypatch):
+ assert self._pinned(monkeypatch, "cpu", family = "cpu") is True
+
+ def test_a_url_whose_leaf_does_name_the_flavor_still_counts(self, monkeypatch):
+ assert (
+ self._pinned(
+ monkeypatch,
+ "cpu",
+ url = "https://download.pytorch.org/whl/cpu",
+ family = "rocm6.4",
+ )
+ is True
+ )
+
+ def test_the_provenance_check_reads_the_family_only_through_the_shared_resolver(self):
+ """One read of the pair, so the precedence cannot be bypassed by a second one."""
+ body = inspect.getsource(stack_mod._expected_torch_flavor_was_pinned)
+ assert "UNSLOTH_TORCH_INDEX_FAMILY" not in body, (
+ "the family has to come through _explicit_torch_index_url(), which applies "
+ "install.sh's precedence; a direct read here reintroduces the bug"
+ )
+ assert "_explicit_torch_index_url()" in body
+
+ def test_asking_without_a_flavor_answers_as_it_always_did(self, monkeypatch):
+ assert self._pinned(monkeypatch, "", url = "https://download.pytorch.org/whl/rocm6.4") is True
+
class TestTheWindowsXpuTritonSwapReachesADirectRun:
"""setup.ps1 performs the swap after this script exits; a direct run has no such
diff --git a/tests/studio/install/test_gpu_detection_followups.py b/tests/studio/install/test_gpu_detection_followups.py
index d2fd7ae8db4..c98b8c4e74d 100644
--- a/tests/studio/install/test_gpu_detection_followups.py
+++ b/tests/studio/install/test_gpu_detection_followups.py
@@ -284,9 +284,9 @@ class TestBackendExportLeafClassification:
def test_export_block_uses_leaf(self, install_src):
anchor = install_src.find("_torch_index_leaf=")
assert anchor >= 0, "backend export must classify on the final path segment"
- # Window spans the leaf-normalization prelude (query/frag drop + all-slash trim loop)
- # through the export case arms.
- window = install_src[anchor : anchor + 900]
+ # Wide enough to clear the provenance check that now sits between the leaf normalisation
+ # and the case arms.
+ window = install_src[anchor : anchor + 1600]
assert 'export UNSLOTH_TORCH_BACKEND="rocm"' in window
assert 'export UNSLOTH_TORCH_BACKEND="cpu"' in window
assert 'export UNSLOTH_TORCH_BACKEND="cuda"' in window
diff --git a/tests/studio/install/test_rocm_arch_table_parity.py b/tests/studio/install/test_rocm_arch_table_parity.py
index 993419f2c61..8919754abe9 100644
--- a/tests/studio/install/test_rocm_arch_table_parity.py
+++ b/tests/studio/install/test_rocm_arch_table_parity.py
@@ -383,9 +383,36 @@ def _name_tables() -> dict[str, object]:
"install.ps1": _name_table_ps(_INSTALL_PS1),
"studio/setup.ps1": _name_table_ps(_SETUP_PS1),
"studio/install_python_stack.py": list(stack_mod._WIN_GPU_NAME_ARCH_TABLE),
+ # The backend carries the seventh copy: it decides whether a Windows adapter the
+ # DirectX registry did not give an AdapterFamily is one a repair could help, and
+ # answering that from a stale table would offer the repair to a card no wheel
+ # index covers (or withhold it from one that is covered).
+ "studio/backend/utils/hardware/hardware.py": _name_table_py_literal(
+ PACKAGE_ROOT / "studio" / "backend" / "utils" / "hardware" / "hardware.py",
+ "_GPU_NAME_GFX_TABLE",
+ ),
}
+def _name_table_py_literal(path: Path, name: str) -> list:
+ """A module-level list-of-pairs literal, read without importing the module."""
+ import ast
+
+ tree = ast.parse(path.read_text(encoding = "utf-8"))
+ for node in tree.body:
+ targets = (
+ [node.target]
+ if isinstance(node, ast.AnnAssign)
+ else node.targets
+ if isinstance(node, ast.Assign)
+ else []
+ )
+ for target in targets:
+ if isinstance(target, ast.Name) and target.id == name:
+ return [tuple(pair) for pair in ast.literal_eval(node.value)]
+ raise AssertionError(f"{name} not found in {path}")
+
+
def _spoof_profiles() -> dict[str, str]:
"""gfx -> marketing name out of tests/_zoo_rocm_spoof.py::_PROFILES.
@@ -577,6 +604,7 @@ _REGISTERED_TABLE_FILES = {
"studio/setup.sh",
"studio/setup.ps1",
"studio/install_python_stack.py",
+ "studio/backend/utils/hardware/hardware.py",
"tests/_zoo_rocm_spoof.py",
}
diff --git a/tests/studio/install/test_rocm_support.py b/tests/studio/install/test_rocm_support.py
index 7bd726be985..da44dfdcbea 100644
--- a/tests/studio/install/test_rocm_support.py
+++ b/tests/studio/install/test_rocm_support.py
@@ -2786,7 +2786,8 @@ class TestInstallShStructure:
sh_path = PACKAGE_ROOT / "install.sh"
source = sh_path.read_text(encoding = "utf-8")
torch_url_pos = source.find("TORCH_INDEX_URL=$(get_torch_index_url)")
- backend_pos = source.find("UNSLOTH_TORCH_BACKEND")
+ # The export itself, not the first mention: a comment above the block mentions it too.
+ backend_pos = source.find("export UNSLOTH_TORCH_BACKEND=")
assert backend_pos > 0, "UNSLOTH_TORCH_BACKEND must be set in install.sh"
assert (
backend_pos > torch_url_pos
diff --git a/tests/studio/install/test_torch_probe_classification_parity.py b/tests/studio/install/test_torch_probe_classification_parity.py
index 8af9f715198..2b9aced2537 100644
--- a/tests/studio/install/test_torch_probe_classification_parity.py
+++ b/tests/studio/install/test_torch_probe_classification_parity.py
@@ -200,10 +200,30 @@ class TestClassificationIsAFaithfulTranslation:
assert new == _old_cuda_fields(ver, hip, cuda)
def test_cpu_gpu_predicate(self, ver, hip, cuda):
- env = {"re": re, "_version": ver, "_hip": hip, "_cuda": cuda}
+ # _TORCH_RUNTIME_XPU is a fourth input the merge-base predicate did not have, so
+ # the equivalence is claimed with it EMPTY: over everything the old one could
+ # see, the two still agree. Its own effect is pinned separately below, because a
+ # reference that cannot model it cannot be asked about it.
+ env = {"re": re, "_version": ver, "_hip": hip, "_cuda": cuda, "_TORCH_RUNTIME_XPU": ""}
_run_assignments("_ensure_cpu_torch", {"_ver", "_is_gpu_build"}, env)
assert env["_is_gpu_build"] == _old_cpu_is_gpu(ver, hip, cuda)
+ def test_the_xpu_runtime_marker_is_the_one_deliberate_divergence(self, ver, hip, cuda):
+ """An untagged source, conda or private-index XPU wheel carries its runtime only
+ in torch.version.xpu. The old predicate read it as CPU and declined to reinstall,
+ which is the whole reason the marker was added."""
+ env = {
+ "re": re,
+ "_version": ver,
+ "_hip": hip,
+ "_cuda": cuda,
+ "_TORCH_RUNTIME_XPU": "20250101",
+ }
+ _run_assignments("_ensure_cpu_torch", {"_ver", "_is_gpu_build"}, env)
+ assert (
+ env["_is_gpu_build"] is True
+ ), "with the marker set every state is a GPU build, however the version is tagged"
+
def test_xpu_supported_range(self, ver, hip, cuda):
env = {"re": re, "_version": ver, "_hip": hip, "_cuda": cuda}
_run_assignments("_ensure_xpu_torch", {"_ver", "_rel", "_n"}, env)
diff --git a/tests/studio/install/test_windows_torch_flavor_invariant.py b/tests/studio/install/test_windows_torch_flavor_invariant.py
index cb2e9e1c140..04dd9f6190b 100644
--- a/tests/studio/install/test_windows_torch_flavor_invariant.py
+++ b/tests/studio/install/test_windows_torch_flavor_invariant.py
@@ -379,7 +379,21 @@ class TestManifestRecordsTheFlavor:
def test_the_stack_carries_a_previous_record_forward(self):
# A platform that never resolves a flavor must not erase the one already recorded.
- assert "expected_torch_tag = torch_flavor_tag or _RECORDED_TORCH_TAG," in _STACK_SRC
+ assert "expected_torch_tag = _recordable_torch_flavor_tag(torch_flavor_tag)," in _STACK_SRC
+ helper = _STACK_SRC[_STACK_SRC.index("def _recordable_torch_flavor_tag(") :]
+ helper = helper[: helper.index("\ndef ", 1)]
+ assert 'return _RECORDED_TORCH_TAG or ""' in helper
+
+ def test_a_mirror_pin_does_not_carry_a_stale_flavor_forward(self):
+ # The wheel came from a mirror whose leaf names no family, so the previous record
+ # describes a venv that no longer exists and would hand a later unpinned run a flavor
+ # to "repair" the mirror's build back to.
+ helper = _STACK_SRC[_STACK_SRC.index("def _recordable_torch_flavor_tag(") :]
+ helper = helper[: helper.index("\ndef ", 1)]
+ assert "_explicit_unknown_family_torch_index_url() is not None" in helper
+ assert helper.index("_explicit_unknown_family_torch_index_url") < helper.index(
+ "_RECORDED_TORCH_TAG"
+ ), "the mirror check has to come before the carry-forward"
def test_the_record_is_read_before_the_manifest_is_dropped(self):
# install_python_stack() removes the manifest before its dependency pass.
@@ -398,6 +412,47 @@ if __name__ == "__main__":
sys.exit(pytest.main([__file__, "-q"]))
+class TestTheFlavorProvenance:
+ """A recorded flavor is only a CHOICE when someone named it."""
+
+ def test_install_sh_marks_a_derived_backend_as_derived(self):
+ source = (PACKAGE_ROOT / "install.sh").read_text(encoding = "utf-8")
+ block = source[source.index('case "$_torch_index_leaf" in') :]
+ block = block[: block.index("_is_pip_rocm_family_leaf")]
+ assert 'UNSLOTH_TORCH_BACKEND_SOURCE="resolved"' in block, (
+ "install.sh derives the backend from the index it resolved -- cpu on any "
+ "GPU-less host -- so the manifest has to be told which it was"
+ )
+
+ def test_a_backend_the_caller_stated_is_not_marked_derived(self):
+ # On a GPU-less host the resolved value is cpu too, so a stated choice and the
+ # automatic one are indistinguishable unless install.sh checks BEFORE overwriting.
+ source = (PACKAGE_ROOT / "install.sh").read_text(encoding = "utf-8")
+ assert "_torch_backend_was_stated" in source
+ check = source.index("_torch_backend_was_stated=true")
+ overwrite = source.index('case "$_torch_index_leaf" in')
+ assert check < overwrite, "the check has to run before the assignment"
+ mark = source.index('UNSLOTH_TORCH_BACKEND_SOURCE="resolved"')
+ line_start = source.rindex("if ", 0, mark)
+ assert (
+ "_torch_backend_was_stated" in source[line_start:mark]
+ ), "a stated backend must not be marked derived"
+
+ def test_the_stack_reads_that_marker(self):
+ pinned = _STACK_SRC[_STACK_SRC.index("def _expected_torch_flavor_was_pinned(") :]
+ pinned = pinned[: pinned.index("\ndef ", 1)]
+ assert "UNSLOTH_TORCH_BACKEND_SOURCE" in pinned
+ assert "resolved" in pinned
+
+ def test_an_untagged_xpu_runtime_counts_as_a_gpu_build(self):
+ # An untagged source or conda XPU build carries its runtime in torch.version.xpu and
+ # nowhere else, and calling it CPU-only fails the update outright.
+ assert "getattr(_v, 'xpu', '')" in _STACK_SRC
+ verdict = _STACK_SRC[_STACK_SRC.index("def _torch_build_is_gpu(") :]
+ verdict = verdict[: verdict.index("\ndef ", 1)]
+ assert "_TORCH_RUNTIME_XPU" in verdict
+
+
class TestABrokenTorchForcesItsOwnReinstall:
"""$script:TorchImportDefinitivelyFailed is raised when the probe reports an import
ERROR rather than a timeout, so the wheel on disk is the suspect and not the driver.
@@ -452,3 +507,88 @@ class TestABrokenTorchForcesItsOwnReinstall:
assert (
"--force-reinstall" in rocm and "$rocmForce" not in rocm
), "the ROCm arm forces every time, so a broken wheel is already replaced there"
+
+ def test_the_flag_is_still_raised_where_the_import_definitively_failed(self):
+ assert "$script:TorchImportDefinitivelyFailed = $true" in _SETUP_SRC
+
+
+class TestTheProvenanceMarkerSurvivesAResolution:
+ """A stated backend that the resolution overwrote is not a stated choice."""
+
+ def test_install_sh_only_keeps_a_stated_backend_that_agreed(self):
+ source = (PACKAGE_ROOT / "install.sh").read_text(encoding = "utf-8")
+ block = source[source.index("_torch_backend_was_stated=true") :]
+ block = block[: block.index("_is_pip_rocm_family_leaf")]
+ assert "_torch_backend_stated_value" in block, (
+ "the case statement overwrites UNSLOTH_TORCH_BACKEND before the marker is "
+ "decided, so the original value has to be kept to compare against"
+ )
+ gate = block[block.index('export UNSLOTH_TORCH_BACKEND_SOURCE="resolved"') - 400 :]
+ gate = gate[: gate.index("unset UNSLOTH_TORCH_BACKEND_SOURCE")]
+ assert '"$_torch_backend_stated_value" != "$UNSLOTH_TORCH_BACKEND"' in gate, (
+ "a caller who said cuda on a host with no visible GPU now carries the "
+ "resolved cpu; recording that as deliberate denies it the repair for good"
+ )
+
+
+class TestAnUnknownMirrorPinNamesNoFlavor:
+ """The wheel came from a mirror whose leaf names no family, so nothing downstream can
+ name one for this venv: not the manifest, which describes the install the mirror
+ replaced, and not the CUDA probe, which reads the mirror's own leaf back as a flavor."""
+
+ def test_the_expectation_stops_before_the_stale_manifest(self):
+ body = _STACK_SRC[_STACK_SRC.index("def _expected_torch_flavor_tag(") :]
+ body = body[: body.index("\ndef ", 1)]
+ guard = body.index("_explicit_unknown_family_torch_index_url() is not None")
+ record = body.index("if _RECORDED_TORCH_TAG:")
+ assert guard < record, (
+ "the guard has to run BEFORE the manifest fallback, or the stale tag is "
+ "returned as `resolved` and _recordable_torch_flavor_tag's own guard, which "
+ "only fires on an empty resolved, never runs"
+ )
+
+ def test_the_handover_still_outranks_it(self):
+ body = _STACK_SRC[_STACK_SRC.index("def _expected_torch_flavor_tag(") :]
+ body = body[: body.index("\ndef ", 1)]
+ assert body.index("UNSLOTH_EXPECTED_TORCH_TAG") < body.index(
+ "_explicit_unknown_family_torch_index_url() is not None"
+ ), "the setup handover describes the index this run installed from"
+
+
+class TestPinProvenanceMustBeABoolean:
+ def test_both_readers_reject_a_stringly_typed_pin(self):
+ stack = (PACKAGE_ROOT / "studio" / "install_manifest.py").read_text(encoding = "utf-8")
+ backend = (
+ PACKAGE_ROOT / "studio" / "backend" / "utils" / "hardware" / "hardware.py"
+ ).read_text(encoding = "utf-8")
+ assert 'manifest.get("expected_torch_tag_pinned") is True' in stack
+ assert "pinned is True" in backend, (
+ 'bool("false") is True, so a migrated or hand-edited manifest would read as a '
+ "deliberate pin and suppress the repair on a host that never chose one"
+ )
+
+ def test_the_manifest_reader_answers_false_for_a_string(self, tmp_path):
+ (tmp_path / install_manifest.MANIFEST_NAME).write_text(
+ json.dumps(
+ {
+ "schema": install_manifest.MANIFEST_SCHEMA,
+ "expected_torch_tag": "cpu",
+ "expected_torch_tag_pinned": "false",
+ }
+ ),
+ encoding = "utf-8",
+ )
+ assert install_manifest.recorded_torch_flavor_was_pinned(tmp_path) is False
+
+ def test_a_real_boolean_still_answers_true(self, tmp_path):
+ (tmp_path / install_manifest.MANIFEST_NAME).write_text(
+ json.dumps(
+ {
+ "schema": install_manifest.MANIFEST_SCHEMA,
+ "expected_torch_tag": "cpu",
+ "expected_torch_tag_pinned": True,
+ }
+ ),
+ encoding = "utf-8",
+ )
+ assert install_manifest.recorded_torch_flavor_was_pinned(tmp_path) is True
diff --git a/tests/studio/test_xpu_triton_swap.py b/tests/studio/test_xpu_triton_swap.py
index 0a40d22c6e8..f75940995c3 100644
--- a/tests/studio/test_xpu_triton_swap.py
+++ b/tests/studio/test_xpu_triton_swap.py
@@ -564,6 +564,7 @@ class TestCpuRepairSeesAnXpuWheel:
ver,
cuda = "",
hip = "",
+ runtime_xpu = "",
):
src = STACK.read_text(encoding = "utf-8")
start = src.index("def _ensure_cpu_torch() -> None:")
@@ -587,13 +588,31 @@ class TestCpuRepairSeesAnXpuWheel:
return (
"gpu"
- if eval(expr, {"re": _re, "_hip": hip, "_cuda": cuda, "_ver": ver.lower()})
+ if eval(
+ expr,
+ {
+ "re": _re,
+ "_hip": hip,
+ "_cuda": cuda,
+ "_ver": ver.lower(),
+ # The probe's XPU marker, which the predicate reads as a module global.
+ # Empty unless a case states otherwise: torch.version has no word for XPU,
+ # so a wheel is only known to be one through this.
+ "_TORCH_RUNTIME_XPU": runtime_xpu,
+ },
+ )
else "cpu"
)
def test_xpu_wheel_is_a_gpu_build(self):
assert self._classify("2.9.1+xpu") == "gpu"
+ def test_an_untagged_xpu_wheel_is_a_gpu_build_through_the_runtime_marker(self):
+ # A private index serves XPU torch with no +xpu local version, so the tag says
+ # nothing and torch.version stays empty on both fields. The runtime marker is the
+ # only thing left that knows, and a CPU pin must still reinstall over that build.
+ assert self._classify("2.9.1", runtime_xpu = "20250101") == "gpu"
+
@pytest.mark.parametrize(
"ver,cuda,hip,want",
[