Repository navigation
fix(tokenizer): encode non-special added tokens on the SentencePiece path - #2319
Merged
Merged
Conversation
…path The SentencePiece loader registered only `"special": true` entries of `added_tokens_decoder` for encoding, so a non-special added token decoded to its spelling but split into ordinary pieces on encode. On IQuest-Coder and IQuest-Coder Loop that turned every tool tag in a rendered tool-calling prompt into 3 to 5 content tokens instead of the reserved id the model was trained on. The encode-side split now matches every added token, special or not, the way transformers' slow tokenizer does: leftmost-longest matching followed by the per-token `rstrip`, `lstrip` and `single_word` handling, ported into the new `sp_added_vocab` module. `token_to_id` resolves non-special tokens, `skip_special_tokens` still drops only special ones, and `parse_special: false` keeps matching non-special tokens as llama.cpp and the tokenizers backend do. When a checkpoint has no `added_tokens_decoder`, `added_tokens.json` supplies plain non-special tokens, as on the tokenizers path. Validated against a transformers 4.57.1 slow-tokenizer reference on the seven catalogued SentencePiece-path tokenizers (IQuest-Coder 7B, IQuest-Coder Loop 40B, Baichuan-M1, ERNIE-4.5-VL-28B, ERNIE-4.5-0.3B, InternLM3, TeleChat3): every prompt now matches it id for id. New unit tests fail with the fix reverted; the pinned IQuest Loop test now asserts the round trip instead of the gap. Closes #1758
Review follow-ups for #1758. The lenient SentencePiece reader and the strict tokenizers-path reader now share one `added_tokens_decoder` entry parser. `token_to_id` resolves through the same added-vocabulary table encode uses, so a content declared twice (special at a lower id, non-special at a higher one) gives the same id both ways, and the now-unused special-token map is gone. The first-byte index is a flat 256-entry table instead of a hashed map. New tests cover the single_word right fold using the unstripped neighbour, a single_word token glued to another added token, multibyte contents, the duplicate-content case, and IQuest's single_word `</s>` (text inside `d</s>e`, a token in `x </s> y`). Module docs, CHANGELOG and supported-models wording now describe the parse_special and single_word effects precisely. Refs #1758
inureyes
force-pushed
the
fix/issue-1758-sp-nonspecial-added-tokens
branch
from
October 15, 2026 18:20
19d8994 to
dca9ede
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The SentencePiece fallback (
tokenizer.model, notokenizer.json) registered only"special": trueadded tokens for encoding, so"special": falseones decoded fine but split into 3 to 5 ordinary pieces on encode. It now matches everyadded_tokens_decoderentry on encode, mirroring the transformers slow tokenizer: leftmost-longest matching, then each token'srstrip/lstrip/single_wordapplied to its neighbours (newsrc/tokenizer/sp_added_vocab.rs).specialkeeps governing onlyskip_special_tokenson decode and whatparse_special: falsestops matching (non-special tokens are still matched there, as llama.cpp and the tokenizers backend do). With no decoder table,added_tokens.jsonsupplies plain non-special tokens, asread_added_tokens_sorteddoes on the tokenizers path.normalizedis ignored (the slow tokenizer reads it only underdo_lower_case). IQuest declares</s>single_word: true, sod</s>eis now text, as in transformers.Survey
Local
~/models/mlx(184 checkpoints): only the two IQuest checkpoints take the SentencePiece path; Baichuan-M1, ERNIE-4.5-0.3B and InternLM3 ship a localtokenizer.json. Remote survey of everydocs/model-catalog.tsvrepo (153 with a repo id) found seven SentencePiece-path snapshots:<think>,</think>, six tool tags)<tool_call>, FIM/repo markers,<C_A>,<B_FUNC>,<function_calling>, ...)No surveyed checkpoint has an
added_tokens.jsonentry missing from its decoder.Verification
mlxcel-server/completion, temp 0,MLXCEL_SDPA_DETERMINISTIC=1, old binary self-parity checked):~/models/mlx/iquest-coder-v1-7b-instruct-8bit: old self-parity holds. Prompts without non-special tokens give identical prompt and generated ids. The tool-calling chat prompt drops from 193 to 181 tokens and still produces theget_weathercall.~/models/mlx/iquest-coder-v1-40b-loop-instruct-4bit: same pattern; the tool-calling chat prompt goes from 205 to 185 tokens, same tool call.<C_A>is non-special: the old path echoed the question and a literal<C_A>, while the new one answers.sp_added_vocab_tests(splitter flags, plus an in-memory SentencePiece model coveringtoken_to_id, encode,skip_special_tokensandparse_special: false) andiquest_tool_prompts_encode_each_tool_tag_as_one_id;the_iquest_loop_coder_tokenizer_round_trips_its_added_tokensnow asserts the round trip. With the fix reverted (non-special entries dropped from the encode table), 5 of the new or inverted tests fail. Review follow-ups added splitter edge cases (single_word folds, multibyte contents, a duplicated content) and IQuest's single_word</s>.tokenizer::(137 pass), fmt,cargo clippy --profile test-fast --features cuda --lib --tests -D warningsand the contract checks pass.Closes #1758