Hoist special-token lookups out of per-token loops in six slow tokenizers - #47425
Merged
Rocketknight1 merged 1 commit intoJul 20, 2026
Merged
Conversation
Contributor
|
[For maintainers] Suggested jobs to run (before merge) run-slow: esm, gpt_sw3, m2m_100, marian, siglip, speech_to_text |
Contributor
CI recapDashboard: View test results in Grafana |
Rocketknight1
approved these changes
Jul 20, 2026
Rocketknight1
left a comment
Member
There was a problem hiding this comment.
Yeah, this is a clean optimization that I'm happy to merge!
Rocketknight1
enabled auto-merge
July 20, 2026 15:30
|
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. |
stevhliu
pushed a commit
to stevhliu/transformers
that referenced
this pull request
Jul 30, 2026
Sainava
pushed a commit
to Sainava/Sai-transformers
that referenced
this pull request
Aug 3, 2026
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.
What this fixes
Fixes #47424. Third instance of the pattern from #46323 and #46535:
all_special_tokens/all_special_idsare rebuilt on every property access, and six slow tokenizers read them once per token inside decode or mask loops.The fix
Hoist the lookup into a local
set(...)before each loop. Files:esm(get_special_tokens_mask),marian,m2m_100,speech_to_text,gpt_sw3,siglip(convert_tokens_to_string). 6 files, +12/-6, membership results unchanged (set vs list membership is identical for these strings/ints).Left out on purpose:
wav2vec2(same pattern, but open PR #46578 edits that line; can follow up after it lands),wav2vec2_phoneme(that check compares a str against int ids and is dead as written; noted in the issue),bartpho(same read but insave_vocabulary, not a hot path).Numbers
ESM,
get_special_tokens_maskon 20k ids (M1, CPU): 26.8 ms before, 0.3 ms after, identical mask.Tests
Notes
Not a duplicate: the only open PR mentioning
all_special_ids(#46574) is an unrelated CodeLlama decode fix; #46578 covers wav2vec2 only. AI-assisted; I reviewed every line and ran the tests and benchmark above.