Blockwise and Semi-Autoregressive Generation › Speculative Decoding: Exact Stochastic Verification

Michael BrenndoerferNovember 17, 20267 min read
Language AI HandbookAll chapters
Part 19 of 67Alternative Sequence and Generative Architectures
Chapter 137Blockwise and Semi-Autoregressive Generation › Speculative Decoding: Exact Stochastic Verification
Reading section 5 of 11 · Speculative Decoding: Exact Stochastic Verification · 7 minSection 5 of 11 · 7 min

Section 5 of 11 · 7 min read

Reading settings

Definitions

Choose how many terms have tooltips.

Article links

Make inline references clickable

Notes & comments

The greedy verifier has one reference output for a given prompt and tie-breaking rule. For a sampling decoder, there is a reference distribution over outputs. Leviathan, Kalman, and Matias (2023) give an algorithm that preserves that distribution while still using multiple proposals per target pass.

Exactness in Speculative Decoding

Exactness means matching the output probability assigned by the baseline sampling policy to every sequence. Two implementations can consume random numbers in different orders. Their outputs with the same seed may differ even when the distributions agree.

The algorithm has four steps.

  • Draft. Sample up to γ\gamma tokens autoregressively from the draft distribution qq within the remaining budget, stopping the draft on a proposed EOS. A latency gain depends on the draft's cost relative to target verification and on how many of its proposals pass.
  • Score. Evaluate pp on the hypothetical prefixes using a batched target pass. For a block of mm proposals, the last committed position supplies p1p_1 and the logits at draft input ii supply pi+1p_{i+1}. The last draft input can supply the distribution for an extra token if the entire block survives and generation can continue.
  • Verify with rejection sampling. At each reached proposal ii, draw an independent r∼Uniform(0,1)r \sim \mathrm{Uniform}(0,1). Accept the proposed token xix_i if r<min⁡(1,pi(xi)/qi(xi))r<\min(1,p_i(x_i)/q_i(x_i)). Both probabilities refer to the same reached prefix.
  • Residual repair. If the first rejection happens at position ii, resample the output token from the residual distribution
