Skip to main content

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 results
    • input_path (str): Path to input file
    • output_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
  • version(): Get library version

    • Returns: str

🧪 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 examples
  • bpe_example.py - BPE tokenization with custom merges
  • performance_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


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.

blt_tokenizer-0.2.1-cp313-cp313-macosx_11_0_arm64.whl (612.5 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

blt_tokenizer-0.2.1-cp38-cp38-win_amd64.whl (508.2 kB view details)

Uploaded CPython 3.8Windows x86-64

blt_tokenizer-0.2.1-cp38-cp38-manylinux_2_34_x86_64.whl (772.5 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.34+ x86-64

blt_tokenizer-0.2.1-cp38-cp38-macosx_11_0_arm64.whl (614.2 kB view details)

Uploaded CPython 3.8macOS 11.0+ ARM64

blt_tokenizer-0.2.1-cp38-cp38-macosx_10_12_x86_64.whl (637.0 kB view details)

Uploaded CPython 3.8macOS 10.12+ x86-64

File details

Details for the file blt_tokenizer-0.2.1-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for blt_tokenizer-0.2.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2dc0261521296c5fe66d54e6ea6eae54e6b20f9612f6bf0df87382bc86913ce1
MD5 72749d07918bd9b1387f63f920b1da75
BLAKE2b-256 9d5647f5dd704df149746400da2b72a1be856d6f9fc6402c15cb28362005f432

See more details on using hashes here.

Provenance

The following attestation bundles were made for blt_tokenizer-0.2.1-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: ci.yml on jtrefon/blt

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

File details

Details for the file blt_tokenizer-0.2.1-cp38-cp38-win_amd64.whl.

File metadata

File hashes

Hashes for blt_tokenizer-0.2.1-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 5feae24a72306900ee9c66ea27fc62335d154cda8f16b33b26806b4df0a84106
MD5 4462c7272387701a85d4da242ff79042
BLAKE2b-256 58fbc1db3fc65b559398ce968d0346392b2241afead9cb9b766f8735d26f6bfa

See more details on using hashes here.

Provenance

The following attestation bundles were made for blt_tokenizer-0.2.1-cp38-cp38-win_amd64.whl:

Publisher: ci.yml on jtrefon/blt

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

File details

Details for the file blt_tokenizer-0.2.1-cp38-cp38-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for blt_tokenizer-0.2.1-cp38-cp38-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 367482da375b2ef793218450aafef0355d647aefcadfb9d09c3e51aa9b87c3d9
MD5 38936dc2d9cd939bc111dad8dab4b8ad
BLAKE2b-256 174d2f2e41cf0d0bfc5732a08ba2d5d4cc6f6ad4c099daa6c3b5922bbe5d2c79

See more details on using hashes here.

Provenance

The following attestation bundles were made for blt_tokenizer-0.2.1-cp38-cp38-manylinux_2_34_x86_64.whl:

Publisher: ci.yml on jtrefon/blt

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

File details

Details for the file blt_tokenizer-0.2.1-cp38-cp38-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for blt_tokenizer-0.2.1-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b5ba09c225e3dfa5036df6ad794d42da8bfef4f36935e37540541d0a87fe1c28
MD5 0fad9dae4b94748972577e924d2e54a4
BLAKE2b-256 bd7a868c315810e474031e8d25f59e9cc788730b296d055669586f4bffaed679

See more details on using hashes here.

Provenance

The following attestation bundles were made for blt_tokenizer-0.2.1-cp38-cp38-macosx_11_0_arm64.whl:

Publisher: ci.yml on jtrefon/blt

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

File details

Details for the file blt_tokenizer-0.2.1-cp38-cp38-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for blt_tokenizer-0.2.1-cp38-cp38-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 9bd4014ef0a6b7a8b7d100cd19f0a5133824865d14e77f7d75dfb1741138317b
MD5 a761e1f571f1db3445144b9cca032f09
BLAKE2b-256 6c453b896393edcc741e81aaa7bc8af231fdcc65712ee3da186e11a124953395

See more details on using hashes here.

Provenance

The following attestation bundles were made for blt_tokenizer-0.2.1-cp38-cp38-macosx_10_12_x86_64.whl:

Publisher: ci.yml on jtrefon/blt

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