Skip to content

Hoist special-token lookups out of per-token loops in six slow tokenizers - #47425

Merged
Rocketknight1 merged 1 commit into
huggingface:mainfrom
ishan-1010:fix/hoist-special-token-lookups
Jul 20, 2026
Merged

Hoist special-token lookups out of per-token loops in six slow tokenizers#47425
Rocketknight1 merged 1 commit into
huggingface:mainfrom
ishan-1010:fix/hoist-special-token-lookups

Conversation

@ishan-1010

@ishan-1010 ishan-1010 commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

CI

What this fixes

Fixes #47424. Third instance of the pattern from #46323 and #46535: all_special_tokens / all_special_ids are 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 in save_vocabulary, not a hot path).

Numbers

ESM, get_special_tokens_mask on 20k ids (M1, CPU): 26.8 ms before, 0.3 ms after, identical mask.

Tests

python -m pytest tests/models/{esm,marian,m2m_100,speech_to_text,gpt_sw3,siglip}/test_tokenization_*.py
# 167 passed, 117 subtests passed; remaining failures/errors are identical on unmodified main (network/fixture dependent)

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.

@github-actions

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: esm, gpt_sw3, m2m_100, marian, siglip, speech_to_text

@github-actions

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 29723351253
Result: success | Grafana metrics are not available yet.

@Rocketknight1 Rocketknight1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is a clean optimization that I'm happy to merge!

@Rocketknight1
Rocketknight1 enabled auto-merge July 20, 2026 15:30
@Rocketknight1
Rocketknight1 added this pull request to the merge queue Jul 20, 2026
@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

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 via the queue into huggingface:main with commit 7155497 Jul 20, 2026
42 checks passed
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Several slow tokenizers read all_special_tokens or all_special_ids once per token in decode loops

3 participants