Skip to content

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

7.8 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

"""Selector's install kind must pass setup.ps1's mismatch gate, or every update deletes and refetches."""

from __future__ import annotations

import itertools
import pathlib
import re
import shutil
import hashlib
import sys

import pytest

REPO_ROOT = pathlib.Path(__file__).resolve().parents[3]
SETUP_PS1 = REPO_ROOT / "studio" / "setup.ps1"
SETUP_SRC = SETUP_PS1.read_text(encoding = "utf-8")

sys.path.insert(0, str(REPO_ROOT / "studio"))
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2] / "_shared"))

import install_llama_prebuilt as ip  # noqa: E402
from unsloth_pwsh_runner import run_pwsh  # noqa: E402

PWSH = shutil.which("pwsh") or shutil.which("powershell")
requires_pwsh = pytest.mark.skipif(PWSH is None, reason = "PowerShell is unavailable")

TAG = "b9334"

# Everything upstream publishes for Windows; there is no ROCm or CUDA bundle for Windows ARM64.
ASSET_NAMES = (
    f"llama-{TAG}-bin-win-cpu-arm64.zip",
    f"llama-{TAG}-bin-win-cpu-x64.zip",
    f"llama-{TAG}-bin-win-cuda-12.4-x64.zip",
    f"llama-{TAG}-bin-win-cuda-13.0-x64.zip",
    f"llama-{TAG}-bin-win-vulkan-x64.zip",
    f"llama-{TAG}-bin-win-hip-radeon-x64.zip",
)


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()


RELEASE = {
    "tag_name": TAG,
    "assets": [
        {
            "name": name,
            "browser_download_url": f"https://example.invalid/{name}",
            "digest": f"sha256:{_fixture_digest(name)}",
        }
        for name in ASSET_NAMES
    ],
}


def _host(*, arm64: bool, nvidia: bool, rocm: bool) -> ip.HostInfo:
    """The interpreter's own view of the machine, which is what the selector reads: an
    emulated x64 Python on an ARM64 box reports AMD64, and is an x64 host here."""
    return ip.HostInfo(
        system = "Windows",
        machine = "ARM64" if arm64 else "AMD64",
        is_windows = True,
        is_linux = False,
        is_macos = False,
        is_x86_64 = not arm64,
        is_arm64 = arm64,
        nvidia_smi = "nvidia-smi" if nvidia else None,
        driver_cuda_version = (13, 0) if nvidia else None,
        compute_caps = ["12.1"] if nvidia else [],
        visible_cuda_devices = None,
        has_physical_nvidia = nvidia,
        has_usable_nvidia = nvidia,
        has_rocm = rocm,
        rocm_gfx_target = "gfx1201" if rocm else None,
    )


def _gate_block() -> str:
    """setup.ps1's own expected-kinds computation, from the opt-out read to the delete."""
    start = SETUP_SRC.index("$_arm64CudaOptOut = ")
    end = SETUP_SRC.index("if ($existingKind -and", start)
    return SETUP_SRC[start:end]


def _expected_kinds(*, arm64_venv: bool, nvidia: bool, rocm: bool, opt_out: bool) -> list[str]:
    block = _gate_block()
    # Keep the parentheses: `if $true {` does not parse.
    block = block.replace("Test-WinArm64Venv", "$true" if arm64_venv else "$false")
    # Only Test-WoaPersistableIndex reads the evidence index, so stub it.
    block = re.sub(
        r"(?s)\$_woaEvidenceIndex = if.*?\n(\s*)\$_nvidiaEvidence",
        "$_woaEvidenceIndex = '{}'\n\\1$_nvidiaEvidence".format(
            "https://pypi.nvidia.com/nvtorch_oot" if nvidia else ""
        ),
        block,
    )
    script = "\n".join(
        [
            "$ErrorActionPreference = 'Stop'",
            "function Test-WoaPersistableIndex { param($i) return ($i -like '*nvidia*') }",
            f"$env:UNSLOTH_LLAMA_ARM64_CUDA = '{'0' if opt_out else '1'}'",
            f"$HasNvidiaSmi = ${str(nvidia).lower()}",
            f"$HasNvidiaDriverEvidence = ${str(nvidia).lower()}",
            f"$HasROCm = ${str(rocm).lower()}",
            "$script:ROCmGfxArch = " + ("'gfx1201'" if rocm else "$null"),
            block,
            "Write-Output ('<<<' + ($expectedKinds -join ',') + '>>>')",
        ]
    )
    # run_pwsh: a shared PowerShell startup cache across xdist workers kills ~1 startup in 500.
    done = run_pwsh(
        [PWSH, "-NoProfile", "-NonInteractive", "-Command", script],
        capture_output = True,
        timeout = 120,
    )
    out = done.stdout.decode("utf-8", "replace")
    assert "<<<" in out, done.stderr.decode("utf-8", "replace")
    return out[out.index("<<<") + 3 : out.rindex(">>>")].split(",")


