Fast Inference from Transformers via Speculative Decoding

研究背景

核心思想

20260903173406

20260903173336

Speculative Sampling: 理论基础


先只考虑生成一个 token. 令 draft proposal 为 $q(x)$.

  1. 从 $q$ 采样候选 $y$.

  2. 以如下概率接受:

$$ a(y) = \min\left(1, \frac{p(y)}{q(y)}\right). $$

因此, 最终“候选正好是 $x$ 且被接受”的概率为:

$$ \mathbb{P}(\mathrm{accept}, y = x) = q(x)a(x) = q(x)\min\left(1, \frac{p(x)}{q(x)}\right) = \min(p(x), q(x)). $$

这正是两分布的重叠概率质量.

候选被拒绝的总概率为:

$$ Z = 1 - \sum_x \min(p(x), q(x)) = \sum_x \max(0, p(x) - q(x)). $$

拒绝后不直接从 $p$ 重采样, 而是从 target 尚未被“接受分支”覆盖的残差分布采样:

$$ p'(x) = \frac{\max(0, p(x) - q(x))}{Z}. $$

于是最终输出为 $x$ 的概率是两条路径之和:

$$ \begin{align*} \mathbb{P}(\mathrm{output} = x) &= \mathbb{P}(\mathrm{accept}, y = x) +\mathbb{P}(\mathrm{reject}) p'(x) \\ &= \min(p(x), q(x)) +Z \cdot \frac{\max(0, p(x) - q(x))}{Z} \\ &= \min(p(x), q(x)) + p(x) - \min(p(x), q(x)) \\ &= p(x). \end{align*} $$

如何确定单次生成长度 $\gamma$

关键洞察

继往开来

参考文献

  1. Leviathan Y., Kalman M. and Matias Y. Fast Inference from Transformers via Speculative Decoding. ICML, 2023.