Skip to content
class

UnigramTokenizerFast

extends_UnigramCommonMixinTokenizer
UnigramTokenizerFast(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
dunder

__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)
Ordered (piece, log_prob) list passed to the C++ backend; index = token id.
unk_tokenstr= "<unk>"
Fallback piece string for unmatchable regions.
unk_log_probfloat= -100.0
Log probability assigned to UNK substitutions in C++.
normalizer(Normalizer or None, optional, keyword - only)= None
Pre-encode normalisation. Defaults to NFKC.
pre_tokenizer(PreTokenizer or None, optional, keyword - only)= None
Chunk splitter. Defaults to SentencePiecePreTokenizer.
special_tokens(SpecialTokens or None, optional, keyword - only)= None
Special-token registry. Defaults to 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

3
prop

algo

→strfrom _UnigramCommonMixin
algo: str

Algorithm identifier (always "unigram").

prop

pieces

→list[tuple[str, float]]from _UnigramCommonMixin
pieces: list[tuple[str, float]]

Raw (piece, log_prob) list.

prop

vocab_size

→int
vocab_size: int

Number of pieces in the live C++ vocabulary.

Returns

int

self._cpp.vocab_size().

Class methods

1
cls

from_file

→Selffrom _UnigramCommonMixin
from_file(directory: str, normalizer: Normalizer | None = None, pre_tokenizer: PreTokenizer | None = None, special_tokens: SpecialTokens | None = None)

Load from a directory containing tokenizer.json.

Instance methods

4
fn

get_vocab

→dict[str, int]
get_vocab()

Return 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.

fn

id_to_token

→str | Nonefrom _UnigramCommonMixin
id_to_token(token_id: int)

Look up the piece string for a token id.

fn

save

→Nonefrom _UnigramCommonMixin
save(directory: str)

Persist as unified tokenizer.json + special_tokens_map.json.

fn

train

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

Re-train in C++ (EM with vocab pruning, Kudo 2018).