Skip to content
E
Educatum

What is Speculative Decoding, and how does it improve LLM inference throughput without changing model outputs?

Curated by Educatum AI teamLast updated:
Inference
Direct Answer

Speculative Decoding accelerates memory-bandwidth-bound LLM inference by using a small, fast draft model to propose γ candidate tokens autoregressively, which the large target model verifies in a single parallel forward pass. Because processing γ tokens in parallel uses nearly the same HBM weight-loading bandwidth as a single decoding step, multiple tokens can be generated per target-model call. Modified rejection sampling accepts or corrects drafted tokens so the final output follows the exact mathematical distribution of the target model alone.

Speculative Decoding addresses the memory-bandwidth bottleneck of autoregressive inference by converting sequential generation into parallel verification (Leviathan et al., 2023, arXiv:2211.17192). In standard decoding, the processor loads the entire massive weight matrix of the target model from High Bandwidth Memory (HBM) to generate a single token, leaving compute units underutilized.

Speculative Decoding mitigates this by introducing a smaller, faster “draft” model to predict a sequence of γ (gamma) future tokens. The large “target” model then processes this entire drafted sequence in a single forward pass. Because modern accelerators are compute-rich but memory-starved, the latency of passing γ tokens through the target model simultaneously is nearly identical to passing just one token.

  • Drafting Phase: A lightweight model (e.g., a 7B parameter model drafting for a 70B parameter model) autoregressively generates a short sequence of γ tokens.
  • Verification Phase: The target model computes the logits for all γ drafted tokens in parallel.
  • Lossless Acceptance: A modified rejection sampling algorithm accepts or rejects the drafted tokens. If a token is rejected, the target model uses its computed logits to sample the correct replacement token, discarding the rest of the draft.

⚠️ Limitations & Caveats

  • The actual speedup is highly dependent on the “acceptance rate” (the alignment between the draft and target models); if the draft model frequently guesses incorrectly, the system incurs the overhead of the draft model without reaping the parallelization benefits.
  • Deploying Speculative Decoding requires hosting two separate models simultaneously, which increases the total VRAM footprint and complicates orchestration logic.

🧪 Core Insights & Mathematical Foundations

