All work

BPE Tokenizer

Apr to Jun 2026, complete

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.

Parity on cl100k_base and o200k_base.

  1. 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.

  2. 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.

  3. 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.

  4. Limitation

    Loses on Arabic and Hindi. Encode trails current tiktoken on single long strings and large batches; the lead is decode and short batches.

  5. Finding

    16 GB of aggressively sub-sampled data beat 40 to 45 GB at the same vocabulary size.

Bytes per token against o200k_base. Higher is better.
LanguageBytes per token against o200k_base
Python1.06x
JS/TS1.06x
Java1.07x
C++1.08x
C#1.10x
Go1.08x
Rust1.07x
Kotlin1.11x
Haskell1.14x
shell1.11x
SQL1.04x
English1.04x
maths1.08x
Japanese1.24x
Chinese1.10x
Arabic0.93x
Hindi0.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

Yash Gurjar

AI & ML Engineer. I build LLM systems and measure them.

Available for a full-time internship, January to June 2027.

  • 3merged PRs in PyTorch, including Dynamo (the torch.compile frontend)
  • 0.94recall@5 on a hand-labelled 48-query gold set
  • 60+users on CodeNeuron, a live ML practice platform