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.

13.3 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-Present the Unsloth team. See /studio/LICENSE.AGPL-3.0

"""_KEEP is for packages whose replacement breaks the tested stack or unsloth, not any notebook dep."""

from __future__ import annotations

import importlib.util
import os
from pathlib import Path

import pytest

REPO_ROOT = Path(__file__).resolve().parents[2]
SHIM_PATH = REPO_ROOT / "docker" / "unsloth_pip_shim.py"

SHIPPED_TRL_CELL = ["--no-deps", "trl==0.22.2"]
UNBAKED = "snac"


class _Exec(Exception):
    def __init__(self, path, argv):
        self.path = path
        self.argv = list(argv)


class _BakedImage:
    """Only membership is asked, so the prefix rule keeps nvidia-* wheels present; a _KEEP set cannot."""

    def __init__(self, mod):
        self._mod = mod

    def __contains__(self, name):
        # transformers is out of _KEEP only because the sidecar replaces its version.
        if name == "transformers":
            return True
        return name in self._mod._KEEP or name.startswith(self._mod._KEEP_PREFIX)


@pytest.fixture()
def shim(tmp_path, monkeypatch):
    monkeypatch.setenv("UNSLOTH_NB_TF_MARKER", str(tmp_path / "requested_transformers"))
    monkeypatch.setenv("UNSLOTH_NB_SHIM", "1")
    assert SHIM_PATH.is_file(), f"missing shim: {SHIM_PATH}"
    spec = importlib.util.spec_from_file_location("unsloth_pip_shim_stack_test", SHIM_PATH)
    mod = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(mod)

    def _fake_execv(path, argv):
        raise _Exec(path, argv)

    monkeypatch.setattr(mod.os, "execv", _fake_execv)
    # Pin the baked image so results do not depend on the CI venv's packages.
    monkeypatch.setattr(mod, "_installed_names", lambda: _BakedImage(mod))
    return mod


def _run(
    shim,
    args,
    tool = "pip",
    sub = "install",
):
    """Args after the subcommand, or None when the shim no-op'd; constraints pair dropped."""
    argv = ["uv", "pip", sub, *args] if tool == "uv" else ["pip", sub, *args]
    with pytest.MonkeyPatch.context() as mp:
        mp.setattr(shim.sys, "argv", argv)
        try:
            shim.main()
            return None
        except _Exec as exc:
            i = exc.argv.index(sub)
            execd = exc.argv[i + 1 :]
            if (
                len(execd) >= 2
                and execd[-2] == "--constraint"
                and os.path.basename(execd[-1]).startswith("unsloth-nb-protected-")
            ):
                execd = execd[:-2]
            return execd


@pytest.mark.parametrize(
    "pkg",
    [
        "trl",
        "peft",
        "datasets",
        "accelerate",
        "torchao",
        "torchcodec",
        "huggingface-hub",
        "tokenizers",
        "safetensors",
    ],
)
def test_training_stack_is_protected(shim, pkg):
    assert (
        pkg in shim._KEEP
    ), f"{pkg} is baked and tested; a notebook pin replacing it invalidates the image"


def test_the_original_gpu_stack_is_still_protected(shim):
    for pkg in [
        "torch",
        "torchvision",
        "torchaudio",
        "triton",
        "xformers",
        "vllm",
        "bitsandbytes",
        "unsloth",
        "unsloth-zoo",
    ]:
        assert pkg in shim._KEEP


def test_unrelated_packages_are_not_swept_in(shim):
    for pkg in [
        "snac",
        "causal-conv1d",
        "mamba-ssm",
        "omegaconf",
        "timm",
        "librosa",
        "trackio",
        "open-spiel",
        "protobuf",
        "sentencepiece",
    ]:
        assert pkg not in shim._KEEP, f"{pkg} must still install for the notebooks that need it"


def test_the_shipped_trl_cell_installs_nothing(shim):
    assert _run(shim, SHIPPED_TRL_CELL) is None


def test_a_mixed_cell_keeps_only_the_unbaked_package(shim):
    execd = _run(
        shim,
        [
            "--no-deps",
            "trl==0.22.2",
            "peft==0.14.0",
            "datasets==3.0.0",
            "accelerate==1.0.0",
            UNBAKED,
        ],
    )
    assert execd == ["--no-deps", UNBAKED], execd


def test_cuda_matched_wheels_are_not_replaced_by_pypi_builds(shim):
    # These come from the cu128 index; a PyPI pin swaps in a generic build.
    assert _run(shim, ["torchao==0.15.0", "torchcodec==0.5"]) is None


def test_transformers_companions_cannot_desynchronise_the_sidecars(shim):
    assert (
        _run(shim, ["huggingface_hub==0.30.0", "tokenizers==0.20.0", "safetensors==0.4.0"]) is None
    )


def test_an_unbaked_package_still_installs(shim):
    assert _run(shim, [UNBAKED]) == [UNBAKED]
    assert _run(shim, [UNBAKED], tool = "uv") == [UNBAKED]


