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

"""Pins the argv llama-server launches with: byte-identical to before when no extra args are set."""

from __future__ import annotations

import pytest

# Reuse the existing harness by path: the tests dir is not a package.
import importlib.util as _importlib_util
from pathlib import Path as _Path

_PLACEMENT_PATH = _Path(__file__).resolve().parent / "test_llama_cpp_placement.py"
_spec = _importlib_util.spec_from_file_location("_placement_harness", _PLACEMENT_PATH)
_placement = _importlib_util.module_from_spec(_spec)
_spec.loader.exec_module(_placement)
_backend = _placement._backend
_launch = _placement._launch


def _cmd(tmp_path, **load_kwargs):
    backend, gguf = _backend(tmp_path, vulkan = False, memory = [(0, 20_000, 24_000)])
    return _launch(backend, gguf, **load_kwargs)["cmd"]


def _stable(cmd: list[str]) -> list[str]:
    """Masks the per-launch port, GGUF path and API key file, leaving the rest of the argv to compare."""
    masked = list(cmd)
    for index, token in enumerate(masked):
        if index and masked[index - 1] == "--port":
            masked[index] = "<port>"
        elif index and masked[index - 1] in {"-m", "--model"}:
            masked[index] = "<model>"
        elif index and masked[index - 1] == "--api-key-file":
            masked[index] = "<api-key-file>"
    return masked


def test_no_extra_args_leaves_the_command_unchanged(tmp_path):
    baseline = _stable(_cmd(tmp_path))
    assert _stable(_cmd(tmp_path, extra_args = None)) == baseline
    assert _stable(_cmd(tmp_path, extra_args = [])) == baseline
    assert "--flash-attn" in baseline and "<port>" in baseline


def test_an_extra_arg_reaches_the_child(tmp_path):
    cmd = _cmd(tmp_path, extra_args = ["--top-k", "20"])

    assert "--top-k" in cmd
    assert cmd[cmd.index("--top-k") + 1] == "20"


@pytest.mark.parametrize("value", ["auto", "all"])
def test_gpu_layer_keywords_reach_the_auto_launcher_verbatim(tmp_path, value):
    args = ["-ngl", value]
    cmd = _cmd(tmp_path, extra_args = args)

    assert cmd[-len(args) :] == args


@pytest.mark.parametrize("value, layers", [("auto", -1), ("all", 2147483647)])
def test_promoted_gpu_layer_keywords_keep_manual_fit_intent(tmp_path, value, layers):
    from core.inference.llama_server_args import parse_gpu_layers_override
    cmd = _cmd(
        tmp_path,
        gpu_memory_mode = "manual",
        gpu_layers = parse_gpu_layers_override(["-ngl", value]),
    )

    if layers < 0:
        assert "--gpu-layers" not in cmd and "-ngl" not in cmd
        assert cmd[cmd.index("--fit") + 1] == "on"
    else:
        assert cmd[cmd.index("--gpu-layers") + 1] == str(layers)
        assert cmd[cmd.index("--fit") + 1] == "off"


def test_extra_args_come_after_unsloths_own(tmp_path):
    cmd = _cmd(tmp_path, extra_args = ["--top-k", "20"])
    managed = [i for i, token in enumerate(cmd) if token in {"--model", "-m", "--port"}]

    assert managed, "expected Unsloth's own flags in the command"
    assert cmd.index("--top-k") > max(managed)


def test_a_shadowing_value_wins_by_arriving_last(tmp_path):
    # The backend allows this shadow, so the user's value must come last.
    cmd = _cmd(tmp_path, extra_args = ["--ctx-size", "4096"])
    positions = [i for i, token in enumerate(cmd) if token in {"--ctx-size", "-c"}]

    assert positions, "expected a context flag in the command"
    assert cmd[max(positions) + 1] == "4096"


def test_a_multi_token_value_stays_one_argv_entry(tmp_path):
    # A template with spaces must stay one argv token.
    template = "{% for m in messages %}{{ m.content }}{% endfor %}"
    cmd = _cmd(tmp_path, extra_args = ["--chat-template", template])

    assert cmd[cmd.index("--chat-template") + 1] == template


