Skip to content

tokenizer: read a Qwen2 slow-tokenizer export when tokenizer.json is absent - #2225

Merged
ashhart merged 2 commits into
ashhart:mainfrom
shuuul:fix/qwen2-slow-tokenizer
Nov 5, 2026
Merged

ashhart merged 2 commits into
ashhart:mainfrom
shuuul:fix/qwen2-slow-tokenizer

Conversation

@shuuul

@shuuul shuuul commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

What

loadTokenizer falls back to a Qwen2 slow tokenizer when path/tokenizer.json is absent and the directory holds vocab.json, merges.txt and tokenizer_config.json with tokenizer_class Qwen2Tokenizer. The new qwen2_slow.zig writes the tokenizer.json a Qwen2 checkpoint would ship, and the existing parser reads it unchanged.

The converter reads tokenizer_config.json first, so another class is refused before the vocab and merges are read. vocab.json must be one object of string keys to non-negative integer ids; the model's vocab is written back from the parsed ids. Every merge must name two tokens in that vocab, and added-token ids must be decimal digits written as parsed integers.

Why

Some checkpoints ship only a slow tokenizer's files. Today the loader returns FileNotFound for them; with this they can be served without a Python conversion step.

Test

zig test zig/src/core/tokenizer/tokenizer.zig: 48/48 pass. The fixture is a 267-token export whose golden tokenizer.json was generated by transformers 4.57.1's convert_slow_tokenizer; encode, decode and <|endoftext|>'s id and flags match the golden on five inputs.

Refusal tests and their errors:

  • a non-object vocab.json: InvalidTokenizer;
  • a merge naming a token missing from the vocab: MissingMergeToken;
  • another tokenizer_class: UnsupportedModel, before vocab and merges are read;
  • added-token ids +5 and 1_0: InvalidAddedTokenId.

Limits

Only Qwen2Tokenizer-class exports are converted; any other slow class still returns UnsupportedModel. Structured output still needs tokenizer.json, because server.zig reads that file itself; the CHANGELOG says so. loadTokenizer on a .json path, and on a directory with tokenizer.json, is unchanged.

@ashhart

ashhart commented Nov 5, 2026

Copy link
Copy Markdown
Owner

Thanks @shuuul. Checking against a golden tokenizer.json made by the converter itself is the right test, and the fallback only runs when tokenizer.json is missing, which keeps every current checkpoint on its old path. A few things:

  1. vocab.json is written into the JSON as-is, so a file that isn't one object can close model early and set other fields. Please parse it first as an object of strings to non-negative integers, refuse anything else, and write it back from the parsed values.
  2. Please write each added token's id from the parsed number, not the key text. parseInt accepts +5 and 1_0, which aren't JSON numbers.
  3. Please add refusal tests: a vocab.json that isn't an object, a merge naming a missing token, another tokenizer_class, and a bad added-token id. Each should give a named error.
  4. tokenizer.zig is 615 lines, past our 600, and lean reads 430 on main (+5, four multi-line comments). Please move qwen2Json into its own file and make the comments one line each.
  5. Structured output reads tokenizer.json itself (server.zig:368), so these checkpoints will refuse it. Please either hand it the converted JSON, or say so in the PR and the CHANGELOG line.
  6. A CHANGELOG line under Unreleased, please. A small one besides: reading tokenizer_config.json first would refuse other classes before reading up to 512 MiB of vocab and merges.
  7. The comment naming transformers' converter can go: "the tokenizer.json a Qwen2 checkpoint ships" says it without pointing at another project's source.

@shuuul

shuuul commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

All seven points make sense. Plan:

  1. Parse vocab.json as a JSON object of string keys to non-negative integer ids and write the model's vocab back from those values; anything else is refused.
  2. Parse each added_tokens_decoder key as a JSON non-negative integer (digits only, so +5 and 1_0 are refused) and write the parsed id.
  3. Add refusal tests for a non-object vocab, a merge naming a token missing from the vocab, another tokenizer_class, and a bad added-token id, each with its own named error.
  4. Move the converter to its own file, make its comments one line each, and keep tokenizer.zig under 600 lines with lean back at 430.
  5. For structured output, I'll document the limitation in the PR and the CHANGELOG: the fallback serves text, but a checkpoint without tokenizer.json still refuses structured output because server.zig reads that file itself. I'd rather keep that separate from this change than thread converted bytes through the server.
  6. Read tokenizer_config.json first, so another class is refused before the vocab and merges are read; add the CHANGELOG line under Unreleased.
  7. Drop the converter-source wording; the comment will say the tokenizer.json a Qwen2 checkpoint ships.

