Skip to content
class

WordPieceTokenizerFast

extends_WordPieceCommonMixinTokenizer
WordPieceTokenizerFast(vocab: dict[str, int], unk_token: str = '[UNK]', continuing_prefix: str = '##', max_chars_per_word: int = 100, normalizer: Normalizer | None = None, pre_tokenizer: PreTokenizer | None = None, special_tokens: SpecialTokens | None = None)

C++-backed WordPiece tokenizer.

Identical algorithm + vocab format to WordPieceTokenizer; the hot loops (greedy longest-match encode + training) run in C++ via the engine WordPiece binding. Encode outputs are bit-identical for the same vocab + same normalizer + same pre-tokenizer.

The Python side still handles normalisation + pre-tokenization (so user-defined Python normalizers compose with the C++ backend) and the BERT-style decode join.

It takes the parameters of WordPieceTokenizer. The C++ backend is constructed transparently in __init__ and held as _cpp.

Examples

>>> from lucid.utils.tokenizer import WordPieceTokenizerFast
>>> tok = WordPieceTokenizerFast(vocab={})
>>> tok.train(["the quick brown fox", "the lazy dog sleeps"],
...           vocab_size=64)
>>> tok.decode(tok.encode("the quick dog"))
'the quick dog'

See Also

  • WordPieceTokenizer—Pure-Python reference; same vocab format.

Used by 3

Constructors

1
dunder

__init__

→None
__init__(vocab: dict[str, int], unk_token: str = '[UNK]', continuing_prefix: str = '##', max_chars_per_word: int = 100, normalizer: Normalizer | None = None, pre_tokenizer: PreTokenizer | None = None, special_tokens: SpecialTokens | None = None)

Construct a C++-backed WordPiece tokenizer.

Parameters

vocabdict[str, int]
BERT-style token → id map; passed to the C++ backend.
unk_tokenstr= "[UNK]"
See WordPieceTokenizer.
continuing_prefixstr= "##"
See WordPieceTokenizer.
max_chars_per_wordint= 100
See WordPieceTokenizer.
normalizer(Normalizer or None, optional, keyword - only)= None
Pre-encode normaliser. Defaults to BERTNormalizer.
pre_tokenizer(PreTokenizer or None, optional, keyword - only)= None
Defaults to WhitespacePunctuationSplit.
special_tokens(SpecialTokens or None, optional, keyword - only)= None
Defaults to SpecialTokens(unk=unk_token).

Notes

Constructs the C++ WordPiece backend once and caches it on _cpp; the Python-side _id_to_token mirror enables fast decode without round-trips.

Properties

2
prop

algo

→strfrom _WordPieceCommonMixin
algo: str

Algorithm identifier (always "wordpiece").

prop

vocab_size

→int
vocab_size: int

Number of WordPiece tokens in the live C++ vocab.

Returns

int

self._cpp.vocab_size().

Class methods

1
cls

from_file

→Selffrom _WordPieceCommonMixin
from_file(directory: str, unk_token: str = '[UNK]', continuing_prefix: str = '##', max_chars_per_word: int = 100, normalizer: Normalizer | None = None, pre_tokenizer: PreTokenizer | None = None, special_tokens: SpecialTokens | None = None)

Load from a BERT-style directory containing vocab.txt.

Instance methods

4
fn

get_vocab

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

Return a shallow copy of the token → id map.

fn

id_to_token

→str | Nonefrom _WordPieceCommonMixin
id_to_token(token_id: int)

Look up the surface string for a token id.

fn

save

→Nonefrom _WordPieceCommonMixin
save(directory: str)

Persist as a BERT-style directory (vocab.txt + unified tokenizer.json + special_tokens_map.json).

fn

train

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

Re-train in C++ via WordPiece.train.

Pre-encodes the corpus through the configured normalizer + pre-tokenizer in Python so the C++ training loop sees already-normalized, whitespace-separated words (its internal splitter is also whitespace, so this composes correctly). After training, the Python-side vocab cache is refreshed from the C++ side so subsequent encodes see the new state.

Parameters

corpusiterable of str
Each item is one document. Materialised to a list before the C++ binding call.
vocab_sizeint= 30 000
Target total vocab size.