Skip to content
class

ByteTokenizer

extendsTokenizer
ByteTokenizer(special_tokens: SpecialTokens | None = None)

Reference (pure-Python) byte-level tokenizer.

Vocab is fixed at 256 (byte value → id == byte value). Equivalent to ByteTokenizerFast but loops in Python — useful when debugging downstream consumers without a C++ dependency.

Parameters

special_tokensSpecialTokens= None
Special-token registry; the canonical 7 slots can hold printable byte values (chr(b) for some b). IDs beyond 255 are NOT supported on the default vocab — extend the vocab manually before adding such specials.

Examples

Nothing to train — every byte is already a token, so the round trip
is exact for any input at all:
>>> from lucid.utils.tokenizer import ByteTokenizer
>>> tok = ByteTokenizer()
>>> tok.encode("the quick dog")[:4]
[116, 104, 101, 32]
>>> tok.decode(tok.encode("héllo — 안녕"))
'héllo — 안녕'

Used by 1

Constructors

1
dunder

__init__

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

Construct a pure-Python byte tokenizer.

Parameters

special_tokensSpecialTokens or None= None
Optional special-token registry. The byte vocab itself is universal (256 entries) and is built unconditionally.

Properties

2
prop

algo

→str
algo: str

Algorithm identifier (always "byte").

prop

vocab_size

→int
vocab_size: int

Fixed vocab size = 256 (one id per possible byte value).

Class methods

1
cls

from_file

→ByteTokenizer
from_file(directory: str, special_tokens: SpecialTokens | None = None)

Load from a directory containing special_tokens_map.json.

The vocab itself is not read from disk (always the canonical 256-entry table); we only consult special_tokens_map.json so user-defined specials survive a round-trip.

Parameters

directorystr
Directory previously written by save.
special_tokensSpecialTokens= None
Override for the on-disk special tokens.

Returns

ByteTokenizer

New instance with the loaded special-token registry.

Instance methods

4
fn

get_vocab

→dict[str, int]
get_vocab()

Return the canonical 256-entry byte → id map.

fn

id_to_token

→str | None
id_to_token(token_id: int)

Inverse of the byte vocab; returns the Latin-1 char for valid byte ids, None otherwise.

fn

save

→None
save(directory: str)

Persist as tokenizer.json only (no per-algorithm legacy file — there's no vocab.txt convention for byte tokenizers).

Parameters

directorystr
Output directory (created if missing). Produces tokenizer.json + special_tokens_map.json.
fn

train

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

No-op — the byte vocab is fixed at 256.

Parameters

corpusiterable of str
Ignored; accepted only for API uniformity with the rest of the tokenizer family.
vocab_sizeint= 256
Ignored; the vocab is always 256.