@pytest.mark.parametrize(
    "extra_args,family",
    [
        pytest.param(["--cache-type-k", "q4_0"], {"-ctk", "--cache-type-k"}, id = "cache-k"),
        pytest.param(["-ctk", "q4_0"], {"-ctk", "--cache-type-k"}, id = "cache-k-short"),
        pytest.param(["--cache-type-v", "q4_0"], {"-ctv", "--cache-type-v"}, id = "cache-v"),
        pytest.param(["-ctv", "q4_0"], {"-ctv", "--cache-type-v"}, id = "cache-v-short"),
        pytest.param(["--flash-attn", "off"], {"-fa", "--flash-attn"}, id = "flash-off"),
        pytest.param(["-fa", "off"], {"-fa", "--flash-attn"}, id = "flash-short"),
        pytest.param(
            ["--context-shift"],
            {"--context-shift", "--no-context-shift"},
            id = "context-shift",
        ),
        pytest.param(
            ["--no-context-shift"],
            {"--context-shift", "--no-context-shift"},
            id = "no-context-shift",
        ),
        pytest.param(["--jinja"], {"--jinja", "--no-jinja"}, id = "jinja"),
        pytest.param(["--no-jinja"], {"--jinja", "--no-jinja"}, id = "no-jinja"),
    ],
)
def test_an_explicit_semantic_override_suppresses_the_automatic_atom(tmp_path, extra_args, family):
    cmd = _cmd(tmp_path, cache_type_kv = "q8_0", extra_args = extra_args)

    assert cmd[-len(extra_args) :] == extra_args
    assert family.isdisjoint(cmd[: -len(extra_args)])


@pytest.mark.parametrize("extra_arg", ["--metrics", "--no-metrics"])
def test_a_supported_metrics_override_suppresses_the_automatic_flag(tmp_path, extra_arg):
    backend, gguf = _backend(tmp_path, vulkan = False, memory = [(0, 20_000, 24_000)])
    caps = backend.probe_server_capabilities()
    caps["supports_metrics"] = True
    backend.probe_server_capabilities = lambda _binary = None: caps

    cmd = _launch(backend, gguf, extra_args = [extra_arg])["cmd"]

    assert cmd[-1] == extra_arg
    assert not {"--metrics", "--no-metrics"}.intersection(cmd[:-1])


@pytest.mark.parametrize(
    "extra_args,other_axis",
    [
        pytest.param(["-ctk", "q4_0"], "--cache-type-v", id = "k-keeps-v"),
        pytest.param(["-ctv", "q4_0"], "--cache-type-k", id = "v-keeps-k"),
    ],
)
def test_cache_axis_overrides_are_independent(tmp_path, extra_args, other_axis):
    cmd = _cmd(tmp_path, cache_type_kv = "q8_0", extra_args = extra_args)

    assert other_axis in cmd[: -len(extra_args)]
    assert cmd[-len(extra_args) :] == extra_args


def test_a_supported_flash_override_is_not_treated_as_a_missing_flag(tmp_path):
    extra_args = ["-fa", "on"]
    cmd = _cmd(tmp_path, cache_type_kv = "q8_0", extra_args = extra_args)

    cache_v = cmd.index("--cache-type-v")
    assert cmd[cache_v + 1] == "q8_0"
    assert cmd[-len(extra_args) :] == extra_args


def test_unknown_and_repeated_extra_args_remain_verbatim_and_last(tmp_path):
    extra_args = [
        "--future-flag",
        "alpha",
        "--metrics",
        "--future-flag",
        "omega",
        "--no-metrics",
    ]
    cmd = _cmd(tmp_path, extra_args = extra_args)

    assert cmd[-len(extra_args) :] == extra_args
    assert not {"--metrics", "--no-metrics"}.intersection(cmd[: -len(extra_args)])


@pytest.mark.parametrize(
    "denied",
    [
        ["--agent"],
        ["--tools-runtime", "docker:x"],
        ["--mcp-servers-json", "{}"],
        ["--parallel", "8"],
        ["--api-key", "secret"],
    ],
)
def test_a_denied_flag_never_reaches_the_child(tmp_path, denied):
    with pytest.raises(ValueError, match = "managed by Unsloth Studio"):
        from core.inference.llama_server_args import validate_extra_args
        validate_extra_args(denied)


def test_the_denied_env_twin_does_not_survive_the_launch(tmp_path, monkeypatch):
    # llama.cpp reads LLAMA_ARG_* before argv, so the env twin must be denied too.
    monkeypatch.setenv("LLAMA_ARG_AGENT", "1")
    monkeypatch.setenv("LLAMA_ARG_TOOLS", "all")
    backend, gguf = _backend(tmp_path, vulkan = False, memory = [(0, 20_000, 24_000)])
    env = _launch(backend, gguf)["env"]

    assert "LLAMA_ARG_AGENT" not in env
    assert "LLAMA_ARG_TOOLS" not in env