Skip to content

[Draft] Feat/ernie tokenizer compat - #68

Closed
fxyfxy777 wants to merge 2 commits into
crusoecloud:mainfrom
fxyfxy777:feat/ernie-tokenizer-compat
Closed

[Draft] Feat/ernie tokenizer compat#68
fxyfxy777 wants to merge 2 commits into
crusoecloud:mainfrom
fxyfxy777:feat/ernie-tokenizer-compat

Conversation

@fxyfxy777

Copy link
Copy Markdown

No description provided.

`PreTokenizerConfig::Digits` was already parsed by json_structs.rs but had
no runtime arm, so any tokenizer.json containing a `Digits` pre-tokenizer
failed to load with `Unsupported("Digits")`. This affects Baidu ERNIE
tokenizers, whose pre_tokenizer is
`Sequence[Digits, Split x5, ByteLevel]`.

HF's Digits splits on `char::is_numeric()`, i.e. Unicode Nd | Nl | No.
`individual_digits: false` maps to `SplitDelimiterBehavior::Contiguous`
(a run of digits stays one piece), `true` maps to `Isolated` (each digit
becomes its own piece). Both are expressible with the existing Split
engine, so reuse it instead of adding another runtime variant.
HF `tokenizers` compiles `Split { pattern: Regex }` with Oniguruma, which
reads a quantifier immediately followed by `+` as "repeat the quantified
atom one or more times": `\p{Nd}{3}+` means `(?:\p{Nd}{3})+`. PCRE2 and
fancy-regex read the same syntax as a *possessive* `{3}` -- exactly three,
never backtrack. Both compile without error, so a tokenizer.json authored
against Oniguruma silently tokenizes differently here.

ERNIE's thousands-grouping rule is a minimal reproduction:

    \A\p{Nd}{1,2}(?=\p{Nd}{3}+\z)

Under Oniguruma the lookahead accepts any multiple of three trailing
digits, so `1000000` groups from the right as `1|000|000`. Under
possessive semantics the lookahead requires exactly three digits and
matches nothing, so grouping falls back to left-to-right: `100|000|0`.
Inputs whose digit count is divisible by 3 happen to agree, which is why
this hides easily.

Normalize `X{n}+` / `X{n,}+` / `X{n,m}+` to `(?:X{n})+` before compiling.
This is done at `Split::from_parts`, the single choke point through which
every pattern reaches the regex engines, so the native, PCRE2 and scan
paths all see the same source. The rewrite is a no-op for patterns without
a double quantifier (it returns the input untouched when nothing matched),
and `atom_start_before` handles groups `(...)`, classes `[...]`,
`\p{...}`/`\x{...}`, and single chars/escape pairs.
@fxyfxy777 fxyfxy777 changed the title Feat/ernie tokenizer compat [Draft] Feat/ernie tokenizer compat Sep 9, 2026
@fxyfxy777 fxyfxy777 closed this Sep 9, 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.

1 participant