Your indexing pipeline tokenizes 10M docs in 18 hours. What single change gives the biggest speedup?
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
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.
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.
What an interviewer would ask next. Try answering before peeking at the approach.
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.