Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: keep CUDA 12 llama.cpp prebuilts off drivers older than CUDA 12.4 (#13031)
Daniel Han0461328105a7
diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.pyindex 64ddc9005ab..ab8b3c2e658 100644--- a/studio/backend/core/inference/llama_cpp.py+++ b/studio/backend/core/inference/llama_cpp.py@@ -21088,6 +21088,10 @@ class LlamaCppBackend:if blocked is not None:return code_integrity_user_message(binary or "the llama.cpp runtime", blocked)+ cuda_image_error = LlamaCppBackend._cuda_kernel_image_error(output)+ if cuda_image_error is not None:+ return LlamaCppBackend._cuda_kernel_image_message(cuda_image_error, binary, log_path)+# The dynamic loader kills llama-server before main(), so nothing below# matches and the fallback blames the file or memory instead. The Linux# prebuilt links libgomp.so.1, which a stock container does not ship.@@ -22354,6 +22358,72 @@ class LlamaCppBackend:text = (output or "").lower()return any(marker in text for marker in cls._KERNEL_IMAGE_INVALID_MARKERS)+ # #12842. A HIP build prints "ROCm error:", so the #7624 crash never matches. Every+ # kernel fails alike, so no fit, flash-attn, slot or drafter retry can help.+ _CUDA_KERNEL_IMAGE_RE = re.compile(+ r"\bCUDA error: (device kernel image is invalid|no kernel image is available for execution)",+ re.IGNORECASE,+ )++ @classmethod+ def _cuda_kernel_image_error(cls, output: str) -> Optional[str]:+ """The CUDA kernel-image error text in ``output`` (lowercased), or None."""+ match = cls._CUDA_KERNEL_IMAGE_RE.search(output or "")+ return match.group(1).lower() if match else None++ @staticmethod+ def _cuda_install_driver_version(binary: Optional[str]) -> Optional[tuple[int, int]]:+ """The driver CUDA version the installer recorded for the managed install that+ owns ``binary`` (UNSLOTH_PREBUILT_INFO.json host_profile), or None."""+ try:+ from utils.llama_cpp_update import _llama_install_root++ root = _llama_install_root(binary) if binary else None+ if root is None:+ return None+ marker = json.loads((root / "UNSLOTH_PREBUILT_INFO.json").read_text(encoding = "utf-8"))+ driver = (marker.get("host_profile") or {}).get("driver_cuda_version")+ if isinstance(driver, list) and len(driver) == 2:+ return (int(driver[0]), int(driver[1]))+ except Exception:+ pass+ return None++ @classmethod+ def _cuda_kernel_image_message(+ cls,+ error: str,+ binary: Optional[str],+ log_path: "Optional[Path | str]" = None,+ ) -> str:+ remedy = cls._runtime_remedy(binary)+ log_hint = f" Full log: {log_path}" if log_path else ""+ if error == "no kernel image is available for execution":+ return (+ 'llama-server could not load its CUDA kernels ("no kernel image is available '+ "for execution\"): this llama.cpp CUDA build has no kernels for this GPU's "+ "architecture. This is not the GGUF file and not out of memory. "+ f"{remedy[0].upper()}{remedy[1:]}, or use the CPU or Vulkan backend.{log_hint}"+ )+ driver = cls._cuda_install_driver_version(binary)+ if driver is not None and driver >= (12, 4):+ return (+ 'llama-server could not load its CUDA kernels ("device kernel image is '+ f'invalid") although this NVIDIA driver supports CUDA {driver[0]}.{driver[1]}: '+ "the llama.cpp CUDA libraries look damaged or mismatched. This is not the "+ f"GGUF file and not out of memory. {remedy[0].upper()}{remedy[1:]}.{log_hint}"+ )+ driver_text = (+ f" (this NVIDIA driver supports CUDA {driver[0]}.{driver[1]})" if driver else ""+ )+ return (+ 'llama-server could not load its CUDA kernels ("device kernel image is '+ 'invalid"): the NVIDIA driver is most likely too old for this llama.cpp CUDA '+ f"build{driver_text}, which needs CUDA 12.4 or newer. This is not the GGUF file "+ "and not out of memory. Update the NVIDIA driver to R550 or newer (CUDA 12.4+), "+ f"or {remedy}.{log_hint}"+ )+@classmethoddef _arch_crash_retry_gpu_ids(cls, selected, enumerated) -> list[int]:"""Devices to retry on after a "device kernel image is invalid" crash.@@ -30194,6 +30264,9 @@ class LlamaCppBackend:_capability_crash = _tensor_capability_crash or self._is_kv_unified_refused("\n".join(self._stdout_lines))+ _capability_crash = _capability_crash or (+ self._cuda_kernel_image_error("\n".join(self._stdout_lines)) is not None+ )if (not _did_rocm_retryand _startup_crashed@@ -31185,6 +31258,27 @@ class LlamaCppBackend:)healthy = _spawn_and_wait(cmd, label = "-archfallback")+ # #12842: after the #7624 respawn every rung below keeps the same kernels.+ if not healthy and not _load_cancelled():+ # Whole buffer: a Linux abort appends a debugger backtrace.+ _cuda_image_out = "\n".join(self._stdout_lines)+ if self._cuda_kernel_image_error(_cuda_image_out) is not None:+ _proc_snap_ki = self._process # snapshot: re-reading races the teardown+ _ki_rc = _proc_snap_ki.poll() if _proc_snap_ki is not None else None+ self._kill_process()+ _raise_terminal_load_failure(+ self._classify_llama_start_failure(+ _cuda_image_out,+ gguf_path,+ self._model_identifier,+ _ki_rc,+ binary,+ self._llama_log_path,+ (self._api_key,),+ self._extra_args,+ )+ )+# Studio adds --kv-unified itself above one slot, so nothing the user# changes reaches it: retry at one slot, context intact. It aborts, so# this MUST stay ahead of the flash-attn rung, which takes any signaldiff --git a/studio/backend/tests/test_cuda_kernel_image_12842.py b/studio/backend/tests/test_cuda_kernel_image_12842.pynew file mode 100644index 00000000000..796a809d0ab--- /dev/null+++ b/studio/backend/tests/test_cuda_kernel_image_12842.py@@ -0,0 +1,235 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""#12842: a CUDA kernel-image crash stops at one launch; the ROCm (#7624) spelling does not."""++from __future__ import annotations++import json+import os+import struct+import subprocess+import sys+from pathlib import Path+from unittest.mock import patch++import pytest++_BACKEND_DIR = str(Path(__file__).resolve().parent.parent)+if _BACKEND_DIR not in sys.path:+ sys.path.insert(0, _BACKEND_DIR)++from core.inference.llama_cpp import GgufLoadIntent, LlamaCppBackend # noqa: E402++_REAL_POPEN = subprocess.Popen++# From the issue (Windows A40, b11443-mix-d65395f cuda12 bundle).+_ISSUE_TAIL = """\+0.31.322.841 I sched_reserve: CUDA0 compute buffer size = 13951.75 MiB+0.31.323.945 I cmn common_init_: warming up the model with an empty run - please wait ... (--no-warmup to disable)+0.31.368.986 E CUDA error: device kernel image is invalid+0.31.369.005 E D:\\a\\llama.cpp\\llama.cpp\\src\\ggml\\src\\ggml-cuda\\ggml-cuda.cu:134: CUDA error+ current device: 0, in function ggml_cuda_kernel_can_use_pdl at D:\\a\\llama.cpp\\llama.cpp\\src\\ggml\\src\\ggml-cuda\\common.cuh:1234+ cudaFuncGetAttributes(&attr, kernel)"""++_NO_IMAGE_TAIL = """\+ggml_cuda_compute_forward: MUL_MAT failed+CUDA error: no kernel image is available for execution on the device+ current device: 0, in function ggml_cuda_compute_forward at ggml-cuda.cu:2367"""++_ROCM_TAIL = """\+ROCm error: device kernel image is invalid+ current device: 0, in function ggml_cuda_compute_forward at ggml-cuda.cu:2367"""++# 0xC0000409, the issue's Windows exit code.+_WIN_FAIL_FAST = 3221226505+++class TestClassification:+ def test_issue_tail_is_a_cuda_kernel_image_error(self):+ assert LlamaCppBackend._cuda_kernel_image_error(_ISSUE_TAIL) == (+ "device kernel image is invalid"+ )++ def test_no_kernel_image_spelling(self):+ assert LlamaCppBackend._cuda_kernel_image_error(_NO_IMAGE_TAIL) == (+ "no kernel image is available for execution"+ )++ @pytest.mark.parametrize(+ "output",+ [+ _ROCM_TAIL,+ "",+ None,+ "CUDA error: out of memory",+ "device kernel image is invalid", # unprefixed: not a ggml CUDA error line+ ],+ )+ def test_other_output_does_not_match(self, output):+ assert LlamaCppBackend._cuda_kernel_image_error(output) is None++ def test_rocm_tail_still_matches_the_7624_marker(self):+ assert LlamaCppBackend._kernel_image_invalid(_ROCM_TAIL)++ def test_message_names_the_driver_not_memory(self):+ msg = LlamaCppBackend._classify_llama_start_failure(+ _ISSUE_TAIL, "/m/model.gguf", "model", _WIN_FAIL_FAST, None+ )+ assert "device kernel image is invalid" in msg+ assert "NVIDIA driver" in msg+ assert "R550" in msg+ assert "not out of memory" in msg+ assert "enough memory" not in msg++ def test_no_kernel_image_message_does_not_blame_the_driver(self):+ msg = LlamaCppBackend._classify_llama_start_failure(+ _NO_IMAGE_TAIL, "/m/model.gguf", "model", 134, None+ )+ assert "no kernels for this GPU" in msg+ assert "R550" not in msg++ def test_rocm_tail_keeps_its_old_classification(self):+ msg = LlamaCppBackend._classify_llama_start_failure(+ _ROCM_TAIL, "/m/model.gguf", "model", 134, None+ )+ assert "NVIDIA driver" not in msg++ @staticmethod+ def _marker(tmp_path, monkeypatch, marker):+ if marker is not None:+ (tmp_path / "UNSLOTH_PREBUILT_INFO.json").write_text(+ marker if isinstance(marker, str) else json.dumps(marker), encoding = "utf-8"+ )+ import utils.llama_cpp_update as update++ monkeypatch.setattr(update, "_llama_install_root", lambda _binary: tmp_path)+ return str(tmp_path / "llama-server")++ def test_driver_version_from_the_install_marker(self, tmp_path, monkeypatch):+ binary = self._marker(+ tmp_path,+ monkeypatch,+ {+ "runtime_line": "cuda12",+ "bundle_profile": "cuda12-older",+ "host_profile": {"driver_cuda_version": [12, 2]},+ },+ )+ assert LlamaCppBackend._cuda_install_driver_version(binary) == (12, 2)+ msg = LlamaCppBackend._cuda_kernel_image_message(+ "device kernel image is invalid", binary, "/logs/llama.log"+ )+ assert "supports CUDA 12.2" in msg and "R550" in msg+ assert msg.endswith("Full log: /logs/llama.log")++ def test_new_driver_blames_the_build_not_the_driver(self, tmp_path, monkeypatch):+ binary = self._marker(+ tmp_path, monkeypatch, {"host_profile": {"driver_cuda_version": [13, 0]}}+ )+ msg = LlamaCppBackend._cuda_kernel_image_message("device kernel image is invalid", binary)+ assert "supports CUDA 13.0" in msg+ assert "damaged or mismatched" in msg+ assert "R550" not in msg++ @pytest.mark.parametrize(+ "marker",+ [None, "not json", {"runtime_line": "cuda12"}, {"host_profile": {}}],+ )+ def test_driver_version_unknown(self, tmp_path, monkeypatch, marker):+ binary = self._marker(tmp_path, monkeypatch, marker)+ assert LlamaCppBackend._cuda_install_driver_version(binary) is None+ msg = LlamaCppBackend._cuda_kernel_image_message("device kernel image is invalid", binary)+ assert "most likely too old" in msg and "supports CUDA" not in msg+++def _write_gguf(path: Path, architecture: str = "llama") -> Path:+ def string(value: str) -> bytes:+ data = value.encode()+ return struct.pack("<Q", len(data)) + data++ metadata = string("general.architecture") + struct.pack("<I", 8) + string(architecture)+ path.write_bytes(struct.pack("<IIQQ", 0x46554747, 3, 0, 1) + metadata)+ return path+++def _backend(tmp_path: Path, gpus):+ backend = LlamaCppBackend()+ rows = [(index, 40000, 46000) for index in gpus]+ backend._get_gpu_memory = lambda _binary = None, **_kw: list(rows)+ backend._get_gpu_free_memory = lambda _binary = None, **_kw: [(i, f) for i, f, _t in rows]+ backend._read_gguf_metadata = lambda _path: None+ backend._can_estimate_kv = lambda: False+ backend._get_gguf_size_bytes = lambda _path: 1024+ backend._mmproj_vram_bytes = lambda _path: 0+ backend._resolve_launch_mmproj_path = lambda **kwargs: None+ backend._apu_ram_shortfall_message = lambda *args, **kwargs: None+ backend._amd_apu_wants_unified_memory = lambda *args, **kwargs: False+ backend._find_llama_server_binary = lambda include_denied = False: "/fake/llama-server"+ backend._is_vulkan_backend = lambda _binary = None: False+ backend._detect_audio_type_strict = lambda: None+ backend._apply_detected_audio = lambda _detected: True++ def _crashed_health(timeout, **_kw):+ # The drain thread owns _stdout_lines; let it finish before the caller reads them.+ thread = getattr(backend, "_stdout_thread", None)+ if thread is not None:+ thread.join(5)+ return False++ backend._wait_for_health = _crashed_health+ return backend, _write_gguf(tmp_path / "model.gguf")+++def _crash_every_spawn(backend, gguf, tail, rc):+ spawns: list[list[str]] = []++ def fake_popen(cmd, **kwargs):+ if not cmd or str(cmd[0]) != "/fake/llama-server":+ return _REAL_POPEN(cmd, **kwargs)+ spawns.append(list(cmd))+ return type(+ "Process",+ (),+ {+ "pid": 123,+ "stdout": [line + "\n" for line in tail.splitlines()],+ "returncode": rc,+ "poll": lambda self: rc,+ "terminate": lambda self: None,+ "wait": lambda self, timeout = None: rc,+ "kill": lambda self: None,+ },+ )()++ with patch.object(subprocess, "Popen", side_effect = fake_popen):+ try:+ result = backend.load_model(+ GgufLoadIntent(gguf_path = str(gguf), model_identifier = "test")+ )+ error = None+ except RuntimeError as exc:+ result, error = None, str(exc)+ return spawns, result, error+++class TestLoadStopsAtTheFirstCrash:+ @pytest.mark.parametrize("rc", [_WIN_FAIL_FAST, -6, 134])+ def test_single_cuda_gpu_spawns_once_and_names_the_driver(self, tmp_path, rc):+ backend, gguf = _backend(tmp_path, [0])+ spawns, result, error = _crash_every_spawn(backend, gguf, _ISSUE_TAIL, rc)+ assert error is not None, (result, len(spawns))+ assert "NVIDIA driver" in error+ assert "enough memory" not in error+ assert len(spawns) == 1, [" ".join(s[-6:]) for s in spawns]++ def test_no_kernel_image_also_stops(self, tmp_path):+ backend, gguf = _backend(tmp_path, [0])+ spawns, _result, error = _crash_every_spawn(backend, gguf, _NO_IMAGE_TAIL, 134)+ assert error is not None and "no kernels for this GPU" in error+ assert len(spawns) == 1++ def test_rocm_crash_is_not_short_circuited(self, tmp_path):+ backend, gguf = _backend(tmp_path, [0])+ spawns, _result, error = _crash_every_spawn(backend, gguf, _ROCM_TAIL, 134)+ assert error is None or "NVIDIA driver" not in errordiff --git a/studio/install_llama_prebuilt.py b/studio/install_llama_prebuilt.pyindex 3059dcaa5c9..da9dea7a55b 100644--- a/studio/install_llama_prebuilt.py+++ b/studio/install_llama_prebuilt.py@@ -2054,6 +2054,10 @@ def linux_cuda_choice_from_release(selection_log.append("linux_cuda_selection: no Linux CUDA runtime line satisfied both runtime libraries and driver compatibility")+ if driver_below_cuda_prebuilt_floor(host):+ floor_message = cuda_driver_floor_message(host)+ selection_log.append(f"linux_cuda_selection: {floor_message}")+ log(floor_message)_warn_uncovered_cuda_host(host_sms,detected_runtime_lines,@@ -3375,13 +3379,17 @@ def detected_windows_runtime_lines() -> tuple[list[str], dict[str, list[str]]]:return _core.detected_windows_runtime_lines(_OPS)+driver_below_cuda_prebuilt_floor = _core.driver_below_cuda_prebuilt_floor+cuda_driver_floor_message = _core.cuda_driver_floor_message++def compatible_windows_runtime_lines(host: HostInfo) -> list[str]:if not host.driver_cuda_version:return []major, _minor = host.driver_cuda_version# cuda12 app bundles are toolkit-12.8 builds with bundled runtime libs; CUDA- # minor-version compatibility runs them on any 12.x driver, same as Linux.- if major < _MIN_CUDA_MAJOR:+ # minor-version compatibility runs them on a 12.x driver from 12.4 on (#12842).+ if major < _MIN_CUDA_MAJOR or driver_below_cuda_prebuilt_floor(host):return []return _cuda_runtime_lines_for_major(major)@@ -3657,6 +3665,10 @@ def published_windows_cuda_attempts("windows_cuda_selection: app-bundle runtime lines (major-gated)="+ (",".join(ordered_lines) if ordered_lines else "none"))+ if driver_below_cuda_prebuilt_floor(host):+ floor_message = cuda_driver_floor_message(host)+ selection_log.append(f"windows_cuda_selection: {floor_message}")+ log(floor_message)host_sms = normalize_compute_caps(host.compute_caps)attempts: list[AssetChoice] = []@@ -4202,6 +4214,19 @@ def resolve_release_asset_choice()if host.is_windows and host.is_x86_64 and (host.has_usable_nvidia or masked_host is not None):selection_host = masked_host or host+ if (+ driver_below_cuda_prebuilt_floor(selection_host)+ and selection_host.driver_cuda_version[0] >= _MIN_CUDA_MAJOR+ ):+ # #12842: the Windows source fallback needs a toolkit this driver runs, which+ # winget rarely offers, so setup would fail. CPU until the driver is updated.+ cpu_choice = published_asset_choice_for_kind(release, "windows-cpu")+ if cpu_choice is not None:+ log(+ f"{cuda_driver_floor_message(selection_host)} Installing the CPU "+ "bundle for now."+ )+ return apply_approved_hashes([cpu_choice], checksums)torch_preference = detect_torch_cuda_runtime_preference(selection_host, gpu_hidden_by_mask = masked_host is not None)@@ -7299,6 +7324,13 @@ def _fork_manifest_release_plans(last_error = excif not allow_older_release_fallback:raise+ # #12842: the floor is the host's, so every older release fails alike.+ if (+ (host.is_linux or host.is_windows)+ and host.has_physical_nvidia+ and driver_below_cuda_prebuilt_floor(host)+ ):+ raise PrebuiltFallback(cuda_driver_floor_message(host)) from exclog("published release skipped for install planning: "f"{bundle.repo}@{bundle.release_tag} upstream_tag={resolved_tag} ({exc})"@@ -9593,6 +9625,10 @@ def reusable_existing_install(install_dir: Path, host: HostInfo) -> bool:"""if not (install_dir / "UNSLOTH_PREBUILT_INFO.json").is_file():return True+ # #12842: below the floor a CUDA prebuilt still answers --version but loads no kernel.+ marker = load_prebuilt_metadata(install_dir) or {}+ if marker_backend(marker) == "cuda" and driver_below_cuda_prebuilt_floor(host):+ return Falsereturn _existing_install_runs(install_dir, host)@@ -11144,9 +11180,15 @@ def select_backend_install(cpu_mechanism = cpu_mechanism,host = host,)- requested_tag, release_plans = resolve_simple_install_release_plans(- llama_tag, route.host, route.published_repo, route.published_release_tag- )+ try:+ requested_tag, release_plans = resolve_simple_install_release_plans(+ llama_tag, route.host, route.published_repo, route.published_release_tag+ )+ except PrebuiltFallback as exc:+ # #12842: a stored cuda choice must fall back to detection, not fail the update.+ if route.backend == "cuda" and driver_below_cuda_prebuilt_floor(route.host):+ raise BackendUnavailable(str(exc)) from exc+ raiseif route.rocm_fallback_host is not None:release_plans = _with_rocm_behind_vulkan(release_plans,diff --git a/studio/prebuilt_core.py b/studio/prebuilt_core.pyindex 1c2e8046744..67a543fa03b 100644--- a/studio/prebuilt_core.py+++ b/studio/prebuilt_core.py@@ -151,6 +151,10 @@ _RATE_LIMIT_WAIT_CAP_SECONDS = 60.0_MIN_CUDA_MAJOR = 12_MAX_PROBE_CUDA_MAJOR = 19+# Toolkit 12.8+ prebuilts use nvcc -compress-mode=size, "not compatible with drivers released+# before CUDA Toolkit's 12.4 Release" (nvcc docs): every kernel load fails (#12842).+_COMPRESSED_FATBIN_MIN_DRIVER = (12, 4)+# Blackwell floor is sm_100 (B100/B200 sm_100, B300/GB300 sm_103 below consumer# RTX 50 sm_120); the family needs toolkit >= 12.8, sm_103/sm_121 need 12.9._BLACKWELL_MIN_SM = 100@@ -1677,11 +1681,27 @@ def detected_windows_runtime_lines(ops: ModuleOps) -> tuple[list[str], dict[str,return detected, runtime_dirs+def driver_below_cuda_prebuilt_floor(host: Any) -> bool:+ """A reported driver too old for any CUDA prebuilt (see _COMPRESSED_FATBIN_MIN_DRIVER)."""+ driver = host.driver_cuda_version+ return bool(driver) and tuple(driver[:2]) < _COMPRESSED_FATBIN_MIN_DRIVER+++def cuda_driver_floor_message(host: Any) -> str:+ major, minor = host.driver_cuda_version[:2]+ return (+ f"NVIDIA driver reports CUDA {major}.{minor}, older than the CUDA "+ f"{_COMPRESSED_FATBIN_MIN_DRIVER[0]}.{_COMPRESSED_FATBIN_MIN_DRIVER[1]} the CUDA "+ "llama.cpp prebuilts need (their kernels fail with 'device kernel image is invalid'); "+ "skipping them. Update the NVIDIA driver to R550 or newer (CUDA 12.4+) to use them."+ )++def compatible_linux_runtime_lines(host: Any) -> list[str]:if not host.driver_cuda_version:return []major, _minor = host.driver_cuda_version- if major < _MIN_CUDA_MAJOR:+ if major < _MIN_CUDA_MAJOR or driver_below_cuda_prebuilt_floor(host):return []return _cuda_runtime_lines_for_major(major)diff --git a/studio/setup.ps1 b/studio/setup.ps1index b192197238c..63680bcb118 100644--- a/studio/setup.ps1+++ b/studio/setup.ps1@@ -186,6 +186,7 @@ $script:CudaToolkitReady = $false$script:NvccPath = $null$script:CudaToolkitRoot = $null$script:CudaArch = $null+$script:DriverMaxCuda = $nullfunction Exit-SetupFailure {param(@@ -1363,6 +1364,15 @@ function Write-CudaDriverToolkitMismatch {substep "Or let Unsloth use the prebuilt CUDA bundle; it does not need the local toolkit." $Color}+# ggml's -compress-mode=size (toolkit >= 12.8) does not load on a driver below 12.4 (#12842).+function Test-CudaDriverNeedsUncompressedFatbin {+ param([string]$DriverMaxCuda)+ if ($DriverMaxCuda -notmatch '^(\d+)\.(\d+)$') { return $false }+ $major = [int]$Matches[1]+ $minor = [int]$Matches[2]+ return (($major -lt 12) -or (($major -eq 12) -and ($minor -lt 4)))+}+function Get-CudaComputeCapability {# $NvidiaSmiExe is an absolute path that survives Refresh-Environment. Not rediscovered# once detection rejected nvidia-smi: the driver library answered, and asking a wedged@@ -5374,6 +5384,7 @@ if (-not $CudaArch) {$script:NvccPath = $NvccPath$script:CudaToolkitRoot = $CudaToolkitRoot$script:CudaArch = $CudaArch+$script:DriverMaxCuda = $DriverMaxCuda$script:CudaToolkitReady = $true}@@ -10789,6 +10800,10 @@ if ($LocalLlamaCppLinked) {$CmakeArgs += '-DGGML_CUDA=OFF'} else {$CmakeArgs += '-DGGML_CUDA=ON'+ if (Test-CudaDriverNeedsUncompressedFatbin -DriverMaxCuda $script:DriverMaxCuda) {+ $CmakeArgs += '-DGGML_CUDA_COMPRESSION_MODE=none'+ substep "driver CUDA $script:DriverMaxCuda predates 12.4; building uncompressed CUDA kernels it can load." "Yellow"+ }# Accept a host MSVC newer than nvcc's whitelist, which would otherwise abort.$nvccAllowFlag = '-allow-unsupported-compiler'if ([string]::IsNullOrEmpty($env:NVCC_PREPEND_FLAGS)) {diff --git a/studio/setup.sh b/studio/setup.shindex df15e9aee22..3420537e10f 100755--- a/studio/setup.sh+++ b/studio/setup.sh@@ -1342,6 +1342,11 @@ _cuda_toolkit_major_gt_driver() {[ "$_toolkit_major" -gt "$_driver_major" ]}+# ggml's -compress-mode=size (toolkit >= 12.8) does not load on a driver below 12.4 (#12842).+_cuda_driver_needs_uncompressed_fatbin() {+ _cuda_version_gt "12.4" "${1:-}"+}+_cuda_nvcc_candidate_paths() {if command -v nvcc >/dev/null 2>&1; thencommand -v nvcc@@ -5381,6 +5386,10 @@ elseif [ -n "$CUDA_ARCHS" ]; thenCMAKE_ARGS="$CMAKE_ARGS -DGGML_CUDA=ON -DCMAKE_CUDA_ARCHITECTURES=${CUDA_ARCHS}"CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_CUDA_FLAGS=--threads=0"+ if _cuda_driver_needs_uncompressed_fatbin "$_DRIVER_MAX_CUDA"; then+ CMAKE_ARGS="$CMAKE_ARGS -DGGML_CUDA_COMPRESSION_MODE=none"+ substep "driver CUDA $_DRIVER_MAX_CUDA predates 12.4; building uncompressed CUDA kernels it can load." "$C_WARN"+ fi_BUILD_DESC="building (CUDA, sm_${CUDA_ARCHS//;/+sm_})"# Allow a host gcc/clang newer than nvcc's whitelist (else a freshdiff --git a/tests/studio/install/test_cuda_driver_floor_12842.py b/tests/studio/install/test_cuda_driver_floor_12842.pynew file mode 100644index 00000000000..7908d840f07--- /dev/null+++ b/tests/studio/install/test_cuda_driver_floor_12842.py@@ -0,0 +1,297 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0+"""#12842: no CUDA 12 prebuilt for a driver below CUDA 12.4 (compressed device code)."""++from __future__ import annotations++import importlib.util+import json+import sys+from pathlib import Path++import pytest++PACKAGE_ROOT = Path(__file__).resolve().parents[3]+MODULE_PATH = PACKAGE_ROOT / "studio" / "install_llama_prebuilt.py"+MODULE_NAME = "studio_install_llama_prebuilt"+if MODULE_NAME in sys.modules:+ m = sys.modules[MODULE_NAME]+else:+ SPEC = importlib.util.spec_from_file_location(MODULE_NAME, MODULE_PATH)+ assert SPEC is not None and SPEC.loader is not None+ m = importlib.util.module_from_spec(SPEC)+ sys.modules[MODULE_NAME] = m+ SPEC.loader.exec_module(m)+++# Trimmed from the b11443-mix-d65395f llama-prebuilt-manifest.json, as published.+TAG = "b11443-mix-d65395f"+_PROFILES = {+ "cuda12-legacy": ("cuda12", "legacy", ["50", "52", "60", "61"], 5, "12.8"),+ "cuda12-older": ("cuda12", "older", ["70", "75", "80", "86", "89"], 10, "12.8"),+ "cuda12-portable": (+ "cuda12",+ "portable",+ ["70", "75", "80", "86", "89", "90", "100", "103", "120"],+ 30,+ "12.8",+ ),+ "cuda13-older": ("cuda13", "older", ["75", "80", "86", "89"], 40, "13.3"),+ "cuda13-portable": (+ "cuda13",+ "portable",+ ["75", "80", "86", "89", "90", "100", "103", "120"],+ 60,+ "13.3",+ ),+}+++def _manifest_artifacts(platform, install_kind, ext):+ rows = []+ for profile, (line, coverage, sms, rank, toolkit) in _PROFILES.items():+ rows.append(+ {+ "asset_name": f"app-{TAG}-{platform}-{profile}.{ext}",+ "install_kind": install_kind,+ "bundle_profile": profile,+ "runtime_line": line,+ "coverage_class": coverage,+ "supported_sms": sms,+ "min_sm": int(sms[0]),+ "max_sm": int(sms[-1]),+ "rank": rank,+ "toolkit_version": toolkit,+ }+ )+ return rows+++def _release():+ raw = _manifest_artifacts("windows-x64", "windows-cuda", "zip") + _manifest_artifacts(+ "linux-x64", "linux-cuda", "tar.gz"+ )+ artifacts = [a for a in (m.parse_published_artifact(r) for r in raw) if a is not None]+ assert len(artifacts) == len(raw)+ return m.PublishedReleaseBundle(+ repo = "unslothai/llama.cpp",+ release_tag = TAG,+ upstream_tag = "b11443",+ assets = {a.asset_name: f"https://example.com/{a.asset_name}" for a in artifacts},+ artifacts = artifacts,+ )+++RELEASE = _release()+++def host(+ system,+ driver,+ caps = ("86",),+):+ return m.HostInfo(+ system = system,+ machine = "AMD64" if system == "Windows" else "x86_64",+ is_windows = system == "Windows",+ is_linux = system == "Linux",+ is_macos = False,+ is_x86_64 = True,+ is_arm64 = False,+ nvidia_smi = "nvidia-smi",+ driver_cuda_version = driver,+ compute_caps = list(caps),+ physical_compute_caps = list(caps),+ visible_cuda_devices = None,+ has_physical_nvidia = True,+ has_usable_nvidia = True,+ )++[email protected](autouse = True)+def _isolated(monkeypatch):+ for key in ("UNSLOTH_LLAMA_CPP_BACKEND", "CUDA_VISIBLE_DEVICES"):+ monkeypatch.delenv(key, raising = False)+ monkeypatch.setattr(+ m, "detect_torch_cuda_runtime_preference", lambda *a, **k: m.CudaRuntimePreference(None, [])+ )+ monkeypatch.setattr(m, "detected_windows_runtime_lines", lambda: ([], {}))+ # Both runtimes on disk, so only the driver decides on Linux.+ monkeypatch.setattr(+ m,+ "detected_linux_runtime_lines",+ lambda: (["cuda13", "cuda12"], {"cuda13": ["/usr/lib"], "cuda12": ["/usr/lib"]}),+ )+++def windows_profiles(driver, caps = ("86",)):+ return [+ a.bundle_profile+ for a in m.published_windows_cuda_attempts(host("Windows", driver, caps), RELEASE, None)+ ]+++def linux_profiles(driver, caps = ("86",)):+ selection = m.linux_cuda_choice_from_release(host("Linux", driver, caps), RELEASE)+ return [] if selection is None else [a.bundle_profile for a in selection.attempts]++[email protected]("driver", [(12, 0), (12, 2), (12, 3)])+class TestADriverBelowCuda124GetsNoCudaPrebuilt:+ def test_windows(self, driver):+ assert m.compatible_windows_runtime_lines(host("Windows", driver)) == []+ assert windows_profiles(driver) == []+ # Pascal included: the legacy bundle is a 12.8 build too.+ assert windows_profiles(driver, caps = ("61",)) == []++ def test_linux(self, driver):+ assert m.compatible_linux_runtime_lines(host("Linux", driver)) == []+ assert linux_profiles(driver) == []++ def test_the_selection_says_why(self, driver, capsys):+ m.published_windows_cuda_attempts(host("Windows", driver), RELEASE, None)+ m.linux_cuda_choice_from_release(host("Linux", driver), RELEASE)+ err = capsys.readouterr().err+ assert err.count(f"NVIDIA driver reports CUDA {driver[0]}.{driver[1]}") == 2+ assert "CUDA 12.4+" in err++ def test_a_kept_cuda12_install_no_longer_covers_the_host(self, driver):+ marker = {"backend": "cuda", "runtime_line": "cuda12", "supported_sms": ["86"]}+ assert not m._kept_install_covers_host(marker, host("Windows", driver))+ assert not m._kept_install_covers_host(marker, host("Linux", driver))++[email protected]("driver", [(12, 4), (12, 8)])+class TestACuda12DriverFromTheFloorStillGetsCuda12:+ def test_windows(self, driver):+ assert windows_profiles(driver) == ["cuda12-older", "cuda12-portable"]+ assert windows_profiles(driver, caps = ("61",)) == ["cuda12-legacy"]++ def test_linux(self, driver):+ assert linux_profiles(driver) == ["cuda12-older", "cuda12-portable"]++ def test_a_kept_cuda12_install_still_covers_the_host(self, driver):+ marker = {"backend": "cuda", "runtime_line": "cuda12", "supported_sms": ["86"]}+ assert m._kept_install_covers_host(marker, host("Windows", driver))+ assert m._kept_install_covers_host(marker, host("Linux", driver))+++def test_a_cuda13_driver_keeps_cuda13_first_then_cuda12():+ order = ["cuda13-older", "cuda13-portable", "cuda12-older", "cuda12-portable"]+ assert windows_profiles((13, 0)) == order+ assert linux_profiles((13, 0)) == order+++def test_an_unknown_driver_is_not_gated_by_the_floor():+ assert m.driver_below_cuda_prebuilt_floor(host("Windows", None)) is False++[email protected]("system", ["Windows", "Linux"])+def test_a_driver_below_the_floor_stops_the_release_walk(monkeypatch, system):+ walked = []++ def releases(*_args, **_kwargs):+ for _ in range(20):+ walked.append(1)+ yield type("Resolved", (), {"bundle": RELEASE, "checksums": None})()++ monkeypatch.setattr(m, "iter_resolved_published_releases", releases)+ monkeypatch.setattr(m, "apply_approved_hashes", lambda attempts, _checksums: attempts)+ monkeypatch.setattr(m, "github_release_assets", lambda _repo, _tag: {})+ with pytest.raises(m.PrebuiltFallback, match = "older than the CUDA 12.4"):+ m._fork_manifest_release_plans("latest", host(system, (12, 2)), "unslothai/llama.cpp", "")+ assert len(walked) == 1+++def _release_with_windows_cpu():+ cpu = m.parse_published_artifact(+ {+ "asset_name": f"app-{TAG}-windows-x64-cpu.zip",+ "install_kind": "windows-cpu",+ "bundle_profile": "windows-cpu-x64",+ "runtime_line": None,+ "coverage_class": None,+ "rank": 1000,+ }+ )+ assert cpu is not None+ return m.PublishedReleaseBundle(+ repo = RELEASE.repo,+ release_tag = RELEASE.release_tag,+ upstream_tag = RELEASE.upstream_tag,+ assets = {**RELEASE.assets, cpu.asset_name: f"https://example.com/{cpu.asset_name}"},+ artifacts = [*RELEASE.artifacts, cpu],+ )+++class TestWindowsBelowTheFloorGetsTheCpuBundle:+ def _choices(self, monkeypatch, driver):+ monkeypatch.setattr(m, "apply_approved_hashes", lambda attempts, _checksums: attempts)+ monkeypatch.setattr(m, "github_release_assets", lambda _repo, _tag: {})+ return m.resolve_release_asset_choice(+ host("Windows", driver), "b11443", _release_with_windows_cpu(), None+ )++ def test_a_12_2_driver_gets_the_cpu_bundle(self, monkeypatch):+ choices = self._choices(monkeypatch, (12, 2))+ assert [c.install_kind for c in choices] == ["windows-cpu"]++ @pytest.mark.parametrize("driver", [(12, 4), (13, 0)])+ def test_a_driver_from_the_floor_keeps_cuda(self, monkeypatch, driver):+ choices = self._choices(monkeypatch, driver)+ assert choices and all(c.install_kind == "windows-cuda" for c in choices)++ def test_a_cuda_11_driver_keeps_its_old_route(self, monkeypatch):+ assert self._choices(monkeypatch, (11, 8)) == []+++class TestTheSourceStageDoesNotKeepABrokenCudaPrebuilt:+ def _tree(self, tmp_path, marker):+ if marker is not None:+ (tmp_path / "UNSLOTH_PREBUILT_INFO.json").write_text(+ json.dumps(marker), encoding = "utf-8"+ )+ return tmp_path++ def test_a_cuda_prebuilt_below_the_floor_is_rebuilt(self, tmp_path, monkeypatch):+ monkeypatch.setattr(m, "_existing_install_runs", lambda *_a: True)+ tree = self._tree(tmp_path, {"backend": "cuda", "runtime_line": "cuda12"})+ assert not m.reusable_existing_install(tree, host("Linux", (12, 2)))++ @pytest.mark.parametrize("driver", [(12, 4), (13, 0), None])+ def test_from_the_floor_it_is_kept_as_before(self, tmp_path, monkeypatch, driver):+ monkeypatch.setattr(m, "_existing_install_runs", lambda *_a: True)+ tree = self._tree(tmp_path, {"backend": "cuda", "runtime_line": "cuda12"})+ assert m.reusable_existing_install(tree, host("Linux", driver))++ def test_a_genuine_source_build_is_kept_as_before(self, tmp_path):+ assert m.reusable_existing_install(self._tree(tmp_path, None), host("Linux", (12, 2)))++ def test_a_cpu_prebuilt_is_kept_as_before(self, tmp_path, monkeypatch):+ monkeypatch.setattr(m, "_existing_install_runs", lambda *_a: True)+ tree = self._tree(tmp_path, {"backend": "cpu"})+ assert m.reusable_existing_install(tree, host("Linux", (12, 2)))+++class TestAStoredCudaChoiceBelowTheFloorFallsBackToDetection:+ def _select(self, monkeypatch, driver):+ def no_plans(*_args, **_kwargs):+ raise m.PrebuiltFallback("no compatible Linux prebuilt asset was found")++ monkeypatch.setattr(m, "resolve_simple_install_release_plans", no_plans)+ return m.select_backend_install(+ backend = "cuda",+ llama_tag = "latest",+ published_repo = "unslothai/llama.cpp",+ published_release_tag = "",+ host = host("Linux", driver),+ )++ def test_below_the_floor_it_reads_as_unavailable(self, monkeypatch):+ with pytest.raises(m.BackendUnavailable):+ self._select(monkeypatch, (12, 2))++ def test_from_the_floor_the_failure_is_unchanged(self, monkeypatch):+ with pytest.raises(m.PrebuiltFallback) as excinfo:+ self._select(monkeypatch, (12, 8))+ assert not isinstance(excinfo.value, m.BackendUnavailable)diff --git a/tests/studio/install/test_cuda_source_build_old_driver_12842.py b/tests/studio/install/test_cuda_source_build_old_driver_12842.pynew file mode 100644index 00000000000..c1811b5f296--- /dev/null+++ b/tests/studio/install/test_cuda_source_build_old_driver_12842.py@@ -0,0 +1,150 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""#12842: a CUDA source build on a driver below CUDA 12.4 passes GGML_CUDA_COMPRESSION_MODE=none."""++from __future__ import annotations++import re+import shutil+import subprocess+from pathlib import Path++import pytest++from unsloth_pwsh_runner import run_pwsh+++PACKAGE_ROOT = Path(__file__).resolve().parents[3]+SETUP_SH_TEXT = (PACKAGE_ROOT / "studio" / "setup.sh").read_text(encoding = "utf-8")+SETUP_PS1_TEXT = (PACKAGE_ROOT / "studio" / "setup.ps1").read_text(encoding = "utf-8")+++def _usable_bash():+ # On Windows a bare which() can find System32's WSL bash.exe with no distro behind it.+ exe = shutil.which("bash")+ if exe is None:+ return None+ try:+ probe = subprocess.run(+ [exe, "-c", "printf ok"],+ stdout = subprocess.PIPE,+ stderr = subprocess.DEVNULL,+ text = True,+ errors = "replace",+ timeout = 60,+ )+ except (OSError, subprocess.TimeoutExpired):+ return None+ return exe if probe.stdout.strip() == "ok" else None+++BASH = _usable_bash()+PWSH = shutil.which("pwsh")++# (driver CUDA version as the scripts parse it, needs uncompressed kernels)+CASES = [+ ("11.8", True),+ ("12.0", True),+ ("12.2", True),+ ("12.3", True),+ ("12.4", False),+ ("12.8", False),+ ("13.0", False),+ ("13.1", False),+ ("", False),+ ("unknown", False),+]+++def _sh_function(name):+ start = SETUP_SH_TEXT.index(f"{name}() {{")+ end = SETUP_SH_TEXT.index("\n}\n", start) + len("\n}\n")+ return SETUP_SH_TEXT[start:end]+++def _ps1_function(name):+ match = re.search(+ rf"^function {re.escape(name)} \{{.*?^\}}\n", SETUP_PS1_TEXT, flags = re.DOTALL | re.MULTILINE+ )+ assert match is not None, f"setup.ps1 function not found: {name}"+ return match.group(0)++[email protected](BASH is None, reason = "a working bash is required")[email protected](("driver", "expected"), CASES)+def test_setup_sh_decision(tmp_path, driver, expected):+ script = tmp_path / "fns.sh"+ script.write_text(+ _sh_function("_cuda_version_gt") + _sh_function("_cuda_driver_needs_uncompressed_fatbin"),+ encoding = "utf-8",+ newline = "\n", # bash cannot parse a CRLF fragment, the Windows default+ )+ result = subprocess.run(+ [+ BASH,+ "-c",+ 'set -u; . "$1"; if _cuda_driver_needs_uncompressed_fatbin "$2"; then echo yes; else echo no; fi',+ "_",+ str(script),+ driver,+ ],+ capture_output = True,+ text = True,+ check = True,+ )+ assert result.stdout.strip() == ("yes" if expected else "no")++[email protected](PWSH is None, reason = "PowerShell is unavailable")+def test_setup_ps1_decision():+ function = _ps1_function("Test-CudaDriverNeedsUncompressedFatbin")+ calls = "".join(+ f"Write-Output ('{driver}=' + (Test-CudaDriverNeedsUncompressedFatbin -DriverMaxCuda '{driver}'))\n"+ for driver, _ in CASES+ )+ # $null is what the script holds when no driver was detected.+ calls += (+ "Write-Output ('null=' + (Test-CudaDriverNeedsUncompressedFatbin -DriverMaxCuda $null))\n"+ )+ result = run_pwsh(+ [+ PWSH,+ "-NoProfile",+ "-NonInteractive",+ "-Command",+ "Set-StrictMode -Version Latest\n" + function + calls,+ ],+ check = True,+ capture_output = True,+ text = True,+ )+ lines = result.stdout.strip().splitlines()+ expected = [f"{driver}={expected}" for driver, expected in CASES] + ["null=False"]+ assert lines == expected+++def test_setup_sh_wires_the_flag_into_the_cuda_build():+ on = SETUP_SH_TEXT.index('CMAKE_ARGS="$CMAKE_ARGS -DGGML_CUDA=ON -DCMAKE_CUDA_ARCHITECTURES=')+ block = SETUP_SH_TEXT[on : SETUP_SH_TEXT.index("_BUILD_DESC=", on)]+ assert '_cuda_driver_needs_uncompressed_fatbin "$_DRIVER_MAX_CUDA"' in block+ assert "-DGGML_CUDA_COMPRESSION_MODE=none" in block+ assert SETUP_SH_TEXT.rindex('_DRIVER_MAX_CUDA="$(_cuda_driver_max_version)"', 0, on) > 0+++def test_setup_ps1_wires_the_flag_into_the_cuda_build():+ on = SETUP_PS1_TEXT.index("$CmakeArgs += '-DGGML_CUDA=ON'")+ block = SETUP_PS1_TEXT[on : SETUP_PS1_TEXT.index('$CmakeArgs += "-DCMAKE_CUDA_COMPILER=', on)]+ assert "Test-CudaDriverNeedsUncompressedFatbin -DriverMaxCuda $script:DriverMaxCuda" in block+ assert "-DGGML_CUDA_COMPRESSION_MODE=none" in block+ # $DriverMaxCuda is local to Resolve-CudaToolkit; the build reads the published copy.+ # Its body holds column-0 braces, so slice to the publish block that ends it.+ start = SETUP_PS1_TEXT.index("function Resolve-CudaToolkit {")+ resolve = SETUP_PS1_TEXT[+ start : SETUP_PS1_TEXT.index("$script:CudaToolkitReady = $true\n}", start)+ ]+ assert "$script:DriverMaxCuda = $DriverMaxCuda" in resolve+ assert (+ "$script:DriverMaxCuda = $null"+ in SETUP_PS1_TEXT[: SETUP_PS1_TEXT.index("function Resolve-CudaToolkit")]+ )diff --git a/tests/studio/install/test_selection_logic.py b/tests/studio/install/test_selection_logic.pyindex a0a83abdf4d..32791d324a8 100644--- a/tests/studio/install/test_selection_logic.py+++ b/tests/studio/install/test_selection_logic.py@@ -558,11 +558,11 @@ class TestCompatibleWindowsRuntimeLines:assert compatible_windows_runtime_lines(host) == ["cuda12"]@pytest.mark.parametrize("minor", [0, 1, 2, 3])- def test_cuda12_runs_on_any_12_x_driver(self, minor):- # Regression: Windows previously gated cuda12 below a 12.4 driver, but minor-version compat runs toolkit-12.8- # bundles on any 12.x driver, same as Linux.+ def test_cuda12_needs_a_12_4_driver(self, minor):+ # Minor-version compat alone would run the toolkit-12.8 bundles on any 12.x driver, but+ # their -compress-mode=size device code does not load before 12.4 (#12842).host = make_host(driver_cuda_version = (12, minor))- assert compatible_windows_runtime_lines(host) == ["cuda12"]+ assert compatible_windows_runtime_lines(host) == []def test_driver_13_1(self):host = make_host(driver_cuda_version = (13, 1))