Files
myWiki/concepts/speculative-decoding.md

39 lines
2.1 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: "Speculative Decoding"
created: 2026-06-28
updated: 2026-06-28
type: concept
tags: [llm-inference, acceleration, draft-then-verify]
sources: []
---
# Speculative Decoding
投机解码Speculative Decoding是一种加速大语言模型推理的范式核心思想是将 token 生成解耦为**草稿生成draft generation**和**目标验证target verification**两个阶段。由一个轻量级草稿模型draft model一次性提议多个候选 token再由完整的目标模型并行验证整个候选块通过拒绝采样rejection sampling接受与目标分布一致的最长前缀并追加一个 bonus token。
因为验证是并行的且接受规则精确保持目标分布,投机解码在零质量损失的前提下实现加速。
## 核心公式
每轮解码的每 token 平均延迟:
$$L = \frac{T_{draft} + T_{verify}}{\tau}$$
其中 $\tau$ 为该轮被接受的 token 数。加速的关键杠杆有三个:降低 $T_{draft}$(更快草稿)、提高 $\tau$(更好草稿)、降低有效 $T_{verify}$(更智能验证)。
## 草稿模型架构分类
- **自回归草稿器Autoregressive Drafter**:逐 token 顺序生成,草稿延迟 $T_{draft} \propto \gamma$,迫使使用浅层网络和小块。代表:[[Eagle3]]、[[MTP]]
- **并行草稿器Parallel Drafter**:一次前向生成全部 $\gamma$ 个候选 token$T_{draft}$ 几乎与块大小无关,可支撑更深网络和更大块。代表:[[DFlash]]
- **半自回归草稿器Semi-Autoregressive Drafter**:并行骨干 + 轻量级顺序头,兼顾速度和质量。代表:[[DSpark]]
## 关键挑战
1. **接受率衰减**:并行草稿器的独立预测导致跨模态碰撞([[cross-mode-collision]]),后缀 token 接受率急剧下降
2. **验证浪费**:固定长度验证在低置信度 token 上浪费目标模型计算,尤其在高并发场景下挤占批容量
## 参考
- [[DSpark]] — 置信度调度 + 半自回归的统一框架
- [[DFlash]] — 基于 KV 注入的并行草稿器
- [[Eagle3]] — 基于 TTT 的自回归草稿器