Skip to main content

Fast BPE tokenizer/trainer with a Rust core and Python bindings

Project description

unitoken

CI PyPI crates.io docs.rs

unitoken is a fast BPE tokenizer/trainer with a Rust core and optional Python bindings.

Install

Rust:

cargo add unitoken

Python (wheels via PyPI):

pip install uni-tokenizer

Quickstart (Python)

from uni_tokenizer import BpeTrainer, BpeEncoder

trainer = BpeTrainer(["<|endoftext|>"], unit="byte")
trainer.add_words({"hello": 10, "world": 7})
trainer.train(vocab_size=256)
model = trainer.validate_model()
model.save("demo", format="gpt2")

enc = BpeEncoder.load("demo")
ids = enc.encode("hello")

Streaming two-pass counting

For corpora that do not fit in memory, expose a replayable source whose scan() method returns a fresh iterator of text records. Rust pulls and processes bounded batches from each scan:

from uni_tokenizer import BpeTrainer, PreTokenizer

pretokenizer = PreTokenizer([])

bigram_counter = pretokenizer.bigram_counter()
bigram_counter.add_source(source.scan())
bigrams = bigram_counter.selected(top_k=100_000, min_freq=16)

word_counter = pretokenizer.with_unicode_bigrams(bigrams).word_counter()
word_counter.add_source(source.scan())

trainer = BpeTrainer([], unit="byte")
trainer.add_word_counter(word_counter)

add_source defaults to at most 4,096 records or 64 MiB per batch. Override max_records and max_bytes for the record sizes and worker memory available. By default, it overlaps Python source iteration with Rust processing using one bounded look-ahead batch; pass prefetch=0 for synchronous processing. Counters can also be merged, so separately counted corpus partitions can be reduced before selecting bigrams or training. add_word_counter consumes the native word inventory without constructing a Python dictionary; the counter is empty and reusable afterward. word_counter.words() remains available for small inventories, but copies the complete result into Python memory.

Tiktoken-compatible API

unitoken also exposes a tiktoken-shaped Python API:

from uni_tokenizer import Encoding

enc = Encoding.from_files(
  "demo",
  vocab_file="vocab.demo[u8].json",
  merges_file="merges.demo[u8].txt",
  special_tokens={"<|endoftext|>": 0},
)

ids = enc.encode("hello world")
text = enc.decode(ids)

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:

uv pip install "tiktoken>=0.12.0"
python benchmarks/compare_tiktoken.py

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:

uv pip install "tokenizers>=0.22.1"
python benchmarks/compare_hf_training.py

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:

python benchmarks/compare_hf_training.py --text out/fineweb2_1GiB.txt --chunk-size 1048576 --boundary line --repeats 1

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.

Latest fixed-word trainer profile on the release build, using compressed _words.json inventories and vocab_size=10000:

dataset unique words occurrences total train train steps
FineWeb English 64MiB 298,156 13,720,494 1.151s 0.968s
FineWeb English 1GiB 1,656,501 219,082,524 4.522s 3.258s
FineWeb2 Chinese 64MiB 1,803,009 5,774,521 26.681s 20.416s
FineWeb2 Chinese bigram 64MiB 606,153 15,901,831 3.702s 3.034s
FineWeb2 Chinese bigram 1GiB 3,855,974 249,919,657 20.197s 14.169s

The Chinese bigram rows use the unicode-bigram split inventory. The default Chinese 1GiB inventory is intentionally omitted from this run; only the bigram 1GiB Chinese inventory was profiled.

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:

python benchmarks/create_fineweb2_sample.py --input-dir /path/to/fineweb2/10BT

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.

maturin develop

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

uni_tokenizer-0.1.5.tar.gz (108.9 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

uni_tokenizer-0.1.5-cp38-abi3-win_amd64.whl (1.5 MB view details)

Uploaded CPython 3.8+Windows x86-64

uni_tokenizer-0.1.5-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ x86-64

uni_tokenizer-0.1.5-cp38-abi3-macosx_11_0_arm64.whl (1.5 MB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

File details

Details for the file uni_tokenizer-0.1.5.tar.gz.

File metadata

  • Download URL: uni_tokenizer-0.1.5.tar.gz
  • Upload date:
  • Size: 108.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for uni_tokenizer-0.1.5.tar.gz
Algorithm Hash digest
SHA256 59ca4058284aa8052030924cefd114a98251907196dde4e8ccddeb3aadcfcd7f
MD5 f9ea8a3ae1183021d822f1275f8c80b7
BLAKE2b-256 76ee22429d6e0c0eb60203087fea8439fa0f4fc52b1f769d37d2f887d9bcb8bf

See more details on using hashes here.

File details

Details for the file uni_tokenizer-0.1.5-cp38-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for uni_tokenizer-0.1.5-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 c1dd87c5646e2b6cba9800ffb000935a5d566dadc1bea2c5083d36bd49908d68
MD5 4f9007fd1df38aad81b1881eb9c17984
BLAKE2b-256 cfc7299d53f5f3f1d195ef86b7826f740363371ed027249015505211c72d8c50

See more details on using hashes here.

File details

Details for the file uni_tokenizer-0.1.5-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for uni_tokenizer-0.1.5-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 40e97c8101a558c42884dc384c2007cff0b92ae004cdcc7484b4d40ee8443a2d
MD5 1dc1d4e1489a6354bb1ed2eb93535951
BLAKE2b-256 15721ec5556d24c85cbbcaf3d5a835f8c99bed2347ffdfebac7ccee3c5f1f1e9

See more details on using hashes here.

File details

Details for the file uni_tokenizer-0.1.5-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for uni_tokenizer-0.1.5-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4c9b19e2d2556264f371779964b431bb14163e06d3be25f57dd2c649fec80b37
MD5 d78b37d8f2a7c653ff4e1d4035592674
BLAKE2b-256 7e93fd6a0bfc67c8d918f501e6b0d0af0613bb4123f8039146dd08a06d1002b6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page