Repository navigation
[Fix] Sparse TikToken tokenizers silently fail - #48446
Merged
Merged
Conversation
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
Merged
ArthurZucker
approved these changes
Sep 1, 2026
remi-or
force-pushed
the
tokenizer-warn
branch
from
September 1, 2026 10:48
a8da111 to
d631f32
Compare
remi-or
enabled auto-merge
September 1, 2026 10:53
Contributor
CI recapDashboard: View test results in Grafana |
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Sep 1, 2026
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Sep 1, 2026
sbucaille
pushed a commit
to sbucaille/transformers
that referenced
this pull request
Sep 16, 2026
* Remove sentencepiece warning for tiktiokens * Fix non contiguous * Fix potential None * Added test * Inline a single use fn * Better size estimation
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.
This PR addresses an issue when loading a tiktoken tokenizer that has holes in its vocabulary.
Currently, when loading a tiktoken tokenizer, if there are tokens in
added_tokens_decoderthat are not in the base vocabulary, they will be added contiguously at the end of the vocab. For instance, Kimi 2.7 has a base vocab size of 163584 and 23 tokens inadded_tokens_decoder: these tokens are added contiguously at the end of the vocab, increasing its final size to 163607. But if we look at the ids of those tokens, they don't match with where they end up: for instance,[PAD]ends up in 163606 instead of 163839. That seems impossible, because 163839 is strictly greater than 163607, the vocab size.This is because the tokenizer was constructed differently: instead of adding extra tokens contiguously, moonshot added a block of 256 token ids at the end of their vocab, making its length 163840, and hen picked some ids in that block for special tokens. So we need to do the same: we look at the maximum id in
added_tokens_decoder, and create a block of placeholder so the vocabulary extends to this id, and we populate that block with the real tokens. This means there will be placeholders left, as is the case withllamaAlso, this PR bypasses the try/except block where we try a SentencePiece conversion when the name of the vocab file is
tiktoken.modeland directly runs a tiktoken conversion in that case. This avoids a nasty warning. For this reasons, I refactored the code for the conversion in their dedicated class methods, which makes the code a bit more readable IMO. This is the major part of the delta but it does not change the underlying behavior.Example script: https://gist.github.com/remi-or/7a0447afdaf432df8e8bdfc399e6eddc
On main:
On this branch: