← Back to AI Infrastructure Mind Map
🖥️ AI InfrastructureID: speculative-decoding

Speculative Decoding

推测解码
🎯Core Definition
Speculative decoding uses a small, fast draft model to guess γ candidate tokens ahead, then has the target model verify the whole candidate sequence in one parallel forward pass, accepting matching tokens and resampling where they diverge — turning each forward pass from producing 1 token into >1, trading decode's bandwidth-bound bottleneck for prefill-style compute parallelism. Pipeline: ① the draft model autoregressively generates γ\gamma candidates (fast per-step on a small model); ② the target model does a single forward pass over the candidates, obtaining true next-token distributions pip_i alongside draft distributions qiq_i; ③ compare position by position — greedy mode accepts when argmax pip_i equals the draft token, otherwise rolls back and resamples from that position; ④ optionally use rejection sampling: accept with probability min⁡(1,pi/qi)\min(1, p_i/q_i), otherwise resample from norm(max⁡(0,pi−qi))\text{norm}(\max(0, p_i - q_i)), which provably preserves the target model's exact distribution (lossless). Speedup formula (acceptance rate α\alpha, draft length γ\gamma, expected accepted tokens per round): E≈1−αγ1−αE \approx \frac{1 - \alpha^{\gamma}}{1 - \alpha}. Worked example: α=0.8, γ=4\alpha = 0.8,\ \gamma = 4 → E=1−0.841−0.8=0.59040.2≈2.95×E = \frac{1 - 0.8^4}{1 - 0.8} = \frac{0.5904}{0.2} \approx 2.95\times. Note the diminishing returns of larger γ\gamma (bounded by 1/(1−α)1/(1-\alpha) as αγ→0\alpha^{\gamma} \to 0): raising the acceptance rate beats lengthening the draft. Variants: EAGLE feeds the target model's previous-token hidden states through a small autoregressive head to predict the next layer's feature before mapping to vocabulary, giving high-quality drafts (α\alpha 0.7-0.9); Medusa attaches multiple parallel decoding heads to the backbone so no separate draft model is needed; both can use tree attention to verify several candidate branches in one forward pass, pushing α\alpha toward 1 while bounding verification cost.
💡Use Cases
any autoregressive serving where decode dominates (prefill already cached, throughput bound by token-at-a-time generation); built into vLLM/SGLang/TensorRT-LLM; interview favorites: "how do you compute the speedup", "why is verification one parallel pass", "EAGLE vs Medusa".
⚡Key Problems Solved
large-model decode produces 1 token per step and is HBM-bandwidth-bound (weight/KV reads ≫ compute), leaving tensor cores idle. Speculative decoding turns one serial step into a γ-token parallel verification at nearly the same forward cost (candidates are fed together, matrix parallelism amortizes), raising expected tokens per pass from 1 to EE — 2-3× throughput in practice, and with rejection sampling the output distribution is provably identical to the target model, so speedup costs no quality.
🎯Core Interview Exam Points
Fundamentals · Production Scenarios · Deep Dive
Core Def
Explain the core definition, underlying mechanism/math, and key principles of Speculative Decoding.
Use Cases
In production engineering, what are the primary use cases for Speculative Decoding and what core pain points does it solve?
🔥 High-Frequency Deep-Dive Questions
1
Describe the draft-verify flow and hand-compute the speedup: E≈1−αγ1−αE \approx \frac{1 - \alpha^{\gamma}}{1 - \alpha}; with α=0.8,γ=4\alpha=0.8,\gamma=4 get ≈ 2.95×; explain why larger γ\gamma has diminishing returns (bounded by 1/(1−α)1/(1-\alpha)).
2
Why verifying γ candidates in one pass costs almost no extra latency: candidates enter as one forward, per-position logits computed in parallel; contrast serial 1-token decode with γ-token verification.
3
How rejection sampling provably matches the target distribution (lossless): the math behind accept probability min⁡(1,pi/qi)\min(1, p_i/q_i) and resampling from norm(max⁡(0,pi−qi))\text{norm}(\max(0, p_i - q_i)).
4
EAGLE vs Medusa vs a separate small draft model: how drafts are produced, acceptance quality, training requirements, and how tree attention verifies multiple branches at once.
5
What determines the acceptance rate α\alpha, how to measure and improve it; how draft-model distillation or weight sharing affects throughput and memory.
📖 In-depth Guide:📄 speculative-decoding →
Updated 2026-08-12
🎯
Verify Mastery: Practice Exam Questions for "Speculative Decoding"
Take pitfall multiple-choice questions with derivation options; mistakes are automatically logged.
🚀 Start Card Practice ➔
← Previous CardKV Cache & PagedAttentionNext Card →Inference Engines vLLM/SGLang/TensorRT-LLM

