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.

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

"""Tokenizer-based audio_type; lives in utils so the cache scanner avoids importing utils/models"""

from __future__ import annotations

import json
import os
import stat
import threading
from collections import OrderedDict
from pathlib import Path
from typing import Optional

NATIVE_OUTPUT_AUDIO_TYPES = frozenset(
    {
        "higgs_tts2",
        "moss_tts_local",
        "moss_tts_nano",
        "higgs_tts3",
        "minimax_music3",
    }
)

VALID_AUDIO_TYPES = (
    "snac",
    "csm",
    "bicodec",
    "dac",
    *sorted(NATIVE_OUTPUT_AUDIO_TYPES),
    "whisper",
    "audio_vlm",
)

# Emit speech; a chat turn sent to one comes back as audio, never as text.
TTS_AUDIO_TYPES = frozenset({"snac", "csm", "bicodec", "dac"})
GGUF_TTS_AUDIO_TYPES = frozenset({"snac", "bicodec", "dac"})
OUTPUT_AUDIO_TYPES = TTS_AUDIO_TYPES | NATIVE_OUTPUT_AUDIO_TYPES


def _count_prefix_exceeds(tokens, prefix: str, threshold: int) -> bool:
    """``sum(...) > threshold``, but stopping at the answer: summing counted all 28k of
    Orpheus's codes to settle a question the first 10,001 decide."""
    count = 0
    for token in tokens:
        if token.startswith(prefix):
            count += 1
            if count > threshold:
                return True
    return False


# ORDER MATTERS: first match wins, so codec fingerprints precede audio_vlm; Orpheus has a stray <|audio|> and would be typed audio-input.
AUDIO_TOKEN_PATTERNS = {
    "csm": lambda tokens: "<|AUDIO|>" in tokens and "<|audio_eos|>" in tokens,
    "whisper": lambda tokens: "<|startoftranscript|>" in tokens,
    "bicodec": lambda tokens: any(t.startswith("<|bicodec_") for t in tokens),
    "dac": lambda tokens: (
        "<|audio_start|>" in tokens
        and "<|audio_end|>" in tokens
        and "<|text_start|>" in tokens
        and "<|text_end|>" in tokens
    ),
    "snac": lambda tokens: _count_prefix_exceeds(tokens, "<custom_token_", 10000),
    # Generic, so last. Gemma 3n <audio_soft_token>; Gemma 4 <|audio|>, not csm's <|AUDIO|>.
    "audio_vlm": lambda tokens: "<audio_soft_token>" in tokens or "<|audio|>" in tokens,
}

# Every substring a pattern needs so text with none skips parsing; a codec missing its marker here goes undetected (test_audio_token_detection.py).
AUDIO_TOKEN_MARKERS = (
    "<|AUDIO|>",
    "<|startoftranscript|>",
    "<|bicodec_",
    "<|audio_start|>",
    "<custom_token_",
    "<audio_soft_token>",
    "<|audio|>",
)

AUDIO_TOKENIZER_CONFIG_PATHS = (
    "tokenizer_config.json",
    "LLM/tokenizer_config.json",
    # transformers 5 tokenizer_configs drop added_tokens_decoder (LFM3-Omni); the processor still names its audio_token.
    "processor_config.json",
)

# A codebook tokenizer runs to a few MB, and the inventory scan reads one per repo.
_MAX_TOKENIZER_CONFIG_BYTES = 32 * 1024 * 1024


def may_hold_audio_tokens(raw: str) -> bool:
    """Whether a tokenizer_config's raw text is worth parsing. A false True costs only
    the parse that would have happened anyway; a false False misclassifies."""
    # '"audio_token"' too: a JSON-escaped placeholder hides the markers from this raw scan.
    return '"audio_token"' in raw or any(marker in raw for marker in AUDIO_TOKEN_MARKERS)


def declares_audio_token(tok_config: dict) -> bool:
    """Whether a transformers 5 tokenizer_config names an audio placeholder in its top-level
    audio_token (Gemma 4) instead of added_tokens_decoder."""
    token = tok_config.get("audio_token")
    return isinstance(token, str) and bool(token)


def classify_audio_tokens(tok_config: dict) -> Optional[str]:
    """The audio_type a parsed tokenizer_config fingerprints, or None."""
    added = tok_config.get("added_tokens_decoder") or {}
    token_contents = [value.get("content", "") for value in added.values()]
    # Only beside a processor that takes input: LFM3-Omni's image processor, LFM3-Audio's feature extractor.
    if isinstance(tok_config.get("audio_token"), str) and (
        tok_config.get("image_processor") or tok_config.get("feature_extractor")
    ):
        token_contents.append(tok_config["audio_token"])
    if not token_contents:
        return None
    for audio_type, check_fn in AUDIO_TOKEN_PATTERNS.items():
        if check_fn(token_contents):
            return audio_type
    return None