def test_protection_survives_a_requirements_file(shim, tmp_path):
    req = tmp_path / "requirements.txt"
    req.write_text(f"trl==0.22.2\npeft==0.14.0\ndatasets==3.0.0\n{UNBAKED}\n")
    execd = _run(shim, ["-r", str(req)])
    assert execd is not None and execd[0] == "-r"
    filtered = Path(execd[1]).read_text()
    assert UNBAKED in filtered
    for dropped in ("trl", "peft", "datasets"):
        assert dropped not in filtered, f"{dropped} slipped through the requirements file"


def test_protection_survives_a_direct_wheel_url(shim):
    url = "https://files.pythonhosted.org/x/trl-0.22.2-py3-none-any.whl"
    assert _run(shim, [url, UNBAKED]) == [UNBAKED]


def test_protection_survives_an_editable_vcs_install(shim):
    assert _run(shim, ["-e", "git+https://github.com/huggingface/trl.git", UNBAKED]) == [UNBAKED]


# An unbaked protected package must install, not be silently dropped.
MISSING = "vllm"


def _without(mod, missing):
    """_installed_names() for an image whose `missing` bake was skipped."""
    baked = _BakedImage(mod)

    class _Partial:
        def __contains__(self, name):
            return name != missing and name in baked

    return _Partial()


@pytest.fixture()
def shim_without_vllm(shim, monkeypatch):
    """The same shim over an image whose vLLM bake was skipped."""
    monkeypatch.setattr(shim, "_installed_names", lambda: _without(shim, MISSING))
    return shim


def test_the_baked_premise_holds_before_the_absence_tests_mean_anything(shim):
    """Non-vacuity: the two views must disagree, or every test below is trivial."""
    assert _run(shim, [MISSING]) is None
    with pytest.MonkeyPatch.context() as mp:
        mp.setattr(shim, "_installed_names", lambda: _without(shim, MISSING))
        assert _run(shim, [MISSING]) == [MISSING]


def test_a_protected_package_the_image_never_baked_still_installs(shim_without_vllm):
    # The arm64 vLLM bake is fail-soft, so `!pip install vllm` is the recovery.
    assert _run(shim_without_vllm, [MISSING]) == [MISSING]
    assert _run(shim_without_vllm, [f"{MISSING}==0.20.0"]) == [f"{MISSING}==0.20.0"]
    assert _run(shim_without_vllm, [MISSING], tool = "uv") == [MISSING]


def test_the_absence_check_reaches_the_requirements_file_path(shim_without_vllm, tmp_path):
    req = tmp_path / "requirements.txt"
    req.write_text(f"trl==0.22.2\n{MISSING}==0.20.0\n")
    execd = _run(shim_without_vllm, ["-r", str(req)])
    assert execd is not None and execd[0] == "-r"
    filtered = Path(execd[1]).read_text()
    assert MISSING in filtered, filtered
    assert "trl" not in filtered, filtered


def test_the_absence_check_reaches_the_flag_target_path(shim_without_vllm):
    assert _run(shim_without_vllm, ["-P", MISSING, UNBAKED]) == ["-P", MISSING, UNBAKED]
    assert _run(shim_without_vllm, ["-P", "trl", UNBAKED]) == [UNBAKED]


def test_an_unreadable_metadata_scan_keeps_the_stricter_answer(shim, monkeypatch):
    """Never open the stack up because the venv could not be read."""
    monkeypatch.setattr(shim, "_installed_names", lambda: None)
    assert _run(shim, [MISSING]) is None
    assert _run(shim, ["torch"]) is None


def test_installed_names_reads_a_real_venv(shim):
    """The helper itself, unpatched: a stub returning an empty set would pass every
    test above while forwarding the whole baked stack in the image."""
    spec = importlib.util.spec_from_file_location("unsloth_pip_shim_unpatched", SHIM_PATH)
    fresh = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(fresh)
    names = fresh._installed_names()
    assert names is not None
    assert "pytest" in names, "the running interpreter must at least see pytest"
    assert "definitely-not-a-real-distribution" not in names


def test_every_drop_decision_goes_through_the_one_predicate(shim):
    """The three call sites drifted apart before; keep them on _is_protected."""
    source = SHIM_PATH.read_text(encoding = "utf-8")
    raw = [
        line
        for line in source.splitlines()
        if "_KEEP_PREFIX)" in line and "_KEEP_PREFIX = " not in line
    ]
    assert len(raw) == 2, raw


def test_the_dockerfile_still_lets_a_protected_bake_fail(shim):
    """Premise pin: if every bake becomes mandatory, the absence path is dead code and
    this file should be revisited rather than left asserting a case that cannot arise."""
    dockerfile = (REPO_ROOT / "docker" / "Dockerfile").read_text(encoding = "utf-8")
    assert "torchcodec bake skipped" in dockerfile
    assert "fail-soft on non-amd64" in dockerfile
    assert MISSING in shim._KEEP


