unitoken
unitoken is a fast BPE tokenizer/trainer with a Rust core and optional Python bindings.
Install
Rust:
Python (wheels via PyPI):
Quickstart (Python)
= # first token is treated as EOT
=
=
Tiktoken-compatible API
unitoken also exposes a tiktoken-shaped Python API:
=
=
=
The package also includes a uni_tokenizer.tiktoken namespace with Encoding,
get_encoding, encoding_for_model, encoding_name_for_model, and
list_encoding_names. Built-in registry names are limited to local unitoken
fixture models for now; use Encoding.from_files(...) for trained models.
Benchmark against tiktoken
Install the dev dependency and run:
The benchmark reports unitoken encode/decode timings and, when upstream
tiktoken is importable, matching upstream timings.
Benchmark training against Hugging Face
Install the dev dependency and run:
The benchmark trains unitoken and Hugging Face tokenizers on the same
word-frequency fixture, checks that the learned byte-level BPE vocabularies
match, and reports median training speed.
For an end-to-end raw text comparison:
Raw text mode reports unitoken pretokenization and BPE training phases
separately, then compares the total against Hugging Face raw training. By
default, Hugging Face receives the same chunk boundaries as unitoken so vocab
parity is not affected by iterator boundary differences. Pass
--hf-chunk-bytes to force fixed byte chunks for Hugging Face.
Chunking supports explicit boundary modes:
auto: split on the EOT token when present, otherwise line boundaries, then UTF-8 byte boundaries as a last resort.eot: split only on the EOT token.line: split on newline boundaries.utf8: split near byte boundaries while preserving valid UTF-8.
Use --chunk-size BYTES when you want target chunk size instead of a fixed
chunk count.
Prepare benchmark data
To create a larger raw UTF-8 text sample from local FineWeb2 Parquet shards:
This is a data-preparation step. Use the generated text with the CLI or a separate benchmark that measures pretokenization/training on raw input.
Building from source
This project uses maturin for the Python extension module.