
tokie
10-20x faster than HuggingFace, 100% accurate drop-in replacement
Install • Quick Start • Examples • Benchmarks • Why tokie?
[!CAUTION] tokie is in its alpha stage and might produce mis-aligned output. Please report any issues you encounter.
tokie is a Rust tokenizer library (with Python bindings) that can load any tokenizer on HuggingFace and tokenize 10-20x faster. It supports every major algorithm — BPE, WordPiece, SentencePiece, and Unigram — and is 100% token-accurate, every time.

Install
Python
Rust
[]
= { = "0.0.7", = ["hf"] }
Quick Start
Python
# Load any HuggingFace tokenizer
=
# Encode — returns Encoding with ids, attention_mask, type_ids, tokens
= # or tokenizer.encode("Hello, world!")
# [101, 7592, 1010, 2088, 999, 102]
# ['[CLS]', 'hello', ',', 'world', '!', '[SEP]']
# [1, 1, 1, 1, 1, 1]
# Decode
= # "hello , world !"
# Count tokens without allocating
= # 6
# Batch encode (parallel across all cores)
=
Rust
use Tokenizer;
let tokenizer = from_pretrained?;
let encoding = tokenizer.encode;
println!; // [101, 7592, 1010, 2088, 999, 102]
println!; // [1, 1, 1, 1, 1, 1]
let text = tokenizer.decode.unwrap;
Examples
Padding & Truncation
For ML inference, you need fixed-length inputs. tokie supports padding and truncation just like HuggingFace:
=
# Truncate to max length
# Pad to fixed length (or use BatchLongest for dynamic padding)
# All outputs are now exactly 128 tokens
=
assert
# attention_mask shows which tokens are real (1) vs padding (0)
# [1, 1, 1, 1, 0, 0, 0, ...]
Cross-Encoder Pair Encoding
For rerankers and cross-encoders that need sentence pairs with token type IDs:
= # or tokenizer.encode_pair(...)
# [101, 2129, 2024, 2017, 1029, 102, 1045, 2572, 2986, 1012, 102]
# [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
# [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
# [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]
Byte Offsets
Track where each token maps back to in the original text:
=
Vocabulary Access
# 30522
# "[CLS]"
# 102
= # {"[CLS]": 101, "[SEP]": 102, ...}
Save and Load .tkz Files
tokie's binary format is ~10x smaller than tokenizer.json and loads in ~5ms:
=
from_pretrained() automatically tries .tkz first, falling back to tokenizer.json.
Benchmarks
All benchmarks run on 1 MB of enwik8 on an Apple M3 Pro. tokie produces identical output to HuggingFace tokenizers — every token matches, every time.
BPE Encoding (GPT-2, Llama, Qwen, ModernBERT)
For tiktoken-style BPE models, tokie uses a backtracking encoder built on an Aho-Corasick automaton. Instead of iteratively merging byte pairs, it does a greedy longest-match in O(n) time, with backtracking only when adjacent tokens form invalid pairs. Combined with parallel chunking across all cores and hand-coded pretokenizers from pretokie, this gives 10-17x faster than HuggingFace and 1.4-3.1x faster than kitoken.

WordPiece (BERT, MiniLM, BGE, GTE)
WordPiece tokenizers use a different algorithm — greedy longest-match prefix search over a vocabulary trie. tokie uses a pre-built Double-Array trie for O(n) lookup with excellent cache locality, combined with a specialized BERT pretokenizer. The result is 15-20x faster than HuggingFace and 2.9-3.6x faster than kitoken on BERT, with identical output.

SentencePiece BPE & Unigram (Gemma, XLM-R, T5)
SentencePiece-style models use a different merge algorithm with non-topological rank orders. tokie uses a radix heap with O(1) amortized operations that exploits BPE's monotonic rank property. tokie is 1.3x faster than HuggingFace on Gemma 3. Note: kitoken appears faster in raw throughput but produces incorrect output on SentencePiece models (13% more tokens, mismatches starting at token 93 on Gemma 3).