def test_forwarded_installs_pin_the_protected_set_for_the_resolver(shim):
    # Argument filtering alone does not stop a dependency from downgrading peft/datasets.
    with pytest.MonkeyPatch.context() as mp:
        mp.setattr(shim.sys, "argv", ["pip", "install", UNBAKED])
        with pytest.raises(_Exec) as exc:
            shim.main()
    argv = exc.value.argv
    assert "--constraint" in argv
    pins = Path(argv[argv.index("--constraint") + 1]).read_text()
    names = {line.split("==")[0].lower().replace("_", "-") for line in pins.splitlines() if line}
    assert names, "the constraints file must not be empty"
    assert all(
        n in shim._KEEP or n == "transformers" or n.startswith("nvidia-") for n in names
    ), sorted(names)


def test_the_shipped_falcon_cell_cannot_remove_unsloth(shim, capsys):
    assert _run(shim, ["unsloth", "-y"], sub = "uninstall") is None
    assert "skipped: unsloth" in capsys.readouterr().out


def test_the_shipped_qwen_moe_cell_keeps_torchcodec(shim):
    assert _run(shim, ["-y", "sentence-transformers", "torchcodec"], sub = "uninstall") == [
        "-y",
        "sentence-transformers",
    ]


@pytest.mark.parametrize("pkg", ["torch", "vllm", "trl", "transformers", "nvidia-cublas-cu12"])
def test_uninstall_of_the_baked_stack_is_dropped(shim, pkg):
    assert _run(shim, ["-y", pkg], sub = "uninstall") is None
    assert _run(shim, [pkg], tool = "uv", sub = "uninstall") is None


def test_uninstall_of_an_unbaked_package_still_runs(shim):
    assert _run(shim, ["-y", UNBAKED], sub = "uninstall") == ["-y", UNBAKED]
    assert _run(shim, ["-qy", UNBAKED], sub = "uninstall") == ["-q", "-y", UNBAKED]


def test_uninstall_of_a_protected_package_the_image_never_baked_still_runs(shim_without_vllm):
    assert _run(shim_without_vllm, ["-y", MISSING], sub = "uninstall") == ["-y", MISSING]


def test_uninstall_protection_survives_a_requirements_file(shim, tmp_path):
    req = tmp_path / "remove.txt"
    req.write_text(f"torchcodec\n{UNBAKED}\n")
    for args in (["-y", "-r", str(req)], ["-y", f"-r{req}"], ["-y", f"--requirement={req}"]):
        execd = _run(shim, args, sub = "uninstall")
        assert execd is not None, args
        path = execd[-1].partition("=")[2] if execd[-1].startswith("--requirement=") else execd[-1]
        assert Path(path).read_text().split() == [UNBAKED], args


def test_uninstall_protection_survives_the_pip_requirement_env(shim, tmp_path, monkeypatch):
    req = tmp_path / "remove.txt"
    req.write_text(f"torchcodec\n{UNBAKED}\n")
    monkeypatch.setenv("PIP_REQUIREMENT", str(req))

    def _fake_execve(path, argv, env):
        assert "PIP_REQUIREMENT" not in env
        raise _Exec(path, argv)

    monkeypatch.setattr(shim.os, "execve", _fake_execve)
    execd = _run(shim, ["-y"], sub = "uninstall")
    assert execd is not None and execd[0] == "-r" and execd[2] == "-y", execd
    assert Path(execd[1]).read_text().split() == [UNBAKED]


def test_uninstall_requirements_file_extras_do_not_reach_the_baked_package(shim, tmp_path):
    req = tmp_path / "remove.txt"
    req.write_text(f"torch[opt]\ntransformers[torch]\n{UNBAKED}\n")
    execd = _run(shim, ["-y", "-r", str(req)], sub = "uninstall")
    assert Path(execd[-1]).read_text().split() == [UNBAKED]


def test_uninstall_requirements_file_of_only_baked_packages_is_a_no_op(shim, tmp_path, capsys):
    req = tmp_path / "remove.txt"
    req.write_text("# baked\ntorchcodec\nunsloth[colab-new]\n")
    for args in (["-y", "-r", str(req)], ["-y", f"-r{req}"], ["-y", f"--requirement={req}"]):
        assert _run(shim, args, sub = "uninstall") is None, args
        assert "nothing to uninstall" in capsys.readouterr().out
    assert _run(shim, ["-y", "-r", str(req), UNBAKED], sub = "uninstall") == ["-y", UNBAKED]


@pytest.mark.parametrize("var", ["PIP_TARGET", "PIP_PREFIX", "PIP_ROOT"])
def test_pip_uninstall_ignores_install_destination_variables(shim, monkeypatch, tmp_path, var):
    monkeypatch.setenv(var, str(tmp_path / "outside"))
    assert _run(shim, ["-y", "torch"], sub = "uninstall") is None
    assert _run(shim, ["-y", UNBAKED], sub = "uninstall") == ["-y", UNBAKED]


@pytest.mark.parametrize("tool", ["pip", "uv"])
def test_targetless_uninstall_reaches_the_real_cli(shim, tool):
    assert _run(shim, ["--help"], tool = tool, sub = "uninstall") == ["--help"]
    assert _run(shim, ["-y"], tool = tool, sub = "uninstall") == ["-y"]