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.

11.1 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

"""torch_warm_in_progress covers the whole warm, not only detection, so the startup grace stays open."""

from __future__ import annotations

import json
import re
import subprocess
import sys
from pathlib import Path

_BACKEND_DIR = Path(__file__).resolve().parent.parent
_COMMANDS_RS = _BACKEND_DIR.parent / "src-tauri" / "src" / "commands.rs"


_SNIPPET = r"""
import json, os, time

# The warm is what would settle the verdict, and these tests decide by hand whether it is
# settled, so keep a real one out of the way. DEFERRED picks the kill switch on or off.
if %(deferred)s:
    os.environ["UNSLOTH_STUDIO_DISABLE_TORCH_WARM"] = "1"
else:
    os.environ.pop("UNSLOTH_STUDIO_DISABLE_TORCH_WARM", None)

import main
main._startup_ml_warmup_features = frozenset() if %(deferred)s else frozenset({"hardware"})
from fastapi import FastAPI
from fastapi.testclient import TestClient

hw = main._hw_module

# The real warm imports the ML stack, which is the cost these tests exist to keep off the
# route, so drive its published state by hand instead. WARM picks which of the four states
# the thread can be observed in.
import utils.torch_warmup as tw

class _FakeWarmThread:
    def __init__(self, alive):
        self._alive = alive
    def is_alive(self):
        return self._alive

# started / finished / thread, per state:
#   running  -- mid-stage, the state the startup grace is for
#   finished -- every stage done, so startup really is over
#   never    -- the kill switch is on and no warm was ever started
#   retired  -- a shutdown stopped it between stages; it will never set finished
tw._status["started"], tw._status["finished"], tw._thread = {
    "running": (True, False, _FakeWarmThread(True)),
    "finished": (True, True, _FakeWarmThread(False)),
    "never": (False, False, None),
    "retired": (True, False, _FakeWarmThread(False)),
}[%(warm)r]

if %(settled)s:
    hw.DEVICE = hw.DeviceType.CPU
    hw.CHAT_ONLY = True
    hw.CHAT_ONLY_REASON = "no_accelerator"
    hw.DETECTION_COMPLETE.set()
else:
    # "detection has not run": DEVICE alone is not the completion signal, the branches
    # assign it partway through, so clear the event too.
    hw.DEVICE = None
    hw.CHAT_ONLY = True
    hw.CHAT_ONLY_REASON = None
    hw.DETECTION_COMPLETE.clear()

def must_not_run(*_):
    raise AssertionError("liveness started hardware detection")

hw.ensure_hardware_detected = must_not_run

app = FastAPI()
app.add_api_route("/api/liveness", main.liveness_check, methods = ["GET"])

# A route that does nothing, served by the same app through the same TestClient. The
# claim here is "liveness costs about what answering costs", and a bare answer is the
# only honest zero: everything a runner charges the real probe, it charges this too.
async def _nothing():
    return {"ok": True}

app.add_api_route("/api/nothing", _nothing, methods = ["GET"])
client = TestClient(app)

# Interleaved, and best of three on each side. Taken in one batch before the measured
# request, a control shares no scheduler delay with it, so a pause that lands on the
# measurement alone is not divided out. Alternating them gives each side the same chance
# of being unlucky, and a minimum over three is not moved by a pause that has to hit all
# three to count.
_controls, _mine, response = [], [], None
for _ in range(3):
    _c0 = time.perf_counter()
    client.get("/api/nothing")
    _controls.append(time.perf_counter() - _c0)
    _m0 = time.perf_counter()
    response = client.get("/api/liveness")
    _mine.append(time.perf_counter() - _m0)
control = min(_controls)
elapsed = min(_mine)
body = response.json()

print("RESULT" + json.dumps({
    "status_code": response.status_code,
    "elapsed": elapsed,
    "control": control,
    "status": body.get("status"),
    "service": body.get("service"),
    "hardware_detecting": body.get("hardware_detecting"),
    "hardware_detection_deferred": body.get("hardware_detection_deferred"),
    "has_detecting_key": "hardware_detecting" in body,
    "torch_warm_in_progress": body.get("torch_warm_in_progress"),
    "has_warm_key": "torch_warm_in_progress" in body,
    "studio_root_id": body.get("studio_root_id"),
    "has_root_id": "studio_root_id" in body,
}))
"""


def _probe(
    settled: bool,
    deferred: bool = False,
    warm: str = "running",
) -> dict:
    proc = subprocess.run(
        [
            sys.executable,
            "-c",
            _SNIPPET % {"settled": settled, "deferred": deferred, "warm": warm},
        ],
        cwd = str(_BACKEND_DIR),
        capture_output = True,
        text = True,
        timeout = 900,
    )
    assert (
        proc.returncode == 0
    ), f"probe failed\nstdout:\n{proc.stdout}\nstderr:\n{proc.stderr[-4000:]}"
    line = next(ln for ln in proc.stdout.splitlines() if ln.startswith("RESULT"))
    return json.loads(line[len("RESULT") :])


def test_an_unsettled_verdict_is_published_as_still_warming_up():
    """Unsettled warm-ups must report warming up, or the watchdog ends startup grace on the first reply."""
    result = _probe(settled = False)

    assert result["status_code"] == 200
    assert result["status"] == "alive"
    assert result["service"] == "Unsloth UI Backend"
    assert result["torch_warm_in_progress"] is True
    assert result["hardware_detecting"] is True
    assert result["hardware_detection_deferred"] is None, (
        "the warm is running here; a deferred marker would tell the watchdog the verdict "
        "will never settle"
    )


