Byte-Pair Encoding (BPE)
4.2.2· AI-ML › Tokenization & Language Modeling
BPE Kaunsi Problem Solve Karta Hai?
Teen tokenization approaches aur unki problems:
- Word-level: Vocabulary explode ho jaati hai (millions of words), typos/"unknownwords" handle nahi ho paate →
<UNK>tokens har jagah - Character-level: Chhoti vocab (26-100 chars), lekin sequences BAHUT lambi ho jaati hain, model ko spelling scratch se seekhni padti hai
- BPE (subword-level): Fixed vocab size (10k-50k), rare words ko decomposition se handle karta hai, morphology capture karta hai
The 80/20: BPE aapko word-level efficiency ka 80% deta hai word-level vocabulary size ke sirf 20% mein, plus zero <UNK> tokens.

BPE Algorithm: First Principles Se
Yeh Design Kyun?
- Greedy frequency-based merging: Compression ke liye optimize karta hai (common patterns ek baar store karo)
- Bottom-up construction: Characters se shuru hota hai (koi bhi input handle karta hai) aur upar build karta hai (frequent patterns capture karta hai)
- Deterministic: Same corpus + same → same vocabulary (reproducible)
BPE Training: Worked Example
Chaliye ek tiny corpus par BPE train karte hain jiska target vocab size = 11 hai. Counting easy karne ke liye, maano corpus mein yeh word frequencies hain: low (×5), lower (×2), newest (×6), widest (×3).
Corpus (counts ke saath): 5× "low" 2× "lower" 6× "newest" 3× "widest"
Step 0: Characters Se Initialize Karo
Har word ko characters mein split karo aur word boundary </w> lagao:
5×: l o w </w>
2×: l o w e r </w>
6×: n e w e s t </w>
3×: w i d e s t </w>
Base vocabulary → size = 11
Pairs ki frequencies (pair occurrences ko word count se multiply karo):
(l, o):low(×5) +lower(×2) mein hai = 7(o, w):low(×5) +lower(×2) = 7(w, </w>):low(×5) = 5(w, e):lower(×2) +newest(×6) = 8 ← highest(e, s):newest(×6) +widest(×3) = 9 ← actually highest(s, t):newest(×6) +widest(×3) = 9(e, r):lower(×2) = 2
Step 1: Most Frequent Pair Merge Karo
Top pairs 9 par tie karte hain: (e, s) aur (s, t). Alphabetically tie break karo → (e, s) merge karo.
Naya token: es
Updated corpus:
5×: l o w </w>
2×: l o w e r </w>
6×: n e w es t </w>
3×: w i d es t </w>
→ size = 12
Yeh step kyun? es newest aur widest dono mein share hota hai (9 baar) → is round mein maximum compression.
Step 2: Agla Merge
Pairs recount karo:
(es, t):newest(×6) +widest(×3) = 9 ← most frequent(w, e):lower(×2) +newest(×6) = 8(l, o): 7,(o, w): 7
(es, t) merge karo → naya token est
Updated corpus:
5×: l o w </w>
2×: l o w e r </w>
6×: n e w est </w>
3×: w i d est </w>
→ size = 13
Target Vocab Size Tak Continue Karo...
Algorithm continue karega, aage (w, e) (=8) merge karega, phir (l, o) (=7), phir (lo, w), eventually low, est</w>, newest</w> jaisi subwords form hongi.
Final learned merges ("BPE merge rules", order mein):
1. e s → es
2. es t → est
3. w e → we
4. l o → lo
5. lo w → low
...
Encoding Example: "lowest"
Merge rules learned (simplified, order mein):
e s → eses t → estl o → lolo w → low
Step-by-step (rules priority ke anusaar apply karo, lowest index pehle):
Input: l o w e s t </w>
Apply rule 1 (e s→es): l o w es t </w>
Apply rule 2 (es t→est): l o w est </w>
Apply rule 3 (l o→lo): lo w est </w>
Apply rule 4 (lo w→low): low est </w>
Output tokens: [low, est, </w>]
Yeh encoding kyun? "low" aur "est" frequent subwords hain. Model "est" ko "lowest", "newest", "widest" mein dekhega → comparative/superlative morphology seekhega.
BPE Kaam Kyun Karta Hai: Math
Yeh formula kyun? Har merge jahan bhi pair hota hai wahan 2 adjacent symbols ko 1 se replace karta hai, aur average token length badhti hai. Isliye sequence length roughly linearly ke saath shrink hoti hai: . Khaas baat, merges ki sankhya ke saath badhti hai lekin diminishing returns ke saath — pehle merges (bahut frequent pairs) sequences bahut chhoti kar dete hain, baad ke merges (rare pairs) mushkil se kuch karte hain. Yeh empirical hai, mein koi clean logarithmic law nahi hai.
Diminishing returns: 10k → 50k vocab jaane par thoda hi badhta hai, isliye extra sequence-length reduction 1k → 10k ke mukable mein bahut kam hai.
Recall BPE Ko Ek 12-Saal Ke Bachche Ko Explain Karo
Socho tum apne friend group ke messages ke liye ek secret code bana rahe ho. Tum kar sakte ho:
-
Poore words use karo: "pizza" = ek code. Lekin tumhe har possible word ke liye MILLIONS of codes chahiye honge, aur jab koi naya slang word invent hoga, tum stuck ho jaoge!
-
Letters use karo: "p" "i" "z" "z" "a" = 5 codes. Total sirf 26 codes (letters), lekin ab har message super long ho jaata hai!
-
BPE approach: Letters se shuru karo. Notice karo ki tum "th" BAHUT BAAR saath type karte ho? "th" ka apna code banao. "the" notice karo? Use ek code banao. Jinhe tum sabse zyada use karte ho unhe combine karte raho jab tak exactly 10,000 codes na ho jaayein. Ab "the" 1 code hai, "pizza" shayad "pizz" + "a" (2 codes) ho sakta hai, aur jab tumhara friend "supercalifragilistic" text karta hai, tum ise un parts mein tod dete ho jo tumne pehle dekhe hain: "super" + "cal" + "i" + "frag" + "il" + "istic".
Jadu: Tumhare 10,000 codes pieces mix aur match karke INFINITE words handle kar sakte hain. Common words = kam pieces (fast). Rare words = zyada pieces (phir bhi kaam karta hai).
Modern BPE Variants
WordPiece (BERT)
- BPE jaisa hi hai lekin har merge ko training corpus likelihood maximize karne ke liye choose karta hai ek unigram language model ke under
- Concretely, yeh woh pair choose karta hai jiska merge log-likelihood mein sabse bada increase deta hai, . Unigram model ke liye yeh closely ratio (pointwise mutual information) se related hai, lekin exact criterion likelihood gain hai, raw PMI nahi.
- Kyun: Behtar theoretical grounding (har merge isko choose kiya jaata hai ki data zyada probable ho, na ki sirf zyada compressed)
SentencePiece (T5, LLaMA)
- Input ko raw byte/character stream treat karta hai (whitespace par koi pre-tokenization nahi)
- BPE (ya unigram LM) directly Unicode characters ya bytes par seekhta hai
- Kyun: Language-agnostic, spaces ko normal symbols treat karta hai, un languages ke liye kaam karta hai jahan clear word boundaries nahi hain
Byte-level BPE (GPT-2, GPT-3)
- BPE ko bytes (UTF-8 encoded) par run karta hai characters ki jagah
- Base vocab = 256 bytes, phir merge karo
- Kyun: Truly universal (kisi bhi language mein koi bhi text), koi special handling ki zaroorat nahi
BPE In Practice: Hyperparameters
| Parameter | Typical Range | Effect |
|---|---|---|
| Vocab size | 10k-50k | Bada → zyada lamba training, chhotas sequences, zyada rare-word memorization |
| Min frequency | 2-10 | Rare pairs filter karo training speed up karne ke liye |
| Pre-tokenization | Word/byte | Word-level: words intact rakhta hai (GPT-2 spaces + punctuation par split karta hai) |
| Special tokens | [PAD], [UNK], [CLS], etc. |
Vocab mein alag se add hote hain, kabhi merge nahi hote |
The 80/20: 30k vocab, min frequency = 2, byte-level pre-tokenization zyaatar modern LM needs cover karta hai.
Connections
- 4.2.01-Tokenization-Overview: BPE teen major tokenization strategies mein se ek hai
- 4.2.03-WordPiece-Tokenization: Variant jo raw frequency ki jagah likelihood gain use karta hai
- 4.2.04-SentencePiece: Unigram LM + BPE bina pre-tokenization ke
- 4.3.01-Language-Model-Basics: Tokenization LMs ke liye preprocessing hai; vocab size model size affect karta hai
- 3.1.05-Embedding-Layer: Vocab size embedding matrix size determine karta hai
- 6.2.02-Inference-Optimization: Chhota vocab → kam embedding lookups → faster inference
#flashcards/ai-ml