High-performance byte-level tokenizer with BPE support
Project description
BLT Python Bindings
High-performance Python bindings for BLT (Byte-Level Tokenizer), providing fast byte-level tokenization with BPE support.
🚀 Features
- High Performance: Rust-powered tokenization with multi-threading support
- BPE Support: Byte-Pair Encoding with custom merge rules
- Memory Efficient: Memory-mapped I/O and configurable memory usage
- Easy to Use: Simple Python API with comprehensive error handling
- Cross-Platform: Works on Linux, macOS, and Windows
📦 Installation
pip install blt-tokenizer
Development Installation
# Clone the repository
git clone https://github.com/jtrefon/blt.git
cd blt/blt_python
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install maturin and build
# On Linux
pip install maturin[patchelf]
# On macOS or Windows
pip install maturin
# Build and install for development
maturin develop
# Install development dependencies
pip install -e .[dev]
🔧 Usage
Basic File Tokenization
import blt
# Create a tokenizer
tokenizer = blt.ByteTokenizer()
# Tokenize a file
tokenizer.tokenize_file("input.txt", "output.bin")
BPE Tokenization
import blt
# Load BPE merges from file
merges = blt.load_bpe_merges("merges.txt")
# Or define merges manually
merges = {
(97, 98): 256, # 'a' + 'b' -> token 256
(99, 100): 257, # 'c' + 'd' -> token 257
(101, 102): 258, # 'e' + 'f' -> token 258
}
# Create tokenizer with BPE merges
tokenizer = blt.ByteTokenizer(merges=merges)
tokenizer.tokenize_file("input.txt", "output.bin")
Advanced Configuration
import blt
# Create tokenizer with custom settings
tokenizer = blt.ByteTokenizer(
content_type="Text", # "Text" or "Bin"
threads=4, # Number of processing threads
chunk_size="1MB", # Chunk size for processing
memory_cap=50 # Memory usage cap (0-100%)
)
tokenizer.tokenize_file("large_file.txt", "output.bin")
📖 API Reference
ByteTokenizer
Main tokenizer class for byte-level tokenization.
Constructor
ByteTokenizer(
merges=None, # Dict[Tuple[int, int], int] - BPE merge rules
content_type=None, # str - "Text" or "Bin"
threads=None, # int - Number of threads
chunk_size=None, # str - Chunk size (e.g., "1MB")
memory_cap=None # int - Memory cap percentage (0-100)
)
Methods
tokenize_file(input_path, output_path): Tokenize a file and save resultsinput_path(str): Path to input fileoutput_path(str): Path to output file- Raises:
RuntimeError,IOError
Utility Functions
-
load_bpe_merges(path): Load BPE merges from file- Returns:
Dict[Tuple[int, int], int] - Raises:
IOError,ValueError
- Returns:
-
version(): Get library version- Returns:
str
- Returns:
🧪 Testing
# Run tests
python -m pytest tests/
# Run with coverage
python -m pytest tests/ --cov=blt
# Run benchmarks
python -m pytest tests/ -k benchmark
⚡ Performance
The Python bindings maintain the same high performance as the CLI version:
| File Size | Processing Time | Throughput |
|---|---|---|
| 1MB | ~0.4ms | ~2.5 GB/s |
| 10MB | ~3.8ms | ~2.6 GB/s |
| 100MB | ~38ms | ~2.6 GB/s |
🔍 Examples
See the examples/ directory for comprehensive usage examples:
basic_usage.py- Basic tokenization examplesbpe_example.py- BPE tokenization with custom mergesperformance_test.py- Performance benchmarking
🐛 Troubleshooting
Common Issues
ImportError: No module named 'blt'
# Ensure package is installed
pip install blt-tokenizer
# Or for development
maturin develop
RuntimeError during tokenization
# Check file permissions and paths
# Reduce memory usage if needed
tokenizer = blt.ByteTokenizer(memory_cap=50)
Performance issues
# Increase thread count and chunk size
tokenizer = blt.ByteTokenizer(threads=8, chunk_size="16MB")
📄 License
This project is licensed under the Apache License, Version 2.0. See LICENSE for details.
🤝 Contributing
Contributions are welcome! Please see the main CONTRIBUTING.md for guidelines.
🔗 Links
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file blt_tokenizer-0.2.0-cp313-cp313-macosx_11_0_arm64.whl.
File metadata
- Download URL: blt_tokenizer-0.2.0-cp313-cp313-macosx_11_0_arm64.whl
- Upload date:
- Size: 612.4 kB
- Tags: CPython 3.13, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6e77bdc9ffbdc5985054ae2358ef73eda6cd9aa692e9c7246a9c71f138688ac6
|
|
| MD5 |
46e3544a3756869d5f32273494f99309
|
|
| BLAKE2b-256 |
d6c55331ee5a80e8041eea93c7dac8aeac1ad2dcdd2554ac48a75b2861905026
|
Provenance
The following attestation bundles were made for blt_tokenizer-0.2.0-cp313-cp313-macosx_11_0_arm64.whl:
Publisher:
ci.yml on jtrefon/blt
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
blt_tokenizer-0.2.0-cp313-cp313-macosx_11_0_arm64.whl -
Subject digest:
6e77bdc9ffbdc5985054ae2358ef73eda6cd9aa692e9c7246a9c71f138688ac6 - Sigstore transparency entry: 255906770
- Sigstore integration time:
-
Permalink:
jtrefon/blt@8b9b7d8cc5b053fd0d88632420f4f086a742e59b -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/jtrefon
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@8b9b7d8cc5b053fd0d88632420f4f086a742e59b -
Trigger Event:
push
-
Statement type:
File details
Details for the file blt_tokenizer-0.2.0-cp38-cp38-win_amd64.whl.
File metadata
- Download URL: blt_tokenizer-0.2.0-cp38-cp38-win_amd64.whl
- Upload date:
- Size: 508.2 kB
- Tags: CPython 3.8, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
34437463b176fff2f4a1e936f401083e9e687a03be965c266cbe449e5e2eb200
|
|
| MD5 |
b9dca0a395fed25fbc776a6462cec3fb
|
|
| BLAKE2b-256 |
3c8f60ee63d750f8e5de0dc2f02a5837c260f152f4d094b72ae0e88f4b41a6c9
|
Provenance
The following attestation bundles were made for blt_tokenizer-0.2.0-cp38-cp38-win_amd64.whl:
Publisher:
ci.yml on jtrefon/blt
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
blt_tokenizer-0.2.0-cp38-cp38-win_amd64.whl -
Subject digest:
34437463b176fff2f4a1e936f401083e9e687a03be965c266cbe449e5e2eb200 - Sigstore transparency entry: 255580167
- Sigstore integration time:
-
Permalink:
jtrefon/blt@33e2f60df641fe650dd1cad742eec833dab7da48 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/jtrefon
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@33e2f60df641fe650dd1cad742eec833dab7da48 -
Trigger Event:
push
-
Statement type:
File details
Details for the file blt_tokenizer-0.2.0-cp38-cp38-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: blt_tokenizer-0.2.0-cp38-cp38-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 772.5 kB
- Tags: CPython 3.8, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8c2d49aa19204ebd58fac6c4d5748a16faf81d029e0b00c2ff0adac270ee39c2
|
|
| MD5 |
fc21e6ddebd2f4f06eca7d814eec05b3
|
|
| BLAKE2b-256 |
7610f21979e09372e2de1069a7ed76c6c6666c4f046e0c8dc6be4a28d50f86e7
|
Provenance
The following attestation bundles were made for blt_tokenizer-0.2.0-cp38-cp38-manylinux_2_34_x86_64.whl:
Publisher:
ci.yml on jtrefon/blt
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
blt_tokenizer-0.2.0-cp38-cp38-manylinux_2_34_x86_64.whl -
Subject digest:
8c2d49aa19204ebd58fac6c4d5748a16faf81d029e0b00c2ff0adac270ee39c2 - Sigstore transparency entry: 255580165
- Sigstore integration time:
-
Permalink:
jtrefon/blt@33e2f60df641fe650dd1cad742eec833dab7da48 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/jtrefon
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@33e2f60df641fe650dd1cad742eec833dab7da48 -
Trigger Event:
push
-
Statement type:
File details
Details for the file blt_tokenizer-0.2.0-cp38-cp38-macosx_11_0_arm64.whl.
File metadata
- Download URL: blt_tokenizer-0.2.0-cp38-cp38-macosx_11_0_arm64.whl
- Upload date:
- Size: 614.1 kB
- Tags: CPython 3.8, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2aaaf1dee146546308e52bd87bf2683619d4e8d9c6c0fff4bf192c09bb90feda
|
|
| MD5 |
09db4b7d4d25affc25409d9e819f49d5
|
|
| BLAKE2b-256 |
ba95fad1d9ecf03e2763f553ff722eec1e3901f9d3fe8a675b212563f86d55c5
|
Provenance
The following attestation bundles were made for blt_tokenizer-0.2.0-cp38-cp38-macosx_11_0_arm64.whl:
Publisher:
ci.yml on jtrefon/blt
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
blt_tokenizer-0.2.0-cp38-cp38-macosx_11_0_arm64.whl -
Subject digest:
2aaaf1dee146546308e52bd87bf2683619d4e8d9c6c0fff4bf192c09bb90feda - Sigstore transparency entry: 255580164
- Sigstore integration time:
-
Permalink:
jtrefon/blt@33e2f60df641fe650dd1cad742eec833dab7da48 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/jtrefon
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@33e2f60df641fe650dd1cad742eec833dab7da48 -
Trigger Event:
push
-
Statement type:
File details
Details for the file blt_tokenizer-0.2.0-cp38-cp38-macosx_10_12_x86_64.whl.
File metadata
- Download URL: blt_tokenizer-0.2.0-cp38-cp38-macosx_10_12_x86_64.whl
- Upload date:
- Size: 636.6 kB
- Tags: CPython 3.8, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b46ad56768d910887538dfe95df03fb36ca234049dfb23599eb8e3f179f6b907
|
|
| MD5 |
4969baf2fec331ed6e41dfcca256c233
|
|
| BLAKE2b-256 |
cfd557e9109229db7fff4d701f5474bb9c2e035952eb39326ab7a371fa3d15b0
|
Provenance
The following attestation bundles were made for blt_tokenizer-0.2.0-cp38-cp38-macosx_10_12_x86_64.whl:
Publisher:
ci.yml on jtrefon/blt
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
blt_tokenizer-0.2.0-cp38-cp38-macosx_10_12_x86_64.whl -
Subject digest:
b46ad56768d910887538dfe95df03fb36ca234049dfb23599eb8e3f179f6b907 - Sigstore transparency entry: 255580166
- Sigstore integration time:
-
Permalink:
jtrefon/blt@33e2f60df641fe650dd1cad742eec833dab7da48 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/jtrefon
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@33e2f60df641fe650dd1cad742eec833dab7da48 -
Trigger Event:
push
-
Statement type: