AI engineering and inferenceLoading progress…

Draft and verify

Trace draft-and-verify decoding, explain distribution-preserving acceptance, and calculate a simplified speedup.

Guess cheaply, check in one pass

Decoding is slow because the big model must run once per token. Speculative decoding uses a much cheaper draft to guess several tokens ahead, then has the big (target) model check all the guesses in a single pass. Guesses that the target agrees with are kept; at the first disagreement the target's own token is used and the rest of the guesses are discarded. One expensive pass can therefore yield several tokens.

draft proposes (cheap, not trusted):
7359
the big model, in ONE pass, would itself pick (change them to see the effect):
target at position 17target at position 23target at position 38target at position 41target at position 5 (bonus)2
accepted draft tokens: 2 emitted this round: [7, 3, 8] = 3 tokens at the first disagreement the target's own token replaces the draft's, and the rest of the draft is thrown away

Nothing from the draft is output before the target has checked it. Every verification yields at least one token.

The draft proposes 7 3 5 9. Change what the big model would pick.

Predict first. Can every draft token be emitted before verification in exact speculative sampling?

Chapter context

A cheap draft proposes several tokens and a target model verifies them with parallel work. The opportunity comes from amortizing an expensive target step, but drafting, verification and rejection recovery all cost time.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

Under constant independent acceptance a and k draft proposals:

E[N]=∑i=0kai=1−ak+11−a(a≠1).E[N]=\sum_{i=0}^{k}a^i=\frac{1-a^{k+1}}{1-a}\quad(a\ne1).

At a=1 the sum is k+1. A simple speed estimate is E[N]/(tverify+ktdraft)E[N]/(t_{verify}+k t_{draft}). Exact samplers use conditional probabilities; this constant-a model is not their full correctness proof.

Worked example

For a=0.8,k=4, expected produced tokens are 1+0.8+0.64+0.512+0.4096=3.3616. If target-only generation takes 10 ms/token, verification costs 12 ms and four draft steps cost 1 ms each, speculative throughput is 3.3616/0.016=210.1 tokens/s versus 100 tokens/s baseline: an illustrative 2.101× speedup.

Reference code

The following example uses the Python standard library and does not call a remote service.

from math import isclose
a,k=.8,4
expected=sum(a**i for i in range(k+1))
assert isclose(expected,3.3616)
assert isclose(expected/.016,210.1)
assert isclose((expected/.016)/(1/.010),2.101)
assert sum(1**i for i in range(k+1)) == 5

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

Expected accepted draft tokens alone omit the final target token in this model. a=1 needs the finite-sum limit. A correctness-preserving acceptance rule is more than an agreement heuristic.