6.1.12Scaling & Efficient Architectures

Speculative decoding

2,927 words13 min readdifficulty · medium

The Sequential Generation Bottleneck

WHAT is the problem?

Standard autoregressive generation produces one token at a time:

p(xtx<t)=softmax(Woutht)p(x_t | x_{<t}) = \text{softmax}(W_{\text{out}} h_t)

For a sequence of length TT, this requires TT sequential forward passes. Each pass:

  • Loads model weights from memory (billions of parameters)
  • Has latency dominated by memory bandwidth, not FLOPs
  • Cannot be parallelized across the sequence dimension

The inefficiency: Modern GPUs have teraflops of compute but spend most time waiting for memory I/O. The model is "idling" between tokens.


How Speculative Decoding Works

The Two-Model Setup

  1. Draft model MqM_q: Small, fast model (e.g., 1B parameters, distilled from target)
  2. Target model MpM_p: Large, slow model (e.g., 70B parameters, the one we actually want)

Key property: MqM_q is 5-10× faster per token but slightly less accurate.

The Algorithm (Step-by-Step)

WHY this works:

  • Lossless: The acceptance sampling ensures output distribution exactly matches MpM_p alone
  • Fast: When draft is correct (often 60-80% of tokens), we accept KK tokens in one pass
  • Bounded: Worst case is rejecting everything = same as standard decoding (no slowdown)

Derivation of Acceptance Criterion

Goal: Match target distribution p(x)p(x) using proposals from draft q(x)q(x).

Start with rejection sampling:

  1. Sample x~q(x)\tilde{x} \sim q(x)
  2. Accept with probability α=min(1,p(x~)/q(x~))\alpha = \min(1, p(\tilde{x}) / q(\tilde{x}))

WHY this gives p(x)p(x):

P(accept x)=q(x)p(x)q(x)=p(x)when p(x)q(x)P(\text{accept } x) = q(x) \cdot \frac{p(x)}{q(x)} = p(x) \quad \text{when } p(x) \leq q(x)

When p(x)>q(x)p(x) > q(x), we reject with probability 1p(x)/q(x)1- p(x)/q(x) and resample from:

presample(x)=max(0,p(x)q(x))max(0,p(y)q(y))dyp_{\text{resample}}(x) = \frac{\max(0,\, p(x) - q(x))}{\int \max(0,\, p(y) - q(y))\, dy}

Combined distribution:

Pfinal(x)=q(x)min(1,p(x)q(x))+(1yq(y)min(1,p(y)q(y)))presample(x)=p(x)P_{\text{final}}(x) = q(x) \min\left(1, \frac{p(x)}{q(x)}\right) + \left(1 - \sum_y q(y) \min\left(1, \frac{p(y)}{q(y)}\right)\right) \cdot p_{\text{resample}}(x) = p(x)

The math guarantees output matches the target model exactly.


Worked Example: 4-Token Speculation

