Repository navigation
npu tokenizer: a SentencePiece vocabulary that keeps its merges is merged by rank (closes #1189) - #1209
Conversation
…rged by rank, not byte-level BPE (closes #1189) The SentencePiece test was `merges.empty() && has_meta`. A SentencePiece model exported to tokenizer.json keeps its merges as ["\u2581","t"] pairs, so a pack whose normalizer is {Prepend \u2581, Replace space -> \u2581} and which ships 61,249 merges took the byte-level GPT-2 path, byte-mapping a metaspace the vocabulary does not spell byte-wise. Measured on the Phi-3.5-mini OGA pack (engine#1189): "The capital of France is" encoded to 11 ids instead of the pack's 5 (450 7483 310 3444 338), so the model was fed off-distribution ids and the same container answered 'Awesome' (top-1 'A', margin +0.032, near-flat logits) instead of 'Paris'. Two changes, both narrow: * detection also accepts a non-empty merge list when the normalizer PREPENDS the metaspace -- the prefix spm() synthesises anyway. A normalizer that only rewrites spaces (ZAYA, laya) does not ask for one and is left exactly as it was; * such a vocabulary is encoded by merging the lowest-ranked adjacent pair, leftmost first, over the normalised text -- the same heap the byte-level BPE already uses, with the <0xNN> byte fallback the model's decoder reverses. Greedy longest-match is not a substitute: it disagrees with the reference on 6 of 20 measured strings of this pack's vocabulary. Before/after, same container, same prompt text: before 11 prompt ids, top-1 'A', margin +0.032, mean +27.778 after 5 prompt ids, top-1 'Paris', margin +4.491, mean +36.776 Tests: tests/data/spm_bpe_tokenizer.json (+ golden from Hugging Face `tokenizers` on that same file, so the expectation is an outside implementation) is 0/10 before and 10/10 after. The Phi-3.5 pack's own 30-case golden is 29/30 (the miss is a literal-metaspace round trip that Hugging Face loses too), qwen3 30/30, llama-3.2-1B 30/30 with specials excluded, the Zamba2 family's tokenizer.json 8/8, and the linear-time tokenizer test is unchanged.
|
Two notes for the reviewer, both measured after the PR was opened:
|
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY at 5df645ef.
The bug is real and I reproduced it on the real pack, not the fixture. The box has the actual
models/oga/Phi-3.5-mini-instruct_rai_1.8.0_npu_4K/tokenizer.json: model.type BPE, 61,249 merges,
normalizer Sequence[Prepend "▁", Replace " " -> "▁"], pre_tokenizer null, decoder Replace + ByteFallback —
exactly the shape the commit message describes. Oracle built with Hugging Face tokenizers 0.23.3 on that
same file, 19 round-trip-safe strings, tokenizer_test against it:
main (be8df65b), same oracle: 1/19 cases match (rc 1)
this head (5df645ef): 19/19 cases match (rc 0)
"The capital of France is" — HF wants [450,7483,310,3444,338] (5 ids, as the commit says); main produced
[1576,31937,5030,2410,31937,974,31937,16066,31937,275]. So the classification slip is as described and the fix
closes it.
The fixture's golden is genuinely HF-derived: re-encoded all 10 spm_bpe_tokenizer_golden.json cases with
tokenizers 0.23.3 on spm_bpe_tokenizer.json — 10/10 byte-identical to the committed ids. ctest -R tokenizer
on this head is 2/2 (the new tokenizer_spm_bpe exists only here; main has 1 tokenizer test), and tokenizer_test
exits 1 on failure (not the #657 zero-case-exits-0 shape).
No regression on the two paths that must not move. Qwen3-0.6B (byte-level GPT-2 vocab, Ġ escapes, no ▁) —
main 14/14, this head 14/14 on tests/data/qwen3_tokenizer_golden.json. Merge-less SPM path
(tokenizer_linear_test, and spm_ = merges.empty() still short-circuits spm_merges_) — identical output on
both arms: many pre-tokens 1048577 -> 1048577 ids, many added tokens 1048590 -> 139812, all cases ok.
Your "greedy is not a substitute" claim is right, and I measured it rather than taking it: rebuilt this head
with spm_merges_ forced to false (one line, nothing else) and re-ran the same oracle — 11/19, i.e. 8 strings
wrong, against 19/19 with rank merging.
Nits, none blocking:
- The commit message says the wrong-path encode was "11 ids"; I measure 10 on this box for that string with
this same oracle (ids above). Worth correcting so the number survives re-derivation. - "disagrees with the reference on 6 of 20 strings" for greedy — my set of 19 gives 8, count
FAILlines in
the forced-off run. The direction is proven; the exact count depends on your string set, so it should be named
by set or dropped. - The classification has a silent branch with no test: a vocabulary with merges whose normalizer is
{Replace " " -> "▁"}withoutPrependdeliberately stays on the byte-level path. One more fixture case
(Replaceonly, merges kept, ids from HF) would make that branch falsifiable instead of documented.
Measured on pi prs, no model, no device, CPU only (thermal-run, Tctl <= 63 C).
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
Root cause
npu/tokenizer.cppdecided "this vocabulary is SentencePiece" fromA SentencePiece model exported to
tokenizer.jsonkeeps its merges (as["▁","t"]pairs), so apack whose normalizer is
{Prepend "▁", Replace " " -> "▁"}and which ships 61,249 merges fell throughto the byte-level GPT-2 path. That path byte-maps each byte of the metaspace (
▁=E2 96 81) intocharacters the vocabulary does not spell byte-wise, so the text was encoded into pieces the model has
never seen.
Measured on the Phi-3.5-mini OGA pack (the pack named in #1189):
tokenizer.json(Hugging Facetokenizers)450 7483 310 3444 338'A', margin +0.032, mean +27.778, then'Awesome''Paris', margin +4.491, mean +36.776, then'.'So the model was never wrong: it was fed off-distribution ids. Same container, same weight tiles, both
runs host-GEMM, only the prompt text path changed. The
'Awesome'/<0x0A>garbage in the issue isthis bug's downstream symptom (the model emitting byte-fallback tokens).
The fix
Two narrow changes:
the prefix
spm()synthesises anyway. A normalizer that only rewrites spaces (ZAYA, laya) does notask for a one and is left exactly as it was; byte-level vocabularies (Qwen, Llama, LFM, Gemma) are
untouched because they carry
Ġ, not▁.normalised text — the same heap the byte-level BPE already uses, plus the
<0xNN>byte fallback themodel's decoder reverses. Greedy longest-match is not a substitute: it disagrees with the reference
on 6 of 20 measured strings of this pack's vocabulary, so a merge-list tokenizer is merged, not
matched.
Verification
tests/data/spm_bpe_tokenizer.json+ golden generated by Hugging Facetokenizerson that same file (an outside implementation, not a transcript of our output):0/10 before, 10/10 after. Wired as ctest
tokenizer_spm_bpe; needs no model.Phi-3.5 pack 29/30 (the one miss is a literal-metaspace string whose round trip Hugging Face
itself loses), Qwen3-1.7B 30/30, Llama-3.2-1B 30/30 (specials excluded — the engine adds BOS
from
config.json), Zamba2-1.2B 8/8 (same shape, previously wrong for the same reason).tokenizer_linearstill passes: the rank merge is the heap path, O(n log n), so a long prompt doesnot go quadratic.
Oracle cross-check (same hour, AMD's own 1.8.0 stack on the same pack,
npu-holdticket, 12 s,amdxdna ERROR 0 -> 0).,and\n;The capital of France is Paris..,and\n;Paris.Identical top-5 order, std and margin, and AMD's own mean logit is +40.5 — i.e. the "+25-nat
offset" the issue treated as a candidate defect is this pack's normal behaviour, not our bug.
Closes #1189.