resi(x)=max⁡(0,pi(x)−qi(x))∑x′max⁡(0,pi(x′)−qi(x′)).\mathrm{res}_i(x) = \frac{\max\bigl(0, p_i(x) - q_i(x)\bigr)}{\sum_{x'} \max\bigl(0, p_i(x') - q_i(x')\bigr)}.

Emit the replacement and discard the remaining draft. If every proposal is accepted and generation can continue, sample one extra token from pm+1p_{m+1}. Apply the baseline's EOS and output-budget rules to the committed tokens.

The acceptance probability uses the actual probability of drawing the proposal from qiq_i. For a token the draft overweights, the ratio accepts only enough proposals to match the smaller target mass. For a token the draft underweights, the residual supplies the missing probability. A draft with qi=piq_i=p_i always passes at that position.

Why the Rule Is Exact

Consider a single position with target distribution pp and draft distribution qq. The sampler produces xx by accepting a proposal of xx or by drawing xx from the residual after a rejection. The first product below is zero whenever q(x)=0q(x)=0.

P(output=x)=q(x)⋅min⁡(1,p(x)/q(x))+P(reject)⋅res(x).P(\text{output} = x) = q(x) \cdot \min\bigl(1, p(x)/q(x)\bigr) + P(\text{reject}) \cdot \mathrm{res}(x).

The first term simplifies to min⁡(q(x),p(x))\min(q(x), p(x)), because min⁡(1,p/q)=min⁡(q,p)/q\min(1, p/q) = \min(q,p)/q when q(x)>0q(x) > 0, and when q(x)=0q(x) = 0 the term is 00.

For a concrete example, take target probabilities (0.5,0.3,0.2)(0.5,0.3,0.2) and proposal probabilities (0.4,0.4,0.2)(0.4,0.4,0.2). The first and third proposals always pass. The second passes with probability 0.3/0.4=0.750.3/0.4=0.75. It carries proposal mass 0.40.4, so three quarters of that mass, 0.30.3, is already the required target mass. The remaining 0.10.1 goes to the rejection branch. A replacement draw assigns it to the first token, the only token whose target mass exceeds its proposal mass.

The rejection probability in general is

P(reject)=∑y:q(y)>0q(y)(1−min⁡(1,p(y)/q(y)))=∑ymax⁡(0,q(y)−p(y)).P(\text{reject}) = \sum_{y:q(y)>0} q(y)\bigl(1 - \min(1, p(y)/q(y))\bigr) = \sum_y \max\bigl(0, q(y) - p(y)\bigr).

Since both pp and qq sum to 1, ∑ymax⁡(0,q(y)−p(y))=∑ymax⁡(0,p(y)−q(y))\sum_y \max(0, q(y) - p(y)) = \sum_y \max(0, p(y) - q(y)), and that common value is the normalizer ZZ of the residual. When Z=0Z=0, the rejection branch has probability zero and every drawn proposal is accepted. For Z>0Z>0 the two paths give

P(output=x)=min⁡(q(x),p(x))+Z⋅max⁡(0,p(x)−q(x))Z=min⁡(q(x),p(x))+max⁡(0,p(x)−q(x))=p(x).P(\text{output} = x) = \min(q(x), p(x)) + Z \cdot \frac{\max(0, p(x) - q(x))}{Z} = \min(q(x), p(x)) + \max(0, p(x) - q(x)) = p(x).

Rejection mass is also half the sum of the absolute differences ∣p(y)−q(y)∣|p(y)-q(y)|. The positive differences and the negative differences have equal total magnitude because both distributions sum to one. This half-sum is total variation distance on the vocabulary. It gives a precise sense in which a better draft can pass more often at a fixed prefix. It also explains why keeping one phrase's top token unchanged can leave a sizable rejection probability when the rest of the distribution shifts.

This identity holds at each reached prefix hh. Reaching the next check depends on earlier draft tokens and their independent acceptance draws. Conditional on their prefix, the next token still comes from the actual proposal qi(⋅∣h)q_i(\cdot\mid h). After rejection, sample the replacement and restore both models to the committed prefix. Applying the resulting target conditional at every position gives the baseline sequence distribution. To compare to an exact top-pp baseline, pip_i must include the baseline's truncation and renormalization at the relevant prefix. The draft can use a different sampling rule. The verifier must use the qiq_i distribution that actually generated the proposal, expressed over the same token events as pip_i.

Expected Tokens per Iteration

Take one speculative iteration that proposes γ\gamma tokens and can emit the extra target token after accepting all of them. Ignore EOS and length stops for this cost calculation. To emit at least i+1i+1 tokens, the first ii proposals have to pass. With independent acceptance events of common probability α\alpha, the probability for that event is αi\alpha^i. The expected committed count CC is the sum of these probabilities:

E[C]=∑i=0γαi={(1−αγ+1)/(1−α),0≤α<1,γ+1,α=1.\mathbb{E}[C]=\sum_{i=0}^{\gamma}\alpha^i= \begin{cases} (1-\alpha^{\gamma+1})/(1-\alpha),& 0\le\alpha<1,\\ \gamma+1,&\alpha=1. \end{cases}

The first term counts the token guaranteed whenever an iteration can emit. In real decoding, acceptance probabilities can depend on the text and position. You can still sum the probabilities of reaching each accepted prefix length. Raising a single observed average acceptance rate to powers generally fails to estimate those probabilities exactly. For example, take two kinds of request that occur equally often. The first kind would pass both checks and the second kind would fail both checks if scored separately under their respective hypothetical draft prefixes. The chance of getting past both proposals is one half. Multiplying two unconditional per-position probabilities of one half would give one quarter under an independence assumption. The second check is reached only on the first kind of request, so its conditional acceptance rate is one. Pooling the first and reached second checks gives two successes for three checks per pair of requests. The exact expectation uses the probability of each whole accepted prefix, which is one half for lengths one and two in this example.

Each curve corresponds to a draft length. The horizontal axis is the assumed per-proposal acceptance probability. The vertical axis is the expected number of committed tokens from one target pass. At low acceptance, extra draft positions add small higher-power terms.

VisualizationOut [5]
CollapseExpand
Line chart of expected committed tokens per pass versus acceptance rate.
Expected committed tokens per target pass under the truncated geometric acceptance model, shown for three draft lengths. The curves approach the corresponding maximum of gamma + 1 tokens as acceptance approaches one.

Let TT be the baseline time for one target token and DD the time for one draft token. The simulation uses the same cost TT for full-block verification and for the baseline single-token step, with the draft and verify stages run serially. With fixed costs and long outputs, its predicted speedup is

S=E[C] TγD+T.S=\frac{\mathbb E[C]\,T}{\gamma D+T}.

The numerator is the time the baseline would spend on the expected number of tokens committed by one speculative iteration. The denominator is the cost of that iteration in this simplified model. Real verification time can differ from TT, and cache rollback, scheduling and synchronization can add costs. Batch-level throughput and the latency seen by each request therefore need separate measurement.

Comments

Add to the conversation

A question, a correction, or an example from your own work.

No comments yet. Be the first to share your thoughts!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy