Files
unslothai--unsloth/studio/backend/tests/test_trained_model_scan.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

179 lines
6.8 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 Studio trained-model discovery used by Chat."""
import json
from pathlib import Path
import sys
import types as _types
import importlib
_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)
from unittest.mock import patch
from utils.models.model_config import (
ModelConfig,
get_base_model_from_checkpoint,
get_base_model_from_lora,
get_base_model_from_lora_identifier,
scan_trained_models,
)
def test_scan_trained_models_includes_lora_and_full_finetune_outputs(tmp_path: Path, monkeypatch):
# resolve_output_dir refuses absolutes outside outputs_root; point it at tmp_path.
from utils.models import model_config as _mc
from utils.paths import storage_roots as _sr
monkeypatch.setattr(_sr, "outputs_root", lambda: tmp_path)
monkeypatch.setattr(_mc, "outputs_root", lambda: tmp_path)
lora_dir = tmp_path / "unsloth_SmolLM-135M_1775412608"
lora_dir.mkdir()
(lora_dir / "adapter_config.json").write_text(
json.dumps({"base_model_name_or_path": "HuggingFaceTB/SmolLM-135M"})
)
(lora_dir / "adapter_model.safetensors").write_bytes(b"")
full_dir = tmp_path / "unsloth_SmolLM-135M_full_1775412609"
full_dir.mkdir()
(full_dir / "config.json").write_text(
json.dumps({"_name_or_path": "HuggingFaceTB/SmolLM-135M"})
)
(full_dir / "model.safetensors").write_bytes(b"")
found = {
name: (path, model_type) for name, path, model_type in scan_trained_models(str(tmp_path))
}
assert found[lora_dir.name] == (str(lora_dir), "lora")
assert found[full_dir.name] == (str(full_dir), "merged")
def test_get_base_model_from_checkpoint_falls_back_to_full_finetune_config(tmp_path: Path):
(tmp_path / "config.json").write_text(
json.dumps({"_name_or_path": "HuggingFaceTB/SmolLM-135M"})
)
(tmp_path / "model.safetensors").write_bytes(b"")
assert get_base_model_from_checkpoint(str(tmp_path)) == "HuggingFaceTB/SmolLM-135M"
def test_get_base_model_from_lora_rejects_full_finetune_dirs(tmp_path: Path):
(tmp_path / "config.json").write_text(
json.dumps({"_name_or_path": "HuggingFaceTB/SmolLM-135M"})
)
(tmp_path / "model.safetensors").write_bytes(b"")
assert get_base_model_from_lora(str(tmp_path)) is None
def test_lora_identifier_resolves_local_dir_like_the_local_helper(tmp_path: Path):
# Local path: behaves like the directory reader, no Hub call.
(tmp_path / "adapter_config.json").write_text(
json.dumps({"base_model_name_or_path": "HuggingFaceTB/SmolLM-135M"})
)
(tmp_path / "adapter_model.safetensors").write_bytes(b"")
with patch("huggingface_hub.hf_hub_download", side_effect = AssertionError("no Hub call")):
assert get_base_model_from_lora_identifier(str(tmp_path)) == "HuggingFaceTB/SmolLM-135M"
def test_lora_identifier_resolves_remote_adapter_base(tmp_path: Path):
# Remote adapter: the identifier helper fetches adapter_config.json from the Hub so
# the gate can scan the base, where the local helper returns None.
cfg = tmp_path / "adapter_config.json"
cfg.write_text(json.dumps({"base_model_name_or_path": "unsloth/Llama-3.2-1B-Instruct"}))
def _dl(
repo,
fn,
token = None,
):
assert repo == "someone/my-remote-lora"
assert fn == "adapter_config.json"
return str(cfg)
assert get_base_model_from_lora("someone/my-remote-lora") is None # local-only: misses it
with patch("huggingface_hub.hf_hub_download", side_effect = _dl):
base = get_base_model_from_lora_identifier("someone/my-remote-lora")
assert base == "unsloth/Llama-3.2-1B-Instruct"
def test_lora_identifier_returns_none_for_non_adapter_remote_repo():
# Non-LoRA remote repo: a 404 on adapter_config.json returns None without retrying.
from huggingface_hub.utils import EntryNotFoundError
mock = patch("huggingface_hub.hf_hub_download", side_effect = EntryNotFoundError("404"))
with mock as m:
assert get_base_model_from_lora_identifier("unsloth/Llama-3.2-1B-Instruct") is None
assert m.call_count == 1 # 404 is definitive -> no retry
def test_lora_identifier_retries_transient_then_resolves(tmp_path: Path):
# A transient error is retried (not treated as "not a LoRA"); the retry resolves the base.
cfg = tmp_path / "adapter_config.json"
cfg.write_text(json.dumps({"base_model_name_or_path": "unsloth/Llama-3.2-1B-Instruct"}))
calls = {"n": 0}
def _dl(
repo,
fn,
token = None,
):
calls["n"] += 1
if calls["n"] == 1:
raise RuntimeError("transient network blip")
return str(cfg)
with patch("huggingface_hub.hf_hub_download", side_effect = _dl):
base = get_base_model_from_lora_identifier("someone/remote-lora")
assert base == "unsloth/Llama-3.2-1B-Instruct"
assert calls["n"] == 2 # retried once
def test_lora_identifier_persistent_transient_returns_none():
# Two transient errors -> None, logged at WARNING (a missed base is gated by neither).
# Assert on the logger directly: robust to the logging backend (structlog vs stub).
from utils.models import model_config as _mc
with (
patch("huggingface_hub.hf_hub_download", side_effect = RuntimeError("down")),
patch.object(_mc.logger, "warning") as mock_warn,
):
assert get_base_model_from_lora_identifier("someone/remote-lora") is None
assert any(
"Could not resolve remote LoRA base" in str(c.args[0]) for c in mock_warn.call_args_list
)
@patch("utils.models.model_config.is_audio_input_type", return_value = False)
@patch("utils.models.model_config.detect_audio_type", return_value = None)
@patch("utils.models.model_config.is_vision_model", return_value = False)
def test_model_config_full_finetune_local_path_is_not_lora(
_mock_vision, _mock_audio_type, _mock_audio_input, tmp_path: Path
):
(tmp_path / "config.json").write_text(json.dumps({"_name_or_path": "unsloth/Qwen3-4B"}))
(tmp_path / "model.safetensors").write_bytes(b"")
config = ModelConfig.from_identifier(str(tmp_path))
assert config is not None
assert config.is_lora is False
assert config.base_model is None
def test_scan_trained_loras_aliases_scan_trained_models():
utils_models = importlib.import_module("utils.models")
core_module = importlib.import_module("core")
assert utils_models.scan_trained_loras is utils_models.scan_trained_models
assert core_module.scan_trained_loras is core_module.scan_trained_models