BPE Tokenizer
Byte-level BPE tokenizer. Rust engine exposed to Python through PyO3. 262,144-token vocabulary trained on a 16 GB curated corpus. A drop-in for tiktoken's cl100k_base and o200k_base and for Hugging Face tokenizer.json files. 185 Rust tests, 140 commits.
- Reading
- 100%byte-identical with tiktokenverified across 28 languages
- Limitation
- Loses on Arabic and Hindi. Encode trails current tiktoken on single long strings and large batches; the lead is decode and short batches.
Problem
Train a byte-level BPE vocabulary of 262,144 tokens and prove the engine against a reference: tiktoken, on cl100k_base and o200k_base.
Approach
A Rust engine exposed to Python through PyO3, with 185 Rust tests across 140 commits. Batches run in parallel with the GIL released, each thread keeps its own cache of merged pieces, and decoding reads from one flat byte array. The vocabulary was trained by a ten-stage pipeline on a 12 vCPU, 96 GB cloud machine; 16 GB of aggressively sub-sampled data beat 40 to 45 GB at the same vocabulary size.
Result
100% byte-identical with tiktoken across 28 languages. About 5.7 million tokens per second in bulk: 1 GB of prose in about 28 s on 16 cores. Decode runs about 10x faster than Hugging Face tokenizers. Profiling found nested thread oversubscription; the fix gave +25% on long batches and +14% on short ones.
Limitation
Loses on Arabic and Hindi. Encode trails current tiktoken on single long strings and large batches; the lead is decode and short batches.
Finding
16 GB of aggressively sub-sampled data beat 40 to 45 GB at the same vocabulary size.
| Language | Bytes per token against o200k_base |
|---|---|
| Python | 1.06x |
| JS/TS | 1.06x |
| Java | 1.07x |
| C++ | 1.08x |
| C# | 1.10x |
| Go | 1.08x |
| Rust | 1.07x |
| Kotlin | 1.11x |
| Haskell | 1.14x |
| shell | 1.11x |
| SQL | 1.04x |
| English | 1.04x |
| maths | 1.08x |
| Japanese | 1.24x |
| Chinese | 1.10x |
| Arabic | 0.93x |
| Hindi | 0.63x |
Specification
- Vocabulary
- 262,144 tokens
- Training corpus
- 16 GB, curated
- Training run
- Ten idempotent stages on a GCP N2 machine (12 vCPU, 96 GB RAM); serial BPE training took about 118 min
- Engine
- Rust, exposed to Python through PyO3
- Tests
- 185 Rust tests, 140 commits
- Parity
- 100% byte-identical with tiktoken on cl100k_base and o200k_base, 28 languages
- Speed
- About 5.7 million tokens per second in bulk (1 GB of prose in about 28 s on 16 cores)
- Decode
- About 10x faster than Hugging Face tokenizers
- Profiling
- Fixing nested Rayon oversubscription: +25% encode on long batches, +14% on short batches, parity unchanged