def _installed_kinds(host: ip.HostInfo) -> list[str]:
    plan = ip.direct_upstream_release_plan(RELEASE, host, "ggml-org/llama.cpp", "latest")
    return [attempt.install_kind for attempt in plan.attempts] if plan else []


# nvidia and rocm together is not a host we ship to: the ROCm arm wins outright on both sides.
COMBINATIONS = [
    (arm64_venv, nvidia, rocm, opt_out)
    for arm64_venv, nvidia, rocm, opt_out in itertools.product([True, False], repeat = 4)
    if not (nvidia and rocm)
]


@requires_pwsh
@pytest.mark.parametrize(
    ("arm64_venv", "nvidia", "rocm", "opt_out"),
    COMBINATIONS,
    ids = [
        "{}-{}{}".format(
            "arm64" if a else "x64",
            "nvidia" if n else ("rocm" if r else "cpu"),
            "-optout" if o else "",
        )
        for a, n, r, o in COMBINATIONS
    ],
)
def test_the_gate_accepts_what_the_selector_installs(arm64_venv, nvidia, rocm, opt_out):
    expected = _expected_kinds(arm64_venv = arm64_venv, nvidia = nvidia, rocm = rocm, opt_out = opt_out)
    installed = _installed_kinds(_host(arm64 = arm64_venv, nvidia = nvidia, rocm = rocm))
    assert installed, "no candidate at all, so this row proves nothing"
    assert installed[0] in expected, (
        f"the gate would delete what the selector just installed, on every update: "
        f"selector picks {installed[0]}, gate accepts {expected}"
    )


@requires_pwsh
def test_no_arm64_row_expects_a_kind_only_published_for_x64():
    """The shape behind both bugs: on an ARM64 venv a gate that expects an x64-only kind and
    nothing else is a delete on every update whatever the selector does. windows-vulkan counts
    as x64-only, since upstream's bundle is vulkan-x64."""
    x64_only = {"windows-cuda", "windows-rocm", "windows-hip", "windows-cpu", "windows-vulkan"}
    for nvidia, rocm, opt_out in itertools.product([True, False], repeat = 3):
        if nvidia and rocm:
            continue
        expected = set(_expected_kinds(arm64_venv = True, nvidia = nvidia, rocm = rocm, opt_out = opt_out))
        assert expected - x64_only, (
            f"nvidia={nvidia} rocm={rocm} optout={opt_out}: the gate expects only x64 kinds "
            f"on an ARM64 venv: {sorted(expected)}"
        )


@requires_pwsh
@pytest.mark.parametrize(
    ("nvidia", "rocm", "opt_out"),
    [(False, False, False), (True, False, False), (True, False, True), (False, True, False)],
)
def test_the_gate_keeps_an_opt_in_arm64_vulkan_install(nvidia, rocm, opt_out):
    """UNSLOTH_LLAMA_CPP_BACKEND=vulkan on Windows ARM64 installs windows-arm64-vulkan; a gate
    without it deletes that tree on every update. An x64 venv never expects it."""
    name = f"llama-{TAG}-bin-win-vulkan-arm64.zip"
    release = {
        **RELEASE,
        "assets": RELEASE["assets"]
        + [
            {
                "name": name,
                "browser_download_url": f"https://example.invalid/{name}",
                "digest": f"sha256:{_fixture_digest(name)}",
            }
        ],
    }
    routed = ip._vulkan_only_host(_host(arm64 = True, nvidia = nvidia, rocm = rocm))
    plan = ip.direct_upstream_release_plan(release, routed, "ggml-org/llama.cpp", "latest")
    assert plan.attempts[0].install_kind == "windows-arm64-vulkan"
    expected = _expected_kinds(arm64_venv = True, nvidia = nvidia, rocm = rocm, opt_out = opt_out)
    assert "windows-arm64-vulkan" in expected
    x64 = _expected_kinds(arm64_venv = False, nvidia = nvidia, rocm = rocm, opt_out = opt_out)
    assert "windows-arm64-vulkan" not in x64