Repository navigation
Studio: settle a non-audio tokenizer without parsing it - #8393
danielhanchen wants to merge 3 commits into
Conversation
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.
There was a problem hiding this comment.
💡 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) |
There was a problem hiding this comment.
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 👍 / 👎.
| if raw.rstrip().endswith("}"): | ||
| read_any = True |
There was a problem hiding this comment.
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.
|
Closing this: after merging current main, the diff is empty. The code landed independently in Worth naming why I took main's side whole rather than blending, because a mechanical conflict resolution here would have caused two regressions:
The branch's Nothing is lost by closing. Reopenable if I have misread which side won. |
detect_audio_typereadstokenizer_config.jsonandjson.loadsthe 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-itships a 1.16 MBtokenizer_config.jsonwith 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:
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.loadsraised 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.