Files
unslothai--unsloth/studio/backend/tests/test_resolve_quant_gguf.py
T
wehub-resource-sync e93507a09c
Lockfile supply-chain audit / lockfile supply-chain audit (push) Has been cancelled
Windows Studio GGUF CI / GPU prebuilt resolves without Visual Studio (push) Has been cancelled
Windows Studio GGUF CI / setup.ps1 unit tests (VS 2026 / CMake guard) (push) Has been cancelled
Windows Studio GGUF CI / real-VS detection (VS 2022) (push) Has been cancelled
Windows Studio GGUF CI / real-VS detection (VS 2026) (push) Has been cancelled
Windows Studio GGUF CI / VC++ runtime detect + install round-trip (windows-2025-vs2026) (push) Has been cancelled
Windows Studio GGUF CI / VC++ runtime detect + install round-trip (windows-latest) (push) Has been cancelled
Windows Studio Update CI / Studio Updating Tests (push) Has been cancelled
Wheel CI / Wheel build + content sanity + import smoke (push) Has been cancelled
Lint CI / Source lint (Python + shell + YAML + JSON + safety nets) (push) Has been cancelled
MLX CI on Mac M1 / dispatch (push) Has been cancelled
Security audit / advisory audit (pip + npm + cargo) (push) Has been cancelled
Security audit / pip scan-packages :: extras (push) Has been cancelled
Security audit / pip scan-packages :: studio (push) Has been cancelled
Security audit / pip scan-packages :: hf-stack (push) Has been cancelled
Security audit / npm scan-packages (Studio frontend tarballs) (push) Has been cancelled
Security audit / workflow-trigger lint (pull_request_target / cache-poisoning) (push) Has been cancelled
Security audit / pytest tests/security (push) Has been cancelled
Security audit / npm provenance + new install-script diff (push) Has been cancelled
Studio API CI / Studio API & Auth Tests (push) Has been cancelled
Backend CI / (Python 3.10) (push) Has been cancelled
Backend CI / (Python 3.11) (push) Has been cancelled
Backend CI / (Python 3.12) (push) Has been cancelled
Backend CI / (Python 3.13) (push) Has been cancelled
Backend CI / Repo tests (CPU) (push) Has been cancelled
Frontend CI / Frontend build + bundle sanity (push) Has been cancelled
Studio GGUF CI / OpenAI, Anthropic API tests (push) Has been cancelled
Studio GGUF CI / Tool calling Tests (push) Has been cancelled
Studio GGUF CI / JSON, images (push) Has been cancelled
Mac Studio GGUF CI / OpenAI, Anthropic API tests (push) Has been cancelled
Mac Studio GGUF CI / Tool calling Tests (push) Has been cancelled
Mac Studio GGUF CI / JSON, images (push) Has been cancelled
Mac Studio Install Matrix CI / Install + load (macos-14) (push) Has been cancelled
Mac Studio Install Matrix CI / Install + load (macos-15) (push) Has been cancelled
Mac Studio Install Matrix CI / Install + load (macos-26) (push) Has been cancelled
Mac Studio Install Matrix CI / Install + load (macos-15-intel) (push) Has been cancelled
Mac Studio API CI / Studio API & Auth Tests (push) Has been cancelled
Mac Studio Install Matrix CI / Install + load (macos-26-intel) (push) Has been cancelled
Mac Studio UI CI / Chat UI Tests (push) Has been cancelled
Studio Tauri CI / Tauri Linux debug build (no codesign) (push) Has been cancelled
Mac Studio Update CI / Studio Updating Tests (push) Has been cancelled
Studio UI CI / Chat UI Tests (push) Has been cancelled
Windows Studio API CI / Studio API & Auth Tests (push) Has been cancelled
Windows Studio UI CI / Chat UI Tests (push) Has been cancelled
Studio Update CI / Studio Updating Tests (push) Has been cancelled
Core / Core (HF=default + TRL=default) (push) Has been cancelled
Core / Core (HF=4.57.6 + TRL<1) (push) Has been cancelled
Core / Core (HF=latest + TRL=latest) (push) Has been cancelled
Core / llama.cpp build + smoke (push) Has been cancelled
Windows Studio GGUF CI / OpenAI, Anthropic API tests (push) Has been cancelled
Windows Studio GGUF CI / Tool calling Tests (push) Has been cancelled
Windows Studio GGUF CI / JSON, images (push) Has been cancelled
Windows Studio GGUF CI / Studio install + inference without Visual Studio (push) Has been cancelled
Studio export capability / capability (macos-latest) (push) Has been cancelled
Studio export capability / capability (ubuntu-latest) (push) Has been cancelled
Studio export capability / capability (windows-latest) (push) Has been cancelled
Cross-platform parity / parity (macos-latest) (push) Has been cancelled
Cross-platform parity / parity (windows-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
Studio load-orchestrator CI / test (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:59:56 +08:00

99 lines
3.4 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
"""Tests for :func:`routes.models._resolve_quant_gguf` (PR #6364 follow-up).
The /kv-cache-estimate resolver must mirror list_local_gguf_variants:
- read the quant label from the snapshot-relative path so nested layouts like
``BF16/model.gguf`` resolve (not just basenames),
- skip MTP drafter files so a ``...-Q8_0-MTP.gguf`` drafter is never returned as
the Q8_0 weights, and
- when several cache snapshots hold the quant, pick the most complete (largest
total) so a partial older revision can't underestimate the weight bytes.
No GPU/network. The resolver only stats sizes and parses file names, so the
GGUF files can be arbitrary bytes.
"""
from __future__ import annotations
import sys
import types
from pathlib import Path
# Keep this test runnable without optional logging deps (mirrors
# test_cached_gguf_routes.py).
if "structlog" not in sys.modules:
class _DummyLogger:
def __getattr__(self, _name):
return lambda *args, **kwargs: None
sys.modules["structlog"] = types.SimpleNamespace(
BoundLogger = _DummyLogger,
get_logger = lambda *args, **kwargs: _DummyLogger(),
)
import routes.models as models_route
def _write(path: Path, size: int) -> Path:
path.parent.mkdir(parents = True, exist_ok = True)
path.write_bytes(b"\0" * size)
return path
def test_resolves_quant_from_parent_directory_layout(tmp_path):
# A repo that puts the quant label in a parent dir (BF16/model.gguf).
root = tmp_path / "repo"
f = _write(root / "BF16" / "model.gguf", 1234)
path, total = models_route._resolve_quant_gguf(str(root), "BF16", is_local = True)
assert path == str(f)
assert total == 1234
def test_skips_mtp_drafter_for_main_weights(tmp_path):
# Main Q8_0 weights next to a same-quant MTP drafter that sorts first by name.
root = tmp_path / "repo"
main = _write(root / "model-Q8_0.gguf", 100)
_write(root / "MTP" / "model-Q8_0-MTP.gguf", 50)
path, total = models_route._resolve_quant_gguf(str(root), "Q8_0", is_local = True)
assert path == str(main)
# Drafter bytes are excluded from the weight total.
assert total == 100
def test_prefers_the_complete_snapshot(tmp_path, monkeypatch):
from huggingface_hub import constants as hf_constants
cache = tmp_path / "hub"
snaps = cache / "models--org--repo" / "snapshots"
# Partial older snapshot: one small shard.
_write(snaps / "aaaa" / "model-Q4_K_M.gguf", 10)
# Complete newer snapshot: two larger shards.
complete_first = _write(snaps / "bbbb" / "model-00001-of-00002-Q4_K_M.gguf", 30)
_write(snaps / "bbbb" / "model-00002-of-00002-Q4_K_M.gguf", 40)
monkeypatch.setattr(hf_constants, "HF_HUB_CACHE", str(cache))
path, total = models_route._resolve_quant_gguf("org/repo", "Q4_K_M", is_local = False)
# The most complete snapshot (70 bytes) wins over the partial one (10).
assert total == 70
# Shard 1 (metadata) of the complete snapshot is returned.
assert path == str(complete_first)
def test_returns_none_when_quant_absent(tmp_path):
root = tmp_path / "repo"
_write(root / "model-Q4_K_M.gguf", 100)
path, total = models_route._resolve_quant_gguf(str(root), "Q8_0", is_local = True)
assert path is None
assert total == 0