# Keep token order and strictness aligned with LlamaCppBackend._detect_audio_type_strict.
SNAC_PROBE_TOKEN_IDS = (128258, 128259)

GGUF_AUDIO_CLASSIFIER_TOKENS = frozenset(
    {
        "<|AUDIO|>",
        "<|audio_eos|>",
        "<|startoftranscript|>",
        "<audio_soft_token>",
        "<|audio|>",
        "<|bicodec_semantic_0|>",
        "<|bicodec_global_0|>",
        "<|c1_0|>",
        "<|c2_0|>",
    }
)


def classify_gguf_vocab_audio_type(tokens: set, snac_probe_is_codes: bool) -> Optional[str]:
    if snac_probe_is_codes:
        return "snac"
    if "<|AUDIO|>" in tokens and "<|audio_eos|>" in tokens:
        return "csm"
    if "<|startoftranscript|>" in tokens:
        return "whisper"
    # Match the serving detector: audio-input markers take precedence over codecs.
    if "<audio_soft_token>" in tokens or "<|audio|>" in tokens:
        return "audio_vlm"
    if "<|bicodec_semantic_0|>" in tokens and "<|bicodec_global_0|>" in tokens:
        return "bicodec"
    if "<|c1_0|>" in tokens and "<|c2_0|>" in tokens:
        return "dac"
    return None


def is_audio_input_type(audio_type: Optional[str]) -> bool:
    """True if an audio_type accepts audio input: whisper (ASR), audio_vlm (Gemma3n)."""
    return audio_type in ("whisper", "audio_vlm")


def is_tts_audio_type(audio_type: Optional[str]) -> bool:
    """True for a speech-emitting codec. audio_vlm is absent on purpose: Gemma 3n takes
    audio in and answers in text."""
    return audio_type in TTS_AUDIO_TYPES


def is_output_audio_type(audio_type: Optional[str]) -> bool:
    """True for a model that emits audio instead of a text chat response."""
    return audio_type in OUTPUT_AUDIO_TYPES


def detect_local_tts_audio_type(directory) -> Optional[str]:
    """The TTS codec a downloaded model directory fingerprints, or None. Local files
    only. Whisper and audio_vlm answer None: both of those chat."""
    try:
        root = Path(directory)
        if not root.is_dir():
            return None
    except OSError:
        return None
    for tok_path in AUDIO_TOKENIZER_CONFIG_PATHS:
        tok_file = root / tok_path
        try:
            st = tok_file.stat()
            if not stat.S_ISREG(st.st_mode) or st.st_size > _MAX_TOKENIZER_CONFIG_BYTES:
                continue
        except Exception:
            continue
        audio_type = _tokenizer_config_audio_type(tok_file, st)
        if is_tts_audio_type(audio_type):
            return audio_type
    return None


# Kept per file version: a rewrite moves mtime/ctime/size/inode and misses.
_TOKENIZER_AUDIO_TYPE_CACHE: "OrderedDict[tuple, Optional[str]]" = OrderedDict()
_TOKENIZER_AUDIO_TYPE_CACHE_MAX = 4096
_TOKENIZER_AUDIO_TYPE_LOCK = threading.Lock()


def _tokenizer_config_audio_type(tok_file: Path, st: os.stat_result) -> Optional[str]:
    key = (
        os.fspath(tok_file),
        st.st_dev,
        st.st_ino,
        st.st_size,
        st.st_mtime_ns,
        st.st_ctime_ns,
    )
    with _TOKENIZER_AUDIO_TYPE_LOCK:
        if key in _TOKENIZER_AUDIO_TYPE_CACHE:
            _TOKENIZER_AUDIO_TYPE_CACHE.move_to_end(key)
            return _TOKENIZER_AUDIO_TYPE_CACHE[key]
    try:
        raw = tok_file.read_text(encoding = "utf-8-sig")
        audio_type = classify_audio_tokens(json.loads(raw)) if may_hold_audio_tokens(raw) else None
    except OSError:
        # Not cached: a failed read (EMFILE, EIO, a Windows sharing lock) can clear with no change to the file's stat.
        return None
    except Exception:
        # Cached too: a writer that finishes the file moves its mtime or size, so this key misses.
        audio_type = None
    with _TOKENIZER_AUDIO_TYPE_LOCK:
        _TOKENIZER_AUDIO_TYPE_CACHE[key] = audio_type
        while len(_TOKENIZER_AUDIO_TYPE_CACHE) > _TOKENIZER_AUDIO_TYPE_CACHE_MAX:
            _TOKENIZER_AUDIO_TYPE_CACHE.popitem(last = False)
    return audio_type