Skip to main content

🦀 Rust BPE Tokenizer (rust-bpe-tokenizer)

High-performance Byte-Pair Encoding (BPE) tokenizer implemented in Rust with Python FFI bindings (via PyO3) for Tolstoy-CLI.

🚀 Key Features

  • Extreme Speed: ~7–10x faster training and ~10–20x faster inference by bypassing Python GIL and leveraging Rayon multi-threading.
  • Drop-in replacement: 100% compatible Python API (train, encode, decode, save, load).
  • Standalone Rust API: Pure Rust file IO (BpeCore::save, BpeCore::load, to_bytes, from_bytes) without Python runtime dependency.
  • Python Protocols Support: Native support for pickle, copy.deepcopy, and multiprocessing (ProcessPoolExecutor).
  • Dual Inference Modes: Parallel Rayon mode (use_parallel=True) and sequential mode (use_parallel=False) for low-overhead tokenization of short texts.
  • Memory Safety: 100% safe, zero segmentation faults or memory leaks.
  • Compact Persistence: Bincode binary format (.bpe) with automatic fallback migration for legacy Python .pkl files.

📦 Installation

pip install .

💻 Python Usage

import pickle, copy
from rust_bpe_tokenizer import BPETokenizer

tokenizer = BPETokenizer()

# Train on text corpus
# Note: n_workers is accepted for API compatibility with the Python
# implementation and ignored — the Rayon thread pool is managed automatically.
tokenizer.train("Hello world! High performance BPE tokenizer.", vocab_size=300)

# Vocabulary inspection
print(f"Vocab size: {len(tokenizer)}, Merges: {tokenizer.merges_len}")

# Encode text into token IDs
tokens = tokenizer.encode("Hello world!", use_parallel=True)
print("Token IDs:", tokens)

# Decode back to text or raw bytes
decoded_text = tokenizer.decode(tokens)
raw_bytes = tokenizer.decode_bytes(tokens)
print("Decoded text:", decoded_text)

# Save & Load
tokenizer.save("model.bpe")
loaded_tokenizer = BPETokenizer.load("model.bpe")

# Multiprocessing & Pickle support
pickled = pickle.dumps(tokenizer)
restored = pickle.loads(pickled)

🦀 Pure Rust Usage

use rust_bpe_tokenizer::BpeCore;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let text = "High-performance Rust BPE tokenizer.";
    let mut tok = BpeCore::new();

    // Training
    tok.train_core::<fn(f64, &str)>(text, 300, None)?;

    // Encoding & Decoding
    let tokens = tok.encode(text)?;
    let decoded = tok.decode(&tokens);
    assert_eq!(decoded, text);

    // Save and Load binary model
    tok.save("model.bpe")?;
    let loaded = BpeCore::load("model.bpe")?;
    
    Ok(())
}

📊 Benchmarks

Run the benchmark suite:

python benchmarks/bench_bpe.py
  • Training: ~7.1x speedup (up to 5.6 MB/s)
  • Encoding (Rayon Parallel): ~10.4x speedup (up to 9.4 MB/s)
  • Decoding: ~3.2x speedup
  • Bincode Model Loading: ~2.3x speedup compared to Python pickle

Metadata

Release files for rust-bpe-tokenizer 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rust-bpe-tokenizer 0.1.2
File Size Uploaded Provenance
rust_bpe_tokenizer-0.1.2.tar.gz 40.0 kB Without provenance Details

Built distributions (wheels)

Table of built distributions (wheels) for rust-bpe-tokenizer 0.1.2
File
rust_bpe_tokenizer-0.1.2-cp38-abi3-win_amd64.whl CPython 3.8 abi3 Windows x86-64 Details
rust_bpe_tokenizer-0.1.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.8 abi3 Linux glibc 2.17+ x86-64 Details
rust_bpe_tokenizer-0.1.2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.8 abi3 Linux glibc 2.17+ ARM64 Details
rust_bpe_tokenizer-0.1.2-cp38-abi3-macosx_11_0_arm64.whl CPython 3.8 abi3 macOS 11.0+ ARM64 Details

Total release size: 3.6 MB

Release files / rust_bpe_tokenizer-0.1.2.tar.gz

Download URL rust_bpe_tokenizer-0.1.2.tar.gz
Size 40.0 kB
Tags Source
SHA-256 checksum
How to use checksums
cc620b99a6425c240663689fe7e261d4e91b337dfea7652355bb796aca5c79d4
BLAKE2b-256 checksum
How to use checksums
0973967832db987877e89eb81a5ffb343382fb57ff53a7936d045823f06fe48e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / rust_bpe_tokenizer-0.1.2-cp38-abi3-win_amd64.whl

Download URL rust_bpe_tokenizer-0.1.2-cp38-abi3-win_amd64.whl
Size 785.0 kB
Tags CPython 3.8 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
dcb3e2a3e5a932e849a57229ec98f122c96f573b5ab61057bb7bd568348a4888
BLAKE2b-256 checksum
How to use checksums
d3a8f09811d44cebe0bef911d1f058444624187551c2376969991f3c80a1463c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / rust_bpe_tokenizer-0.1.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL rust_bpe_tokenizer-0.1.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 966.1 kB
Tags CPython 3.8 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
a2975d161a1e4127258a447a9100834cf5e2f47e5d6bb2de50c491340058f86a
BLAKE2b-256 checksum
How to use checksums
2b5bab5bec00591db80be2ec3baf52b4d77874527772e77d52df6ef6d35c5989
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / rust_bpe_tokenizer-0.1.2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL rust_bpe_tokenizer-0.1.2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 943.3 kB
Tags CPython 3.8 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
a38b208eef30b8911e2ccef539cd770b6c8f6af1d625dbec0bfad267da6e4c7a
BLAKE2b-256 checksum
How to use checksums
0c908c11e51a79a81a65fff6aecd7893823caad7e8ff4f725b11a0993d83557e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / rust_bpe_tokenizer-0.1.2-cp38-abi3-macosx_11_0_arm64.whl

Download URL rust_bpe_tokenizer-0.1.2-cp38-abi3-macosx_11_0_arm64.whl
Size 838.3 kB
Tags CPython 3.8 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
69040c47daeb9594e8425b37dd6c8721f9463929288c57f367e30a8b680eb6b1
BLAKE2b-256 checksum
How to use checksums
f2f5a21e2dd5e7c29d384db7cef6e587ebbc42ea94ac2956a2c88c5903ee9eca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for rust-bpe-tokenizer 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.2 This release

5 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page