Skip to content
class

ByteLevelBPETokenizer

extends_ByteLevelDecodeMixinBPETokenizer
ByteLevelBPETokenizer(vocab: dict[str, int], merges: list[tuple[str, str]], add_prefix_space: bool = False, normalizer: Normalizer | None = None, special_tokens: SpecialTokens | None = None)

Pure-Python byte-level BPE (GPT-2 / RoBERTa convention).

Subclasses BPETokenizer and swaps in a lucid.utils.tokenizer._pre_tokenizers.ByteLevel pre-tokenizer so the underlying BPE algorithm sees a printable Unicode alphabet of size 256 instead of raw bytes. The GPT-2 byte-to-unicode mapping shifts the unprintable bytes (0x00–0x20, 0x7F–0xA0) into a safe Unicode block so every byte sequence is representable and no UNK token is needed.

The merge table + training loop are inherited unchanged; the only differences are the pre-tokenizer (encode side) and the byte-mapping reversal in ByteLevelBPETokenizer.decode (decode side).

For production / latency-sensitive use, prefer ByteLevelBPETokenizerFast.

Parameters

vocabdict[str, int]
Token-string → id map. Tokens are the GPT-2 byte-mapped Unicode strings (e.g. "Ġworld" for world), not raw bytes.
mergeslist[tuple[str, str]]
Ordered BPE merge pairs — index = rank (lower = higher priority). Same format as plain BPE.
add_prefix_spacebool= False
Whether to prepend a space to the input before pre-tokenizing (so the first word also gets a Ġ prefix). GPT-2 default is False; RoBERTa default is True.
normalizerNormalizer= None
Pre-encode normalisation chain. Default lucid.utils.tokenizer._normalizers.NFC (matches GPT-2).
special_tokensSpecialTokens= None
Special-token registry — see lucid.utils.tokenizer.SpecialTokens.

Notes

Any published Hugging Face GPT-2 / RoBERTa / BART / distilGPT checkpoint loads without modification via ByteLevelBPETokenizer.from_pretrained.

Examples

>>> from lucid.utils.tokenizer import ByteLevelBPETokenizer
>>> tok = ByteLevelBPETokenizer(vocab={}, merges=[])
>>> tok.train(["the quick brown fox", "the lazy dog sleeps"],
...           vocab_size=300)
>>> tok.decode(tok.encode("the quick dog"))
'the quick dog'
Exact, unlike raw BPETokenizer — the byte-level alphabet
keeps the spaces that plain BPE drops.

See Also

  • ByteLevelBPETokenizerFast—C++-backed equivalent with the same API.
  • BPETokenizer—Classical (non byte-level) BPE base.

Used by 2

Constructors

1
dunder

__init__

→None
__init__(vocab: dict[str, int], merges: list[tuple[str, str]], add_prefix_space: bool = False, normalizer: Normalizer | None = None, special_tokens: SpecialTokens | None = None)

Construct a pure-Python byte-level BPE tokenizer.

Parameters

vocabdict[str, int]
GPT-2 byte-mapped token → id map.
mergeslist of (str, str)
BPE merge pairs over the byte-mapped alphabet.
add_prefix_spacebool= False
Prepend a space before pre-tokenizing (GPT-2: False, RoBERTa: True).
normalizer(Normalizer or None, optional, keyword - only)= None
Pre-encode normaliser. Defaults to NFC.
special_tokens(SpecialTokens or None, optional, keyword - only)= None
Special-token registry.

Notes

The pre-tokenizer is fixed to ByteLevel with the chosen add_prefix_space — that's the whole point of byte-level BPE. Forwards everything else to BPETokenizer.__init__.

Properties

1
prop

algo

→strfrom _ByteLevelDecodeMixin
algo: str

Algorithm identifier (always "byte_bpe").

Class methods

1
cls

from_file

→Self
from_file(directory: str, add_prefix_space: bool = False, normalizer: Normalizer | None = None, special_tokens: SpecialTokens | None = None)

Load from a Hugging Face-compatible directory.

Instance methods

4
fn

get_vocab

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

Return a shallow copy of the token → id map.

fn

id_to_token

→str | Nonefrom _BPECommonMixin
id_to_token(token_id: int)

Look up the surface string for a token id.

fn

save

→Nonefrom _BPECommonMixin
save(directory: str)

Persist as both legacy (vocab.json + merges.txt) AND unified (tokenizer.json) formats.

fn

train

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

Re-train this tokenizer from scratch on corpus.

Delegates to the parent BPETokenizer.train. The configured ByteLevel pre-tokenizer means the algorithm sees byte-mapped chunks throughout training, so the resulting vocab + merges are in GPT-2 byte-mapped form (compatible with Hugging Face GPT-2 / RoBERTa export).

Parameters

corpusiterable of str
Each item is one document. Generators are consumed exactly once.
vocab_sizeint= 50 000
Target total vocab size. GPT-2's published vocab is 50 257; RoBERTa's is 50 265.