I'll push the new head with these.

@ashhart

ashhart commented Nov 5, 2026

Copy link
Copy Markdown
Owner

Thanks @shuuul, that plan covers all seven. Documenting the structured-output limit rather than threading the bytes through the server is fine. Please push it when it's ready, with the refusal tests' error names in the description.

@shuuul

shuuul commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

Pushed 8637c4f with all seven points: the converter is in qwen2_slow.zig, tokenizer.zig is 550 lines, lean is 425, and zig test zig/src/core/tokenizer/tokenizer.zig is 48/48. The refusal errors are InvalidTokenizer (vocab not an object), MissingMergeToken, UnsupportedModel (another class, refused before vocab and merges), and InvalidAddedTokenId (+5, 1_0). The structured-output limit is in the description and CHANGELOG.

@ashhart

ashhart commented Nov 5, 2026

Copy link
Copy Markdown
Owner

Thanks @shuuul. All seven are in, and refusing another class before reading half a gigabyte of vocab and merges is a nice touch. Ready to land, and it's in the queue.

…is absent

loadTokenizer now reads a directory's vocab.json, merges.txt and
tokenizer_config.json when tokenizer.json is missing and tokenizer_class
is Qwen2Tokenizer, emitting the tokenizer.json transformers 4.57's
Qwen2Converter produces and parsing it as before. Checkpoints that ship
only slow-tokenizer files need no Python conversion at serve time.

The test converts a 267-token export and compares encode, decode and the
added token against a golden tokenizer.json from transformers 4.57.1.
Move the converter to qwen2_slow.zig, parse vocab.json as an object of
non-negative integer ids, and write added-token ids from parsed numbers
so non-JSON keys cannot slip through. Read tokenizer_config.json first,
so another tokenizer class is refused before the vocab and merges.
Missing merge tokens and malformed adds get their own errors.

The refusal tests cover a non-object vocab, a merge naming a missing
token, another class and bad added ids. Structured output still reads
tokenizer.json itself; the CHANGELOG says so.
@ashhart
ashhart force-pushed the fix/qwen2-slow-tokenizer branch from 8973794 to d9175a2 Compare November 5, 2026 10:00
@ashhart
ashhart merged commit 92df615 into ashhart:main Nov 5, 2026
0 of 7 checks passed
@ashhart

ashhart commented Nov 5, 2026

Copy link
Copy Markdown
Owner

Thank you @shuuul. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

@shuuul
shuuul deleted the fix/qwen2-slow-tokenizer branch November 5, 2026 10:01
jschmied added a commit to jschmied/TensorFold that referenced this pull request Nov 5, 2026
…hart#2105)

Qwen3-ASR's 0.6B and 1.7B admitted from config.json: the audio tower's geometry (chunks of 100 frames, windows of
n_window_infer frames) and the Qwen3 language model's, its interleaved M-RoPE refused unless its sections span the
head (audio positions are equal on all three axes, so it is plain RoPE). Every stored tensor checked by name, shape
and dtype, none left over; the stored lm_head must be bit-equal to the tied embedding.

