Skip to main content

High-performance Byte Pair Encoding (BPE) tokenizer in Rust with Python bindings

Project description

bpe_rs

PyPI Python Versions License CI Rust

A high-performance Byte Pair Encoding (BPE) tokenizer implemented in Rust with Python bindings via PyO3. Extracted from ground_llm for standalone reuse across LLM pipelines.

Features

  • Fast BPE Training — Efficiently learns merge rules from training data.
  • Parallelized Statistics — Uses rayon to parallelize pair-frequency computation.
  • Incremental Updates — Pair counts are updated on each merge instead of a full recount.
  • Python Bindings — Seamless integration with LLM pipelines via PyO3/maturin.
  • Tiktoken-Compatible Pre-Tokenizer — GPT-family regex splitting.

Installation

pip install bpe_rs

Requirements: Python 3.8+

Quick Start

from bpe_rs import BPETokenizer

tok = BPETokenizer()
tok.train("hello world hello rust bpe tokenizer is fast")

tokens = tok.encode("hello rust")
print(tokens)

text = tok.decode(tokens)
print(text)

API

BPETokenizer

class BPETokenizer:
    def __init__(self, pattern: str | None = None): ...
    def train(self, text: str | list[str]) -> None: ...
    def encode(self, text: str) -> list[int]: ...
    def decode(self, ids: list[int]) -> str: ...
    def save(self, path: str) -> None: ...
    @classmethod
    def load(cls, path: str, pattern: str | None = None) -> BPETokenizer: ...

Low-level functions

def build_info() -> str: ...
def encode_train(s: list[str]) -> list[tuple[tuple[int, int], int]]: ...
def encode(s: list[str], merges: list[tuple[tuple[int, int], int]]) -> list[int]: ...
def decode_string(ids: list[int], vocab_list: dict[int, list[int]]) -> str: ...

Pre-tokenization

from bpe_rs import pre_tokenize

chunks = pre_tokenize.split_text("hello world! this is a test.")
chunks = pre_tokenize.split_text_file("dataset.txt")

Examples

Training

from bpe_rs import BPETokenizer

tok = BPETokenizer()
tok.train(["hello world", "hello rust", "bpe tokenizer is fast"])
print(f"Vocabulary size: {len(tok._merges) + 256}")

Encoding and decoding

from bpe_rs import BPETokenizer

tok = BPETokenizer()
tok.train("the cat sat on the mat")

ids = tok.encode("the cat sat")
print(f"Token IDs: {ids}")

reconstructed = tok.decode(ids)
print(f"Reconstructed: {reconstructed}")

Saving and loading

from bpe_rs import BPETokenizer

# Train and save
tok = BPETokenizer()
tok.train("large training corpus goes here")
tok.save("my_tokenizer/")

# Load and reuse
tok2 = BPETokenizer.load("my_tokenizer/")
result = tok2.decode(tok2.encode("hello world"))
print(result)

Using a custom regex pattern

from bpe_rs import BPETokenizer

tok = BPETokenizer(pattern=r"\w+")
tok.train("hello world")

Precomputed vocab runs

