UnigramTokenizerFast
_UnigramCommonMixinTokenizerUnigramTokenizerFast(pieces: list[tuple[str, float]], unk_token: str = '<unk>', unk_log_prob: float = -100.0, normalizer: Normalizer | None = None, pre_tokenizer: PreTokenizer | None = None, special_tokens: SpecialTokens | None = None)C++-backed Unigram tokenizer.
Identical algorithm + on-disk format to
UnigramTokenizer; the per-chunk Viterbi loop runs in
C++ via the engine Unigram binding. Encode outputs are
bit-identical for the same pieces + same normalizer + same
pre-tokenizer.
Use this in production training / inference. Use
UnigramTokenizer for debugging / extending the
algorithm with custom Python-only normalizers without touching
C++.
It takes the parameters of UnigramTokenizer. The C++ backend is constructed
transparently in __init__ and held as _cpp.
Examples
>>> from lucid.utils.tokenizer import UnigramTokenizerFast
>>> tok = UnigramTokenizerFast(pieces=[])
>>> tok.train(["the quick brown fox", "the lazy dog sleeps"],
... vocab_size=48)
>>> tok.decode(tok.encode("the quick dog"))
' the quick dog'See Also
UnigramTokenizer—Pure-Python reference flavour.
Used by 1
Constructors
1__init__
→None__init__(pieces: list[tuple[str, float]], unk_token: str = '<unk>', unk_log_prob: float = -100.0, normalizer: Normalizer | None = None, pre_tokenizer: PreTokenizer | None = None, special_tokens: SpecialTokens | None = None)Construct a C++-backed Unigram tokenizer.
Parameters
pieceslist of (str, float)(piece, log_prob) list passed to the C++
backend; index = token id.unk_ tokenstr= "<unk>"unk_ log_ probfloat= -100.0normalizer(Normalizer or None, optional, keyword - only)= NoneNFKC.pre_ tokenizer(PreTokenizer or None, optional, keyword - only)= NoneSentencePiecePreTokenizer.SpecialTokens(unk=unk_token).Notes
Constructs the C++ Unigram backend once and caches it
on _cpp; the Python-side _id_to_piece reverse map mirrors
the pieces list for fast decode.
Properties
3Algorithm identifier (always "unigram").
Raw (piece, log_prob) list.
Number of pieces in the live C++ vocabulary.
Returns
intself._cpp.vocab_size().
Class methods
1Instance methods
4Return a piece → id map from the C++ backend.
Returns
dict[str, int]Fresh dict built from self._cpp.get_vocab(), plus any
added tokens declared outside the piece list.
Look up the piece string for a token id.
Persist as unified tokenizer.json + special_tokens_map.json.
Re-train in C++ (EM with vocab pruning, Kudo 2018).