Zack Li logoZack Li

Back to Blog

Speculative Decoding Deep Dive

Created on July 30, 2026

This session explains speculative decoding: a cheap draft proposes several tokens, the target verifies them in one pass, and rejection sampling keeps the output law equal to the target.

1. Overview

Autoregressive decoding: one token per target pass. Latency scales with length.

Speculative decoding (Chen et al. 2023; Leviathan et al. 2023): draft several tokens cheaply, verify them in one target pass. Methods differ in how they build qq.

cheaper q+ more accurate q+ verify many tokens in one pass\mathrm{cheaper}\ q +\ \mathrm{more\ accurate}\ q +\ \mathrm{verify\ many\ tokens\ in\ one\ pass}

Core. Exact speculative sampling keeps the target law. A worse qq only lowers the accept rate.

pSD=pp_{\mathrm{SD}}=p

Draft then parallel verify, then reject

Prefix: Under the pale moon

  • Case 1: Reject. Draft a cat crossed the garden, keep a cat, drop the suffix, resample slept from pp'. Emit Under the pale moon a cat slept.
  • Case 2: All accept. Prefix Beside the silent lake, draft birds began to sing, bonus softly. Emit Beside the silent lake birds began to sing softly.

2. Why it is fast

Low-batch decode is memory-bound: each target pass reloads weights for one token. Speculative decoding spends idle compute — the target checks γ+1\gamma+1 positions in one pass.

  • Acceptance rate α\alpha — mean keep-probability over positions/contexts
  • Accepted length τ\tau — expected tokens emitted per cycle (accepted drafts plus one correction or bonus)
α=Exq[min(1,p(x)q(x))]=xmin(p(x),q(x))\alpha=\mathbb{E}_{x\sim q}\left[\min\left(1,\frac{p(x)}{q(x)}\right)\right]=\sum_x\min(p(x),q(x))

If accepts are i.i.d. with mean α\alpha and draft length γ\gamma:

τ=i=0γαi=1αγ+11α\tau=\sum_{i=0}^{\gamma}\alpha^{i}=\frac{1-\alpha^{\gamma+1}}{1-\alpha}

If α=1\alpha=1, then τ=γ+1\tau=\gamma+1. The i=0i=0 term is the token the target always emits, so τ[1,γ+1]\tau\in[1,\gamma+1].

Per-token latency (Sadhukhan et al.):

L=Tdraft+TverifyτL=\frac{T_{\mathrm{draft}}+T_{\mathrm{verify}}}{\tau}

Latency per token drops if (1) drafts match pp more often (larger τ\tau); (2) drafting is cheaper; (3) verification is cheaper.

3. Math

This section proves that speculative sampling outputs xoutpx_{\mathrm{out}}\sim p, the same as the target distribution.

Let pp be the target next-token distribution and qq the draft distribution, same context.

Split pp into overlap (accepted drafts) plus leftover (filled only after reject):

p(x)=min(p(x),q(x))+max(0,p(x)q(x))p(x)=\min(p(x),q(x))+\max(0,p(x)-q(x))

3.1 Acceptance

α(x)\alpha(x) is the probability we accept the draft token, given that the draft proposed xx:

α(x)=P(acceptdraft=x)\alpha(x)=P(\mathrm{accept}\mid \mathrm{draft}=x)

Draw uUnif[0,1]u\sim\mathrm{Unif}[0,1]; keep xx iff uα(x)u\le\alpha(x). For q(x)>0q(x)>0,

α(x)=min(1,p(x)q(x))\alpha(x)=\min\left(1,\frac{p(x)}{q(x)}\right)

Then

P(x,accept)=q(x)α(x)=q(x)min(1,p(x)q(x))=min(p(x),q(x))\begin{aligned} P(x,\mathrm{accept}) &= q(x)\,\alpha(x) \\ &= q(x)\min\left(1,\frac{p(x)}{q(x)}\right) \\ &= \min(p(x),q(x)) \end{aligned}
  • q(x)>p(x)q(x)>p(x): over-propose; keep fraction p/qp/q
  • q(x)p(x)q(x)\le p(x): under-propose; keep every such xx

Acceptance stores exactly the overlap min(p,q)\min(p,q).

3.2 Rejection correction

r(x)r(x) is the uncovered part of p(x)p(x): how much target mass is still missing after the accept path has taken min(p,q)\min(p,q).

  • p(x)>q(x)p(x)>q(x): accept only covered q(x)q(x), so r(x)=p(x)q(x)r(x)=p(x)-q(x)
  • p(x)q(x)p(x)\le q(x): accept already covered all of p(x)p(x), so r(x)=0r(x)=0
r(x)=p(x)min(p(x),q(x))=max(0,p(x)q(x))r(x)=p(x)-\min(p(x),q(x))=\max(0,p(x)-q(x))

ZZ is the total uncovered mass (over the vocabulary):

Z=yr(y)Z=\sum_y r(y)

If rejected and Z>0Z>0, sample the correction from p(x)=r(x)/Zp'(x)=r(x)/Z.

Accept probability A=xq(x)α(x)=xmin(p(x),q(x))A=\sum_x q(x)\alpha(x)=\sum_x\min(p(x),q(x)). Since p=1\sum p=1,

P(reject)=1A=ZP(\mathrm{reject})=1-A=Z

Correction path:

P(reject)p(x)=Zr(x)Z=max(0,p(x)q(x))P(\mathrm{reject})\,p'(x)=Z\cdot\frac{r(x)}{Z}=\max(0,p(x)-q(x))

Add the two paths:

PSD(x)=min(p(x),q(x))+max(0,p(x)q(x))=p(x)\begin{aligned} P_{\mathrm{SD}}(x) &= \min(p(x),q(x))+\max(0,p(x)-q(x)) \\ &= p(x) \end{aligned}

So PSD=pP_{\mathrm{SD}}=p. If p=qp=q then Z=0Z=0: every proposal is kept and pp' is unused.

3.3 Multiple draft tokens

Draft d1,,dγd_1,\ldots,d_\gamma. After d1,,di1d_1,\ldots,d_{i-1} are kept, both models share context ci=prefix+d1++di1c_i=\mathrm{prefix}+d_1+\cdots+d_{i-1}. Verify left to right:

qi(x)=q(xci),pi(x)=p(xci),αi=min(1,pi(di)qi(di))q_i(x)=q(x \mid c_i), \qquad p_i(x)=p(x \mid c_i), \qquad \alpha_i=\min\left(1,\frac{p_i(d_i)}{q_i(d_i)}\right)

Reject at kk. Keep d1,,dk1d_1,\ldots,d_{k-1}. Drop dk,,dγd_k,\ldots,d_\gamma (later drafts assumed dkd_k). Sample xcorrpkx_{\mathrm{corr}}\sim p_k'. Emit d1,,dk1,xcorrd_1,\ldots,d_{k-1},x_{\mathrm{corr}}.

All γ\gamma accepted. The same pass has pγ+1(x)=p(xprefix+d1++dγ)p_{\gamma+1}(x)=p(x\mid \mathrm{prefix}+d_1+\cdots+d_\gamma). Sample xbonuspγ+1x_{\mathrm{bonus}}\sim p_{\gamma+1}. Emit d1,,dγ,xbonusd_1,\ldots,d_\gamma,x_{\mathrm{bonus}}.

A cycle emits between 11 and γ+1\gamma+1 tokens. Repeating the one-step rule at each valid context gives the target sequence law:

PSD(x1:n)=i=1np(xix1:i1)=Ptarget(x1:n)P_{\mathrm{SD}}(x_{1:n}) = \prod_{i=1}^{n}p(x_i \mid x_{1:i-1}) = P_{\mathrm{target}}(x_{1:n})

Speculative decoding changes how many target passes are needed, not the target output distribution.

© Copyright 2026 Zack Li.