Supply exactly the target mass missing from accepted proposals
Normalize the positive part of p−q only on a positive-probability rejection branch, and verify the mixture identity including zero-rejection and missing-support cases.
On this page
The question for today
What you’ll understand
- Derive the rejection probability and normalized residual.
- Verify the accepted-plus-corrected output distribution token by token.
- Handle p=q and disjoint supports without fabricated division rules.
A proposed token is rejected, and the decoder draws a replacement directly from the original target distribution. That may sound safe, but accepted proposals have already supplied part of the target mass. Drawing the full target again can double-count that covered region. Exact speculative sampling instead draws the correction from the normalized positive part of p−q at the same first-failing history. The residual is a conditional distribution used only when rejection has positive probability; its zero-normalizer branch needs an explicit mathematical treatment.
Derive the mass left after the overlap
Accepted proposals contribute min(p(x), q(x)) to each token's unconditional output mass. Subtracting that from p(x) gives max(0, p(x)−q(x)), the mass still required. Its sum Z is exactly the rejection probability 1−α because p and q are normalized. When Z>0, divide those nonnegative values by Z to form the correction distribution r. Draw the correction with fresh randomness so its conditional law given rejection and the current history is r. The correction can assign mass to tokens that q never proposes, which is how the algorithm preserves target support without requiring a full-support draft.
The residual is attached to the current verified history. In a block, once the first rejection occurs, use p and q from that position and discard downstream speculative states that assumed the rejected token. The replacement is sampled once from the residual, then subsequent generation conditions on the replacement. Do not keep testing later proposals as though they followed the corrected prefix. The normalized residual is not a distribution over only the rejected token or over whichever candidates happened to be in a small tree; it is defined over the target's stated vocabulary domain.
Check the unconditional output identity
A token can emerge by being proposed and retained, or by being selected after a rejection. The first contribution is min(p, q); the second is Z times r. Their sum is p token by token, giving the exact one-step law under the stated probability and random-draw assumptions. Inductively applying the valid conditional rule after every committed prefix preserves the autoregressive target law, with matching stop semantics. This argument concerns distributions, so it does not require the same pseudorandom seed to produce the same text as an ordinary sampler using a different sequence of random draws.
If Z=0, normalized finite p and q are identical and the ideal proposal is accepted with probability one. No rejection event occurs and the residual 0/0 is never evaluated. An implementation should follow the appropriate nonrejection branch rather than add a numerical floor and claim the resulting fallback is part of this proof. If a finite-precision path reports rejection together with a zero residual sum, that is a numerical or consistency case requiring explicit investigation. For disjoint supports, α=0 and Z=1, so every proposal is rejected and r=p; correctness remains possible even though speculation provides no acceptance benefit.
Validate branches and numerical behavior directly
Test simple finite distributions where the entire output law can be enumerated, including equal distributions, disjoint support, partial zeros, and near equality. Check that every residual probability is nonnegative and sums to one when its branch is reachable. Preserve the actual processed p and q used for proposing and checking. Small numerical errors can be important when Z is tiny because normalization magnifies them. Pin dtype and backend, compare with a precise reference on small cases, and distinguish tolerances in implementation testing from the mathematical identity itself.
The probability lab displays the residual's two positive outcomes in the example below. Reset, set both lab success p=0.75 and n=1, predict the B/C bars, then inspect. Interpret success as B and failure as C; A has zero residual mass and is outside this two-outcome display. This is an exact Bernoulli visualization of a stipulated conditional residual, not a rejection sampler or a three-token model. Multiply the displayed probabilities by the separately calculated rejection probability before adding accepted masses to recover the unconditional target vector.
Work it through
Recover a three-token target law
At one fictional history, p(A, B, C)=(0.2, 0.5, 0.3) and q(A, B, C)=(0.6, 0.2, 0.2). Apply the exact acceptance rule and residual correction.
Accepted masses are min(p, q)=(0.2, 0.2, 0.2), so α=0.6 and rejection probability Z=0.4.
The positive difference is(0, 0.3, 0.1). Divide by 0.4 to obtain residual r=(0, 0.75, 0.25).
Add Zr=(0, 0.3, 0.1) to the accepted masses, giving final output(0.2, 0.5, 0.3)=p. Drawing the full p after rejection would generally produce another vector.
The normalized residual fills exactly the missing target mass. Its probabilities are conditional on the rejection branch, not the unconditional output frequencies.
The living notebook
Explore the model
Interactive lab · probability
Display a conditional residual before mixing it back
Reset, set both lab p=0.75 and n=1, predict, then inspect. Map success to B and failure to C, giving conditional residual masses 0.75 and 0.25; A’s residual mass is zero. This display does not generate proposals, detect rejection, or calculate the residual.
- Mean · np
- 5
- Variance · np(1−p)
- 2.5
- Total probability
- 1
These are exact binomial probabilities for independent trials with one fixed success probability. Playback tours the distribution; it does not resample outcomes. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forWeight the conditional residual by its rejection probability before adding accepted masses. Branch probabilities and unconditional output probabilities are different quantities.
Pause & reason
Check your understanding
Your turn
Try it yourself
For p=(1, 0) and q=(0, 1), compute acceptance probability, rejection probability, and residual. Then state what happens if p and q instead become identical.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Inspect overlap and positive differences before dividing.
Compare with the solution
Disjoint supports giveα=0, Z=1, and residual r=(1, 0)=p. Every proposal is corrected. If p=q, thenα=1 and Z=0, so all proposals pass and the unused residual is not evaluated.
Keep the idea
Take it with you
- The residual is the positive missing target mass divided by rejection probability.
- Accepted and corrected contributions recover p only under the stated mixture rule.
- Zero rejection and numerical inconsistencies need explicit branches, not invented division behavior.
Read further
Original sources and deeper explanations to follow at your own pace.
- Leviathan et al.: Fast Inference from Transformers via Speculative Decoding, v 2
- Chen et al.: Accelerating Large Language Model Decoding with Speculative Sampling
- Transformers 4.56.2: Assisted Generation and Speculative Sampling Implementation
- vLLM 0.10.2: Speculative Decoding, Compatibility, and Numerical Limits
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.