Your indexing pipeline tokenizes 10M docs in 18 hours. What single change gives the biggest speedup?
Use the Rust-backed fast tokenizer with batch encoding (tokenizer(list_of_texts)) instead of a per-doc Python loop. CPU-parallel internally, FFI overhead amortized. Typical 20-100x speedup.
Imagine you have a million letters to stamp. The slow way is to walk one letter to the stamping desk, stamp it, walk back, pick up the next one. The fast way is to dump all million letters on the desk and use a machine that stamps them in parallel. The slow Python tokenizer takes a one at a time approach. The Rust fast tokenizer is the parallel-stamping machine. Batch encoding is dumping all the letters at once instead of one at a time. Same model, same output, dramatically less wasted time at the door of the stamping room. Moving to a GPU is wrong because the stamping machine is a CPU machine. Caching the stamped letters helps next time but does not fix the slow loop now. Setting a higher length cap changes how you cut the letters, not how fast you stamp them.
Detailed answer & concept explanation~7 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
Open with the framing that tokenization is CPU-bound Python-call-overhead, not a model or algorithm issue. Name the two fixes that compose: use_fast=True for the Rust-backed tokenizer (internal CPU parallelism), batch encoding to amortize FFI overhead. Quote the typical 20-100x speedup. Dismantle the wrong options: GPU does not help on regex+BPE-lookup work, max_length affects cut not throughput, disk caching is a secondary win. Close with multi-machine fan-out as the next lever for truly large corpora.
from transformers import AutoTokenizer
# Slow: per-document loop with slow tokenizer
tok_slow = AutoTokenizer.from_pretrained('meta-llama/Meta-Llama-3-8B', use_fast=False)
results_slow = [tok_slow.encode(doc) for doc in docs] # 18 hours on 10M docs
# Fast: Rust tokenizer + batch encoding
tok_fast = AutoTokenizer.from_pretrained('meta-llama/Meta-Llama-3-8B', use_fast=True)
results_fast = tok_fast(
docs, # full list, not loop
padding=False,
truncation=True,
max_length=2048,
return_tensors=None,
)['input_ids']
# 15-60 minutes on 10M docs, 20-100x speedup
# Even better for very large corpora: batch in chunks of 1K-10K
def batched(seq, n):
for i in range(0, len(seq), n):
yield seq[i:i+n]
results = []
for batch in batched(docs, 5000):
results.extend(tok_fast(batch, padding=False, truncation=True)['input_ids'])Real products, models, and research that use this idea.
- HuggingFace tokenizers library ships Rust-backed fast tokenizers for Llama 3+, Mistral, Qwen 3.5, and most modern open-weight models; use_fast=True is the default in transformers 4.40+.
- tiktoken (OpenAI's Rust-backed BPE library) achieves similar throughput characteristics for cl100k_base and o200k_base workloads.
- Ray Data and Dask DataFrames provide multi-machine fan-out patterns where each worker runs a fast tokenizer on a document partition.
- LlamaIndex and Haystack indexing pipelines for RAG over 10M+ document corpora explicitly recommend the fast-tokenizer + batch pattern as the baseline configuration.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhen does the fast tokenizer NOT provide a 20-100x speedup?
QYou batch in chunks of 5,000 and the process hits OOM. What is going on and how do you fix it?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Assuming tokenization performance is a GPU problem or an algorithm problem, when the real issue is Python call overhead on a slow per-document loop.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.