BPETokenizer
_BPECommonMixinTokenizerBPETokenizer(vocab: dict[str, int], merges: list[tuple[str, str]], normalizer: Normalizer | None = None, pre_tokenizer: PreTokenizer | None = None, special_tokens: SpecialTokens | None = None)Reference (pure-Python) BPE tokenizer.
Implements the classical Sennrich-2016 BPE encode loop directly in Python. Easy to read, easy to step through with a debugger, works on every platform (no C++ build needed).
For production / latency-sensitive use, prefer the matching
BPETokenizerFast — same algorithm, same vocab format,
bit-identical encode outputs, but the hot loop is in C++.
Parameters
vocabdict[str, int]mergeslist[tuple[str, str]]normalizerNormalizer= Nonelucid.utils.tokenizer._normalizers.NFC.pre_ tokenizerPreTokenizer= Nonelucid.utils.tokenizer._pre_tokenizers.WhitespaceSplit.lucid.utils.tokenizer.SpecialTokens.Examples
Trained in place from an empty vocabulary and no merges:
>>> from lucid.utils.tokenizer import BPETokenizer
>>> tok = BPETokenizer(vocab={}, merges=[])
>>> tok.train(["the quick brown fox", "the lazy dog sleeps"],
... vocab_size=80)
>>> tok.decode(tok.encode("the quick dog"))
'thequickdog'
The whitespace is gone, and that is the algorithm rather than a
defect: raw BPE has no notion of a word boundary. Use
ByteLevelBPETokenizer where the round trip has to be exact.Used by 4
Constructors
1__init__
→None__init__(vocab: dict[str, int], merges: list[tuple[str, str]], normalizer: Normalizer | None = None, pre_tokenizer: PreTokenizer | None = None, special_tokens: SpecialTokens | None = None)Construct a pure-Python BPE tokenizer.
Parameters
vocabdict[str, int]merges.mergeslist of (str, str)normalizer(Normalizer or None, optional, keyword - only)= NoneNFC.pre_ tokenizer(PreTokenizer or None, optional, keyword - only)= NoneWhitespaceSplit.Notes
Mutates internal state: copies vocab / merges, builds
the _pair_to_merge compiled lookup, and registers special
tokens via the base Tokenizer constructor.
Properties
2Class methods
1from_file(directory: str, normalizer: Normalizer | None = None, pre_tokenizer: PreTokenizer | None = None, special_tokens: SpecialTokens | None = None)Load a pure-Python BPE tokenizer from directory.
Parameters
directorystrtokenizer.json or vocab.json + merges.txt.normalizerNormalizer | None= Nonepre_ tokenizerPreTokenizer | None= NoneInstance methods
4Return a shallow copy of the token → id map.
Look up the surface string for a token id.
Persist as both legacy (vocab.json + merges.txt)
AND unified (tokenizer.json) formats.
Re-train this tokenizer from scratch on corpus.
Implements the classical Sennrich-2016 training loop:
- Pre-tokenize each document (using the configured pre-tokenizer chain) into a frequency-counted set of words.
- Seed the vocab with every distinct codepoint appearing in any word.
- Iteratively count adjacent symbol-pair frequencies + merge
the most frequent pair, growing the vocab by 1 each step
until
vocab_sizeis reached or no pair appears more than once.
Replaces _vocab + _merges in place; previous contents
are discarded.
Parameters
corpusiterable of strvocab_ sizeint= 30 000