Repository navigation
fix(Tokenizers): avoid quadratic regex matching for added tokens without whitespace padding - #386
Merged
pcuenca merged 4 commits intoOct 29, 2026
Conversation
…out whitespace padding
pcuenca
reviewed
Oct 29, 2026
pcuenca
left a comment
Member
There was a problem hiding this comment.
Hi @shubhransh-gupta, thanks a lot for your contribution, and sorry for the delay 🙏
Your optimization seems plausible, but the tests you propose are not checking for correctness or performance. I'm happy to merge without them and rely on the existing test corpus, as long as you can provide some indication about performance improvements you measured. Would you happen to have something handy?
| #expect(tokenizerOpt != nil) | ||
| let tokenizer = tokenizerOpt! | ||
|
|
||
| // Verify that added tokens and newlines are matched and encoded properly without quadratic explosion |
Member
There was a problem hiding this comment.
This doesn't really verify the quadratic explosion claim.
Member
|
Actually, #383 provides sufficient evidence, I'll add @xocialize as a co-author to this PR. |
pcuenca
reviewed
Oct 29, 2026
pcuenca
approved these changes
Oct 29, 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.
Fixes #383.
Summary
In
PreTrainedTokenizer,addedTokensRegexwas constructed by wrapping every added token in a capturing group(\(token))even when neitherlstripnorrstripwas requested.For tokenizers with large added token vocabularies (e.g. Google Gemma-3 with 6,415 added tokens), this caused an$O(N^2)$ quadratic slowdown in Foundation's ICU regular expression engine due to capturing group table allocations, slowing down single newline
\nencoding from ~0.02 ms to ~29 ms (~1.1s for a 40-line prompt on device).Changes
$0.prefixand$0.suffixare bothfalse, compile the token using a non-capturing group(?:\(token))instead of(\(token)).lstriporrstripstill retain their capturing group sosplit(by: NSRegularExpression)continues to isolate and strip surrounding whitespace as expected.unwrappedAddedTokensis empty.addedTokensRegexPerformanceAndCorrectnessinTests/TokenizersTests/TokenizerTests.swift.Verification
swift testacross all targets (50/50 tests passing).swift-format lint --strict --recursive Sources Tests(clean, 0 warnings).