Skip to content

← Modern Techniques step 10 of 10

Hard Research

Speculative Decoding Accept/Reject

A draft model proposes a token. A target model may assign that token a lower probability, so we cannot always keep the proposal. Compute the probability of keeping it and the replacement distribution to use if it is rejected.

This exercise covers a deterministic calculation for one proposed token. Do not sample a token, make a random accept/reject decision, or run a model.

Inputs

Implement speculative_decode_step(draft_probs, target_probs, drafted_token). The two arrays have shape (V,), with V >= 1, and contain probability distributions over the same vocabulary. They have the same floating-point dtype and device. Values are finite, nonnegative and sum to one.

Write q = draft_probs and p = target_probs. The integer drafted_token is a valid vocabulary index t, and q[t] > 0, as it must be for a token sampled from the draft distribution. Other entries of q may be zero. The inputs are probabilities, so do not apply softmax or renormalize them.

Calculation

  1. Compute accept_prob = min(1, p[t] / q[t]).
  2. For every vocabulary entry, compute r = maximum(p - q, 0).
  3. If r.sum() > 0, divide r by its sum. Otherwise, return the zero vector as the residual. In this case the distributions match and every possible draft token is accepted, so no replacement is needed.

Return a dictionary with exactly two entries:

  • "accept_prob": an ordinary Python float. Do not round it.
  • "residual": the resulting array of shape (V,), preserving the input dtype and device. Compute it even when this particular token is always accepted. It depends on the full distributions, not the selected token.

Leave both input arrays unchanged.

Example

For q = [0.8, 0.2], p = [0.4, 0.6] and t = 0, the acceptance probability is 0.4 / 0.8 = 0.5. The positive differences are [0, 0.4], so the residual is [0, 1]. A rejected proposal of token 0 would therefore be replaced by token 1.

In the full sampling procedure, proposals drawn from q are accepted with this probability; rejected proposals are replaced by a draw from the residual. Together, these two steps give the target distribution p. The acceptance rule alone does not provide that guarantee.