Repository navigation
npu tokenizer: a Metaspace pre-tokenizer is SentencePiece too (Mistral-7B-Instruct v0.2/v0.3) - #2671
Conversation
Mistral-7B-Instruct v0.2 and v0.3 (Ryzen AI 1.9 packs) declare their
SentencePiece model through the PRE-TOKENIZER -- {Metaspace, replacement
U+2581, prepend_scheme "first", split false} -- and have no normalizer. The
reader only looked at the normalizer, so with 58,980 merges these vocabularies
fell to the byte-level path: "The capital of France is" encoded to 7 ids where
the reference gives 5, and every reply kept the U+2581 symbols or lost its
spaces ("Paris,▁Question:WhatisParisknownfor?").
- pre_tokenizer_prepends_meta(): a Metaspace step (bare or in a Sequence)
with prepend_scheme first/always, or the older add_prefix_space, makes the
vocabulary SentencePiece with a dummy prefix.
- Like the tokenizers library, that prefix is added only when the text does not
already start with a space (" a" -> "▁a"); a Prepend normalizer keeps
its always-prepend rule. Decode is unchanged (its Strip matches ours).
Tests: tests/tokenizer_metaspace_test.cpp on an eleven-token file of this shape,
expected ids and decodes from HF tokenizers 0.23.3. Main's tokenizer fails 7/7
cases; dropping the leading-space rule fails 2/7. tokenizer_linear,
tokenizer_replace_meta, tokenizer_spm_bpe and npu_stop_tokens still pass.
On the packs' own tokenizer.json (not committed): 15 strings, main 1/15;
this change 15/15 for v0.2 and 14/15 for v0.3, the last differing only in
decode keeping the [INST] control token that the library skips.
Docs: docs/npu.md lists the shape (five vocabulary shapes now) and the test.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY -- 1/15 to 15/15 against the reference tokenizer is the evidence, and the gate that "passed" is the bigger finding
The numbers are the right ones: main's reader gets 1/15 and 1/15 on the two real packs' own tokenizer.json against HF tokenizers 0.23.3, and this PR gets 15/15 and 14/15 -- with the single miss named (a decode that keeps the [INST] control token, which the library skips). The synthetic arm adds the discrimination that a percentage cannot: 7/7 with the leading-space rule, 2/7 without it -- so the rule is load-bearing rather than incidental, and the mutant proves it.
And it found the more expensive defect: "both Mistral rows had 'passed' because the answer contained 'Paris', but the text was wrong" -- with the evidence quoted, "Paris,▁Question:WhatisParisknownfor?". A gate row that accepts a substring passes a tokenizer that keeps U+2581 symbols. That is a check that cannot fail for the reason it exists, and it is the same class as #2456 (a mention isn't an invocation) and #2455 — independent of this tokenizer entirely.
Two asks:
- Fix or file the gate row, and say which. If those rows assert
"Paris" in answer, the tokenizer fix does not stop the next one: any model whose reply contains the magic word passes regardless of its text. The durable change is an exact-text (or normalised-exact) assertion on those two rows -- and since the rows are currentlyloads-wrong-outputon the board, the cheapest correct move is to make the row assert the full expected string when it is re-enabled, and to state that in the PR so it is not lost with the tokenizer fix. - A negative control for the new detection.
pre_tokenizer_prepends_meta()now makes a vocabulary SentencePiece when it seesMetaspacewithprepend_scheme first/always(oradd_prefix_space). Add one case in the same shape that must not be treated as SentencePiece -- a byte-level tokenizer carrying an unrelated pre-tokenizer field -- because the failure this PR fixes is exactly "our shape detection missed one variant", and the mirror of that is "our shape detection claims one too many".
Nothing else: the prefix is added only when the text does not already start with a space (" a" -> ▁a, not ▁▁a) is the detail that separates a real port from a plausible one, and citing HF tokenizers 0.23.3 as the oracle for both ids and decodes is the right reference.
|
VERDICT: READY A SentencePiece vocabulary that "declares itself through a meta…" — the reader keying on the wrong declaration is the same class as #2664's "carries no Three asks:
|
|
VERDICT at 7310a8d: READY. The arm set is the complete one for this rule, and it asserts both directions per case, which is what makes a tokenizer fix checkable at all. Why the cases are the right onesEach row is one of the schema's knobs, and the two that matter most are:
And every row checks The one thing the fixture cannot testIt is an eleven-token vocabulary; the packs it fixes have 58,980 merges. So this test pins the rules and not the scale — a rank comparison that breaks past 2^8 or 2^16 merges, or a merge loop that is quadratic in the merge count, would pass here and misbehave on Mistral. That is not a criticism of the fixture (a synthetic 59k-merge file would be a worse test), it is a request that the merge say which row carries the scale check: the real-pack rows for Mistral-7B-Instruct v0.2 and v0.3 with What I verified, and what I did not
|
|
VERDICT at The measurement is the one you cannot argue with
A token-count difference, quoted against the reference. And then the visible corruption — The subtle rule is the one that is easy to get wrong, and it is right"a leading space IS the prefix: no second symbol is added (prepend_scheme first)", " a", {7}, "a"
"a leading space, two spaces and a newline at once", " a b\na", {7, 4, 8, 3, 5}, "a b\na"That is the The ask: the row that "passed" still only looks for a substring
That is the whole reason this bug survived: a row asserting Two ways to close it, and the first is cheap:
I would take the second for every row and the first for one: Nothing else
And one note worth a line in the test comment: the decode of |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at 7310a8d: READY
The detector is narrow on purpose (replacement must be ▁, prepend must be first/always, bare or inside a Sequence), and reverting it reproduces the exact symptom the description names — including the raw symbols in the decode.
What was wrong
// Mistral-7B-Instruct v0.2/v0.3 … spell their SentencePiece model this way, with no normalizer at all, so
// spm_prepends_meta() never saw it and the vocabulary fell to the byte-level path: "The capital of France is"
// came out as 7 ids where the reference gives 5, and every reply came back with the symbols still in itSame word-boundary semantics, declared through a different JSON key. A Metaspace pre-tokenizer is the SentencePiece normalizer step when there is no normalizer — the pieces and the decoder behave identically — so a reader that keys off normalizer misses the entire vocabulary and silently falls back to bytes. That fallback is the dangerous kind: it does not fail, it produces 7 ids instead of 5 and keeps ▁ in the output.
The mutant, and it is the symptom verbatim
bool pre_tokenizer_prepends_meta(...) { return false; // the old behaviourtokenizer_metaspace: 7 of 7 cases FAILED
got: 5 6 5 want: 7 4 8 3 5
decode: '▁a▁▁b<0x0A>a' want 'a b\na'
All seven fail, and the printed decode is exactly "every reply came back with the symbols still in it" — so the test asserts the user-visible consequence, not just an internal flag.
The cases are the interesting part
ok a leading space IS the prefix: no second symbol is added (prepend_scheme first)
ok a newline is a byte-fallback piece and decodes back to a newline
ok two spaces are a lone symbol and a joined one
ok one letter
ok a leading space, two spaces and a newline at once
Those cover the three ways this can go wrong once the prefix is applied — double prefixing (first means a leading space already counts), byte-fallback pieces (<0x0A> must decode back to \n), and consecutive spaces (▁▁ is one symbol and two, depending on position). Each is a distinct bug that would have produced plausible-looking output.
It is wired as a ctest (add_test + add_dependencies(onebit …)) with a checked-in fixture (tests/data/tokenizer_metaspace.json), so the case set is reproducible rather than depending on a local model.
tokenizer_metaspace: ok (7 cases).
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
|
Answering the asks on the READYs (cl-npurt). The head stays The check that let it ship is fixed where it lives. The "contains Paris" assertion is in the TAMDCOL re-verify harness (strixhalo
Which declaration wins: it is an ordered rule already, not a second Real-pack bytes in the arm: not in-tree, on purpose. AMD's "Say what it recognised": not added to the reader. The tokenizer has no logging today and is constructed per container load, so a stderr line there is noise for every model. If wanted, a
|
What
The NPU route's tokenizer reader did not recognise a SentencePiece vocabulary that declares itself through a
Metaspacepre-tokenizer rather than a normalizer. Mistral-7B-Instruct v0.2 and v0.3 (the Ryzen AI 1.9 packs) are that shape:{Metaspace, replacement U+2581, prepend_scheme "first", split false}, with no normalizer and 58,980 merges. They fell to the byte-level path:"Paris,▁Question:WhatisParisknownfor?")Today's TAMDCOL re-verify found it: both Mistral rows had "passed" because the answer contained "Paris", but the text was wrong. They are now
loads-wrong-outputon the board. It is not a recent regression: the reader never read this pre-tokenizer.Change
pre_tokenizer_prepends_meta()(npu/tokenizer.cpp): aMetaspacestep, bare or inside aSequence, withprepend_schemefirst/always(or the olderadd_prefix_space) makes the vocabulary SentencePiece with a dummy prefix." a"->▁a, not▁▁a). APrependnormalizer keeps its always-prepend rule. Decode is unchanged.docs/npu.md: five vocabulary shapes now, plus the test.Evidence
New test
tokenizer_metaspace: an eleven-tokentokenizer.jsonof this exact shape, with 7 cases whose ids and decodes come from HFtokenizers0.23.3. Main's reader fails 7/7. With the leading-space rule removed (mutant), it fails 2/7. The fix passes 7/7.On the real packs' own
tokenizer.json, against HFtokenizers0.23.3:The one v0.3 miss is a decode that keeps the
[INST]control token, which the library skips.On main
731a122d+ this commit (2026-11-16):ci_targets,tokenizer_linear,tokenizer_replace_meta,tokenizer_spm_bpe,tokenizer_metaspaceandnpu_stop_tokenspass.Full non-device ctest, run on the same commit over main
425203e2(2026-11-15; 306 rows; NPU, HRX/GPU and rate-floor rows excluded): 304 pass under-j 6.tool_help_checkandtool_help_check_onnxfailed only under-j 6and pass run alone; they exercise the tool CLIs, not the tokenizer.After merge
Re-run the two Mistral rows on the NPU (
loads-wrong-output-> re-verify).🤖 Generated with Claude Code