Skip to main content

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

Project description

unitoken

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|>"])  # first token is treated as EOT
trainer.add_words({"hello": 10, "world": 7})
trainer.train(vocab_size=256)
trainer.save("demo")

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

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.

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.2.tar.gz (70.7 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.2-cp38-abi3-win_amd64.whl (1.4 MB view details)

Uploaded CPython 3.8+Windows x86-64

uni_tokenizer-0.1.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.5 MB view details)

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

uni_tokenizer-0.1.2-cp38-abi3-macosx_11_0_arm64.whl (1.3 MB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: uni_tokenizer-0.1.2.tar.gz
  • Upload date:
  • Size: 70.7 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.2.tar.gz
Algorithm Hash digest
SHA256 3df97f873594c9ad1345c52eec02608734ade8e070857dd8039d82e567586101
MD5 68c9a501407970e4d1d7a1acd905dc14
BLAKE2b-256 6d6d37af07eb2a40020a601eff4afaa988c841d2348fcb75b5abec5d90942b52

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for uni_tokenizer-0.1.2-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 1aba045bf1b2054325e508c37050c72cfef0e9102fbe6c7bdb752408763f1f53
MD5 b7b145983cac7a7613036ee84c32b7a9
BLAKE2b-256 c5e3de4d8f323b797af880d4dc8c33903b662dc6d5ea0b53bd4b959102d5badc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for uni_tokenizer-0.1.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f13e38537ecb881b08bc1a9c68eb782259721609729dd9987ef63514287ca91a
MD5 91d892f7ca711df9df68b1c8a33e63d6
BLAKE2b-256 7df156ce1a5ac9bc13d07b22a0927ab56225dc6bf0a2202b784b62ab43a624bb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for uni_tokenizer-0.1.2-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8ca9bb3a001ed6c6ec14081e2fda34e19e869c69d8734813f60fea3f5836f957
MD5 7738e844ec06663bd6f87a28b02199c4
BLAKE2b-256 b05a4b94d00cd76dc5361316057a060710fb3be362b07c898cabc2e749d25c72

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