Skip to content
class

ByteTokenizerFast

extendsTokenizer
ByteTokenizerFast(special_tokens: SpecialTokens | None = None)

C++-backed byte tokenizer; bit-identical to ByteTokenizer.

Same semantics as ByteTokenizer, hot loop in C++. Speedup vs the Python flavour is modest (the algorithm is already a trivial UTF-8 encode loop) but the API uniformity matters: any code that swaps XxxTokenizer ↔ XxxTokenizerFast works consistently across the family.

Parameters

special_tokensSpecialTokens= None
Special-token registry; see ByteTokenizer for the constraints on default-vocab compatibility.

Examples

>>> from lucid.utils.tokenizer import ByteTokenizerFast
>>> tok = ByteTokenizerFast()
>>> tok.decode(tok.encode("the quick dog"))
'the quick dog'

Used by 1

Constructors

1
dunder

__init__

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

Construct a C++-backed byte tokenizer.

Parameters

special_tokensSpecialTokens or None= None
Optional special-token registry.

Notes

Instantiates the C++ ByteTokenizer backend; the canonical 256-entry Python vocab is also cached for get_vocab / id_to_token parity.

Properties

2
prop

algo

→str
algo: str

Algorithm identifier (always "byte").

prop

vocab_size

→int
vocab_size: int

Fixed vocab size (256), as reported by the C++ backend.

Class methods

1
cls

from_file

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

Load from directory (only the special-token map is read).

Parameters

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

Returns

ByteTokenizerFast

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)

Return the Latin-1 char for token_id or None.

fn

save

→None
save(directory: str)

Persist as tokenizer.json + special_tokens_map.json.

Parameters

directorystr
Output directory (created if missing).
fn

train

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

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