Python Benchmarks
All results on Apple M3 Pro, single-string encode, median of 10 runs.
tokie vs HuggingFace tokenizers vs kitoken
| Model | Text Size | tokie | HF tokenizers | kitoken | vs HF | vs kitoken |
|---|---|---|---|---|---|---|
| BERT | 45 KB | 0.76 ms | 11.2 ms | 2.19 ms | 15x | 2.9x |
| BERT | 900 KB | 14.3 ms | 279 ms | 51.8 ms | 20x | 3.6x |
| GPT-2 | 45 KB | 0.91 ms | 8.34 ms | 1.27 ms | 9x | 1.4x |
| GPT-2 | 900 KB | 15.8 ms | 246 ms | 29.8 ms | 16x | 1.9x |
| Llama 3 | 45 KB | 0.84 ms | 8.60 ms | 1.58 ms | 10x | 1.9x |
| Llama 3 | 900 KB | 15.0 ms | 222 ms | 35.1 ms | 15x | 2.3x |
| Qwen 3 | 45 KB | 0.84 ms | 9.80 ms | 2.22 ms | 12x | 2.6x |
| Qwen 3 | 900 KB | 15.8 ms | 270 ms | 49.3 ms | 17x | 3.1x |
| ModernBERT | 45 KB | 1.11 ms | 10.0 ms | 1.76 ms | 9x | 1.6x |
| ModernBERT | 900 KB | 26.7 ms | 276 ms | 50.7 ms | 10x | 1.9x |
| Gemma 3 | 45 KB | 9.29 ms | 10.6 ms | 5.01 ms* | 1.1x | — |
| Gemma 3 | 900 KB | 160 ms | 214 ms | 93.7 ms* | 1.3x | — |
tokie vs tiktoken (OpenAI models)
| Model | Text Size | tokie | tiktoken | Speedup |
|---|---|---|---|---|
| cl100k (GPT-4) | 45 KB | 0.82 ms | 2.87 ms | 3.5x |
| cl100k (GPT-4) | 900 KB | 16.4 ms | 61.3 ms | 3.7x |
| o200k (GPT-4o) | 45 KB | 0.88 ms | 4.68 ms | 5.3x |
| o200k (GPT-4o) | 900 KB | 17.5 ms | 97.4 ms | 5.6x |
100% token-accurate across all models. Batch encoding is 4-6x faster than HF and 2-3x faster than kitoken.
* kitoken produces incorrect output on Gemma 3 SentencePiece (13% more tokens, diverges at token 93 — reported). Speedup comparison not meaningful for incorrect output.
Tokenizer Loading
Loading a tokenizer from tokenizer.json requires JSON parsing, vocabulary construction, and — for BPE models — building the Aho-Corasick automaton from scratch. tiktoken similarly has to parse its BPE data and compile regex patterns on every load. tokie's .tkz binary format stores all of this pre-built: the Double-Array Aho-Corasick (DAAC) automaton state, the normalized vocabulary, and the encoder configuration are serialized directly. Loading becomes a near-zero-cost deserialization — no parsing, no construction — achieving 4x–9x faster cold load times than HuggingFace and 2x–3x faster than tiktoken.

