bpe_rs
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
rayonto 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
- Update
versioninCargo.toml - Commit:
git commit -m "bump version to x.y.z" - Tag:
git tag vx.y.z - Push:
git push && git push --tags - Create a GitHub Release from the tag
- The
publish.ymlworkflow 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
Metadata
Release files for bpe-rs 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distributions (wheels)
Total release size: 3.3 MB
Release files / bpe_rs-0.1.1-cp314-cp314-manylinux_2_34_x86_64.whl
| Download URL | bpe_rs-0.1.1-cp314-cp314-manylinux_2_34_x86_64.whl |
|---|---|
| Size | 350.5 kB |
| Tags | CPython 3.14 Linux glibc 2.34+ x86-64 |
|
SHA-256 checksum How to use checksums |
2cb36cdba720cc904b6b2cb395d9c3d9559fd115d86f78e997515e2ca15717e6
|
|
BLAKE2b-256 checksum How to use checksums |
cbe62f4b54ef118d48e3df05fa2d0da85bbca996e3b62f2f274fad0ab41e50d7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.
Transparency logRelease files / bpe_rs-0.1.1-cp314-cp314-macosx_11_0_arm64.whl
| Download URL | bpe_rs-0.1.1-cp314-cp314-macosx_11_0_arm64.whl |
|---|---|
| Size | 301.1 kB |
| Tags | CPython 3.14 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
1e943c6d33432abc544622cbb2e4d68f30edd0433e78c42eb61c9468c5ac11ff
|
|
BLAKE2b-256 checksum How to use checksums |
4a4f19da0035762ec9ab4aac0c060b76c42c319261fd81ae5c1a703b03540d79
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.
Transparency logRelease files / bpe_rs-0.1.1-cp313-cp313-manylinux_2_34_x86_64.whl
| Download URL | bpe_rs-0.1.1-cp313-cp313-manylinux_2_34_x86_64.whl |
|---|---|
| Size | 351.1 kB |
| Tags | CPython 3.13 Linux glibc 2.34+ x86-64 |
|
SHA-256 checksum How to use checksums |
4dcf06f66f0458eb175d6d4d0f793e09fd0e7124b5a7381a16f6af95ac8f555a
|
|
BLAKE2b-256 checksum How to use checksums |
c6cc730632552ada488fec2b2d2a32e9aed8d100e415bca7d1378228275492fa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.
Transparency logRelease files / bpe_rs-0.1.1-cp313-cp313-macosx_11_0_arm64.whl
| Download URL | bpe_rs-0.1.1-cp313-cp313-macosx_11_0_arm64.whl |
|---|---|
| Size | 301.8 kB |
| Tags | CPython 3.13 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
5ccb3002426193b61bdb2d764491dc244f425ff8e34afeadec8d79c5eb0e5c17
|
|
BLAKE2b-256 checksum How to use checksums |
e84c1e8c1f49e0ed6d5583021f920f55ea8354ccf709c3e4e74103f66399eb90
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.
Transparency logRelease files / bpe_rs-0.1.1-cp312-cp312-manylinux_2_34_x86_64.whl
| Download URL | bpe_rs-0.1.1-cp312-cp312-manylinux_2_34_x86_64.whl |
|---|---|
| Size | 351.3 kB |
| Tags | CPython 3.12 Linux glibc 2.34+ x86-64 |
|
SHA-256 checksum How to use checksums |
90997f8e7f4bbb0249bd7a0e04a2af52a4f94b57b3c1646d276bf38a7f709984
|
|
BLAKE2b-256 checksum How to use checksums |
263890d382bd7dc8ffa7772dba8202707ff3760f51bf20c36697658f7a391344
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.
Transparency logRelease files / bpe_rs-0.1.1-cp312-cp312-macosx_11_0_arm64.whl
| Download URL | bpe_rs-0.1.1-cp312-cp312-macosx_11_0_arm64.whl |
|---|---|
| Size | 301.8 kB |
| Tags | CPython 3.12 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
f358642e271da1ed308b3dd65063f000cb3090dff63ced71693f677856d85305
|
|
BLAKE2b-256 checksum How to use checksums |
c52fe6698ce792652035dc0543ebc15cd255827fa4951e5eebc1b4edbcb7cdd2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.
Transparency logRelease files / bpe_rs-0.1.1-cp311-cp311-manylinux_2_34_x86_64.whl
| Download URL | bpe_rs-0.1.1-cp311-cp311-manylinux_2_34_x86_64.whl |
|---|---|
| Size | 352.7 kB |
| Tags | CPython 3.11 Linux glibc 2.34+ x86-64 |
|
SHA-256 checksum How to use checksums |
6e750df48f0d027a8e7cbfb8b22a0c47df5f8adae4a64d593b12147f28f3f522
|
|
BLAKE2b-256 checksum How to use checksums |
a32e0b0b6039ec937b7c7d02b631e5ef3e18e9086595073a7cceaa5c1db86681
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.
Transparency logRelease files / bpe_rs-0.1.1-cp311-cp311-macosx_11_0_arm64.whl
| Download URL | bpe_rs-0.1.1-cp311-cp311-macosx_11_0_arm64.whl |
|---|---|
| Size | 303.2 kB |
| Tags | CPython 3.11 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
6df7401c79a12288b58742b7e4ae71f5340c30045f957643052cef9b0cc36170
|
|
BLAKE2b-256 checksum How to use checksums |
f70f209bb768c51bcc998f07658f8ef43a2d426a23d9fb242dce2f2f187d583e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.
Transparency logRelease files / bpe_rs-0.1.1-cp310-cp310-manylinux_2_34_x86_64.whl
| Download URL | bpe_rs-0.1.1-cp310-cp310-manylinux_2_34_x86_64.whl |
|---|---|
| Size | 352.9 kB |
| Tags | CPython 3.10 Linux glibc 2.34+ x86-64 |
|
SHA-256 checksum How to use checksums |
6d30cb94a329543e8639cfece3dcafd65343ed8081f01a0ac104448f9dba1358
|
|
BLAKE2b-256 checksum How to use checksums |
9315bbf5be47d6e2650cdabcb3edbc6c693461f4ebe81c9d667d1fe7250e245b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.
Transparency logRelease files / bpe_rs-0.1.1-cp310-cp310-macosx_11_0_arm64.whl
| Download URL | bpe_rs-0.1.1-cp310-cp310-macosx_11_0_arm64.whl |
|---|---|
| Size | 303.5 kB |
| Tags | CPython 3.10 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
39963a10e5bb3739d7a7e62e7755095a4ccd1973744e87f8f354bcaf8a8ed88d
|
|
BLAKE2b-256 checksum How to use checksums |
6c44e6d0e6502a102e0859637683550a29b6c5832c733c6855c0440d9f6a232b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.
Transparency logSecurity for bpe-rs 0.1.1
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.
Full provenance
All 10 files in this release have been uploaded with provenance metadata.
Attestation for bpe_rs-0.1.1-cp314-cp314-manylinux_2_34_x86_64.whl
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.
Transparency logA 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.