Run Path Vocab size Notes
8,000 tokens data/8000/*.json 8,000 Default run
Full run data/full/*.json ~50,000 Larger experimental

Development

git clone https://github.com/vvaibhavv11/bpe_rs.git
cd bpe_rs

# Create virtual environment
uv venv
source .venv/bin/activate

# Install dev dependencies
uv sync

# Build and install in development mode
maturin develop

# Run tests
cargo test                 # Rust unit tests
python tests/test_bindings.py  # Python integration tests

Release

  1. Update version in Cargo.toml
  2. Commit: git commit -m "bump version to x.y.z"
  3. Tag: git tag vx.y.z
  4. Push: git push && git push --tags
  5. Create a GitHub Release from the tag
  6. The publish.yml workflow builds multi-platform wheels and publishes to PyPI automatically.

License

MIT — see LICENSE.

Project structure

bpe_rs/
├── src/
│   ├── lib.rs          # PyO3 module definition
│   └── tokenizer.rs    # Core BPE logic
├── python/bpe_rs/
│   ├── __init__.py     # Public API
│   ├── __init__.pyi    # Type stubs
│   ├── py.typed        # PEP 561 marker
│   ├── tokenizer.py    # Python BPETokenizer class
│   └── pre_tokenize.py # Regex pre-tokenizer
├── data/               # Precomputed vocab files
├── tests/
├── Cargo.toml
├── pyproject.toml
└── README.md

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

bpe_rs-0.1.0-cp314-cp314-win_amd64.whl (182.1 kB view details)

Uploaded CPython 3.14Windows x86-64

bpe_rs-0.1.0-cp314-cp314-macosx_11_0_arm64.whl (294.3 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

bpe_rs-0.1.0-cp313-cp313-win_amd64.whl (181.7 kB view details)

Uploaded CPython 3.13Windows x86-64

bpe_rs-0.1.0-cp313-cp313-manylinux_2_34_x86_64.whl (339.6 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ x86-64

bpe_rs-0.1.0-cp313-cp313-macosx_11_0_arm64.whl (294.0 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

bpe_rs-0.1.0-cp312-cp312-win_amd64.whl (181.5 kB view details)

Uploaded CPython 3.12Windows x86-64

bpe_rs-0.1.0-cp312-cp312-manylinux_2_34_x86_64.whl (339.7 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

bpe_rs-0.1.0-cp312-cp312-macosx_11_0_arm64.whl (293.8 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

bpe_rs-0.1.0-cp311-cp311-win_amd64.whl (182.5 kB view details)

Uploaded CPython 3.11Windows x86-64

bpe_rs-0.1.0-cp311-cp311-macosx_11_0_arm64.whl (295.6 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

bpe_rs-0.1.0-cp310-cp310-win_amd64.whl (182.4 kB view details)

Uploaded CPython 3.10Windows x86-64

File details

Details for the file bpe_rs-0.1.0-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: bpe_rs-0.1.0-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 182.1 kB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for bpe_rs-0.1.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 e55ee7a8130cb4230f7e3d7631a826bd6734f25c10021494c580d9696d8ff4f5
MD5 17e166131c4ed221d8ff73b9d377c10d
BLAKE2b-256 cee871998b2071dfec562545c421fc89d117a0d64884a72d3251680b9b3f1f8b

See more details on using hashes here.

Provenance

The following attestation bundles were made for bpe_rs-0.1.0-cp314-cp314-win_amd64.whl:

Publisher: publish.yml on vvaibhavv11/bpe_rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bpe_rs-0.1.0-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for bpe_rs-0.1.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 df0c00614695281639150b3ef6b12c7fcd7a738b61037d88426549fbd1478dd2
MD5 4ad9ce1aed965dbb74a56e5589496dad
BLAKE2b-256 5ad4509b9837c9adf488b0fbe055c51b4807a1e2f8b4eccb302e3e2b2e1fa05f

See more details on using hashes here.

Provenance

The following attestation bundles were made for bpe_rs-0.1.0-cp314-cp314-macosx_11_0_arm64.whl:

Publisher: publish.yml on vvaibhavv11/bpe_rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bpe_rs-0.1.0-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: bpe_rs-0.1.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 181.7 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for bpe_rs-0.1.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 adb8b25707fe397f481739a684ffbcb6fa06b86e9114c2a9dcd4cf40f656d0c4
MD5 06dcccd55f275b47c5b98aca7898ad68
BLAKE2b-256 aec72994c1fad07fe2e9e0c19ff4f851eeac8146a2e6716a2fae66c0d9087768

See more details on using hashes here.

Provenance

The following attestation bundles were made for bpe_rs-0.1.0-cp313-cp313-win_amd64.whl:

Publisher: publish.yml on vvaibhavv11/bpe_rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bpe_rs-0.1.0-cp313-cp313-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for bpe_rs-0.1.0-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 5ab833c86f5dc6bab6a07f4560583d15d48f86132421bd23558853490681b7c6
MD5 bbff246891c7949f06c7d2f189687886
BLAKE2b-256 cca96f2362384dada4ca11acdcc387effab011a3ec35c58efbe9abf30270a4c8

See more details on using hashes here.

Provenance

The following attestation bundles were made for bpe_rs-0.1.0-cp313-cp313-manylinux_2_34_x86_64.whl:

Publisher: publish.yml on vvaibhavv11/bpe_rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bpe_rs-0.1.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for bpe_rs-0.1.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6bde8f226b279ceb0836dd59e22d06fd3f0d7ced441c3d8908242e27645c5111
MD5 7c94be9593ea6ebc3ac4b8fc9794fd07
BLAKE2b-256 f564ae53177c7d86ce980b30295b8d2ecf367efce1e97c14fa518d0adbc66edf

See more details on using hashes here.

Provenance

The following attestation bundles were made for bpe_rs-0.1.0-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: publish.yml on vvaibhavv11/bpe_rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bpe_rs-0.1.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: bpe_rs-0.1.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 181.5 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for bpe_rs-0.1.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 d34361c54253a8f62ab1ab0f8eee28ca1422506a2781f7221d43331d77fbfd3d
MD5 6db887afb69f7758d2f5dcd6266ea283
BLAKE2b-256 a268d58076ea47c2da34ac1983ae112e27e55de2fc8513722be0ae5d4fa79cf5

See more details on using hashes here.

Provenance

The following attestation bundles were made for bpe_rs-0.1.0-cp312-cp312-win_amd64.whl:

Publisher: publish.yml on vvaibhavv11/bpe_rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bpe_rs-0.1.0-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for bpe_rs-0.1.0-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 274cba89d5ae8080a787d245a1099b3b6e979fc256cec5dbe0ff9389d5e2384d
MD5 85b7be2e3f583a55b0e275e9198307b2
BLAKE2b-256 3d4c706ebbb5b8cc7f027600620564932faee6b0ecd020ca7bf8613fd338f830

See more details on using hashes here.

Provenance

The following attestation bundles were made for bpe_rs-0.1.0-cp312-cp312-manylinux_2_34_x86_64.whl:

Publisher: publish.yml on vvaibhavv11/bpe_rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bpe_rs-0.1.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for bpe_rs-0.1.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 00c8a0d5b7d1dde3ce4cf2566b362a18c7d7d156679a826c55ed68991091120e
MD5 6c7267484aff0cd97600c64ef8f5a6ec
BLAKE2b-256 c08c7ee12c7680cf17bfba8bbc6933126c45742a2a36fe9f2920415b5fe40589

See more details on using hashes here.

Provenance

The following attestation bundles were made for bpe_rs-0.1.0-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: publish.yml on vvaibhavv11/bpe_rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bpe_rs-0.1.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: bpe_rs-0.1.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 182.5 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for bpe_rs-0.1.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 46cc8e1fd7683b18d3ca1a1c0b59df92edd93ed306ac43d56d4f0d9eb8173efe
MD5 2321292e51f55f67c3e046932a5c7ca3
BLAKE2b-256 77a0a57bad7998a0c0c1eb38b58d71224343f3e1faf76c2ba9c99f3ee0b8929e

See more details on using hashes here.

Provenance

The following attestation bundles were made for bpe_rs-0.1.0-cp311-cp311-win_amd64.whl:

Publisher: publish.yml on vvaibhavv11/bpe_rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bpe_rs-0.1.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for bpe_rs-0.1.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4c94b4b00d398f4c1458395a8cc9c3bfd55deaa1f1326187a85c574c6692f816
MD5 7f407f026935f0acee40804aa7674f65
BLAKE2b-256 06c4853b64f8004f2dd82f5c7613efb1af41f4fed2c729c9d3aedaad27f31cd5

See more details on using hashes here.

Provenance

The following attestation bundles were made for bpe_rs-0.1.0-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: publish.yml on vvaibhavv11/bpe_rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bpe_rs-0.1.0-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: bpe_rs-0.1.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 182.4 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for bpe_rs-0.1.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 98dd1d8395c31132994680a26d345e135bbb04152360b40f2346a66f9b8b8332
MD5 32087f73cfc4fba26b56ef20b78cb7f9
BLAKE2b-256 b46f6e1be53e5ce04d0ffda5b247d91c8701d910f383833f4424fb7454012b5c

See more details on using hashes here.

Provenance

The following attestation bundles were made for bpe_rs-0.1.0-cp310-cp310-win_amd64.whl:

Publisher: publish.yml on vvaibhavv11/bpe_rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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