def test_a_late_warm_stage_still_holds_the_startup_grace_open():
    """Detection is only the first warm stage, so hardware_detecting alone would end the grace too early."""
    result = _probe(settled = True, warm = "running")

    assert not result["has_detecting_key"], (
        "the point of this case is a settled verdict; if detection still reads unsettled "
        "the late-stage window is not being modelled"
    )
    assert result["torch_warm_in_progress"] is True, (
        "liveness reports startup finished while the warm is still importing transformers, "
        "datasets and unsloth_zoo; the watchdog ends its grace and the next GIL stall kills "
        "a backend that is starting normally"
    )


def test_a_settled_verdict_carries_no_warming_up_marker():
    """The markers are the whole signal, so they have to disappear once the warm is done, or
    the watchdog never counts a real failure until the grace period expires."""
    result = _probe(settled = True, warm = "finished")

    assert result["status"] == "alive"
    assert not result["has_detecting_key"], (
        "liveness still reports hardware_detecting after detection settled; the desktop "
        "watchdog would hold its startup grace open for the full 5 minutes"
    )
    assert not result["has_warm_key"], (
        "liveness still reports torch_warm_in_progress after every stage finished; the "
        "watchdog would never count a failure against a genuinely hung backend"
    )


def test_a_deferred_warm_says_so_instead_of_looking_like_a_slow_start():
    """With UNSLOTH_STUDIO_DISABLE_TORCH_WARM=1 nothing will settle the verdict, so a bare
    hardware_detecting would be indistinguishable from a backend still importing torch."""
    result = _probe(settled = False, deferred = True, warm = "never")

    assert result["hardware_detecting"] is True
    assert result["hardware_detection_deferred"] is True
    assert not result["has_warm_key"], (
        "no warm was started, so nothing will ever finish one; a warm marker here would "
        "hold the watchdog's startup grace open until it expired on its own"
    )


def test_a_warm_retired_mid_stage_is_not_reported_as_warming_forever():
    """A retired warm never sets finished, so the marker must also check that its thread has exited."""
    result = _probe(settled = False, warm = "retired")

    assert not result["has_warm_key"], (
        "a warm whose thread has exited is reported as still running; the watchdog would "
        "hold its startup grace open for the full 5 minutes and never trust the backend"
    )


def _watchdog_probe_budget_s() -> float:
    """Reads HEALTH_PROBE_TIMEOUT from the Rust launcher so the liveness ceiling cannot drift past it."""
    assert _COMMANDS_RS.is_file(), f"{_COMMANDS_RS} moved; update this guard"
    match = re.search(
        r"const HEALTH_PROBE_TIMEOUT: Duration = Duration::from_secs\((\d+)\)",
        _COMMANDS_RS.read_text(encoding = "utf-8"),
    )
    assert match, "commands.rs no longer sets a whole-seconds probe timeout"
    return float(match.group(1))


def test_liveness_answers_immediately_and_never_starts_detection():
    """The route exists because health's detection wait is too expensive to probe every
    15s. The stub raises if detection is started, so returning at all proves it was not."""
    result = _probe(settled = False)

    relative = max(result["control"] * 40, 0.05)
    ceiling = min(relative, _watchdog_probe_budget_s() / 2)
    assert result["elapsed"] < ceiling, (
        f"/api/liveness took {result['elapsed'] * 1000:.1f}ms against "
        f"{result['control'] * 1000:.1f}ms to answer a route that does nothing, so it was "
        "waiting on something rather than reading the snapshot"
    )
    # check has_root_id: `result` always has the key via body.get(), so `in result` proves nothing
    assert result["has_root_id"], (
        "/api/liveness dropped studio_root_id; desktop_backend_owner.rs deserializes it "
        "into DesktopLiveness and rejects a sibling port without it"
    )


def test_the_desktop_watchdog_still_reads_these_fields():
    """Guard: commands.rs reads this marker, and either side can change without the other."""
    assert _COMMANDS_RS.is_file(), f"{_COMMANDS_RS} moved; update this guard"
    rust = _COMMANDS_RS.read_text(encoding = "utf-8")
    probe = rust[rust.index("async fn check_health_inner") :]
    end = probe.find("\n}\n")
    if end != -1:
        probe = probe[: end + 3]

    assert '"/api/liveness"' in probe, (
        "the watchdog probe no longer asks for /api/liveness; it must not go back to "
        "/api/health, which awaits hardware detection"
    )
    assert '"torch_warm_in_progress"' in probe, (
        "the watchdog probe no longer reads torch_warm_in_progress, so one early reply ends "
        "the startup grace again and a GIL stall can kill a healthy backend"
    )
    assert '"hardware_detecting"' in probe, (
        "the watchdog probe dropped its hardware_detecting fallback; a backend older than "
        "torch_warm_in_progress then gets no startup grace at all"
    )
    assert '"hardware_detection_deferred"' in probe

    match = re.search(r"const HEALTH_PROBE_TIMEOUT: Duration = Duration::from_secs\((\d+)\)", rust)
    assert match, "commands.rs no longer sets a whole-seconds probe timeout"
    interval = re.search(
        r"const HEALTH_WATCHDOG_INTERVAL: Duration = Duration::from_secs\((\d+)\)", rust
    )
    assert interval, "commands.rs no longer sets a whole-seconds watchdog interval"
    assert int(match.group(1)) < int(interval.group(1)), (
        f"a {match.group(1)}s probe outlives the {interval.group(1)}s watchdog interval, "
        f"so the next tick starts on top of the last one"
    )