Skip to content

tokenizer: \A, \z, $, \G and atomic groups, as the ERNIE 5.5 pre-tokenizer uses them - #4984

Merged
ashhart merged 3 commits into
ashhart:mainfrom
Bizuayeu:pr/tokenizer-anchors
Nov 25, 2026
Merged

ashhart merged 3 commits into
ashhart:mainfrom
Bizuayeu:pr/tokenizer-anchors

Conversation

@Bizuayeu

@Bizuayeu Bizuayeu commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

The tokenizer core's split-pattern compiler refused \A, \z, $, \G and atomic groups (?>...) with UnsupportedRegex, so the tokenizer.json of baidu/ERNIE-5.5-Flash-Preview (revision ace1100dcde7bf4eb06add6ae11ce4f43a11ddfe) did not load: three of its five Split patterns use them to group a digit run in threes from the right. It now reads them as Oniguruma reads them in Ruby syntax, the syntax the tokenizers library compiles patterns with:

  • \A and \z match at the start and the end of the piece the Split is applied to; b\z does not match ab\n (\Z stays refused).
  • $ matches at the end of the piece or before a \n, and not before the \r of \r\n.
  • \G matches where the search started, which after the first match is the previous match's end, as Oniguruma's find_iter searches; \Gb finds b twice in bbab.
  • (?>...) compiles to the atomic step that possessive quantifiers already use.

^, \Z, these escapes inside a class, and named groups stay refused. On main each of the five constructs failed at load (the parser refused $ and (?>, and its escape table refuses an alphanumeric escape it does not know), so every pattern that loaded before parses to the same tree and splits as it did.

ERNIE's first digit pattern, \p{Nd}{1,510}(?=(?>\p{Nd}{510})*(?:\P{Nd}|$))|\G\p{Nd}{510}, does work that grows with the square of a digit run's length, in the tokenizers library as well: tokenizers 0.23.3 (Python, x86-64 Windows 11) takes 35 s on a run of 1,048,576 digits and 144 s on 2,097,152. Here it stays inside the matcher's existing allowance up to about 1,400,000 digits (a scratch probe, ReleaseSafe: 22% of the allowance at 8,192 digits, 16% at 65,536, 75% at 1,048,576, 90% at 1,400,000; the share moves with the run's length mod 510), and a run of 2,097,152 digits is refused with RegexBudget instead of being split wrong. The three patterns together give the run's digits in threes from the right, so a linear path for that one shape could follow if longer runs are wanted; this pull request does not add one.