| Position | Draft Token | qq (draft) | pp (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:

  • Accept "Par" and "is" (2 tokens in one target pass!)
  • Reject "," and resample from max(0,pq)\max(0, p - q) distribution at position t+2
  • Likely samples "." (which had p=0.5,q=0.1p=0.5, q=0.1)
  • Final: Generated 3 tokens with 1 draft pass + 1 target pass (vs. 3 passes normally)

WHY this step: Position t+2 fails because target model strongly prefers "." over ",", so draft's confidence in "," is misleading. The adjusted distribution removes draft's bias.


Speedup Analysis

Expected Acceptance Rate

Let α\alpha = probability a draft token is accepted (empirically 0.6-0.8 for good draft models).

Tokens accepted per speculation:

E[tokens]=i=0K1(i+1)αi(1α)+(K+1)αK=1αK+11α\mathbb{E}[\text{tokens}] = \sum_{i=0}^{K-1} (i+1)\,\alpha^{i}(1-\alpha) + (K+1)\,\alpha^K = \frac{1 - \alpha^{K+1}}{1 - \alpha}

Derivation (careful with indices):

  • Accept exactly ii tokens then reject (for 0i<K0 \le i < K): the first ii candidates are accepted (probability αi\alpha^i) and the (i+1)(i+1)-th is rejected (probability 1α1-\alpha). We then resample one token from the adjusted distribution, giving i+1i+1 output tokens. So the term is (i+1)αi(1α)(i+1)\,\alpha^i(1-\alpha).
  • Accept all KK candidates (probability αK\alpha^K): we additionally sample a bonus token, giving K+1K+1 output tokens. So the term is (K+1)αK(K+1)\,\alpha^K.
  • Summing and simplifying the geometric series gives the closed form 1αK+11α\dfrac{1 - \alpha^{K+1}}{1 - \alpha}.

WHY speedup is less than KK: Rejections waste the target pass. The maximum speedup (as α1\alpha \to 1, so output rate K+1\to K+1) is

Speedupmax=cp(K+1)Kcq+cp,\text{Speedup}_{\max} = \frac{c_p (K+1)}{K c_q + c_p},

which equals K+1K+1 only in the idealized limit cq0c_q \to 0 (a free draft model). With a nonzero draft cost the draft phase itself takes time, so the true ceiling is below K+1K+1. As α0\alpha \to 0, speedup cp/(Kcq+cp)1\to c_p/(K c_q + c_p) \le 1 (no gain, possibly slight overhead from the draft passes).


Optimizing the Draft Model

WHAT makes a good draft model?

  1. High agreement (α\alpha): Minimize distribution mismatch with target
  2. Low latency: Must be much faster than target (typically >5× speedup)
  3. Same vocabulary: Token spaces must align exactly

HOW to create one:

WHY distillation works best: Explicitly optimizes for distribution match, not just task accuracy. A model with 95% task accuracy might have only 60% token-level agreement, while a distilled model can achieve 75%+ agreement at lower task accuracy.


Common Mistakes


Practical Implementation Details

Batching Speculation

Challenge: Different sequences in a batch may accept different numbers of tokens.

Solution: Process in "sync points":

  1. Draft all sequences with KK tokens
  2. Target verifies all
  3. Each sequence accepts 0 to KK tokens independently
  4. Pad shorter sequences, continue from their current position

WHY this works: Target verification is still a single batched forward pass. The asynchrony is handled in post-processing.

Temperature and Sampling

With temperature τ\tau:

  • Draft samples from qτ(x)=softmax(logitsq/τ)q_\tau(x) = \text{softmax}(\text{logits}_q / \tau)
  • Target uses pτ(x)=softmax(logitsp/τ)p_\tau(x) = \text{softmax}(\text{logits}_p / \tau)
  • Acceptance criterion unchanged: min(1,pτ/qτ)\min(1, p_\tau / q_\tau)

Top-k, nucleus sampling: Apply after acceptance sampling on the adjusted distribution.


Extensions and Variants

Medusa decoding

Uses multiple "draft heads" on the target model itself (adds KK small prediction heads at intermediate layers). Trades some memory for not needing a separate draft model.

Speculative RAG

For retrieval-augmented generation, draft model proposes both tokens and retrieval queries. Target verifies both.

Multi-candidate speculation

Draft model proposes a tree of KK candidates with multiple branches. Target verifies all paths. Handles uncertainty better when α\alpha is low.


Recall Feynman Explanation (ELI12)

Imagine you're writing an essay and you have a younger sibling who writes really fast but makes some mistakes. Here's the trick:

  1. Your sibling writes the next 4 sentences quickly (the "draft")
  2. You read all 4 sentences at once and check each one
  3. If a sentence is good, you keep it. If it's wrong, you stop there, fix it, and have your sibling start again from that point Why is this faster than you writing alone? Because reading and checking 4 sentences takes almost the same time as writing 1 sentence yourself. If your sibling gets even 2 out of 4 right, you're saving time!

In AI, the "you" is a giant smart model (slow), the "sibling" is a small quick model (fast but less accurate), and "sentences" are tokens. The small model guesses ahead, the big model checks everything in one shot. Most guesses are right, so we skip most of the slow generation steps.


Connections

  • Model Quantization — Complementary: quantize both draft and target for further speedup
  • Flash Attention — Orthogonal memory optimization; both can be combined
  • Beam Search — Alternative decoding strategy; speculative decoding compatible with beam search
  • Knowledge Distillation — Used to train the draft model
  • Transformer Architecture — Understanding self-attention explains why memory-bound
  • Distributed Training — Speculative decoding reduces inference latency; distributed training reduces time

#flashcards/ai-ml

What is speculative decoding? :: A lossless inference acceleration technique where a fast draft model generates KK candidate tokens in parallel, and a slow target model verifies all candidates in a single forward pass using rejection sampling to maintain exact target distribution.

Why is autoregressive decoding slow?
Each token requires a sequential forward pass through the model, which is memory-bound (loading billions of parameters) rather than compute-bound, leaving GPU compute resources idle between tokens.
What is the acceptance criterion in speculative decoding?
Accept candidate token x~\tilde{x} with probability min(1,p(x~)/q(x~))\min(1, p(\tilde{x})/q(\tilde{x})) where pp is target model probability and qq is draft model probability. This ensures output matches target distribution exactly.
What is the expected number of output tokens per speculation?
E[tokens]=1αK+11α\mathbb{E}[\text{tokens}] = \frac{1 - \alpha^{K+1}}{1 - \alpha} where α\alpha is the probability a draft token is accepted and KK is the lookahead length. It equals the sum i=0K1(i+1)αi(1α)+(K+1)αK\sum_{i=0}^{K-1}(i+1)\alpha^i(1-\alpha) + (K+1)\alpha^K, ranging from 1 (worst case) to K+1K+1 (best case).
What makes a good draft model for speculative decoding?
(1) High agreement rate α\alpha with target model (60-80%), (2) Much faster than target (>5× speedup), (3) Same vocabulary and valid probability distributions. Architecture can differ from target.
Why doesn't speculative decoding change the output distribution?
The rejection sampling in Step 3 mathematically guarantees the output matches the target model's distribution exactly, even though we use draft model proposals. It's "trust but verify" with statistical correction.
What is the maximum theoretical speedup, accounting for draft cost?
Speedupmax=cp(K+1)Kcq+cp\text{Speedup}_{\max} = \frac{c_p(K+1)}{K c_q + c_p} where cq,cpc_q, c_p are draft/target per-token costs. This equals K+1K+1 only in the idealized limit cq0c_q \to 0; a nonzero draft cost lowers the ceiling.
What is the optimal lookahead KK in speculative decoding?
Typically K=4K=4-88. Larger KK increases wasted computation when early rejections occur. Optimal KK balances expected accepted tokens against cost: K=argmaxKE[accepted]Kcq+cpK^* = \arg\max_K \frac{\mathbb{E}[\text{accepted}]}{K \cdot c_q + c_p}
How does speculative decoding handle different temperatures?
Both draft and target apply temperature τ\tau to their logits before sampling: qτ=softmax(logitsq/τ)q_\tau = \text{softmax}(\text{logits}_q/\tau), pτ=softmax(logitsp/τ)p_\tau = \text{softmax}(\text{logits}_p/\tau). Acceptance criterion uses these temperature-adjusted probabilities. Top-k/nucleus applied after acceptance.

Concept Map

is

needs T sequential passes

GPU idles ~2% throughput

motivates

uses

verifies with

samples K candidates

single parallel pass

provides p distribution

acceptance sampling

min 1, p over q

60-80% accepted

yields

Autoregressive decoding

Memory-bound bottleneck

Slow generation

Wasted compute

Speculative decoding

Draft model Mq fast small

Target model Mp slow large

Draft phase parallel

Verification phase

Accept or resample

Lossless output matches Mp

Fewer sequential passes

Inference speedup

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Chalo isko simple tarike se samajhte hain. Socho aapke paas ek bahut bada aur intelligent model hai (target model, jaise 70B parameters), jo har token generate karne mein slow hai kyunki har baar poore model ke weights ko memory se load karna padta hai. Isko autoregressive generation kehte hain—ek time pe ek hi token, aur ye memory-bound hota hai, matlab GPU ka compute idle rehta hai bas memory I/O ka wait karta rehta hai. Yehi asli bottleneck hai—hamare paas teraflops ka compute hai par hum uska sirf ~2% use kar paate hain.

Ab speculative decoding ka jugaad ye hai: ek chhota, fast draft model (jaise 1B parameters) use karo jo jaldi-jaldi kuch tokens guess kar deta hai—jaise ek intern jaldi se sentence sketch kar deta hai. Fir bada target model in saare guessed tokens ko ek hi parallel forward pass mein check kar leta hai—jaise expert editor ek nazar mein poora draft approve kar deta hai. Kyunki draft model aksar 60-80% tokens sahi guess kar leta hai, hum ek hi mehnge pass mein kai tokens accept kar lete hain, aur bahut saare slow sequential steps skip ho jaate hain. Isliye speed bahut badh jaati hai.

Sabse important baat—ye technique lossless hai. Acceptance sampling ka jo maths hai (min(1, p/q) probability se accept karna, warna adjusted distribution se resample karna) wo mathematically guarantee karta hai ki final output bilkul wahi distribution follow karega jo akela target model deta. Matlab quality mein zero compromise, sirf speed ka fayda. Aur worst case bhi safe hai—agar saare guesses galat ho jaayein, tab bhi ye normal decoding jitna hi time lega, usse slow nahi. Isliye ye real-world mein LLM inference fast karne ke liye ek bahut powerful aur practical trick hai.

Go deeper — visual, from zero

Test yourself — Scaling & Efficient Architectures

Connections