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.0-cp313-cp313-macosx_11_0_arm64.whl (612.4 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.8Windows x86-64

blt_tokenizer-0.2.0-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.0-cp38-cp38-macosx_11_0_arm64.whl (614.1 kB view details)

Uploaded CPython 3.8macOS 11.0+ ARM64

blt_tokenizer-0.2.0-cp38-cp38-macosx_10_12_x86_64.whl (636.6 kB view details)

Uploaded CPython 3.8macOS 10.12+ x86-64

File details

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

File metadata

File hashes

Hashes for blt_tokenizer-0.2.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6e77bdc9ffbdc5985054ae2358ef73eda6cd9aa692e9c7246a9c71f138688ac6
MD5 46e3544a3756869d5f32273494f99309
BLAKE2b-256 d6c55331ee5a80e8041eea93c7dac8aeac1ad2dcdd2554ac48a75b2861905026

See more details on using hashes here.

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

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.0-cp38-cp38-win_amd64.whl.

File metadata

File hashes

Hashes for blt_tokenizer-0.2.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 34437463b176fff2f4a1e936f401083e9e687a03be965c266cbe449e5e2eb200
MD5 b9dca0a395fed25fbc776a6462cec3fb
BLAKE2b-256 3c8f60ee63d750f8e5de0dc2f02a5837c260f152f4d094b72ae0e88f4b41a6c9

See more details on using hashes here.

Provenance

The following attestation bundles were made for blt_tokenizer-0.2.0-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.0-cp38-cp38-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for blt_tokenizer-0.2.0-cp38-cp38-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 8c2d49aa19204ebd58fac6c4d5748a16faf81d029e0b00c2ff0adac270ee39c2
MD5 fc21e6ddebd2f4f06eca7d814eec05b3
BLAKE2b-256 7610f21979e09372e2de1069a7ed76c6c6666c4f046e0c8dc6be4a28d50f86e7

See more details on using hashes here.

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

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.0-cp38-cp38-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for blt_tokenizer-0.2.0-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2aaaf1dee146546308e52bd87bf2683619d4e8d9c6c0fff4bf192c09bb90feda
MD5 09db4b7d4d25affc25409d9e819f49d5
BLAKE2b-256 ba95fad1d9ecf03e2763f553ff722eec1e3901f9d3fe8a675b212563f86d55c5

See more details on using hashes here.

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

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.0-cp38-cp38-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for blt_tokenizer-0.2.0-cp38-cp38-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 b46ad56768d910887538dfe95df03fb36ca234049dfb23599eb8e3f179f6b907
MD5 4969baf2fec331ed6e41dfcca256c233
BLAKE2b-256 cfd557e9109229db7fff4d701f5474bb9c2e035952eb39326ab7a371fa3d15b0

See more details on using hashes here.

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

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