WhisperFeatureExtractor's log-mel rows in float64, and the tower in float64 stage by stage, either exact or at
torch's bf16 rounding points. The checkpoint ships no tokenizer.json; the shared tokenizer reads its slow export
(ashhart#2225).

tf-qwen3-asr-check compares weights, prompt ids, the reply's decode, mel rows and every tower stage with a case from
tools/zig/qwen3_asr_case.py (the qwen-asr 0.0.6 package's transformers backend, hooked stage by stage).
@ashhart

ashhart commented Nov 5, 2026

Copy link
Copy Markdown
Owner

jschmied added a commit to jschmied/TensorFold that referenced this pull request Nov 10, 2026
…hart#2105)

Qwen3-ASR's 0.6B and 1.7B admitted from config.json: the audio tower's geometry (chunks of 100 frames, windows of
n_window_infer frames) and the Qwen3 language model's, its interleaved M-RoPE refused unless its sections span the
head (audio positions are equal on all three axes, so it is plain RoPE). Every stored tensor checked by name, shape
and dtype, none left over; the stored lm_head must be bit-equal to the tied embedding.

WhisperFeatureExtractor's log-mel rows in float64, and the tower in float64 stage by stage, either exact or at
torch's bf16 rounding points. The checkpoint ships no tokenizer.json; the shared tokenizer reads its slow export
(ashhart#2225).

tf-qwen3-asr-check compares weights, prompt ids, the reply's decode, mel rows and every tower stage with a case from
tools/zig/qwen3_asr_case.py (the qwen-asr 0.0.6 package's transformers backend, hooked stage by stage).
jschmied added a commit to jschmied/TensorFold that referenced this pull request Nov 12, 2026
…hart#2105)

Qwen3-ASR's 0.6B and 1.7B admitted from config.json: the audio tower's geometry (chunks of 100 frames, windows of
n_window_infer frames) and the Qwen3 language model's, its interleaved M-RoPE refused unless its sections span the
head (audio positions are equal on all three axes, so it is plain RoPE). Every stored tensor checked by name, shape
and dtype, none left over; the stored lm_head must be bit-equal to the tied embedding.

WhisperFeatureExtractor's log-mel rows in float64, and the tower in float64 stage by stage, either exact or at
torch's bf16 rounding points. The checkpoint ships no tokenizer.json; the shared tokenizer reads its slow export
(ashhart#2225).

tf-qwen3-asr-check compares weights, prompt ids, the reply's decode, mel rows and every tower stage with a case from
tools/zig/qwen3_asr_case.py (the qwen-asr 0.0.6 package's transformers backend, hooked stage by stage).
jschmied added a commit to jschmied/TensorFold that referenced this pull request Nov 17, 2026
…hart#2105)

Qwen3-ASR's 0.6B and 1.7B admitted from config.json: the audio tower's geometry (chunks of 100 frames, windows of
n_window_infer frames) and the Qwen3 language model's, its interleaved M-RoPE refused unless its sections span the
head (audio positions are equal on all three axes, so it is plain RoPE). Every stored tensor checked by name, shape
and dtype, none left over; the stored lm_head must be bit-equal to the tied embedding.

WhisperFeatureExtractor's log-mel rows in float64, and the tower in float64 stage by stage, either exact or at
torch's bf16 rounding points. The checkpoint ships no tokenizer.json; the shared tokenizer reads its slow export
(ashhart#2225).

tf-qwen3-asr-check compares weights, prompt ids, the reply's decode, mel rows and every tower stage with a case from
tools/zig/qwen3_asr_case.py (the qwen-asr 0.0.6 package's transformers backend, hooked stage by stage).
ashhart pushed a commit to jschmied/TensorFold that referenced this pull request Nov 18, 2026
…hart#2105)

Qwen3-ASR's 0.6B and 1.7B admitted from config.json: the audio tower's geometry (chunks of 100 frames, windows of
n_window_infer frames) and the Qwen3 language model's, its interleaved M-RoPE refused unless its sections span the
head (audio positions are equal on all three axes, so it is plain RoPE). Every stored tensor checked by name, shape
and dtype, none left over; the stored lm_head must be bit-equal to the tied embedding.

WhisperFeatureExtractor's log-mel rows in float64, and the tower in float64 stage by stage, either exact or at
torch's bf16 rounding points. The checkpoint ships no tokenizer.json; the shared tokenizer reads its slow export
(ashhart#2225).

tf-qwen3-asr-check compares weights, prompt ids, the reply's decode, mel rows and every tower stage with a case from
tools/zig/qwen3_asr_case.py (the qwen-asr 0.0.6 package's transformers backend, hooked stage by stage).
ashhart pushed a commit to jschmied/TensorFold that referenced this pull request Nov 19, 2026
…hart#2105)

Qwen3-ASR's 0.6B and 1.7B admitted from config.json: the audio tower's geometry (chunks of 100 frames, windows of
n_window_infer frames) and the Qwen3 language model's, its interleaved M-RoPE refused unless its sections span the
head (audio positions are equal on all three axes, so it is plain RoPE). Every stored tensor checked by name, shape
and dtype, none left over; the stored lm_head must be bit-equal to the tied embedding.

WhisperFeatureExtractor's log-mel rows in float64, and the tower in float64 stage by stage, either exact or at
torch's bf16 rounding points. The checkpoint ships no tokenizer.json; the shared tokenizer reads its slow export
(ashhart#2225).

tf-qwen3-asr-check compares weights, prompt ids, the reply's decode, mel rows and every tower stage with a case from
tools/zig/qwen3_asr_case.py (the qwen-asr 0.0.6 package's transformers backend, hooked stage by stage).
ashhart pushed a commit to jschmied/TensorFold that referenced this pull request Nov 21, 2026
…hart#2105)

Qwen3-ASR's 0.6B and 1.7B admitted from config.json: the audio tower's geometry (chunks of 100 frames, windows of
n_window_infer frames) and the Qwen3 language model's, its interleaved M-RoPE refused unless its sections span the
head (audio positions are equal on all three axes, so it is plain RoPE). Every stored tensor checked by name, shape
and dtype, none left over; the stored lm_head must be bit-equal to the tied embedding.

WhisperFeatureExtractor's log-mel rows in float64, and the tower in float64 stage by stage, either exact or at
torch's bf16 rounding points. The checkpoint ships no tokenizer.json; the shared tokenizer reads its slow export
(ashhart#2225).

tf-qwen3-asr-check compares weights, prompt ids, the reply's decode, mel rows and every tower stage with a case from
tools/zig/qwen3_asr_case.py (the qwen-asr 0.0.6 package's transformers backend, hooked stage by stage).
ashhart pushed a commit to jschmied/TensorFold that referenced this pull request Nov 21, 2026
…hart#2105)

Qwen3-ASR's 0.6B and 1.7B admitted from config.json: the audio tower's geometry (chunks of 100 frames, windows of
n_window_infer frames) and the Qwen3 language model's, its interleaved M-RoPE refused unless its sections span the
head (audio positions are equal on all three axes, so it is plain RoPE). Every stored tensor checked by name, shape
and dtype, none left over; the stored lm_head must be bit-equal to the tied embedding.

WhisperFeatureExtractor's log-mel rows in float64, and the tower in float64 stage by stage, either exact or at
torch's bf16 rounding points. The checkpoint ships no tokenizer.json; the shared tokenizer reads its slow export
(ashhart#2225).

tf-qwen3-asr-check compares weights, prompt ids, the reply's decode, mel rows and every tower stage with a case from
tools/zig/qwen3_asr_case.py (the qwen-asr 0.0.6 package's transformers backend, hooked stage by stage).
ashhart pushed a commit to jschmied/TensorFold that referenced this pull request Nov 22, 2026
…hart#2105)

Qwen3-ASR's 0.6B and 1.7B admitted from config.json: the audio tower's geometry (chunks of 100 frames, windows of
n_window_infer frames) and the Qwen3 language model's, its interleaved M-RoPE refused unless its sections span the
head (audio positions are equal on all three axes, so it is plain RoPE). Every stored tensor checked by name, shape
and dtype, none left over; the stored lm_head must be bit-equal to the tied embedding.

WhisperFeatureExtractor's log-mel rows in float64, and the tower in float64 stage by stage, either exact or at
torch's bf16 rounding points. The checkpoint ships no tokenizer.json; the shared tokenizer reads its slow export
(ashhart#2225).

tf-qwen3-asr-check compares weights, prompt ids, the reply's decode, mel rows and every tower stage with a case from
tools/zig/qwen3_asr_case.py (the qwen-asr 0.0.6 package's transformers backend, hooked stage by stage).
ashhart pushed a commit that referenced this pull request Nov 22, 2026
Qwen3-ASR's 0.6B and 1.7B admitted from config.json: the audio tower's geometry (chunks of 100 frames, windows of
n_window_infer frames) and the Qwen3 language model's, its interleaved M-RoPE refused unless its sections span the
head (audio positions are equal on all three axes, so it is plain RoPE). Every stored tensor checked by name, shape
and dtype, none left over; the stored lm_head must be bit-equal to the tied embedding.

WhisperFeatureExtractor's log-mel rows in float64, and the tower in float64 stage by stage, either exact or at
torch's bf16 rounding points. The checkpoint ships no tokenizer.json; the shared tokenizer reads its slow export
(#2225).

tf-qwen3-asr-check compares weights, prompt ids, the reply's decode, mel rows and every tower stage with a case from
tools/zig/qwen3_asr_case.py (the qwen-asr 0.0.6 package's transformers backend, hooked stage by stage).
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.

2 participants