Skip to content
abstract

Tokenizer

extendsABC
Tokenizer(special_tokens: SpecialTokens | None = None)

Abstract base for every tokenizer in lucid.utils.tokenizer.

Subclasses implement four hooks:

  • _encode_one(text) -> list[int] — algorithm-specific encode for one pre-normalized + pre-tokenized chunk. The base class handles normalization, pre-tokenization, and special-token insertion around this hook.
  • _decode_one(ids) -> str — algorithm-specific decode of one id sequence (special tokens NOT yet stripped — the base class handles skip_special_tokens).
  • vocab_size — total number of distinct ids.
  • algo — short algorithm name for serialisation ("bpe", "wordpiece", "unigram", "byte_bpe").

The base class layers encode / decode / encode_batch / decode_batch / __call__ on top — those are fully generic and don't need to be overridden.

Examples

Every tokenizer here is one of these, so the three calls below are
the whole surface a caller needs — build or train, encode, decode:
>>> from lucid.utils.tokenizer import WordTokenizer
>>> tok = WordTokenizer()
>>> tok.train(["the quick brown fox", "the lazy dog"], vocab_size=32)
>>> tok.decode(tok.encode("the quick dog"))
'the quick dog'
Subclassing one means supplying the algorithm and inheriting the
rest — the special-token handling, save / from_pretrained,
and the batch helpers.

See Also

  • algorithm with identical encode outputs.

Used by 7

Constructors

2
dunder

__init__

→None
__init__(special_tokens: SpecialTokens | None = None)

Initialise the special-token registry; subclasses must populate the vocab + algorithm state before calling this.

dunder

__call__

→dict
__call__(text: str | list[str], add_special_tokens: bool = True, padding: bool | str = False, truncation: bool = False, max_length: int | None = None, return_tensors: str | None = None, return_attention_mask: bool = True)

Hugging-Face-style entry point.

Parameters

textstr or list of str
Single string → returns dict whose values are 1-D sequences. List → returns dict whose values are 2-D batched sequences (rectangular when padded).
add_special_tokensbool= True
Whether to wrap each sequence with BOS/EOS / CLS/SEP.
paddingbool or str= False
False — no padding. True or "longest" — pad every sequence to the longest in the batch. "max_length" — pad every sequence to max_length.
truncationbool= False
When True, truncate each sequence to max_length (no-op if max_length is None). The content is cut, never the framing: max_length counts the special tokens and they stay, so an EOS survives truncation.
max_lengthint= None
Bound for both truncation + padding="max_length".
return_tensorslucid= "lucid"
When "lucid", wrap each output value as a lucid.Tensor (dtype int32) and stack the batch into a 2-D tensor. When None, return plain Python lists.
return_attention_maskbool= True
When True (and padding is set), include an "attention_mask" entry: 1 for real tokens, 0 for padding.

Returns

dict

Always contains "input_ids". Optionally "attention_mask" (when padding + return_attention_mask) and "special_tokens_mask" (always, for downstream BERT-style MLM consumers).

Properties

12
prop

algo

→str
algo: str

Short algorithm name ("bpe" / "wordpiece" / ...).

Used by save to route serialisation to the right format and by from_pretrained to pick the right subclass when loading from a generic path.

prop

all_special_ids

→list[int]
all_special_ids: list[int]

Every special-token id (canonical + extras), deduped.

prop

bos_token_id

→int | None
bos_token_id: int | None

Id of the bos special token (None if undefined).

prop

cls_token_id

→int | None
cls_token_id: int | None

Id of the cls special token (None if undefined).

prop

eos_token_id

→int | None
eos_token_id: int | None

Id of the eos special token (None if undefined).

prop

mask_token_id

→int | None
mask_token_id: int | None

Id of the mask special token (None if undefined).

prop

pad_token_id

→int | None
pad_token_id: int | None

Id of the pad special token (None if undefined).

prop rw

post_processor

→PostProcessor or None
post_processor: PostProcessor | None

The configured post-processor, or None for the class default.

Assign a lucid.utils.tokenizer._post_processors.PostProcessor to change how encode frames sequences — for example a lucid.utils.tokenizer._post_processors.TemplateProcessing reproducing a checkpoint's template — or None to go back to the class's default layout.

Returns

PostProcessor or None

The processor loaded from tokenizer.json or assigned here.

Examples

>>> from lucid.utils.tokenizer import SpecialTokens, WordTokenizer
>>> from lucid.utils.tokenizer._post_processors import TemplateProcessing
>>> tok = WordTokenizer(
...     {"<unk>": 0, "</s>": 1, "hi": 2},
...     special_tokens=SpecialTokens(unk="<unk>", eos="</s>"),
... )
>>> tok.encode("hi")
[2, 1]
>>> tok.post_processor = TemplateProcessing("$A", special_tokens={})
>>> tok.encode("hi")
[2]
prop

