Designing a cache key for reproducible WordPiece results
Include raw input, pinned revision, library version, and encoding options; explain privacy and invalidation tradeoffs
To make WordPiece results reproducible in a cache, the key must bind raw input, tokenizer identity and pinned revision, Transformers version, and every encoding option that changes returned token IDs, masks, or offsets. Raw text alone is not a key. A model name alone is not a key. A key that omits add_special_tokens, padding, or truncation can hit an entry produced with different options and return a plausible but wrong model input.
WordPiece is deterministic only relative to a specific vocabulary, normalization, pre-tokenization, and library implementation. The pinned tokenizer.json for google-bert/bert-base-uncased@86b5e0934494bd15c9632b12f734a8a67f723594 is the version authority for that tokenizer. The pinned tokenizer.json for google-bert/bert-base-multilingual-cased@3f076fdb1ab68d5b2880cb87a0886f315b8146f8 is the corresponding authority for the multilingual cased model. If either file changes under a new revision, the cache key should change.
What belongs in the key
A practical key has five components. First, the exact model identifier and revision, such as google-bert/bert-base-uncased at 86b5e0934494bd15c9632b12f734a8a67f723594. Second, the tokenizer loading choice, including use_fast=True and the tokenizer behavior in transformers==4.56.2. Third, the exact raw text. Fourth, the encoding options that affect the cached value: add_special_tokens, padding, truncation, max_length, return_offsets_mapping, return_special_tokens_mask, and any other flag that changes output. Fifth, the returned fields you cache. If you cache only input_ids, say so; if you cache attention_mask and token_type_ids, include them. The key should describe the cached artifact, not just the text.
Content tokens and model-input tokens are different. With add_special_tokens=False, the tokenizer returns the content sequence for the raw text. With add_special_tokens=True, the model-input sequence includes [CLS] and [SEP] for BERT. Padding and truncation can add or remove positions. The same raw text therefore yields different model inputs under different options. The cache key must include those options if the cached value is a model input.
Unknown tokens and fragmentation are also different. An unknown token is the reserved token, [UNK] for these BERT tokenizers, with its own token ID. Fragmentation is one word being split into multiple WordPiece pieces. A word can fragment into several known pieces without producing [UNK]; conversely, a single [UNK] can replace a word that cannot be matched. Do not count pieces as unknown tokens, and do not treat an unknown token as fragmentation.
A diagnostic you can run
The following self-contained Python 3 script uses transformers==4.56.2 and AutoTokenizer.from_pretrained(..., revision=FULL_COMMIT, use_fast=True) for the pinned English uncased model. It builds a canonical key material object, hashes it, and prints content tokens, model-input tokens, special-token mask, offsets, and unknown-token information. Printed values depend on the local environment and on TEXT; the script does not hardcode expected outputs.
import hashlib
import json
import transformers
from transformers import AutoTokenizer
MODEL_ID = "google-bert/bert-base-uncased"
FULL_COMMIT = "86b5e0934494bd15c9632b12f734a8a67f723594"
TEXT = "The Quick Brown foxes can't be tokenized reproducibly."
ENCODING_OPTIONS = {
"add_special_tokens": True,
"padding": False,
"truncation": False,
}
assert transformers.__version__ == "4.56.2", transformers.__version__
tokenizer = AutoTokenizer.from_pretrained(
MODEL_ID,
revision=FULL_COMMIT,
use_fast=True,
)
key_material = {
"model_id": MODEL_ID,
"revision": FULL_COMMIT,
"transformers_version": transformers.__version__,
"raw_text": TEXT,
"encoding_options": ENCODING_OPTIONS,
}
canonical = json.dumps(key_material, sort_keys=True, ensure_ascii=False)
cache_key = hashlib.sha256(canonical.encode("utf-8")).hexdigest()
content = tokenizer(
TEXT,
add_special_tokens=False,
padding=False,
truncation=False,
return_offsets_mapping=True,
return_special_tokens_mask=True,
)
model_input = tokenizer(
TEXT,
**ENCODING_OPTIONS,
return_offsets_mapping=True,
return_special_tokens_mask=True,
)
content_tokens = tokenizer.convert_ids_to_tokens(content["input_ids"])
model_input_tokens = tokenizer.convert_ids_to_tokens(model_input["input_ids"])
print("cache_key", cache_key)
print("content_tokens", content_tokens)
print("content_ids", content["input_ids"])
print("model_input_tokens", model_input_tokens)
print("model_input_ids", model_input["input_ids"])
print("special_tokens_mask", model_input["special_tokens_mask"])
print("offset_mapping", model_input["offset_mapping"])
print("word_ids", model_input.word_ids())
print("unknown_token", tokenizer.unk_token, tokenizer.unk_token_id)
print("unknown_in_content", tokenizer.unk_token in content_tokens)
Run it locally. If transformers.__version__ is not exactly 4.56.2, the assertion stops. That is intentional: the diagnostic is scoped to that dependency version. The script loads the tokenizer from the full commit hash, not a moving branch. It prints content_tokens from an add_special_tokens=False call and model_input_tokens from an add_special_tokens=True call. It also prints word_ids, which helps distinguish special tokens, fragmentation, and unknown tokens by alignment.
Interpreting the output
When you compare runs, the hash should change if any key component changes. If TEXT changes by one byte, the raw-text component changes. If ENCODING_OPTIONS changes from add_special_tokens=True to False, the model-input token sequence changes. If the pinned revision changes, vocabulary or normalization may change. If the Transformers version changes, loading and encoding behavior may change. A cache hit under a complete key is evidence that the same configuration produced the cached value, within the limits of the local installation.
The token fields answer a different question. content_tokens tells you how raw text is fragmented before special tokens. model_input_tokens tells you what the model would see after special tokens and any padding or truncation. special_tokens_mask identifies positions that are special rather than content. offset_mapping maps fast-tokenizer tokens back to character spans in raw text, which is useful for checking fragmentation. word_ids groups pieces that came from the same word. If a word has multiple word IDs, it fragmented. If a token is [UNK], matching failed; that is not fragmentation.
For the English uncased model, the pinned tokenizer.json at 86b5e0934494bd15c9632b12f734a8a67f723594 defines a 30,522-entry WordPiece vocabulary. For the multilingual cased model, the pinned file at 3f076fdb1ab68d5b2880cb87a0886f315b8146f8 defines a 119,547-entry vocabulary. Vocabulary size alone does not determine a cache key, but the two pinned revisions are not interchangeable.
Privacy and invalidation tradeoffs
A cache key can leak information if it contains raw text and is stored or shared. Hashing raw text reduces accidental exposure, but short or predictable inputs can still be guessed by dictionary or brute-force attacks against an unsalted hash. If the cache is shared, use a secret-keyed HMAC or a salted hash, keep raw text in a protected store, and treat the key as an opaque identifier. The key material need not include raw text in plaintext; it must include enough to reproduce the encoding, which may mean storing raw text separately.
Invalidation follows the same components. When the pinned tokenizer.json changes under a new revision, invalidate entries that used the old revision. When transformers changes from 4.56.2, invalidate unless you have re-run the diagnostic and verified equivalence for your inputs. When encoding options change, invalidate the affected namespace. When the cached field set changes, for example from input_ids only to input_ids plus attention_mask, invalidate because the artifact is different.
Re-checking when pins move
The limitation is that this diagnostic verifies a local installation for the chosen TEXT and options. It does not prove that every Unicode input, every padding length, or every truncation boundary behaves identically after an update. It also does not cover an unrelated LLM, and it should not be used to assert token counts for one. The scope is the pinned BERT tokenizer revision and the pinned Transformers version under test.
To re-check, update FULL_COMMIT to the new revision and update transformers to the new version, then run the script with representative raw inputs: empty string, whitespace, punctuation, casing, accented characters, CJK text if relevant, long inputs that trigger truncation, and inputs that produce [UNK]. Compare content_ids, model_input_ids, special_tokens_mask, offset_mapping, and word_ids. If any differ, treat the cache namespace as stale and rebuild. Verified: 2026-10-05 against google-bert/bert-base-uncased@86b5e0934494bd15c9632b12f734a8a67f723594 and transformers==4.56.2. If the pinned file or dependency changes, repeat this procedure before trusting old cache entries.
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.