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.Used by 2
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
2Algorithm identifier (always "bpe").
Returns
strConstant string "bpe" — used by the unified
tokenizer.json writer to label the algorithm.
Number of tokens currently registered in the vocab.
Returns
intlen(self._vocab) — includes special tokens that were
registered into the main vocab.
Class methods
1from_file(directory: str, normalizer: Normalizer | None = None, pre_tokenizer: PreTokenizer | None = None, special_tokens: SpecialTokens | None = None)Load from a directory containing either
tokenizer.json or the legacy
vocab.json + merges.txt pair.
Unified format wins when present.
Parameters
directorystrtokenizer.json or the legacy pair
(vocab.json + merges.txt). Optionally also
special_tokens_map.json for the special-token registry.normalizer(Normalizer, optional, keyword - only)= Nonelucid.utils.tokenizer._normalizers.NFC when
not supplied.pre_tokenizer(PreTokenizer, optional, keyword - only)= Nonelucid.utils.tokenizer._pre_tokenizers.WhitespaceSplit.None (the
default), parsed from special_tokens_map.json if
present.Returns
BPETokenizerFreshly-constructed instance ready for encode / decode.
Instance methods
4Return a shallow copy of the token → id map.
Returns
dict[str, int]Copy of the internal vocab; mutating it does not affect the tokenizer.
Look up the surface string for a token id.
Parameters
token_idintReturns
str or NoneThe token string for token_id, or None if the id
is not in the reverse table.
Persist as both legacy (vocab.json + merges.txt)
AND unified (tokenizer.json) formats.
Writing both means the resulting directory loads back via
from_pretrained regardless of which format the caller
looks for first — and matches how HF distributes BPE
checkpoints on the Hub.
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