Skip to content
class

BPETokenizerFast

extends_BPECommonMixinTokenizer
BPETokenizerFast(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
dunder

__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]
Token-string → id map; copied into both Python and C++ state.
mergeslist of (str, str)
Ordered merge pairs; rank = list index.
normalizer(Normalizer or None, optional, keyword - only)= None
Pre-encode normalisation chain. Defaults to NFC.
pre_tokenizer(PreTokenizer or None, optional, keyword - only)= None
Chunk splitter. Defaults to WhitespaceSplit.
special_tokens(SpecialTokens or None, optional, keyword - only)= None
Special-token registry; mirrored into the C++ side via _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

2
prop

algo

→strfrom _BPECommonMixin
algo: str

Algorithm identifier ("bpe").

prop

vocab_size

→int
vocab_size: int

Number of tokens in the C++ side vocab.

Returns

int

Result of self._cpp.vocab_size() — authoritative count for the live tokenizer (kept in sync with the Python cache after every train).

Class methods

1
cls

from_file

→BPETokenizerFast
from_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

directorystr
Checkpoint directory path on disk.
normalizerNormalizer | None= None
Custom text normaliser to apply before splitting.
pre_tokenizerPreTokenizer | None= None
Custom chunk splitter for the C++ merge loop.
special_tokensSpecialTokens | None= None
Custom special-token configuration.

Instance methods

4
fn

get_vocab

→dict[str, int]from _BPECommonMixin
get_vocab()

Return a shallow copy of the token → id map.

fn

id_to_token

→str | Nonefrom _BPECommonMixin
id_to_token(token_id: int)

Look up the surface string for a token id.

fn

save

→Nonefrom _BPECommonMixin
save(directory: str)

Persist as both legacy (vocab.json + merges.txt) AND unified (tokenizer.json) formats.

fn

train

→None
train(corpus: Iterable[str], vocab_size: int = 30000)

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 str
Documents to train on — each item is one document (or chunk thereof). Generators are consumed exactly once and materialised into a list before crossing into C++.
vocab_size(int, optional, keyword - only)= 30000
Target total vocab size (characters + merges). The C++ trainer stops early if no symbol pair appears more than once.