Testing NFC and NFD inputs against pinned BERT tokenizers
Use unicodedata to compare canonically equivalent strings; discuss accent stripping, cased preservation, and offsets
Start with two strings that look identical but contain different Unicode code-point sequences. The diagnostic below changes only NFC versus NFD form, loads only the two approved revisions, and compares token IDs, token strings, unknown-token use, model-input boundary tokens, and source offsets. The engineering problem is narrow: can a normalization change upstream of inference alter the token sequence produced by a fixed BERT tokenizer, and can the resulting pieces still be traced to the submitted text? This procedure does not measure model quality or make a universal claim about canonical equivalence.
Verified: 2026-10-05. The applicable matrix is google-bert/bert-base-uncased@86b5e0934494bd15c9632b12f734a8a67f723594 and google-bert/bert-base-multilingual-cased@3f076fdb1ab68d5b2880cb87a0886f315b8146f8, both read through transformers==4.56.2. The evidence boundary is the pinned English tokenizer.json, the pinned multilingual tokenizer.json, and the Transformers v4.56.2 tokenizer API. The JSON files govern normalization and WordPiece data; the API reference governs encoding options. The code is a procedure to run, not a transcript of a run.
Establish canonical equivalence
Establish equivalence with unicodedata, rather than trusting font rendering. NFC composes canonically equivalent sequences where possible; NFD decomposes characters where possible. For the fixed sample below, the assertions prove that normalizing the NFD string back to NFC recovers the NFC string, and vice versa. That is the only input-space guarantee being tested. It does not claim that NFD is invalid, recommend a project-wide normalization form, or imply that two different strings should share tokens.
Runnable diagnostic
Run this in a clean Python 3 environment after installing transformers==4.56.2. Access to the two Hub repositories, or equivalent cached files, is required. Every displayed sample token count is computed locally.
import unicodedata
import tokenizers
import transformers
from transformers import AutoTokenizer
SOURCE = "Café café 東京 🧪."
NFC_TEXT = unicodedata.normalize("NFC", SOURCE)
NFD_TEXT = unicodedata.normalize("NFD", SOURCE)
assert NFC_TEXT != NFD_TEXT
assert NFC_TEXT == unicodedata.normalize("NFC", NFD_TEXT)
assert NFD_TEXT == unicodedata.normalize("NFD", NFC_TEXT)
CHECKS = (
(
"google-bert/bert-base-uncased",
"86b5e0934494bd15c9632b12f734a8a67f723594",
),
(
"google-bert/bert-base-multilingual-cased",
"3f076fdb1ab68d5b2880cb87a0886f315b8146f8",
),
)
assert transformers.__version__ == "4.56.2", transformers.__version__
print(
f"transformers={transformers.__version__}; "
f"tokenizers-backend={tokenizers.__version__}"
)
def visible(text):
return repr(text)
print(f"NFC source: {visible(NFC_TEXT)}")
print(f"NFD source: {visible(NFD_TEXT)}")
for model_id, full_commit in CHECKS:
FULL_COMMIT = full_commit
tokenizer = AutoTokenizer.from_pretrained(
model_id,
revision=FULL_COMMIT,
use_fast=True,
)
assert tokenizer.is_fast
def encode(text, add_special_tokens):
return tokenizer(
text,
add_special_tokens=add_special_tokens,
return_offsets_mapping=True,
padding=False,
truncation=False,
)
def token_strings(input_ids):
return tokenizer.convert_ids_to_tokens(input_ids)
def trace(encoded, source):
pieces = []
ids = encoded["input_ids"]
tokens = token_strings(ids)
for token, token_id, (start, end) in zip(
tokens, ids, encoded["offset_mapping"]
):
assert 0 <= start <= end <= len(source)
raw_piece = visible(source[start:end])
pieces.append(
f"{token!r}[{token_id}] {start}:{end}={raw_piece}"
)
return " | ".join(pieces)
nfc_content = encode(NFC_TEXT, add_special_tokens=False)
nfd_content = encode(NFD_TEXT, add_special_tokens=False)
nfc_model_input = encode(NFC_TEXT, add_special_tokens=True)
nfd_model_input = encode(NFD_TEXT, add_special_tokens=True)
nfc_ids = nfc_content["input_ids"]
nfd_ids = nfd_content["input_ids"]
nfc_model_ids = nfc_model_input["input_ids"]
nfd_model_ids = nfd_model_input["input_ids"]
nfc_tokens = token_strings(nfc_ids)
nfd_tokens = token_strings(nfd_ids)
nfc_model_tokens = token_strings(nfc_model_ids)
nfd_model_tokens = token_strings(nfd_model_ids)
assert nfc_model_tokens[0] == tokenizer.cls_token
assert nfc_model_tokens[-1] == tokenizer.sep_token
assert nfd_model_tokens[0] == tokenizer.cls_token
assert nfd_model_tokens[-1] == tokenizer.sep_token
assert nfc_model_ids[1:-1] == nfc_ids
assert nfd_model_ids[1:-1] == nfd_ids
print(f"\n{model_id}@{FULL_COMMIT}")
print(
f"NFC content={len(nfc_ids)}, model-input={len(nfc_model_ids)}, "
f"unknown={nfc_tokens.count(tokenizer.unk_token)}"
)
print(
f"NFD content={len(nfd_ids)}, model-input={len(nfd_model_ids)}, "
f"unknown={nfd_tokens.count(tokenizer.unk_token)}"
)
print("same content IDs:", nfc_ids == nfd_ids)
print("same model-input IDs:", nfc_model_ids == nfd_model_ids)
print("NFC pieces:", trace(nfc_content, NFC_TEXT))
print("NFD pieces:", trace(nfd_content, NFD_TEXT))
Interpret the output by layer
A content token comes from add_special_tokens=False; it can be punctuation, a normal WordPiece, or [UNK]. A model-input token is returned with BERT post-processing enabled. For these short, unpadded, untruncated encodings, the assertions require the content sequence between [CLS] and [SEP]. The two lengths are therefore intentionally different, and neither is the vocabulary size. Unknowns are counted only in the content sequence so boundary tokens do not distort the comparison.
Use offsets, not token length alone, to connect pieces to the source. Python string slicing is by Unicode code point, so the combining acute in an NFD é occupies a separate index even though it renders with e. The trace slices the exact NFC or NFD input at each reported (start, end) pair and displays each source slice with Python’s repr. Treat these as the fast-tokenizer’s API-reported alignment and inspect them: normalization can make a token span a different number of source code points from the visually obvious grapheme. Do not mistake [CLS] or [SEP] positions for word offsets.
Normalization policy and vocabulary answer different questions. The English pin’s normalizer lowercases and strips accents, so NFC and NFD forms of Café can converge before WordPiece lookup. The multilingual-cased pin preserves case, but “cased” does not by itself settle diacritic handling: lowercase and strip_accents are separate controls. Inspect the exact strip_accents field in the pinned multilingual JSON. Compare the first Café with the later café to examine case handling, then compare NFC with NFD to examine canonical-form handling. Equal IDs show that this input reached the same terminal sequence; they do not alone prove why it happened.
An [UNK] is WordPiece’s unknown-vocabulary result. Fragmentation is two or more non-[UNK] content pieces covering one lexical unit. The sample makes those cases inspectable without assuming either result. A longer content sequence alone does not establish fragmentation, and [UNK] must not be described merely as fragmentation. The offset trace lets you assign each token to a source span and make the distinction. Punctuation and CJK handling can also add pieces, so compare like with like rather than attributing every extra token to Unicode form.
Read the two equality lines conditionally. If NFC and NFD content IDs match, the pinned pipeline mapped this sample to the same sequence; model-input IDs should then match as well under the fixed boundary wrapper. If they differ, the distinction survived normalization, pretokenization, or WordPiece lookup. Compare token traces and offsets before classifying the change as a regression. Do not generalize from one sample to every canonical-equivalence class, or compare a count made with special tokens to one made without them.
Limitations and re-check procedure
This is deliberately a narrow diagnostic. It uses one short string, no padding or truncation, Python’s unicodedata implementation, and fast-tokenizer offsets. It does not benchmark throughput or quality, cover every script, or prove behavior for all canonical-equivalence classes. Pinning transformers==4.56.2 also does not freeze every transitive dependency; the script prints the separately installed tokenizers backend version because that implementation participates in loading and alignment.
When a pinned tokenizer file or dependency changes, create a new test record rather than silently changing expected output. In a clean environment, install exactly transformers==4.56.2, run the block unchanged, and retain its complete stdout and stderr. Confirm the model ID, full commit, package versions, and fast-tokenizer status. Diff the new tokenizer.json, especially normalizer, pre_tokenizer, model.vocab, and post_processor, against the cited commit. Recheck canonical equivalence, the NFC/NFD ID comparison, unknown-token count, model-input length, and every offset trace. Publish the new revision, dependency versions, verification date, and observed diff together; never present old expected tokens as results from the new pin.
Sources
- google-bert/bert-base-uncased tokenizer.json at 86b5e093
Pinned WordPiece vocabulary and normalization settings for the English uncased model.
- google-bert/bert-base-multilingual-cased tokenizer.json at 3f076fdb
Pinned WordPiece vocabulary and normalization settings for the multilingual cased model.
- Hugging Face Transformers v4.56.2 tokenizer API
Versioned reference for encoding options, output fields, and fast-tokenizer alignment methods.
Token counts in this article were produced by the tokenizers pinned on the sources page and are checked by the site's test suite. If a count ever stops matching the pinned vocabulary, the build fails.