Verified Tokenizers
Every tokenizer below is tested against the original HuggingFace tokenizer on 1MB of enwik8 (~300K tokens) in CI. Pass = every token matches.
| Model | Type | Status |
|---|---|---|
| GPT-2 | BPE | ✅ Pass |
| cl100k | BPE | ✅ Pass (vs tiktoken-rs) |
| o200k | BPE | ✅ Pass (vs tiktoken-rs) |
| RoBERTa | BPE | ✅ Pass |
| Phi-2 | BPE | ✅ Pass |
| Phi-3 Mini | BPE | ✅ Pass |
| ModernBERT | BPE | ✅ Pass |
| CodeLlama 7B | BPE | ✅ Pass |
| DeepSeek-V3 | BPE | ✅ Pass |
| DeepSeek-R1 | BPE | ✅ Pass |
| Gemma 2 2B | SentencePiece BPE | ✅ Pass |
| Gemma 3 4B | SentencePiece BPE | ✅ Pass |
| Llama 3.2 1B | BPE | ✅ Pass |
| Llama 4 Scout | BPE | ✅ Pass |
| Mistral 7B | BPE | ✅ Pass |
| Mistral Nemo | BPE | ✅ Pass |
| Mixtral 8x7B | BPE | ✅ Pass |
| NV-Embed-v2 | SentencePiece BPE | ✅ Pass |
| Qwen2 7B | BPE | ✅ Pass |
| Qwen3 Embed 0.6B | BPE | ✅ Pass |
| Qwen3 Embed 4B | BPE | ✅ Pass |
| Qwen3 Embed 8B | BPE | ✅ Pass |
| Qwen3 0.6B | BPE | ✅ Pass |
| Qwen3 8B | BPE | ✅ Pass |
| Qwen3 Coder 30B | BPE | ✅ Pass |
| Qwen3.5 0.8B | BPE | ✅ Pass |
| Qwen3.5 4B | BPE | ✅ Pass |
| SmolLM2 135M | BPE | ✅ Pass |
| StableLM 2 1.6B | BPE | ✅ Pass |
| Nomic Embed v1 | WordPiece | ✅ Pass |
| BERT base | WordPiece | ✅ Pass |
| all-MiniLM-L6-v2 | WordPiece | ✅ Pass |
| all-MiniLM-L12-v2 | WordPiece | ✅ Pass |
| all-mpnet-base-v2 | WordPiece | ✅ Pass |
| BGE base en v1.5 | WordPiece | ✅ Pass |
| BGE large en v1.5 | WordPiece | ✅ Pass |
| BGE small en v1.5 | WordPiece | ✅ Pass |
| BGE en ICL | BPE | ✅ Pass |
| BGE M3 | SentencePiece BPE | ✅ Pass |
| E5 base v2 | WordPiece | ✅ Pass |
| E5 large v2 | WordPiece | ✅ Pass |
| E5 small v2 | WordPiece | ✅ Pass |
| GTE base | WordPiece | ✅ Pass |
| GTE large | WordPiece | ✅ Pass |
| GTE small | WordPiece | ✅ Pass |
| GTE Qwen2 7B | BPE | ✅ Pass |
| MS MARCO MiniLM L-4 | WordPiece | ✅ Pass |
| MS MARCO MiniLM L-6 | WordPiece | ✅ Pass |
| mxbai embed large v1 | WordPiece | ✅ Pass |
| mxbai embed 2d large v1 | WordPiece | ✅ Pass |
| mxbai embed xsmall v1 | WordPiece | ✅ Pass |
| deepset mxbai embed de large | Unigram | ✅ Pass |
| Jina v2 base en | BPE | ✅ Pass |
| Jina v2 base code | BPE | ✅ Pass |
| Jina v3 | Unigram | ✅ Pass |
| Jina v4 | BPE | ✅ Pass |
| Cohere embed english v3 | BPE | ✅ Pass |
| Cohere embed english light v3 | BPE | ✅ Pass |
| Cohere embed multilingual v3 | Unigram | ✅ Pass |
| Cohere embed multilingual light v3 | Unigram | ✅ Pass |
| Voyage 3 | BPE | ✅ Pass |
| Voyage 3 large | BPE | ✅ Pass |
| Voyage 3 lite | BPE | ✅ Pass |
| Voyage 3.5 | BPE | ✅ Pass |
| Voyage 3.5 lite | BPE | ✅ Pass |
| Voyage Code 2 | BPE | ✅ Pass |
| Voyage Code 3 | BPE | ✅ Pass |
| Voyage Finance 2 | BPE | ✅ Pass |
| Voyage Law 2 | BPE | ✅ Pass |
| Voyage Multilingual 2 | BPE | ✅ Pass |
| Voyage Multimodal 3 | BPE | ✅ Pass |
| Snowflake Arctic Embed v2 | SentencePiece BPE | ✅ Pass |
| T5 base | Unigram | ✅ Pass |
| XLM-RoBERTa | SentencePiece BPE | ✅ Pass |
Summary: 75 pass, 0 fail out of 75 tested. Every tokenizer produces identical output to HuggingFace.
Why tokie?
When I started building Chonkie, the biggest bottleneck wasn't chunking — it was tokenization. We were spending more time counting tokens than actually chunking text.
tokie uses hand-written parsers for each pretokenization pattern — GPT-2, cl100k, o200k, BERT — that understand the exact character classes needed without the overhead of a general-purpose regex engine. That alone gets you a 10–20x speedup on pretokenization.
The second problem was that no single library could load everything. I actually tried to solve this before with AutoTikTokenizer, believing tiktoken's BPE engine could handle all of HuggingFace. I was wrong — you need fundamentally different algorithms for each encoder type: backtracking BPE for tiktoken-style models, heap-based BPE for models with non-topological merge orders, radix-heap BPE for SentencePiece, plus WordPiece and Unigram each with their own tricks.
The third insight was parallelism. Tokenization is embarrassingly parallel if you split text at the right boundaries. We use chunk to SIMD-split text into chunks that respect token boundaries, then encode each chunk on a separate core and concatenate. This gives near-linear scaling — about 5x on 8 cores.
Finally, we built the .tkz format to eliminate load-time overhead. A tokenizer.json file has to be parsed, validated, and used to reconstruct all the internal data structures (including the Aho-Corasick automaton, which is expensive to build for large vocabularies). The .tkz format stores the pre-built DAAC automaton, vocabulary, and configuration as a flat binary — loading is just deserialization, no construction required. This cuts load times from 150ms to 15ms for large models like O200K.
The result is tokie — one tokenizer to rule them all.
Acknowledgements
tokie builds on ideas from HuggingFace tokenizers, tiktoken, GitHub's rust-gems (backtracking BPE via Aho-Corasick), and chunk (SIMD text splitting).
Citation
If you use tokie in your research, please cite it as follows: