WordPieceTokenizerFast
_WordPieceCommonMixinTokenizerWordPieceTokenizerFast(vocab: dict[str, int], unk_token: str = '[UNK]', continuing_prefix: str = '##', max_chars_per_word: int = 100, normalizer: Normalizer | None = None, pre_tokenizer: PreTokenizer | None = None, special_tokens: SpecialTokens | None = None)C++-backed WordPiece tokenizer.
Identical algorithm + vocab format to WordPieceTokenizer;
the hot loops (greedy longest-match encode + training) run in C++
via the engine WordPiece binding. Encode outputs are
bit-identical for the same vocab + same normalizer + same
pre-tokenizer.
The Python side still handles normalisation + pre-tokenization (so user-defined Python normalizers compose with the C++ backend) and the BERT-style decode join.
It takes the parameters of WordPieceTokenizer. The C++ backend is constructed
transparently in __init__ and held as _cpp.
Examples
>>> from lucid.utils.tokenizer import WordPieceTokenizerFast
>>> tok = WordPieceTokenizerFast(vocab={})
>>> tok.train(["the quick brown fox", "the lazy dog sleeps"],
... vocab_size=64)
>>> tok.decode(tok.encode("the quick dog"))
'the quick dog'See Also
WordPieceTokenizer—Pure-Python reference; same vocab format.
Used by 3
Constructors
1__init__
→None__init__(vocab: dict[str, int], unk_token: str = '[UNK]', continuing_prefix: str = '##', max_chars_per_word: int = 100, normalizer: Normalizer | None = None, pre_tokenizer: PreTokenizer | None = None, special_tokens: SpecialTokens | None = None)Construct a C++-backed WordPiece tokenizer.
Parameters
vocabdict[str, int]unk_ tokenstr= "[UNK]"WordPieceTokenizer.continuing_ prefixstr= "##"WordPieceTokenizer.max_ chars_ per_ wordint= 100WordPieceTokenizer.normalizer(Normalizer or None, optional, keyword - only)= NoneBERTNormalizer.pre_ tokenizer(PreTokenizer or None, optional, keyword - only)= NoneWhitespacePunctuationSplit.SpecialTokens(unk=unk_token).Notes
Constructs the C++ WordPiece backend once and caches it
on _cpp; the Python-side _id_to_token mirror enables fast
decode without round-trips.
Properties
2Class methods
1from_file
→Selffrom _WordPieceCommonMixinfrom_file(directory: str, unk_token: str = '[UNK]', continuing_prefix: str = '##', max_chars_per_word: int = 100, normalizer: Normalizer | None = None, pre_tokenizer: PreTokenizer | None = None, special_tokens: SpecialTokens | None = None)Load from a BERT-style directory containing vocab.txt.
Instance methods
4Return a shallow copy of the token → id map.
Look up the surface string for a token id.
Persist as a BERT-style directory (vocab.txt +
unified tokenizer.json + special_tokens_map.json).
Re-train in C++ via WordPiece.train.
Pre-encodes the corpus through the configured normalizer + pre-tokenizer in Python so the C++ training loop sees already-normalized, whitespace-separated words (its internal splitter is also whitespace, so this composes correctly). After training, the Python-side vocab cache is refreshed from the C++ side so subsequent encodes see the new state.
Parameters
corpusiterable of strvocab_ sizeint= 30 000