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.

[Bug] Unsloth Studio: chat-only installs run llama.cpp on the CPU because the CUDA bundles ship no cudart/cublas (#17463)

Leo Borcherdinge91e7be4d088
diff --git a/studio/install_llama_prebuilt.py b/studio/install_llama_prebuilt.py
index 277e59132fe..253a2485bf3 100644
--- a/studio/install_llama_prebuilt.py
+++ b/studio/install_llama_prebuilt.py
@@ -28,6 +28,7 @@ import stat
import struct
import subprocess
import sys
+import sysconfig
import tempfile
import textwrap
import time
@@ -10969,6 +10970,8 @@ def install_prebuilt(
override_rocm_gfx = override_rocm_gfx,
cpu_mechanism = force_cpu,
)
+ # Before the kept-install check and selection, which both read the runtime on disk.
+ ensure_linux_cuda_runtime_before_selection(initial_route)
if existing_install_current_without_plan(
install_dir,
llama_tag = llama_tag,
@@ -11494,6 +11497,130 @@ def resolve_backends_payload(
}
+# The fork's x64 CUDA bundles ship no cudart/cublas; full installs borrow torch's, chat-only installs
+# have none and llama-server silently runs on the CPU. NVIDIA's pip wheels fill the gap, once per CUDA line.
+_CUDA_RUNTIME_WHEELS = {
+ ("windows-cuda", 12): ("nvidia-cuda-runtime-cu12", "nvidia-cublas-cu12==12.9.2.10"),
+ ("windows-cuda", 13): ("nvidia-cuda-runtime", "nvidia-cublas"),
+ ("linux-cuda", 12): ("nvidia-cuda-runtime-cu12", "nvidia-cublas-cu12"),
+ ("linux-cuda", 13): ("nvidia-cuda-runtime", "nvidia-cublas"),
+}
+
+
+def _x64_cuda_install_kind(asset: str) -> str | None:
+ """windows-cuda or linux-cuda for an x64 CUDA asset name. The marker records the asset but no
+ install kind, so the name is what tells an install's platform and architecture."""
+ if not re.search(r"cuda-?\d+", asset) or not re.search(r"-x64[-.]", asset):
+ return None
+ if re.search(r"-win(dows)?-", asset):
+ return "windows-cuda"
+ return "linux-cuda" if "-linux-" in asset else None
+
+
+def _missing_cuda_runtime_wheels(install_dir: Path) -> tuple[str, ...]:
+ """The wheels to install when this CUDA install lacks cudart or cublas of its major, else ()."""
+ asset = str((load_prebuilt_metadata(install_dir) or {}).get("asset") or "")
+ match = re.search(r"cuda-?(\d+)", asset)
+ kind = _x64_cuda_install_kind(asset)
+ wheels = _CUDA_RUNTIME_WHEELS.get((kind, int(match.group(1)) if match else 0))
+ if not wheels:
+ return ()
+ major = match.group(1)
+ if kind == "linux-cuda":
+ # The detector selection uses: a second, narrower scan would call a runtime that
+ # selection found (an Ollama or versioned toolkit dir) missing.
+ return () if f"cuda{major}" in detected_linux_runtime_lines()[0] else wheels
+ site_packages = Path(sysconfig.get_paths()["purelib"])
+ roots = [install_dir, site_packages / "torch" / "lib", site_packages / "nvidia"]
+ cuda_home = os.environ.get("CUDA_PATH")
+ if cuda_home:
+ roots.append(Path(cuda_home))
+ # The loader's own dirs (PATH, Program Files toolkit): checked flat, PATH holds System32.
+ flat_dirs = [Path(d) for d in windows_runtime_dirs()]
+
+ def _found(name: str) -> bool:
+ if any((d / name).is_file() for d in flat_dirs):
+ return True
+ for root in roots:
+ try:
+ if root.is_dir() and next(root.rglob(name), None) is not None:
+ return True
+ except OSError:
+ continue
+ return False
+
+ # ggml-cuda needs all three, as windows_runtime_line_info does; a runtime-only toolkit has cudart alone.
+ if all(_found(f"{lib}64_{major}.dll") for lib in ("cudart", "cublas", "cublasLt")):
+ return ()
+ return wheels
+
+
+def _install_cuda_runtime_wheels(wheels: tuple[str, ...]) -> bool:
+ """Installs these wheels into this env; False when the install failed."""
+ uv = shutil.which("uv")
+ cmd = (
+ [uv, "pip", "install", "--python", sys.executable]
+ if uv
+ else [sys.executable, "-m", "pip", "install", "--disable-pip-version-check"]
+ )
+ log(f"no CUDA runtime found for llama.cpp; installing {', '.join(wheels)}")
+ result = subprocess.run([*cmd, "--only-binary", ":all:", *wheels], timeout = 1800)
+ if result.returncode != 0:
+ log(
+ f"CUDA runtime wheel install failed (exit {result.returncode}); llama.cpp will run on the CPU"
+ )
+ return result.returncode == 0
+
+
+def _linux_cuda_runtime_wheels_to_supply(host: HostInfo) -> tuple[str, ...]:
+ """The wheels a Linux NVIDIA host needs before selection can pick a CUDA bundle, else ().
+
+ linux_cuda_choice_from_release only takes a CUDA bundle whose runtime line is on disk, so a
+ host with the driver and nothing else selects none and falls back to a source build. The
+ line supplied is the newest one the driver runs; CUDA 13 dropped pre-Turing GPUs, which
+ take CUDA 12.
+ """
+ if not (host.is_linux and host.is_x86_64 and host.has_usable_nvidia):
+ return ()
+ pre_turing = any(int(sm) < _TURING_MIN_SM for sm in normalize_compute_caps(host.compute_caps))
+ # A CUDA 13 runtime cannot serve a pre-Turing GPU, so it does not count as present.
+ driver_lines = [
+ line
+ for line in compatible_linux_runtime_lines(host)
+ if not (pre_turing and int(line.removeprefix("cuda")) >= 13)
+ ]
+ if any(line in driver_lines for line in detected_linux_runtime_lines()[0]):
+ return ()
+ for line in driver_lines:
+ major = int(line.removeprefix("cuda"))
+ wheels = _CUDA_RUNTIME_WHEELS.get(("linux-cuda", major))
+ if wheels:
+ return wheels
+ return ()
+
+
+def ensure_linux_cuda_runtime_before_selection(route: BackendRoute) -> None:
+ """Installs the CUDA runtime wheels a chat-only Linux NVIDIA host lacks; never raises."""
+ try:
+ if route.backend not in (None, "auto", "cuda"):
+ return
+ wheels = _linux_cuda_runtime_wheels_to_supply(route.host)
+ if wheels:
+ _install_cuda_runtime_wheels(wheels)
+ except Exception as exc:
+ log(f"CUDA runtime wheel step skipped: {exc}")
+
+
+def ensure_cuda_runtime_wheels(install_dir: Path) -> None:
+ """Installs NVIDIA's runtime wheels into this env when the CUDA install has none; never raises."""
+ try:
+ wheels = _missing_cuda_runtime_wheels(install_dir)
+ if wheels:
+ _install_cuda_runtime_wheels(wheels)
+ except Exception as exc:
+ log(f"CUDA runtime wheel step skipped: {exc}")
+
+
def main() -> int:
args = parse_args()
if args.check_existing_install is not None:
@@ -11507,6 +11634,8 @@ def main() -> int:
runs = _existing_install_runs(install_dir, host) and _kept_install_covers_host(
load_prebuilt_metadata(install_dir), host
)
+ # A CUDA install with no runtime falls back to the CPU, so let setup reinstall and fill it.
+ runs = runs and not _missing_cuda_runtime_wheels(install_dir)
except Exception as exc:
print(f"install check failed: {exc}", file = sys.stderr)
runs = False
@@ -11613,6 +11742,7 @@ def main() -> int:
llama_backend = requested_backend_arg(args),
instruction_cleanup_root = install_arg.absolute(),
)
+ ensure_cuda_runtime_wheels(install_arg.resolve())
return EXIT_SUCCESS
diff --git a/tests/studio/install/test_no_torch_cuda_runtime_wheels.py b/tests/studio/install/test_no_torch_cuda_runtime_wheels.py
new file mode 100644
index 00000000000..ae9486184a0
--- /dev/null
+++ b/tests/studio/install/test_no_torch_cuda_runtime_wheels.py
@@ -0,0 +1,256 @@
+# 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 CUDA install with no cudart anywhere gets NVIDIA's runtime wheels; anything else gets none.
+On Linux the wheels go in before selection, which only takes a CUDA bundle whose runtime is on disk."""
+
+from __future__ import annotations
+
+import json
+import pathlib
+import sys
+
+import pytest
+
+REPO_ROOT = pathlib.Path(__file__).resolve().parents[3]
+sys.path.insert(0, str(REPO_ROOT / "studio"))
+
+import install_llama_prebuilt as ip # noqa: E402
+
+
+def _install(tmp_path, asset, kind):
+ # The fields the installer's marker has: it records the backend and the asset, no install kind.
+ (tmp_path / "UNSLOTH_PREBUILT_INFO.json").write_text(
+ json.dumps({"asset": asset, "backend": ip.backend_for_install_kind(kind)})
+ )
+ return tmp_path
+
+
+def _host(
+ system = "Linux",
+ machine = "x86_64",
+ driver = (13, 0),
+ caps = ("86",),
+ nvidia = True,
+):
+ return ip.HostInfo(
+ system = system,
+ machine = machine,
+ is_windows = system == "Windows",
+ is_linux = system == "Linux",
+ is_macos = False,
+ is_x86_64 = machine == "x86_64",
+ is_arm64 = machine == "aarch64",
+ nvidia_smi = "nvidia-smi" if nvidia else None,
+ driver_cuda_version = driver if nvidia else None,
+ compute_caps = list(caps) if nvidia else [],
+ visible_cuda_devices = None,
+ has_physical_nvidia = nvidia,
+ has_usable_nvidia = nvidia,
+ )
+
+
[email protected](autouse = True)
+def _no_host_runtime(monkeypatch, tmp_path_factory):
+ site = tmp_path_factory.mktemp("site")
+ monkeypatch.setattr(ip.sysconfig, "get_paths", lambda: {"purelib": str(site)})
+ monkeypatch.delenv("CUDA_PATH", raising = False)
+ monkeypatch.setattr(ip, "detected_linux_runtime_lines", lambda: ([], {}))
+ monkeypatch.setattr(ip, "windows_runtime_dirs", lambda: [])
+ return site
+
+
+ "asset, kind, wheels",
+ [
+ (
+ "app-b1-windows-x64-cuda13-newer.zip",
+ "windows-cuda",
+ ("nvidia-cuda-runtime", "nvidia-cublas"),
+ ),
+ (
+ "app-b1-windows-x64-cuda12-newer.zip",
+ "windows-cuda",
+ ("nvidia-cuda-runtime-cu12", "nvidia-cublas-cu12==12.9.2.10"),
+ ),
+ (
+ "app-b1-linux-x64-cuda12-newer.tar.gz",
+ "linux-cuda",
+ ("nvidia-cuda-runtime-cu12", "nvidia-cublas-cu12"),
+ ),
+ ("app-b1-windows-x64-cpu.zip", "windows-cpu", ()),
+ ("app-b1-windows-arm64-cuda13-portable.zip", "windows-arm64-cuda", ()),
+ ],
+)
+def test_wheels_for_a_bare_install(tmp_path, asset, kind, wheels):
+ assert ip._missing_cuda_runtime_wheels(_install(tmp_path, asset, kind)) == wheels
+
+
+def test_bundled_cudart_needs_nothing(tmp_path):
+ install = _install(tmp_path, "app-b1-windows-x64-cuda13-newer.zip", "windows-cuda")
+ (install / "build" / "bin" / "Release").mkdir(parents = True)
+ (install / "build" / "bin" / "Release" / "cudart64_13.dll").touch()
+ (install / "build" / "bin" / "Release" / "cublas64_13.dll").touch()
+ (install / "build" / "bin" / "Release" / "cublasLt64_13.dll").touch()
+ assert ip._missing_cuda_runtime_wheels(install) == ()
+
+
+def test_torch_runtime_needs_nothing(tmp_path, _no_host_runtime):
+ (_no_host_runtime / "torch" / "lib").mkdir(parents = True)
+ (_no_host_runtime / "torch" / "lib" / "cudart64_12.dll").touch()
+ (_no_host_runtime / "torch" / "lib" / "cublas64_12.dll").touch()
+ (_no_host_runtime / "torch" / "lib" / "cublasLt64_12.dll").touch()
+ install = _install(tmp_path, "app-b1-windows-x64-cuda12-newer.zip", "windows-cuda")
+ assert ip._missing_cuda_runtime_wheels(install) == ()
+
+
+def test_other_major_does_not_count(tmp_path, _no_host_runtime):
+ (_no_host_runtime / "torch" / "lib").mkdir(parents = True)
+ (_no_host_runtime / "torch" / "lib" / "cudart64_12.dll").touch()
+ install = _install(tmp_path, "app-b1-windows-x64-cuda13-newer.zip", "windows-cuda")
+ assert ip._missing_cuda_runtime_wheels(install) == ("nvidia-cuda-runtime", "nvidia-cublas")
+
+
+def test_the_marker_the_installer_writes_is_recognised(tmp_path):
+ # Through the real writer: a hand-written marker with fields the installer never records
+ # would pass here while every real install went unnoticed.
+ choice = ip.AssetChoice(
+ repo = "unslothai/llama.cpp",
+ tag = "b1",
+ name = "app-b1-windows-x64-cuda13-newer.zip",
+ url = "https://example.invalid/app-b1-windows-x64-cuda13-newer.zip",
+ source_label = "published",
+ is_ready_bundle = True,
+ install_kind = "windows-cuda",
+ runtime_line = "cuda13",
+ expected_sha256 = "a" * 64,
+ )
+ checksums = ip.ApprovedReleaseChecksums(
+ repo = choice.repo,
+ release_tag = "b1",
+ upstream_tag = "b1",
+ artifacts = {
+ choice.name: ip.ApprovedArtifactHash(
+ choice.name, "a" * 64, choice.repo, choice.install_kind
+ )
+ },
+ )
+ ip.write_prebuilt_metadata(
+ tmp_path,
+ requested_tag = "latest",
+ llama_tag = "b1",
+ release_tag = "b1",
+ choice = choice,
+ approved_checksums = checksums,
+ prebuilt_fallback_used = False,
+ )
+ assert ip._missing_cuda_runtime_wheels(tmp_path) == ("nvidia-cuda-runtime", "nvidia-cublas")
+
+
+def test_linux_runtime_selection_can_see_needs_nothing(tmp_path, monkeypatch):
+ # Wherever selection found it (a versioned toolkit dir, Ollama's), not just the env.
+ monkeypatch.setattr(ip, "detected_linux_runtime_lines", lambda: (["cuda12"], {}))
+ install = _install(tmp_path, "app-b1-linux-x64-cuda12-newer.tar.gz", "linux-cuda")
+ assert ip._missing_cuda_runtime_wheels(install) == ()
+
+
+ "host, detected, wheels",
+ [
+ (_host(driver = (13, 0)), [], ("nvidia-cuda-runtime", "nvidia-cublas")),
+ (_host(driver = (12, 8)), [], ("nvidia-cuda-runtime-cu12", "nvidia-cublas-cu12")),
+ # CUDA 13 dropped pre-Turing GPUs.
+ (
+ _host(driver = (13, 0), caps = ("61",)),
+ [],
+ ("nvidia-cuda-runtime-cu12", "nvidia-cublas-cu12"),
+ ),
+ # A runtime the driver cannot run is no runtime.
+ (_host(driver = (12, 8)), ["cuda13"], ("nvidia-cuda-runtime-cu12", "nvidia-cublas-cu12")),
+ (_host(driver = (13, 0)), ["cuda12"], ()),
+ (_host(driver = (13, 0)), ["cuda13", "cuda12"], ()),
+ # A CUDA 13 runtime cannot serve a pre-Turing GPU either.
+ (
+ _host(driver = (13, 0), caps = ("61",)),
+ ["cuda13"],
+ ("nvidia-cuda-runtime-cu12", "nvidia-cublas-cu12"),
+ ),
+ (_host(driver = (13, 0), caps = ("61",)), ["cuda13", "cuda12"], ()),
+ (_host(nvidia = False), [], ()),
+ (_host(machine = "aarch64"), [], ()),
+ (_host(system = "Windows"), [], ()),
+ ],
+)
+def test_wheels_a_linux_host_needs_before_selection(monkeypatch, host, detected, wheels):
+ monkeypatch.setattr(ip, "detected_linux_runtime_lines", lambda: (list(detected), {}))
+ assert ip._linux_cuda_runtime_wheels_to_supply(host) == wheels
+
+
+ "backend, installs", [(None, 1), ("auto", 1), ("cuda", 1), ("vulkan", 0), ("cpu", 0)]
+)
+def test_only_a_cuda_bound_request_supplies_the_runtime(monkeypatch, backend, installs):
+ calls = []
+ monkeypatch.setattr(ip, "_install_cuda_runtime_wheels", lambda wheels: calls.append(wheels))
+ route = ip.BackendRoute(
+ backend = backend,
+ host = _host(),
+ published_repo = "unslothai/llama.cpp",
+ published_release_tag = "",
+ persist_llama_backend = None,
+ persist_rocm_gfx = None,
+ )
+ ip.ensure_linux_cuda_runtime_before_selection(route)
+ assert len(calls) == installs
+
+
+def test_a_failing_wheel_step_does_not_stop_the_install(monkeypatch):
+ def boom(wheels):
+ raise OSError("no network")
+
+ monkeypatch.setattr(ip, "_install_cuda_runtime_wheels", boom)
+ route = ip.BackendRoute(
+ backend = "auto",
+ host = _host(),
+ published_repo = "unslothai/llama.cpp",
+ published_release_tag = "",
+ persist_llama_backend = None,
+ persist_rocm_gfx = None,
+ )
+ ip.ensure_linux_cuda_runtime_before_selection(route)
+
+
+def test_cudart_without_cublas_still_installs(tmp_path, monkeypatch):
+ toolkit = tmp_path / "toolkit"
+ (toolkit / "bin").mkdir(parents = True)
+ (toolkit / "bin" / "cudart64_13.dll").touch()
+ monkeypatch.setenv("CUDA_PATH", str(toolkit))
+ install = tmp_path / "install"
+ install.mkdir()
+ _install(install, "app-b1-windows-x64-cuda13-newer.zip", "windows-cuda")
+ assert ip._missing_cuda_runtime_wheels(install) == ("nvidia-cuda-runtime", "nvidia-cublas")
+
+
+def test_windows_loader_dirs_count(tmp_path, monkeypatch):
+ on_path = tmp_path / "on_path"
+ on_path.mkdir()
+ (on_path / "cudart64_13.dll").touch()
+ (on_path / "cublas64_13.dll").touch()
+ (on_path / "cublasLt64_13.dll").touch()
+ monkeypatch.setattr(ip, "windows_runtime_dirs", lambda: [str(on_path)])
+ install = tmp_path / "install"
+ install.mkdir()
+ _install(install, "app-b1-windows-x64-cuda13-newer.zip", "windows-cuda")
+ assert ip._missing_cuda_runtime_wheels(install) == ()
+
+
+def test_cublas_without_cublaslt_still_installs(tmp_path, monkeypatch):
+ on_path = tmp_path / "on_path"
+ on_path.mkdir()
+ (on_path / "cudart64_13.dll").touch()
+ (on_path / "cublas64_13.dll").touch()
+ monkeypatch.setattr(ip, "windows_runtime_dirs", lambda: [str(on_path)])
+ install = tmp_path / "install"
+ install.mkdir()
+ _install(install, "app-b1-windows-x64-cuda13-newer.zip", "windows-cuda")
+ assert ip._missing_cuda_runtime_wheels(install) == ("nvidia-cuda-runtime", "nvidia-cublas")