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.

fix(audio): do not read an unreadable tokenizer_config.json as "not an audio model" (#8625)

Leo Borcherdinge10ed80725dd
diff --git a/studio/backend/core/training/trainer.py b/studio/backend/core/training/trainer.py
index 368f4e2a9c9..37c4b8e3569 100644
--- a/studio/backend/core/training/trainer.py
+++ b/studio/backend/core/training/trainer.py
@@ -68,7 +68,7 @@ from utils.third_party_source import (
import_sparktts_module,
)
-from utils.models import is_vision_model, detect_audio_type
+from utils.models import is_vision_model, detect_audio_type_checked
from utils.models.model_identity import restore_hf_cache_repo_identity
from utils.models.model_config import _env_offline
from utils.datasets import format_and_template_dataset
@@ -194,6 +194,84 @@ def _spark_tts_tokenizer_kwargs(audio_type: Optional[str], lookup_name: str) ->
return {"subfolder": "LLM"}
+# Enough rows to see past a leading null or malformed value, few enough to stay cheap on a
+# streamed dataset, where each row is pulled over the network.
+_AUDIO_SNIFF_ROWS = 16
+
+# Kept in step with detect_multimodal_dataset, so the veto answers False only about the very
+# columns that set the flag it is vetoing.
+_AUDIO_COLUMN_KEYWORDS = ("audio", "speech", "wav", "waveform", "sound")
+
+
+def _dataset_has_audio_column(dataset) -> Optional[bool]:
+ """Whether `dataset` really carries audio. None when it cannot be told.
+
+ `is_dataset_audio` comes from the format check's `is_audio`, set by a column-NAME
+ keyword match that never inspects the value, so a text dataset with a column called
+ `audio` holding prose reports audio. Refusing such a run would take away a text path
+ that works today, so the dataset itself gets the last word.
+
+ An ``Audio`` feature is audio without reading a row. Otherwise look at the values: an
+ audio column loaded from JSON/CSV is a ``Value("string")`` of paths until the
+ preprocessor casts it (``_preprocess_snac_dataset`` does exactly that), so trusting the
+ schema would call a real audio dataset textual.
+
+ None for a DatasetDict, a schema-less iterable dataset, or an unreadable row: an
+ unreadable model probe is still the likelier explanation, so the caller keeps refusing
+ unless this says False outright.
+ """
+ try:
+ from datasets.features.audio import Audio
+ except Exception: # noqa: BLE001 - datasets missing or too old to have the feature
+ return None
+
+ features = getattr(dataset, "features", None)
+ if not features:
+ return None
+ try:
+ if any(isinstance(f, Audio) for f in features.values()):
+ return True
+ except Exception: # noqa: BLE001 - a mapping that is not a features dict
+ return None
+
+ # No Audio feature, so nothing decodes here and reading rows cannot hit the
+ # missing-FFmpeg path. Several rows, not one: a JSON/CSV audio dataset can carry a null
+ # or malformed first value and real paths after it, and the preprocessors skip such a
+ # row rather than fail, so row 0 alone would call it textual.
+ #
+ # Audio-like values count anywhere, but only an audio-NAMED column can answer False: a
+ # transcript is populated in every row of a path-backed audio dataset, so counting it
+ # would report "no audio here" on the strength of the text. No evidence stays None.
+ try:
+ from utils.datasets.format_detection import (
+ _AUDIO_EXTENSIONS,
+ _is_audio_value,
+ _keyword_in_column,
+ )
+
+ def looks_like_audio(value) -> bool:
+ if _is_audio_value(value):
+ return True
+ return isinstance(value, str) and value.lower().endswith(_AUDIO_EXTENSIONS)
+
+ saw_a_usable_value = False
+ for index, row in enumerate(dataset):
+ if index >= _AUDIO_SNIFF_ROWS:
+ break
+ for name, value in row.items():
+ if looks_like_audio(value):
+ return True
+ if not any(_keyword_in_column(k, str(name)) for k in _AUDIO_COLUMN_KEYWORDS):
+ continue
+ if isinstance(value, str):
+ saw_a_usable_value = saw_a_usable_value or bool(value.strip())
+ elif value is not None:
+ saw_a_usable_value = True
+ except Exception: # noqa: BLE001 - unreadable row, empty dataset, odd row type
+ return None
+ return False if saw_a_usable_value else None
+
+
class UnslothTrainer:
"""
Unsloth Training Backend
@@ -221,6 +299,10 @@ class UnslothTrainer:
self.is_audio = False
self.is_audio_vlm = False # e.g. Gemma 3N trained on audio
self._audio_type = None # 'csm', 'whisper', 'snac', 'bicodec', 'dac'
+ # True until a probe says otherwise, so a path that never probes cannot trip the
+ # inconclusive-detection guard.
+ self._audio_type_known = True
+ self._is_dataset_audio = False
self._cuda_audio_used = False # Set once after audio CUDA preprocessing; never cleared
self._spark_tts_repo_dir = None # Spark-TTS repo path, for BiCodecTokenizer
self._spark_tts_code_dir = None
@@ -265,13 +347,17 @@ class UnslothTrainer:
if hf_token:
os.environ["HF_TOKEN"] = hf_token
- # Detect audio type (config.json only, no VRAM)
- self._audio_type = detect_audio_type(
+ # Detect audio type (config.json only, no VRAM). Checked, because an unreadable
+ # tokenizer_config.json (gated repo, offline, or a cache holding weights but not the
+ # tokenizer files) otherwise reads as "not an audio model" and sends a TTS run down
+ # the text path.
+ self._audio_type, self._audio_type_known = detect_audio_type_checked(
lookup_name,
hf_token,
local_files_only = local_files_only,
revision = model_revision,
)
+ self._is_dataset_audio = is_dataset_audio
if self._audio_type == "audio_vlm":
self.is_audio = False
self.is_audio_vlm = is_dataset_audio
@@ -305,32 +391,93 @@ class UnslothTrainer:
# Load tokenizer/processor (CPU only, no VRAM)
# Whisper needs AutoProcessor; others AutoTokenizer (CSM loads its own inline).
- if self._audio_type == "whisper":
- from transformers import AutoProcessor
- self.tokenizer = AutoProcessor.from_pretrained(
+ def _load_tokenizer_for_detected_type():
+ if self._audio_type == "whisper":
+ from transformers import AutoProcessor
+ self.tokenizer = AutoProcessor.from_pretrained(
+ lookup_name,
+ trust_remote_code = trust_remote_code,
+ token = hf_token,
+ local_files_only = local_files_only,
+ revision = model_revision,
+ )
+ else:
+ from transformers import AutoTokenizer
+ self.tokenizer = AutoTokenizer.from_pretrained(
+ lookup_name,
+ trust_remote_code = trust_remote_code,
+ token = hf_token,
+ local_files_only = local_files_only,
+ revision = model_revision,
+ # Spark-TTS keeps its tokenizer under LLM/, the subfolder _load_model
+ # loads weights from. Reading the root finds no vocab and fails blaming
+ # a missing sentencepiece that is installed and irrelevant.
+ **_spark_tts_tokenizer_kwargs(self._audio_type, lookup_name),
+ )
+
+ # The probe and the tokenizer load are separate reads of the same repo, so a transient
+ # timeout or 5xx can leave the probe inconclusive while the read after it succeeds.
+ # Ask again rather than refuse the run later claiming tokenizer_config.json could not
+ # be read when it just was. detect_audio_type_checked caches only definitive results,
+ # so this costs a cache hit on the common path.
+ def _retry_audio_detection() -> bool:
+ """Re-probe an inconclusive type. True if the answer changed."""
+ if self._audio_type_known:
+ return False
+ previous_type = self._audio_type
+ retried_type, retried_known = detect_audio_type_checked(
lookup_name,
- trust_remote_code = trust_remote_code,
- token = hf_token,
+ hf_token,
local_files_only = local_files_only,
revision = model_revision,
)
- else:
- from transformers import AutoTokenizer
- self.tokenizer = AutoTokenizer.from_pretrained(
- lookup_name,
- trust_remote_code = trust_remote_code,
- token = hf_token,
- local_files_only = local_files_only,
- revision = model_revision,
- # Spark-TTS keeps only BiCodec, config.yaml and the source tree at its repo
- # root; the tokenizer lives under LLM/, the same subfolder _load_model loads
- # weights from. Reading the root finds no vocab and fails to build a backend
- # tokenizer, blaming a missing sentencepiece that is installed and irrelevant.
- **_spark_tts_tokenizer_kwargs(self._audio_type, lookup_name),
+ if not retried_known:
+ return False
+ self._audio_type, self._audio_type_known = retried_type, True
+ if self._audio_type == "audio_vlm":
+ self.is_audio = False
+ self.is_audio_vlm = is_dataset_audio
+ self._audio_type = None
+ else:
+ self.is_audio = self._audio_type is not None
+ self.is_audio_vlm = False
+ logger.info(
+ "pre_detect retry: audio_type=%s, is_audio=%s",
+ self._audio_type,
+ self.is_audio,
+ )
+ return self._audio_type != previous_type
+
+ # An inconclusive probe leaves _audio_type None and the kwargs are chosen from it, so
+ # a Spark-TTS repo is read at its tokenizer-less root and raises here. Re-probe before
+ # giving up, or the retry below is unreachable for exactly the models whose layout the
+ # type decides.
+ try:
+ _load_tokenizer_for_detected_type()
+ except Exception:
+ if not _retry_audio_detection():
+ raise
+ logger.info(
+ "Retrying the tokenizer load for %s as %s after the first read failed",
+ model_name,
+ self._audio_type,
)
+ _load_tokenizer_for_detected_type()
logger.info("Pre-loaded tokenizer for %s", model_name)
+ # The loader and its kwargs are chosen from the type, so a retry that changes the
+ # answer has left the wrong object in self.tokenizer: whisper needs an AutoProcessor
+ # (_preprocess_whisper_dataset reads .feature_extractor and .tokenizer off it), and
+ # Spark-TTS its LLM/ subfolder.
+ if _retry_audio_detection():
+ _load_tokenizer_for_detected_type()
+ logger.info(
+ "Reloaded tokenizer for %s after detection changed to %s",
+ model_name,
+ self._audio_type,
+ )
+
def add_progress_callback(self, callback: Callable[[TrainingProgress], None]):
"""Add callback for training progress updates"""
self.progress_callbacks.append(callback)
@@ -735,13 +882,16 @@ class UnslothTrainer:
_preserve = ["Unsloth*Trainer.py"] if sys.platform in ("win32", "darwin") else None
clear_unsloth_compiled_cache(preserve_patterns = _preserve)
- # Detect audio type (config.json + tokenizer)
- self._audio_type = detect_audio_type(
+ # Detect audio type (config.json + tokenizer). Checked: this reassigns
+ # _audio_type, so an unchecked answer would leave the flag describing the
+ # previous probe.
+ self._audio_type, self._audio_type_known = detect_audio_type_checked(
lookup_name,
hf_token,
local_files_only = local_files_only,
revision = model_revision,
)
+ self._is_dataset_audio = is_dataset_audio
# audio_vlm is detected as an audio_type now; handle separately
if self._audio_type == "audio_vlm":
self.is_audio = False
@@ -2863,6 +3013,30 @@ class UnslothTrainer:
return None
# ========== AUDIO MODELS: custom preprocessing ==========
+ # An inconclusive probe must not read as "not an audio model": falling through
+ # with an audio dataset lands on the text path, which fails much later with
+ # "Could not auto-detect format mapping", a column-mapping complaint that says
+ # nothing about the repo read that actually failed.
+ # getattr, because this method is also driven against lightweight stand-ins; both
+ # defaults leave the guard closed.
+ # _is_dataset_audio arrives from the client and is true on a column-NAME match
+ # alone, so _dataset_has_audio_column is the tiebreaker: only a positive "no audio
+ # here" reopens the text path, unknown still refuses.
+ # raw/CPT is exempt: it reads the text column and ignores any audio one by design.
+ if (
+ not self._audio_type
+ and not getattr(self, "_audio_type_known", True)
+ and getattr(self, "_is_dataset_audio", False)
+ and not raw_text_mode
+ and _dataset_has_audio_column(dataset) is not False
+ ):
+ raise RuntimeError(
+ "Could not determine whether this model is an audio model: its "
+ "tokenizer_config.json could not be read (gated repo, offline, or a "
+ "cache holding the weights but not the tokenizer files). The dataset "
+ "has an audio column, so training cannot fall back to the text path. "
+ "Re-download the model or provide a token, then retry."
+ )
# Every audio branch below, and the VLM path, casts an Audio column and reads its array.
if self._audio_type or self.is_audio_vlm:
if not ensure_audio_decoding():
diff --git a/studio/backend/tests/test_audio_type_inconclusive.py b/studio/backend/tests/test_audio_type_inconclusive.py
new file mode 100644
index 00000000000..7afc4924a06
--- /dev/null
+++ b/studio/backend/tests/test_audio_type_inconclusive.py
@@ -0,0 +1,450 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""An unreadable tokenizer_config.json must not read as "not an audio model".
+
+`detect_audio_type` returns None for both "definitely not audio" and "could not tell", and
+the trainer used it to choose a preprocessing path. A TTS run whose repo could not be read
+therefore took the text path and failed much later with "Could not auto-detect format
+mapping", which names a column-mapping problem rather than the read that failed.
+"""
+
+from __future__ import annotations
+
+import sys
+from pathlib import Path
+from types import SimpleNamespace
+
+import pytest
+
+from core.training.trainer import _AUDIO_SNIFF_ROWS as _SNIFF_ROWS
+from core.training.trainer import UnslothTrainer
+
+_BACKEND = Path(__file__).resolve().parents[1]
+_load_and_format_dataset = UnslothTrainer.load_and_format_dataset
+
+
+class _Dataset:
+ column_names = ["audio", "text"]
+
+ def __len__(self):
+ return 4
+
+
+def _trainer(*, audio_type, known, dataset_audio):
+ """A stand-in carrying only what load_and_format_dataset reads."""
+ errors: list[str] = []
+ trainer = SimpleNamespace(
+ should_stop = False,
+ _audio_type = audio_type,
+ _audio_type_known = known,
+ _is_dataset_audio = dataset_audio,
+ is_audio_vlm = False,
+ is_vlm = False,
+ model_name = "org/model",
+ tokenizer = None,
+ errors = errors,
+ _update_progress = lambda **kw: errors.append(kw.get("error")) if kw.get("error") else None,
+ _resolve_eval_split_from_dataset = lambda dataset: None,
+ )
+ trainer.load_and_format_dataset = _load_and_format_dataset.__get__(trainer)
+ return trainer
+
+
+def _run(
+ trainer,
+ monkeypatch,
+ dataset = None,
+ format_type = "auto",
+):
+ from hub.utils import dataset_cache
+
+ monkeypatch.setattr(
+ dataset_cache, "load_cached_hf_dataset", lambda *a, **k: dataset or _Dataset()
+ )
+ monkeypatch.setattr(
+ "core.training.trainer.format_and_template_dataset",
+ lambda dataset, **kw: {"dataset": dataset, "detected_format": "test", "warnings": []},
+ )
+ monkeypatch.setattr(
+ "core.training.trainer.load_dataset",
+ lambda *a, **k: (_ for _ in ()).throw(AssertionError("no remote access")),
+ )
+ monkeypatch.setattr(
+ sys.modules["datasets"],
+ "get_dataset_split_names",
+ lambda *a, **k: (_ for _ in ()).throw(AssertionError("no remote access")),
+ )
+ return trainer.load_and_format_dataset(
+ "org/dataset",
+ format_type = format_type,
+ dataset_local_files_only = True,
+ dataset_local_path = "/cache/snapshot",
+ )
+
+
+def test_an_inconclusive_probe_on_an_audio_dataset_is_refused(monkeypatch):
+ trainer = _trainer(audio_type = None, known = False, dataset_audio = True)
+ assert _run(trainer, monkeypatch) is None
+ assert trainer.errors, "the run failed without reporting why"
+ reported = trainer.errors[0]
+ assert "tokenizer_config.json" in reported
+ assert "Could not auto-detect format mapping" not in reported
+
+
+def test_a_definitive_non_audio_model_still_takes_the_text_path(monkeypatch):
+ # The common case, unaffected.
+ trainer = _trainer(audio_type = None, known = True, dataset_audio = True)
+ assert _run(trainer, monkeypatch) is not None
+ assert not trainer.errors
+
+
+def test_an_inconclusive_probe_without_audio_data_is_left_alone(monkeypatch):
+ # A text run whose repo happens to be gated must not start failing.
+ trainer = _trainer(audio_type = None, known = False, dataset_audio = False)
+ assert _run(trainer, monkeypatch) is not None
+ assert not trainer.errors
+
+
+def test_the_trainer_probes_with_the_checked_variant():
+ # detect_audio_type collapses "not audio" and "unreadable" into None.
+ text = (_BACKEND / "core/training/trainer.py").read_text(encoding = "utf-8")
+ assert "detect_audio_type_checked(" in text
+ for line in text.splitlines():
+ stripped = line.strip()
+ if stripped.startswith("self._audio_type") and "detect_audio_type" in stripped:
+ assert "detect_audio_type_checked(" in stripped, stripped
+
+
+def test_every_probe_site_records_whether_it_was_definitive():
+ # A site that reassigns _audio_type without its flag leaves the flag describing the
+ # previous probe, which is worse than not having it.
+ lines = (_BACKEND / "core/training/trainer.py").read_text(encoding = "utf-8").splitlines()
+ sites = [
+ i for i, l in enumerate(lines) if "detect_audio_type_checked(" in l and "import" not in l
+ ]
+ assert sites, "no probe sites found"
+ for i in sites:
+ # A window, because the retry site unpacks into locals first so a still-inconclusive
+ # answer cannot overwrite a good earlier one.
+ window = "\n".join(lines[i : i + 25])
+ assert "self._audio_type_known" in window, lines[i].strip()
+
+
+class _TypedDataset(_Dataset):
+ """A dataset with a real `features` schema and rows, as a loaded HF dataset has."""
+
+ def __init__(
+ self,
+ features,
+ rows = None,
+ ):
+ self.features = features
+ self._rows = rows if rows is not None else [{"audio": "hello", "text": "hi"}]
+
+ def __iter__(self):
+ return iter(self._rows)
+
+
+def _audio_features():
+ from datasets import Audio, Value
+ return {"audio": Audio(sampling_rate = 16000), "text": Value("string")}
+
+
+def _text_features():
+ from datasets import Value
+ return {"audio": Value("string"), "text": Value("string")}
+
+
+def test_a_text_dataset_with_a_column_named_audio_is_not_refused(monkeypatch):
+ # is_dataset_audio is true on a column-NAME keyword match alone, never looking at the
+ # value, so a text dataset with an `audio` column arrives here flagged as audio.
+ # Refusing it would take away a text path that works.
+ trainer = _trainer(audio_type = None, known = False, dataset_audio = True)
+ result = _run(trainer, monkeypatch, dataset = _TypedDataset(_text_features()))
+ assert result is not None, "a text dataset was refused for having an audio-named column"
+ assert not trainer.errors
+
+
+def test_a_real_audio_column_is_still_refused(monkeypatch):
+ # The case the guard exists for.
+ trainer = _trainer(audio_type = None, known = False, dataset_audio = True)
+ assert _run(trainer, monkeypatch, dataset = _TypedDataset(_audio_features())) is None
+ assert trainer.errors and "tokenizer_config.json" in trainer.errors[0]
+
+
+def test_a_dataset_that_cannot_report_a_schema_is_still_refused(monkeypatch):
+ # A DatasetDict or iterable dataset has no usable `features`. Unknown is not "no audio
+ # here", and an unreadable model probe remains the likelier explanation.
+ trainer = _trainer(audio_type = None, known = False, dataset_audio = True)
+ assert _run(trainer, monkeypatch, dataset = _Dataset()) is None
+ assert trainer.errors and "tokenizer_config.json" in trainer.errors[0]
+
+
+def test_a_path_backed_audio_column_is_still_refused(monkeypatch):
+ # A JSON/CSV audio dataset carries paths as a Value("string") until the preprocessor
+ # casts it (_preprocess_snac_dataset does exactly that), so the schema alone would call
+ # it textual and hand it to the text path this guard prevents.
+ trainer = _trainer(audio_type = None, known = False, dataset_audio = True)
+ dataset = _TypedDataset(
+ _text_features(), rows = [{"audio": "clips/utt_0001.wav", "text": "hello"}]
+ )
+ assert _run(trainer, monkeypatch, dataset = dataset) is None
+ assert trainer.errors and "tokenizer_config.json" in trainer.errors[0]
+
+
+def test_a_raw_text_run_is_never_refused(monkeypatch):
+ # raw/CPT reads the text column and ignores any audio one by design, so it is not the
+ # unsafe audio-to-text fallback this guard targets.
+ trainer = _trainer(audio_type = None, known = False, dataset_audio = True)
+ _run(
+ trainer,
+ monkeypatch,
+ dataset = _TypedDataset(_audio_features()),
+ format_type = "raw",
+ )
+ # The stub cannot drive the whole raw-text branch, so assert only that the guard did not
+ # fire; anything reported past this point is the stub.
+ reported = " ".join(trainer.errors)
+ assert "tokenizer_config.json" not in reported, f"raw-text run was refused: {reported}"
+
+
+def test_a_transient_probe_failure_is_rechecked_after_the_tokenizer_loads(monkeypatch):
+ # The probe and the tokenizer load are two reads of the same repo, so a timeout or 5xx
+ # can leave the probe inconclusive while the download right after it succeeds. Without a
+ # recheck the run is refused later claiming an unreadable file that has just been read.
+ import core.training.trainer as trainer_mod
+
+ answers = [(None, False), ("whisper", True)]
+ calls = []
+
+ def fake_probe(*a, **kw):
+ calls.append(a[0] if a else None)
+ return answers[min(len(calls) - 1, len(answers) - 1)]
+
+ monkeypatch.setattr(trainer_mod, "detect_audio_type_checked", fake_probe)
+ monkeypatch.setattr(trainer_mod, "is_vision_model", lambda *a, **kw: False)
+
+ class _Proc:
+ @classmethod
+ def from_pretrained(cls, *a, **kw):
+ return object()
+
+ import transformers
+
+ monkeypatch.setattr(transformers, "AutoProcessor", _Proc, raising = False)
+ monkeypatch.setattr(transformers, "AutoTokenizer", _Proc, raising = False)
+
+ trainer = UnslothTrainer()
+ trainer.pre_detect_and_load_tokenizer("org/model", is_dataset_audio = True)
+
+ assert len(calls) == 2, "the probe was not retried after the tokenizer load"
+ assert trainer._audio_type == "whisper"
+ assert trainer._audio_type_known is True
+ assert trainer.is_audio is True
+
+
+def test_a_probe_that_stays_inconclusive_is_not_overwritten(monkeypatch):
+ # A second inconclusive read must leave the flag False so the guard still protects the run.
+ import core.training.trainer as trainer_mod
+
+ monkeypatch.setattr(trainer_mod, "detect_audio_type_checked", lambda *a, **kw: (None, False))
+ monkeypatch.setattr(trainer_mod, "is_vision_model", lambda *a, **kw: False)
+
+ class _Proc:
+ @classmethod
+ def from_pretrained(cls, *a, **kw):
+ return object()
+
+ import transformers
+
+ monkeypatch.setattr(transformers, "AutoTokenizer", _Proc, raising = False)
+
+ trainer = UnslothTrainer()
+ trainer.pre_detect_and_load_tokenizer("org/model", is_dataset_audio = True)
+ assert trainer._audio_type_known is False
+ assert trainer._audio_type is None
+
+
+def test_a_null_first_audio_value_does_not_decide_the_dataset(monkeypatch):
+ # A JSON/CSV audio dataset can carry a null first value and real paths after it, and the
+ # preprocessors skip such a row rather than fail, so row 0 alone would call it textual.
+ trainer = _trainer(audio_type = None, known = False, dataset_audio = True)
+ dataset = _TypedDataset(
+ _text_features(),
+ rows = [
+ {"audio": None, "text": "hello"},
+ {"audio": "", "text": "there"},
+ {"audio": "clips/utt_0003.wav", "text": "world"},
+ ],
+ )
+ assert _run(trainer, monkeypatch, dataset = dataset) is None
+ assert trainer.errors and "tokenizer_config.json" in trainer.errors[0]
+
+
+def test_rows_that_offer_no_usable_value_are_unknown_not_textual(monkeypatch):
+ # All-null candidate values answer neither way, and unknown must keep refusing.
+ trainer = _trainer(audio_type = None, known = False, dataset_audio = True)
+ dataset = _TypedDataset(
+ _text_features(), rows = [{"audio": None, "text": None}, {"audio": None, "text": None}]
+ )
+ assert _run(trainer, monkeypatch, dataset = dataset) is None
+ assert trainer.errors and "tokenizer_config.json" in trainer.errors[0]
+
+
+def test_the_retry_reloads_the_processor_when_it_discovers_whisper(monkeypatch):
+ # The loader is chosen from the audio type, so a retry that flips the answer to whisper
+ # has already stored an AutoTokenizer. _preprocess_whisper_dataset reads
+ # .feature_extractor and .tokenizer off the processor, so every sample would be skipped.
+ import core.training.trainer as trainer_mod
+
+ answers = [(None, False), ("whisper", True)]
+ calls = []
+
+ def fake_probe(*a, **kw):
+ calls.append(1)
+ return answers[min(len(calls) - 1, len(answers) - 1)]
+
+ monkeypatch.setattr(trainer_mod, "detect_audio_type_checked", fake_probe)
+ monkeypatch.setattr(trainer_mod, "is_vision_model", lambda *a, **kw: False)
+
+ loaded: list[str] = []
+
+ class _Processor:
+ @classmethod
+ def from_pretrained(cls, *a, **kw):
+ loaded.append("processor")
+ return object()
+
+ class _Tokenizer:
+ @classmethod
+ def from_pretrained(cls, *a, **kw):
+ loaded.append("tokenizer")
+ return object()
+
+ import transformers
+
+ monkeypatch.setattr(transformers, "AutoProcessor", _Processor, raising = False)
+ monkeypatch.setattr(transformers, "AutoTokenizer", _Tokenizer, raising = False)
+
+ trainer = UnslothTrainer()
+ trainer.pre_detect_and_load_tokenizer("org/model", is_dataset_audio = True)
+
+ assert trainer._audio_type == "whisper"
+ assert loaded == [
+ "tokenizer",
+ "processor",
+ ], f"expected a processor reload once the retry found whisper, got {loaded}"
+
+
+def test_the_retry_does_not_reload_when_the_answer_is_unchanged(monkeypatch):
+ # A retry that confirms the same type must not pay for a second load.
+ import core.training.trainer as trainer_mod
+
+ monkeypatch.setattr(trainer_mod, "detect_audio_type_checked", lambda *a, **kw: (None, True))
+ monkeypatch.setattr(trainer_mod, "is_vision_model", lambda *a, **kw: False)
+
+ loaded: list[str] = []
+
+ class _Tokenizer:
+ @classmethod
+ def from_pretrained(cls, *a, **kw):
+ loaded.append("tokenizer")
+ return object()
+
+ import transformers
+
+ monkeypatch.setattr(transformers, "AutoTokenizer", _Tokenizer, raising = False)
+
+ trainer = UnslothTrainer()
+ trainer.pre_detect_and_load_tokenizer("org/model", is_dataset_audio = True)
+ assert loaded == ["tokenizer"], f"tokenizer loaded more than once: {loaded}"
+
+
+def test_transcripts_are_not_evidence_that_the_audio_column_is_empty(monkeypatch):
+ # A path-backed audio dataset has a populated transcript in every row, so all-null audio
+ # values in the sniff window answer "unknown", not "textual" on the strength of the text:
+ # later rows may hold real paths, and the preprocessors skip the leading bad ones.
+ trainer = _trainer(audio_type = None, known = False, dataset_audio = True)
+ rows = [{"audio": None, "text": f"transcript {i}"} for i in range(_SNIFF_ROWS + 4)]
+ dataset = _TypedDataset(_text_features(), rows = rows)
+ assert _run(trainer, monkeypatch, dataset = dataset) is None
+ assert trainer.errors and "tokenizer_config.json" in trainer.errors[0]
+
+
+def test_a_populated_audio_named_column_of_prose_still_answers_textual(monkeypatch):
+ # The other half: an audio-NAMED column that really holds prose is the false positive
+ # this veto exists for, and it must keep answering False.
+ trainer = _trainer(audio_type = None, known = False, dataset_audio = True)
+ rows = [{"audio": f"a sentence {i}", "text": "hello"} for i in range(4)]
+ dataset = _TypedDataset(_text_features(), rows = rows)
+ _run(trainer, monkeypatch, dataset = dataset)
+ assert not any(
+ "tokenizer_config.json" in e for e in trainer.errors
+ ), f"a prose column named audio should not be refused: {trainer.errors}"
+
+
+def test_the_first_tokenizer_load_failing_still_reaches_the_retry(monkeypatch):
+ # Spark-TTS keeps its tokenizer under LLM/ and the subfolder kwarg is chosen from the
+ # audio type, so an inconclusive first probe reads the root and raises: a retry placed
+ # only after a SUCCESSFUL load can never run for the models whose layout the type decides.
+ import core.training.trainer as trainer_mod
+
+ answers = [(None, False), ("bicodec", True)]
+ calls = []
+
+ def fake_probe(*a, **kw):
+ calls.append(1)
+ return answers[min(len(calls) - 1, len(answers) - 1)]
+
+ monkeypatch.setattr(trainer_mod, "detect_audio_type_checked", fake_probe)
+ monkeypatch.setattr(trainer_mod, "is_vision_model", lambda *a, **kw: False)
+ monkeypatch.setattr(
+ trainer_mod,
+ "_spark_tts_tokenizer_kwargs",
+ lambda audio_type, name: {"subfolder": "LLM"} if audio_type == "bicodec" else {},
+ )
+
+ attempts = []
+
+ class _Tokenizer:
+ @classmethod
+ def from_pretrained(cls, *a, **kw):
+ attempts.append(kw.get("subfolder"))
+ if kw.get("subfolder") != "LLM":
+ raise OSError("Can't load tokenizer for 'org/spark'")
+ return object()
+
+ import transformers
+
+ monkeypatch.setattr(transformers, "AutoTokenizer", _Tokenizer, raising = False)
+
+ trainer = UnslothTrainer()
+ trainer.pre_detect_and_load_tokenizer("org/spark", is_dataset_audio = True)
+
+ assert attempts == [None, "LLM"], f"expected a retry under LLM/, got {attempts}"
+ assert trainer.tokenizer is not None
+ assert trainer._audio_type == "bicodec"
+
+
+def test_a_load_failure_with_a_definitive_type_is_not_swallowed(monkeypatch):
+ # The retry must not turn a genuine "this repo has no tokenizer" into a silent pass.
+ import core.training.trainer as trainer_mod
+
+ monkeypatch.setattr(
+ trainer_mod, "detect_audio_type_checked", lambda *a, **kw: ("bicodec", True)
+ )
+ monkeypatch.setattr(trainer_mod, "is_vision_model", lambda *a, **kw: False)
+
+ class _Tokenizer:
+ @classmethod
+ def from_pretrained(cls, *a, **kw):
+ raise OSError("Can't load tokenizer for 'org/broken'")
+
+ import transformers
+
+ monkeypatch.setattr(transformers, "AutoTokenizer", _Tokenizer, raising = False)
+
+ trainer = UnslothTrainer()
+ with pytest.raises(OSError):
+ trainer.pre_detect_and_load_tokenizer("org/broken", is_dataset_audio = True)
diff --git a/studio/backend/utils/models/__init__.py b/studio/backend/utils/models/__init__.py
index 4a5fb7274cf..0a15861bf34 100644
--- a/studio/backend/utils/models/__init__.py
+++ b/studio/backend/utils/models/__init__.py
@@ -9,6 +9,7 @@ from .model_config import (
is_vision_model,
is_embedding_model,
detect_audio_type,
+ detect_audio_type_checked,
is_audio_input_type,
VALID_AUDIO_TYPES,
scan_trained_models,
@@ -33,6 +34,7 @@ __all__ = [
"is_vision_model",
"is_embedding_model",
"detect_audio_type",
+ "detect_audio_type_checked",
"is_audio_input_type",
"VALID_AUDIO_TYPES",
"scan_trained_models",