Speculative Decoding

646 字
3 分钟
Speculative Decoding

Datawhale
Datawhale

大家好,我是芯缘,是 Datawhale 社区发起的 2026 年 8 月“llm-algo-leetcode 推理优化方向”组队学习活动的运营助教。本文记录了我学习 Task 3:解码算法的笔记。

基本原理#

投机解码(Speculative Decoding)的目标是减少大模型串行解码次数:小模型先生成一段草稿,大模型一次性验证多个 token。

// Step 1: draft model generates γ tokensx^t+1:t+γq(xt)// Step 2: target model verifies them in one forwardpi=p(x^t+ixt,x^t+1:t+i1)qi=q(x^t+ixt,x^t+1:t+i1)// Step 3: accept prefix tokens sequentiallyai=min ⁣(1,piqi)\begin{aligned} &\text{// Step 1: draft model generates } \gamma \text{ tokens} \\ &\hat{x}_{t+1:t+\gamma}\sim q(\cdot\mid x_{\le t}) \\ &\text{// Step 2: target model verifies them in one forward} \\ &p_i=p(\hat{x}_{t+i}\mid x_{\le t},\hat{x}_{t+1:t+i-1}) \\ &q_i=q(\hat{x}_{t+i}\mid x_{\le t},\hat{x}_{t+1:t+i-1}) \\ &\text{// Step 3: accept prefix tokens sequentially} \\ &a_i=\min\!\left(1,\frac{p_i}{q_i}\right) \end{aligned}

分类#

解码方法可以先按“是否严格一 token 一步”和“是否需要草稿验证”来分:

类别核心方式是否一次推进多 token例子
自回归解码每次用目标模型生成下一个 tokenGreedy Search
Sampling
Beam Search
Multi-Token Decoding一次解码步骤尝试产出多个 tokenSpeculative Decoding
MTP
DFlash
DSpark
Speculative Decoding先生成草稿 token
再由目标模型验证
通常是经典 Draft-Verify
MTP
DFlash
DSpark

比如 Speculative Decoding 中,当 γ=1\gamma=1 时,主体模型还是自回归解码;当 γ>1\gamma>1 时,如果一次验证能接受多个草稿 token,整体流程就具有 Multi-Token Decoding 的特点。但这并不要求 draft model 本身也必须是 Multi-Token Decoding:经典 draft model 通常仍是自回归生成草稿,而在 DFlash 中,draft model 本身也采用了并行的 multi-token 生成方式,因此 draft 阶段本身也属于 Multi-Token Decoding。

MTP#

MTP(Multi-Token Prediction)是在模型内部加入未来 token 预测头,让主模型自己生成草稿 token,因此常被看作 self-speculative decoding。

它的优势是不需要额外部署独立 draft model;代价是模型结构和训练目标要配合 MTP 模块。

【待完善】

DFlash#

DFlash 用块扩散模型生成草稿 token,用并行去噪替代传统自回归草稿生成。

它优化的是 draft 阶段:让草稿 token 的生成更并行。

【待完善】

DSpark#

DSpark 使用半自回归草稿生成,并结合置信度调度来决定草稿长度和验证策略。相比固定长度草稿,它更关注不同请求、不同位置上的动态接受率。

它优化的是“草稿生成 + 接受率调度”。

【待完善】

参考资料#

文章分享

如果这篇文章对你有帮助,欢迎分享给更多人!

评论区

Profile Image of the Author
平昊阳
乘长风,破巨浪, 展鸿图于未央!
--
总访问量
--
访客数
公告
欢迎来到我的个人博客!欢迎关注交流吖!
更多相关公告,见
社交-留言」。
音乐
封面

音乐

暂未播放

0:000:00
暂无歌词
站点统计
文章
123
分类
20
标签
174
总字数
1,103,045
运行时长
0
最后活动
0 天前

文章目录