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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
# 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 loaded-GGUF branch sits in a try that reports 500, so an undefined name shows only when run."""
import asyncio
import os
import pytest
import routes.inference as inference_route
from models.inference import _InferenceRuntimeFields
class _StatusBackend:
"""Runtime fields come from the response model defaults, since a None fallback fails bool validation."""
def __init__(
self,
model_identifier,
*,
native_grant_backed = None,
display_label = None,
):
for name, value in _InferenceRuntimeFields().model_dump().items():
setattr(self, name, value)
self.model_identifier = model_identifier
self.is_loaded = True
if native_grant_backed is not None:
self._native_grant_backed = native_grant_backed
if display_label is not None:
self._native_display_label = display_label
self.is_vision = False
self.is_diffusion = False
self.supports_reasoning = False
self.reasoning_always_on = False
self.supports_preserve_thinking = False
self.supports_tools = False
self.reasoning_style = "enable_thinking"
self.reasoning_effort_levels = []
self.requested_parallel_slots = 1
self.effective_parallel_slots = 1
self._is_audio = False
self._has_audio_input = False
self.tensor_parallel = False
self.gpu_memory_mode = "auto"
self.gpu_layers = 0
self.n_cpu_moe = 0
self.n_moe_layers = 0
def __getattr__(self, name):
# Not a MagicMock on purpose: a bool or int field must fail validation, not pass.
if name.startswith("__"):
raise AttributeError(name)
return None
@pytest.fixture
def status_route(monkeypatch):
"""The handler with its filesystem and config lookups stubbed out."""
monkeypatch.setattr(inference_route, "load_inference_config", lambda *a, **k: None)
monkeypatch.setattr(
inference_route, "resolve_effective_chat_template_override", lambda **k: None
)
def _run(backend):
monkeypatch.setattr(inference_route, "get_llama_cpp_backend", lambda: backend)
return asyncio.run(inference_route.get_status("tester"))
return _run
def test_a_loaded_repo_gguf_reports_its_public_id(status_route):
status = status_route(_StatusBackend("org/A-GGUF", native_grant_backed = False))
assert status.is_gguf is True
assert status.active_model == "org/A-GGUF"
assert status.model_identifier == "org/A-GGUF"
assert status.is_local_model is False
def test_a_backend_without_the_flag_still_reports(status_route):
backend = _StatusBackend("org/A-GGUF")
assert not hasattr(backend, "__dict__") or "_native_grant_backed" not in backend.__dict__
status = status_route(backend)
assert status.is_gguf is True
assert status.is_local_model is False
def test_a_native_lease_load_reports_the_label_not_the_leased_path(status_route):
leased = os.path.join(os.sep, "models", "private", "A-Q4_K_M.gguf")
status = status_route(_StatusBackend(leased, native_grant_backed = True))
assert status.model_identifier is None, "the leased path must not be published"
assert status.active_model == "A-Q4_K_M.gguf"
assert status.is_local_model is True
def test_a_local_path_load_without_a_lease_is_still_local(status_route):
local = os.path.join(os.sep, "models", "local", "A-Q4_K_M.gguf")
status = status_route(_StatusBackend(local, native_grant_backed = False))
assert status.is_gguf is True
assert status.is_local_model is True
def test_a_model_cached_behind_the_gguf_is_still_reported(status_route, monkeypatch):
# Loading a GGUF unloads only the active model; a cached Transformers model keeps its memory.
monkeypatch.setattr(
inference_route,
"_peek_inference_backend",
lambda: type("_Reg", (), {"models": {"org/Cached": {}}})(),
)
status = status_route(_StatusBackend("org/A-GGUF", native_grant_backed = False))
assert status.active_model == "org/A-GGUF"
assert status.loaded == ["org/A-GGUF", "org/Cached"]
def test_the_gguf_is_not_listed_twice_when_the_registry_names_it(status_route, monkeypatch):
monkeypatch.setattr(
inference_route,
"_peek_inference_backend",
lambda: type("_Reg", (), {"models": {"org/A-GGUF": {}}})(),
)
status = status_route(_StatusBackend("org/A-GGUF", native_grant_backed = False))
assert status.loaded == ["org/A-GGUF"]
def test_no_orchestrator_leaves_the_gguf_alone(status_route, monkeypatch):
monkeypatch.setattr(inference_route, "_peek_inference_backend", lambda: None)
status = status_route(_StatusBackend("org/A-GGUF", native_grant_backed = False))
assert status.loaded == ["org/A-GGUF"]
def test_the_users_chat_template_override_wins_over_the_runtime_projection(status_route):
# The field is on shared runtime fields; override it, not pass alongside (TypeError).
backend = _StatusBackend("org/A-GGUF")
backend.chat_template_override = "user-template"
status = status_route(backend)
assert status.chat_template_override == "user-template"
def test_an_auto_applied_chat_template_is_not_reported_as_the_users(status_route, monkeypatch):
# A bundled family template is not a user override; re-sending pins it on the next model.
monkeypatch.setattr(
inference_route,
"resolve_effective_chat_template_override",
lambda **k: "bundled-template",
)
backend = _StatusBackend("org/A-GGUF")
backend.chat_template_override = "bundled-template"
assert status_route(backend).chat_template_override is None
def test_a_bundled_template_on_a_snapshot_path_load_is_not_reported_as_the_users(
status_route, monkeypatch
):
from core.inference.chat_templates import resolve_effective_chat_template_override
monkeypatch.setattr(
inference_route,
"resolve_effective_chat_template_override",
resolve_effective_chat_template_override,
)
bundled = resolve_effective_chat_template_override(
model_identifier = "unsloth/gemma-4-12b-it-GGUF", user_override = None
)
backend = _StatusBackend("/scan/models--unsloth--gemma-4-12b-it-GGUF/snapshots/abc")
backend.chat_template_override = bundled
assert status_route(backend).chat_template_override is None
def test_status_publishes_the_running_pass_through_arguments(status_route):
# A tab opened mid-run never saw the load; it learns the server's args only here,
# which a rollback after a failed switch needs.
backend = _StatusBackend("org/A-GGUF")
backend.requested_extra_args = ["--numa", "distribute"]
status = status_route(backend)
assert status.requested_llama_extra_args == ["--numa", "distribute"]
def test_an_explicit_empty_list_is_not_a_missing_one(status_route):
# A model run with no extras must roll back with none, not the failed load's args.
backend = _StatusBackend("org/A-GGUF")
backend.requested_extra_args = []
assert status_route(backend).requested_llama_extra_args == []
backend.requested_extra_args = None
assert status_route(backend).requested_llama_extra_args is None
def test_status_publishes_mmproj_cpu_recovery(status_route):
backend = _StatusBackend("org/Vision-GGUF")
backend.is_vision = True
backend.mmproj_fallback_reason = "cpu_offload"
status = status_route(backend)
assert status.is_vision is True
assert status.mmproj_fallback_reason == "cpu_offload"
def test_status_publishes_the_running_load_warning(status_route):
assert status_route(_StatusBackend("org/A-GGUF")).memory_warning is None
backend = _StatusBackend("org/A-GGUF")
backend.last_load_warning = (
"Not enough disk space to download BF16 (7.5 GB needed, 7.5 GB free), "
"so Q4_1 (2.4 GB) was loaded instead."
)
assert status_route(backend).memory_warning == backend.last_load_warning
def test_status_publishes_an_explicit_text_only_mmproj_fallback(status_route):
backend = _StatusBackend("org/Vision-GGUF")
backend.is_vision = False
backend.mmproj_fallback_reason = "projector_startup_failure"
status = status_route(backend)
assert status.is_vision is False
assert status.mmproj_fallback_reason == "projector_startup_failure"
def test_the_diffusion_runner_reports_none(status_route):
backend = _StatusBackend("org/A-GGUF")
backend.is_diffusion = True
backend.requested_extra_args = ["--numa", "distribute"]
assert status_route(backend).requested_llama_extra_args is None