Repository navigation
fix(regex): refuse overlapping regex matches - #2671
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. |
A `\K` inside a look-behind lets fancy-regex start a match before the previous one ended. `Split` turned such matches into reversed spans, which the models slice unchecked. `SysRegex::find_iter` now errors on a match that is reversed or overlaps the previous one, so `Split` and both `Replace` components return an error.
SBrandeis
force-pushed
the
fix/segfault-tokenizer-spans
branch
from
November 19, 2026 17:33
33a15e0 to
0989616
Compare
SBrandeis
marked this pull request as ready for review
November 19, 2026 18:15
Closed
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.
Fix a segfault from a bad regex:
\Kwithin a look-behind breaks the safety assumption attokenizers/tokenizers/tk-encode/src/models/bpe/model.rs
Lines 311 to 313 in 9916e28
This PR adds a runtime check to every pattern match in Split and Replace: if a match starts before the previous one ended, encoding stops with an error instead of crashing the process.
Performance
Result: models optimized with bitcannon are not affected. Their speed is unchanged.
fancy-regexfallback, which these models do not call.main. The difference tomainis as small as the difference between two builds ofmainitself.Numbers and method
How to read the tables: "main, rebuilt" is a second build of the same
maincommit. Its row shows the noise of the setup, which reaches 1.9% here. A "slower" or "faster" figure in a "branch vs main" row only means something when it is clearly larger than the "main, rebuilt vs main" figure next to it.llama-3: bitcannon split, not affected. Every "branch vs main" figure is within its own ± interval.
clip-vit-base-patch32: a real model on the regex fallback. Its
tokenizer.jsonhas a regexReplacenormalizer and a regexSplit. The branch is 0.4% to 1.2% slower on three corpora and 2.0% faster on Chinese. The noise row, 0.5% to 1.9%, is of the same size.Method:
pipelineengine, single-thread encode) in one HF Job on an AMD EPYC 7R13, pinned to one core.mainat f73f728, a second build of the same commit, and this branch at 0989616. 20 rounds, the three builds run back to back in rotated order each round.clip-vit-base-patch32is thetokenizer.jsonofopenai/clip-vit-base-patch32at revision 3d74acf9.fancy-regexby default. All three builds were compiled with it.