< Will Bosler

Speculative Rejection Sampling ·

Speculative decoding can increase the speed of a target model, but does it reduce the quality of its output? It can be shown that it does not. With the right rejection sampling algorithm, the distribution of the target model is recovered completely, which is exactly what we want.

0:04 / 0:04

The goal is to accept or reject the draft model's guesses so that the output is distributed exactly like the target model's. A basic algorithm for doing so, from Accelerating Large Language Model Decoding with Speculative Sampling, drafts several tokens, scores them all with the target model in a single pass, then accepts or rejects each draft token in turn.

0:12 / 0:14

First, some terminology. The target model is denoted q and the draft model p. Both score every candidate next token for the same context, but with different odds. In speculative decoding, tokens are sampled from p, the fast model, yet the distribution we want to recover is that of q.

0:05 / 0:14

The acceptance test can now be formalised. A drafted token x̃ is accepted if r < min(1, q(x̃) ÷ p(x̃)). The test takes the minimum of 1 and the ratio between the target model's probability, which comes from the verification pass, and the draft model's probability. Here r is a random number drawn from the uniform distribution between 0 and 1, so every value in that range is equally likely.

0:09 / 0:10

The ratio between target and draft is therefore compared against this random number, and where it sits relative to r decides whether the token is accepted or rejected. If the accept probability is 0.4, about 40% of draws land below it and the token is accepted. There are three cases to consider.

In the first, the target is more confident than the draft. The ratio is larger than 1, so the minimum is 1, and since r is drawn from between 0 and 1, the token is always accepted. A token the target likes at least as much as the draft is never rejected.

0:08 / 0:11

In the second, the target is slightly less confident than the draft, and the outcome depends on the size of the difference. If the gap is small, the ratio might be around 0.9. Most of the time the random number will be less than this, and the token is accepted.

0:09 / 0:14

In the third, the target is significantly less confident in the drafted token, and the token will most likely be rejected. The draft has over-produced it, so some of those guesses must be thrown away.

0:09 / 0:14

When a token is rejected, a replacement must be resampled. The replacement is drawn from x ∼ (q(x) − p(x))₊: the draft's probability p is subtracted from the target's q, and the result is clipped at zero. Tokens the draft over-produced therefore have no chance of being resampled. The mass that remains sums to the rejection probability, and it is renormalised across the remaining tokens to form a new, valid probability distribution. A rejection therefore still yields a valid token.

0:12 / 0:28

It can be verified that this sampling process recovers the original target distribution q. The intuition lies in the equation min(p, q) + (q − p)₊ = q, where the first term is the probability that the draft proposes a token and it survives the test, and the second is the mass that resampling adds back. There are two cases.

Where the target is less confident than the draft, the minimum is q and the second term is clipped at zero, so nothing is added and the result is simply q. Where the target is more confident than the draft, the minimum is p, and resampling adds the difference q − p, precisely the amount that was missing. In both cases, the two terms sum to q.

0:16 / 0:20

Speculative decoding therefore delivers the speed-up of generating with a draft model, while this rejection sampling technique fully recovers the original target distribution. Nothing is lost in the quality of the output.