Below, I'll provide a detailed explanation based on the "Leviathan paper," which I interpret as referring to the influential work Fast Inference from Transformers via Speculative Decoding by Yaniv Leviathan, Matan Kalman, and Yordan Yordanov (arXiv:2211.17192, 2023). This paper formalizes speculative decoding (also called speculative sampling) and provides a theoretical proof that the tokens generated via this method are sampled from a distribution identical to that of the original large language model (LLM). I'll explain the reasoning step by step, drawing directly from the paper's concepts, including its mathematical formulation.
Background from the Paper
Speculative decoding accelerates LLM inference by using a smaller "draft" model to propose (or "speculate") multiple future tokens, which are then verified in parallel by the larger "target" model. The key claim is that this process doesn't just speed things up—it produces outputs that are statistically indistinguishable from those generated by standard autoregressive sampling from the target model alone. This is proven through a rejection sampling framework, ensuring no loss in quality or alteration of the probabilistic distribution.
The paper emphasizes that speculative sampling is not an approximation; it's an exact method for sampling from the target model's distribution, just faster.
Why Speculatively Sampled Datapoints Are Identical to Sampling from the Original Distribution
The core insight is that speculative sampling can be viewed as a form of rejection sampling, where the draft model acts as a proposal distribution, and the target model enforces acceptance or rejection to match its own (original) probability distribution. Here's the breakdown:
1. Setup and Notation
- Let ( p ) be the target model (the large LLM), with probability distribution ( p(x_t | x_{<t}) ) for predicting the next token ( x_t ) given the prefix ( x_{<t} ).
- Let ( q ) be the draft model (smaller and faster), with its own distribution ( q(x_t | x_{<t}) ).
- In speculative sampling:
- The draft model ( q ) generates a sequence of proposed tokens ( \hat{x}{t+1}, \hat{x}{t+2}, \dots, \hat{x}_{t+k} ) (e.g., ( k ) tokens ahead).
- These are fed into the target model ( p ) in a single parallel forward pass to compute probabilities for each position.
- For each proposed token ( \hat{x}{t+i} ), acceptance is determined by comparing ( p(\hat{x}{t+i} | x_{<t+i}) ) against a random sample or threshold derived from ( q )'s proposal (more on this below).
2. The Verification (Acceptance/Rejection) Mechanism
- For each proposed token ( \hat{x}_{t+i} ), the target model computes its probability under ( p ).
- Acceptance is probabilistic: A proposed token ( \hat{x} ) from ( q ) is accepted with probability ( \min\left(1, \frac{p(\hat{x} | x_{<t})}{q(\hat{x} | x_{<t})}\right) ). This is the classic rejection sampling acceptance ratio.
- If accepted, ( \hat{x} ) is kept as part of the output sequence.
- If rejected, the process "rolls back" to that point, and the target model directly samples a new token from its own distribution ( p(\cdot | x_{<t}) ).
- This continues sequentially for the chain of proposals until a rejection occurs or all are accepted. If all ( k ) proposals are accepted, the target model's prediction for the next token (after the last proposal) is sampled directly from ( p ).
The paper notes that this can be implemented efficiently without explicitly computing the min-ratio for every token—often using a simplified comparison where the proposed token is accepted if it's the argmax or sampled consistently, but the underlying math ensures equivalence.
3. Mathematical Proof of Equivalence
The paper proves that the overall sampling process produces sequences with the exact same joint probability as direct sampling from ( p ). Here's the key reasoning:
-
Rejection Sampling Guarantee: Rejection sampling generates samples from a target distribution ( p ) by proposing from an easier-to-sample distribution ( q ) (assuming ( q ) dominates ( p ), i.e., ( q(x) > 0 ) whenever ( p(x) > 0 ), and there's a bound on ( p/q )). The acceptance step ensures that accepted samples are distributed exactly according to ( p ).
- In speculative sampling, for each position ( t ), the draft ( q ) proposes a candidate ( \hat{x}_t ).
- The acceptance probability ( \alpha = \min\left(1, \frac{p(\hat{x}t | x{<t})}{q(\hat{x}t | x{<t})}\right) ) corrects for any discrepancy between ( q ) and ( p ).
- If rejected, you resample directly from ( p ) (or continue the process), but the net effect is that the final token at position ( t ) is drawn from ( p(\cdot | x_{<t}) ).
-
Chain Rule for Sequences: For a full sequence ( x = [x_1, x_2, \dots, x_n] ), the joint probability under speculative sampling is:
[
P_{\text{spec}}(x) = \prod_{t=1}^n p(x_t | x_{<t})
]
This is identical to the joint probability under standard autoregressive sampling from ( p ), because:
- Accepted tokens are those that ( p ) would have sampled with the correct probability (after correction via rejection).
- Rejected proposals are replaced by direct samples from ( p ), preserving the conditional probabilities.
-
No Bias Introduced: The paper shows that even though proposals from ( q ) might be "wrong" (low probability under ( p )), the rejection mechanism filters them out without biasing the final distribution. The expected number of rejections is finite (assuming ( q ) is a reasonable approximation of ( p )), and the process converges to exact samples from ( p ).
-
Edge Cases: If ( q = p ) (perfect draft), all proposals are accepted with probability 1, and it's equivalent to direct sampling. If ( q ) is poor, more rejections occur, but the distribution remains exact—it's just less efficient.
4. Intuition: Why Doesn't It Alter the Distribution?
- Imagine sampling lottery numbers: The draft model guesses a bunch of numbers quickly. The target model checks if those guesses match what it would "expect" probabilistically. If they do, great—keep them. If not, throw them out and pick a number directly from the true lottery rules.
- Over many runs, the numbers you end up with are exactly as if you'd picked directly from the true rules every time. The guesses just speed things up by batching checks.
This ensures unbiased sampling: The generated text has the same diversity, creativity, and statistical properties (e.g., under temperature or top-p sampling) as the original model.
5. Empirical Validation in the Paper
- The authors demonstrate this theoretically and empirically, showing that perplexity and other metrics match exactly between speculative and standard sampling.
- Speedups (e.g., 2-3x) come from parallel verification, but the proof guarantees no quality loss.
Implications and Limitations
- Why This Matters: This equivalence allows speculative decoding to be a "drop-in" accelerator for LLMs without retraining or fine-tuning. It's used in systems like DeepMind's Genie or Hugging Face implementations.
- Assumptions: The proof assumes the draft model ( q ) is autoregressive and can propose valid tokens (i.e., it doesn't propose impossible tokens). In practice, ( q ) is often a smaller version of ( p ) or an approximation.
- Extensions: Later works (e.g., Medusa or Eagle) build on this, but the Leviathan paper lays the foundational proof.
If this isn't the exact "Leviathan paper" you meant (or if you have a specific section/figure in mind), feel free to clarify! For deeper dives, I recommend reading the paper's Section 3 (on the algorithm) and Appendix A (proofs).