UnigramTokenizer
_UnigramCommonMixinTokenizerUnigramTokenizer(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)Reference (pure-Python) Unigram tokenizer.
Encodes via Viterbi over the lattice of all candidate
sub-piece segmentations: for each chunk we build a DP table
dp[i] = best (highest log-prob) path ending at byte offset
i, with single-codepoint UNK fallback when no piece spans
a region. Decode is a trivial "".join(pieces).replace("▁", " ").
Training is delegated to C++ (see train for the
rationale) — even this "pure-Python" flavour does not implement
EM in Python because the runtime would be unusable on any
real corpus.
For production / latency-sensitive use, prefer
UnigramTokenizerFast — same vocab format, bit-identical
encode output, but the Viterbi hot loop runs in C++.
Parameters
pieceslist of (str, float)(piece_str, log_prob) list; index = token id.
Larger (less-negative) log-probs are preferred during
Viterbi decode.unk_ tokenstr= "<unk>"pieces
spans a region of the input. Must appear in pieces for
the UNK id to be defined; otherwise encode silently drops
unmatchable codepoints.unk_ log_ probfloat= -100.0normalizerNormalizer= Nonelucid.utils.tokenizer._normalizers.NFKC (matches
LLaMA / Mistral / T5).pre_ tokenizerPreTokenizer= NoneSentencePiecePreTokenizer for canonical
SentencePiece behaviour with ▁ word-start markers.lucid.utils.tokenizer.SpecialTokens. Defaults to
SpecialTokens(unk=unk_token).Notes
The Viterbi DP runs in per chunk, where
N is the chunk byte length and M is the max piece byte
length. UTF-8 boundary masking ensures sub-piece offsets only
land on codepoint boundaries (no mid-codepoint cuts), which
matches the C++ flavour bit-for-bit.
Examples
>>> from lucid.utils.tokenizer import UnigramTokenizer
>>> tok = UnigramTokenizer(pieces=[])
>>> tok.train(["the quick brown fox", "the lazy dog sleeps"],
... vocab_size=48)
>>> tok.decode(tok.encode("the quick dog"))
' the quick dog'
The leading space is the SentencePiece convention this follows — a
piece carries the boundary before it — and it survives the round
trip rather than being trimmed away.See Also
UnigramTokenizerFast—C++-backed flavour with identicalencode output and a much faster—meth:~UnigramTokenizerFast.encode.
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 pure-Python Unigram tokenizer.
Parameters
pieceslist of (str, float)(piece, log_prob) list; 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
Builds piece-id maps and caches the longest piece in bytes
for the Viterbi DP via _rebuild_tables.
Properties
3Algorithm identifier (always "unigram").
Raw (piece, log_prob) list.
Number of pieces in the Unigram vocabulary.
Returns
intlen(self._pieces).
Class methods
1Instance methods
4Return a copy of the piece → id map.
Returns
dict[str, int]Shallow copy; mutating it does not affect the tokenizer.
Added tokens a tokenizer.json declares outside the piece
list are included.
Look up the piece string for a token id.
Persist as unified tokenizer.json + special_tokens_map.json.
Re-train this tokenizer from scratch on corpus.