Speculative decoding (also known as Assisted Generation or Lookahead Decoding) is a clever technique designed to speed up the generation process of large language models (LLMs), especially during autoregressive inference (generating one token at a time).
The core idea is to "guess" a few future tokens using a small, fast draft model, and then verify these guesses in parallel using the large, accurate target model.
Why is it needed?
Traditional autoregressive decoding is sequential:
- Predict token 1.
- Add token 1 to the input.
- Predict token 2.
- Add token 2 to the input.
...and so on.
Each step requires a full forward pass through the (often very large) LLM, which can be computationally expensive and slow. Speculative decoding aims to reduce the number of these expensive full passes.
How it works (Step-by-Step):
Speculative decoding involves two models:
- Draft Model (or "Assistant Model"): A much smaller, faster, and less accurate LLM. Its job is to quickly generate a speculative sequence of candidate tokens.
- Target Model: The large, accurate LLM you actually want to use for generation. Its job is to verify and accept or reject the candidate tokens proposed by the draft model.
Here's the process:
-
Initial State: You have a prompt or a sequence of already generated tokens (the "prefix").
-
Draft Model Generates Candidates:
- The current prefix is fed into the Draft Model.
- The Draft Model quickly generates
N candidate tokens in an autoregressive fashion (i.e., it predicts token 1, then token 2 based on token 1, etc., up to N tokens). This is fast because the Draft Model is small.
- Let's say the draft model generates
t_1_cand, t_2_cand, ..., t_N_cand.
-
Target Model Verifies (in Parallel):
- The original prefix plus all
N candidate tokens (prefix + t_1_cand + t_2_cand + ... + t_N_cand) are concatenated into a single input sequence.
- This entire longer sequence is then fed into the Target Model for a single forward pass.
- Because of the attention mechanism in transformers, the target model can compute the log probabilities for each position in this combined sequence in parallel. Specifically, it will output:
P(t_1_actual | prefix)
P(t_2_actual | prefix, t_1_cand)
P(t_3_actual | prefix, t_1_cand, t_2_cand)
- ...and so on, up to
P(t_N_actual | prefix, t_1_cand, ..., t_{N-1}_cand)
- It also outputs
P(t_{N+1}_actual | prefix, t_1_cand, ..., t_N_cand) (the very next token after the whole speculative batch).
-
Acceptance/Rejection (Token-by-Token Verification):
- The algorithm then iterates through the
N candidate tokens generated by the draft model and compares them against the probabilities computed by the target model at each corresponding position.
- For each candidate
t_i_cand:
- Compare
P_target(t_i_cand) (the probability assigned by the target model to the draft's guess) with P_draft(t_i_cand) (the probability the draft model assigned to its own guess).
- Acceptance Condition: If a token
t_i_cand is accepted, it means the target model's output distribution for that position aligns sufficiently with the draft model's prediction. The typical acceptance criterion involves a statistical trick: if a random number u ~ U(0,1) is less than P_target(t_i_cand) / P_draft(t_i_cand), accept t_i_cand. If this condition is met for t_i_cand, it becomes part of the accepted sequence.
- Rejection and Resampling: If
t_i_cand is rejected (i.e., the condition is not met), it means the target model doesn't agree with the draft model's prediction for that token. At this point:
- All previously accepted tokens
t_1, ..., t_{i-1} are kept.
- For the current position
i, the target model generates the correct token based on its own P(t_i_actual) distribution.
- The speculation process is reset. The next round of speculation will start from the new prefix that includes the accepted tokens and the newly generated token from the target model.
-
Loop: The process repeats from step 2 with the updated prefix.
Example:
Let's say your prefix is "The quick brown".
-
Draft Model (small LLM) predicts k=3 tokens: "fox jumps over"
- Draft output:
P_draft(fox), P_draft(jumps | fox), P_draft(over | fox, jumps)
-
Target Model (large LLM) gets input: "The quick brown fox jumps over"
- Target outputs probabilities for these positions:
P_target(token_1 | "The quick brown")
P_target(token_2 | "The quick brown fox")
P_target(token_3 | "The quick brown fox jumps")
P_target(token_4 | "The quick brown fox jumps over") (the next token after the speculative batch)
-
Verification:
- Token 1 ("fox"): Is
P_target(fox) high enough compared to P_draft(fox)? Yes. Accept "fox".
- Token 2 ("jumps"): Is
P_target(jumps | fox) high enough compared to P_draft(jumps | fox)? Yes. Accept "jumps".
- Token 3 ("over"): Is
P_target(over | fox, jumps) high enough compared to P_draft(over | fox, jumps)? No, the target model thought "the" was more likely.
- Result: "fox" and "jumps" are accepted. The speculation stops here.
- The target model generates the next token, which it determined to be "the".
- The new prefix is "The quick brown fox jumps the".
- The process restarts with "The quick brown fox jumps the" as the new prefix.
Benefits:
- Speed-up: This is the primary benefit. By batching multiple tokens into a single (expensive) target model forward pass, you drastically reduce the total number of target model computations required. The larger the
N (number of speculative tokens) you can get away with, the greater the speed-up.
- No Loss of Accuracy: Crucially, speculative decoding ensures that the final output distribution is identical to what you would get from sampling directly from the large target model. The draft model only proposes candidates; the target model always verifies and ultimately decides what is accepted or what the next token should be. This is a powerful theoretical guarantee.
- Energy Efficiency: Fewer large model forward passes can lead to lower energy consumption.
Key Enablers:
- A readily available smaller, faster draft model. This could be a distilled version of the target model, a smaller LLM, or even just the first few layers of the target model.
- The parallel nature of transformer attention, which allows the target model to compute probabilities for multiple tokens in one pass.
Speculative decoding is a significant advancement in LLM inference, making it possible to achieve much faster generation speeds without compromising on the quality of the output.