Skip to content

Tokenizer: NFKC/NFKD normalization, and normalized added tokens decode as declared - #1931

Merged
ashhart merged 3 commits into
ashhart:mainfrom
laudney:pr-ocr-tokenizer-nfkc
Nov 2, 2026
Merged

ashhart merged 3 commits into
ashhart:mainfrom
laudney:pr-ocr-tokenizer-nfkc

Conversation

@laudney

@laudney laudney commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Part of #1927.

Nemotron Parse's tokenizer declares an NFKC normalizer, which the tokenizer does not implement today. Its added tokens are also normalized, and that shows a second difference from the Rust tokenizers library: we decode a normalized added token by its normalized match key, but tokenizers decodes it by the content the checkpoint declares. The two commits fix these two problems separately, so that the second commit, which changes behaviour, can be reviewed on its own.

Before (main) After (this PR)
Tokenizer with an NFKC or NFKD normalizer (e.g. Nemotron Parse) fails to load: UnsupportedNormalizer loads; Unicode 9 forms as in Rust tokenizers
NFC, NFD supported unchanged
NFKC/NFKD checks against Rust tokenizers goldens none 198 curated cases (NFC, NFD, NFKC and NFKD each), plus every compatibility mapping in Zig
Decoded text of a normalized added token its normalized match key its declared spelling, as tokenizers decodes it (commit 2)
Tokenizer identity digest — changes only for models whose normalizer rewrites one of their normalized added tokens

1. fix(tokenizer): Unicode 9 NFKC and NFKD

  • unicode_data.zig gains the compatibility decomposition table. It is generated by tools/zig/unicode_tables.py from the same Unicode 9 tables that tokenizers uses, and --check confirms that the committed file matches its generator. unicode.zig adds compatibility decomposition (recursive), Hangul decomposition and composition, and canonical ordering and composition for .nfkc and .nfkd. steps.zig maps the NFKC and NFKD normalizer types.
  • fixtures/nfkc.json holds 198 curated cases from Rust tokenizers 0.22.2: for each compatibility tag, evenly spaced cases plus the first, the last and the longest; compatibility and half-width jamo; half-width voiced sound marks; and hand-made composition-boundary strings. Each case checks NFC, NFD, NFKC and NFKD. tools/zig/nfkc_fixtures.py regenerates exactly this sample.
  • A Zig test goes through every entry of the compatibility table and checks that NFKC and NFKD change the code point and are idempotent. Thus the full table stays covered without a 25k-line fixture.
  • NFC and NFD do not change for any existing model.

2. fix(tokenizer): decode normalized added tokens by their spelling

  • The match key stays normalized. Decoding now returns the added token's declared content, as tokenizers' added_tokens_map_r does. A WordLevel test with an NFKC normalizer checks both spellings: they encode to the same ID and decode to the declared text.
  • Behaviour change: a model whose normalizer rewrites one of its normalized added tokens now decodes that token as the checkpoint spells it. Its tokenizer identity digest therefore changes once. A model whose normalizer does not rewrite its added tokens is unaffected.

Gates

  • zig test zig/src/core/tokenizer/tokenizer.zig: 42/42 after commit 1, and 43/43 after commit 2.
  • unicode_tables.py --check passes.
  • zig build test with this PR on the prompt-cache PR: 465/465 steps, 6,177/6,222 tests (45 skipped).
  • Lean checker: zero new problems over the 425 baseline.

@ashhart

ashhart commented Nov 2, 2026

Copy link
Copy Markdown
Owner

Thanks @laudney. We checked what commit 2 would change for the models we serve. Across the tokenizers of the 27B, 9B and 2B, Flash Next (MLX and FP4-Local), GLM-5.3-Flash, DeepSeek-V4.1, Gemma 4 (12B and 31B), Nemotron Lightning and Bolt, Ornith, Spark-X and drama3, only DeepSeek-V4.1 has normalized added tokens (47), and its normalizer leaves every one unchanged. So no model we ship decodes differently, and it matches Hugging Face's tokenizers for the ones that would. It's in our gate and lands in the next cycles, with #1929 and #1930.

laudney and others added 3 commits November 2, 2026 16:45
Some checkpoints, such as Nemotron Parse, declare NFKC normalization,
which the native tokenizer could not apply. Add Unicode 9 NFKC and
NFKD forms, generated from the Unicode tables the tokenizers library
uses, while NFC and NFD stay unchanged for every existing model. A
small fixture, a curated sample that nfkc_fixtures.py selects by its
rule from Rust tokenizers goldens, checks all four forms, and a Zig
test checks that every compatibility mapping changes its code point
under NFKC and NFKD and that both forms are idempotent.
A normalized added token was decoded by its normalized match key, but
the tokenizers library decodes it by the content the checkpoint
declares. The match key stays normalized and decoding returns the
original spelling. This changes decoded text, and so the tokenizer
identity digest, for any model whose normalizer rewrites one of its
normalized added tokens.
@ashhart
ashhart force-pushed the pr-ocr-tokenizer-nfkc branch from 0742a4c to 52c2012 Compare November 2, 2026 16:45
@ashhart
ashhart merged commit 81edd82 into ashhart:main Nov 2, 2026
5 checks passed
@ashhart

ashhart commented Nov 2, 2026

Copy link
Copy Markdown
Owner

Thank you @laudney. 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.

@laudney
laudney deleted the pr-ocr-tokenizer-nfkc branch November 2, 2026 21:03
@ashhart

ashhart commented Nov 3, 2026

Copy link
Copy Markdown
Owner

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