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

"""One place that knows which dictation models are resident, and loads them.

The sidecars still own their processes: whisper.cpp serves GGML through
whisper-server, llama.cpp serves mtmd models, and Transformers loads in a spawn
child of its own. What lives here is the lifecycle above them, so the
orchestrator has a single view of dictation the way it has one of chat, and
Voice settings and Model Hub cannot report different things about the same model.
"""

from __future__ import annotations

import threading
from typing import Any, Optional, Sequence

from core.inference.inference_gate import holds_inference
from loggers import get_logger
from utils.gpu_memory_events import invalidates_gpu_memory as _invalidates_gpu_memory

logger = get_logger(__name__)

# Unload sweep order; only matters for logging.
STT_ENGINES = ("transformers", "gguf", "mtmd", "audiocpp")

# Serialises load-then-release so two loads on different engines cannot leave both resident.
_load_lock = threading.Lock()


def sidecar_for(engine: str) -> Any:
    """The sidecar serving ``engine``. Transformers is the catch-all."""
    if engine == "mtmd":
        from core.inference.stt_mtmd_sidecar import get_mtmd_stt_sidecar
        return get_mtmd_stt_sidecar()
    if engine == "gguf":
        from core.inference.stt_ggml_sidecar import get_ggml_stt_sidecar
        return get_ggml_stt_sidecar()
    if engine == "audiocpp":
        from core.inference.stt_audiocpp_sidecar import get_audio_cpp_stt_sidecar
        return get_audio_cpp_stt_sidecar()
    from core.inference.stt_sidecar import get_stt_sidecar

    return get_stt_sidecar()


@_invalidates_gpu_memory("stt load")
@holds_inference
def load(
    model: Optional[str],
    engine: str,
    request_cancel_event: Optional[threading.Event] = None,
    device: Optional[str] = None,
    *,
    context_length: Optional[int] = None,
    **options,
) -> None:
    """Make ``model`` resident on ``engine``, then release every idle other engine.

    ``device`` is the user's audio device preference (``auto``/``cpu``/``gpu``);
    every engine honours it, on CPU by holding the weights in system RAM instead
    of the accelerator.

    Dictation is one user-visible choice, so engines are alternatives, not slots:
    holding two at once doubles VRAM for the whole keep-alive window. An engine serving
    a request keeps its model and releases it on its own idle timer. Raises what the
    sidecar raises, before anything is released: a 409 for a model that is not
    downloaded must not cost the user the engine they were already using.
    """
    # This option belongs to the llama.cpp ASR sidecar; other engines do not accept it.
    if engine == "mtmd" and context_length is not None:
        options["context_length"] = context_length
    others = [name for name in STT_ENGINES if name != engine]
    with _load_lock:
        # Release other engines before allocating only once the checkpoint is on disk, so a switch
        # does not OOM but a 409 never costs the current engine.
        if _model_is_downloaded(engine, model):
            unload(others, wait = False)
            sidecar_for(engine).load(
                model, request_cancel_event = request_cancel_event, device = device, **options
            )
        else:
            sidecar_for(engine).load(
                model, request_cancel_event = request_cancel_event, device = device, **options
            )
            unload(others, wait = False)


def _model_is_downloaded(engine: str, model: str) -> bool:
    """Any doubt, including an import or lookup failure, answers False, so a resident engine is kept."""
    try:
        if engine == "mtmd":
            from core.inference import stt_mtmd_sidecar
            return bool(stt_mtmd_sidecar.is_model_downloaded(model))
        if engine == "gguf":
            from core.inference import stt_ggml_sidecar
            return stt_ggml_sidecar._cached_model_path(model) is not None
        if engine == "audiocpp":
            from core.inference import stt_audiocpp_sidecar

            # A missing runtime refuses the load just as surely as missing weights.
            return stt_audiocpp_sidecar.is_available() and stt_audiocpp_sidecar.is_model_downloaded(
                model
            )
        from core.inference import stt_sidecar

        return (
            stt_sidecar._find_complete_cached_snapshot(stt_sidecar.resolve_model_id(model))
            is not None
        )
    except Exception:  # noqa: BLE001 - a probe must never fail the load it precedes
        return False


@_invalidates_gpu_memory("stt unload")
def unload(
    engines: Optional[Sequence[str]] = None,
    *,
    wait: bool = True,
    expected_model: Optional[str] = None,
) -> list[str]:
    """Release every named engine (all of them by default), reporting refusals.

    Each is attempted even after a failure: more than one can hold memory at
    once after an engine switch, so stopping early would strand the rest.
    ``wait=False`` leaves a sidecar that is mid-request resident instead of
    blocking on it, for callers releasing engines they do not own.
    ``expected_model`` releases only a sidecar still holding that model, compared
    under its own lock, so a caller that owns one model cannot tear down another
    surface's newer one.
    """
    failed: list[str] = []
    for name in STT_ENGINES if engines is None else engines:
        try:
            sidecar_for(name).unload(wait = wait, expected_model = expected_model)
        except Exception as exc:  # noqa: BLE001 - report after attempting all
            logger.warning("Failed to unload STT engine '%s': %s", name, exc)
            failed.append(name)
    return failed


def resident() -> dict:
    """Never raises: a sidecar that cannot be imported reports nothing rather than break status."""
    for engine in STT_ENGINES:
        try:
            sidecar = sidecar_for(engine)
            model = sidecar.loaded_model
            if model:
                return {
                    "model": model,
                    "engine": engine,
                    "device": sidecar.device,
                    "loading": False,
                }
            if sidecar.is_loading():
                return {
                    "model": None,
                    "engine": engine,
                    "device": None,
                    "loading": True,
                }
        except Exception as exc:  # noqa: BLE001 - one engine must not hide the rest
            logger.debug("Could not inspect STT engine '%s': %s", engine, exc)
    return {"model": None, "engine": None, "device": None, "loading": False}