[Acceptance Probability]: Paccept(x)=min⁡ ⁣(1,Ptarget(x)Pdraft(x))[Resampling Distribution (if rejected)]: Presample(x)=max⁡(0,Ptarget(x)−Pdraft(x))∑x′max⁡(0,Ptarget(x′)−Pdraft(x′))[Expected Tokens per Step]: E[N]=1+∑i=1γ∏j=1iαj=1−αγ+11−α(αj=E ⁣[Paccept(xj)]; i.i.d. case αj=α)[Wall-Clock Speedup Ratio]: S≈E[N]⋅ttargetttarget+γ⋅tdraft=E[N]1+γ(tdraft/ttarget)[Medusa Head Objective]: LMedusa=∑k=1Kλk CrossEntropy ⁣(Ptarget(t+k), Pheadk(t))\begin{aligned} & \text{[Acceptance Probability]: } P_{\text{accept}}(x) = \min\!\left(1, \frac{P_{\text{target}}(x)}{P_{\text{draft}}(x)}\right) \\ & \text{[Resampling Distribution (if rejected)]: } P_{\text{resample}}(x) = \frac{\max(0, P_{\text{target}}(x) - P_{\text{draft}}(x))}{\sum_{x'} \max(0, P_{\text{target}}(x') - P_{\text{draft}}(x'))} \\ & \text{[Expected Tokens per Step]: } \mathbb{E}[N] = 1 + \sum_{i=1}^{\gamma} \prod_{j=1}^{i} \alpha_j = \frac{1 - \alpha^{\gamma+1}}{1 - \alpha} \quad \left(\alpha_j = \mathbb{E}\!\left[P_{\text{accept}}(x_j)\right]; \text{ i.i.d. case } \alpha_j = \alpha\right) \\ & \text{[Wall-Clock Speedup Ratio]: } S \approx \frac{\mathbb{E}[N] \cdot t_{\text{target}}}{t_{\text{target}} + \gamma \cdot t_{\text{draft}}} = \frac{\mathbb{E}[N]}{1 + \gamma (t_{\text{draft}} / t_{\text{target}})} \\ & \text{[Medusa Head Objective]: } \mathcal{L}_{\text{Medusa}} = \sum_{k=1}^{K} \lambda_k \, \text{CrossEntropy}\!\left(P_{\text{target}}^{(t+k)}, \, P_{\text{head}_k}^{(t)}\right) \end{aligned}

Follow-up 1: Explain the rejection sampling mechanism that guarantees the target distribution is preserved.

To ensure the output distribution is mathematically identical to what the target model would have produced alone, Speculative Decoding uses a specialized rejection sampling scheme. For a given drafted token xx, the system compares the probability assigned by the target model (PtargetP_{\text{target}}) to the probability assigned by the draft model (PdraftP_{\text{draft}}).

If Ptarget(x)≥Pdraft(x)P_{\text{target}}(x) \ge P_{\text{draft}}(x), the token is accepted with 100% probability, meaning the draft model correctly anticipated a highly likely token. If Ptarget(x)<Pdraft(x)P_{\text{target}}(x) < P_{\text{draft}}(x), the token is accepted with a probability equal to the ratio Ptarget(x)/Pdraft(x)P_{\text{target}}(x) / P_{\text{draft}}(x); this handles cases where the draft model was overly confident about a token that the target model considers less likely.

If a token is rejected through this stochastic process, the sequence breaks. The system must then sample a new token from a modified probability distribution (the “Resampling Distribution” in the math block) that subtracts the draft model’s probability mass from the target model’s distribution. This correction ensures the final marginal probability exactly matches PtargetP_{\text{target}}.

⚠️ Limitations & Caveats

  • Because the sampling relies on comparing full probability distributions, both models must use the exact same tokenizer.
  • When using temperature scaling (T>0T > 0), the acceptance rate typically drops because the distributions become flatter, making exact matches between the draft and target models less frequent.

Follow-up 2: How do draft-free architectures like Medusa address the complexities of two-model speculative decoding?

Standard Speculative Decoding requires finding a smaller draft model that perfectly matches the tokenizer and adequately approximates the reasoning of the target model, which is often difficult in practice. Architectures like Medusa (Cai et al., 2024, arXiv:2401.10774) eliminate the secondary model entirely by grafting multiple independent “decoding heads” onto the final hidden layer of the target model itself.

Each Medusa head is trained to predict a different future token offset (e.g., Head 1 predicts token t+1t+1, Head 2 predicts token t+2t+2). During inference, the model generates a tree of possible future token sequences in a single forward pass. The system then uses a hardware-efficient “Tree Attention” mechanism to verify all candidate paths simultaneously, accepting the longest valid sequence.

Dimension Standard Speculative Decoding Medusa (Draft-Free)
Computational Cost Moderate (Adds γ\gamma draft-model steps; target verifies in parallel) Low (Only adds a few linear layers to the base model)
VRAM Overhead High (Must load weights of a second model) Minimal (Base model + small projection heads)
Data Requirements None (Uses off-the-shelf pre-trained models) Requires fine-tuning the base model to train the Medusa heads
Known Failure Modes Frequent rejections if draft model is poorly aligned Tree Attention adds complex custom CUDA kernel dependencies

⚠️ Limitations & Caveats

  • Medusa requires an additional training phase to optimize the specialized heads, meaning it cannot be applied “zero-shot” to a newly released base model without access to a training pipeline and compute.
  • The length of the candidate tree grows exponentially with the number of heads, which can quickly saturate the KV cache and memory bandwidth if the tree depth is set too high.
View all →

Try keywords like RoPE, FlashAttention, DPO, or Blind 75.