feat(tokenizer): add early exit for truncation - #1990
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. |
McPatate
force-pushed
the
feat/early_exit_right_truncation
branch
from
March 26, 2026 18:15
db190ee to
a7fb4e2
Compare
Member
Author
|
So actually implementing left truncation support was pretty easy, yields a perf boost consitent with the right truncation of ~17%:
|
ArthurZucker
approved these changes
Mar 27, 2026
ArthurZucker
left a comment
Collaborator
There was a problem hiding this comment.
Mega nice actually
| continue; | ||
| } | ||
|
|
||
| let tokens = tokenize(&split.normalized)?; |
Collaborator
There was a problem hiding this comment.
If you have 1 split, HUGE ass one -> slow -> chinese don't benefit AFAIK.
If you have many small splits -> normal
If you have many big splits -> huge wins (after 2 splits boom your done).
McPatate
commented
Apr 1, 2026
ArthurZucker
approved these changes
Apr 1, 2026
ArthurZucker
added a commit
to huggingface/transformers
that referenced
this pull request
Jun 3, 2026
tokenizers 0.23.1 early-exits right-direction truncation (huggingface/tokenizers#1990), so return_overflowing_tokens may omit the trailing overflow segment when it is shorter than the window. Validate the overflow content only when that segment is present; truncation is always checked. Keeps passing on 0.22.x (segment always present).
ArthurZucker
added a commit
to huggingface/transformers
that referenced
this pull request
Jun 3, 2026
The trailing overflow segment is not essential to validate here, and 0.23.1's truncation early-exit (huggingface/tokenizers#1990) may omit it. Keep the truncation checks; remove the now-dead overflow expectations.
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.
Start tackling #1573.
Here we skip tokenizing remaining splits after the
max_lengthof theTruncationParamshas been reached in the caseTruncationDirection::Right.This leaves further questions as to how we tackle shortening the pre-tokenization pipeline as well as the tokenization. Since here we introduce the bound check on the token count, we still do the full context's pretok before the token count skip.
In the benchmark I've left for reproduction in case that is of interest to anyone, there's a
pre-truncated-input-512bench that is orders of magnitude faster, because it cuts the actual input text before passing it to the tokenizer.I also only handled right truncation as it's the simplest, need to figure out how to do left tokenization. I think I'll tackle both topics in subsequent PRs.
Benchmark improvement results (if unspecified,
direction == right):