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.

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

"""The disk preflight must not count cached GGUF bytes as needing download, or cached loads fail."""

from __future__ import annotations

import sys
import tempfile
import types as _types
from pathlib import Path
from unittest.mock import patch

import pytest

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

_loggers_stub = _types.ModuleType("loggers")
_loggers_stub.get_logger = lambda name: __import__("logging").getLogger(name)
sys.modules.setdefault("loggers", _loggers_stub)

_structlog_stub = _types.ModuleType("structlog")
sys.modules.setdefault("structlog", _structlog_stub)

_httpx_stub = _types.ModuleType("httpx")
for _exc_name in (
    "ConnectError",
    "TimeoutException",
    "ReadTimeout",
    "ReadError",
    "RemoteProtocolError",
    "CloseError",
):
    setattr(_httpx_stub, _exc_name, type(_exc_name, (Exception,), {}))


class _FakeTimeout:
    def __init__(self, *a, **kw):
        pass


_httpx_stub.Timeout = _FakeTimeout
_httpx_stub.Client = type(
    "Client",
    (),
    {
        "__init__": lambda self, **kw: None,
        "__enter__": lambda self: self,
        "__exit__": lambda self, *a: None,
    },
)
# only when httpx is absent: a stub without Response breaks starlette.testclient later
try:
    import httpx  # noqa: F401
except ImportError:
    sys.modules.setdefault("httpx", _httpx_stub)


GIB = 1024**3


class _FakePathInfo:
    """Mimics huggingface_hub's RepoFile-ish return type from get_paths_info."""

    def __init__(self, path: str, size: int):
        self.path = path
        self.size = size


def _preflight(
    repo_files,
    cached_files,
    free_bytes,
    hf_repo = "unsloth/Example-GGUF",
    hf_token = None,
):
    """Mirrors the disk preflight over (name, bytes) repo_files and cached_files; returns a dict."""
    import os
    import shutil

    path_infos = [_FakePathInfo(name, size) for name, size in repo_files]

    with tempfile.TemporaryDirectory() as tmp:
        # sparse files: exists/getsize pass without allocating multi-GB on disk
        cache_paths = {}
        for name, sz in cached_files.items():
            p = Path(tmp) / name.replace("/", "_")
            with open(p, "wb") as fh:
                if sz > 0:
                    fh.truncate(sz)
            cache_paths[name] = str(p)

        def fake_try_to_load_from_cache(repo_id, filename):
            return cache_paths.get(filename)

        # mirrors the real code's names and control flow so drift is caught
        total_bytes = sum((p.size or 0) for p in path_infos)
        already_cached_bytes = 0
        for p in path_infos:
            if not p.size:
                continue
            cached_path = fake_try_to_load_from_cache(hf_repo, p.path)
            if isinstance(cached_path, str) and os.path.exists(cached_path):
                try:
                    on_disk = os.path.getsize(cached_path)
                except OSError:
                    on_disk = 0
                if on_disk >= p.size:
                    already_cached_bytes += p.size

        total_download_bytes = max(0, total_bytes - already_cached_bytes)
        needed_download = total_download_bytes > free_bytes
        return {
            "total_bytes": total_bytes,
            "already_cached_bytes": already_cached_bytes,
            "total_download_bytes": total_download_bytes,
            "would_raise_disk_error": (needed_download and total_download_bytes > 0),
        }


class TestCacheAwarePreflight:
    def test_fully_cached_model_does_not_require_disk(self):
        """MiniMax case: 131 GB weights cached, only 36 GB free; preflight must not raise."""
        shards = [(f"UD-Q4_K_XL/shard-{i}.gguf", 35 * GIB) for i in range(4)]
        cached = {name: size for name, size in shards}
        out = _preflight(
            repo_files = shards,
            cached_files = cached,
            free_bytes = 36 * GIB,
        )
        assert out["total_download_bytes"] == 0
        assert out["already_cached_bytes"] == 140 * GIB
        assert out["would_raise_disk_error"] is False

    def test_partial_cache_only_counts_remaining_bytes(self):
        """Two of four shards cached: preflight against remaining 70 GB."""
        shards = [(f"UD-Q4_K_XL/shard-{i}.gguf", 35 * GIB) for i in range(4)]
        cached = {
            shards[0][0]: shards[0][1],
            shards[1][0]: shards[1][1],
        }
        out = _preflight(
            repo_files = shards,
            cached_files = cached,
            free_bytes = 80 * GIB,
        )
        assert out["already_cached_bytes"] == 70 * GIB
        assert out["total_download_bytes"] == 70 * GIB
        assert out["would_raise_disk_error"] is False

    def test_partial_cache_insufficient_disk_for_rest_still_raises(self):
        """Two of four shards cached; 70 GB still exceeds free disk, so preflight should raise."""
        shards = [(f"UD-Q4_K_XL/shard-{i}.gguf", 35 * GIB) for i in range(4)]
        cached = {
            shards[0][0]: shards[0][1],
            shards[1][0]: shards[1][1],
        }
        out = _preflight(
            repo_files = shards,
            cached_files = cached,
            free_bytes = 50 * GIB,
        )
        assert out["total_download_bytes"] == 70 * GIB
        assert out["would_raise_disk_error"] is True

    def test_nothing_cached_preserves_existing_behavior(self):
        """Cold-cache path still compares full download vs free disk."""
        shards = [("UD-Q4_K_XL/shard-0.gguf", 40 * GIB)]
        out = _preflight(
            repo_files = shards,
            cached_files = {},
            free_bytes = 50 * GIB,
        )
        assert out["already_cached_bytes"] == 0
        assert out["total_download_bytes"] == 40 * GIB
        assert out["would_raise_disk_error"] is False

    def test_incomplete_cached_blob_is_not_credited(self):
        """A partial file on disk (e.g. an interrupted download) is not credited as cached."""
        shards = [("UD-Q4_K_XL/shard-0.gguf", 40 * GIB)]
        partial = {"UD-Q4_K_XL/shard-0.gguf": 10 * GIB}
        out = _preflight(
            repo_files = shards,
            cached_files = partial,
            free_bytes = 50 * GIB,
        )
        assert out["already_cached_bytes"] == 0
        assert out["total_download_bytes"] == 40 * GIB
        assert out["would_raise_disk_error"] is False

    def test_zero_size_path_infos_do_not_crash(self):
        """A path_info with size=0 must not be credited or break the arithmetic."""
        shards = [("mmproj.gguf", 0), ("UD-Q4_K_XL/shard-0.gguf", 40 * GIB)]
        out = _preflight(
            repo_files = shards,
            cached_files = {},
            free_bytes = 50 * GIB,
        )
        assert out["already_cached_bytes"] == 0
        assert out["total_bytes"] == 40 * GIB