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
"""Audio weights go to the accelerator or CPU RAM: auto detects, cpu forces RAM, gpu prefers the
accelerator (a failed load still retries on CPU). Values match RAG_EMBED_DEVICE
(core/rag/config.py); UNSLOTH_AUDIO_DEVICE sets the default."""
from __future__ import annotations
import os
from typing import Any, Optional
__all__ = [
"AUDIO_DEVICE_CHOICES",
"audio_device_default",
"audio_device_env_locked",
"audio_device_forces_cpu",
"mask_accelerators_for_cpu_audio",
"normalize_audio_device",
"stt_device_default",
"stt_device_forces_cpu",
]
AUDIO_DEVICE_CHOICES = ("auto", "cpu", "gpu")
# Spellings other Studio surfaces already use; names arrive from a status echo.
_CPU_ALIASES = frozenset({"cpu", "ram", "cpu_ram", "system", "system_ram"})
_GPU_ALIASES = frozenset(
{"gpu", "cuda", "rocm", "hip", "xpu", "mps", "metal", "accelerator", "accel"}
)
def normalize_audio_device(value: Optional[str]) -> str:
"""Unknown values become auto so an env or status value never fails a load; HTTP models pin canonical values, so a typo is a 422"""
text = str(value or "").strip().lower()
if not text:
return "auto"
if text in _CPU_ALIASES:
return "cpu"
if text in _GPU_ALIASES:
return "gpu"
if text == "auto":
return "auto"
return "auto"
def audio_device_default() -> str:
"""Reads UNSLOTH_AUDIO_DEVICE; does not reach GGUF TTS models, so a headless caller must send gpu_memory_mode and gpu_layers itself"""
return normalize_audio_device(os.environ.get("UNSLOTH_AUDIO_DEVICE"))
def audio_device_env_locked() -> bool:
"""Whether ``UNSLOTH_AUDIO_DEVICE`` names a device, which Settings then cannot override."""
return bool(str(os.environ.get("UNSLOTH_AUDIO_DEVICE") or "").strip())
def stt_device_default() -> str:
"""Use the saved STT choice unless the environment overrides it; TTS is unaffected."""
if audio_device_env_locked():
return audio_device_default()
try:
from utils.stt_device_settings import get_stored_stt_device
return get_stored_stt_device()
except Exception: # noqa: BLE001 - store unavailable: detect as before
return "auto"
def stt_device_forces_cpu(value: Optional[str]) -> bool:
"""``audio_device_forces_cpu`` with ``None`` read through ``stt_device_default``."""
if value is None:
return stt_device_default() == "cpu"
return normalize_audio_device(value) == "cpu"
def audio_device_forces_cpu(value: Optional[str]) -> bool:
"""None falls back to the environment default, so older callers still honour a server-wide setting."""
if value is None:
return audio_device_default() == "cpu"
return normalize_audio_device(value) == "cpu"
def audio_load_runs_on_cpu(
audio_type: Optional[str],
value: Optional[str],
model: Any = None,
) -> bool:
"""True when a native audio load of this type ends up in CPU RAM: asked for, or a GGUF
audio model on an audio.cpp runtime that only runs on the CPU (Auto cannot place it on a GPU).
A CPU-only audio.cpp package (``model``, TQ2_1) runs on the CPU too."""
if audio_device_forces_cpu(value) or getattr(model, "cpu_only", False):
return True
from core.inference.audio_cpp_models import AUDIO_CPP_AUDIO_TYPES
if audio_type not in AUDIO_CPP_AUDIO_TYPES:
return False
from core.inference.audio_cpp_server import runtime_runs_on_cpu
return runtime_runs_on_cpu()
def mask_accelerators_for_cpu_audio(env: dict) -> None:
"""Call before torch import: blank CUDA and -1 HIP visibility; an inherited ROCR mask stays, since clearing it exposes more agents; mutates env"""
env["CUDA_VISIBLE_DEVICES"] = ""
env["HIP_VISIBLE_DEVICES"] = "-1"