- Python 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| data | ||
| src/axe | ||
| tests | ||
| .gitignore | ||
| .python-version | ||
| pyproject.toml | ||
| README.md | ||
| uv.lock | ||
axe
Three small byte-based tokenizer baselines are available: BPE, which learns frequent adjacent merges, and unigram, which learns piece probabilities and uses Viterbi segmentation, and MinGram, which uses a BPE seed and hard EM to learn a vocabulary for minimum-token segmentation. All preserve whitespace and support unseen Unicode text through byte fallback. Vocabulary sizes must be at least 256 and are upper bounds; a small corpus may produce fewer tokens.
Train and save a tokenizer from a UTF-8 file (each line is a training sample):
axe-train-tokenizer --file corpus.txt --tokenizer bpe --vocab-size 1000 --output tokenizer.json
For a streamed Hugging Face dataset, install the optional dependency with
pip install 'axe[hf]' and specify the dataset and text column:
axe-train-tokenizer --dataset wikitext --config wikitext-2-raw-v1 --split train \
--text-column text --max-samples 10000 --tokenizer unigram --vocab-size 1000 --output tokenizer.json
The tokenizer can be bpe, unigram, or mingram. Use --pretokenizer boundary
to select boundary markers. --max-samples limits file lines or dataset rows;
without it the entire source is consumed. Dataset input is streamed, but the
trainers still retain corpus statistics in memory. See axe-train-tokenizer --help
for all options. The CLI reports processed rows or lines and file bytes to standard
error, followed by progress through vocabulary training stages. Training may make
multiple passes over the corpus. Use --workers 4 (or workers=4 in the Python training API)
to parallelize Unigram training and MinGram preparation and encoding passes across CPU processes;
BPE merge learning runs in one process using incremental counts. The default is one worker. Process
startup and data transfer can make parallel training slower on small corpora and
use more memory. Unigram results may vary slightly with worker count due to
floating-point summation order. When using multiple workers from a Python script,
call training under an if __name__ == "__main__": guard. Load the resulting JSON with the corresponding tokenizer class's
deserialize method.
from axe import BPE, MinGram, Unigram
corpus = ["Some training text.", "More training text."]
tokenizer = BPE.train(corpus, vocab_size=300)
# Unigram.train(corpus, vocab_size=300) uses the same interface.
# MinGram.train(corpus, vocab_size=300) uses the same interface.
ids = tokenizer.encode("Some text. 🐈")
assert tokenizer.decode(ids) == "Some text. 🐈"
tokenizer.serialize("tokenizer.json")
tokenizer = BPE.deserialize("tokenizer.json")
BaseTokenizer.pretokenize(text) splits text into whitespace, alphanumeric
runs, underscores, and individual punctuation or symbols without discarding
characters. Tokens do not cross these boundaries. Unigram considers candidate
pieces up to eight bytes long. MinGram defaults to an overshoot factor of 1.15
for learned tokens and two hard-EM iterations; these can be adjusted with the
overshoot and em_iterations training arguments. Its byte alphabet and
pretokenization differ from the paper's SCRIPT encoding. These implementations
are intended as simple baselines rather than optimized trainers for large corpora.
The optional boundary pretokenizer implements the word, punctuation, digit,
and case marker scheme from Explicit Boundary Markers for Subword Vocabularies:
tokenizer = BPE.train(corpus, vocab_size=300, pretokenizer="boundary")
ids = tokenizer.encode("Ash caught 3 cats. WOW!")
assert tokenizer.decode(ids) == "Ash caught 3 cats. WOW!"
assert tokenizer.decode(ids, direct=True) == "↑¦ash¦¦caught¦¦3¦¦cats¦.¦⇑¦wow¦!"
The same option works with Unigram and MinGram and is saved with the model.
Direct decoding displays the boundary marker as ¦, title-case code as ↑, and
uppercase code as ⇑. These are distinct internal byte tokens, so literal
occurrences of those characters still round-trip through regular decoding. Direct
decoding is a visualization and cannot distinguish literal marker characters from
introduced markers. BaseTokenizer.pretokenize(text, "boundary") returns internal
strings containing surrogate escapes for the three markers.
Consecutive numeric characters are split into individual pretoken pieces, with
space boundary markers attached to the first and last pieces of the run.
The scheme uses the paper's 20 space-delimited scripts and case reversibility checks, but retains this repository's UTF-8 byte alphabet instead of SCRIPT's script-category alphabet. Pretoken boundaries for other scripts and symbols can therefore differ from those in the paper.