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

BLT provides true tokenization by default - each input byte is converted to a 16-bit token, ensuring all digital content is properly tokenized for AI/LLM workflows.

Basic File Tokenization

import blt

# Create a tokenizer (uses basic byte-to-u16 tokenization by default)
tokenizer = blt.ByteTokenizer()

# Tokenize a file (each byte becomes a 16-bit token)
tokenizer.tokenize_file("input.txt", "output.bin")

Advanced BPE Tokenization

import blt

# Load BPE merges from file
merges = blt.load_bpe_merges("merges.txt")

# Or define merges manually for compression
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 for better compression
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.2-cp313-cp313-macosx_11_0_arm64.whl (614.6 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

blt_tokenizer-0.2.2-cp38-cp38-win_amd64.whl (510.0 kB view details)

Uploaded CPython 3.8Windows x86-64

blt_tokenizer-0.2.2-cp38-cp38-manylinux_2_34_x86_64.whl (775.5 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.34+ x86-64

blt_tokenizer-0.2.2-cp38-cp38-macosx_11_0_arm64.whl (616.1 kB view details)

Uploaded CPython 3.8macOS 11.0+ ARM64

blt_tokenizer-0.2.2-cp38-cp38-macosx_10_12_x86_64.whl (639.4 kB view details)

Uploaded CPython 3.8macOS 10.12+ x86-64

File details

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

File metadata

File hashes

Hashes for blt_tokenizer-0.2.2-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 76583a8a19c5c70865fa1d64470cf729110dbd551921fff31de1e80f47e4270f
MD5 5810472259742205463f600d8a8441f3
BLAKE2b-256 2ab688c728b4e9f799004d70479f86203d9bacb63b803827e8ea0faf9fcd94f7

See more details on using hashes here.

Provenance

The following attestation bundles were made for blt_tokenizer-0.2.2-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.2-cp38-cp38-win_amd64.whl.

File metadata

File hashes

Hashes for blt_tokenizer-0.2.2-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 77709e46f1965884835bb41cfbb41113809d9f8936ad8e47a3c8941b15ac2712
MD5 0c9489bfdd9d17d8333e24d750e53572
BLAKE2b-256 35ec39d380c0d8ed5528ffcedf3bf04fcd436c712f5e21d9cd5bddaa202686bb

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for blt_tokenizer-0.2.2-cp38-cp38-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 46200b62c644e19acc1de80beb947f91a0f8da7eda733adc82bb9722f73a2cee
MD5 02a737db741b539e47fbd752ce6fce08
BLAKE2b-256 4d8953e4332b3cd072de3d051e54bc00144303e18123da329072dd3f5095d02f

See more details on using hashes here.

Provenance

The following attestation bundles were made for blt_tokenizer-0.2.2-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.2-cp38-cp38-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for blt_tokenizer-0.2.2-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c0a4e103599df48eab73fd20a53ac86f7401d3a2cbe93cf6757edb5cec0bf172
MD5 079a7fca143ce0d09cce355cf44582f3
BLAKE2b-256 93f4d30c52731e78402470b0b0afb217dcd2ae58cbf7aedfce237d677de8950c

See more details on using hashes here.

Provenance

The following attestation bundles were made for blt_tokenizer-0.2.2-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.2-cp38-cp38-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for blt_tokenizer-0.2.2-cp38-cp38-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 ea650ec31df470474d755e0d0e2d38417fd35333c99c16b8b3f2ba56f4732e41
MD5 bde66ca259552bcf708afc70c146182d
BLAKE2b-256 b111d23ff86ab173f19b0a492d8db518b1c8f36460e1f6cf23f269b3874ef69e

See more details on using hashes here.

Provenance

The following attestation bundles were made for blt_tokenizer-0.2.2-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