Speculative decoding
6.1.12· AI-ML › Scaling & Efficient Architectures
The Sequential Generation Bottleneck
WHAT is the problem?
Standard autoregressive generation ek time par ek token produce karta hai:
Length ki ek sequence ke liye, iska matlab hai sequential forward passes. Har pass:
- Memory se model weights load karta hai (billions of parameters)
- Latency memory bandwidth se dominate hoti hai, FLOPs se nahi
- Sequence dimension ke across parallelize nahi ho sakta
The inefficiency: Modern GPUs ke paas teraflops of compute hai, lekin zyada time memory I/O ke wait mein jaata hai. Model tokens ke beech "idle" rehta hai.
How Speculative Decoding Works
The Two-Model Setup
- Draft model : Chhota, fast model (e.g., 1B parameters, target se distilled)
- Target model : Bada, slow model (e.g., 70B parameters, jo hume actually chahiye)
Key property: per token 5-10× faster hai lekin thoda kam accurate hai.
The Algorithm (Step-by-Step)
WHY this works:
- Lossless: Acceptance sampling ensure karta hai ki output distribution exactly akele se match kare
- Fast: Jab draft sahi ho (often 60-80% tokens), hum ek pass mein tokens accept kar lete hain
- Bounded: Worst case sab kuch reject karna hai = standard decoding jaisa hi (koi slowdown nahi)
Derivation of Acceptance Criterion
Goal: Proposals from draft use karke target distribution match karo.
Rejection sampling se shuru karo:
- sample karo
- Probability se accept karo
WHY this gives :
Jab , hum probability se reject karte hain aur yahan se resample karte hain:
Combined distribution:
Math guarantee karta hai ki output exactly target model se match kare.
Worked Example: 4-Token Speculation
| Position | Draft Token | (draft) | (target) | Accept? | |----------|-------------|------------|------| | t | "Par" | 0.7 | 0.8 | ✓ (min(1, 0.8/0.7) = 1) | | t+1 | "is" | 0.5 | 0.6 | ✓ (min(1, 0.6/0.5) = 1) | | t+2 | "," | 0.4 | 0.2 | ✗ (min(1, 0.2/0.4) = 0.5, coin flip fails) |
Outcome:
- "Par" aur "is" accept karo (ek target pass mein 2 tokens!)
- "," reject karo aur position t+2 par distribution se resample karo
- Probably "." sample hoga (jiska tha)
- Final: 1 draft pass + 1 target pass se 3 tokens generate hue (vs. normally 3 passes)
WHY this step: Position t+2 fail hota hai kyunki target model "," ke upar "." strongly prefer karta hai, toh draft ka "," mein confidence misleading hai. Adjusted distribution draft ki bias hata deta hai.
Speedup Analysis
Expected Acceptance Rate
Maano = probability ki ek draft token accept ho jaaye (empirically good draft models ke liye 0.6-0.8).
Tokens accepted per speculation:
Derivation (indices par dhyan se):
- Exactly tokens accept karo phir reject (for ): pehle candidates accept hote hain (probability ) aur -waan reject hota hai (probability ). Phir hum adjusted distribution se ek token resample karte hain, jisse output tokens milte hain. Toh term hai .
- Saare candidates accept karo (probability ): hum additionally ek bonus token sample karte hain, jisse output tokens milte hain. Toh term hai .
- Sum karke aur geometric series simplify karne par closed form milta hai.
WHY speedup is less than : Rejections target pass waste kar dete hain. Maximum speedup (jab , toh output rate ) hai
jo sirf idealized limit mein equal hota hai (ek free draft model). Nonzero draft cost ke saath draft phase khud time leta hai, isliye true ceiling se neeche hai. Jab , speedup ho jaata hai (koi gain nahi, possibly draft passes se thoda overhead).
Optimizing the Draft Model
WHAT makes a good draft model?
- High agreement (): Target ke saath distribution mismatch minimize karo
- Low latency: Target se bahut zyada faster hona chahiye (typically >5× speedup)
- Same vocabulary: Token spaces bilkul align hone chahiye
HOW to create one:
WHY distillation works best: Explicitly distribution match ke liye optimize karta hai, sirf task accuracy ke liye nahi. 95% task accuracy wala model sirf 60% token-level agreement rakh sakta hai, jabki ek distilled model kam task accuracy par 75%+ agreement achieve kar sakta hai.
Common Mistakes
Practical Implementation Details
Batching Speculation
Challenge: Batch mein alag-alag sequences alag sankhya mein tokens accept kar sakti hain.
Solution: "Sync points" mein process karo:
- Saari sequences ko tokens se draft karo
- Target sab verify kare
- Har sequence independently 0 se tokens accept kare
- Chhoti sequences ko pad karo, unki current position se continue karo
WHY this works: Target verification phir bhi ek single batched forward pass hai. Asynchrony post-processing mein handle hoti hai.
Temperature and Sampling
Temperature ke saath:
- Draft se sample karta hai
- Target use karta hai
- Acceptance criterion unchanged:
Top-k, nucleus sampling: Adjusted distribution par acceptance sampling ke baad apply karo.
Extensions and Variants
Medusa decoding
Target model pe hi multiple "draft heads" use karta hai (intermediate layers par chhote prediction heads add karta hai). Alag draft model ki zaroorat nahi, lekin thodi memory trade hoti hai.
Speculative RAG
Retrieval-augmented generation ke liye, draft model tokens aur retrieval queries dono propose karta hai. Target dono verify karta hai.
Multi-candidate speculation
Draft model multiple branches ke saath candidates ka ek tree propose karta hai. Target saare paths verify karta hai. Jab low ho tab uncertainty better handle hoti hai.
Recall Feynman Explanation (ELI12)
Socho tum ek essay likh rahe ho aur tumhara ek chhota bhai/behen hai jo bahut fast likhta hai lekin kuch galtiyan karta hai. Trick yeh hai:
- Tumhara bhai/behen agle 4 sentences quickly likhta hai ("draft")
- Tum ek saath 4 sentences padhte ho aur har ek check karte ho
- Agar sentence sahi hai, rakh lo. Agar galat hai, wahan ruko, fix karo, aur bhai/behen ko wahan se dobara shuru karne kaho Yeh tumhare akele likhne se faster kyun hai? Kyunki 4 sentences padhna aur check karna almost utna hi time leta hai jitna tum khud 1 sentence likhte ho. Agar tumhara bhai/behen 4 mein se 2 bhi sahi kar de, tum time bacha rahe ho!
AI mein, "tum" ek giant smart model ho (slow), "bhai/behen" ek small quick model hai (fast lekin kam accurate), aur "sentences" tokens hain. Small model aage guess karta hai, big model ek saath sab check karta hai. Zyada tar guesses sahi hoti hain, toh hum zyada tar slow generation steps skip kar dete hain.
Connections
- Model Quantization — Complementary: draft aur target dono quantize karo aur speedup aur badha lo
- Flash Attention — Orthogonal memory optimization; dono combine kiye ja sakte hain
- Beam Search — Alternative decoding strategy; speculative decoding beam search ke saath compatible hai
- Knowledge Distillation — Draft model train karne ke liye use hoti hai
- Transformer Architecture — Self-attention samajhne se pata chalta hai memory-bound kyun hai
- Distributed Training — Speculative decoding inference latency kam karta hai; distributed training time kam karta hai
#flashcards/ai-ml
Speculative decoding kya hai? :: Ek lossless inference acceleration technique jahan ek fast draft model parallel mein candidate tokens generate karta hai, aur ek slow target model rejection sampling use karke ek single forward pass mein saare candidates verify karta hai taaki exact target distribution maintain rahe.