[Draft] Feat/ernie tokenizer compat - #68
Closed
fxyfxy777 wants to merge 2 commits into
Closed
Conversation
`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.
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.
No description provided.