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.9 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

"""Scoped downloads fetch an explicit file list, since a full snapshot would pull tens of GB unused."""

from __future__ import annotations

import asyncio
import json
import sys
from pathlib import Path
from types import SimpleNamespace

import pytest


def _shared_setup_1(monkeypatch):
    monkeypatch.setattr(dl, "_reject_if_load_in_flight", lambda repo_id: None)
    monkeypatch.setattr(dl, "resolve_cached_repo_id_case", lambda repo, **k: repo)
    monkeypatch.setattr(dl, "scoped_file_blob_hashes", lambda *a, **k: frozenset())
    monkeypatch.setattr(download_lifecycle, "launch_worker", lambda *a, **k: "running")


_BACKEND_DIR = str(Path(__file__).resolve().parent.parent)
if _BACKEND_DIR not in sys.path:
    sys.path.insert(0, _BACKEND_DIR)

from fastapi import HTTPException

from hub.schemas.downloads import DownloadModelRequest, DownloadStartResponse
from hub.services import download_lifecycle
from hub.services.models import downloads as dl
from hub.utils.paths import is_valid_gguf_variant


FILES = ["model_index.json", "vae/diffusion_pytorch_model.safetensors"]


REPO = "black-forest-labs/FLUX.1-dev"


@pytest.fixture(autouse = True)
def _repo_with_no_running_job():
    """The download registry is process-global, so each test must retire the repo's running jobs."""

    def _retire():
        for ref in download_lifecycle.active_download_refs(dl._registry, REPO, with_variant = True):
            dl._registry.set_job(dl._download_job_key(REPO, ref.variant), "complete")

    _retire()
    yield
    _retire()


def _request(**over) -> DownloadModelRequest:
    body = {
        "repo_id": "black-forest-labs/FLUX.1-dev",
        "scope_id": "diffusion",
        "files": list(FILES),
        "use_xet": False,
    }
    body.update(over)
    return DownloadModelRequest(**body)


def test_scope_keys_apart_from_the_full_snapshot():
    # The scoped job must not adopt or overwrite the full snapshot's manifest.
    full = dl._download_job_key("black-forest-labs/FLUX.1-dev", None)
    scoped = dl._download_job_key("black-forest-labs/FLUX.1-dev", dl._scope_variant("diffusion"))
    assert full != scoped
    assert scoped.endswith("@diffusion")
    assert is_valid_gguf_variant("@diffusion")
    # The "@" prefix keeps a scope out of the quant namespace: a job scoped "diffusion" and a quant named "diffusion" stay distinct.
    assert dl._download_job_key("org/m", "diffusion") != dl._download_job_key(
        "org/m", dl._scope_variant("diffusion")
    )


def test_scope_requires_files_and_rejects_a_variant(monkeypatch):
    monkeypatch.setattr(dl, "_reject_if_load_in_flight", lambda repo_id: None)
    monkeypatch.setattr(dl, "resolve_cached_repo_id_case", lambda repo, **k: repo)

    with pytest.raises(Exception) as no_files:
        asyncio.run(dl.download_model_response(_request(files = [])))
    assert "files" in str(no_files.value)

    with pytest.raises(Exception) as both:
        asyncio.run(dl.download_model_response(_request(gguf_variant = "Q4_K_M")))
    assert "mutually exclusive" in str(both.value)


def test_a_scoped_start_runs_the_old_audio_runtime_gate_first(monkeypatch):
    _shared_setup_1(monkeypatch)
    seen = []

    async def refuse(
        repo_id,
        files,
        hf_token,
        hub_cache = None,
    ):
        seen.append((repo_id, files))
        raise HTTPException(status_code = 409, detail = "needs a newer audio runtime")

    monkeypatch.setattr(dl, "_refuse_stale_scoped_download", refuse)
    monkeypatch.setattr(
        download_lifecycle, "launch_worker", lambda *a, **k: pytest.fail("job started")
    )
    with pytest.raises(HTTPException) as refused:
        asyncio.run(dl.download_model_response(_request()))
    assert refused.value.status_code == 409 and seen == [(REPO, FILES)]


def test_scoped_start_spawns_a_file_scoped_worker(monkeypatch, tmp_path):
    spawned: dict = {}

    monkeypatch.setattr(dl, "_reject_if_load_in_flight", lambda repo_id: None)
    monkeypatch.setattr(dl, "resolve_cached_repo_id_case", lambda repo, **k: repo)
    monkeypatch.setattr(dl, "scoped_file_blob_hashes", lambda *a, **k: frozenset({"h1"}))

    def _fake_launch(registry, key, *, spawn, **kwargs):
        spawn()
        return "running"

    def _fake_spawn(args, **kwargs):
        spawned["args"] = args
        return object()

    monkeypatch.setattr(download_lifecycle, "launch_worker", _fake_launch)
    monkeypatch.setattr(download_lifecycle.subprocess, "Popen", _fake_spawn)
    monkeypatch.setattr("huggingface_hub.utils.get_token_to_send", lambda token: None)
    monkeypatch.setattr("tempfile.tempdir", str(tmp_path))

    result = asyncio.run(dl.download_model_response(_request()))
    assert result["accepted"] is True
    scope_variant = dl._scope_variant("diffusion")
    assert result["job_key"].endswith(scope_variant)

    args = spawned["args"]
    assert "--variant" in args and args[args.index("--variant") + 1] == scope_variant
    manifest_path = args[args.index("--files-json") + 1]
    assert json.loads(Path(manifest_path).read_text(encoding = "utf-8")) == FILES
    Path(manifest_path).unlink(missing_ok = True)


