Skip to content

fix(regex): refuse overlapping regex matches - #2671

Merged
SBrandeis merged 1 commit into
mainfrom
fix/segfault-tokenizer-spans
Nov 19, 2026
Merged

SBrandeis merged 1 commit into
mainfrom
fix/segfault-tokenizer-spans

Conversation

@SBrandeis

@SBrandeis SBrandeis commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

Fix a segfault from a bad regex:
\K within a look-behind breaks the safety assumption at

// SAFETY: the pre-tokenizer cuts on char boundaries, so a span is always a valid slice
// of this chunk. Bounds- and UTF-8-checking it again per word measured worth removing.
let sequence = unsafe { chunk.get_unchecked(span.range()) };

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.

  • bitcannon models never run the new check. GPT-2, cl100k (Llama-3, Qwen, GLM), o200k (gpt-oss, Llama-4), Tekken, Kimi and DeepSeek split with a native bitcannon grammar. The check lives only in the fancy-regex fallback, which these models do not call.
  • Measured on Llama-3: same speed as main. The difference to main is as small as the difference between two builds of main itself.
  • The regex fallback, measured on CLIP: about 1% slower, which is the size of the noise. CLIP is a model whose regexes bitcannon does not cover, so it runs the new check on every match.
Numbers and method

How to read the tables: "main, rebuilt" is a second build of the same main commit. 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.

english chinese agentic-swe chat-deepseek
main 100.5 ± 2.3 MiB/s 29.5 ± 0.4 MiB/s 152.7 ± 1.6 MiB/s 154.4 ± 2.0 MiB/s
main, rebuilt 99.0 ± 2.7 MiB/s 29.1 ± 0.5 MiB/s 152.9 ± 1.0 MiB/s 154.3 ± 3.5 MiB/s
branch 99.5 ± 2.1 MiB/s 29.4 ± 0.3 MiB/s 153.1 ± 1.1 MiB/s 157.5 ± 2.4 MiB/s
main, rebuilt vs main 0.5% faster ± 2.7 0.3% slower ± 1.2 0.7% faster ± 1.4 0.5% slower ± 2.3
branch vs main 1.4% faster ± 2.7 0.9% faster ± 1.9 0.4% faster ± 1.0 1.9% faster ± 2.1

clip-vit-base-patch32: a real model on the regex fallback. Its tokenizer.json has a regex Replace normalizer and a regex Split. 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.

english chinese agentic-swe chat-deepseek
main 15.1 ± 0.1 MiB/s 15.1 ± 0.1 MiB/s 17.5 ± 0.1 MiB/s 17.1 ± 0.1 MiB/s
main, rebuilt 15.2 ± 0.1 MiB/s 15.4 ± 0.1 MiB/s 17.7 ± 0.1 MiB/s 17.2 ± 0.1 MiB/s
branch 15.0 ± 0.1 MiB/s 15.5 ± 0.1 MiB/s 17.3 ± 0.1 MiB/s 16.9 ± 0.1 MiB/s
main, rebuilt vs main 0.7% faster ± 0.5 1.9% faster ± 0.9 0.5% faster ± 0.5 0.6% faster ± 0.6
branch vs main 0.4% slower ± 0.4 2.0% faster ± 0.6 1.2% slower ± 0.5 1.1% slower ± 0.6

Method:

  • Measured with tokbench (pipeline engine, single-thread encode) in one HF Job on an AMD EPYC 7R13, pinned to one core.
  • Three builds: main at 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.
  • Values are the median over the rounds ± half the 95% bootstrap interval. The "vs main" rows are the median of the per-round ratios.
  • clip-vit-base-patch32 is the tokenizer.json of openai/clip-vit-base-patch32 at revision 3d74acf9.
  • tokbench does not enable fancy-regex by default. All three builds were compiled with it.
  • The ids were identical across the three builds in every round, and no cell produced an empty output.

@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.

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
SBrandeis force-pushed the fix/segfault-tokenizer-spans branch from 33a15e0 to 0989616 Compare November 19, 2026 17:33
@SBrandeis
SBrandeis marked this pull request as ready for review November 19, 2026 18:15
@SBrandeis
SBrandeis merged commit 17fc348 into main Nov 19, 2026
35 checks passed
@SBrandeis
SBrandeis deleted the fix/segfault-tokenizer-spans branch November 19, 2026 18:38
@SBrandeis SBrandeis mentioned this pull request Nov 19, 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.

2 participants