Skip to content

Studio: settle a non-audio tokenizer without parsing it - #8393

Closed
danielhanchen wants to merge 3 commits into
mainfrom
studio-skip-parsing-non-audio-tokenizers
Closed

danielhanchen wants to merge 3 commits into
mainfrom
studio-skip-parsing-non-audio-tokenizers

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

detect_audio_type reads tokenizer_config.json and json.loads the whole file to look for codec tokens. Ordinary text checkpoints carry big tokenizers, so that is a lot of parsing for a file that was never going to match.

Concretely, from this box's cache: unsloth/gemma-3-270m-it ships a 1.16 MB tokenizer_config.json with 6415 added tokens. It is not an audio model, and parsing it was most of the cost of establishing that.

The change

A pattern can only match if its marker text appears in the file at all, so the raw text is scanned for the markers first and a non-audio tokenizer never reaches the parser.

On that real gemma file:

parse + classify   4.97 ms
marker scan only   2.64 ms
saved              2.33 ms per checkpoint  (47 percent)

The bigger the tokenizer, the more it saves. Audio models still parse, because they match.

Two details that matter more than the speedup

The markers cannot be derived from _AUDIO_TOKEN_PATTERNS, which is lambdas, so a codec added there without a marker here would silently stop being detected. The test pins the pattern set and drives every pattern through the prescan, so adding a codec fails the suite until its marker is added too.

A marker miss counts as "read" only when the text ends in a closing brace. Without that, a training run part-way through writing its tokenizer would become a definitive "not audio" and be cached for the life of the process. Previously json.loads raised on the truncated text and the answer stayed unknown, which is the behaviour that is preserved and pinned by a test.

Also stops the snac count summing all 28k of Orpheus's codes to answer a question settled by the first 10,001.

Tests

Four, covering the drift guard, that an ordinary tokenizer never reaches json.loads, that a real snac tokenizer is still detected, and the half-written case.

Backend: 796 passed across the audio, capability, token and model config suites.

Note

PR #7984 makes this path much hotter by probing every checkpoint in the LoRA scan, and it currently carries this same change. If this lands first that branch should drop its copy on the next merge from main. It also reorders _AUDIO_TOKEN_PATTERNS, so expect a small conflict here; the marker list is order independent.

danielhanchen and others added 2 commits August 11, 2026 06:31
detect_audio_type reads tokenizer_config.json and json.loads the whole thing to look
for codec tokens. Ordinary text checkpoints carry big tokenizers: unsloth/gemma-3-270m-it
is 1.16 MB with 6415 added tokens, and parsing it was most of the cost of answering
"is this an audio model", for a file that was never going to match.

A pattern can only match if its marker text appears in the file at all, so the raw text
is scanned for markers first. On that real file: 4.97 ms -> 2.64 ms, a 47 percent saving
per checkpoint, and the bigger the tokenizer the more it saves.

Two details:

- The markers cannot be derived from _AUDIO_TOKEN_PATTERNS, which is lambdas, so a codec
  added there without a marker here would silently stop being detected. The test pins the
  pattern set and drives every pattern through the prescan, so adding a codec fails the
  suite until its marker is added.
- A marker miss counts as "read" only when the text ends in a closing brace. Without that,
  a training run part-way through writing its tokenizer would become a definitive "not
  audio" and be cached for the life of the process, where before json.loads raised on the
  truncated text and it stayed unknown.

Also stops the snac count summing all 28k of Orpheus's codes to answer a question settled
by the first 10,001.