def test_scoped_files_survive_into_the_registry(monkeypatch):
    # The XET to HTTP retry rebuilds args from registry metadata, so the file list must be stored.
    captured: dict = {}
    real_claim = dl._registry.claim

    def _spy_claim(key, transport, **kwargs):
        captured.update(kwargs)
        return real_claim(key, transport, **kwargs)

    monkeypatch.setattr(dl, "_reject_if_load_in_flight", lambda repo_id: None)
    monkeypatch.setattr(dl, "resolve_cached_repo_id_case", lambda repo, **k: repo)
    monkeypatch.setattr(dl, "scoped_file_blob_hashes", lambda *a, **k: frozenset())
    monkeypatch.setattr(dl._registry, "claim", _spy_claim)
    monkeypatch.setattr(download_lifecycle, "launch_worker", lambda *a, **k: "running")

    asyncio.run(dl.download_model_response(_request()))
    assert captured["scoped_files"] == FILES

    metadata = dl._registry.get_job_metadata(
        dl._download_job_key("black-forest-labs/FLUX.1-dev", dl._scope_variant("diffusion"))
    )
    assert metadata is not None and list(metadata.scoped_files) == FILES


def test_files_manifest_round_trips():
    path = download_lifecycle.write_files_manifest(FILES)
    try:
        assert json.loads(Path(path).read_text(encoding = "utf-8")) == FILES
    finally:
        Path(path).unlink(missing_ok = True)


def test_a_different_file_set_is_not_adopted(monkeypatch):
    # Two quants share the "@diffusion" slot; a second one is refused while the first runs.
    _shared_setup_1(monkeypatch)

    key = dl._download_job_key("black-forest-labs/FLUX.1-dev", dl._scope_variant("diffusion"))
    try:
        first = asyncio.run(dl.download_model_response(_request()))
        assert first["accepted"] is True

        with pytest.raises(HTTPException) as other_files:
            asyncio.run(
                dl.download_model_response(
                    _request(files = ["model_index.json", "flux1-dev-Q2_K.gguf"])
                )
            )
        assert other_files.value.status_code == 409
        assert "different" in other_files.value.detail

        same = asyncio.run(
            dl.download_model_response(_request(files = [FILES[1], FILES[0], FILES[0]]))
        )
        assert same["accepted"] is True and same["job_key"] == key
    finally:
        dl._registry.set_job(key, "complete")


def test_a_start_reports_whether_it_attached_to_a_live_job(monkeypatch):
    _shared_setup_1(monkeypatch)

    repo = "unsloth/attach-flag-probe"
    key = dl._download_job_key(repo, dl._scope_variant("diffusion"))
    try:
        started = asyncio.run(dl.download_model_response(_request(repo_id = repo)))
        assert started["accepted"] is True and started["attached"] is False

        attached = asyncio.run(dl.download_model_response(_request(repo_id = repo)))
        assert attached["accepted"] is True and attached["attached"] is True
        assert attached["job_key"] == key

        # response_model drops undeclared keys, so the flag must survive the schema.
        assert DownloadStartResponse(**attached).model_dump()["attached"] is True
        assert DownloadStartResponse(**started).model_dump()["attached"] is False

        monkeypatch.setattr(dl._registry, "claim", lambda *a, **k: (False, "repository_owned"))
        monkeypatch.setattr(dl._registry, "adoptable", lambda *a, **k: False)
        refused = asyncio.run(dl.download_model_response(_request(repo_id = repo)))
        assert refused["accepted"] is False and refused["attached"] is False
    finally:
        dl._registry.set_job(key, "complete")


def test_the_http_retry_keeps_the_scoped_file_list_on_the_record(monkeypatch):
    # The retry reclaims with replace_active, which overwrites stored metadata including the file list.
    _shared_setup_1(monkeypatch)

    class _Proc:
        pid = 4242

        def poll(self):
            return None

    monkeypatch.setattr(download_lifecycle, "spawn_worker", lambda *a, **k: _Proc())
    monkeypatch.setattr(download_lifecycle, "register_worker", lambda *a, **k: True)

    key = dl._download_job_key("black-forest-labs/FLUX.1-dev", dl._scope_variant("diffusion"))
    try:
        assert asyncio.run(dl.download_model_response(_request(use_xet = True)))["accepted"] is True

        retried = download_lifecycle._try_http_retry(
            dl._registry,
            key,
            hf_token = None,
            label = "FLUX.1-dev [@diffusion]",
            log_prefix = "[test]",
            logger = download_lifecycle.logging.getLogger("test"),
            repo_type = "model",
            repo_id = "black-forest-labs/FLUX.1-dev",
            watch_name = "test",
        )
        assert retried is True

        metadata = dl._registry.get_job_metadata(key)
        assert metadata is not None and list(metadata.scoped_files) == FILES
        again = asyncio.run(dl.download_model_response(_request()))
        assert again["accepted"] is True and again["job_key"] == key
    finally:
        dl._registry.set_job(key, "complete")


def test_scope_key_stays_derivable_from_the_scope_alone():
    # The download manager builds this key client-side (it polls and cancels before any server round-trip), so the scope name alone must produce it.
    assert dl._scope_variant("diffusion") == "@diffusion"
    assert dl._scope_variant("video") == "@video"
    assert dl._scope_variant("  ") is None
    assert is_valid_gguf_variant("@diffusion")


def _fake_backend(*loading: str):
    return SimpleNamespace(loading_repo_ids = lambda: tuple(loading))


def test_an_images_load_staging_a_repo_blocks_a_download_of_it(monkeypatch):
    # The Images/Video backends share the HF cache with the download worker.
    from core.inference import diffusion_engine_router, video as video_backend

    monkeypatch.setattr(
        diffusion_engine_router,
        "get_active_diffusion_engine",
        lambda: _fake_backend("Tongyi-MAI/Z-Image-Turbo", "unsloth/Z-Image-Turbo-GGUF"),
    )
    monkeypatch.setattr(video_backend, "get_video_backend", lambda: _fake_backend())

    assert dl._load_in_flight("Tongyi-MAI/Z-Image-Turbo") is True
    assert dl._load_in_flight("tongyi-mai/z-image-turbo") is True
    assert dl._load_in_flight("unsloth/Z-Image-Turbo-GGUF") is True
    assert dl._load_in_flight("Org/Unrelated") is False


def test_a_video_load_staging_a_repo_blocks_a_download_of_it(monkeypatch):
    from core.inference import diffusion_engine_router, video as video_backend

    monkeypatch.setattr(diffusion_engine_router, "get_active_diffusion_engine", _fake_backend)
    monkeypatch.setattr(
        video_backend,
        "get_video_backend",
        lambda: _fake_backend("Wan-AI/Wan2.2-TI2V-5B-Diffusers"),
    )

    assert dl._load_in_flight("Wan-AI/Wan2.2-TI2V-5B-Diffusers") is True
    assert dl._load_in_flight("Org/Unrelated") is False


def test_an_unavailable_backend_never_blocks_a_download(monkeypatch):
    from core.inference import diffusion_engine_router, video as video_backend

    def _boom():
        raise RuntimeError("no engine")

    monkeypatch.setattr(diffusion_engine_router, "get_active_diffusion_engine", _boom)
    monkeypatch.setattr(video_backend, "get_video_backend", _boom)

    assert dl._load_in_flight("Org/Anything") is False


def test_active_downloads_publish_the_scoped_file_list(monkeypatch):
    """An adopting client has no local record of a live job, so the scoped file list must be published."""
    _shared_setup_1(monkeypatch)

    key = dl._download_job_key("black-forest-labs/FLUX.1-dev", dl._scope_variant("diffusion"))
    try:
        asyncio.run(dl.download_model_response(_request()))
        rows = download_lifecycle.active_download_refs(
            dl._registry, "black-forest-labs/FLUX.1-dev", with_variant = True
        )
        scoped = [r for r in rows if r.variant == "@diffusion"]
        assert scoped, f"no scoped row in {rows}"
        assert list(scoped[0].files or []) == FILES
    finally:
        dl._registry.set_job(key, "complete")


def test_a_full_snapshot_download_reports_no_file_list(monkeypatch):
    monkeypatch.setattr(dl, "_reject_if_load_in_flight", lambda repo_id: None)
    monkeypatch.setattr(dl, "resolve_cached_repo_id_case", lambda repo, **k: repo)
    monkeypatch.setattr(download_lifecycle, "launch_worker", lambda *a, **k: "running")

    key = dl._download_job_key("black-forest-labs/FLUX.1-dev", None)
    try:
        asyncio.run(
            dl.download_model_response(
                DownloadModelRequest(repo_id = "black-forest-labs/FLUX.1-dev", use_xet = False)
            )
        )
        rows = download_lifecycle.active_download_refs(
            dl._registry, "black-forest-labs/FLUX.1-dev", with_variant = True
        )
        full = [r for r in rows if r.variant is None]
        assert full, f"no full-snapshot row in {rows}"
        assert full[0].files is None
    finally:
        dl._registry.set_job(key, "complete")