Encoder¶
ffbpe.encoder.BpeEncoder
¶
BpeEncoder(unit='byte', *, special_tokens=None, merges=None, vocab=None, pat_str=None, unicode_bigrams=None, unicode_bigram_mixed_boundary='keep', split_on_vocab_bigrams=True)
BPE encoder.
This is a thin Python wrapper around the Rust implementation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unit
|
Unit
|
Primary segmentation unit. Unicode models may include UTF-8 byte fallback merges. |
'byte'
|
special_tokens
|
Sequence[str] | None
|
Optional list of special tokens. When provided, they are treated as indivisible tokens. |
None
|
merges
|
list[tuple[bytes, bytes]] | None
|
In-memory merge rules and vocabulary. If omitted, use :meth: |
None
|
split_on_vocab_bigrams
|
bool
|
Whether encoding may partition PAT words using bigrams derived from the model vocabulary. Disable it for byte models when that optimization is slower for the workload. |
True
|
Source code in python/ffbpe/encoder.py
load
classmethod
¶
load(name=None, *, unit='byte', format=None, special_tokens=None, input_dir=None, merges_file=None, vocab_file=None, pat_str=None, unicode_bigrams=None, unicode_bigram_mixed_boundary='keep', split_on_vocab_bigrams=True)
Load an encoder from vocab/merge files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
Optional model name used to derive default filenames:
|
None
|
unit
|
Unit
|
Primary segmentation unit ( |
'byte'
|
format
|
FileFormat | None
|
Override the format used to decode the files ( |
None
|
special_tokens
|
Sequence[str] | None
|
Optional list of special tokens to configure the encoder. |
None
|
input_dir
|
str | PathLike | None
|
Optional directory to resolve |
None
|
merges_file
|
str | PathLike | None
|
Explicit filenames/paths for merges and vocab. |
None
|
pat_str
|
str | None
|
Optional pretokenizer regex. |
None
|
unicode_bigrams
|
Sequence[str] | None
|
Optional retained Unicode bigrams used to shape pretokenizer boundaries. |
None
|
unicode_bigram_mixed_boundary
|
str
|
Mixed-boundary policy: |
'keep'
|
split_on_vocab_bigrams
|
bool
|
Whether encoding may partition PAT words using model-vocabulary bigrams. Disable it for byte models when benchmarking shows no benefit. |
True
|
Source code in python/ffbpe/encoder.py
from_pretrained
classmethod
¶
Load an encoder from a directory created by BpeModel.save_pretrained.
Source code in python/ffbpe/encoder.py
encode_word
¶
encode_words
¶
encode
¶
encode_to_numpy
¶
encode_file
¶
Encode a text file into a NumPy array of token ids.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike
|
Path to a UTF-8 text file. |
required |
num_chunks
|
int
|
Number of chunks to split the file into. Chunk boundaries are aligned on the end-of-text token. |
1024
|