When we tokenize text, we're making a compression-granularity tradeoff. A small vocabulary (say, 256 tokens for byte-level) treats every character separately. A large vocabulary (100K+ tokens) can represent entire common words or phrases as single tokens.
WHY this matters:
Sequence length directly impacts transformer compute: O(n2d) for self-attention where n is sequence length
Embedding table size is V×d parameters where V is vocabulary size
Token granularity affects how well the model can generalize to rare/unseen words
WHY: Each of the V tokens needs a d-dimensional dense vector representation.
WHAT this means: If V=5000 and d=768 (BERT-base), that's 5000×768=3.84M parameters just for the embedding table. (If instead V=50000, that's 50000×768=38.4M.) This grows linearly with V.
Large vocab wins if:extra embedding params(Vl−Vs)d<attention memory savings(ns2−nl2)d⋅batch count
WHY this formula: The left side is the permanent parameter overhead of the larger vocabulary. The right side is the per-batch activation memory savings from shorter sequences. If you process many batches (training, high-throughput inference), the right side accumulates and can outweigh the left.
What is the vocabulary size tradeoff in tokenization?
Smaller vocabulary → longer sequences (more attention cost) but fewer embedding parameters. Larger vocabulary → shorter sequences (less attention cost) but more embedding parameters. It's a resource allocation decision.
Why does sequence length matter more than vocab size for attention cost?
Attention cost is O(n2d) where n is sequence length. Doubling sequence length quadruples attention cost. Embedding cost is only O(V×d), linear in vocab size V.
What is the embedding table parameter count formula?
Embedding parameters = V×d where V is vocabulary size and d is embedding dimension. For GPT-2 small (V=50257, d=768), that's 38.6M parameters.
Why don't character-level models solve the vocabulary tradeoff?
They eliminate embedding table cost but explode sequence length (8-10x longer). Attention is O(n2), so this increases compute by ~64x. The tradeoff just moves from parameters to compute.
When does a larger vocabulary save resources?
When (Vl−Vs)d<(ns2−nl2)d×batch count. The extra embedding params must be outweighed by accumulated attention memory savings over many batches.
What's a typical vocabulary size for code models vs. general text?
Code models use 50K-100K (many unique identifiers). General text uses 32K-50K. Code needs larger vocab to avoid splitting function/variable names into many tokens.
Why is the bottom 10% of vocabulary by frequency important?
Rare tokens (bottom 10%) should still appear 50-100+ times in training for good embeddings. Otherwise you waste parameters on noisy embeddings that don't generalize. This guides vocab size choice.
Recall Explain to a 12-year-old
Imagine you're texting a friend, but you only have a limited "dictionary" of pre-approved text shortcuts.
Small dictionary (256 shortcuts): You can only send individual letters like "h", "e", "l", "o". To say "hello" you send 5 messages. That's slow!
Medium dictionary (32,000 shortcuts): You have shortcuts for common words. "hello" is one shortcut, "how" is another, "are" is another. Now "hello how are you" is just 4 shortcuts. Much faster!
Huge dictionary (200,000 shortcuts): You have shortcuts for entire phrases like "hello how are you doing today". Super fast! But now your phone needs to store 200,000 shortcuts in memory. That takes up space.
The tradeoff: Small dictionary = send LOTS of messages (slow). Huge dictionary = your phone is full of shortcuts (eats memory). You want the "goldilocks" size: enough shortcuts to be fast, but not so many that your phone runs out of space.
For AI models, "sending messages" is like the computer processing each token. More tokens = more work (especially because the computer has to compare every token with every other token, which gets really slow). But storing shortcuts (the vocabulary) also takes memory. So we pick a size that's "just right" for the task!
Dekho, is note ka core intuition ye hai ki vocabulary size choose karna ek "goldilocks problem" hai — na bahut chhota, na bahut bada, balki bilkul theek hona chahiye. Jab hum text ko tokenize karte hain, toh basically ek tradeoff kar rahe hote hain. Agar vocabulary chhoti rakhein (jaise 256 tokens byte-level ke liye), toh har character alag token banega, matlab sequence lambi ho jayegi — aur transformer ka self-attention cost O(n2d) hai, toh lambi sequence se compute bahut zyada badh jaata hai. Doosri taraf, agar vocabulary badi rakhein (100K+ tokens), toh sequence chhoti ho jayegi, par embedding table ka size V×d hone ki wajah se memory aur parameters bahut zyada kha jaate hain. Yahi fundamental tension hai.
Ab ye matter kyun karta hai? Kyunki resource allocation ki poori decision isi pe depend karti hai. Sochke dekho — V=5000 aur d=768 pe embedding table sirf 3.84M parameters ka hai, par V=50000 pe wahi 38.4M ho jaata hai, yaani seedha linear growth. Waise hi sequence length double karo toh attention cost 4 guna badh jaata hai (kyunki n2 term dominate karta hai), jabki feedforward sirf double hota hai. Interesting baat ye hai ki jab vocabulary badhaate hain, toh average token length sublinear tarike se badhti hai — V ko double karne se ℓˉ sirf 20-30% badhta hai, poore 2x nahi. Isiliye simple calculation se decide nahi hota.
Toh practically iska matlab ye hai ki bada vocabulary tabhi worthwhile hai jab uske extra embedding parameters ka permanent cost, shorter sequences se milne wale attention memory savings se kam ho. Real-world engineering mein — jaise koi model design karte time — yahi breakeven analysis batata hai ki apne use case ke hisaab se kaunsa vocabulary size lena chahiye. Agar tum long documents process kar rahe ho, toh attention cost dominate karega, isliye bada vocabulary faydemand ho sakta hai; par agar zyaadatar chhote texts hain, toh chhota vocabulary better hai. Ye samajhna important hai kyunki ye tumhe rattafication se aage le jaake, actual tradeoffs ke basis pe smart decisions lena sikhata hai.