BPETokenizerFast
_BPECommonMixinTokenizerBPETokenizerFast(vocab: dict[str, int], merges: list[tuple[str, str]], normalizer: Normalizer | None = None, pre_tokenizer: PreTokenizer | None = None, special_tokens: SpecialTokens | None = None)C++-backed BPE tokenizer.
Identical algorithm + vocab format to BPETokenizer; the
hot loop (per-chunk merge application) runs in C++ via the engine
BPE binding. Encode outputs are bit-identical for the same
vocab + same merges + same normalizer + same pre-tokenizer.
Use this in production training / inference. Use
BPETokenizer for debugging / extending the algorithm
with custom Python-only normalizers without touching C++.
It takes the parameters of BPETokenizer. The C++ backend is constructed
transparently in __init__ and held as _cpp.
Examples
Given the same vocabulary and merges, this and
BPETokenizer encode identically — so one can be trained and
the other used to run it:
>>> from lucid.utils.tokenizer import BPETokenizer, BPETokenizerFast
>>> slow = BPETokenizer(vocab={}, merges=[])
>>> slow.train(["the quick brown fox", "the lazy dog sleeps"],
... vocab_size=80)
>>> fast = BPETokenizerFast(vocab=dict(slow._vocab),
... merges=list(slow._merges))
>>> fast.encode("the quick dog") == slow.encode("the quick dog")
True
Trained separately they will not match, and that is the training
rather than the encoder: two runs settle on different merge orders,
both correct, and the ids follow the merges.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 C++-backed BPE tokenizer.
Parameters
vocabdict[str, int]mergeslist of (str, str)normalizer(Normalizer or None, optional, keyword - only)= NoneNFC.pre_ tokenizer(PreTokenizer or None, optional, keyword - only)= NoneWhitespaceSplit._sync_special_tokens_to_cpp.Notes
Builds the C++ BPE backend once and caches it on _cpp;
the Python-side _id_to_token is kept in sync so decode
avoids a binding round-trip.
Properties
2Algorithm identifier ("bpe").
Number of tokens in the C++ side vocab.
Returns
intResult of self._cpp.vocab_size() — authoritative
count for the live tokenizer (kept in sync with the
Python cache after every train).
Class methods
1from_file(directory: str, normalizer: Normalizer | None = None, pre_tokenizer: PreTokenizer | None = None, special_tokens: SpecialTokens | None = None)Load a C++-backed BPE tokenizer from directory.
Parameters
directorystrnormalizerNormalizer | 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 in C++.
Materialises corpus into a list before handing off (the
C++ binding takes std::vector<std::string>); for very
large corpora the user is responsible for chunking. After
training, the Python-side vocab / merges caches are refreshed
from the C++ side so subsequent encodes see the new state.
Parameters
corpusiterable of strvocab_ size(int, optional, keyword - only)= 30000