Checks on this head

  • Head commit: 7c4f403911 on main 3795e6054c. The runs below are on c7f3c86ce9, which differs from 7c4f403911 only in the number in its CHANGELOG.md line and in its commit title, except where a line names the stacked head 83d1fe7bdc: the ERNIE package's commit on this one, which adds files under zig/src/families/ernie5_5/, a build file, its fixtures script, one line in build.zig, and its own section and line in THIRD_PARTY_NOTICES.md and CHANGELOG.md, and changes none of this commit's code, tests, fixture or script.
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 165 problems on main 3795e6054c and 165 on this head, 0 new (tools/zig/lean_new.py; each tree ran its own lean_check.py, which this branch does not change). The 165 are inherited from main.
  • Tests run, with the command and pass, fail and skip counts; Zig 0.17.0:
    • x86-64 Linux (Ubuntu 24.04 under WSL 2), zig build -Dfixed-cpu test-tokenizer-host --summary all: main 3795e6054c 3 of 3 steps, 61 of 61 tests passed; this head 3 of 3 steps, 64 of 64 tests passed.
    • x86-64 Windows 11, zig build test-tokenizer-host --summary all: main 59 of 61 passed, 2 skipped; this head 62 of 64 passed, 2 skipped (the same two skip on main).
    • Planted control: this head with main's regex.zig put back: 59 passed, 2 skipped, 3 failed, the three new tests (text and line ends, the search start and atomic groups split as the tokenizers library's Oniguruma does; ERNIE 5.5's pre-tokenizer cuts digit runs into 510s and then threes from the right, as the tokenizers library does; ERNIE 5.5's 510-digit pattern, whose work grows with the square of a run, splits a long run within its allowance).
    • The expectations come from tokenizers 0.23.3: tools/zig/onig_anchor_fixtures.py --tokenizer <the release's tokenizer.json> --output <file> (the script checks the library version and the file's sha256, 20e7c81f36fdfb90db664583fc6426d5c2ac6d6bcaf631c910941a8d8f30f2d9) writes 19 anchored cases, 17 texts through ERNIE's pre-tokenizer and 8 digit runs of 509 to 1,025 digits; rerun on this head, its output equals the committed fixture byte for byte. The tests also refuse [\A], [\G], [\z], ^a, a\Z, an unclosed (?>a and a named group.
    • Scratch probes, not in this branch, with the release's tokenizer: 8 raw texts (444 ids, among them a 1,021-digit run, the protocol marker text, CJK text and ½) encode to the same ids as tokenizers 0.23.3 and decode to the same text; 4 chats from the release's chat encoding encode to its ids through encodeMasked. The stacked ERNIE package commits that check, on 20 requests.
    • x86-64 Linux, on the stacked head 83d1fe7bdc in a fresh tree: plain zig build -Dfixed-cpu: exit 0. zig build -Dfixed-cpu test test-golden test-kernel-contract fuzz-cortheon --test-timeout 20m --summary failures: 915 of 918 steps succeeded, 13,844 of 13,942 tests passed, 97 skipped, 1 failed, start_cli_test ("Ctrl-C during a deferred download has its normal process effect"), whose forked child raises SIGINT. My job runner had started the build as a background job of a non-interactive shell, so every child process ignored SIGINT and SIGQUIT; that step's test binary, built from this branch on an earlier base (1e5072adbd, where start_cli_test.zig is the same), passes 29 of 29 when run directly. With the runner restoring the default handling, the same command succeeded, 918 of 918 steps (the steps that had passed came from the cache).
    • python -m pytest -q -rs -p no:cacheprovider from the root, on Linux with pytest 9.2.0 and PyYAML 6.0.4 (what CI's job installs), Zig 0.17.0 and a clang++ on PATH: main 3795e6054c, this head and the stacked head each 808 passed, 106 skipped, 825 subtests passed, with the same skip reasons. Main's runs on 3795e6054c were cancelled before its pytest job finished; its last green pytest job, on the earlier 39931281a0, has 754 passed and 104 skipped. Two of the skips here are tools/zig/test_nfkd_tables.py ("this interpreter has Unicode 15.0.0").
    • zig fmt --check zig/src/core/tokenizer/regex.zig zig/src/core/tokenizer/regex_test.zig, zig ast-check of both, and git diff --check origin/main..HEAD: exit 0.
    • python3 tools/pr_check.py --base origin/main --pr 4984: 0 errors, 0 warnings.
  • CI: green on 7c4f403911: fmt, zig, zig-b, zig-windows, zig-macos-dev-tools, pytest (809 passed, 105 skipped, 826 subtests passed; the Linux run above has 808 and 106, and the CI log does not print skip reasons), release-paths (ubuntu-24.04), comfyui-moss-installer and both benchmark-clients jobs passed; host-nightly-linux and host-nightly-macos were skipped. The annotations are GitHub's Node.js 20 deprecation warnings and a macOS runner queue notice.
  • Not run, and why: everything on a GPU, Metal, HIP or the desktop and mobile apps (this pull request changes no code there); macOS (no Mac at hand; CI's macOS job runs the host tests).

Receipt

n/a for decode and prompt speed: the new steps run only when a split pattern uses one of the five constructs, each of which failed at load on main, so no tokenizer that loads on main splits differently (in the repository, neither of the two "Regex" fields in JSON fixtures nor any pretokenize_regex uses them). Split patterns reach the compiler from a tokenizer.json's pre-tokenizer (steps.zig) and from a Clef checkpoint's pretokenize_regex (Tokenizer.overrideSplit, called by server/hf_text.zig); both match through steps.zig's Pattern.find on a whole piece. test-golden over the frozen server corpus passed in the suite above.

  • Main against branch: n/a, as above.
  • Boxes it reaches: n/a, CPU tokenization only.
  • New switch? None.
  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: main 3795e6054c and this head, Zig 0.17.0, x86-64 Linux (Ubuntu 24.04 under WSL 2) and x86-64 Windows 11, CPU only; the tokenizer.json of baidu/ERNIE-5.5-Flash-Preview at ace1100dcde7bf4eb06add6ae11ce4f43a11ddfe for the fixture and the probes, and tokenizers 0.23.3 for the expectations.
  • Paths and platforms the change reaches, which I ran and which I could not: the two paths above, on every platform that tokenizes; I ran Linux and Windows, not macOS.
  • Exactness: n/a, no served model's tokens change.
  • Main against this branch, same machine and checkpoint: n/a, as above.
  • Decode speed before and after: n/a, as above.
  • Prompt speed before and after: n/a, the change cannot reach a pattern that loads on main.
  • Licence of any new weights or drafter: n/a. The fixture holds the release's pre-tokenizer steps, under the Apache License 2.0 on its model card; THIRD_PARTY_NOTICES.md has a section for it.

Checklist

  • The title and description match the head commit, including the receipt and the stack.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency (they fail with main's regex.zig as above; they read only embedded fixtures, so nothing makes them skip).
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.

@Bizuayeu
Bizuayeu force-pushed the pr/tokenizer-anchors branch from c7f3c86 to 7c4f403 Compare November 25, 2026 02:41
@Bizuayeu
Bizuayeu marked this pull request as ready for review November 25, 2026 04:50
@AIFlowML

Copy link
Copy Markdown
Contributor

I reran the tokenizer checks on 7c4f403911c4b3a151e61316614917a2e937ad4a on Apple silicon with Zig 0.17.0. zig build test-tokenizer-host -Dcpu=apple_m1 --summary all passed 64/64 tests. The head test-merges cleanly onto main 4c79cfe346d3c239ff7dfd7aa694df799755bafa.

I separately downloaded tokenizer.json at the ERNIE revision pinned in the PR and used tokenizers 0.23.3 in a fresh environment. python3 tools/zig/onig_anchor_fixtures.py --tokenizer tokenizer.json --output regenerated.json verified the pinned hash and generated 19 anchor cases, 17 text cases and eight digit-run cases. cmp regenerated.json zig/src/core/tokenizer/fixtures/onig_anchors.json passed byte for byte. This covers the checked-in pre-tokenizer fixtures and the host suite.

Igor Lessio - AIFLOW LABS

@Bizuayeu

Copy link
Copy Markdown
Contributor Author

cmp regenerated.json zig/src/core/tokenizer/fixtures/onig_anchors.json passed byte for byte.

(#4984 (comment))

Thank you for rerunning this. Regenerating the fixture in a fresh tokenizers 0.23.3 environment is the check I could not make myself: it shows the expected outputs come from the reference and not from my machine. The host suite on Apple silicon also covers a platform I did not run locally.

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thanks, @Bizuayeu. Previously accepted patterns retain their parser and matcher behavior; the new syntax takes branches that were previously refused. The independent 64-test result and exact fixture regeneration are useful. The digit-run budget limit and fixture attribution are clear. This tokenizer-core change looks ready; full ERNIE token-ID and model checks remain with the family slice.

  • Synthena

@Bizuayeu

Copy link
Copy Markdown
Contributor Author

This tokenizer-core change looks ready; full ERNIE token-ID and model checks remain with the family slice.

(#4984 (comment))

Thank you for the review. The ERNIE token-ID tests (plain and chat prompts against the reference tokenizer) are in #4985, which I'll rebase onto main once this lands; the model checks come with the slices after it.

ashhart and others added 3 commits November 25, 2026 10:58
…nizer uses them

ERNIE 5.5's tokenizer.json splits digit runs into 510s and then threes from the right with
`(?>...)`, `$`, `\A`, `\z` and `\G`, which the matcher refused, so the tokenizer did not load
(UnsupportedRegex). Each is now a zero-width step as Oniguruma reads it in Ruby syntax: `\A` and
`\z` are the piece's start and end, `$` is its end or a position before "\n", `\G` is where the
search started (find_iter's last end), and `(?>...)` is the atomic step possessive quantifiers
already use. A pattern that compiled before compiles to the same program.

fixtures/onig_anchors.json holds what tokenizers 0.23.3 splits for each, and for the release's
pre-tokenizer on digit texts and runs from 509 to 1,025 digits; tools/zig/onig_anchor_fixtures.py
rebuilds it from the release's tokenizer.json.
@ashhart
ashhart force-pushed the pr/tokenizer-anchors branch from 7c4f403 to 7b65724 Compare November 25, 2026 10:59
@ashhart
ashhart merged commit a3c3e5c into ashhart:main Nov 25, 2026
2 of 12 checks passed
@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you @Bizuayeu. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

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.

3 participants