🔗 More AI Infrastructure Knowledge Cards

引用出处: TalentMe 450+ 工业级 AI 架构考点知识库 (Tech Vault) · 遵循 CC-BY-4.0 知识共享协议
URL: https://talentme.airsota.com/resources/tech/roadmap/mindmap/AI_Infra/card/speculative-decoding
中文速览与面试要点

推测解码 (Speculative Decoding)

定义: 推测解码 = 用一个小而快的草稿模型 (draft model) 一次性猜出 γ 个候选 token,再把整段候选交给目标大模型做一次并行验证,接受匹配的 token,未匹配处回退重采样 —— 目标是让每次前向的产出 token 数从 1 变成 >1,把 decode 的带宽瓶颈换成 prefill 的算力并行。流程: ① 草稿模型自回归生成 个候选 token(小模型单步很快);② 目标模型对候选序列做单次前向,并行得到每个位置的真实分布 p_i 与草稿分布 q_i;③ 从 i=1 开始逐位比较,贪心模式下 p_i 的 argmax 等于草稿 token 则接受,否则回退到该位置重新采样;④ 可选 rejection sampling(修改拒绝采样): 以 (1, p_i/q_i) 概率接受草稿,拒绝时按 norm ( (0, p_i - q_i) ) 重新采样,保证输出分布与目标模型完全一致(lossless)。加速比公式(接受率 、草稿长度 ,每轮有效产出期望): E 1 - ^ 1 - 。数值例: = 0.8,\ = 4 → E = 1 - 0.8^41 - 0.8 = 0.59040.2 2.95 加速;可见 增大收益递减( ^ 趋近 0 后受 1/(1- ) 上限约束),提升接受率比无限加长草稿更有效。变体: EAGLE 用目标模型上一 token 的 hidden state 特征自回归预测下一层特征再映射回词汇分布,草稿质量高( 可达 0.7-0.9);Medusa 在模型头部挂多个并行的解码头直接草稿多步,无需独立草稿模型;两者都可用树状验证 (tree attention): 一次前向并行验证多条候选分支,让 接近 1 的同时控制验证开销。 使用场景: 任何 decode 占主流的自回归服务(TTFT 已缓存、吞吐瓶颈在逐 token 生成的场景);vLLM/SGLang/TensorRT-LLM 均内置;面试高频“加速比怎么算”“为什么一次验证是并行的”“EAGLE 与 Medusa 的区别”。 解决的问题: 大模型 decode 每步只产 1 个 token 且受限于 HBM 带宽(权重/ KV 读取 ≫ 计算),GPU 算力严重闲置。推测解码用草稿模型把“串行的 1 步”变成“并行的 γ 步验证”,单次前向成本不变(候选 token 一起喂入,矩阵并行摊薄),产出期望从 1 提升到 E 个 token —— 数值上 2-3 倍吞吐,且 rejection sampling 保证输出分布无损,加速不牺牲质量。

核心要点

  • 写出推测解码流程并手算加速比: 公式 E 1 - ^ 1 - ;给 =0.8, =4 算出 ≈ 2.95×;解释为什么 增大收益递减(上限 1/(1- ))。
  • 为什么“一次验证 γ 个候选”几乎不增加延迟: 候选 token 单次前向输入、按位置并行计算 logits;比较 decode 串行 1 token 与验证 γ token 的计算特性。
  • rejection sampling 如何保证输出分布与目标模型一致(lossless): 接受概率 (1, p_i/q_i) 与重采样 norm( (0, p_i - q_i)) 的数学含义。
  • EAGLE vs Medusa vs 独立小草稿模型: 各自草稿怎么来、接受率高低、是否需要额外训练、树状验证 (tree attention) 如何一次验证多分支。
  • 接受率 受什么影响、如何测量与提升;草稿模型与小模型的蒸馏/共享权重策略对吞吐与显存的影响。