sep_token_id

→int | None
sep_token_id: int | None

Id of the sep special token (None if undefined).

prop

special_tokens

→SpecialTokens
special_tokens: SpecialTokens

The registry of canonical + extra special tokens.

prop

unk_token_id

→int | None
unk_token_id: int | None

Id of the unk special token (None if undefined).

prop

vocab_size

→int
vocab_size: int

Total number of distinct token ids (including specials).

Instance methods

10
fn

convert_ids_to_tokens

→str | list[str]
convert_ids_to_tokens(ids: int | Iterable[int])

Inverse of convert_tokens_to_ids.

fn

convert_tokens_to_ids

→int | list[int]
convert_tokens_to_ids(tokens: str | Iterable[str])

Map one or many token strings to their ids.

Unknown tokens raise KeyError (callers wanting an UNK fallback should do their own .get(token, unk_id) lookup).

fn

decode

→str
decode(ids: Iterable[int] | list[int], skip_special_tokens: bool = True)

Decode a sequence of ids back to text.

When skip_special_tokens=True every id in all_special_ids is dropped before the algorithm-specific _decode_one runs.

Parameters

idsiterable of int
Token ids to decode (typically a model's argmax / sample output). Consumed exactly once when an iterator is passed.
skip_special_tokens(bool, optional, keyword - only)= True
When True, every id in all_special_ids (BOS/EOS/PAD/CLS/SEP/MASK + any extras) is filtered out before _decode_one reassembles the surface string.

Returns

str

Reconstructed text — exact reverse of encode for lossless algorithms (Unigram / byte-BPE); lossy for normalising flavours (NFC + Lowercase).

fn

decode_batch

→list[str]
decode_batch(batch_ids: Iterable[Iterable[int]] | list[list[int]], skip_special_tokens: bool = True)

Vectorised decode over a list of id sequences.

fn

encode

→list of int
encode(text: str, add_special_tokens: bool = True)

Encode a single string to a list of token ids.

When add_special_tokens=True the sequence is framed by the tokenizer's post_processor if one is configured (a tokenizer.json carries it — T5's appends only </s>), and otherwise by the class's default layout: BOS/EOS, BERT's [CLS] … [SEP], or nothing for GPT-2.

Parameters

textstr
Input text to tokenise. Normalisation + pre-tokenisation are applied internally by the algorithm-specific _encode_one hook before the BPE / WordPiece / Unigram merge loop runs.
add_special_tokens(bool, optional, keyword - only)= True
When True, frame the ids with special tokens as described above, via _build_inputs_with_special_tokens. Pass False to return the raw merge-loop output untouched (useful for offline analysis, alignment, or batching pipelines that inject specials elsewhere).

Returns

list of int

Token ids for text in left-to-right order.

fn

encode_batch

→list of list of int
encode_batch(texts: Iterable[str] | list[str], add_special_tokens: bool = True)

Vectorised encode over a list of strings.

Default loops through encode; subclasses with a true batched fast path (the C++ Fast tokenizers) override.

Parameters

textsiterable of str
Input strings — one entry per sequence to encode. An iterable is consumed exactly once.
add_special_tokens(bool, optional, keyword - only)= True
Forwarded to encode for each item — when True, every sequence is wrapped with its canonical BOS/EOS or CLS/SEP markers.

Returns

list of list of int

One id sequence per input string, same order as texts.

fn

get_vocab

→dict[str, int]
get_vocab()

Return the token-string → id map.

Default returns an empty dict; subclasses with a real vocab override this. Used by save and by special-token-id resolution in _refresh_special_ids.

fn

id_to_token

→str | None
id_to_token(token_id: int)

Return the surface form for token_id or None if out of range.

Default does an O(N) reverse scan of get_vocab; subclasses with an O(1) reverse table should override.

fn

num_special_tokens_to_add

→int
num_special_tokens_to_add(pair: bool = False)

Count the special tokens encode(add_special_tokens=True) adds.

Parameters

pairbool= False
Count for a pair of sequences instead of one.

Returns

int

How many ids framing adds around the content — the amount __call__ reserves out of max_length when it truncates.

Examples

>>> from lucid.utils.tokenizer import SpecialTokens, WordTokenizer
>>> tok = WordTokenizer(
...     {"<unk>": 0, "<s>": 1, "</s>": 2},
...     special_tokens=SpecialTokens(unk="<unk>", bos="<s>", eos="</s>"),
... )
>>> tok.num_special_tokens_to_add()
2
fn

save

→None
save(directory: str)

Persist the tokenizer to directory.

Default writes tokenizer.json (unified format) + special_tokens_map.json. Subclasses with algorithm- specific legacy files (vocab.json + merges.txt for BPE, vocab.txt for WordPiece) override this hook to emit those alongside.