Backend: 796 passed across the audio, capability, token and model config suites.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 28bf979731

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Conservative: a false True only costs the parse that would have happened anyway.
A false False would misclassify, which is why the markers are pinned by a test.
"""
return any(marker in raw for marker in _AUDIO_TOKEN_MARKERS)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Account for JSON-escaped audio markers before rejecting

When a valid tokenizer config encodes marker characters with JSON Unicode escapes—such as Go's default \u003c|AUDIO|\u003e representation—json.loads would recover <|AUDIO|>, but this raw substring check returns false. The caller then treats the file as definitively non-audio and caches that result, so these checkpoints are silently misclassified; the prescan must conservatively recognize escaped marker forms or fall back to parsing when escapes could form a marker.

Useful? React with 👍 / 👎.

Comment on lines +1290 to +1291
if raw.rstrip().endswith("}"):
read_any = True

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Validate the complete JSON object before caching a negative

When an in-progress write stops after any nested object but before the root is complete, the truncated text still ends in } even though json.loads would reject it. If that prefix has not yet reached an audio marker, this condition marks the read definitive and detect_audio_type caches None for the process lifetime, so completing the file later cannot restore audio detection; an ending brace alone cannot establish that the tokenizer config is whole.

Useful? React with 👍 / 👎.

One conflict, studio/backend/utils/models/model_config.py, and reading both
sides shows the branch has been overtaken.

This branch wanted: skip json.loads of a tokenizer_config whose raw text holds
none of the audio markers (_AUDIO_TOKEN_MARKERS + _may_hold_audio_tokens), a
short-circuiting _count_prefix_exceeds in place of the full sum over Orpheus's
28k SNAC codes, and a truncated file still counted as unread so a half-written
training checkpoint is not cached as a definitive "not audio".

main wants the same thing and already has it, via #7984, character for
character in _detect_audio_from_tokenizer. On top of that main carries two
things this branch does not:

  - _AUDIO_TOKEN_PATTERNS is reordered so the generic audio_vlm marker is tried
    LAST. First match wins, and Orpheus ships 28k <custom_token_N> SNAC codes
    AND a stray <|audio|>, so with audio_vlm second (this branch's order) it is
    typed as audio-input and is_audio stays False.
  - _AUDIO_OFFLINE_MISS_TTL_S / _audio_offline_miss_cache, which stop /loras
    re-probing an unanswerable offline adapter on every poll.

So the resolution is main's side whole, not a blend: taking this branch's
ordering would reintroduce the Orpheus misclassification, and taking its
detect_audio_type would drop the definitive/non-definitive split.

tests/test_audio_marker_prescan.py goes with it. main's
tests/test_audio_token_detection.py already asserts the same three things under
different names (test_every_pattern_has_a_marker_so_the_parse_can_be_skipped,
test_a_large_text_tokenizer_is_not_parsed,
test_a_half_written_tokenizer_stays_unknown) plus one assertion this file did
not have, so keeping both would be two copies of one suite drifting apart.

The branch is now byte-identical to main and this PR has nothing left to land.
@danielhanchen

Copy link
Copy Markdown
Member Author

Closing this: after merging current main, the diff is empty.

changedFiles 0, +0/-0

The code landed independently in d20db3f1f ("studio: add audio page with tts/stt create tab, train tab, and openai audio endpoints (#7984)"), which carries _AUDIO_TOKEN_MARKERS, _may_hold_audio_tokens, _count_prefix_exceeds and the raw.rstrip().endswith("}") half-written guard character for character with this branch's.

Worth naming why I took main's side whole rather than blending, because a mechanical conflict resolution here would have caused two regressions:

  1. _AUDIO_TOKEN_PATTERNS ordering. Main tries the generic audio_vlm arm last. First match wins, and Orpheus ships 28k <custom_token_N> SNAC codes alongside a stray <|audio|>; with audio_vlm second, which is this branch's order, Orpheus is typed as audio-input and is_audio stays False. Keeping this branch's ordering would have put that back.
  2. Main also has _AUDIO_OFFLINE_MISS_TTL_S / _audio_offline_miss_cache and the detect_audio_type_checked definitive/non-definitive split, which this branch predates. Taking its detect_audio_type would have dropped both.

The branch's test_audio_marker_prescan.py is also redundant: main's test_audio_token_detection.py already asserts the same three properties (test_every_pattern_has_a_marker_so_the_parse_can_be_skipped, test_a_large_text_tokenizer_is_not_parsed, test_a_half_written_tokenizer_stays_unknown) plus one this file lacked. Two copies of one suite would only drift apart.

studio/backend/tests/test_audio_token_detection.py   17 passed

Nothing is lost by closing. Reopenable if I have misread which side won.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant