Skip to content
class

ByteLevelBPETokenizerFast

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

C++-backed byte-level BPE tokenizer.

Identical configuration semantics to ByteLevelBPETokenizer; uses the engine BPE binding for the merge hot loop and the same ByteLevel pre-tokenizer in Python. Encode outputs are bit-identical to the pure-Python flavour for the same vocab + same merges.

It takes the parameters of ByteLevelBPETokenizer.

Examples

>>> from lucid.utils.tokenizer import ByteLevelBPETokenizerFast
>>> tok = ByteLevelBPETokenizerFast(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'

See Also

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

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 C++-backed 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 default False; RoBERTa default 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

Fixes the pre-tokenizer to ByteLevel and forwards to BPETokenizerFast.__init__ — which constructs the C++ BPE backend and caches it on _cpp.

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 in C++ via the parent BPETokenizerFast.train.

The ByteLevel pre-tokenizer normalizes every byte to its printable form before the corpus reaches the C++ training loop, so the resulting vocab is in byte-mapped form (Hugging Face GPT-2 compatible).

Note: the C++ BPE::train does its OWN whitespace split on the raw input, so we pre-encode the corpus through the ByteLevel pre-tokenizer here in Python before handing off — otherwise the training would learn merges on raw bytes rather than byte-mapped codepoints.

Parameters

corpusiterable of str
Each item is one document.
vocab_sizeint= 50 000
Target total vocab size.