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
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
"""Backs the in-app update check for source-build installs; network and host detection are stubbed."""
from __future__ import annotations
import contextlib
import ctypes
import dataclasses
import hashlib
import importlib
import json
import ntpath
import os
import subprocess
import sys
from pathlib import Path
from types import ModuleType, SimpleNamespace
import pytest
_studio = Path(__file__).resolve().parent.parent.parent
if str(_studio) not in sys.path:
sys.path.insert(0, str(_studio))
ilp = importlib.import_module("install_llama_prebuilt")
if not hasattr(ilp, "resolve_simple_install_release_plans"):
pytest.skip("PR symbols not present - check branch", allow_module_level = True)
# Captured before the autouse fixture stubs it, so the probe's own tests reach the real one.
_REAL_AMD_VULKAN_ICD_PRESENT = ilp._amd_vulkan_icd_present
FORK = ilp.DEFAULT_PUBLISHED_REPO
UPSTREAM = ilp.UPSTREAM_REPO
@pytest.fixture(autouse = True)
def _no_ambient_hip_device_mask(monkeypatch):
"""Clears inherited device-mask variables so the host is described by HostInfo alone, not the shell."""
for _env in ("HIP_VISIBLE_DEVICES", "ROCR_VISIBLE_DEVICES", "CUDA_VISIBLE_DEVICES"):
monkeypatch.delenv(_env, raising = False)
@pytest.fixture(autouse = True)
def _no_amd_vulkan_icd(monkeypatch):
"""Forces the AMD Vulkan ICD probe absent, since mesa ships that manifest on non-AMD hosts too."""
monkeypatch.setattr(ilp, "_amd_vulkan_icd_present", lambda: False)
def _host(**kw):
base = dict(
system = "Linux",
machine = "x86_64",
is_windows = False,
is_linux = False,
is_macos = False,
is_x86_64 = False,
is_arm64 = False,
nvidia_smi = None,
driver_cuda_version = None,
compute_caps = [],
visible_cuda_devices = None,
has_physical_nvidia = False,
has_usable_nvidia = False,
has_rocm = False,
rocm_gfx_target = None,
macos_version = None,
)
base.update(kw)
return ilp.HostInfo(**base)
def test_force_cpu_clears_all_gpu_attributes_including_intel():
# --cpu-fallback must drop EVERY GPU attribute incl. has_intel_gpu, or Vulkan is prepended.
host = _host(
is_linux = True,
is_x86_64 = True,
has_usable_nvidia = True,
has_physical_nvidia = True,
has_rocm = True,
rocm_gfx_target = "gfx1100",
has_intel_gpu = True,
)
forced = ilp._apply_host_overrides(host, force_cpu = True)
assert forced.has_usable_nvidia is False
assert forced.has_physical_nvidia is False
assert forced.has_rocm is False
assert forced.rocm_gfx_target is None
assert forced.has_intel_gpu is False
def test_macos_upstream_pin_only_for_explicit_pre26_upstream():
pre26 = _host(
system = "Darwin",
is_macos = True,
is_arm64 = True,
machine = "arm64",
macos_version = (15, 5),
)
assert ilp.pinned_macos_release_tag(pre26, UPSTREAM) == "b9415"
assert ilp.pinned_macos_release_tag(pre26, FORK) is None
tahoe = _host(
system = "Darwin",
is_macos = True,
is_arm64 = True,
machine = "arm64",
macos_version = (26, 0),
)
assert ilp.pinned_macos_release_tag(tahoe, UPSTREAM) is None
def _run_resolve(
monkeypatch,
capsys,
plans_or_exc,
*,
host = None,
extra_args = (),
):
monkeypatch.setattr(
ilp,
"detect_host",
lambda: host or _host(system = "Darwin", is_macos = True, is_arm64 = True, machine = "arm64"),
)
def _resolver(tag, host, repo, published_release_tag):
if isinstance(plans_or_exc, Exception):
raise plans_or_exc
return ("b9585", plans_or_exc)
monkeypatch.setattr(ilp, "resolve_simple_install_release_plans", _resolver)
monkeypatch.setattr(
sys,
"argv",
[
"install_llama_prebuilt.py",
"--resolve-prebuilt",
"latest",
"--output-format",
"json",
*extra_args,
],
)
rc = ilp.main()
assert rc == ilp.EXIT_SUCCESS
return json.loads(capsys.readouterr().out.strip().splitlines()[-1])
def test_resolve_prebuilt_available(monkeypatch, capsys):
plan = SimpleNamespace(
release_tag = "b9585",
llama_tag = "b9585",
attempts = [
SimpleNamespace(name = "llama-b9585-bin-macos-arm64.tar.gz", install_kind = "macos-arm64")
],
)
out = _run_resolve(monkeypatch, capsys, [plan])
assert out["prebuilt_available"] is True
assert out["repo"] == FORK
assert out["release_tag"] == "b9585"
assert out["asset"] == "llama-b9585-bin-macos-arm64.tar.gz"
assert out["install_kind"] == "macos-arm64"
def test_resolve_prebuilt_unavailable(monkeypatch, capsys):
out = _run_resolve(monkeypatch, capsys, ilp.PrebuiltFallback("no macOS asset"))
assert out["prebuilt_available"] is False
assert out["repo"] == FORK
def test_resolve_prebuilt_backend_flag_filters_cpu_fallback(monkeypatch, capsys):
cpu_plan = ilp.InstallReleasePlan(
requested_tag = "latest",
llama_tag = "b9585",
release_tag = "release",
attempts = [
ilp.AssetChoice(
repo = FORK,
tag = "release",
name = "app-release-linux-x64-cpu",
url = "https://example/cpu",
source_label = "published",
install_kind = "linux-cpu",
)
],
approved_checksums = SimpleNamespace(),
)
out = _run_resolve(
monkeypatch,
capsys,
[cpu_plan],
host = _host(is_linux = True, is_x86_64 = True),
extra_args = ("--llama-backend", "vulkan"),
)
assert out["prebuilt_available"] is False
def _run_resolve_capture_host(monkeypatch, capsys):
"""Drive --resolve-prebuilt and return the host the resolver was handed."""
seen = {}
def _resolver(tag, host, repo, published_release_tag):
seen["repo"] = repo
seen["host"] = host
raise ilp.PrebuiltFallback("no asset")
monkeypatch.setattr(ilp, "resolve_simple_install_release_plans", _resolver)
monkeypatch.setattr(
sys,
"argv",
["install_llama_prebuilt.py", "--resolve-prebuilt", "latest", "--output-format", "json"],
)
assert ilp.main() == ilp.EXIT_SUCCESS
out = json.loads(capsys.readouterr().out.strip().splitlines()[-1])
return seen, out
def test_resolve_prebuilt_cpu_linux_routes_to_fork(monkeypatch, capsys):
monkeypatch.setattr(ilp, "detect_host", lambda: _host(is_linux = True, is_x86_64 = True))
seen, out = _run_resolve_capture_host(monkeypatch, capsys)
assert seen["repo"] == FORK
assert out["repo"] == FORK
def test_resolve_prebuilt_rocm_sdk_only_host_still_offered_cpu(monkeypatch, capsys):
# ROCm tools on PATH without an AMD GPU must not reclassify a CPU host as ROCm.
monkeypatch.setattr(ilp, "detect_host", lambda: _host(is_linux = True, is_x86_64 = True))
monkeypatch.setattr(
ilp.shutil, "which", lambda tool: "/opt/rocm/bin/hipconfig" if tool == "hipconfig" else None
)
seen, out = _run_resolve_capture_host(monkeypatch, capsys)
assert seen["repo"] == FORK
assert seen["host"].has_rocm is False
# Blackwell floor is sm_100 (data-center), below consumer sm_120.
def _gpu_linux_host(caps):
return _host(
is_linux = True,
is_x86_64 = True,
has_physical_nvidia = True,
has_usable_nvidia = True,
driver_cuda_version = (13, 1),
compute_caps = caps,
)
def _linux_cuda_artifact(runtime_line, supported_sms, min_sm, max_sm, profile):
return ilp.PublishedLlamaArtifact(
asset_name = f"app-b9739-linux-x64-{profile}.tar.gz",
install_kind = "linux-cuda",
runtime_line = runtime_line,
coverage_class = "newer",
supported_sms = supported_sms,
min_sm = min_sm,
max_sm = max_sm,
bundle_profile = profile,
rank = 50,
)
def test_linux_blackwell_override_prefers_cuda13_for_datacenter(monkeypatch):
# Coverage alone can't decide; only the sm_100 Blackwell floor lifts cuda13 to the front.
cuda12 = _linux_cuda_artifact(
"cuda12", ["86", "89", "90", "100", "120"], 86, 120, "cuda12-newer"
)
cuda13 = _linux_cuda_artifact(
"cuda13", ["86", "89", "90", "100", "103", "120"], 86, 120, "cuda13-newer"
)
release = ilp.PublishedReleaseBundle(
repo = FORK,
release_tag = "b9739-mix",
upstream_tag = "b9739",
assets = {cuda12.asset_name: "https://x/cuda12", cuda13.asset_name: "https://x/cuda13"},
artifacts = [cuda12, cuda13],
)
monkeypatch.setattr(
ilp,
"detected_linux_runtime_lines",
lambda: (["cuda13", "cuda12"], {"cuda13": ["/usr/lib"], "cuda12": ["/usr/lib"]}),
)
selection = ilp.linux_cuda_choice_from_release(
_gpu_linux_host(["10.0"]), release, preferred_runtime_line = "cuda12"
)
assert selection is not None
assert selection.primary.runtime_line == "cuda13"
assert selection.primary.bundle_profile == "cuda13-newer"
def test_drop_blackwell_incapable_windows_cuda_applies_to_datacenter():
host = _host(
system = "Windows",
is_windows = True,
is_x86_64 = True,
has_physical_nvidia = True,
has_usable_nvidia = True,
compute_caps = ["10.0"],
)
cuda124 = ilp.AssetChoice(
repo = FORK,
tag = "b9739",
name = "llama-b9739-bin-win-cuda-12.4-x64.zip",
url = "https://x/124",
source_label = "published",
install_kind = "windows-cuda",
)
cuda13 = ilp.AssetChoice(
repo = FORK,
tag = "b9739",
name = "app-b9739-windows-x64-cuda13-newer.zip",
url = "https://x/13",
source_label = "published",
install_kind = "windows-cuda",
max_sm = 120,
)
kept = ilp._drop_blackwell_incapable_windows_cuda(host, [cuda124, cuda13])
assert [a.name for a in kept] == [cuda13.name]
def test_sm103_host_drops_cuda128_windows_build():
host = _host(
system = "Windows",
is_windows = True,
is_x86_64 = True,
has_physical_nvidia = True,
has_usable_nvidia = True,
compute_caps = ["10.3"],
)
cuda128 = ilp.AssetChoice(
repo = FORK,
tag = "b9739",
name = "llama-b9739-bin-win-cuda-12.8-x64.zip",
url = "https://x/128",
source_label = "published",
install_kind = "windows-cuda",
)
cuda129 = ilp.AssetChoice(
repo = FORK,
tag = "b9739",
name = "llama-b9739-bin-win-cuda-12.9-x64.zip",
url = "https://x/129",
source_label = "published",
install_kind = "windows-cuda",
)
kept = ilp._drop_blackwell_incapable_windows_cuda(host, [cuda128, cuda129])
assert [a.name for a in kept] == [cuda129.name]
b200 = _host(
system = "Windows",
is_windows = True,
is_x86_64 = True,
has_physical_nvidia = True,
has_usable_nvidia = True,
compute_caps = ["10.0"],
)
kept_b200 = ilp._drop_blackwell_incapable_windows_cuda(b200, [cuda128, cuda129])
assert [a.name for a in kept_b200] == [cuda128.name, cuda129.name]
def _fixture_digest(name: str) -> str:
"""Stand-in for the digest GitHub publishes; a fixture without one selects nothing."""
return hashlib.sha256(name.encode()).hexdigest()
def _upstream_release(tag, asset_names):
return {
"tag_name": tag,
"assets": [
{
"name": n,
"browser_download_url": f"https://example/{n}",
"digest": f"sha256:{_fixture_digest(n)}",
}
for n in asset_names
],
}
def test_direct_upstream_arm64_intel_prefers_vulkan():
host = _host(is_linux = True, is_arm64 = True, machine = "aarch64", has_intel_gpu = True)
rel = _upstream_release(
"b9925",
["llama-b9925-bin-ubuntu-vulkan-arm64.tar.gz", "llama-b9925-bin-ubuntu-arm64.tar.gz"],
)
plan = ilp.direct_upstream_release_plan(rel, host, UPSTREAM, "latest")
kinds = [a.install_kind for a in plan.attempts]
assert kinds[0] == "linux-vulkan", kinds
assert "linux-arm64" in kinds
assert plan.attempts[0].name == "llama-b9925-bin-ubuntu-vulkan-arm64.tar.gz"
def test_direct_upstream_intel_with_hidden_nvidia_is_cpu_only():
# Vulkan ignores CUDA_VISIBLE_DEVICES and could grab a hidden NVIDIA card; fall to CPU.
host = _host(
is_linux = True,
is_x86_64 = True,
has_intel_gpu = True,
has_physical_nvidia = True,
has_usable_nvidia = False,
)
rel = _upstream_release(
"b9925",
["llama-b9925-bin-ubuntu-vulkan-x64.tar.gz", "llama-b9925-bin-ubuntu-x64.tar.gz"],
)
plan = ilp.direct_upstream_release_plan(rel, host, UPSTREAM, "latest")
assert [a.install_kind for a in plan.attempts] == ["linux-cpu"]
def test_direct_upstream_arm64_without_intel_is_cpu_only():
host = _host(is_linux = True, is_arm64 = True, machine = "aarch64")
rel = _upstream_release(
"b9925",
["llama-b9925-bin-ubuntu-vulkan-arm64.tar.gz", "llama-b9925-bin-ubuntu-arm64.tar.gz"],
)
plan = ilp.direct_upstream_release_plan(rel, host, UPSTREAM, "latest")
assert [a.install_kind for a in plan.attempts] == ["linux-arm64"]
def test_direct_upstream_x86_intel_prefers_vulkan():
host = _host(is_linux = True, is_x86_64 = True, has_intel_gpu = True)
rel = _upstream_release(
"b9925",
["llama-b9925-bin-ubuntu-vulkan-x64.tar.gz", "llama-b9925-bin-ubuntu-x64.tar.gz"],
)
plan = ilp.direct_upstream_release_plan(rel, host, UPSTREAM, "latest")
kinds = [a.install_kind for a in plan.attempts]
assert kinds[0] == "linux-vulkan", kinds
assert "linux-cpu" in kinds
def _published_vulkan_bundle(*install_kinds):
profiles = {
"linux-vulkan": "linux-vulkan-x64",
"windows-vulkan": "windows-vulkan-x64",
"linux-cpu": "linux-cpu-x64",
"linux-arm64": "linux-cpu-arm64",
"windows-cpu": "windows-cpu-x64",
}
artifacts = [
ilp.PublishedLlamaArtifact(
asset_name = f"app-release-{install_kind}",
install_kind = install_kind,
runtime_line = None,
coverage_class = None,
supported_sms = [],
min_sm = None,
max_sm = None,
bundle_profile = profiles.get(install_kind),
rank = 60,
)
for install_kind in install_kinds
]
return ilp.PublishedReleaseBundle(
repo = FORK,
release_tag = "release",
upstream_tag = "b9925",
assets = {
artifact.asset_name: f"https://example/{artifact.asset_name}" for artifact in artifacts
},
artifacts = artifacts,
)
def test_fork_linux_intel_prefers_published_vulkan_bundle():
bundle = _published_vulkan_bundle("linux-vulkan", "linux-cpu")
attempts = ilp._linux_published_attempts(
_host(is_linux = True, is_x86_64 = True, has_intel_gpu = True),
bundle,
)
assert [attempt.install_kind for attempt in attempts] == ["linux-vulkan", "linux-cpu"]
assert attempts[0].source_label == "published"
assert ["llama-diffusion-gemma-visual-server"] in ilp.runtime_payload_health_groups(
attempts[0].install_kind, source_label = attempts[0].source_label
)
def test_fork_linux_arm64_does_not_select_x64_vulkan_bundle():
bundle = _published_vulkan_bundle("linux-vulkan", "linux-arm64")
attempts = ilp._linux_published_attempts(
_host(
machine = "aarch64",
is_linux = True,
is_arm64 = True,
has_intel_gpu = True,
),
bundle,
)
assert [attempt.install_kind for attempt in attempts] == ["linux-arm64"]
def test_fork_windows_intel_prefers_published_vulkan_bundle():
bundle = _published_vulkan_bundle("windows-vulkan", "windows-cpu")
checksums = ilp.ApprovedReleaseChecksums(
repo = FORK,
release_tag = bundle.release_tag,
upstream_tag = bundle.upstream_tag,
artifacts = {
artifact.asset_name: ilp.ApprovedArtifactHash(
asset_name = artifact.asset_name,
sha256 = "a" * 64,
repo = FORK,
kind = artifact.install_kind,
)
for artifact in bundle.artifacts
},
)
attempts = ilp.resolve_release_asset_choice(
_host(
system = "Windows",
is_windows = True,
is_x86_64 = True,
has_intel_gpu = True,
),
bundle.upstream_tag,
bundle,
checksums,
)
assert [attempt.install_kind for attempt in attempts] == ["windows-vulkan", "windows-cpu"]
assert attempts[0].source_label == "published"
assert ["llama-diffusion-gemma-visual-server.exe"] in ilp.runtime_payload_health_groups(
attempts[0].install_kind, source_label = attempts[0].source_label
)
def test_linux_vulkan_health_glob_matches_bare_cpu_lib():
# Glob must cover arch-suffixed (x64) and bare (arm64) CPU libs, or Vulkan installs get flagged.
choice = ilp.AssetChoice(
repo = UPSTREAM,
tag = "b9925",
name = "llama-b9925-bin-ubuntu-vulkan-arm64.tar.gz",
url = "https://example/x",
source_label = "upstream",
install_kind = "linux-vulkan",
)
groups = ilp.runtime_payload_health_groups(choice.install_kind)
assert ["libggml-cpu*.so*"] in groups
assert ["libggml-cpu-*.so*"] not in groups
@pytest.mark.parametrize(
"backend, legacy, expected_backend, expected",
[
(None, None, None, False),
("vulkan", None, "vulkan", True),
(" VULKAN ", None, "vulkan", True),
("auto", "1", "auto", False),
(None, "true", None, True),
(None, "on", None, True),
("auto", None, "auto", False),
("cpu", "0", "cpu", False),
("cuda", "1", "cuda", False),
("hip", "1", "rocm", False),
("rocm", "on", "rocm", False),
(" HIP ", "1", "rocm", False),
(" ROCM ", "1", "rocm", False),
],
)
def test_force_vulkan_requested_accepts_public_selector_and_legacy_alias(
monkeypatch, backend, legacy, expected_backend, expected
):
# A recognized non-vulkan backend outranks a stale legacy alias; hip/rocm share a marker.
for name, value in (
("UNSLOTH_LLAMA_CPP_BACKEND", backend),
("UNSLOTH_FORCE_VULKAN", legacy),
):
if value is None:
monkeypatch.delenv(name, raising = False)
else:
monkeypatch.setenv(name, value)
assert ilp.llama_backend_from_env() == expected_backend
assert ilp.force_vulkan_requested() is expected
def test_forced_vulkan_filters_cpu_fallback_before_validation():
vulkan = ilp.AssetChoice(
repo = UPSTREAM,
tag = "b9925",
name = "llama-b9925-bin-ubuntu-vulkan-x64.tar.gz",
url = "https://example/vulkan",
source_label = "upstream",
install_kind = "linux-vulkan",
)
cpu = ilp.AssetChoice(
repo = UPSTREAM,
tag = "b9925",
name = "llama-b9925-bin-ubuntu-x64.tar.gz",
url = "https://example/cpu",
source_label = "upstream",
install_kind = "linux-cpu",
)
assert ilp._backend_only_attempts([vulkan, cpu], "vulkan") == [vulkan]
assert ilp._backend_only_attempts([vulkan, cpu], "cpu") == [cpu]
def test_forced_vulkan_skips_cpu_only_release_plans():
vulkan = ilp.AssetChoice(
repo = UPSTREAM,
tag = "b9924",
name = "llama-b9924-bin-ubuntu-vulkan-x64.tar.gz",
url = "https://example/vulkan",
source_label = "upstream",
install_kind = "linux-vulkan",
)
cpu = ilp.AssetChoice(
repo = UPSTREAM,
tag = "b9925",
name = "llama-b9925-bin-ubuntu-x64.tar.gz",
url = "https://example/cpu",
source_label = "upstream",
install_kind = "linux-cpu",
)
plans = [
ilp.InstallReleasePlan("latest", "b9925", "b9925", [cpu], SimpleNamespace()),
ilp.InstallReleasePlan("latest", "b9924", "b9924", [vulkan], SimpleNamespace()),
]
filtered = ilp._backend_only_release_plans(plans, "vulkan")
assert [plan.release_tag for plan in filtered] == ["b9924"]
assert filtered[0].attempts == [vulkan]
def test_route_to_vulkan_prebuilt_auto_intel_keeps_fork_release():
host = _host(is_linux = True, is_x86_64 = True, has_intel_gpu = True)
routed, repo, tag, _persist = ilp._route_to_vulkan_prebuilt(
host, FORK, "b9596-mix-abc", force_cpu = False
)
assert repo == FORK
assert tag == "b9596-mix-abc"
assert routed.has_intel_gpu is True
def test_route_to_vulkan_prebuilt_preserves_explicit_upstream_pin():
host = _host(is_linux = True, is_x86_64 = True, has_intel_gpu = True)
_routed, repo, tag, _persist = ilp._route_to_vulkan_prebuilt(
host, UPSTREAM, "b9596", force_cpu = False
)
assert repo == UPSTREAM
assert tag == "b9596"
def _linux_arm64_vulkan_host():
return _host(
system = "Linux",
machine = "aarch64",
is_linux = True,
is_arm64 = True,
has_intel_gpu = True,
)
def test_route_to_vulkan_prebuilt_linux_arm64_stays_on_the_fork():
# The fork publishes a linux-vulkan-arm64 bundle, so a forced-Vulkan Linux ARM64
# host keeps planning against the fork and its pin; nothing comes from upstream.
routed, repo, tag, persist = ilp._route_to_vulkan_prebuilt(
_linux_arm64_vulkan_host(),
FORK,
"b9596-mix-abc",
force_cpu = False,
llama_backend = "vulkan",
)
assert repo == FORK
assert tag == "b9596-mix-abc"
assert persist == "vulkan"
assert routed.has_intel_gpu is True
def test_forced_vulkan_linux_arm64_still_resolves_a_vulkan_bundle():
# End to end: the fork's ARM64 Vulkan bundle survives the strict-Vulkan filter.
routed, repo, _tag, _persist = ilp._route_to_vulkan_prebuilt(
_linux_arm64_vulkan_host(),
FORK,
"",
force_cpu = False,
llama_backend = "vulkan",
)
assert repo == FORK
bundle = _published_vulkan_bundle("linux-arm64")
bundle.artifacts.insert(
0,
ilp.PublishedLlamaArtifact(
asset_name = "app-release-linux-arm64-vulkan",
install_kind = "linux-vulkan",
runtime_line = None,
coverage_class = None,
supported_sms = [],
min_sm = None,
max_sm = None,
bundle_profile = "linux-vulkan-arm64",
rank = 60,
),
)
bundle.assets["app-release-linux-arm64-vulkan"] = "https://example.invalid/arm64-vulkan.tar.gz"
attempts = ilp._linux_published_attempts(routed, bundle)
assert [attempt.name for attempt in attempts][:1] == ["app-release-linux-arm64-vulkan"]
def test_route_to_vulkan_prebuilt_linux_arm64_keeps_an_explicit_repo_override():
other = "someone/llama.cpp"
_routed, repo, tag, _persist = ilp._route_to_vulkan_prebuilt(
_linux_arm64_vulkan_host(),
other,
"b9596",
force_cpu = False,
llama_backend = "vulkan",
)
assert repo == other
assert tag == "b9596"
def test_route_to_vulkan_prebuilt_linux_x86_64_keeps_the_fork_bundle():
# Only ARM64 lacks a fork Vulkan bundle; x86_64 must stay on the fork release
# so it keeps the DiffusionGemma visual server.
_routed, repo, tag, _persist = ilp._route_to_vulkan_prebuilt(
_host(is_linux = True, is_x86_64 = True, has_intel_gpu = True),
FORK,
"b9596-mix-abc",
force_cpu = False,
llama_backend = "vulkan",
)
assert repo == FORK
assert tag == "b9596-mix-abc"
def test_route_to_vulkan_prebuilt_cpu_fallback_wins():
host = _host(is_linux = True, is_x86_64 = True, has_intel_gpu = True)
routed, repo, tag, _persist = ilp._route_to_vulkan_prebuilt(
host, FORK, "b9596-mix-abc", force_cpu = True
)
assert repo == FORK
assert tag == "b9596-mix-abc"
assert routed is host
@pytest.mark.parametrize("via", ["env", "argument"])
@pytest.mark.parametrize("backend", ["hip", "rocm", "cpu"])
def test_non_vulkan_backend_suppresses_intel_auto_route(monkeypatch, backend, via):
# Pinned to Linux ARM64: the only host where suppression changes a returned value.
kwargs = {}
if via == "env":
monkeypatch.setenv("UNSLOTH_LLAMA_CPP_BACKEND", backend)
else:
kwargs["llama_backend"] = backend
host = _linux_arm64_vulkan_host()
routed, repo, tag, persist = ilp._route_to_vulkan_prebuilt(
host, FORK, "b9596-mix-abc", force_cpu = False, **kwargs
)
assert routed is host
assert repo == FORK, repo
assert tag == "b9596-mix-abc", tag
assert persist is None
@pytest.mark.parametrize("cpu_flag", ["--cpu-fallback", "--force-cpu"])
def test_resolve_prebuilt_cpu_fallback_overrides_intel_vulkan(monkeypatch, capsys, cpu_flag):
"""Either CPU flag via CLI must suppress Vulkan even on an Intel GPU host: both
drop GPU detection (--force-cpu additionally persists, on the install path)."""
monkeypatch.setattr(
ilp,
"detect_host",
lambda: _host(is_linux = True, is_x86_64 = True, has_intel_gpu = True),
)
seen = {}
def _resolver(tag, host, repo, published_release_tag):
seen["host"] = host
seen["repo"] = repo
raise ilp.PrebuiltFallback("no asset")
monkeypatch.setattr(ilp, "resolve_simple_install_release_plans", _resolver)
monkeypatch.setattr(
sys,
"argv",
[
"install_llama_prebuilt.py",
"--resolve-prebuilt",
"latest",
cpu_flag,
"--output-format",
"json",
],
)
assert ilp.main() == ilp.EXIT_SUCCESS
assert seen["host"].has_intel_gpu is False
assert seen["repo"] == FORK
def test_cpu_backend_env_forces_cpu_in_resolver(monkeypatch, capsys):
monkeypatch.setenv("UNSLOTH_LLAMA_CPP_BACKEND", "cpu")
monkeypatch.setattr(
ilp,
"detect_host",
lambda: _host(is_linux = True, is_x86_64 = True, has_intel_gpu = True),
)
seen, _ = _run_resolve_capture_host(monkeypatch, capsys)
assert seen["host"].has_intel_gpu is False
assert seen["repo"] == FORK
def test_cpu_backend_env_forces_cpu_in_direct_install(monkeypatch, tmp_path):
monkeypatch.setenv("UNSLOTH_LLAMA_CPP_BACKEND", "cpu")
monkeypatch.setattr(
ilp,
"detect_host",
lambda: _host(is_linux = True, is_x86_64 = True, has_intel_gpu = True),
)
seen = {}
def _route(
host,
repo,
tag,
*,
force_cpu,
llama_backend = None,
):
seen["host"] = host
seen["force_cpu"] = force_cpu
raise RuntimeError("stop after routing")
monkeypatch.setattr(ilp, "_route_to_vulkan_prebuilt", _route)
with pytest.raises(RuntimeError, match = "stop after routing"):
ilp.install_prebuilt(tmp_path, "latest", FORK, "")
assert seen["host"].has_intel_gpu is False
assert seen["force_cpu"] is True
@pytest.mark.parametrize(
"flags, expect_force, expect_backend",
[
([], False, None),
# Transient CPU recovery does not persist.
(["--cpu-fallback"], True, None),
# Deliberate CPU selection persists.
(["--force-cpu"], False, "cpu"),
(["--cpu-fallback", "--force-cpu"], True, "cpu"),
(["--llama-backend", "vulkan"], False, "vulkan"),
(["--llama-backend", "cuda", "--force-cpu"], False, "cuda"),
],
)
def test_cli_cpu_flags_thread_force_and_backend(
monkeypatch, tmp_path, flags, expect_force, expect_backend
):
captured = {}
monkeypatch.setattr(ilp, "install_prebuilt", lambda **kw: captured.update(kw))
monkeypatch.setattr(
sys,
"argv",
["install_llama_prebuilt.py", "--install-dir", str(tmp_path / "llama.cpp"), *flags],
)
assert ilp.main() == ilp.EXIT_SUCCESS
assert captured["force_cpu"] is expect_force
assert captured["llama_backend"] == expect_backend
@pytest.mark.parametrize(
"existing, requested, expected",
[
(False, True, True),
(None, True, True),
(True, True, True),
(False, False, False),
(True, False, False),
],
)
def test_sync_marker_selection_records_force_cpu(tmp_path, existing, requested, expected):
marker = {"tag": "b9585", "asset": "llama-b9585-bin-ubuntu-x64.tar.gz"}
if existing is not None:
marker["force_cpu"] = existing
marker_path = tmp_path / "UNSLOTH_PREBUILT_INFO.json"
marker_path.write_text(json.dumps(marker))
ilp.sync_marker_selection(
tmp_path,
choice = _choice("linux-cpu", "llama-b9585-bin-ubuntu-x64.tar.gz"),
backend_request = "cpu" if requested else "auto",
persist_force_cpu = requested,
)
written = json.loads(marker_path.read_text())
assert written["force_cpu"] is expected
assert written["asset"] == "llama-b9585-bin-ubuntu-x64.tar.gz"
def test_sync_marker_selection_records_the_backend_of_a_reused_bundle(tmp_path):
"""A reused bundle still needs its recorded backend corrected, or the next update re-detects it away."""
marker_path = tmp_path / "UNSLOTH_PREBUILT_INFO.json"
marker_path.write_text(
json.dumps({"tag": "b9585", "asset": "cpu.tar.gz", "backend_request": "auto"})
)
ilp.sync_marker_selection(
tmp_path,
choice = _choice("linux-cpu", "cpu.tar.gz"),
backend_request = "cpu",
persist_force_cpu = True,
)
written = json.loads(marker_path.read_text())
assert written["backend_request"] == "cpu"
assert written["backend"] == "cpu"
assert written["force_cpu"] is True
def test_sync_marker_selection_missing_marker_is_noop(tmp_path):
ilp.sync_marker_selection(
tmp_path,
choice = _choice("linux-cpu", "cpu.tar.gz"),
backend_request = "cpu",
persist_force_cpu = True,
)
assert not (tmp_path / "UNSLOTH_PREBUILT_INFO.json").exists()
def test_route_to_vulkan_prebuilt_hidden_nvidia_not_rerouted():
# Hidden NVIDIA + Intel: no Vulkan auto-route, since Vulkan ignores CUDA_VISIBLE_DEVICES.
host = _host(
is_linux = True,
is_x86_64 = True,
has_intel_gpu = True,
has_physical_nvidia = True,
has_usable_nvidia = False,
)
_routed, repo, _tag, _persist = ilp._route_to_vulkan_prebuilt(host, FORK, "", force_cpu = False)
assert repo == FORK
def test_route_to_vulkan_prebuilt_rocm_host_not_rerouted():
host = _host(is_linux = True, is_x86_64 = True, has_intel_gpu = True, has_rocm = True)
_routed, repo, _tag, _persist = ilp._route_to_vulkan_prebuilt(host, FORK, "", force_cpu = False)
assert repo == FORK
def test_route_to_vulkan_prebuilt_non_intel_unchanged():
host = _host(is_linux = True, is_x86_64 = True)
routed, repo, _tag, _persist = ilp._route_to_vulkan_prebuilt(host, FORK, "", force_cpu = False)
assert repo == FORK
assert routed is host
def test_resolve_prebuilt_intel_host_keeps_fork_manifest(monkeypatch, capsys):
monkeypatch.setattr(
ilp, "detect_host", lambda: _host(is_linux = True, is_x86_64 = True, has_intel_gpu = True)
)
seen, out = _run_resolve_capture_host(monkeypatch, capsys)
assert seen["repo"] == FORK
assert out["repo"] == FORK
class _FakeRegKey:
def __init__(
self,
subkeys = None,
values = None,
denied = False,
):
self.subkeys = subkeys or {}
self.values = values or {}
self.denied = denied
def __enter__(self):
return self
def __exit__(self, *exc):
return False
class _FakeWinreg:
HKEY_LOCAL_MACHINE = object()
def __init__(self, root_key):
self._root_key = root_key
def OpenKey(self, parent, name):
if parent is self.HKEY_LOCAL_MACHINE:
# Pin the production constant: a typo'd class GUID must fail here,
# not silently return the fake tree.
if name != ilp._WINDOWS_DISPLAY_CLASS_KEY:
raise FileNotFoundError(name)
if self._root_key is None:
raise FileNotFoundError(name)
return self._root_key
key = parent.subkeys.get(name)
if key is None:
# Real winreg raises OSError, never KeyError, for a missing key.
raise FileNotFoundError(name)
if key.denied:
raise PermissionError(name)
return key
def QueryInfoKey(self, key):
return (len(key.subkeys), len(key.values), 0)
def EnumKey(self, key, index):
return list(key.subkeys)[index]
def QueryValueEx(self, key, value_name):
if value_name not in key.values:
raise FileNotFoundError(value_name)
return (key.values[value_name], 1)
def _probe_with_display_class(monkeypatch, adapters):
# The helper lazily imports winreg; plant the fake in sys.modules.
monkeypatch.setitem(sys.modules, "winreg", _FakeWinreg(_FakeRegKey(subkeys = adapters)))
return ilp.windows_intel_gpu_in_registry()
def test_windows_intel_registry_matches_vendor_id(monkeypatch):
assert (
_probe_with_display_class(
monkeypatch,
{
"0000": _FakeRegKey(
values = {
"MatchingDeviceId": r"PCI\VEN_8086&DEV_56A0&SUBSYS_12345678",
"DriverDesc": "Intel(R) Arc(TM) A770 Graphics",
}
),
},
)
is True
)
def test_windows_intel_registry_matches_driver_desc_without_device_id(monkeypatch):
assert (
_probe_with_display_class(
monkeypatch,
{
"0000": _FakeRegKey(values = {"DriverDesc": "Intel(R) UHD Graphics 630"}),
},
)
is True
)
def test_windows_intel_registry_ignores_non_intel_adapters(monkeypatch):
assert (
_probe_with_display_class(
monkeypatch,
{
"0000": _FakeRegKey(
values = {
"MatchingDeviceId": r"PCI\VEN_10DE&DEV_2684",
"DriverDesc": "NVIDIA GeForce RTX 4090",
}
),
"0001": _FakeRegKey(
values = {
"MatchingDeviceId": r"PCI\VEN_1002&DEV_744C",
"DriverDesc": "AMD Radeon RX 7900 XTX",
}
),
},
)
is False
)
def test_windows_intel_registry_skips_restricted_properties_subkey(monkeypatch):
# The real class key has an ACL-restricted Properties subkey; access denials must not abort.
assert (
_probe_with_display_class(
monkeypatch,
{
"Properties": _FakeRegKey(denied = True),
"0000": _FakeRegKey(denied = True),
"0001": _FakeRegKey(
values = {
"MatchingDeviceId": r"PCI\VEN_8086&DEV_56A0",
}
),
},
)
is True
)
def test_windows_intel_registry_missing_class_key_is_false(monkeypatch):
monkeypatch.setitem(sys.modules, "winreg", _FakeWinreg(None))
assert ilp.windows_intel_gpu_in_registry() is False
def _detect_windows_host(
monkeypatch,
winreg_fake,
powershell_stdout = "",
env = None,
):
"""Real detect_host() on a GPU-less Windows host: registry is read first, CIM only on a registry
miss."""
monkeypatch.setitem(sys.modules, "winreg", winreg_fake)
monkeypatch.setattr(ilp.platform, "system", lambda: "Windows")
monkeypatch.setattr(ilp.platform, "machine", lambda: "AMD64")
for _env in (
"CUDA_VISIBLE_DEVICES",
"HIP_VISIBLE_DEVICES",
"ROCR_VISIBLE_DEVICES",
"HIP_PATH",
"ROCM_PATH",
):
monkeypatch.delenv(_env, raising = False)
# After the wipe, or the wipe would undo the very mask a caller set.
for _name, _value in (env or {}).items():
monkeypatch.setenv(_name, _value)
monkeypatch.setattr(
ilp.shutil,
"which",
lambda name: "powershell" if name in ("powershell", "pwsh") else None,
)
captured = []
def _fake_run_capture(command, **kwargs):
captured.append(command[0])
if command[0] == "powershell":
return SimpleNamespace(returncode = 0, stdout = powershell_stdout, stderr = "")
return SimpleNamespace(returncode = 1, stdout = "", stderr = "")
monkeypatch.setattr(ilp, "run_capture", _fake_run_capture)
return ilp.detect_host(), captured
def test_detect_host_registry_intel_skips_cim_probe(monkeypatch):
winreg = _FakeWinreg(
_FakeRegKey(
subkeys = {
"0000": _FakeRegKey(values = {"MatchingDeviceId": r"PCI\VEN_8086&DEV_56A0"}),
}
)
)
host, captured = _detect_windows_host(monkeypatch, winreg)
assert host.has_intel_gpu is True
assert "powershell" not in captured
def test_detect_host_registry_amd_skips_cim_probe(monkeypatch):
# A single-vendor AMD box has no Intel match, so an `and` gate is what earns this skip.
winreg = _FakeWinreg(
_FakeRegKey(
subkeys = {
"0000": _FakeRegKey(values = {"MatchingDeviceId": r"PCI\VEN_1002&DEV_1586"}),
}
)
)
host, captured = _detect_windows_host(monkeypatch, winreg)
assert host.has_amd_gpu_without_rocm is True
assert host.has_intel_gpu is False
assert "powershell" not in captured
@pytest.mark.parametrize(
"mask_var", ["HIP_VISIBLE_DEVICES", "ROCR_VISIBLE_DEVICES", "CUDA_VISIBLE_DEVICES"]
)
def test_detect_host_windows_amd_honours_every_hip_mask(monkeypatch, mask_var):
# hipinfo honours all three HIP masks but Vulkan does not, so masked hosts must not route there.
winreg = _FakeWinreg(
_FakeRegKey(
subkeys = {
"0000": _FakeRegKey(values = {"MatchingDeviceId": r"PCI\VEN_1002&DEV_1586"}),
}
)
)
host, _captured = _detect_windows_host(monkeypatch, winreg, env = {mask_var: ""})
assert host.has_amd_gpu_without_rocm is False
def test_detect_host_cim_fallback_fires_on_registry_miss(monkeypatch):
winreg = _FakeWinreg(
_FakeRegKey(
subkeys = {
"0000": _FakeRegKey(values = {"MatchingDeviceId": r"PCI\VEN_10DE&DEV_2684"}),
}
)
)
host, captured = _detect_windows_host(
monkeypatch, winreg, powershell_stdout = "Intel(R) Arc(TM) A770 Graphics"
)
assert host.has_intel_gpu is True
assert "powershell" in captured
def test_windows_intel_registry_unexpected_error_is_false(monkeypatch):
# Advisory probe: any walk error returns False (CIM fallback), never crashes detect_host.
class _ExplodingWinreg:
HKEY_LOCAL_MACHINE = object()
def OpenKey(self, parent, name):
raise TypeError(name)
monkeypatch.setitem(sys.modules, "winreg", _ExplodingWinreg())
assert ilp.windows_intel_gpu_in_registry() is False
def test_detect_host_cim_rescues_exploding_registry(monkeypatch):
class _ExplodingWinreg:
HKEY_LOCAL_MACHINE = object()
def OpenKey(self, parent, name):
raise TypeError(name)
host, captured = _detect_windows_host(
monkeypatch, _ExplodingWinreg(), powershell_stdout = "Intel(R) Arc(TM) A770 Graphics"
)
assert host.has_intel_gpu is True
assert "powershell" in captured
def _windows_amd_host(**overrides):
defaults = dict(
system = "Windows",
machine = "amd64",
is_windows = True,
is_linux = False,
is_macos = False,
is_x86_64 = True,
is_arm64 = False,
nvidia_smi = None,
driver_cuda_version = None,
compute_caps = [],
visible_cuda_devices = None,
has_physical_nvidia = False,
has_usable_nvidia = False,
has_rocm = True,
has_intel_gpu = False,
)
defaults.update(overrides)
return ilp.HostInfo(**defaults)
def _windows_gfx803_host(
*args,
rocm_gfx_target = "gfx803",
rocm_gfx_targets = ["gfx1201", "gfx803"],
**kwargs,
):
"""_windows_amd_host with the gfx803 target list these cases share."""
return _windows_amd_host(
*args, rocm_gfx_target = rocm_gfx_target, rocm_gfx_targets = rocm_gfx_targets, **kwargs
)
def test_route_to_vulkan_prebuilt_auto_fallback_for_legacy_amd_gfx():
host = _windows_amd_host(rocm_gfx_target = "gfx803", rocm_gfx_targets = ["gfx803"])
routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert repo == FORK
assert persist == "auto"
assert routed.has_intel_gpu is True
assert routed.has_rocm is False
def test_route_to_vulkan_prebuilt_keeps_hip_when_one_gpu_is_supported():
host = _windows_gfx803_host(rocm_gfx_target = "gfx1201")
routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert routed is host
assert repo == FORK
assert persist is None
def test_route_to_vulkan_prebuilt_auto_fallback_skips_hip_masked_hosts():
# Vulkan honours no HIP mask, so auto-routing would grab the masked gfx1201.
host = _windows_gfx803_host()
routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert repo == FORK
assert persist is None
assert routed is host
def test_route_to_vulkan_prebuilt_auto_fallback_when_no_amd_gpu_reaches_floor():
host = _windows_gfx803_host(rocm_gfx_target = "gfx900", rocm_gfx_targets = ["gfx803", "gfx900"])
routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert repo == FORK
assert persist == "auto"
assert routed.has_rocm is False
def test_a_lone_rdna1_card_takes_the_vulkan_route(monkeypatch):
# RDNA 1 has ROCm torch but no HIP llama.cpp prebuilt: route to Vulkan.
for var in ("HIP_VISIBLE_DEVICES", "ROCR_VISIBLE_DEVICES", "CUDA_VISIBLE_DEVICES"):
monkeypatch.delenv(var, raising = False)
host = _windows_amd_host(rocm_gfx_target = "gfx1010", rocm_gfx_targets = ["gfx1010"])
assert ilp._should_auto_vulkan_for_amd_windows(host, FORK) is True
routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert repo == FORK
assert persist == "auto"
assert routed.has_rocm is False
@pytest.fixture
def _unpinned_amd_env(monkeypatch):
for var in (
"HIP_VISIBLE_DEVICES",
"ROCR_VISIBLE_DEVICES",
"CUDA_VISIBLE_DEVICES",
"UNSLOTH_ROCM_GFX_ARCH",
"UNSLOTH_LLAMA_CPP_BACKEND",
"UNSLOTH_FORCE_VULKAN",
):
monkeypatch.delenv(var, raising = False)
monkeypatch.setattr(ilp, "_rocm_torch_preferred", lambda: False)
def _route_mixed(
targets,
backend = None,
cpu_mechanism = False,
repo = FORK,
):
host = _windows_amd_host(
rocm_gfx_target = ilp._pick_rocm_gfx_target(" ".join(targets)),
rocm_gfx_targets = list(targets),
)
return ilp.route_backend_request(
backend = backend,
published_repo = repo,
published_release_tag = "pin",
cpu_mechanism = cpu_mechanism,
host = host,
)
@pytest.mark.parametrize(
"targets",
[
("gfx1010", "gfx1034"), # RX 5700 XT at device 0 beside an RX 6500 XT (#13680)
("gfx1010", "gfx1100"),
("gfx1103", "gfx1010"), # covered iGPU first, uncovered dGPU active (#7776)
],
)
def test_an_uncovered_active_card_beside_a_covered_one_takes_vulkan(targets, _unpinned_amd_env):
# No HIP prebuilt serves the card HIP runs on, so main found no bundle at all.
route = _route_mixed(targets)
assert route.host.has_rocm is False
assert route.host.has_intel_gpu is True
assert route.persist_llama_backend == "auto"
assert route.persist_rocm_gfx == "gfx1010"
assert route.published_repo == FORK
@pytest.mark.parametrize(
"targets",
[
("gfx1034", "gfx1010"), # covered card active: its bundle, the arch gate pins it
("gfx1036", "gfx1200"), # shadowing iGPU first: the gfx1200 decides (#7776)
("gfx1100", "gfx1034"),
],
)
def test_a_covered_active_card_keeps_the_hip_bundle(targets, _unpinned_amd_env):
route = _route_mixed(targets)
assert route.host.has_rocm is True
assert route.persist_llama_backend is None
def test_upstream_answers_per_repo_for_the_active_card(_unpinned_amd_env):
# ggml-org's win-hip build has no gfx1034, so the same host moves on upstream only.
assert _route_mixed(("gfx1034", "gfx1030")).host.has_rocm is True
assert _route_mixed(("gfx1034", "gfx1030"), repo = UPSTREAM).host.has_rocm is False
@pytest.mark.parametrize(
"mask_env", ["HIP_VISIBLE_DEVICES", "ROCR_VISIBLE_DEVICES", "CUDA_VISIBLE_DEVICES"]
)
@pytest.mark.parametrize("mask_value", ["0", "1", "", "-1"])
def test_a_masked_mixed_host_never_moves_to_vulkan(
mask_env, mask_value, _unpinned_amd_env, monkeypatch
):
# Vulkan honours none of these masks, so it would hand over the card the user hid.
monkeypatch.setenv(mask_env, mask_value)
host = _windows_amd_host(rocm_gfx_target = "gfx1010", rocm_gfx_targets = ["gfx1010", "gfx1034"])
assert ilp._should_auto_vulkan_for_amd_windows(host, FORK) is False
def test_a_forwarded_arch_behind_a_probed_igpu_is_not_the_probe_pick(_unpinned_amd_env):
# Probe saw only the gfx1103 iGPU; setup forwarded gfx1010. Re-picking over the augmented
# list would skip the iGPU and mistake the forward for the probe's own pick.
host = _windows_amd_host(rocm_gfx_target = "gfx1103", rocm_gfx_targets = ["gfx1103"])
host = ilp._apply_host_overrides(host, override_rocm_gfx = "gfx1010")
assert ilp._active_rocm_gfx_target(host) == "gfx1010"
assert host.rocm_gfx_forwarded is True
assert ilp._should_auto_vulkan_for_amd_windows(host, FORK) is False
def test_a_forward_matching_the_probe_pick_still_takes_vulkan(_unpinned_amd_env):
host = _windows_amd_host(rocm_gfx_target = "gfx1010", rocm_gfx_targets = ["gfx1010", "gfx1034"])
host = ilp._apply_host_overrides(host, override_rocm_gfx = "gfx1010")
assert host.rocm_gfx_forwarded is False
assert ilp._should_auto_vulkan_for_amd_windows(host, FORK) is True
def test_a_manual_arch_keeps_the_all_cards_rule(_unpinned_amd_env, monkeypatch):
monkeypatch.setenv("UNSLOTH_ROCM_GFX_ARCH", "gfx1010")
host = _windows_amd_host(rocm_gfx_target = "gfx1010", rocm_gfx_targets = ["gfx1010", "gfx1034"])
assert ilp._should_auto_vulkan_for_amd_windows(host, FORK) is False
@pytest.mark.parametrize(
"backend, cpu_mechanism", [("hip", False), ("rocm", False), ("cpu", False), (None, True)]
)
def test_an_explicit_backend_or_cpu_flag_still_wins_on_the_mixed_host(
backend, cpu_mechanism, _unpinned_amd_env
):
route = _route_mixed(("gfx1010", "gfx1034"), backend = backend, cpu_mechanism = cpu_mechanism)
assert route.host.has_intel_gpu is False
assert route.persist_llama_backend is None
def test_a_physical_nvidia_card_keeps_the_mixed_host_off_vulkan(_unpinned_amd_env):
host = _windows_amd_host(
rocm_gfx_target = "gfx1010",
rocm_gfx_targets = ["gfx1010", "gfx1034"],
has_physical_nvidia = True,
)
assert ilp._should_auto_vulkan_for_amd_windows(host, FORK) is False
def test_rocm_torch_preference_does_not_hide_nvidia_from_the_mixed_host_rule(
_unpinned_amd_env, monkeypatch
):
# Following ROCm torch clears has_physical_nvidia; Vulkan would still enumerate the card.
host = _windows_amd_host(
nvidia_smi = r"C:\Windows\System32\nvidia-smi.exe",
compute_caps = ["89"],
physical_compute_caps = ["89"],
has_physical_nvidia = True,
has_usable_nvidia = True,
rocm_gfx_target = "gfx1010",
rocm_gfx_targets = ["gfx1010", "gfx1034"],
)
monkeypatch.setattr(ilp, "_rocm_torch_preferred", lambda: True)
route = ilp.route_backend_request(
backend = None, published_repo = FORK, published_release_tag = "pin", host = host
)
assert route.host.has_rocm is True
assert route.persist_llama_backend is None
def test_the_mixed_host_rule_is_windows_only(_unpinned_amd_env):
host = _windows_amd_host(
system = "Linux",
is_windows = False,
is_linux = True,
rocm_gfx_target = "gfx1010",
rocm_gfx_targets = ["gfx1010", "gfx1034"],
)
assert ilp._should_auto_vulkan_for_amd_windows(host, FORK) is False
def test_resolve_prebuilt_reports_vulkan_for_the_mixed_host(monkeypatch, capsys, _unpinned_amd_env):
monkeypatch.setattr(
ilp,
"detect_host",
lambda **_kw: _windows_amd_host(
rocm_gfx_target = "gfx1010", rocm_gfx_targets = ["gfx1010", "gfx1034"]
),
)
seen, _out = _run_resolve_capture_host(monkeypatch, capsys)
assert seen["host"].has_rocm is False
assert seen["host"].has_intel_gpu is True
assert seen["repo"] == FORK
@pytest.mark.parametrize(
"mask_env", ["HIP_VISIBLE_DEVICES", "ROCR_VISIBLE_DEVICES", "CUDA_VISIBLE_DEVICES"]
)
def test_auto_vulkan_declines_when_a_hip_device_mask_filtered_the_probe(mask_env, monkeypatch):
# Under a mask hipinfo sees only visible cards, so the auto fallback must decline.
monkeypatch.setenv(mask_env, "1")
host = _windows_amd_host(rocm_gfx_target = "gfx803", rocm_gfx_targets = ["gfx803"])
assert ilp._should_auto_vulkan_for_amd_windows(host, FORK) is False
routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert routed is host
assert repo == FORK
assert persist is None
@pytest.mark.parametrize("mask_value", ["", " ", "-1"])
def test_auto_vulkan_declines_when_the_mask_hides_every_amd_gpu(mask_value, monkeypatch):
# A forwarded --rocm-gfx still yields an arch under an all-hiding mask; still decline.
monkeypatch.setenv("HIP_VISIBLE_DEVICES", mask_value)
host = _windows_amd_host(rocm_gfx_target = None, rocm_gfx_targets = [])
host = ilp._apply_host_overrides(host, override_rocm_gfx = "gfx803")
assert ilp._active_rocm_gfx_target(host) == "gfx803"
assert ilp._should_auto_vulkan_for_amd_windows(host, FORK) is False
routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert routed is host
assert repo == FORK
assert persist is None
def test_hip_device_mask_check_is_presence_not_value(monkeypatch):
# Presence is the whole test: any value means the HIP view is not the physical one, and
# no value can be read as "the probe saw everything".
assert ilp._hip_visible_device_mask_set() is False
for value in ("", " ", "-1", "0", "1", "0,1"):
monkeypatch.setenv("HIP_VISIBLE_DEVICES", value)
assert ilp._hip_visible_device_mask_set() is True, value
monkeypatch.delenv("HIP_VISIBLE_DEVICES")
monkeypatch.setenv("ROCR_VISIBLE_DEVICES", "0")
assert ilp._hip_visible_device_mask_set() is True
monkeypatch.delenv("ROCR_VISIBLE_DEVICES")
monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "0")
assert ilp._hip_visible_device_mask_set() is True
def test_masked_probe_suppression_does_not_touch_non_amd_auto_paths(monkeypatch):
monkeypatch.setenv("HIP_VISIBLE_DEVICES", "1")
host = _host(
system = "Windows",
is_windows = True,
has_intel_gpu = True,
has_rocm = False,
has_physical_nvidia = False,
has_usable_nvidia = False,
)
_routed, repo, _tag, _persist = ilp._route_to_vulkan_prebuilt(
host, FORK, "pin", force_cpu = False
)
assert repo == FORK
def test_route_to_vulkan_prebuilt_hip_masked_host_still_honours_explicit_optin(monkeypatch):
# The mask guard only suppresses the AUTOMATIC fallback; explicit opt-in wins.
monkeypatch.delenv("UNSLOTH_FORCE_VULKAN", raising = False)
host = _windows_gfx803_host()
_routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(
host, FORK, "pin", force_cpu = False, llama_backend = "vulkan"
)
assert repo == FORK
assert persist == "vulkan"
def test_forced_vulkan_on_an_auto_routing_amd_host_persists_as_automatic(monkeypatch):
# Persist as automatic so older installs stay eligible for Vulkan CPU crash recovery.
monkeypatch.delenv("UNSLOTH_FORCE_VULKAN", raising = False)
host = _windows_amd_host(rocm_gfx_target = "gfx1034", rocm_gfx_targets = ["gfx1034"])
_routed, _repo, _tag, persist = ilp._route_to_vulkan_prebuilt(
host, UPSTREAM, "pin", force_cpu = False, llama_backend = "vulkan"
)
assert persist == "auto"
supported = _windows_amd_host(rocm_gfx_target = "gfx1100", rocm_gfx_targets = ["gfx1100"])
_routed, _repo, _tag, persist = ilp._route_to_vulkan_prebuilt(
supported, UPSTREAM, "pin", force_cpu = False, llama_backend = "vulkan"
)
assert persist == "vulkan"
def test_auto_vulkan_is_repository_specific_for_fork_only_gfx():
# gfx1034 is fork-only (gfx103X bundle); upstream has no Vulkan branch, so answer per repo.
host = _windows_amd_host(rocm_gfx_target = "gfx1034", rocm_gfx_targets = ["gfx1034"])
assert ilp._should_auto_vulkan_for_amd_windows(host, FORK) is False
assert ilp._should_auto_vulkan_for_amd_windows(host, UPSTREAM) is True
supported = _windows_amd_host(rocm_gfx_target = "gfx1100", rocm_gfx_targets = ["gfx1100"])
assert ilp._should_auto_vulkan_for_amd_windows(supported, FORK) is False
assert ilp._should_auto_vulkan_for_amd_windows(supported, UPSTREAM) is False
# A family label can't identify the card, so it stays on HIP.
family = _windows_amd_host(rocm_gfx_target = "gfx110X", rocm_gfx_targets = ["gfx110X"])
assert ilp._should_auto_vulkan_for_amd_windows(family, UPSTREAM) is False
@pytest.mark.parametrize(
"repo", ["acme/llama.cpp-mirror", "GGML-ORG/llama.cpp", "unslothAI/llama.cpp"]
)
def test_fork_only_gfx_coverage_is_not_granted_to_other_repos(repo):
# Only the exact fork repo is planned from a manifest; gate on the fork, not one name.
host = _windows_amd_host(rocm_gfx_target = "gfx1034", rocm_gfx_targets = ["gfx1034"])
assert ilp._should_auto_vulkan_for_amd_windows(host, repo) is True
supported = _windows_amd_host(rocm_gfx_target = "gfx1100", rocm_gfx_targets = ["gfx1100"])
assert ilp._should_auto_vulkan_for_amd_windows(supported, repo) is False
@pytest.mark.parametrize("repo", [None, ""])
def test_empty_published_repo_gets_fork_coverage(repo):
# The resolver defaults an empty repo to the fork, so the predicate must too.
host = _windows_amd_host(rocm_gfx_target = "gfx1034", rocm_gfx_targets = ["gfx1034"])
assert ilp._should_auto_vulkan_for_amd_windows(host, repo) is False
def test_upstream_windows_hip_targets_are_a_subset_of_the_combined_floor():
# The floor must stay a superset, else auto-Vulkan steals a host upstream builds for.
assert ilp.UPSTREAM_WINDOWS_HIP_GFX_TARGETS <= ilp.WINDOWS_HIP_PREBUILT_GFX_TARGETS
# The fork-only extras are exactly the archs that must route to Vulkan upstream.
assert ilp.WINDOWS_HIP_PREBUILT_GFX_TARGETS - ilp.UPSTREAM_WINDOWS_HIP_GFX_TARGETS == {
"gfx908",
"gfx90a",
"gfx1034",
"gfx1103",
}
def test_route_to_vulkan_prebuilt_unknown_gfx_does_not_auto_fallback():
host = _windows_gfx803_host(has_rocm = True, rocm_gfx_target = None, rocm_gfx_targets = [])
routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert routed is host
assert repo == FORK
assert persist is None
def test_route_to_vulkan_prebuilt_family_gfx_token_keeps_rocm():
host = _windows_amd_host(rocm_gfx_target = "gfx110X", rocm_gfx_targets = ["gfx110X"])
routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert routed is host
assert repo == FORK
assert persist is None
def test_route_to_vulkan_prebuilt_gfx1103_keeps_rocm():
host = _windows_amd_host(rocm_gfx_target = "gfx1103", rocm_gfx_targets = ["gfx1103"])
routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert routed is host
assert repo == FORK
assert persist is None
def test_route_to_vulkan_prebuilt_gfx1034_keeps_rocm():
host = _windows_amd_host(rocm_gfx_target = "gfx1034", rocm_gfx_targets = ["gfx1034"])
routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert routed is host
assert repo == FORK
assert persist is None
def test_route_to_vulkan_prebuilt_explicit_opt_in_on_mixed_amd(monkeypatch):
monkeypatch.setenv("UNSLOTH_LLAMA_CPP_BACKEND", "vulkan")
host = _windows_gfx803_host(rocm_gfx_target = "gfx1201")
routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert repo == FORK
assert persist == "vulkan"
assert routed.has_rocm is False
def test_direct_upstream_windows_amd_legacy_gfx_routes_to_vulkan():
host = _windows_amd_host(rocm_gfx_target = "gfx803", rocm_gfx_targets = ["gfx803"])
routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
rel = _upstream_release(
"b9925",
[
"llama-b9925-bin-win-hip-radeon-x64.zip",
"llama-b9925-bin-win-vulkan-x64.zip",
"llama-b9925-bin-win-cpu-x64.zip",
],
)
plan = ilp.direct_upstream_release_plan(rel, routed, repo, "latest")
assert persist == "auto"
assert plan.attempts[0].install_kind == "windows-vulkan"
def test_hip_backend_env_suppresses_auto_vulkan_fallback_on_unsupported_gfx(monkeypatch):
monkeypatch.delenv("UNSLOTH_FORCE_VULKAN", raising = False)
monkeypatch.setenv("UNSLOTH_LLAMA_CPP_BACKEND", "hip")
host = _windows_amd_host(rocm_gfx_target = "gfx803", rocm_gfx_targets = ["gfx803"])
routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert routed is host
assert repo == FORK
assert persist is None
def test_route_to_vulkan_prebuilt_hidden_physical_nvidia_amd_not_rerouted():
# Vulkan ignores CUDA_VISIBLE_DEVICES, so a CUDA-masked NVIDIA card next to a legacy
# AMD gfx must not auto-route: Vulkan could grab the reserved NVIDIA GPU.
host = _windows_gfx803_host(
rocm_gfx_targets = ["gfx803"],
has_physical_nvidia = True,
has_usable_nvidia = False,
)
routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert routed is host
assert repo == FORK
assert persist is None
def test_route_to_vulkan_prebuilt_explicit_opt_in_overrides_hidden_nvidia(monkeypatch):
monkeypatch.setenv("UNSLOTH_LLAMA_CPP_BACKEND", "vulkan")
host = _windows_gfx803_host(
rocm_gfx_targets = ["gfx803"],
has_physical_nvidia = True,
has_usable_nvidia = False,
)
routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert repo == FORK
assert persist == "vulkan"
# Static mirror of the fork manifest's windows-rocm archs (offline parametrisation);
# the guard below re-derives it from the published manifest.
_FORK_WINDOWS_ROCM_GFX = (
"gfx908",
"gfx90a",
"gfx1030",
"gfx1031",
"gfx1032",
"gfx1034",
"gfx1100",
"gfx1101",
"gfx1102",
"gfx1103",
"gfx1150",
"gfx1151",
"gfx1200",
"gfx1201",
)
def _published_fork_windows_rocm_artifacts():
"""Reads the published manifest of the fork; OSError and PrebuiltFallback skip, parse errors fail."""
try:
resolved = ilp._download_host_resolved_release(FORK)
except OSError as exc:
pytest.skip(f"{FORK} release manifest unreachable: {exc}")
except ilp.PrebuiltFallback as exc:
pytest.skip(f"{FORK} latest release was rejected before its manifest parsed: {exc}")
if resolved is None:
pytest.skip(f"{FORK} published no resolvable latest release")
tag = resolved.bundle.release_tag
artifacts = [
artifact
for artifact in resolved.bundle.artifacts
if artifact.install_kind == "windows-rocm"
]
assert artifacts, f"{FORK}@{tag} manifest listed no windows-rocm artifacts"
return tag, artifacts
def test_windows_hip_gfx_floor_covers_every_fork_windows_rocm_bundle():
# Derived from the published manifest so a newly published arch fails here.
tag, artifacts = _published_fork_windows_rocm_artifacts()
# Bundles match a mapped_targets entry or the umbrella gfx_target, so both must clear a floor.
concrete = {target.lower() for artifact in artifacts for target in artifact.mapped_targets}
labels = {
artifact.gfx_target.lower()
for artifact in artifacts
if artifact.gfx_target and artifact.gfx_target.lower() not in concrete
}
unfloored = sorted(concrete - ilp.WINDOWS_HIP_PREBUILT_GFX_TARGETS)
assert (
not unfloored
), f"auto-Vulkan would steal windows-rocm archs published in {FORK}@{tag}: {unfloored}"
unlabelled = sorted(labels - ilp.WINDOWS_ROCM_FAMILY_GFX_LABELS)
assert not unlabelled, (
f"update markers forward family labels {FORK}@{tag} publishes but "
f"WINDOWS_ROCM_FAMILY_GFX_LABELS omits: {unlabelled}"
)
assert set(_FORK_WINDOWS_ROCM_GFX) == concrete, (
f"_FORK_WINDOWS_ROCM_GFX drifted from {FORK}@{tag}: "
f"gained {sorted(concrete - set(_FORK_WINDOWS_ROCM_GFX))}, "
f"lost {sorted(set(_FORK_WINDOWS_ROCM_GFX) - concrete)}"
)
@pytest.mark.parametrize("gfx", _FORK_WINDOWS_ROCM_GFX)
def test_route_to_vulkan_prebuilt_keeps_every_fork_windows_rocm_arch(gfx, monkeypatch):
monkeypatch.delenv("UNSLOTH_LLAMA_CPP_BACKEND", raising = False)
monkeypatch.delenv("UNSLOTH_FORCE_VULKAN", raising = False)
host = _windows_amd_host(rocm_gfx_target = gfx, rocm_gfx_targets = [gfx])
routed, repo, tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert routed is host
assert (repo, tag) == (FORK, "pin")
assert persist is None
@pytest.mark.parametrize("gfx", sorted(ilp.VULKAN_PREFERRED_GFX_TARGETS))
def test_the_integrated_archs_are_the_one_exception_to_that_sweep(gfx, monkeypatch):
monkeypatch.delenv("UNSLOTH_LLAMA_CPP_BACKEND", raising = False)
monkeypatch.delenv("UNSLOTH_FORCE_VULKAN", raising = False)
monkeypatch.setattr(ilp, "_amd_vulkan_icd_present", lambda: True)
assert gfx in ilp.WINDOWS_HIP_PREBUILT_GFX_TARGETS, "a preference, not a missing bundle"
host = _windows_amd_host(rocm_gfx_target = gfx, rocm_gfx_targets = [gfx])
routed, repo, tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert routed.has_rocm is False
assert (repo, tag) == (FORK, "pin")
assert persist == "auto"
def test_forwarded_gfx_does_not_undo_visible_device_auto_vulkan(monkeypatch):
# Under CUDA_VISIBLE_DEVICES=1 setup forwards gfx1100 but the visible GPU is gfx1010;
# the forward must not reinstate gfx1100.
monkeypatch.delenv("UNSLOTH_LLAMA_CPP_BACKEND", raising = False)
monkeypatch.delenv("UNSLOTH_FORCE_VULKAN", raising = False)
monkeypatch.delenv("UNSLOTH_ROCM_GFX_ARCH", raising = False)
host = _windows_amd_host(rocm_gfx_target = "gfx1010", rocm_gfx_targets = ["gfx1100", "gfx1010"])
host = ilp._apply_host_overrides(host, override_rocm_gfx = "gfx1100")
assert ilp._active_rocm_gfx_target(host) == "gfx1010"
assert host.rocm_gfx_targets == ["gfx1100", "gfx1010"]
# gfx1100 is masked off, not absent, and Vulkan does not honour the HIP mask, so the
# automatic fallback stays off and the HIP / fork path is kept.
assert ilp._should_auto_vulkan_for_amd_windows(host, FORK) is False
_routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert repo == FORK
assert persist is None
def test_forwarded_gfx_absent_from_probe_keeps_the_physical_hip_card(monkeypatch):
# A stale forwarded arch must not delete the probe's inventory, or Vulkan grabs the masked card.
monkeypatch.delenv("UNSLOTH_LLAMA_CPP_BACKEND", raising = False)
monkeypatch.delenv("UNSLOTH_FORCE_VULKAN", raising = False)
monkeypatch.delenv("UNSLOTH_ROCM_GFX_ARCH", raising = False)
host = _windows_amd_host(rocm_gfx_target = "gfx803", rocm_gfx_targets = ["gfx1100", "gfx803"])
host = ilp._apply_host_overrides(host, override_rocm_gfx = "gfx900")
assert ilp._active_rocm_gfx_target(host) == "gfx900"
assert host.rocm_gfx_targets == ["gfx1100", "gfx803", "gfx900"]
assert ilp._should_auto_vulkan_for_amd_windows(host, FORK) is False
_routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert repo == FORK
assert persist is None
def test_forwarded_gfx_absent_from_probe_keeps_a_single_probed_hip_card(monkeypatch):
monkeypatch.delenv("UNSLOTH_LLAMA_CPP_BACKEND", raising = False)
monkeypatch.delenv("UNSLOTH_FORCE_VULKAN", raising = False)
monkeypatch.delenv("UNSLOTH_ROCM_GFX_ARCH", raising = False)
host = _windows_amd_host(rocm_gfx_target = "gfx1100", rocm_gfx_targets = ["gfx1100"])
host = ilp._apply_host_overrides(host, override_rocm_gfx = "gfx803")
assert ilp._active_rocm_gfx_target(host) == "gfx803"
assert host.rocm_gfx_targets == ["gfx1100", "gfx803"]
assert ilp._should_auto_vulkan_for_amd_windows(host, FORK) is False
def test_forwarded_gfx_absent_from_probe_still_allows_explicit_vulkan(monkeypatch):
monkeypatch.setenv("UNSLOTH_LLAMA_CPP_BACKEND", "vulkan")
monkeypatch.delenv("UNSLOTH_ROCM_GFX_ARCH", raising = False)
host = _windows_amd_host(rocm_gfx_target = "gfx1100", rocm_gfx_targets = ["gfx1100"])
host = ilp._apply_host_overrides(host, override_rocm_gfx = "gfx803")
_routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert repo == FORK
assert persist == "vulkan"
def test_forwarded_gfx_on_unprobed_host_still_auto_vulkans(monkeypatch):
# Driver-only host: --rocm-gfx is the only arch source, so it must still reach Vulkan.
monkeypatch.delenv("UNSLOTH_LLAMA_CPP_BACKEND", raising = False)
monkeypatch.delenv("UNSLOTH_FORCE_VULKAN", raising = False)
monkeypatch.delenv("UNSLOTH_ROCM_GFX_ARCH", raising = False)
host = _windows_amd_host(rocm_gfx_target = None, rocm_gfx_targets = [])
host = ilp._apply_host_overrides(host, override_rocm_gfx = "gfx803")
assert host.rocm_gfx_targets == ["gfx803"]
assert ilp._should_auto_vulkan_for_amd_windows(host, FORK) is True
_routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert repo == FORK
assert persist == "auto"
def test_forwarded_gfx_still_fills_an_unprobed_arch(monkeypatch):
# amd-smi-only host reports no arch, so the forward must still apply.
monkeypatch.delenv("UNSLOTH_LLAMA_CPP_BACKEND", raising = False)
monkeypatch.delenv("UNSLOTH_FORCE_VULKAN", raising = False)
monkeypatch.delenv("UNSLOTH_ROCM_GFX_ARCH", raising = False)
host = _windows_amd_host(rocm_gfx_target = None, rocm_gfx_targets = [])
host = ilp._apply_host_overrides(host, override_rocm_gfx = "gfx1100")
assert ilp._active_rocm_gfx_target(host) == "gfx1100"
assert ilp._should_auto_vulkan_for_amd_windows(host) is False
_routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert repo == FORK
assert persist is None
@pytest.fixture
def amd_vulkan_icd(monkeypatch):
"""This host has an AMD Vulkan driver, the precondition for the integrated route."""
monkeypatch.delenv("UNSLOTH_LLAMA_CPP_BACKEND", raising = False)
monkeypatch.delenv("UNSLOTH_FORCE_VULKAN", raising = False)
monkeypatch.setattr(ilp, "_amd_vulkan_icd_present", lambda: True)
def _amd_host(*, windows, **overrides):
if windows:
return _windows_amd_host(**overrides)
defaults = dict(
system = "Linux",
machine = "x86_64",
is_linux = True,
is_x86_64 = True,
has_rocm = True,
)
defaults.update(overrides)
return _host(**defaults)
@pytest.mark.parametrize("windows", [True, False])
@pytest.mark.parametrize("gfx", sorted(ilp.VULKAN_PREFERRED_GFX_TARGETS))
def test_integrated_amd_prefers_vulkan_on_both_platforms(gfx, windows, amd_vulkan_icd):
host = _amd_host(windows = windows, rocm_gfx_target = gfx, rocm_gfx_targets = [gfx])
assert ilp._should_prefer_vulkan_for_amd_igpu(host) is True
routed, repo, tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert routed.has_rocm is False
assert routed.has_intel_gpu is True
assert (repo, tag) == (FORK, "pin")
assert persist == "auto"
def test_integrated_amd_keeps_rocm_without_an_amd_vulkan_driver(monkeypatch):
monkeypatch.delenv("UNSLOTH_LLAMA_CPP_BACKEND", raising = False)
monkeypatch.delenv("UNSLOTH_FORCE_VULKAN", raising = False)
host = _windows_amd_host(rocm_gfx_target = "gfx1151", rocm_gfx_targets = ["gfx1151"])
assert ilp._should_prefer_vulkan_for_amd_igpu(host) is False
routed, _repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert routed is host
assert persist is None
@pytest.mark.parametrize("gfx", ["gfx908", "gfx90a", "gfx1030", "gfx1100", "gfx1201"])
def test_only_the_integrated_archs_are_preferred_onto_vulkan(gfx, amd_vulkan_icd):
# Why this is an allowlist: ROCm ships no Vulkan ICD, so CDNA would get a dead backend
# rather than a slower one, and discrete RDNA is unmeasured.
host = _windows_amd_host(rocm_gfx_target = gfx, rocm_gfx_targets = [gfx])
assert ilp._should_prefer_vulkan_for_amd_igpu(host) is False
routed, _repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert routed is host
assert persist is None
def test_a_discrete_card_beside_the_apu_keeps_the_whole_host_on_rocm(amd_vulkan_icd):
# Judged over every physical gfx: Vulkan honours no HIP mask, so routing here would
# hand the discrete card over too.
host = _windows_amd_host(
rocm_gfx_target = "gfx1151",
rocm_gfx_targets = ["gfx1151", "gfx1100"],
)
assert ilp._should_prefer_vulkan_for_amd_igpu(host) is False
def test_integrated_route_declines_beside_a_physical_nvidia_card(amd_vulkan_icd):
host = _windows_amd_host(
rocm_gfx_target = "gfx1151",
rocm_gfx_targets = ["gfx1151"],
has_physical_nvidia = True,
)
assert ilp._should_prefer_vulkan_for_amd_igpu(host) is False
def test_integrated_route_declines_under_a_vulkan_device_mask(amd_vulkan_icd, monkeypatch):
# An ICD proves a driver, not visibility: a device mask excluding the APU enumerates nothing.
host = _windows_amd_host(rocm_gfx_target = "gfx1151", rocm_gfx_targets = ["gfx1151"])
monkeypatch.setenv("GGML_VK_VISIBLE_DEVICES", "1")
assert ilp._should_prefer_vulkan_for_amd_igpu(host) is False
monkeypatch.delenv("GGML_VK_VISIBLE_DEVICES")
assert ilp._should_prefer_vulkan_for_amd_igpu(host) is True
@pytest.mark.parametrize(
"mask_env", ["HIP_VISIBLE_DEVICES", "ROCR_VISIBLE_DEVICES", "CUDA_VISIBLE_DEVICES"]
)
def test_integrated_route_declines_under_a_hip_device_mask(mask_env, amd_vulkan_icd, monkeypatch):
# Under a mask the inventory is unknowable, so "every GPU is integrated" is unprovable.
monkeypatch.setenv(mask_env, "0")
host = _windows_amd_host(rocm_gfx_target = "gfx1151", rocm_gfx_targets = ["gfx1151"])
assert ilp._should_prefer_vulkan_for_amd_igpu(host) is False
@pytest.mark.parametrize("backend", ["rocm", "cpu", "cuda"])
def test_an_explicit_backend_opts_out_of_the_integrated_route(backend, amd_vulkan_icd):
host = _windows_amd_host(rocm_gfx_target = "gfx1151", rocm_gfx_targets = ["gfx1151"])
routed, _repo, _tag, persist = ilp._route_to_vulkan_prebuilt(
host, FORK, "pin", force_cpu = False, llama_backend = backend
)
assert routed is host
assert persist is None
def test_forced_vulkan_on_an_integrated_host_persists_as_automatic(amd_vulkan_icd):
host = _windows_amd_host(rocm_gfx_target = "gfx1151", rocm_gfx_targets = ["gfx1151"])
_routed, _repo, _tag, persist = ilp._route_to_vulkan_prebuilt(
host, FORK, "pin", force_cpu = False, llama_backend = "vulkan"
)
assert persist == "auto"
def test_force_cpu_wins_over_the_integrated_route(amd_vulkan_icd):
host = _windows_amd_host(rocm_gfx_target = "gfx1151", rocm_gfx_targets = ["gfx1151"])
routed, _repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = True)
assert routed is host
assert persist is None
def test_route_backend_request_keeps_the_arch_for_a_migrated_integrated_host(amd_vulkan_icd):
# Read the arch before routing to Vulkan-only, or the next update re-detects a ROCm-less host.
host = _windows_amd_host(rocm_gfx_target = "gfx1151", rocm_gfx_targets = ["gfx1151"])
route = ilp.route_backend_request(
backend = None, published_repo = FORK, published_release_tag = "pin", host = host
)
assert route.host.has_rocm is False
assert route.persist_llama_backend == "auto"
assert route.persist_rocm_gfx == "gfx1151"
def test_route_backend_request_keeps_rocm_as_the_fallback_for_the_preference(amd_vulkan_icd):
# _vulkan_only_host clears has_rocm, so the ROCm fallback must be carried or selectors read CPU.
host = _windows_amd_host(rocm_gfx_target = "gfx1151", rocm_gfx_targets = ["gfx1151"])
route = ilp.route_backend_request(
backend = None, published_repo = FORK, published_release_tag = "pin", host = host
)
assert route.rocm_fallback_host is not None
assert route.rocm_fallback_host.has_rocm is True
assert route.rocm_fallback_host.rocm_gfx_target == "gfx1151"
def test_route_backend_request_carries_no_rocm_fallback_for_the_no_hip_route(monkeypatch):
# Vulkan route happens because no HIP prebuilt covers the arch, so no ROCm fallback.
monkeypatch.setattr(ilp, "_amd_vulkan_icd_present", lambda: False)
host = _windows_amd_host(rocm_gfx_target = "gfx900", rocm_gfx_targets = ["gfx900"])
route = ilp.route_backend_request(
backend = None, published_repo = FORK, published_release_tag = "pin", host = host
)
assert route.host.has_rocm is False, "the #7357 route should still have fired"
assert route.rocm_fallback_host is None
def test_the_preference_plan_tries_rocm_before_the_cpu_fallback(monkeypatch, amd_vulkan_icd):
plan_calls: list[bool] = []
def _plans(_tag, plan_host, _repo, _release, **_kw):
plan_calls.append(bool(plan_host.has_rocm))
kinds = ["windows-hip"] if plan_host.has_rocm else ["windows-vulkan", "windows-cpu"]
return "b1", [
ilp.InstallReleasePlan(
requested_tag = "b1",
llama_tag = "b1",
release_tag = "b1",
attempts = [
ilp.AssetChoice(
repo = FORK,
tag = "b1",
name = k,
url = f"https://x/{k}",
source_label = "test",
install_kind = k,
)
for k in kinds
],
approved_checksums = {},
)
]
monkeypatch.setattr(ilp, "resolve_simple_install_release_plans", _plans)
host = _windows_amd_host(rocm_gfx_target = "gfx1151", rocm_gfx_targets = ["gfx1151"])
selection = ilp.select_backend_install(
backend = None,
llama_tag = "b1",
published_repo = FORK,
published_release_tag = "pin",
host = host,
)
assert [a.install_kind for a in selection.release_plans[0].attempts] == [
"windows-vulkan",
"windows-hip",
"windows-cpu",
]
assert plan_calls == [False, True], "the ROCm plan is resolved from the pre-route host"
named = ilp.select_backend_install(
backend = "vulkan",
llama_tag = "b1",
published_repo = FORK,
published_release_tag = "pin",
host = host,
)
assert [a.install_kind for a in named.release_plans[0].attempts] == ["windows-vulkan"]
def _one_plan(*kinds, release_tag = "b1"):
return ilp.InstallReleasePlan(
requested_tag = "b1",
llama_tag = "b1",
release_tag = release_tag,
attempts = [
ilp.AssetChoice(
repo = FORK,
tag = "b1",
name = k,
url = f"https://x/{k}",
source_label = "test",
install_kind = k,
)
for k in kinds
],
approved_checksums = {},
)
@pytest.mark.parametrize("rocm_plans", ["raises", "empty"])
def test_no_rocm_attempt_means_no_cpu_tail_either(monkeypatch, amd_vulkan_icd, rocm_plans):
def _plans(_tag, plan_host, _repo, _release, **_kw):
if not plan_host.has_rocm:
return "b1", [_one_plan("windows-vulkan", "windows-cpu")]
if rocm_plans == "raises":
raise RuntimeError("no ROCm asset for this release")
return "b1", []
monkeypatch.setattr(ilp, "resolve_simple_install_release_plans", _plans)
host = _windows_amd_host(rocm_gfx_target = "gfx1151", rocm_gfx_targets = ["gfx1151"])
selection = ilp.select_backend_install(
backend = None,
llama_tag = "b1",
published_repo = FORK,
published_release_tag = "pin",
host = host,
)
assert [a.install_kind for a in selection.release_plans[0].attempts] == ["windows-vulkan"]
@pytest.mark.parametrize("rocm_plans", ["raises", "empty"])
def test_a_release_that_is_nothing_but_cpu_fails_rather_than_installing_it(
monkeypatch, amd_vulkan_icd, rocm_plans
):
def _plans(_tag, plan_host, _repo, _release, **_kw):
if not plan_host.has_rocm:
return "b1", [_one_plan("windows-cpu")]
if rocm_plans == "raises":
raise RuntimeError("no ROCm asset for this release")
return "b1", []
monkeypatch.setattr(ilp, "resolve_simple_install_release_plans", _plans)
host = _windows_amd_host(rocm_gfx_target = "gfx1151", rocm_gfx_targets = ["gfx1151"])
with pytest.raises(ilp.PrebuiltFallback):
ilp.select_backend_install(
backend = None,
llama_tag = "b1",
published_repo = FORK,
published_release_tag = "pin",
host = host,
)
def test_release_order_survives_narrowing(monkeypatch, amd_vulkan_icd):
# Release order is preference order, and reordering it installs an older build.
def _plans(_tag, plan_host, _repo, _release, **_kw):
if not plan_host.has_rocm:
return "b2", [
_one_plan("windows-vulkan", "windows-cpu", release_tag = "b2"),
_one_plan("windows-vulkan", "windows-cpu", release_tag = "b1"),
]
return "b2", [_one_plan("windows-hip", release_tag = "b1")]
monkeypatch.setattr(ilp, "resolve_simple_install_release_plans", _plans)
host = _windows_amd_host(rocm_gfx_target = "gfx1151", rocm_gfx_targets = ["gfx1151"])
selection = ilp.select_backend_install(
backend = None,
llama_tag = "b2",
published_repo = FORK,
published_release_tag = "pin",
host = host,
)
assert [p.release_tag for p in selection.release_plans] == ["b2", "b1"]
assert [a.install_kind for a in selection.release_plans[0].attempts] == ["windows-vulkan"]
assert [a.install_kind for a in selection.release_plans[1].attempts] == [
"windows-vulkan",
"windows-hip",
"windows-cpu",
]
def test_a_plan_that_already_carries_rocm_keeps_its_cpu_tail(monkeypatch, amd_vulkan_icd):
def _plans(_tag, plan_host, _repo, _release, **_kw):
if not plan_host.has_rocm:
return "b1", [_one_plan("windows-vulkan", "windows-hip", "windows-cpu")]
return "b1", [_one_plan("windows-hip")]
monkeypatch.setattr(ilp, "resolve_simple_install_release_plans", _plans)
host = _windows_amd_host(rocm_gfx_target = "gfx1151", rocm_gfx_targets = ["gfx1151"])
selection = ilp.select_backend_install(
backend = None,
llama_tag = "b1",
published_repo = FORK,
published_release_tag = "pin",
host = host,
)
assert [a.install_kind for a in selection.release_plans[0].attempts] == [
"windows-vulkan",
"windows-hip",
"windows-cpu",
]
def _icd(path, library = "libvulkan_driver.so"):
"""Writes a real Vulkan ICD manifest where the loader looks; library None leaves the driver missing."""
path.parent.mkdir(parents = True, exist_ok = True)
driver = path.parent / (library or "gone_driver.so")
if library is not None:
driver.write_text("", encoding = "utf-8")
path.write_text(json.dumps({"ICD": {"library_path": str(driver)}}), encoding = "utf-8")
return str(path)
def test_amd_vulkan_icd_probe_reads_the_loader_overrides_first(monkeypatch, tmp_path):
monkeypatch.setattr(ilp.sys, "platform", "linux")
monkeypatch.setattr(ilp, "_amd_vulkan_icd_present", _REAL_AMD_VULKAN_ICD_PRESENT)
monkeypatch.delenv("VK_DRIVER_FILES", raising = False)
monkeypatch.setenv("VK_ICD_FILENAMES", _icd(tmp_path / "intel_icd.json"))
assert ilp._amd_vulkan_icd_present() is False
monkeypatch.setenv(
"VK_DRIVER_FILES",
os.pathsep.join([_icd(tmp_path / "intel_icd.json"), _icd(tmp_path / "radeon_icd.json")]),
)
assert ilp._amd_vulkan_icd_present() is True
def test_an_override_naming_a_removed_amd_manifest_does_not_answer_for_the_driver(
monkeypatch, tmp_path
):
monkeypatch.setattr(ilp.sys, "platform", "linux")
monkeypatch.setattr(ilp, "_amd_vulkan_icd_present", _REAL_AMD_VULKAN_ICD_PRESENT)
monkeypatch.delenv("VK_ICD_FILENAMES", raising = False)
monkeypatch.setattr(ilp, "_vulkan_icd_search_dirs", lambda: [])
monkeypatch.setenv("VK_DRIVER_FILES", str(tmp_path / "gone" / "radeon_icd.x86_64.json"))
assert ilp._amd_vulkan_icd_present() is False
monkeypatch.setenv("VK_DRIVER_FILES", _icd(tmp_path / "gone" / "radeon_icd.x86_64.json"))
assert ilp._amd_vulkan_icd_present() is True
def test_a_manifest_the_loader_filters_out_does_not_answer_for_the_driver(monkeypatch, tmp_path):
# VK_LOADER_DRIVERS_DISABLE / _SELECT exclude a registered driver from being loaded, so
# counting one hands this host a Vulkan build with no AMD device.
monkeypatch.setattr(ilp.sys, "platform", "linux")
monkeypatch.setattr(ilp, "_amd_vulkan_icd_present", _REAL_AMD_VULKAN_ICD_PRESENT)
for name in (
"VK_DRIVER_FILES",
"VK_ICD_FILENAMES",
"VK_LOADER_DRIVERS_SELECT",
"VK_LOADER_DRIVERS_DISABLE",
):
monkeypatch.delenv(name, raising = False)
icd_dir = tmp_path / "usr/share/vulkan/icd.d"
monkeypatch.setattr(ilp, "_vulkan_icd_search_dirs", lambda: [icd_dir])
_icd(icd_dir / "radeon_icd.x86_64.json")
assert ilp._amd_vulkan_icd_present() is True
monkeypatch.setenv("VK_LOADER_DRIVERS_DISABLE", "*radeon*")
assert ilp._amd_vulkan_icd_present() is False
# Disable wins over select/enable (Vulkan-Loader LoaderInterfaceArchitecture.md).
monkeypatch.setenv("VK_LOADER_DRIVERS_SELECT", "RADEON_ICD.X86_64.JSON")
assert ilp._amd_vulkan_icd_present() is False
monkeypatch.delenv("VK_LOADER_DRIVERS_DISABLE")
assert ilp._amd_vulkan_icd_present() is True
monkeypatch.setenv("VK_LOADER_DRIVERS_DISABLE", "*radeon*")
monkeypatch.setenv("VK_LOADER_DRIVERS_SELECT", "intel_*")
assert ilp._amd_vulkan_icd_present() is False
def test_the_loader_filters_reach_a_force_list_as_well(monkeypatch, tmp_path):
monkeypatch.setattr(ilp.sys, "platform", "linux")
monkeypatch.setattr(ilp, "_amd_vulkan_icd_present", _REAL_AMD_VULKAN_ICD_PRESENT)
monkeypatch.delenv("VK_ICD_FILENAMES", raising = False)
for name in ("VK_LOADER_DRIVERS_SELECT", "VK_LOADER_DRIVERS_DISABLE"):
monkeypatch.delenv(name, raising = False)
monkeypatch.setattr(ilp, "_vulkan_icd_search_dirs", lambda: [])
monkeypatch.setenv("VK_DRIVER_FILES", _icd(tmp_path / "radeon_icd.x86_64.json"))
assert ilp._amd_vulkan_icd_present() is True
monkeypatch.setenv("VK_LOADER_DRIVERS_DISABLE", "*")
assert ilp._amd_vulkan_icd_present() is False
def test_a_32_bit_linux_manifest_does_not_answer_for_the_x64_bundle(monkeypatch, tmp_path):
# multilib mesa ships radeon_icd.i686.json, which a 64-bit llama-server cannot load.
monkeypatch.setattr(ilp.sys, "platform", "linux")
monkeypatch.setattr(ilp, "_amd_vulkan_icd_present", _REAL_AMD_VULKAN_ICD_PRESENT)
for name in ("VK_DRIVER_FILES", "VK_ICD_FILENAMES"):
monkeypatch.delenv(name, raising = False)
icd_dir = tmp_path / "usr/share/vulkan/icd.d"
monkeypatch.setattr(ilp, "_vulkan_icd_search_dirs", lambda: [icd_dir])
for name in ("radeon_icd.i686.json", "amd_icd32.json", "amd-vulkan32.json"):
_icd(icd_dir / name)
assert ilp._amd_vulkan_icd_present() is False
_icd(icd_dir / "radeon_icd.x86_64.json")
assert ilp._amd_vulkan_icd_present() is True
def test_an_override_of_only_stale_paths_answers_on_its_own(monkeypatch, tmp_path):
# A force list disables directory search, so manifests elsewhere are never read.
monkeypatch.setattr(ilp.sys, "platform", "linux")
monkeypatch.setattr(ilp, "_amd_vulkan_icd_present", _REAL_AMD_VULKAN_ICD_PRESENT)
monkeypatch.delenv("VK_ICD_FILENAMES", raising = False)
icd_dir = tmp_path / "usr/share/vulkan/icd.d"
icd_dir.mkdir(parents = True)
monkeypatch.setattr(ilp, "_vulkan_icd_search_dirs", lambda: [icd_dir])
_icd(icd_dir / "radeon_icd.x86_64.json")
monkeypatch.delenv("VK_DRIVER_FILES", raising = False)
assert ilp._amd_vulkan_icd_present() is True
monkeypatch.setenv("VK_DRIVER_FILES", str(tmp_path / "gone" / "intel_icd.json"))
assert ilp._amd_vulkan_icd_present() is False
def test_the_deprecated_override_is_not_consulted_behind_the_current_one(monkeypatch, tmp_path):
# VK_DRIVER_FILES supersedes VK_ICD_FILENAMES rather than joining it.
monkeypatch.setattr(ilp.sys, "platform", "linux")
monkeypatch.setattr(ilp, "_amd_vulkan_icd_present", _REAL_AMD_VULKAN_ICD_PRESENT)
monkeypatch.setattr(ilp, "_vulkan_icd_search_dirs", lambda: [])
amd = _icd(tmp_path / "radeon_icd.x86_64.json")
intel = _icd(tmp_path / "intel_icd.x86_64.json")
monkeypatch.setenv("VK_ICD_FILENAMES", amd)
monkeypatch.delenv("VK_DRIVER_FILES", raising = False)
assert ilp._amd_vulkan_icd_present() is True
monkeypatch.setenv("VK_DRIVER_FILES", intel)
assert ilp._amd_vulkan_icd_present() is False
def test_the_icd_search_dirs_follow_the_xdg_variables(monkeypatch, tmp_path):
monkeypatch.setenv("XDG_DATA_DIRS", f"{tmp_path / 'a'}{os.pathsep}{tmp_path / 'b'}")
monkeypatch.setenv("XDG_DATA_HOME", str(tmp_path / "home-data"))
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path / "home-config"))
monkeypatch.setenv("XDG_CONFIG_DIRS", str(tmp_path / "sys-config"))
dirs = [str(d) for d in ilp._vulkan_icd_search_dirs()]
for base in ("home-config", "sys-config", "home-data", "a", "b"):
assert str(tmp_path / base / "vulkan/icd.d") in dirs, base
assert "/usr/share/vulkan/icd.d" not in dirs, "a set XDG_DATA_DIRS replaces the default"
assert dirs.index(str(tmp_path / "home-config" / "vulkan/icd.d")) < dirs.index(
str(tmp_path / "home-data" / "vulkan/icd.d")
)
for var in ("XDG_DATA_DIRS", "XDG_DATA_HOME", "XDG_CONFIG_HOME", "XDG_CONFIG_DIRS"):
monkeypatch.delenv(var, raising = False)
defaults = [str(d) for d in ilp._vulkan_icd_search_dirs()]
assert "/usr/share/vulkan/icd.d" in defaults
assert "/usr/local/share/vulkan/icd.d" in defaults
assert "/etc/vulkan/icd.d" in defaults
@pytest.mark.parametrize(
"manifest",
["radeon_icd.x86_64.json", "amd_icd64.json", "amd_pro_icd64.json", "amdvlk64.json"],
)
def test_amd_vulkan_icd_probe_recognises_every_shipped_amd_manifest(
manifest, monkeypatch, tmp_path
):
monkeypatch.setattr(ilp.sys, "platform", "linux")
monkeypatch.setattr(ilp, "_amd_vulkan_icd_present", _REAL_AMD_VULKAN_ICD_PRESENT)
monkeypatch.delenv("VK_ICD_FILENAMES", raising = False)
monkeypatch.setenv("VK_DRIVER_FILES", _icd(tmp_path / manifest))
assert ilp._amd_vulkan_icd_present() is True
def test_amd_vulkan_icd_probe_judges_the_manifest_name_not_the_directory(monkeypatch, tmp_path):
# Matching 'amd' anywhere in the path gave True for pytest's tmp_path.
monkeypatch.setattr(ilp.sys, "platform", "linux")
monkeypatch.setattr(ilp, "_amd_vulkan_icd_present", _REAL_AMD_VULKAN_ICD_PRESENT)
monkeypatch.delenv("VK_ICD_FILENAMES", raising = False)
monkeypatch.setattr(ilp, "_vulkan_icd_search_dirs", lambda: [])
monkeypatch.setenv("VK_DRIVER_FILES", _icd(tmp_path / "amd_vulkan_stuff" / "intel_icd.json"))
assert ilp._amd_vulkan_icd_present() is False
def test_amd_vulkan_icd_probe_scans_the_loader_search_directories(monkeypatch, tmp_path):
monkeypatch.setattr(ilp.sys, "platform", "linux")
monkeypatch.setattr(ilp, "_amd_vulkan_icd_present", _REAL_AMD_VULKAN_ICD_PRESENT)
for _env in ("VK_DRIVER_FILES", "VK_ICD_FILENAMES"):
monkeypatch.delenv(_env, raising = False)
icd_dir = tmp_path / "usr/share/vulkan/icd.d"
icd_dir.mkdir(parents = True)
monkeypatch.setattr(ilp, "_vulkan_icd_search_dirs", lambda: [icd_dir])
_icd(icd_dir / "nvidia_icd.json")
assert ilp._amd_vulkan_icd_present() is False
_icd(icd_dir / "radeon_icd.x86_64.json")
assert ilp._amd_vulkan_icd_present() is True
def test_a_manifest_whose_driver_is_gone_is_not_evidence(monkeypatch, tmp_path):
monkeypatch.setattr(ilp.sys, "platform", "linux")
monkeypatch.setattr(ilp, "_amd_vulkan_icd_present", _REAL_AMD_VULKAN_ICD_PRESENT)
monkeypatch.delenv("VK_ICD_FILENAMES", raising = False)
monkeypatch.setattr(ilp, "_vulkan_icd_search_dirs", lambda: [])
stale = _icd(tmp_path / "stale" / "radeon_icd.x86_64.json", library = None)
monkeypatch.setenv("VK_DRIVER_FILES", stale)
assert ilp._amd_vulkan_icd_present() is False
monkeypatch.setenv("VK_DRIVER_FILES", _icd(tmp_path / "live" / "radeon_icd.x86_64.json"))
assert ilp._amd_vulkan_icd_present() is True
def test_an_unparsable_or_libraryless_manifest_is_not_evidence(monkeypatch, tmp_path):
monkeypatch.setattr(ilp.sys, "platform", "linux")
monkeypatch.setattr(ilp, "_amd_vulkan_icd_present", _REAL_AMD_VULKAN_ICD_PRESENT)
monkeypatch.delenv("VK_ICD_FILENAMES", raising = False)
monkeypatch.setattr(ilp, "_vulkan_icd_search_dirs", lambda: [])
for body in ("{ not json", "{}", '{"ICD": {}}', '{"ICD": {"library_path": ""}}'):
path = tmp_path / "radeon_icd.x86_64.json"
path.write_text(body, encoding = "utf-8")
monkeypatch.setenv("VK_DRIVER_FILES", str(path))
assert ilp._amd_vulkan_icd_present() is False, body
def test_a_bare_driver_name_is_accepted(monkeypatch, tmp_path):
# Adrenalin registers a bare amdvlk64.dll resolved via the system search path.
monkeypatch.setattr(ilp.sys, "platform", "linux")
monkeypatch.setattr(ilp, "_amd_vulkan_icd_present", _REAL_AMD_VULKAN_ICD_PRESENT)
monkeypatch.delenv("VK_ICD_FILENAMES", raising = False)
monkeypatch.setattr(ilp, "_vulkan_icd_search_dirs", lambda: [])
path = tmp_path / "amdvlk64.json"
path.write_text(json.dumps({"ICD": {"library_path": "amdvlk64.dll"}}), encoding = "utf-8")
monkeypatch.setenv("VK_DRIVER_FILES", str(path))
assert ilp._amd_vulkan_icd_present() is True
def test_amd_vulkan_icd_probe_never_raises(monkeypatch):
monkeypatch.setattr(
ilp, "_amd_vulkan_icd_manifest_paths", lambda: (_ for _ in ()).throw(OSError("boom"))
)
assert ilp._amd_vulkan_icd_present() is False
def test_llama_cpp_backend_cpu_opts_out_of_auto_vulkan(monkeypatch):
monkeypatch.setenv("UNSLOTH_LLAMA_CPP_BACKEND", "cpu")
host = _windows_amd_host(rocm_gfx_target = "gfx803", rocm_gfx_targets = ["gfx803"])
routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert routed is host
assert repo == FORK
assert persist is None
assert ilp.force_vulkan_requested() is False
def test_explicit_backend_beats_legacy_force_vulkan(monkeypatch):
monkeypatch.setenv("UNSLOTH_FORCE_VULKAN", "1")
monkeypatch.setenv("UNSLOTH_LLAMA_CPP_BACKEND", "cpu")
assert ilp.resolved_llama_backend() == "cpu"
assert ilp.force_vulkan_requested() is False
host = _windows_amd_host(rocm_gfx_target = "gfx1100", rocm_gfx_targets = ["gfx1100"])
_routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert repo == FORK
assert persist is None
def test_unknown_llama_backend_value_falls_through_to_legacy_flag(monkeypatch):
monkeypatch.setenv("UNSLOTH_LLAMA_CPP_BACKEND", "banana")
assert ilp.resolved_llama_backend() is None
assert ilp.force_vulkan_requested() is False
monkeypatch.setenv("UNSLOTH_FORCE_VULKAN", "1")
assert ilp.force_vulkan_requested() is True
def test_llama_backend_flag_beats_conflicting_env(monkeypatch):
monkeypatch.setenv("UNSLOTH_LLAMA_CPP_BACKEND", "cpu")
assert ilp.force_vulkan_requested("vulkan") is True
host = _windows_amd_host(rocm_gfx_target = "gfx1100", rocm_gfx_targets = ["gfx1100"])
_routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(
host, FORK, "pin", force_cpu = False, llama_backend = "vulkan"
)
assert repo == FORK
assert persist == "vulkan"
def _windows_arm64_host(**overrides):
defaults = dict(
system = "Windows",
machine = "ARM64",
is_windows = True,
is_linux = False,
is_macos = False,
is_x86_64 = False,
is_arm64 = True,
nvidia_smi = None,
driver_cuda_version = None,
compute_caps = [],
visible_cuda_devices = None,
has_physical_nvidia = False,
has_usable_nvidia = False,
has_rocm = False,
has_intel_gpu = False,
)
defaults.update(overrides)
return ilp.HostInfo(**defaults)
@pytest.mark.parametrize(
"env, flag",
[
({"UNSLOTH_LLAMA_CPP_BACKEND": "vulkan"}, None),
({"UNSLOTH_FORCE_VULKAN": "1"}, None),
({}, "vulkan"),
],
)
def test_vulkan_opt_in_routes_windows_arm64_to_vulkan(monkeypatch, env, flag):
# Windows arm64 Vulkan bundles exist (#11674): an explicit request is honoured.
for name, value in env.items():
monkeypatch.setenv(name, value)
routed, repo, tag, persist = ilp._route_to_vulkan_prebuilt(
_windows_arm64_host(), FORK, "pin", force_cpu = False, llama_backend = flag
)
assert routed.vulkan_routed is True
assert routed.is_arm64 is True
assert routed.vulkan_device_required is False
assert (repo, tag) == (FORK, "pin")
assert persist == "vulkan"
# VK_DRIVER_ID_QUALCOMM_PROPRIETARY = 8, VK_DRIVER_ID_ARM_PROPRIETARY = 9, VK_DRIVER_ID_MESA_TURNIP = 18.
_ADRENO = (ilp._VulkanGpuName("Qualcomm(R) Adreno(TM) X1-85 GPU", 0x5143, 8),)
_TURNIP = (ilp._VulkanGpuName("Turnip Adreno (TM) 750", 0x5143, 18),)
_MALI = (ilp._VulkanGpuName("Mali-G720", 0x13B5, 9),)
_ARM64_GPU = (ilp._VulkanGpuName("Arm64 Vulkan GPU", 0x13B5, 9),)
def _no_vulkan_probe():
raise AssertionError("this host must not probe for a Vulkan GPU")
@pytest.mark.parametrize("has_intel_gpu", [False, True])
@pytest.mark.parametrize("flag", [None, "auto"])
def test_windows_arm64_auto_routes_to_vulkan_when_a_gpu_enumerates(
monkeypatch, flag, has_intel_gpu
):
monkeypatch.delenv("UNSLOTH_LLAMA_CPP_BACKEND", raising = False)
monkeypatch.delenv("UNSLOTH_FORCE_VULKAN", raising = False)
monkeypatch.setattr(ilp, "_vulkan_gpu_devices", lambda: _ARM64_GPU)
routed, repo, tag, persist = ilp._route_to_vulkan_prebuilt(
_windows_arm64_host(has_intel_gpu = has_intel_gpu),
FORK,
"pin",
force_cpu = False,
llama_backend = flag,
)
assert routed.vulkan_routed is True
# Detected, not chosen: updates re-detect and the Vulkan CPU crash recovery stays armed.
assert (repo, tag, persist) == (FORK, "pin", None)
@pytest.mark.parametrize("flag", [None, "auto"])
def test_windows_arm64_adreno_stays_on_cpu_unless_vulkan_is_requested(monkeypatch, capsys, flag):
# ggml-org/llama.cpp#28290: garbage output on Snapdragon X, so Adreno is opt-in only.
monkeypatch.delenv("UNSLOTH_LLAMA_CPP_BACKEND", raising = False)
monkeypatch.delenv("UNSLOTH_FORCE_VULKAN", raising = False)
monkeypatch.setattr(ilp, "_vulkan_gpu_devices", lambda: _ADRENO)
host = _windows_arm64_host()
routed, repo, tag, persist = ilp._route_to_vulkan_prebuilt(
host, FORK, "pin", force_cpu = False, llama_backend = flag
)
assert routed is host and routed.vulkan_routed is False
assert (repo, tag, persist) == (FORK, "pin", None)
captured = capsys.readouterr()
assert "UNSLOTH_LLAMA_CPP_BACKEND=vulkan" in captured.out + captured.err
@pytest.mark.parametrize(
"env, flag",
[
({"UNSLOTH_LLAMA_CPP_BACKEND": "vulkan"}, None),
({"UNSLOTH_FORCE_VULKAN": "1"}, None),
({}, "vulkan"),
],
)
def test_windows_arm64_adreno_takes_vulkan_when_requested(monkeypatch, env, flag):
monkeypatch.delenv("UNSLOTH_LLAMA_CPP_BACKEND", raising = False)
monkeypatch.delenv("UNSLOTH_FORCE_VULKAN", raising = False)
for name, value in env.items():
monkeypatch.setenv(name, value)
monkeypatch.setattr(ilp, "_vulkan_gpu_devices", lambda: _ADRENO)
routed, _repo, _tag, persist = ilp._route_to_vulkan_prebuilt(
_windows_arm64_host(), FORK, "pin", force_cpu = False, llama_backend = flag
)
assert routed.vulkan_routed is True
assert persist == "vulkan"
@pytest.mark.parametrize("devices", [_MALI, _TURNIP], ids = ["mali", "turnip"])
def test_linux_arm64_mesa_and_mali_still_auto_route_to_vulkan(monkeypatch, devices):
monkeypatch.delenv("UNSLOTH_LLAMA_CPP_BACKEND", raising = False)
monkeypatch.delenv("UNSLOTH_FORCE_VULKAN", raising = False)
monkeypatch.delenv("GGML_VK_VISIBLE_DEVICES", raising = False)
monkeypatch.setattr(ilp, "_vulkan_gpu_devices", lambda: devices)
routed, _repo, _tag, persist = ilp._route_to_vulkan_prebuilt(
_cpu_bound_host("Linux", "arm64"), FORK, "pin", force_cpu = False
)
assert routed.vulkan_routed is True and routed.vulkan_device_required is True
assert persist is None
def test_the_probe_keeps_each_device_vendor_and_driver(monkeypatch):
probe = _probe_result(
monkeypatch, [_vk_device("Adreno X1-85"), _vk_device("Mali", vendor = 0x13B5, driver = 9)]
)
devices = probe()
assert devices == ("Adreno X1-85", "Mali")
assert [d.vendor for d in devices] == [0x5143, 0x13B5]
assert [d.driver for d in devices] == [8, 9]
@pytest.mark.parametrize("has_intel_gpu", [False, True])
def test_windows_arm64_without_a_vulkan_gpu_keeps_its_cpu_bundle(monkeypatch, has_intel_gpu):
# A stale Intel adapter entry in the registry names no GPU here: only the probe counts.
monkeypatch.delenv("UNSLOTH_LLAMA_CPP_BACKEND", raising = False)
monkeypatch.delenv("UNSLOTH_FORCE_VULKAN", raising = False)
monkeypatch.setattr(ilp, "_vulkan_gpu_devices", lambda: ())
host = _windows_arm64_host(has_intel_gpu = has_intel_gpu)
routed, repo, tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert routed is host
assert (repo, tag, persist) == (FORK, "pin", None)
def _cpu_bound_host(system, arch, **overrides):
is_arm64 = arch == "arm64"
defaults = dict(
system = system,
machine = "ARM64" if is_arm64 else "x86_64",
is_windows = system == "Windows",
is_linux = system == "Linux",
is_macos = system == "Darwin",
is_x86_64 = not is_arm64,
is_arm64 = is_arm64,
nvidia_smi = None,
driver_cuda_version = None,
compute_caps = [],
visible_cuda_devices = None,
has_physical_nvidia = False,
has_usable_nvidia = False,
has_rocm = False,
has_intel_gpu = False,
)
defaults.update(overrides)
return ilp.HostInfo(**defaults)
@pytest.mark.parametrize(
"system, arch, overrides",
[
("Windows", "x64", {}),
("Linux", "x64", {}),
("Linux", "arm64", {}),
# Linux arm64 never took the AMD widening x86_64 has; a GPU that enumerates now counts.
("Linux", "arm64", {"has_amd_gpu_without_rocm": True}),
],
)
def test_cpu_bound_host_with_a_vulkan_gpu_routes_to_vulkan(monkeypatch, system, arch, overrides):
monkeypatch.delenv("UNSLOTH_LLAMA_CPP_BACKEND", raising = False)
monkeypatch.delenv("UNSLOTH_FORCE_VULKAN", raising = False)
monkeypatch.delenv("GGML_VK_VISIBLE_DEVICES", raising = False)
monkeypatch.setattr(ilp, "_vulkan_gpu_devices", lambda: ("Mali-G720",))
host = _cpu_bound_host(system, arch, **overrides)
routed, _repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert routed.vulkan_routed is True and routed.has_intel_gpu is True
assert routed.vulkan_device_required is True
assert persist is None
@pytest.mark.parametrize(
"host_args, kwargs, env",
[
(("Darwin", "arm64"), {}, {}),
(("Linux", "x64", {"has_physical_nvidia": True}), {}, {}),
(("Linux", "x64", {"has_rocm": True}), {}, {}),
(("Windows", "arm64", {"has_physical_nvidia": True, "has_usable_nvidia": True}), {}, {}),
# x86_64 AMD without ROCm already takes Vulkan in the selectors.
(("Windows", "x64", {"has_amd_gpu_without_rocm": True}), {}, {}),
(("Linux", "x64"), {"force_cpu": True}, {}),
(("Linux", "x64"), {}, {"UNSLOTH_LLAMA_CPP_BACKEND": "cpu"}),
(("Windows", "arm64"), {}, {"UNSLOTH_LLAMA_CPP_BACKEND": "cuda"}),
(("Linux", "arm64"), {}, {"GGML_VK_VISIBLE_DEVICES": ""}),
],
ids = [
"macos",
"nvidia",
"rocm",
"arm64-nvidia",
"x64-amd-no-rocm",
"force-cpu",
"backend-cpu",
"backend-cuda",
"vk-mask",
],
)
def test_hosts_not_headed_for_cpu_never_probe_vulkan(monkeypatch, host_args, kwargs, env):
monkeypatch.delenv("UNSLOTH_LLAMA_CPP_BACKEND", raising = False)
monkeypatch.delenv("UNSLOTH_FORCE_VULKAN", raising = False)
monkeypatch.delenv("GGML_VK_VISIBLE_DEVICES", raising = False)
for name, value in env.items():
monkeypatch.setenv(name, value)
monkeypatch.setattr(ilp, "_vulkan_gpu_devices", _no_vulkan_probe)
host = _cpu_bound_host(*host_args[:2], **(host_args[2] if len(host_args) > 2 else {}))
routed, _repo, _tag, persist = ilp._route_to_vulkan_prebuilt(
host, FORK, "pin", force_cpu = kwargs.get("force_cpu", False)
)
assert routed.vulkan_routed is False
assert persist is None
def _probe_result(
monkeypatch,
devices,
returncode = 0,
):
out = devices if isinstance(devices, str) else json.dumps(devices)
monkeypatch.setattr(
ilp,
"run_capture",
lambda *a, **k: subprocess.CompletedProcess(a[0], returncode, out + "\n", ""),
)
for name in ("CUDA_VISIBLE_DEVICES", "ROCR_VISIBLE_DEVICES", "HIP_VISIBLE_DEVICES"):
monkeypatch.delenv(name, raising = False)
monkeypatch.setenv("UNSLOTH_VULKAN_DEVICE_PROBE", "1")
return ilp._vulkan_gpu_devices.__wrapped__
def _vk_device(
name,
vendor = 0x5143,
type_ = 1,
api = (1 << 22) | (3 << 12),
driver = 8,
fp16 = True,
):
return {
"vendor": vendor,
"type": type_,
"api": api,
"driver": driver,
"fp16": fp16,
"name": name,
}
def test_vulkan_probe_keeps_only_gpus_llama_cpp_takes(monkeypatch):
probe = _probe_result(
monkeypatch,
[
_vk_device("Adreno"),
_vk_device("Radeon", vendor = 0x1002, type_ = 2, driver = 3),
_vk_device("llvmpipe", vendor = 0x10005, type_ = 4, driver = 13),
_vk_device("Dozen", driver = 23),
_vk_device("Vulkan 1.1 part", api = (1 << 22) | (1 << 12)),
_vk_device("No 16-bit storage", fp16 = False),
_vk_device("virtio", type_ = 3),
],
)
assert probe() == ("Adreno", "Radeon")
def test_vulkan_probe_hides_vendors_the_caller_masked(monkeypatch):
devices = [
_vk_device("RTX", vendor = 0x10DE, type_ = 2, driver = 4),
_vk_device("Radeon", vendor = 0x1002),
]
probe = _probe_result(monkeypatch, devices)
monkeypatch.setattr(ilp.sys, "platform", "linux")
monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "")
assert probe() == ("Radeon",)
monkeypatch.setenv("ROCR_VISIBLE_DEVICES", "0")
assert probe() == ()
@pytest.mark.parametrize("stdout, returncode", [("[]", 0), ("not json", 0), ("", 0), ("[]", 1)])
def test_vulkan_probe_failures_mean_no_gpu(monkeypatch, stdout, returncode):
probe = _probe_result(monkeypatch, stdout, returncode)
assert probe() == ()
def test_vulkan_probe_off_switch_skips_the_child(monkeypatch):
probe = _probe_result(monkeypatch, [_vk_device("Adreno")])
monkeypatch.setattr(ilp, "run_capture", lambda *a, **k: pytest.fail("probe ran"))
monkeypatch.setenv("UNSLOTH_VULKAN_DEVICE_PROBE", "0")
assert probe() == ()
def test_vulkan_probe_script_with_no_driver_lists_nothing(tmp_path):
# The real child, with the loader forced onto a driver file that does not exist.
env = {**os.environ, "VK_DRIVER_FILES": str(tmp_path / "x.json")}
result = subprocess.run(
[sys.executable, "-I", "-c", ilp._VULKAN_DEVICE_PROBE],
capture_output = True,
text = True,
timeout = 60,
env = env,
)
# No loader at all (macOS, a bare container) exits non-zero, which also reads as no GPU.
if result.returncode == 0:
assert json.loads(result.stdout) == []
_FORK_TAG = "b11541-mix-2500203"
_ARM64_VULKAN = f"app-{_FORK_TAG}-windows-arm64-vulkan.zip"
_X64_VULKAN = f"app-{_FORK_TAG}-windows-x64-vulkan.zip"
_ARM64_CPU = f"app-{_FORK_TAG}-windows-arm64-cpu.zip"
def _windows_arm64_release(*names):
# Manifest shape the fork publishes (install_kind, bundle_profile, rank, sha index kind).
kinds = {
_ARM64_VULKAN: ("windows-arm64-vulkan", "windows-vulkan-arm64", 60),
_X64_VULKAN: ("windows-vulkan", "windows-vulkan-x64", 60),
_ARM64_CPU: ("windows-arm64", "windows-cpu-arm64", 1000),
}
artifacts = [
ilp.PublishedLlamaArtifact(
asset_name = name,
install_kind = kinds[name][0],
runtime_line = None,
coverage_class = None,
supported_sms = [],
min_sm = None,
max_sm = None,
bundle_profile = kinds[name][1],
rank = kinds[name][2],
)
for name in names
]
bundle = ilp.PublishedReleaseBundle(
repo = FORK,
release_tag = _FORK_TAG,
upstream_tag = "b11541",
assets = {name: f"https://example/{name}" for name in names},
artifacts = artifacts,
)
checksums = ilp.ApprovedReleaseChecksums(
repo = FORK,
release_tag = bundle.release_tag,
upstream_tag = bundle.upstream_tag,
artifacts = {
artifact.asset_name: ilp.ApprovedArtifactHash(
asset_name = artifact.asset_name,
sha256 = "a" * 64,
repo = FORK,
kind = f"{artifact.install_kind}-app",
)
for artifact in artifacts
},
)
return bundle, checksums
def _forced_windows_arm64_host():
routed, _repo, _tag, _persist = ilp._route_to_vulkan_prebuilt(
_windows_arm64_host(), FORK, "", force_cpu = False, llama_backend = "vulkan"
)
return routed
def test_auto_routed_windows_arm64_picks_arm64_vulkan_then_cpu(monkeypatch):
monkeypatch.delenv("UNSLOTH_LLAMA_CPP_BACKEND", raising = False)
monkeypatch.delenv("UNSLOTH_FORCE_VULKAN", raising = False)
monkeypatch.setattr(ilp, "_vulkan_gpu_devices", lambda: _ARM64_GPU)
routed, _repo, _tag, _persist = ilp._route_to_vulkan_prebuilt(
_windows_arm64_host(), FORK, "", force_cpu = False
)
bundle, checksums = _windows_arm64_release(_X64_VULKAN, _ARM64_VULKAN, _ARM64_CPU)
attempts = ilp.resolve_release_asset_choice(routed, bundle.upstream_tag, bundle, checksums)
assert [a.name for a in attempts] == [_ARM64_VULKAN, _ARM64_CPU]
def test_auto_routed_linux_arm64_picks_arm64_vulkan_then_cpu(monkeypatch):
monkeypatch.delenv("UNSLOTH_LLAMA_CPP_BACKEND", raising = False)
monkeypatch.delenv("UNSLOTH_FORCE_VULKAN", raising = False)
monkeypatch.delenv("GGML_VK_VISIBLE_DEVICES", raising = False)
monkeypatch.setattr(ilp, "_vulkan_gpu_devices", lambda: ("Mali-G720",))
routed, _repo, _tag, _persist = ilp._route_to_vulkan_prebuilt(
_cpu_bound_host("Linux", "arm64"), FORK, "", force_cpu = False
)
names = {
f"app-{_FORK_TAG}-linux-x64-vulkan.tar.gz": ("linux-vulkan", "linux-vulkan-x64", 60),
f"app-{_FORK_TAG}-linux-arm64-vulkan.tar.gz": ("linux-vulkan", "linux-vulkan-arm64", 60),
f"app-{_FORK_TAG}-linux-arm64-cpu.tar.gz": ("linux-arm64", "linux-cpu-arm64", 1000),
}
bundle = ilp.PublishedReleaseBundle(
repo = FORK,
release_tag = _FORK_TAG,
upstream_tag = "b11541",
assets = {name: f"https://example/{name}" for name in names},
artifacts = [
ilp.PublishedLlamaArtifact(
asset_name = name,
install_kind = kind,
runtime_line = None,
coverage_class = None,
supported_sms = [],
min_sm = None,
max_sm = None,
bundle_profile = profile,
rank = rank,
)
for name, (kind, profile, rank) in names.items()
],
)
attempts = ilp._linux_published_attempts(routed, bundle)
assert [a.name for a in attempts] == [
f"app-{_FORK_TAG}-linux-arm64-vulkan.tar.gz",
f"app-{_FORK_TAG}-linux-arm64-cpu.tar.gz",
]
def test_forced_vulkan_windows_arm64_picks_the_published_arm64_vulkan_bundle():
bundle, checksums = _windows_arm64_release(
_X64_VULKAN,
_ARM64_VULKAN,
_ARM64_CPU,
)
attempts = ilp.resolve_release_asset_choice(
_forced_windows_arm64_host(), bundle.upstream_tag, bundle, checksums
)
assert [(a.name, a.install_kind) for a in attempts] == [
(_ARM64_VULKAN, "windows-arm64-vulkan"),
(_ARM64_CPU, "windows-arm64"),
]
# The strict Vulkan filter a named request applies keeps the arm64 Vulkan bundle.
assert [a.install_kind for a in ilp._backend_only_attempts(attempts, "vulkan")] == [
"windows-arm64-vulkan"
]
def test_forced_vulkan_windows_arm64_without_the_bundle_falls_back_to_cpu(capsys):
# Current fork releases ship no arm64 Vulkan archive; the x64 one must never be taken.
bundle, checksums = _windows_arm64_release(_X64_VULKAN, _ARM64_CPU)
attempts = ilp.resolve_release_asset_choice(
_forced_windows_arm64_host(), bundle.upstream_tag, bundle, checksums
)
assert [(a.name, a.install_kind) for a in attempts] == [(_ARM64_CPU, "windows-arm64")]
captured = capsys.readouterr()
assert "no published Windows ARM64 Vulkan bundle" in captured.out + captured.err
@pytest.mark.parametrize("has_intel_gpu", [False, True])
def test_unforced_windows_arm64_keeps_the_published_cpu_bundle(has_intel_gpu):
bundle, checksums = _windows_arm64_release(_ARM64_VULKAN, _ARM64_CPU)
attempts = ilp.resolve_release_asset_choice(
_windows_arm64_host(has_intel_gpu = has_intel_gpu), bundle.upstream_tag, bundle, checksums
)
assert [(a.name, a.install_kind) for a in attempts] == [(_ARM64_CPU, "windows-arm64")]
_WIN_ARM64_UPSTREAM_ASSETS = [
"llama-b11400-bin-win-vulkan-x64.zip",
"llama-b11400-bin-win-vulkan-arm64.zip",
"llama-b11400-bin-win-cpu-arm64.zip",
]
def test_direct_upstream_forced_vulkan_windows_arm64_prefers_vulkan():
rel = _upstream_release("b11400", _WIN_ARM64_UPSTREAM_ASSETS)
plan = ilp.direct_upstream_release_plan(rel, _forced_windows_arm64_host(), UPSTREAM, "latest")
assert [(a.name, a.install_kind) for a in plan.attempts] == [
("llama-b11400-bin-win-vulkan-arm64.zip", "windows-arm64-vulkan"),
("llama-b11400-bin-win-cpu-arm64.zip", "windows-arm64"),
]
def test_direct_upstream_unforced_windows_arm64_stays_cpu():
rel = _upstream_release("b11400", _WIN_ARM64_UPSTREAM_ASSETS)
plan = ilp.direct_upstream_release_plan(
rel, _windows_arm64_host(has_intel_gpu = True), UPSTREAM, "latest"
)
assert [a.install_kind for a in plan.attempts] == ["windows-arm64"]
def test_upstream_resolver_forced_vulkan_windows_arm64(monkeypatch):
assets = {name: f"https://example/{name}" for name in _WIN_ARM64_UPSTREAM_ASSETS}
monkeypatch.setattr(ilp, "github_release_assets", lambda repo, tag: dict(assets))
choice = ilp.resolve_upstream_asset_choice(_forced_windows_arm64_host(), "b11400")
assert (choice.name, choice.install_kind) == (
"llama-b11400-bin-win-vulkan-arm64.zip",
"windows-arm64-vulkan",
)
del assets["llama-b11400-bin-win-vulkan-arm64.zip"]
choice = ilp.resolve_upstream_asset_choice(_forced_windows_arm64_host(), "b11400")
assert choice.install_kind == "windows-arm64"
choice = ilp.resolve_upstream_asset_choice(_windows_arm64_host(has_intel_gpu = True), "b11400")
assert choice.install_kind == "windows-arm64"
def test_windows_arm64_vulkan_kind_is_a_vulkan_bundle():
from utils.prebuilt.llama_backend import backend_for_install_kind, marker_backend
assert backend_for_install_kind("windows-arm64-vulkan") == "vulkan"
assert "windows-arm64-vulkan" in ilp.VULKAN_INSTALL_KINDS
assert "windows-arm64-vulkan" in ilp.install_kinds_for_backend("vulkan")
assert marker_backend({"install_kind": "windows-arm64-vulkan"}) == "vulkan"
choice = _choice("windows-arm64-vulkan", "llama-b11400-bin-win-vulkan-arm64.zip")
assert ilp.persisted_llama_backend("vulkan", choice) == "vulkan"
assert "*.dll" in ilp.runtime_patterns_for_choice(choice)
assert ["ggml-vulkan.dll"] in ilp.runtime_payload_health_groups(
"windows-arm64-vulkan", source_label = "upstream", tag = "b11400"
)
def test_vulkan_opt_in_still_routes_on_windows_x64(monkeypatch):
monkeypatch.setenv("UNSLOTH_LLAMA_CPP_BACKEND", "vulkan")
host = _windows_amd_host(rocm_gfx_target = "gfx1100", rocm_gfx_targets = ["gfx1100"])
_routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert repo == FORK
assert persist == "vulkan"
def _choice(install_kind, name = "asset.zip"):
return ilp.AssetChoice(
repo = UPSTREAM,
tag = "b9925",
name = name,
url = f"https://example/{name}",
source_label = "upstream",
install_kind = install_kind,
)
@pytest.mark.parametrize("kind", ["windows-vulkan", "linux-vulkan"])
def test_persisted_llama_backend_keeps_vulkan_for_a_vulkan_bundle(kind):
assert ilp.persisted_llama_backend("vulkan", _choice(kind)) == "vulkan"
@pytest.mark.parametrize("kind", ["windows-arm64", "windows-cpu", "linux-cpu", "windows-rocm"])
def test_persisted_llama_backend_drops_vulkan_for_a_non_vulkan_bundle(kind):
# _plan_llama_phase re-asserts the marker backend, so a CPU fallthrough must not claim Vulkan.
assert ilp.persisted_llama_backend("vulkan", _choice(kind)) is None
def test_persisted_llama_backend_passes_none_through():
assert ilp.persisted_llama_backend(None, _choice("windows-vulkan")) is None
def test_persisted_llama_backend_keeps_automatic_vulkan_distinct():
assert ilp.persisted_llama_backend("auto", _choice("windows-vulkan")) == "auto"
def test_marker_records_no_backend_when_vulkan_fell_back_to_cpu(tmp_path):
checksums = ilp.ApprovedReleaseChecksums(
repo = UPSTREAM,
release_tag = "b9925",
upstream_tag = "b9925",
source_repo = UPSTREAM,
source_repo_url = f"https://github.com/{UPSTREAM}",
)
cpu = _choice("windows-arm64", "llama-b9925-bin-win-cpu-arm64.zip")
ilp.write_prebuilt_metadata(
tmp_path,
requested_tag = "latest",
llama_tag = "b9925",
release_tag = "b9925",
choice = cpu,
approved_checksums = checksums,
prebuilt_fallback_used = False,
llama_backend = "vulkan",
)
marker = json.loads((tmp_path / "UNSLOTH_PREBUILT_INFO.json").read_text())
assert marker["asset"] == "llama-b9925-bin-win-cpu-arm64.zip"
assert marker["llama_backend"] is None
vulkan = _choice("windows-vulkan", "llama-b9925-bin-win-vulkan-x64.zip")
ilp.write_prebuilt_metadata(
tmp_path,
requested_tag = "latest",
llama_tag = "b9925",
release_tag = "b9925",
choice = vulkan,
approved_checksums = checksums,
prebuilt_fallback_used = False,
llama_backend = "vulkan",
)
marker = json.loads((tmp_path / "UNSLOTH_PREBUILT_INFO.json").read_text())
assert marker["llama_backend"] == "vulkan"
def test_automatic_amd_vulkan_marker_records_the_routing_gfx(tmp_path, monkeypatch):
"""The auto Vulkan marker must store the routing gfx; the asset name has none for an update to read."""
monkeypatch.delenv("UNSLOTH_FORCE_VULKAN", raising = False)
monkeypatch.delenv("HIP_VISIBLE_DEVICES", raising = False)
monkeypatch.delenv("ROCR_VISIBLE_DEVICES", raising = False)
monkeypatch.delenv("CUDA_VISIBLE_DEVICES", raising = False)
unprobed = ilp.HostInfo(
system = "Windows",
machine = "amd64",
is_windows = True,
is_linux = False,
is_macos = False,
is_x86_64 = True,
is_arm64 = False,
nvidia_smi = None,
driver_cuda_version = None,
compute_caps = [],
visible_cuda_devices = None,
has_physical_nvidia = False,
has_usable_nvidia = False,
has_rocm = False,
has_intel_gpu = False,
)
host = ilp._apply_host_overrides(unprobed, override_rocm_gfx = "gfx1034")
assert ilp._should_auto_vulkan_for_amd_windows(host, UPSTREAM) is True
gfx = ilp._active_rocm_gfx_target(host)
routed, _repo, _tag, persist = ilp._route_to_vulkan_prebuilt(
host, UPSTREAM, "pin", force_cpu = False
)
assert persist == "auto"
# The route drops the arch, so it must be read before it, not after.
assert ilp._active_rocm_gfx_target(routed) is None
assert gfx == "gfx1034"
checksums = ilp.ApprovedReleaseChecksums(
repo = UPSTREAM,
release_tag = "b9925",
upstream_tag = "b9925",
source_repo = UPSTREAM,
source_repo_url = f"https://github.com/{UPSTREAM}",
)
ilp.write_prebuilt_metadata(
tmp_path,
requested_tag = "latest",
llama_tag = "b9925",
release_tag = "b9925",
choice = _choice("windows-vulkan", "llama-b9925-bin-win-vulkan-x64.zip"),
approved_checksums = checksums,
prebuilt_fallback_used = False,
llama_backend = persist,
rocm_gfx = gfx,
)
marker = json.loads((tmp_path / "UNSLOTH_PREBUILT_INFO.json").read_text())
assert marker["llama_backend"] == "auto"
assert marker["rocm_gfx"] == "gfx1034"
replayed = ilp._apply_host_overrides(unprobed, override_rocm_gfx = marker["rocm_gfx"])
assert ilp._should_auto_vulkan_for_amd_windows(replayed, UPSTREAM) is True
assert ilp._should_auto_vulkan_for_amd_windows(unprobed, UPSTREAM) is False
assert ilp._route_to_vulkan_prebuilt(unprobed, UPSTREAM, "pin", force_cpu = False)[3] is None
def test_a_remembered_arch_never_outranks_a_live_probe(monkeypatch):
"""A remembered arch is a stale probe, not an override; a changed GPU must report its live arch."""
monkeypatch.delenv("UNSLOTH_ROCM_GFX_ARCH", raising = False)
unprobed = dataclasses.replace(
_windows_amd_host(rocm_gfx_target = None, rocm_gfx_targets = []), has_rocm = False
)
monkeypatch.setenv("UNSLOTH_ROCM_GFX_REMEMBERED", "gfx1034")
filled = ilp._apply_host_overrides(unprobed)
assert ilp._active_rocm_gfx_target(filled) == "gfx1034"
assert filled.has_rocm is True
probed = _windows_amd_host(rocm_gfx_target = "gfx1100", rocm_gfx_targets = ["gfx1100"])
assert ilp._active_rocm_gfx_target(ilp._apply_host_overrides(probed)) == "gfx1100"
def _sync_gfx(install_dir, legacy_backend, rocm_gfx):
"""sync_marker_selection with only the routing arch in play."""
ilp.sync_marker_selection(
install_dir,
choice = _choice("windows-vulkan", "win-vulkan.zip"),
backend_request = "auto",
persist_llama_backend = legacy_backend,
rocm_gfx = rocm_gfx,
)
def test_reusing_an_install_still_records_the_routing_gfx(tmp_path):
"""The reuse paths skip write_prebuilt_metadata, so they must sync it too."""
marker_path = tmp_path / "UNSLOTH_PREBUILT_INFO.json"
marker_path.write_text(json.dumps({"llama_backend": "auto", "asset": "win-vulkan.zip"}))
_sync_gfx(tmp_path, "auto", "gfx1034")
assert json.loads(marker_path.read_text())["rocm_gfx"] == "gfx1034"
_sync_gfx(tmp_path, "auto", None)
assert json.loads(marker_path.read_text())["rocm_gfx"] == "gfx1034"
@pytest.mark.parametrize("backend", ["vulkan", None, "hip"])
def test_only_the_automatic_route_records_a_routing_gfx(tmp_path, backend):
"""Every other install names its arch in the asset. A stale copy there would
assert ROCm on a host whose AMD GPU is gone."""
marker_path = tmp_path / "UNSLOTH_PREBUILT_INFO.json"
marker_path.write_text(json.dumps({"llama_backend": backend, "asset": "win-hip.zip"}))
_sync_gfx(tmp_path, backend, "gfx1034")
assert "rocm_gfx" not in json.loads(marker_path.read_text())
checksums = ilp.ApprovedReleaseChecksums(
repo = UPSTREAM,
release_tag = "b9925",
upstream_tag = "b9925",
source_repo = UPSTREAM,
source_repo_url = f"https://github.com/{UPSTREAM}",
)
ilp.write_prebuilt_metadata(
tmp_path,
requested_tag = "latest",
llama_tag = "b9925",
release_tag = "b9925",
choice = _choice("windows-vulkan", "llama-b9925-bin-win-vulkan-x64.zip"),
approved_checksums = checksums,
prebuilt_fallback_used = False,
llama_backend = backend,
rocm_gfx = "gfx1034",
)
assert "rocm_gfx" not in json.loads(marker_path.read_text())
def test_non_amd_installs_record_no_routing_gfx(tmp_path):
"""The key is absent unless an arch actually routed the install."""
checksums = ilp.ApprovedReleaseChecksums(
repo = UPSTREAM,
release_tag = "b9925",
upstream_tag = "b9925",
source_repo = UPSTREAM,
source_repo_url = f"https://github.com/{UPSTREAM}",
)
ilp.write_prebuilt_metadata(
tmp_path,
requested_tag = "latest",
llama_tag = "b9925",
release_tag = "b9925",
choice = _choice("windows-vulkan", "llama-b9925-bin-win-vulkan-x64.zip"),
approved_checksums = checksums,
prebuilt_fallback_used = False,
llama_backend = None,
)
marker = json.loads((tmp_path / "UNSLOTH_PREBUILT_INFO.json").read_text())
assert "rocm_gfx" not in marker
# Setup maps BACKEND=cpu to --force-cpu to avoid Intel iGPU Vulkan crashes; nothing outranks it.
_SIM_PLATFORMS = {
# WSL presents as Linux to this resolver, so it rides the Linux row.
"Linux": dict(
system = "Linux",
is_windows = False,
is_linux = True,
is_macos = False,
machine = "x86_64",
is_x86_64 = True,
is_arm64 = False,
),
"Windows": dict(
system = "Windows",
is_windows = True,
is_linux = False,
is_macos = False,
machine = "amd64",
is_x86_64 = True,
is_arm64 = False,
),
"macOS": dict(
system = "Darwin",
is_windows = False,
is_linux = False,
is_macos = True,
machine = "arm64",
is_x86_64 = False,
is_arm64 = True,
),
}
_SIM_GPUS = {
"nvidia": dict(
has_physical_nvidia = True,
has_usable_nvidia = True,
has_rocm = False,
has_intel_gpu = False,
nvidia_smi = "/usr/bin/nvidia-smi",
driver_cuda_version = "12.4",
compute_caps = ["8.9"],
),
"amd": dict(
has_physical_nvidia = False,
has_usable_nvidia = False,
has_rocm = True,
has_intel_gpu = False,
nvidia_smi = None,
driver_cuda_version = None,
compute_caps = [],
rocm_gfx_target = "gfx803",
rocm_gfx_targets = ["gfx803"],
),
"intel": dict(
has_physical_nvidia = False,
has_usable_nvidia = False,
has_rocm = False,
has_intel_gpu = True,
nvidia_smi = None,
driver_cuda_version = None,
compute_caps = [],
),
"cpu_only": dict(
has_physical_nvidia = False,
has_usable_nvidia = False,
has_rocm = False,
has_intel_gpu = False,
nvidia_smi = None,
driver_cuda_version = None,
compute_caps = [],
),
}
def _sim_host(platform_name, gpu_name):
base = dict(visible_cuda_devices = None)
base.update(_SIM_PLATFORMS[platform_name])
base.update(_SIM_GPUS[gpu_name])
return ilp.HostInfo(**base)
@pytest.mark.parametrize("platform_name", sorted(_SIM_PLATFORMS))
@pytest.mark.parametrize("gpu_name", sorted(_SIM_GPUS))
@pytest.mark.parametrize("backend_env", [None, "auto", "vulkan", "cpu"])
def test_forced_cpu_outranks_every_vulkan_trigger(
monkeypatch, platform_name, gpu_name, backend_env
):
"""A deliberate CPU install stays CPU on every host, whatever asks for Vulkan."""
monkeypatch.delenv("UNSLOTH_FORCE_VULKAN", raising = False)
if backend_env is None:
monkeypatch.delenv("UNSLOTH_LLAMA_CPP_BACKEND", raising = False)
else:
monkeypatch.setenv("UNSLOTH_LLAMA_CPP_BACKEND", backend_env)
monkeypatch.setenv("UNSLOTH_FORCE_VULKAN", "1")
repo, tag = "unslothai/llama.cpp-prebuilt", "latest"
_, out_repo, _, persist = ilp._route_to_vulkan_prebuilt(
_sim_host(platform_name, gpu_name),
repo,
tag,
force_cpu = True,
llama_backend = "vulkan",
)
assert out_repo == repo, (platform_name, gpu_name, backend_env)
assert persist is None, (platform_name, gpu_name, backend_env)
def test_the_forced_cpu_guard_is_not_vacuous():
"""Without the pin this host takes Vulkan, so a resolver that stopped routing would fail the check."""
repo, tag = "unslothai/llama.cpp-prebuilt", "latest"
_, out_repo, _, persist = ilp._route_to_vulkan_prebuilt(
_sim_host("Linux", "amd"),
repo,
tag,
force_cpu = False,
llama_backend = "vulkan",
)
assert out_repo != repo or persist == "vulkan"
def test_the_icd_search_path_is_built_per_call_not_at_import(monkeypatch):
"""Path.home() can raise with no home dir, so the ICD search path is built per call, not at import."""
def _no_home():
raise RuntimeError("Could not determine home directory.")
monkeypatch.setattr(ilp.Path, "home", staticmethod(_no_home))
dirs = ilp._vulkan_icd_search_dirs()
assert dirs, "the system search directories must survive an unresolvable home"
assert all("icd.d" in str(entry) for entry in dirs)
monkeypatch.setattr(ilp.sys, "platform", "linux")
assert _REAL_AMD_VULKAN_ICD_PRESENT() in (True, False)
class _FakeKeyHandle:
def __init__(self, payload):
self.payload = payload
def __enter__(self):
return self.payload
def __exit__(self, *exc):
return False
class _FakeDeviceClass(dict):
"""A device class key: instance-name -> {value name: (data, kind)}."""
class _FakeIcdWinreg:
HKEY_LOCAL_MACHINE = object()
REG_DWORD = 4
REG_SZ = 1
REG_MULTI_SZ = 7
def __init__(
self,
by_key,
devices = None,
):
self._by_key = by_key
self._devices = devices or {}
def OpenKey(self, parent, name):
if isinstance(parent, _FakeDeviceClass):
instance = parent.get(name)
if instance is None:
raise FileNotFoundError(name)
if instance == "denied":
raise PermissionError(name)
return _FakeKeyHandle(instance)
if name in self._devices:
return _FakeKeyHandle(self._devices[name])
if name in self._by_key:
return _FakeKeyHandle(self._by_key[name])
raise FileNotFoundError(name)
def QueryInfoKey(self, payload):
if isinstance(payload, _FakeDeviceClass):
return (len(payload), 0, 0)
return (0, len(payload), 0)
def EnumKey(self, payload, index):
return list(payload)[index]
def EnumValue(self, entries, index):
return entries[index]
def QueryValueEx(self, instance, value_name):
if value_name not in instance:
raise FileNotFoundError(value_name)
return instance[value_name]
def _icd_paths(
monkeypatch,
by_key,
devices = None,
present = None,
):
"""Every instance counts as present unless a per-class callback says otherwise."""
monkeypatch.setattr(ilp.sys, "platform", "win32")
for name in ("VK_DRIVER_FILES", "VK_ICD_FILENAMES"):
monkeypatch.delenv(name, raising = False)
monkeypatch.setitem(sys.modules, "winreg", _FakeIcdWinreg(by_key, devices))
if present is None:
def present(class_key_path):
return set((devices or {}).get(class_key_path) or ())
monkeypatch.setattr(ilp, "_windows_present_class_instances", present)
return ilp._amd_vulkan_icd_manifest_paths()
_DISPLAY_CLASS_KEY = ilp._WINDOWS_DISPLAY_CLASS_KEY
_SOFTWARE_COMPONENT_CLASS_KEY = ilp._WINDOWS_VULKAN_DEVICE_CLASS_KEYS[1]
def _display_class(
*,
instance = "0000",
value,
kind = _FakeIcdWinreg.REG_SZ,
key = None,
):
"""One device class key holding a single VulkanDriverName registration."""
return {
key or _DISPLAY_CLASS_KEY: _FakeDeviceClass(
{instance: {ilp._WINDOWS_VULKAN_DRIVER_VALUE: (value, kind)}}
)
}
def test_the_windows_registry_is_not_read_when_the_loader_is_forced(
monkeypatch, _present_manifest, tmp_path
):
# VK_DRIVER_FILES overrides discovery on Windows too.
monkeypatch.setattr(ilp.sys, "platform", "win32")
registry = {_DRIVERS_KEY: [(_present_manifest, 0, _FakeIcdWinreg.REG_DWORD)]}
monkeypatch.setitem(sys.modules, "winreg", _FakeIcdWinreg(registry))
for name in ("VK_DRIVER_FILES", "VK_ICD_FILENAMES"):
monkeypatch.delenv(name, raising = False)
assert ilp._amd_vulkan_icd_manifest_paths() == [_present_manifest]
intel = _icd(tmp_path / "intel_icd.json")
monkeypatch.setenv("VK_DRIVER_FILES", intel)
assert ilp._amd_vulkan_icd_manifest_paths() == [intel]
assert _REAL_AMD_VULKAN_ICD_PRESENT() is False
monkeypatch.setenv("VK_ICD_FILENAMES", _present_manifest)
assert ilp._amd_vulkan_icd_manifest_paths() == [intel]
monkeypatch.delenv("VK_DRIVER_FILES")
assert ilp._amd_vulkan_icd_manifest_paths() == [_present_manifest]
_DRIVERS_KEY = r"SOFTWARE\Khronos\Vulkan\Drivers"
@pytest.fixture
def _present_manifest(tmp_path):
"""A registered manifest that is really on disk, since the probe now checks."""
return _icd(tmp_path / "amd-vulkan64.json")
def test_the_windows_radeon_manifest_is_recognized(monkeypatch):
# Adrenalin registers amd-vulkan64.json, not amdvlk64.json.
monkeypatch.setattr(
ilp,
"_amd_vulkan_icd_manifest_paths",
lambda: [r"C:\Windows\System32\amd-vulkan64.json"],
)
monkeypatch.setattr(ilp, "_amd_vulkan_icd_usable", lambda _path: True)
assert _REAL_AMD_VULKAN_ICD_PRESENT() is True
def test_a_disabled_registry_registration_does_not_answer_for_the_driver(
monkeypatch, _present_manifest
):
# Value name = manifest path, DWORD = enable flag; only zero loads (LoaderDriverInterface.md).
disabled = _icd_paths(
monkeypatch,
{_DRIVERS_KEY: [(_present_manifest, 1, _FakeIcdWinreg.REG_DWORD)]},
)
assert disabled == []
enabled = _icd_paths(
monkeypatch,
{_DRIVERS_KEY: [(_present_manifest, 0, _FakeIcdWinreg.REG_DWORD)]},
)
assert enabled == [_present_manifest]
def test_an_enabled_registration_whose_manifest_is_gone_does_not_answer(monkeypatch, tmp_path):
missing = str(tmp_path / "gone" / "amd-vulkan64.json")
assert _icd_paths(monkeypatch, {_DRIVERS_KEY: [(missing, 0, _FakeIcdWinreg.REG_DWORD)]}) == []
def test_a_32_bit_registration_does_not_answer_for_the_x64_bundle(monkeypatch, tmp_path):
wow = str(tmp_path / "amd-vulkan32.json")
_icd(tmp_path / "amd-vulkan32.json")
paths = _icd_paths(
monkeypatch,
{r"SOFTWARE\WOW6432Node\Khronos\Vulkan\Drivers": [(wow, 0, _FakeIcdWinreg.REG_DWORD)]},
)
assert paths == []
def test_a_non_dword_icd_registration_is_ignored(monkeypatch, _present_manifest):
paths = _icd_paths(
monkeypatch,
{_DRIVERS_KEY: [(_present_manifest, "0", _FakeIcdWinreg.REG_SZ)]},
)
assert paths == []
@pytest.mark.parametrize(
"class_key", [_DISPLAY_CLASS_KEY, _SOFTWARE_COMPONENT_CLASS_KEY], ids = ["adapter", "component"]
)
@pytest.mark.parametrize(
"kind, count",
[(_FakeIcdWinreg.REG_SZ, 1), (_FakeIcdWinreg.REG_MULTI_SZ, 2)],
ids = ["single", "multiple"],
)
def test_device_registrations_find_the_amd_driver(monkeypatch, tmp_path, class_key, kind, count):
paths = [_icd(tmp_path / str(i) / "amd-vulkan64.json") for i in range(count)]
value = paths[0] if kind == _FakeIcdWinreg.REG_SZ else paths
devices = _display_class(value = value, kind = kind, key = class_key)
assert _icd_paths(monkeypatch, {}, devices) == paths
assert _REAL_AMD_VULKAN_ICD_PRESENT() is True
def test_an_unexpected_value_type_is_ignored(monkeypatch, _present_manifest):
binary = _display_class(value = _present_manifest, kind = 3)
assert _icd_paths(monkeypatch, {}, binary) == []
def test_a_stale_device_registration_is_not_evidence(monkeypatch, tmp_path):
missing = str(tmp_path / "B419548" / "amd-vulkan64.json")
stale = _display_class(value = missing, key = _SOFTWARE_COMPONENT_CLASS_KEY)
assert _icd_paths(monkeypatch, {}, stale) == []
good = _icd(tmp_path / "store" / "amd-vulkan64.json")
both = {**stale, **_display_class(value = good)}
assert _icd_paths(monkeypatch, {}, both) == [good]
def test_a_manifest_registered_twice_is_listed_once(monkeypatch, _present_manifest):
devices = {
**_display_class(value = _present_manifest),
**_display_class(value = _present_manifest, key = _SOFTWARE_COMPONENT_CLASS_KEY),
}
legacy = {_DRIVERS_KEY: [(_present_manifest, 0, _FakeIcdWinreg.REG_DWORD)]}
assert _icd_paths(monkeypatch, legacy, devices) == [_present_manifest]
def test_a_relative_device_registration_is_not_guessed_at(monkeypatch):
assert _icd_paths(monkeypatch, {}, _display_class(value = "amd-vulkan64.json")) == []
@pytest.mark.parametrize(
"name, entry",
[
("Properties", "denied"),
("0000", {"DriverDesc": ("Some other adapter", 1)}),
# Digit-named so it reaches OpenKey; 'Properties' is filtered by isdigit first.
("0002", "denied"),
],
ids = ["restricted-subkey", "unregistered-adapter", "denied-instance"],
)
def test_other_registry_entries_do_not_hide_a_manifest(monkeypatch, _present_manifest, name, entry):
devices = {
_DISPLAY_CLASS_KEY: _FakeDeviceClass(
{name: entry, "0001": {ilp._WINDOWS_VULKAN_DRIVER_VALUE: (_present_manifest, 1)}}
)
}
assert _icd_paths(monkeypatch, {}, devices) == [_present_manifest]
def test_the_device_scan_is_not_read_when_the_loader_is_forced(
monkeypatch, _present_manifest, tmp_path
):
assert _icd_paths(monkeypatch, {}, _display_class(value = _present_manifest)) == [
_present_manifest
]
monkeypatch.setenv("VK_DRIVER_FILES", _icd(tmp_path / "intel_icd.json"))
assert ilp._amd_vulkan_icd_manifest_paths() == [str(tmp_path / "intel_icd.json")]
def test_a_32_bit_device_registration_does_not_answer_for_the_x64_bundle(
monkeypatch, _present_manifest
):
devices = {
_DISPLAY_CLASS_KEY: _FakeDeviceClass(
{"0000": {"VulkanDriverNameWow": (_present_manifest, 1)}}
)
}
assert _icd_paths(monkeypatch, {}, devices) == []
def test_the_legacy_key_still_answers_on_its_own(monkeypatch, _present_manifest):
assert _icd_paths(
monkeypatch, {_DRIVERS_KEY: [(_present_manifest, 0, _FakeIcdWinreg.REG_DWORD)]}, {}
) == [_present_manifest]
def test_a_removed_device_registration_is_not_evidence(monkeypatch, tmp_path):
left_behind = _icd(tmp_path / "removed" / "amd-vulkan64.json")
devices = {
_DISPLAY_CLASS_KEY: _FakeDeviceClass(
{
"0000": {ilp._WINDOWS_VULKAN_DRIVER_VALUE: (left_behind, 1)},
"0001": {"DriverDesc": ("The adapter that is actually here", 1)},
}
)
}
assert os.path.isfile(left_behind)
assert _icd_paths(monkeypatch, {}, devices, present = lambda _key: {"0001"}) == []
assert _icd_paths(monkeypatch, {}, devices, present = lambda _key: {"0000"}) == [left_behind]
def test_a_removed_device_does_not_hide_a_present_one(monkeypatch, tmp_path):
removed = _icd(tmp_path / "removed" / "amd-vulkan64.json")
live = _icd(tmp_path / "live" / "amd-vulkan64.json")
devices = {
_DISPLAY_CLASS_KEY: _FakeDeviceClass(
{
"0000": {ilp._WINDOWS_VULKAN_DRIVER_VALUE: (removed, 1)},
"0001": {ilp._WINDOWS_VULKAN_DRIVER_VALUE: (live, 1)},
}
)
}
assert _icd_paths(monkeypatch, {}, devices, present = lambda _key: {"0001"}) == [live]
def test_presence_is_resolved_per_class_key(monkeypatch, tmp_path):
adapter = _icd(tmp_path / "adapter" / "amd-vulkan64.json")
component = _icd(tmp_path / "component" / "amd-vulkan64.json")
devices = {
**_display_class(value = adapter),
**_display_class(value = component, key = _SOFTWARE_COMPONENT_CLASS_KEY),
}
only_adapter = _icd_paths(
monkeypatch,
{},
devices,
present = lambda key: {"0000"} if key == _DISPLAY_CLASS_KEY else set(),
)
assert only_adapter == [adapter]
def test_an_unknowable_presence_answer_keeps_the_legacy_key_alone(
monkeypatch, tmp_path, _present_manifest
):
device_only = _display_class(value = _icd(tmp_path / "device" / "amd-vulkan64.json"))
assert _icd_paths(monkeypatch, {}, device_only, present = lambda _key: None) == []
legacy = {_DRIVERS_KEY: [(_present_manifest, 0, _FakeIcdWinreg.REG_DWORD)]}
assert _icd_paths(monkeypatch, legacy, device_only, present = lambda _key: None) == [
_present_manifest
]
def test_no_present_device_of_that_class_is_an_answer(monkeypatch, tmp_path):
device_only = _display_class(value = _icd(tmp_path / "device" / "amd-vulkan64.json"))
assert _icd_paths(monkeypatch, {}, device_only, present = lambda _key: set()) == []
class _FakeCfgMgr:
"""Fake CfgMgr32 that writes ctypes output parameters on any platform."""
def __init__(
self,
by_class,
fail_at = None,
driver_of = None,
# device id -> (CM_Get_DevNode_Status, problem number). Absent means healthy.
status_of = None,
# Ids that arrive after the list is sized, outgrowing the caller's buffer.
arriving = None,
):
# class guid -> device ids; device id -> CM_DRP_DRIVER value.
self._by_class = by_class
self._fail_at = fail_at
self._driver_of = driver_of or {}
self._status_of = status_of or {}
self._arriving = list(arriving or [])
self.calls = []
def _result(self, call):
return _CR_FAILURE if self._fail_at == call else ilp._CR_SUCCESS
def CM_Get_Device_ID_List_SizeW(self, size_ptr, guid, flags):
self.calls.append(("size", guid, flags))
ids = list(self._by_class.get(guid, []))
# Include each ID's terminator and the final NUL.
size_ptr[0] = sum(len(entry) + 1 for entry in ids) + 1
return self._result("size")
def CM_Get_Device_ID_ListW(self, guid, buffer, length, flags):
self.calls.append(("list", guid, flags))
if self._arriving:
self._by_class[guid] = list(self._by_class.get(guid, [])) + self._arriving
self._arriving = []
ids = list(self._by_class.get(guid, []))
block = "".join(entry + "\0" for entry in ids) + "\0"
if len(block) > length:
# The real API refuses an undersized buffer rather than truncating.
return ilp._CR_BUFFER_SMALL
for index, char in enumerate(block):
buffer[index] = char
return self._result("list")
def CM_Get_DevNode_Status(self, status_ptr, problem_ptr, devinst, flags):
device_id = list(self._driver_of)[int(devinst.value) - 1]
self.calls.append(("status", device_id))
if self._fail_at == "status":
return _CR_FAILURE
status, problem = self._status_of.get(device_id, (0, 0))
status_ptr[0] = status
problem_ptr[0] = problem
return ilp._CR_SUCCESS
def CM_Locate_DevNodeW(self, devinst_ptr, device_id, flags):
self.calls.append(("locate", device_id))
if device_id not in self._driver_of:
return _CR_FAILURE
devinst_ptr[0] = list(self._driver_of).index(device_id) + 1
return self._result("locate")
def CM_Get_DevNode_Registry_PropertyW(self, devinst, prop, kind, buffer, size_ptr, flags):
device_id = list(self._driver_of)[int(devinst.value) - 1]
driver = self._driver_of[device_id]
if buffer is None:
size_ptr[0] = (len(driver) + 1) * ctypes.sizeof(ctypes.c_wchar) if driver else 0
return ilp._CR_SUCCESS
for index, char in enumerate(driver):
buffer[index] = char
return self._result("property")
_CR_FAILURE = 0x00000013 # cfg.h CR_FAILURE; any non-zero is a failed CM_ call.
_DISPLAY_GUID = "{4d36e968-e325-11ce-bfc1-08002be10318}"
def _with_cfgmgr(monkeypatch, fake):
# WinDLL is absent on Linux.
monkeypatch.setattr(ctypes, "WinDLL", lambda _name: fake, raising = False)
def test_the_presence_probe_reads_the_present_device_list(monkeypatch):
fake = _FakeCfgMgr(
{_DISPLAY_GUID: ["PCI\\VEN_1002&DEV_1586\\0", "PCI\\VEN_1002&DEV_7448\\1"]},
driver_of = {
"PCI\\VEN_1002&DEV_1586\\0": _DISPLAY_GUID + "\\0000",
"PCI\\VEN_1002&DEV_7448\\1": _DISPLAY_GUID + "\\0003",
},
)
_with_cfgmgr(monkeypatch, fake)
assert ilp._windows_present_class_instances(ilp._WINDOWS_DISPLAY_CLASS_KEY) == {
"0000",
"0003",
}
assert fake.calls[0] == (
"size",
_DISPLAY_GUID,
ilp._CM_GETIDLIST_FILTER_CLASS | ilp._CM_GETIDLIST_FILTER_PRESENT,
)
def test_a_present_device_with_no_driver_bound_names_no_instance(monkeypatch):
fake = _FakeCfgMgr(
{_DISPLAY_GUID: ["PCI\\VEN_1002&DEV_1586\\0", "SWD\\NO_DRIVER\\1"]},
driver_of = {
"PCI\\VEN_1002&DEV_1586\\0": _DISPLAY_GUID + "\\0000",
"SWD\\NO_DRIVER\\1": "",
},
)
_with_cfgmgr(monkeypatch, fake)
assert ilp._windows_present_class_instances(ilp._WINDOWS_DISPLAY_CLASS_KEY) == {"0000"}
def test_no_present_device_of_the_class_is_an_empty_answer(monkeypatch):
_with_cfgmgr(monkeypatch, _FakeCfgMgr({_DISPLAY_GUID: []}))
assert ilp._windows_present_class_instances(ilp._WINDOWS_DISPLAY_CLASS_KEY) == set()
@pytest.mark.parametrize("failing_call", ["size", "list", "locate"])
def test_a_failed_cm_call_answers_none(monkeypatch, failing_call):
fake = _FakeCfgMgr(
{_DISPLAY_GUID: ["PCI\\VEN_1002&DEV_1586\\0"]},
fail_at = failing_call,
driver_of = {"PCI\\VEN_1002&DEV_1586\\0": _DISPLAY_GUID + "\\0000"},
)
_with_cfgmgr(monkeypatch, fake)
answer = ilp._windows_present_class_instances(ilp._WINDOWS_DISPLAY_CLASS_KEY)
assert answer is None if failing_call in ("size", "list") else answer == set()
def test_the_presence_probe_answers_none_when_cfgmgr_cannot_be_loaded(monkeypatch):
def _no_library(_name):
raise OSError("cfgmgr32.dll not found")
monkeypatch.setattr(ctypes, "WinDLL", _no_library, raising = False)
assert ilp._windows_present_class_instances(ilp._WINDOWS_DISPLAY_CLASS_KEY) is None
def test_the_presence_probe_answers_none_when_the_walk_raises(monkeypatch):
class _Exploding:
def __getattr__(self, _name):
raise RuntimeError("boom")
_with_cfgmgr(monkeypatch, _Exploding())
assert ilp._windows_present_class_instances(ilp._WINDOWS_DISPLAY_CLASS_KEY) is None
class _Utf16Buffer:
"""Emulates create_unicode_buffer over 2-byte code units, with ctypes indexing and .value semantics."""
def __init__(self, length):
self._units = [0] * length
def __len__(self):
return len(self._units)
def __setitem__(self, index, char):
self._units[index] = ord(char)
def __getitem__(self, index):
if isinstance(index, slice):
return "".join(chr(unit) for unit in self._units[index])
return chr(self._units[index])
@property
def value(self):
out = []
for unit in self._units:
if unit == 0:
break
out.append(chr(unit))
return "".join(out)
class _Windows32Bit:
"""DWORD, ULONG and DEVINST are all a 32-bit unsigned, pointers are not."""
def __init__(self, value = 0):
self.value = int(value) & 0xFFFFFFFF
class _WindowsPointer:
def __init__(self, box):
self.box = box
def __getitem__(self, _index):
return self.box.value
def __setitem__(self, _index, value):
self.box.value = value
class _WindowsWcharT:
"""Only ever handed to sizeof, which answers 2 on Windows and 4 here."""
class _WindowsCfgMgr(_FakeCfgMgr):
"""CM_Get_DevNode_Registry_PropertyW sizes in bytes; under_allocated is asserted outside the probe."""
under_allocated = False
def CM_Get_DevNode_Registry_PropertyW(self, devinst, prop, kind, buffer, size_ptr, flags):
device_id = list(self._driver_of)[int(devinst.value) - 1]
driver = self._driver_of[device_id]
wanted = (len(driver) + 1) * 2 if driver else 0
if buffer is None:
size_ptr[0] = wanted
return ilp._CR_SUCCESS
if len(buffer) * 2 < wanted:
self.under_allocated = True
return _CR_FAILURE
for index, char in enumerate(driver):
buffer[index] = char
buffer[len(driver)] = "\0"
return self._result("property")
@contextlib.contextmanager
def _windows_widths(cfgmgr):
"""Runs the probe at Windows widths, since native ctypes hides the byte-to-element conversion."""
shim = ModuleType("ctypes")
wintypes = ModuleType("ctypes.wintypes")
wintypes.ULONG = _Windows32Bit
wintypes.DWORD = _Windows32Bit
shim.wintypes = wintypes
shim.c_wchar = _WindowsWcharT
shim.sizeof = lambda obj: 2 if obj is _WindowsWcharT else ctypes.sizeof(obj)
shim.pointer = _WindowsPointer
shim.create_unicode_buffer = _Utf16Buffer
shim.WinDLL = lambda _name: cfgmgr
saved = sys.modules["ctypes"]
sys.modules["ctypes"] = shim
try:
yield
finally:
sys.modules["ctypes"] = saved
def test_the_driver_property_survives_a_two_byte_wchar():
# 43 chars = 88 bytes on Windows; dividing by a 4-byte wchar_t undersizes the buffer.
driver = _DISPLAY_GUID + "\\0000"
fake = _WindowsCfgMgr(
{_DISPLAY_GUID: ["PCI\\VEN_1002&DEV_1586\\0"]},
driver_of = {"PCI\\VEN_1002&DEV_1586\\0": driver},
)
with _windows_widths(fake):
answer = ilp._windows_present_class_instances(ilp._WINDOWS_DISPLAY_CLASS_KEY)
assert fake.under_allocated is False, "the byte length was read as an element count"
assert answer == {"0000"}
def test_the_device_id_list_is_sized_in_characters_not_bytes():
# BufferLen is a CHARACTER count, so the size query passes through unscaled.
fake = _WindowsCfgMgr(
{_DISPLAY_GUID: ["PCI\\VEN_1002&DEV_1586\\0", "PCI\\VEN_1002&DEV_7448\\1"]},
driver_of = {
"PCI\\VEN_1002&DEV_1586\\0": _DISPLAY_GUID + "\\0000",
"PCI\\VEN_1002&DEV_7448\\1": _DISPLAY_GUID + "\\0003",
},
)
with _windows_widths(fake):
answer = ilp._windows_present_class_instances(ilp._WINDOWS_DISPLAY_CLASS_KEY)
assert answer == {"0000", "0003"}
def test_the_forced_loader_list_reads_neither_the_registry_nor_cfgmgr():
class _CountingWinreg(_FakeIcdWinreg):
def __init__(self):
super().__init__({}, None)
self.reads = 0
def OpenKey(self, parent, name):
self.reads += 1
return super().OpenKey(parent, name)
winreg = _CountingWinreg()
fake = _FakeCfgMgr({_DISPLAY_GUID: ["PCI\\VEN_1002&DEV_1586\\0"]})
with pytest.MonkeyPatch.context() as patch:
patch.setattr(ilp.sys, "platform", "win32")
patch.setitem(sys.modules, "winreg", winreg)
patch.setattr(ctypes, "WinDLL", lambda _name: fake, raising = False)
patch.setenv("VK_DRIVER_FILES", __file__)
assert ilp._amd_vulkan_icd_manifest_paths() == [__file__]
assert winreg.reads == 0
assert fake.calls == []
class _WindowsPaths:
"""os as the probe sees it on Windows, over a fixed set of existing files."""
def __init__(self, existing):
self.existing = set(existing)
self.pathsep = ";"
self.environ = os.environ
self.path = SimpleNamespace(
isabs = ntpath.isabs,
isfile = lambda name: name in self.existing,
dirname = ntpath.dirname,
join = ntpath.join,
)
@pytest.mark.parametrize(
"value, taken",
[
(r"C:\Windows\System32\DriverStore\amd-vulkan64.json", True),
(r"\\?\C:\Windows\System32\amd-vulkan64.json", True),
(r"\\host\share\amd-vulkan64.json", True),
("amd-vulkan64.json", False),
(r"..\amd-vulkan64.json", False),
(r"FileRepository\amd-vulkan64.json", False),
(" ", False),
],
)
def test_a_registration_is_taken_only_when_windows_calls_it_absolute(monkeypatch, value, taken):
# tmp_path is POSIX, so other tests run isabs as posixpath; this uses real Windows paths.
monkeypatch.setattr(ilp, "os", _WindowsPaths({value.strip()}))
got = ilp._windows_vulkan_driver_value_paths(_FakeIcdWinreg, value, _FakeIcdWinreg.REG_SZ)
assert got == ([value.strip()] if taken else [])
def test_a_multi_sz_registration_keeps_only_the_absolute_entries_that_exist(monkeypatch):
here = r"C:\Windows\System32\amd-vulkan64.json"
gone = r"C:\Windows\System32\amdvlk64.json"
monkeypatch.setattr(ilp, "os", _WindowsPaths({here}))
got = ilp._windows_vulkan_driver_value_paths(
_FakeIcdWinreg,
[here, gone, r"FileRepository\amd-vulkan64.json", "", None],
_FakeIcdWinreg.REG_MULTI_SZ,
)
assert got == [here]
_DN_HAS_PROBLEM = 0x00000400 # cfg.h status flags
_DN_NEED_RESTART = 0x00000100 # DN_LIAR, a STATUS bit and not a CM_PROB_ code
_CM_PROB_NEED_RESTART = 0x0000000E # cfg.h problem codes, which stop at 0x39
_CM_PROB_DISABLED = 0x00000016
@pytest.mark.parametrize(
"status, problem",
[
(_DN_HAS_PROBLEM, _CM_PROB_NEED_RESTART),
# DN_NEED_RESTART is a status bit, not a problem code; pulProblemNumber never matches it.
(_DN_NEED_RESTART, 0),
(_DN_NEED_RESTART | _DN_HAS_PROBLEM, _CM_PROB_DISABLED),
],
ids = ["problem-code", "status-bit-alone", "status-bit-with-other-problem"],
)
def test_a_device_pending_reboot_is_not_evidence_of_a_loadable_driver(monkeypatch, status, problem):
device = "PCI\\VEN_1002&DEV_1586\\0"
fake = _FakeCfgMgr(
{_DISPLAY_GUID: [device]},
driver_of = {device: _DISPLAY_GUID + "\\0000"},
status_of = {device: (status, problem)},
)
_with_cfgmgr(monkeypatch, fake)
assert ilp._windows_present_class_instances(ilp._WINDOWS_DISPLAY_CLASS_KEY) == set()
def test_an_unrelated_device_problem_does_not_hide_a_working_adapter(monkeypatch):
working = "PCI\\VEN_1002&DEV_1586\\0"
disabled = "PCI\\VEN_1002&DEV_7448\\1"
fake = _FakeCfgMgr(
{_DISPLAY_GUID: [working, disabled]},
driver_of = {
working: _DISPLAY_GUID + "\\0000",
disabled: _DISPLAY_GUID + "\\0003",
},
status_of = {disabled: (_DN_HAS_PROBLEM, _CM_PROB_DISABLED)},
)
_with_cfgmgr(monkeypatch, fake)
assert ilp._windows_present_class_instances(ilp._WINDOWS_DISPLAY_CLASS_KEY) == {
"0000",
"0003",
}
def test_a_device_whose_status_cannot_be_read_is_skipped(monkeypatch):
device = "PCI\\VEN_1002&DEV_1586\\0"
fake = _FakeCfgMgr(
{_DISPLAY_GUID: [device]},
fail_at = "status",
driver_of = {device: _DISPLAY_GUID + "\\0000"},
)
_with_cfgmgr(monkeypatch, fake)
assert ilp._windows_present_class_instances(ilp._WINDOWS_DISPLAY_CLASS_KEY) == set()
def test_a_device_arriving_mid_enumeration_is_read_on_the_retry(monkeypatch):
# A device added between size and read gives CR_BUFFER_SMALL; must retry.
settled = "PCI\\VEN_1002&DEV_1586\\0"
late = "PCI\\VEN_1002&DEV_7448\\1"
fake = _FakeCfgMgr(
{_DISPLAY_GUID: [settled]},
driver_of = {settled: _DISPLAY_GUID + "\\0000", late: _DISPLAY_GUID + "\\0003"},
arriving = [late],
)
_with_cfgmgr(monkeypatch, fake)
assert ilp._windows_present_class_instances(ilp._WINDOWS_DISPLAY_CLASS_KEY) == {
"0000",
"0003",
}
assert [call[0] for call in fake.calls].count("list") == 2, "the read was retried"
def test_a_list_that_never_settles_answers_unknown_rather_than_spinning(monkeypatch):
class _AlwaysGrowing(_FakeCfgMgr):
def CM_Get_Device_ID_ListW(self, guid, buffer, length, flags):
self.calls.append(("list", guid, flags))
return ilp._CR_BUFFER_SMALL
fake = _AlwaysGrowing({_DISPLAY_GUID: ["PCI\\VEN_1002&DEV_1586\\0"]})
_with_cfgmgr(monkeypatch, fake)
assert ilp._windows_present_class_instances(ilp._WINDOWS_DISPLAY_CLASS_KEY) is None
assert [call[0] for call in fake.calls].count("list") == ilp._CM_DEVICE_LIST_ATTEMPTS
def test_one_unreadable_instance_does_not_cost_the_rest_of_the_class(monkeypatch, tmp_path):
# Per instance, not per class: the integrated part often enumerates first.
manifest = _icd(tmp_path / "store" / "amd-vulkan64.json")
class _HalfBrokenWinreg(_FakeIcdWinreg):
def QueryValueEx(self, instance, value_name):
if instance.get("explode"):
raise RuntimeError("an unexpected registry payload")
return _FakeIcdWinreg.QueryValueEx(self, instance, value_name)
devices = {
_DISPLAY_CLASS_KEY: _FakeDeviceClass(
{
"0000": {"explode": True, ilp._WINDOWS_VULKAN_DRIVER_VALUE: (manifest, 1)},
"0001": {ilp._WINDOWS_VULKAN_DRIVER_VALUE: (manifest, 1)},
}
)
}
monkeypatch.setattr(ilp, "_windows_present_class_instances", lambda _key: {"0000", "0001"})
assert _HalfBrokenWinreg({}, devices) is not None
winreg = _HalfBrokenWinreg({}, devices)
assert ilp._windows_device_icd_manifest_paths(winreg) == [manifest]
# Linux: ROCm userspace installed, but no AMD GPU here has ROCm kernels (RDNA 1, Polaris).
@pytest.fixture
def linux_no_rocm_env(amd_vulkan_icd, monkeypatch):
for var in ("GGML_VK_VISIBLE_DEVICES", "HSA_OVERRIDE_GFX_VERSION"):
monkeypatch.delenv(var, raising = False)
def _linux_amd(*targets, **overrides):
return _amd_host(
windows = False, rocm_gfx_target = targets[0], rocm_gfx_targets = list(targets), **overrides
)
@pytest.mark.parametrize(
"targets",
[("gfx1010",), ("gfx1011",), ("gfx1012",), ("gfx803",), ("gfx90c",), ("gfx1010", "gfx803")],
)
def test_linux_amd_without_rocm_kernels_takes_the_vulkan_route(targets, linux_no_rocm_env):
host = _linux_amd(*targets)
assert ilp._should_auto_vulkan_for_amd_linux(host) is True
routed, repo, tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert routed.has_rocm is False
assert routed.has_intel_gpu is True
assert (repo, tag) == (FORK, "pin")
assert persist == "auto"
@pytest.mark.parametrize(
"targets",
[
("gfx1010", "gfx1100"),
("gfx90c", "gfx1100"),
("gfx1100",),
("gfx1030",),
("gfx906",),
("gfx1151",),
],
)
def test_linux_any_rocm_capable_card_keeps_todays_path(targets, linux_no_rocm_env):
# Vulkan honours no HIP mask, so one ROCm-capable card keeps the whole host on ROCm.
assert ilp._should_auto_vulkan_for_amd_linux(_linux_amd(*targets)) is False
@pytest.mark.parametrize(
"env",
[
"HIP_VISIBLE_DEVICES",
"ROCR_VISIBLE_DEVICES",
"CUDA_VISIBLE_DEVICES",
"GGML_VK_VISIBLE_DEVICES",
"HSA_OVERRIDE_GFX_VERSION",
],
)
def test_linux_no_rocm_route_declines_under_a_mask_or_override(env, linux_no_rocm_env, monkeypatch):
monkeypatch.setenv(env, "10.3.0" if env == "HSA_OVERRIDE_GFX_VERSION" else "0")
host = _linux_amd("gfx1010")
assert ilp._should_auto_vulkan_for_amd_linux(host) is False
routed, _repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert routed is host
assert persist is None
def test_linux_no_rocm_route_declines_beside_a_physical_nvidia_card(linux_no_rocm_env):
assert (
ilp._should_auto_vulkan_for_amd_linux(_linux_amd("gfx1010", has_physical_nvidia = True))
is False
)
def test_linux_no_rocm_route_needs_an_amd_vulkan_driver(linux_no_rocm_env, monkeypatch):
monkeypatch.setattr(ilp, "_amd_vulkan_icd_present", lambda: False)
host = _linux_amd("gfx1010")
assert ilp._should_auto_vulkan_for_amd_linux(host) is False
routed, _repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert routed is host
assert persist is None
def test_linux_no_rocm_route_is_linux_only(linux_no_rocm_env):
# Windows has its own floor (_should_auto_vulkan_for_amd_windows).
host = _windows_amd_host(rocm_gfx_target = "gfx1010", rocm_gfx_targets = ["gfx1010"])
assert ilp._should_auto_vulkan_for_amd_linux(host) is False
@pytest.mark.parametrize("backend", ["rocm", "cpu", "cuda"])
def test_linux_no_rocm_route_yields_to_an_explicit_backend(backend, linux_no_rocm_env):
host = _linux_amd("gfx1010")
routed, _repo, _tag, persist = ilp._route_to_vulkan_prebuilt(
host, FORK, "pin", force_cpu = False, llama_backend = backend
)
assert routed is host
assert persist is None
def test_linux_no_rocm_route_forced_vulkan_persists_as_automatic(linux_no_rocm_env):
_routed, _repo, _tag, persist = ilp._route_to_vulkan_prebuilt(
_linux_amd("gfx1010"), FORK, "pin", force_cpu = False, llama_backend = "vulkan"
)
assert persist == "auto"
def test_linux_no_rocm_route_force_cpu_wins(linux_no_rocm_env):
host = _linux_amd("gfx1010")
routed, _repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = True)
assert routed is host
assert persist is None
def test_linux_no_rocm_route_carries_no_rocm_fallback(linux_no_rocm_env):
route = ilp.route_backend_request(
backend = None, published_repo = FORK, published_release_tag = "pin", host = _linux_amd("gfx1010")
)
assert route.host.has_rocm is False
assert route.persist_llama_backend == "auto"
assert route.persist_rocm_gfx == "gfx1010"
assert route.rocm_fallback_host is None
def _linux_rocm_and_vulkan_bundle():
def _artifact(
kind,
profile,
gfx_target = None,
mapped = (),
):
return ilp.PublishedLlamaArtifact(
asset_name = f"app-release-{profile}",
install_kind = kind,
runtime_line = None,
coverage_class = None,
supported_sms = [],
min_sm = None,
max_sm = None,
bundle_profile = profile,
rank = 60,
gfx_target = gfx_target,
mapped_targets = list(mapped),
)
# The shape of b11541-mix-2500203: no linux-rocm bundle maps RDNA 1 or Polaris.
artifacts = [
_artifact(
"linux-rocm",
"linux-rocm-gfx103X",
"gfx103X",
["gfx1030", "gfx1031", "gfx1032", "gfx1034"],
),
_artifact(
"linux-rocm",
"linux-rocm-gfx110X",
"gfx110X",
["gfx1100", "gfx1101", "gfx1102", "gfx1103"],
),
_artifact("linux-vulkan", "linux-vulkan-x64"),
_artifact("linux-cpu", "linux-cpu-x64"),
]
return ilp.PublishedReleaseBundle(
repo = FORK,
release_tag = "release",
upstream_tag = "b9925",
assets = {a.asset_name: f"https://example/{a.asset_name}" for a in artifacts},
artifacts = artifacts,
)
def test_linux_rdna1_plan_installs_the_vulkan_bundle_not_a_source_build(
linux_no_rocm_env, monkeypatch
):
bundle = _linux_rocm_and_vulkan_bundle()
host = _linux_amd("gfx1010")
# With hipcc, HIP is source-built; without it the build ends CPU-only, so Vulkan is taken.
monkeypatch.setattr(ilp, "_hipcc_available", lambda: True)
assert ilp._linux_published_attempts(host, bundle) == []
monkeypatch.setattr(ilp, "_hipcc_available", lambda: False)
assert [a.install_kind for a in ilp._linux_published_attempts(host, bundle)] == ["linux-vulkan"]
routed, *_ = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
kinds = [a.install_kind for a in ilp._linux_published_attempts(routed, bundle)]
assert kinds == ["linux-vulkan", "linux-cpu"]
def test_linux_rdna2_plan_keeps_its_rocm_bundle(linux_no_rocm_env):
bundle = _linux_rocm_and_vulkan_bundle()
host = _linux_amd("gfx1030")
routed, *_ = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)
assert routed is host
assert [a.install_kind for a in ilp._linux_published_attempts(routed, bundle)] == ["linux-rocm"]
def test_linux_no_rocm_set_matches_the_torch_installer():
# Duplicated because this module is vendored standalone; keep the two in step.
# gfx1153 is torch-only: no torch wheel, but the llama.cpp HIP route is left alone.
ips = importlib.import_module("install_python_stack")
assert ilp.LINUX_NO_ROCM_GFX_TARGETS == ips._LINUX_UNCOVERED_AMD_GFX - {"gfx1153"}