Speculative Decoding is an efficient inference technique used in Large Language Models (LLMs) to accelerate generation by leveraging a smaller, "speculative" model to predict the output of a larger, more accurate "teacher" model. Here's a detailed breakdown of how it handles token probability distributions:
Overview:
- Teacher Model (TM): The large, accurate model that generates the target output distribution
P_TM(y | x).
- Speculative Model (SM): A smaller, faster model that approximates TM's output distribution
P_SM(y | x).
Key Insight: The speculative model doesn't need to exactly match the teacher's output probabilities. Instead, it focuses on preserving the rank order of likely tokens, allowing for efficient top-k filtering.
Speculative Decoding Algorithm:
1. Speculative Generation:
* Feed input x to SM, generating a truncated distribution P_SM(y | x) over the top-k tokens (e.g., k = 16).
* Compute the speculative scores s_i = log P_SM(y_i | x) for each of the top-k tokens y_i.
2. Refinement:
* Select the top-m tokens (e.g., m ≤ k) based on speculative scores, forming a candidate set C = {y_1, ..., y_m}.
* For each y_i ∈ C, compute the refined scores using the teacher model: r_i = log P_TM(y_i | x).
3. Final Ranking:
* Combine speculative and refined scores: score_i = s_i + (r_i - s_i) / τ, where τ is a temperature hyperparameter (e.g., τ = 2).
* Normalize scores to obtain the final distribution P(y | x) ∝ exp(score_i).
Key benefits:
- Efficiency: Speculative decoding reduces computation by:
- Using the smaller SM for initial filtering (step 1).
- Limiting TM evaluations to the top-m candidates (step 2).
- Accuracy: The refinement step (2) corrects for SM errors, leveraging TM's superior modeling capacity.
Handling token probability distributions:
- Approximation: SM's truncated distribution
P_SM(y | x) approximates TM's distribution, preserving the relative ranking of likely tokens.
- Refinement: The teacher model refines the top candidates, correcting errors and producing a more accurate final distribution.
Temperature (τ):
- Trade-off:
τ balances between:
- Speculative speed (high
τ: more weight on SM scores, faster but less accurate).
- Teacher accuracy (low
τ: more weight on refined scores, slower but more accurate).
By adaptively combining the strengths of both models, speculative decoding achieves significant speedups (e.g., 2-5x) while maintaining acceptable accuracy losses (e.g., < 1% perplexity increase). This technique has been successfully applied in various LLM architectures, including transformer-based models and mixture-of-experts architectures.